-
Notifications
You must be signed in to change notification settings - Fork 2
/
train-r1-bad.py
802 lines (701 loc) · 29 KB
/
train-r1-bad.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
import sys
import os
import argparse
import time
import numpy as np
from scipy.interpolate import interp1d
from scipy.interpolate import UnivariateSpline
import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt
from smoothing import smooth
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR
parser = argparse.ArgumentParser('IKr real data fit with NN-f using insufficient protocols.')
parser.add_argument('--method', type=str, choices=['dopri5', 'adams'], default='dopri5')
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--debug', action='store_true')
parser.add_argument('--pred', action='store_true')
parser.add_argument('--cached', action='store_true')
parser.add_argument('--smoothi', action='store_true')
args = parser.parse_args()
from torchdiffeq import odeint
device = torch.device('cuda:' + str(args.gpu) if torch.cuda.is_available() else 'cpu')
# Set random seed
np.random.seed(0)
torch.manual_seed(0)
noise_sigma = 0.1
true_y0s = [torch.tensor([[1., 0.]]).to(device), # what you get after holding at +40mV
torch.tensor([[0., 1.]]).to(device)] # (roughly) what you get after holding at -80mV
# B1.2 in https://physoc.onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1113%2FJP275733&file=tjp12905-sup-0001-textS1.pdf#page=4
e = torch.tensor([-88.4]).to(device) # assume we know
# Table F11, Cell #5 GKr, in https://physoc.onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1113%2FJP275733&file=tjp12905-sup-0001-textS1.pdf#page=20
g = torch.tensor([0.1524]).to(device) # assume we know
g *= 1.2 # just because we see a-gate gets to ~1.2 at some point (in prt V=50), so can absorb that into the g.
#
#
#
def makedirs(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname)
makedirs('r1-bad')
if not args.cached or args.pred:
#
# Load data
#
raw_data1 = np.loadtxt('data/pr3-steady-activation-cell-5.csv', delimiter=',', skiprows=1)
raw_data2 = np.loadtxt('data/pr4-inactivation-cell-5.csv', delimiter=',', skiprows=1)
time1 = raw_data1[:, 0]
time1_torch = torch.from_numpy(raw_data1[:, 0]).to(device)
current1 = raw_data1[:, 1]
voltage1 = raw_data1[:, 2]
time2 = raw_data2[:, 0]
time2_torch = torch.from_numpy(raw_data2[:, 0]).to(device)
current2 = raw_data2[:, 1]
voltage2 = raw_data2[:, 2]
#
# Make filters
#
n_ms = 3
dt = 0.1 # ms
n_points = int(n_ms / dt)
change_pt1 = np.append([True], ~(voltage1[1:] != voltage1[:-1]))
cap_mask1 = np.copy(change_pt1)
for i in range(n_points):
cap_mask1 = cap_mask1 & np.roll(change_pt1, i + 1)
change_pt2 = np.append([True], ~(voltage2[1:] != voltage2[:-1]))
cap_mask2 = np.copy(change_pt2)
for i in range(n_points):
cap_mask2 = cap_mask2 & np.roll(change_pt2, i + 1)
# A bigger/final filter mask
extra_points = 20 # for numerical derivative or smoothing issue
mask1 = np.copy(cap_mask1)
for i in range(extra_points):
mask1 = mask1 & np.roll(change_pt1, i + n_points + 1)
mask1 = mask1 & np.roll(change_pt1, -i - 1)
mask2 = np.copy(cap_mask2)
for i in range(extra_points):
mask2 = mask2 & np.roll(change_pt2, i + n_points + 1)
mask2 = mask2 & np.roll(change_pt2, -i - 1)
prediction1 = np.loadtxt('data/ap-cell-5.csv', delimiter=',', skiprows=1)
timep1 = prediction1[:, 0]
timep1_torch = torch.from_numpy(prediction1[:, 0]).to(device)
currentp1 = prediction1[:, 1]
voltagep1 = prediction1[:, 2]
#
#
#
class ODEFunc(nn.Module):
def __init__(self):
super(ODEFunc, self).__init__()
self.net = nn.Sequential(
nn.Linear(2, 200),
nn.LeakyReLU(),
nn.Linear(200, 200),
nn.LeakyReLU(),
nn.Linear(200, 200),
nn.LeakyReLU(),
nn.Linear(200, 200),
nn.LeakyReLU(),
nn.Linear(200, 200),
nn.LeakyReLU(),
nn.Linear(200, 200),
nn.LeakyReLU(),
nn.Linear(200, 1),
)
for m in self.net.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=0.1)
nn.init.constant_(m.bias, val=0)
self.vrange = torch.tensor([100.]).to(device)
self.netscale = torch.tensor([1000.]).to(device)
# https://physoc.onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1113%2FJP275733&file=tjp12905-sup-0001-textS1.pdf#page=20
# Table F11, Cell #5 parameters
self.p5 = 8.73e-2
self.p6 = 8.90e-3
self.p7 = 5.20e-3
self.p8 = 3.16e-2
self.unity = torch.tensor([1]).to(device)
def set_fixed_form_voltage_protocol(self, t, v):
# Regular time point voltage protocol time series
self._t_regular = t
self._v_regular = v
self.__v = interp1d(t, v)
def _v(self, t):
return torch.from_numpy(self.__v([t.cpu().detach().numpy()]))
def voltage(self, t):
# Return voltage
return self._v(t).numpy()
def forward(self, t, y):
a, r = torch.unbind(y, dim=1)
try:
v = self._v(t).to(device)
except ValueError:
v = torch.tensor([-80]).to(device)
nv = v / self.vrange
k3 = self.p5 * torch.exp(self.p6 * v)
k4 = self.p7 * torch.exp(-self.p8 * v)
drdt = -k3 * r + k4 * (self.unity - r)
dadt = self.net(torch.stack([nv[0], a[0]]).float()) / self.netscale
return torch.stack([dadt[0], drdt[0]]).reshape(1, -1)
#
#
#
#
#
#
if args.pred:
func = ODEFunc().to(device)
func.load_state_dict(torch.load('r1-bad/model-state-dict.pt'))
func.eval()
ii = 999
# Load more prediction data
prediction2 = np.loadtxt('data/cell-5.csv', delimiter=',', skiprows=1)
timep2 = prediction2[:, 0]
timep2_torch = torch.from_numpy(prediction2[:, 0]).to(device)
currentp2 = prediction2[:, 1]
voltagep2 = prediction2[:, 2]
# Look at the fitted surface
v_batches = torch.load('r1-bad/v.pt')
a_batches = torch.load('r1-bad/a.pt')
dadt_batches = torch.load('r1-bad/dadt.pt')
x1 = torch.reshape(torch.linspace(-130, 70, 50).to(device), (-1, 1))
xx1 = x1 / func.vrange
x2 = torch.reshape(torch.linspace(0, 1, 50).to(device), (-1, 1))
X1, X2 = torch.meshgrid(x1.reshape(-1), x2.reshape(-1))
XX1, X2 = torch.meshgrid(xx1.reshape(-1), x2.reshape(-1))
XX = torch.stack([XX1.reshape(-1), X2.reshape(-1)]).T
ax = plt.axes(projection='3d')
ax.scatter(v_batches.reshape(-1).detach().cpu().numpy(), a_batches.reshape(-1).detach().cpu().numpy(),
dadt_batches.reshape(-1).detach().cpu().numpy())
pred = func.net(XX) / func.netscale
ax.plot_surface(X1.cpu().numpy(), X2.cpu().numpy(), pred.reshape(50, 50).detach().cpu().numpy(), color='C1')
ax.set_ylim([0, 1])
ax.set_xlabel('V')
ax.set_ylabel('a')
ax.set_zlabel('da/dt')
plt.savefig('r1-bad/rates3d-{:03d}'.format(ii), dpi=200)
#plt.show()
plt.close()
true_y0 = true_y0s[1] # (roughly holding at -80mV)
with torch.no_grad():
###
### Predict unseen protocols
###
#
# APs
#
# Trained Neural ODE
func.set_fixed_form_voltage_protocol(timep1, voltagep1)
pred_y = odeint(func, true_y0, timep1_torch).to(device)
pred_yo = g * pred_y[:, 0, 0] * pred_y[:, 0, 1] * (func._v(timep1_torch).to(device) - e)
loss = torch.mean(torch.abs(pred_yo - torch.from_numpy(currentp1).to(device)))
print('APs prediction | Total Loss {:.6f}'.format(loss.item()))
fig1, ax1 = plt.subplots(1, 1, figsize=(6, 4))
ax1.set_xlabel('Time (ms)')
ax1.set_ylabel('Current (nA)')
ax1.plot(timep1, currentp1, c='#7f7f7f', label='Data')
ax1.plot(timep1, pred_yo.reshape(-1).cpu().numpy(), '--', label='Prediction')
ax1.set_xlim(timep1.min(), timep1.max())
ax1.legend()
fig1.tight_layout()
fig1.savefig('r1-bad/{:03d}-aps'.format(ii), dpi=200)
#plt.show()
plt.close(fig1)
#
# Sinewave
#
# Trained Neural ODE
func.set_fixed_form_voltage_protocol(timep2, voltagep2)
pred_y = odeint(func, true_y0, timep2_torch).to(device)
pred_yo = g * pred_y[:, 0, 0] * pred_y[:, 0, 1] * (func._v(timep2_torch).to(device) - e)
loss = torch.mean(torch.abs(pred_yo - torch.from_numpy(currentp2).to(device)))
print('Sinewave prediction | Total Loss {:.6f}'.format(loss.item()))
fig1, ax1 = plt.subplots(1, 1, figsize=(6, 4))
ax1.set_xlabel('Time (ms)')
ax1.set_ylabel('Current (nA)')
ax1.plot(timep2, currentp2, c='#7f7f7f', label='Data')
ax1.plot(timep2, pred_yo.reshape(-1).cpu().numpy(), '--', label='Prediction')
ax1.set_xlim(timep2.min(), timep2.max())
ax1.legend()
fig1.tight_layout()
fig1.savefig('r1-bad/{:03d}-sinewave'.format(ii), dpi=200)
#plt.show()
plt.close(fig1)
#
# Pr3
#
# Trained Neural ODE
func.set_fixed_form_voltage_protocol(time1, voltage1)
pred_y = odeint(func, true_y0, time1_torch).to(device)
pred_yo = g * pred_y[:, 0, 0] * pred_y[:, 0, 1] * (func._v(time1_torch).to(device) - e)
loss = torch.mean(torch.abs(pred_yo - torch.from_numpy(current1).to(device)))
print('Pr3 prediction | Total Loss {:.6f}'.format(loss.item()))
l = int(len(time1) / 7) # 7 steps
fig1, ax1 = plt.subplots(1, 1, figsize=(6, 4))
ax1.set_xlabel('Time (ms)')
ax1.set_ylabel('Current (nA)')
for i in range(7):
ax1.plot(time1[:l], current1[l*i:l*(i+1)], c='#7f7f7f', label='__nolegend__' if i else 'Data')
ax1.plot(time1[:l], pred_yo.reshape(-1).cpu().numpy()[l*i:l*(i+1)], '--', label='__nolegend__' if i else 'Prediction')
ax1.set_xlim(time1[:l].min(), time1[:l].max())
ax1.set_ylim(-4, 1.9)
ax1.legend()
fig1.tight_layout()
fig1.savefig('r1-bad/{:03d}-pr3'.format(ii), dpi=200)
# do another one with zooms
ax1.set_xlim(5000, 7000)
ax1.set_ylim(-2, 1.7)
fig1.tight_layout()
fig1.savefig('r1-bad/{:03d}-pr3-z'.format(ii), dpi=200)
#plt.show()
plt.close(fig1)
#
# Pr4
#
# Trained Neural ODE
func.set_fixed_form_voltage_protocol(time2, voltage2)
pred_y = odeint(func, true_y0, time2_torch).to(device)
pred_yo = g * pred_y[:, 0, 0] * pred_y[:, 0, 1] * (func._v(time2_torch).to(device) - e)
loss = torch.mean(torch.abs(pred_yo - torch.from_numpy(current2).to(device)))
print('Pr4 prediction | Total Loss {:.6f}'.format(loss.item()))
l = int(len(time2) / 16) # 16 steps
fig1, ax1 = plt.subplots(1, 1, figsize=(6, 4))
ax1.set_xlabel('Time (ms)')
ax1.set_ylabel('Current (nA)')
for i in range(16):
ax1.plot(time2[:l], current2[l*i:l*(i+1)], c='#7f7f7f', label='__nolegend__' if i else 'Data')
ax1.plot(time2[:l], pred_yo.reshape(-1).cpu().numpy()[l*i:l*(i+1)], '--', label='__nolegend__' if i else 'Prediction')
ax1.set_xlim(time2[:l].min(), time2[:l].max())
ax1.set_ylim(-3, 7.5)
ax1.legend()
fig1.tight_layout()
fig1.savefig('r1-bad/{:03d}-pr4'.format(ii), dpi=200)
# do another one with zooms
ax1.set_xlim(1175, 1475)
ax1.set_ylim(-2.5, 7)
fig1.tight_layout()
fig1.savefig('r1-bad/{:03d}-pr4-z'.format(ii), dpi=200)
#plt.show()
plt.close(fig1)
sys.exit()
#
#
#
if args.cached:
v_batches = torch.load('r1-bad/v.pt')
a_batches = torch.load('r1-bad/a.pt')
dadt_batches = torch.load('r1-bad/dadt.pt')
if args.smoothi:
d2adt2_batches = torch.load('r1-bad/d2adt2.pt')
else:
###
### 'post-processing': estimating dadt and a
###
skip = 10 # not accurate for the first few time points for estimating derivatives
sparse = 7 # use less data points
r_batches1 = [] # assume we know to the extent of which we can ignore its discrepancy
r_batches2 = []
v_batches1 = []
v_batches2 = []
with torch.no_grad():
m = ODEFunc().to(device)
true_y0 = true_y0s[1]
m.set_fixed_form_voltage_protocol(time1, voltage1)
true_y = odeint(m, true_y0, time1_torch, method='dopri5')
r_batches1.append(true_y[:, 0, 1].to(device))
v_batches1.append(m._v(time1_torch)[0].to(device))
true_y0 = true_y0s[1]
m.set_fixed_form_voltage_protocol(time2, voltage2)
true_y = odeint(m, true_y0, time2_torch, method='dopri5')
r_batches2.append(true_y[:, 0, 1].to(device))
v_batches2.append(m._v(time2_torch)[0].to(device))
i_batches1 = [torch.from_numpy(current1).to(device)]
i_batches2 = [torch.from_numpy(current2).to(device)]
if args.smoothi:
# Calculate a and dadt
import pints
from scipy import optimize
x02 = [0.7, 1./50., 0.2, 1./100., 0.1, 1./200., 0.01]
x0 = [1, 1./100., 0.5, 1./200., 0.25, 1./400., 0.1]
def tri_exp(t, x):
a, b, c, d, e, f, g = x
return a * np.exp(-b * t) + c * np.exp(-d * t) + e * np.exp(-f * t) + g
def dtri_exp(t, x):
a, b, c, d, e, f, g = x
return -a*b * np.exp(-b * t) - c*d * np.exp(-d * t) - e*f * np.exp(-f * t)
def d2tri_exp(t, x):
a, b, c, d, e, f, g = x
return a*b*b * np.exp(-b * t) + c*d*d * np.exp(-d * t) + e*f*f * np.exp(-f * t)
a_batches1 = []
dadt_batches1 = []
d2adt2_batches1 = []
for j, (i, r, v) in enumerate(zip(i_batches1, r_batches1, v_batches1)):
std_cutoff = 0.01
#std_cutoff = np.nan
pt = time1
pv = voltage1
cc = change_pt1
dd = cap_mask1
t_split = pt[~cc]
t_split = np.append(t_split, pt[-1] + 1)
t_i = 0
a = (i / (g * r * (v - e))).cpu().numpy()
tt = pt[dd]
aa = a.reshape(-1)[dd]
ao = np.zeros(pt.shape)
dadto = np.zeros(pt.shape)
d2adt2o = np.zeros(pt.shape)
for t_f in t_split:
idx = np.where((tt >= t_i) & (tt < t_f))[0]
std = np.std(aa[idx])
tfit = tt[idx]
idx_full = np.where((pt >= tfit[0]) & (pt <= tfit[-1]))[0]
if std > std_cutoff:
afit = aa[idx]
t = tfit - tfit[0]
def f(x):
return np.sqrt(np.mean((tri_exp(t, x) - afit)**2))
xopt = optimize.fmin(f, x0, disp=False)
ao[idx_full] = tri_exp(t, xopt)
dadto[idx_full] = dtri_exp(t, xopt)
d2adt2o[idx_full] = d2tri_exp(t, xopt)
print(v[idx_full][0].item(), std, 'exp fit')
else:
afit = smooth(aa[idx], 51)[25:-25] # smoothing with 51/10ms
spl = UnivariateSpline(tfit, afit, k=4) # want smooth 2nd derivate, so k>3
spl.set_smoothing_factor(0.2)
ao[idx_full] = spl(tfit)
dadto[idx_full] = spl(tfit, 1)
d2adt2o[idx_full] = spl(tfit, 2)
print(v[idx_full][0].item(), std, 'spline fit')
t_i = t_f
a_batches1.append(torch.from_numpy(ao).to(device))
dadt_batches1.append(torch.from_numpy(dadto).to(device))
d2adt2_batches1.append(torch.from_numpy(d2adt2o).to(device))
if True:
plt.plot(time1[dd], a[dd])
plt.plot(time1, a_batches1[-1].reshape(-1).cpu().numpy())
plt.plot(time1, dadt_batches1[-1].reshape(-1).cpu().numpy())
plt.plot(time1, d2adt2_batches1[-1].reshape(-1).cpu().numpy())
#plt.show()
plt.savefig('r1-bad/tmp1', dpi=200)
plt.close()
a_batches2 = []
dadt_batches2 = []
d2adt2_batches2 = []
for j, (i, r, v) in enumerate(zip(i_batches2, r_batches2, v_batches2)):
std_cutoff = 0.015
#std_cutoff = np.nan
pt = time2
pv = voltage2
cc = change_pt2
dd = cap_mask2
t_split = pt[~cc]
t_split = np.append(t_split, pt[-1] + 1)
t_i = 0
a = (i / (g * r * (v - e))).cpu().numpy()
tt = pt[dd]
aa = a.reshape(-1)[dd]
ao = np.zeros(pt.shape)
dadto = np.zeros(pt.shape)
d2adt2o = np.zeros(pt.shape)
for t_f in t_split:
idx = np.where((tt >= t_i) & (tt < t_f))[0]
std = np.std(aa[idx])
tfit = tt[idx]
idx_full = np.where((pt >= tfit[0]) & (pt <= tfit[-1]))[0]
if std > std_cutoff:
afit = aa[idx]
t = tfit - tfit[0]
def f(x):
return np.sqrt(np.mean((tri_exp(t, x) - afit)**2))
if v[idx_full][0].item() == -90:
xopt, _ = pints.fmin(f, x02, method=pints.CMAES)
print('PINTS...')
else:
xopt = optimize.fmin(f, x02, disp=False)
ao[idx_full] = tri_exp(t, xopt)
dadto[idx_full] = dtri_exp(t, xopt)
d2adt2o[idx_full] = d2tri_exp(t, xopt)
print(v[idx_full][0].item(), std, 'exp fit')
else:
afit = smooth(aa[idx], 51)[25:-25] # smoothing with 51/10ms
spl = UnivariateSpline(tfit, afit, k=4) # want smooth 2nd derivate, so k>3
spl.set_smoothing_factor(0.2)
ao[idx_full] = spl(tfit)
dadto[idx_full] = spl(tfit, 1)
d2adt2o[idx_full] = spl(tfit, 2)
print(v[idx_full][0].item(), std, 'spline fit')
t_i = t_f
a_batches2.append(torch.from_numpy(ao).to(device))
dadt_batches2.append(torch.from_numpy(dadto).to(device))
d2adt2_batches2.append(torch.from_numpy(d2adt2o).to(device))
if True:
plt.plot(time2[dd], a[dd])
plt.plot(time2, a_batches2[-1].reshape(-1).cpu().numpy())
plt.plot(time2, dadt_batches2[-1].reshape(-1).cpu().numpy())
plt.plot(time2, d2adt2_batches2[-1].reshape(-1).cpu().numpy())
#plt.show()
plt.savefig('r1-bad/tmp2', dpi=200)
plt.close()
#sys.exit()
else:
dvdt_constant = torch.tensor([0]).to(device) # for now yes
drdt_batches1 = []
drdt_batches2 = []
for r, v in zip(r_batches1, v_batches1):
k3 = m.p5 * torch.exp(m.p6 * v)
k4 = m.p7 * torch.exp(-m.p8 * v)
drdt = -k3 * r + k4 * (1. - r)
drdt_batches1.append(drdt)
for r, v in zip(r_batches2, v_batches2):
k3 = m.p5 * torch.exp(m.p6 * v)
k4 = m.p7 * torch.exp(-m.p8 * v)
drdt = -k3 * r + k4 * (1. - r)
drdt_batches2.append(drdt)
didt_batches1 = []
for j, (i, v) in enumerate(zip(i_batches1, v_batches1)):
ii = i.cpu().numpy().reshape(-1)
pt = time1
pv = voltage1
cc = change_pt1
dd = cap_mask1
t_split = pt[~cc]
t_split = np.append(t_split, pt[-1] + 1)
t_i = 0
tt = pt[dd]
ii = ii[dd]
io = np.zeros(pt.shape)
didto = np.zeros(pt.shape)
for t_f in t_split:
idx = np.where((tt >= t_i) & (tt < t_f))[0]
tfit = tt[idx]
idx_full = np.where((pt >= tfit[0]) & (pt <= tfit[-1]))[0]
ifit = smooth(ii[idx], 61)[30:-30] # smoothing
spl = UnivariateSpline(tfit, ifit, k=3)
spl.set_smoothing_factor(0)
io[idx_full] = spl(tfit)
didto[idx_full] = spl(tfit, 1)
t_i = t_f
i_batches1[j] = torch.from_numpy(io).to(device)
didt_batches1.append(torch.from_numpy(didto).to(device))
didt_batches2 = []
for j, (i, v) in enumerate(zip(i_batches2, v_batches2)):
ii = i.cpu().numpy().reshape(-1)
pt = time2
pv = voltage2
cc = change_pt2
dd = cap_mask2
t_split = pt[~cc]
t_split = np.append(t_split, pt[-1] + 1)
t_i = 0
tt = pt[dd]
ii = ii[dd]
io = np.zeros(pt.shape)
didto = np.zeros(pt.shape)
for t_f in t_split:
idx = np.where((tt >= t_i) & (tt < t_f))[0]
tfit = tt[idx]
idx_full = np.where((pt >= tfit[0]) & (pt <= tfit[-1]))[0]
ifit = smooth(ii[idx], 61)[30:-30] # smoothing
spl = UnivariateSpline(tfit, ifit, k=3)
spl.set_smoothing_factor(0)
io[idx_full] = spl(tfit)
didto[idx_full] = spl(tfit, 1)
t_i = t_f
i_batches2[j] = torch.from_numpy(io).to(device)
didt_batches2.append(torch.from_numpy(didto).to(device))
# Calculate a and dadt
a_batches1 = []
dadt_batches1 = []
for j, (i, r, v, drdt, didt) in enumerate(zip(i_batches1, r_batches1, v_batches1, drdt_batches1, didt_batches1)):
ii = i.reshape(-1)
a = ii / (g * r * (v - e))
if np.all(v.cpu().numpy() == v.cpu().numpy()[0]) or True: # all steps even different values
dvdt = dvdt_constant
else:
spl = UnivariateSpline(range(len(v.cpu().numpy())), v.cpu().numpy(), k=3, s=0)
dvdt = torch.from_numpy(spl.derivative()(range(len(v.cpu().numpy()))))
dadt = r ** (-1) * (
(didt / g - a * r * dvdt) / (v - e)
- a * drdt
)
a_batches1.append(a)
dadt_batches1.append(dadt)
a_batches2 = []
dadt_batches2 = []
for j, (i, r, v, drdt, didt) in enumerate(zip(i_batches2, r_batches2, v_batches2, drdt_batches2, didt_batches2)):
ii = i.reshape(-1)
a = ii / (g * r * (v - e))
if np.all(v.cpu().numpy() == v.cpu().numpy()[0]) or True: # all steps even different values
dvdt = dvdt_constant
else:
spl = UnivariateSpline(range(len(v.cpu().numpy())), v.cpu().numpy(), k=3, s=0)
dvdt = torch.from_numpy(spl.derivative()(range(len(v.cpu().numpy()))))
dadt = r ** (-1) * (
(didt / g - a * r * dvdt) / (v - e)
- a * drdt
)
a_batches2.append(a)
dadt_batches2.append(dadt)
# To tensors
for i, (v, a, dadt) in enumerate(zip(v_batches1, a_batches1, dadt_batches1)):
v_batches1[i] = v[mask1,...][skip::sparse]
a_batches1[i] = a[mask1,...][skip::sparse]
dadt_batches1[i] = dadt[mask1,...][skip::sparse]
if args.smoothi:
d2adt2_batches1[i] = d2adt2_batches1[i][mask1,...][skip::sparse]
for i, (v, a, dadt) in enumerate(zip(v_batches2, a_batches2, dadt_batches2)):
v_batches2[i] = v[mask2,...][skip::sparse]
a_batches2[i] = a[mask2,...][skip::sparse]
dadt_batches2[i] = dadt[mask2,...][skip::sparse]
if args.smoothi:
d2adt2_batches2[i] = d2adt2_batches2[i][mask2,...][skip::sparse]
v_batches = torch.cat(v_batches1 + v_batches2).to(device)
a_batches = torch.cat(a_batches1 + a_batches2).to(device)
dadt_batches = torch.cat(dadt_batches1 + dadt_batches2).to(device)
# Cache it
torch.save(v_batches, 'r1-bad/v.pt')
torch.save(a_batches, 'r1-bad/a.pt')
torch.save(dadt_batches, 'r1-bad/dadt.pt')
if args.smoothi:
d2adt2_batches = torch.cat(d2adt2_batches1 + d2adt2_batches2).to(device)
torch.save(d2adt2_batches, 'r1-bad/d2adt2.pt')
if args.debug:
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter(v_batches.reshape(-1).detach().cpu().numpy(), a_batches.reshape(-1).detach().cpu().numpy(),
dadt_batches.reshape(-1).detach().cpu().numpy())
ax.set_xlabel('V')
ax.set_ylabel('a')
ax.set_zlabel('da/dt')
plt.show()
#sys.exit()
###
###
###
if __name__ == '__main__':
ii = 0
func = ODEFunc().to(device)
#"""
###
### Pretrain
###
loss_fn = torch.nn.MSELoss(reduction='sum')
x1 = torch.reshape(torch.linspace(-140, 80, 100).to(device), (-1, 1))
xx1 = x1 / func.vrange
x2 = torch.reshape(torch.linspace(-0.5, 1.5, 100).to(device), (-1, 1))
X1, X2 = torch.meshgrid(x1.reshape(-1), x2.reshape(-1))
XX1, X2 = torch.meshgrid(xx1.reshape(-1), x2.reshape(-1))
p1 = torch.tensor([1.13e-4]).to(device)
p2 = torch.tensor([7.45e-2]).to(device)
p3 = torch.tensor([3.60e-5]).to(device)
p4 = torch.tensor([4.49e-2]).to(device)
k1 = p1 * torch.exp(p2 * X1)
k2 = p3 * torch.exp(-p4 * X1)
Y = k1 * (torch.tensor([1]).to(device) - X2) - k2 * X2
XX = torch.stack([XX1.reshape(-1), X2.reshape(-1)]).T
YY = Y.reshape(-1)
opt = optim.Adam(func.net.parameters(), lr=0.001)
for _ in range(1000):
p = func.net(XX).to(device) / func.netscale
loss = loss_fn(p.reshape(-1), YY)
opt.zero_grad()
loss.backward()
opt.step()
if args.debug:
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X1.cpu().numpy(), X2.cpu().numpy(), Y.cpu().numpy())
pred = func.net(XX) / func.netscale
ax.plot_surface(X1.cpu().numpy(), X2.cpu().numpy(), pred.reshape(50, 50).detach().cpu().numpy())
plt.show()
#sys.exit()
###"""
###
### To predict
###
x1 = torch.reshape(torch.linspace(-130, 70, 50).to(device), (-1, 1))
xx1 = x1 / func.vrange
x2 = torch.reshape(torch.linspace(0, 1, 50).to(device), (-1, 1))
X1, X2 = torch.meshgrid(x1.reshape(-1), x2.reshape(-1))
XX1, X2 = torch.meshgrid(xx1.reshape(-1), x2.reshape(-1))
XX = torch.stack([XX1.reshape(-1), X2.reshape(-1)]).T
# pretrained
with torch.no_grad():
pretrain_pred = func.net(XX) / func.netscale
###
###
### Training
###
#'''
x_av = torch.stack([v_batches.reshape(-1) / func.vrange, a_batches.reshape(-1)]).T
y_dadt = dadt_batches.reshape(-1)
# Remove a < 0 (probably due to the slight error in the reverseal potential)
#to_keep = x_av[:, 1] > 0.01
to_keep = x_av[:, 1] > 0
x_av = x_av[to_keep, :]
y_dadt = y_dadt[to_keep]
opt = optim.Adam(func.net.parameters(), lr=0.0001)
# gamma = decaying factor
scheduler = StepLR(opt, step_size=100, gamma=0.9) # 0.9**(4000steps/100) ~ 0.016
for itr in range(4000):
p = func.net(x_av.float()).to(device) / func.netscale
loss = loss_fn(p.reshape(-1), y_dadt.float())
opt.zero_grad()
loss.backward()
opt.step()
# Decay Learning Rate
scheduler.step()
if (itr % 400) == 0:
print('Iter', itr, 'Loss', loss.item())
#'''
# Save model
torch.save(func.state_dict(), 'r1-bad/model-state-dict.pt')
torch.save(func, 'r1-bad/model-entire.pt')
# To load model:
# func = TheModelClass(*args, **kwargs)
# func.set_fixed_form_voltage_protocol(protocol[:, 0], protocol[:, 1])
# func.load_state_dict(torch.load('r1-bad/model-state-dict.pt'))
# func.eval()
#
# Or:
# func = torch.load('r1-bad/model-entire.pt')
# func.eval()
with torch.no_grad():
true_y0 = true_y0s[1]
func.set_fixed_form_voltage_protocol(timep1, voltagep1)
pred_y = odeint(func, true_y0, timep1_torch).to(device)
pred_yo = g * pred_y[:, 0, 0] * pred_y[:, 0, 1] * (func._v(timep1_torch).to(device) - e)
loss = torch.mean(torch.abs(pred_yo - torch.from_numpy(currentp1).to(device)))
print('Training | Total Loss {:.6f}'.format(loss.item()))
fig1, ax1 = plt.subplots(1, 1, figsize=(6, 4))
ax1.set_xlabel('t')
ax1.set_ylabel('i')
ax1.plot(timep1, currentp1, 'g-')
ax1.plot(timep1, pred_yo.reshape(-1).cpu().numpy(), 'b--')
ax1.set_xlim(timep1.min(), timep1.max())
fig1.tight_layout()
fig1.savefig('r1-bad/{:03d}'.format(ii), dpi=200)
plt.close(fig1)
ax = plt.axes(projection='3d')
ax.scatter(v_batches.reshape(-1).detach().cpu().numpy(), a_batches.reshape(-1).detach().cpu().numpy(),
dadt_batches.reshape(-1).detach().cpu().numpy())
pred = func.net(XX) / func.netscale
ax.plot_surface(X1.cpu().numpy(), X2.cpu().numpy(), pred.reshape(50, 50).detach().cpu().numpy(), color='C1')
ax.set_xlabel('V')
ax.set_ylabel('a')
ax.set_zlabel('da/dt')
plt.savefig('r1-bad/rates3d-{:03d}'.format(ii), dpi=200)
#plt.show()
plt.close()
ii += 1
###
###