Skip to content

Commit 7da1bb4

Browse files
can now fit and plot spectra in eV, user bound inputs are still in nm but converted to eV in the backend
1 parent f102eac commit 7da1bb4

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

PythonGUI_apps/Spectrum_analysis/Spectra_fit_funcs.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def background_correction(self):
4040
y = y/wlref
4141

4242
if self.fit_in_eV is True:
43-
x = 1240/x
43+
x = np.sort(1240/self.data[:, 0]) # converting to eV and sorting in ascending order
44+
y = [y[i] for i in np.argsort(1240/x)] # sorting argument of y acc to x sorting index
45+
# need to do this because data is collected in wavelength
4446

4547
return [x,y]
4648

@@ -69,12 +71,16 @@ def gaussian_model(self):
6971
# return result #770 760 780 sigma 15
7072
def gaussian_model_w_lims(self, peak_pos, sigma, min_max_range):
7173
x,y = self.background_correction()
74+
if self.fit_in_eV is True:
75+
peak_pos = 1240/peak_pos
76+
sigma = 1240/sigma
77+
min_max_range = np.sort(1240/np.asarray(min_max_range))
7278
gmodel = GaussianModel(prefix = 'g1_') # calling gaussian model
7379
pars = gmodel.guess(y, x=x) # parameters - center, width, height
7480
pars['g1_center'].set(peak_pos, min=min_max_range[0], max=min_max_range[1])
7581
pars['g1_sigma'].set(sigma)
7682
result = gmodel.fit(y, pars, x=x, nan_policy='propagate')
77-
return result #770 760 780 sigma 15
83+
return result
7884

7985
class Single_Lorentzian(Spectra_Fit):
8086
"""Fit a single Lorentzian to the spectrum
@@ -93,6 +99,10 @@ def lorentzian_model(self):
9399

94100
def lorentzian_model_w_lims(self, peak_pos, sigma, min_max_range):
95101
x,y = self.background_correction()
102+
if self.fit_in_eV is True:
103+
peak_pos = 1240/peak_pos
104+
sigma = 1240/sigma
105+
min_max_range = np.sort(1240/np.asarray(min_max_range))
96106
lmodel = LorentzianModel(prefix = 'l1_') # calling lorentzian model
97107
pars = lmodel.guess(y, x=x) # parameters - center, width, height
98108
pars['l1_center'].set(peak_pos, min = min_max_range[0], max = min_max_range[1])
@@ -127,6 +137,10 @@ def gaussian_model_w_lims(self, peak_pos, sigma, min_max_range):
127137
#min_max_range - list containing lists of min and max for peak center. [ [min1, max1], [min2, max2] ]
128138

129139
x,y = self.background_correction()
140+
if self.fit_in_eV is True:
141+
peak_pos = 1240/np.asarray(peak_pos)
142+
sigma = 1240/np.asarray(sigma)
143+
min_max_range = np.sort(1240/np.asarray(min_max_range))
130144
gmodel_1 = GaussianModel(prefix='g1_') # calling gaussian model
131145
pars = gmodel_1.guess(y, x=x) # parameters - center, width, height
132146
pars['g1_center'].set(peak_pos[0], min = min_max_range[0][0], max = min_max_range[0][1])
@@ -136,7 +150,7 @@ def gaussian_model_w_lims(self, peak_pos, sigma, min_max_range):
136150
gmodel_2 = GaussianModel(prefix='g2_')
137151
pars.update(gmodel_2.make_params()) # update parameters - center, width, height
138152
pars['g2_center'].set(peak_pos[1], min = min_max_range[1][0], max = min_max_range[1][1])
139-
pars['g2_sigma'].set(sigma[1], min = composite_pars['g1_sigma'].value)
153+
pars['g2_sigma'].set(sigma[1], min = pars['g1_sigma'].value)
140154
pars['g2_amplitude'].set(min = 0)
141155

142156
gmodel = gmodel_1 + gmodel_2
@@ -192,6 +206,11 @@ def gaussian_model_w_lims(self, peak_pos, sigma, min_max_range):
192206
assert self.num_of_gaussians == len(self.peak_pos), ("Number of gaussians must be equal to the number of peak positions")
193207
assert len(self.min_max_range) == len(self.peak_pos), ("Number of bounds on the range must be equal to the number of peak positions")
194208

209+
if self.fit_in_eV is True:
210+
peak_pos = 1240/np.asarray(peak_pos)
211+
sigma = 1240/np.asarray(sigma)
212+
min_max_range = np.sort(1240/np.asarray(min_max_range))
213+
195214

196215
for i in range(self.num_of_gaussians):
197216

PythonGUI_apps/Spectrum_analysis/Spectra_plot_fit.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ def plot(self):
347347
self.y = self.file[:,1]
348348

349349
self.check_loaded_files()
350-
self.check_eV_state()
351350

352351
if self.check_loaded_files() == True: #check the following conditions if all required files have been provided
353352
if self.ui.subtract_bck_radioButton.isChecked() == True and self.ui.WLRef_checkBox.isChecked() == False:
@@ -365,6 +364,8 @@ def plot(self):
365364

366365
if self.ui.norm_checkBox.isChecked():
367366
self.normalize()
367+
368+
self.check_eV_state()
368369

369370
self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
370371

@@ -384,9 +385,11 @@ def normalize(self):
384385

385386
def check_eV_state(self):
386387
if self.ui.fit_in_eV.isChecked():
387-
self.x = 1240/self.file[:,0]
388+
self.x = np.sort(1240/self.file[:,0])
389+
self.y = [self.y[i] for i in np.argsort(1240/self.file[:,0])]
388390
else:
389391
self.x = self.file[:,0]
392+
self.y = self.file[:,1]
390393

391394
def clear_plot(self):
392395
self.ui.plot.clear()
@@ -430,7 +433,7 @@ def fit_and_plot(self):
430433
pass
431434
else:
432435
self.check_eV_state()
433-
if fit_func == "Single Gaussian": #and self.ui.subtract_bck_radioButton.isChecked() == True:
436+
if fit_func == "Single Gaussian":
434437
single_gauss = Single_Gaussian(self.file, self.bck_file, wlref=self.wlref_file, fit_in_eV=self.ui.fit_in_eV.isChecked())
435438
if self.ui.adjust_param_checkBox.isChecked():
436439
center1_min = self.ui.single_peakcenter1_min_spinBox.value()
@@ -441,7 +444,8 @@ def fit_and_plot(self):
441444
[center1_min, center1_max])
442445
else:
443446
self.result = single_gauss.gaussian_model()
444-
self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
447+
#self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
448+
self.plot()
445449
self.ui.plot.plot(self.x, self.result.best_fit, clear=False, pen='k')
446450
self.ui.result_textBrowser.setText(self.result.fit_report())
447451

@@ -457,7 +461,8 @@ def fit_and_plot(self):
457461
[center1_min, center1_max])
458462
else:
459463
self.result = single_lorentzian.lorentzian_model()
460-
self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
464+
#self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
465+
self.plot()
461466
self.ui.plot.plot(self.x, self.result.best_fit, clear=False, pen='k')
462467
self.ui.result_textBrowser.setText(self.result.fit_report())
463468

@@ -481,7 +486,8 @@ def fit_and_plot(self):
481486
else:
482487
self.result = double_gauss.gaussian_model()
483488

484-
self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
489+
#self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
490+
self.plot()
485491
self.ui.plot.plot(self.x, self.result.best_fit, clear=False, pen='k')
486492
if self.ui.plot_components_checkBox.isChecked():
487493
comps = self.result.eval_components(x=self.x)
@@ -515,7 +521,8 @@ def fit_and_plot(self):
515521
else:
516522
self.result = multiple_gauss.gaussian_model()
517523

518-
self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
524+
#self.ui.plot.plot(self.x, self.y, clear=self.clear_check(), pen='r')
525+
self.plot()
519526
self.ui.plot.plot(self.x, self.result.best_fit, clear=False, pen='k')
520527
if self.ui.plot_components_checkBox.isChecked():
521528
comps = self.result.eval_components(x=self.x)

PythonGUI_apps/Spectrum_analysis/Spectra_plot_fit_gui.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>2284</width>
10-
<height>1434</height>
10+
<height>1476</height>
1111
</rect>
1212
</property>
1313
<property name="minimumSize">
@@ -620,7 +620,7 @@
620620
</font>
621621
</property>
622622
<property name="text">
623-
<string>Fit in eV</string>
623+
<string>Fit/Plot in eV</string>
624624
</property>
625625
<property name="checked">
626626
<bool>true</bool>

0 commit comments

Comments
 (0)