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