%load_ext watermark
%watermark -a "Yoav Ram" -t -z -u -n -v -m -h -g -p numpy,scipy,matplotlib
The watermark extension is already loaded. To reload it, use: %reload_ext watermark Yoav Ram Last updated: Fri Sep 40 2014 14:40:23 Jerusalem Daylight Time CPython 2.7.8 IPython 2.0.0-dev numpy 1.7.1 scipy 0.12.0 matplotlib 1.2.1 compiler : MSC v.1500 64 bit (AMD64) system : Windows release : 7 machine : AMD64 processor : Intel64 Family 6 Model 42 Stepping 7, GenuineIntel CPU cores : 4 interpreter: 64bit host name : BlackMamba Git hash : 8607eb61eaa4bd1fc7b37a7c4d80f78ed8ae4d6b
def neighborhood(X, x, y, N):
if N % 2 == 0:
raise NotImplementedError("N must be odd, but it is",N)
xlim = max(x-N/2, 0), min(x+N/2+1, X.shape[0])
ylim = max(y-N/2, 0), min(y+N/2+1, X.shape[0])
return X[xlim[0]:xlim[1], ylim[0]:ylim[1]]
N = 5
X = random_integers(0,10,(30,30))
x,y = 10,10
nei = neighborhood(X, x,y, N)
print nei
print nei.flatten()[(N**2)/2], X[x,y]
[[ 5 3 1 8 5] [ 1 0 7 2 2] [ 2 1 4 0 1] [10 2 9 10 10] [ 0 5 6 2 10]] 4 4
def display(X):
fig, (ax2, ax1) = subplots(1, 2, figsize=(7,3))
im = ax1.imshow(X, cmap="Spectral", vmin=0, vmax=7)
ax1.set_xticks([])
ax1.set_yticks([])
cb = plt.colorbar(im)
Xflat = X.flatten()
ticks = sorted(unique(Xflat))
cb.set_ticks(ticks)
ax2.hist(Xflat)
ax2.set_xticks(ticks)
fig.tight_layout()
# return fig,ax1,ax2
X = np.zeros((30, 30), dtype=uint8)
s = 0.01
N = 3
mu = 0.3
display(X);
for t in range(1000):
# drift
x,y = random_integers(0, X.shape[0] - 1, 2)
# selection
nei = neighborhood(X, x, y, N)
W = ((1-s)**nei).flatten()
W /= W.sum()
X[x,y] = nei.flatten()[argmax(multinomial(1, W))]
# mutation
if rand() < mu:
X[x,y] += 1
display(X)
print ((1-s)**nei).flatten().mean(), exp(-mu)
0.97253167 0.740818220682
X = [np.zeros((30, 30), dtype=uint8)]
s = 0.01
H = 2
N = 3
mu = 0.3
m = 5
for t in range(1000):
X.append(X[-1].copy())
# drift
x,y = random_integers(0, X[-1].shape[0] - 1, 2)
# selection
nei = neighborhood(X[-1], x, y, N)
W = ((1-s)**nei)
W[nei==m] = 1 + s*H
W = W.flatten()
W /= W.sum()
X[-1][x,y] = nei.flatten()[argmax(multinomial(1, W))]
# mutation
if rand() < mu:
X[-1][x,y] += 1
display(X[-1])
print ((1-s)**nei).flatten().mean(), exp(-mu)
0.945790989421 0.740818220682
from matplotlib import animation
#http://jakevdp.github.io/blog/2013/05/19/a-javascript-viewer-for-matplotlib-animations/
from JSAnimation import IPython_display
fig, ax = subplots(1, 1, figsize=(4,3))
ax.set_xticks([])
ax.set_yticks([])
im = ax.imshow(X[0], cmap="Spectral", vmin=0, vmax=7)
cb = plt.colorbar(im)
cb.set_ticks(range(0,8))
def init():
im.set_data(X[0])
return im,
def animate(i):
im.set_data(X[i*len(X)/100])
return im,
animation.FuncAnimation(fig, animate, frames=100, interval=30)