-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanimate.py
executable file
·278 lines (240 loc) · 7.89 KB
/
animate.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
#
# Animated versions of the protocol plots.
#
from __future__ import division
from __future__ import print_function
import os
import sys
import numpy as np
import myokit
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from matplotlib.animation import FuncAnimation
# Load project modules
sys.path.append(os.path.abspath(os.path.join('..', 'python')))
import plots
import sumstat
#
# Check input arguments
#
args = sys.argv[1:]
if len(args) != 2:
print('Syntax: animate.py <protocol> <live/save>')
sys.exit(1)
protocol = int(args[0])
save = args[1].lower().strip() == 'save'
print('Selected protocol ' + str(protocol))
if save:
print('Storing output as movie')
else:
print('Showing output live on screen')
# Set font
matplotlib.rc('font', family='arial')
# Create empty figure
fig = plt.figure(figsize=(10, 4.8))
fig.subplots_adjust(0.065, 0.08, 0.98, 0.98)
grid = GridSpec(3, 2, hspace=0, wspace=0.2)
vx = fig.add_subplot(grid[0, 0])
cx = fig.add_subplot(grid[1:, 0])
px = fig.add_subplot(grid[:, 1])
# Set labels
cx.set_xlabel('Time (ms)')
cx.set_ylabel('I (nA)', labelpad=9)
vx.set_ylabel('V (mV)')
px.set_xlabel('Deactivated' + ' '*72 + 'Activated')
px.set_ylabel('Inactivated' + ' '*72 + 'Recovered')
# Update ticks
cx.xaxis.set_tick_params()
cx.yaxis.set_tick_params()
vx.set_xticklabels([])
vx.yaxis.set_tick_params()
px.xaxis.set_tick_params()
px.yaxis.set_tick_params()
# Run simulation
m, t, v, c, a, r = plots._phase_simulation(protocol, all_signals=True)
# Draw attractor
plots._phase_attractor(px, m, '--', '#999999')
ainf = m.get('ikr.act.inf').pyfunc()
rinf = m.get('ikr.rec.inf').pyfunc()
# Update axes
vx.set_ylim(-130, 70)
cx.set_ylim(1.1 * np.min(c), 1.1 * np.max(c))
px.set_xlim(-0.01, 1.01)
px.set_ylim(-0.01, 1.01)
# Choose colormap
colormap = 'jet'
# Set speed
fps = 20
if protocol < 6:
#
# Periodic protocols
#
# Split signal
split = sumstat.split_points_nocap(protocol)
norm = matplotlib.colors.Normalize(0, len(split) - 1)
cmap = matplotlib.cm.get_cmap(colormap)
# Update x-axes of current/voltage pltos
vx.set_xlim(0, t[split[0][-1]])
cx.set_xlim(0, t[split[0][-1]])
# Work out timing
stride = 100
points_inner = split[0][1]
steps_outer = len(split)
steps_inner = (points_inner + stride - 1) // stride
steps_total = steps_outer * steps_inner
time_text = 'Current time: '
# Draw 'ghost' of protocol
for i in range(steps_outer):
lo = i * points_inner
hi = lo + points_inner
vx.plot(t[0:points_inner], v[lo:hi], color='k', alpha=0.1)
# If saving, show speed
sim_seconds_per_frame = stride * 0.1 * 1e-3
speed = fps * sim_seconds_per_frame
if save:
time_text = (
'Displaying at ' + str(np.round(speed, 1)) + 'x real time. '
+ time_text)
# Add objects to be updated during animation
objects = [
vx.plot([], [], color=cmap(norm(i)), animated=True)[0]
for i in range(steps_outer)]
objects.extend([
cx.plot([], [], color=cmap(norm(i)), animated=True)[0]
for i in range(steps_outer)])
objects.extend([
px.plot([], [], color=cmap(norm(i)), animated=True)[0]
for i in range(steps_outer)])
objects.extend([
vx.plot([], [], 'o', color='k', animated=True, fillstyle='none')[0],
cx.plot([], [], 'o', color='k', animated=True, fillstyle='none')[0],
px.plot([], [], 'o', color='k', animated=True, fillstyle='none')[0],
px.plot([], [], 'x', color='k', animated=True)[0],
cx.text(0.01, 0.016, time_text + '0 s', transform=cx.transAxes),
])
# Provide some feedback while creating the animation
dots = 20
print('Animation has ' + str(steps_total) + ' steps.')
print(
'(dot per ' + str(dots) + ' steps = '
+ str(steps_total // dots) + ' dots)')
sys.stdout.write('Animating')
# Update the graph
def update(k):
if k % dots == 0:
sys.stdout.write('.')
sys.stdout.flush()
i = k // steps_inner
j = k % steps_inner + 1
thi = min(j * stride, points_inner)
lo = points_inner * i
hi = lo + thi
objects[i + 0 * steps_outer].set_data(t[:thi], v[lo:hi])
objects[i + 1 * steps_outer].set_data(t[:thi], c[lo:hi])
objects[i + 2 * steps_outer].set_data(a[lo:hi], r[lo:hi])
vv = v[hi - 1]
objects[-5].set_data(t[thi - 1], vv)
objects[-4].set_data(t[thi - 1], c[hi - 1])
objects[-3].set_data(a[hi - 1], r[hi - 1])
objects[-2].set_data(ainf(vv), rinf(vv))
objects[-1].set_text(
time_text + str(np.round(t[thi - 1] * 1e-3, 1)) + ' s')
return objects
else:
#
# Non-periodic protocols
#
# Update x-axes of current/voltage pltos
vx.set_xlim(0, t[-1])
cx.set_xlim(0, t[-1])
# Work out timing
stride = 100
n = 1 + (len(t) - 1) // stride
steps_total = n
time_text = 'Current time: '
# If saving, show speed
sim_seconds_per_frame = stride * 0.1 * 1e-3
speed = fps * sim_seconds_per_frame
if save:
time_text = (
'Displaying at ' + str(np.round(speed, 1)) + 'x real time. '
+ time_text)
# Draw 'ghost' of protocol
vx.plot(t, v, color='k', alpha=0.1)
# Create colormap
norm = matplotlib.colors.Normalize(0, 1)
cmap = matplotlib.cm.get_cmap(colormap)
# Create line segments, collections, and objects
v_segments = []
c_segments = []
p_segments = []
v_lines = matplotlib.collections.LineCollection(
[], cmap=cmap, norm=norm, array=np.linspace(0, 1, n))
c_lines = matplotlib.collections.LineCollection(
[], cmap=cmap, norm=norm, array=np.linspace(0, 1, n))
p_lines = matplotlib.collections.LineCollection(
[], cmap=cmap, norm=norm, array=np.linspace(0, 1, n))
vx.add_collection(v_lines)
cx.add_collection(c_lines)
px.add_collection(p_lines)
objects = [
v_lines,
c_lines,
p_lines,
vx.plot([], [], 'o', color='k', animated=True, fillstyle='none')[0],
cx.plot([], [], 'o', color='k', animated=True, fillstyle='none')[0],
px.plot([], [], 'o', color='k', animated=True, fillstyle='none')[0],
px.plot([], [], 'x', color='k', animated=True)[0],
cx.text(0.01, 0.016, time_text + '0 s', transform=cx.transAxes),
]
# Provide some feedback while creating the animation
dots = 20
print('Animation has ' + str(steps_total) + ' steps.')
print(
'(dot per ' + str(dots) + ' steps = '
+ str(steps_total // dots) + ' dots)')
sys.stdout.write('Animating')
# Update the graph
def update(i):
if i % dots == 0:
sys.stdout.write('.')
sys.stdout.flush()
lo = i * stride
hi = min(lo + stride, len(t) - 1)
v_segments.append(np.vstack((t[lo:hi + 1], v[lo:hi + 1])).T)
c_segments.append(np.vstack((t[lo:hi + 1], c[lo:hi + 1])).T)
p_segments.append(np.vstack((a[lo:hi + 1], r[lo:hi + 1])).T)
objects[0].set_segments(v_segments)
objects[1].set_segments(c_segments)
objects[2].set_segments(p_segments)
objects[3].set_data(t[hi], v[hi])
objects[4].set_data(t[hi], c[hi])
objects[5].set_data(a[hi], r[hi])
objects[6].set_data(ainf(v[hi]), rinf(v[hi]))
objects[7].set_text(
time_text + str(np.round(t[hi] * 1e-3, 1)) + ' s')
return objects
# Create animation
ani = FuncAnimation(
fig,
update,
steps_total,
blit=True,
# Interval is only used for live animation -- and is not accurate
interval=1/fps,
repeat=False,
)
# Display or store
if not save:
# Note: slows down during run, timing is way off
plt.show()
else:
ani.save(
'pr' + str(protocol) + '.mp4',
writer='ffmpeg',
#codec='ffv1',
bitrate=3000,
fps=fps)
print('\nDone')