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