1 | import sys |
---|
2 | import wx |
---|
3 | import wx.lib |
---|
4 | import numpy |
---|
5 | import copy |
---|
6 | |
---|
7 | |
---|
8 | from sans.guicomm.events import StatusEvent |
---|
9 | from sans.guiframe.utils import format_number |
---|
10 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
11 | _BOX_WIDTH = 80 |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | class ModelPage(wx.ScrolledWindow): |
---|
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="" |
---|
29 | def __init__(self, parent,model,name, *args, **kwargs): |
---|
30 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
31 | """ |
---|
32 | Initialization of the Panel |
---|
33 | """ |
---|
34 | # model on which the fit would be performed |
---|
35 | self.model=model |
---|
36 | #list of dispersion paramaters |
---|
37 | self.disp_list=[] |
---|
38 | try: |
---|
39 | self.disp_list=self.model.getDispParamList() |
---|
40 | except: |
---|
41 | pass |
---|
42 | self.manager = None |
---|
43 | self.parent = parent |
---|
44 | self.event_owner = None |
---|
45 | # this panel does contain data .existing data allow a different drawing |
---|
46 | #on set_model parameters |
---|
47 | self.data=None |
---|
48 | #panel interface |
---|
49 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
50 | self.sizer11 = wx.BoxSizer(wx.HORIZONTAL) |
---|
51 | self.sizer10 = wx.GridBagSizer(5,5) |
---|
52 | self.sizer9 = wx.GridBagSizer(5,5) |
---|
53 | self.sizer8 = wx.GridBagSizer(5,5) |
---|
54 | self.sizer7 = wx.GridBagSizer(5,5) |
---|
55 | self.sizer6 = wx.GridBagSizer(5,5) |
---|
56 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
57 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
58 | |
---|
59 | #model selection |
---|
60 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
61 | self.vbox.Add(self.sizer4) |
---|
62 | #model description |
---|
63 | self.vbox.Add(self.sizer11) |
---|
64 | #model paramaters layer |
---|
65 | self.vbox.Add(self.sizer5) |
---|
66 | #polydispersion selected |
---|
67 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
68 | self.vbox.Add(self.sizer6) |
---|
69 | #combox box for type of dispersion |
---|
70 | self.vbox.Add(self.sizer7) |
---|
71 | #dispersion parameters layer |
---|
72 | self.vbox.Add(self.sizer8) |
---|
73 | # plotting range |
---|
74 | self.vbox.Add(self.sizer9) |
---|
75 | #close layer |
---|
76 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
77 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
78 | self.vbox.Add(self.sizer10) |
---|
79 | |
---|
80 | |
---|
81 | #------------------ sizer 4 draw------------------------ |
---|
82 | |
---|
83 | |
---|
84 | # define combox box |
---|
85 | self.modelbox = wx.ComboBox(self, -1) |
---|
86 | # preview selected model name |
---|
87 | self.prevmodel_name=name |
---|
88 | #print "model view prev_model",name |
---|
89 | self.modelbox.SetValue(self.prevmodel_name) |
---|
90 | #enable model 2D draw |
---|
91 | self.enable2D= False |
---|
92 | #filling sizer2 |
---|
93 | ix = 0 |
---|
94 | iy = 1 |
---|
95 | self.sizer4.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
96 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
97 | ix += 1 |
---|
98 | self.sizer4.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
99 | ix += 1 |
---|
100 | id = wx.NewId() |
---|
101 | self.model_view =wx.Button(self,id,'View 2D') |
---|
102 | self.model_view.Bind(wx.EVT_BUTTON, self.onModel2D,id=id) |
---|
103 | self.model_view.SetToolTipString("View model in 2D") |
---|
104 | |
---|
105 | self.sizer4.Add(self.model_view,(iy,ix),(1,1),\ |
---|
106 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
107 | |
---|
108 | self.model_view.Enable() |
---|
109 | self.model_view.SetFocus() |
---|
110 | |
---|
111 | ix = 0 |
---|
112 | iy += 1 |
---|
113 | self.sizer4.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
114 | |
---|
115 | #----------sizer6------------------------------------------------- |
---|
116 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
117 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
118 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.disable_disp.GetId()) |
---|
119 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.enable_disp.GetId()) |
---|
120 | ix= 0 |
---|
121 | iy=1 |
---|
122 | self.sizer6.Add(wx.StaticText(self,-1,'Polydispersity: '),(iy,ix),(1,1)\ |
---|
123 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
124 | ix += 1 |
---|
125 | self.sizer6.Add(self.enable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
126 | ix += 1 |
---|
127 | self.sizer6.Add(self.disable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
128 | ix =0 |
---|
129 | iy+=1 |
---|
130 | self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
131 | |
---|
132 | |
---|
133 | #---------sizer 9 draw---------------------------------------- |
---|
134 | |
---|
135 | ## Q range |
---|
136 | self.qmin_x= 0.001 |
---|
137 | self.qmax_x= 0.1 |
---|
138 | self.num_points= 100 |
---|
139 | |
---|
140 | |
---|
141 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
142 | self.qmin.SetValue(format_number(self.qmin_x)) |
---|
143 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
144 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
145 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
146 | |
---|
147 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
148 | self.qmax.SetValue(format_number(self.qmax_x)) |
---|
149 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
150 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
151 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
152 | |
---|
153 | |
---|
154 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
155 | self.npts.SetValue(format_number(self.num_points)) |
---|
156 | self.npts.SetToolTipString("Number of point to plot.") |
---|
157 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
158 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
159 | |
---|
160 | ix = 0 |
---|
161 | iy = 1 |
---|
162 | self.sizer9.Add(wx.StaticText(self, -1, 'Plotting Range'),(iy, ix),(1,1),\ |
---|
163 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
164 | ix += 1 |
---|
165 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
166 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
167 | ix += 1 |
---|
168 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
169 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
170 | ix += 1 |
---|
171 | self.sizer9.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\ |
---|
172 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
173 | ix = 0 |
---|
174 | iy += 1 |
---|
175 | self.sizer9.Add(wx.StaticText(self, -1, 'Q range'),(iy, ix),(1,1),\ |
---|
176 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
177 | ix += 1 |
---|
178 | self.sizer9.Add(self.qmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
179 | ix += 1 |
---|
180 | self.sizer9.Add(self.qmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
181 | ix += 1 |
---|
182 | self.sizer9.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
183 | |
---|
184 | ix =0 |
---|
185 | iy+=1 |
---|
186 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
187 | #----------sizer 10 draw------------------------------------------------------ |
---|
188 | id = wx.NewId() |
---|
189 | self.btClose =wx.Button(self,id,'Close') |
---|
190 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
191 | self.btClose.SetToolTipString("Close page.") |
---|
192 | |
---|
193 | ix= 3 |
---|
194 | iy= 1 |
---|
195 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
196 | ix +=1 |
---|
197 | self.sizer10.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
198 | ix =0 |
---|
199 | iy+=1 |
---|
200 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
201 | |
---|
202 | # contains link between model ,all its parameters, and panel organization |
---|
203 | self.parameters=[] |
---|
204 | self.fixed_param=[] |
---|
205 | self.fittable_param=[] |
---|
206 | self.polydisp= {} |
---|
207 | #contains link between a model and selected parameters to fit |
---|
208 | self.param_toFit=[] |
---|
209 | |
---|
210 | #dictionary of model name and model class |
---|
211 | self.model_list_box={} |
---|
212 | #Draw initial panel |
---|
213 | #-----sizer 11--------------------model description------ |
---|
214 | if self.model!=None: |
---|
215 | self.set_panel(self.model) |
---|
216 | |
---|
217 | |
---|
218 | |
---|
219 | self.vbox.Layout() |
---|
220 | self.vbox.Fit(self) |
---|
221 | self.SetSizer(self.vbox) |
---|
222 | self.SetScrollbars(20,20,55,40) |
---|
223 | |
---|
224 | self.Centre() |
---|
225 | self.Layout() |
---|
226 | self.parent.GetSizer().Layout() |
---|
227 | def set_model_description(self, model): |
---|
228 | |
---|
229 | if model !=None and str(model.description)!=""and self.data==None: |
---|
230 | self.sizer11.Clear(True) |
---|
231 | self.box_description= wx.StaticBox(self, -1, 'Model Description') |
---|
232 | boxsizer1 = wx.StaticBoxSizer(self.box_description, wx.VERTICAL) |
---|
233 | boxsizer1.SetMinSize((320,20)) |
---|
234 | self.description = wx.StaticText(self,-1,str(model.description)) |
---|
235 | boxsizer1.Add(self.description, 0, wx.EXPAND) |
---|
236 | self.sizer11.Add(boxsizer1,1, wx.EXPAND | wx.ALL, 2) |
---|
237 | |
---|
238 | |
---|
239 | def set_owner(self,owner): |
---|
240 | """ |
---|
241 | set owner of fitpage |
---|
242 | @param owner: the class responsible of plotting |
---|
243 | """ |
---|
244 | self.event_owner=owner |
---|
245 | |
---|
246 | |
---|
247 | def set_manager(self, manager): |
---|
248 | """ |
---|
249 | set panel manager |
---|
250 | @param manager: instance of plugin fitting |
---|
251 | """ |
---|
252 | self.manager = manager |
---|
253 | |
---|
254 | def populate_box(self, dict): |
---|
255 | """ |
---|
256 | Populate each combox box of each page |
---|
257 | @param page: the page to populate |
---|
258 | """ |
---|
259 | id=0 |
---|
260 | self.model_list_box=dict |
---|
261 | list_name=[] |
---|
262 | for item in self.model_list_box.itervalues(): |
---|
263 | name = item.__name__ |
---|
264 | if hasattr(item, "name"): |
---|
265 | name = item.name |
---|
266 | list_name.append(name) |
---|
267 | list_name.sort() |
---|
268 | |
---|
269 | for name in list_name: |
---|
270 | self.modelbox.Insert(name,int(id)) |
---|
271 | id+=1 |
---|
272 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
273 | return 0 |
---|
274 | |
---|
275 | |
---|
276 | def Set_DipersParam(self, event): |
---|
277 | if self.enable_disp.GetValue(): |
---|
278 | if len(self.disp_list)==0: |
---|
279 | ix=0 |
---|
280 | iy=1 |
---|
281 | self.fittable_param=[] |
---|
282 | self.fixed_param=[] |
---|
283 | self.sizer8.Clear(True) |
---|
284 | model_disp = wx.StaticText(self, -1, 'No PolyDispersity for this model') |
---|
285 | self.sizer7.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
286 | self.vbox.Layout() |
---|
287 | self.SetScrollbars(20,20,55,40) |
---|
288 | self.Layout() |
---|
289 | self.parent.GetSizer().Layout() |
---|
290 | return |
---|
291 | else: |
---|
292 | if self.data !=None and self.model !=None: # allow to recognize data panel from model panel |
---|
293 | #if self.data !=None and self.model !=None: |
---|
294 | self.cb1.SetValue(False) |
---|
295 | self.select_all_param_helper() |
---|
296 | |
---|
297 | self.populate_disp_box() |
---|
298 | self.set_panel_dispers(self.disp_list) |
---|
299 | |
---|
300 | else: |
---|
301 | if self.data !=None and self.model!=None: |
---|
302 | if self.cb1.GetValue(): |
---|
303 | self.select_all_param_helper() |
---|
304 | self.fittable_param=[] |
---|
305 | self.fixed_param=[] |
---|
306 | self.sizer7.Clear(True) |
---|
307 | self.sizer8.Clear(True) |
---|
308 | self.vbox.Layout() |
---|
309 | self.SetScrollbars(20,20,55,40) |
---|
310 | self.Layout() |
---|
311 | self.parent.GetSizer().Layout() |
---|
312 | |
---|
313 | def populate_disp_box(self): |
---|
314 | self.sizer7.Clear(True) |
---|
315 | if len(self.disp_list)>0: |
---|
316 | ix=0 |
---|
317 | iy=1 |
---|
318 | model_disp = wx.StaticText(self, -1, 'Model Disp') |
---|
319 | self.sizer7.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
320 | ix += 1 |
---|
321 | # set up the combox box |
---|
322 | id = 0 |
---|
323 | import sans.models.dispersion_models |
---|
324 | self.polydisp= sans.models.dispersion_models.models |
---|
325 | self.disp_box = wx.ComboBox(self, -1) |
---|
326 | self.disp_box.SetValue("GaussianModel") |
---|
327 | for k,v in self.polydisp.iteritems(): |
---|
328 | self.disp_box.Insert(str(v),id) |
---|
329 | id+=1 |
---|
330 | wx.EVT_COMBOBOX(self.disp_box,-1, self._on_select_Disp) |
---|
331 | self.sizer7.Add(self.disp_box,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
332 | self.vbox.Layout() |
---|
333 | self.SetScrollbars(20,20,55,40) |
---|
334 | self.Layout() |
---|
335 | self.parent.GetSizer().Layout() |
---|
336 | |
---|
337 | |
---|
338 | def set_range(self, qmin_x, qmax_x, npts): |
---|
339 | """ |
---|
340 | Set the range for the plotted models |
---|
341 | @param qmin: minimum Q |
---|
342 | @param qmax: maximum Q |
---|
343 | @param npts: number of Q bins |
---|
344 | """ |
---|
345 | # Set the data members |
---|
346 | self.qmin_x = qmin_x |
---|
347 | self.qmax_x = qmax_x |
---|
348 | self.num_points = npts |
---|
349 | |
---|
350 | # Set the controls |
---|
351 | self.qmin.SetValue(format_number(self.qmin_x)) |
---|
352 | self.qmax.SetValue(format_number(self.qmax_x)) |
---|
353 | self.npts.SetValue(format_number(self.num_points)) |
---|
354 | def checkFitRange(self): |
---|
355 | """ |
---|
356 | Check the validity of fitting range |
---|
357 | @note: qmin should always be less than qmax or else each control box |
---|
358 | background is colored in pink. |
---|
359 | """ |
---|
360 | |
---|
361 | flag = True |
---|
362 | valueMin = self.qmin.GetValue() |
---|
363 | valueMax = self.qmax.GetValue() |
---|
364 | # Check for possible values entered |
---|
365 | #print "fitpage: checkfitrange:",valueMin,valueMax |
---|
366 | try: |
---|
367 | if (float(valueMax)> float(valueMin)): |
---|
368 | self.qmax.SetBackgroundColour(wx.WHITE) |
---|
369 | self.qmin.SetBackgroundColour(wx.WHITE) |
---|
370 | else: |
---|
371 | flag = False |
---|
372 | self.qmin.SetBackgroundColour("pink") |
---|
373 | self.qmax.SetBackgroundColour("pink") |
---|
374 | except: |
---|
375 | flag = False |
---|
376 | self.qmin.SetBackgroundColour("pink") |
---|
377 | self.qmax.SetBackgroundColour("pink") |
---|
378 | |
---|
379 | self.qmin.Refresh() |
---|
380 | self.qmax.Refresh() |
---|
381 | return flag |
---|
382 | |
---|
383 | |
---|
384 | |
---|
385 | def onClose(self,event): |
---|
386 | """ close the page associated with this panel""" |
---|
387 | self.parent.onClose() |
---|
388 | |
---|
389 | |
---|
390 | |
---|
391 | def onModel2D(self, event): |
---|
392 | """ |
---|
393 | call manager to plot model in 2D |
---|
394 | """ |
---|
395 | # If the 2D display is not currently enabled, plot the model in 2D |
---|
396 | # and set the enable2D flag. |
---|
397 | if self.enable2D==False: |
---|
398 | self.enable2D=True |
---|
399 | self._draw_model() |
---|
400 | self.model_view.Disable() |
---|
401 | |
---|
402 | |
---|
403 | def select_model(self, model, name): |
---|
404 | """ |
---|
405 | Select a new model |
---|
406 | @param model: model object |
---|
407 | """ |
---|
408 | self.model = model |
---|
409 | self.parent.model_page.name = name |
---|
410 | self.parent.draw_model_name = name |
---|
411 | self.model_view.Enable() |
---|
412 | self.enable2D=False |
---|
413 | print "select_model", self.name,model.__class__ |
---|
414 | self.set_panel(model) |
---|
415 | self._draw_model(name) |
---|
416 | |
---|
417 | # Select the model from the combo box |
---|
418 | items = self.modelbox.GetItems() |
---|
419 | for i in range(len(items)): |
---|
420 | print "model name",items[i],model.name, model.__class__.__name__ |
---|
421 | if items[i]==name: |
---|
422 | self.modelbox.SetSelection(i) |
---|
423 | |
---|
424 | |
---|
425 | def _on_select_Disp(self,event): |
---|
426 | """ |
---|
427 | allow selecting different dispersion |
---|
428 | self.disp_list should change type later .now only gaussian |
---|
429 | """ |
---|
430 | type =event.GetString() |
---|
431 | self.set_panel_dispers( self.disp_list,type ) |
---|
432 | |
---|
433 | def _on_select_model(self,event): |
---|
434 | """ |
---|
435 | react when a model is selected from page's combo box |
---|
436 | post an event to its owner to draw an appropriate theory |
---|
437 | """ |
---|
438 | self.disable_disp.SetValue(True) |
---|
439 | self.sizer8.Clear(True) |
---|
440 | self.sizer7.Clear(True) |
---|
441 | self.vbox.Layout() |
---|
442 | self.SetScrollbars(20,20,55,40) |
---|
443 | self.Layout() |
---|
444 | self.parent.GetSizer().Layout() |
---|
445 | for item in self.model_list_box.itervalues(): |
---|
446 | name = item.__name__ |
---|
447 | if hasattr(item, "name"): |
---|
448 | name = item.name |
---|
449 | if name ==event.GetString(): |
---|
450 | model=item() |
---|
451 | self.model= model |
---|
452 | self.set_panel(model) |
---|
453 | self.name= name |
---|
454 | |
---|
455 | self.parent.model_page.name=name |
---|
456 | self.parent.draw_model_name=name |
---|
457 | #self.manager.draw_model(model, name) |
---|
458 | #self.enable2D=False |
---|
459 | #self.model_view.Enable() |
---|
460 | self._draw_model(name) |
---|
461 | |
---|
462 | |
---|
463 | def get_model_box(self): |
---|
464 | """ return reference to combox box self.model""" |
---|
465 | return self.modelbox |
---|
466 | |
---|
467 | |
---|
468 | def get_param_list(self): |
---|
469 | """ |
---|
470 | @return self.param_toFit: list containing references to TextCtrl |
---|
471 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
472 | @raise: if return an empty list of parameter fit will nnote work |
---|
473 | properly so raise ValueError,"missing parameter to fit" |
---|
474 | """ |
---|
475 | if self.param_toFit !=[]: |
---|
476 | return self.param_toFit |
---|
477 | else: |
---|
478 | raise ValueError,"missing parameter to fit" |
---|
479 | |
---|
480 | |
---|
481 | def set_panel(self,model): |
---|
482 | """ |
---|
483 | Build the panel from the model content |
---|
484 | @param model: the model selected in combo box for fitting purpose |
---|
485 | """ |
---|
486 | print "set_panel", model |
---|
487 | |
---|
488 | self.sizer5.Clear(True) |
---|
489 | self.parameters = [] |
---|
490 | self.param_toFit=[] |
---|
491 | self.fixed_param=[] |
---|
492 | self.model = model |
---|
493 | |
---|
494 | self.set_model_description( self.model) |
---|
495 | |
---|
496 | keys = self.model.getParamList() |
---|
497 | #list of dispersion paramaters |
---|
498 | self.disp_list=self.model.getDispParamList() |
---|
499 | |
---|
500 | keys.sort() |
---|
501 | ik=0 |
---|
502 | im=1 |
---|
503 | |
---|
504 | iy = 1 |
---|
505 | ix = 0 |
---|
506 | self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) |
---|
507 | if self.data!=None: |
---|
508 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
509 | self.cb1.SetValue(False) |
---|
510 | else: |
---|
511 | self.cb1.Disable() |
---|
512 | self.cb1.Hide() |
---|
513 | |
---|
514 | self.sizer5.Add(self.cb1,(iy, ix),(1,1),\ |
---|
515 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
516 | ix +=1 |
---|
517 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
518 | self.sizer5.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
519 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
520 | ix +=2 |
---|
521 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
522 | self.sizer5.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
523 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
524 | self.text2_3.Hide() |
---|
525 | ix +=1 |
---|
526 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
527 | self.sizer5.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
528 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
529 | self.text2_4.Hide() |
---|
530 | disp_list=self.model.getDispParamList() |
---|
531 | for item in keys: |
---|
532 | if not item in disp_list: |
---|
533 | iy += 1 |
---|
534 | ix = 0 |
---|
535 | |
---|
536 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
537 | if self.data!=None: |
---|
538 | cb.SetValue(False) |
---|
539 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
540 | else: |
---|
541 | cb.Disable() |
---|
542 | self.sizer5.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
543 | |
---|
544 | ix += 1 |
---|
545 | value= self.model.getParam(item) |
---|
546 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
547 | ctl1.SetValue(str (format_number(value))) |
---|
548 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
549 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
550 | self.sizer5.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
551 | |
---|
552 | ix += 1 |
---|
553 | text2=wx.StaticText(self, -1, '+/-') |
---|
554 | self.sizer5.Add(text2,(iy, ix),(1,1),\ |
---|
555 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
556 | text2.Hide() |
---|
557 | ix += 1 |
---|
558 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
559 | self.sizer5.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
560 | ctl2.Hide() |
---|
561 | ix +=1 |
---|
562 | # Units |
---|
563 | try: |
---|
564 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
565 | except: |
---|
566 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
567 | self.sizer5.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
568 | |
---|
569 | self.parameters.append([cb,ctl1,text2,ctl2]) |
---|
570 | |
---|
571 | iy+=1 |
---|
572 | self.sizer5.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
573 | |
---|
574 | #Display units text on panel |
---|
575 | for item in keys: |
---|
576 | if self.model.details[item][0]!='': |
---|
577 | self.text2_4.Show() |
---|
578 | break |
---|
579 | else: |
---|
580 | self.text2_4.Hide() |
---|
581 | #Disable or enable fit button |
---|
582 | """ |
---|
583 | if not (len(self.param_toFit ) >0): |
---|
584 | self.qmin.Disable() |
---|
585 | self.qmax.Disable() |
---|
586 | else: |
---|
587 | self.qmin.Enable() |
---|
588 | self.qmax.Enable() |
---|
589 | """ |
---|
590 | self.vbox.Layout() |
---|
591 | self.SetScrollbars(20,20,55,40) |
---|
592 | self.Layout() |
---|
593 | self.parent.GetSizer().Layout() |
---|
594 | def _selectDlg(self): |
---|
595 | import os |
---|
596 | dlg = wx.FileDialog(self, "Choose a weight file", os.getcwd(), "", "*.*", wx.OPEN) |
---|
597 | path = None |
---|
598 | if dlg.ShowModal() == wx.ID_OK: |
---|
599 | path = dlg.GetPath() |
---|
600 | dlg.Destroy() |
---|
601 | return path |
---|
602 | def read_file(self, path): |
---|
603 | try: |
---|
604 | input_f = open(path, 'r') |
---|
605 | buff = input_f.read() |
---|
606 | lines = buff.split('\n') |
---|
607 | |
---|
608 | angles = [] |
---|
609 | weights=[] |
---|
610 | for line in lines: |
---|
611 | toks = line.split() |
---|
612 | if len(toks)==2: |
---|
613 | try: |
---|
614 | angle = float(toks[0]) |
---|
615 | weight = float(toks[1]) |
---|
616 | except: |
---|
617 | # Skip non-data lines |
---|
618 | pass |
---|
619 | angles.append(angle) |
---|
620 | weights.append(weight) |
---|
621 | return numpy.array(angles), numpy.array(weights) |
---|
622 | except: |
---|
623 | raise |
---|
624 | |
---|
625 | def set_panel_dispers(self, disp_list, type="GaussianModel" ): |
---|
626 | |
---|
627 | self.fittable_param=[] |
---|
628 | self.fixed_param=[] |
---|
629 | |
---|
630 | ix=0 |
---|
631 | iy=1 |
---|
632 | ### this will become a separate method |
---|
633 | if type== "MyModel": |
---|
634 | |
---|
635 | self.sizer8.Clear(True) |
---|
636 | path = self._selectDlg() |
---|
637 | dispersion=None |
---|
638 | for key, value in self.polydisp.iteritems(): |
---|
639 | if value =="MyModel": |
---|
640 | dispersion= key() |
---|
641 | break |
---|
642 | values,weights = self.read_file(path) |
---|
643 | dispersion.set_weights( values, weights) |
---|
644 | print "sipersion model", dispersion.value |
---|
645 | for param in self.model.dispersion.keys(): |
---|
646 | print |
---|
647 | print "on MyModel disp",dispersion,param, self.model.set_dispersion(param, dispersion) |
---|
648 | |
---|
649 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
650 | " Selected Distribution: %s"%path)) |
---|
651 | |
---|
652 | if type== "GaussianModel" : |
---|
653 | print "went here" |
---|
654 | |
---|
655 | self.sizer8.Clear(True) |
---|
656 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
657 | self.sizer8.Add(disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
658 | ix += 1 |
---|
659 | values = wx.StaticText(self, -1, 'Values') |
---|
660 | self.sizer8.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
661 | ix +=2 |
---|
662 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
663 | self.sizer8.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
664 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
665 | self.text2_3.Hide() |
---|
666 | |
---|
667 | ix += 1 |
---|
668 | npts = wx.StaticText(self, -1, 'Npts') |
---|
669 | self.sizer8.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
670 | ix += 1 |
---|
671 | nsigmas = wx.StaticText(self, -1, 'Nsigmas') |
---|
672 | self.sizer8.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
673 | |
---|
674 | disp_list.sort() |
---|
675 | print disp_list,self.model.dispersion |
---|
676 | for item in self.model.dispersion.keys(): |
---|
677 | name1=item+".width" |
---|
678 | name2=item+".npts" |
---|
679 | name3=item+".nsigmas" |
---|
680 | iy += 1 |
---|
681 | for p in self.model.dispersion[item].keys(): |
---|
682 | #print "name 1 2 3", name1, name2, name3 |
---|
683 | if p=="width": |
---|
684 | ix = 0 |
---|
685 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
686 | if self.data !=None: |
---|
687 | cb.SetValue(False) |
---|
688 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
689 | else: |
---|
690 | cb.Disable() |
---|
691 | self.sizer8.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
692 | ix = 1 |
---|
693 | value= self.model.getParam(name1) |
---|
694 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
695 | ctl1.SetValue(str (format_number(value))) |
---|
696 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
697 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
698 | self.sizer8.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
699 | |
---|
700 | ix = 2 |
---|
701 | text2=wx.StaticText(self, -1, '+/-') |
---|
702 | self.sizer8.Add(text2,(iy, ix),(1,1),\ |
---|
703 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
704 | text2.Hide() |
---|
705 | ix = 3 |
---|
706 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
707 | self.sizer8.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
708 | ctl2.Hide() |
---|
709 | self.fittable_param.append([cb,ctl1,text2,ctl2]) |
---|
710 | |
---|
711 | |
---|
712 | elif p=="npts": |
---|
713 | ix =4 |
---|
714 | value= self.model.getParam(name2) |
---|
715 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
716 | Tctl.SetValue(str (format_number(value))) |
---|
717 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
718 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
719 | self.sizer8.Add(Tctl, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
720 | self.fixed_param.append([name2, Tctl]) |
---|
721 | elif p=="nsigmas": |
---|
722 | ix =5 |
---|
723 | value= self.model.getParam(name3) |
---|
724 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER) |
---|
725 | Tctl.SetValue(str (format_number(value))) |
---|
726 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
727 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
728 | self.sizer8.Add(Tctl, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
729 | self.fixed_param.append([name3, Tctl]) |
---|
730 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
731 | " Selected Distribution: Gaussian")) |
---|
732 | ix =0 |
---|
733 | iy +=1 |
---|
734 | self.sizer8.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
735 | self.vbox.Layout() |
---|
736 | self.SetScrollbars(20,20,55,40) |
---|
737 | self.Layout() |
---|
738 | self.parent.GetSizer().Layout() |
---|
739 | |
---|
740 | |
---|
741 | |
---|
742 | |
---|
743 | def _onparamEnter(self,event): |
---|
744 | """ |
---|
745 | when enter value on panel redraw model according to changed |
---|
746 | """ |
---|
747 | self.set_model_parameter() |
---|
748 | |
---|
749 | def set_model_parameter(self): |
---|
750 | if len(self.parameters) !=0 and self.model !=None: |
---|
751 | # Flag to register when a parameter has changed. |
---|
752 | is_modified = False |
---|
753 | for item in self.fittable_param: |
---|
754 | try: |
---|
755 | name=str(item[0].GetLabelText()) |
---|
756 | value= float(item[1].GetValue()) |
---|
757 | #print "model para", name,value |
---|
758 | # If the value of the parameter has changed, |
---|
759 | # update the model and set the is_modified flag |
---|
760 | if value != self.model.getParam(name): |
---|
761 | #print "went hereee" |
---|
762 | self.model.setParam(name,value) |
---|
763 | is_modified = True |
---|
764 | |
---|
765 | except: |
---|
766 | raise |
---|
767 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
768 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
769 | |
---|
770 | for item in self.fixed_param: |
---|
771 | try: |
---|
772 | name=str(item[0]) |
---|
773 | value= float(item[1].GetValue()) |
---|
774 | #print "model para", name,value,self.model.getParam(name) |
---|
775 | # If the value of the parameter has changed, |
---|
776 | # update the model and set the is_modified flag |
---|
777 | if value != self.model.getParam(name): |
---|
778 | #print "went hereee" |
---|
779 | self.model.setParam(name,value) |
---|
780 | is_modified = True |
---|
781 | |
---|
782 | except: |
---|
783 | raise |
---|
784 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
785 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
786 | |
---|
787 | for item in self.parameters: |
---|
788 | try: |
---|
789 | name=str(item[0].GetLabelText()) |
---|
790 | value= float(item[1].GetValue()) |
---|
791 | #print " fittable model para", name,value |
---|
792 | # If the value of the parameter has changed, |
---|
793 | # update the model and set the is_modified flag |
---|
794 | if value != self.model.getParam(name): |
---|
795 | #print "went here", name,value |
---|
796 | self.model.setParam(name,value) |
---|
797 | is_modified = True |
---|
798 | except: |
---|
799 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
800 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
801 | |
---|
802 | # Here we should check whether the boundaries have been modified. |
---|
803 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
804 | # set the is_modified flag to True |
---|
805 | if float(self.qmin.GetValue()) != self.qmin_x: |
---|
806 | self.qmin_x = float(self.qmin.GetValue()) |
---|
807 | is_modified = True |
---|
808 | if float(self.qmax.GetValue()) != self.qmax_x: |
---|
809 | self.qmax_x = float(self.qmax.GetValue()) |
---|
810 | is_modified = True |
---|
811 | |
---|
812 | if float(self.npts.GetValue()) != self.num_points: |
---|
813 | self.num_points = float(self.npts.GetValue()) |
---|
814 | is_modified = True |
---|
815 | |
---|
816 | if is_modified: |
---|
817 | self._draw_model() |
---|
818 | |
---|
819 | def _draw_model(self, name=None): |
---|
820 | """ |
---|
821 | Method to draw or refresh a plotted model. |
---|
822 | The method will use the data member from the model page |
---|
823 | to build a call to the fitting perspective manager. |
---|
824 | |
---|
825 | [Note to coder: This way future changes will be done in only one place.] |
---|
826 | """ |
---|
827 | #print "_draw_model",self.model |
---|
828 | if name==None: |
---|
829 | name= self.model.name |
---|
830 | |
---|
831 | self.manager.draw_model(self.model, name, data=self.data, |
---|
832 | qmin=self.qmin_x, qmax=self.qmax_x, |
---|
833 | qstep= self.num_points, |
---|
834 | enable2D=self.enable2D) |
---|
835 | |
---|
836 | def select_param(self,event): |
---|
837 | pass |
---|
838 | def select_all_param(self,event): |
---|
839 | pass |
---|
840 | def select_all_param_helper(self): |
---|
841 | """ |
---|
842 | Allows selecting or delecting button |
---|
843 | """ |
---|
844 | self.param_toFit=[] |
---|
845 | if self.parameters !=[]: |
---|
846 | if self.cb1.GetValue()==True: |
---|
847 | for item in self.parameters: |
---|
848 | item[0].SetValue(True) |
---|
849 | list= [item[0],item[1],item[2],item[3]] |
---|
850 | self.param_toFit.append(list ) |
---|
851 | if len(self.fittable_param)>0: |
---|
852 | for item in self.fittable_param: |
---|
853 | item[0].SetValue(True) |
---|
854 | list= [item[0],item[1],item[2],item[3]] |
---|
855 | self.param_toFit.append(list ) |
---|
856 | else: |
---|
857 | for item in self.parameters: |
---|
858 | item[0].SetValue(False) |
---|
859 | for item in self.fittable_param: |
---|
860 | item[0].SetValue(False) |
---|
861 | self.param_toFit=[] |
---|
862 | |
---|
863 | |
---|
864 | |
---|