-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotter.py
1689 lines (1435 loc) · 65.9 KB
/
plotter.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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import matplotlib as mpl
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.colors as mcolors
import matplotlib.cm as cm
import numpy as np
from dolfin import *
import string
import os
import subprocess
# set font & text parameters
font = {'family' : 'serif',
'weight' : 'bold',
'size' : 15}
plt.rc('font', **font)
plt.rc('text', usetex=True)
mpl.rcParams['image.cmap'] = 'jet'
# font size of labels
fosi = 17
# set colors
colormap = cm.viridis
mus = [1,2,3,4,5,6]
colorparams = mus
colormap = cm.viridis
normalize = mcolors.Normalize(vmin=np.min(colorparams), vmax=np.max(colorparams))
c2 = colormap(normalize(mus[0]))
c1 = colormap(normalize(mus[1]))
c0 = colormap(normalize(mus[2]))
c3 = colormap(normalize(mus[3]))
c4 = colormap(normalize(mus[4]))
c5 = colormap(normalize(mus[5]))
fs = 0.85
lw = 3.5
class Plotter():
def __init__(self, problem, path_data=None):
self.problem = problem
N_ions = self.problem.N_ions
N_comparts = self.problem.N_comparts
self.N_unknows = N_comparts*(2 + N_ions) - 1
# initialize mesh and data file
if path_data is not None:
self.set_mesh_and_datafile(path_data)
return
def set_mesh_and_datafile(self, path_data):
# read data file
h5_fname = path_data + 'PDE/results.h5'
self.hdf5 = HDF5File(MPI.comm_world, h5_fname, 'r')
# create mesh
self.mesh = Mesh()
self.hdf5.read(self.mesh, '/mesh', False)
# convert coordinates from m to mm
self.mesh.coordinates()[:] *= 1e3
return
def set_mesh_and_datafile_compare(self, path_data):
# read data file
h5_fname_compare = path_data + 'PDE/results.h5'
self.hdf5_compare = HDF5File(MPI.comm_world, h5_fname_compare, 'r')
# create mesh
self.mesh_compare = Mesh()
self.hdf5_compare.read(self.mesh_compare, '/mesh', False)
# convert coordinates from m to mm
self.mesh_compare.coordinates()[:] *= 1e3
return
def read_from_file(self, n, i, scale=1.):
""" get snapshot of solution w[i] at time = n seconds """
N_comparts = self.problem.N_comparts
N_unknows = self.N_unknows
DG0 = FiniteElement('DG', self.mesh.ufl_cell(), 0)
CG1 = FiniteElement('CG', self.mesh.ufl_cell(), 1)
e = [DG0]*(N_comparts - 1) + [CG1]*(N_unknows - (N_comparts - 1))
W = FunctionSpace(self.mesh, MixedElement(e))
u = Function(W)
if i < (N_comparts - 1):
V_DG0 = FunctionSpace(self.mesh, DG0)
f = Function(V_DG0)
else:
V_CG1 = FunctionSpace(self.mesh, CG1)
f = Function(V_CG1)
# read native data file
self.hdf5.read(u, "/solution/vector_" + str(n))
# assign data to function
assign(f, u.split()[i])
f.vector()[:] = scale*f.vector().get_local()
return f
def read_from_file_compare(self, n, i, scale=1.):
N_comparts = self.problem.N_comparts
N_unknows = self.N_unknows
DG0 = FiniteElement('DG', self.mesh_compare.ufl_cell(), 0)
CG1 = FiniteElement('CG', self.mesh_compare.ufl_cell(), 1)
e = [DG0]*(N_comparts - 1) + [CG1]*(N_unknows - (N_comparts - 1))
W = FunctionSpace(self.mesh_compare, MixedElement(e))
u = Function(W)
if i < (N_comparts - 1):
V_DG0 = FunctionSpace(self.mesh_compare, DG0)
f = Function(V_DG0)
else:
V_CG1 = FunctionSpace(self.mesh_compare, CG1)
f = Function(V_CG1)
# read native data file
self.hdf5_compare.read(u, "/solution/vector_" + str(n))
# assign data to function
assign(f, u.split()[i])
f.vector()[:] = scale*f.vector().get_local()
return f
def project_to_function_space(self, u):
""" project u onto function space """
CG1 = FiniteElement('CG', self.mesh.ufl_cell(), 1)
V = FunctionSpace(self.mesh, CG1)
f = project(u, V)
return f
def make_amplitude(self, path_figs, n):
""" check contribution from amplitude at given time n """
# read solutions from file
alpha_N = self.read_from_file(n, 0)
alpha_G = self.read_from_file(n, 1)
#Na_N = self.read_from_file(n, 2)
#Na_G = self.read_from_file(n, 3)
#Na_E = self.read_from_file(n, 4)
#K_N = self.read_from_file(n, 5)
#K_G = self.read_from_file(n, 6)
K_E = self.read_from_file(n, 7)
#Cl_N = self.read_from_file(n, 8)
#Cl_G = self.read_from_file(n, 9)
Cl_E = self.read_from_file(n, 10)
#Glu_N = self.read_from_file(n, 11)
#Glu_G = self.read_from_file(n, 12)
Glu_E = self.read_from_file(n, 13)
# concert to mV
phi_N = self.read_from_file(n, 14, scale=1.0e3)
phi_G = self.read_from_file(n, 15, scale=1.0e3)
phi_E = self.read_from_file(n, 16, scale=1.0e3)
alpha_N_init = float(self.problem.alpha_N_init)
alpha_G_init = float(self.problem.alpha_G_init)
K_E_init = float(self.problem.K_E_init)
Cl_E_init = float(self.problem.Cl_E_init)
Glu_E_init = float(self.problem.Glu_E_init)
phi_N_init = float(self.problem.phi_N_init)*1.0e3
phi_G_init = float(self.problem.phi_G_init)*1.0e3
phi_E_init = float(self.problem.phi_E_init)*1.0e3
phi_NE_init = phi_N_init - phi_E_init
phi_GE_init = phi_G_init - phi_E_init
phi_NE = self.project_to_function_space(phi_N - phi_E)
phi_GE = self.project_to_function_space(phi_G - phi_E)
# calculate extracellular volume fraction
u_alpha_E = 1.0 - alpha_N - alpha_G
alpha_E = self.project_to_function_space(u_alpha_E)
alpha_E_init = 1.0 - alpha_N_init - alpha_G_init
# calculate charge in volume fractions (alpha) in %
u_alpha_N_diff = (alpha_N - alpha_N_init)/alpha_N_init*100
u_alpha_G_diff = (alpha_G - alpha_G_init)/alpha_G_init*100
u_alpha_E_diff = (alpha_E - alpha_E_init)/alpha_E_init*100
alpha_N_diff = self.project_to_function_space(u_alpha_N_diff)
alpha_G_diff = self.project_to_function_space(u_alpha_G_diff)
alpha_E_diff = self.project_to_function_space(u_alpha_E_diff)
# calculate amplitudes
# ECS concentrations
self.amplitude_K_E = max(abs(K_E.vector().get_local() - K_E_init))
self.amplitude_Glu_E = max(abs(Glu_E.vector().get_local() - Glu_E_init))
self.amplitude_Cl_E = max(abs(Cl_E.vector().get_local() - Cl_E_init))
# potentials
self.amplitude_phi_NE = max(abs(phi_NE.vector().get_local() - phi_NE_init))
self.amplitude_phi_GE = max(abs(phi_GE.vector().get_local() - phi_GE_init))
self.amplitude_phi_E = max(abs(phi_E.vector().get_local() - phi_E_init))
# volume fractions
self.amplitude_alpha_N = max(abs(alpha_N_diff.vector().get_local()))
self.amplitude_alpha_G = max(abs(alpha_G_diff.vector().get_local()))
self.amplitude_alpha_E = max(abs(alpha_E_diff.vector().get_local()))
# write amplitudes to file
title_f = path_figs + "amplitude.txt"
f = open(title_f, 'w+')
f.write('ECS concentrations:')
f.write('\n')
f.write('amplitude K_E %g' % self.amplitude_K_E)
f.write('\n')
f.write('amplitude Glu_E %g' % self.amplitude_Glu_E)
f.write('\n')
f.write('amplitude Cl_E %g' % self.amplitude_Cl_E)
f.write('\n')
f.write('\n')
f.write('Potentials:')
f.write('\n')
f.write('amplitude phi_NE %g' % self.amplitude_phi_NE)
f.write('\n')
f.write('amplitude phi_GE %g' % self.amplitude_phi_GE)
f.write('\n')
f.write('amplitude phi_E %g' % self.amplitude_phi_E)
f.write('\n')
f.write('\n')
f.write('Volume fractions:')
f.write('\n')
f.write('amplitude alpha_N %g' % self.amplitude_alpha_N)
f.write('\n')
f.write('amplitude alpha_G %g' % self.amplitude_alpha_G)
f.write('\n')
f.write('amplitude alpha_E %g' % self.amplitude_alpha_E)
f.write('\n')
f.close()
return
def init_duration(self):
# initiate calculation of duration (s)
# ECS concentrations
self.duration_K_E = 0
self.duration_Glu_E = 0
self.duration_Cl_E = 0
# potentials
self.duration_phi_NE = 0
self.duration_phi_GE = 0
self.duration_phi_E = 0
# volume fractions
self.duration_alpha_N = 0
self.duration_alpha_G = 0
self.duration_alpha_E = 0
return
def get_duration(self, n):
""" check contribution from duration at given time n """
# read solutions from file
alpha_N = self.read_from_file(n, 0)
alpha_G = self.read_from_file(n, 1)
#Na_N = self.read_from_file(n, 2)
#Na_G = self.read_from_file(n, 3)
#Na_E = self.read_from_file(n, 4)
#K_N = self.read_from_file(n, 5)
#K_G = self.read_from_file(n, 6)
K_E = self.read_from_file(n, 7)
#Cl_N = self.read_from_file(n, 8)
#Cl_G = self.read_from_file(n, 9)
Cl_E = self.read_from_file(n, 10)
#Glu_N = self.read_from_file(n, 11)
#Glu_G = self.read_from_file(n, 12)
Glu_E = self.read_from_file(n, 13)
# concert to mV
phi_N = self.read_from_file(n, 14, scale=1.0e3)
phi_G = self.read_from_file(n, 15, scale=1.0e3)
phi_E = self.read_from_file(n, 16, scale=1.0e3)
# calculate extracellular volume fraction
u_alpha_E = 1.0 - alpha_N - alpha_G
alpha_E = self.project_to_function_space(u_alpha_E)
alpha_N_init = float(self.problem.alpha_N_init)
alpha_G_init = float(self.problem.alpha_G_init)
alpha_E_init = 1.0 - alpha_N_init - alpha_G_init
# calculate charge in volume fractions (alpha) in %
u_alpha_N_diff = (alpha_N - alpha_N_init)/alpha_N_init*100
u_alpha_G_diff = (alpha_G - alpha_G_init)/alpha_G_init*100
u_alpha_E_diff = (alpha_E - alpha_E_init)/alpha_E_init*100
alpha_N_diff = self.project_to_function_space(u_alpha_N_diff)
alpha_G_diff = self.project_to_function_space(u_alpha_G_diff)
alpha_E_diff = self.project_to_function_space(u_alpha_E_diff)
phi_NE = self.project_to_function_space(phi_N - phi_E)
phi_GE = self.project_to_function_space(phi_G - phi_E)
# point at which to evaluate solution
point = 1.0
# evaluate ECS concentrations at point
K_E_x = K_E(point)
Glu_E_x = Glu_E(point)
Cl_E_x = Cl_E(point)
# evaluate potentials at point
phi_NE_x = phi_NE(point)
phi_GE_x = phi_GE(point)
phi_E_x = phi_E(point)
# evaluate volume fractions at point
alpha_N_x = alpha_N_diff(point)
alpha_G_x = alpha_G_diff(point)
alpha_E_x = alpha_E_diff(point)
# add one second to duration if wave is present (i.e u > u_thres):
# if value is greater/lesser than threshold for ECS concentrations (mM)
if K_E_x > 8: self.duration_K_E += 1 # init 4 mM
if Glu_E_x > 0.02: self.duration_Glu_E += 1 # init 0.01 mM
if Cl_E_x < 111: self.duration_Cl_E += 1 # init 113 mM
# add one second to duration if wave is present (i.e k > k_thres):
# if value is greater/lesser than threshold for potentials (mV)
if phi_NE_x > -66: self.duration_phi_NE += 1 # init -71 mV
if phi_GE_x > -77: self.duration_phi_GE += 1 # init -82 mV
#if phi_E_x < -0.05: self.duration_phi_E += 1 # init 0 mV
if phi_E_x < -0.5: self.duration_phi_E += 1 # init 0 mV
# add one second to duration if wave is present (i.e k > k_thres):
# if value is greater/lesser than threshold for volume fractions (%)
if alpha_N_x > 0.5: self.duration_alpha_N += 1
if alpha_G_x > 0.5: self.duration_alpha_G += 1
if alpha_E_x < -0.5: self.duration_alpha_E += 1
return
def save_duration(self, res_path):
# ECS concentrations
duration_K_E = self.duration_K_E
duration_Glu_E = self.duration_Glu_E
duration_Cl_E = self.duration_Cl_E
# potentials
duration_phi_NE = self.duration_phi_NE
duration_phi_GE = self.duration_phi_GE
duration_phi_E = self.duration_phi_E
# volume fractions
duration_alpha_N = self.duration_alpha_N
duration_alpha_G = self.duration_alpha_G
duration_alpha_E = self.duration_alpha_E
# write durations to file
title_f = res_path + "duration.txt"
f = open(title_f, 'w+')
f.write('ECS concentrations:')
f.write('\n')
f.write('duration K_E %g ' % self.duration_K_E)
f.write('\n')
f.write('duration Glu_E %g ' % self.duration_Glu_E)
f.write('\n')
f.write('duration Cl_E %g \n' % self.duration_Cl_E)
f.write('\n')
f.write('\n')
f.write('Potentials:')
f.write('\n')
f.write('duration phi_NE %g ' % self.duration_phi_NE)
f.write('\n')
f.write('duration phi_GE %g ' % self.duration_phi_GE)
f.write('\n')
f.write('duration phi_E %g \n ' % self.duration_phi_E)
f.write('\n')
f.write('\n')
f.write('Volume fractions:')
f.write('\n')
f.write('duration alpha_N %g ' % self.duration_alpha_N)
f.write('\n')
f.write('duration alpha_G %g ' % self.duration_alpha_G)
f.write('\n')
f.write('duration alpha_E %g \n' % self.duration_alpha_E)
f.write('\n')
f.close()
return
def init_wavespeed(self):
""" initiate calculation of wave speed """
# for saving pairs of space and time coordinates when wave passes
self.wavefront_space_time = []
# get coordinates (mm)
self.coordinates = self.mesh.coordinates()
return
def get_wavespeed(self, n):
""" save wave speed at given time n """
# get neuron potential
phi_N = self.read_from_file(n, 14, scale=1.0e3)
# get values of phi N
phi_N_values = phi_N.compute_vertex_values()
# get max value of potential (phi) at time n
index_max = max(range(len(phi_N_values)), key=phi_N_values.__getitem__)
v_max = phi_N_values[index_max] # max value of phi N (mV)
p_max = self.coordinates[index_max][0] # point of max value (mm)
t_max = n # time for max value (s)
# check that wave has passed
# (assumption: wave has passed if neuron potential phi > -20 mV)
if v_max > -20:
# save point in space and time of wave front
# (assumption: wave front is where phi_N has greatest value)
self.wavefront_space_time.append([p_max, t_max])
return
def save_wavespeed(self, res_path):
p_max_prev = 0 # previous value of p_max
t_max_prev = 0 # previous value of t_max
speeds = [] # for saving speeds
for i, pair in enumerate(self.wavefront_space_time):
# get values
(p_max, t_max) = pair
if i > 0:
if p_max_prev == p_max:
# wave front has not moved to next point -> continue
continue
else:
# wave front has moved to next point -> calculate speed
# distance between wave front (mm)
dx = p_max - p_max_prev
# time between wave front (convert from second to min)
dt = (t_max - t_max_prev)/60.0
# calculate and append speed
speeds.append(dx/dt)
# update p_max_prev and t_max_prev
p_max_prev = p_max
t_max_prev = t_max
# remove effects from initiation and termination of wave
speeds_mid = speeds[20:-20]
# plot speeds
if (len(speeds_mid) > 0):
# calculate average speed
avg = sum(speeds_mid)/len(speeds_mid)
# plot speeds
plt.figure()
plt.plot(speeds_mid, 'ro')
plt.ylabel(r'mm/min')
plt.xlabel('intervals')
plt.title('Wave speed, avg = %.2f mm/min' % avg)
# save figure
fname_res = res_path + 'wavespeed.png'
plt.savefig(fname_res)
plt.close()
return speeds_mid
def make_timeplot(self, path_figs, Tstop):
""" plot variables at t=Tstop """
# point at which to calculate duration
point = 2.0
# list of function values at point
alpha_Ns = []; alpha_Gs = []; alpha_Es = []
Na_Ns = []; K_Ns = []; Cl_Ns = []; Glu_Ns = []
Na_Gs = []; K_Gs = []; Cl_Gs = []; Glu_Gs = []
Na_Es = []; K_Es = []; Cl_Es = []; Glu_Es = []
phi_Ns = []; phi_Gs = []; phi_Es = []
for n in range(Tstop):
# get and append data PDEs
alpha_N = self.read_from_file(n, 0)
alpha_G = self.read_from_file(n, 1)
Na_N = self.read_from_file(n, 2)
Na_G = self.read_from_file(n, 3)
Na_E = self.read_from_file(n, 4)
K_N = self.read_from_file(n, 5)
K_G = self.read_from_file(n, 6)
K_E = self.read_from_file(n, 7)
Cl_N = self.read_from_file(n, 8)
Cl_G = self.read_from_file(n, 9)
Cl_E = self.read_from_file(n, 10)
Glu_N = self.read_from_file(n, 11)
Glu_G = self.read_from_file(n, 12)
Glu_E = self.read_from_file(n, 13)
# concert to mV
phi_N = self.read_from_file(n, 14, scale=1.0e3)
phi_G = self.read_from_file(n, 15, scale=1.0e3)
phi_E = self.read_from_file(n, 16, scale=1.0e3)
# calculate extracellular volume fraction
u_alpha_E = 1.0 - alpha_N - alpha_G
alpha_E = self.project_to_function_space(u_alpha_E)
alpha_N_init = float(self.problem.alpha_N_init)
alpha_G_init = float(self.problem.alpha_G_init)
alpha_E_init = 1.0 - alpha_N_init - alpha_G_init
# calculate charge in volume fractions (alpha) in %
u_alpha_N_diff = (alpha_N - alpha_N_init)/alpha_N_init*100
u_alpha_G_diff = (alpha_G - alpha_G_init)/alpha_G_init*100
u_alpha_E_diff = (alpha_E - alpha_E_init)/alpha_E_init*100
alpha_N_diff = self.project_to_function_space(u_alpha_N_diff)
alpha_G_diff = self.project_to_function_space(u_alpha_G_diff)
alpha_E_diff = self.project_to_function_space(u_alpha_E_diff)
alpha_Ns.append(alpha_N_diff(point))
alpha_Gs.append(alpha_G_diff(point))
alpha_Es.append(alpha_E_diff(point))
Na_Ns.append(Na_N(point))
K_Ns.append(K_N(point))
Cl_Ns.append(Cl_N(point))
Na_Gs.append(Na_G(point))
K_Gs.append(K_G(point))
Cl_Gs.append(Cl_G(point))
Na_Es.append(Na_E(point))
K_Es.append(K_E(point))
Cl_Es.append(Cl_E(point))
Glu_Ns.append(Glu_N(point))
Glu_Gs.append(Glu_G(point))
Glu_Es.append(Glu_E(point))
phi_Ns.append(phi_N(point))
phi_Gs.append(phi_G(point))
phi_Es.append(phi_E(point))
######################################################################
print("-------------------")
print("-------------------")
print("ION CONCENTRATIONS")
print("-------------------")
print("Neuron")
print("Na 0", Na_Ns[0])
print("Na Tstop", Na_Ns[-1])
print("K 0", K_Ns[0])
print("K Tstop", K_Ns[-1])
print("Cl 0", Cl_Ns[0])
print("Cl Tstop", Cl_Ns[-1])
print("-------------------")
print("Glial")
print("Na 0", Na_Gs[0])
print("Na Tstop", Na_Gs[-1])
print("K 0", K_Gs[0])
print("K Tstop", K_Gs[-1])
print("Cl 0", Cl_Gs[0])
print("Cl Tstop", Cl_Gs[-1])
print("-------------------")
print("ECS")
print("Na 0", Na_Es[0])
print("Na Tstop", Na_Es[-1])
print("K 0", K_Es[0])
print("K Tstop", K_Es[-1])
print("Cl 0", Cl_Es[0])
print("Cl Tstop", Cl_Es[-1])
print("-------------------")
print("-------------------")
print("POTENTIALS")
print("-------------------")
print("N 0", phi_Ns[0])
print("N Tstop", phi_Ns[-1])
print("G 0", phi_Gs[0])
print("G Tstop", phi_Gs[-1])
print("ECS 0", phi_Es[0])
print("ECS Tstop", phi_Es[-1])
print("-------------------")
print("-------------------")
print("VOLUME FRACTIONS")
print("-------------------")
print("N 0", alpha_Ns[0])
print("N Tstop", alpha_Ns[-1])
print("G 0", alpha_Gs[0])
print("G Tstop", alpha_Gs[-1])
print("ECS 0", alpha_Es[0])
print("ECS Tstop", alpha_Es[-1])
print("-------------------")
# range of x values
xlim = [0.0, Tstop]
# create plot
fig = plt.figure(figsize=(19.5*fs, 5*fs))
ax = plt.gca()
ax1 = fig.add_subplot(1,4,1, xlim=xlim, ylim=[0.0, 1.5])
plt.ylabel(r'[Glu$]_e$ (mM)', fontsize=fosi)
plt.xlabel(r'time (s)', fontsize=fosi)
plt.yticks([0.25, 0.5, 0.75, 1, 1.25])
#plt.xticks([0, 25, 50, 75])
plt.plot(Glu_Es, label=r'Glu', linewidth=lw)
ax2 = fig.add_subplot(1,4,2, xlim=xlim, ylim=[0.0, 150])
plt.ylabel(r'[k$]_e$ (mM)', fontsize=fosi)
plt.xlabel(r'time (s)', fontsize=fosi)
plt.yticks([20, 40, 60, 80, 100, 120, 140])
#plt.xticks([0, 25, 50, 75])
plt.plot(Na_Es, color=c0, label=r'Na$^+$', linewidth=lw)
plt.plot(K_Es, color=c1, label=r'K$^+$',linewidth=lw)
plt.plot(Cl_Es, color=c2, label=r'Cl$^-$',linewidth=lw)
ax3 = fig.add_subplot(1,4,3, xlim=xlim, ylim=[-100, 20])
plt.ylabel(r'$\phi$ (mV)', fontsize=fosi)
plt.xlabel(r'time (s)', fontsize=fosi)
plt.yticks([-90, -70, -50, -30, -10, 10])
#plt.xticks([0, 25, 50, 75])
plt.plot(phi_Es, color=c3, label=r'ECS', linewidth=lw)
plt.plot(phi_Ns, color=c4, label=r'neuron',linewidth=lw)
plt.plot(phi_Gs, color=c5, label=r'glial', linewidth=lw)
ax4 = fig.add_subplot(1,4,4, xlim=xlim, ylim=[-50, 20])
plt.ylabel(r'$\Delta \alpha $ (\%)', fontsize=fosi)
plt.xlabel(r'time (s)', fontsize=fosi)
plt.yticks([-40, -30, -20, -10, 0, 10])
#plt.xticks([0, 25, 50, 75])
plt.plot(alpha_Es, color=c3, linewidth=lw)
plt.plot(alpha_Ns, color=c4, linewidth=lw)
plt.plot(alpha_Gs, color=c5, linewidth=lw)
# make pretty
ax.axis('off')
plt.subplots_adjust(wspace=0.35, left=0.1)
# add legend
plt.figlegend(bbox_to_anchor=(1.0, 0.9))
# add numbering for the subplots (A, B, C etc)
letters = [r'\textbf{E}', r'\textbf{F}', r'\textbf{G}', r'\textbf{H}']
for num, ax in enumerate([ax1, ax2, ax3, ax4]):
ax.text(-0.12, 1.06, letters[num], transform=ax.transAxes)
# save figure to file
fname_res = path_figs + 'timeplot_summary_%d' % Tstop
plt.savefig(fname_res + '.svg', format='svg')
plt.close()
# convert from svg to pdf
os.system('inkscape -D -z --file=' + fname_res + '.svg --export-pdf=' \
+ fname_res + '.pdf --export-latex')
######################################################################
# range of x values
xlim = [0.0, Tstop]
# create plot
fig = plt.figure(figsize=(18.0*fs, 10*fs))
ax = plt.gca()
ax1 = fig.add_subplot(2,4,1, xlim=xlim, ylim=[0.0, 1.5])
plt.title(r'ECS glutamate')
plt.ylabel(r'mM')
plt.xlabel(r's')
plt.plot(Glu_Es, color=c0, linewidth=lw)
ax2 = fig.add_subplot(2,4,2, xlim=xlim, ylim=[0.0, 150])
plt.title(r'ECS ion concentrations')
plt.ylabel(r'mM')
plt.xlabel(r's')
plt.yticks([0, 20, 40, 60, 80, 100, 120, 140])
plt.plot(Na_Es, color=c0, label=r'Na$^+$', linewidth=lw)
plt.plot(K_Es, color=c1, label=r'K$^+$',linewidth=lw)
plt.plot(Cl_Es, color=c2, label=r'Cl$^-$',linewidth=lw)
ax3 = fig.add_subplot(2,4,3, xlim=xlim, ylim=[0.0, 150])
plt.title(r'neuron ion concentrations')
plt.ylabel(r'mM')
plt.xlabel(r's')
plt.yticks([0, 20, 40, 60, 80, 100, 120, 140])
plt.plot(Na_Ns, color=c0, label=r'Na$^+$', linewidth=lw)
plt.plot(K_Ns, color=c1, label=r'K$^+$',linewidth=lw)
plt.plot(Cl_Ns, color=c2, label=r'Cl$^-$',linewidth=lw)
ax4 = fig.add_subplot(2,4,4, xlim=xlim, ylim=[0.0, 150])
plt.title(r'glial ion concentrations')
plt.ylabel(r'mM')
plt.xlabel(r's')
plt.yticks([0, 20, 40, 60, 80, 100, 120, 140])
plt.plot(Na_Gs, color=c0, label=r'Na$^+$', linewidth=lw)
plt.plot(K_Gs, color=c1, label=r'K$^+$',linewidth=lw)
plt.plot(Cl_Gs, color=c2, label=r'Cl$^-$',linewidth=lw)
ax5 = fig.add_subplot(2,4,5, xlim=xlim, ylim=[-100, 20])
plt.title(r'potentials')
plt.ylabel(r'mV')
plt.xlabel(r's')
plt.yticks([-90, -70, -50, -30, -10, 10])
plt.plot(phi_Ns, color=c4, linewidth=lw)
plt.plot(phi_Gs, color=c5, linewidth=lw)
plt.plot(phi_Es, color=c3, linewidth=lw)
phi_NEs = [phi_N - phi_E for phi_N, phi_E in zip(phi_Ns, phi_Es)]
phi_GEs = [phi_G - phi_E for phi_G, phi_E in zip(phi_Gs, phi_Es)]
ax6 = fig.add_subplot(2,4,6, xlim=xlim, ylim=[-100, 20])
plt.title(r'membrane potentials')
plt.ylabel(r'mV')
plt.xlabel(r's')
plt.yticks([-90, -70, -50, -30, -10, 10])
plt.plot(phi_NEs, color=c4, linewidth=lw)
plt.plot(phi_GEs, color=c5, linewidth=lw)
ax7 = fig.add_subplot(2,4,7, xlim=xlim, ylim=[-50, 20])
plt.title(r'\% change volume fractions')
plt.ylabel(r'\%')
plt.xlabel(r's')
plt.yticks([-40, -30, -20, -10, 0, 10])
plt.plot(alpha_Ns, color=c4, label=r'neuron', linewidth=lw)
plt.plot(alpha_Gs, color=c5, label=r'glial', linewidth=lw)
plt.plot(alpha_Es, color=c3, label=r'ECS', linewidth=lw)
# add legend
plt.figlegend(bbox_to_anchor=(1.0, 0.85))
# make pretty
ax.axis('off')
plt.subplots_adjust(wspace=0.25)
# define filename
fname_res = path_figs + 'timeplot_%d.png' % Tstop
# save figure to file
fig.savefig(fname_res, bbox_inches='tight')
plt.close()
######################################################################
# set bar width
barWidth = 0.55
# create plot Glutamate
fig = plt.figure(figsize=(14.5*fs, 10*fs))
ax = plt.gca()
ax1 = fig.add_subplot(1,3,1)
types = [r'original', r'AQP4$^{-}$', r'KIR$^{-}$', r'gap junc', r'new $\gamma$s']
spees = [7.43, 7.43, 7.49, 7.88, 5.4]
plt.bar(types, spees, width=barWidth, edgecolor='black', hatch='//')
plt.title(r"CSD wave speed propagation")
plt.xticks([r for r in range(len(spees))], types, rotation=90)
plt.ylim([5, 8])
plt.yticks([5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0])
plt.ylabel(r"mm/min")
ax2 = fig.add_subplot(2,3,2, xlim=xlim, ylim=[0, 150])
plt.title(r'neuron ion concentrations')
plt.ylabel(r'mM')
plt.plot(Na_Ns, color=c0, label=r'Na$^{+}$', linewidth=lw)
plt.plot(K_Ns, color=c1, label=r'K$^{+}$', linewidth=lw)
plt.plot(Cl_Ns, color=c2, label=r'Cl$^{-}$', linewidth=lw)
plt.xlabel(r's')
ax3 = fig.add_subplot(2,3,3, xlim=xlim, ylim=[0, 150])
plt.title(r'glial ion concentrations')
plt.ylabel(r'mM')
plt.xlabel(r's')
plt.plot(Na_Gs, color=c0, linewidth=lw)
plt.plot(K_Gs, color=c1, linewidth=lw)
plt.plot(Cl_Gs, color=c2, linewidth=lw)
ax4 = fig.add_subplot(2,3,5, xlim=xlim, ylim=[8, 11])
plt.title(r'neuron glutamate')
plt.ylabel(r'mM')
plt.xlabel(r's')
plt.plot(Glu_Ns, color=c4, label=r'neuron', linewidth=lw)
ax5 = fig.add_subplot(2,3,6, xlim=xlim, ylim=[0, 1.5])
plt.title(r'glial and ECS glutamate')
plt.ylabel(r'mM')
plt.xlabel(r's')
plt.plot(Glu_Es, color=c3, label=r'ECS', linewidth=lw)
plt.plot(Glu_Gs, color=c5, label=r'glial', linewidth=lw)
# add legend
plt.figlegend(bbox_to_anchor=(1.0, 0.85))
ax.axis('off')
plt.subplots_adjust(hspace=0.3, wspace=0.3, left=0.065)
plt.subplots_adjust(hspace=0.3, wspace=0.3, left=0.065)
# add numbering for the subplots (A, B, C etc)
ax1.text(-0.12, 1.025, r'\textbf{A}', transform=ax1.transAxes)
letters = [ r'\textbf{B}', r'\textbf{C}', r'\textbf{D}', r'\textbf{E}']
for num, ax in enumerate([ax2, ax3, ax4, ax5]):
ax.text(-0.12, 1.06, letters[num], transform=ax.transAxes)
# define filename
fname_res = path_figs + 'timeplot_wavespeed.png'
# save figure to file
fig.savefig(fname_res, bbox_inches='tight')
plt.close()
return
def make_spaceplot(self, path_figs, n):
""" plot ECS concentrations, phi and % change volume fraction at t=n """
# get data
alpha_N = self.read_from_file(n, 0)
alpha_G = self.read_from_file(n, 1)
Na_N = self.read_from_file(n, 2)
Na_G = self.read_from_file(n, 3)
Na_E = self.read_from_file(n, 4)
K_N = self.read_from_file(n, 5)
K_G = self.read_from_file(n, 6)
K_E = self.read_from_file(n, 7)
Cl_N = self.read_from_file(n, 8)
Cl_G = self.read_from_file(n, 9)
Cl_E = self.read_from_file(n, 10)
Glu_N = self.read_from_file(n, 11)
Glu_G = self.read_from_file(n, 12)
Glu_E = self.read_from_file(n, 13)
# concert to mV
phi_N = self.read_from_file(n, 14, scale=1.0e3)
phi_G = self.read_from_file(n, 15, scale=1.0e3)
phi_E = self.read_from_file(n, 16, scale=1.0e3)
phi_NE = self.project_to_function_space(phi_N - phi_E)
phi_GE = self.project_to_function_space(phi_G - phi_E)
# calculate extracellular volume fraction
u_alpha_E = 1.0 - alpha_N - alpha_G
alpha_E = self.project_to_function_space(u_alpha_E)
alpha_N_init = float(self.problem.alpha_N_init)
alpha_G_init = float(self.problem.alpha_G_init)
alpha_E_init = 1.0 - alpha_N_init - alpha_G_init
# calculate charge in volume fractions (alpha) in %
u_alpha_N_diff = (alpha_N - alpha_N_init)/alpha_N_init*100
u_alpha_G_diff = (alpha_G - alpha_G_init)/alpha_G_init*100
u_alpha_E_diff = (alpha_E - alpha_E_init)/alpha_E_init*100
alpha_N_diff = self.project_to_function_space(u_alpha_N_diff)
alpha_G_diff = self.project_to_function_space(u_alpha_G_diff)
alpha_E_diff = self.project_to_function_space(u_alpha_E_diff)
print("phi_N", max(phi_N.vector().get_local()))
print("phi_G", max(phi_G.vector().get_local()))
dx = 0.01/8000.*1000
g_vec = phi_N.compute_vertex_values()
index_max = max(range(len(g_vec)), key=g_vec.__getitem__)
point_of_wf = dx*index_max
print("point_of_wf", point_of_wf)
# range of x values
xlim = [0.0, 10.0]
# create plot
fig = plt.figure(figsize=(19.5*fs, 5*fs))
ax = plt.gca()
ax1 = fig.add_subplot(1,4,1, xlim=xlim, ylim=[0, 1.5])
plt.ylabel(r'[Glu$]_e$ (mM)', fontsize=fosi)
plt.xlabel(r'x (mm)', fontsize=fosi)
plt.xticks([0, 2.5, 5, 7.5, 10])
plt.yticks([0.25, 0.5, 0.75, 1.0, 1.25])
plot(Glu_E, label=r'Glu', linewidth=lw)
ax2 = fig.add_subplot(1,4,2, xlim=xlim, ylim=[0.0, 150])
plt.ylabel(r'[k$]_e$ (mM)', fontsize=fosi)
plt.xlabel(r'x (mm)', fontsize=fosi)
plt.xticks([0, 2.5, 5, 7.5, 10])
plt.yticks([20, 40, 60, 80, 100, 120, 140])
plot(Na_E, color=c0, label=r'Na$^+$', linewidth=lw)
plot(K_E, color=c1, label=r'K$^+$',linewidth=lw)
plot(Cl_E, color=c2, label=r'Cl$^-$',linewidth=lw)
ax3 = fig.add_subplot(1,4,3, xlim=xlim, ylim=[-100, 20])
plt.ylabel(r'$\phi$ (mV)', fontsize=fosi)
plt.xlabel(r'x (mm)', fontsize=fosi)
plt.xticks([0, 2.5, 5, 7.5, 10])
plt.yticks([-90, -70, -50, -30, -10, 10])
plot(phi_E, color=c3, label=r'ECS', linewidth=lw)
plot(phi_N, color=c4, label=r'neuron', linewidth=lw)
plot(phi_G, color=c5, label=r'glial', linewidth=lw)
ax4 = fig.add_subplot(1,4,4, xlim=xlim, ylim=[-50, 20])
plt.ylabel(r'$\Delta \alpha $ (\%)', fontsize=fosi)
plt.xlabel(r'x (mm)', fontsize=fosi)
plt.xticks([0, 2.5, 5, 7.5, 10])
plt.yticks([-40, -30, -20, -10, 0, 10])
plot(alpha_E_diff, color=c3, linewidth=lw)
plot(alpha_N_diff, color=c4, linewidth=lw)
plot(alpha_G_diff, color=c5, linewidth=lw)
# make pretty
ax.axis('off')
plt.subplots_adjust(wspace=0.35, left=0.1)
# add numbering for the subplots (A, B, C etc)
if path_figs == 'results/figures/stim_KCl/':
letters = [r'\textbf{A}', r'\textbf{B}', r'\textbf{C}', r'\textbf{D}']
plt.figlegend(bbox_to_anchor=(1.0, 0.9)) # add legend
elif path_figs == 'results/figures/stim_pumpsoff/':
letters = [r'\textbf{A}', r'\textbf{B}', r'\textbf{C}', r'\textbf{D}']
plt.figlegend(bbox_to_anchor=(1.0, 0.9)) # add legend
else:
letters = [r'\textbf{A}', r'\textbf{B}', r'\textbf{C}', r'\textbf{D}']
plt.figlegend(bbox_to_anchor=(1.0, 0.9)) # add legend
for num, ax in enumerate([ax1, ax2, ax3, ax4]):
ax.text(-0.12, 1.06, letters[num], transform=ax.transAxes)
# save figure to file
fname_res = path_figs + 'spaceplot_summary'
plt.savefig(fname_res + '.svg', format='svg')
plt.close()
# convert from svg to pdf
os.system('inkscape -D -z --file=' + fname_res + '.svg --export-pdf=' \
+ fname_res + '.pdf --export-latex')
# create plot
fig = plt.figure(figsize=(14.5*fs, 5*fs))
ax = plt.gca()
# subplot number 1 - extracellular concentrations
ax1 = fig.add_subplot(1,3,1, xlim=xlim, ylim=[0, 1.5])
plt.title(r'ECS glutamate')
plt.ylabel(r'mM')
plt.xlabel(r'mm')
plt.xticks([0, 2.5, 5, 7.5, 10])
plot(Glu_E, linewidth=lw)
# subplot number 2 - potentials
ax2 = fig.add_subplot(1,3,2, xlim=xlim, ylim=[8, 11])
plt.title(r'neuron glutamate concentration')
plt.ylabel(r'mV')
plt.xlabel(r'mm')
plt.xticks([0, 2.5, 5, 7.5, 10])
plot(Glu_N, linewidth=lw)
# subplot number 3 - volume fractions
ax3 = fig.add_subplot(1,3,3, xlim=xlim, ylim=[0, 1.5])
plt.title(r'glial glutamate concentration')
plt.ylabel(r'\%')
plt.xlabel(r'mm')
plt.xticks([0, 2.5, 5, 7.5, 10])
plot(Glu_G, linewidth=lw)
# make pretty
ax.axis('off')
plt.tight_layout()
# add numbering for the subplots (A, B, C etc)
letters = [r'\textbf{A}', r'\textbf{B}', r'\textbf{C}']
for num, ax in enumerate([ax1, ax2, ax3]):
ax.text(-0.12, 1.06, letters[num], transform=ax.transAxes)
# save figure to file
fname_res = path_figs + 'spaceplot_glu'
plt.savefig(fname_res + '.svg', format='svg')
plt.close()
# convert from svg to pdf
os.system('inkscape -D -z --file=' + fname_res + '.svg --export-pdf=' \
+ fname_res + '.pdf --export-latex')
# range of x values
xlim = [1.0, 10.0]
# create plot
fig = plt.figure(figsize=(19.5*fs, 5*fs))
ax = plt.gca()
ax1 = fig.add_subplot(1,4,1, xlim=xlim, ylim=[0, 15])
plt.ylabel(r'[Glu$]$ (mM)')
plt.xlabel(r'x (mm)')
plt.xticks([0, 2.5, 5, 7.5, 10])
plot(Glu_N, color=c4, linewidth=lw)
plot(Glu_G, color=c5, linewidth=lw)
ax2 = fig.add_subplot(1,4,2, xlim=xlim, ylim=[0.0, 150])
plt.ylabel(r'[k$]_n$ (mM)')
plt.xlabel(r'x (mm)')
plt.xticks([0, 2.5, 5, 7.5, 10])
plt.yticks([0, 20, 40, 60, 80, 100, 120, 140])
plot(Na_N, color=c0, linewidth=lw)
plot(K_N, color=c1, linewidth=lw)
plot(Cl_N, color=c2, linewidth=lw)
ax3 = fig.add_subplot(1,4,3, xlim=xlim, ylim=[0.0, 150])
plt.ylabel(r'[k$]_g$ (mM)')