1 | |
---|
2 | |
---|
3 | import sys |
---|
4 | import wx |
---|
5 | import wx.lib.newevent |
---|
6 | import numpy |
---|
7 | import copy |
---|
8 | import math |
---|
9 | from sans.models.dispersion_models import ArrayDispersion, GaussianDispersion |
---|
10 | |
---|
11 | from sans.guicomm.events import StatusEvent |
---|
12 | from sans.guiframe.utils import format_number,check_float |
---|
13 | |
---|
14 | ## event to post model to fit to fitting plugins |
---|
15 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
16 | |
---|
17 | ## event to know the selected fit engine |
---|
18 | (FitterTypeEvent, EVT_FITTER_TYPE) = wx.lib.newevent.NewEvent() |
---|
19 | (FitStopEvent, EVT_FIT_STOP) = wx.lib.newevent.NewEvent() |
---|
20 | _BOX_WIDTH = 76 |
---|
21 | |
---|
22 | import basepage |
---|
23 | from basepage import BasicPage |
---|
24 | from basepage import PageInfoEvent |
---|
25 | from DataLoader.qsmearing import smear_selection |
---|
26 | |
---|
27 | class FitPage(BasicPage): |
---|
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 | def __init__(self,parent, page_info): |
---|
36 | BasicPage.__init__(self, parent, page_info) |
---|
37 | """ |
---|
38 | Initialization of the Panel |
---|
39 | """ |
---|
40 | ## fit page does not content npts txtcrtl |
---|
41 | self.npts=None |
---|
42 | ## thread for compute Chisqr |
---|
43 | self.calc_Chisqr=None |
---|
44 | ## default fitengine type |
---|
45 | self.engine_type = None |
---|
46 | ## draw sizer |
---|
47 | self._fill_datainfo_sizer() |
---|
48 | |
---|
49 | self._fill_model_sizer( self.sizer1) |
---|
50 | self._fill_range_sizer() |
---|
51 | #self._on_select_model(event=None) |
---|
52 | if self.data !=None: |
---|
53 | self.smearer = smear_selection( self.data ) |
---|
54 | if self.smearer ==None: |
---|
55 | self.enable_smearer.Disable() |
---|
56 | self.disable_smearer.Disable() |
---|
57 | |
---|
58 | ## to update the panel according to the fit engine type selected |
---|
59 | self.Bind(EVT_FITTER_TYPE,self._on_engine_change) |
---|
60 | self.Bind(EVT_FIT_STOP,self._on_fit_complete) |
---|
61 | |
---|
62 | def _on_fit_complete(self, event): |
---|
63 | """ |
---|
64 | When fit is complete ,reset the fit button label. |
---|
65 | """ |
---|
66 | #self.btFit.SetLabel("Fit") |
---|
67 | #self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
68 | #self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
69 | pass |
---|
70 | |
---|
71 | |
---|
72 | def _on_engine_change(self, event): |
---|
73 | """ |
---|
74 | get an event containing the current name of the fit engine type |
---|
75 | @param event: FitterTypeEvent containing the name of the current engine |
---|
76 | """ |
---|
77 | self.engine_type = event.type |
---|
78 | |
---|
79 | if len(self.parameters)==0: |
---|
80 | return |
---|
81 | if event.type =="park": |
---|
82 | self.btFit.SetLabel("Fit") |
---|
83 | for item in self.parameters: |
---|
84 | if event.type =="scipy": |
---|
85 | item[5].SetValue("") |
---|
86 | item[5].Hide() |
---|
87 | item[6].SetValue("") |
---|
88 | item[6].Hide() |
---|
89 | self.text2_min.Hide() |
---|
90 | self.text2_max.Hide() |
---|
91 | else: |
---|
92 | item[5].Show(True) |
---|
93 | item[6].Show(True) |
---|
94 | self.text2_min.Show(True) |
---|
95 | self.text2_max.Show(True) |
---|
96 | for item in self.orientation_params: |
---|
97 | if event.type =="scipy": |
---|
98 | item[5].SetValue("") |
---|
99 | item[5].Hide() |
---|
100 | item[6].SetValue("") |
---|
101 | item[6].Hide() |
---|
102 | self.text2_min.Hide() |
---|
103 | self.text2_max.Hide() |
---|
104 | else: |
---|
105 | item[5].Show(True) |
---|
106 | item[6].Show(True) |
---|
107 | self.text2_min.Show(True) |
---|
108 | self.text2_max.Show(True) |
---|
109 | |
---|
110 | for item in self.orientation_params_disp: |
---|
111 | if event.type =="scipy": |
---|
112 | item[5].SetValue("") |
---|
113 | item[5].Hide() |
---|
114 | item[6].SetValue("") |
---|
115 | item[6].Hide() |
---|
116 | self.text2_min.Hide() |
---|
117 | self.text2_max.Hide() |
---|
118 | else: |
---|
119 | item[5].Show(True) |
---|
120 | item[6].Show(True) |
---|
121 | self.text2_min.Show(True) |
---|
122 | self.text2_max.Show(True) |
---|
123 | |
---|
124 | self.sizer3.Layout() |
---|
125 | self.SetScrollbars(20,20,200,100) |
---|
126 | |
---|
127 | |
---|
128 | def _fill_range_sizer(self): |
---|
129 | """ |
---|
130 | Fill the sizer containing the plotting range |
---|
131 | add access to npts |
---|
132 | """ |
---|
133 | sizer_fit = wx.GridSizer(1, 1,0, 0) |
---|
134 | |
---|
135 | self.btFit = wx.Button(self,wx.NewId(),'Fit') |
---|
136 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId()) |
---|
137 | self.btFit.SetToolTipString("Perform fit.") |
---|
138 | |
---|
139 | |
---|
140 | sizer_fit.Add((5,5),1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
141 | sizer_fit.Add(self.btFit,0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
142 | |
---|
143 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
144 | #Filling the sizer containing instruments smearing info. |
---|
145 | self.disable_smearer = wx.RadioButton(self, -1, 'No', style=wx.RB_GROUP) |
---|
146 | self.enable_smearer = wx.RadioButton(self, -1, 'Yes') |
---|
147 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
148 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
149 | |
---|
150 | sizer_smearer.Add(wx.StaticText(self,-1,'Instrument Smearing? ')) |
---|
151 | sizer_smearer.Add((10, 10)) |
---|
152 | sizer_smearer.Add( self.enable_smearer ) |
---|
153 | sizer_smearer.Add((10,10)) |
---|
154 | sizer_smearer.Add( self.disable_smearer ) |
---|
155 | |
---|
156 | #Display Chi^2/dof |
---|
157 | sizer_smearer.Add((68,10)) |
---|
158 | box_description= wx.StaticBox(self, -1,'Chi2/dof') |
---|
159 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
160 | boxsizer1.SetMinSize((60,-1)) |
---|
161 | temp_smearer = None |
---|
162 | if self.enable_smearer.GetValue(): |
---|
163 | temp_smearer= self.smearer |
---|
164 | |
---|
165 | self.tcChi = wx.StaticText(self, -1, "-", style=wx.ALIGN_LEFT) |
---|
166 | |
---|
167 | boxsizer1.Add( self.tcChi ) |
---|
168 | sizer_smearer.Add( boxsizer1 ) |
---|
169 | |
---|
170 | #Set sizer for Fitting section |
---|
171 | self._set_range_sizer( title="Fitting",object1=sizer_smearer, object= sizer_fit) |
---|
172 | |
---|
173 | |
---|
174 | def _fill_datainfo_sizer(self): |
---|
175 | """ |
---|
176 | fill sizer 0 with data info |
---|
177 | """ |
---|
178 | self.sizer0.Clear(True) |
---|
179 | ## no loaded data , don't fill the sizer |
---|
180 | if self.data== None: |
---|
181 | self.sizer0.Layout() |
---|
182 | return |
---|
183 | |
---|
184 | box_description= wx.StaticBox(self, -1, 'Data') |
---|
185 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
186 | #---------------------------------------------------------- |
---|
187 | sizer_data = wx.GridSizer(3, 3,5, 5) |
---|
188 | #Filling the sizer containing data related fields |
---|
189 | DataSource =wx.StaticText(self, -1,str(self.data.name)) |
---|
190 | |
---|
191 | sizer_data.Add(wx.StaticText(self, -1, 'Source Name : ')) |
---|
192 | sizer_data.Add(DataSource ) |
---|
193 | sizer_data.Add( (0,5) ) |
---|
194 | |
---|
195 | #---------sizer 2 draw-------------------------------- |
---|
196 | #set maximum range for x in linear scale |
---|
197 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
198 | # Minimum value of data |
---|
199 | data_min = min(self.data.x) |
---|
200 | # Maximum value of data |
---|
201 | data_max = max(self.data.x) |
---|
202 | text4_3 = wx.StaticText(self, -1, 'Total Q Range (1/A)', |
---|
203 | style=wx.ALIGN_LEFT) |
---|
204 | sizer_data.Add( text4_3 ) |
---|
205 | sizer_data.Add(wx.StaticText(self, -1, "Min : %s"%str(data_min))) |
---|
206 | sizer_data.Add(wx.StaticText(self, -1, "Max : %s"%str(data_max))) |
---|
207 | |
---|
208 | else: |
---|
209 | ## Minimum value of data |
---|
210 | data_min= 0 |
---|
211 | x= max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
212 | y= max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
213 | ## Maximum value of data |
---|
214 | data_max = math.sqrt(x*x + y*y) |
---|
215 | |
---|
216 | #For qmin and qmax, do not use format_number |
---|
217 | #.(If do, qmin and max could be different from what is in the data.) |
---|
218 | text4_3 = wx.StaticText(self, -1, "Total Q Range (1/A)", |
---|
219 | style=wx.ALIGN_LEFT) |
---|
220 | sizer_data.Add( text4_3 ) |
---|
221 | sizer_data.Add(wx.StaticText(self, -1, "Min : %s"%str(data_min))) |
---|
222 | sizer_data.Add(wx.StaticText(self, -1, "Max : %s"%str(data_max))) |
---|
223 | ## set q range to plot |
---|
224 | self.qmin_x= data_min |
---|
225 | self.qmax_x= data_max |
---|
226 | |
---|
227 | boxsizer1.Add(sizer_data) |
---|
228 | #------------------------------------------------------------ |
---|
229 | self.sizer0.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
230 | self.sizer0.Layout() |
---|
231 | |
---|
232 | |
---|
233 | |
---|
234 | def _fill_model_sizer(self, sizer): |
---|
235 | """ |
---|
236 | fill sizer containing model info |
---|
237 | """ |
---|
238 | |
---|
239 | ## class base method to add view 2d button |
---|
240 | self._set_model_sizer(sizer=sizer, title="Model",object=None ) |
---|
241 | |
---|
242 | |
---|
243 | def _set_sizer_gaussian(self): |
---|
244 | """ |
---|
245 | draw sizer with gaussian dispersity parameters |
---|
246 | """ |
---|
247 | self.fittable_param=[] |
---|
248 | self.fixed_param=[] |
---|
249 | self.orientation_params_disp=[] |
---|
250 | |
---|
251 | self.sizer4_4.Clear(True) |
---|
252 | |
---|
253 | if self.model==None: |
---|
254 | ##no model is selected |
---|
255 | return |
---|
256 | if not self.enable_disp.GetValue(): |
---|
257 | ## the user didn't select dispersity display |
---|
258 | return |
---|
259 | |
---|
260 | self._reset_dispersity() |
---|
261 | # Create the dispersion objects |
---|
262 | for item in self.model.dispersion.keys(): |
---|
263 | disp_model = GaussianDispersion() |
---|
264 | self._disp_obj_dict[item] = disp_model |
---|
265 | self.model.set_dispersion(item, disp_model) |
---|
266 | self.state._disp_obj_dict[item]= disp_model |
---|
267 | |
---|
268 | ix=0 |
---|
269 | iy=1 |
---|
270 | disp = wx.StaticText(self, -1, 'Names') |
---|
271 | self.sizer4_4.Add(disp,( iy, ix),(1,1), |
---|
272 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
273 | ix += 1 |
---|
274 | values = wx.StaticText(self, -1, 'Values') |
---|
275 | self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
276 | ix +=2 |
---|
277 | self.text_disp_1 = wx.StaticText(self, -1, 'Errors') |
---|
278 | self.sizer4_4.Add( self.text_disp_1,(iy, ix),(1,1),\ |
---|
279 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
280 | self.text_disp_1.Hide() |
---|
281 | ix += 1 |
---|
282 | npts = wx.StaticText(self, -1, 'Npts') |
---|
283 | self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
284 | ix += 1 |
---|
285 | nsigmas = wx.StaticText(self, -1, 'Nsigmas') |
---|
286 | self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
287 | |
---|
288 | for item in self.model.dispersion.keys(): |
---|
289 | if not item in self.model.orientation_params: |
---|
290 | self.disp_cb_dict[item]= None |
---|
291 | name1=item+".width" |
---|
292 | name2=item+".npts" |
---|
293 | name3=item+".nsigmas" |
---|
294 | iy += 1 |
---|
295 | for p in self.model.dispersion[item].keys(): |
---|
296 | |
---|
297 | if p=="width": |
---|
298 | ix = 0 |
---|
299 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
300 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
301 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
302 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
303 | ix = 1 |
---|
304 | value= self.model.getParam(name1) |
---|
305 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
306 | style=wx.TE_PROCESS_ENTER) |
---|
307 | ctl1.SetValue(str (format_number(value))) |
---|
308 | ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
309 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
310 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
311 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
312 | ## text to show error sign |
---|
313 | ix = 2 |
---|
314 | text2=wx.StaticText(self, -1, '+/-') |
---|
315 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
316 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
317 | text2.Hide() |
---|
318 | ## txtcrtl to add error from fit |
---|
319 | ix = 3 |
---|
320 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
321 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
322 | ctl2.Hide() |
---|
323 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
324 | ctl2, None, None,None]) |
---|
325 | elif p=="npts": |
---|
326 | ix = 4 |
---|
327 | value= self.model.getParam(name2) |
---|
328 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
329 | style=wx.TE_PROCESS_ENTER) |
---|
330 | |
---|
331 | Tctl.SetValue(str (format_number(value))) |
---|
332 | Tctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
333 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
334 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
335 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
336 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
337 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
338 | None, None,None]) |
---|
339 | elif p=="nsigmas": |
---|
340 | ix = 5 |
---|
341 | value= self.model.getParam(name3) |
---|
342 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
343 | style=wx.TE_PROCESS_ENTER) |
---|
344 | Tctl.SetValue(str (format_number(value))) |
---|
345 | Tctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
346 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
347 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
348 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
349 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
350 | ix +=1 |
---|
351 | self.sizer4_4.Add((20,20), (iy,ix),(1,1), |
---|
352 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
353 | |
---|
354 | self.fixed_param.append([None,name3, Tctl |
---|
355 | ,None,None, None, None,None]) |
---|
356 | ix =0 |
---|
357 | iy +=1 |
---|
358 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
359 | for item in self.model.dispersion.keys(): |
---|
360 | if item in self.model.orientation_params: |
---|
361 | self.disp_cb_dict[item]= None |
---|
362 | name1=item+".width" |
---|
363 | name2=item+".npts" |
---|
364 | name3=item+".nsigmas" |
---|
365 | iy += 1 |
---|
366 | for p in self.model.dispersion[item].keys(): |
---|
367 | |
---|
368 | if p=="width": |
---|
369 | ix = 0 |
---|
370 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
371 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
372 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
373 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
374 | if self.data.__class__.__name__ =="Data2D": |
---|
375 | cb.Enable() |
---|
376 | else: |
---|
377 | cb.Disable() |
---|
378 | ix = 1 |
---|
379 | value= self.model.getParam(name1) |
---|
380 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
381 | style=wx.TE_PROCESS_ENTER) |
---|
382 | ctl1.SetValue(str (format_number(value))) |
---|
383 | if self.data.__class__.__name__ =="Data2D": |
---|
384 | ctl1.Enable() |
---|
385 | else: |
---|
386 | ctl1.Disable() |
---|
387 | ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
388 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
389 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
390 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
391 | ## text to show error sign |
---|
392 | ix = 2 |
---|
393 | text2=wx.StaticText(self, -1, '+/-') |
---|
394 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
395 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
396 | text2.Hide() |
---|
397 | ## txtcrtl to add error from fit |
---|
398 | ix = 3 |
---|
399 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
400 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
401 | ctl2.Hide() |
---|
402 | if self.data.__class__.__name__ =="Data2D": |
---|
403 | ctl2.Enable() |
---|
404 | else: |
---|
405 | ctl2.Disable() |
---|
406 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
407 | ctl2, None, None,None]) |
---|
408 | self.orientation_params_disp.append([cb,name1,ctl1,text2, |
---|
409 | ctl2, None, None,None]) |
---|
410 | elif p=="npts": |
---|
411 | ix = 4 |
---|
412 | value= self.model.getParam(name2) |
---|
413 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
414 | style=wx.TE_PROCESS_ENTER) |
---|
415 | |
---|
416 | Tctl.SetValue(str (format_number(value))) |
---|
417 | if self.data.__class__.__name__ =="Data2D": |
---|
418 | Tctl.Enable() |
---|
419 | else: |
---|
420 | Tctl.Disable() |
---|
421 | Tctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
422 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
423 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
424 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
425 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
426 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
427 | None, None,None]) |
---|
428 | self.orientation_params_disp.append([None,name2, Tctl,None,None, |
---|
429 | None, None,None]) |
---|
430 | elif p=="nsigmas": |
---|
431 | ix = 5 |
---|
432 | value= self.model.getParam(name3) |
---|
433 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
434 | style=wx.TE_PROCESS_ENTER) |
---|
435 | Tctl.SetValue(str (format_number(value))) |
---|
436 | if self.data.__class__.__name__ =="Data2D": |
---|
437 | Tctl.Enable() |
---|
438 | else: |
---|
439 | Tctl.Disable() |
---|
440 | Tctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
441 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
442 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
443 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
444 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
445 | ix +=1 |
---|
446 | self.sizer4_4.Add((20,20), (iy,ix),(1,1), |
---|
447 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
448 | self.fixed_param.append([None,name3, Tctl |
---|
449 | ,None,None, None, None,None]) |
---|
450 | self.orientation_params_disp.append([None,name3, Tctl |
---|
451 | ,None,None, None, None,None]) |
---|
452 | |
---|
453 | self.state.disp_cb_dict = copy.deepcopy(self.disp_cb_dict) |
---|
454 | self.state.model = self.model.clone() |
---|
455 | ## save state into |
---|
456 | self._copy_parameters_state(self.orientation_params_disp, |
---|
457 | self.state.orientation_params_disp) |
---|
458 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
459 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
460 | |
---|
461 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
462 | " Selected Distribution: Gaussian")) |
---|
463 | ix =0 |
---|
464 | iy +=1 |
---|
465 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
466 | self.sizer4_4.Layout() |
---|
467 | self.sizer4.Layout() |
---|
468 | self.Layout() |
---|
469 | self.SetScrollbars(20,20,200,100) |
---|
470 | self.Refresh() |
---|
471 | |
---|
472 | |
---|
473 | def _onFit(self, event): |
---|
474 | """ |
---|
475 | Allow to fit |
---|
476 | """ |
---|
477 | from sans.guiframe.utils import check_value |
---|
478 | flag = check_value( self.qmin, self.qmax) |
---|
479 | |
---|
480 | if not flag: |
---|
481 | msg= "Fitting range invalid" |
---|
482 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
483 | return |
---|
484 | |
---|
485 | if len(self.param_toFit) <= 0: |
---|
486 | msg= "Select at least one parameter to fit" |
---|
487 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
488 | return |
---|
489 | |
---|
490 | # Remove or do not allow fitting on the Q=0 point, especially when y(q=0)=None at x[0]. |
---|
491 | #ToDo: Fix this. |
---|
492 | self.qmin_x = float(self.qmin.GetValue()) |
---|
493 | self.qmax_x =float( self.qmax.GetValue()) |
---|
494 | self.manager._reset_schedule_problem( value=0) |
---|
495 | self.manager.schedule_for_fit( value=1,page=self,fitproblem =None) |
---|
496 | self.manager.set_fit_range(page= self,qmin= self.qmin_x, qmax= self.qmax_x) |
---|
497 | |
---|
498 | #single fit |
---|
499 | self.manager.onFit() |
---|
500 | ## allow stopping the fit |
---|
501 | #if self.engine_type=="scipy": |
---|
502 | # self.btFit.SetLabel("Stop") |
---|
503 | # self.btFit.Unbind(event=wx.EVT_BUTTON, id= self.btFit.GetId()) |
---|
504 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._StopFit, id=self.btFit.GetId()) |
---|
505 | #else: |
---|
506 | # self.btFit.SetLabel("Fit") |
---|
507 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._onFit, id=self.btFit.GetId()) |
---|
508 | self.btFit.SetFocus() |
---|
509 | self.sizer5.Layout() |
---|
510 | self.SetScrollbars(20,20,55,40) |
---|
511 | |
---|
512 | def _StopFit(self, event): |
---|
513 | """ |
---|
514 | Stop fit |
---|
515 | """ |
---|
516 | self.btFit.SetLabel("Fit") |
---|
517 | if self.engine_type=="scipy": |
---|
518 | self.manager.stop_fit() |
---|
519 | self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
520 | self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
521 | |
---|
522 | |
---|
523 | self.btFit.SetFocus() |
---|
524 | self.sizer5.Layout() |
---|
525 | self.SetScrollbars(20,20,55,40) |
---|
526 | |
---|
527 | def _on_select_model(self, event): |
---|
528 | """ |
---|
529 | call back for model selection |
---|
530 | """ |
---|
531 | self._on_select_model_helper() |
---|
532 | self.set_model_param_sizer(self.model) |
---|
533 | |
---|
534 | self.enable_disp.SetValue(False) |
---|
535 | self.disable_disp.SetValue(True) |
---|
536 | self.set_dispers_sizer() |
---|
537 | if self.model !=None: |
---|
538 | try: |
---|
539 | temp_smear= None |
---|
540 | if self.enable_smearer.GetValue(): |
---|
541 | temp_smear= self.smearer |
---|
542 | self.compute_chisqr(temp_smear) |
---|
543 | except: |
---|
544 | ## error occured on chisqr computation |
---|
545 | pass |
---|
546 | evt = ModelEventbox(model=self.model) |
---|
547 | wx.PostEvent(self.event_owner, evt) |
---|
548 | self.btFit.SetFocus() |
---|
549 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
550 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
551 | |
---|
552 | self.state.structurecombobox = self.structurebox.GetCurrentSelection() |
---|
553 | self.state.formfactorcombobox = self.formfactorbox.GetCurrentSelection() |
---|
554 | #print "_on_select_model",self.state.structurecombobox,self.state.formfactorcombobox |
---|
555 | |
---|
556 | if event !=None: |
---|
557 | self._undo.Enable(True) |
---|
558 | ## post state to fit panel |
---|
559 | event = PageInfoEvent(page = self) |
---|
560 | wx.PostEvent(self.parent, event) |
---|
561 | |
---|
562 | |
---|
563 | def _onparamRangeEnter(self, event): |
---|
564 | """ |
---|
565 | Check validity of value enter in the parameters range field |
---|
566 | """ |
---|
567 | tcrtl= event.GetEventObject() |
---|
568 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
569 | try: |
---|
570 | value = float(tcrtl.GetValue()) |
---|
571 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
572 | tcrtl.Refresh() |
---|
573 | except: |
---|
574 | tcrtl.SetBackgroundColour("pink") |
---|
575 | tcrtl.Refresh() |
---|
576 | return |
---|
577 | else: |
---|
578 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
579 | tcrtl.Refresh() |
---|
580 | self._onparamEnter_helper() |
---|
581 | |
---|
582 | ##compute chisqr for range change |
---|
583 | temp_smearer = None |
---|
584 | if self.enable_smearer.GetValue(): |
---|
585 | msg="" |
---|
586 | temp_smearer= self.smearer |
---|
587 | ##Calculate chi2 |
---|
588 | self.compute_chisqr(smearer= temp_smearer) |
---|
589 | ## new state posted |
---|
590 | if self.state_change: |
---|
591 | self._undo.Enable(True) |
---|
592 | event = PageInfoEvent(page = self) |
---|
593 | wx.PostEvent(self.parent, event) |
---|
594 | self.state_change= False |
---|
595 | |
---|
596 | |
---|
597 | def _onparamEnter(self,event): |
---|
598 | """ |
---|
599 | when enter value on panel redraw model according to changed |
---|
600 | """ |
---|
601 | tcrtl= event.GetEventObject() |
---|
602 | |
---|
603 | if check_float(tcrtl): |
---|
604 | self._onparamEnter_helper() |
---|
605 | temp_smearer = None |
---|
606 | if self.enable_smearer.GetValue(): |
---|
607 | temp_smearer= self.smearer |
---|
608 | self.compute_chisqr(smearer= temp_smearer) |
---|
609 | ## new state posted |
---|
610 | if self.state_change: |
---|
611 | self._undo.Enable(True) |
---|
612 | event = PageInfoEvent(page = self) |
---|
613 | wx.PostEvent(self.parent, event) |
---|
614 | self.state_change= False |
---|
615 | else: |
---|
616 | msg= "Cannot Plot :Must enter a number!!! " |
---|
617 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
618 | return |
---|
619 | |
---|
620 | |
---|
621 | def set_data(self, data ): |
---|
622 | """ |
---|
623 | reset the current data |
---|
624 | """ |
---|
625 | if data ==None: |
---|
626 | return |
---|
627 | self.data =data |
---|
628 | self.state.data= data |
---|
629 | self._fill_datainfo_sizer() |
---|
630 | self.SetScrollbars(20,20,200,100) |
---|
631 | self.Layout() |
---|
632 | |
---|
633 | def reset_page(self, state,first=False): |
---|
634 | """ |
---|
635 | reset the state |
---|
636 | """ |
---|
637 | if first: |
---|
638 | self._undo.Enable(False) |
---|
639 | |
---|
640 | self.reset_page_helper(state) |
---|
641 | evt = ModelEventbox(model=self.model) |
---|
642 | wx.PostEvent(self.event_owner, evt) |
---|
643 | |
---|
644 | |
---|
645 | def get_range(self): |
---|
646 | """ |
---|
647 | return the fitting range |
---|
648 | """ |
---|
649 | return float(self.qmin_x) , float(self.qmax_x) |
---|
650 | |
---|
651 | def get_param_list(self): |
---|
652 | """ |
---|
653 | @return self.param_toFit: list containing references to TextCtrl |
---|
654 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
655 | @raise: if return an empty list of parameter fit will nnote work |
---|
656 | properly so raise ValueError,"missing parameter to fit" |
---|
657 | """ |
---|
658 | if self.param_toFit !=[]: |
---|
659 | return self.param_toFit |
---|
660 | else: |
---|
661 | raise ValueError,"missing parameter to fit" |
---|
662 | |
---|
663 | def onsetValues(self,chisqr, out,cov): |
---|
664 | """ |
---|
665 | Build the panel from the fit result |
---|
666 | @param chisqr:Value of the goodness of fit metric |
---|
667 | @param out:list of parameter with the best value found during fitting |
---|
668 | @param cov:Covariance matrix |
---|
669 | |
---|
670 | """ |
---|
671 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
672 | params = {} |
---|
673 | is_modified = False |
---|
674 | has_error = False |
---|
675 | self.text2_3.Hide() |
---|
676 | try: |
---|
677 | n = self.disp_box.GetCurrentSelection() |
---|
678 | dispersity= self.disp_box.GetClientData(n) |
---|
679 | name= dispersity.__name__ |
---|
680 | if name == "GaussianDispersion": |
---|
681 | if hasattr(self,"text_disp_1" ): |
---|
682 | if self.text_disp_1 !=None: |
---|
683 | self.text_disp_1.Hide() |
---|
684 | except: |
---|
685 | pass |
---|
686 | #set the panel when fit result are float not list |
---|
687 | if out.__class__==numpy.float64: |
---|
688 | self.param_toFit[0][2].SetValue(format_number(out)) |
---|
689 | self.param_toFit[0][2].Refresh() |
---|
690 | |
---|
691 | self.param_toFit[0][4].Clear() |
---|
692 | self.param_toFit[0][4].Hide() |
---|
693 | if cov !=None : |
---|
694 | self.text2_3.Show(True) |
---|
695 | try: |
---|
696 | name= dispersity.__name__ |
---|
697 | if name == "GaussianDispersion": |
---|
698 | if hasattr(self,"text_disp_1" ): |
---|
699 | if self.text_disp_1 !=None: |
---|
700 | self.text_disp_1.Show(True) |
---|
701 | except: |
---|
702 | pass |
---|
703 | if cov[0]==None or not numpy.isfinite(cov[0]): |
---|
704 | self.param_toFit[0][3].Hide() |
---|
705 | self.param_toFit[0][4].Clear() |
---|
706 | self.param_toFit[0][4].Hide() |
---|
707 | self.param_toFit[0][4].Refresh() |
---|
708 | else: |
---|
709 | self.param_toFit[0][3].Show(True) |
---|
710 | self.param_toFit[0][4].Clear() |
---|
711 | self.param_toFit[0][4].SetValue(format_number(cov[0])) |
---|
712 | self.param_toFit[0][4].Show(True) |
---|
713 | self.param_toFit[0][4].Refresh() |
---|
714 | else: |
---|
715 | i=0 |
---|
716 | j=0 |
---|
717 | #Set the panel when fit result are list |
---|
718 | for item in self.param_toFit: |
---|
719 | ## reset error value to initial state |
---|
720 | item[4].Clear() |
---|
721 | item[4].Hide() |
---|
722 | item[4].Refresh() |
---|
723 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
724 | item[2].SetValue(format_number(self.model.getParam(item[1]))) |
---|
725 | item[2].Refresh() |
---|
726 | if(cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
727 | self.text2_3.Show(True) |
---|
728 | try: |
---|
729 | name= dispersity.__name__ |
---|
730 | if name == "GaussianDispersion": |
---|
731 | if hasattr(self,"text_disp_1" ): |
---|
732 | if self.text_disp_1!=None: |
---|
733 | self.text_disp_1.Show(True) |
---|
734 | except: |
---|
735 | pass |
---|
736 | item[3].Show(True) |
---|
737 | item[4].Clear() |
---|
738 | for j in range(len(out)): |
---|
739 | if out[j]==self.model.getParam(item[1]): |
---|
740 | break |
---|
741 | ## unable to compare cov[j]==numpy.nan so switch to None |
---|
742 | if cov[j]==None or not numpy.isfinite(cov[j]): |
---|
743 | item[3].Hide() |
---|
744 | item[4].Refresh() |
---|
745 | item[4].Clear() |
---|
746 | item[4].Hide() |
---|
747 | else: |
---|
748 | item[4].SetValue(format_number(cov[j])) |
---|
749 | item[4].Refresh() |
---|
750 | item[4].Show(True) |
---|
751 | i+=1 |
---|
752 | |
---|
753 | self.sizer3.Layout() |
---|
754 | self.sizer4.Layout() |
---|
755 | self.Layout() |
---|
756 | self.Refresh() |
---|
757 | self.SetScrollbars(20,20,200,100) |
---|
758 | |
---|
759 | |
---|
760 | def onSmear(self, event): |
---|
761 | """ |
---|
762 | Create a smear object that will change the way residuals |
---|
763 | are compute when fitting |
---|
764 | """ |
---|
765 | temp_smearer = None |
---|
766 | if self.enable_smearer.GetValue(): |
---|
767 | msg="" |
---|
768 | temp_smearer= self.smearer |
---|
769 | if hasattr(self.data,"dxl"): |
---|
770 | msg= ": Resolution smearing parameters" |
---|
771 | if hasattr(self.data,"dxw"): |
---|
772 | msg= ": Slit smearing parameters" |
---|
773 | if self.smearer ==None: |
---|
774 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
775 | "Data contains no smearing information")) |
---|
776 | else: |
---|
777 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
778 | "Data contains smearing information %s"%msg)) |
---|
779 | |
---|
780 | ## set smearing value whether or not the data contain the smearing info |
---|
781 | self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
782 | qmax= float(self.qmax_x)) |
---|
783 | ##Calculate chi2 |
---|
784 | self.compute_chisqr(smearer= temp_smearer) |
---|
785 | ## save the state enable smearing |
---|
786 | self.state.smearer= temp_smearer |
---|
787 | #self.save_current_state() |
---|
788 | |
---|
789 | |
---|
790 | def complete_chisqr(self, output, elapsed=None): |
---|
791 | """ |
---|
792 | print result chisqr |
---|
793 | """ |
---|
794 | try: |
---|
795 | self.tcChi.SetLabel(str(output)) |
---|
796 | |
---|
797 | self.sizer5.Layout() |
---|
798 | self.Layout() |
---|
799 | self.Refresh |
---|
800 | ## post state to fit panel |
---|
801 | self.state.tcChi =output |
---|
802 | #event = PageInfoEvent(page = self) |
---|
803 | #wx.PostEvent(self.parent, event) |
---|
804 | except: |
---|
805 | pass |
---|
806 | |
---|
807 | |
---|
808 | def compute_chisqr1D(self, smearer=None): |
---|
809 | """ |
---|
810 | Compute chisqr for 1D |
---|
811 | """ |
---|
812 | from sans.guiframe.utils import check_value |
---|
813 | flag = check_value( self.qmin, self.qmax) |
---|
814 | |
---|
815 | if not flag: |
---|
816 | return |
---|
817 | |
---|
818 | try: |
---|
819 | self.qmin_x = float(self.qmin.GetValue()) |
---|
820 | self.qmax_x = float(self.qmax.GetValue()) |
---|
821 | ##return residuals within self.qmin_x and self.qmax_x |
---|
822 | from gui_thread import CalcChisqr1D |
---|
823 | ## If a thread is already started, stop it |
---|
824 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
825 | self.calc_Chisqr.stop() |
---|
826 | |
---|
827 | self.calc_Chisqr= CalcChisqr1D( x= self.data.x, |
---|
828 | y= self.data.y, |
---|
829 | dy= self.data.dy, |
---|
830 | model= self.model, |
---|
831 | smearer=smearer, |
---|
832 | qmin=self.qmin_x, |
---|
833 | qmax=self.qmax_x, |
---|
834 | completefn = self.complete_chisqr, |
---|
835 | updatefn = None) |
---|
836 | |
---|
837 | self.calc_Chisqr.queue() |
---|
838 | |
---|
839 | except: |
---|
840 | raise ValueError," Could not compute Chisqr for %s Model 2D: "%self.model.name |
---|
841 | |
---|
842 | |
---|
843 | |
---|
844 | |
---|
845 | |
---|
846 | def compute_chisqr2D(self): |
---|
847 | """ |
---|
848 | compute chi square given a model and data 2D and set the value |
---|
849 | to the tcChi txtcrl |
---|
850 | """ |
---|
851 | from sans.guiframe.utils import check_value |
---|
852 | flag = check_value( self.qmin, self.qmax) |
---|
853 | if not flag: |
---|
854 | return |
---|
855 | |
---|
856 | try: |
---|
857 | self.qmin_x = float(self.qmin.GetValue()) |
---|
858 | self.qmax_x = float(self.qmax.GetValue()) |
---|
859 | |
---|
860 | ##return residuals within self.qmin_x and self.qmax_x |
---|
861 | from gui_thread import CalcChisqr2D |
---|
862 | ## If a thread is already started, stop it |
---|
863 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
864 | self.calc_Chisqr.stop() |
---|
865 | |
---|
866 | self.calc_Chisqr= CalcChisqr2D( x_bins= self.data.x_bins, |
---|
867 | y_bins= self.data.y_bins, |
---|
868 | data= self.data.data, |
---|
869 | err_data = self.data.err_data, |
---|
870 | model= self.model, |
---|
871 | qmin= self.qmin_x, |
---|
872 | qmax = self.qmax_x, |
---|
873 | completefn = self.complete_chisqr, |
---|
874 | updatefn = None) |
---|
875 | |
---|
876 | self.calc_Chisqr.queue() |
---|
877 | |
---|
878 | except: |
---|
879 | raise ValueError," Could not compute Chisqr for %s Model 2D: "%self.model.name |
---|
880 | |
---|
881 | |
---|
882 | |
---|
883 | def compute_chisqr(self , smearer=None): |
---|
884 | """ |
---|
885 | compute chi square given a model and data 1D and set the value |
---|
886 | to the tcChi txtcrl |
---|
887 | """ |
---|
888 | from sans.guiframe.utils import check_value |
---|
889 | flag = check_value( self.qmin, self.qmax) |
---|
890 | if flag== True: |
---|
891 | try: |
---|
892 | if hasattr(self.data,"data"): |
---|
893 | self.compute_chisqr2D() |
---|
894 | return |
---|
895 | else: |
---|
896 | self.compute_chisqr1D(smearer=smearer) |
---|
897 | return |
---|
898 | except: |
---|
899 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
900 | "Chisqr Error: %s"% sys.exc_value)) |
---|
901 | return |
---|
902 | |
---|
903 | |
---|
904 | def select_all_param(self,event): |
---|
905 | """ |
---|
906 | set to true or false all checkBox given the main checkbox value cb1 |
---|
907 | """ |
---|
908 | |
---|
909 | self.param_toFit=[] |
---|
910 | if self.parameters !=[]: |
---|
911 | if self.cb1.GetValue(): |
---|
912 | for item in self.parameters: |
---|
913 | ## for data2D select all to fit |
---|
914 | if self.data.__class__.__name__=="Data2D": |
---|
915 | item[0].SetValue(True) |
---|
916 | self.param_toFit.append(item ) |
---|
917 | else: |
---|
918 | ## for 1D all parameters except orientation |
---|
919 | if not item in self.orientation_params: |
---|
920 | item[0].SetValue(True) |
---|
921 | self.param_toFit.append(item ) |
---|
922 | if len(self.fittable_param)>0: |
---|
923 | for item in self.fittable_param: |
---|
924 | if self.data.__class__.__name__=="Data2D": |
---|
925 | item[0].SetValue(True) |
---|
926 | self.param_toFit.append(item ) |
---|
927 | else: |
---|
928 | ## for 1D all parameters except orientation |
---|
929 | if not item in self.orientation_params_disp: |
---|
930 | item[0].SetValue(True) |
---|
931 | self.param_toFit.append(item ) |
---|
932 | else: |
---|
933 | for item in self.parameters: |
---|
934 | item[0].SetValue(False) |
---|
935 | for item in self.fittable_param: |
---|
936 | item[0].SetValue(False) |
---|
937 | self.param_toFit=[] |
---|
938 | self._copy_parameters_state(self.orientation_params, |
---|
939 | self.state.orientation_params) |
---|
940 | self._copy_parameters_state(self.orientation_params_disp, |
---|
941 | self.state.orientation_params_disp) |
---|
942 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
943 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
944 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
945 | |
---|
946 | #self.save_current_state() |
---|
947 | if event !=None: |
---|
948 | self._undo.Enable(True) |
---|
949 | ## post state to fit panel |
---|
950 | event = PageInfoEvent(page = self) |
---|
951 | wx.PostEvent(self.parent, event) |
---|
952 | |
---|
953 | |
---|
954 | |
---|
955 | def select_param(self,event): |
---|
956 | """ |
---|
957 | Select TextCtrl checked for fitting purpose and stores them |
---|
958 | in self.param_toFit=[] list |
---|
959 | """ |
---|
960 | self.param_toFit=[] |
---|
961 | for item in self.parameters: |
---|
962 | #Select parameters to fit for list of primary parameters |
---|
963 | if item[0].GetValue(): |
---|
964 | if not (item in self.param_toFit): |
---|
965 | self.param_toFit.append(item ) |
---|
966 | else: |
---|
967 | #remove parameters from the fitting list |
---|
968 | if item in self.param_toFit: |
---|
969 | self.param_toFit.remove(item) |
---|
970 | #Select parameters to fit for list of fittable parameters with dispersion |
---|
971 | for item in self.fittable_param: |
---|
972 | if item[0].GetValue(): |
---|
973 | if not (item in self.param_toFit): |
---|
974 | self.param_toFit.append(item) |
---|
975 | else: |
---|
976 | #remove parameters from the fitting list |
---|
977 | if item in self.param_toFit: |
---|
978 | self.param_toFit.remove(item) |
---|
979 | #Set the value of checkbox that selected every checkbox or not |
---|
980 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
981 | self.cb1.SetValue(True) |
---|
982 | else: |
---|
983 | self.cb1.SetValue(False) |
---|
984 | ## save current state of the page |
---|
985 | self._copy_parameters_state(self.orientation_params, |
---|
986 | self.state.orientation_params) |
---|
987 | self._copy_parameters_state(self.orientation_params_disp, |
---|
988 | self.state.orientation_params_disp) |
---|
989 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
990 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
991 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
992 | |
---|
993 | #self.save_current_state() |
---|
994 | if event !=None: |
---|
995 | self._undo.Enable(True) |
---|
996 | ## post state to fit panel |
---|
997 | event = PageInfoEvent(page = self) |
---|
998 | wx.PostEvent(self.parent, event) |
---|
999 | |
---|
1000 | |
---|
1001 | |
---|
1002 | def set_model_param_sizer(self, model): |
---|
1003 | """ |
---|
1004 | Build the panel from the model content |
---|
1005 | @param model: the model selected in combo box for fitting purpose |
---|
1006 | """ |
---|
1007 | self.sizer3.Clear(True) |
---|
1008 | self.parameters = [] |
---|
1009 | self.param_toFit=[] |
---|
1010 | self.fittable_param=[] |
---|
1011 | self.fixed_param=[] |
---|
1012 | self.orientation_params=[] |
---|
1013 | self.orientation_params_disp=[] |
---|
1014 | |
---|
1015 | if model ==None: |
---|
1016 | self.sizer3.Layout() |
---|
1017 | self.SetScrollbars(20,20,200,100) |
---|
1018 | return |
---|
1019 | ## the panel is drawn using the current value of the fit engine |
---|
1020 | if self.engine_type==None and self.manager !=None: |
---|
1021 | self.engine_type= self.manager._return_engine_type() |
---|
1022 | |
---|
1023 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
1024 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1025 | sizer = wx.GridBagSizer(5,5) |
---|
1026 | ## save the current model |
---|
1027 | self.model = model |
---|
1028 | |
---|
1029 | keys = self.model.getParamList() |
---|
1030 | #list of dispersion paramaters |
---|
1031 | self.disp_list=self.model.getDispParamList() |
---|
1032 | |
---|
1033 | keys.sort() |
---|
1034 | |
---|
1035 | iy = 1 |
---|
1036 | ix = 0 |
---|
1037 | self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) |
---|
1038 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
1039 | self.cb1.SetValue(False) |
---|
1040 | |
---|
1041 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
1042 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
1043 | ix +=1 |
---|
1044 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
1045 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
1046 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1047 | ix +=2 |
---|
1048 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
1049 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
1050 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1051 | self.text2_3.Hide() |
---|
1052 | ix +=1 |
---|
1053 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
1054 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
1055 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1056 | self.text2_min.Hide() |
---|
1057 | ix +=1 |
---|
1058 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
1059 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
1060 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1061 | self.text2_max.Hide() |
---|
1062 | ix += 1 |
---|
1063 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
1064 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
1065 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1066 | self.text2_4.Hide() |
---|
1067 | if self.engine_type=="park": |
---|
1068 | self.text2_max.Show(True) |
---|
1069 | self.text2_min.Show(True) |
---|
1070 | |
---|
1071 | for item in keys: |
---|
1072 | if not item in self.disp_list and not item in self.model.orientation_params: |
---|
1073 | |
---|
1074 | iy += 1 |
---|
1075 | ix = 0 |
---|
1076 | ## add parameters name with checkbox for selecting to fit |
---|
1077 | cb = wx.CheckBox(self, -1, item ) |
---|
1078 | cb.SetValue(False) |
---|
1079 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
1080 | sizer.Add( cb,( iy, ix),(1,1), |
---|
1081 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
1082 | |
---|
1083 | ## add parameter value |
---|
1084 | ix += 1 |
---|
1085 | value= self.model.getParam(item) |
---|
1086 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
1087 | style=wx.TE_PROCESS_ENTER) |
---|
1088 | |
---|
1089 | ctl1.SetValue(format_number(value)) |
---|
1090 | ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1091 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
1092 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
1093 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
1094 | ## text to show error sign |
---|
1095 | ix += 1 |
---|
1096 | text2=wx.StaticText(self, -1, '+/-') |
---|
1097 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
1098 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1099 | text2.Hide() |
---|
1100 | ## txtcrtl to add error from fit |
---|
1101 | ix += 1 |
---|
1102 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
1103 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1104 | ctl2.Hide() |
---|
1105 | |
---|
1106 | param_min, param_max= self.model.details[item][1:] |
---|
1107 | ix += 1 |
---|
1108 | ctl3 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
1109 | if param_min ==None: |
---|
1110 | ctl3.SetValue("") |
---|
1111 | else: |
---|
1112 | ctl3.SetValue(str(param_min)) |
---|
1113 | ctl3.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1114 | ctl3.Bind(wx.EVT_KILL_FOCUS, self._onparamRangeEnter) |
---|
1115 | ctl3.Bind(wx.EVT_TEXT_ENTER,self._onparamRangeEnter) |
---|
1116 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1117 | ctl3.Hide() |
---|
1118 | |
---|
1119 | ix += 1 |
---|
1120 | ctl4 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
1121 | ctl4.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1122 | ctl4.Bind(wx.EVT_KILL_FOCUS, self._onparamRangeEnter) |
---|
1123 | ctl4.Bind(wx.EVT_TEXT_ENTER,self._onparamRangeEnter) |
---|
1124 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1125 | if param_max==None: |
---|
1126 | ctl4.SetValue("") |
---|
1127 | else: |
---|
1128 | ctl4.SetValue(str(param_max)) |
---|
1129 | ctl4.Hide() |
---|
1130 | |
---|
1131 | if self.engine_type=="park": |
---|
1132 | ctl3.Show(True) |
---|
1133 | ctl4.Show(True) |
---|
1134 | ix +=1 |
---|
1135 | # Units |
---|
1136 | try: |
---|
1137 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
1138 | except: |
---|
1139 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
1140 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1141 | |
---|
1142 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1143 | self.parameters.append([cb,item, ctl1, |
---|
1144 | text2,ctl2, ctl3, ctl4,units]) |
---|
1145 | |
---|
1146 | iy+=1 |
---|
1147 | sizer.Add((10,10),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1148 | for item in self.model.orientation_params: |
---|
1149 | if not item in self.disp_list : |
---|
1150 | iy += 1 |
---|
1151 | ix = 0 |
---|
1152 | ## add parameters name with checkbox for selecting to fit |
---|
1153 | cb = wx.CheckBox(self, -1, item ) |
---|
1154 | cb.SetValue(False) |
---|
1155 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
1156 | if self.data.__class__.__name__ =="Data2D": |
---|
1157 | cb.Enable() |
---|
1158 | else: |
---|
1159 | cb.Disable() |
---|
1160 | sizer.Add( cb,( iy, ix),(1,1), |
---|
1161 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
1162 | |
---|
1163 | ## add parameter value |
---|
1164 | ix += 1 |
---|
1165 | value= self.model.getParam(item) |
---|
1166 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
1167 | style=wx.TE_PROCESS_ENTER) |
---|
1168 | |
---|
1169 | ctl1.SetValue(format_number(value)) |
---|
1170 | if self.data.__class__.__name__ =="Data2D": |
---|
1171 | ctl1.Enable() |
---|
1172 | else: |
---|
1173 | ctl1.Disable() |
---|
1174 | ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1175 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
1176 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
1177 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
1178 | ## text to show error sign |
---|
1179 | ix += 1 |
---|
1180 | text2=wx.StaticText(self, -1, '+/-') |
---|
1181 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
1182 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1183 | text2.Hide() |
---|
1184 | ## txtcrtl to add error from fit |
---|
1185 | ix += 1 |
---|
1186 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
1187 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1188 | ctl2.Hide() |
---|
1189 | if self.data.__class__.__name__ =="Data2D": |
---|
1190 | ctl1.Enable() |
---|
1191 | else: |
---|
1192 | ctl1.Disable() |
---|
1193 | param_min, param_max= self.model.details[item][1:] |
---|
1194 | ix += 1 |
---|
1195 | ctl3 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
1196 | if param_min ==None: |
---|
1197 | ctl3.SetValue("") |
---|
1198 | else: |
---|
1199 | ctl3.SetValue(str(param_min)) |
---|
1200 | ctl3.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1201 | ctl3.Bind(wx.EVT_KILL_FOCUS, self._onparamRangeEnter) |
---|
1202 | ctl3.Bind(wx.EVT_TEXT_ENTER,self._onparamRangeEnter) |
---|
1203 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1204 | ctl3.Hide() |
---|
1205 | if self.data.__class__.__name__ =="Data2D": |
---|
1206 | ctl3.Enable() |
---|
1207 | else: |
---|
1208 | ctl3.Disable() |
---|
1209 | ix += 1 |
---|
1210 | ctl4 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
1211 | ctl4.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1212 | ctl4.Bind(wx.EVT_KILL_FOCUS, self._onparamRangeEnter) |
---|
1213 | ctl4.Bind(wx.EVT_TEXT_ENTER,self._onparamRangeEnter) |
---|
1214 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1215 | if param_max ==None: |
---|
1216 | ctl4.SetValue("") |
---|
1217 | else: |
---|
1218 | ctl4.SetValue(str(param_max)) |
---|
1219 | ctl4.Hide() |
---|
1220 | if self.data.__class__.__name__ =="Data2D": |
---|
1221 | ctl4.Enable() |
---|
1222 | else: |
---|
1223 | ctl4.Disable() |
---|
1224 | if self.engine_type=="park": |
---|
1225 | ctl3.Show(True) |
---|
1226 | ctl4.Show(True) |
---|
1227 | ix +=1 |
---|
1228 | # Units |
---|
1229 | try: |
---|
1230 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
1231 | except: |
---|
1232 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
1233 | if self.data.__class__.__name__ =="Data2D": |
---|
1234 | units.Enable() |
---|
1235 | else: |
---|
1236 | units.Disable() |
---|
1237 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1238 | |
---|
1239 | |
---|
1240 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1241 | self.parameters.append([cb,item, ctl1, |
---|
1242 | text2,ctl2, ctl3, ctl4,units]) |
---|
1243 | self.orientation_params.append([cb,item, ctl1, |
---|
1244 | text2,ctl2, ctl3, ctl4,units]) |
---|
1245 | |
---|
1246 | iy+=1 |
---|
1247 | sizer.Add((10,10),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1248 | |
---|
1249 | #Display units text on panel |
---|
1250 | for item in keys: |
---|
1251 | if self.model.details[item][0]!='': |
---|
1252 | self.text2_4.Show() |
---|
1253 | break |
---|
1254 | else: |
---|
1255 | self.text2_4.Hide() |
---|
1256 | |
---|
1257 | self._copy_parameters_state(self.orientation_params, |
---|
1258 | self.state.orientation_params) |
---|
1259 | self._copy_parameters_state(self.orientation_params_disp, |
---|
1260 | self.state.orientation_params_disp) |
---|
1261 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
1262 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
1263 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
1264 | |
---|
1265 | boxsizer1.Add(sizer) |
---|
1266 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1267 | self.sizer3.Layout() |
---|
1268 | self.Layout() |
---|
1269 | self.Refresh() |
---|
1270 | self.SetScrollbars(20,20,200,100) |
---|
1271 | |
---|
1272 | |
---|
1273 | |
---|
1274 | class HelpWindow(wx.Frame): |
---|
1275 | def __init__(self, parent, id, title): |
---|
1276 | wx.Frame.__init__(self, parent, id, title, size=(570, 400)) |
---|
1277 | |
---|
1278 | from sans.models.CylinderModel import CylinderModel |
---|
1279 | model = CylinderModel() |
---|
1280 | |
---|
1281 | from danse.common.plottools.plottables import Data1D |
---|
1282 | data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1]) |
---|
1283 | |
---|
1284 | from fitpanel import PageInfo |
---|
1285 | myinfo = PageInfo(self, model, data=data ) |
---|
1286 | |
---|
1287 | ## add data |
---|
1288 | |
---|
1289 | from models import ModelList |
---|
1290 | mylist= ModelList() |
---|
1291 | |
---|
1292 | from sans.models.SphereModel import SphereModel |
---|
1293 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
1294 | from sans.models.DebyeModel import DebyeModel |
---|
1295 | from sans.models.LineModel import LineModel |
---|
1296 | name= "shapes" |
---|
1297 | list1= [SphereModel] |
---|
1298 | mylist.set_list( name, list1) |
---|
1299 | |
---|
1300 | name= "Shape-independent" |
---|
1301 | list1= [DebyeModel] |
---|
1302 | mylist.set_list( name, list1) |
---|
1303 | |
---|
1304 | name= "Structure Factors" |
---|
1305 | list1= [SquareWellStructure] |
---|
1306 | mylist.set_list( name, list1) |
---|
1307 | |
---|
1308 | name= "Added models" |
---|
1309 | list1= [LineModel] |
---|
1310 | mylist.set_list( name, list1) |
---|
1311 | |
---|
1312 | myinfo.model_list_box = mylist.get_list() |
---|
1313 | |
---|
1314 | self.page = FitPage(self, myinfo) |
---|
1315 | |
---|
1316 | |
---|
1317 | |
---|
1318 | self.Centre() |
---|
1319 | self.Show(True) |
---|
1320 | |
---|
1321 | |
---|
1322 | |
---|
1323 | if __name__=="__main__": |
---|
1324 | app = wx.App() |
---|
1325 | HelpWindow(None, -1, 'HelpWindow') |
---|
1326 | app.MainLoop() |
---|
1327 | |
---|