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

Commit 181abd8

Browse files
added lorentzian model and fixed kilo nm unit situation
1 parent 438ae06 commit 181abd8

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

PythonGUI_apps/Spectrum_analysis/Spectra_fit_funcs.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@author: Sarthak
1313
"""
1414
import numpy as np
15-
from lmfit.models import GaussianModel
15+
from lmfit.models import GaussianModel, LorentzianModel
1616

1717
class Spectra_Fit(object):
1818
"""
@@ -37,7 +37,7 @@ def background_correction(self):
3737
return [x,y]
3838

3939
class Single_Gaussian(Spectra_Fit):
40-
"""Fit a single gaussian to the spectrum, plot it and save it
40+
"""Fit a single gaussian to the spectrum
4141
4242
Attributes:
4343
data: spectrum data (x-axis and y-axis)
@@ -51,6 +51,21 @@ def gaussian_model(self):
5151
result = gmodel.fit(y, pars, x=x, nan_policy='propagate')
5252
return result
5353

54+
class Single_Lorentzian(Spectra_Fit):
55+
"""Fit a single Lorentzian to the spectrum
56+
57+
Attributes:
58+
data: spectrum data (x-axis and y-axis)
59+
ref: reference spectrum (both x and y-axis) for background correction
60+
"""
61+
62+
def lorentzian_model(self):
63+
x,y = self.background_correction()
64+
lmodel = LorentzianModel(prefix = 'l1_') # calling lorentzian model
65+
pars = lmodel.guess(y, x=x) # parameters - center, width, height
66+
result = lmodel.fit(y, pars, x=x, nan_policy='propagate')
67+
return result
68+
5469
class Double_Gaussian(Spectra_Fit):
5570
"""Fit two gaussians to the spectrum
5671

PythonGUI_apps/Spectrum_analysis/Spectra_plot_fit.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
# local modules
1818
try:
19-
from Spectra_fit_funcs import Spectra_Fit, Single_Gaussian
19+
from Spectra_fit_funcs import Spectra_Fit, Single_Gaussian, Single_Lorentzian
2020
except:
21-
from Spectrum_analysis.Spectra_fit_funcs import Spectra_Fit, Single_Gaussian
21+
from Spectrum_analysis.Spectra_fit_funcs import Spectra_Fit, Single_Gaussian, Single_Lorentzian
2222

2323

2424
"""Recylce params for plotting"""
@@ -48,7 +48,7 @@ def __init__(self):
4848
self.ui = WindowTemplate()
4949
self.ui.setupUi(self)
5050

51-
self.ui.fitFunc_comboBox.addItems(["Single Gaussian","Double Gaussian", "Multiple Gaussians"])
51+
self.ui.fitFunc_comboBox.addItems(["Single Gaussian","Single Lorentzian", "Double Gaussian", "Multiple Gaussians"])
5252

5353
# self.ui.actionSave.triggered.connect(self.save_file)
5454
# self.ui.actionExit.triggered.connect(self.close_application)
@@ -119,7 +119,7 @@ def plot(self):
119119

120120
self.ui.plot.plot(self.x, self.y, clear=False, pen='r')
121121
self.ui.plot.setLabel('left', 'Intensity', units='a.u.')
122-
self.ui.plot.setLabel('bottom', 'Wavelength', units='nm')
122+
self.ui.plot.setLabel('bottom', 'Wavelength (nm)')
123123

124124

125125
def normalize(self):
@@ -144,6 +144,14 @@ def fit_and_plot(self):
144144
self.ui.plot.plot(self.x, self.result.best_fit, clear=False, pen='k')
145145
self.ui.result_textBrowser.setText(self.result.fit_report())
146146

147+
elif fit_func == "Single Lorentzian" and self.ui.subtract_bck_checkBox.isChecked() == True:
148+
149+
single_lorentzian = Single_Lorentzian(self.file, self.bck_file)
150+
self.result = single_lorentzian.lorentzian_model()
151+
self.ui.plot.plot(self.x, self.y, clear=True, pen='r')
152+
self.ui.plot.plot(self.x, self.result.best_fit, clear=False, pen='k')
153+
self.ui.result_textBrowser.setText(self.result.fit_report())
154+
147155
elif fit_func == "Double Gaussian" and self.ui.subtract_bck_checkBox.isChecked() == True:
148156
self.ui.result_textBrowser.setText("Not Implemented Yet!")
149157

0 commit comments

Comments
 (0)