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