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