Created
February 6, 2017 12:27
-
-
Save zhengyangchoong/707b56b45667002177b5cc34676b577e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import os | |
import matplotlib | |
#matplotlib.use('Agg') | |
import matplotlib.pyplot as plt | |
import matplotlib.animation | |
import numpy as np | |
import subprocess | |
from mpl_toolkits.mplot3d import Axes3D | |
def plot2dgaussian(): | |
N = 100 | |
def gs(x): | |
a = 0.1 | |
return np.exp(-a * (x[0]**2 + x[1]**2)) | |
def scale(x,N): | |
return (x - N/2) / (0.1 * N) | |
a = np.zeros((N,N)) | |
b = np.zeros((N,N)) | |
for (x,y), value in np.ndenumerate(a): | |
G = np.array([scale(x,N),scale(y,N)]) | |
b[x,y] = gs(G) | |
plt.imshow(b, cmap = 'plasma') | |
plt.show() | |
def generategif(): | |
fn = "gauss" | |
if not os.path.exists(fn): | |
os.mkdir(fn) | |
for i in xrange(100): | |
a = np.random.normal(0,1,(5,5)) | |
fig = plt.figure(frameon=False) | |
fig.set_size_inches(1,1) | |
#plt.axis('off') | |
ax = plt.Axes(fig, [0., 0., 1., 1.]) | |
#ax.set _adjustable('box-forced') | |
#ax.axis('tight') | |
#ax.axis('off') | |
ax.set_axis_off() | |
fig.add_axes(ax) | |
plt.imshow(a, cmap = 'plasma', interpolation = "nearest") | |
fig = plt.gcf() | |
# fig.set_size_inches(2, 2) | |
#fig.savefig('test2png.png', dpi=300) | |
plt.savefig("{0}/{1:03d}.png".format(fn,i), dpi = 200) | |
plt.cla() | |
plt.clf() | |
plt.close() | |
subprocess.call("convert -delay 4 -loop 0 {0}/*.png {0}/animated.gif".format(fn).split(" ")) | |
def onedimensionalgaussian(): | |
fn = "sigmoid_onedimensionalgaussian" | |
if not os.path.exists(fn): | |
os.mkdir(fn) | |
N = 20 | |
sampling = 0.001 | |
x = np.arange(-N/2, N/2 + sampling, sampling) | |
def gs(x): | |
a = 0.1 | |
return np.exp(-a * x**2) | |
#for i in xrange(int(len(x)/2)): | |
#y = np.array([1 if i < len(x)/2 else 0 for i in xrange(len(x))]) | |
def sig(x): | |
a = 0.1 | |
return 1/(1 + np.exp(-x)) | |
y = sig(x) | |
#print y | |
#y[0], y[-1] = 0.5, 0.5 | |
for i in xrange(100): | |
first = np.gradient(y) | |
second = np.gradient(first) | |
#print second | |
for j,value in np.ndenumerate(second): | |
y[j] += 10 * second[j] | |
if i % 1 == 0: | |
fig = plt.figure(frameon=False) | |
fig.set_size_inches(1,1) | |
ax = plt.Axes(fig, [0., 0., 1., 1.]) | |
ax.set_axis_off() | |
fig.add_axes(ax) | |
plt.ylim([0,1]) | |
plt.xlim(-N/2, N/2 + sampling ) | |
plt.plot(x,y) | |
plt.savefig("{0}/{1:03d}.png".format(fn,int(i)), dpi = 200) | |
plt.cla() | |
plt.clf() | |
plt.close() | |
subprocess.call("convert -delay 4 -loop 0 {0}/*.png {0}/animated.gif".format(fn).split(" ")) | |
onedimensionalgaussian() | |
def initialiseGaussianSpace(): | |
#fig = plt.figure() | |
N = 100 | |
sampling = 0.1 | |
D = 0.2 | |
x = np.arange(-N/2,N/2+sampling,sampling) | |
y = np.arange(-N/2,N/2+sampling,sampling) | |
def gs(x,y): | |
a = 0.1 | |
return np.exp(-a * (x**2 + y**2)) | |
xs,ys = np.meshgrid(x,y) | |
zs = gs(xs,ys) | |
#print b | |
steps = 200 | |
for i in xrange(steps): | |
first = np.gradient(zs) | |
for (x,y), value in np.ndenumerate(first[0]): | |
zs[x,y] += (first[0][x,y] + first[1][x,y]) * D | |
fig = plt.figure() | |
ax = fig.gca(projection='3d') | |
ax = Axes3D(fig) | |
ax.plot_surface(xs, ys, zs, rstride=1, cstride=1, cmap='hot') | |
plt.show() | |
#plt.clf() | |
#ax = fig.gca(projection='3d') | |
#ax = Axes3D(fig) | |
#ax.plot_surface(xs,ys,second[1], cmap = 'plasma', rstride = 1, cstride = 1) | |
#plt.imshow(first[1], cmap = 'plasma', interpolation = "nearest") | |
#plt.show() | |
#plt.imshow(zs, cmap = 'plasma', interpolation = "nearest") | |
#plt.show() | |
#initialiseGaussianSpace() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment