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 FitPage2D(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.manager = None |
---|
47 | self.parent = parent |
---|
48 | self.event_owner=None |
---|
49 | #panel interface |
---|
50 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
51 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
52 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
53 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
54 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
55 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
56 | #self.DataSource = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
57 | #self.DataSource.SetToolTipString("name of data to fit") |
---|
58 | #self.DataSource.SetValue(str(data.name)) |
---|
59 | #self.DataSource.Disable() |
---|
60 | self.DataSource =wx.StaticText(self, -1,str(data.name)) |
---|
61 | self.modelbox = wx.ComboBox(self, -1) |
---|
62 | id = wx.NewId() |
---|
63 | self.btFit =wx.Button(self,id,'Fit') |
---|
64 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
65 | self.btFit.SetToolTipString("Perform fit.") |
---|
66 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
67 | |
---|
68 | self.vbox.Add(self.sizer3) |
---|
69 | self.vbox.Add(self.sizer2) |
---|
70 | self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
71 | self.vbox.Add(self.sizer5) |
---|
72 | self.vbox.Add(self.sizer4) |
---|
73 | self.vbox.Add(self.sizer1) |
---|
74 | ## Q range |
---|
75 | self.qmin= 0.001 |
---|
76 | self.qmax= 0.1 |
---|
77 | |
---|
78 | id = wx.NewId() |
---|
79 | self.btClose =wx.Button(self,id,'Close') |
---|
80 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
81 | self.btClose.SetToolTipString("Close page.") |
---|
82 | ix = 0 |
---|
83 | iy = 1 |
---|
84 | self.sizer3.Add(wx.StaticText(self, -1, 'Data Source'),(iy,ix),\ |
---|
85 | (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
86 | ix += 1 |
---|
87 | self.sizer3.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
88 | ix += 1 |
---|
89 | self.sizer3.Add((20,20),(iy,ix),(1,1),wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
90 | ix = 0 |
---|
91 | iy += 1 |
---|
92 | self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
93 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
94 | ix += 1 |
---|
95 | self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
96 | |
---|
97 | ix = 0 |
---|
98 | iy = 1 |
---|
99 | #set maximum range for x in linear scale |
---|
100 | self.text4_3 = wx.StaticText(self, -1, 'Maximum Data\n Range (Linear)', style=wx.ALIGN_LEFT) |
---|
101 | self.sizer4.Add(self.text4_3,(iy,ix),(1,1),\ |
---|
102 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
103 | ix += 1 |
---|
104 | self.text4_1 = wx.StaticText(self, -1, 'Min') |
---|
105 | self.sizer4.Add(self.text4_1,(iy, ix),(1,1),\ |
---|
106 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
107 | #self.text4_1.Hide() |
---|
108 | ix += 2 |
---|
109 | self.text4_2 = wx.StaticText(self, -1, 'Max') |
---|
110 | self.sizer4.Add(self.text4_2,(iy, ix),(1,1),\ |
---|
111 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
112 | #self.text4_2.Hide() |
---|
113 | |
---|
114 | #self.text4_3.Hide() |
---|
115 | ix = 0 |
---|
116 | iy += 1 |
---|
117 | self.text4_4 = wx.StaticText(self, -1, 'x range') |
---|
118 | self.sizer4.Add(self.text4_4,(iy, ix),(1,1),\ |
---|
119 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
120 | ix += 1 |
---|
121 | self.xmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
122 | self.xmin.SetValue(format_number(data.xmin)) |
---|
123 | self.xmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
124 | self.sizer4.Add(self.xmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
125 | self.xmin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
126 | self.xmin.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
127 | |
---|
128 | ix += 2 |
---|
129 | self.xmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
130 | self.xmax.SetValue(format_number(data.xmax)) |
---|
131 | self.xmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
132 | self.sizer4.Add(self.xmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
133 | self.xmax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
134 | self.xmax.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
135 | |
---|
136 | iy +=1 |
---|
137 | ix = 0 |
---|
138 | self.text4_5 = wx.StaticText(self, -1, 'y range') |
---|
139 | self.sizer4.Add(self.text4_5,(iy, ix),(1,1),\ |
---|
140 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
141 | ix += 1 |
---|
142 | self.ymin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
143 | self.ymin.SetValue(format_number(data.ymin)) |
---|
144 | self.ymin.SetToolTipString("Minimun value of y in linear scale.") |
---|
145 | self.sizer4.Add(self.ymin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
146 | self.ymin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
147 | self.ymin.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
148 | |
---|
149 | ix += 2 |
---|
150 | self.ymax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
151 | self.ymax.SetValue(format_number(data.ymax)) |
---|
152 | self.ymax.SetToolTipString("Maximum value of y in linear scale.") |
---|
153 | self.sizer4.Add(self.ymax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
154 | self.ymax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
155 | self.ymax.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
156 | |
---|
157 | #Set chisqr result into TextCtrl |
---|
158 | ix = 0 |
---|
159 | iy = 1 |
---|
160 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
161 | self.sizer1.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
162 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
163 | ix += 1 |
---|
164 | self.tcChi = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
165 | self.tcChi.SetToolTipString("Chi^2 over degrees of freedom.") |
---|
166 | self.sizer1.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
167 | ix +=2 |
---|
168 | self.sizer1.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
169 | ix+= 1 |
---|
170 | self.sizer1.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
171 | ix= 1 |
---|
172 | iy+=1 |
---|
173 | self.sizer1.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
174 | # contains link between model ,all its parameters, and panel organization |
---|
175 | self.parameters=[] |
---|
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 | # preview selected model name |
---|
181 | |
---|
182 | |
---|
183 | #dictionary of model name and model class |
---|
184 | self.model_list_box={} |
---|
185 | |
---|
186 | self.data = data |
---|
187 | self.vbox.Layout() |
---|
188 | self.vbox.Fit(self) |
---|
189 | self.SetSizer(self.vbox) |
---|
190 | self.SetScrollbars(20,20,55,40) |
---|
191 | self.Centre() |
---|
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 | flag=self.checkFitRange() |
---|
220 | res=[] |
---|
221 | if flag== True: |
---|
222 | try: |
---|
223 | xmin = float(self.xmin.GetValue()) |
---|
224 | xmax = float(self.xmax.GetValue()) |
---|
225 | ymin = float(self.ymin.GetValue()) |
---|
226 | ymax = float(self.ymax.GetValue()) |
---|
227 | |
---|
228 | for i in range(len(self.data.y_bins)): |
---|
229 | if self.data.y_bins[i]>= ymin and self.data.y_bins[i]<= ymax: |
---|
230 | for j in range(len(self.data.x_bins)): |
---|
231 | if self.data.x_bins[j]>= xmin and self.data.x_bins[j]<= xmax: |
---|
232 | res.append( (self.data.data[j][i]- self.model.runXY(\ |
---|
233 | [self.data.x_bins[j],self.data.y_bins[i]]))\ |
---|
234 | /self.data.err_data[j][i] ) |
---|
235 | sum=0 |
---|
236 | |
---|
237 | for item in res: |
---|
238 | if numpy.isfinite(item): |
---|
239 | sum +=item |
---|
240 | self.tcChi.SetValue(format_number(math.fabs(sum))) |
---|
241 | except: |
---|
242 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
243 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
244 | |
---|
245 | |
---|
246 | def onFit(self,event): |
---|
247 | """ signal for fitting""" |
---|
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 | evt = ModelEventbox(model=item(),name=name) |
---|
294 | wx.PostEvent(self.event_owner, evt) |
---|
295 | except: |
---|
296 | raise #ValueError,"model.name is not equal to model class name" |
---|
297 | break |
---|
298 | |
---|
299 | def _onTextEnter(self,event): |
---|
300 | """ |
---|
301 | set a flag to determine if the fitting range entered by the user is valid |
---|
302 | """ |
---|
303 | |
---|
304 | try: |
---|
305 | flag=self.checkFitRange() |
---|
306 | if flag==True and self.model!=None: |
---|
307 | print"fit page",self.xmin.GetValue(),self.xmax.GetValue() |
---|
308 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
309 | ,float(self.xmax.GetValue())) |
---|
310 | except: |
---|
311 | |
---|
312 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
313 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
314 | |
---|
315 | def checkFitRange(self): |
---|
316 | """ |
---|
317 | Check the validity of fitting range |
---|
318 | @note: xmin should always be less than xmax or else each control box |
---|
319 | background is colored in pink. |
---|
320 | """ |
---|
321 | |
---|
322 | flag = True |
---|
323 | valueMin = self.xmin.GetValue() |
---|
324 | valueMax = self.xmax.GetValue() |
---|
325 | # Check for possible values entered |
---|
326 | print "fitpage: checkfitrange:",valueMin,valueMax |
---|
327 | try: |
---|
328 | if (float(valueMax)> float(valueMin)): |
---|
329 | self.xmax.SetBackgroundColour(wx.WHITE) |
---|
330 | self.xmin.SetBackgroundColour(wx.WHITE) |
---|
331 | else: |
---|
332 | flag = False |
---|
333 | self.xmin.SetBackgroundColour("pink") |
---|
334 | self.xmax.SetBackgroundColour("pink") |
---|
335 | except: |
---|
336 | flag = False |
---|
337 | self.xmin.SetBackgroundColour("pink") |
---|
338 | self.xmax.SetBackgroundColour("pink") |
---|
339 | |
---|
340 | self.xmin.Refresh() |
---|
341 | self.xmax.Refresh() |
---|
342 | return flag |
---|
343 | |
---|
344 | |
---|
345 | def get_model_box(self): |
---|
346 | """ return reference to combox box self.model""" |
---|
347 | return self.modelbox |
---|
348 | |
---|
349 | |
---|
350 | def get_param_list(self): |
---|
351 | """ |
---|
352 | @return self.param_toFit: list containing references to TextCtrl |
---|
353 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
354 | @raise: if return an empty list of parameter fit will nnote work |
---|
355 | properly so raise ValueError,"missing parameter to fit" |
---|
356 | """ |
---|
357 | if self.param_toFit !=[]: |
---|
358 | return self.param_toFit |
---|
359 | else: |
---|
360 | raise ValueError,"missing parameter to fit" |
---|
361 | |
---|
362 | |
---|
363 | def set_panel(self,model): |
---|
364 | """ |
---|
365 | Build the panel from the model content |
---|
366 | @param model: the model selected in combo box for fitting purpose |
---|
367 | """ |
---|
368 | self.sizer2.Clear(True) |
---|
369 | self.sizer5.Clear(True) |
---|
370 | self.parameters = [] |
---|
371 | self.param_toFit=[] |
---|
372 | self.model = model |
---|
373 | keys = self.model.getParamList() |
---|
374 | #print "fitpage1D : dispersion list",self.model.getDispParamList() |
---|
375 | keys.sort() |
---|
376 | disp_list=self.model.getDispParamList() |
---|
377 | ip=0 |
---|
378 | iq=1 |
---|
379 | if len(disp_list)>0: |
---|
380 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
381 | self.sizer5.Add(disp,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
382 | ip += 1 |
---|
383 | values = wx.StaticText(self, -1, 'Values') |
---|
384 | self.sizer5.Add(values,( iq, ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
385 | |
---|
386 | disp_list.sort() |
---|
387 | iy = 1 |
---|
388 | ix = 0 |
---|
389 | self.cb1 = wx.CheckBox(self, -1,'Parameters', (10, 10)) |
---|
390 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
391 | self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ |
---|
392 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
393 | ix +=1 |
---|
394 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
395 | self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
396 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
397 | ix +=2 |
---|
398 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
399 | self.sizer2.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
400 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
401 | self.text2_3.Hide() |
---|
402 | ix +=1 |
---|
403 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
404 | self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
405 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
406 | self.text2_4.Hide() |
---|
407 | |
---|
408 | for item in keys: |
---|
409 | if not item in disp_list: |
---|
410 | iy += 1 |
---|
411 | ix = 0 |
---|
412 | |
---|
413 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
414 | cb.SetValue(False) |
---|
415 | self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
416 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
417 | |
---|
418 | ix += 1 |
---|
419 | value= self.model.getParam(item) |
---|
420 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
421 | ctl1.SetValue(str (format_number(value))) |
---|
422 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
423 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
424 | self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
425 | ix += 1 |
---|
426 | text2=wx.StaticText(self, -1, '+/-') |
---|
427 | self.sizer2.Add(text2,(iy, ix),(1,1),\ |
---|
428 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
429 | text2.Hide() |
---|
430 | ix += 1 |
---|
431 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
432 | self.sizer2.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
433 | ctl2.Hide() |
---|
434 | ix +=1 |
---|
435 | # Units |
---|
436 | try: |
---|
437 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
438 | except: |
---|
439 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
440 | self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
441 | else: |
---|
442 | ip = 0 |
---|
443 | iq += 1 |
---|
444 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
445 | cb.SetValue(False) |
---|
446 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
447 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
448 | |
---|
449 | ip += 1 |
---|
450 | value= self.model.getParam(item) |
---|
451 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
452 | ctl1.SetValue(str (format_number(value))) |
---|
453 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
454 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
455 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
456 | |
---|
457 | #save data |
---|
458 | self.parameters.append([cb,ctl1,text2,ctl2]) |
---|
459 | |
---|
460 | iy+=1 |
---|
461 | self.sizer2.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
462 | |
---|
463 | #Display units text on panel |
---|
464 | for item in keys: |
---|
465 | if self.model.details[item][0]!='': |
---|
466 | self.text2_4.Show() |
---|
467 | break |
---|
468 | else: |
---|
469 | self.text2_4.Hide() |
---|
470 | #Disable or enable fit button |
---|
471 | |
---|
472 | if not (len(self.param_toFit ) >0): |
---|
473 | self.xmin.Disable() |
---|
474 | self.xmax.Disable() |
---|
475 | self.ymin.Disable() |
---|
476 | self.ymax.Disable() |
---|
477 | else: |
---|
478 | self.xmin.Enable() |
---|
479 | self.xmax.Enable() |
---|
480 | self.ymin.Enable() |
---|
481 | self.ymax.Enable() |
---|
482 | |
---|
483 | self.compute_chisqr() |
---|
484 | self.vbox.Layout() |
---|
485 | self.GrandParent.GetSizer().Layout() |
---|
486 | |
---|
487 | |
---|
488 | |
---|
489 | def _onparamEnter(self,event): |
---|
490 | """ |
---|
491 | when enter value on panel redraw model according to changed |
---|
492 | """ |
---|
493 | self.set_model_parameter() |
---|
494 | self.compute_chisqr() |
---|
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 | print "went here",len(self.parameters) ,self.model |
---|
502 | if len(self.parameters) !=0 and self.model !=None: |
---|
503 | # Flag to register when a parameter has changed. |
---|
504 | is_modified = False |
---|
505 | for item in self.parameters: |
---|
506 | try: |
---|
507 | item[2].Hide() |
---|
508 | item[3].Clear() |
---|
509 | item[3].Hide() |
---|
510 | name=str(item[0].GetLabelText()) |
---|
511 | value= float(item[1].GetValue()) |
---|
512 | # If the value of the parameter has changed, |
---|
513 | # update the model and set the is_modified flag |
---|
514 | if value != self.model.getParam(name): |
---|
515 | self.model.setParam(name,value) |
---|
516 | is_modified = True |
---|
517 | except: |
---|
518 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
519 | "Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
520 | # Here we should check whether the boundaries have been modified. |
---|
521 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
522 | # set the is_modified flag to True |
---|
523 | if float(self.xmin.GetValue()) != self.qmin: |
---|
524 | self.qmin = float(self.xmin.GetValue()) |
---|
525 | is_modified = True |
---|
526 | if float(self.xmax.GetValue()) != self.qmax: |
---|
527 | self.qmax = float(self.xmax.GetValue()) |
---|
528 | is_modified = True |
---|
529 | |
---|
530 | if is_modified: |
---|
531 | |
---|
532 | self.manager.redraw_model( |
---|
533 | qmin=self.qmin, qmax=self.qmax, |
---|
534 | ) |
---|
535 | #self.manager.draw_model(self,model,description=None, |
---|
536 | # enable1D=True,qmin=None,qmax=None, qstep=None) |
---|
537 | |
---|
538 | def select_all_param(self,event): |
---|
539 | """ |
---|
540 | set to true or false all checkBox given the main checkbox value cb1 |
---|
541 | """ |
---|
542 | self.param_toFit=[] |
---|
543 | if self.parameters !=[]: |
---|
544 | if self.cb1.GetValue()==True: |
---|
545 | for item in self.parameters: |
---|
546 | item[0].SetValue(True) |
---|
547 | list= [item[0],item[1],item[2],item[3]] |
---|
548 | self.param_toFit.append(list ) |
---|
549 | |
---|
550 | if not (len(self.param_toFit ) >0): |
---|
551 | self.xmin.Disable() |
---|
552 | self.xmax.Disable() |
---|
553 | self.ymin.Disable() |
---|
554 | self.ymax.Disable() |
---|
555 | else: |
---|
556 | self.xmin.Enable() |
---|
557 | self.xmax.Enable() |
---|
558 | self.ymin.Enable() |
---|
559 | self.ymax.Enable() |
---|
560 | else: |
---|
561 | for item in self.parameters: |
---|
562 | item[0].SetValue(False) |
---|
563 | self.param_toFit=[] |
---|
564 | |
---|
565 | self.xmin.Disable() |
---|
566 | self.xmax.Disable() |
---|
567 | self.ymin.Disable() |
---|
568 | self.ymax.Disable() |
---|
569 | |
---|
570 | |
---|
571 | def select_param(self,event): |
---|
572 | """ |
---|
573 | Select TextCtrl checked for fitting purpose and stores them |
---|
574 | in self.param_toFit=[] list |
---|
575 | """ |
---|
576 | self.param_toFit=[] |
---|
577 | for item in self.parameters: |
---|
578 | if item[0].GetValue()==True: |
---|
579 | list= [item[0],item[1],item[2],item[3]] |
---|
580 | self.param_toFit.append(list ) |
---|
581 | else: |
---|
582 | if item in self.param_toFit: |
---|
583 | self.param_toFit.remove(item) |
---|
584 | if len(self.parameters)==len(self.param_toFit): |
---|
585 | self.cb1.SetValue(True) |
---|
586 | else: |
---|
587 | self.cb1.SetValue(False) |
---|
588 | |
---|
589 | if not (len(self.param_toFit ) >0): |
---|
590 | self.xmin.Disable() |
---|
591 | self.xmax.Disable() |
---|
592 | self.ymin.Disable() |
---|
593 | self.ymax.Disable() |
---|
594 | else: |
---|
595 | self.xmin.Enable() |
---|
596 | self.xmax.Enable() |
---|
597 | self.ymin.Enable() |
---|
598 | self.ymax.Enable() |
---|
599 | |
---|
600 | |
---|
601 | |
---|
602 | |
---|
603 | def onsetValues(self,chisqr, out,cov): |
---|
604 | """ |
---|
605 | Build the panel from the fit result |
---|
606 | @param chisqr:Value of the goodness of fit metric |
---|
607 | @param out:list of parameter with the best value found during fitting |
---|
608 | @param cov:Covariance matrix |
---|
609 | |
---|
610 | """ |
---|
611 | #print "fitting : onsetvalues out",out |
---|
612 | self.tcChi.Clear() |
---|
613 | self.tcChi.SetValue(format_number(chisqr)) |
---|
614 | params = {} |
---|
615 | is_modified = False |
---|
616 | has_error = False |
---|
617 | if out.__class__==numpy.float64: |
---|
618 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
619 | self.param_toFit[0][1].Refresh() |
---|
620 | if cov !=None : |
---|
621 | self.text2_3.Show() |
---|
622 | self.param_toFit[0][2].Show() |
---|
623 | self.param_toFit[0][3].Clear() |
---|
624 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
625 | self.param_toFit[0][3].Show() |
---|
626 | #out is a list : set parameters and errors in TextCtrl |
---|
627 | else: |
---|
628 | i=0 |
---|
629 | #print "fitpage: list param model",list |
---|
630 | #for item in self.param_toFit: |
---|
631 | # print "fitpage: list display",item[0].GetLabelText() |
---|
632 | for item in self.param_toFit: |
---|
633 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
634 | #item[1].SetValue(format_number(out[i])) |
---|
635 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
636 | item[1].Refresh() |
---|
637 | if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
638 | self.text2_3.Show() |
---|
639 | item[2].Show() |
---|
640 | item[3].Clear() |
---|
641 | item[3].SetValue(format_number(cov[i])) |
---|
642 | item[3].Show() |
---|
643 | i+=1 |
---|
644 | |
---|
645 | self.vbox.Layout() |
---|
646 | self.GrandParent.GetSizer().Layout() |
---|
647 | |
---|