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