-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhipsc.py
executable file
·215 lines (191 loc) · 5.9 KB
/
hipsc.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
#!/usr/bin/env python3
#
# Relative contributions of the major ionic currents in human atrial models.
#
import os
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import myokit
import myokit.lib.plots as mp
import shared
# Update matplotlib styles
matplotlib.rcParams['axes.spines.right'] = False
matplotlib.rcParams['axes.spines.top'] = False
matplotlib.rcParams['mathtext.default'] = 'regular'
# Current colors
cmap = matplotlib.colormaps['tab20']
current_colours = dict(shared.current_colours)
del(current_colours['I_Kb'])
del(current_colours['I_Kur'])
del(current_colours['I_ClCa'])
del(current_colours['I_Cl,B'])
del(current_colours['I_K,ACh'])
del(current_colours['I_K,ATP'])
# Human atrial models
model_names = {
'paci-2013': 'paci-2013-ventricular.mmt',
'paci-2018': 'paci-2018.mmt',
'paci-2020': 'paci-2020.mmt',
'kernik': 'kernik-2019.mmt',
}
def current_variables(model, colours=False):
""" Returns an ordered list of transmembrane current variable names. """
name = model.name().lower()
if 'paci-2013' in name:
currents = {
'I_NaCa': 'inaca.INaCa',
'I_to': 'ito.Ito',
'I_Kr': 'ikr.IKr',
'I_Ks': 'iks.IKs',
'I_f': 'if.If',
'I_K1': 'ik1.IK1',
'I_NaK': 'inak.INaK',
'I_CaL': 'ical.ICaL',
'I_Na,B': 'ibna.IbNa',
'I_Ca,B': 'ibca.IbCa',
'I_Ca,P': 'ipca.IpCa',
'I_Na': 'ina.INa',
}
elif 'paci-2018' in name or 'paci-2020' in name:
currents = {
'I_NaCa': 'inaca.INaCa',
'I_to': 'ito.Ito',
'I_Kr': 'ikr.IKr',
'I_Ks': 'iks.IKs',
'I_f': 'if.If',
'I_K1': 'ik1.IK1',
'I_NaK': 'inak.INaK',
'I_CaL': 'ical.ICaL',
'I_NaL': 'inal.INaL',
'I_Na,B': 'ibna.IbNa',
'I_Ca,B': 'ibca.IbCa',
'I_Ca,P': 'ipca.IpCa',
'I_Na': 'ina.INa',
}
elif 'kernik-' in name:
currents = {
'I_NaCa': 'inaca.i_NaCa',
'I_to': 'ito.i_to',
'I_Kr': 'ikr.i_Kr',
'I_Ks': 'iks.i_Ks',
'I_f': 'ifunny.i_f',
'I_K1': 'ik1.i_K1',
'I_NaK': 'inak.i_NaK',
'I_CaL': 'ical.i_CaL',
'I_CaT': 'icat.i_CaT',
'I_Na,B': 'ibna.i_b_Na',
'I_Ca,B': 'ibca.i_b_Ca',
'I_Ca,P': 'ipca.i_PCa',
'I_Na': 'ina.i_Na',
}
else:
currents = shared.guess_currents(model)
print('\n'.join(currents))
print(len(currents))
raise NotImplementedError('Unknown model: ' + model.name())
if colours:
colours = [cmap(current_colours[x]) for x in currents.keys()]
currents = list(currents.values())
return currents, colours
return list(currents.values())
# Create protocol
cl = 800
protocol = myokit.pacing.blocktrain(cl, duration=5, offset=50)
# Load and prepare models
models = {}
for name, fname in model_names.items():
pre_pace = True
if 'kernik' in name:
# 2024-09-03 Kernik model destabilises when pre-paced
pre_pace = False
model = myokit.load_model(os.path.join('models', 'c', fname))
shared.prepare_model(model, protocol, current_variables(model), pre_pace)
models[name] = model
print('Finished preparation.\nPreparing plots')
# Maximum time to show in plots
tmax = 800
def text(ax, x, y, t, c='w'):
ax.text(x, y, t, color=c, transform=ax.transAxes, fontweight='bold',
horizontalalignment='right', verticalalignment='center')
# Create figure
fig = plt.figure(figsize=(9, 9))
fig.subplots_adjust(0.075, 0.05, 0.98, 0.97, hspace=0.35, wspace=0.2)
grid = GridSpec(3, 3)
#
# Top row: Paci models
#
# Paci 2013
code = 'paci-2013'
model = models[code]
currents, colours = current_variables(model, True)
s = myokit.Simulation(model, protocol)
s.set_tolerance(1e-8, 1e-8)
d = s.run(tmax)
ax = fig.add_subplot(grid[0, 0])
ax.set_title(model.meta['display_name'])
ax.set_xlabel('Time (s)')
ax.set_ylabel('Relative contribution')
ax.set_xlim(0, tmax)
ax.set_ylim(-1.02, 1.02)
mp.cumulative_current(d, currents, ax, colors=colours, normalize=True)
# Paci 2018
code = 'paci-2018'
model = models[code]
currents, colours = current_variables(model, True)
s = myokit.Simulation(model, protocol)
s.set_tolerance(1e-8, 1e-8)
d = s.run(tmax)
ax = fig.add_subplot(grid[0, 1])
ax.set_title(model.meta['display_name'])
ax.set_xlabel('Time (s)')
ax.set_xlim(0, tmax)
ax.set_ylim(-1.02, 1.02)
mp.cumulative_current(d, currents, ax, colors=colours, normalize=True)
# Paci 2020
code = 'paci-2020'
model = models[code]
currents, colours = current_variables(model, True)
s = myokit.Simulation(model, protocol)
s.set_tolerance(1e-8, 1e-8)
d = s.run(tmax)
ax = fig.add_subplot(grid[0, 2])
ax.set_title(model.meta['display_name'])
ax.set_xlabel('Time (s)')
ax.set_xlim(0, tmax)
ax.set_ylim(-1.02, 1.02)
mp.cumulative_current(d, currents, ax, colors=colours, normalize=True)
#
# Middle row
#
# Kernik 2019
code = 'kernik'
model = models[code]
currents, colours = current_variables(model, True)
s = myokit.Simulation(model, protocol)
s.set_tolerance(1e-8, 1e-8)
d = s.run(tmax)
ax = fig.add_subplot(grid[1, 0])
ax.set_title(model.meta['display_name'])
ax.set_xlabel('Time (s)')
ax.set_ylabel('Relative contribution')
ax.set_xlim(0, tmax)
ax.set_ylim(-1.02, 1.02)
mp.cumulative_current(d, currents, ax, colors=colours, normalize=True)
#
# Legend
#
ax = fig.add_subplot(grid[1, 2])
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.set_frame_on(False)
lines = []
for current, i in current_colours.items():
lines.append(matplotlib.lines.Line2D([0], [0], color=cmap(i), lw=5))
labels = [shared.current_names[x] for x in current_colours]
#ax.legend(lines, labels, loc=(0.05, 0.05), ncol=2)
ax.legend(lines, labels, loc=(0.05, -0.7), ncol=1)
# Show / store
plt.savefig('hipsc.png')
plt.savefig('hipsc.pdf')
print('Done')