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 | self.tcChi.Disable() |
---|
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 | ## Q range |
---|
184 | self.qmin= 0.001 |
---|
185 | self.qmax= 0.1 |
---|
186 | """ |
---|
187 | bs = wx.BoxSizer(wx.VERTICAL) |
---|
188 | bs.Add(self.scroll, 1, wx.EXPAND) |
---|
189 | self.SetSizer(bs) |
---|
190 | self.scroll.SetSizer(self.vbox) |
---|
191 | self.scroll.SetScrollbars(20,20,55,40) |
---|
192 | #width,height = self.GetSize() |
---|
193 | """ |
---|
194 | self.vbox.Layout() |
---|
195 | self.vbox.Fit(self) |
---|
196 | self.SetSizer(self.vbox) |
---|
197 | self.SetScrollbars(20,20,55,40) |
---|
198 | |
---|
199 | self.Centre() |
---|
200 | self.Layout() |
---|
201 | self.GrandParent.GetSizer().Layout() |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | def set_owner(self,owner): |
---|
211 | """ |
---|
212 | set owner of fitpage |
---|
213 | @param owner: the class responsible of plotting |
---|
214 | """ |
---|
215 | self.event_owner = owner |
---|
216 | |
---|
217 | |
---|
218 | def set_manager(self, manager): |
---|
219 | """ |
---|
220 | set panel manager |
---|
221 | @param manager: instance of plugin fitting |
---|
222 | """ |
---|
223 | self.manager = manager |
---|
224 | |
---|
225 | |
---|
226 | def onClose(self,event): |
---|
227 | """ close the page associated with this panel""" |
---|
228 | self.GrandParent.onClose() |
---|
229 | |
---|
230 | |
---|
231 | def compute_chisqr(self): |
---|
232 | """ @param fn: function that return model value |
---|
233 | @return residuals |
---|
234 | """ |
---|
235 | |
---|
236 | flag=self.checkFitRange() |
---|
237 | if flag== True: |
---|
238 | try: |
---|
239 | qmin = float(self.xmin.GetValue()) |
---|
240 | qmax = float(self.xmax.GetValue()) |
---|
241 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
242 | if qmin==None and qmax==None: |
---|
243 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
244 | res=(y - fx)/dy |
---|
245 | else: |
---|
246 | idx = (x>= qmin) & (x <=qmax) |
---|
247 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
248 | res= (y[idx] - fx)/dy[idx] |
---|
249 | |
---|
250 | |
---|
251 | sum=0 |
---|
252 | for item in res: |
---|
253 | if numpy.isfinite(item): |
---|
254 | sum +=item |
---|
255 | self.tcChi.SetValue(format_number(math.fabs(sum))) |
---|
256 | except: |
---|
257 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
258 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
259 | |
---|
260 | |
---|
261 | def onFit(self,event): |
---|
262 | """ signal for fitting""" |
---|
263 | |
---|
264 | flag=self.checkFitRange() |
---|
265 | self.set_manager(self.manager) |
---|
266 | |
---|
267 | qmin=float(self.xmin.GetValue()) |
---|
268 | qmax =float( self.xmax.GetValue()) |
---|
269 | if len(self.param_toFit) >0 and flag==True: |
---|
270 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
271 | self.manager._on_single_fit(qmin=qmin,qmax=qmax) |
---|
272 | else: |
---|
273 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
274 | "Select at least on parameter to fit ")) |
---|
275 | def populate_box(self, dict): |
---|
276 | """ |
---|
277 | Populate each combox box of each page |
---|
278 | @param page: the page to populate |
---|
279 | """ |
---|
280 | id=0 |
---|
281 | self.model_list_box=dict |
---|
282 | list_name=[] |
---|
283 | for item in self.model_list_box.itervalues(): |
---|
284 | name = item.__name__ |
---|
285 | if hasattr(item, "name"): |
---|
286 | name = item.name |
---|
287 | list_name.append(name) |
---|
288 | list_name.sort() |
---|
289 | for name in list_name: |
---|
290 | self.modelbox.Insert(name,int(id)) |
---|
291 | id+=1 |
---|
292 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
293 | return 0 |
---|
294 | |
---|
295 | |
---|
296 | def _on_select_model(self,event): |
---|
297 | """ |
---|
298 | react when a model is selected from page's combo box |
---|
299 | post an event to its owner to draw an appropriate theory |
---|
300 | """ |
---|
301 | self.btFit.SetFocus() |
---|
302 | for item in self.model_list_box.itervalues(): |
---|
303 | name = item.__name__ |
---|
304 | if hasattr(item, "name"): |
---|
305 | name = item.name |
---|
306 | #print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
307 | if name ==event.GetString(): |
---|
308 | try: |
---|
309 | self.model=item() |
---|
310 | evt = ModelEventbox(model=self.model,name=name) |
---|
311 | wx.PostEvent(self.event_owner, evt) |
---|
312 | #self.model= item() |
---|
313 | #self.set_panel(self.model) |
---|
314 | except: |
---|
315 | raise #ValueError,"model.name is not equal to model class name" |
---|
316 | break |
---|
317 | |
---|
318 | def _onTextEnter(self,event): |
---|
319 | """ |
---|
320 | set a flag to determine if the fitting range entered by the user is valid |
---|
321 | """ |
---|
322 | |
---|
323 | try: |
---|
324 | flag=self.checkFitRange() |
---|
325 | if flag==True and self.model!=None: |
---|
326 | #print"fit page",self.xmin.GetValue(),self.xmax.GetValue() |
---|
327 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
328 | ,float(self.xmax.GetValue())) |
---|
329 | except: |
---|
330 | |
---|
331 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
332 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
333 | |
---|
334 | def checkFitRange(self): |
---|
335 | """ |
---|
336 | Check the validity of fitting range |
---|
337 | @note: xmin should always be less than xmax or else each control box |
---|
338 | background is colored in pink. |
---|
339 | """ |
---|
340 | |
---|
341 | flag = True |
---|
342 | valueMin = self.xmin.GetValue() |
---|
343 | valueMax = self.xmax.GetValue() |
---|
344 | # Check for possible values entered |
---|
345 | #print "fitpage: checkfitrange:",valueMin,valueMax |
---|
346 | try: |
---|
347 | if (float(valueMax)> float(valueMin)): |
---|
348 | self.xmax.SetBackgroundColour(wx.WHITE) |
---|
349 | self.xmin.SetBackgroundColour(wx.WHITE) |
---|
350 | else: |
---|
351 | flag = False |
---|
352 | self.xmin.SetBackgroundColour("pink") |
---|
353 | self.xmax.SetBackgroundColour("pink") |
---|
354 | except: |
---|
355 | flag = False |
---|
356 | self.xmin.SetBackgroundColour("pink") |
---|
357 | self.xmax.SetBackgroundColour("pink") |
---|
358 | |
---|
359 | self.xmin.Refresh() |
---|
360 | self.xmax.Refresh() |
---|
361 | return flag |
---|
362 | |
---|
363 | |
---|
364 | def get_model_box(self): |
---|
365 | """ return reference to combox box self.model""" |
---|
366 | return self.modelbox |
---|
367 | |
---|
368 | |
---|
369 | def get_param_list(self): |
---|
370 | """ |
---|
371 | @return self.param_toFit: list containing references to TextCtrl |
---|
372 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
373 | @raise: if return an empty list of parameter fit will nnote work |
---|
374 | properly so raise ValueError,"missing parameter to fit" |
---|
375 | """ |
---|
376 | if self.param_toFit !=[]: |
---|
377 | return self.param_toFit |
---|
378 | else: |
---|
379 | raise ValueError,"missing parameter to fit" |
---|
380 | |
---|
381 | |
---|
382 | def set_panel(self,model): |
---|
383 | """ |
---|
384 | Build the panel from the model content |
---|
385 | @param model: the model selected in combo box for fitting purpose |
---|
386 | """ |
---|
387 | self.sizer2.Clear(True) |
---|
388 | self.sizer5.Clear(True) |
---|
389 | self.sizer6.Clear(True) |
---|
390 | self.parameters = [] |
---|
391 | self.param_toFit=[] |
---|
392 | self.model = model |
---|
393 | keys = self.model.getParamList() |
---|
394 | #print "fitpage1D : dispersion list",self.model.getDispParamList() |
---|
395 | keys.sort() |
---|
396 | disp_list=self.model.getDispParamList() |
---|
397 | fixed=self.model.fixed |
---|
398 | ip=0 |
---|
399 | iq=1 |
---|
400 | |
---|
401 | ik=0 |
---|
402 | im=1 |
---|
403 | if len(disp_list)>0: |
---|
404 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
405 | self.sizer5.Add(disp,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
406 | ip += 1 |
---|
407 | values = wx.StaticText(self, -1, 'Values') |
---|
408 | self.sizer5.Add(values,( iq, ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
409 | |
---|
410 | disp_list.sort() |
---|
411 | iy = 1 |
---|
412 | ix = 0 |
---|
413 | self.cb1 = wx.CheckBox(self, -1,'Parameters', (10, 10)) |
---|
414 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
415 | self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ |
---|
416 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
417 | ix +=1 |
---|
418 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
419 | self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
420 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
421 | ix +=2 |
---|
422 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
423 | self.sizer2.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
424 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
425 | self.text2_3.Hide() |
---|
426 | ix +=1 |
---|
427 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
428 | self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
429 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
430 | self.text2_4.Hide() |
---|
431 | |
---|
432 | for item in keys: |
---|
433 | if not item in disp_list: |
---|
434 | iy += 1 |
---|
435 | ix = 0 |
---|
436 | |
---|
437 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
438 | cb.SetValue(False) |
---|
439 | self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
440 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
441 | |
---|
442 | ix += 1 |
---|
443 | value= self.model.getParam(item) |
---|
444 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
445 | ctl1.SetValue(str (format_number(value))) |
---|
446 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
447 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
448 | self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
449 | |
---|
450 | ix += 1 |
---|
451 | text2=wx.StaticText(self, -1, '+/-') |
---|
452 | self.sizer2.Add(text2,(iy, ix),(1,1),\ |
---|
453 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
454 | text2.Hide() |
---|
455 | ix += 1 |
---|
456 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
457 | self.sizer2.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
458 | ctl2.Hide() |
---|
459 | ix +=1 |
---|
460 | # Units |
---|
461 | try: |
---|
462 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
463 | except: |
---|
464 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
465 | self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
466 | else: |
---|
467 | if not item in fixed: |
---|
468 | ip = 0 |
---|
469 | iq += 1 |
---|
470 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
471 | cb.SetValue(False) |
---|
472 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
473 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
474 | |
---|
475 | ip += 1 |
---|
476 | value= self.model.getParam(item) |
---|
477 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
478 | ctl1.SetValue(str (format_number(value))) |
---|
479 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
480 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
481 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
482 | else: |
---|
483 | ik = 0 |
---|
484 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
485 | self.sizer6.Add(text,( im, ik),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
486 | |
---|
487 | ik += 1 |
---|
488 | value= self.model.getParam(item) |
---|
489 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
490 | Tctl.SetValue(str (format_number(value))) |
---|
491 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
492 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
493 | self.sizer6.Add(Tctl, (im,ik),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
494 | self.fixed_param.append([item, Tctl]) |
---|
495 | im += 1 |
---|
496 | #save data |
---|
497 | self.parameters.append([cb,ctl1,text2,ctl2]) |
---|
498 | |
---|
499 | iy+=1 |
---|
500 | self.sizer2.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
501 | |
---|
502 | #Display units text on panel |
---|
503 | for item in keys: |
---|
504 | if self.model.details[item][0]!='': |
---|
505 | self.text2_4.Show() |
---|
506 | break |
---|
507 | else: |
---|
508 | self.text2_4.Hide() |
---|
509 | #Disable or enable fit button |
---|
510 | |
---|
511 | if not (len(self.param_toFit ) >0): |
---|
512 | self.xmin.Disable() |
---|
513 | self.xmax.Disable() |
---|
514 | else: |
---|
515 | self.xmin.Enable() |
---|
516 | self.xmax.Enable() |
---|
517 | |
---|
518 | self.compute_chisqr() |
---|
519 | self.vbox.Layout() |
---|
520 | self.Layout() |
---|
521 | self.GrandParent.GetSizer().Layout() |
---|
522 | |
---|
523 | |
---|
524 | |
---|
525 | def _onparamEnter(self,event): |
---|
526 | """ |
---|
527 | when enter value on panel redraw model according to changed |
---|
528 | """ |
---|
529 | self.set_model_parameter() |
---|
530 | self.compute_chisqr() |
---|
531 | |
---|
532 | |
---|
533 | def set_model_parameter(self): |
---|
534 | """ |
---|
535 | this method redraws the model according to parameters values changes |
---|
536 | and the reset model according to paramaters changes |
---|
537 | """ |
---|
538 | print "went here",len(self.parameters) ,self.model |
---|
539 | if len(self.parameters) !=0 and self.model !=None: |
---|
540 | # Flag to register when a parameter has changed. |
---|
541 | is_modified = False |
---|
542 | for item in self.parameters: |
---|
543 | try: |
---|
544 | item[2].Hide() |
---|
545 | item[3].Clear() |
---|
546 | item[3].Hide() |
---|
547 | name=str(item[0].GetLabelText()) |
---|
548 | value= float(item[1].GetValue()) |
---|
549 | # If the value of the parameter has changed, |
---|
550 | # update the model and set the is_modified flag |
---|
551 | if value != self.model.getParam(name): |
---|
552 | self.model.setParam(name,value) |
---|
553 | is_modified = True |
---|
554 | except: |
---|
555 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
556 | "Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
557 | # Here we should check whether the boundaries have been modified. |
---|
558 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
559 | # set the is_modified flag to True |
---|
560 | if float(self.xmin.GetValue()) != self.qmin: |
---|
561 | self.qmin = float(self.xmin.GetValue()) |
---|
562 | is_modified = True |
---|
563 | if float(self.xmax.GetValue()) != self.qmax: |
---|
564 | self.qmax = float(self.xmax.GetValue()) |
---|
565 | is_modified = True |
---|
566 | |
---|
567 | if is_modified: |
---|
568 | self.manager.redraw_model( |
---|
569 | qmin=self.qmin, qmax=self.qmax, |
---|
570 | ) |
---|
571 | #self.manager.draw_model(self,model,description=None, |
---|
572 | # enable1D=True,qmin=None,qmax=None, qstep=None) |
---|
573 | |
---|
574 | def select_all_param(self,event): |
---|
575 | """ |
---|
576 | set to true or false all checkBox given the main checkbox value cb1 |
---|
577 | """ |
---|
578 | self.param_toFit=[] |
---|
579 | if self.parameters !=[]: |
---|
580 | if self.cb1.GetValue()==True: |
---|
581 | for item in self.parameters: |
---|
582 | item[0].SetValue(True) |
---|
583 | list= [item[0],item[1],item[2],item[3]] |
---|
584 | self.param_toFit.append(list ) |
---|
585 | |
---|
586 | if not (len(self.param_toFit ) >0): |
---|
587 | self.xmin.Disable() |
---|
588 | self.xmax.Disable() |
---|
589 | else: |
---|
590 | self.xmin.Enable() |
---|
591 | self.xmax.Enable() |
---|
592 | else: |
---|
593 | for item in self.parameters: |
---|
594 | item[0].SetValue(False) |
---|
595 | self.param_toFit=[] |
---|
596 | |
---|
597 | self.xmin.Disable() |
---|
598 | self.xmax.Disable() |
---|
599 | |
---|
600 | |
---|
601 | def select_param(self,event): |
---|
602 | """ |
---|
603 | Select TextCtrl checked for fitting purpose and stores them |
---|
604 | in self.param_toFit=[] list |
---|
605 | """ |
---|
606 | self.param_toFit=[] |
---|
607 | for item in self.parameters: |
---|
608 | if item[0].GetValue()==True: |
---|
609 | list= [item[0],item[1],item[2],item[3]] |
---|
610 | self.param_toFit.append(list ) |
---|
611 | else: |
---|
612 | if item in self.param_toFit: |
---|
613 | self.param_toFit.remove(item) |
---|
614 | if len(self.parameters)==len(self.param_toFit): |
---|
615 | self.cb1.SetValue(True) |
---|
616 | else: |
---|
617 | self.cb1.SetValue(False) |
---|
618 | |
---|
619 | if not (len(self.param_toFit ) >0): |
---|
620 | self.xmin.Disable() |
---|
621 | self.xmax.Disable() |
---|
622 | else: |
---|
623 | self.xmin.Enable() |
---|
624 | self.xmax.Enable() |
---|
625 | |
---|
626 | |
---|
627 | |
---|
628 | |
---|
629 | def onsetValues(self,chisqr, out,cov): |
---|
630 | """ |
---|
631 | Build the panel from the fit result |
---|
632 | @param chisqr:Value of the goodness of fit metric |
---|
633 | @param out:list of parameter with the best value found during fitting |
---|
634 | @param cov:Covariance matrix |
---|
635 | |
---|
636 | """ |
---|
637 | #print "fitting : onsetvalues out",out |
---|
638 | self.tcChi.Clear() |
---|
639 | self.tcChi.SetValue(format_number(chisqr)) |
---|
640 | params = {} |
---|
641 | is_modified = False |
---|
642 | has_error = False |
---|
643 | if out.__class__==numpy.float64: |
---|
644 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
645 | self.param_toFit[0][1].Refresh() |
---|
646 | if cov !=None : |
---|
647 | self.text2_3.Show() |
---|
648 | self.param_toFit[0][2].Show() |
---|
649 | self.param_toFit[0][3].Clear() |
---|
650 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
651 | self.param_toFit[0][3].Show() |
---|
652 | #out is a list : set parameters and errors in TextCtrl |
---|
653 | else: |
---|
654 | i=0 |
---|
655 | #print "fitpage: list param model",list |
---|
656 | #for item in self.param_toFit: |
---|
657 | # print "fitpage: list display",item[0].GetLabelText() |
---|
658 | for item in self.param_toFit: |
---|
659 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
660 | #item[1].SetValue(format_number(out[i])) |
---|
661 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
662 | item[1].Refresh() |
---|
663 | if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
664 | self.text2_3.Show() |
---|
665 | item[2].Show() |
---|
666 | item[3].Clear() |
---|
667 | item[3].SetValue(format_number(cov[i])) |
---|
668 | item[3].Show() |
---|
669 | i+=1 |
---|
670 | |
---|
671 | self.vbox.Layout() |
---|
672 | self.GrandParent.GetSizer().Layout() |
---|
673 | |
---|
674 | |
---|
675 | def onSmear(self, event): |
---|
676 | if event.GetString()=="None": |
---|
677 | self.manager.set_smearer(None) |
---|
678 | |
---|
679 | |
---|
680 | if event.GetString()=="GaussianModel": |
---|
681 | from DataLoader.qsmearing import smear_selection |
---|
682 | smear =smear_selection( self.data ) |
---|
683 | self.manager.set_smearer(smear) |
---|
684 | print "on smearing" |
---|
685 | |
---|