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