-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_plot.py
403 lines (315 loc) · 16.1 KB
/
model_plot.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
"""
This file manages the plotting after solving the model.
"""
import jax.numpy as jnp
import matplotlib.pyplot as plt
import numpy as np
import os
###### Plotting overview of results for comparison #######
def LV_PV_Loop(axs, Vlv, Plv):
""" Plots the LV-PV Loop.
Args:
- axs: [array] -> axes for the plotting
- Vlv: [array] -> volume of the left ventricle through time
- Plv: [array] -> pressure of the left ventricle through time
Return:
X.
"""
# Plot pV-loop
axs[0, 0].title.set_text('PV Loops')
axs[0, 0].plot(Vlv, Plv)
axs[0, 0].set_title('LV PV Loop')
axs[0, 0].set_xlabel("V [ml]")
axs[0, 0].set_ylabel("P [mmHg]")
def Pao_Plv(axs, T, Pao, Plv, Pla, Pra, Prv):
""" Plots the aortic and left ventricle pressure.
Args:
- axs: [array] -> axes for the plotting
- T: [array] -> x axis of the plot/time
- Pao: [array] -> pressure of the aorta through time
- Plv: [array] -> pressure of the left ventricle through time
Return:
X.
"""
# Plot pressue of aorta and left ventricle
axs[0, 1].title.set_text('AO and LV Pressures [mmHg]')
axs[0, 1].plot(T, Pao, T, Plv)
axs[0, 1].legend(['Ao','LV', 'LA', 'RA', 'RV'],loc='upper left')
axs[0, 1].set_xlabel("t [s]")
axs[0, 1].set_ylabel("P [mmHg]")
def Q_Pulm(axs, T, Qpa, Qpp, Qps):
""" Plots the pulmonary flows.
Args:
- axs: [array] -> axes for the plotting
- T: [array] -> x axis of the plot/time
- Qpa: [array] -> flow of the pulmonary artery through time
- Qpart: [array] -> flow of pulmonary arteries through time
Return:
X.
"""
# Plot flows of pulmonary arteries
axs[1, 0].title.set_text('Pulmonary flows')
axs[1, 0].plot(T, Qpa, T, Qpp, T, Qps)
axs[1, 0].legend(['artery','periph. vessels', 'shunt'],loc='upper left')
axs[1, 0].set_xlabel("t [s]")
axs[1, 0].set_ylabel("Q [mL/s]")
def mean_Flows(axs, Qs, Vs):
""" Plots the means flows of the model.
Args:
- axs: [array] -> axes for the plotting
- Qs: dict() -> the flows of the simPostTree
- Vs: [array] -> the volumes of the simPostTree (to calculate the EF)
Return:
X.
"""
# Create bar plot with mean flows of certain compartments
CompartmentsPlot = {'Qlv': 'CO', 'Qsart': 'sart', 'Qsvn': 'svn', 'Qra': 'ra', 'Qrv': 'rv', 'Qecmopump': 'EPump', 'Qcrrtpump': 'CPump'}
meanFlows= {k: jnp.mean(Qs[k]*60/1000) for k in CompartmentsPlot.keys()}
EDV=jnp.max(Vs['Vlv'])
ESV=jnp.min(Vs['Vlv'])
EF=str(jnp.round(100*(EDV-ESV)/EDV))
axs[1, 1].title.set_text('Mean cardiac output')
axs[1, 1].bar(list(CompartmentsPlot.values()), jnp.array(list(meanFlows.values())))
axs[1, 1].text(0, 1.75, "EF: " + EF + "%", size=12, ha='center', va='center', weight='bold')
axs[1, 1].set_ylabel("Q [l/min]")
def plot_results(initTree, simPostTree):
""" Manages the plotting for the model. Plotting based on the ListOfPlots.txt file.
Args:
- initTree: [pytree] -> Initial parameters of the model
- simPostTree: [pytree] -> Contains solution keys for post processing
Return:
- /
"""
plt.rcParams['mathtext.fontset'] = 'stix'
plt.rcParams['font.family'] = 'STIXGeneral'
plt.rcParams['font.size'] = 16
cm = 1/2.54
# Check if folder Plots exist and if not create a new one
if not os.path.exists('Plots'):
os.makedirs('Plots')
T=initTree.timeTree['T']
paramsModel=initTree.paramsModel
compartments=['ao', 'sart', 'svn', 'ra', 'rv', 'la', 'lv', 'pas', 'part', 'pvn']
for c in compartments:
plt.plot(T, simPostTree.Pressures['P'+c], label=c+' pressure')
plt.xlabel('Time [s]')
plt.ylabel('Pressure [mmHg]')
plt.legend(fontsize=10, bbox_to_anchor=(1.04, 1), loc="upper left")
plt.tight_layout()
plt.savefig('Plots/AllPressures.png')
listOfPlots = np.genfromtxt('ListOfPlots.txt',dtype=str)
for plot in listOfPlots:
match plot:
case 'heartvolumes':
fig, axs = plt.subplots(1, 2, figsize=(12, 6))
axs[0].plot(T, simPostTree.Volumes['Vlv'], label='Vlv')
axs[0].plot(T, simPostTree.Volumes['Vla'], label='Vla')
axs[0].legend()
axs[0].set_title('Left Ventricle')
axs[0].set_xlabel('Time in s')
axs[0].set_ylabel('Volume in mL')
axs[0].set_ylim([0, 140])
axs[1].plot(T, simPostTree.Volumes['Vrv'], label='Vrv')
axs[1].plot(T, simPostTree.Volumes['Vra'], label='Vra')
axs[1].legend()
axs[1].set_title('Right Ventricle')
axs[1].set_xlabel('Time in s')
axs[1].set_ylabel('Volume in mL')
axs[1].set_ylim([0, 140])
print("\nEDVLV: {:.2f} mL".format(jnp.max(simPostTree.Volumes['Vlv'])))
print("ESVLV: {:.2f} mL".format(jnp.min(simPostTree.Volumes['Vlv'])))
print("EDVRV: {:.2f} mL".format(jnp.max(simPostTree.Volumes['Vrv'])))
print("ESVRV: {:.2f} mL".format(jnp.min(simPostTree.Volumes['Vrv'])))
plt.savefig('Plots/heartvolumes.png')
case 'Pressures':
plt.figure()
plt.title('CVS pressures evolution')
compartments=['ao', 'sart', 'svn', 'ra', 'rv', 'la', 'lv', 'pas', 'part', 'pvn']
n=1
for c in compartments:
plt.plot(n*T, simPostTree.Pressures['P'+c], label=c+' pressure')
n=n+1
plt.xlabel('Time in s')
plt.ylabel('Pressure in mmHg')
plt.legend(fontsize=10, bbox_to_anchor=(1.04, 1), loc="upper left")
plt.tight_layout()
plt.savefig('Plots/CVS_pressures.png')
if initTree.ECMO['status'] == 1:
plt.figure()
plt.title('ECMO pressures')
ecmocompartments=['ecmodrain', 'ecmotudp', 'ecmopump', 'ecmooxy', 'ecmotuor', 'ecmoreturn']
for cecmo in ecmocompartments:
plt.plot(T, (simPostTree.Pressures['P'+cecmo]), label=cecmo)
plt.xlabel('Time in s')
plt.ylabel('Pressure in mmHg')
plt.legend(fontsize=10, bbox_to_anchor=(1.04, 1), loc="upper left")
plt.tight_layout()
plt.savefig('Plots/ECMO_pressure.png')
case 'CVSpressuresMean':
plt.figure()
plt.title('CVS pressures Mean evolution')
compartments=['ao', 'sart', 'svn', 'ra', 'rv', 'la', 'lv', 'pas', 'part', 'pvn']
n=1
for c in compartments:
plt.plot(n*T, jnp.mean(simPostTree.Pressures['P'+c])*jnp.ones(len(T)), label=c+' pressure')
n=n+1
plt.xlabel('Time [s]')
plt.ylabel('Pressure [mmHg]')
plt.legend()
plt.savefig('Plots/CVSpressuresMean.png')
case 'pvloops':
fig, axs = plt.subplots(1, 2, figsize=(12, 6))
# LV pV Loop
axs[0].plot(simPostTree.Volumes['Vlv'], simPostTree.Pressures['Plv'])
axs[0].set_title('LV pV Loop')
axs[0].set_xlabel('V in mL')
axs[0].set_ylabel('P in mmHg')
# RV pV Loop
axs[1].plot(simPostTree.Volumes['Vrv'], simPostTree.Pressures['Prv'])
axs[1].set_title('RV pV Loop')
axs[1].set_xlabel('V in mL')
axs[1].set_ylabel('P in mmHg')
plt.tight_layout()
plt.savefig('Plots/pvloops.png')
case 'paoplv':
plt.figure()
plt.title('Left ventricle and aortic pressures')
plt.plot(T, simPostTree.Pressures['Pao'], label='Aortic')
plt.plot(T, simPostTree.Pressures['Plv'], label='Left ventricle')
plt.legend()
plt.xlabel('Time [s]')
plt.ylabel('Pressure [mmHg]')
plt.savefig('Plots/paoplv.png')
case 'qpulm':
# TODO: pmc not available anymore!
plt.figure()
plt.title('Pulmonary flows')
plt.plot(T, simPostTree.Flows['Qpart'], label='Pulmonary artery')
plt.plot(T, simPostTree.Flows['Qpas'], label='Pulmonary aortic sinus')
plt.legend()
plt.xlabel('Time [s]')
plt.ylabel('Flow [mL/s]')
plt.savefig('Plots/qpulm.png')
case 'meanflows':
plt.figure()
CompartmentsPlot = {'Qlv': 'CO', 'Qao': 'ao', 'Qsart': 'sart', 'Qsvn': 'svn', 'Qra': 'ra', 'Qrv': 'rv', 'Qecmopump': 'EPump', 'Qcrrtpump': 'CPump'}
meanFlows= {k: jnp.mean(simPostTree.Flows[k]*60/1000) for k in CompartmentsPlot.keys()}
EDV=jnp.max(simPostTree.Volumes['Vlv'])
ESV=jnp.min(simPostTree.Volumes['Vlv'])
EF=str(jnp.round(100*(EDV-ESV)/EDV))
plt.title('Mean cardiac output')
plt.bar(list(CompartmentsPlot.values()), jnp.array(list(meanFlows.values())))
plt.text(0, 1.75, "EF: " + EF + "%", size=12, ha='center', va='center', weight='bold')
plt.ylabel("Q [l/min]")
plt.savefig('Plots/meanflows.png')
case 'checkPressures':
print('\nPressure right atrium should be 2: ', jnp.round(jnp.mean(simPostTree.Pressures['Pra'])))
print('Pressure before part should be 14: ', jnp.round(jnp.mean(simPostTree.Pressures['Ppart'])))
print('Pressure left atrium should be 5 : ', jnp.round(jnp.mean(simPostTree.Pressures['Pla'])))
print('Pressure after left ventricle should be 100: ', jnp.round(jnp.mean(simPostTree.Pressures['Pao'])))
print('Pressure before sart should be 30: ', jnp.round(jnp.mean(simPostTree.Pressures['Psart'])))
print('Pressure after sart/before svn should be 10: ', jnp.round(jnp.mean(simPostTree.Pressures['Psvn'])))
case 'LVAD':
fig, axs = plt.subplots(2, 2,figsize=(15,15))
axs[0, 0].title.set_text('LV-pV Loop')
axs[0, 0].plot(simPostTree.Volumes['Vlv'], simPostTree.Pressures['Plv'])
axs[0, 0].set_xlim(40, 150)
axs[0, 0].set_ylim(0, 140)
axs[0, 0].set_xlabel("Volume in mL")
axs[0, 0].set_ylabel("Pressure in mmHg")
axs[0, 1].plot(T, simPostTree.Pressures['Pao'], label='Aorta')
axs[0, 1].plot(T, simPostTree.Pressures['Plv'], label='LV')
axs[0, 1].set_ylim(0, 140)
axs[0, 1].set_xlabel("Time in s")
axs[0, 1].set_ylabel("Pressure in mmHg")
axs[0, 1].legend()
axs[1, 0].plot(T, simPostTree.Flows['Qlv']/16.67, label='LV')
axs[1, 0].plot(T, simPostTree.Flows['Qlvadpump']/16.67, label='LVAD')
axs[1, 0].set_xlabel("Time in s")
axs[1, 0].set_ylabel("Flow in L/min")
axs[1, 0].legend()
CompartmentsPlot = {'Qlv': 'CO', 'Qlvadpump': 'LVAD'}
meanFlows= {k: jnp.mean(simPostTree.Flows[k]*60/1000) for k in CompartmentsPlot.keys()}
EDV=jnp.max(simPostTree.Volumes['Vlv'])
ESV=jnp.min(simPostTree.Volumes['Vlv'])
EF=str(jnp.round(100*(EDV-ESV)/EDV))
axs[1, 1].title.set_text('Mean Cardiac Output')
axs[1, 1].bar(list(CompartmentsPlot.values()), jnp.array(list(meanFlows.values())))
axs[1, 1].text(0, 1.75, "EF: " + EF + "%", size=12, ha='center', va='center', weight='bold')
axs[1, 1].set_ylabel("Flow in L/min")
plt.savefig('Plots/LVAD.png')
case 'Flows':
plt.figure()
print('\nCVS: Flow in ...')
for c in compartments:
plt.plot(T, simPostTree.Flows['Q'+c]*0.06, label=c+' flow')
print(c, 'value =', \
jnp.round(jnp.mean(simPostTree.Flows['Q'+c])*0.06, decimals = 4) \
, 'L/min')
plt.xlabel('Time in s')
plt.ylabel('Flow in L/min')
plt.legend(fontsize=10, bbox_to_anchor=(1.04, 1), loc="upper left")
plt.grid(True)
plt.tight_layout()
plt.savefig('Plots/CVS_flows.png')
if initTree.ECMO['status'] == 1:
plt.figure()
ecmocompartments=['ecmodrain', 'ecmotudp', 'ecmopump', 'ecmooxy', 'ecmotuor', 'ecmoreturn']
print('\nECMO: Flow in ...')
for cecmo in ecmocompartments:
plt.plot(T, (simPostTree.Flows['Q'+cecmo])*0.06, label=cecmo)
print(cecmo, 'value =', \
jnp.round(jnp.mean(simPostTree.Flows['Q'+cecmo])*0.06, decimals = 4) \
, 'L/min')
plt.xlabel('Time in s')
plt.ylabel('Flow in L/min')
plt.grid(True)
plt.legend(fontsize=10, bbox_to_anchor=(1.04, 1), loc="upper left")
plt.tight_layout()
plt.savefig('Plots/ECMO_flows.png')
if initTree.CRRT['status'] == 1:
plt.figure()
compartmentscrrt=['crrttuin', 'crrttupf', 'crrtfil', 'crrttuout']
print('\nCRRT: Flow in ...')
for ccrrt in compartmentscrrt:
plt.plot(T, (simPostTree.Flows['Q'+ccrrt])*0.06, label=ccrrt+' flow')
print(ccrrt, 'value =', \
jnp.round(jnp.mean(simPostTree.Flows['Q'+ccrrt])*0.06, decimals = 4) \
, 'L/min')
plt.xlabel('Time in s')
plt.ylabel('Flow in L/min')
plt.legend(fontsize=10, bbox_to_anchor=(1.04, 1), loc="upper left")
plt.grid(True)
plt.tight_layout()
plt.savefig('Plots/CRRT_flows.png')
if initTree.LVAD['status'] == 1:
plt.figure()
compartmentslvad=['lvadpump']
print('\nLVAD: Flow in ...')
for clvad in compartmentslvad:
plt.plot(T, (simPostTree.Flows['Q'+clvad])*0.06, label=clvad+' flow')
print(clvad, 'value =', \
jnp.round(jnp.mean(simPostTree.Flows['Q'+clvad])*0.06, decimals = 4) \
, 'L/min')
plt.xlabel('Time in s')
plt.ylabel('Flow in L/min')
plt.grid(True)
plt.legend(fontsize=10, bbox_to_anchor=(1.04, 1), loc="upper left")
plt.tight_layout()
plt.savefig('Plots/LVAD_flows.png')
case 'valveflows':
plt.figure(figsize=(12*cm, 8*cm))
compartments=['la', 'lv', 'rv', 'ra']
print('\nValves: Flow out of ...')
for c in compartments:
plt.plot(T, simPostTree.Flows['Q'+c]*0.06, label=c+' flow')
print(c, 'value =', \
jnp.round(jnp.mean(simPostTree.Flows['Q'+c])*0.06, decimals = 4), \
'L/min')
plt.xlabel('Time in s')
plt.ylabel('Flow in L/min)')
plt.grid(True)
plt.ylim(0, 80)
plt.legend(fontsize=10, bbox_to_anchor=(1.04, 1), loc="upper left")
plt.tight_layout()
plt.savefig('Plots/Valve_flows.png')