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 | 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.SetScrollbars(20,20,200,100) |
---|
463 | |
---|
464 | |
---|
465 | def _onFit(self, event): |
---|
466 | """ |
---|
467 | Allow to fit |
---|
468 | """ |
---|
469 | from sans.guiframe.utils import check_value |
---|
470 | flag = check_value( self.qmin, self.qmax) |
---|
471 | |
---|
472 | if not flag: |
---|
473 | msg= "Fitting range invalid" |
---|
474 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
475 | return |
---|
476 | |
---|
477 | if len(self.param_toFit) <= 0: |
---|
478 | msg= "Select at least one parameter to fit" |
---|
479 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
480 | return |
---|
481 | |
---|
482 | # Remove or do not allow fitting on the Q=0 point, especially when y(q=0)=None at x[0]. |
---|
483 | #ToDo: Fix this. |
---|
484 | self.qmin_x = float(self.qmin.GetValue()) |
---|
485 | self.qmax_x =float( self.qmax.GetValue()) |
---|
486 | self.manager._reset_schedule_problem( value=0) |
---|
487 | self.manager.schedule_for_fit( value=1,page=self,fitproblem =None) |
---|
488 | self.manager.set_fit_range(page= self,qmin= self.qmin_x, qmax= self.qmax_x) |
---|
489 | |
---|
490 | #single fit |
---|
491 | self.manager.onFit() |
---|
492 | ## allow stopping the fit |
---|
493 | #if self.engine_type=="scipy": |
---|
494 | # self.btFit.SetLabel("Stop") |
---|
495 | # self.btFit.Unbind(event=wx.EVT_BUTTON, id= self.btFit.GetId()) |
---|
496 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._StopFit, id=self.btFit.GetId()) |
---|
497 | #else: |
---|
498 | # self.btFit.SetLabel("Fit") |
---|
499 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._onFit, id=self.btFit.GetId()) |
---|
500 | self.btFit.SetFocus() |
---|
501 | self.sizer5.Layout() |
---|
502 | self.SetScrollbars(20,20,55,40) |
---|
503 | |
---|
504 | def _StopFit(self, event): |
---|
505 | """ |
---|
506 | Stop fit |
---|
507 | """ |
---|
508 | self.btFit.SetLabel("Fit") |
---|
509 | if self.engine_type=="scipy": |
---|
510 | self.manager.stop_fit() |
---|
511 | self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
512 | self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
513 | |
---|
514 | |
---|
515 | self.btFit.SetFocus() |
---|
516 | self.sizer5.Layout() |
---|
517 | self.SetScrollbars(20,20,55,40) |
---|
518 | |
---|
519 | def _on_select_model(self, event): |
---|
520 | """ |
---|
521 | call back for model selection |
---|
522 | """ |
---|
523 | self._on_select_model_helper() |
---|
524 | self.set_model_param_sizer(self.model) |
---|
525 | |
---|
526 | self.enable_disp.SetValue(False) |
---|
527 | self.disable_disp.SetValue(True) |
---|
528 | self.set_dispers_sizer() |
---|
529 | if self.model !=None: |
---|
530 | try: |
---|
531 | temp_smear= None |
---|
532 | if self.enable_smearer.GetValue(): |
---|
533 | temp_smear= self.smearer |
---|
534 | self.compute_chisqr(temp_smear) |
---|
535 | except: |
---|
536 | ## error occured on chisqr computation |
---|
537 | pass |
---|
538 | evt = ModelEventbox(model=self.model) |
---|
539 | wx.PostEvent(self.event_owner, evt) |
---|
540 | self.btFit.SetFocus() |
---|
541 | |
---|
542 | def _onparamRangeEnter(self, event): |
---|
543 | """ |
---|
544 | Check validity of value enter in the parameters range field |
---|
545 | """ |
---|
546 | tcrtl= event.GetEventObject() |
---|
547 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
548 | try: |
---|
549 | value = float(tcrtl.GetValue()) |
---|
550 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
551 | tcrtl.Refresh() |
---|
552 | except: |
---|
553 | tcrtl.SetBackgroundColour("pink") |
---|
554 | tcrtl.Refresh() |
---|
555 | else: |
---|
556 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
557 | tcrtl.Refresh() |
---|
558 | self._onparamEnter_helper() |
---|
559 | |
---|
560 | def _onparamEnter(self,event): |
---|
561 | """ |
---|
562 | when enter value on panel redraw model according to changed |
---|
563 | """ |
---|
564 | tcrtl= event.GetEventObject() |
---|
565 | if check_float(tcrtl): |
---|
566 | self._onparamEnter_helper() |
---|
567 | temp_smearer = None |
---|
568 | if self.enable_smearer.GetValue(): |
---|
569 | temp_smearer= self.smearer |
---|
570 | self.compute_chisqr(smearer= temp_smearer) |
---|
571 | else: |
---|
572 | msg= "Cannot Plot :Must enter a number!!! " |
---|
573 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
574 | return |
---|
575 | |
---|
576 | |
---|
577 | def set_data(self, data ): |
---|
578 | """ |
---|
579 | reset the current data |
---|
580 | """ |
---|
581 | if data ==None: |
---|
582 | return |
---|
583 | self.data =data |
---|
584 | self.state.data= data |
---|
585 | self._fill_datainfo_sizer() |
---|
586 | self.SetScrollbars(20,20,200,100) |
---|
587 | self.Layout() |
---|
588 | |
---|
589 | def reset_page(self, state): |
---|
590 | """ |
---|
591 | reset the state |
---|
592 | """ |
---|
593 | self.reset_page_helper(state) |
---|
594 | evt = ModelEventbox(model=self.model) |
---|
595 | wx.PostEvent(self.event_owner, evt) |
---|
596 | |
---|
597 | |
---|
598 | def get_range(self): |
---|
599 | """ |
---|
600 | return the fitting range |
---|
601 | """ |
---|
602 | return float(self.qmin_x) , float(self.qmax_x) |
---|
603 | |
---|
604 | def get_param_list(self): |
---|
605 | """ |
---|
606 | @return self.param_toFit: list containing references to TextCtrl |
---|
607 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
608 | @raise: if return an empty list of parameter fit will nnote work |
---|
609 | properly so raise ValueError,"missing parameter to fit" |
---|
610 | """ |
---|
611 | if self.param_toFit !=[]: |
---|
612 | return self.param_toFit |
---|
613 | else: |
---|
614 | raise ValueError,"missing parameter to fit" |
---|
615 | |
---|
616 | def onsetValues(self,chisqr, out,cov): |
---|
617 | """ |
---|
618 | Build the panel from the fit result |
---|
619 | @param chisqr:Value of the goodness of fit metric |
---|
620 | @param out:list of parameter with the best value found during fitting |
---|
621 | @param cov:Covariance matrix |
---|
622 | |
---|
623 | """ |
---|
624 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
625 | params = {} |
---|
626 | is_modified = False |
---|
627 | has_error = False |
---|
628 | self.text2_3.Hide() |
---|
629 | try: |
---|
630 | n = self.disp_box.GetCurrentSelection() |
---|
631 | dispersity= self.disp_box.GetClientData(n) |
---|
632 | name= dispersity.__name__ |
---|
633 | if name == "GaussianDispersion": |
---|
634 | if hasattr(self,"text_disp_1" ): |
---|
635 | if self.text_disp_1 !=None: |
---|
636 | self.text_disp_1.Hide() |
---|
637 | except: |
---|
638 | pass |
---|
639 | #set the panel when fit result are float not list |
---|
640 | if out.__class__==numpy.float64: |
---|
641 | self.param_toFit[0][2].SetValue(format_number(out)) |
---|
642 | self.param_toFit[0][2].Refresh() |
---|
643 | |
---|
644 | self.param_toFit[0][4].Clear() |
---|
645 | self.param_toFit[0][4].Hide() |
---|
646 | if cov !=None : |
---|
647 | self.text2_3.Show(True) |
---|
648 | try: |
---|
649 | name= dispersity.__name__ |
---|
650 | if name == "GaussianDispersion": |
---|
651 | if hasattr(self,"text_disp_1" ): |
---|
652 | if self.text_disp_1 !=None: |
---|
653 | self.text_disp_1.Show(True) |
---|
654 | except: |
---|
655 | pass |
---|
656 | if cov[0]==None or not numpy.isfinite(cov[0]): |
---|
657 | self.param_toFit[0][3].Hide() |
---|
658 | self.param_toFit[0][4].Clear() |
---|
659 | self.param_toFit[0][4].Hide() |
---|
660 | self.param_toFit[0][4].Refresh() |
---|
661 | else: |
---|
662 | self.param_toFit[0][3].Show(True) |
---|
663 | self.param_toFit[0][4].Clear() |
---|
664 | self.param_toFit[0][4].SetValue(format_number(cov[0])) |
---|
665 | self.param_toFit[0][4].Show(True) |
---|
666 | self.param_toFit[0][4].Refresh() |
---|
667 | else: |
---|
668 | i=0 |
---|
669 | j=0 |
---|
670 | #Set the panel when fit result are list |
---|
671 | for item in self.param_toFit: |
---|
672 | ## reset error value to initial state |
---|
673 | item[4].Clear() |
---|
674 | item[4].Hide() |
---|
675 | item[4].Refresh() |
---|
676 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
677 | item[2].SetValue(format_number(self.model.getParam(item[1]))) |
---|
678 | item[2].Refresh() |
---|
679 | if(cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
680 | self.text2_3.Show(True) |
---|
681 | try: |
---|
682 | name= dispersity.__name__ |
---|
683 | if name == "GaussianDispersion": |
---|
684 | if hasattr(self,"text_disp_1" ): |
---|
685 | if self.text_disp_1!=None: |
---|
686 | self.text_disp_1.Show(True) |
---|
687 | except: |
---|
688 | pass |
---|
689 | item[3].Show(True) |
---|
690 | item[4].Clear() |
---|
691 | for j in range(len(out)): |
---|
692 | if out[j]==self.model.getParam(item[1]): |
---|
693 | break |
---|
694 | ## unable to compare cov[j]==numpy.nan so switch to None |
---|
695 | if cov[j]==None or not numpy.isfinite(cov[j]): |
---|
696 | item[3].Hide() |
---|
697 | item[4].Refresh() |
---|
698 | item[4].Clear() |
---|
699 | item[4].Hide() |
---|
700 | else: |
---|
701 | item[4].SetValue(format_number(cov[j])) |
---|
702 | item[4].Refresh() |
---|
703 | item[4].Show(True) |
---|
704 | i+=1 |
---|
705 | |
---|
706 | self.sizer3.Layout() |
---|
707 | self.sizer4.Layout() |
---|
708 | self.Layout() |
---|
709 | self.SetScrollbars(20,20,200,100) |
---|
710 | |
---|
711 | |
---|
712 | def onSmear(self, event): |
---|
713 | """ |
---|
714 | Create a smear object that will change the way residuals |
---|
715 | are compute when fitting |
---|
716 | """ |
---|
717 | temp_smearer = None |
---|
718 | if self.enable_smearer.GetValue(): |
---|
719 | msg="" |
---|
720 | temp_smearer= self.smearer |
---|
721 | if hasattr(self.data,"dxl"): |
---|
722 | msg= ": Resolution smearing parameters" |
---|
723 | if hasattr(self.data,"dxw"): |
---|
724 | msg= ": Slit smearing parameters" |
---|
725 | if self.smearer ==None: |
---|
726 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
727 | "Data contains no smearing information")) |
---|
728 | else: |
---|
729 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
730 | "Data contains smearing information %s"%msg)) |
---|
731 | |
---|
732 | ## set smearing value whether or not the data contain the smearing info |
---|
733 | self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
734 | qmax= float(self.qmax_x)) |
---|
735 | ##Calculate chi2 |
---|
736 | self.compute_chisqr(smearer= temp_smearer) |
---|
737 | ## save the state enable smearing |
---|
738 | self.save_current_state() |
---|
739 | |
---|
740 | def complete_chisqr(self, output, elapsed=None): |
---|
741 | """ |
---|
742 | print result chisqr |
---|
743 | """ |
---|
744 | try: |
---|
745 | self.tcChi.SetLabel(format_number(output)) |
---|
746 | except: |
---|
747 | raise |
---|
748 | |
---|
749 | |
---|
750 | def compute_chisqr1D(self, smearer=None): |
---|
751 | """ |
---|
752 | Compute chisqr for 1D |
---|
753 | """ |
---|
754 | from sans.guiframe.utils import check_value |
---|
755 | flag = check_value( self.qmin, self.qmax) |
---|
756 | |
---|
757 | if not flag: |
---|
758 | return |
---|
759 | |
---|
760 | try: |
---|
761 | self.qmin_x = float(self.qmin.GetValue()) |
---|
762 | self.qmax_x = float(self.qmax.GetValue()) |
---|
763 | ##return residuals within self.qmin_x and self.qmax_x |
---|
764 | from gui_thread import CalcChisqr1D |
---|
765 | ## If a thread is already started, stop it |
---|
766 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
767 | self.calc_Chisqr.stop() |
---|
768 | |
---|
769 | self.calc_Chisqr= CalcChisqr1D( x= self.data.x, |
---|
770 | y= self.data.y, |
---|
771 | dy= self.data.dy, |
---|
772 | model= self.model, |
---|
773 | smearer=smearer, |
---|
774 | qmin=self.qmin_x, |
---|
775 | qmax=self.qmax_x, |
---|
776 | completefn = self.complete_chisqr, |
---|
777 | updatefn = None) |
---|
778 | |
---|
779 | self.calc_Chisqr.queue() |
---|
780 | |
---|
781 | except: |
---|
782 | raise ValueError," Could not compute Chisqr for %s Model 2D: "%self.model.name |
---|
783 | |
---|
784 | |
---|
785 | |
---|
786 | |
---|
787 | |
---|
788 | def compute_chisqr2D(self): |
---|
789 | """ |
---|
790 | compute chi square given a model and data 2D and set the value |
---|
791 | to the tcChi txtcrl |
---|
792 | """ |
---|
793 | from sans.guiframe.utils import check_value |
---|
794 | flag = check_value( self.qmin, self.qmax) |
---|
795 | if not flag: |
---|
796 | return |
---|
797 | |
---|
798 | try: |
---|
799 | self.qmin_x = float(self.qmin.GetValue()) |
---|
800 | self.qmax_x = float(self.qmax.GetValue()) |
---|
801 | |
---|
802 | ##return residuals within self.qmin_x and self.qmax_x |
---|
803 | from gui_thread import CalcChisqr2D |
---|
804 | ## If a thread is already started, stop it |
---|
805 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
806 | self.calc_Chisqr.stop() |
---|
807 | |
---|
808 | self.calc_Chisqr= CalcChisqr2D( x_bins= self.data.x_bins, |
---|
809 | y_bins= self.data.y_bins, |
---|
810 | data= self.data.data, |
---|
811 | err_data = self.data.err_data, |
---|
812 | model= self.model, |
---|
813 | qmin= self.qmin_x, |
---|
814 | qmax = self.qmax_x, |
---|
815 | completefn = self.complete_chisqr, |
---|
816 | updatefn = None) |
---|
817 | |
---|
818 | self.calc_Chisqr.queue() |
---|
819 | |
---|
820 | except: |
---|
821 | raise ValueError," Could not compute Chisqr for %s Model 2D: "%self.model.name |
---|
822 | |
---|
823 | |
---|
824 | |
---|
825 | def compute_chisqr(self , smearer=None): |
---|
826 | """ |
---|
827 | compute chi square given a model and data 1D and set the value |
---|
828 | to the tcChi txtcrl |
---|
829 | """ |
---|
830 | from sans.guiframe.utils import check_value |
---|
831 | flag = check_value( self.qmin, self.qmax) |
---|
832 | if flag== True: |
---|
833 | try: |
---|
834 | if hasattr(self.data,"data"): |
---|
835 | self.compute_chisqr2D() |
---|
836 | return |
---|
837 | else: |
---|
838 | self.compute_chisqr1D(smearer=smearer) |
---|
839 | return |
---|
840 | except: |
---|
841 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
842 | "Chisqr Error: %s"% sys.exc_value)) |
---|
843 | return |
---|
844 | |
---|
845 | |
---|
846 | def select_all_param(self,event): |
---|
847 | """ |
---|
848 | set to true or false all checkBox given the main checkbox value cb1 |
---|
849 | """ |
---|
850 | |
---|
851 | self.param_toFit=[] |
---|
852 | if self.parameters !=[]: |
---|
853 | if self.cb1.GetValue(): |
---|
854 | for item in self.parameters: |
---|
855 | ## for data2D select all to fit |
---|
856 | if self.data.__class__.__name__=="Data2D": |
---|
857 | item[0].SetValue(True) |
---|
858 | self.param_toFit.append(item ) |
---|
859 | else: |
---|
860 | ## for 1D all parameters except orientation |
---|
861 | if not item in self.orientation_params: |
---|
862 | item[0].SetValue(True) |
---|
863 | self.param_toFit.append(item ) |
---|
864 | if len(self.fittable_param)>0: |
---|
865 | for item in self.fittable_param: |
---|
866 | if self.data.__class__.__name__=="Data2D": |
---|
867 | item[0].SetValue(True) |
---|
868 | self.param_toFit.append(item ) |
---|
869 | else: |
---|
870 | ## for 1D all parameters except orientation |
---|
871 | if not item in self.orientation_params_disp: |
---|
872 | item[0].SetValue(True) |
---|
873 | self.param_toFit.append(item ) |
---|
874 | else: |
---|
875 | for item in self.parameters: |
---|
876 | item[0].SetValue(False) |
---|
877 | for item in self.fittable_param: |
---|
878 | item[0].SetValue(False) |
---|
879 | self.param_toFit=[] |
---|
880 | |
---|
881 | self.save_current_state() |
---|
882 | |
---|
883 | |
---|
884 | |
---|
885 | def select_param(self,event): |
---|
886 | """ |
---|
887 | Select TextCtrl checked for fitting purpose and stores them |
---|
888 | in self.param_toFit=[] list |
---|
889 | """ |
---|
890 | self.param_toFit=[] |
---|
891 | for item in self.parameters: |
---|
892 | #Select parameters to fit for list of primary parameters |
---|
893 | if item[0].GetValue(): |
---|
894 | if not (item in self.param_toFit): |
---|
895 | self.param_toFit.append(item ) |
---|
896 | else: |
---|
897 | #remove parameters from the fitting list |
---|
898 | if item in self.param_toFit: |
---|
899 | self.param_toFit.remove(item) |
---|
900 | #Select parameters to fit for list of fittable parameters with dispersion |
---|
901 | for item in self.fittable_param: |
---|
902 | if item[0].GetValue(): |
---|
903 | if not (item in self.param_toFit): |
---|
904 | self.param_toFit.append(item) |
---|
905 | else: |
---|
906 | #remove parameters from the fitting list |
---|
907 | if item in self.param_toFit: |
---|
908 | self.param_toFit.remove(item) |
---|
909 | #Set the value of checkbox that selected every checkbox or not |
---|
910 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
911 | self.cb1.SetValue(True) |
---|
912 | else: |
---|
913 | self.cb1.SetValue(False) |
---|
914 | ## save current state of the page |
---|
915 | self.save_current_state() |
---|
916 | |
---|
917 | |
---|
918 | |
---|
919 | def set_model_param_sizer(self, model): |
---|
920 | """ |
---|
921 | Build the panel from the model content |
---|
922 | @param model: the model selected in combo box for fitting purpose |
---|
923 | """ |
---|
924 | self.sizer3.Clear(True) |
---|
925 | self.parameters = [] |
---|
926 | self.param_toFit=[] |
---|
927 | self.fittable_param=[] |
---|
928 | self.fixed_param=[] |
---|
929 | self.orientation_params=[] |
---|
930 | self.orientation_params_disp=[] |
---|
931 | |
---|
932 | if model ==None: |
---|
933 | self.sizer3.Layout() |
---|
934 | self.SetScrollbars(20,20,200,100) |
---|
935 | return |
---|
936 | ## the panel is drawn using the current value of the fit engine |
---|
937 | if self.engine_type==None and self.manager !=None: |
---|
938 | self.engine_type= self.manager._return_engine_type() |
---|
939 | |
---|
940 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
941 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
942 | sizer = wx.GridBagSizer(5,5) |
---|
943 | ## save the current model |
---|
944 | self.model = model |
---|
945 | |
---|
946 | keys = self.model.getParamList() |
---|
947 | #list of dispersion paramaters |
---|
948 | self.disp_list=self.model.getDispParamList() |
---|
949 | |
---|
950 | keys.sort() |
---|
951 | |
---|
952 | iy = 1 |
---|
953 | ix = 0 |
---|
954 | self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) |
---|
955 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
956 | self.cb1.SetValue(False) |
---|
957 | |
---|
958 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
959 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
960 | ix +=1 |
---|
961 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
962 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
963 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
964 | ix +=2 |
---|
965 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
966 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
967 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
968 | self.text2_3.Hide() |
---|
969 | ix +=1 |
---|
970 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
971 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
972 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
973 | self.text2_min.Hide() |
---|
974 | ix +=1 |
---|
975 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
976 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
977 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
978 | self.text2_max.Hide() |
---|
979 | ix += 1 |
---|
980 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
981 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
982 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
983 | self.text2_4.Hide() |
---|
984 | if self.engine_type=="park": |
---|
985 | self.text2_max.Show(True) |
---|
986 | self.text2_min.Show(True) |
---|
987 | |
---|
988 | for item in keys: |
---|
989 | if not item in self.disp_list and not item in self.model.orientation_params: |
---|
990 | iy += 1 |
---|
991 | ix = 0 |
---|
992 | ## add parameters name with checkbox for selecting to fit |
---|
993 | cb = wx.CheckBox(self, -1, item ) |
---|
994 | cb.SetValue(False) |
---|
995 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
996 | sizer.Add( cb,( iy, ix),(1,1), |
---|
997 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
998 | |
---|
999 | ## add parameter value |
---|
1000 | ix += 1 |
---|
1001 | value= self.model.getParam(item) |
---|
1002 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
1003 | style=wx.TE_PROCESS_ENTER) |
---|
1004 | |
---|
1005 | ctl1.SetValue(format_number(value)) |
---|
1006 | ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1007 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
1008 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
1009 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
1010 | ## text to show error sign |
---|
1011 | ix += 1 |
---|
1012 | text2=wx.StaticText(self, -1, '+/-') |
---|
1013 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
1014 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1015 | text2.Hide() |
---|
1016 | ## txtcrtl to add error from fit |
---|
1017 | ix += 1 |
---|
1018 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
1019 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1020 | ctl2.Hide() |
---|
1021 | |
---|
1022 | param_min, param_max= self.model.details[item][1:] |
---|
1023 | ix += 1 |
---|
1024 | ctl3 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
1025 | if param_min ==None: |
---|
1026 | ctl3.SetValue("") |
---|
1027 | else: |
---|
1028 | ctl3.SetValue(str(param_min)) |
---|
1029 | ctl3.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1030 | ctl3.Bind(wx.EVT_KILL_FOCUS, self._onparamRangeEnter) |
---|
1031 | ctl3.Bind(wx.EVT_TEXT_ENTER,self._onparamRangeEnter) |
---|
1032 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1033 | ctl3.Hide() |
---|
1034 | |
---|
1035 | ix += 1 |
---|
1036 | ctl4 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
1037 | ctl4.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1038 | ctl4.Bind(wx.EVT_KILL_FOCUS, self._onparamRangeEnter) |
---|
1039 | ctl4.Bind(wx.EVT_TEXT_ENTER,self._onparamRangeEnter) |
---|
1040 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1041 | if param_max==None: |
---|
1042 | ctl4.SetValue("") |
---|
1043 | else: |
---|
1044 | ctl4.SetValue(str(param_max)) |
---|
1045 | ctl4.Hide() |
---|
1046 | |
---|
1047 | if self.engine_type=="park": |
---|
1048 | ctl3.Show(True) |
---|
1049 | ctl4.Show(True) |
---|
1050 | ix +=1 |
---|
1051 | # Units |
---|
1052 | try: |
---|
1053 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
1054 | except: |
---|
1055 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
1056 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1057 | |
---|
1058 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1059 | self.parameters.append([cb,item, ctl1, |
---|
1060 | text2,ctl2, ctl3, ctl4,None]) |
---|
1061 | |
---|
1062 | iy+=1 |
---|
1063 | sizer.Add((10,10),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1064 | for item in self.model.orientation_params: |
---|
1065 | if not item in self.disp_list : |
---|
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 | if self.data.__class__.__name__ =="Data2D": |
---|
1073 | cb.Enable() |
---|
1074 | else: |
---|
1075 | cb.Disable() |
---|
1076 | sizer.Add( cb,( iy, ix),(1,1), |
---|
1077 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
1078 | |
---|
1079 | ## add parameter value |
---|
1080 | ix += 1 |
---|
1081 | value= self.model.getParam(item) |
---|
1082 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
1083 | style=wx.TE_PROCESS_ENTER) |
---|
1084 | |
---|
1085 | ctl1.SetValue(format_number(value)) |
---|
1086 | if self.data.__class__.__name__ =="Data2D": |
---|
1087 | ctl1.Enable() |
---|
1088 | else: |
---|
1089 | ctl1.Disable() |
---|
1090 | ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1091 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
1092 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
1093 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
1094 | ## text to show error sign |
---|
1095 | ix += 1 |
---|
1096 | text2=wx.StaticText(self, -1, '+/-') |
---|
1097 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
1098 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1099 | text2.Hide() |
---|
1100 | ## txtcrtl to add error from fit |
---|
1101 | ix += 1 |
---|
1102 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
1103 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1104 | ctl2.Hide() |
---|
1105 | if self.data.__class__.__name__ =="Data2D": |
---|
1106 | ctl1.Enable() |
---|
1107 | else: |
---|
1108 | ctl1.Disable() |
---|
1109 | param_min, param_max= self.model.details[item][1:] |
---|
1110 | ix += 1 |
---|
1111 | ctl3 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
1112 | if param_min ==None: |
---|
1113 | ctl3.SetValue("") |
---|
1114 | else: |
---|
1115 | ctl3.SetValue(str(param_min)) |
---|
1116 | ctl3.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1117 | ctl3.Bind(wx.EVT_KILL_FOCUS, self._onparamRangeEnter) |
---|
1118 | ctl3.Bind(wx.EVT_TEXT_ENTER,self._onparamRangeEnter) |
---|
1119 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1120 | ctl3.Hide() |
---|
1121 | if self.data.__class__.__name__ =="Data2D": |
---|
1122 | ctl3.Enable() |
---|
1123 | else: |
---|
1124 | ctl3.Disable() |
---|
1125 | ix += 1 |
---|
1126 | ctl4 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
1127 | ctl4.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1128 | ctl4.Bind(wx.EVT_KILL_FOCUS, self._onparamRangeEnter) |
---|
1129 | ctl4.Bind(wx.EVT_TEXT_ENTER,self._onparamRangeEnter) |
---|
1130 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1131 | if param_max ==None: |
---|
1132 | ctl4.SetValue("") |
---|
1133 | else: |
---|
1134 | ctl4.SetValue(str(param_max)) |
---|
1135 | ctl4.Hide() |
---|
1136 | if self.data.__class__.__name__ =="Data2D": |
---|
1137 | ctl4.Enable() |
---|
1138 | else: |
---|
1139 | ctl4.Disable() |
---|
1140 | if self.engine_type=="park": |
---|
1141 | ctl3.Show(True) |
---|
1142 | ctl4.Show(True) |
---|
1143 | ix +=1 |
---|
1144 | # Units |
---|
1145 | try: |
---|
1146 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
1147 | except: |
---|
1148 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
1149 | if self.data.__class__.__name__ =="Data2D": |
---|
1150 | units.Enable() |
---|
1151 | else: |
---|
1152 | units.Disable() |
---|
1153 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1154 | |
---|
1155 | |
---|
1156 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1157 | self.parameters.append([cb,item, ctl1, |
---|
1158 | text2,ctl2, ctl3, ctl4,None]) |
---|
1159 | self.orientation_params.append([cb,item, ctl1, |
---|
1160 | text2,ctl2, ctl3, ctl4,None]) |
---|
1161 | |
---|
1162 | iy+=1 |
---|
1163 | sizer.Add((10,10),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1164 | |
---|
1165 | #Display units text on panel |
---|
1166 | for item in keys: |
---|
1167 | if self.model.details[item][0]!='': |
---|
1168 | self.text2_4.Show() |
---|
1169 | break |
---|
1170 | else: |
---|
1171 | self.text2_4.Hide() |
---|
1172 | |
---|
1173 | boxsizer1.Add(sizer) |
---|
1174 | |
---|
1175 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1176 | self.sizer3.Layout() |
---|
1177 | self.SetScrollbars(20,20,200,100) |
---|
1178 | |
---|
1179 | |
---|
1180 | |
---|
1181 | |
---|
1182 | class HelpWindow(wx.Frame): |
---|
1183 | def __init__(self, parent, id, title): |
---|
1184 | wx.Frame.__init__(self, parent, id, title, size=(570, 400)) |
---|
1185 | |
---|
1186 | from sans.models.CylinderModel import CylinderModel |
---|
1187 | model = CylinderModel() |
---|
1188 | |
---|
1189 | from danse.common.plottools.plottables import Data1D |
---|
1190 | data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1]) |
---|
1191 | |
---|
1192 | from fitpanel import PageInfo |
---|
1193 | myinfo = PageInfo(self, model, data=data ) |
---|
1194 | |
---|
1195 | ## add data |
---|
1196 | |
---|
1197 | from models import ModelList |
---|
1198 | mylist= ModelList() |
---|
1199 | |
---|
1200 | from sans.models.SphereModel import SphereModel |
---|
1201 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
1202 | from sans.models.DebyeModel import DebyeModel |
---|
1203 | from sans.models.LineModel import LineModel |
---|
1204 | name= "shapes" |
---|
1205 | list1= [SphereModel] |
---|
1206 | mylist.set_list( name, list1) |
---|
1207 | |
---|
1208 | name= "Shape-independent" |
---|
1209 | list1= [DebyeModel] |
---|
1210 | mylist.set_list( name, list1) |
---|
1211 | |
---|
1212 | name= "Structure Factors" |
---|
1213 | list1= [SquareWellStructure] |
---|
1214 | mylist.set_list( name, list1) |
---|
1215 | |
---|
1216 | name= "Added models" |
---|
1217 | list1= [LineModel] |
---|
1218 | mylist.set_list( name, list1) |
---|
1219 | |
---|
1220 | myinfo.model_list_box = mylist.get_list() |
---|
1221 | |
---|
1222 | self.page = FitPage(self, myinfo) |
---|
1223 | |
---|
1224 | |
---|
1225 | |
---|
1226 | self.Centre() |
---|
1227 | self.Show(True) |
---|
1228 | |
---|
1229 | |
---|
1230 | |
---|
1231 | if __name__=="__main__": |
---|
1232 | app = wx.App() |
---|
1233 | HelpWindow(None, -1, 'HelpWindow') |
---|
1234 | app.MainLoop() |
---|
1235 | |
---|