public
Created

Code for percolation network simulation used in my talk at #BOSC July 19 2013 in Berlin. Based on code from http://dragly.org/2013/03/25/working-with-percolation-clusters-in-python/. The license for the source code is not entirely clear so this code is made available under a CC BY-SA license as per the blog it was derived from. For reasons not entirely clear to me for this to work on my machine it has to be called with: ipython --pylab=tk percolation.py

  • Download Gist
percolation.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
from pylab import *
from scipy.ndimage import measurements
import numpy as np
import matplotlib.pyplot as plt
import time
import sys
import signal
 
def signal_handler(signal, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# Set up the figures
plt.figure(0, figsize=(9,9))
plt.title("Clusters by area")
 
plt.figure(1, figsize=(7,4))
plt.title("Number of clusters")
 
plt.figure(2, figsize = (7,4))
plt.title("Largest Cluster")
plt.ion()
 
time.sleep(20)
for L in [50, 100, 200, 500, 1000]:
 
for marker in ['ko', 'bo', 'ro']:
# Randomise the percolation network
r = rand(L,L)
for p in np.arange(0., 1., 0.05):
# Determine the connectivity for probability p
z = r<p
 
# Define the clusters using the ndimage.measurements routines
lw, num = measurements.label(z)
 
# Plot the clusters for given p
plt.figure(0)
area = measurements.sum(z, lw, index=arange(lw.max() + 1))
areaImg = area[lw]
plt.imshow(areaImg, origin='lower', interpolation='nearest', vmin=1, vmax=L*5)
plt.draw()
 
# Plot the number of clusters
plt.figure(1)
plt.plot(p, lw.max(), marker)
plt.draw()
 
# Plot the size of the largest cluster
plt.figure(2)
plt.plot(p, area.max(), marker)
plt.draw()
plt.ioff()
plt.show()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.