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 | |
---|
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 | else: |
---|
187 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
188 | radius1= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymin, 2)) |
---|
189 | radius2= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymin, 2)) |
---|
190 | radius3= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymax, 2)) |
---|
191 | radius4= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymax, 2)) |
---|
192 | self.qmin_x =0 |
---|
193 | self.qmax_x= max(radius1, radius2, radius3, radius4) |
---|
194 | #self.qmin_x= self.data.xmin |
---|
195 | #self.qmax_x= self.data.xmax |
---|
196 | print "data2D range",self.qmax_x |
---|
197 | |
---|
198 | self.num_points= 100 |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
203 | self.qmin.SetValue(str(format_number(self.qmin_x))) |
---|
204 | self.qmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
205 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
206 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
207 | self.qmin.Disable() |
---|
208 | |
---|
209 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
210 | self.qmax.SetValue(str(format_number(self.qmax_x))) |
---|
211 | self.qmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
212 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
213 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
214 | self.qmax.Disable() |
---|
215 | |
---|
216 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
217 | self.npts.SetValue(format_number(self.num_points)) |
---|
218 | self.npts.SetToolTipString("Number of point to plot.") |
---|
219 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
220 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
221 | self.npts.Disable() |
---|
222 | self.npts.Hide() |
---|
223 | ix = 0 |
---|
224 | iy = 1 |
---|
225 | self.sizer9.Add(wx.StaticText(self, -1, 'Fitting Range'),(iy, ix),(1,1),\ |
---|
226 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
227 | ix += 1 |
---|
228 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
229 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
230 | ix += 1 |
---|
231 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
232 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
233 | #ix += 1 |
---|
234 | #self.sizer9.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\ |
---|
235 | # wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
236 | ix = 0 |
---|
237 | iy += 1 |
---|
238 | self.sizer9.Add(wx.StaticText(self, -1, 'Q range'),(iy, ix),(1,1),\ |
---|
239 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
240 | ix += 1 |
---|
241 | self.sizer9.Add(self.qmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
242 | ix += 1 |
---|
243 | self.sizer9.Add(self.qmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
244 | ix += 1 |
---|
245 | self.sizer9.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
246 | |
---|
247 | ix += 1 |
---|
248 | self.sizer9.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
249 | ix =0 |
---|
250 | iy+=1 |
---|
251 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
252 | #----------sizer 10 draw------------------------------------------------------ |
---|
253 | id = wx.NewId() |
---|
254 | self.btClose =wx.Button(self,id,'Close') |
---|
255 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
256 | self.btClose.SetToolTipString("Close page.") |
---|
257 | |
---|
258 | ix= 3 |
---|
259 | iy= 1 |
---|
260 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
261 | ix +=1 |
---|
262 | self.sizer10.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
263 | ix =0 |
---|
264 | iy+=1 |
---|
265 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
266 | |
---|
267 | # contains link between model ,all its parameters, and panel organization |
---|
268 | self.parameters=[] |
---|
269 | self.fixed_param=[] |
---|
270 | self.fittable_param=[] |
---|
271 | #list of dispersion paramaters |
---|
272 | self.disp_list=[] |
---|
273 | #contains link between a model and selected parameters to fit |
---|
274 | self.param_toFit=[] |
---|
275 | # model on which the fit would be performed |
---|
276 | self.model=None |
---|
277 | #dictionary of model name and model class |
---|
278 | self.model_list_box={} |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | self.vbox.Layout() |
---|
283 | self.vbox.Fit(self) |
---|
284 | self.SetSizer(self.vbox) |
---|
285 | self.SetScrollbars(20,20,55,40) |
---|
286 | |
---|
287 | self.Centre() |
---|
288 | self.Layout() |
---|
289 | self.GrandParent.GetSizer().Layout() |
---|
290 | |
---|
291 | def compute_chisqr2D(self): |
---|
292 | """ @param fn: function that return model value |
---|
293 | @return residuals |
---|
294 | """ |
---|
295 | flag=self.checkFitRange() |
---|
296 | res=[] |
---|
297 | if flag== True: |
---|
298 | try: |
---|
299 | self.qmin_x = float(self.qmin.GetValue()) |
---|
300 | self.qmax_x = float(self.qmax.GetValue()) |
---|
301 | |
---|
302 | for i in range(len(self.data.x_bins)): |
---|
303 | if self.data.x_bins[i]>= self.qmin_x and self.data.x_bins[i]<= self.qmax_x: |
---|
304 | for j in range(len(self.data.y_bins)): |
---|
305 | if self.data.y_bins[j]>= self.qmin_x and self.data.y_bins[j]<= self.qmax_x: |
---|
306 | res.append( (self.data.data[j][i]- self.model.runXY(\ |
---|
307 | [self.data.x_bins[i],self.data.y_bins[j]]))\ |
---|
308 | /self.data.err_data[j][i] ) |
---|
309 | sum=0 |
---|
310 | |
---|
311 | for item in res: |
---|
312 | if numpy.isfinite(item): |
---|
313 | sum +=item |
---|
314 | #print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum |
---|
315 | #print res |
---|
316 | self.tcChi.SetLabel(format_number(math.fabs(sum))) |
---|
317 | except: |
---|
318 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
319 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
320 | |
---|
321 | |
---|
322 | def compute_chisqr(self): |
---|
323 | """ @param fn: function that return model value |
---|
324 | @return residuals |
---|
325 | """ |
---|
326 | |
---|
327 | flag=self.checkFitRange() |
---|
328 | print "flag", flag |
---|
329 | if flag== True: |
---|
330 | try: |
---|
331 | if hasattr(self.data,"data"): |
---|
332 | self.compute_chisqr2D() |
---|
333 | return |
---|
334 | else: |
---|
335 | self.qmin_x = float(self.qmin.GetValue()) |
---|
336 | self.qmax_x = float(self.qmax.GetValue()) |
---|
337 | print "self.qmin_x, self.qmax_x",self.qmin_x,self.qmax_x |
---|
338 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
339 | if self.qmin_x==None and self.qmax_x==None: |
---|
340 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
341 | res=(y - fx)/dy |
---|
342 | else: |
---|
343 | idx = (x>= self.qmin_x) & (x <=self.qmax_x) |
---|
344 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
345 | res= (y[idx] - fx)/dy[idx] |
---|
346 | |
---|
347 | sum=0 |
---|
348 | for item in res: |
---|
349 | if numpy.isfinite(item): |
---|
350 | sum +=item |
---|
351 | self.tcChi.SetLabel(format_number(math.fabs(sum))) |
---|
352 | except: |
---|
353 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
354 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
355 | |
---|
356 | def _on_select_model(self,event): |
---|
357 | """ |
---|
358 | react when a model is selected from page's combo box |
---|
359 | post an event to its owner to draw an appropriate theory |
---|
360 | """ |
---|
361 | self.btFit.SetFocus() |
---|
362 | self.disable_disp.SetValue(True) |
---|
363 | self.sizer8.Clear(True) |
---|
364 | self.sizer7.Clear(True) |
---|
365 | self.vbox.Layout() |
---|
366 | self.SetScrollbars(20,20,55,40) |
---|
367 | self.Layout() |
---|
368 | self.parent.GetSizer().Layout() |
---|
369 | for item in self.model_list_box.itervalues(): |
---|
370 | name = item.__name__ |
---|
371 | if hasattr(item, "name"): |
---|
372 | name = item.name |
---|
373 | #print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
374 | if name ==event.GetString(): |
---|
375 | try: |
---|
376 | self.model=item() |
---|
377 | evt = ModelEventbox(model=self.model,name=name) |
---|
378 | wx.PostEvent(self.event_owner, evt) |
---|
379 | self.text1_1.Show() |
---|
380 | self.compute_chisqr() |
---|
381 | self.tcChi.Show() |
---|
382 | except: |
---|
383 | raise #ValueError,"model.name is not equal to model class name" |
---|
384 | break |
---|
385 | def onFit(self,event): |
---|
386 | """ signal for fitting""" |
---|
387 | |
---|
388 | flag=self.checkFitRange() |
---|
389 | self.set_manager(self.manager) |
---|
390 | |
---|
391 | self.qmin_x=float(self.qmin.GetValue()) |
---|
392 | self.qmax_x =float( self.qmax.GetValue()) |
---|
393 | if len(self.param_toFit) >0 and flag==True: |
---|
394 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
395 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) |
---|
396 | else: |
---|
397 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
398 | "Select at least on parameter to fit ")) |
---|
399 | |
---|
400 | |
---|
401 | def _onTextEnter(self,event): |
---|
402 | """ |
---|
403 | set a flag to determine if the fitting range entered by the user is valid |
---|
404 | """ |
---|
405 | |
---|
406 | try: |
---|
407 | flag=self.checkFitRange() |
---|
408 | if flag==True and self.model!=None: |
---|
409 | #print"fit page",self.xmin.GetValue(),self.xmax.GetValue() |
---|
410 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
411 | ,float(self.xmax.GetValue())) |
---|
412 | except: |
---|
413 | |
---|
414 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
415 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
416 | |
---|
417 | |
---|
418 | |
---|
419 | def get_param_list(self): |
---|
420 | """ |
---|
421 | @return self.param_toFit: list containing references to TextCtrl |
---|
422 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
423 | @raise: if return an empty list of parameter fit will nnote work |
---|
424 | properly so raise ValueError,"missing parameter to fit" |
---|
425 | """ |
---|
426 | if self.param_toFit !=[]: |
---|
427 | return self.param_toFit |
---|
428 | else: |
---|
429 | raise ValueError,"missing parameter to fit" |
---|
430 | |
---|
431 | |
---|
432 | |
---|
433 | def old_set_panel(self,model): |
---|
434 | """ |
---|
435 | Build the panel from the model content |
---|
436 | @param model: the model selected in combo box for fitting purpose |
---|
437 | """ |
---|
438 | self.sizer5.Clear(True) |
---|
439 | self.parameters = [] |
---|
440 | self.param_toFit=[] |
---|
441 | self.model = model |
---|
442 | keys = self.model.getParamList() |
---|
443 | #print "fitpage1D : dispersion list",self.model.getDispParamList() |
---|
444 | keys.sort() |
---|
445 | ik=0 |
---|
446 | im=1 |
---|
447 | |
---|
448 | iy = 1 |
---|
449 | ix = 0 |
---|
450 | self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) |
---|
451 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
452 | self.sizer5.Add(self.cb1,(iy, ix),(1,1),\ |
---|
453 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
454 | ix +=1 |
---|
455 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
456 | self.sizer5.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
457 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
458 | ix +=2 |
---|
459 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
460 | self.sizer5.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
461 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
462 | self.text2_3.Hide() |
---|
463 | ix +=1 |
---|
464 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
465 | self.sizer5.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
466 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
467 | self.text2_4.Hide() |
---|
468 | disp_list=self.model.getDispParamList() |
---|
469 | for item in keys: |
---|
470 | if not item in disp_list: |
---|
471 | iy += 1 |
---|
472 | ix = 0 |
---|
473 | |
---|
474 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
475 | cb.SetValue(False) |
---|
476 | self.sizer5.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
477 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
478 | |
---|
479 | ix += 1 |
---|
480 | value= self.model.getParam(item) |
---|
481 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
482 | ctl1.SetValue(str (format_number(value))) |
---|
483 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
484 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
485 | self.sizer5.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
486 | |
---|
487 | ix += 1 |
---|
488 | text2=wx.StaticText(self, -1, '+/-') |
---|
489 | self.sizer5.Add(text2,(iy, ix),(1,1),\ |
---|
490 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
491 | text2.Hide() |
---|
492 | ix += 1 |
---|
493 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
494 | self.sizer5.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
495 | ctl2.Hide() |
---|
496 | ix +=1 |
---|
497 | # Units |
---|
498 | try: |
---|
499 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
500 | except: |
---|
501 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
502 | self.sizer5.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
503 | """ |
---|
504 | else: |
---|
505 | if not item in fixed: |
---|
506 | ip = 0 |
---|
507 | iq += 1 |
---|
508 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
509 | cb.SetValue(False) |
---|
510 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
511 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
512 | |
---|
513 | ip += 1 |
---|
514 | value= self.model.getParam(item) |
---|
515 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
516 | ctl1.SetValue(str (format_number(value))) |
---|
517 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
518 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
519 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
520 | else: |
---|
521 | ik = 0 |
---|
522 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
523 | self.sizer6.Add(text,( im, ik),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
524 | |
---|
525 | ik += 1 |
---|
526 | value= self.model.getParam(item) |
---|
527 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
528 | Tctl.SetValue(str (format_number(value))) |
---|
529 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
530 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
531 | self.sizer6.Add(Tctl, (im,ik),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
532 | self.fixed_param.append([item, Tctl]) |
---|
533 | im += 1 |
---|
534 | #save data |
---|
535 | """ |
---|
536 | self.parameters.append([cb,ctl1,text2,ctl2]) |
---|
537 | |
---|
538 | iy+=1 |
---|
539 | self.sizer5.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
540 | |
---|
541 | #Display units text on panel |
---|
542 | for item in keys: |
---|
543 | if self.model.details[item][0]!='': |
---|
544 | self.text2_4.Show() |
---|
545 | break |
---|
546 | else: |
---|
547 | self.text2_4.Hide() |
---|
548 | #Disable or enable fit button |
---|
549 | |
---|
550 | if not (len(self.param_toFit ) >0): |
---|
551 | self.xmin.Disable() |
---|
552 | self.xmax.Disable() |
---|
553 | else: |
---|
554 | self.xmin.Enable() |
---|
555 | self.xmax.Enable() |
---|
556 | |
---|
557 | self.compute_chisqr() |
---|
558 | self.vbox.Layout() |
---|
559 | self.SetScrollbars(20,20,55,40) |
---|
560 | self.Layout() |
---|
561 | self.GrandParent.GetSizer().Layout() |
---|
562 | |
---|
563 | |
---|
564 | |
---|
565 | def _onparamEnter(self,event): |
---|
566 | """ |
---|
567 | when enter value on panel redraw model according to changed |
---|
568 | """ |
---|
569 | self.set_model() |
---|
570 | self.compute_chisqr() |
---|
571 | |
---|
572 | def set_model(self): |
---|
573 | if len(self.parameters) !=0 and self.model !=None: |
---|
574 | # Flag to register when a parameter has changed. |
---|
575 | for item in self.parameters: |
---|
576 | try: |
---|
577 | self.text2_3.Hide() |
---|
578 | item[2].Hide() |
---|
579 | item[3].Clear() |
---|
580 | item[3].Hide() |
---|
581 | except: |
---|
582 | #enter dispersion value |
---|
583 | pass |
---|
584 | self.set_model_parameter() |
---|
585 | |
---|
586 | |
---|
587 | def select_all_param(self,event): |
---|
588 | """ |
---|
589 | set to true or false all checkBox given the main checkbox value cb1 |
---|
590 | """ |
---|
591 | self.select_all_param_helper() |
---|
592 | |
---|
593 | |
---|
594 | def select_param(self,event): |
---|
595 | """ |
---|
596 | Select TextCtrl checked for fitting purpose and stores them |
---|
597 | in self.param_toFit=[] list |
---|
598 | """ |
---|
599 | self.param_toFit=[] |
---|
600 | for item in self.parameters: |
---|
601 | if item[0].GetValue()==True: |
---|
602 | list= [item[0],item[1],item[2],item[3]] |
---|
603 | if not (list in self.param_toFit): |
---|
604 | self.param_toFit.append(list ) |
---|
605 | else: |
---|
606 | if item in self.param_toFit: |
---|
607 | self.param_toFit.remove(item) |
---|
608 | |
---|
609 | for item in self.fittable_param: |
---|
610 | if item[0].GetValue()==True: |
---|
611 | list= [item[0],item[1],item[2],item[3]] |
---|
612 | if not (list in self.param_toFit): |
---|
613 | self.param_toFit.append(list ) |
---|
614 | else: |
---|
615 | if item in self.param_toFit: |
---|
616 | self.param_toFit.remove(item) |
---|
617 | |
---|
618 | |
---|
619 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
620 | self.cb1.SetValue(True) |
---|
621 | else: |
---|
622 | self.cb1.SetValue(False) |
---|
623 | |
---|
624 | if not (len(self.param_toFit ) >0): |
---|
625 | self.qmin.Disable() |
---|
626 | self.qmax.Disable() |
---|
627 | else: |
---|
628 | self.qmin.Enable() |
---|
629 | self.qmax.Enable() |
---|
630 | |
---|
631 | |
---|
632 | |
---|
633 | |
---|
634 | def onsetValues(self,chisqr, out,cov): |
---|
635 | """ |
---|
636 | Build the panel from the fit result |
---|
637 | @param chisqr:Value of the goodness of fit metric |
---|
638 | @param out:list of parameter with the best value found during fitting |
---|
639 | @param cov:Covariance matrix |
---|
640 | |
---|
641 | """ |
---|
642 | #print "fitting : onsetvalues out",out |
---|
643 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
644 | params = {} |
---|
645 | is_modified = False |
---|
646 | has_error = False |
---|
647 | if out.__class__==numpy.float64: |
---|
648 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
649 | self.param_toFit[0][1].Refresh() |
---|
650 | if cov !=None : |
---|
651 | self.text2_3.Show() |
---|
652 | self.param_toFit[0][2].Show() |
---|
653 | self.param_toFit[0][3].Clear() |
---|
654 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
655 | self.param_toFit[0][3].Show() |
---|
656 | #out is a list : set parameters and errors in TextCtrl |
---|
657 | else: |
---|
658 | i=0 |
---|
659 | #print "fitpage: list param model",list |
---|
660 | #for item in self.param_toFit: |
---|
661 | # print "fitpage: list display",item[0].GetLabelText() |
---|
662 | for item in self.param_toFit: |
---|
663 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
664 | #item[1].SetValue(format_number(out[i])) |
---|
665 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
666 | item[1].Refresh() |
---|
667 | if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
668 | self.text2_3.Show() |
---|
669 | item[2].Show() |
---|
670 | item[3].Clear() |
---|
671 | item[3].SetValue(format_number(cov[i])) |
---|
672 | item[3].Show() |
---|
673 | i+=1 |
---|
674 | |
---|
675 | self.vbox.Layout() |
---|
676 | self.SetScrollbars(20,20,55,40) |
---|
677 | self.Layout() |
---|
678 | self.GrandParent.GetSizer().Layout() |
---|
679 | |
---|
680 | |
---|
681 | def onSmear(self, event): |
---|
682 | if event.GetString()=="None": |
---|
683 | self.manager.set_smearer(None) |
---|
684 | |
---|
685 | |
---|
686 | if event.GetString()=="Gaussian Resolution": |
---|
687 | from DataLoader.qsmearing import smear_selection |
---|
688 | smear =smear_selection( self.data ) |
---|
689 | self.manager.set_smearer(smear) |
---|
690 | # print "on smearing" |
---|
691 | |
---|