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