1 | |
---|
2 | import sys |
---|
3 | import wx |
---|
4 | import numpy |
---|
5 | |
---|
6 | from sans.guiframe.utils import format_number |
---|
7 | from sans.guicomm.events import StatusEvent |
---|
8 | |
---|
9 | _BOX_WIDTH = 80 |
---|
10 | |
---|
11 | class BasicPage(wx.ScrolledWindow): |
---|
12 | """ |
---|
13 | This class provide general structure of fitpanel page |
---|
14 | """ |
---|
15 | ## Internal name for the AUI manager |
---|
16 | window_name = "Fit page" |
---|
17 | ## Title to appear on top of the window |
---|
18 | window_caption = "Fit Page" |
---|
19 | name="" |
---|
20 | def __init__(self,parent, pageinfo, name): |
---|
21 | wx.ScrolledWindow.__init__(self, parent) |
---|
22 | ## store name |
---|
23 | self.name = name |
---|
24 | ## store info of panel |
---|
25 | self.page_info= pageinfo |
---|
26 | self.page_info.page_name = name |
---|
27 | ## parent of the page |
---|
28 | self.parent = parent |
---|
29 | ## owner of the page (fitting plugin) |
---|
30 | self.event_owner= None |
---|
31 | ## create the basic structure of the panel with empty sizer |
---|
32 | self.define_page_structure() |
---|
33 | ## dictionary containing list of models |
---|
34 | self.model_list_box={} |
---|
35 | ## Data member to store the dispersion object created |
---|
36 | self._disp_obj_dict = {} |
---|
37 | self.disp_cb_dict ={} |
---|
38 | ##list of model parameters. each item must have same length |
---|
39 | ## each item related to a given parameters |
---|
40 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
41 | self.parameters=[] |
---|
42 | ## list of parameters to fit , must be like self.parameters |
---|
43 | self.param_toFit=[] |
---|
44 | ## list of looking like parameters but with non fittable parameters info |
---|
45 | self.fixed_param=[] |
---|
46 | ## list of looking like parameters but with fittable parameters info |
---|
47 | self.fittable_param=[] |
---|
48 | ##list of dispersion parameters |
---|
49 | self.disp_list=[] |
---|
50 | |
---|
51 | ##enable model 2D draw |
---|
52 | self.enable2D= False |
---|
53 | ## check that the fit range is correct to plot the model again |
---|
54 | self.fitrange= True |
---|
55 | ## current model |
---|
56 | self.model= None |
---|
57 | ## data |
---|
58 | self.data=None |
---|
59 | ## Q range |
---|
60 | self.qmin_x= 0.001 |
---|
61 | self.qmax_x= 0.1 |
---|
62 | self.num_points= 100 |
---|
63 | ## changing initial values given page_info value |
---|
64 | if self.page_info !=None and len(self.page_info.model_list_box) >0: |
---|
65 | self.model_list_box = self.page_info.model_list_box |
---|
66 | ## current model |
---|
67 | self.model=self.page_info.model |
---|
68 | self.data = self.page_info.data |
---|
69 | self.event_owner = self.page_info.event_owner |
---|
70 | if self.model !=None: |
---|
71 | self.disp_list= self.model.getDispParamList() |
---|
72 | ## drawing Initial dispersion parameters sizer |
---|
73 | self.set_dispers_sizer() |
---|
74 | ## layout |
---|
75 | self.set_layout() |
---|
76 | |
---|
77 | |
---|
78 | def define_page_structure(self): |
---|
79 | """ |
---|
80 | Create empty sizer for a panel |
---|
81 | """ |
---|
82 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
83 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
84 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
85 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
86 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
87 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
88 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
89 | |
---|
90 | self.sizer0.SetMinSize((375,-1)) |
---|
91 | self.sizer1.SetMinSize((375,-1)) |
---|
92 | self.sizer2.SetMinSize((375,-1)) |
---|
93 | self.sizer3.SetMinSize((375,-1)) |
---|
94 | self.sizer4.SetMinSize((375,-1)) |
---|
95 | self.sizer5.SetMinSize((375,-1)) |
---|
96 | |
---|
97 | self.vbox.Add(self.sizer0) |
---|
98 | self.vbox.Add(self.sizer1) |
---|
99 | self.vbox.Add(self.sizer2) |
---|
100 | self.vbox.Add(self.sizer3) |
---|
101 | self.vbox.Add(self.sizer4) |
---|
102 | self.vbox.Add(self.sizer5) |
---|
103 | |
---|
104 | def set_layout(self): |
---|
105 | """ |
---|
106 | layout |
---|
107 | """ |
---|
108 | self.vbox.Layout() |
---|
109 | self.vbox.Fit(self) |
---|
110 | self.SetSizer(self.vbox) |
---|
111 | |
---|
112 | self.set_scroll() |
---|
113 | self.Centre() |
---|
114 | |
---|
115 | def set_scroll(self): |
---|
116 | self.SetScrollbars(20,20,200,100) |
---|
117 | self.Layout() |
---|
118 | |
---|
119 | def set_owner(self,owner): |
---|
120 | """ |
---|
121 | set owner of fitpage |
---|
122 | @param owner: the class responsible of plotting |
---|
123 | """ |
---|
124 | self.event_owner = owner |
---|
125 | self.page_info.event_owner = owner |
---|
126 | |
---|
127 | def set_manager(self, manager): |
---|
128 | """ |
---|
129 | set panel manager |
---|
130 | @param manager: instance of plugin fitting |
---|
131 | """ |
---|
132 | self.manager = manager |
---|
133 | self.page_info.manager = manager |
---|
134 | |
---|
135 | def populate_box(self, dict): |
---|
136 | """ |
---|
137 | Store list of model |
---|
138 | @param dict: dictionary containing list of models |
---|
139 | """ |
---|
140 | self.model_list_box = dict |
---|
141 | if self.page_info!=None: |
---|
142 | self.page_info.model_list_box = self.model_list_box |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | def set_dispers_sizer(self): |
---|
147 | """ |
---|
148 | fill sizer containing dispersity info |
---|
149 | """ |
---|
150 | self.sizer4.Clear(True) |
---|
151 | box_description= wx.StaticBox(self, -1,"PolyDispersity") |
---|
152 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
153 | #---------------------------------------------------- |
---|
154 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
155 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
156 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.disable_disp.GetId()) |
---|
157 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.enable_disp.GetId()) |
---|
158 | |
---|
159 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
160 | sizer_dispersion.Add((20,20)) |
---|
161 | sizer_dispersion.Add(wx.StaticText(self,-1,'Polydispersity: ')) |
---|
162 | sizer_dispersion.Add(self.enable_disp ) |
---|
163 | sizer_dispersion.Add((20,20)) |
---|
164 | sizer_dispersion.Add(self.disable_disp ) |
---|
165 | sizer_dispersion.Add((10,10)) |
---|
166 | |
---|
167 | ## fill a sizer with the combobox to select dispersion type |
---|
168 | sizer_select_dispers = wx.BoxSizer(wx.HORIZONTAL) |
---|
169 | self.model_disp = wx.StaticText(self, -1, 'Model Disp') |
---|
170 | |
---|
171 | import sans.models.dispersion_models |
---|
172 | self.polydisp= sans.models.dispersion_models.models |
---|
173 | self.disp_box = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
174 | self.disp_box.SetValue("GaussianModel") |
---|
175 | |
---|
176 | for key in self.polydisp.iterkeys(): |
---|
177 | name = str(key.__name__) |
---|
178 | if name=="ArrayDispersion": |
---|
179 | # Remove the option until the rest of the code is ready for it |
---|
180 | self.disp_box.Append("Select customized Model",key) |
---|
181 | pass |
---|
182 | else: |
---|
183 | self.disp_box.Append(name,key) |
---|
184 | wx.EVT_COMBOBOX(self.disp_box,-1, self._on_select_Disp) |
---|
185 | |
---|
186 | sizer_select_dispers.Add((10,10)) |
---|
187 | sizer_select_dispers.Add(self.model_disp) |
---|
188 | sizer_select_dispers.Add(self.disp_box) |
---|
189 | sizer_select_dispers.Add((10,10)) |
---|
190 | self.model_disp.Hide() |
---|
191 | self.disp_box.Hide() |
---|
192 | |
---|
193 | boxsizer1.Add( sizer_dispersion ) |
---|
194 | boxsizer1.Add( (10,10) ) |
---|
195 | boxsizer1.Add( sizer_select_dispers ) |
---|
196 | self.sizer4_4 = wx.GridBagSizer(5,5) |
---|
197 | boxsizer1.Add( self.sizer4_4 ) |
---|
198 | #----------------------------------------------------- |
---|
199 | self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
200 | self.sizer4.Layout() |
---|
201 | self.SetScrollbars(20,20,200,100) |
---|
202 | |
---|
203 | def select_disp_angle(self, event): |
---|
204 | """ |
---|
205 | Event for when a user select a parameter to average over. |
---|
206 | @param event: check box event |
---|
207 | """ |
---|
208 | |
---|
209 | |
---|
210 | # Go through the list of dispersion check boxes to identify which one has changed |
---|
211 | for p in self.disp_cb_dict: |
---|
212 | # Catch which one of the box was just checked or unchecked. |
---|
213 | if event.GetEventObject() == self.disp_cb_dict[p]: |
---|
214 | |
---|
215 | |
---|
216 | if self.disp_cb_dict[p].GetValue() == True: |
---|
217 | # The user wants this parameter to be averaged. |
---|
218 | # Pop up the file selection dialog. |
---|
219 | path = self._selectDlg() |
---|
220 | |
---|
221 | # If nothing was selected, just return |
---|
222 | if path is None: |
---|
223 | self.disp_cb_dict[p].SetValue(False) |
---|
224 | return |
---|
225 | |
---|
226 | try: |
---|
227 | values,weights = self.read_file(path) |
---|
228 | except: |
---|
229 | msg="Could not read input file" |
---|
230 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
231 | return |
---|
232 | |
---|
233 | # If any of the two arrays is empty, notify the user that we won't |
---|
234 | # proceed |
---|
235 | if values is None or weights is None: |
---|
236 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
237 | "The loaded %s distrubtion is corrupted or empty" % p)) |
---|
238 | return |
---|
239 | |
---|
240 | # Tell the user that we are about to apply the distribution |
---|
241 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
242 | "Applying loaded %s distribution: %s" % (p, path))) |
---|
243 | |
---|
244 | # Create the dispersion objects |
---|
245 | from sans.models.dispersion_models import ArrayDispersion |
---|
246 | disp_model = ArrayDispersion() |
---|
247 | disp_model.set_weights(values, weights) |
---|
248 | # Store the object to make it persist outside the scope of this method |
---|
249 | #TODO: refactor model to clean this up? |
---|
250 | self._disp_obj_dict[p] = disp_model |
---|
251 | |
---|
252 | # Set the new model as the dispersion object for the selected parameter |
---|
253 | self.model.set_dispersion(p, disp_model) |
---|
254 | |
---|
255 | else: |
---|
256 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
257 | self._reset_dispersity() |
---|
258 | |
---|
259 | ## Redraw the model |
---|
260 | self._draw_model() |
---|
261 | return |
---|
262 | |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | def read_file(self, path): |
---|
267 | """ |
---|
268 | Read two columns file |
---|
269 | @param path: the path to the file to read |
---|
270 | """ |
---|
271 | try: |
---|
272 | if path==None: |
---|
273 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
274 | " Selected Distribution was not loaded: %s"%path)) |
---|
275 | return None, None |
---|
276 | input_f = open(path, 'r') |
---|
277 | buff = input_f.read() |
---|
278 | lines = buff.split('\n') |
---|
279 | |
---|
280 | angles = [] |
---|
281 | weights=[] |
---|
282 | for line in lines: |
---|
283 | toks = line.split() |
---|
284 | if len(toks)==2: |
---|
285 | try: |
---|
286 | angle = float(toks[0]) |
---|
287 | weight = float(toks[1]) |
---|
288 | except: |
---|
289 | # Skip non-data lines |
---|
290 | pass |
---|
291 | angles.append(angle) |
---|
292 | weights.append(weight) |
---|
293 | return numpy.array(angles), numpy.array(weights) |
---|
294 | except: |
---|
295 | raise |
---|
296 | |
---|
297 | |
---|
298 | def _selectDlg(self): |
---|
299 | """ |
---|
300 | open a dialog file to selected the customized dispersity |
---|
301 | """ |
---|
302 | import os |
---|
303 | dlg = wx.FileDialog(self, "Choose a weight file", os.getcwd(), "", "*.*", wx.OPEN) |
---|
304 | path = None |
---|
305 | if dlg.ShowModal() == wx.ID_OK: |
---|
306 | path = dlg.GetPath() |
---|
307 | dlg.Destroy() |
---|
308 | return path |
---|
309 | |
---|
310 | |
---|
311 | def _onparamEnter_helper(self): |
---|
312 | """ |
---|
313 | check if values entered by the user are changed and valid to replot |
---|
314 | model |
---|
315 | use : _check_value_enter |
---|
316 | """ |
---|
317 | if self.model !=None: |
---|
318 | # Flag to register when a parameter has changed. |
---|
319 | is_modified = False |
---|
320 | is_modified =self._check_value_enter( self.fittable_param ,is_modified) |
---|
321 | is_modified =self._check_value_enter( self.fixed_param ,is_modified) |
---|
322 | is_modified =self._check_value_enter( self.parameters ,is_modified) |
---|
323 | |
---|
324 | # Here we should check whether the boundaries have been modified. |
---|
325 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
326 | # set the is_modified flag to True |
---|
327 | from sans.guiframe.utils import check_value |
---|
328 | if check_value( self.qmin, self.qmax): |
---|
329 | if float(self.qmin.GetValue()) != self.qmin_x: |
---|
330 | self.qmin_x = float(self.qmin.GetValue()) |
---|
331 | is_modified = True |
---|
332 | if float(self.qmax.GetValue()) != self.qmax_x: |
---|
333 | self.qmax_x = float(self.qmax.GetValue()) |
---|
334 | is_modified = True |
---|
335 | self.fitrange = True |
---|
336 | else: |
---|
337 | self.fitrange = False |
---|
338 | if self.npts != None: |
---|
339 | if float(self.npts.GetValue()) != self.num_points: |
---|
340 | self.num_points = float(self.npts.GetValue()) |
---|
341 | is_modified = True |
---|
342 | ## if any value is modify draw model with new value |
---|
343 | if is_modified: |
---|
344 | self._draw_model() |
---|
345 | |
---|
346 | |
---|
347 | def _set_model_sizer_selection(self, model): |
---|
348 | """ |
---|
349 | Display the sizer according to the type of the current model |
---|
350 | """ |
---|
351 | if hasattr(model ,"model2"): |
---|
352 | |
---|
353 | class_name= model.model2.__class__ |
---|
354 | name= model.model2.name |
---|
355 | flag= name != "NoStructure" |
---|
356 | if flag and (class_name in self.model_list_box["Structure Factors"]): |
---|
357 | self.structurebox.Show(True) |
---|
358 | self.text2.Show(True) |
---|
359 | self.multip_cb.SetValue(True) |
---|
360 | items = self.structurebox.GetItems() |
---|
361 | self.sizer1.Layout() |
---|
362 | self.SetScrollbars(20,20,200,100) |
---|
363 | for i in range(len(items)): |
---|
364 | if items[i]== str(name): |
---|
365 | self.structurebox.SetSelection(i) |
---|
366 | break |
---|
367 | |
---|
368 | if hasattr(model ,"model1"): |
---|
369 | class_name = model.model1.__class__ |
---|
370 | name = model.model1.name |
---|
371 | self.formfactorbox.Clear() |
---|
372 | |
---|
373 | for k, list in self.model_list_box.iteritems(): |
---|
374 | if k == "shapes": |
---|
375 | self.shape_rbutton.SetValue(True) |
---|
376 | elif k == "Shape-independent": |
---|
377 | self.shape_indep_rbutton.SetValue(True) |
---|
378 | elif k == "Structure Factors": |
---|
379 | self.struct_rbutton.SetValue(True) |
---|
380 | else: |
---|
381 | self.plugin_rbutton.SetValue(True) |
---|
382 | |
---|
383 | if class_name in list: |
---|
384 | ## fill the form factor list with new model |
---|
385 | self._populate_box(self.formfactorbox, list) |
---|
386 | items = self.formfactorbox.GetItems() |
---|
387 | ## set comboxbox to the selected item |
---|
388 | for i in range(len(items)): |
---|
389 | if items[i]== str(name): |
---|
390 | self.formfactorbox.SetSelection(i) |
---|
391 | break |
---|
392 | break |
---|
393 | else: |
---|
394 | ## Select the model from the combo box |
---|
395 | class_name = model.__class__ |
---|
396 | name = model.name |
---|
397 | self.formfactorbox.Clear() |
---|
398 | items = self.formfactorbox.GetItems() |
---|
399 | for k, list in self.model_list_box.iteritems(): |
---|
400 | if k == "shapes": |
---|
401 | self.shape_rbutton.SetValue(True) |
---|
402 | elif k == "Shape-independent": |
---|
403 | self.shape_indep_rbutton.SetValue(True) |
---|
404 | elif k == "Structure Factors": |
---|
405 | self.struct_rbutton.SetValue(True) |
---|
406 | else: |
---|
407 | self.plugin_rbutton.SetValue(True) |
---|
408 | if class_name in list: |
---|
409 | ## fill the form factor list with new model |
---|
410 | self._populate_box(self.formfactorbox, list) |
---|
411 | items = self.formfactorbox.GetItems() |
---|
412 | ## set comboxbox to the selected item |
---|
413 | for i in range(len(items)): |
---|
414 | if items[i]== str(name): |
---|
415 | self.formfactorbox.SetSelection(i) |
---|
416 | break |
---|
417 | break |
---|
418 | |
---|
419 | |
---|
420 | |
---|
421 | def _draw_model(self): |
---|
422 | """ |
---|
423 | Method to draw or refresh a plotted model. |
---|
424 | The method will use the data member from the model page |
---|
425 | to build a call to the fitting perspective manager. |
---|
426 | |
---|
427 | [Note to coder: This way future changes will be done in only one place.] |
---|
428 | """ |
---|
429 | if self.model !=None: |
---|
430 | self.manager.draw_model(self.model, data=self.data, |
---|
431 | qmin=self.qmin_x, qmax=self.qmax_x, |
---|
432 | qstep= self.num_points, |
---|
433 | enable2D=self.enable2D) |
---|
434 | |
---|
435 | def _set_model_sizer(self, sizer, title="", object=None): |
---|
436 | """ |
---|
437 | Use lists to fill a sizer for model info |
---|
438 | """ |
---|
439 | |
---|
440 | sizer.Clear(True) |
---|
441 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
442 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
443 | #-------------------------------------------------------- |
---|
444 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shape', style=wx.RB_GROUP) |
---|
445 | self.shape_indep_rbutton = wx.RadioButton(self, -1, "Shape Independent") |
---|
446 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure ") |
---|
447 | self.plugin_rbutton = wx.RadioButton(self, -1, "customized Model") |
---|
448 | self.multip_cb = wx.CheckBox(self, -1,"P(Q)*S(Q)") |
---|
449 | |
---|
450 | |
---|
451 | |
---|
452 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
453 | id= self.shape_rbutton.GetId() ) |
---|
454 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
455 | id= self.shape_indep_rbutton.GetId() ) |
---|
456 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
457 | id= self.struct_rbutton.GetId() ) |
---|
458 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
459 | id= self.plugin_rbutton.GetId() ) |
---|
460 | wx.EVT_CHECKBOX(self, self.multip_cb.GetId() , self._show_combox ) |
---|
461 | ## store state |
---|
462 | self.page_info.save_radiobox_state( self.shape_rbutton ) |
---|
463 | self.page_info.save_radiobox_state( self.shape_indep_rbutton ) |
---|
464 | self.page_info.save_radiobox_state( self.struct_rbutton ) |
---|
465 | self.page_info.save_radiobox_state( self.plugin_rbutton ) |
---|
466 | |
---|
467 | |
---|
468 | sizer_radiobutton = wx.GridSizer(3, 3,5, 5) |
---|
469 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
470 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
471 | sizer_radiobutton.Add(self.multip_cb) |
---|
472 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
473 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
474 | |
---|
475 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
476 | |
---|
477 | self.text1 = wx.StaticText( self,-1,"P(Q)" ) |
---|
478 | self.text2 = wx.StaticText( self,-1,"* S(Q)" ) |
---|
479 | |
---|
480 | self.text2.Hide() |
---|
481 | |
---|
482 | self.formfactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
483 | if self.model!=None: |
---|
484 | self.formfactorbox.SetValue(self.model.name) |
---|
485 | |
---|
486 | self.structurebox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
487 | wx.EVT_COMBOBOX(self.formfactorbox,-1, self._on_select_model) |
---|
488 | wx.EVT_COMBOBOX(self.structurebox,-1, self._on_select_model) |
---|
489 | self.structurebox.Hide() |
---|
490 | |
---|
491 | ## fill combox box |
---|
492 | if len(self.model_list_box)>0: |
---|
493 | self._populate_box( self.formfactorbox,self.model_list_box["shapes"]) |
---|
494 | |
---|
495 | if len(self.model_list_box)>0: |
---|
496 | self._populate_box( self.structurebox, |
---|
497 | self.model_list_box["Structure Factors"]) |
---|
498 | |
---|
499 | ## check model type to show sizer |
---|
500 | if self.model !=None: |
---|
501 | self._set_model_sizer_selection( self.model ) |
---|
502 | |
---|
503 | sizer_selection.Add(self.text1) |
---|
504 | sizer_selection.Add((5,5)) |
---|
505 | sizer_selection.Add(self.formfactorbox) |
---|
506 | sizer_selection.Add((5,5)) |
---|
507 | sizer_selection.Add(self.text2) |
---|
508 | sizer_selection.Add((5,5)) |
---|
509 | sizer_selection.Add(self.structurebox) |
---|
510 | sizer_selection.Add((5,5)) |
---|
511 | |
---|
512 | boxsizer1.Add( sizer_radiobutton ) |
---|
513 | boxsizer1.Add( (20,20)) |
---|
514 | boxsizer1.Add( sizer_selection ) |
---|
515 | if object !=None: |
---|
516 | boxsizer1.Add( (20,20)) |
---|
517 | boxsizer1.Add( object ) |
---|
518 | #-------------------------------------------------------- |
---|
519 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
520 | sizer.Layout() |
---|
521 | self.SetScrollbars(20,20,200,100) |
---|
522 | |
---|
523 | |
---|
524 | def _show_combox(self, event): |
---|
525 | """ |
---|
526 | Show combox box associate with type of model selected |
---|
527 | """ |
---|
528 | self.page_info.save_radiobox_state( self.shape_rbutton ) |
---|
529 | self.page_info.save_radiobox_state( self.shape_indep_rbutton ) |
---|
530 | self.page_info.save_radiobox_state( self.struct_rbutton ) |
---|
531 | self.page_info.save_radiobox_state( self.plugin_rbutton ) |
---|
532 | |
---|
533 | |
---|
534 | |
---|
535 | if self.shape_rbutton.GetValue(): |
---|
536 | ##fill the comboxbox with form factor list |
---|
537 | self.structurebox.Hide() |
---|
538 | self.text2.Hide() |
---|
539 | self.formfactorbox.Clear() |
---|
540 | self._populate_box( self.formfactorbox,self.model_list_box["shapes"]) |
---|
541 | |
---|
542 | if self.shape_indep_rbutton.GetValue(): |
---|
543 | ##fill the comboxbox with shape independent factor list |
---|
544 | self.structurebox.Hide() |
---|
545 | self.text2.Hide() |
---|
546 | self.formfactorbox.Clear() |
---|
547 | self._populate_box( self.formfactorbox, |
---|
548 | self.model_list_box["Shape-independent"]) |
---|
549 | |
---|
550 | if self.struct_rbutton.GetValue(): |
---|
551 | ##fill the comboxbox with structure factor list |
---|
552 | self.structurebox.Hide() |
---|
553 | self.text2.Hide() |
---|
554 | self.formfactorbox.Clear() |
---|
555 | self._populate_box( self.formfactorbox, |
---|
556 | self.model_list_box["Structure Factors"]) |
---|
557 | |
---|
558 | if self.plugin_rbutton.GetValue(): |
---|
559 | ##fill the comboxbox with form factor list |
---|
560 | self.structurebox.Hide() |
---|
561 | self.text2.Hide() |
---|
562 | self.formfactorbox.Clear() |
---|
563 | self._populate_box( self.formfactorbox, |
---|
564 | self.model_list_box["Added models"]) |
---|
565 | |
---|
566 | |
---|
567 | if self.multip_cb.GetValue(): |
---|
568 | ## multplication not available for structure factor |
---|
569 | if self.struct_rbutton.GetValue(): |
---|
570 | self.multip_cb.SetValue(False) |
---|
571 | self.structurebox.Hide() |
---|
572 | return |
---|
573 | ##fill the comboxbox with form factor list |
---|
574 | self.structurebox.Show(True) |
---|
575 | self.text2.Show(True) |
---|
576 | |
---|
577 | self.set_scroll() |
---|
578 | |
---|
579 | |
---|
580 | |
---|
581 | |
---|
582 | def _populate_box(self, combobox, list): |
---|
583 | """ |
---|
584 | fill combox box with dict item |
---|
585 | @param list: contains item to fill the combox |
---|
586 | item must model class |
---|
587 | """ |
---|
588 | for models in list: |
---|
589 | model= models() |
---|
590 | name = model.__class__.__name__ |
---|
591 | if hasattr(model, "name"): |
---|
592 | name = model.name |
---|
593 | combobox.Append(name,models) |
---|
594 | |
---|
595 | wx.EVT_COMBOBOX(combobox,-1, self._on_select_model) |
---|
596 | return 0 |
---|
597 | |
---|
598 | |
---|
599 | def _on_select_model_helper(self,event): |
---|
600 | """ |
---|
601 | call back for model selection |
---|
602 | """ |
---|
603 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
604 | s_id = self.structurebox.GetCurrentSelection() |
---|
605 | form_factor = self.formfactorbox.GetClientData( f_id ) |
---|
606 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
607 | |
---|
608 | if self.multip_cb.GetValue(): |
---|
609 | if struct_factor != None and struct_factor.__name__ != "NoStructure": |
---|
610 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
611 | if form_factor != None: |
---|
612 | self.model= MultiplicationModel(form_factor(),struct_factor()) |
---|
613 | msg= "select one P(Q) and one S(Q) to plot" |
---|
614 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
615 | else: |
---|
616 | if form_factor != None: |
---|
617 | self.model= form_factor() |
---|
618 | |
---|
619 | |
---|
620 | def _onparamEnter(self,event): |
---|
621 | """ |
---|
622 | when enter value on panel redraw model according to changed |
---|
623 | """ |
---|
624 | self._onparamEnter_helper() |
---|
625 | |
---|
626 | |
---|
627 | def _check_value_enter(self, list, modified): |
---|
628 | """ |
---|
629 | @param list: model parameter and panel info |
---|
630 | each item of the list should be as follow: |
---|
631 | item=[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
632 | """ |
---|
633 | is_modified = modified |
---|
634 | if len(list)==0: |
---|
635 | return is_modified |
---|
636 | for item in list: |
---|
637 | try: |
---|
638 | name = str(item[1]) |
---|
639 | value= float(item[2].GetValue()) |
---|
640 | # If the value of the parameter has changed, |
---|
641 | # update the model and set the is_modified flag |
---|
642 | if value != self.model.getParam(name): |
---|
643 | self.model.setParam(name,value) |
---|
644 | is_modified = True |
---|
645 | except: |
---|
646 | msg= "Model Drawing Error:wrong value entered : %s"% sys.exc_value |
---|
647 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
648 | return |
---|
649 | return is_modified |
---|
650 | |
---|
651 | |
---|
652 | |
---|
653 | def _set_dipers_Param(self, event): |
---|
654 | """ |
---|
655 | Add more item to select user dispersity |
---|
656 | """ |
---|
657 | if self.model == None: |
---|
658 | msg= " Select non - model value:%s !"%self.model |
---|
659 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
660 | return |
---|
661 | else: |
---|
662 | if self.enable_disp.GetValue(): |
---|
663 | self.model_disp.Show(True) |
---|
664 | self.disp_box.Show(True) |
---|
665 | ## layout for model containing no dispersity parameters |
---|
666 | if len(self.disp_list)==0: |
---|
667 | self._layout_sizer_noDipers() |
---|
668 | else: |
---|
669 | ## set gaussian sizer |
---|
670 | self._set_sizer_gaussian() |
---|
671 | else: |
---|
672 | self.model_disp.Hide() |
---|
673 | self.disp_box.Hide() |
---|
674 | self.sizer4_4.Clear(True) |
---|
675 | self._reset_dispersity() |
---|
676 | self._draw_model() |
---|
677 | |
---|
678 | self.sizer4_4.Layout() |
---|
679 | self.sizer4.Layout() |
---|
680 | self.SetScrollbars(20,20,200,100) |
---|
681 | |
---|
682 | |
---|
683 | def _layout_sizer_noDipers(self): |
---|
684 | """ |
---|
685 | Draw a sizer with no dispersity info |
---|
686 | """ |
---|
687 | ix=0 |
---|
688 | iy=1 |
---|
689 | self.fittable_param=[] |
---|
690 | self.fixed_param=[] |
---|
691 | self.model_disp.Hide() |
---|
692 | self.disp_box.Hide() |
---|
693 | self.sizer4_4.Clear(True) |
---|
694 | model_disp = wx.StaticText(self, -1, 'No PolyDispersity for this model') |
---|
695 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
696 | self.sizer4_4.Layout() |
---|
697 | self.sizer4.Layout() |
---|
698 | self.SetScrollbars(20,20,200,100) |
---|
699 | return |
---|
700 | |
---|
701 | def _reset_gaussian_dispers(self): |
---|
702 | """ |
---|
703 | reset model paramaters to gaussian |
---|
704 | TO IMPLEMENT |
---|
705 | """ |
---|
706 | |
---|
707 | return |
---|
708 | from sans.models.dispersion_models import GaussianDispersion |
---|
709 | if self.model==None: |
---|
710 | return |
---|
711 | list= self.model.dispersion.keys() |
---|
712 | for key in list: |
---|
713 | |
---|
714 | ## The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
715 | disp_model = GaussianDispersion() |
---|
716 | ## Set the new model as the dispersion object for the selected parameter |
---|
717 | self.model.set_dispersion(key,disp_model) |
---|
718 | self._draw_model() |
---|
719 | |
---|
720 | |
---|
721 | def _reset_dispersity(self): |
---|
722 | """ |
---|
723 | put gaussian dispersity into current model |
---|
724 | """ |
---|
725 | self.fittable_param=[] |
---|
726 | self.fixed_param=[] |
---|
727 | from sans.models.dispersion_models import GaussianDispersion |
---|
728 | if len(self.disp_cb_dict)==0: |
---|
729 | return |
---|
730 | |
---|
731 | for p in self.disp_cb_dict: |
---|
732 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
733 | disp_model = GaussianDispersion() |
---|
734 | # Store the object to make it persist outside the scope of this method |
---|
735 | #TODO: refactor model to clean this up? |
---|
736 | self._disp_obj_dict[p] = disp_model |
---|
737 | |
---|
738 | # Set the new model as the dispersion object for the selected parameter |
---|
739 | self.model.set_dispersion(p, disp_model) |
---|
740 | |
---|
741 | # Redraw the model |
---|
742 | |
---|
743 | |
---|
744 | |
---|
745 | def _on_select_Disp(self,event): |
---|
746 | """ |
---|
747 | allow selecting different dispersion |
---|
748 | self.disp_list should change type later .now only gaussian |
---|
749 | """ |
---|
750 | dispersity =event.GetClientData() |
---|
751 | name= dispersity.__name__ |
---|
752 | |
---|
753 | if name == "GaussianModel": |
---|
754 | self._set_sizer_gaussian() |
---|
755 | |
---|
756 | if name=="ArrayDispersion": |
---|
757 | self._set_sizer_arraydispersion() |
---|
758 | |
---|
759 | |
---|
760 | |
---|
761 | |
---|
762 | def _set_sizer_arraydispersion(self): |
---|
763 | """ |
---|
764 | draw sizer with array dispersity parameters |
---|
765 | """ |
---|
766 | self.sizer4_4.Clear(True) |
---|
767 | ix=0 |
---|
768 | iy=1 |
---|
769 | disp1 = wx.StaticText(self, -1, 'Array Dispersion') |
---|
770 | self.sizer4_4.Add(disp1,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
771 | |
---|
772 | # Look for model parameters to which we can apply an ArrayDispersion model |
---|
773 | # Add a check box for each parameter. |
---|
774 | self.disp_cb_dict = {} |
---|
775 | for p in self.model.dispersion.keys(): |
---|
776 | ix+=1 |
---|
777 | self.disp_cb_dict[p] = wx.CheckBox(self, -1, p, (10, 10)) |
---|
778 | |
---|
779 | wx.EVT_CHECKBOX(self, self.disp_cb_dict[p].GetId(), self.select_disp_angle) |
---|
780 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
781 | |
---|
782 | ix =0 |
---|
783 | iy +=1 |
---|
784 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
785 | self.sizer4_4.Layout() |
---|
786 | self.sizer4.Layout() |
---|
787 | self.SetScrollbars(20,20,200,100) |
---|
788 | |
---|
789 | |
---|
790 | def _set_range_sizer(self, title, object1=None,object=None): |
---|
791 | """ |
---|
792 | Fill the |
---|
793 | """ |
---|
794 | self.sizer5.Clear(True) |
---|
795 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
796 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
797 | #-------------------------------------------------------------- |
---|
798 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
799 | self.qmin.SetValue(format_number(self.qmin_x)) |
---|
800 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
801 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
802 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
803 | |
---|
804 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
805 | self.qmax.SetValue(format_number(self.qmax_x)) |
---|
806 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
807 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
808 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
809 | |
---|
810 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
811 | sizer= wx.GridSizer(3, 3,5, 5) |
---|
812 | |
---|
813 | sizer.Add((5,5)) |
---|
814 | sizer.Add(wx.StaticText(self, -1, 'Min')) |
---|
815 | sizer.Add(wx.StaticText(self, -1, 'Max')) |
---|
816 | sizer.Add(wx.StaticText(self, -1, 'Q range')) |
---|
817 | |
---|
818 | sizer.Add(self.qmin) |
---|
819 | sizer.Add(self.qmax) |
---|
820 | sizer_horizontal.Add(sizer) |
---|
821 | if object!=None: |
---|
822 | sizer_horizontal.Add(object) |
---|
823 | |
---|
824 | if object1!=None: |
---|
825 | boxsizer1.Add(object1) |
---|
826 | boxsizer1.Add((10,10)) |
---|
827 | boxsizer1.Add(sizer_horizontal) |
---|
828 | |
---|
829 | #---------------------------------------------------------------- |
---|
830 | self.sizer5.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
831 | self.sizer5.Layout() |
---|
832 | self.SetScrollbars(20,20,200,100) |
---|
833 | |
---|
834 | |
---|
835 | |
---|