1 | import sys |
---|
2 | import wx |
---|
3 | import wx.lib |
---|
4 | import numpy,math |
---|
5 | import copy |
---|
6 | import sans.models.dispersion_models |
---|
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 | Initialization of the Panel |
---|
34 | """ |
---|
35 | self.data = data |
---|
36 | self.enable2D=False |
---|
37 | self.manager = None |
---|
38 | self.parent = parent |
---|
39 | self.event_owner = None |
---|
40 | #panel interface |
---|
41 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
42 | self.sizer10 = wx.GridBagSizer(5,5) |
---|
43 | self.sizer9 = wx.GridBagSizer(5,5) |
---|
44 | self.sizer8 = wx.GridBagSizer(5,5) |
---|
45 | self.sizer7 = wx.GridBagSizer(5,5) |
---|
46 | self.sizer6 = wx.GridBagSizer(5,5) |
---|
47 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
48 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
49 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
50 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
51 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
52 | # Add layer |
---|
53 | #data info layer |
---|
54 | self.vbox.Add(self.sizer1) |
---|
55 | #data range |
---|
56 | self.vbox.Add(self.sizer2) |
---|
57 | #instrument smearing selection layer |
---|
58 | self.vbox.Add(self.sizer3) |
---|
59 | #model selection |
---|
60 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
61 | self.vbox.Add(self.sizer4) |
---|
62 | #model paramaters layer |
---|
63 | self.vbox.Add(self.sizer5) |
---|
64 | #polydispersion selected |
---|
65 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
66 | self.vbox.Add(self.sizer6) |
---|
67 | #combox box for type of dispersion |
---|
68 | self.vbox.Add(self.sizer7) |
---|
69 | #dispersion parameters layer |
---|
70 | self.vbox.Add(self.sizer8) |
---|
71 | #fit info layer |
---|
72 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
73 | self.vbox.Add(self.sizer9) |
---|
74 | #close layer |
---|
75 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
76 | self.vbox.Add(self.sizer10) |
---|
77 | |
---|
78 | #---------sizer 1 draw-------------------------------- |
---|
79 | self.DataSource =wx.StaticText(self, -1,str(data.name)) |
---|
80 | self.smearer_box = wx.ComboBox(self, -1) |
---|
81 | wx.EVT_COMBOBOX( self.smearer_box,-1, self.onSmear ) |
---|
82 | self.smeares= sans.models.dispersion_models.models |
---|
83 | i=0 |
---|
84 | self.smearer_box.SetValue(str(None)) |
---|
85 | self.smearer_box.Insert(str(None),i) |
---|
86 | |
---|
87 | for k,v in self.smeares.iteritems(): |
---|
88 | if str(v)=="GaussianModel": |
---|
89 | self.smearer_box.Insert("Gaussian Resolution",i) |
---|
90 | else: |
---|
91 | self.smearer_box.Insert(str(v),i) |
---|
92 | i+=1 |
---|
93 | |
---|
94 | # Minimum value of data |
---|
95 | self.data_min = wx.StaticText(self, -1,str(format_number(numpy.min(data.x)))) |
---|
96 | # Maximum value of data |
---|
97 | self.data_max = wx.StaticText(self, -1,str(format_number(numpy.max(data.x)))) |
---|
98 | #Filing the sizer containing data related fields |
---|
99 | ix = 0 |
---|
100 | iy = 1 |
---|
101 | self.sizer1.Add(wx.StaticText(self, -1, 'Data Source Name : '),(iy,ix),\ |
---|
102 | (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
103 | |
---|
104 | ix += 1 |
---|
105 | self.sizer1.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
106 | |
---|
107 | #---------sizer 2 draw-------------------------------- |
---|
108 | ix = 0 |
---|
109 | iy = 1 |
---|
110 | #set maximum range for x in linear scale |
---|
111 | self.text4_3 = wx.StaticText(self, -1, 'Maximum Data Range(Linear)', style=wx.ALIGN_LEFT) |
---|
112 | self.sizer2.Add(self.text4_3,(iy,ix),(1,1),\ |
---|
113 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
114 | ix += 2 |
---|
115 | self.sizer2.Add(wx.StaticText(self, -1, 'Min :'),(iy, ix),(1,1),\ |
---|
116 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
117 | ix += 1 |
---|
118 | self.sizer2.Add(self.data_min,(iy, ix),(1,1),\ |
---|
119 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
120 | ix += 1 |
---|
121 | self.sizer2.Add(wx.StaticText(self, -1, 'Max : '),(iy, ix),(1,1),\ |
---|
122 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
123 | ix += 1 |
---|
124 | self.sizer2.Add(self.data_max,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
125 | |
---|
126 | #----sizer 3 draw-------------------------------- |
---|
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.smearer_box,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
133 | ix =0 |
---|
134 | iy+=1 |
---|
135 | self.sizer3.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
136 | |
---|
137 | #------------------ sizer 4 draw------------------------ |
---|
138 | self.modelbox = wx.ComboBox(self, -1) |
---|
139 | self.tcChi = wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT) |
---|
140 | self.tcChi.Hide() |
---|
141 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
142 | self.text1_1.Hide() |
---|
143 | #filling sizer2 |
---|
144 | ix = 0 |
---|
145 | iy = 1 |
---|
146 | self.sizer4.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
147 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
148 | ix += 1 |
---|
149 | self.sizer4.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
150 | ix += 1 |
---|
151 | self.sizer4.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
152 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
153 | ix += 1 |
---|
154 | self.sizer4.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
155 | #----------sizer6------------------------------------------------- |
---|
156 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
157 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
158 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.disable_disp.GetId()) |
---|
159 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.enable_disp.GetId()) |
---|
160 | ix= 0 |
---|
161 | iy=1 |
---|
162 | self.sizer6.Add(wx.StaticText(self,-1,'Polydispersity: '),(iy,ix),(1,1)\ |
---|
163 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
164 | ix += 1 |
---|
165 | self.sizer6.Add(self.enable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
166 | ix += 1 |
---|
167 | self.sizer6.Add(self.disable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
168 | ix =0 |
---|
169 | iy+=1 |
---|
170 | self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
171 | |
---|
172 | |
---|
173 | #---------sizer 9 draw---------------------------------------- |
---|
174 | |
---|
175 | |
---|
176 | id = wx.NewId() |
---|
177 | self.btFit =wx.Button(self,id,'Fit') |
---|
178 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
179 | self.btFit.SetToolTipString("Perform fit.") |
---|
180 | ## Q range |
---|
181 | #print "self.data fitpage1D" , self.data,hasattr(self.data,"data") |
---|
182 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
183 | if not hasattr(self.data,"data"): |
---|
184 | self.qmin_x= numpy.min(self.data.x) |
---|
185 | self.qmax_x= numpy.max(self.data.x) |
---|
186 | self.num_points= len(self.data.x) |
---|
187 | else: |
---|
188 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
189 | radius1= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymin, 2)) |
---|
190 | radius2= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymin, 2)) |
---|
191 | radius3= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymax, 2)) |
---|
192 | radius4= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymax, 2)) |
---|
193 | #self.qmin_x = 0 |
---|
194 | #self.qmax_x = max(radius1, radius2, radius3, radius4) |
---|
195 | self.qmin_x= 0#self.data.xmin |
---|
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 | print "data2D range",self.qmax_x |
---|
199 | |
---|
200 | self.num_points= 100 |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
205 | self.qmin.SetValue(str(format_number(self.qmin_x))) |
---|
206 | self.qmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
207 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
208 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
209 | self.qmin.Enable() |
---|
210 | |
---|
211 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
212 | self.qmax.SetValue(str(format_number(self.qmax_x))) |
---|
213 | self.qmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
214 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
215 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
216 | self.qmax.Enable() |
---|
217 | |
---|
218 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
219 | self.npts.SetValue(format_number(self.num_points)) |
---|
220 | self.npts.SetToolTipString("Number of point to plot.") |
---|
221 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
222 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
223 | self.npts.Disable() |
---|
224 | self.npts.Hide() |
---|
225 | ix = 0 |
---|
226 | iy = 1 |
---|
227 | self.sizer9.Add(wx.StaticText(self, -1, 'Fitting Range'),(iy, ix),(1,1),\ |
---|
228 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
229 | ix += 1 |
---|
230 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
231 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
232 | ix += 1 |
---|
233 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
234 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
235 | #ix += 1 |
---|
236 | #self.sizer9.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\ |
---|
237 | # wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
238 | ix = 0 |
---|
239 | iy += 1 |
---|
240 | self.sizer9.Add(wx.StaticText(self, -1, 'Qx range'),(iy, ix),(1,1),\ |
---|
241 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
242 | ix += 1 |
---|
243 | self.sizer9.Add(self.qmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
244 | ix += 1 |
---|
245 | self.sizer9.Add(self.qmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
246 | ix += 1 |
---|
247 | self.sizer9.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
248 | |
---|
249 | ix += 1 |
---|
250 | self.sizer9.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
251 | ix =0 |
---|
252 | iy+=1 |
---|
253 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
254 | #----------sizer 10 draw------------------------------------------------------ |
---|
255 | id = wx.NewId() |
---|
256 | self.btClose =wx.Button(self,id,'Close') |
---|
257 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
258 | self.btClose.SetToolTipString("Close page.") |
---|
259 | |
---|
260 | ix= 3 |
---|
261 | iy= 1 |
---|
262 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
263 | ix +=1 |
---|
264 | self.sizer10.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
265 | ix =0 |
---|
266 | iy+=1 |
---|
267 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
268 | |
---|
269 | # contains link between model ,all its parameters, and panel organization |
---|
270 | self.parameters=[] |
---|
271 | self.fixed_param=[] |
---|
272 | self.fittable_param=[] |
---|
273 | #list of dispersion paramaters |
---|
274 | self.disp_list=[] |
---|
275 | #contains link between a model and selected parameters to fit |
---|
276 | self.param_toFit=[] |
---|
277 | # model on which the fit would be performed |
---|
278 | self.model=None |
---|
279 | #dictionary of model name and model class |
---|
280 | self.model_list_box={} |
---|
281 | |
---|
282 | if self.model == None: |
---|
283 | self.qmin.Disable() |
---|
284 | self.qmax.Disable() |
---|
285 | else: |
---|
286 | self.qmin.Enable() |
---|
287 | self.qmax.Enable() |
---|
288 | |
---|
289 | |
---|
290 | self.vbox.Layout() |
---|
291 | self.vbox.Fit(self) |
---|
292 | self.SetSizer(self.vbox) |
---|
293 | self.SetScrollbars(20,20,55,40) |
---|
294 | |
---|
295 | self.Centre() |
---|
296 | self.Layout() |
---|
297 | self.GrandParent.GetSizer().Layout() |
---|
298 | |
---|
299 | def compute_chisqr2D(self): |
---|
300 | """ @param fn: function that return model value |
---|
301 | @return residuals |
---|
302 | """ |
---|
303 | flag=self.checkFitRange() |
---|
304 | res=[] |
---|
305 | if flag== True: |
---|
306 | try: |
---|
307 | print "compute",self.data.err_data |
---|
308 | self.qmin_x = float(self.qmin.GetValue()) |
---|
309 | self.qmax_x = float(self.qmax.GetValue()) |
---|
310 | |
---|
311 | for i in range(len(self.data.x_bins)): |
---|
312 | if self.data.x_bins[i]>= self.qmin_x and self.data.x_bins[i]<= self.qmax_x: |
---|
313 | for j in range(len(self.data.y_bins)): |
---|
314 | if self.data.y_bins[j]>= self.qmin_x and self.data.y_bins[j]<= self.qmax_x: |
---|
315 | res.append( (self.data.data[j][i]- self.model.runXY(\ |
---|
316 | [self.data.x_bins[i],self.data.y_bins[j]]))\ |
---|
317 | /self.data.err_data[j][i] ) |
---|
318 | sum=0 |
---|
319 | |
---|
320 | for item in res: |
---|
321 | if numpy.isfinite(item): |
---|
322 | sum +=item |
---|
323 | #print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum |
---|
324 | #print res |
---|
325 | self.tcChi.SetLabel(format_number(math.fabs(sum))) |
---|
326 | except: |
---|
327 | raise |
---|
328 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
329 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
330 | |
---|
331 | |
---|
332 | def compute_chisqr(self): |
---|
333 | """ @param fn: function that return model value |
---|
334 | @return residuals |
---|
335 | """ |
---|
336 | |
---|
337 | flag=self.checkFitRange() |
---|
338 | print "flag", flag |
---|
339 | if flag== True: |
---|
340 | try: |
---|
341 | if hasattr(self.data,"data"): |
---|
342 | self.compute_chisqr2D() |
---|
343 | return |
---|
344 | else: |
---|
345 | self.qmin_x = float(self.qmin.GetValue()) |
---|
346 | self.qmax_x = float(self.qmax.GetValue()) |
---|
347 | #print "self.qmin_x, self.qmax_x",self.qmin_x,self.qmax_x |
---|
348 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
349 | if self.qmin_x==None and self.qmax_x==None: |
---|
350 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
351 | res=(y - fx)/dy |
---|
352 | else: |
---|
353 | idx = (x>= self.qmin_x) & (x <=self.qmax_x) |
---|
354 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
355 | res= (y[idx] - fx)/dy[idx] |
---|
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))) |
---|
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 | evt = ModelEventbox(model=self.model,name=name) |
---|
396 | wx.PostEvent(self.event_owner, evt) |
---|
397 | self.text1_1.Show() |
---|
398 | self.compute_chisqr() |
---|
399 | self.tcChi.Show() |
---|
400 | except: |
---|
401 | raise #ValueError,"model.name is not equal to model class name" |
---|
402 | break |
---|
403 | def onFit(self,event): |
---|
404 | """ signal for fitting""" |
---|
405 | |
---|
406 | flag=self.checkFitRange() |
---|
407 | self.set_manager(self.manager) |
---|
408 | |
---|
409 | self.qmin_x=float(self.qmin.GetValue()) |
---|
410 | self.qmax_x =float( self.qmax.GetValue()) |
---|
411 | if len(self.param_toFit) >0 and flag==True: |
---|
412 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
413 | if hasattr(self.data, "data"): |
---|
414 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, |
---|
415 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
416 | xmin=self.data.xmin,xmax=self.data.xmax) |
---|
417 | #self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, |
---|
418 | #ymin=self.data.ymin, ymax=self.data.ymax) |
---|
419 | else: |
---|
420 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) |
---|
421 | else: |
---|
422 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
423 | "Select at least on parameter to fit ")) |
---|
424 | |
---|
425 | |
---|
426 | def _onTextEnter(self,event): |
---|
427 | """ |
---|
428 | set a flag to determine if the fitting range entered by the user is valid |
---|
429 | """ |
---|
430 | |
---|
431 | try: |
---|
432 | flag=self.checkFitRange() |
---|
433 | if flag==True and self.model!=None: |
---|
434 | #print"fit page",self.xmin.GetValue(),self.xmax.GetValue() |
---|
435 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
436 | ,float(self.xmax.GetValue())) |
---|
437 | except: |
---|
438 | |
---|
439 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
440 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
441 | |
---|
442 | |
---|
443 | |
---|
444 | def get_param_list(self): |
---|
445 | """ |
---|
446 | @return self.param_toFit: list containing references to TextCtrl |
---|
447 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
448 | @raise: if return an empty list of parameter fit will nnote work |
---|
449 | properly so raise ValueError,"missing parameter to fit" |
---|
450 | """ |
---|
451 | if self.param_toFit !=[]: |
---|
452 | return self.param_toFit |
---|
453 | else: |
---|
454 | raise ValueError,"missing parameter to fit" |
---|
455 | |
---|
456 | |
---|
457 | |
---|
458 | def old_set_panel(self,model): |
---|
459 | """ |
---|
460 | Build the panel from the model content |
---|
461 | @param model: the model selected in combo box for fitting purpose |
---|
462 | """ |
---|
463 | self.sizer5.Clear(True) |
---|
464 | self.parameters = [] |
---|
465 | self.param_toFit=[] |
---|
466 | self.model = model |
---|
467 | keys = self.model.getParamList() |
---|
468 | #print "fitpage1D : dispersion list",self.model.getDispParamList() |
---|
469 | keys.sort() |
---|
470 | ik=0 |
---|
471 | im=1 |
---|
472 | |
---|
473 | iy = 1 |
---|
474 | ix = 0 |
---|
475 | self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) |
---|
476 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
477 | self.sizer5.Add(self.cb1,(iy, ix),(1,1),\ |
---|
478 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
479 | ix +=1 |
---|
480 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
481 | self.sizer5.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
482 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
483 | ix +=2 |
---|
484 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
485 | self.sizer5.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
486 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
487 | self.text2_3.Hide() |
---|
488 | ix +=1 |
---|
489 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
490 | self.sizer5.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
491 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
492 | self.text2_4.Hide() |
---|
493 | disp_list=self.model.getDispParamList() |
---|
494 | for item in keys: |
---|
495 | if not item in disp_list: |
---|
496 | iy += 1 |
---|
497 | ix = 0 |
---|
498 | |
---|
499 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
500 | cb.SetValue(False) |
---|
501 | self.sizer5.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
502 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
503 | |
---|
504 | ix += 1 |
---|
505 | value= self.model.getParam(item) |
---|
506 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
507 | ctl1.SetValue(str (format_number(value))) |
---|
508 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
509 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
510 | self.sizer5.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
511 | |
---|
512 | ix += 1 |
---|
513 | text2=wx.StaticText(self, -1, '+/-') |
---|
514 | self.sizer5.Add(text2,(iy, ix),(1,1),\ |
---|
515 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
516 | text2.Hide() |
---|
517 | ix += 1 |
---|
518 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
519 | self.sizer5.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
520 | ctl2.Hide() |
---|
521 | ix +=1 |
---|
522 | # Units |
---|
523 | try: |
---|
524 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
525 | except: |
---|
526 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
527 | self.sizer5.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
528 | """ |
---|
529 | else: |
---|
530 | if not item in fixed: |
---|
531 | ip = 0 |
---|
532 | iq += 1 |
---|
533 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
534 | cb.SetValue(False) |
---|
535 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
536 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
537 | |
---|
538 | ip += 1 |
---|
539 | value= self.model.getParam(item) |
---|
540 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
541 | ctl1.SetValue(str (format_number(value))) |
---|
542 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
543 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
544 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
545 | else: |
---|
546 | ik = 0 |
---|
547 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
548 | self.sizer6.Add(text,( im, ik),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
549 | |
---|
550 | ik += 1 |
---|
551 | value= self.model.getParam(item) |
---|
552 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
553 | Tctl.SetValue(str (format_number(value))) |
---|
554 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
555 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
556 | self.sizer6.Add(Tctl, (im,ik),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
557 | self.fixed_param.append([item, Tctl]) |
---|
558 | im += 1 |
---|
559 | #save data |
---|
560 | """ |
---|
561 | self.parameters.append([cb,ctl1,text2,ctl2]) |
---|
562 | |
---|
563 | iy+=1 |
---|
564 | self.sizer5.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
565 | |
---|
566 | #Display units text on panel |
---|
567 | for item in keys: |
---|
568 | if self.model.details[item][0]!='': |
---|
569 | self.text2_4.Show() |
---|
570 | break |
---|
571 | else: |
---|
572 | self.text2_4.Hide() |
---|
573 | |
---|
574 | #(Disable or )Enable fit button |
---|
575 | """ |
---|
576 | if not (len(self.param_toFit ) >0): |
---|
577 | self.xmin.Enable() |
---|
578 | self.xmax.Enable() |
---|
579 | else: |
---|
580 | self.xmin.Enable() |
---|
581 | self.xmax.Enable() |
---|
582 | """ |
---|
583 | |
---|
584 | self.compute_chisqr() |
---|
585 | self.vbox.Layout() |
---|
586 | self.SetScrollbars(20,20,55,40) |
---|
587 | self.Layout() |
---|
588 | self.GrandParent.GetSizer().Layout() |
---|
589 | |
---|
590 | |
---|
591 | |
---|
592 | def _onparamEnter(self,event): |
---|
593 | """ |
---|
594 | when enter value on panel redraw model according to changed |
---|
595 | """ |
---|
596 | self.set_model() |
---|
597 | self.compute_chisqr() |
---|
598 | |
---|
599 | def set_model(self): |
---|
600 | if len(self.parameters) !=0 and self.model !=None: |
---|
601 | # Flag to register when a parameter has changed. |
---|
602 | for item in self.parameters: |
---|
603 | try: |
---|
604 | self.text2_3.Hide() |
---|
605 | item[2].Hide() |
---|
606 | item[3].Clear() |
---|
607 | item[3].Hide() |
---|
608 | except: |
---|
609 | #enter dispersion value |
---|
610 | pass |
---|
611 | self.set_model_parameter() |
---|
612 | |
---|
613 | |
---|
614 | def select_all_param(self,event): |
---|
615 | """ |
---|
616 | set to true or false all checkBox given the main checkbox value cb1 |
---|
617 | """ |
---|
618 | self.select_all_param_helper() |
---|
619 | |
---|
620 | |
---|
621 | def select_param(self,event): |
---|
622 | """ |
---|
623 | Select TextCtrl checked for fitting purpose and stores them |
---|
624 | in self.param_toFit=[] list |
---|
625 | """ |
---|
626 | self.param_toFit=[] |
---|
627 | for item in self.parameters: |
---|
628 | if item[0].GetValue()==True: |
---|
629 | list= [item[0],item[1],item[2],item[3]] |
---|
630 | if not (list in self.param_toFit): |
---|
631 | self.param_toFit.append(list ) |
---|
632 | else: |
---|
633 | if item in self.param_toFit: |
---|
634 | self.param_toFit.remove(item) |
---|
635 | |
---|
636 | for item in self.fittable_param: |
---|
637 | if item[0].GetValue()==True: |
---|
638 | list= [item[0],item[1],item[2],item[3]] |
---|
639 | if not (list in self.param_toFit): |
---|
640 | self.param_toFit.append(list ) |
---|
641 | else: |
---|
642 | if item in self.param_toFit: |
---|
643 | self.param_toFit.remove(item) |
---|
644 | |
---|
645 | |
---|
646 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
647 | self.cb1.SetValue(True) |
---|
648 | else: |
---|
649 | self.cb1.SetValue(False) |
---|
650 | """ |
---|
651 | #qmax, qmin Input enabled all the time |
---|
652 | self.qmin.Enable() |
---|
653 | self.qmax.Enable() |
---|
654 | """ |
---|
655 | |
---|
656 | def onsetValues(self,chisqr, out,cov): |
---|
657 | """ |
---|
658 | Build the panel from the fit result |
---|
659 | @param chisqr:Value of the goodness of fit metric |
---|
660 | @param out:list of parameter with the best value found during fitting |
---|
661 | @param cov:Covariance matrix |
---|
662 | |
---|
663 | """ |
---|
664 | #print "fitting : onsetvalues out",out |
---|
665 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
666 | params = {} |
---|
667 | is_modified = False |
---|
668 | has_error = False |
---|
669 | if out.__class__==numpy.float64: |
---|
670 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
671 | self.param_toFit[0][1].Refresh() |
---|
672 | if cov !=None : |
---|
673 | self.text2_3.Show() |
---|
674 | self.param_toFit[0][2].Show() |
---|
675 | self.param_toFit[0][3].Clear() |
---|
676 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
677 | self.param_toFit[0][3].Show() |
---|
678 | #out is a list : set parameters and errors in TextCtrl |
---|
679 | else: |
---|
680 | i=0 |
---|
681 | #print "fitpage: list param model",list |
---|
682 | #for item in self.param_toFit: |
---|
683 | # print "fitpage: list display",item[0].GetLabelText() |
---|
684 | for item in self.param_toFit: |
---|
685 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
686 | #item[1].SetValue(format_number(out[i])) |
---|
687 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
688 | item[1].Refresh() |
---|
689 | if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
690 | self.text2_3.Show() |
---|
691 | item[2].Show() |
---|
692 | item[3].Clear() |
---|
693 | item[3].SetValue(format_number(cov[i])) |
---|
694 | item[3].Show() |
---|
695 | i+=1 |
---|
696 | |
---|
697 | self.vbox.Layout() |
---|
698 | self.SetScrollbars(20,20,55,40) |
---|
699 | self.Layout() |
---|
700 | self.GrandParent.GetSizer().Layout() |
---|
701 | |
---|
702 | |
---|
703 | def onSmear(self, event): |
---|
704 | if event.GetString()=="None": |
---|
705 | self.manager.set_smearer(None) |
---|
706 | |
---|
707 | |
---|
708 | if event.GetString()=="Gaussian Resolution": |
---|
709 | from DataLoader.qsmearing import smear_selection |
---|
710 | smear =smear_selection( self.data ) |
---|
711 | self.manager.set_smearer(smear) |
---|
712 | # print "on smearing" |
---|
713 | |
---|