1414from pyqtgraph .Qt import QtCore , QtGui , QtWidgets #, QColorDialog
1515import numpy as np
1616import matplotlib .pyplot as plt
17+ import matplotlib
1718import pickle
1819import lmfit
1920from lmfit .models import GaussianModel
2526# local modules
2627try :
2728 from Spectra_fit_funcs import Spectra_Fit , Single_Gaussian , Single_Lorentzian , Double_Gaussian , Multi_Gaussian
29+ from pyqtgraph_MATPLOTLIBWIDGET import MatplotlibWidget
2830except :
2931 from Spectrum_analysis .Spectra_fit_funcs import Spectra_Fit , Single_Gaussian , Single_Lorentzian , Double_Gaussian , Multi_Gaussian
32+ from Spectrum_analysis .pyqtgraph_MATPLOTLIBWIDGET import MatplotlibWidget
3033
34+ matplotlib .use ('Qt5Agg' )
3135
3236"""Recylce params for plotting"""
33- plt .rc ('xtick' , labelsize = 20 )
34- plt .rc ('xtick.major' , pad = 3 )
35- plt .rc ('ytick' , labelsize = 20 )
36- plt .rc ('lines' , lw = 1.5 , markersize = 7 .5 )
37- plt .rc ('legend' , fontsize = 20 )
38- plt .rc ('axes' , linewidth = 3 .5 )
37+ plt .rc ('xtick' , labelsize = 10 )
38+ plt .rc ('xtick.major' , pad = 1 )
39+ plt .rc ('ytick' , labelsize = 10 )
40+ plt .rc ('lines' , lw = 1.5 , markersize = 3 .5 )
41+ plt .rc ('legend' , fontsize = 10 )
42+ plt .rc ('axes' , linewidth = 1 .5 )
3943
4044pg .mkQApp ()
4145pg .setConfigOption ('background' , 'w' )
@@ -85,6 +89,7 @@ def __init__(self):
8589 self .ui .clear_pushButton .clicked .connect (self .clear_plot )
8690 self .ui .export_single_figure_pushButton .clicked .connect (self .pub_ready_plot_export )
8791 self .ui .export_scan_figure_pushButton .clicked .connect (self .pub_ready_plot_export )
92+ self .ui .analyze_spectra_fits_pushButton .clicked .connect (self .analyze_spectra_fits )
8893
8994 self .ui .import_pkl_pushButton .clicked .connect (self .open_pkl_file )
9095 self .ui .data_txt_pushButton .clicked .connect (self .pkl_data_to_txt )
@@ -117,6 +122,9 @@ def __init__(self):
117122 #variables accounting for data received from FLIM analysis
118123 self .opened_from_flim = False #switched to True in FLIM_plot when "analyze lifetime" clicked
119124 self .sum_data_from_flim = []
125+
126+ # fit scan file variable set to None for analyze_spectra_fits
127+ self .fit_scan_file = None
120128
121129 #container for data to append to txt file
122130 self .data_list = []
@@ -172,11 +180,13 @@ def open_spectra_scan_file(self):
172180 self .filename_for_viewer_launch = filename [0 ]
173181 if ".pkl" in filename [0 ]:
174182 self .spec_scan_file = pickle .load (open (filename [0 ], 'rb' ))
175- self .launch_h5_pkl_viewer ()
183+ if self .ui .launch_data_viewer_checkBox .isChecked ():
184+ self .launch_h5_pkl_viewer ()
176185 self .scan_file_type = "pkl"
177186 elif ".h5" in filename [0 ]:
178187 self .spec_scan_file = h5py .File (filename [0 ], 'r' )
179- self .launch_h5_pkl_viewer ()
188+ if self .ui .launch_data_viewer_checkBox .isChecked ():
189+ self .launch_h5_pkl_viewer ()
180190 self .scan_file_type = "h5"
181191 self .get_data_params ()
182192 self .ui .result_textBrowser2 .append ("Done Loading - Spectra Scan File" )
@@ -199,7 +209,8 @@ def open_fit_scan_file(self):
199209 with pg .BusyCursor ():
200210 self .filename_for_viewer_launch = filename [0 ]
201211 self .fit_scan_file = pickle .load (open (filename [0 ], 'rb' ))
202- self .launch_h5_pkl_viewer () # TODO Needs to implement reading the fit result datatype in PKL Viewer
212+ if self .ui .launch_data_viewer_checkBox .isChecked ():
213+ self .launch_h5_pkl_viewer () # TODO Needs to implement reading the fit result datatype in PKL Viewer
203214 self .ui .result_textBrowser2 .append ("Done Loading - Scan Fit File" )
204215 except Exception as e :
205216 self .ui .result_textBrowser2 .append (str (e ))
@@ -219,6 +230,14 @@ def launch_h5_pkl_viewer(self):
219230 viewer_window = h5_pkl_view .H5PklView (sys .argv )
220231 viewer_window .settings ['data_filename' ] = self .filename_for_viewer_launch
221232
233+ def analyze_spectra_fits (self ):
234+ """Analyze fits to the individual spectrum within a spectra scan fit file"""
235+ if self .fit_scan_file is None :
236+ self .open_fit_scan_file ()
237+
238+ analyze_window = Analyze (scan_fit_file = self .fit_scan_file )
239+ analyze_window .run ()
240+
222241 def switch_overall_tab (self ):
223242 """ Enable/disable fit settings on right depending on current tab """
224243 if self .ui .tabWidget .currentIndex () == 0 :
@@ -465,6 +484,13 @@ def fit_and_plot(self):
465484
466485 def pub_ready_plot_export (self ):
467486 filename = QtWidgets .QFileDialog .getSaveFileName (self ,caption = "Filename with EXTENSION" )
487+ """Recylce params for plotting"""
488+ plt .rc ('xtick' , labelsize = 20 )
489+ plt .rc ('xtick.major' , pad = 3 )
490+ plt .rc ('ytick' , labelsize = 20 )
491+ plt .rc ('lines' , lw = 1.5 , markersize = 7.5 )
492+ plt .rc ('legend' , fontsize = 20 )
493+ plt .rc ('axes' , linewidth = 3.5 )
468494 try :
469495 try :
470496 try :
@@ -801,36 +827,49 @@ def close_application(self):
801827 pass
802828
803829
804- """Parameter Window GUI and Functions"""
805- # param_file_path = (base_path / "scan_params_input.ui").resolve()
830+ """Analyze Window GUI and Functions"""
831+ base_path = Path (__file__ ).parent
832+ file_path = (base_path / "analyze_fit_results.ui" ).resolve ()
806833
807- # param_uiFile = param_file_path
834+ uiFile = file_path
808835
809- # param_WindowTemplate, param_TemplateBaseClass = pg.Qt.loadUiType(param_uiFile )
836+ Analyze_WindowTemplate , Analyze_TemplateBaseClass = pg .Qt .loadUiType (uiFile )
810837
811- # class ParamWindow(param_TemplateBaseClass):
838+ class Analyze ( Analyze_TemplateBaseClass ):
812839
813- # #peak_range = QtCore.pyqtSignal(list)
814-
815- # def __init__(self):
816- # # super(param_TemplateBaseClass, self).__init__()
817- # param_TemplateBaseClass.__init__(self)
840+ def __init__ (self , scan_fit_file ):
841+ pg .setConfigOption ('imageAxisOrder' , 'row-major' )
842+ super (Analyze_TemplateBaseClass , self ).__init__ ()
818843
819- # # Create the param window
820- # self.pui = param_WindowTemplate ()
821- # self.pui .setupUi(self)
844+ # Create the main window
845+ self .ui = Analyze_WindowTemplate ()
846+ self .ui .setupUi (self )
822847
823- # self.pui.done_pushButton .clicked.connect(self.done )
848+ self .ui . plot_pushButton .clicked .connect (self .plot )
824849
825- # self.show()
826-
827-
850+ self .fit_scan_file = scan_fit_file
851+ self .show ()
852+
853+ def plot (self ):
854+ matplotlib .use ('Qt5Agg' )
855+ try :
856+ result_no = int (self .ui .result_spinBox .value ())
857+ if type (self .fit_scan_file ['result_0' ]) == lmfit .model .ModelResult :
858+ self .ui .graphicsView = MatplotlibWidget (dpi = 300 )
859+ self .fit_scan_file ['result_' + str (result_no )].plot (fig = MatplotlibWidget (size = (12 ,8 )).getFigure ().add_subplot (111 ))
860+ plt .tick_params (length = 8 , width = 3 )
861+ plt .xlabel ("Wavelength (nm)" , fontsize = 25 , fontweight = 'bold' )
862+ plt .ylabel ("Intensity (a.u.)" , fontsize = 25 , fontweight = 'bold' )
863+ plt .show ()
864+ plt .tight_layout ()
865+ except Exception as e :
866+ print (str (e ))
867+ pass
828868
829- # def done(self):
830- # #center_min, center_max = self.current_peak_range()
831- # #self.peak_range.emit([center_min, center_max])
832- # self.close()
833- # self.scan_params_entered = True
869+ def run (self ):
870+ win = Analyze ()
871+ QtGui .QApplication .instance ().exec_ ()
872+ return win
834873
835874"""Run the Main Window"""
836875def run ():
0 commit comments