-
Notifications
You must be signed in to change notification settings - Fork 2
/
Controller.py
703 lines (633 loc) · 25 KB
/
Controller.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
from Model import Model
import numpy as np
from pathlib import Path
from datetime import datetime
import shelve
from Importers import RichertImport as RI
class Controller():
def __init__(self, path):
"""
Initiates the controller and loads the data from the given path.
Parameters
----------
path : string
Path where the folder containing the data is located.
Returns
-------
None.
"""
self.path = path
importer = RI(path)
self.lambdas_filename, self.delays_filename, self.spectra_filename = importer.get_Data()
def calcDAS(self, preparam, d_limits, l_limits, opt_method):
"""
Calculates the Decay Associated Spectra and outputs the fitted decay
constants tau, the calculated spectra, the residuals and the DAS.
Parameters
----------
preparam : list
A list containing tuples with the lifetime and a boolean stating
if the lifetime will be varied.
d_limits : list with two int/float elements
Lower and upper limits for the delay values.
l_limits : list with two int/float elements
Lower and upper limits for the lambda values.
opt_method : string
The algorithm used by the minimize function.
Returns
-------
tau_fit : list
The fitted decay constants for the DAS.
spec : np.array
The spectra calculated from the fitted tau and the original data.
res : np.array
Residuals from the DAS. The difference between the calculated
and the original spectra.
D_fit : np.array
Matrix D with the fitted values for x.
fit_report : string
Fitting results and other fit statistics created by lmfit.
"""
tau = [tau[0] for tau in preparam]
self.DAS = Model(self.delays_filename, self.spectra_filename,
self.lambdas_filename, d_limits, l_limits, 0, opt_method, None)
self.DAS.M = self.DAS.getM(tau)
tau_fit, fit_report = self.DAS.findTau_fit(preparam, opt_method)
D_fit = self.DAS.calcD_fit()
spec = self.DAS.calcA_fit()
res = self.DAS.calcResiduals()
self.saveResults(0, tau, tau_fit,
l_limits, d_limits, spec, D_fit,
self.DAS.getTauBounds(tau), self.DAS.lambdas,
self.DAS.delays, self.DAS.spectra, fit_report)
return tau_fit, spec, res, D_fit, fit_report
def calcSAS(self, K, preparam, C_0, d_limits, l_limits, model, tau_low, tau_high, opt_method, ivp_method):
"""
Calculated the Species Associated Spectra and outputs the fitted
decay constants tau, the calculated spectra and the residuals.
Parameters
----------
K : np.array
The kinetic matrix for a custom model.
preparam : list
A list containing tuples with the lifetime and a boolean stating
if the lifetime will be varied.
C_0 : list
The list that contains values for C_0 set by the user.
Can be empty.
d_limits : list with two int/float elements
Lower and upper limits for the delay values.
l_limits : list with two int/float elements
Lower and upper limits for the lambda values.
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8, "custom model" or "custom matrix".
tau_low: list
The lower bound for each tau value.
tau_high: list
The upper bound for each tau value.
opt_method : string
The algorithm used by the minimize function.
ivp_method : string
The algorithm used by the initial value problem solver.
Returns
-------
tau_fit : list
The fitted decay constants for the SAS.
spec : np.array
The spectra calculated from the fitted tau and the original data.
res : np.array
Residuals from the SAS. The difference between the calculated
and the original spectra.
D_fit : np.array
Matrix D with the fitted values for tau.
fit_report : string
Fitting results and other fit statistics created by lmfit.
"""
tau = [tau[0] for tau in preparam]
self.SAS = Model(self.delays_filename, self.spectra_filename,
self.lambdas_filename, d_limits, l_limits, model,
opt_method, ivp_method)
if (model == "custom model" or model == "custom matrix"):
M_lin = self.SAS.getM_lin(K)
K, n = self.SAS.getK(M_lin)
self.SAS.setTauBounds(tau_low, tau_high, M_lin)
if model == "custom matrix":
preparam = [(tau, True) for tau in M_lin]
else:
self.SAS.setTauBounds(tau_low, tau_high, tau)
K, n = self.SAS.getK(tau)
self.SAS.setInitialConcentrations(C_0)
self.SAS.solveDiff(ivp_method)
tau_fit, fit_report = self.SAS.findTau_fit(preparam, opt_method)
D_fit = self.SAS.calcD_fit()
spec = self.SAS.calcA_fit()
res = self.SAS.calcResiduals()
self.saveResults(model, tau, tau_fit, l_limits, d_limits, spec, D_fit,
self.SAS.getTauBounds(tau), self.SAS.lambdas,
self.SAS.delays, self.SAS.spectra, fit_report)
return tau_fit, spec, res, D_fit, fit_report
def plot3OrigData(self, wave, time, v_min, v_max,
cont, mul):
"""
Allows the plotting of the original data in a 3-in-1 plot.
If the lists wave and/or time are empty the corresponding subplots
won't be plotted.
Parameters
----------
wave : list
Can contain 0-10 values for the wavelengths which will be plotted
in a subplot of delays against absorption change.
time : list
Can contain values for the delays which will be plotted in a
subplot of absorption change against wavelengths.
v_min : float
Lower limit for the colorbar.
v_max : float
Upper limit for the colorbar.
cont : float
Determines how much contour lines will be shown in the 2D plot.
High values will show more lines.
mul : float
The value by which data will be multiplied.
Returns
-------
None.
"""
if len(list(wave)) <= 0:
if len(list(time)) <= 0:
custom = "2"
else:
custom = "2+3"
elif len(list(time)) <= 0:
custom = "1+2"
else:
custom = "1+2+3"
self.origData.plotCustom(self.origData.spectra, wave, time,
v_min, v_max, custom, cont, mul, self.labels)
def plot3DOrigData(self, v_min, v_max, mul):
"""
Allows the plotting of the original data in a 3D contour plot.
Parameters
----------
v_min : float
Lower limit for the colorbar.
v_max : float
Upper limit for the colorbar.
mul : float
The value by which data will be multiplied.
Returns
-------
None.
"""
self.origData.plot3D(self.origData.spectra, v_min, v_max, mul, self.labels)
def plot3FittedData(self, wave, time, v_min, v_max, model, cont, mul):
"""
Allows the plotting of the fitted data in a 3-in-1 plot.
If the lists wave and/or time are empty the corresponding subplots
won't be plotted.
Parameters
----------
wave : list
Can contain 0-10 values for the wavelengths which will be plotted
in a subplot of delays against absorption change.
time : list
Can contain values for the delays which will be plotted in a
subplot of absorption change against wavelengths.
v_min : float
Lower limit for the colorbar.
v_max : float
Upper limit for the colorbar.
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 or "custom model" or "custom matrix".
cont : float
Determines how much contour lines will be shown in the 2D plot.
High values will show more lines.
mul : float
The value by which data will be multiplied.
Returns
-------
None.
"""
if len(wave) <= 0 or len(wave) > 10:
if len(time) <= 0:
custom = "2"
else:
custom = "2+3"
elif len(time) <= 0:
custom = "1+2"
else:
custom = "1+2+3"
if model == 0:
self.DAS.plotCustom(self.DAS.spec, wave, time,
v_min, v_max, custom, cont, mul, self.labels, add="_GLA")
else:
self.SAS.plotCustom(self.SAS.spec, wave, time,
v_min, v_max, custom, cont, mul, self.labels, add="_GTA")
def plot3DFittedData(self, v_min, v_max, model, mul):
"""
Allows the plotting of the original data in a 3D contour plot.
Parameters
----------
v_min : float
Lower limit for the colorbar.
v_max : float
Upper limit for the colorbar.
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8, "custom model" or "custom matrix.
mul : float
The value by which data will be multiplied.
Returns
-------
None.
"""
if model == 0:
self.DAS.plot3D(self.DAS.spec, v_min, v_max, mul, self.labels, add="_GLA")
else:
self.SAS.plot3D(self.SAS.spec, v_min, v_max, mul, self.labels, add="_GTA")
def createOrigData(self, d_limits, l_limits, opt_method, ivp_method):
"""
Creates an object origData to allow the plotting of the original data.
Parameters
----------
d_limits : list
The upper and lower bound for the delay values.
l_limits : list
The upper and lower value for the wavelenght bounds.
opt_method: str
The name of the optimizer algorithm.
ivp_method: str
The name of the ivp solver algorithm.
Returns
-------
None.
"""
self.origData = Model(self.delays_filename, self.spectra_filename,
self.lambdas_filename, d_limits, l_limits, None,
opt_method, ivp_method)
def plotCustom(self, wave, time, v_min, v_max, model, cont, custom, mul,
add=""):
"""
Allows for the creation of 1-3 subplots in one plot.
Parameters
----------
wave : list
Wavelenghts which should be plotted.
time : list
Delays which should be plotted.
v_min : float
Lower limit for the colorbar.
v_max : float
Upper limit for the colorbar.
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 ,"custom model" or "custom matrix".
cont : float
Determines how much contour lines will be shown in the 2D plot.
High values will show more lines.
custom : string
Describes which subplots will be plotted.
mul : float
The value by which data will be multiplied.
add : string
Addition to the title of the plot. The default is "".
Returns
-------
None.
"""
if model is None:
self.origData.plotCustom(self.origData.spectra, wave, time,
v_min, v_max, custom, cont, mul, self.labels,
add="_" + add)
elif model == 0:
self.DAS.plotCustom(self.DAS.spec, wave, time,
v_min, v_max, custom, cont, mul, self.labels,
add="_GLA" + "_" + add)
else:
self.SAS.plotCustom(self.SAS.spec, wave, time,
v_min, v_max, custom, cont, mul,
add="_GTA" + "_" + add)
def plotSolo(self, wave, time, v_min, v_max, model, cont, solo, mul, add=""):
"""
Allows for the creation of single plots.
Parameters
----------
wave : list
Wavelenghts which should be plotted.
time : list
Delays which should be plotted.
v_min : float
Lower limit for the colorbar.
v_max : float
Upper limit for the colorbar.
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 ,"custom model" or "custom matrix".
cont : float
Determines how much contour lines will be shown in the 2D plot.
High values will show more lines.
solo : string
Describes which soloplot will be plotted.
mul : float
The value by which data will be multiplied.
add : string
Addition to the title of the plot. The default is "".
Returns
-------
None.
"""
if model is None:
self.origData.plotSolo(self.origData.spectra, wave, time,
v_min, v_max, solo, cont, mul, self.labels,
add="_" + add)
elif model == 0:
self.DAS.plotSolo(self.DAS.spec, wave, time,
v_min, v_max, solo, cont, mul, self.labels,
add="_GLA" + "_" + add)
else:
self.SAS.plotSolo(self.SAS.spec, wave, time,
v_min, v_max, solo, cont, mul, self.labels,
add="_GTA" + "_" + add)
def plot1Dresiduals(self, model, mul):
"""
Allows for the plotting of the residuals in a plot of
residuals(wavelenght) against the delays.
Parameters
----------
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 ,"custom model" or "custom matrix".
mul : float
The value by which data will be multiplied.
Returns
-------
None.
"""
ltx = str(mul).count("0")
dot = ""
if mul != 1:
dot = " \cdot " + "10^" + str(ltx) + "$"
if model == 0:
self.DAS.plotData(self.DAS.delays, self.DAS.residuals.T,
self.labels[1], self.labels[2] + dot,
add="_GLA_Residuals_")
else:
self.SAS.plotData(self.SAS.delays, self.SAS.residuals.T,
self.labels[1], self.labels[2] + dot,
add="_GTA_Residuals_")
def plot2Dresiduals(self, v_min, v_max, model, cont, mul):
"""
Allows for the ploting of the residuals in a 2D plot.
Parameters
----------
v_min : float
Lower limit for the colorbar.
v_max : float
Upper limit for the colorbar.
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 ,"custom model" or "custom matrix".
cont : float
Determines how much contour lines will be shown in the 2D plot.
High values will show more lines.
mul : float
The value by which data will be multiplied.
Returns
-------
fig: matplotlib.pyplot.figure
The figure containing the plot.
"""
if model == 0:
self.DAS.plotHeat([], [], None, None, self.DAS.residuals, cont, mul,
self.labels, add="_GLA_Residuals")
else:
self.SAS.plotHeat([], [], None, None, self.SAS.residuals, cont, mul,
self.labels, add="_GTA_Residuals")
def plotKinetics(self, model):
"""
This method will plot the concentration against the time for the DAS
or the SAS.
Parameters
----------
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 ,"custom model" or "custom matrix".
Returns
-------
fig: matplotlib.pyplot.figure
The figure containing the plot.
"""
if model == 0:
self.DAS.plotData(self.DAS.delays, self.DAS.M.T, self.labels[1],
"concentration", label=None, add="_GLA_kin")
else:
self.SAS.plotData(self.SAS.delays, self.SAS.M.T, self.labels[1],
"concentration", label=None, add="_GTA_kin")
def plotDAS(self, model, tau, mul):
"""
Allows the plotting of the DAS or SAS with the indicated tau_fit
values.
Parameters
----------
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 ,"custom model" or "custom matrix".
tau_fit : list
The decay constants for the DAS or SAS.
Returns
-------
None.
"""
ltx = str(mul).count("0")
dot = ""
if mul != 1:
dot = f" $\cdot 10^{ltx}$"
unit = self.labels[1].split("/")[1]
for i, t in enumerate(tau):
if t < 1:
tau[i] = round(t, 3)
elif t < 10 and t > 1:
tau[i] = round(t, 2)
elif t < 100 and t > 10:
tau[i] = round(t, 1)
else:
tau[i] = round(t)
if model == 0:
label = []
for ind, tau in enumerate(tau):
label.append(f"$\\tau_{ind}=$ {tau}{unit}")
self.DAS.plotData(self.DAS.lambdas, self.DAS.D_fit,
self.labels[0], self.labels[2] + dot,
label=label, add="_DAS")
elif (model == "custom model" or model == "custom matrix"):
custom_tau = list(self.SAS.getM_lin(np.array(tau)))
label = []
for ind, tau in enumerate(tau):
label.append(f"$\\tau_{ind}=$ {custom_tau}{unit}")
self.SAS.plotData(self.SAS.lambdas, self.SAS.D_fit,
self.labels[0], self.labels[2] + dot,
label=label, add="_SAS")
else:
label = []
for ind, tau in enumerate(tau):
label.append(f"$\\tau_{ind}=$ {tau}{unit}")
if model == 2:
label.append("inf")
self.SAS.plotData(self.SAS.lambdas, self.SAS.D_fit,
self.labels[0], self.labels[2] + dot,
label=label, add="_SAS")
def saveResults(self, model, tau_start, tau_fit, l_limits, d_limits, A_fit,
D_fit, bounds, lambdas, delays, spectra, fit_report):
"""
Saves the results of the DAS or SAS at the end of the optimizing in a
.txt file.
Parameters
----------
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 ,"custom model" or "custom matrix".
tau_start : list, np.array
An array containing the start values for tau.
tau_fit : list, np.array
An array containing the fitted tau values.
l_limits : list with two int/float elements
Lower and upper limits for the lambda values.
d_limits : list with two int/float elements
Lower and upper limits for the delay values.
A_fit : np.array
The reconstructed data matrix for the values of tau_fit and x_fix,
if DAS.
D_fit : np.array
Matrix D with the fitted values for tau.
Returns
-------
None.
"""
for i, n in enumerate(tau_fit):
tau_fit[i] = round(n, 2)
time_unit = self.labels[1].split("/")[1]
x_axis_unit = self.labels[0].split("/")[1]
if model == 0:
path = self.DAS.path
name = self.DAS.name + "_GLA"
txt = name + "_results.txt"
else:
path = self.SAS.path
name = self.SAS.name + "_GTA"
txt = name + "_results.txt"
myfile = Path(path + txt)
myfile.touch(exist_ok=True)
f = open(myfile, "w")
if (model != "custom model" or model != "custom matrix"):
k_fit = 1 / np.array(tau_fit)
else:
ones = np.full(tau_fit.shape, 1)
k_fit = np.divide(ones, tau_fit, out=np.zeros_like(tau_fit), where=tau_fit != 0)
now = datetime.now()
dt_string = now.strftime("%d.%m.%Y %H:%M:%S")
f.write(
f"{dt_string}\n{name}\nSolver: scipy.optimize.minimize\n"
f"Model: {model}\nStarting Parameters: {tau_start}\n"
f"Bounds: {bounds}\nWavelength/Field range: {l_limits[0]} - {l_limits[1]} {x_axis_unit}\n"
f"delay range: {d_limits[0]} - {d_limits[1]} {time_unit}\n\n"
f"Time constants / {time_unit}: {tau_fit}\n"
f"Rate constants / {time_unit}^-1: {k_fit}\n\n"
f"lmfit fit_report:\n\n{fit_report}\n\n"
f"All results and plots can be found here:\n\n{path}"
)
np.savetxt(path + name + "_A_fit.txt", A_fit)
if model == 0:
np.savetxt(path + name + "_DAS.txt", D_fit)
else:
np.savetxt(path + name + "_SAS.txt", D_fit)
np.savetxt(path + name + "_limited_lambda.txt", lambdas)
np.savetxt(path + name + "_limited_delays.txt", delays)
np.savetxt(path + name + "_limited_spectra.txt", spectra)
f.close()
def getResults(self, model):
"""
Reads the results txt file in a string variable.
Parameters
----------
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 ,"custom model" or "custom matrix".
Returns
-------
text : string
A string with the content of the results file.
"""
if model == 0:
path = self.DAS.path
name = self.DAS.name + "_GLA"
txt = name + "_results.txt"
else:
path = self.SAS.path
name = self.SAS.name + "_GTA"
txt = name + "_results.txt"
with open(path + txt) as f:
text = f.read()
return text
def pickleData(self, dict_):
"""
This method saves the given data in the shelve.
Keyword arguments must be given as key=value.
Parameters
----------
model : int/string
Describes the desired model. 0 for the GLA. For GTA it can be a
number 1-8 ,"custom model" or "custom matrix".
d_limits : list with two int/float elements
Lower and upper limits for the delay values.
l_limits : list with two int/float elements
Lower and upper limits for the lambda values.
cont : float
Determines how much contour lines will be shown in the 2D plot.
High values will show more lines.
time : list
Can contain values for the delays which will be plotted in a
subplot of absorption change against wavelengths.
wave : list
Can contain 0-10 values for the wavelengths which will be plotted
in a subplot of delays against absorption change.
tau : list/np.array
The decay constants tau for a model 1-10 or a matrix Tau for a
"custom" model.
C_0 : list
The list that contains values for C_0 set by the user.
The default is None.
Returns
-------
None.
"""
path = self.path + "/"
temp = self.delays_filename[::-1]
temp = temp.index("/")
name = self.delays_filename[-temp:-11]
txt = name + "_input_backup"
s = shelve.open(path + txt, writeback=True)
s.clear()
for key, value in dict_.items():
s[key] = value
s.close()
def getPickle(self):
"""
Transforms the shelve in a dictionary which can be accessed to obtain
the saved information.
Returns
-------
shelf : dict
A dictionary containing parameters chosen in the last run of the
programm.
"""
path = self.path + "/"
temp = self.delays_filename[::-1]
temp = temp.index("/")
name = self.delays_filename[-temp:-11]
txt = name + "_input_backup"
s = shelve.open(path + txt, writeback=False)
shelf = dict(s).copy()
s.close()
return shelf