1 | |
---|
2 | |
---|
3 | import sys |
---|
4 | import wx |
---|
5 | import wx.lib.newevent |
---|
6 | import numpy |
---|
7 | import copy |
---|
8 | import math |
---|
9 | import time |
---|
10 | from sans.models.dispersion_models import ArrayDispersion, GaussianDispersion |
---|
11 | |
---|
12 | from sans.guicomm.events import StatusEvent |
---|
13 | from sans.guiframe.utils import format_number,check_float |
---|
14 | |
---|
15 | ## event to post model to fit to fitting plugins |
---|
16 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
17 | |
---|
18 | ## event to know the selected fit engine |
---|
19 | (FitterTypeEvent, EVT_FITTER_TYPE) = wx.lib.newevent.NewEvent() |
---|
20 | (FitStopEvent, EVT_FIT_STOP) = wx.lib.newevent.NewEvent() |
---|
21 | _BOX_WIDTH = 76 |
---|
22 | _DATA_BOX_WIDTH = 300 |
---|
23 | import basepage |
---|
24 | from basepage import BasicPage |
---|
25 | from basepage import PageInfoEvent |
---|
26 | from DataLoader.qsmearing import smear_selection |
---|
27 | |
---|
28 | class FitPage(BasicPage): |
---|
29 | """ |
---|
30 | FitPanel class contains fields allowing to display results when |
---|
31 | fitting a model and one data |
---|
32 | @note: For Fit to be performed the user should check at least one parameter |
---|
33 | on fit Panel window. |
---|
34 | |
---|
35 | """ |
---|
36 | |
---|
37 | def __init__(self,parent, page_info): |
---|
38 | BasicPage.__init__(self, parent, page_info) |
---|
39 | |
---|
40 | """ |
---|
41 | Initialization of the Panel |
---|
42 | """ |
---|
43 | ## fit page does not content npts txtcrtl |
---|
44 | self.npts=None |
---|
45 | ## thread for compute Chisqr |
---|
46 | self.calc_Chisqr=None |
---|
47 | ## default fitengine type |
---|
48 | self.engine_type = None |
---|
49 | |
---|
50 | ## draw sizer |
---|
51 | self._fill_datainfo_sizer() |
---|
52 | |
---|
53 | self._fill_model_sizer( self.sizer1) |
---|
54 | |
---|
55 | self._fill_range_sizer() |
---|
56 | #self._on_select_model(event=None) |
---|
57 | if self.data is None: |
---|
58 | self.formfactorbox.Disable() |
---|
59 | self.structurebox.Disable() |
---|
60 | else: |
---|
61 | self.smearer = smear_selection( self.data ) |
---|
62 | if self.smearer ==None: |
---|
63 | self.enable_smearer.Disable() |
---|
64 | self.disable_smearer.Disable() |
---|
65 | self.disp_cb_dict = {} |
---|
66 | ## to update the panel according to the fit engine type selected |
---|
67 | self.Bind(EVT_FITTER_TYPE,self._on_engine_change) |
---|
68 | self.Bind(EVT_FIT_STOP,self._on_fit_complete) |
---|
69 | |
---|
70 | |
---|
71 | def _on_fit_complete(self, event): |
---|
72 | """ |
---|
73 | When fit is complete ,reset the fit button label. |
---|
74 | """ |
---|
75 | #self.btFit.SetLabel("Fit") |
---|
76 | #self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
77 | #self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
78 | pass |
---|
79 | |
---|
80 | |
---|
81 | def _on_engine_change(self, event): |
---|
82 | """ |
---|
83 | get an event containing the current name of the fit engine type |
---|
84 | @param event: FitterTypeEvent containing the name of the current engine |
---|
85 | """ |
---|
86 | self.engine_type = event.type |
---|
87 | if len(self.parameters)==0: |
---|
88 | self.Layout() |
---|
89 | return |
---|
90 | if event.type =="park": |
---|
91 | self.btFit.SetLabel("Fit") |
---|
92 | |
---|
93 | for item in self.parameters: |
---|
94 | if event.type =="scipy" : |
---|
95 | item[5].SetValue("") |
---|
96 | item[5].Hide() |
---|
97 | item[6].SetValue("") |
---|
98 | item[6].Hide() |
---|
99 | self.text2_min.Hide() |
---|
100 | self.text2_max.Hide() |
---|
101 | |
---|
102 | else: |
---|
103 | item[5].Show(True) |
---|
104 | item[6].Show(True) |
---|
105 | self.text2_min.Show(True) |
---|
106 | self.text2_max.Show(True) |
---|
107 | for item in self.fittable_param: |
---|
108 | if item[5]!=None and item[6]!=None and not item in self.orientation_params_disp: |
---|
109 | if event.type =="scipy" and not item in self.orientation_params: |
---|
110 | item[5].SetValue("") |
---|
111 | item[5].Hide() |
---|
112 | item[6].SetValue("") |
---|
113 | item[6].Hide() |
---|
114 | self.text2_min.Hide() |
---|
115 | self.text2_max.Hide() |
---|
116 | self.text_disp_min.Hide() |
---|
117 | self.text_disp_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 | self.text_disp_min.Show(True) |
---|
124 | self.text_disp_max.Show(True) |
---|
125 | |
---|
126 | for item in self.orientation_params: |
---|
127 | if item[5]!=None and item[6]!=None: |
---|
128 | if event.type =="scipy" or self.data.__class__.__name__ !="Data2D": |
---|
129 | item[5].SetValue("") |
---|
130 | item[5].Hide() |
---|
131 | item[6].SetValue("") |
---|
132 | item[6].Hide() |
---|
133 | else: |
---|
134 | item[5].Show(True) |
---|
135 | item[6].Show(True) |
---|
136 | |
---|
137 | for item in self.orientation_params_disp: |
---|
138 | if item[5]!=None and item[6]!=None: |
---|
139 | if event.type =="scipy" or self.data.__class__.__name__ !="Data2D": |
---|
140 | item[5].SetValue("") |
---|
141 | item[5].Hide() |
---|
142 | item[6].SetValue("") |
---|
143 | item[6].Hide() |
---|
144 | else: |
---|
145 | item[5].Show(True) |
---|
146 | item[6].Show(True) |
---|
147 | self.Layout() |
---|
148 | self.Refresh() |
---|
149 | |
---|
150 | def _fill_range_sizer(self): |
---|
151 | """ |
---|
152 | Fill the sizer containing the plotting range |
---|
153 | add access to npts |
---|
154 | """ |
---|
155 | title = "Fitting" |
---|
156 | box_description_range = wx.StaticBox(self, -1,str(title)) |
---|
157 | boxsizer_range = wx.StaticBoxSizer(box_description_range, wx.VERTICAL) |
---|
158 | |
---|
159 | sizer_fit = wx.GridSizer(1, 1,0, 0) |
---|
160 | |
---|
161 | self.btFit = wx.Button(self,wx.NewId(),'Fit', size=(80,23)) |
---|
162 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId()) |
---|
163 | self.btFit.SetToolTipString("Perform fit.") |
---|
164 | |
---|
165 | |
---|
166 | sizer_fit.Add((5,5),1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
167 | sizer_fit.Add(self.btFit,0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 35) |
---|
168 | sizer_fit.Layout() |
---|
169 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
170 | #Filling the sizer containing instruments smearing info. |
---|
171 | self.disable_smearer = wx.RadioButton(self, -1, 'No', style=wx.RB_GROUP) |
---|
172 | self.enable_smearer = wx.RadioButton(self, -1, 'Yes') |
---|
173 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
174 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
175 | self.disable_smearer.SetValue(True) |
---|
176 | |
---|
177 | sizer_smearer.Add(wx.StaticText(self,-1,'Instrument Smearing? ')) |
---|
178 | sizer_smearer.Add((10, 10)) |
---|
179 | sizer_smearer.Add( self.enable_smearer ) |
---|
180 | sizer_smearer.Add((10,10)) |
---|
181 | sizer_smearer.Add( self.disable_smearer ) |
---|
182 | #Display Chi^2/dof |
---|
183 | sizer_smearer.Add((78,10)) |
---|
184 | box_description= wx.StaticBox(self, -1,'Chi2/dof') |
---|
185 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
186 | boxsizer1.SetMinSize((80,-1)) |
---|
187 | temp_smearer = None |
---|
188 | if self.enable_smearer.GetValue(): |
---|
189 | temp_smearer= self.smearer |
---|
190 | |
---|
191 | self.tcChi = wx.StaticText(self, -1, "-", style=wx.ALIGN_LEFT) |
---|
192 | |
---|
193 | boxsizer1.Add( self.tcChi ) |
---|
194 | sizer_smearer.Add( boxsizer1 ) |
---|
195 | |
---|
196 | #Set sizer for Fitting section |
---|
197 | self._set_range_sizer( title=title,box_sizer=boxsizer_range, object1=sizer_smearer, object= sizer_fit) |
---|
198 | |
---|
199 | def _fill_datainfo_sizer(self): |
---|
200 | """ |
---|
201 | fill sizer 0 with data info |
---|
202 | """ |
---|
203 | ## no loaded data , don't fill the sizer |
---|
204 | if self.data is None: |
---|
205 | data_min = "" |
---|
206 | data_max = "" |
---|
207 | data_name = "" |
---|
208 | else: |
---|
209 | data_name = self.data.name |
---|
210 | #set maximum range for x in linear scale |
---|
211 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
212 | # Minimum value of data |
---|
213 | data_min = min(self.data.x) |
---|
214 | # Maximum value of data |
---|
215 | data_max = max(self.data.x) |
---|
216 | else: |
---|
217 | ## Minimum value of data |
---|
218 | data_min = 0 |
---|
219 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
220 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
221 | ## Maximum value of data |
---|
222 | data_max = math.sqrt(x*x + y*y) |
---|
223 | |
---|
224 | box_description= wx.StaticBox(self, -1, 'Data') |
---|
225 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
226 | #---------------------------------------------------------- |
---|
227 | sizer_data = wx.BoxSizer(wx.HORIZONTAL) |
---|
228 | #Filling the sizer containing data related fields |
---|
229 | #self.dataSource = wx.StaticText(self, -1,str(self.data.name)) |
---|
230 | self.dataSource = wx.TextCtrl(self, -1) |
---|
231 | self.dataSource.SetValue(str(data_name)) |
---|
232 | self.dataSource.SetEditable(False) |
---|
233 | self.dataSource.SetMinSize((_DATA_BOX_WIDTH, -1)) |
---|
234 | sizer_data.Add(wx.StaticText(self, -1, 'Source Name : ')) |
---|
235 | sizer_data.Add(self.dataSource ) |
---|
236 | sizer_data.Add( (0,5) ) |
---|
237 | |
---|
238 | #---------sizer 2 draw-------------------------------- |
---|
239 | text4_3 = wx.StaticText(self, -1, 'Total Q Range (1/A)', |
---|
240 | style=wx.ALIGN_LEFT) |
---|
241 | sizer_range = wx.BoxSizer(wx.HORIZONTAL) |
---|
242 | sizer_range.Add( text4_3 ,0, wx.RIGHT, 10) |
---|
243 | self.minimum_q = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20)) |
---|
244 | self.minimum_q.SetValue(str(data_min)) |
---|
245 | self.minimum_q.SetEditable(False) |
---|
246 | self.maximum_q = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
247 | self.maximum_q.SetValue(str(data_max)) |
---|
248 | self.maximum_q.SetEditable(False) |
---|
249 | sizer_range.Add(wx.StaticText(self, -1, "Min: "),0, wx.LEFT, 10) |
---|
250 | sizer_range.Add(self.minimum_q,0, wx.LEFT, 10) |
---|
251 | sizer_range.Add(wx.StaticText(self, -1, "Max: "),0, wx.LEFT, 10) |
---|
252 | sizer_range.Add(self.maximum_q,0, wx.LEFT, 10) |
---|
253 | |
---|
254 | ## set q range to plot |
---|
255 | self.qmin_x = data_min |
---|
256 | self.qmax_x = data_max |
---|
257 | |
---|
258 | boxsizer1.Add(sizer_data,0, wx.ALL, 10) |
---|
259 | boxsizer1.Add(sizer_range, 0 , wx.LEFT, 10) |
---|
260 | #------------------------------------------------------------ |
---|
261 | self.sizer0.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
262 | self.sizer0.Layout() |
---|
263 | |
---|
264 | def _fill_model_sizer(self, sizer): |
---|
265 | """ |
---|
266 | fill sizer containing model info |
---|
267 | """ |
---|
268 | ##Add model function Details button in fitpanel. |
---|
269 | ##The following 3 lines are for Mac. Let JHC know before modifying... |
---|
270 | title = "Model" |
---|
271 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
272 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
273 | |
---|
274 | id = wx.NewId() |
---|
275 | self.model_help =wx.Button(self,id,'Details', size=(80,23)) |
---|
276 | self.model_help.Bind(wx.EVT_BUTTON, self.on_model_help_clicked,id=id) |
---|
277 | self.model_help.SetToolTipString("Model Function Help") |
---|
278 | |
---|
279 | ## class base method to add view 2d button |
---|
280 | self._set_model_sizer(sizer=sizer, box_sizer=boxsizer1, title="Model",object=self.model_help ) |
---|
281 | |
---|
282 | |
---|
283 | def _set_sizer_dispersion(self, dispersity): |
---|
284 | """ |
---|
285 | draw sizer with gaussian dispersity parameters |
---|
286 | """ |
---|
287 | self.fittable_param=[] |
---|
288 | self.fixed_param=[] |
---|
289 | self.orientation_params_disp=[] |
---|
290 | |
---|
291 | self.sizer4_4.Clear(True) |
---|
292 | if self.model==None: |
---|
293 | ##no model is selected |
---|
294 | return |
---|
295 | if not self.enable_disp.GetValue(): |
---|
296 | ## the user didn't select dispersity display |
---|
297 | return |
---|
298 | |
---|
299 | self._reset_dispersity() |
---|
300 | |
---|
301 | # Create the dispersion objects |
---|
302 | for item in self.model.dispersion.keys(): |
---|
303 | #disp_model = GaussianDispersion() |
---|
304 | disp_model = dispersity() |
---|
305 | self._disp_obj_dict[item] = disp_model |
---|
306 | self.model.set_dispersion(item, disp_model) |
---|
307 | self.state._disp_obj_dict[item]= disp_model |
---|
308 | |
---|
309 | |
---|
310 | ix=0 |
---|
311 | iy=1 |
---|
312 | disp = wx.StaticText(self, -1, 'Names') |
---|
313 | self.sizer4_4.Add(disp,( iy, ix),(1,1), |
---|
314 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
315 | ix += 1 |
---|
316 | values = wx.StaticText(self, -1, 'Sigmas (STD)') |
---|
317 | self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
318 | ix +=2 |
---|
319 | self.text_disp_1 = wx.StaticText(self, -1, '') |
---|
320 | self.sizer4_4.Add( self.text_disp_1,(iy, ix),(1,1),\ |
---|
321 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
322 | self.text_disp_1.Hide() |
---|
323 | |
---|
324 | |
---|
325 | ix +=1 |
---|
326 | self.text_disp_min = wx.StaticText(self, -1, 'Min') |
---|
327 | self.sizer4_4.Add(self.text_disp_min,(iy, ix),(1,1),\ |
---|
328 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
329 | self.text_disp_min.Hide() |
---|
330 | ix +=1 |
---|
331 | self.text_disp_max = wx.StaticText(self, -1, 'Max') |
---|
332 | self.sizer4_4.Add(self.text_disp_max,(iy, ix),(1,1),\ |
---|
333 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
334 | self.text_disp_max.Hide() |
---|
335 | |
---|
336 | |
---|
337 | ix += 1 |
---|
338 | npts = wx.StaticText(self, -1, 'Npts') |
---|
339 | self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
340 | ix += 1 |
---|
341 | nsigmas = wx.StaticText(self, -1, 'Nsigmas') |
---|
342 | self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
343 | |
---|
344 | if self.engine_type=="park": |
---|
345 | self.text_disp_max.Show(True) |
---|
346 | self.text_disp_min.Show(True) |
---|
347 | |
---|
348 | for item in self.model.dispersion.keys(): |
---|
349 | if not item in self.model.orientation_params: |
---|
350 | if not self.disp_cb_dict.has_key(item): |
---|
351 | self.disp_cb_dict[item]= None |
---|
352 | name1=item+".width" |
---|
353 | name2=item+".npts" |
---|
354 | name3=item+".nsigmas" |
---|
355 | if not self.model.details.has_key(name1): |
---|
356 | self.model.details [name1] = ["",None,None] |
---|
357 | |
---|
358 | iy += 1 |
---|
359 | for p in self.model.dispersion[item].keys(): |
---|
360 | |
---|
361 | if p=="width": |
---|
362 | ix = 0 |
---|
363 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
364 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
365 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
366 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
367 | ix = 1 |
---|
368 | value= self.model.getParam(name1) |
---|
369 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
370 | style=wx.TE_PROCESS_ENTER) |
---|
371 | ctl1.SetValue(str (format_number(value))) |
---|
372 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
373 | ## text to show error sign |
---|
374 | ix = 2 |
---|
375 | text2=wx.StaticText(self, -1, '+/-') |
---|
376 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
377 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
378 | text2.Hide() |
---|
379 | |
---|
380 | ix = 3 |
---|
381 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
382 | |
---|
383 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
384 | |
---|
385 | ctl2.Hide() |
---|
386 | |
---|
387 | ix = 4 |
---|
388 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
389 | kill_focus_callback = self._onparamRangeEnter, |
---|
390 | text_enter_callback = self._onparamRangeEnter) |
---|
391 | |
---|
392 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
393 | ctl3.Hide() |
---|
394 | |
---|
395 | ix = 5 |
---|
396 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
397 | kill_focus_callback = self._onparamRangeEnter, |
---|
398 | text_enter_callback = self._onparamRangeEnter) |
---|
399 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
400 | |
---|
401 | ctl4.Hide() |
---|
402 | |
---|
403 | if self.engine_type=="park": |
---|
404 | ctl3.Show(True) |
---|
405 | ctl4.Show(True) |
---|
406 | |
---|
407 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
408 | ctl2, ctl3, ctl4,None]) |
---|
409 | |
---|
410 | elif p=="npts": |
---|
411 | ix = 6 |
---|
412 | value= self.model.getParam(name2) |
---|
413 | Tctl = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
414 | style=wx.TE_PROCESS_ENTER) |
---|
415 | |
---|
416 | Tctl.SetValue(str (format_number(value))) |
---|
417 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
418 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
419 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
420 | None, None,None]) |
---|
421 | elif p=="nsigmas": |
---|
422 | ix = 7 |
---|
423 | value= self.model.getParam(name3) |
---|
424 | Tct2 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
425 | style=wx.TE_PROCESS_ENTER) |
---|
426 | Tct2.SetValue(str (format_number(value))) |
---|
427 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
428 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
429 | ix +=1 |
---|
430 | self.sizer4_4.Add((20,20), (iy,ix),(1,1), |
---|
431 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
432 | |
---|
433 | self.fixed_param.append([None,name3, Tct2 |
---|
434 | ,None,None,None, None,None]) |
---|
435 | |
---|
436 | ix =0 |
---|
437 | iy +=1 |
---|
438 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
439 | for item in self.model.dispersion.keys(): |
---|
440 | if item in self.model.orientation_params: |
---|
441 | if not self.disp_cb_dict.has_key(item): |
---|
442 | self.disp_cb_dict[item]= None |
---|
443 | name1=item+".width" |
---|
444 | name2=item+".npts" |
---|
445 | name3=item+".nsigmas" |
---|
446 | if not self.model.details.has_key(name1): |
---|
447 | self.model.details [name1] = ["",None,None] |
---|
448 | |
---|
449 | |
---|
450 | iy += 1 |
---|
451 | for p in self.model.dispersion[item].keys(): |
---|
452 | |
---|
453 | if p=="width": |
---|
454 | ix = 0 |
---|
455 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
456 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
457 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
458 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
459 | if self.data.__class__.__name__ =="Data2D": |
---|
460 | cb.Show(True) |
---|
461 | elif cb.IsShown(): |
---|
462 | cb.Hide() |
---|
463 | ix = 1 |
---|
464 | value= self.model.getParam(name1) |
---|
465 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
466 | style=wx.TE_PROCESS_ENTER) |
---|
467 | ctl1.SetValue(str (format_number(value))) |
---|
468 | if self.data.__class__.__name__ =="Data2D": |
---|
469 | ctl1.Show(True) |
---|
470 | elif ctl1.IsShown(): |
---|
471 | ctl1.Hide() |
---|
472 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
473 | ## text to show error sign |
---|
474 | ix = 2 |
---|
475 | text2=wx.StaticText(self, -1, '+/-') |
---|
476 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
477 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
478 | text2.Hide() |
---|
479 | |
---|
480 | ix = 3 |
---|
481 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
482 | |
---|
483 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
484 | ctl2.Hide() |
---|
485 | |
---|
486 | ix = 4 |
---|
487 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
488 | kill_focus_callback = self._onparamRangeEnter, |
---|
489 | text_enter_callback = self._onparamRangeEnter) |
---|
490 | |
---|
491 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
492 | |
---|
493 | ctl3.Hide() |
---|
494 | |
---|
495 | ix = 5 |
---|
496 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
497 | kill_focus_callback = self._onparamRangeEnter, |
---|
498 | text_enter_callback = self._onparamRangeEnter) |
---|
499 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
500 | ctl4.Hide() |
---|
501 | #if self.data.__class__.__name__ =="Data2D": |
---|
502 | #ctl4.Enable(True) |
---|
503 | #elif ctl4.Shown(): |
---|
504 | #ctl4.Hide() |
---|
505 | |
---|
506 | if self.engine_type=="park" and self.data.__class__.__name__ =="Data2D": |
---|
507 | ctl3.Show(True) |
---|
508 | ctl4.Show(True) |
---|
509 | |
---|
510 | |
---|
511 | |
---|
512 | |
---|
513 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
514 | ctl2, ctl3, ctl4,None]) |
---|
515 | self.orientation_params_disp.append([cb,name1,ctl1,text2, |
---|
516 | ctl2, ctl3, ctl4,None]) |
---|
517 | elif p=="npts": |
---|
518 | ix = 6 |
---|
519 | value= self.model.getParam(name2) |
---|
520 | Tctl = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
521 | style=wx.TE_PROCESS_ENTER) |
---|
522 | |
---|
523 | Tctl.SetValue(str (format_number(value))) |
---|
524 | if self.data.__class__.__name__ =="Data2D": |
---|
525 | Tctl.Show(True) |
---|
526 | else: |
---|
527 | Tctl.Hide() |
---|
528 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
529 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
530 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
531 | None, None,None]) |
---|
532 | self.orientation_params_disp.append([None,name2, Tctl,None,None, |
---|
533 | None, None,None]) |
---|
534 | elif p=="nsigmas": |
---|
535 | ix = 7 |
---|
536 | value= self.model.getParam(name3) |
---|
537 | Tct2 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
538 | style=wx.TE_PROCESS_ENTER) |
---|
539 | Tct2.SetValue(str (format_number(value))) |
---|
540 | if self.data.__class__.__name__ =="Data2D": |
---|
541 | Tct2.Show(True) |
---|
542 | else: |
---|
543 | Tct2.Hide() |
---|
544 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
545 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
546 | ix +=1 |
---|
547 | |
---|
548 | self.fixed_param.append([None,name3, Tct2 |
---|
549 | ,None,None, None, None,None]) |
---|
550 | |
---|
551 | self.orientation_params_disp.append([None,name3, Tct2 |
---|
552 | ,None,None, None, None,None]) |
---|
553 | """ |
---|
554 | #Display units text on panel |
---|
555 | for item in self.model.dispersion.keys(): |
---|
556 | name = item +".width" |
---|
557 | """ |
---|
558 | self.state.disp_cb_dict = copy.deepcopy(self.disp_cb_dict) |
---|
559 | |
---|
560 | self.state.model = self.model.clone() |
---|
561 | ## save state into |
---|
562 | self.state.cb1 = self.cb1.GetValue() |
---|
563 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
564 | self._copy_parameters_state(self.orientation_params_disp, |
---|
565 | self.state.orientation_params_disp) |
---|
566 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
567 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
568 | |
---|
569 | |
---|
570 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
571 | " Selected Distribution: Gaussian")) |
---|
572 | ix =0 |
---|
573 | iy +=1 |
---|
574 | |
---|
575 | self.Layout() |
---|
576 | |
---|
577 | |
---|
578 | def _onFit(self, event): |
---|
579 | """ |
---|
580 | Allow to fit |
---|
581 | """ |
---|
582 | #make sure all parameter values are updated. |
---|
583 | if self.check_invalid_panel(): |
---|
584 | return |
---|
585 | flag = self._update_paramv_on_fit() |
---|
586 | |
---|
587 | if not flag: |
---|
588 | msg= "Fitting range invalid" |
---|
589 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
590 | return |
---|
591 | |
---|
592 | if len(self.param_toFit) <= 0: |
---|
593 | msg= "Select at least one parameter to fit" |
---|
594 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
595 | return |
---|
596 | |
---|
597 | |
---|
598 | self.select_param(event =None) |
---|
599 | |
---|
600 | #Clear errors if exist from previous fitting |
---|
601 | #self._clear_Err_on_Fit() |
---|
602 | |
---|
603 | # Remove or do not allow fitting on the Q=0 point, especially when y(q=0)=None at x[0]. |
---|
604 | self.qmin_x = float(self.qmin.GetValue()) |
---|
605 | self.qmax_x = float( self.qmax.GetValue()) |
---|
606 | self.manager._reset_schedule_problem( value=0) |
---|
607 | self.manager.schedule_for_fit( value=1,page=self,fitproblem =None) |
---|
608 | self.manager.set_fit_range(page= self,qmin= self.qmin_x, qmax= self.qmax_x) |
---|
609 | |
---|
610 | #single fit |
---|
611 | self.manager.onFit() |
---|
612 | ## allow stopping the fit |
---|
613 | #if self.engine_type=="scipy": |
---|
614 | # self.btFit.SetLabel("Stop") |
---|
615 | # self.btFit.Unbind(event=wx.EVT_BUTTON, id= self.btFit.GetId()) |
---|
616 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._StopFit, id=self.btFit.GetId()) |
---|
617 | #else: |
---|
618 | # self.btFit.SetLabel("Fit") |
---|
619 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._onFit, id=self.btFit.GetId()) |
---|
620 | |
---|
621 | |
---|
622 | |
---|
623 | def _StopFit(self, event): |
---|
624 | """ |
---|
625 | Stop fit |
---|
626 | """ |
---|
627 | self.btFit.SetLabel("Fit") |
---|
628 | if self.engine_type=="scipy": |
---|
629 | self.manager.stop_fit() |
---|
630 | self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
631 | self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
632 | |
---|
633 | |
---|
634 | def _on_select_model(self, event): |
---|
635 | """ |
---|
636 | call back for model selection |
---|
637 | """ |
---|
638 | self._on_select_model_helper() |
---|
639 | self.set_model_param_sizer(self.model) |
---|
640 | |
---|
641 | self.enable_disp.SetValue(False) |
---|
642 | self.disable_disp.SetValue(True) |
---|
643 | try: |
---|
644 | self.set_dispers_sizer() |
---|
645 | except: |
---|
646 | pass |
---|
647 | if self.model != None: |
---|
648 | try: |
---|
649 | temp_smear= None |
---|
650 | if self.enable_smearer.GetValue(): |
---|
651 | temp_smear= self.smearer |
---|
652 | self.compute_chisqr(temp_smear) |
---|
653 | except: |
---|
654 | ## error occured on chisqr computation |
---|
655 | pass |
---|
656 | if self.data is not None and self.data.__class__.__name__ !="Data2D": |
---|
657 | ## set smearing value whether or not the data contain the smearing info |
---|
658 | self.manager.set_smearer(smearer=temp_smear, qmin= float(self.qmin_x), |
---|
659 | qmax= float(self.qmax_x)) |
---|
660 | |
---|
661 | evt = ModelEventbox(model=self.model) |
---|
662 | wx.PostEvent(self.event_owner, evt) |
---|
663 | |
---|
664 | self.btFit.SetFocus() |
---|
665 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
666 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
667 | |
---|
668 | self.state.structurecombobox = self.structurebox.GetCurrentSelection() |
---|
669 | self.state.formfactorcombobox = self.formfactorbox.GetCurrentSelection() |
---|
670 | |
---|
671 | |
---|
672 | if event !=None: |
---|
673 | #self._undo.Enable(True) |
---|
674 | ## post state to fit panel |
---|
675 | event = PageInfoEvent(page = self) |
---|
676 | wx.PostEvent(self.parent, event) |
---|
677 | |
---|
678 | |
---|
679 | def _onparamEnter(self,event): |
---|
680 | """ |
---|
681 | when enter value on panel redraw model according to changed |
---|
682 | """ |
---|
683 | flag = False |
---|
684 | tcrtl= event.GetEventObject() |
---|
685 | |
---|
686 | #Clear msg if previously shown. |
---|
687 | msg= "" |
---|
688 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
689 | |
---|
690 | if check_float(tcrtl): |
---|
691 | flag = self._onparamEnter_helper() |
---|
692 | if flag: |
---|
693 | temp_smearer = None |
---|
694 | if self.enable_smearer.GetValue(): |
---|
695 | temp_smearer= self.smearer |
---|
696 | self.compute_chisqr(smearer= temp_smearer) |
---|
697 | |
---|
698 | ## new state posted |
---|
699 | if self.state_change: |
---|
700 | #self._undo.Enable(True) |
---|
701 | event = PageInfoEvent(page = self) |
---|
702 | wx.PostEvent(self.parent, event) |
---|
703 | self.state_change= False |
---|
704 | self.save_current_state() |
---|
705 | else: |
---|
706 | self.save_current_state() |
---|
707 | msg= "Cannot Plot :Must enter a number!!! " |
---|
708 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
709 | return |
---|
710 | |
---|
711 | |
---|
712 | def _onparamRangeEnter(self, event): |
---|
713 | """ |
---|
714 | Check validity of value enter in the parameters range field |
---|
715 | """ |
---|
716 | if self.check_invalid_panel(): |
---|
717 | return |
---|
718 | tcrtl= event.GetEventObject() |
---|
719 | #Clear msg if previously shown. |
---|
720 | msg= "" |
---|
721 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
722 | # Flag to register when a parameter has changed. |
---|
723 | is_modified = False |
---|
724 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
725 | try: |
---|
726 | value = float(tcrtl.GetValue()) |
---|
727 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
728 | self._check_value_enter(self.fittable_param ,is_modified) |
---|
729 | self._check_value_enter(self.parameters ,is_modified) |
---|
730 | except: |
---|
731 | tcrtl.SetBackgroundColour("pink") |
---|
732 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
733 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
734 | return |
---|
735 | else: |
---|
736 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
737 | |
---|
738 | #self._undo.Enable(True) |
---|
739 | self.save_current_state() |
---|
740 | event = PageInfoEvent(page = self) |
---|
741 | wx.PostEvent(self.parent, event) |
---|
742 | self.state_change= False |
---|
743 | |
---|
744 | |
---|
745 | def _onQrangeEnter(self, event): |
---|
746 | """ |
---|
747 | Check validity of value enter in the Q range field |
---|
748 | """ |
---|
749 | if self.check_invalid_panel(): |
---|
750 | return |
---|
751 | tcrtl= event.GetEventObject() |
---|
752 | #Clear msg if previously shown. |
---|
753 | msg= "" |
---|
754 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
755 | # Flag to register when a parameter has changed. |
---|
756 | is_modified = False |
---|
757 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
758 | try: |
---|
759 | value = float(tcrtl.GetValue()) |
---|
760 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
761 | |
---|
762 | # If qmin and qmax have been modified, update qmin and qmax |
---|
763 | if self._validate_qrange( self.qmin, self.qmax): |
---|
764 | tempmin = float(self.qmin.GetValue()) |
---|
765 | if tempmin != self.qmin_x: |
---|
766 | self.qmin_x = tempmin |
---|
767 | tempmax = float(self.qmax.GetValue()) |
---|
768 | if tempmax != self.qmax_x: |
---|
769 | self.qmax_x = tempmax |
---|
770 | else: |
---|
771 | tcrtl.SetBackgroundColour("pink") |
---|
772 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
773 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
774 | return |
---|
775 | |
---|
776 | except: |
---|
777 | tcrtl.SetBackgroundColour("pink") |
---|
778 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
779 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
780 | return |
---|
781 | #Check if # of points for theory model are valid(>0). |
---|
782 | if self.npts != None: |
---|
783 | if check_float(self.npts): |
---|
784 | temp_npts = float(self.npts.GetValue()) |
---|
785 | if temp_npts != self.num_points: |
---|
786 | self.num_points = temp_npts |
---|
787 | is_modified = True |
---|
788 | else: |
---|
789 | msg= "Cannot Plot :No npts in that Qrange!!! " |
---|
790 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
791 | |
---|
792 | else: |
---|
793 | tcrtl.SetBackgroundColour("pink") |
---|
794 | msg= "Model Error:wrong value entered!!!" |
---|
795 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
796 | |
---|
797 | self.compute_chisqr() |
---|
798 | #self._undo.Enable(True) |
---|
799 | self.save_current_state() |
---|
800 | event = PageInfoEvent(page = self) |
---|
801 | wx.PostEvent(self.parent, event) |
---|
802 | self.state_change= False |
---|
803 | self._draw_model() |
---|
804 | |
---|
805 | def _clear_Err_on_Fit(self): |
---|
806 | """ |
---|
807 | hide the error text control shown |
---|
808 | after fitting |
---|
809 | """ |
---|
810 | |
---|
811 | if hasattr(self,"text2_3"): |
---|
812 | self.text2_3.Hide() |
---|
813 | |
---|
814 | if len(self.parameters)>0: |
---|
815 | for item in self.parameters: |
---|
816 | #Skip t ifhe angle parameters if 1D data |
---|
817 | if self.data.__class__.__name__ !="Data2D": |
---|
818 | if item in self.orientation_params: |
---|
819 | continue |
---|
820 | if item in self.param_toFit: |
---|
821 | continue |
---|
822 | ## hide statictext +/- |
---|
823 | if item[3]!=None and item[3].IsShown(): |
---|
824 | item[3].Hide() |
---|
825 | ## hide textcrtl for error after fit |
---|
826 | if item[4]!=None and item[4].IsShown(): |
---|
827 | item[4].Hide() |
---|
828 | |
---|
829 | if len(self.fittable_param)>0: |
---|
830 | for item in self.fittable_param: |
---|
831 | #Skip t ifhe angle parameters if 1D data |
---|
832 | if self.data.__class__.__name__ !="Data2D": |
---|
833 | if item in self.orientation_params: |
---|
834 | continue |
---|
835 | if item in self.param_toFit: |
---|
836 | continue |
---|
837 | ## hide statictext +/- |
---|
838 | if item[3]!=None and item[3].IsShown(): |
---|
839 | item[3].Hide() |
---|
840 | ## hide textcrtl for error after fit |
---|
841 | if item[4]!=None and item[4].IsShown(): |
---|
842 | item[4].Hide() |
---|
843 | return |
---|
844 | |
---|
845 | |
---|
846 | def set_data(self, data): |
---|
847 | """ |
---|
848 | reset the current data |
---|
849 | """ |
---|
850 | self.data = data |
---|
851 | if self.data is None: |
---|
852 | data_min = "" |
---|
853 | data_max = "" |
---|
854 | data_name = "" |
---|
855 | self.formfactorbox.Disable() |
---|
856 | self.structurebox.Disable() |
---|
857 | else: |
---|
858 | self.formfactorbox.Enable() |
---|
859 | self.structurebox.Enable() |
---|
860 | data_name = self.data.name |
---|
861 | #set maximum range for x in linear scale |
---|
862 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
863 | # Minimum value of data |
---|
864 | data_min = min(self.data.x) |
---|
865 | # Maximum value of data |
---|
866 | data_max = max(self.data.x) |
---|
867 | else: |
---|
868 | ## Minimum value of data |
---|
869 | data_min = 0 |
---|
870 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
871 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
872 | ## Maximum value of data |
---|
873 | data_max = math.sqrt(x*x + y*y) |
---|
874 | |
---|
875 | self.dataSource.SetValue(data_name) |
---|
876 | self.qmin_x = data_min |
---|
877 | self.qmax_x = data_max |
---|
878 | self.minimum_q.SetValue(str(data_min)) |
---|
879 | self.maximum_q.SetValue(str(data_max)) |
---|
880 | self.qmin.SetValue(str(data_min)) |
---|
881 | self.qmax.SetValue(str(data_max)) |
---|
882 | self.qmin.SetBackgroundColour("white") |
---|
883 | self.qmax.SetBackgroundColour("white") |
---|
884 | self.state.data = data |
---|
885 | self.state.qmin = self.qmin_x |
---|
886 | self.state.qmax = self.qmax_x |
---|
887 | |
---|
888 | |
---|
889 | def reset_page(self, state,first=False): |
---|
890 | """ |
---|
891 | reset the state |
---|
892 | """ |
---|
893 | self.reset_page_helper(state) |
---|
894 | import sans.guiframe.gui_manager |
---|
895 | evt = ModelEventbox(model=state.model) |
---|
896 | wx.PostEvent(self.event_owner, evt) |
---|
897 | |
---|
898 | if self.engine_type != None: |
---|
899 | self.manager._on_change_engine(engine=self.engine_type) |
---|
900 | |
---|
901 | self.select_param(event = None) |
---|
902 | #Save state_fit |
---|
903 | self.save_current_state_fit() |
---|
904 | self._lay_out() |
---|
905 | self.Refresh() |
---|
906 | |
---|
907 | def get_range(self): |
---|
908 | """ |
---|
909 | return the fitting range |
---|
910 | """ |
---|
911 | return float(self.qmin_x) , float(self.qmax_x) |
---|
912 | |
---|
913 | def get_npts2fit(self): |
---|
914 | """ |
---|
915 | return numbers of data points within qrange |
---|
916 | Note: This is for Park where chi2 is not normalized by Npts of fit |
---|
917 | """ |
---|
918 | npts2fit = 0 |
---|
919 | qmin,qmax = self.get_range() |
---|
920 | if self.data.__class__.__name__ =="Data2D": |
---|
921 | for qx in self.data.x_bins: |
---|
922 | for qy in self.data.y_bins: |
---|
923 | if math.sqrt((qx*qx)+(qy*qy)) >= qmin \ |
---|
924 | and math.sqrt((qx*qx)+(qy*qy)) <= qmax: |
---|
925 | npts2fit += 1 |
---|
926 | else: |
---|
927 | for qx in self.data.x: |
---|
928 | if qx >= qmin and qx <= qmax: |
---|
929 | npts2fit += 1 |
---|
930 | return npts2fit |
---|
931 | |
---|
932 | |
---|
933 | def get_chi2(self): |
---|
934 | """ |
---|
935 | return the current chi2 |
---|
936 | """ |
---|
937 | return self.tcChi.GetLabel() |
---|
938 | |
---|
939 | def get_param_list(self): |
---|
940 | """ |
---|
941 | @return self.param_toFit: list containing references to TextCtrl |
---|
942 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
943 | @raise: if return an empty list of parameter fit will nnote work |
---|
944 | properly so raise ValueError,"missing parameter to fit" |
---|
945 | """ |
---|
946 | if self.param_toFit !=[]: |
---|
947 | return self.param_toFit |
---|
948 | else: |
---|
949 | raise ValueError,"missing parameter to fit" |
---|
950 | |
---|
951 | def onsetValues(self,chisqr,p_name, out,cov): |
---|
952 | """ |
---|
953 | Build the panel from the fit result |
---|
954 | @param chisqr:Value of the goodness of fit metric |
---|
955 | @p_name: the name of parameters |
---|
956 | @param out:list of parameter with the best value found during fitting |
---|
957 | @param cov:Covariance matrix |
---|
958 | |
---|
959 | """ |
---|
960 | if out == None or not numpy.isfinite(chisqr): |
---|
961 | raise ValueError,"Fit error occured..." |
---|
962 | |
---|
963 | is_modified = False |
---|
964 | has_error = False |
---|
965 | |
---|
966 | #Hide textctrl boxes of errors. |
---|
967 | self._clear_Err_on_Fit() |
---|
968 | |
---|
969 | #Check if chi2 is finite |
---|
970 | if chisqr != None or numpy.isfinite(chisqr): |
---|
971 | #format chi2 |
---|
972 | npt_fit = float(self.get_npts2fit()) |
---|
973 | if self.engine_type == "park" and npt_fit > 0: |
---|
974 | chisqr =chisqr/npt_fit |
---|
975 | chi2 = format_number(chisqr) |
---|
976 | self.tcChi.SetLabel(chi2) |
---|
977 | self.tcChi.Refresh() |
---|
978 | else: |
---|
979 | self.tcChi.SetLabel("-") |
---|
980 | |
---|
981 | #Hide error title |
---|
982 | if self.text2_3.IsShown(): |
---|
983 | self.text2_3.Hide() |
---|
984 | |
---|
985 | try: |
---|
986 | n = self.disp_box.GetCurrentSelection() |
---|
987 | dispersity= self.disp_box.GetClientData(n) |
---|
988 | if dispersity !=None and self.enable_disp.GetValue(): |
---|
989 | name= dispersity.__name__ |
---|
990 | if name == "GaussianDispersion": |
---|
991 | if hasattr(self,"text_disp_1" ): |
---|
992 | if self.text_disp_1 !=None: |
---|
993 | self.text_disp_1.Hide() |
---|
994 | except: |
---|
995 | dispersty = None |
---|
996 | pass |
---|
997 | #set the panel when fit result are float not list |
---|
998 | if out.__class__== numpy.float64: |
---|
999 | self.param_toFit[0][2].SetValue(format_number(out)) |
---|
1000 | |
---|
1001 | if self.param_toFit[0][4].IsShown: |
---|
1002 | self.param_toFit[0][4].Hide() |
---|
1003 | if cov !=None : |
---|
1004 | self.text2_3.Show(True) |
---|
1005 | try: |
---|
1006 | if dispersity !=None: |
---|
1007 | name= dispersity.__name__ |
---|
1008 | if name == "GaussianDispersion" and self.enable_disp.GetValue(): |
---|
1009 | if hasattr(self,"text_disp_1" ): |
---|
1010 | if self.text_disp_1 !=None: |
---|
1011 | self.text_disp_1.Show(True) |
---|
1012 | except: |
---|
1013 | pass |
---|
1014 | |
---|
1015 | if cov[0]==None or not numpy.isfinite(cov[0]): |
---|
1016 | if self.param_toFit[0][3].IsShown: |
---|
1017 | self.param_toFit[0][3].Hide() |
---|
1018 | else: |
---|
1019 | self.param_toFit[0][3].Show(True) |
---|
1020 | self.param_toFit[0][4].Show(True) |
---|
1021 | self.param_toFit[0][4].SetValue(format_number(cov[0])) |
---|
1022 | has_error = True |
---|
1023 | else: |
---|
1024 | |
---|
1025 | i = 0 |
---|
1026 | #Set the panel when fit result are list |
---|
1027 | for item in self.param_toFit: |
---|
1028 | if len(item)>5 and item != None: |
---|
1029 | ## reset error value to initial state |
---|
1030 | item[3].Hide() |
---|
1031 | item[4].Hide() |
---|
1032 | |
---|
1033 | for ind in range(len(out)): |
---|
1034 | |
---|
1035 | if item[1] == p_name[ind]: |
---|
1036 | break |
---|
1037 | if len(out)<=len(self.param_toFit) and out[ind] !=None: |
---|
1038 | val_out = format_number(out[ind]) |
---|
1039 | item[2].SetValue(val_out) |
---|
1040 | |
---|
1041 | if(cov !=None): |
---|
1042 | |
---|
1043 | try: |
---|
1044 | if dispersity !=None: |
---|
1045 | name= dispersity.__name__ |
---|
1046 | if name == "GaussianDispersion" and self.enable_disp.GetValue(): |
---|
1047 | if hasattr(self,"text_disp_1" ): |
---|
1048 | if self.text_disp_1!=None: |
---|
1049 | if not self.text_disp_1.IsShown(): |
---|
1050 | self.text_disp_1.Show(True) |
---|
1051 | except: |
---|
1052 | pass |
---|
1053 | |
---|
1054 | if cov[ind]!=None : |
---|
1055 | if numpy.isfinite(float(cov[ind])): |
---|
1056 | val_err = format_number(cov[ind]) |
---|
1057 | item[3].Show(True) |
---|
1058 | item[4].Show(True) |
---|
1059 | item[4].SetValue(val_err) |
---|
1060 | |
---|
1061 | has_error = True |
---|
1062 | i += 1 |
---|
1063 | #Show error title when any errors displayed |
---|
1064 | if has_error: |
---|
1065 | if not self.text2_3.IsShown(): |
---|
1066 | self.text2_3.Show(True) |
---|
1067 | |
---|
1068 | ## save current state |
---|
1069 | self.save_current_state() |
---|
1070 | #plot model |
---|
1071 | self._draw_model() |
---|
1072 | self._lay_out() |
---|
1073 | #PostStatusEvent |
---|
1074 | msg = "Fit completed! " |
---|
1075 | wx.PostEvent(self.manager.parent, StatusEvent(status=msg)) |
---|
1076 | |
---|
1077 | |
---|
1078 | def onSmear(self, event): |
---|
1079 | """ |
---|
1080 | Create a smear object that will change the way residuals |
---|
1081 | are compute when fitting |
---|
1082 | """ |
---|
1083 | if self.check_invalid_panel(): |
---|
1084 | return |
---|
1085 | if self.model ==None: |
---|
1086 | msg="Need model and data to smear plot" |
---|
1087 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1088 | "Smear: %s"%msg)) |
---|
1089 | return |
---|
1090 | temp_smearer = None |
---|
1091 | if self.enable_smearer.GetValue(): |
---|
1092 | temp_smearer= self.smearer |
---|
1093 | if hasattr(self.data,"dxl"): |
---|
1094 | msg= ": Resolution smearing parameters" |
---|
1095 | if hasattr(self.data,"dxw"): |
---|
1096 | msg= ": Slit smearing parameters" |
---|
1097 | if self.smearer ==None: |
---|
1098 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1099 | "Data contains no smearing information")) |
---|
1100 | else: |
---|
1101 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1102 | "Data contains smearing information %s"%msg)) |
---|
1103 | |
---|
1104 | ## set smearing value whether or not the data contain the smearing info |
---|
1105 | self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
1106 | qmax= float(self.qmax_x)) |
---|
1107 | ##Calculate chi2 |
---|
1108 | self.compute_chisqr(smearer= temp_smearer) |
---|
1109 | |
---|
1110 | self.state.enable_smearer= self.enable_smearer.GetValue() |
---|
1111 | self.state.disable_smearer=self.disable_smearer.GetValue() |
---|
1112 | |
---|
1113 | def complete_chisqr(self, output, elapsed=None): |
---|
1114 | """ |
---|
1115 | print result chisqr |
---|
1116 | """ |
---|
1117 | try: |
---|
1118 | if output ==None: |
---|
1119 | output= "-" |
---|
1120 | |
---|
1121 | self.tcChi.SetLabel(str(format_number(output))) |
---|
1122 | |
---|
1123 | self.state.tcChi =self.tcChi |
---|
1124 | |
---|
1125 | except: |
---|
1126 | pass |
---|
1127 | |
---|
1128 | |
---|
1129 | def compute_chisqr1D(self, smearer=None): |
---|
1130 | """ |
---|
1131 | Compute chisqr for 1D |
---|
1132 | """ |
---|
1133 | try: |
---|
1134 | self.qmin_x = float(self.qmin.GetValue()) |
---|
1135 | self.qmax_x = float(self.qmax.GetValue()) |
---|
1136 | ##return residuals within self.qmin_x and self.qmax_x |
---|
1137 | from gui_thread import CalcChisqr1D |
---|
1138 | ## If a thread is already started, stop it |
---|
1139 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
1140 | self.calc_Chisqr.stop() |
---|
1141 | |
---|
1142 | self.calc_Chisqr= CalcChisqr1D( data1d= self.data, |
---|
1143 | model= self.model, |
---|
1144 | smearer=smearer, |
---|
1145 | qmin=self.qmin_x, |
---|
1146 | qmax=self.qmax_x, |
---|
1147 | completefn = self.complete_chisqr, |
---|
1148 | updatefn = None) |
---|
1149 | |
---|
1150 | self.calc_Chisqr.queue() |
---|
1151 | |
---|
1152 | except: |
---|
1153 | raise ValueError," Could not compute Chisqr for %s Model 2D: "%self.model.name |
---|
1154 | |
---|
1155 | |
---|
1156 | |
---|
1157 | |
---|
1158 | |
---|
1159 | def compute_chisqr2D(self): |
---|
1160 | """ |
---|
1161 | compute chi square given a model and data 2D and set the value |
---|
1162 | to the tcChi txtcrl |
---|
1163 | """ |
---|
1164 | try: |
---|
1165 | self.qmin_x = float(self.qmin.GetValue()) |
---|
1166 | self.qmax_x = float(self.qmax.GetValue()) |
---|
1167 | |
---|
1168 | ##return residuals within self.qmin_x and self.qmax_x |
---|
1169 | from gui_thread import CalcChisqr2D |
---|
1170 | ## If a thread is already started, stop it |
---|
1171 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
1172 | self.calc_Chisqr.stop() |
---|
1173 | |
---|
1174 | self.calc_Chisqr= CalcChisqr2D( data2d= self.data, |
---|
1175 | model= self.model, |
---|
1176 | qmin= self.qmin_x, |
---|
1177 | qmax = self.qmax_x, |
---|
1178 | completefn = self.complete_chisqr, |
---|
1179 | updatefn = None) |
---|
1180 | |
---|
1181 | self.calc_Chisqr.queue() |
---|
1182 | |
---|
1183 | except: |
---|
1184 | raise |
---|
1185 | |
---|
1186 | |
---|
1187 | def compute_chisqr(self , smearer=None): |
---|
1188 | """ |
---|
1189 | compute chi square given a model and data 1D and set the value |
---|
1190 | to the tcChi txtcrl |
---|
1191 | """ |
---|
1192 | flag = self._validate_qrange( self.qmin, self.qmax) |
---|
1193 | if flag== True: |
---|
1194 | try: |
---|
1195 | if hasattr(self.data,"data"): |
---|
1196 | self.compute_chisqr2D() |
---|
1197 | return |
---|
1198 | else: |
---|
1199 | self.compute_chisqr1D(smearer=smearer) |
---|
1200 | return |
---|
1201 | except: |
---|
1202 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
1203 | "Chisqr Error: %s"% sys.exc_value)) |
---|
1204 | return |
---|
1205 | |
---|
1206 | |
---|
1207 | def select_all_param(self,event): |
---|
1208 | """ |
---|
1209 | set to true or false all checkBox given the main checkbox value cb1 |
---|
1210 | """ |
---|
1211 | |
---|
1212 | self.param_toFit=[] |
---|
1213 | if self.parameters !=[]: |
---|
1214 | if self.cb1.GetValue(): |
---|
1215 | for item in self.parameters: |
---|
1216 | ## for data2D select all to fit |
---|
1217 | if self.data.__class__.__name__=="Data2D": |
---|
1218 | item[0].SetValue(True) |
---|
1219 | self.param_toFit.append(item ) |
---|
1220 | else: |
---|
1221 | ## for 1D all parameters except orientation |
---|
1222 | if not item in self.orientation_params: |
---|
1223 | item[0].SetValue(True) |
---|
1224 | self.param_toFit.append(item ) |
---|
1225 | if len(self.fittable_param)>0: |
---|
1226 | for item in self.fittable_param: |
---|
1227 | if self.data.__class__.__name__=="Data2D": |
---|
1228 | item[0].SetValue(True) |
---|
1229 | self.param_toFit.append(item ) |
---|
1230 | else: |
---|
1231 | ## for 1D all parameters except orientation |
---|
1232 | if not item in self.orientation_params_disp: |
---|
1233 | item[0].SetValue(True) |
---|
1234 | self.param_toFit.append(item ) |
---|
1235 | else: |
---|
1236 | for item in self.parameters: |
---|
1237 | item[0].SetValue(False) |
---|
1238 | for item in self.fittable_param: |
---|
1239 | item[0].SetValue(False) |
---|
1240 | self.param_toFit=[] |
---|
1241 | |
---|
1242 | self.save_current_state_fit() |
---|
1243 | if event !=None: |
---|
1244 | #self._undo.Enable(True) |
---|
1245 | ## post state to fit panel |
---|
1246 | event = PageInfoEvent(page = self) |
---|
1247 | wx.PostEvent(self.parent, event) |
---|
1248 | |
---|
1249 | |
---|
1250 | |
---|
1251 | def select_param(self,event): |
---|
1252 | """ |
---|
1253 | Select TextCtrl checked for fitting purpose and stores them |
---|
1254 | in self.param_toFit=[] list |
---|
1255 | """ |
---|
1256 | self.param_toFit=[] |
---|
1257 | for item in self.parameters: |
---|
1258 | #Skip t ifhe angle parameters if 1D data |
---|
1259 | if self.data.__class__.__name__ !="Data2D": |
---|
1260 | if item in self.orientation_params: |
---|
1261 | continue |
---|
1262 | #Select parameters to fit for list of primary parameters |
---|
1263 | if item[0].GetValue(): |
---|
1264 | if not (item in self.param_toFit): |
---|
1265 | self.param_toFit.append(item ) |
---|
1266 | else: |
---|
1267 | #remove parameters from the fitting list |
---|
1268 | if item in self.param_toFit: |
---|
1269 | self.param_toFit.remove(item) |
---|
1270 | |
---|
1271 | #Select parameters to fit for list of fittable parameters with dispersion |
---|
1272 | for item in self.fittable_param: |
---|
1273 | #Skip t ifhe angle parameters if 1D data |
---|
1274 | if self.data.__class__.__name__ !="Data2D": |
---|
1275 | if item in self.orientation_params: |
---|
1276 | continue |
---|
1277 | if item[0].GetValue(): |
---|
1278 | if not (item in self.param_toFit): |
---|
1279 | self.param_toFit.append(item) |
---|
1280 | else: |
---|
1281 | #remove parameters from the fitting list |
---|
1282 | if item in self.param_toFit: |
---|
1283 | self.param_toFit.remove(item) |
---|
1284 | |
---|
1285 | #Calculate num. of angle parameters |
---|
1286 | if self.data.__class__.__name__ =="Data2D": |
---|
1287 | len_orient_para = 0 |
---|
1288 | else: |
---|
1289 | len_orient_para = len(self.orientation_params) #assume even len |
---|
1290 | #Total num. of angle parameters |
---|
1291 | if len(self.fittable_param) > 0: |
---|
1292 | len_orient_para *= 2 |
---|
1293 | #Set the value of checkbox that selected every checkbox or not |
---|
1294 | if len(self.parameters)+len(self.fittable_param)-len_orient_para ==len(self.param_toFit): |
---|
1295 | self.cb1.SetValue(True) |
---|
1296 | else: |
---|
1297 | self.cb1.SetValue(False) |
---|
1298 | self.save_current_state_fit() |
---|
1299 | if event !=None: |
---|
1300 | #self._undo.Enable(True) |
---|
1301 | ## post state to fit panel |
---|
1302 | event = PageInfoEvent(page = self) |
---|
1303 | wx.PostEvent(self.parent, event) |
---|
1304 | |
---|
1305 | |
---|
1306 | |
---|
1307 | def set_model_param_sizer(self, model): |
---|
1308 | """ |
---|
1309 | Build the panel from the model content |
---|
1310 | @param model: the model selected in combo box for fitting purpose |
---|
1311 | """ |
---|
1312 | self.sizer3.Clear(True) |
---|
1313 | self.parameters = [] |
---|
1314 | self.param_toFit=[] |
---|
1315 | self.fittable_param=[] |
---|
1316 | self.fixed_param=[] |
---|
1317 | self.orientation_params=[] |
---|
1318 | self.orientation_params_disp=[] |
---|
1319 | |
---|
1320 | if model ==None: |
---|
1321 | self.sizer3.Layout() |
---|
1322 | self.SetScrollbars(20,20,25,65) |
---|
1323 | return |
---|
1324 | ## the panel is drawn using the current value of the fit engine |
---|
1325 | if self.engine_type==None and self.manager !=None: |
---|
1326 | self.engine_type= self.manager._return_engine_type() |
---|
1327 | |
---|
1328 | |
---|
1329 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
1330 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1331 | sizer = wx.GridBagSizer(5,5) |
---|
1332 | ## save the current model |
---|
1333 | self.model = model |
---|
1334 | |
---|
1335 | keys = self.model.getParamList() |
---|
1336 | #list of dispersion paramaters |
---|
1337 | self.disp_list=self.model.getDispParamList() |
---|
1338 | |
---|
1339 | keys.sort() |
---|
1340 | |
---|
1341 | iy = 0 |
---|
1342 | ix = 0 |
---|
1343 | self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) |
---|
1344 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
1345 | self.cb1.SetValue(False) |
---|
1346 | |
---|
1347 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
1348 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
1349 | ix +=1 |
---|
1350 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
1351 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
1352 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1353 | ix +=2 |
---|
1354 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
1355 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
1356 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1357 | self.text2_3.Hide() |
---|
1358 | ix +=1 |
---|
1359 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
1360 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
1361 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1362 | self.text2_min.Hide() |
---|
1363 | ix +=1 |
---|
1364 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
1365 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
1366 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1367 | self.text2_max.Hide() |
---|
1368 | ix += 1 |
---|
1369 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
1370 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
1371 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1372 | self.text2_4.Hide() |
---|
1373 | if self.engine_type=="park": |
---|
1374 | self.text2_max.Show(True) |
---|
1375 | self.text2_min.Show(True) |
---|
1376 | |
---|
1377 | for item in keys: |
---|
1378 | if not item in self.disp_list and not item in self.model.orientation_params: |
---|
1379 | |
---|
1380 | ##prepare a spot to store errors |
---|
1381 | if not self.model.details.has_key(item): |
---|
1382 | self.model.details [item] = ["",None,None] |
---|
1383 | |
---|
1384 | iy += 1 |
---|
1385 | ix = 0 |
---|
1386 | ## add parameters name with checkbox for selecting to fit |
---|
1387 | cb = wx.CheckBox(self, -1, item ) |
---|
1388 | cb.SetValue(False) |
---|
1389 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
1390 | sizer.Add( cb,( iy, ix),(1,1), |
---|
1391 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
1392 | |
---|
1393 | ## add parameter value |
---|
1394 | ix += 1 |
---|
1395 | value= self.model.getParam(item) |
---|
1396 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
1397 | style=wx.TE_PROCESS_ENTER) |
---|
1398 | |
---|
1399 | ctl1.SetValue(format_number(value)) |
---|
1400 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
1401 | ## text to show error sign |
---|
1402 | ix += 1 |
---|
1403 | text2=wx.StaticText(self, -1, '+/-') |
---|
1404 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
1405 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1406 | text2.Hide() |
---|
1407 | ix += 1 |
---|
1408 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
1409 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1410 | ctl2.Hide() |
---|
1411 | |
---|
1412 | ix += 1 |
---|
1413 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
1414 | kill_focus_callback = self._onparamRangeEnter, |
---|
1415 | text_enter_callback = self._onparamRangeEnter) |
---|
1416 | |
---|
1417 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1418 | ctl3.Hide() |
---|
1419 | |
---|
1420 | ix += 1 |
---|
1421 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
1422 | kill_focus_callback = self._onparamRangeEnter, |
---|
1423 | text_enter_callback = self._onparamRangeEnter) |
---|
1424 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1425 | |
---|
1426 | ctl4.Hide() |
---|
1427 | |
---|
1428 | if self.engine_type=="park": |
---|
1429 | ctl3.Show(True) |
---|
1430 | ctl4.Show(True) |
---|
1431 | ix +=1 |
---|
1432 | # Units |
---|
1433 | if self.model.details.has_key(item): |
---|
1434 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
1435 | else: |
---|
1436 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
1437 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1438 | |
---|
1439 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1440 | self.parameters.append([cb,item, ctl1, |
---|
1441 | text2,ctl2, ctl3, ctl4,units]) |
---|
1442 | |
---|
1443 | iy+=1 |
---|
1444 | sizer.Add((10,10),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1445 | |
---|
1446 | # type can be either Guassian or Array |
---|
1447 | if len(self.model.dispersion.values())>0: |
---|
1448 | type= self.model.dispersion.values()[0]["type"] |
---|
1449 | else: |
---|
1450 | type = "Gaussian" |
---|
1451 | |
---|
1452 | iy += 1 |
---|
1453 | ix = 0 |
---|
1454 | #Add tile for orientational angle |
---|
1455 | for item in keys: |
---|
1456 | if item in self.model.orientation_params: |
---|
1457 | orient_angle = wx.StaticText(self, -1, '[For 2D only]:') |
---|
1458 | sizer.Add(orient_angle,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1459 | if not self.data.__class__.__name__ =="Data2D": |
---|
1460 | orient_angle.Hide() |
---|
1461 | else: |
---|
1462 | orient_angle.Show(True) |
---|
1463 | break |
---|
1464 | |
---|
1465 | #For Gaussian only |
---|
1466 | if type.lower() != "array": |
---|
1467 | for item in self.model.orientation_params: |
---|
1468 | if not item in self.disp_list: |
---|
1469 | ##prepare a spot to store min max |
---|
1470 | if not self.model.details.has_key(item): |
---|
1471 | self.model.details [item] = ["",None,None] |
---|
1472 | |
---|
1473 | iy += 1 |
---|
1474 | ix = 0 |
---|
1475 | ## add parameters name with checkbox for selecting to fit |
---|
1476 | cb = wx.CheckBox(self, -1, item ) |
---|
1477 | cb.SetValue(False) |
---|
1478 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
1479 | if self.data.__class__.__name__ =="Data2D": |
---|
1480 | cb.Show(True) |
---|
1481 | else: |
---|
1482 | cb.Hide() |
---|
1483 | sizer.Add( cb,( iy, ix),(1,1), |
---|
1484 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
1485 | |
---|
1486 | ## add parameter value |
---|
1487 | ix += 1 |
---|
1488 | value= self.model.getParam(item) |
---|
1489 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
1490 | style=wx.TE_PROCESS_ENTER) |
---|
1491 | |
---|
1492 | ctl1.SetValue(format_number(value)) |
---|
1493 | if self.data.__class__.__name__ =="Data2D": |
---|
1494 | ctl1.Show(True) |
---|
1495 | else: |
---|
1496 | ctl1.Hide() |
---|
1497 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
1498 | ## text to show error sign |
---|
1499 | ix += 1 |
---|
1500 | text2=wx.StaticText(self, -1, '+/-') |
---|
1501 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
1502 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1503 | text2.Hide() |
---|
1504 | ix += 1 |
---|
1505 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
1506 | |
---|
1507 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1508 | ctl2.Hide() |
---|
1509 | |
---|
1510 | |
---|
1511 | ix += 1 |
---|
1512 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
1513 | kill_focus_callback = self._onparamRangeEnter, |
---|
1514 | text_enter_callback = self._onparamRangeEnter) |
---|
1515 | |
---|
1516 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1517 | ctl3.Hide() |
---|
1518 | #if self.data.__class__.__name__ =="Data2D": |
---|
1519 | # ctl3.Show(True) |
---|
1520 | |
---|
1521 | #else: |
---|
1522 | # ctl3.Hide() |
---|
1523 | |
---|
1524 | ix += 1 |
---|
1525 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
1526 | kill_focus_callback = self._onparamRangeEnter, |
---|
1527 | text_enter_callback = self._onparamRangeEnter) |
---|
1528 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1529 | |
---|
1530 | ctl4.Hide() |
---|
1531 | |
---|
1532 | if self.engine_type =="park" and self.data.__class__.__name__ =="Data2D": |
---|
1533 | ctl3.Show(True) |
---|
1534 | ctl4.Show(True) |
---|
1535 | |
---|
1536 | ix +=1 |
---|
1537 | # Units |
---|
1538 | if self.model.details.has_key(item): |
---|
1539 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
1540 | else: |
---|
1541 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
1542 | if self.data.__class__.__name__ =="Data2D": |
---|
1543 | units.Show(True) |
---|
1544 | |
---|
1545 | else: |
---|
1546 | units.Hide() |
---|
1547 | |
---|
1548 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1549 | |
---|
1550 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1551 | self.parameters.append([cb,item, ctl1, |
---|
1552 | text2,ctl2, ctl3, ctl4,units]) |
---|
1553 | self.orientation_params.append([cb,item, ctl1, |
---|
1554 | text2,ctl2, ctl3, ctl4,units]) |
---|
1555 | |
---|
1556 | iy+=1 |
---|
1557 | |
---|
1558 | #Display units text on panel |
---|
1559 | for item in keys: |
---|
1560 | if self.model.details.has_key(item): |
---|
1561 | self.text2_4.Show() |
---|
1562 | |
---|
1563 | #self.state.cb1 = self.cb1.GetValue() |
---|
1564 | #self._copy_parameters_state(self.orientation_params, |
---|
1565 | # self.state.orientation_params) |
---|
1566 | #self._copy_parameters_state(self.orientation_params_disp, |
---|
1567 | # self.state.orientation_params_disp) |
---|
1568 | #self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
1569 | #self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
1570 | #self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
1571 | |
---|
1572 | self.save_current_state_fit() |
---|
1573 | boxsizer1.Add(sizer) |
---|
1574 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1575 | self.sizer3.Layout() |
---|
1576 | self.Layout() |
---|
1577 | self.Refresh() |
---|
1578 | self.SetScrollbars(20,20,25,65) |
---|
1579 | |
---|
1580 | |
---|
1581 | |
---|
1582 | class HelpWindow(wx.Frame): |
---|
1583 | def __init__(self, parent, id, title): |
---|
1584 | wx.Frame.__init__(self, parent, id, title, size=(570, 400)) |
---|
1585 | |
---|
1586 | from sans.models.CylinderModel import CylinderModel |
---|
1587 | model = CylinderModel() |
---|
1588 | |
---|
1589 | from danse.common.plottools.plottables import Data1D |
---|
1590 | data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1]) |
---|
1591 | |
---|
1592 | from fitpanel import PageInfo |
---|
1593 | myinfo = PageInfo(self, model, data=data ) |
---|
1594 | |
---|
1595 | ## add data |
---|
1596 | |
---|
1597 | from models import ModelList |
---|
1598 | mylist= ModelList() |
---|
1599 | |
---|
1600 | from sans.models.SphereModel import SphereModel |
---|
1601 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
1602 | from sans.models.DebyeModel import DebyeModel |
---|
1603 | from sans.models.LineModel import LineModel |
---|
1604 | name= "shapes" |
---|
1605 | list1= [SphereModel] |
---|
1606 | mylist.set_list( name, list1) |
---|
1607 | |
---|
1608 | name= "Shape-independent" |
---|
1609 | list1= [DebyeModel] |
---|
1610 | mylist.set_list( name, list1) |
---|
1611 | |
---|
1612 | name= "Structure Factors" |
---|
1613 | list1= [SquareWellStructure] |
---|
1614 | mylist.set_list( name, list1) |
---|
1615 | |
---|
1616 | name= "Added models" |
---|
1617 | list1= [LineModel] |
---|
1618 | mylist.set_list( name, list1) |
---|
1619 | |
---|
1620 | myinfo.model_list_box = mylist.get_list() |
---|
1621 | |
---|
1622 | self.page = FitPage(self, myinfo) |
---|
1623 | |
---|
1624 | self.Centre() |
---|
1625 | self.Show(True) |
---|
1626 | |
---|
1627 | if __name__=="__main__": |
---|
1628 | app = wx.App() |
---|
1629 | HelpWindow(None, -1, 'HelpWindow') |
---|
1630 | app.MainLoop() |
---|
1631 | |
---|