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