1 | import sys |
---|
2 | import wx |
---|
3 | import wx.lib |
---|
4 | import numpy,math |
---|
5 | import copy |
---|
6 | |
---|
7 | from sans.guicomm.events import StatusEvent |
---|
8 | from sans.guiframe.utils import format_number |
---|
9 | from modelpage import ModelPage |
---|
10 | from modelpage import format_number |
---|
11 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
12 | _BOX_WIDTH = 80 |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | class FitPage1D(ModelPage): |
---|
17 | """ |
---|
18 | FitPanel class contains fields allowing to display results when |
---|
19 | fitting a model and one data |
---|
20 | @note: For Fit to be performed the user should check at least one parameter |
---|
21 | on fit Panel window. |
---|
22 | |
---|
23 | """ |
---|
24 | ## Internal name for the AUI manager |
---|
25 | window_name = "Fit page" |
---|
26 | ## Title to appear on top of the window |
---|
27 | window_caption = "Fit Page" |
---|
28 | name=None |
---|
29 | |
---|
30 | def __init__(self, parent,data, *args, **kwargs): |
---|
31 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
32 | """ |
---|
33 | Initialization of the Panel |
---|
34 | """ |
---|
35 | self.data = data |
---|
36 | self.enable2D=False |
---|
37 | self.manager = None |
---|
38 | self.parent = parent |
---|
39 | self.event_owner = None |
---|
40 | #panel interface |
---|
41 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
42 | self.sizer10 = wx.GridBagSizer(5,5) |
---|
43 | self.sizer9 = wx.GridBagSizer(5,5) |
---|
44 | self.sizer8 = wx.GridBagSizer(5,5) |
---|
45 | self.sizer7 = wx.GridBagSizer(5,5) |
---|
46 | self.sizer6 = wx.GridBagSizer(5,5) |
---|
47 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
48 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
49 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
50 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
51 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
52 | # Add layer |
---|
53 | #data info layer |
---|
54 | self.vbox.Add(self.sizer1) |
---|
55 | #data range |
---|
56 | self.vbox.Add(self.sizer2) |
---|
57 | #instrument smearing selection layer |
---|
58 | self.vbox.Add(self.sizer3) |
---|
59 | #model selection |
---|
60 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
61 | self.vbox.Add(self.sizer4) |
---|
62 | #model paramaters layer |
---|
63 | self.vbox.Add(self.sizer5) |
---|
64 | #polydispersion selected |
---|
65 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
66 | self.vbox.Add(self.sizer6) |
---|
67 | #combox box for type of dispersion |
---|
68 | self.vbox.Add(self.sizer7) |
---|
69 | #dispersion parameters layer |
---|
70 | self.vbox.Add(self.sizer8) |
---|
71 | #fit info layer |
---|
72 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
73 | self.vbox.Add(self.sizer9) |
---|
74 | #close layer |
---|
75 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
76 | self.vbox.Add(self.sizer10) |
---|
77 | |
---|
78 | #---------sizer 1 draw-------------------------------- |
---|
79 | self.DataSource =wx.StaticText(self, -1,str(data.name)) |
---|
80 | #Filing the sizer containing data related fields |
---|
81 | ix = 0 |
---|
82 | iy = 1 |
---|
83 | self.sizer1.Add(wx.StaticText(self, -1, 'Data Source Name : '),(iy,ix),\ |
---|
84 | (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
85 | |
---|
86 | ix += 1 |
---|
87 | self.sizer1.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
88 | |
---|
89 | #---------sizer 2 draw-------------------------------- |
---|
90 | #set maximum range for x in linear scale |
---|
91 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
92 | ix = 0 |
---|
93 | iy = 1 |
---|
94 | # Minimum value of data |
---|
95 | self.data_min = wx.StaticText(self, -1,str(format_number(numpy.min(data.x)))) |
---|
96 | # Maximum value of data |
---|
97 | self.data_max = wx.StaticText(self, -1,str(format_number(numpy.max(data.x)))) |
---|
98 | self.text4_3 = wx.StaticText(self, -1, 'Maximum Data Range(Linear)', style=wx.ALIGN_LEFT) |
---|
99 | self.sizer2.Add(self.text4_3,(iy,ix),(1,1),\ |
---|
100 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
101 | ix += 2 |
---|
102 | self.sizer2.Add(wx.StaticText(self, -1, 'Min :'),(iy, ix),(1,1),\ |
---|
103 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
104 | ix += 1 |
---|
105 | self.sizer2.Add(self.data_min,(iy, ix),(1,1),\ |
---|
106 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
107 | ix += 1 |
---|
108 | self.sizer2.Add(wx.StaticText(self, -1, 'Max : '),(iy, ix),(1,1),\ |
---|
109 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
110 | ix += 1 |
---|
111 | self.sizer2.Add(self.data_max,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
112 | |
---|
113 | #----sizer 3 draw-------------------------------- |
---|
114 | self.disable_smearer = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
115 | self.enable_smearer = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
116 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
117 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
118 | ix = 0 |
---|
119 | iy = 1 |
---|
120 | self.sizer3.Add(wx.StaticText(self,-1,'Instrument Smearing'),(iy,ix),(1,1)\ |
---|
121 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
122 | ix += 1 |
---|
123 | self.sizer3.Add(self.enable_smearer,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
124 | ix += 1 |
---|
125 | self.sizer3.Add(self.disable_smearer,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
126 | ix =0 |
---|
127 | iy+=1 |
---|
128 | self.sizer3.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
129 | |
---|
130 | #------------------ sizer 4 draw------------------------ |
---|
131 | self.modelbox = wx.ComboBox(self, -1) |
---|
132 | self.tcChi = wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT) |
---|
133 | self.tcChi.Hide() |
---|
134 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
135 | self.text1_1.Hide() |
---|
136 | #filling sizer2 |
---|
137 | ix = 0 |
---|
138 | iy = 1 |
---|
139 | self.sizer4.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
140 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
141 | ix += 1 |
---|
142 | self.sizer4.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
143 | ix += 1 |
---|
144 | self.sizer4.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
145 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
146 | ix += 1 |
---|
147 | self.sizer4.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
148 | #----------sizer6------------------------------------------------- |
---|
149 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
150 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
151 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.disable_disp.GetId()) |
---|
152 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.enable_disp.GetId()) |
---|
153 | ix= 0 |
---|
154 | iy=1 |
---|
155 | self.sizer6.Add(wx.StaticText(self,-1,'Polydispersity: '),(iy,ix),(1,1)\ |
---|
156 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
157 | ix += 1 |
---|
158 | self.sizer6.Add(self.enable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
159 | ix += 1 |
---|
160 | self.sizer6.Add(self.disable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
161 | ix =0 |
---|
162 | iy+=1 |
---|
163 | self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
164 | |
---|
165 | |
---|
166 | #---------sizer 9 draw---------------------------------------- |
---|
167 | |
---|
168 | |
---|
169 | id = wx.NewId() |
---|
170 | self.btFit =wx.Button(self,id,'Fit') |
---|
171 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
172 | self.btFit.SetToolTipString("Perform fit.") |
---|
173 | ## Q range |
---|
174 | #print "self.data fitpage1D" , self.data,hasattr(self.data,"data") |
---|
175 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
176 | if not hasattr(self.data,"data"): |
---|
177 | self.qmin_x= numpy.min(self.data.x) |
---|
178 | self.qmax_x= numpy.max(self.data.x) |
---|
179 | self.num_points= len(self.data.x) |
---|
180 | else: |
---|
181 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
182 | radius1= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymin, 2)) |
---|
183 | radius2= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymin, 2)) |
---|
184 | radius3= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymax, 2)) |
---|
185 | radius4= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymax, 2)) |
---|
186 | #self.qmin_x = 0 |
---|
187 | #self.qmax_x = max(radius1, radius2, radius3, radius4) |
---|
188 | self.qmin_x= 0#self.data.xmin |
---|
189 | self.qmax_x= math.sqrt(math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
190 | +math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2))#self.data.xmax |
---|
191 | #print "data2D range",self.qmax_x |
---|
192 | |
---|
193 | self.num_points= 100 |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
198 | self.qmin.SetValue(str(format_number(self.qmin_x))) |
---|
199 | self.qmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
200 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
201 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
202 | self.qmin.Enable() |
---|
203 | |
---|
204 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
205 | self.qmax.SetValue(str(format_number(self.qmax_x))) |
---|
206 | self.qmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
207 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
208 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
209 | self.qmax.Enable() |
---|
210 | |
---|
211 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
212 | self.npts.SetValue(format_number(self.num_points)) |
---|
213 | self.npts.SetToolTipString("Number of point to plot.") |
---|
214 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
215 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
216 | self.npts.Disable() |
---|
217 | self.npts.Hide() |
---|
218 | ix = 0 |
---|
219 | iy = 1 |
---|
220 | self.sizer9.Add(wx.StaticText(self, -1, 'Fitting Range'),(iy, ix),(1,1),\ |
---|
221 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
222 | ix += 1 |
---|
223 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
224 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
225 | ix += 1 |
---|
226 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
227 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
228 | #ix += 1 |
---|
229 | #self.sizer9.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\ |
---|
230 | # wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
231 | ix = 0 |
---|
232 | iy += 1 |
---|
233 | self.sizer9.Add(wx.StaticText(self, -1, 'Q range'),(iy, ix),(1,1),\ |
---|
234 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
235 | ix += 1 |
---|
236 | self.sizer9.Add(self.qmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
237 | ix += 1 |
---|
238 | self.sizer9.Add(self.qmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
239 | ix += 1 |
---|
240 | self.sizer9.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
241 | |
---|
242 | ix += 1 |
---|
243 | self.sizer9.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
244 | ix =0 |
---|
245 | iy+=1 |
---|
246 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
247 | #----------sizer 10 draw------------------------------------------------------ |
---|
248 | id = wx.NewId() |
---|
249 | self.btClose =wx.Button(self,id,'Close') |
---|
250 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
251 | self.btClose.SetToolTipString("Close page.") |
---|
252 | |
---|
253 | ix= 3 |
---|
254 | iy= 1 |
---|
255 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
256 | ix +=1 |
---|
257 | self.sizer10.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
258 | ix =0 |
---|
259 | iy+=1 |
---|
260 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
261 | |
---|
262 | # contains link between model ,all its parameters, and panel organization |
---|
263 | self.parameters=[] |
---|
264 | self.fixed_param=[] |
---|
265 | self.fittable_param=[] |
---|
266 | #list of dispersion paramaters |
---|
267 | self.disp_list=[] |
---|
268 | #contains link between a model and selected parameters to fit |
---|
269 | self.param_toFit=[] |
---|
270 | # model on which the fit would be performed |
---|
271 | self.model=None |
---|
272 | #dictionary of model name and model class |
---|
273 | self.model_list_box={} |
---|
274 | |
---|
275 | if self.model == None: |
---|
276 | self.qmin.Disable() |
---|
277 | self.qmax.Disable() |
---|
278 | else: |
---|
279 | self.qmin.Enable() |
---|
280 | self.qmax.Enable() |
---|
281 | |
---|
282 | |
---|
283 | self.vbox.Layout() |
---|
284 | self.vbox.Fit(self) |
---|
285 | self.SetSizer(self.vbox) |
---|
286 | self.SetScrollbars(20,20,55,40) |
---|
287 | |
---|
288 | self.Centre() |
---|
289 | self.Layout() |
---|
290 | self.GrandParent.GetSizer().Layout() |
---|
291 | |
---|
292 | def compute_chisqr2D(self): |
---|
293 | """ @param fn: function that return model value |
---|
294 | @return residuals |
---|
295 | """ |
---|
296 | flag=self.checkFitRange() |
---|
297 | res=[] |
---|
298 | if flag== True: |
---|
299 | try: |
---|
300 | print "compute",self.data.err_data |
---|
301 | self.qmin_x = float(self.qmin.GetValue()) |
---|
302 | self.qmax_x = float(self.qmax.GetValue()) |
---|
303 | for i in range(len(self.data.x_bins)): |
---|
304 | #if self.data.x_bins[i]>= self.qmin_x and self.data.x_bins[i]<= self.qmax_x: |
---|
305 | for j in range(len(self.data.y_bins)): |
---|
306 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)>=math.pow(self.qmin_x,2): |
---|
307 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)<=math.pow(self.qmax_x,2): |
---|
308 | #if self.data.y_bins[j]>= self.qmin_x and self.data.y_bins[j]<= self.qmax_x: |
---|
309 | chisqrji=(self.data.data[j][i]- self.model.runXY(\ |
---|
310 | [self.data.y_bins[j],self.data.x_bins[i]]))\ |
---|
311 | /self.data.err_data[j][i] |
---|
312 | res.append( math.pow(chisqrji,2) ) |
---|
313 | sum=0 |
---|
314 | |
---|
315 | for item in res: |
---|
316 | if numpy.isfinite(item): |
---|
317 | sum +=item |
---|
318 | #print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum |
---|
319 | #print len(res) |
---|
320 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
321 | except: |
---|
322 | raise |
---|
323 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
324 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
325 | |
---|
326 | |
---|
327 | def compute_chisqr(self): |
---|
328 | """ @param fn: function that return model value |
---|
329 | @return residuals |
---|
330 | """ |
---|
331 | |
---|
332 | flag=self.checkFitRange() |
---|
333 | print "flag", flag |
---|
334 | if flag== True: |
---|
335 | try: |
---|
336 | if hasattr(self.data,"data"): |
---|
337 | self.compute_chisqr2D() |
---|
338 | return |
---|
339 | else: |
---|
340 | self.qmin_x = float(self.qmin.GetValue()) |
---|
341 | self.qmax_x = float(self.qmax.GetValue()) |
---|
342 | #print "self.qmin_x, self.qmax_x",self.qmin_x,self.qmax_x |
---|
343 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
344 | if self.qmin_x==None and self.qmax_x==None: |
---|
345 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
346 | res=(y - fx)/dy |
---|
347 | else: |
---|
348 | idx = (x>= self.qmin_x) & (x <=self.qmax_x) |
---|
349 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
350 | res= (y[idx] - fx)/dy[idx] |
---|
351 | |
---|
352 | sum=0 |
---|
353 | for item in res: |
---|
354 | if numpy.isfinite(item): |
---|
355 | sum +=item |
---|
356 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
357 | except: |
---|
358 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
359 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
360 | |
---|
361 | def _on_select_model(self,event): |
---|
362 | """ |
---|
363 | react when a model is selected from page's combo box |
---|
364 | post an event to its owner to draw an appropriate theory |
---|
365 | """ |
---|
366 | self.btFit.SetFocus() |
---|
367 | self.disable_disp.SetValue(True) |
---|
368 | self.sizer8.Clear(True) |
---|
369 | self.sizer7.Clear(True) |
---|
370 | self.vbox.Layout() |
---|
371 | self.SetScrollbars(20,20,55,40) |
---|
372 | self.Layout() |
---|
373 | self.parent.GetSizer().Layout() |
---|
374 | |
---|
375 | for item in self.model_list_box.itervalues(): |
---|
376 | name = item.__name__ |
---|
377 | if hasattr(item, "name"): |
---|
378 | name = item.name |
---|
379 | #print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
380 | if name == None: |
---|
381 | self.qmin.Disable() |
---|
382 | self.qmax.Disable() |
---|
383 | else: |
---|
384 | self.qmin.Enable() |
---|
385 | self.qmax.Enable() |
---|
386 | |
---|
387 | if name ==event.GetString(): |
---|
388 | try: |
---|
389 | self.model=item() |
---|
390 | evt = ModelEventbox(model=self.model,name=name) |
---|
391 | wx.PostEvent(self.event_owner, evt) |
---|
392 | self.text1_1.Show() |
---|
393 | self.compute_chisqr() |
---|
394 | self.tcChi.Show() |
---|
395 | except: |
---|
396 | raise #ValueError,"model.name is not equal to model class name" |
---|
397 | break |
---|
398 | def onFit(self,event): |
---|
399 | """ signal for fitting""" |
---|
400 | |
---|
401 | flag=self.checkFitRange() |
---|
402 | self.set_manager(self.manager) |
---|
403 | |
---|
404 | self.qmin_x=float(self.qmin.GetValue()) |
---|
405 | self.qmax_x =float( self.qmax.GetValue()) |
---|
406 | if len(self.param_toFit) >0 and flag==True: |
---|
407 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
408 | if hasattr(self.data, "data"): |
---|
409 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, |
---|
410 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
411 | xmin=self.data.xmin,xmax=self.data.xmax) |
---|
412 | #self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, |
---|
413 | #ymin=self.data.ymin, ymax=self.data.ymax) |
---|
414 | else: |
---|
415 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) |
---|
416 | else: |
---|
417 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
418 | "Select at least on parameter to fit ")) |
---|
419 | |
---|
420 | |
---|
421 | def _onTextEnter(self,event): |
---|
422 | """ |
---|
423 | set a flag to determine if the fitting range entered by the user is valid |
---|
424 | """ |
---|
425 | |
---|
426 | try: |
---|
427 | flag=self.checkFitRange() |
---|
428 | if flag==True and self.model!=None: |
---|
429 | #print"fit page",self.xmin.GetValue(),self.xmax.GetValue() |
---|
430 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
431 | ,float(self.xmax.GetValue())) |
---|
432 | except: |
---|
433 | |
---|
434 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
435 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
436 | |
---|
437 | |
---|
438 | |
---|
439 | def get_param_list(self): |
---|
440 | """ |
---|
441 | @return self.param_toFit: list containing references to TextCtrl |
---|
442 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
443 | @raise: if return an empty list of parameter fit will nnote work |
---|
444 | properly so raise ValueError,"missing parameter to fit" |
---|
445 | """ |
---|
446 | if self.param_toFit !=[]: |
---|
447 | return self.param_toFit |
---|
448 | else: |
---|
449 | raise ValueError,"missing parameter to fit" |
---|
450 | |
---|
451 | |
---|
452 | def _onparamEnter(self,event): |
---|
453 | """ |
---|
454 | when enter value on panel redraw model according to changed |
---|
455 | """ |
---|
456 | self.set_model() |
---|
457 | self.compute_chisqr() |
---|
458 | |
---|
459 | def set_model(self): |
---|
460 | if len(self.parameters) !=0 and self.model !=None: |
---|
461 | # Flag to register when a parameter has changed. |
---|
462 | for item in self.parameters: |
---|
463 | try: |
---|
464 | self.text2_3.Hide() |
---|
465 | item[2].Hide() |
---|
466 | item[3].Clear() |
---|
467 | item[3].Hide() |
---|
468 | except: |
---|
469 | #enter dispersion value |
---|
470 | pass |
---|
471 | self.set_model_parameter() |
---|
472 | |
---|
473 | |
---|
474 | def select_all_param(self,event): |
---|
475 | """ |
---|
476 | set to true or false all checkBox given the main checkbox value cb1 |
---|
477 | """ |
---|
478 | self.select_all_param_helper() |
---|
479 | |
---|
480 | |
---|
481 | def select_param(self,event): |
---|
482 | """ |
---|
483 | Select TextCtrl checked for fitting purpose and stores them |
---|
484 | in self.param_toFit=[] list |
---|
485 | """ |
---|
486 | self.param_toFit=[] |
---|
487 | for item in self.parameters: |
---|
488 | if item[0].GetValue()==True: |
---|
489 | list= [item[0],item[1],item[2],item[3]] |
---|
490 | if not (list in self.param_toFit): |
---|
491 | self.param_toFit.append(list ) |
---|
492 | else: |
---|
493 | if item in self.param_toFit: |
---|
494 | self.param_toFit.remove(item) |
---|
495 | |
---|
496 | for item in self.fittable_param: |
---|
497 | if item[0].GetValue()==True: |
---|
498 | list= [item[0],item[1],item[2],item[3]] |
---|
499 | if not (list in self.param_toFit): |
---|
500 | self.param_toFit.append(list ) |
---|
501 | else: |
---|
502 | if item in self.param_toFit: |
---|
503 | self.param_toFit.remove(item) |
---|
504 | |
---|
505 | |
---|
506 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
507 | self.cb1.SetValue(True) |
---|
508 | else: |
---|
509 | self.cb1.SetValue(False) |
---|
510 | """ |
---|
511 | #qmax, qmin Input enabled all the time |
---|
512 | self.qmin.Enable() |
---|
513 | self.qmax.Enable() |
---|
514 | """ |
---|
515 | |
---|
516 | def onsetValues(self,chisqr, out,cov): |
---|
517 | """ |
---|
518 | Build the panel from the fit result |
---|
519 | @param chisqr:Value of the goodness of fit metric |
---|
520 | @param out:list of parameter with the best value found during fitting |
---|
521 | @param cov:Covariance matrix |
---|
522 | |
---|
523 | """ |
---|
524 | #print "fitting : onsetvalues out",out |
---|
525 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
526 | params = {} |
---|
527 | is_modified = False |
---|
528 | has_error = False |
---|
529 | if out.__class__==numpy.float64: |
---|
530 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
531 | self.param_toFit[0][1].Refresh() |
---|
532 | if cov !=None : |
---|
533 | self.text2_3.Show() |
---|
534 | self.param_toFit[0][2].Show() |
---|
535 | self.param_toFit[0][3].Clear() |
---|
536 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
537 | self.param_toFit[0][3].Show() |
---|
538 | #out is a list : set parameters and errors in TextCtrl |
---|
539 | else: |
---|
540 | i=0 |
---|
541 | #print "fitpage: list param model",list |
---|
542 | #for item in self.param_toFit: |
---|
543 | # print "fitpage: list display",item[0].GetLabelText() |
---|
544 | for item in self.param_toFit: |
---|
545 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
546 | #item[1].SetValue(format_number(out[i])) |
---|
547 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
548 | item[1].Refresh() |
---|
549 | if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
550 | self.text2_3.Show() |
---|
551 | item[2].Show() |
---|
552 | item[3].Clear() |
---|
553 | item[3].SetValue(format_number(cov[i])) |
---|
554 | item[3].Show() |
---|
555 | i+=1 |
---|
556 | |
---|
557 | self.vbox.Layout() |
---|
558 | self.SetScrollbars(20,20,55,40) |
---|
559 | self.Layout() |
---|
560 | self.GrandParent.GetSizer().Layout() |
---|
561 | |
---|
562 | |
---|
563 | def onSmear(self, event): |
---|
564 | #print "in smearer",self.enable_smearer.GetValue() |
---|
565 | smear =None |
---|
566 | if self.enable_smearer.GetValue(): |
---|
567 | from DataLoader.qsmearing import smear_selection |
---|
568 | smear =smear_selection( self.data ) |
---|
569 | if smear ==None: |
---|
570 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
571 | "Data contains no smearing information")) |
---|
572 | else: |
---|
573 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
574 | "Data contains smearing information")) |
---|
575 | self.manager.set_smearer(smear) |
---|
576 | |
---|
577 | |
---|
578 | |
---|
579 | |
---|