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