1 | import sys |
---|
2 | import wx |
---|
3 | import wx.lib.newevent |
---|
4 | import numpy |
---|
5 | import copy |
---|
6 | import math |
---|
7 | from sans.models.dispersion_models import ArrayDispersion, GaussianDispersion |
---|
8 | |
---|
9 | from sans.guicomm.events import StatusEvent |
---|
10 | from sans.guiframe.utils import format_number |
---|
11 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
12 | _BOX_WIDTH = 80 |
---|
13 | |
---|
14 | import basepage |
---|
15 | from basepage import BasicPage |
---|
16 | |
---|
17 | |
---|
18 | class FitPage(BasicPage): |
---|
19 | """ |
---|
20 | FitPanel class contains fields allowing to display results when |
---|
21 | fitting a model and one data |
---|
22 | @note: For Fit to be performed the user should check at least one parameter |
---|
23 | on fit Panel window. |
---|
24 | |
---|
25 | """ |
---|
26 | def __init__(self,parent, page_info, name=""): |
---|
27 | BasicPage.__init__(self, parent, page_info,name) |
---|
28 | """ |
---|
29 | Initialization of the Panel |
---|
30 | """ |
---|
31 | ## fit page does not content npts txtcrtl |
---|
32 | self.npts=None |
---|
33 | ## if no dispersity parameters is avaible |
---|
34 | self.text_disp_1=None |
---|
35 | |
---|
36 | self._fill_datainfo_sizer() |
---|
37 | self._fill_model_sizer( self.sizer1) |
---|
38 | self._fill_range_sizer() |
---|
39 | if hasattr(self.page_info,"model"): |
---|
40 | self.set_model_param_sizer(self.model) |
---|
41 | |
---|
42 | |
---|
43 | def _on_display_description(self, event): |
---|
44 | """ |
---|
45 | Show or Hide description |
---|
46 | @param event: wx.EVT_RADIOBUTTON |
---|
47 | """ |
---|
48 | self._on_display_description_helper() |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | def _on_display_description_helper(self): |
---|
53 | """ |
---|
54 | Show or Hide description |
---|
55 | @param event: wx.EVT_RADIOBUTTON |
---|
56 | """ |
---|
57 | ## save state of radiobox |
---|
58 | self.page_info. save_radiobox_state( self.description_hide ) |
---|
59 | self.page_info. save_radiobox_state( self.description_show ) |
---|
60 | ## Show description |
---|
61 | if not self.description_show.GetValue(): |
---|
62 | self.sizer_description.Clear(True) |
---|
63 | |
---|
64 | else: |
---|
65 | model=self.page_info.model |
---|
66 | description="" |
---|
67 | if model!=None: |
---|
68 | description = self.page_info.model.description |
---|
69 | self.description = wx.StaticText( self,-1,str(description) ) |
---|
70 | self.sizer_description.Add( self.description, 1, wx.EXPAND | wx.ALL, 10 ) |
---|
71 | |
---|
72 | self.Layout() |
---|
73 | |
---|
74 | |
---|
75 | def _fill_range_sizer(self): |
---|
76 | """ |
---|
77 | Fill the sizer containing the plotting range |
---|
78 | add access to npts |
---|
79 | """ |
---|
80 | sizer_fit = wx.GridSizer(2, 2,0, 0) |
---|
81 | |
---|
82 | self.btFit = wx.Button(self,wx.NewId(),'Fit') |
---|
83 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId()) |
---|
84 | self.btFit.SetToolTipString("Perform fit.") |
---|
85 | |
---|
86 | self.btStopFit = wx.Button(self,wx.NewId(),'Stop Fit') |
---|
87 | #self.btStopFit.Bind(wx.EVT_BUTTON, self.onStopFit,id= self.btStopFit.GetId()) |
---|
88 | self.btStopFit.SetToolTipString("Stop Current fit job.") |
---|
89 | #self.btStopFit.Hide() |
---|
90 | |
---|
91 | sizer_fit.Add((5,5),1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
92 | sizer_fit.Add((5,5)) |
---|
93 | sizer_fit.Add(self.btFit,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
94 | sizer_fit.Add(self.btStopFit,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
95 | |
---|
96 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
97 | #Filling the sizer containing instruments smearing info. |
---|
98 | self.disable_smearer = wx.RadioButton(self, -1, 'No', style=wx.RB_GROUP) |
---|
99 | self.enable_smearer = wx.RadioButton(self, -1, 'Yes') |
---|
100 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
101 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
102 | |
---|
103 | sizer_smearer.Add(wx.StaticText(self,-1,'Instrument Smearing? ')) |
---|
104 | sizer_smearer.Add((10, 10)) |
---|
105 | sizer_smearer.Add( self.enable_smearer ) |
---|
106 | sizer_smearer.Add((10,10)) |
---|
107 | sizer_smearer.Add( self.disable_smearer ) |
---|
108 | |
---|
109 | self._set_range_sizer( title="Fitted Q Range", |
---|
110 | object1=sizer_smearer, object= sizer_fit) |
---|
111 | |
---|
112 | |
---|
113 | def _fill_datainfo_sizer(self): |
---|
114 | """ |
---|
115 | fill sizer 0 with data info |
---|
116 | """ |
---|
117 | self.sizer0.Clear(True) |
---|
118 | ## no loaded data , don't fill the sizer |
---|
119 | if self.data== None: |
---|
120 | self.sizer0.Layout() |
---|
121 | return |
---|
122 | |
---|
123 | box_description= wx.StaticBox(self, -1, 'Loaded Data') |
---|
124 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
125 | #---------------------------------------------------------- |
---|
126 | sizer_data = wx.GridSizer(3, 3,5, 5) |
---|
127 | #Filling the sizer containing data related fields |
---|
128 | DataSource =wx.StaticText(self, -1,str(self.data.name)) |
---|
129 | |
---|
130 | sizer_data.Add(wx.StaticText(self, -1, 'Data Source Name : ')) |
---|
131 | sizer_data.Add(DataSource ) |
---|
132 | sizer_data.Add( (5,5) ) |
---|
133 | |
---|
134 | #---------sizer 2 draw-------------------------------- |
---|
135 | #set maximum range for x in linear scale |
---|
136 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
137 | # Minimum value of data |
---|
138 | data_min = str(format_number(numpy.min(self.data.x))) |
---|
139 | # Maximum value of data |
---|
140 | data_max = str(format_number(numpy.max(self.data.x))) |
---|
141 | text4_3 = wx.StaticText(self, -1, 'Maximum Q Range(Linear)', |
---|
142 | style=wx.ALIGN_LEFT) |
---|
143 | sizer_data.Add( text4_3 ) |
---|
144 | sizer_data.Add(wx.StaticText(self, -1, "Min : %s"%data_min)) |
---|
145 | sizer_data.Add(wx.StaticText(self, -1, "Max : %s"%data_max)) |
---|
146 | |
---|
147 | else: |
---|
148 | radius_min= 0 |
---|
149 | x= numpy.max(self.data.xmin, self.data.xmax) |
---|
150 | y= numpy.max(self.data.ymin, self.data.ymax) |
---|
151 | radius_max = math.sqrt(x*x + y*y) |
---|
152 | # Minimum value of data |
---|
153 | data_min = str(format_number(radius_min)) |
---|
154 | # Maximum value of data |
---|
155 | data_max = str(format_number(radius_max)) |
---|
156 | text4_3 = wx.StaticText(self, -1, 'Maximum Q Range', |
---|
157 | style=wx.ALIGN_LEFT) |
---|
158 | sizer_data.Add( text4_3 ) |
---|
159 | sizer_data.Add(wx.StaticText(self, -1, "Min : %s"%data_min)) |
---|
160 | sizer_data.Add(wx.StaticText(self, -1, "Max : %s"%data_max)) |
---|
161 | |
---|
162 | boxsizer1.Add(sizer_data) |
---|
163 | #------------------------------------------------------------ |
---|
164 | self.sizer0.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
165 | self.sizer0.Layout() |
---|
166 | |
---|
167 | self.qmin_x= data_min |
---|
168 | self.qmax_x= data_max |
---|
169 | |
---|
170 | def _fill_model_sizer(self, sizer): |
---|
171 | """ |
---|
172 | fill sizer containing model info |
---|
173 | """ |
---|
174 | sizer_tcChi = wx.GridSizer(2, 2,5, 5) |
---|
175 | self.tcChi = wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT) |
---|
176 | #self.tcChi.Hide() |
---|
177 | |
---|
178 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
179 | #self.text1_1.Hide() |
---|
180 | |
---|
181 | self.btChi = wx.Button(self,wx.NewId(),'View Chi') |
---|
182 | self.btChi.Bind(wx.EVT_BUTTON, self._onFit,id= self.btChi.GetId()) |
---|
183 | self.btChi.SetToolTipString("View data - model.") |
---|
184 | |
---|
185 | sizer_tcChi.Add(self.text1_1,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
186 | sizer_tcChi.Add(self.btChi,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
187 | sizer_tcChi.Add(self.tcChi,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
188 | sizer_tcChi.Add((5,5),1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
189 | |
---|
190 | ## class base method to add view 2d button |
---|
191 | self._set_model_sizer(sizer=sizer, title="Model",object= sizer_tcChi ) |
---|
192 | |
---|
193 | |
---|
194 | def _set_sizer_gaussian(self): |
---|
195 | """ |
---|
196 | draw sizer with gaussian dispersity parameters |
---|
197 | """ |
---|
198 | self.fittable_param=[] |
---|
199 | self.fixed_param=[] |
---|
200 | ##reset model dispersity to gaussian |
---|
201 | self._reset_gaussian_dispers() |
---|
202 | |
---|
203 | self.sizer4_4.Clear(True) |
---|
204 | |
---|
205 | if self.model==None: |
---|
206 | ##no model is selected |
---|
207 | return |
---|
208 | if not self.enable_disp.GetValue(): |
---|
209 | ## the user didn't select dispersity display |
---|
210 | return |
---|
211 | ix=0 |
---|
212 | iy=1 |
---|
213 | disp = wx.StaticText(self, -1, 'Names') |
---|
214 | self.sizer4_4.Add(disp,( iy, ix),(1,1), |
---|
215 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
216 | ix += 1 |
---|
217 | values = wx.StaticText(self, -1, 'Values') |
---|
218 | self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
219 | ix +=2 |
---|
220 | self.text_disp_1 = wx.StaticText(self, -1, 'Errors') |
---|
221 | self.sizer4_4.Add( self.text_disp_1,(iy, ix),(1,1),\ |
---|
222 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
223 | self.text_disp_1.Hide() |
---|
224 | ix += 1 |
---|
225 | npts = wx.StaticText(self, -1, 'Npts') |
---|
226 | self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
227 | ix += 1 |
---|
228 | nsigmas = wx.StaticText(self, -1, 'Nsigmas') |
---|
229 | self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
230 | |
---|
231 | for item in self.model.dispersion.keys(): |
---|
232 | name1=item+".width" |
---|
233 | name2=item+".npts" |
---|
234 | name3=item+".nsigmas" |
---|
235 | iy += 1 |
---|
236 | for p in self.model.dispersion[item].keys(): |
---|
237 | |
---|
238 | if p=="width": |
---|
239 | ix = 0 |
---|
240 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
241 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
242 | |
---|
243 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
244 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
245 | ix = 1 |
---|
246 | value= self.model.getParam(name1) |
---|
247 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
248 | style=wx.TE_PROCESS_ENTER) |
---|
249 | |
---|
250 | ctl1.SetValue(str (format_number(value))) |
---|
251 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
252 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
253 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
254 | |
---|
255 | ## text to show error sign |
---|
256 | ix = 2 |
---|
257 | text2=wx.StaticText(self, -1, '+/-') |
---|
258 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
259 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
260 | text2.Hide() |
---|
261 | ## txtcrtl to add error from fit |
---|
262 | ix = 3 |
---|
263 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
264 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
265 | ctl2.Hide() |
---|
266 | |
---|
267 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
268 | ctl2, None, None,None]) |
---|
269 | elif p=="npts": |
---|
270 | ix = 4 |
---|
271 | value= self.model.getParam(name2) |
---|
272 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
273 | style=wx.TE_PROCESS_ENTER) |
---|
274 | |
---|
275 | Tctl.SetValue(str (format_number(value))) |
---|
276 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
277 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
278 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
279 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
280 | |
---|
281 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
282 | None, None,None]) |
---|
283 | |
---|
284 | elif p=="nsigmas": |
---|
285 | ix = 5 |
---|
286 | value= self.model.getParam(name3) |
---|
287 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
288 | style=wx.TE_PROCESS_ENTER) |
---|
289 | Tctl.SetValue(str (format_number(value))) |
---|
290 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
291 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
292 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
293 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
294 | ix +=1 |
---|
295 | self.sizer4_4.Add((20,20), (iy,ix),(1,1), |
---|
296 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
297 | |
---|
298 | self.fixed_param.append([None,name3, Tctl |
---|
299 | ,None,None, None, None,None]) |
---|
300 | |
---|
301 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
302 | " Selected Distribution: Gaussian")) |
---|
303 | ix =0 |
---|
304 | iy +=1 |
---|
305 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
306 | self.sizer4_4.Layout() |
---|
307 | self.sizer4.Layout() |
---|
308 | self.SetScrollbars(20,20,200,100) |
---|
309 | |
---|
310 | def _onFit(self, event): |
---|
311 | """ |
---|
312 | Allow to fit |
---|
313 | """ |
---|
314 | from sans.guiframe.utils import check_value |
---|
315 | flag = check_value( self.qmin, self.qmax) |
---|
316 | |
---|
317 | if not flag: |
---|
318 | msg= "Fitting range invalid" |
---|
319 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
320 | return |
---|
321 | |
---|
322 | if len(self.param_toFit) <= 0: |
---|
323 | msg= "Select at least one parameter to fit" |
---|
324 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
325 | return |
---|
326 | |
---|
327 | self.qmin_x=float(self.qmin.GetValue()) |
---|
328 | self.qmax_x =float( self.qmax.GetValue()) |
---|
329 | |
---|
330 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
331 | |
---|
332 | #single fit |
---|
333 | self.manager.on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) |
---|
334 | |
---|
335 | self.sizer5.Layout() |
---|
336 | self.SetScrollbars(20,20,55,40) |
---|
337 | |
---|
338 | |
---|
339 | def _on_select_model(self, event): |
---|
340 | """ |
---|
341 | call back for model selection |
---|
342 | """ |
---|
343 | self._on_select_model_helper(event) |
---|
344 | self.set_model_param_sizer(self.model) |
---|
345 | self._set_sizer_gaussian() |
---|
346 | |
---|
347 | evt = ModelEventbox(model=self.model) |
---|
348 | wx.PostEvent(self.event_owner, evt) |
---|
349 | try: |
---|
350 | self.compute_chisqr() |
---|
351 | self.text1_1.Show() |
---|
352 | self.tcChi.Show() |
---|
353 | except: |
---|
354 | ## error occured on chisqr computation |
---|
355 | pass |
---|
356 | def get_range(self): |
---|
357 | """ |
---|
358 | return the fitting range |
---|
359 | """ |
---|
360 | return self.qmin_x , self.qmax_x |
---|
361 | |
---|
362 | def get_param_list(self): |
---|
363 | """ |
---|
364 | @return self.param_toFit: list containing references to TextCtrl |
---|
365 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
366 | @raise: if return an empty list of parameter fit will nnote work |
---|
367 | properly so raise ValueError,"missing parameter to fit" |
---|
368 | """ |
---|
369 | if self.param_toFit !=[]: |
---|
370 | return self.param_toFit |
---|
371 | else: |
---|
372 | raise ValueError,"missing parameter to fit" |
---|
373 | |
---|
374 | def onsetValues(self,chisqr, out,cov): |
---|
375 | """ |
---|
376 | Build the panel from the fit result |
---|
377 | @param chisqr:Value of the goodness of fit metric |
---|
378 | @param out:list of parameter with the best value found during fitting |
---|
379 | @param cov:Covariance matrix |
---|
380 | |
---|
381 | """ |
---|
382 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
383 | params = {} |
---|
384 | is_modified = False |
---|
385 | has_error = False |
---|
386 | #set the panel when fit result are float not list |
---|
387 | if out.__class__==numpy.float64: |
---|
388 | self.param_toFit[0][2].SetValue(format_number(out)) |
---|
389 | self.param_toFit[0][2].Refresh() |
---|
390 | if cov !=None : |
---|
391 | self.text2_3.Show(True) |
---|
392 | if self.text_disp_1!=None: |
---|
393 | self.text_disp_1.Show(True) |
---|
394 | |
---|
395 | self.param_toFit[0][3].Show(True) |
---|
396 | self.param_toFit[0][4].Clear() |
---|
397 | self.param_toFit[0][4].SetValue(format_number(cov[0])) |
---|
398 | self.param_toFit[0][4].Show(True) |
---|
399 | else: |
---|
400 | i=0 |
---|
401 | j=0 |
---|
402 | #Set the panel when fit result are list |
---|
403 | for item in self.param_toFit: |
---|
404 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
405 | item[2].SetValue(format_number(self.model.getParam(item[1]))) |
---|
406 | item[2].Refresh() |
---|
407 | if(cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
408 | self.text2_3.Show(True) |
---|
409 | if self.text_disp_1!=None: |
---|
410 | self.text_disp_1.Show(True) |
---|
411 | item[3].Show(True) |
---|
412 | item[4].Clear() |
---|
413 | for j in range(len(out)): |
---|
414 | if out[j]==self.model.getParam(item[1]): |
---|
415 | break |
---|
416 | item[4].SetValue(format_number(cov[j])) |
---|
417 | item[4].Refresh() |
---|
418 | item[4].Show(True) |
---|
419 | i+=1 |
---|
420 | |
---|
421 | self.sizer3.Layout() |
---|
422 | self.sizer4_4.Layout() |
---|
423 | self.sizer4.Layout() |
---|
424 | self.SetScrollbars(20,20,200,100) |
---|
425 | |
---|
426 | |
---|
427 | |
---|
428 | def onSmear(self, event): |
---|
429 | """ |
---|
430 | Create a smear object that will change the way residuals |
---|
431 | are compute when fitting |
---|
432 | """ |
---|
433 | smear =None |
---|
434 | msg="" |
---|
435 | if self.enable_smearer.GetValue(): |
---|
436 | from DataLoader.qsmearing import smear_selection |
---|
437 | smear = smear_selection( self.data ) |
---|
438 | if hasattr(self.data,"dxl"): |
---|
439 | msg= ": Resolution smearing parameters" |
---|
440 | if hasattr(self.data,"dxw"): |
---|
441 | msg= ": Slit smearing parameters" |
---|
442 | if smear ==None: |
---|
443 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
444 | "Data contains no smearing information")) |
---|
445 | else: |
---|
446 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
447 | "Data contains smearing information %s"%msg)) |
---|
448 | self.manager.set_smearer(smear, qmin= self.qmin_x, qmax= self.qmax_x) |
---|
449 | |
---|
450 | |
---|
451 | |
---|
452 | def compute_chisqr2D(self): |
---|
453 | """ |
---|
454 | compute chi square given a model and data 2D and set the value |
---|
455 | to the tcChi txtcrl |
---|
456 | """ |
---|
457 | from sans.guiframe.utils import check_value |
---|
458 | flag = check_value( self.qmin, self.qmax) |
---|
459 | |
---|
460 | res=[] |
---|
461 | if flag== True: |
---|
462 | try: |
---|
463 | self.qmin_x = float(self.qmin.GetValue()) |
---|
464 | self.qmax_x = float(self.qmax.GetValue()) |
---|
465 | for i in range(len(self.data.x_bins)): |
---|
466 | for j in range(len(self.data.y_bins)): |
---|
467 | #Check the range containing data between self.qmin_x and self.qmax_x |
---|
468 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)>=math.pow(self.qmin_x,2): |
---|
469 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)<=math.pow(self.qmax_x,2): |
---|
470 | chisqrji=(self.data.data[j][i]- self.model.runXY(\ |
---|
471 | [self.data.y_bins[j],self.data.x_bins[i]]))\ |
---|
472 | /self.data.err_data[j][i] |
---|
473 | #Vector containing residuals |
---|
474 | res.append( math.pow(chisqrji,2) ) |
---|
475 | # compute sum of residual |
---|
476 | sum=0 |
---|
477 | for item in res: |
---|
478 | if numpy.isfinite(item): |
---|
479 | sum +=item |
---|
480 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
481 | except: |
---|
482 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
483 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
484 | return |
---|
485 | |
---|
486 | |
---|
487 | def compute_chisqr(self): |
---|
488 | """ |
---|
489 | compute chi square given a model and data 1D and set the value |
---|
490 | to the tcChi txtcrl |
---|
491 | """ |
---|
492 | from sans.guiframe.utils import check_value |
---|
493 | flag = check_value( self.qmin, self.qmax) |
---|
494 | |
---|
495 | if flag== True: |
---|
496 | try: |
---|
497 | if hasattr(self.data,"data"): |
---|
498 | self.compute_chisqr2D() |
---|
499 | return |
---|
500 | else: |
---|
501 | self.qmin_x = float(self.qmin.GetValue()) |
---|
502 | self.qmax_x = float(self.qmax.GetValue()) |
---|
503 | # return residuals within self.qmin_x and self.qmax_x |
---|
504 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
505 | if self.qmin_x==None and self.qmax_x==None: |
---|
506 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
507 | temp=(y - fx)/dy |
---|
508 | res= temp*temp |
---|
509 | else: |
---|
510 | idx = (x>= self.qmin_x) & (x <=self.qmax_x) |
---|
511 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
512 | temp=(y[idx] - fx)/dy[idx] |
---|
513 | res= temp*temp |
---|
514 | #sum of residuals |
---|
515 | sum=0 |
---|
516 | for item in res: |
---|
517 | if numpy.isfinite(item): |
---|
518 | sum +=item |
---|
519 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
520 | except: |
---|
521 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
522 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
523 | return |
---|
524 | |
---|
525 | |
---|
526 | def select_all_param(self,event): |
---|
527 | """ |
---|
528 | set to true or false all checkBox given the main checkbox value cb1 |
---|
529 | """ |
---|
530 | self.param_toFit=[] |
---|
531 | if self.parameters !=[]: |
---|
532 | if self.cb1.GetValue()==True: |
---|
533 | for item in self.parameters: |
---|
534 | item[0].SetValue(True) |
---|
535 | self.param_toFit.append(item ) |
---|
536 | if len(self.fittable_param)>0: |
---|
537 | for item in self.fittable_param: |
---|
538 | item[0].SetValue(True) |
---|
539 | self.param_toFit.append(item ) |
---|
540 | else: |
---|
541 | for item in self.parameters: |
---|
542 | item[0].SetValue(False) |
---|
543 | for item in self.fittable_param: |
---|
544 | item[0].SetValue(False) |
---|
545 | self.param_toFit=[] |
---|
546 | |
---|
547 | |
---|
548 | |
---|
549 | def select_param(self,event): |
---|
550 | """ |
---|
551 | Select TextCtrl checked for fitting purpose and stores them |
---|
552 | in self.param_toFit=[] list |
---|
553 | """ |
---|
554 | self.param_toFit=[] |
---|
555 | for item in self.parameters: |
---|
556 | #Select parameters to fit for list of primary parameters |
---|
557 | if item[0].GetValue()==True: |
---|
558 | if not (item in self.param_toFit): |
---|
559 | self.param_toFit.append(item ) |
---|
560 | else: |
---|
561 | #remove parameters from the fitting list |
---|
562 | if item in self.param_toFit: |
---|
563 | self.param_toFit.remove(item) |
---|
564 | #Select parameters to fit for list of fittable parameters with dispersion |
---|
565 | for item in self.fittable_param: |
---|
566 | if item[0].GetValue()==True: |
---|
567 | if not (item in self.param_toFit): |
---|
568 | self.param_toFit.append(item) |
---|
569 | else: |
---|
570 | #remove parameters from the fitting list |
---|
571 | if item in self.param_toFit: |
---|
572 | self.param_toFit.remove(item) |
---|
573 | #Set the value of checkbox that selected every checkbox or not |
---|
574 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
575 | self.cb1.SetValue(True) |
---|
576 | else: |
---|
577 | self.cb1.SetValue(False) |
---|
578 | |
---|
579 | |
---|
580 | def set_model_description(self,description,sizer): |
---|
581 | """ |
---|
582 | fill a sizer with description |
---|
583 | @param description: of type string |
---|
584 | @param sizer: wx.BoxSizer() |
---|
585 | """ |
---|
586 | |
---|
587 | sizer.Clear(True) |
---|
588 | box_description= wx.StaticBox(self, -1, 'Model Description') |
---|
589 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
590 | |
---|
591 | self.description = wx.StaticText( self,-1,str(description) ) |
---|
592 | sizer_selection=wx.BoxSizer(wx.HORIZONTAL) |
---|
593 | |
---|
594 | |
---|
595 | self.description_show = wx.RadioButton(self, -1, 'Show', style=wx.RB_GROUP) |
---|
596 | self.description_hide = wx.RadioButton(self, -1, 'Hide') |
---|
597 | |
---|
598 | self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description, |
---|
599 | id=self.description_hide.GetId() ) |
---|
600 | |
---|
601 | self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description, |
---|
602 | id=self.description_show.GetId() ) |
---|
603 | |
---|
604 | self.model_description = wx.Button(self, -1, "More Details") |
---|
605 | |
---|
606 | |
---|
607 | self.page_info.save_radiobox_state( self.description_hide ) |
---|
608 | self.page_info.save_radiobox_state( self.description_show ) |
---|
609 | |
---|
610 | sizer_selection.Add( self.description_show ) |
---|
611 | sizer_selection.Add( (20,20)) |
---|
612 | sizer_selection.Add( self.description_hide ) |
---|
613 | sizer_selection.Add( (20,20)) |
---|
614 | sizer_selection.Add( self.model_description ) |
---|
615 | |
---|
616 | |
---|
617 | self.sizer_description=wx.BoxSizer(wx.HORIZONTAL) |
---|
618 | self.sizer_description.Add( self.description, 1, wx.EXPAND | wx.ALL, 10 ) |
---|
619 | |
---|
620 | boxsizer1.Add( sizer_selection) |
---|
621 | boxsizer1.Add( (20,20)) |
---|
622 | boxsizer1.Add( self.sizer_description) |
---|
623 | |
---|
624 | |
---|
625 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
626 | sizer.Layout() |
---|
627 | |
---|
628 | |
---|
629 | |
---|
630 | |
---|
631 | def set_model_param_sizer(self, model, engine_type="park"): |
---|
632 | """ |
---|
633 | Build the panel from the model content |
---|
634 | @param model: the model selected in combo box for fitting purpose |
---|
635 | """ |
---|
636 | self.sizer3.Clear(True) |
---|
637 | self.parameters = [] |
---|
638 | self.param_toFit=[] |
---|
639 | self.fittable_param=[] |
---|
640 | self.fixed_param=[] |
---|
641 | |
---|
642 | if model ==None: |
---|
643 | self.sizer3.Layout() |
---|
644 | self.SetScrollbars(20,20,200,100) |
---|
645 | return |
---|
646 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
647 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
648 | sizer = wx.GridBagSizer(5,5) |
---|
649 | ## save the current model |
---|
650 | self.model = model |
---|
651 | |
---|
652 | keys = self.model.getParamList() |
---|
653 | #list of dispersion paramaters |
---|
654 | self.disp_list=self.model.getDispParamList() |
---|
655 | |
---|
656 | keys.sort() |
---|
657 | |
---|
658 | iy = 1 |
---|
659 | ix = 0 |
---|
660 | self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) |
---|
661 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
662 | self.cb1.SetValue(False) |
---|
663 | |
---|
664 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
665 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
666 | ix +=1 |
---|
667 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
668 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
669 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
670 | ix +=2 |
---|
671 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
672 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
673 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
674 | self.text2_3.Hide() |
---|
675 | ix +=1 |
---|
676 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
677 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
678 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
679 | self.text2_min.Hide() |
---|
680 | ix +=1 |
---|
681 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
682 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
683 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
684 | self.text2_max.Hide() |
---|
685 | ix += 1 |
---|
686 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
687 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
688 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
689 | self.text2_4.Hide() |
---|
690 | if engine_type == "park": |
---|
691 | self.text2_max.Show(True) |
---|
692 | self.text2_min.Show(True) |
---|
693 | |
---|
694 | for item in keys: |
---|
695 | if not item in self.disp_list: |
---|
696 | iy += 1 |
---|
697 | ix = 0 |
---|
698 | ## add parameters name with checkbox for selecting to fit |
---|
699 | cb = wx.CheckBox(self, -1, item ) |
---|
700 | cb.SetValue(False) |
---|
701 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
702 | sizer.Add( cb,( iy, ix),(1,1), |
---|
703 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
704 | ## add parameter value |
---|
705 | ix += 1 |
---|
706 | value= self.model.getParam(item) |
---|
707 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
708 | style=wx.TE_PROCESS_ENTER) |
---|
709 | |
---|
710 | ctl1.SetValue(str (format_number(value))) |
---|
711 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
712 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
713 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
714 | ## text to show error sign |
---|
715 | ix += 1 |
---|
716 | text2=wx.StaticText(self, -1, '+/-') |
---|
717 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
718 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
719 | text2.Hide() |
---|
720 | ## txtcrtl to add error from fit |
---|
721 | ix += 1 |
---|
722 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
723 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
724 | ctl2.Hide() |
---|
725 | |
---|
726 | ix += 1 |
---|
727 | ctl3 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
728 | ctl3.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
729 | ctl3.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
730 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
731 | ctl3.Hide() |
---|
732 | |
---|
733 | ix += 1 |
---|
734 | ctl4 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
735 | ctl4.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
736 | ctl4.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
737 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
738 | ctl4.Hide() |
---|
739 | if engine_type == "park": |
---|
740 | ctl3.Show(True) |
---|
741 | ctl4.Show(True) |
---|
742 | |
---|
743 | ix +=1 |
---|
744 | # Units |
---|
745 | try: |
---|
746 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
747 | except: |
---|
748 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
749 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
750 | |
---|
751 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
752 | self.parameters.append([cb,item, ctl1, |
---|
753 | text2,ctl2, ctl3, ctl4,None]) |
---|
754 | |
---|
755 | iy+=1 |
---|
756 | sizer.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
757 | |
---|
758 | #Display units text on panel |
---|
759 | for item in keys: |
---|
760 | if self.model.details[item][0]!='': |
---|
761 | self.text2_4.Show() |
---|
762 | break |
---|
763 | else: |
---|
764 | self.text2_4.Hide() |
---|
765 | |
---|
766 | boxsizer1.Add(sizer) |
---|
767 | |
---|
768 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
769 | self.sizer3.Layout() |
---|
770 | self.SetScrollbars(20,20,200,100) |
---|
771 | |
---|
772 | |
---|
773 | |
---|
774 | |
---|
775 | class HelpWindow(wx.Frame): |
---|
776 | def __init__(self, parent, id, title): |
---|
777 | wx.Frame.__init__(self, parent, id, title, size=(570, 400)) |
---|
778 | |
---|
779 | from sans.models.CylinderModel import CylinderModel |
---|
780 | model = CylinderModel() |
---|
781 | #from sans.models.LineModel import LineModel |
---|
782 | #model = LineModel() |
---|
783 | from danse.common.plottools.plottables import Data1D |
---|
784 | data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1]) |
---|
785 | |
---|
786 | from pageInfo import PageInfo |
---|
787 | myinfo = PageInfo(self, model, data=data ) |
---|
788 | |
---|
789 | ## add data |
---|
790 | |
---|
791 | from models import ModelList |
---|
792 | mylist= ModelList() |
---|
793 | |
---|
794 | from sans.models.SphereModel import SphereModel |
---|
795 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
796 | from sans.models.DebyeModel import DebyeModel |
---|
797 | from sans.models.LineModel import LineModel |
---|
798 | name= "shapes" |
---|
799 | list1= [SphereModel] |
---|
800 | mylist.set_list( name, list1) |
---|
801 | |
---|
802 | name= "Shape-independent" |
---|
803 | list1= [DebyeModel] |
---|
804 | mylist.set_list( name, list1) |
---|
805 | |
---|
806 | name= "Structure Factors" |
---|
807 | list1= [SquareWellStructure] |
---|
808 | mylist.set_list( name, list1) |
---|
809 | |
---|
810 | name= "Added models" |
---|
811 | list1= [LineModel] |
---|
812 | mylist.set_list( name, list1) |
---|
813 | |
---|
814 | myinfo.model_list_box = mylist.get_list() |
---|
815 | |
---|
816 | self.page = FitPage(self, myinfo) |
---|
817 | |
---|
818 | |
---|
819 | |
---|
820 | self.Centre() |
---|
821 | self.Show(True) |
---|
822 | |
---|
823 | |
---|
824 | |
---|
825 | if __name__=="__main__": |
---|
826 | app = wx.App() |
---|
827 | HelpWindow(None, -1, 'HelpWindow') |
---|
828 | app.MainLoop() |
---|
829 | |
---|