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

Commit e68f729

Browse files
handle exceptions in fit and export
1 parent 56b0ea9 commit e68f729

1 file changed

Lines changed: 82 additions & 74 deletions

File tree

PythonGUI_apps/Lifetime_analysis/Lifetime_plot_fit.py

Lines changed: 82 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -130,84 +130,92 @@ def clear_plot(self):
130130
self.ui.Result_textBrowser.clear()
131131

132132
def fit_and_plot(self):
133-
134-
x,y = self.acquire_settings()
135-
136-
y_norm = y/np.max(y)
137-
# find the max intensity in the array and start things from there
138-
find_max_int = np.nonzero(y_norm == 1)
139-
y = y[np.asscalar(find_max_int[0]):]
140-
141-
resolution = float(self.ui.Res_comboBox.currentText())
142-
# x = x[np.asscalar(find_max_int[0]):]
143-
x = np.arange(0, len(y), 1) * resolution
144-
145-
t = x
146-
147-
time_fit = t
148-
TRPL_interp = np.interp(time_fit, t, y)
149-
150-
fit_func = self.ui.FittingFunc_comboBox.currentText()
151-
self.ui.plot.plot(t, y, clear=True, pen='r')
152-
153-
if fit_func == "Stretched Exponential":
154-
tc, beta, a, avg_tau, PL_fit = stretch_exp_fit(TRPL_interp, t)
155-
self.out = np.empty((len(t), 3))
156-
self.out[:,0] = t #time
157-
self.out[:,1] = TRPL_interp #Raw PL
158-
self.out[:,2] = PL_fit # PL fit
159-
self.ui.plot.plot(t, PL_fit, clear=False, pen='k')
160-
self.ui.Result_textBrowser.setText("Fit Results:\n\nFit Function: Stretched Exponential"
161-
"\nAverage Lifetime = " + str(avg_tau)+ " ns"
162-
"\nCharacteristic Tau = " + str(tc)+" ns"
163-
"\nBeta = "+str(beta))
164-
165-
elif fit_func == "Double Exponential":
166-
tau1, a1, tau2, a2, avg_tau, PL_fit = double_exp_fit(TRPL_interp, t)
167-
self.out = np.empty((len(t), 3))
168-
self.out[:,0] = t #time
169-
self.out[:,1] = TRPL_interp #Raw PL
170-
self.out[:,2] = PL_fit # PL fit
171-
self.ui.plot.plot(t, PL_fit, clear=False, pen='k')
172-
self.ui.Result_textBrowser.setText("Fit Results:\n\nFit Function: Double Exponential"
173-
"\nAverage Lifetime = " + str(avg_tau)+ " ns"
174-
"\nTau 1 = " + str(tau1)+" ns"
175-
"\nA 1 = " + str(a1)+
176-
"\nTau 2 = " + str(tau2)+" ns"
177-
"\nA 2 = " + str(a2))
178-
179-
elif fit_func == "Single Exponential":
180-
tau, a, PL_fit = single_exp_fit(TRPL_interp, t)
181-
self.out = np.empty((len(t), 3))
182-
self.out[:,0] = t #time
183-
self.out[:,1] = TRPL_interp #Raw PL
184-
self.out[:,2] = PL_fit # PL fit
185-
self.ui.plot.plot(t, PL_fit, clear=False, pen='k')
186-
self.ui.Result_textBrowser.setText("Fit Results:\n\nFit Function: Single Exponential"
187-
"\nLifetime = " + str(tau)+ " ns"
188-
"\nA = " + str(a))
133+
try:
134+
x,y = self.acquire_settings()
189135

190-
self.ui.plot.setLabel('left', 'Intensity', units='a.u.')
191-
self.ui.plot.setLabel('bottom', 'Time (ns)')
192-
return self.out
136+
y_norm = y/np.max(y)
137+
# find the max intensity in the array and start things from there
138+
find_max_int = np.nonzero(y_norm == 1)
139+
y = y[np.asscalar(find_max_int[0]):]
140+
141+
resolution = float(self.ui.Res_comboBox.currentText())
142+
# x = x[np.asscalar(find_max_int[0]):]
143+
x = np.arange(0, len(y), 1) * resolution
144+
145+
t = x
146+
147+
time_fit = t
148+
TRPL_interp = np.interp(time_fit, t, y)
149+
150+
fit_func = self.ui.FittingFunc_comboBox.currentText()
151+
self.ui.plot.plot(t, y, clear=True, pen='r')
152+
153+
if fit_func == "Stretched Exponential":
154+
tc, beta, a, avg_tau, PL_fit = stretch_exp_fit(TRPL_interp, t)
155+
self.out = np.empty((len(t), 3))
156+
self.out[:,0] = t #time
157+
self.out[:,1] = TRPL_interp #Raw PL
158+
self.out[:,2] = PL_fit # PL fit
159+
self.ui.plot.plot(t, PL_fit, clear=False, pen='k')
160+
self.ui.Result_textBrowser.setText("Fit Results:\n\nFit Function: Stretched Exponential"
161+
"\nAverage Lifetime = " + str(avg_tau)+ " ns"
162+
"\nCharacteristic Tau = " + str(tc)+" ns"
163+
"\nBeta = "+str(beta))
164+
165+
elif fit_func == "Double Exponential":
166+
tau1, a1, tau2, a2, avg_tau, PL_fit = double_exp_fit(TRPL_interp, t)
167+
self.out = np.empty((len(t), 3))
168+
self.out[:,0] = t #time
169+
self.out[:,1] = TRPL_interp #Raw PL
170+
self.out[:,2] = PL_fit # PL fit
171+
self.ui.plot.plot(t, PL_fit, clear=False, pen='k')
172+
self.ui.Result_textBrowser.setText("Fit Results:\n\nFit Function: Double Exponential"
173+
"\nAverage Lifetime = " + str(avg_tau)+ " ns"
174+
"\nTau 1 = " + str(tau1)+" ns"
175+
"\nA 1 = " + str(a1)+
176+
"\nTau 2 = " + str(tau2)+" ns"
177+
"\nA 2 = " + str(a2))
178+
179+
elif fit_func == "Single Exponential":
180+
tau, a, PL_fit = single_exp_fit(TRPL_interp, t)
181+
self.out = np.empty((len(t), 3))
182+
self.out[:,0] = t #time
183+
self.out[:,1] = TRPL_interp #Raw PL
184+
self.out[:,2] = PL_fit # PL fit
185+
self.ui.plot.plot(t, PL_fit, clear=False, pen='k')
186+
self.ui.Result_textBrowser.setText("Fit Results:\n\nFit Function: Single Exponential"
187+
"\nLifetime = " + str(tau)+ " ns"
188+
"\nA = " + str(a))
189+
190+
self.ui.plot.setLabel('left', 'Intensity', units='a.u.')
191+
self.ui.plot.setLabel('bottom', 'Time (ns)')
192+
return self.out
193+
194+
except:
195+
pass
193196

194197
def pub_ready_plot_export(self):
195-
filename = QtWidgets.QFileDialog.getSaveFileName(self,caption="Filename with EXTENSION")
196-
197-
plt.figure(figsize=(8,6))
198-
plt.tick_params(direction='out', length=8, width=3.5)
199-
if self.ui.save_w_fit_checkBox.isChecked():
200-
plt.plot(self.out[:,0],self.out[:,1]/np.max(self.out[:,1]))
201-
plt.plot(self.out[:,0],self.out[:,2]/np.max(self.out[:,1]),'k')
202-
else:
203-
plt.plot(self.acquire_settings()[0],self.acquire_settings()[1]/np.max(self.acquire_settings()[1]))
204-
plt.yscale('log')
205-
plt.xlabel("Time (ns)", fontsize=20, fontweight='bold')
206-
plt.ylabel("Intensity (norm.)", fontsize=20, fontweight='bold')
207-
plt.tight_layout()
198+
try:
199+
filename = QtWidgets.QFileDialog.getSaveFileName(self,caption="Filename with EXTENSION")
200+
201+
plt.figure(figsize=(8,6))
202+
plt.tick_params(direction='out', length=8, width=3.5)
203+
if self.ui.save_w_fit_checkBox.isChecked():
204+
plt.plot(self.out[:,0],self.out[:,1]/np.max(self.out[:,1]))
205+
plt.plot(self.out[:,0],self.out[:,2]/np.max(self.out[:,1]),'k')
206+
else:
207+
plt.plot(self.acquire_settings()[0],self.acquire_settings()[1]/np.max(self.acquire_settings()[1]))
208+
plt.yscale('log')
209+
plt.xlabel("Time (ns)", fontsize=20, fontweight='bold')
210+
plt.ylabel("Intensity (norm.)", fontsize=20, fontweight='bold')
211+
plt.tight_layout()
212+
213+
plt.savefig(filename[0],bbox_inches='tight', dpi=300)
214+
plt.close()
208215

209-
plt.savefig(filename[0],bbox_inches='tight', dpi=300)
210-
plt.close()
216+
except:
217+
pass
218+
211219

212220
def close_application(self):
213221
choice = QtGui.QMessageBox.question(self, 'EXIT!',

0 commit comments

Comments
 (0)