1 | import sys |
---|
2 | import wx |
---|
3 | import wx.lib |
---|
4 | import numpy,math |
---|
5 | import copy |
---|
6 | |
---|
7 | from sans.guicomm.events import StatusEvent |
---|
8 | from sans.guiframe.utils import format_number |
---|
9 | from modelpage import ModelPage |
---|
10 | from modelpage import format_number |
---|
11 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
12 | _BOX_WIDTH = 80 |
---|
13 | |
---|
14 | |
---|
15 | #TODO: FitPage1D inherits from ModelPage but doesn't call its __init__ method! |
---|
16 | #TODO: refactor this class to have an __init__ that deals with data only, then calls a UI builder method. |
---|
17 | |
---|
18 | class FitPage1D(ModelPage): |
---|
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 | ## Internal name for the AUI manager |
---|
26 | window_name = "Fit page" |
---|
27 | ## Title to appear on top of the window |
---|
28 | window_caption = "Fit Page" |
---|
29 | |
---|
30 | name=None |
---|
31 | |
---|
32 | def __init__(self, parent,data, *args, **kwargs): |
---|
33 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
34 | |
---|
35 | """ |
---|
36 | Initialization of the Panel |
---|
37 | """ |
---|
38 | #TODO: remove this once the inheritence is cleaned up |
---|
39 | ## Data member to store the dispersion object created |
---|
40 | self._disp_obj_dict = {} |
---|
41 | |
---|
42 | #Data used for fitting |
---|
43 | self.data = data |
---|
44 | # flag to allow data2D plot |
---|
45 | self.enable2D=False |
---|
46 | #fit page manager |
---|
47 | self.manager = None |
---|
48 | #Store the parent of this panel parent |
---|
49 | # For this application fitpanel is the parent |
---|
50 | self.parent = parent |
---|
51 | # Event_owner is the owner of model event |
---|
52 | self.event_owner = None |
---|
53 | #panel interface |
---|
54 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
55 | |
---|
56 | self.sizer9 = wx.GridBagSizer(5,5) |
---|
57 | self.sizer8 = wx.GridBagSizer(5,5) |
---|
58 | self.sizer7 = wx.GridBagSizer(5,5) |
---|
59 | self.sizer6 = wx.GridBagSizer(5,5) |
---|
60 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
61 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
62 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
63 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
64 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
65 | # Add layer |
---|
66 | #data info layer |
---|
67 | self.vbox.Add(self.sizer1) |
---|
68 | #data range |
---|
69 | self.vbox.Add(self.sizer2) |
---|
70 | #instrument smearing selection layer |
---|
71 | self.vbox.Add(self.sizer3) |
---|
72 | #model selection |
---|
73 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
74 | self.vbox.Add(self.sizer4) |
---|
75 | #model paramaters layer |
---|
76 | self.vbox.Add(self.sizer5) |
---|
77 | #polydispersion selected |
---|
78 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
79 | self.vbox.Add(self.sizer6) |
---|
80 | #combox box for type of dispersion |
---|
81 | self.vbox.Add(self.sizer7) |
---|
82 | #dispersion parameters layer |
---|
83 | self.vbox.Add(self.sizer8) |
---|
84 | #fit info layer |
---|
85 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
86 | self.vbox.Add(self.sizer9) |
---|
87 | #---------sizer 1 draw-------------------------------- |
---|
88 | #Filling the sizer containing data related fields |
---|
89 | self.DataSource =wx.StaticText(self, -1,str(data.name)) |
---|
90 | ix = 0 |
---|
91 | iy = 1 |
---|
92 | self.sizer1.Add(wx.StaticText(self, -1, 'Data Source Name : '),(iy,ix),\ |
---|
93 | (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
94 | ix += 1 |
---|
95 | self.sizer1.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
96 | |
---|
97 | #---------sizer 2 draw-------------------------------- |
---|
98 | #set maximum range for x in linear scale |
---|
99 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
100 | ix = 0 |
---|
101 | iy = 1 |
---|
102 | # Minimum value of data |
---|
103 | self.data_min = wx.StaticText(self, -1,str(format_number(numpy.min(data.x)))) |
---|
104 | # Maximum value of data |
---|
105 | self.data_max = wx.StaticText(self, -1,str(format_number(numpy.max(data.x)))) |
---|
106 | self.text4_3 = wx.StaticText(self, -1, 'Maximum Data Range(Linear)', style=wx.ALIGN_LEFT) |
---|
107 | self.sizer2.Add(self.text4_3,(iy,ix),(1,1),\ |
---|
108 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
109 | ix += 2 |
---|
110 | self.sizer2.Add(wx.StaticText(self, -1, 'Min :'),(iy, ix),(1,1),\ |
---|
111 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
112 | ix += 1 |
---|
113 | self.sizer2.Add(self.data_min,(iy, ix),(1,1),\ |
---|
114 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
115 | ix += 1 |
---|
116 | self.sizer2.Add(wx.StaticText(self, -1, 'Max : '),(iy, ix),(1,1),\ |
---|
117 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
118 | ix += 1 |
---|
119 | self.sizer2.Add(self.data_max,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
120 | |
---|
121 | #----sizer 3 draw-------------------------------- |
---|
122 | #Filling the sizer containing instruments smearing info. |
---|
123 | self.disable_smearer = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
124 | self.enable_smearer = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
125 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
126 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
127 | ix = 0 |
---|
128 | iy = 1 |
---|
129 | self.sizer3.Add(wx.StaticText(self,-1,'Instrument Smearing'),(iy,ix),(1,1)\ |
---|
130 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
131 | ix += 1 |
---|
132 | self.sizer3.Add(self.enable_smearer,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
133 | ix += 1 |
---|
134 | self.sizer3.Add(self.disable_smearer,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
135 | ix =0 |
---|
136 | iy+=1 |
---|
137 | self.sizer3.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
138 | |
---|
139 | #------------------ sizer 4 draw------------------------ |
---|
140 | # This sizer contains model list and chisqr value |
---|
141 | #filling sizer4 |
---|
142 | self.modelbox = wx.ComboBox(self, -1) |
---|
143 | self.tcChi = wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT) |
---|
144 | self.tcChi.Hide() |
---|
145 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
146 | self.text1_1.Hide() |
---|
147 | ix = 0 |
---|
148 | iy = 1 |
---|
149 | self.sizer4.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
150 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
151 | ix += 1 |
---|
152 | self.sizer4.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
153 | ix += 1 |
---|
154 | self.sizer4.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
155 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
156 | ix += 1 |
---|
157 | self.sizer4.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
158 | #----------sizer6------------------------------------------------- |
---|
159 | #Sizer containing info on polydispersity |
---|
160 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
161 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
162 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.disable_disp.GetId()) |
---|
163 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.enable_disp.GetId()) |
---|
164 | ix= 0 |
---|
165 | iy=1 |
---|
166 | self.sizer6.Add(wx.StaticText(self,-1,'Polydispersity: '),(iy,ix),(1,1)\ |
---|
167 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
168 | ix += 1 |
---|
169 | self.sizer6.Add(self.enable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
170 | ix += 1 |
---|
171 | self.sizer6.Add(self.disable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
172 | ix =0 |
---|
173 | iy+=1 |
---|
174 | self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
175 | #---------sizer 9 draw---------------------------------------- |
---|
176 | |
---|
177 | |
---|
178 | id = wx.NewId() |
---|
179 | self.btFit =wx.Button(self,id,'Fit') |
---|
180 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
181 | self.btFit.SetToolTipString("Perform fit.") |
---|
182 | ## Q range |
---|
183 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
184 | if not hasattr(self.data,"data"): |
---|
185 | self.qmin_x= numpy.min(self.data.x) |
---|
186 | self.qmax_x= numpy.max(self.data.x) |
---|
187 | self.num_points= len(self.data.x) |
---|
188 | else: |
---|
189 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
190 | radius1= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymin, 2)) |
---|
191 | radius2= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymin, 2)) |
---|
192 | radius3= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymax, 2)) |
---|
193 | radius4= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymax, 2)) |
---|
194 | |
---|
195 | self.qmin_x= 0 |
---|
196 | self.qmax_x= math.sqrt(math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
197 | +math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2))#self.data.xmax |
---|
198 | |
---|
199 | self.num_points= 100 |
---|
200 | # The minimum fitting range |
---|
201 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
202 | self.qmin.SetValue(str(format_number(self.qmin_x))) |
---|
203 | self.qmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
204 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
205 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
206 | self.qmin.Enable() |
---|
207 | #The maximum fitting range |
---|
208 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
209 | self.qmax.SetValue(str(format_number(self.qmax_x))) |
---|
210 | self.qmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
211 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
212 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
213 | self.qmax.Enable() |
---|
214 | #The number of points to plots |
---|
215 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
216 | self.npts.SetValue(format_number(self.num_points)) |
---|
217 | self.npts.SetToolTipString("Number of point to plot.") |
---|
218 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
219 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
220 | self.npts.Disable() |
---|
221 | self.npts.Hide() |
---|
222 | ix = 0 |
---|
223 | iy = 1 |
---|
224 | self.sizer9.Add(wx.StaticText(self, -1, 'Fitting Range'),(iy, ix),(1,1),\ |
---|
225 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
226 | ix += 1 |
---|
227 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
228 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
229 | ix += 1 |
---|
230 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
231 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
232 | ix = 0 |
---|
233 | iy += 1 |
---|
234 | self.sizer9.Add(wx.StaticText(self, -1, 'Q range'),(iy, ix),(1,1),\ |
---|
235 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
236 | ix += 1 |
---|
237 | self.sizer9.Add(self.qmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
238 | ix += 1 |
---|
239 | self.sizer9.Add(self.qmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
240 | ix += 1 |
---|
241 | self.sizer9.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
242 | |
---|
243 | ix += 1 |
---|
244 | self.sizer9.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
245 | |
---|
246 | ix =0 |
---|
247 | iy+=1 |
---|
248 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
249 | |
---|
250 | # Contains link between model ,all its parameters, and panel organization |
---|
251 | self.parameters=[] |
---|
252 | # Contains list of parameters that cannot be fitted and reference to |
---|
253 | #panel objects |
---|
254 | self.fixed_param=[] |
---|
255 | # Contains list of parameters with dispersity and reference to |
---|
256 | #panel objects |
---|
257 | self.fittable_param=[] |
---|
258 | #list of dispersion paramaters |
---|
259 | self.disp_list=[] |
---|
260 | #contains link between a model and selected parameters to fit |
---|
261 | self.param_toFit=[] |
---|
262 | # model on which the fit would be performed |
---|
263 | self.model=None |
---|
264 | |
---|
265 | self.back_up_model= None |
---|
266 | #dictionary of model name and model class |
---|
267 | self.model_list_box={} |
---|
268 | |
---|
269 | if self.model == None: |
---|
270 | self.qmin.Disable() |
---|
271 | self.qmax.Disable() |
---|
272 | else: |
---|
273 | self.qmin.Enable() |
---|
274 | self.qmax.Enable() |
---|
275 | |
---|
276 | |
---|
277 | self.vbox.Layout() |
---|
278 | self.vbox.Fit(self) |
---|
279 | self.SetSizer(self.vbox) |
---|
280 | self.SetScrollbars(20,20,55,40) |
---|
281 | |
---|
282 | self.Centre() |
---|
283 | self.Layout() |
---|
284 | self.GrandParent.GetSizer().Layout() |
---|
285 | |
---|
286 | def compute_chisqr2D(self): |
---|
287 | """ |
---|
288 | compute chi square given a model and data 2D and set the value |
---|
289 | to the tcChi txtcrl |
---|
290 | """ |
---|
291 | flag=self.checkFitRange() |
---|
292 | res=[] |
---|
293 | if flag== True: |
---|
294 | try: |
---|
295 | self.qmin_x = float(self.qmin.GetValue()) |
---|
296 | self.qmax_x = float(self.qmax.GetValue()) |
---|
297 | for i in range(len(self.data.x_bins)): |
---|
298 | for j in range(len(self.data.y_bins)): |
---|
299 | #Check the range containing data between self.qmin_x and self.qmax_x |
---|
300 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)>=math.pow(self.qmin_x,2): |
---|
301 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)<=math.pow(self.qmax_x,2): |
---|
302 | chisqrji=(self.data.data[j][i]- self.model.runXY(\ |
---|
303 | [self.data.y_bins[j],self.data.x_bins[i]]))\ |
---|
304 | /self.data.err_data[j][i] |
---|
305 | #Vector containing residuals |
---|
306 | res.append( math.pow(chisqrji,2) ) |
---|
307 | # compute sum of residual |
---|
308 | sum=0 |
---|
309 | for item in res: |
---|
310 | if numpy.isfinite(item): |
---|
311 | sum +=item |
---|
312 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
313 | except: |
---|
314 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
315 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
316 | return |
---|
317 | |
---|
318 | |
---|
319 | def compute_chisqr(self): |
---|
320 | """ |
---|
321 | compute chi square given a model and data 1D and set the value |
---|
322 | to the tcChi txtcrl |
---|
323 | """ |
---|
324 | flag=self.checkFitRange() |
---|
325 | if flag== True: |
---|
326 | try: |
---|
327 | if hasattr(self.data,"data"): |
---|
328 | self.compute_chisqr2D() |
---|
329 | return |
---|
330 | else: |
---|
331 | self.qmin_x = float(self.qmin.GetValue()) |
---|
332 | self.qmax_x = float(self.qmax.GetValue()) |
---|
333 | # return residuals within self.qmin_x and self.qmax_x |
---|
334 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
335 | if self.qmin_x==None and self.qmax_x==None: |
---|
336 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
337 | temp=(y - fx)/dy |
---|
338 | res= temp*temp |
---|
339 | else: |
---|
340 | idx = (x>= self.qmin_x) & (x <=self.qmax_x) |
---|
341 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
342 | temp=(y[idx] - fx)/dy[idx] |
---|
343 | res= temp*temp |
---|
344 | #sum of residuals |
---|
345 | sum=0 |
---|
346 | for item in res: |
---|
347 | if numpy.isfinite(item): |
---|
348 | sum +=item |
---|
349 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
350 | except: |
---|
351 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
352 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
353 | return |
---|
354 | |
---|
355 | def _on_select_model(self,event): |
---|
356 | """ |
---|
357 | react when a model is selected from page's combo box |
---|
358 | post an event to its owner to draw an appropriate theory |
---|
359 | """ |
---|
360 | self.btFit.SetFocus() |
---|
361 | self.disable_disp.SetValue(True) |
---|
362 | self.sizer8.Clear(True) |
---|
363 | self.sizer7.Clear(True) |
---|
364 | self.vbox.Layout() |
---|
365 | self.SetScrollbars(20,20,55,40) |
---|
366 | self.Layout() |
---|
367 | self.parent.GetSizer().Layout() |
---|
368 | |
---|
369 | for item in self.model_list_box.itervalues(): |
---|
370 | name = item.__name__ |
---|
371 | if hasattr(item, "name"): |
---|
372 | name = item.name |
---|
373 | |
---|
374 | if name == None: |
---|
375 | self.qmin.Disable() |
---|
376 | self.qmax.Disable() |
---|
377 | else: |
---|
378 | self.qmin.Enable() |
---|
379 | self.qmax.Enable() |
---|
380 | |
---|
381 | if name ==event.GetString(): |
---|
382 | try: |
---|
383 | self.model=item() |
---|
384 | self.back_up_model= self.model.clone() |
---|
385 | evt = ModelEventbox(model=self.model,name=name) |
---|
386 | wx.PostEvent(self.event_owner, evt) |
---|
387 | self.text1_1.Show() |
---|
388 | self.compute_chisqr() |
---|
389 | self.tcChi.Show() |
---|
390 | except: |
---|
391 | raise #ValueError,"model.name is not equal to model class name" |
---|
392 | break |
---|
393 | |
---|
394 | |
---|
395 | def onFit(self,event): |
---|
396 | """ signal for fitting""" |
---|
397 | |
---|
398 | flag=self.checkFitRange() |
---|
399 | self.set_manager(self.manager) |
---|
400 | |
---|
401 | self.qmin_x=float(self.qmin.GetValue()) |
---|
402 | self.qmax_x =float( self.qmax.GetValue()) |
---|
403 | if len(self.param_toFit) >0 and flag==True: |
---|
404 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
405 | if hasattr(self.data, "data"): |
---|
406 | #single fit for Data2D |
---|
407 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, |
---|
408 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
409 | xmin=self.data.xmin,xmax=self.data.xmax) |
---|
410 | else: |
---|
411 | #single fit for Data1D |
---|
412 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) |
---|
413 | |
---|
414 | self.vbox.Layout() |
---|
415 | self.SetScrollbars(20,20,55,40) |
---|
416 | self.Layout() |
---|
417 | self.parent.GetSizer().Layout() |
---|
418 | else: |
---|
419 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
420 | "Select at least one parameter to fit ")) |
---|
421 | |
---|
422 | |
---|
423 | def _onTextEnter(self,event): |
---|
424 | """ |
---|
425 | set a flag to determine if the fitting range entered by the user is valid |
---|
426 | """ |
---|
427 | |
---|
428 | try: |
---|
429 | flag=self.checkFitRange() |
---|
430 | if flag==True and self.model!=None: |
---|
431 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
432 | ,float(self.xmax.GetValue())) |
---|
433 | except: |
---|
434 | |
---|
435 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
436 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
437 | |
---|
438 | |
---|
439 | |
---|
440 | def get_param_list(self): |
---|
441 | """ |
---|
442 | @return self.param_toFit: list containing references to TextCtrl |
---|
443 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
444 | @raise: if return an empty list of parameter fit will nnote work |
---|
445 | properly so raise ValueError,"missing parameter to fit" |
---|
446 | """ |
---|
447 | if self.param_toFit !=[]: |
---|
448 | return self.param_toFit |
---|
449 | else: |
---|
450 | raise ValueError,"missing parameter to fit" |
---|
451 | |
---|
452 | |
---|
453 | def _onparamEnter(self,event): |
---|
454 | """ |
---|
455 | when enter value on panel redraw model according to changed |
---|
456 | """ |
---|
457 | self.set_model() |
---|
458 | try: |
---|
459 | self.compute_chisqr() |
---|
460 | except: |
---|
461 | pass |
---|
462 | |
---|
463 | def set_model(self): |
---|
464 | """ |
---|
465 | Hide panel object related to the previous fit and set |
---|
466 | values entered by the used inside the model |
---|
467 | """ |
---|
468 | if len(self.parameters) !=0 and self.model !=None: |
---|
469 | # Flag to register when a parameter has changed. |
---|
470 | for item in self.parameters: |
---|
471 | try: |
---|
472 | self.text2_3.Hide() |
---|
473 | item[2].Hide() |
---|
474 | item[3].Clear() |
---|
475 | item[3].Hide() |
---|
476 | except: |
---|
477 | pass |
---|
478 | self.set_model_parameter() |
---|
479 | |
---|
480 | |
---|
481 | def select_all_param(self,event): |
---|
482 | """ |
---|
483 | set to true or false all checkBox given the main checkbox value cb1 |
---|
484 | """ |
---|
485 | self.select_all_param_helper() |
---|
486 | |
---|
487 | |
---|
488 | def select_param(self,event): |
---|
489 | """ |
---|
490 | Select TextCtrl checked for fitting purpose and stores them |
---|
491 | in self.param_toFit=[] list |
---|
492 | """ |
---|
493 | self.param_toFit=[] |
---|
494 | for item in self.parameters: |
---|
495 | #Select parameters to fit for list of primary parameters |
---|
496 | if item[0].GetValue()==True: |
---|
497 | list= [item[0],item[1],item[2],item[3]] |
---|
498 | if not (list in self.param_toFit): |
---|
499 | self.param_toFit.append(list ) |
---|
500 | else: |
---|
501 | #remove parameters from the fitting list |
---|
502 | if item in self.param_toFit: |
---|
503 | self.param_toFit.remove(item) |
---|
504 | #Select parameters to fit for list of fittable parameters with dispersion |
---|
505 | for item in self.fittable_param: |
---|
506 | if item[0].GetValue()==True: |
---|
507 | list= [item[0],item[1],item[2],item[3]] |
---|
508 | if not (list in self.param_toFit): |
---|
509 | self.param_toFit.append(list ) |
---|
510 | else: |
---|
511 | #remove parameters from the fitting list |
---|
512 | if item in self.param_toFit: |
---|
513 | self.param_toFit.remove(item) |
---|
514 | #Set the value of checkbox that selected every checkbox or not |
---|
515 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
516 | self.cb1.SetValue(True) |
---|
517 | else: |
---|
518 | self.cb1.SetValue(False) |
---|
519 | |
---|
520 | |
---|
521 | def onsetValues(self,chisqr, out,cov): |
---|
522 | """ |
---|
523 | Build the panel from the fit result |
---|
524 | @param chisqr:Value of the goodness of fit metric |
---|
525 | @param out:list of parameter with the best value found during fitting |
---|
526 | @param cov:Covariance matrix |
---|
527 | |
---|
528 | """ |
---|
529 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
530 | params = {} |
---|
531 | is_modified = False |
---|
532 | has_error = False |
---|
533 | #set the panel when fit result are float not list |
---|
534 | if out.__class__==numpy.float64: |
---|
535 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
536 | self.param_toFit[0][1].Refresh() |
---|
537 | if cov !=None : |
---|
538 | self.text2_3.Show() |
---|
539 | self.param_toFit[0][2].Show() |
---|
540 | self.param_toFit[0][3].Clear() |
---|
541 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
542 | self.param_toFit[0][3].Show() |
---|
543 | else: |
---|
544 | i=0 |
---|
545 | j=0 |
---|
546 | #Set the panel when fit result are list |
---|
547 | for item in self.param_toFit: |
---|
548 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
549 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
550 | item[1].Refresh() |
---|
551 | if(cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
552 | self.text2_3.Show() |
---|
553 | item[2].Show() |
---|
554 | item[3].Clear() |
---|
555 | for j in range(len(out)): |
---|
556 | if out[j]==self.model.getParam(item[0].GetLabelText()): |
---|
557 | break |
---|
558 | item[3].SetValue(format_number(cov[j])) |
---|
559 | item[3].Show() |
---|
560 | i+=1 |
---|
561 | |
---|
562 | self.vbox.Layout() |
---|
563 | self.SetScrollbars(20,20,55,40) |
---|
564 | self.Layout() |
---|
565 | self.GrandParent.GetSizer().Layout() |
---|
566 | |
---|
567 | |
---|
568 | def onSmear(self, event): |
---|
569 | """ |
---|
570 | Create a smear object that will change the way residuals |
---|
571 | are compute when fitting |
---|
572 | """ |
---|
573 | smear =None |
---|
574 | msg="" |
---|
575 | if self.enable_smearer.GetValue(): |
---|
576 | from DataLoader.qsmearing import smear_selection |
---|
577 | smear =smear_selection( self.data ) |
---|
578 | if hasattr(self.data,"dx"): |
---|
579 | msg= ": Resolution smearing parameters" |
---|
580 | if hasattr(self.data,"dxl"): |
---|
581 | msg= ": Slit_l smearing parameters" |
---|
582 | if hasattr(self.data,"dxw"): |
---|
583 | msg= ": Slit_w smearing parameters" |
---|
584 | if smear ==None: |
---|
585 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
586 | "Data contains no smearing information")) |
---|
587 | else: |
---|
588 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
589 | "Data contains smearing information %s"%msg)) |
---|
590 | self.manager.set_smearer(smear) |
---|
591 | |
---|
592 | |
---|
593 | |
---|
594 | |
---|