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