-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
251 lines (195 loc) · 7.65 KB
/
functions.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
import myokit
import matplotlib.pyplot as plt
import numpy as np
class VCProtocol():
def __init__(self, segments):
self.segments = segments #list of VCSegments
def get_protocol_length(self):
proto_length = 0
for s in self.segments:
proto_length += s.duration
return proto_length
def get_myokit_protocol(self, scale=1):
segment_dict = {'v0': f'{-80*scale}'}
piecewise_txt = f'piecewise((engine.time >= 0 and engine.time < {99.9*scale}), v0, '
current_time = 99.9*scale
#piecewise_txt = 'piecewise( '
#current_time = 0
#segment_dict = {}
for i, segment in enumerate(self.segments):
start = current_time
end = current_time + segment.duration
curr_step = f'v{i+1}'
time_window = f'(engine.time >= {start} and engine.time < {end})'
piecewise_txt += f'{time_window}, {curr_step}, '
if segment.end_voltage is None:
segment_dict[curr_step] = f'{segment.start_voltage}'
else:
slope = ((segment.end_voltage - segment.start_voltage) /
segment.duration)
intercept = segment.start_voltage - slope * start
segment_dict[curr_step] = f'{slope} * engine.time + {intercept}'
current_time = end
piecewise_txt += 'vp)'
return piecewise_txt, segment_dict, current_time
def plot_protocol(self, is_shown=False):
fig, ax = plt.subplots(1, 1, figsize=(12, 8))
pts_v = []
pts_t = []
current_t = 0
for seg in self.segments:
pts_v.append(seg.start_voltage)
if seg.end_voltage is None:
pts_v.append(seg.start_voltage)
else:
pts_v.append(seg.end_voltage)
pts_t.append(current_t)
pts_t.append(current_t + seg.duration)
current_t += seg.duration
plt.plot(pts_t, pts_v)
if is_shown:
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.set_xlabel('Time (ms)', fontsize=16)
ax.set_xlabel('Voltage (mV)', fontsize=16)
plt.show()
def plot_with_curr(self, curr, cm=60):
mod = myokit.load_model('mmt-files/kernik_2019_NaL_art.mmt')
p = mod.get('engine.pace')
p.set_binding(None)
c_m = mod.get('artifact.c_m')
c_m.set_rhs(cm)
v_cmd = mod.get('artifact.v_cmd')
v_cmd.set_rhs(0)
v_cmd.set_binding('pace') # Bind to the pacing mechanism
# Run for 20 s before running the VC protocol
holding_proto = myokit.Protocol()
holding_proto.add_step(-81, 30000)
t = holding_proto.characteristic_time()
sim = myokit.Simulation(mod, holding_proto)
dat = sim.run(t)
mod.set_state(sim.state())
# Get protocol to run
piecewise_function, segment_dict, t_max = self.get_myokit_protocol()
mem = mod.get('artifact')
for v_name, st in segment_dict.items():
v_new = mem.add_variable(v_name)
v_new.set_rhs(st)
vp = mem.add_variable('vp')
vp.set_rhs(0)
v_cmd = mod.get('artifact.v_cmd')
v_cmd.set_binding(None)
vp.set_binding('pace')
v_cmd.set_rhs(piecewise_function)
times = np.arange(0, t_max, 0.1)
## CHANGE THIS FROM holding_proto TO SOMETHING ELSE
sim = myokit.Simulation(mod, holding_proto)
dat = sim.run(t_max, log_times=times)
fig, axs = plt.subplots(3, 1, sharex=True, figsize=(12, 8))
axs[0].plot(times, dat['membrane.V'])
axs[0].plot(times, dat['artifact.v_cmd'], 'k--')
axs[1].plot(times, np.array(dat['artifact.i_out']) / cm)
axs[2].plot(times, dat[curr])
axs[0].set_ylabel('Voltage (mV)')
axs[1].set_ylabel('I_out (A/F)')
axs[2].set_ylabel(curr)
for ax in axs:
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()
class VCSegment():
def __init__(self, duration, start_voltage, end_voltage=None):
self.duration = duration
self.start_voltage = start_voltage
self.end_voltage = end_voltage
def return_vc_proto(scale=1, prestep_size=0):
segments = [
VCSegment(756.9, 6),
VCSegment(7.3, -41),
VCSegment(100.6, 8.5),
VCSegment(500, -80),
VCSegment(106.2, -81),
VCSegment(103.7, -2, -34),
VCSegment(500, -80),
VCSegment(183, -87),
VCSegment(101.9, -52, 14),
VCSegment(500, -80),
VCSegment(272.1, 54, -107),
VCSegment(102.8, 60),
VCSegment(500, -80),
VCSegment(52.2, -76, -80),
VCSegment(102.7, -120),
VCSegment(500, -80),
VCSegment(936.9, -120),
VCSegment(94.9, -77),
VCSegment(8, -118),
VCSegment(500, -80),
VCSegment(729.4, 55),
VCSegment(996.6, 48),
VCSegment(894.9, 59, 28),
VCSegment(900, -80)
]
if prestep_size != 0:
segments = [VCSegment(prestep_size, -80)] + segments
new_segments = []
for seg in segments:
if seg.end_voltage is None:
new_segments.append(VCSegment(seg.duration*scale, seg.start_voltage*scale))
else:
new_segments.append(VCSegment(seg.duration*scale,
seg.start_voltage*scale,
seg.end_voltage*scale))
return VCProtocol(new_segments)
def change_vcp_start(t_vcp, i_out, v_vcp, prestep):
start = np.where(t_vcp-prestep == np.min(np.abs((t_vcp-prestep))))[0][0]
t_vcp = t_vcp[start:]-t_vcp[start]
i_out = i_out[start:]
v_vcp = v_vcp[start:]
return(t_vcp, i_out, v_vcp)
def get_vc_artifact_response(mod_name, all_params, with_all_dat=False):
if mod_name == 'Kernik':
model_path = './models/kernik_artifact_fixed.mmt'
mod = myokit.load_model(model_path)
if mod_name == 'Paci':
model_path = './models/paci_artifact_ms_fixed.mmt'
mod = myokit.load_model(model_path)
for k, value in all_params.items():
group, name = k.split('.')
mod[group][name].set_rhs(value)
p = mod.get('engine.pace')
p.set_binding(None)
prestep = 50000
vc_proto = return_vc_proto(prestep_size=prestep)
proto = myokit.Protocol()
proto.add_step(-80, prestep+10000)
piecewise, segment_dict, t_max = vc_proto.get_myokit_protocol()
####
p = mod.get('engine.pace')
p.set_binding(None)
new_seg_dict = {}
for k, vol in segment_dict.items():
new_seg_dict[k] = vol
segment_dict = new_seg_dict
mem = mod.get('voltageclamp')
for v_name, st in segment_dict.items():
v_new = mem.add_variable(v_name)
v_new.set_rhs(st)
vp = mem.add_variable('vp')
vp.set_rhs(0)
v_cmd = mod.get('voltageclamp.Vc')
v_cmd.set_binding(None)
vp.set_binding('pace')
v_cmd.set_rhs(piecewise)
sim = myokit.Simulation(mod, proto)
times = np.arange(0, t_max, 0.1)
sim.set_max_step_size(1)
dat = sim.run(t_max, log_times=times)
cm = mod['voltageclamp']['cm_est'].value()
t = dat.time()
i_out = [v / cm for v in dat['voltageclamp.Iout']]
v = dat['voltageclamp.Vc']
times, i_out, v = change_vcp_start(times, i_out, v, prestep) #added by kristin to remove prestep
if with_all_dat:
return times, i_out, v, dat
else:
return times, i_out, v