-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ferret.py
1329 lines (1131 loc) · 58.4 KB
/
Ferret.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
"""This module of classes contains the code for building the GUI of the
FERRET Model Fitting application.
The GUI was built using PyQT5.
How to Use.
-----------
The FERRET-Model-Fitting application allows the user to analyse
data that varies over time by fitting a model to one of the time curves.
The FERRET-Model-Fitting application provides the following functionality:
1. Load a python script that describes the models to be used in the
analysis.
2. Then load a CSV file of MR Signal/time data.
The first column must contain time data in seconds.
remaining columns of data must contain MR signal data
at the time points in the time column.
There must be at least 2 columns of signal data.
There is no upper limit on the number of columns of signal data.
Each time a CSV file is loaded, the screen is reset to its initial state.
3. Select the variables to be plotted on a line graph.
4. The user can then select a model they would like to fit one of the curves
displayed in 3.
5. The selected model is used to create a time series
based on default values for the models input parameters,
which is also plotted on the above axes.
6. The default model parameters are displayed on the form
and the user may vary them and observe the effect on
the curve generated in step 5.
7. Clicking the 'Reset' button resets the model input parameters to their default values.
9. Clicking the 'Fit Model' button, fits the model to one of the curves
and the resulting values of the model input parameters are displayed on
the screen together with their 95% confidence limits. The variable, whose curve is
used to fit the model to, is specified in the definition of the model.
10. By clicking the 'Save plot data to CSV file' button the data plotted on the screen is saved
to a CSV file - one column for each plot and a column for time.
A file dialog box is displayed allowing the user to select a location
and enter a file name.
11. By clicking the 'Save Report in PDF Format', current state of the model fitting session
is saved in PDF format. This includes a image of the plot, name of the model, name of the
data file and the values of the model input parameters.
If curve fitting has been carried out and the values of the model input parameters
have not been manually adjusted, then the report
will contain the 95% confidence limits of the
model input parameter values arrived at by curve fitting.
12. While this application is running, events & function
calls with data where appropriate are logged to a file called Ferret.log,
stored at the same location as the source code or executable.
This file can be used as a debugging aid.
When a new instance of the application is started,
FERRET.log from the last session will be deleted and a new log file started.
Application Module Structure.
---------------------------
The code in FERRET.py defines the GUI, built using PyQT5.
The styleSheet.py module contains style instructions using CSS
notation for each control/widget.
GUI Structure
--------------
The GUI is based on the QWidget class.
The GUI contains a single form. Controls are arranged in two
vertical columns on this form using Vertical Layout widgets.
Consequently, a horizontal layout control in placed on this form.
Within this horizontal layout are placed the 2 vertical layout controls.
The left-hand side vertical layout holds controls for the selection of a model
to analyse the data and the selection & input of data. The module FerretLoadData.py
contains the functionality for the creation and operation of the load model
and load data file buttons.
The GUI is built using the definition of the selected model in this module.
At the bottom of the left-handside vertical layout are 3 buttons for the
export of data from Ferret: Save plot data to a CSV file, Save plot data to
DICOM (currently not implemented) and Create a PDF report of the current analysis.
The code in module FerretExportData.py creates these buttons & implements their functionality.
The right-hand side vertical layout holds a canvas widget for the
graphical display of the data using Matplotlib. The module FerretPlotData.py contains
the functionality for the display of the Matplotlib graph as well as the logic
to run models and perform curve fitting.
The appearance of the GUI is controlled by the CSS commands in styleSheet.py
Reading Data into Ferret.
----------------------------------
A Python list of model objects can be passed into Ferret when a Ferret object
is created from the Ferret class. If a list of model objects is not presented
to the Ferret class during object instantiation, then a Load Model Library is
displayed on the GUI, which the user can use to browse to a Python script containing
the definition of thier model list.
Clicking the 'Load Data File' button executes the LoadDataFile function in FerretLoadData.py.
The function LoadDataFile loads the contents of a CSV file
containing time and MR signal data into a dictionary of lists.
The header label of each column of data is taken as a dictionary key and the
corresponding value is a list of MR signals for that data type
(or times when the key is 'time').
The following validation is applied to the data file:
-The CSV file must contain at least 3 columns of data separated by commas.
-The first column in the CSV file must contain time data.
-The header of the time column must contain the word 'time'.
A list of keys is created and displayed in a drop down list for each model variable.
Model variables are described in the definition of each model.
As the time data is read, it is divided by 60 in order to convert it into minutes.
"""
__author__ = "Steve Shillitoe"
__version__ = "1.0"
__date__ = "Date: 2018/12/12"
import sys
import os
import pathlib
import numpy as np
import logging
from typing import List
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QPushButton, QDoubleSpinBox,\
QVBoxLayout, QHBoxLayout, QGroupBox, QComboBox, QLabel, \
QMessageBox, QFileDialog, QCheckBox, QSpacerItem, \
QGridLayout, QWidget, QMainWindow
########################################
## CONSTANTS ##
########################################
IMAGE_NAME = 'model.png' #Used to save an image of the plot to disc
#Determine the location of this file on the fly
pathToThisFile = pathlib.Path(__file__).parent.absolute()
#The following 5 lines of code are necessary for Ferret to access the user's
#mathematical models
defaultPathModelLibrary = os.path.join(pathToThisFile, "Developer\ModelLibrary")
defaultPathModelLibrarySupportModules = os.path.join(pathToThisFile, "Developer\ModelLibrary\SupportModules")
sys.path.append(defaultPathModelLibrary)
sys.path.append(defaultPathModelLibrarySupportModules)
sys.path.append(os.path.join(pathToThisFile, "Core"))
#Image Files
FERRET_LOGO = os.path.join(pathToThisFile, 'images\FERRET_LOGO.png')
#Create and configure the logger
#First delete the previous log file if there is one
LOG_FILE_NAME = "Ferret.log"
LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename=LOG_FILE_NAME,
level=logging.INFO,
format=LOG_FORMAT)
logger = logging.getLogger(__name__)
#CoreModules folder renamed to Core because of name clash
#with folder of the same name in Weasel
#Note relative import from, using .Core
if __name__ == '__main__':
#run stand alone
from Core.PDFWriter import PDF
from Core.ExcelWriter import ExcelWriter
from Core.FerretLoadData import FerretLoadData
from Core.FerretExportData import FerretExportData
from Core.FerretPlotData import FerretPlotData
from Core.FerretConstants import FerretConstants
else:
#run from Weasel
from Ferret.Core.PDFWriter import PDF
from Ferret.Core.ExcelWriter import ExcelWriter
from Ferret.Core.FerretLoadData import FerretLoadData
from Ferret.Core.FerretExportData import FerretExportData
from Ferret.Core.FerretPlotData import FerretPlotData
from Ferret.Core.FerretConstants import FerretConstants
#Custom widgets for the display of parameter & constant data
#A shortName property is added to each widget so that it can
#be identified with the parameter or constant it is displaying
class ModelParameterSpinBox(QDoubleSpinBox):
"""
A spin box for the display of a model parameter called shortName
"""
def __init__(self, shortName):
super().__init__()
self._shortName = shortName
self.setMaximumWidth(150)
@property
def shortName(self):
return self._shortName
class ModelParameterCheckBox(QCheckBox):
"""
A check box, when checked, for fixing the value
of a model parameter during curve fitting.
"""
def __init__(self, shortName):
super().__init__()
self._shortName = shortName
self.setStyleSheet("spacing: 0px; padding : 0px; alignment: center;")
@property
def shortName(self):
return self._shortName
class ModelLabel(QLabel):
"""
A label that displays the text in the input argument shortName
"""
def __init__(self, shortName):
super().__init__()
self._shortName = shortName
self.setText(shortName)
@property
def shortName(self):
return self._shortName
class ModelParameterConfLimits(QLabel):
"""
A label for the display of optimum parameter
confidence limits after curve fitting.
"""
def __init__(self, shortName):
super().__init__()
self._shortName = shortName
@property
def shortName(self):
return self._shortName
class ModelComboBox(QComboBox):
"""
A combo box for the display & selection
of parameter and constant values.
"""
def __init__(self, shortName, longName=None):
super().__init__()
self._shortName = shortName
if longName:
self.setToolTip('Select {}'.format(longName))
@property
def shortName(self):
return self._shortName
class Ferret(QWidget):
"""
This class defines the FERRET Model Fitting GUI.
"""
def __init__(self, statusBar=None, dataFileFolder=None, modelList=None):
"""
Creates the GUI.
Controls on the GUI are placed on 2 vertical
layout panals placed on a horizontal layout panal.
The horizontal layout panal is contained by a QWidget object
that is returned to the MDI subwindow hosting FERRET, where
it is displayed.
The left-handside vertical layout panal holds widgets for the
input of data & the selection of the model to fit to the data.
Optimum parameter data and their confidence limits resulting
from the model fit are also displayed.
The right-handside vertical layout panal holds the graph
displaying the time/concentration data and the fitted model.
This method coordinates the calling of methods that set up the
widgets on the 2 vertical layout panals.
The appearance of the widgets is determined by CSS
commands in the module styleSheet.py.
Input parameters
------------------------------------
statusBar - object reference to the status bar on the MDI
dataFileFolder - file path of the folder containing signal/time data
modelList - list of names of models available to Ferret
"""
try:
super().__init__()
self.statusBar = statusBar
self.constantsString = None
# Store path to time/concentration data files for use
# in batch processing.
self.dataFileFolder = dataFileFolder
# Boolean variable indicating that the last
# change to the model parameters was caused
# by curve fitting.
self.isCurveFittingDone = False
# Dictionary to store signal data from the data input file
self.signalData={}
# List to store data calculated by the model
self.listModelPredictedValues = []
#List to store the models available to the user of Ferret
#Each model is represented by an object of class Model
self.listModelObjects = modelList
#Object representing the model selected from the above list
self.currentModelObject = None
#store dynamically created parameter widgets in lists
self.parameterLabelName = []
self.parameterLabelUnits = []
self.parameterSpinBoxList = []
self.parameterFixedCheckBoxList = []
self.parameterIntervalLimitList = []
#store dynamically created constants widgets in a list
self.constantsLabelName = []
self.constantsLabelUnits = []
self.constantsWidgetList = []
#store dynamically created variable widgets in lists
self.variableComboList = []
self.variableLabelList = []
# Stores optimum parameters from Curve fitting
self.optimisedParamaterDict = {}
self.setUpMainLayouts()
# Set up the graph to plot signal/time data on
# the right-hand side vertical layout
self.setUpPlotArea()
#Add widgets to the left-hand side vertical layout
self.setUpLeftVerticalLayout()
logger.info("FERRET GUI created successfully.")
except Exception as e:
print('Error creating FERRET object: ' + str(e))
logger.error('Error creating FERRET object: ' + str(e))
def setUpMainLayouts(self):
"""
Creates the main layouts that divide the Ferret GUI
into two columns.
"""
self.horizontalGridLayout = QGridLayout() #Parent layout
self.verticalLayoutLeft = QVBoxLayout()
self.verticalLayoutRight = QVBoxLayout()
self.horizontalGridLayout.setColumnStretch(0, 2)
self.horizontalGridLayout.setColumnStretch(1, 2)
self.horizontalGridLayout.addLayout(self.verticalLayoutLeft, 0, 0)
self.horizontalGridLayout.addLayout(self.verticalLayoutRight, 0, 1)
#Add the parent layout to the Ferret widget
self.setLayout(self.horizontalGridLayout)
def setListModelPredictedValues(self, listPredictedValues):
self.listModelPredictedValues = listPredictedValues
def setSignalData(self, signalData):
self.signalData = signalData
def returnFerretLogo(self):
return FERRET_LOGO
def setUpLeftVerticalLayout(self):
"""
Creates widgets and places them on the left-handside vertical layout.
"""
try:
self.setUpLoadDataWidget()
self.setUpModelGroupBox()
self.setUpExportGroupBox()
self.verticalLayoutLeft.addStretch(1)
except Exception as e:
print('Error in FERRET.setUpLeftVerticalLayout: ' + str(e))
logger.error('Error in FERRET.setUpLeftVerticalLayout: ' + str(e))
def setUpLoadDataWidget(self):
"""
Sets up the Load Data widget at the top of the left-handside vertical layout.
"""
try:
self.loadDataWidget = FerretLoadData(self.listModelObjects, self.dataFileFolder)
self.verticalLayoutLeft.addWidget(self.loadDataWidget)
self.loadDataWidget.sigClearGUI.connect(self.HideAllControlsOnGUI)
self.loadDataWidget.sigClearGUI.connect(self.lineGraph.clearGraph)
self.loadDataWidget.sigReturnList.connect(lambda modelList:
self.setListModelObjects(modelList))
self.loadDataWidget.sigReturnList.connect(self.populateModelListCombo)
self.loadDataWidget.sigReturnStatus.connect(lambda statusText:
self.statusBar.showMessage(statusText))
self.loadDataWidget.sigReturnDataDictionary.connect(lambda signalData: self.setSignalData(signalData))
self.loadDataWidget.sigUpdateGUI.connect(self.ConfigureGUIAfterLoadingData)
except Exception as e:
print('Error in FERRET.setUpLoadDataWidget: ' + str(e))
logger.error('Error in FERRET.setUpLoadDataWidget: ' + str(e))
def setUpModelDropDownList(self):
"""
Set up the drop down list of models available in Ferret
"""
self.modelLabel = QLabel("Model:")
self.cmbModels = QComboBox()
self.cmbModels.setToolTip('Select a model to fit to the data')
#Display first item in list, the string "Please Select"
self.cmbModels.setCurrentIndex(0)
self.modelLabel.hide()
self.cmbModels.hide()
#activated signal used, so function only connected
#when the user selects a model in the dropdown list
self.cmbModels.activated.connect(self.deleteVariableWidgets)
self.cmbModels.activated.connect(self.getSelectedModelObject)
self.cmbModels.activated.connect(self.UncheckFixParameterCheckBoxes)
self.cmbModels.activated.connect(lambda: self.clearOptimisedParamaterList('cmbModels'))
self.cmbModels.activated.connect(self.setUpModelVariableWidgits)
self.cmbModels.activated.connect(self.configureGUIForEachModel)
self.cmbModels.activated.connect(self.displayFitModelButton)
self.modelHorizontalLayoutTopRow.addWidget(self.modelLabel)
self.modelHorizontalLayoutTopRow.addWidget(self.cmbModels)
self.variablesGridLayout = QGridLayout()
self.modelHorizontalLayoutTopRow.addLayout(self.variablesGridLayout)
if self.listModelObjects is not None:
self.populateModelListCombo()
def setUpModelVariableWidgits(self):
"""
Creats a label-combobox pair for each model variable
and places them in a grid layout.
"""
try:
if self.currentModelObject is not None:
listDataNames = []
listDataNames = self.GetListDataNames()
colNumber = 0
for obj in self.currentModelObject.variablesList:
self.label = ModelLabel(obj.shortName)
self.comboBox = ModelComboBox(obj.shortName, obj.longName)
self.comboBox.setSizeAdjustPolicy(QComboBox.AdjustToContents)
self.comboBox.addItems(listDataNames)
self.variablesGridLayout.addWidget(self.label, 0, colNumber)
self.variablesGridLayout.addWidget( self.comboBox, 0, colNumber+1)
self.comboBox.activated.connect(self.lineGraph.plotGraph)
self.comboBox.activated.connect(self.displayFitModelButton)
self.variableComboList.append(self.comboBox)
self.variableLabelList.append(self.label)
colNumber+=2
except Exception as e:
print('Error in function FERRET setUpModelVariableWidgits: ' + str(e) )
logger.error('Error in function FERRET setUpModelVariableWidgits: ' + str(e) )
def getSelectedModelObject(self):
"""
When the user selects a model in the drop down list of models,
this function gets the corresponding model object
"""
self.setCurrentModelObject(self.cmbModels.currentText())
def setCurrentModelObject(self, shortModelName):
"""
This function interates through a list of model objects
and returns the model object with the shortName, shortModelName
"""
self.currentModelObject = None
for obj in self.listModelObjects:
if obj.shortName == shortModelName:
self.currentModelObject = obj
break
def setUpResetButton(self):
"""
Sets up the Reset button. Clicking this button reset parameter
and constant fields to their default values.
"""
self.btnReset = QPushButton('Reset')
self.btnReset.setMaximumSize(QtCore.QSize(100,45))
self.btnReset.setToolTip('Reset parameters to their default values.')
self.btnReset.hide()
self.btnReset.clicked.connect(self.resetParameterSpinBoxes)
self.btnReset.clicked.connect(self.resetConstantValues)
self.btnReset.clicked.connect(self.OptimumParameterChanged)
# If parameters are reset to their default values,
# replot the concentration and model data
self.btnReset.clicked.connect(self.lineGraph.plotGraph)
self.modelHorizontalLayoutReset.addWidget(self.btnReset)
def setUpFitModelButton(self):
"""
Sets up the Fit Model button
"""
self.btnFitModel = QPushButton('Fit Model')
self.btnFitModel.setMaximumSize(QtCore.QSize(130,45))
self.btnFitModel.setToolTip('Fit the selected model to the data')
##self.btnFitModel.clicked.connect(self.hideSolverMessage())
self.btnFitModel.clicked.connect(self.lineGraph.curveFit)
self.modelHorizontalLayoutReset.addWidget(self.btnFitModel)
def setUpParameterGridHeader(self):
"""
Widgets displaying parameters are placed in a grid layout.
This function creates the first header row in the parameter grid layout.
"""
self.lblConfInt = QLabel("<u>95% Conf' Interval</u>")
self.lblFix = QLabel("<u>Fix</u>")
self.lblConfInt.setAlignment(QtCore.Qt.AlignRight)
self.lblFix.setAlignment(QtCore.Qt.AlignLeft)
self.paramGridLayout.addWidget(self.lblFix, 0, 3)
self.paramGridLayout.addWidget(self.lblConfInt, 0, 4)
def setUpExportGroupBox(self):
"""
Creates the Export Data group box and adds it to
left-hand side vertical layout.
"""
try:
self.groupBoxExport = FerretExportData()
self.groupBoxExport.setExportGroupBoxVisible(False)
self.verticalLayoutLeft.addWidget(self.groupBoxExport)
self.groupBoxExport.sigPrepareForDataExport.connect(self.collectDataForExport)
except Exception as e:
print('Error in FERRET.setUpExportGroupBox: ' + str(e))
logger.error('Error in FERRET.setUpExportGroupBox: ' + str(e))
def getVariableValueFromComboBox(self, name):
"""
This function returns the value of a variable called name
from the corresponding combobox.
"""
valueVariable = None
for comboBox in self.variableComboList:
if comboBox.shortName == name:
valueVariable = comboBox.currentText()
return valueVariable
def setVariableValuesInModelObject(self):
"""
This function iterates through all the variables associated with
the current model and sets their values to those selected in thier
associated widgets on the GUI.
"""
for variable in self.currentModelObject.variablesList:
variable.setValue(self.getVariableValueFromComboBox(variable.shortName))
def getListModelVariableValues(self):
"""
Returns a list of variable values from their associated comboboxes
"""
tempList = []
for variable in self.currentModelObject.variablesList:
tempList.append(self.getVariableValueFromComboBox(variable.shortName))
return tempList
def collectDataForPlotting(self):
"""
This function ensures all data is available for
running the model and plotting the data on a graph.
"""
try:
self.setVariableValuesInModelObject()
self.lineGraph.setCurrentModelObject(self.currentModelObject)
self.lineGraph.setParameterList(self.buildParameterArray())
self.lineGraph.setSignalData(self.signalData)
except Exception as e:
print("Error in Ferret.collectDataForPlotting: " + str(e))
def collectDataForExport(self):
"""
This function ensures all data is available for
export out of Ferret.
"""
self.setVariableValuesInModelObject()
shortModelName = self.cmbModels.currentText()
longModelName = self.currentModelObject.longName
self.groupBoxExport.setLongModelName(longModelName)
self.groupBoxExport.setModelName(shortModelName)
if self.isCurveFittingDone:
parameterDict = self.optimisedParamaterDict
else:
parameterDict = self.BuildParameterDictionary()
self.groupBoxExport.setDataFileName(self.loadDataWidget.dataFileName)
self.groupBoxExport.setParameterDictionary(parameterDict)
self.groupBoxExport.setSignalData(self.signalData)
self.groupBoxExport.setListModelValues(self.listModelPredictedValues)
self.groupBoxExport.setListModelVariableValues(self.getListModelVariableValues())
self.lineGraph.savePlotToPDF(IMAGE_NAME)
def setUpConstantsGroupBox(self):
self.groupBoxConstants = QGroupBox('Model Constants')
self.groupBoxConstants.hide()
#Grid layout to manage constants widgets
self.constantsGridLayout = QGridLayout()
self.modelHorizontalLayoutMiddleRow.addWidget(self.groupBoxConstants)
self.groupBoxConstants.setLayout(self.constantsGridLayout)
def setUpParametersGroupBox(self):
self.groupBoxParameters = QGroupBox('Model Parameters')
self.groupBoxParameters.hide()
#Grid layout to manage parameter widgets
self.paramGridLayout = QGridLayout()
self.modelHorizontalLayoutMiddleRow.addWidget(self.groupBoxParameters)
self.groupBoxParameters.setLayout(self.paramGridLayout)
def connectLineGraphSignalsToSlots(self):
self.lineGraph.sigGetPlotData.connect(self.collectDataForPlotting)
self.lineGraph.sigGetPlotData.connect(self.buildConstantsString)
self.lineGraph.sigGetCurveFitData.connect(self.curveFitCollectParameterData)
self.lineGraph.sigGetCurveFitData.connect(self.buildConstantsString)
self.lineGraph.sigCurveFittingComplete.connect(lambda listResults:
self.postCurveFittingProcessing(listResults))
self.lineGraph.sigMessageReturnedFromSolver.connect(lambda msg: self.displaySolverMessage(msg))
self.lineGraph.sigReturnOptimumParamDict.connect(lambda optParamDict:
self.display95ConfidenceLimits(optParamDict))
def displaySolverMessage(self, msg):
self.messageLabel.setText(msg)
self.messageLabel.show()
self.groupBoxMessage.show()
def hideSolverMessage(self):
self.messageLabel.setText('')
self.messageLabel.hide()
self.groupBoxMessage.hide()
def setUpLayoutsInModelGroupBox(self):
""" Creates the 3 horizontal layouts in the Model group box.
They are added to a vertical layout, which is
then added to the Model group box.
One row of widgets is added to each horizontal layout. """
# modelHorizontalLayoutTopRow contains the combo boxes
# for selecting the model and its variables
self.modelHorizontalLayoutTopRow = QHBoxLayout()
self.modelHorizontalLayoutMiddleRow = QHBoxLayout()
self.modelHorizontalLayoutMessageRow = QHBoxLayout()
self.modelHorizontalLayoutReset = QHBoxLayout()
#The above horizontal layouts are stacked in the following vertical layout
self.modelVerticalLayout = QVBoxLayout()
self.modelVerticalLayout.addLayout(self.modelHorizontalLayoutTopRow)
self.modelVerticalLayout.addLayout(self.modelHorizontalLayoutMiddleRow)
self.modelVerticalLayout.addLayout(self.modelHorizontalLayoutMessageRow)
self.modelVerticalLayout.addLayout(self.modelHorizontalLayoutReset)
self.groupBoxModel.setLayout(self.modelVerticalLayout)
def setUpSolverMessageGroupBox(self):
try:
self.groupBoxMessage = QGroupBox('Message from equation solver function')
self.groupBoxMessage.hide()
self.messageLabel = QLabel()
self.messageLabel.hide()
self.groupBoxMessageLayout = QHBoxLayout()
self.groupBoxMessageLayout.addWidget(self.messageLabel, alignment=Qt.AlignCenter)
self.groupBoxMessage.setLayout(self.groupBoxMessageLayout)
self.modelHorizontalLayoutMessageRow.addWidget(self.groupBoxMessage)
except Exception as e:
print('Error in FERRET.setUpSolverMessageGroupBox: ' + str(e))
logger.error('Error in FERRET.setUpSolverMessageGroupBox: ' + str(e))
def setUpModelGroupBox(self):
"""Creates a group box to hold widgets associated with the
selection of a model and for inputing/displaying that model's
parameter data."""
try:
self.groupBoxModel = QGroupBox('Model Fitting')
# The group box is hidden until a Model is selected.
self.groupBoxModel.hide()
self.verticalLayoutLeft.addWidget(self.groupBoxModel)
self.setUpLayoutsInModelGroupBox()
self.setUpConstantsGroupBox()
self.setUpParametersGroupBox()
self.setUpModelDropDownList()
self.setUpSolverMessageGroupBox()
self.setUpResetButton()
self.setUpFitModelButton()
self.connectLineGraphSignalsToSlots()
except Exception as e:
print('Error in FERRET.setUpModelGroupBox: ' + str(e))
logger.error('Error in FERRET.setUpModelGroupBox: ' + str(e))
def postCurveFittingProcessing(self, listResults):
"""
After curve fitting has been performed, this function
configures the Ferret GUI.
"""
self.isCurveFittingDone = True
self.ClearOptimumParamaterConfLimitsOnGUI()
self.SetParameterSpinBoxValues(listResults)
self.lineGraph.setParameterFixedCheckBoxList(self.parameterFixedCheckBoxList)
def displayModelList(self):
"""
Makes the Model label-combobox pair visible
"""
self.cmbModels.show()
self.modelLabel.show()
def setUpPlotArea(self):
"""Adds widgets for the display of the graph onto the
right-hand side vertical layout.
"""
try:
self.lineGraph = FerretPlotData(yLabel="Signal",
xLabel="Time",
title="Signal-Time Curves")
self.lineGraph.sigReturnListModelConcentrations.connect(lambda listConcs:
self.setListModelPredictedValues(listConcs))
self.verticalLayoutRight.setAlignment(QtCore.Qt.AlignTop)
self.verticalLayoutRight.addWidget(self.lineGraph)
except Exception as e:
print('Error in FERRET.setUpPlotArea: ' + str(e))
logger.error('Error in FERRET.setUpPlotArea: ' + str(e))
def OptimumParameterChanged(self):
"""Sets boolean self.isCurveFittingDone to false if the
plot of the model curve is changed by manually changing the values of
model input parameters rather than by running curve fitting.
Also, clears the labels that display the optimum value of each
parameter and its confidence inteval."""
self.isCurveFittingDone=False
self.clearOptimisedParamaterList('Function-OptimumParameterChanged')
def ClearOptimumParamaterConfLimitsOnGUI(self):
"""Clears the contents of the labels on the left
handside of the GUI that display parameter value
confidence limits resulting from curve fitting. """
try:
logger.info('Function FERRET.ClearOptimumParamaterConfLimitsOnGUI called.')
for confIntLabel in self.parameterIntervalLimitList:
confIntLabel.clear()
except Exception as e:
print('Error in function FERRET.ClearOptimumParamaterConfLimitsOnGUI: ' + str(e))
logger.error('Error in function FERRET.ClearOptimumParamaterConfLimitsOnGUI: ' + str(e))
def clearOptimisedParamaterList(self, callingControl: str):
"""Clears results of curve fitting from the GUI
and from the global list self.optimisedParamaterDict """
try:
logger.info('FERRET.clearOptimisedParamaterList called from ' + callingControl)
self.optimisedParamaterDict.clear()
self.ClearOptimumParamaterConfLimitsOnGUI()
except Exception as e:
print('Error in function FERRET.clearOptimisedParamaterList: ' + str(e))
logger.error('Error in function FERRET.clearOptimisedParamaterList: ' + str(e))
def displayFitModelButton(self):
"""Displays the Fit Model, Save CSV and Save PFD Report
buttons if a model selected. Otherwise hides them."""
try:
flag = True
for dropDownList in self.variableComboList:
if dropDownList.currentText() == FerretConstants.PLEASE_SELECT:
flag = False
break
self.btnFitModel.setVisible(flag)
self.groupBoxExport.setExportGroupBoxVisible(flag)
except Exception as e:
print('Error in function FERRET.displayFitModelButton: ' + str(e))
logger.error('Error in function FERRET.displayFitModelButton: ' + str(e))
def buildConstantsString(self):
"""
This function builds a string representation of a
python dictionary of constant name:value pairs.
This string forms part of the input to a model function.
It is passed to the FerretPlotData object for use with
the model function.
"""
constantsDict = {}
for widget in self.constantsWidgetList:
if isinstance(widget, QComboBox):
constantsDict[widget.shortName] = widget.currentText()
elif isinstance(widget, QDoubleSpinBox):
constantsDict[widget.shortName] = widget.value()
self.lineGraph.setConstantsString(str(constantsDict))
def buildParameterArray(self) -> List[float]:
"""Forms a 1D array of model input parameters
for input to the model function.
Returns
-------
A list of model input parameter values.
"""
try:
logger.info('Function FERRET.buildParameterArray called.')
initialParametersArray = []
for spinBox in self.parameterSpinBoxList:
#print("parameter {}, value {}".format(spinBox.shortName, spinBox.value()))
if spinBox.suffix() == '%':
# This is a volume fraction so convert % to a decimal fraction
initialParametersArray.append(spinBox.value()/100.0)
else:
initialParametersArray.append(spinBox.value())
return initialParametersArray
except Exception as e:
print('Error in function FERRET.buildParameterArray ' + str(e))
logger.error('Error in function FERRET.buildParameterArray ' + str(e))
def SetParameterSpinBoxValues(self, parameterList):
"""Sets the value displayed in the model parameter spinboxes
to the calculated optimum model parameter values.
Input Parameters
----------------
parameterList - List of optimum model input parameter values.
"""
try:
logger.info('Function FERRET.SetParameterSpinBoxValues called with parameterList = {}'.format(parameterList))
for objSpinBox in self.parameterSpinBoxList:
objSpinBox.blockSignals(True)
value = float(parameterList[objSpinBox.shortName])
if objSpinBox.suffix() == '%':
value = value * 100
objSpinBox.setValue(round(value, 4))
objSpinBox.blockSignals(False)
except Exception as e:
print('Error in function FERRET.SetParameterSpinBoxValues ' + str(e))
logger.error('Error in function FERRET.SetParameterSpinBoxValues ' + str(e))
def display95ConfidenceLimits(self, optParamDict):
"""
This function displays the lower and upper confidence limits
of each optimal parameter value for the best curve fit on the
Ferret GUI.
"""
self.optimisedParamaterDict = optParamDict
for objLabel in self.parameterIntervalLimitList:
tempList = self.optimisedParamaterDict[objLabel.shortName]
lowerLimit = tempList[1]
upperLimit = tempList[2]
confidenceStr = '[{} {}]'.format(lowerLimit, upperLimit)
objLabel.setText(confidenceStr)
def curveFitCollectParameterData(self)-> List[float]:
"""
Forms a list of model input parameters to
be used as input to model for curve fitting.
It is passed to the FerretPlotData object for use with
the model function.
"""
try:
logger.info('function FERRET curveFitCollectParameterData called.')
parameterDataList = []
for paramObject in self.currentModelObject.parameterList:
paramShortName = paramObject.shortName
units = paramObject.units
upper = paramObject.upperConstraint
lower = paramObject.lowerConstraint
value = self.getParamaterSpinBoxValue(paramShortName)
if units == "%":
value = value/100
vary = True
self.getFixedCheckBoxValue(paramShortName)
if self.getFixedCheckBoxValue(paramShortName):
vary = False
#each tuple must be (name, value, vary, min, max, expr, brute_step).
tempTuple = (paramShortName, float(value), vary, lower, upper, None, None)
parameterDataList.append(tempTuple)
self.lineGraph.setCurveFitParameterList(parameterDataList)
except Exception as e:
print('Error in function FERRET curveFitCollectParameterData ' + str(e))
logger.error('Error in function FERRET curveFitCollectParameterData ' + str(e))
def BuildParameterDictionary(self):
"""
This function builds a dictionary of parameter names:value pairs
that is used to create the parameter value table used in the PDF report.
"""
try:
logger.info('BuildParameterDictionary called.')
parameterDictionary = {}
for objSpinBox in self.parameterSpinBoxList:
parameterList = []
parameterList.append(str(round(objSpinBox.value(), 4)))
parameterList.append('N/A')
parameterList.append('N/A')
units = self.currentModelObject.getParameterUnits(objSpinBox.shortName)
parameterList.append(units)
parameterDictionary[objSpinBox.shortName] = parameterList
return parameterDictionary
except Exception as e:
print('Error in function FERRET BuildParameterDictionary: ' + str(e))
logger.error('Error in function FERRET BuildParameterDictionary: ' + str(e))
def setListModelObjects(self, modelObjectList):
"""
This function assigns the list of model objects returned by the
load data widget to a local class property.
"""
self.listModelObjects = modelObjectList
def populateModelListCombo(self):
"""
Builds a list of model short names and adds this list to the
cmbModels combo box for display on the GUI.
"""
try:
logger.info('function FERRET populateModelListCombo called.')
listModelNames = [FerretConstants.PLEASE_SELECT]
for obj in self.listModelObjects:
listModelNames.append(obj.shortName)
self.cmbModels.clear()
self.cmbModels.blockSignals(True)
self.cmbModels.addItems(listModelNames)
self.cmbModels.blockSignals(False)
except Exception as e:
print('Error in function FERRET populateModelListCombo: ' + str(e))
logger.error('Error in function FERRET populateModelListCombo: ' + str(e))
def HideAllControlsOnGUI(self):
"""
Hides/clears all the widgets on left-hand side of the application
except for the Load & Display Data buttons.
It is called before a data file is loaded in case the
Cancel button on the dialog is clicked.
This prevents the scenario where buttons are displayed
but there is no data loaded to process when they are clicked.
"""
try:
logger.info('function FERRET HideAllControlsOnGUI called')
if self.statusBar is not None:
self.statusBar.clearMessage()
self.groupBoxModel.hide()
self.groupBoxConstants.hide()
self.hideSolverMessage()
self.groupBoxParameters.hide()
self.deleteVariableWidgets()
self.groupBoxExport.setExportGroupBoxVisible(False)
self.btnFitModel.hide()
self.btnReset.hide()
except Exception as e:
print('Error in function FERRET HideAllControlsOnGUI: ' + str(e))
logger.error('Error in function FERRET HideAllControlsOnGUI: ' + str(e))
def ConfigureGUIAfterLoadingData(self):
"""
After a model library has been selected
and a data file is loaded, this function makes
the model group box and model list visible.
"""