fix fitting

factor in peak index + shift while graphing, only fit relevant data (peak & afterward), fix default A and Tau values to use tick scale, NOT time
master
michael 2021-08-09 09:55:29 -07:00
parent d8b0b6d0fd
commit 04b549d106
4 changed files with 56 additions and 32 deletions

2
app.py
View File

@ -94,6 +94,7 @@ class AppWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.spin_end_time.setDisabled(True) self.spin_end_time.setDisabled(True)
self.check_custom_end.stateChanged.connect(set_end_time) self.check_custom_end.stateChanged.connect(set_end_time)
# Sync up peak detection settings between input locations # Sync up peak detection settings between input locations
self.spin_min_peakheight.valueChanged.connect(lambda x: self.spin_min_peakheight_2.setValue(x)) self.spin_min_peakheight.valueChanged.connect(lambda x: self.spin_min_peakheight_2.setValue(x))
self.spin_min_peakheight_2.valueChanged.connect(lambda x: self.spin_min_peakheight.setValue(x)) self.spin_min_peakheight_2.valueChanged.connect(lambda x: self.spin_min_peakheight.setValue(x))
@ -202,6 +203,7 @@ class AppWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.spin_shift_over_fit.value(), self.spin_shift_over_fit.value(),
self.check_advanced_peak_detection.isChecked() self.check_advanced_peak_detection.isChecked()
) )
mem['shift_over_fit'] = self.spin_shift_over_fit.value()
# print(mem['fit_equations']) # print(mem['fit_equations'])
self.peak_fit_viewer.plot() self.peak_fit_viewer.plot()
self.graph_tabs.setCurrentIndex(4) self.graph_tabs.setCurrentIndex(4)

View File

@ -267,8 +267,6 @@ def fit_peaks(
use_advanced: bool use_advanced: bool
): ):
print(f'{use_advanced=}')
""" """
Returns Returns
------- -------
@ -277,8 +275,10 @@ def fit_peaks(
params_guess = (0.0000, a, y0, tau) params_guess = (0.0000, a, y0, tau)
equations = [] equations = []
overlayed_peak_indices = []
for peaks_cut in isolated_peaks: for peaks_cut in isolated_peaks:
row = [] equation_row = []
overlayed_peak_row = []
for peak_data in peaks_cut: for peak_data in peaks_cut:
x_data = np.arange(len(peak_data)) # just placeholder indices x_data = np.arange(len(peak_data)) # just placeholder indices
if not use_advanced: if not use_advanced:
@ -291,7 +291,31 @@ def fit_peaks(
peak_data_target = peak_data[peak_index+shift_over:] peak_data_target = peak_data[peak_index+shift_over:]
# popt, pcov = curve_fit(exp_func, x_data_target, peak_data_target, bounds=([-np.inf, 0.0, -np.inf, 0.0], np.inf)) # popt, pcov = curve_fit(exp_func, x_data_target, peak_data_target, bounds=([-np.inf, 0.0, -np.inf, 0.0], np.inf))
popt, pcov = curve_fit(exp_func, x_data_target, peak_data_target, bounds=([-np.inf, 0.0, -np.inf, 0.0], np.inf), p0=params_guess, maxfev=10000000) popt, pcov = curve_fit(exp_func, x_data_target, peak_data_target, bounds=([-np.inf, 0.0, -np.inf, 0.0], np.inf), p0=params_guess, maxfev=10000000)
row.append({'popt': popt, 'pcov': pcov}) equation_row.append({'popt': popt, 'pcov': pcov})
equations.append(row) overlayed_peak_row.append(peak_index)
equations.append(equation_row)
overlayed_peak_indices.append(overlayed_peak_row)
mem['overlayed_peak_indices'] = overlayed_peak_indices
return equations # list linked with isolated_peaks return equations # list linked with isolated_peaks
def get_tau_data(equation_data):
"""
Extracts time constant from all equations (2d array)
Returns
-------
Tau data in same dimensions as `equation_data`
"""
tau_data = []
for r in equation_data:
row = []
for e in r:
tau = e['popt'][3]
row.append(tau)
equation_data.append(row)
return tau_data

View File

@ -39,9 +39,9 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>-158</y>
<width>261</width> <width>261</width>
<height>812</height> <height>818</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
@ -141,8 +141,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>221</width> <width>222</width>
<height>108</height> <height>101</height>
</rect> </rect>
</property> </property>
<layout class="QFormLayout" name="formLayout"> <layout class="QFormLayout" name="formLayout">
@ -268,8 +268,8 @@
<rect> <rect>
<x>10</x> <x>10</x>
<y>160</y> <y>160</y>
<width>230</width> <width>235</width>
<height>52</height> <height>53</height>
</rect> </rect>
</property> </property>
<layout class="QFormLayout" name="formLayout_5"> <layout class="QFormLayout" name="formLayout_5">
@ -371,7 +371,7 @@
<x>9</x> <x>9</x>
<y>20</y> <y>20</y>
<width>221</width> <width>221</width>
<height>108</height> <height>111</height>
</rect> </rect>
</property> </property>
<layout class="QFormLayout" name="formLayout_2"> <layout class="QFormLayout" name="formLayout_2">
@ -531,7 +531,7 @@
<x>10</x> <x>10</x>
<y>90</y> <y>90</y>
<width>221</width> <width>221</width>
<height>106</height> <height>111</height>
</rect> </rect>
</property> </property>
<layout class="QFormLayout" name="formLayout_3"> <layout class="QFormLayout" name="formLayout_3">
@ -542,19 +542,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1">
<widget class="QDoubleSpinBox" name="spin_var_tau">
<property name="suffix">
<string/>
</property>
<property name="decimals">
<number>8</number>
</property>
<property name="value">
<double>0.000002000000000</double>
</property>
</widget>
</item>
<item row="3" column="0"> <item row="3" column="0">
<widget class="QLabel" name="label_13"> <widget class="QLabel" name="label_13">
<property name="text"> <property name="text">
@ -582,7 +569,7 @@
<number>6</number> <number>6</number>
</property> </property>
<property name="value"> <property name="value">
<double>1.000500000000000</double> <double>0.000500000000000</double>
</property> </property>
</widget> </widget>
</item> </item>
@ -603,6 +590,16 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1">
<widget class="QSpinBox" name="spin_var_tau">
<property name="maximum">
<number>10000</number>
</property>
<property name="value">
<number>150</number>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<widget class="QGraphicsView" name="equation_view"> <widget class="QGraphicsView" name="equation_view">
@ -641,8 +638,8 @@
<rect> <rect>
<x>10</x> <x>10</x>
<y>260</y> <y>260</y>
<width>221</width> <width>222</width>
<height>81</height> <height>82</height>
</rect> </rect>
</property> </property>
<layout class="QFormLayout" name="formLayout_6"> <layout class="QFormLayout" name="formLayout_6">
@ -833,7 +830,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>279</width> <width>279</width>
<height>641</height> <height>633</height>
</rect> </rect>
</property> </property>
<widget class="QGroupBox" name="groupBox_3"> <widget class="QGroupBox" name="groupBox_3">

View File

@ -103,9 +103,10 @@ class FitGraph(BaseGraph):
for g_i in range(len(mem['isolated_peaks'])): for g_i in range(len(mem['isolated_peaks'])):
peak = mem['isolated_peaks'][g_i][self.peak_index] peak = mem['isolated_peaks'][g_i][self.peak_index]
x_data = np.arange(len(peak)) x_data = np.arange(len(peak))
x_data_target = x_data[mem['overlayed_peak_indices'][g_i][self.peak_index]+mem['shift_over_fit']:]
popt = mem['fit_equations'][g_i][self.peak_index]['popt'] popt = mem['fit_equations'][g_i][self.peak_index]['popt']
self.canv.axes.plot(peak) self.canv.axes.plot(peak)
self.canv.axes.plot(x_data, exp_func(x_data, *popt), color='red') self.canv.axes.plot(x_data_target, exp_func(x_data_target, *popt), color='red')
class FitsGraphViewer(QtWidgets.QTabWidget): class FitsGraphViewer(QtWidgets.QTabWidget):
def __init__(self, x): def __init__(self, x):