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