-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfit_try.py
More file actions
226 lines (199 loc) · 8.63 KB
/
fit_try.py
File metadata and controls
226 lines (199 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from PyQt5.QtWidgets import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
#from matplotlib.backends.backend_pgf import FigureCanvasPgf as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
#from PyQt5.QtCore import QChar
import numpy as np
from scipy.optimize import curve_fit
from math import pi, isnan
import pylab
import matplotlib
from matplotlib import rc
from lmfit import Model
#rc('text',usetex=True)
#rc('text.latex', preamble= r'\usepackage{color}')
def fit_func(x, c, a, tau, nu, phi):
y1=c
y2=a*np.exp((-x)/float(tau))
y3=np.sin(2*pi*nu*x+phi)
return y1+y2*y3
class FitPanel(QWidget):
def __init__(self, filepath, parent=None):
super(FitPanel, self).__init__(parent)
x=[]
y=[]
with open(filepath, 'r') as in_file:
input=in_file.readlines()
for line in input:
if '#' in line or 'nan' in line:
continue
x.append(float(line.split('\t')[1].strip('\n')))
y.append(float(line.split('\t')[0]))
self.x=np.asarray(x)/1000.
self.y=np.asarray(y)
for i in range(len(x)):
if isnan(x[i])or isnan(y[i]):
print(x[i], y[i])
self.results=[0 for i in range(len(x))]
self.x_min=0
self.x_max=self.x[len(self.x)-1]
self.i_min=0
self.i_max=len(x)
print('x_max value:'+str(self.x_max))
self.plot=PlotCanvas(self.x, self.y, self.results, self.i_min, self.i_max, self)
self.fit_model=Model(fit_func)
#self.params=self.fit_model.make_params(
self.initGUI()
self.show()
def initGUI(self):
grid=QGridLayout(self)
Fit_widget=QWidget(self)
wgrid=QGridLayout(Fit_widget)
vbox=QVBoxLayout(self)
func_lbl=QLabel(u'Fit Function:\nC + A\u22C5e^(t/\u03C4)\u22C5sin(2\u03C0\u03BD\u22C5t + \u03C6)')
par_lbl=QLabel('Set parameters boundaries:')
fit_btn=QPushButton('Fit')
t_lim_lbl=QLabel('Time interval in which to execute the fit: (default=all data)')
fit_btn.clicked.connect(self.Fit)
self.hboxes=[]
self.WidgetsC=[]
self.WidgetsA=[]
self.WidgetsTau=[]
self.WidgetsOmega=[]
self.WidgetsPhi=[]
self.WidgetsT=[]
self.hboxes.append(self.create_par_box('C', self.WidgetsC))
self.hboxes.append(self.create_par_box('A', self.WidgetsA))
self.hboxes.append(self.create_par_box('\u03C4', self.WidgetsTau))
self.hboxes.append(self.create_par_box('\u03BD', self.WidgetsOmega))
self.hboxes.append(self.create_par_box('\u03C6', self.WidgetsPhi))
self.hbox_t=self.create_par_box('t', self.WidgetsT)
vbox.addStretch()
vbox.addWidget(func_lbl)
vbox.setSpacing(10)
vbox.addWidget(par_lbl)
vbox.setSpacing(10)
for i, box in enumerate(self.hboxes):
vbox.addLayout(box)
vbox.setSpacing(20)
vbox.addWidget(t_lim_lbl)
vbox.addLayout(self.hbox_t)
vbox.addWidget(fit_btn)
vbox.addStretch()
wgrid.addLayout(vbox, 0, 0)
grid.addWidget(self.plot, 0, 0)
grid.addWidget(Fit_widget, 0, 1, 1, 1)
grid.setColumnMinimumWidth(0, 1300)
self.setGeometry(450, 350, 1500, 800)
self.setWindowTitle('Oscillator')
def get_int(self, textwidg):
num,ok = QInputDialog.getDouble(self,"insert number","enter a number")
if ok:
textwidg.setText(str(num))
def create_par_box(self, name, Widgets):
hbox=QHBoxLayout(self)
#Widgets=[]
Widgets.append(QLabel(name+' min:'))
Widgets.append(QLineEdit())
Widgets[1].setMaximumHeight(15)
Widgets[1].setMaximumWidth(30)
Widgets.append(QLabel(name+' max:'))
Widgets.append(QLineEdit())
Widgets[3].setMaximumHeight(15)
Widgets[3].setMaximumWidth(30)
hbox.addStretch()
for i, W in enumerate(Widgets):
hbox.addWidget(W)
hbox.setSpacing(10)
hbox.addStretch()
return hbox
def Fit(self):
if self.WidgetsT[1].text()=='':
self.x_min=0
else:
self.x_min=float(self.WidgetsT[1].text())
if self.WidgetsT[3].text()=='':
self.x_max=float(self.x[len(self.x)-1])
else:
self.x_max=float(self.WidgetsT[3].text())
keep_going=True
i=0
while keep_going:
if i<len(self.x):
if self.x[i]<=self.x_min:
self.i_min=i
if self.x[i]<=self.x_max :
self.i_max=i
else:
keep_going=False
i+=1
else:
keep_going=False
par_mins=[]
par_maxs=[]
try:
par_mins.append(float(self.WidgetsC[1].text()))
par_maxs.append(float(self.WidgetsC[3].text()))
par_mins.append(float(self.WidgetsA[1].text()))
par_maxs.append(float(self.WidgetsA[3].text()))
par_mins.append(float(self.WidgetsTau[1].text()))
par_maxs.append(float(self.WidgetsTau[3].text()))
par_mins.append(float(self.WidgetsOmega[1].text()))
par_maxs.append(float(self.WidgetsOmega[3].text()))
par_mins.append(float(self.WidgetsPhi[1].text()))
par_maxs.append(float(self.WidgetsPhi[3].text()))
self.fit_model.set_param_hint('c', value=(par_maxs[0]-par_mins[0])/2., min=par_mins[0], max=par_maxs[0])
self.fit_model.set_param_hint('a', value=(par_maxs[1]-par_mins[1])/2., min=par_mins[1], max=par_maxs[1])
self.fit_model.set_param_hint('tau', value=(par_maxs[2]-par_mins[2])/2., min=par_mins[2], max=par_maxs[2])
self.fit_model.set_param_hint('nu', value=(par_maxs[3]-par_mins[3])/2., min=par_mins[3], max=par_maxs[3])
self.fit_model.set_param_hint('phi', value=(par_maxs[4]-par_mins[4])/2., min=par_mins[4], max=par_maxs[4])
self.params=self.fit_model.make_params()
self.results=self.fit_model.fit(self.y[self.i_min:self.i_max], self.params, x=self.x[self.i_min:self.i_max])
print("geppo")
except:
self.results=self.fit_model.fit(np.array(self.y[self.i_min:self.i_max]),a=1, c=1, nu=1, tau=1, phi=1, x=np.array(self.x[self.i_min:self.i_max]))
self.best_fit=self.results.best_fit
self.plot.axes.clear()
self.plot.draw()
self.plot.plot(self.x, self.y, self.best_fit, self.i_min, self.i_max, self.results)
class PlotCanvas(FigureCanvas):
def __init__(self, x, y, results, i_min, i_max, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
self.x=x
self.y=y
self.results=results
self.i_min=i_min
self.i_max=i_max
self.results_dic=0
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
self.plot(self.x, self.y, self.results, self.i_min, self.i_max, self.results_dic)
def plot(self, x, y, results, i_min, i_max, results_dic):
ax = self.figure.add_subplot(111)
ax.set_xlabel('time (s)')
ax.set_ylabel('distance (cm)')
#x2=np.linspace(x_min, x_max, int(float(x_max-x_min)*100))
x2=x[i_min:i_max]
y2=results
#y2=fit_func(x2, results[0], results[1], results[2], results[3], results[4])
ax.plot(x, y, label='Data')
ax.plot(x2, y2, label='Fit')
ax.legend()
if results_dic!=0:
fit_string='Fitted values:\n'+'C: '+str(round(results_dic.best_values["c"], 3))+'\nA: '+str(round(results_dic.best_values["a"],3))+'\n\u03C4: '+str(round(results_dic.best_values["tau"],3))+'\n\u03BD: '+str(round(results_dic.best_values["nu"], 3))+'\n\u03C6: '+str(round(results_dic.best_values["phi"], 3))+'\nChi squared value:'+'\nchi: '+str(round(results_dic.chisqr, 3))
ax.text(0.95, 0.1, fit_string,
verticalalignment='bottom', horizontalalignment='right',
transform=ax.transAxes,
fontsize=15)
#color='green', fontsize=15)
ax.set_title('PyQt Matplotlib Example')
self.draw()
def clear(self):
self.clear()