-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvisualisation.py
51 lines (41 loc) · 1.45 KB
/
visualisation.py
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
"""
Visualising animations
"""
import numpy as np
def reshape_and_tile_images(array, shape=(28, 28), n_cols=None):
if n_cols is None:
n_cols = int(math.sqrt(array.shape[0]))
n_rows = int(np.ceil(float(array.shape[0])/n_cols))
if len(shape) == 2:
order = 'C'
else:
order = 'F'
def cell(i, j):
ind = i*n_cols+j
if i*n_cols+j < array.shape[0]:
return array[ind].reshape(*shape, order='C')
else:
return np.zeros(shape)
def row(i):
return np.concatenate([cell(i, j) for j in range(n_cols)], axis=1)
return np.concatenate([row(i) for i in range(n_rows)], axis=0)
def plot_gif(x_seq, shape, path, filename):
n_cols = int(np.sqrt(x_seq.shape[0]))
x_seq = x_seq[:n_cols**2]
T = x_seq.shape[1]
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure()
x0 = reshape_and_tile_images(x_seq[:, 0], shape, n_cols)
im = plt.imshow(x0, animated=True, cmap='gray')
plt.axis('off')
def update(t):
x_frame = reshape_and_tile_images(x_seq[:, t], shape, n_cols)
im.set_array(x_frame)
return im,
anim = FuncAnimation(fig, update, frames=np.arange(T), \
interval=1000, blit=True)
anim.save(path+filename+'.gif', writer='imagemagick')
print 'image saved as ' + path+filename+'.gif'