Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 78ecc3f

Browse files
analyze individual spectrum fits from spectra fit file, will open matplotlibwindow for plots, and checkbox for controlling when to launch h5-pkl dataviewer
1 parent 48ddbf1 commit 78ecc3f

4 files changed

Lines changed: 228 additions & 31 deletions

File tree

PythonGUI_apps/Spectrum_analysis/Spectra_plot_fit.py

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pyqtgraph.Qt import QtCore, QtGui, QtWidgets#, QColorDialog
1515
import numpy as np
1616
import matplotlib.pyplot as plt
17+
import matplotlib
1718
import pickle
1819
import lmfit
1920
from lmfit.models import GaussianModel
@@ -25,17 +26,20 @@
2526
# local modules
2627
try:
2728
from Spectra_fit_funcs import Spectra_Fit, Single_Gaussian, Single_Lorentzian, Double_Gaussian, Multi_Gaussian
29+
from pyqtgraph_MATPLOTLIBWIDGET import MatplotlibWidget
2830
except:
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

4044
pg.mkQApp()
4145
pg.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"""
836875
def run():

PythonGUI_apps/Spectrum_analysis/Spectra_plot_fit_gui.ui

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,18 @@ Ready Figure</string>
11281128
</property>
11291129
</widget>
11301130
</item>
1131+
<item row="7" column="3">
1132+
<widget class="QPushButton" name="analyze_spectra_fits_pushButton">
1133+
<property name="font">
1134+
<font>
1135+
<pointsize>12</pointsize>
1136+
</font>
1137+
</property>
1138+
<property name="text">
1139+
<string>Analyze Spectra Fits</string>
1140+
</property>
1141+
</widget>
1142+
</item>
11311143
</layout>
11321144
</item>
11331145
<item row="0" column="2">
@@ -1214,6 +1226,18 @@ Ready Figure</string>
12141226
</property>
12151227
</widget>
12161228
</item>
1229+
<item>
1230+
<widget class="QCheckBox" name="launch_data_viewer_checkBox">
1231+
<property name="font">
1232+
<font>
1233+
<pointsize>12</pointsize>
1234+
</font>
1235+
</property>
1236+
<property name="text">
1237+
<string>Launch Data Viewer</string>
1238+
</property>
1239+
</widget>
1240+
</item>
12171241
<item>
12181242
<widget class="QPushButton" name="load_spectra_scan_pushButton">
12191243
<property name="font">
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>Form</class>
4+
<widget class="QWidget" name="Form">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>543</width>
10+
<height>199</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Analze Fit Results</string>
15+
</property>
16+
<layout class="QGridLayout" name="gridLayout">
17+
<item row="1" column="1">
18+
<widget class="QSpinBox" name="result_spinBox">
19+
<property name="font">
20+
<font>
21+
<pointsize>12</pointsize>
22+
</font>
23+
</property>
24+
<property name="maximum">
25+
<number>1000000000</number>
26+
</property>
27+
</widget>
28+
</item>
29+
<item row="0" column="0" colspan="3">
30+
<widget class="QLabel" name="label_2">
31+
<property name="font">
32+
<font>
33+
<pointsize>15</pointsize>
34+
</font>
35+
</property>
36+
<property name="text">
37+
<string>Analyze Fit Results</string>
38+
</property>
39+
<property name="alignment">
40+
<set>Qt::AlignCenter</set>
41+
</property>
42+
</widget>
43+
</item>
44+
<item row="1" column="0">
45+
<widget class="QLabel" name="label">
46+
<property name="font">
47+
<font>
48+
<pointsize>12</pointsize>
49+
</font>
50+
</property>
51+
<property name="text">
52+
<string>Result number:</string>
53+
</property>
54+
<property name="alignment">
55+
<set>Qt::AlignCenter</set>
56+
</property>
57+
</widget>
58+
</item>
59+
<item row="1" column="2">
60+
<widget class="QPushButton" name="plot_pushButton">
61+
<property name="font">
62+
<font>
63+
<pointsize>12</pointsize>
64+
</font>
65+
</property>
66+
<property name="text">
67+
<string>Check!</string>
68+
</property>
69+
</widget>
70+
</item>
71+
<item row="2" column="0" colspan="3">
72+
<widget class="QGraphicsView" name="graphicsView"/>
73+
</item>
74+
</layout>
75+
</widget>
76+
<resources/>
77+
<connections/>
78+
</ui>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Sep 2 13:02:50 2019
4+
5+
@author: Sarthak
6+
"""
7+
8+
from pyqtgraph.Qt import QtGui, QtCore, USE_PYSIDE, USE_PYQT5
9+
import matplotlib
10+
11+
#if not USE_PYQT5:
12+
# if USE_PYSIDE:
13+
# matplotlib.rcParams['backend.qt4']='PySide'
14+
#
15+
# from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
16+
# from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
17+
#else:
18+
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
19+
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
20+
21+
from matplotlib.figure import Figure
22+
23+
if matplotlib.get_backend() is not 'Qt5Agg':
24+
matplotlib.use('Qt5Agg')
25+
26+
class MatplotlibWidget(QtGui.QWidget):
27+
"""
28+
Implements a Matplotlib figure inside a QWidget.
29+
Use getFigure() and redraw() to interact with matplotlib.
30+
31+
Example::
32+
33+
mw = MatplotlibWidget()
34+
subplot = mw.getFigure().add_subplot(111)
35+
subplot.plot(x,y)
36+
mw.draw()
37+
"""
38+
39+
def __init__(self, size=(5.0, 4.0), dpi=100):
40+
QtGui.QWidget.__init__(self)
41+
self.fig = Figure(size, dpi=dpi)
42+
self.canvas = FigureCanvas(self.fig)
43+
self.canvas.setParent(self)
44+
self.toolbar = NavigationToolbar(self.canvas, self)
45+
46+
self.vbox = QtGui.QVBoxLayout()
47+
self.vbox.addWidget(self.toolbar)
48+
self.vbox.addWidget(self.canvas)
49+
50+
self.setLayout(self.vbox)
51+
52+
def getFigure(self):
53+
return self.fig
54+
55+
def draw(self):
56+
self.canvas.draw()

0 commit comments

Comments
 (0)