Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Code for percolation network simulation used in my talk at #BOSC July 19 2013 in Berlin. Based on code from 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 e…
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):
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")
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
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)
# Plot the number of clusters
plt.plot(p, lw.max(), marker)
# Plot the size of the largest cluster
plt.plot(p, area.max(), marker)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.