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

Commit 6bff538

Browse files
author
LAKESIDE\LindaT18
committed
implemented multi gaussian for n=3, changed some var names
1 parent 20b3f46 commit 6bff538

3 files changed

Lines changed: 313 additions & 148 deletions

File tree

PythonGUI_apps/Spectrum_analysis/Spectra_fit_funcs.py

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,20 @@ def gaussian_model(self):
5656
result = gmodel.fit(y, pars, x=x, nan_policy='propagate')
5757
return result
5858

59-
def gaussian_model_w_lims(self, center_initial_guess=None, sigma_initial_guess=None, center_min=None, center_max=None):
59+
# def gaussian_model_w_lims(self, center_initial_guess=None, sigma_initial_guess=None, center_min=None, center_max=None):
60+
# x,y = self.background_correction()
61+
# gmodel = GaussianModel(prefix = 'g1_') # calling gaussian model
62+
# pars = gmodel.guess(y, x=x) # parameters - center, width, height
63+
# pars['g1_center'].set(center_initial_guess, min=center_min, max=center_max)
64+
# pars['g1_sigma'].set(sigma_initial_guess)
65+
# result = gmodel.fit(y, pars, x=x, nan_policy='propagate')
66+
# return result #770 760 780 sigma 15
67+
def gaussian_model_w_lims(self, peak_pos, sigma, min_max_range):
6068
x,y = self.background_correction()
6169
gmodel = GaussianModel(prefix = 'g1_') # calling gaussian model
6270
pars = gmodel.guess(y, x=x) # parameters - center, width, height
63-
pars['g1_center'].set(center_initial_guess, min=center_min, max=center_max)
64-
pars['g1_sigma'].set(sigma_initial_guess)
71+
pars['g1_center'].set(peak_pos, min=min_max_range[0], max=min_max_range[1])
72+
pars['g1_sigma'].set(sigma)
6573
result = gmodel.fit(y, pars, x=x, nan_policy='propagate')
6674
return result #770 760 780 sigma 15
6775

@@ -88,12 +96,12 @@ def lorentzian_model(self):
8896
# result = lmodel.fit(y, pars, x=x, nan_policy='propagate')
8997
# return result
9098

91-
def lorentzian_model_w_lims(self, center_initial_guess=None, sigma_initial_guess=None, center_min = None, center_max = None):
99+
def lorentzian_model_w_lims(self, peak_pos, sigma, min_max_range):
92100
x,y = self.background_correction()
93101
lmodel = LorentzianModel(prefix = 'l1_') # calling lorentzian model
94102
pars = lmodel.guess(y, x=x) # parameters - center, width, height
95-
pars['l1_center'].set(center_initial_guess, min = center_min, max = center_max)
96-
pars['l1_sigma'].set(sigma_initial_guess)
103+
pars['l1_center'].set(peak_pos, min = min_max_range[0], max = min_max_range[1])
104+
pars['l1_sigma'].set(sigma)
97105
result = lmodel.fit(y, pars, x=x, nan_policy='propagate')
98106
return result
99107

@@ -118,22 +126,22 @@ def gaussian_model(self):
118126
result = gmodel.fit(y, pars, x=x, nan_policy='propagate')
119127
return result
120128

121-
def gaussian_model_w_lims(self, center_initial_guesses=None, sigma_initial_guesses=None, min_max_range=None):
129+
def gaussian_model_w_lims(self, peak_pos, sigma, min_max_range):
122130
#center_initial_guesses - list containing initial guesses for peak centers. [center_guess1, center_guess2]
123131
#sigma_initial_guesses - list containing initial guesses for sigma. [sigma1, sigma2]
124132
#min_max_range - list containing lists of min and max for peak center. [ [min1, max1], [min2, max2] ]
125133

126134
x,y = self.background_correction()
127135
gmodel_1 = GaussianModel(prefix='g1_') # calling gaussian model
128136
pars = gmodel_1.guess(y, x=x) # parameters - center, width, height
129-
pars['g1_center'].set(center_initial_guesses[0], min = min_max_range[0][0], max = min_max_range[0][1])
130-
pars['g1_sigma'].set(sigma_initial_guesses[0])
137+
pars['g1_center'].set(peak_pos[0], min = min_max_range[0][0], max = min_max_range[0][1])
138+
pars['g1_sigma'].set(sigma[0])
131139
pars['g1_amplitude'].set(min=0)
132140

133141
gmodel_2 = GaussianModel(prefix='g2_')
134142
pars.update(gmodel_2.make_params()) # update parameters - center, width, height
135-
pars['g2_center'].set(center_initial_guesses[1], min = min_max_range[1][0], max = min_max_range[1][1])
136-
pars['g2_sigma'].set(sigma_initial_guesses[1])
143+
pars['g2_center'].set(peak_pos[1], min = min_max_range[1][0], max = min_max_range[1][1])
144+
pars['g2_sigma'].set(sigma[1], min = composite_pars['g1_sigma'].value)
137145
pars['g2_amplitude'].set(min = 0)
138146

139147
gmodel = gmodel_1 + gmodel_2
@@ -142,13 +150,45 @@ def gaussian_model_w_lims(self, center_initial_guesses=None, sigma_initial_guess
142150

143151
class Multi_Gaussian(Spectra_Fit):
144152

145-
def __init__(self, data, ref, num_of_gaussians, peak_pos, min_max_range):
146-
Spectra_Fit.__init__(self, data, ref)
153+
# def __init__(self, data, ref, num_of_gaussians, peak_pos, sigma, min_max_range):
154+
# Spectra_Fit.__init__(self, data, ref)
155+
# self.num_of_gaussians = num_of_gaussians
156+
# self.peak_pos = peak_pos
157+
# self.min_max_range = min_max_range
158+
def __init__(self, data, ref, num_of_gaussians, wlref=None):
159+
Spectra_Fit.__init__(self, data, ref, wlref)
147160
self.num_of_gaussians = num_of_gaussians
161+
162+
def gaussian_model(self):
163+
composite_model = None
164+
composite_pars = None
165+
166+
x,y = self.background_correction()
167+
168+
for i in range(self.num_of_gaussians):
169+
170+
model = GaussianModel(prefix='g'+str(i+1)+'_')
171+
172+
if composite_pars is None:
173+
composite_pars = model.guess(y, x=x)
174+
# composite_pars = model.make_params()
175+
176+
else:
177+
composite_pars.update(model.make_params())
178+
179+
if composite_model is None:
180+
composite_model = model
181+
else:
182+
composite_model += model
183+
184+
result = composite_model.fit(y, composite_pars, x=x, nan_policy='propagate')
185+
return result
186+
187+
def gaussian_model_w_lims(self, peak_pos, sigma, min_max_range):
148188
self.peak_pos = peak_pos
189+
self.sigma = sigma
149190
self.min_max_range = min_max_range
150191

151-
def multi_gaussian(self):
152192
composite_model = None
153193
composite_pars = None
154194

@@ -167,14 +207,14 @@ def multi_gaussian(self):
167207
# composite_pars = model.make_params()
168208
composite_pars['g'+str(i+1)+'_center'].set(self.peak_pos[i],
169209
min = self.min_max_range[0][0], max = self.min_max_range[0][1])
170-
composite_pars['g'+str(i+1)+'_sigma'].set(15)
210+
composite_pars['g'+str(i+1)+'_sigma'].set(self.sigma[i])
171211
composite_pars['g'+str(i+1)+'_amplitude'].set(min = 0)
172212

173213
else:
174214
composite_pars.update(model.make_params())
175215
composite_pars['g'+str(i+1)+'_center'].set(self.peak_pos[i],
176216
min = self.min_max_range[i][0], max = self.min_max_range[i][1])
177-
composite_pars['g'+str(i+1)+'_sigma'].set(min = composite_pars['g1_sigma'].value)
217+
composite_pars['g'+str(i+1)+'_sigma'].set(self.sigma[i], min = composite_pars['g1_sigma'].value)
178218
composite_pars['g'+str(i+1)+'_amplitude'].set(min = 0)
179219

180220

PythonGUI_apps/Spectrum_analysis/Spectra_plot_fit.py

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import customplotting.mscope as cpm
2020
# local modules
2121
try:
22-
from Spectra_fit_funcs import Spectra_Fit, Single_Gaussian, Single_Lorentzian, Double_Gaussian
22+
from Spectra_fit_funcs import Spectra_Fit, Single_Gaussian, Single_Lorentzian, Double_Gaussian, Multi_Gaussian
2323
except:
24-
from Spectrum_analysis.Spectra_fit_funcs import Spectra_Fit, Single_Gaussian, Single_Lorentzian, Double_Gaussian
24+
from Spectrum_analysis.Spectra_fit_funcs import Spectra_Fit, Single_Gaussian, Single_Lorentzian, Double_Gaussian, Multi_Gaussian
2525

2626

2727
"""Recylce params for plotting"""
@@ -71,14 +71,15 @@ def __init__(self):
7171

7272
self.ui.fit_pushButton.clicked.connect(self.fit_and_plot)
7373
self.ui.fit_scan_pushButton.clicked.connect(self.fit_and_plot_scan)
74-
self.ui.config_fit_params_pushButton.clicked.connect(self.configure_fit_params)
74+
# self.ui.config_fit_params_pushButton.clicked.connect(self.configure_fit_params)
7575
self.ui.clear_pushButton.clicked.connect(self.clear_plot)
7676
self.ui.export_fig_pushButton.clicked.connect(self.pub_ready_plot_export)
7777

7878
self.ui.import_pkl_pushButton.clicked.connect(self.open_pkl_file)
7979
self.ui.data_txt_pushButton.clicked.connect(self.pkl_data_to_txt)
8080
self.ui.scan_params_txt_pushButton.clicked.connect(self.pkl_params_to_txt)
8181

82+
self.ui.tabWidget.currentChanged.connect(self.switch_overall_tab)
8283
self.ui.fitFunc_comboBox.currentTextChanged.connect(self.switch_bounds_and_guess_tab)
8384
self.ui.adjust_param_checkBox.stateChanged.connect(self.switch_adjust_param)
8485

@@ -173,6 +174,17 @@ def save_file(self):# not used yet!
173174
except:
174175
pass
175176

177+
def switch_overall_tab(self):
178+
if self.ui.tabWidget.currentIndex() == 0:
179+
self.ui.fitting_settings_groupBox.setEnabled(True)
180+
self.ui.scan_fit_settings_groupBox.setEnabled(False)
181+
elif self.ui.tabWidget.currentIndex() == 1:
182+
self.ui.fitting_settings_groupBox.setEnabled(False)
183+
self.ui.scan_fit_settings_groupBox.setEnabled(True)
184+
elif self.ui.tabWidget.currentIndex() == 2:
185+
self.ui.fitting_settings_groupBox.setEnabled(False)
186+
self.ui.scan_fit_settings_groupBox.setEnabled(False)
187+
176188
def switch_bounds_and_guess_tab(self):
177189
fit_func = self.ui.fitFunc_comboBox.currentText()
178190
if fit_func == "Single Gaussian" or fit_func == "Single Lorentzian":
@@ -345,9 +357,9 @@ def clear_check(self):
345357
return False
346358

347359
"""Open param window and get peak center range values and assign it to variables to use later"""
348-
def configure_fit_params(self):
349-
self.param_window = ParamWindow()
350-
self.param_window.peak_range.connect(self.peak_range)
360+
# def configure_fit_params(self):
361+
# self.param_window = ParamWindow()
362+
# self.param_window.peak_range.connect(self.peak_range)
351363

352364
def peak_range(self, peaks):
353365
self.center_min = peaks[0]
@@ -373,8 +385,8 @@ def fit_and_plot(self):
373385
center1_max = self.ui.single_peakcenter1_max_spinBox.value()
374386
center1_guess = self.ui.single_peakcenter1_guess_spinBox.value()
375387
sigma1_guess = self.ui.single_sigma1_guess_spinBox.value()
376-
self.result = single_gauss.gaussian_model_w_lims(center_initial_guess=center1_guess, sigma_initial_guess=sigma1_guess,
377-
center_min=center1_min, center_max=center1_max)
388+
self.result = single_gauss.gaussian_model_w_lims(center1_guess, sigma1_guess,
389+
[center1_min, center1_max])
378390
else:
379391
self.result = single_gauss.gaussian_model()
380392
self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
@@ -389,16 +401,15 @@ def fit_and_plot(self):
389401
center1_max = self.ui.single_peakcenter1_max_spinBox.value()
390402
center1_guess = self.ui.single_peakcenter1_guess_spinBox.value()
391403
sigma1_guess = self.ui.single_sigma1_guess_spinBox.value()
392-
self.result = single_lorentzian.lorentzian_model_w_lims(center_initial_guess=center1_guess, sigma_initial_guess=sigma1_guess,
393-
center_min = center1_min, center_max = center1_max)
404+
self.result = single_lorentzian.lorentzian_model_w_lims(center1_guess, sigma1_guess,
405+
[center1_min, center1_max])
394406
else:
395407
self.result = single_lorentzian.lorentzian_model()
396408
self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
397409
self.ui.plot.plot(self.x, self.result.best_fit, clear=False, pen='k')
398410
self.ui.result_textBrowser.setText(self.result.fit_report())
399411

400412
elif fit_func == "Double Gaussian" and self.ui.subtract_bck_checkBox.isChecked() == True:
401-
self.ui.result_textBrowser.setText("Not Implemented Yet!")
402413
double_gauss = Double_Gaussian(self.file, self.bck_file, wlref=self.wlref_file)
403414
if self.ui.adjust_param_checkBox.isChecked():
404415
center1_min = self.ui.double_peakcenter1_min_spinBox.value()
@@ -410,8 +421,10 @@ def fit_and_plot(self):
410421
center2_guess = self.ui.double_peakcenter2_guess_spinBox.value()
411422
sigma2_guess = self.ui.double_sigma2_guess_spinBox.value()
412423

413-
self.result = double_gauss.gaussian_model_w_lims(center_initial_guesses=[center1_guess, center2_guess],
414-
sigma_initial_guesses=[sigma1_guess, sigma2_guess], min_max_range=[ [center1_min, center1_max], [center2_min, center2_max]])
424+
peak_pos = [center1_guess, center2_guess]
425+
sigma = [sigma1_guess, sigma2_guess]
426+
min_max_range = [ [center1_min, center1_max], [center2_min, center2_max] ]
427+
self.result = double_gauss.gaussian_model_w_lims(peak_pos, sigma, min_max_range)
415428

416429
else:
417430
self.result = double_gauss.gaussian_model()
@@ -426,7 +439,39 @@ def fit_and_plot(self):
426439
self.ui.result_textBrowser.setText(self.result.fit_report())
427440

428441
elif fit_func == "Multiple Gaussians" and self.ui.subtract_bck_checkBox.isChecked() == True:
429-
self.ui.result_textBrowser.setText("Not Implemented Yet!")
442+
#currently only works for triple gaussian (n=3)
443+
multiple_gauss = Multi_Gaussian(self.file, self.bck_file, 3, wlref=self.wlref_file)
444+
if self.ui.adjust_param_checkBox.isChecked():
445+
center1_min = self.ui.multi_peakcenter1_min_spinBox.value()
446+
center1_max = self.ui.multi_peakcenter1_max_spinBox.value()
447+
center2_min = self.ui.multi_peakcenter2_min_spinBox.value()
448+
center2_max = self.ui.multi_peakcenter2_max_spinBox.value()
449+
center3_min = self.ui.multi_peakcenter3_min_spinBox.value()
450+
center3_max = self.ui.multi_peakcenter3_max_spinBox.value()
451+
center1_guess = self.ui.multi_peakcenter1_guess_spinBox.value()
452+
sigma1_guess = self.ui.multi_sigma1_guess_spinBox.value()
453+
center2_guess = self.ui.multi_peakcenter2_guess_spinBox.value()
454+
sigma2_guess = self.ui.multi_sigma2_guess_spinBox.value()
455+
center3_guess = self.ui.multi_peakcenter3_guess_spinBox.value()
456+
sigma3_guess = self.ui.multi_sigma3_guess_spinBox.value()
457+
num_gaussians = 3
458+
peak_pos = [center1_guess, center2_guess, center3_guess]
459+
sigma = [sigma1_guess, sigma2_guess, sigma3_guess]
460+
min_max_range = [ [center1_min, center1_max], [center2_min, center2_max], [center3_min, center3_max] ]
461+
462+
self.result = multiple_gauss.gaussian_model_w_lims(peak_pos, sigma, min_max_range)
463+
else:
464+
self.result = multiple_gauss.gaussian_model()
465+
466+
self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
467+
self.ui.plot.plot(self.x, self.result.best_fit, clear=False, pen='k')
468+
if self.ui.plot_components_checkBox.isChecked():
469+
comps = self.result.eval_components(x=self.x)
470+
self.ui.plot.plot(self.x, comps['g1_'], pen='b', clear=False)
471+
self.ui.plot.plot(self.x, comps['g2_'], pen='g', clear=False)
472+
self.ui.plot.plot(self.x, comps['g3_'], pen='c', clear=False)
473+
self.ui.result_textBrowser.setText(self.result.fit_report())
474+
430475

431476
except Exception as e:
432477
self.ui.result_textBrowser.setText(str(e))
@@ -571,37 +616,37 @@ def close_application(self):
571616

572617

573618
"""Parameter Window GUI and Functions"""
574-
param_file_path = (base_path / "peak_bounds_input.ui").resolve()
619+
# param_file_path = (base_path / "peak_bounds_input.ui").resolve()
575620

576-
param_uiFile = param_file_path
621+
# param_uiFile = param_file_path
577622

578-
param_WindowTemplate, param_TemplateBaseClass = pg.Qt.loadUiType(param_uiFile)
623+
# param_WindowTemplate, param_TemplateBaseClass = pg.Qt.loadUiType(param_uiFile)
579624

580-
class ParamWindow(param_TemplateBaseClass):
625+
# class ParamWindow(param_TemplateBaseClass):
581626

582-
peak_range = QtCore.pyqtSignal(list)
627+
# peak_range = QtCore.pyqtSignal(list)
583628

584-
def __init__(self):
585-
# super(param_TemplateBaseClass, self).__init__()
586-
param_TemplateBaseClass.__init__(self)
629+
# def __init__(self):
630+
# # super(param_TemplateBaseClass, self).__init__()
631+
# param_TemplateBaseClass.__init__(self)
587632

588-
# Create the param window
589-
self.pui = param_WindowTemplate()
590-
self.pui.setupUi(self)
633+
# # Create the param window
634+
# self.pui = param_WindowTemplate()
635+
# self.pui.setupUi(self)
591636

592-
self.pui.pushButton.clicked.connect(self.done)
637+
# self.pui.pushButton.clicked.connect(self.done)
593638

594-
self.show()
639+
# self.show()
595640

596-
def current_peak_range(self):
597-
center_min = self.pui.cent_min_doubleSpinBox.value()
598-
center_max = self.pui.cent_max_doubleSpinBox.value()
599-
return center_min, center_max
641+
# def current_peak_range(self):
642+
# center_min = self.pui.cent_min_doubleSpinBox.value()
643+
# center_max = self.pui.cent_max_doubleSpinBox.value()
644+
# return center_min, center_max
600645

601-
def done(self):
602-
center_min, center_max = self.current_peak_range()
603-
self.peak_range.emit([center_min, center_max])
604-
self.close()
646+
# def done(self):
647+
# center_min, center_max = self.current_peak_range()
648+
# self.peak_range.emit([center_min, center_max])
649+
# self.close()
605650

606651
"""Run the Main Window"""
607652
def run():

0 commit comments

Comments
 (0)