1 | #!/usr/bin/python |
---|
2 | |
---|
3 | # fitDialog.py |
---|
4 | |
---|
5 | import wx |
---|
6 | from PlotPanel import PlotPanel |
---|
7 | from plottables import Theory1D |
---|
8 | import math,pylab,fittings |
---|
9 | import transform |
---|
10 | |
---|
11 | |
---|
12 | class LinearFit(wx.Dialog): |
---|
13 | def __init__(self, parent, plottable, push_data,transform, id, title): |
---|
14 | wx.Dialog.__init__(self, parent, id, title, size=(450, 400)) |
---|
15 | """ |
---|
16 | Dialog window pops- up when select Linear fit on Context menu |
---|
17 | Displays fitting parameters |
---|
18 | """ |
---|
19 | self.parent = parent |
---|
20 | self.transform = transform |
---|
21 | #dialog panel self call function to plot the fitting function |
---|
22 | self.push_data = push_data |
---|
23 | #dialog self plottable |
---|
24 | |
---|
25 | self.plottable = plottable |
---|
26 | # Receive transformations of x and y |
---|
27 | self.xLabel,self.yLabel,self.Avalue,self.Bvalue,\ |
---|
28 | self.ErrAvalue,self.ErrBvalue,self.Chivalue= self.transform() |
---|
29 | #Dialog interface |
---|
30 | panel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER) |
---|
31 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
32 | sizer = wx.GridBagSizer(5,5) |
---|
33 | |
---|
34 | vbox.Add(panel, 1, wx.EXPAND | wx.ALL) |
---|
35 | |
---|
36 | self.tcA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
37 | self.tcErrA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
38 | self.tcB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
39 | self.tcErrB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
40 | self.tcChi = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
41 | self.xminFit = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
42 | self.xmaxFit = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
43 | self.xminTransFit = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
44 | self.xmaxTransFit = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
45 | self.initXmin = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
46 | self.initXmax = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
47 | self.btFit =wx.Button(panel,-1,'Fit',size=(120, 30)) |
---|
48 | self.btClose =wx.Button(panel, wx.ID_CANCEL,'Close',size=(90, 30) ) |
---|
49 | self.static_line_1 = wx.StaticLine(panel, -1) |
---|
50 | |
---|
51 | ix = 0 |
---|
52 | iy = 1 |
---|
53 | sizer.Add(wx.StaticText(panel, -1, 'y = Ax +B'),(iy, ix),(1,1),\ |
---|
54 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
55 | iy+=1 |
---|
56 | sizer.Add(wx.StaticText(panel, -1, 'Param A'),(iy, ix),\ |
---|
57 | (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
58 | ix += 1 |
---|
59 | sizer.Add(self.tcA,(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
60 | ix += 1 |
---|
61 | sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
62 | ix += 1 |
---|
63 | sizer.Add(self.tcErrA, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
64 | iy += 1 |
---|
65 | ix = 0 |
---|
66 | sizer.Add(wx.StaticText(panel, -1, 'Param B'),(iy, ix),(1,1),\ |
---|
67 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
68 | ix += 1 |
---|
69 | sizer.Add(self.tcB, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
70 | ix += 1 |
---|
71 | sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
72 | ix += 1 |
---|
73 | sizer.Add(self.tcErrB, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
74 | iy += 1 |
---|
75 | ix = 0 |
---|
76 | sizer.Add(wx.StaticText(panel, -1, 'Chi ^{2}'),(iy, ix),(1,1),\ |
---|
77 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
78 | ix += 1 |
---|
79 | sizer.Add(self.tcChi, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
80 | iy += 1 |
---|
81 | ix = 1 |
---|
82 | sizer.Add(wx.StaticText(panel, -1, 'Xmin'),(iy, ix),(1,1),\ |
---|
83 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
84 | ix += 2 |
---|
85 | sizer.Add(wx.StaticText(panel, -1, 'Xmax'),(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
86 | iy += 1 |
---|
87 | ix = 0 |
---|
88 | sizer.Add(wx.StaticText(panel, -1, 'Plotted Range'),(iy, ix),(1,1),\ |
---|
89 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
90 | ix +=1 |
---|
91 | sizer.Add(self.initXmin, (iy, ix),(1,1),\ |
---|
92 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
93 | ix += 2 |
---|
94 | sizer.Add(self.initXmax, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
95 | |
---|
96 | iy += 1 |
---|
97 | ix = 0 |
---|
98 | sizer.Add(wx.StaticText(panel, -1, 'Fit Range of '+self.xLabel),(iy, ix),(1,1),\ |
---|
99 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
100 | ix += 1 |
---|
101 | sizer.Add(self.xminTransFit, (iy, ix),(1,1),\ |
---|
102 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
103 | ix += 2 |
---|
104 | sizer.Add(self.xmaxTransFit, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
105 | |
---|
106 | iy += 1 |
---|
107 | ix = 0 |
---|
108 | sizer.Add(wx.StaticText(panel, -1, 'Fit Range of x'),(iy, ix),(1,1),\ |
---|
109 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
110 | ix += 1 |
---|
111 | sizer.Add(self.xminFit, (iy, ix),(1,1),\ |
---|
112 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
113 | ix += 2 |
---|
114 | sizer.Add(self.xmaxFit, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
115 | iy += 1 |
---|
116 | ix = 1 |
---|
117 | |
---|
118 | sizer.Add(self.btFit, (iy, ix),(1,1), wx.LEFT|wx.ADJUST_MINSIZE, 0) |
---|
119 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit) |
---|
120 | ix += 2 |
---|
121 | sizer.Add(self.btClose, (iy, ix),(1,1),\ |
---|
122 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
123 | |
---|
124 | panel.SetSizer(sizer) |
---|
125 | self.SetSizer(vbox) |
---|
126 | self.Centre() |
---|
127 | |
---|
128 | # Receives the type of model for the fitting |
---|
129 | from LineModel import LineModel |
---|
130 | self.model = LineModel() |
---|
131 | |
---|
132 | #Display the fittings values |
---|
133 | self.default_A = self.model.getParam('A') |
---|
134 | self.default_B = self.model.getParam('B') |
---|
135 | self.cstA = fittings.Parameter(self.model, 'A', self.default_A) |
---|
136 | self.cstB = fittings.Parameter(self.model, 'B', self.default_B) |
---|
137 | |
---|
138 | # Set default value of parameter in fit dialog |
---|
139 | |
---|
140 | if self.Avalue==None: |
---|
141 | self.tcA.SetLabel(str(self.default_A)) |
---|
142 | else : |
---|
143 | self.tcA.SetLabel(str(self.Avalue)) |
---|
144 | if self.Bvalue==None: |
---|
145 | self.tcB.SetLabel(str(self.default_B)) |
---|
146 | else: |
---|
147 | self.tcB.SetLabel(str(self.Bvalue)) |
---|
148 | if self.ErrAvalue==None: |
---|
149 | self.tcErrA.SetLabel(str(0.0)) |
---|
150 | else: |
---|
151 | self.tcErrA.SetLabel(str(self.ErrAvalue)) |
---|
152 | if self.ErrBvalue==None: |
---|
153 | self.tcErrB.SetLabel(str(0.0)) |
---|
154 | else: |
---|
155 | self.tcErrB.SetLabel(str(self.ErrBvalue)) |
---|
156 | if self.Chivalue==None: |
---|
157 | self.tcChi.SetLabel(str(0.0)) |
---|
158 | else: |
---|
159 | self.tcChi.SetLabel(str(self.Chivalue)) |
---|
160 | if self.plottable.x !=[]: |
---|
161 | self.mini =min(self.plottable.x) |
---|
162 | self.maxi =max(self.plottable.x) |
---|
163 | #store the values of View in self.x,self.y,self.dx,self.dy |
---|
164 | self.x,self.y,self.dx,self.dy= self.plottable.returnValuesOfView() |
---|
165 | |
---|
166 | self.xminTransFit.SetLabel(str(min(self.x))) |
---|
167 | self.xmaxTransFit.SetLabel(str(max(self.x))) |
---|
168 | self.xminTransFit.Disable() |
---|
169 | self.xmaxTransFit.Disable() |
---|
170 | |
---|
171 | self.initXmin.SetValue(str(self.mini)) |
---|
172 | self.initXmax.SetValue(str(self.maxi)) |
---|
173 | self.initXmin.Disable() |
---|
174 | self.initXmax.Disable() |
---|
175 | |
---|
176 | self.xminFit.SetLabel(str(self.mini)) |
---|
177 | self.xmaxFit.SetLabel(str(self.maxi)) |
---|
178 | |
---|
179 | |
---|
180 | def _onFit(self ,event): |
---|
181 | """ |
---|
182 | Performs the fit. Receive an event when clicking on the button Fit.Computes chisqr , |
---|
183 | A and B parameters of the best linear fit y=Ax +B |
---|
184 | Push a plottable to |
---|
185 | """ |
---|
186 | tempx=[] |
---|
187 | tempy=[] |
---|
188 | tempdy = [] |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | # Check if View contains a x array .we online fit when x exits |
---|
194 | # makes transformation for y as a line to fit |
---|
195 | if self.x != []: |
---|
196 | |
---|
197 | |
---|
198 | if(self.checkFitValues(self.xminFit) == True): |
---|
199 | #Check if the field of Fit Dialog contain values and use the x max and min of the user |
---|
200 | xmin,xmax = self._checkVal(self.xminFit.GetValue(),self.xmaxFit.GetValue()) |
---|
201 | |
---|
202 | xminView=self.floatTransform(xmin) |
---|
203 | xmaxView=self.floatTransform(xmax) |
---|
204 | if (self.xLabel=="log10(x)"): |
---|
205 | self.xminTransFit.SetValue(str(math.log10(xminView))) |
---|
206 | self.xmaxTransFit.SetValue(str(math.log10(xmaxView))) |
---|
207 | else: |
---|
208 | self.xminTransFit.SetValue(str(xminView)) |
---|
209 | self.xmaxTransFit.SetValue(str(xmaxView)) |
---|
210 | self.xminTransFit.Disable() |
---|
211 | self.xmaxTransFit.Disable() |
---|
212 | # Store the transformed values of view x, y,dy in variables before the fit |
---|
213 | if self.yLabel.lower() == "log10(y)": |
---|
214 | if (self.xLabel.lower() == "log10(x)"): |
---|
215 | for i in range(len(self.x)): |
---|
216 | if self.x[i]>= math.log10(xmin): |
---|
217 | tempy.append(math.log10(self.y[i])) |
---|
218 | tempdy.append(transform.errToLogX(self.y[i],0,self.dy[i],0)) |
---|
219 | else: |
---|
220 | for i in range(len(self.y)): |
---|
221 | tempy.append(math.log10(self.y[i])) |
---|
222 | tempdy.append(transform.errToLogX(self.y[i],0,self.dy[i],0)) |
---|
223 | else: |
---|
224 | tempy = self.y |
---|
225 | tempdy = self.dy |
---|
226 | |
---|
227 | if (self.xLabel.lower() == "log10(x)"): |
---|
228 | for x_i in self.x: |
---|
229 | if x_i >= math.log10(xmin): |
---|
230 | tempx.append(math.log10(x_i)) |
---|
231 | else: |
---|
232 | tempx = self.x |
---|
233 | |
---|
234 | #Find the fitting parameters |
---|
235 | |
---|
236 | if (self.xLabel.lower() == "log10(x)"): |
---|
237 | chisqr, out, cov = fittings.sansfit(self.model, [self.cstA, self.cstB], |
---|
238 | tempx, tempy,tempdy,math.log10(xmin),math.log10(xmax)) |
---|
239 | else: |
---|
240 | chisqr, out, cov = fittings.sansfit(self.model, |
---|
241 | [self.cstA, self.cstB],tempx, tempy,tempdy,xminView,xmaxView) |
---|
242 | #print "this out",out |
---|
243 | #Check that cov and out are iterable before displaying them |
---|
244 | if cov ==None: |
---|
245 | errA =0.0 |
---|
246 | errB =0.0 |
---|
247 | else: |
---|
248 | errA= math.sqrt(cov[0][0]) |
---|
249 | errB= math.sqrt(cov[1][1]) |
---|
250 | if out==None: |
---|
251 | cstA=0.0 |
---|
252 | cstB=0.0 |
---|
253 | else: |
---|
254 | cstA=out[0] |
---|
255 | cstB=out[1] |
---|
256 | # Reset model with the right values of A and B |
---|
257 | self.model.setParam('A', float(cstA)) |
---|
258 | self.model.setParam('B', float(cstB)) |
---|
259 | |
---|
260 | tempx = [] |
---|
261 | tempy = [] |
---|
262 | y_model = 0.0 |
---|
263 | # load tempy with the minimum transformation |
---|
264 | |
---|
265 | if self.xLabel == "log10(x)": |
---|
266 | y_model = self.model.run(math.log10(xmin)) |
---|
267 | tempx.append(xmin) |
---|
268 | else: |
---|
269 | y_model = self.model.run(xminView) |
---|
270 | tempx.append(xminView) |
---|
271 | |
---|
272 | if self.yLabel == "log10(y)": |
---|
273 | tempy.append(math.pow(10,y_model)) |
---|
274 | print "tempy",tempy |
---|
275 | else: |
---|
276 | tempy.append(y_model) |
---|
277 | |
---|
278 | # load tempy with the maximum transformation |
---|
279 | if self.xLabel == "log10(x)": |
---|
280 | y_model = self.model.run(math.log10(xmax)) |
---|
281 | tempx.append(xmax) |
---|
282 | else: |
---|
283 | y_model = self.model.run(xmaxView) |
---|
284 | tempx.append(xmaxView) |
---|
285 | |
---|
286 | if self.yLabel == "log10(y)": |
---|
287 | tempy.append(math.pow(10,y_model)) |
---|
288 | else: |
---|
289 | tempy.append(y_model) |
---|
290 | #Set the fit parameter display when FitDialog is opened again |
---|
291 | self.Avalue=cstB |
---|
292 | self.Bvalue=cstA |
---|
293 | self.ErrAvalue=errA |
---|
294 | self.ErrBvalue=errB |
---|
295 | self.Chivalue=chisqr |
---|
296 | self.push_data(tempx,tempy,xminView,xmaxView,xmin,xmax,self._ongetValues()) |
---|
297 | |
---|
298 | # Display the fitting value on the Fit Dialog |
---|
299 | self._onsetValues(cstB, cstA, errA,errB,chisqr) |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | def _onsetValues(self,cstA,cstB,errA,errB,Chi): |
---|
304 | """ |
---|
305 | Display the value on fit Dialog |
---|
306 | """ |
---|
307 | self.tcA.SetValue(str(cstA)) |
---|
308 | self.tcB.SetValue(str(cstB)) |
---|
309 | self.tcErrA.SetValue(str(errA)) |
---|
310 | self.tcErrB.SetValue(str(errB)) |
---|
311 | self.tcChi.SetValue(str(Chi)) |
---|
312 | |
---|
313 | def _ongetValues(self): |
---|
314 | """ |
---|
315 | Display the value on fit Dialog |
---|
316 | """ |
---|
317 | return self.Avalue, self.Bvalue,self.ErrAvalue,self.ErrBvalue,self.Chivalue |
---|
318 | |
---|
319 | |
---|
320 | def _checkVal(self,usermin, usermax): |
---|
321 | """ |
---|
322 | Ensure that fields parameter contains a min and a max value |
---|
323 | within x min and x max range |
---|
324 | """ |
---|
325 | if float(usermin) < float(usermax): |
---|
326 | if float(usermin) >= float(self.mini) and float(usermin) < float(self.maxi): |
---|
327 | self.xminFit.SetValue(str(usermin)) |
---|
328 | else: |
---|
329 | self.xminFit.SetValue(str(self.mini)) |
---|
330 | |
---|
331 | if float(usermax) > float(self.mini) and float(usermax) <= float(self.maxi): |
---|
332 | self.xmaxFit.SetLabel(str(usermax)) |
---|
333 | else: |
---|
334 | self.xmaxFit.SetLabel(str(self.maxi)) |
---|
335 | |
---|
336 | mini =float(self.xminFit.GetValue()) |
---|
337 | maxi =float(self.xmaxFit.GetValue()) |
---|
338 | |
---|
339 | return mini, maxi |
---|
340 | def floatTransform(self,x): |
---|
341 | """ |
---|
342 | transform a float.It is use to determine the x.View min and x.View max for values |
---|
343 | not in x |
---|
344 | """ |
---|
345 | if ( self.xLabel=="x" ): |
---|
346 | return transform.toX(x) |
---|
347 | |
---|
348 | if ( self.xLabel=="x^(2)" ): |
---|
349 | return transform.toX2(x) |
---|
350 | |
---|
351 | if (self.xLabel=="log10(x)" ): |
---|
352 | if x >0: |
---|
353 | return x |
---|
354 | else: |
---|
355 | raise ValueError,"cannot compute log of a negative number" |
---|
356 | |
---|
357 | def checkFitValues(self,item): |
---|
358 | """ |
---|
359 | Check the validity of input values |
---|
360 | """ |
---|
361 | flag = True |
---|
362 | value = item.GetValue() |
---|
363 | # Check for possible values entered |
---|
364 | if (self.xLabel=="log10(x)"): |
---|
365 | if (float(value) > 0): |
---|
366 | item.SetBackgroundColour(wx.WHITE) |
---|
367 | item.Refresh() |
---|
368 | else: |
---|
369 | flag = False |
---|
370 | item.SetBackgroundColour("pink") |
---|
371 | item.Refresh() |
---|
372 | |
---|
373 | return flag |
---|
374 | |
---|
375 | def setFitRange(self,xmin,xmax,xminTrans,xmaxTrans): |
---|
376 | """ |
---|
377 | Set fit parameters |
---|
378 | """ |
---|
379 | self.xminFit.SetValue(str(xmin)) |
---|
380 | self.xmaxFit.SetValue(str(xmax)) |
---|
381 | self.xminTransFit.SetValue(str(xminTrans)) |
---|
382 | self.xmaxTransFit.SetValue(str(xmaxTrans)) |
---|
383 | |
---|
384 | |
---|
385 | if __name__ == "__main__": |
---|
386 | app = wx.App() |
---|
387 | dialog=LinearFit(None, -1, 'Fitting') |
---|
388 | dialog.ShowModal() |
---|
389 | app.MainLoop() |
---|
390 | |
---|
391 | |
---|