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