1 | |
---|
2 | import sys, os |
---|
3 | import wx |
---|
4 | import numpy |
---|
5 | import time |
---|
6 | import copy |
---|
7 | import math |
---|
8 | from sans.guiframe.utils import format_number,check_float |
---|
9 | from sans.guicomm.events import StatusEvent |
---|
10 | import pagestate |
---|
11 | from pagestate import PageState |
---|
12 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
13 | (PreviousStateEvent, EVT_PREVIOUS_STATE) = wx.lib.newevent.NewEvent() |
---|
14 | (NextStateEvent, EVT_NEXT_STATE) = wx.lib.newevent.NewEvent() |
---|
15 | _BOX_WIDTH = 76 |
---|
16 | _QMIN_DEFAULT = 0.001 |
---|
17 | _QMAX_DEFAULT = 0.13 |
---|
18 | _NPTS_DEFAULT = 50 |
---|
19 | #Control panel width |
---|
20 | if sys.platform.count("win32")>0: |
---|
21 | PANEL_WIDTH = 450 |
---|
22 | else: |
---|
23 | PANEL_WIDTH = 500 |
---|
24 | |
---|
25 | class BasicPage(wx.ScrolledWindow): |
---|
26 | """ |
---|
27 | This class provide general structure of fitpanel page |
---|
28 | """ |
---|
29 | ## Internal name for the AUI manager |
---|
30 | window_name = "Basic Page" |
---|
31 | ## Title to appear on top of the window |
---|
32 | window_caption = "Basic page " |
---|
33 | |
---|
34 | def __init__(self,parent, page_info): |
---|
35 | wx.ScrolledWindow.__init__(self, parent, |
---|
36 | style= wx.FULL_REPAINT_ON_RESIZE ) |
---|
37 | ##window_name |
---|
38 | self.window_name = page_info.window_name |
---|
39 | ##window_caption |
---|
40 | self.window_caption = page_info.window_caption |
---|
41 | ## parent of the page |
---|
42 | self.parent = parent |
---|
43 | ## manager is the fitting plugin |
---|
44 | self.manager= page_info.manager |
---|
45 | ## owner of the page (fitting plugin) |
---|
46 | self.event_owner= page_info.event_owner |
---|
47 | ## current model |
---|
48 | self.model = page_info.model |
---|
49 | ## data |
---|
50 | self.data = page_info.data |
---|
51 | ## dictionary containing list of models |
---|
52 | self.model_list_box = page_info.model_list_box |
---|
53 | ## Data member to store the dispersion object created |
---|
54 | self._disp_obj_dict = {} |
---|
55 | ## selected parameters to apply dispersion |
---|
56 | self.disp_cb_dict ={} |
---|
57 | |
---|
58 | ## smearer object |
---|
59 | self.smearer = None |
---|
60 | |
---|
61 | ##list of model parameters. each item must have same length |
---|
62 | ## each item related to a given parameters |
---|
63 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
64 | self.parameters=[] |
---|
65 | ## list of parameters to fit , must be like self.parameters |
---|
66 | self.param_toFit=[] |
---|
67 | ## list of looking like parameters but with non fittable parameters info |
---|
68 | self.fixed_param=[] |
---|
69 | ## list of looking like parameters but with fittable parameters info |
---|
70 | self.fittable_param=[] |
---|
71 | ##list of dispersion parameters |
---|
72 | self.disp_list=[] |
---|
73 | self.disp_name="" |
---|
74 | ## list of orientation parameters |
---|
75 | self.orientation_params=[] |
---|
76 | self.orientation_params_disp=[] |
---|
77 | if self.model !=None: |
---|
78 | self.disp_list= self.model.getDispParamList() |
---|
79 | |
---|
80 | ##enable model 2D draw |
---|
81 | self.enable2D= False |
---|
82 | ## check that the fit range is correct to plot the model again |
---|
83 | self.fitrange= True |
---|
84 | |
---|
85 | ## Q range |
---|
86 | self.qmin_x= _QMIN_DEFAULT |
---|
87 | self.qmax_x= _QMAX_DEFAULT |
---|
88 | self.num_points= _NPTS_DEFAULT |
---|
89 | |
---|
90 | ## Create memento to save the current state |
---|
91 | self.state= PageState(parent= self.parent,model=self.model, data=self.data) |
---|
92 | ## flag to determine if state has change |
---|
93 | self.state_change= False |
---|
94 | ## save customized array |
---|
95 | self.values=[] |
---|
96 | self.weights=[] |
---|
97 | ## retrieve saved state |
---|
98 | self.number_saved_state= 0 |
---|
99 | ## dictionary of saved state |
---|
100 | self.saved_states={} |
---|
101 | |
---|
102 | ## Create context menu for page |
---|
103 | self.popUpMenu = wx.Menu() |
---|
104 | #id = wx.NewId() |
---|
105 | #self._undo = wx.MenuItem(self.popUpMenu,id, "Undo","cancel the previous action") |
---|
106 | #self.popUpMenu.AppendItem(self._undo) |
---|
107 | #self._undo.Enable(False) |
---|
108 | #wx.EVT_MENU(self, id, self.onUndo) |
---|
109 | |
---|
110 | #id = wx.NewId() |
---|
111 | #self._redo = wx.MenuItem(self.popUpMenu,id,"Redo"," Restore the previous action") |
---|
112 | #self.popUpMenu.AppendItem(self._redo) |
---|
113 | #self._redo.Enable(False) |
---|
114 | #wx.EVT_MENU(self, id, self.onRedo) |
---|
115 | #self.popUpMenu.AppendSeparator() |
---|
116 | #if sys.platform.count("win32")>0: |
---|
117 | id = wx.NewId() |
---|
118 | self._keep = wx.MenuItem(self.popUpMenu,id,"BookMark"," Keep the panel status to recall it later") |
---|
119 | self.popUpMenu.AppendItem(self._keep) |
---|
120 | self._keep.Enable(True) |
---|
121 | wx.EVT_MENU(self, id, self.onSave) |
---|
122 | self.popUpMenu.AppendSeparator() |
---|
123 | |
---|
124 | ## Default locations |
---|
125 | self._default_save_location = os.getcwd() |
---|
126 | ## save initial state on context menu |
---|
127 | #self.onSave(event=None) |
---|
128 | self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) |
---|
129 | |
---|
130 | ## create the basic structure of the panel with empty sizer |
---|
131 | self.define_page_structure() |
---|
132 | ## drawing Initial dispersion parameters sizer |
---|
133 | self.set_dispers_sizer() |
---|
134 | self._fill_save_sizer() |
---|
135 | ## layout |
---|
136 | self.set_layout() |
---|
137 | |
---|
138 | class ModelTextCtrl(wx.TextCtrl): |
---|
139 | """ |
---|
140 | Text control for model and fit parameters. |
---|
141 | Binds the appropriate events for user interactions. |
---|
142 | Default callback methods can be overwritten on initialization |
---|
143 | |
---|
144 | @param kill_focus_callback: callback method for EVT_KILL_FOCUS event |
---|
145 | @param set_focus_callback: callback method for EVT_SET_FOCUS event |
---|
146 | @param mouse_up_callback: callback method for EVT_LEFT_UP event |
---|
147 | @param text_enter_callback: callback method for EVT_TEXT_ENTER event |
---|
148 | """ |
---|
149 | ## Set to True when the mouse is clicked while the whole string is selected |
---|
150 | full_selection = False |
---|
151 | ## Call back for EVT_SET_FOCUS events |
---|
152 | _on_set_focus_callback = None |
---|
153 | |
---|
154 | def __init__(self, parent, id=-1, value=wx.EmptyString, pos=wx.DefaultPosition, |
---|
155 | size=wx.DefaultSize, style=0, validator=wx.DefaultValidator, name=wx.TextCtrlNameStr, |
---|
156 | kill_focus_callback = None, set_focus_callback = None, |
---|
157 | mouse_up_callback = None, text_enter_callback = None): |
---|
158 | |
---|
159 | wx.TextCtrl.__init__(self, parent, id, value, pos, size, style, validator, name) |
---|
160 | |
---|
161 | # Bind appropriate events |
---|
162 | self._on_set_focus_callback = parent.onSetFocus \ |
---|
163 | if set_focus_callback is None else set_focus_callback |
---|
164 | self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus) |
---|
165 | self.Bind(wx.EVT_KILL_FOCUS, parent._onparamEnter \ |
---|
166 | if kill_focus_callback is None else kill_focus_callback) |
---|
167 | self.Bind(wx.EVT_TEXT_ENTER, parent._onparamEnter \ |
---|
168 | if text_enter_callback is None else text_enter_callback) |
---|
169 | self.Bind(wx.EVT_LEFT_UP, self._highlight_text \ |
---|
170 | if mouse_up_callback is None else mouse_up_callback) |
---|
171 | |
---|
172 | def _on_set_focus(self, event): |
---|
173 | """ |
---|
174 | Catch when the text control is set in focus to highlight the whole |
---|
175 | text if necessary |
---|
176 | @param event: mouse event |
---|
177 | """ |
---|
178 | event.Skip() |
---|
179 | self.full_selection = True |
---|
180 | return self._on_set_focus_callback(event) |
---|
181 | |
---|
182 | def _highlight_text(self, event): |
---|
183 | """ |
---|
184 | Highlight text of a TextCtrl only of no text has be selected |
---|
185 | @param event: mouse event |
---|
186 | """ |
---|
187 | # Make sure the mouse event is available to other listeners |
---|
188 | event.Skip() |
---|
189 | control = event.GetEventObject() |
---|
190 | if self.full_selection: |
---|
191 | self.full_selection = False |
---|
192 | # Check that we have a TextCtrl |
---|
193 | if issubclass(control.__class__, wx.TextCtrl): |
---|
194 | # Check whether text has been selected, |
---|
195 | # if not, select the whole string |
---|
196 | (start, end) = control.GetSelection() |
---|
197 | if start==end: |
---|
198 | control.SetSelection(-1,-1) |
---|
199 | |
---|
200 | |
---|
201 | def onContextMenu(self, event): |
---|
202 | """ |
---|
203 | Retrieve the state selected state |
---|
204 | """ |
---|
205 | # Skipping the save state functionality for release 0.9.0 |
---|
206 | #return |
---|
207 | |
---|
208 | pos = event.GetPosition() |
---|
209 | pos = self.ScreenToClient(pos) |
---|
210 | |
---|
211 | self.PopupMenu(self.popUpMenu, pos) |
---|
212 | |
---|
213 | |
---|
214 | def onUndo(self, event): |
---|
215 | """ |
---|
216 | Cancel the previous action |
---|
217 | """ |
---|
218 | #print "enable undo" |
---|
219 | event = PreviousStateEvent(page = self) |
---|
220 | wx.PostEvent(self.parent, event) |
---|
221 | |
---|
222 | |
---|
223 | def onRedo(self, event): |
---|
224 | """ |
---|
225 | Restore the previous action cancelled |
---|
226 | """ |
---|
227 | #print "enable redo" |
---|
228 | event = NextStateEvent(page= self) |
---|
229 | wx.PostEvent(self.parent, event) |
---|
230 | |
---|
231 | |
---|
232 | def define_page_structure(self): |
---|
233 | """ |
---|
234 | Create empty sizer for a panel |
---|
235 | """ |
---|
236 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
237 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
238 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
239 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
240 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
241 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
242 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
243 | self.sizer6 = wx.BoxSizer(wx.VERTICAL) |
---|
244 | |
---|
245 | self.sizer0.SetMinSize((PANEL_WIDTH,-1)) |
---|
246 | self.sizer1.SetMinSize((PANEL_WIDTH,-1)) |
---|
247 | self.sizer2.SetMinSize((PANEL_WIDTH,-1)) |
---|
248 | self.sizer3.SetMinSize((PANEL_WIDTH,-1)) |
---|
249 | self.sizer4.SetMinSize((PANEL_WIDTH,-1)) |
---|
250 | self.sizer5.SetMinSize((PANEL_WIDTH,-1)) |
---|
251 | #self.sizer6.SetMinSize((375,-1)) |
---|
252 | |
---|
253 | self.vbox.Add(self.sizer0) |
---|
254 | self.vbox.Add(self.sizer1) |
---|
255 | self.vbox.Add(self.sizer2) |
---|
256 | self.vbox.Add(self.sizer3) |
---|
257 | self.vbox.Add(self.sizer4) |
---|
258 | self.vbox.Add(self.sizer5) |
---|
259 | #self.vbox.Add(self.sizer6) |
---|
260 | |
---|
261 | |
---|
262 | def set_layout(self): |
---|
263 | """ |
---|
264 | layout |
---|
265 | """ |
---|
266 | self.vbox.Layout() |
---|
267 | self.vbox.Fit(self) |
---|
268 | self.SetSizer(self.vbox) |
---|
269 | |
---|
270 | self.set_scroll() |
---|
271 | self.Centre() |
---|
272 | |
---|
273 | |
---|
274 | def set_scroll(self): |
---|
275 | self.SetScrollbars(20,20,25,65) |
---|
276 | self.Layout() |
---|
277 | self.SetAutoLayout(True) |
---|
278 | |
---|
279 | |
---|
280 | def set_owner(self,owner): |
---|
281 | """ |
---|
282 | set owner of fitpage |
---|
283 | @param owner: the class responsible of plotting |
---|
284 | """ |
---|
285 | self.event_owner = owner |
---|
286 | self.state.event_owner = owner |
---|
287 | |
---|
288 | def set_manager(self, manager): |
---|
289 | """ |
---|
290 | set panel manager |
---|
291 | @param manager: instance of plugin fitting |
---|
292 | """ |
---|
293 | self.manager = manager |
---|
294 | self.state.manager = manager |
---|
295 | |
---|
296 | def populate_box(self, dict): |
---|
297 | """ |
---|
298 | Store list of model |
---|
299 | @param dict: dictionary containing list of models |
---|
300 | """ |
---|
301 | self.model_list_box = dict |
---|
302 | self.state.model_list_box = self.model_list_box |
---|
303 | |
---|
304 | |
---|
305 | |
---|
306 | def set_dispers_sizer(self): |
---|
307 | """ |
---|
308 | fill sizer containing dispersity info |
---|
309 | """ |
---|
310 | self.sizer4.Clear(True) |
---|
311 | name="Polydispersity and Orientational Distribution" |
---|
312 | box_description= wx.StaticBox(self, -1,name) |
---|
313 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
314 | #---------------------------------------------------- |
---|
315 | self.disable_disp = wx.RadioButton(self, -1, 'Off', (10, 10), style=wx.RB_GROUP) |
---|
316 | self.enable_disp = wx.RadioButton(self, -1, 'On', (10, 30)) |
---|
317 | |
---|
318 | |
---|
319 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.disable_disp.GetId()) |
---|
320 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.enable_disp.GetId()) |
---|
321 | #MAC needs SetValue |
---|
322 | self.disable_disp.SetValue(True) |
---|
323 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
324 | sizer_dispersion.Add((20,20)) |
---|
325 | name=""#Polydispersity and \nOrientational Distribution " |
---|
326 | sizer_dispersion.Add(wx.StaticText(self,-1,name)) |
---|
327 | sizer_dispersion.Add(self.enable_disp ) |
---|
328 | sizer_dispersion.Add((20,20)) |
---|
329 | sizer_dispersion.Add(self.disable_disp ) |
---|
330 | sizer_dispersion.Add((10,10)) |
---|
331 | |
---|
332 | ## fill a sizer with the combobox to select dispersion type |
---|
333 | sizer_select_dispers = wx.BoxSizer(wx.HORIZONTAL) |
---|
334 | self.model_disp = wx.StaticText(self, -1, 'Distribution Function ') |
---|
335 | |
---|
336 | import sans.models.dispersion_models |
---|
337 | self.polydisp= sans.models.dispersion_models.models |
---|
338 | self.disp_box = wx.ComboBox(self, -1) |
---|
339 | |
---|
340 | for key, value in self.polydisp.iteritems(): |
---|
341 | name = str(key) |
---|
342 | self.disp_box.Append(name,value) |
---|
343 | self.disp_box.SetStringSelection("gaussian") |
---|
344 | wx.EVT_COMBOBOX(self.disp_box,-1, self._on_select_Disp) |
---|
345 | |
---|
346 | sizer_select_dispers.Add((10,10)) |
---|
347 | sizer_select_dispers.Add(self.model_disp) |
---|
348 | sizer_select_dispers.Add(self.disp_box,0, |
---|
349 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
350 | |
---|
351 | self.model_disp.Hide() |
---|
352 | self.disp_box.Hide() |
---|
353 | |
---|
354 | boxsizer1.Add( sizer_dispersion,0, |
---|
355 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
356 | #boxsizer1.Add( (10,10) ) |
---|
357 | boxsizer1.Add( sizer_select_dispers ) |
---|
358 | self.sizer4_4 = wx.GridBagSizer(5,5) |
---|
359 | boxsizer1.Add( self.sizer4_4 ) |
---|
360 | #----------------------------------------------------- |
---|
361 | self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
362 | self.sizer4_4.Layout() |
---|
363 | self.sizer4.Layout() |
---|
364 | self.Layout() |
---|
365 | self.SetScrollbars(20,20,25,65) |
---|
366 | self.Refresh() |
---|
367 | ## saving the state of enable dispersity button |
---|
368 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
369 | self.state.disable_disp= self.disable_disp.GetValue() |
---|
370 | |
---|
371 | |
---|
372 | def select_disp_angle(self, event): |
---|
373 | """ |
---|
374 | Event for when a user select a parameter to average over. |
---|
375 | @param event: radiobutton event |
---|
376 | """ |
---|
377 | self.values=[] |
---|
378 | self.weights=[] |
---|
379 | if event.GetEventObject()==self.noDisper_rbox: |
---|
380 | if self.noDisper_rbox.GetValue(): |
---|
381 | #No array dispersity apply yet |
---|
382 | self._reset_dispersity() |
---|
383 | ## Redraw the model ??? |
---|
384 | self._draw_model() |
---|
385 | # Go through the list of dispersion check boxes to identify which one has changed |
---|
386 | for p in self.disp_cb_dict: |
---|
387 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
388 | # Catch which one of the box was just checked or unchecked. |
---|
389 | if event.GetEventObject() == self.disp_cb_dict[p]: |
---|
390 | if self.disp_cb_dict[p].GetValue() == True: |
---|
391 | |
---|
392 | ##Temp. FIX for V1.0 regarding changing checkbox to radiobutton. |
---|
393 | ##This (self._reset_dispersity) should be removed when the array dispersion is fixed. |
---|
394 | self._reset_dispersity() |
---|
395 | |
---|
396 | # The user wants this parameter to be averaged. |
---|
397 | # Pop up the file selection dialog. |
---|
398 | path = self._selectDlg() |
---|
399 | |
---|
400 | # If nothing was selected, just return |
---|
401 | if path is None: |
---|
402 | self.disp_cb_dict[p].SetValue(False) |
---|
403 | return |
---|
404 | try: |
---|
405 | self._default_save_location = os.path.dirname(path) |
---|
406 | except: |
---|
407 | pass |
---|
408 | try: |
---|
409 | self.values,self.weights = self.read_file(path) |
---|
410 | except: |
---|
411 | msg="Could not read input file" |
---|
412 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
413 | return |
---|
414 | |
---|
415 | # If any of the two arrays is empty, notify the user that we won't |
---|
416 | # proceed |
---|
417 | if self.values is None or self.weights is None or \ |
---|
418 | self.values ==[] or self.weights ==[]: |
---|
419 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
420 | "The loaded %s distrubtion is corrupted or empty" % p)) |
---|
421 | return |
---|
422 | |
---|
423 | # Tell the user that we are about to apply the distribution |
---|
424 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
425 | "Applying loaded %s distribution: %s" % (p, path))) |
---|
426 | |
---|
427 | # Create the dispersion objects |
---|
428 | from sans.models.dispersion_models import ArrayDispersion |
---|
429 | disp_model = ArrayDispersion() |
---|
430 | disp_model.set_weights(self.values, self.weights) |
---|
431 | |
---|
432 | # Store the object to make it persist outside the scope of this method |
---|
433 | #TODO: refactor model to clean this up? |
---|
434 | self._disp_obj_dict[p] = disp_model |
---|
435 | self.state._disp_obj_dict [p]= disp_model |
---|
436 | self.state.values=[] |
---|
437 | self.state.weights=[] |
---|
438 | self.state.values = copy.deepcopy(self.values) |
---|
439 | self.state.weights = copy.deepcopy(self.weights) |
---|
440 | # Set the new model as the dispersion object for the selected parameter |
---|
441 | self.model.set_dispersion(p, disp_model) |
---|
442 | # Store a reference to the weights in the model object so that |
---|
443 | # it's not lost when we use the model within another thread. |
---|
444 | #TODO: total hack - fix this |
---|
445 | self.state.model= self.model.clone() |
---|
446 | #if not hasattr(self.model, "_persistency_dict"): |
---|
447 | self.model._persistency_dict = {} |
---|
448 | self.model._persistency_dict[p] = [self.values, self.weights] |
---|
449 | self.state.model._persistency_dict[p] = [self.values, self.weights] |
---|
450 | else: |
---|
451 | self._reset_dispersity() |
---|
452 | |
---|
453 | ## Redraw the model |
---|
454 | self._draw_model() |
---|
455 | |
---|
456 | ## post state to fit panel |
---|
457 | event = PageInfoEvent(page = self) |
---|
458 | wx.PostEvent(self.parent, event) |
---|
459 | |
---|
460 | |
---|
461 | def onResetModel(self, event): |
---|
462 | """ |
---|
463 | Reset model state |
---|
464 | """ |
---|
465 | ## post help message for the selected model |
---|
466 | msg = self.popUpMenu.GetHelpString(event.GetId()) |
---|
467 | msg +=" reloaded" |
---|
468 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
469 | |
---|
470 | name= self.popUpMenu.GetLabel(event.GetId()) |
---|
471 | self._on_select_model_helper() |
---|
472 | |
---|
473 | if name in self.saved_states.keys(): |
---|
474 | previous_state = self.saved_states[name] |
---|
475 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
476 | self.reset_page(previous_state) |
---|
477 | |
---|
478 | def onSave(self, event): |
---|
479 | """ |
---|
480 | save history of the data and model |
---|
481 | """ |
---|
482 | if self.model==None: |
---|
483 | return |
---|
484 | if hasattr(self,"enable_disp"): |
---|
485 | self.state.enable_disp = copy.deepcopy(self.enable_disp.GetValue()) |
---|
486 | if hasattr(self, "disp_box"): |
---|
487 | self.state.disp_box = copy.deepcopy(self.disp_box.GetSelection()) |
---|
488 | |
---|
489 | self.state.model = self.model.clone() |
---|
490 | self.state.model.name= self.model.name |
---|
491 | #if not hasattr(self.model, "_persistency_dict"): |
---|
492 | #self.model._persistency_dict = {} |
---|
493 | #self.state.model._persistency_dict= copy.deepcopy(self.model._persistency_dict) |
---|
494 | |
---|
495 | #Remember fit engine_type for fit panel |
---|
496 | if self.engine_type == None and self.manager !=None: |
---|
497 | self.engine_type = self.manager._return_engine_type() |
---|
498 | |
---|
499 | self.state.engine_type = self.engine_type |
---|
500 | |
---|
501 | new_state = self.state.clone() |
---|
502 | new_state.model.name = self.state.model.name |
---|
503 | |
---|
504 | new_state.enable2D = copy.deepcopy(self.enable2D) |
---|
505 | ##Add model state on context menu |
---|
506 | self.number_saved_state += 1 |
---|
507 | #name= self.model.name+"[%g]"%self.number_saved_state |
---|
508 | name= self.model.__class__.__name__+"[%g]"%self.number_saved_state |
---|
509 | self.saved_states[name]= new_state |
---|
510 | |
---|
511 | ## Add item in the context menu |
---|
512 | |
---|
513 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
514 | my_time= str(hour)+" : "+str(minute)+" : "+str(second)+" " |
---|
515 | date= str( month)+"|"+str(day)+"|"+str(year) |
---|
516 | msg= "Model saved at %s on %s"%(my_time, date) |
---|
517 | ## post help message for the selected model |
---|
518 | msg +=" Saved! right click on this page to retrieve this model" |
---|
519 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
520 | |
---|
521 | id = wx.NewId() |
---|
522 | self.popUpMenu.Append(id,name,str(msg)) |
---|
523 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
524 | |
---|
525 | def onSetFocus(self, evt): |
---|
526 | """ |
---|
527 | highlight the current textcrtl and hide the error text control shown |
---|
528 | after fitting |
---|
529 | :Not implemented. |
---|
530 | """ |
---|
531 | return |
---|
532 | |
---|
533 | def read_file(self, path): |
---|
534 | """ |
---|
535 | Read two columns file |
---|
536 | @param path: the path to the file to read |
---|
537 | """ |
---|
538 | try: |
---|
539 | if path==None: |
---|
540 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
541 | " Selected Distribution was not loaded: %s"%path)) |
---|
542 | return None, None |
---|
543 | input_f = open(path, 'r') |
---|
544 | buff = input_f.read() |
---|
545 | lines = buff.split('\n') |
---|
546 | |
---|
547 | angles = [] |
---|
548 | weights=[] |
---|
549 | for line in lines: |
---|
550 | toks = line.split() |
---|
551 | try: |
---|
552 | angle = float(toks[0]) |
---|
553 | weight = float(toks[1]) |
---|
554 | except: |
---|
555 | # Skip non-data lines |
---|
556 | pass |
---|
557 | angles.append(angle) |
---|
558 | weights.append(weight) |
---|
559 | return numpy.array(angles), numpy.array(weights) |
---|
560 | except: |
---|
561 | raise |
---|
562 | |
---|
563 | |
---|
564 | def createMemento(self): |
---|
565 | """ |
---|
566 | return the current state of the page |
---|
567 | """ |
---|
568 | return self.state.clone() |
---|
569 | |
---|
570 | |
---|
571 | def save_current_state(self): |
---|
572 | """ |
---|
573 | Store current state |
---|
574 | """ |
---|
575 | ## save model option |
---|
576 | if self.model!= None: |
---|
577 | self.disp_list= self.model.getDispParamList() |
---|
578 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
579 | self.state.model = self.model.clone() |
---|
580 | |
---|
581 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
582 | self.state.values= copy.deepcopy(self.values) |
---|
583 | self.state.weights = copy.deepcopy( self.weights) |
---|
584 | ## save data |
---|
585 | self.state.data= copy.deepcopy(self.data) |
---|
586 | try: |
---|
587 | n = self.disp_box.GetCurrentSelection() |
---|
588 | dispersity= self.disp_box.GetClientData(n) |
---|
589 | name= dispersity.__name__ |
---|
590 | self.disp_name = name |
---|
591 | if name == "GaussianDispersion" : |
---|
592 | if hasattr(self,"cb1"): |
---|
593 | self.state.cb1= self.cb1.GetValue() |
---|
594 | except: |
---|
595 | pass |
---|
596 | |
---|
597 | if hasattr(self,"enable_disp"): |
---|
598 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
599 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
600 | |
---|
601 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
602 | if hasattr(self,"enable_smearer"): |
---|
603 | self.state.enable_smearer = copy.deepcopy(self.enable_smearer.GetValue()) |
---|
604 | self.state.disable_smearer = copy.deepcopy(self.disable_smearer.GetValue()) |
---|
605 | |
---|
606 | if hasattr(self,"disp_box"): |
---|
607 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
608 | |
---|
609 | if len(self.disp_cb_dict)>0: |
---|
610 | for k , v in self.disp_cb_dict.iteritems(): |
---|
611 | |
---|
612 | if v ==None : |
---|
613 | self.state.disp_cb_dict[k]= v |
---|
614 | else: |
---|
615 | try: |
---|
616 | self.state.disp_cb_dict[k]=v.GetValue() |
---|
617 | except: |
---|
618 | self.state.disp_cb_dict[k]= None |
---|
619 | |
---|
620 | if len(self._disp_obj_dict)>0: |
---|
621 | for k , v in self._disp_obj_dict.iteritems(): |
---|
622 | |
---|
623 | self.state._disp_obj_dict[k]= v |
---|
624 | |
---|
625 | |
---|
626 | self.state.values = copy.deepcopy(self.values) |
---|
627 | self.state.weights = copy.deepcopy(self.weights) |
---|
628 | ## save plotting range |
---|
629 | self._save_plotting_range() |
---|
630 | |
---|
631 | self.state.orientation_params =[] |
---|
632 | self.state.orientation_params_disp =[] |
---|
633 | self.state.parameters =[] |
---|
634 | self.state.fittable_param =[] |
---|
635 | self.state.fixed_param =[] |
---|
636 | |
---|
637 | |
---|
638 | ## save checkbutton state and txtcrtl values |
---|
639 | self._copy_parameters_state(self.orientation_params, |
---|
640 | self.state.orientation_params) |
---|
641 | self._copy_parameters_state(self.orientation_params_disp, |
---|
642 | self.state.orientation_params_disp) |
---|
643 | |
---|
644 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
645 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
646 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
647 | |
---|
648 | |
---|
649 | def save_current_state_fit(self): |
---|
650 | """ |
---|
651 | Store current state for fit_page |
---|
652 | """ |
---|
653 | ## save model option |
---|
654 | if self.model!= None: |
---|
655 | self.disp_list= self.model.getDispParamList() |
---|
656 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
657 | self.state.model = self.model.clone() |
---|
658 | #if hasattr(self,self.engine_type)> 0: |
---|
659 | #self.state.engine_type = self.engine_type.clone() |
---|
660 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
661 | self.state.values= copy.deepcopy(self.values) |
---|
662 | self.state.weights = copy.deepcopy( self.weights) |
---|
663 | ## save data |
---|
664 | self.state.data= copy.deepcopy(self.data) |
---|
665 | try: |
---|
666 | n = self.disp_box.GetCurrentSelection() |
---|
667 | dispersity= self.disp_box.GetClientData(n) |
---|
668 | name= dispersity.__name__ |
---|
669 | self.disp_name = name |
---|
670 | if name == "GaussianDispersion" : |
---|
671 | if hasattr(self,"cb1"): |
---|
672 | self.state.cb1= self.cb1.GetValue() |
---|
673 | |
---|
674 | except: |
---|
675 | pass |
---|
676 | |
---|
677 | if hasattr(self,"enable_disp"): |
---|
678 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
679 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
680 | |
---|
681 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
682 | if hasattr(self,"enable_smearer"): |
---|
683 | self.state.enable_smearer = copy.deepcopy(self.enable_smearer.GetValue()) |
---|
684 | self.state.disable_smearer = copy.deepcopy(self.disable_smearer.GetValue()) |
---|
685 | |
---|
686 | if hasattr(self,"disp_box"): |
---|
687 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
688 | |
---|
689 | if len(self.disp_cb_dict)>0: |
---|
690 | for k , v in self.disp_cb_dict.iteritems(): |
---|
691 | |
---|
692 | if v ==None : |
---|
693 | self.state.disp_cb_dict[k]= v |
---|
694 | else: |
---|
695 | try: |
---|
696 | self.state.disp_cb_dict[k]=v.GetValue() |
---|
697 | except: |
---|
698 | self.state.disp_cb_dict[k]= None |
---|
699 | |
---|
700 | if len(self._disp_obj_dict)>0: |
---|
701 | for k , v in self._disp_obj_dict.iteritems(): |
---|
702 | |
---|
703 | self.state._disp_obj_dict[k]= v |
---|
704 | |
---|
705 | |
---|
706 | self.state.values = copy.deepcopy(self.values) |
---|
707 | self.state.weights = copy.deepcopy(self.weights) |
---|
708 | |
---|
709 | ## save plotting range |
---|
710 | self._save_plotting_range() |
---|
711 | |
---|
712 | #self.state.orientation_params =[] |
---|
713 | #self.state.orientation_params_disp =[] |
---|
714 | #self.state.parameters =[] |
---|
715 | #self.state.fittable_param =[] |
---|
716 | #self.state.fixed_param =[] |
---|
717 | |
---|
718 | |
---|
719 | ## save checkbutton state and txtcrtl values |
---|
720 | self._copy_parameters_state(self.orientation_params, |
---|
721 | self.state.orientation_params) |
---|
722 | self._copy_parameters_state(self.orientation_params_disp, |
---|
723 | self.state.orientation_params_disp) |
---|
724 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
725 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
726 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
727 | |
---|
728 | |
---|
729 | def reset_page_helper(self, state): |
---|
730 | """ |
---|
731 | Use page_state and change the state of existing page |
---|
732 | @precondition: the page is already drawn or created |
---|
733 | @postcondition: the state of the underlying data change as well as the |
---|
734 | state of the graphic interface |
---|
735 | """ |
---|
736 | if state ==None: |
---|
737 | #self._undo.Enable(False) |
---|
738 | return |
---|
739 | |
---|
740 | self.model= state.model |
---|
741 | self.data = state.data |
---|
742 | if self.data !=None: |
---|
743 | from DataLoader.qsmearing import smear_selection |
---|
744 | self.smearer= smear_selection( self.data ) |
---|
745 | self.enable2D= state.enable2D |
---|
746 | self.engine_type = state.engine_type |
---|
747 | |
---|
748 | #??? |
---|
749 | self.disp_cb_dict = state.disp_cb_dict |
---|
750 | self.disp_list = state.disp_list |
---|
751 | |
---|
752 | ## set the state of the radio box |
---|
753 | self.shape_rbutton.SetValue(state.shape_rbutton ) |
---|
754 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
755 | self.struct_rbutton.SetValue(state.struct_rbutton ) |
---|
756 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
757 | ##draw sizer containing model parameters value for the current model |
---|
758 | self._set_model_sizer_selection( self.model ) |
---|
759 | self.set_model_param_sizer(self.model) |
---|
760 | |
---|
761 | ## reset value of combox box |
---|
762 | self.structurebox.SetSelection(state.structurecombobox ) |
---|
763 | self.formfactorbox.SetSelection(state.formfactorcombobox) |
---|
764 | |
---|
765 | |
---|
766 | ## enable the view 2d button if this is a modelpage type |
---|
767 | if hasattr(self,"model_view"): |
---|
768 | if self.enable2D: |
---|
769 | self.model_view.Disable() |
---|
770 | else: |
---|
771 | self.model_view.Enable() |
---|
772 | ## set the select all check box to the a given state |
---|
773 | if hasattr(self, "cb1"): |
---|
774 | self.cb1.SetValue(state.cb1) |
---|
775 | |
---|
776 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
777 | |
---|
778 | self._reset_parameters_state(self.orientation_params_disp, |
---|
779 | state.orientation_params_disp) |
---|
780 | self._reset_parameters_state(self.orientation_params, |
---|
781 | state.orientation_params) |
---|
782 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
783 | ## display dispersion info layer |
---|
784 | self.enable_disp.SetValue(state.enable_disp) |
---|
785 | self.disable_disp.SetValue(state.disable_disp) |
---|
786 | |
---|
787 | if hasattr(self, "disp_box"): |
---|
788 | |
---|
789 | self.disp_box.SetSelection(state.disp_box) |
---|
790 | n= self.disp_box.GetCurrentSelection() |
---|
791 | dispersity= self.disp_box.GetClientData(n) |
---|
792 | name= dispersity.__name__ |
---|
793 | |
---|
794 | self._set_dipers_Param(event=None) |
---|
795 | |
---|
796 | if name=="ArrayDispersion": |
---|
797 | |
---|
798 | for item in self.disp_cb_dict.keys(): |
---|
799 | |
---|
800 | if hasattr(self.disp_cb_dict[item],"SetValue") : |
---|
801 | self.disp_cb_dict[item].SetValue(state.disp_cb_dict[item]) |
---|
802 | # Create the dispersion objects |
---|
803 | from sans.models.dispersion_models import ArrayDispersion |
---|
804 | disp_model = ArrayDispersion() |
---|
805 | if hasattr(state,"values")and self.disp_cb_dict[item].GetValue()==True: |
---|
806 | if len(state.values)>0: |
---|
807 | self.values=state.values |
---|
808 | self.weights=state.weights |
---|
809 | disp_model.set_weights(self.values, state.weights) |
---|
810 | else: |
---|
811 | self._reset_dispersity() |
---|
812 | |
---|
813 | self._disp_obj_dict[item] = disp_model |
---|
814 | # Set the new model as the dispersion object for the selected parameter |
---|
815 | self.model.set_dispersion(item, disp_model) |
---|
816 | |
---|
817 | #self.model._persistency_dict = {} |
---|
818 | self.model._persistency_dict[item] = [state.values, state.weights] |
---|
819 | |
---|
820 | else: |
---|
821 | |
---|
822 | for k,v in self.state.disp_cb_dict.iteritems(): |
---|
823 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
824 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
825 | |
---|
826 | ##plotting range restore |
---|
827 | self._reset_plotting_range(state) |
---|
828 | ## smearing info restore |
---|
829 | if hasattr(self,"enable_smearer"): |
---|
830 | ## set smearing value whether or not the data contain the smearing info |
---|
831 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
832 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
833 | self.onSmear(event=None) |
---|
834 | self.tcChi.SetLabel(str( state.tcChi)) |
---|
835 | self.sizer5.Layout() |
---|
836 | self.Layout() |
---|
837 | self.Refresh() |
---|
838 | |
---|
839 | |
---|
840 | ## reset state of checkbox,textcrtl and dispersity parameters value |
---|
841 | self._reset_parameters_state(self.fittable_param,state.fittable_param) |
---|
842 | self._reset_parameters_state(self.fixed_param,state.fixed_param) |
---|
843 | |
---|
844 | ## draw the model with previous parameters value |
---|
845 | self._onparamEnter_helper() |
---|
846 | |
---|
847 | ## reset context menu items |
---|
848 | self._reset_context_menu() |
---|
849 | |
---|
850 | ## set the value of the current state to the state given as parameter |
---|
851 | self.state = state.clone() |
---|
852 | self._draw_model() |
---|
853 | |
---|
854 | def _clear_Err_on_Fit(self): |
---|
855 | """ |
---|
856 | hide the error text control shown |
---|
857 | after fitting |
---|
858 | """ |
---|
859 | |
---|
860 | if hasattr(self,"text2_3"): |
---|
861 | self.text2_3.Hide() |
---|
862 | |
---|
863 | if len(self.parameters)>0: |
---|
864 | for item in self.parameters: |
---|
865 | ## hide statictext +/- |
---|
866 | if item[3]!=None and item[3].IsShown(): |
---|
867 | item[3].Hide() |
---|
868 | ## hide textcrtl for error after fit |
---|
869 | if item[4]!=None and item[4].IsShown(): |
---|
870 | item[4].Clear() |
---|
871 | item[4].Hide() |
---|
872 | if len(self.fittable_param)>0: |
---|
873 | for item in self.fittable_param: |
---|
874 | ## hide statictext +/- |
---|
875 | if item[3]!=None and item[3].IsShown(): |
---|
876 | item[3].Hide() |
---|
877 | ## hide textcrtl for error after fit |
---|
878 | if item[4]!=None and item[4].IsShown(): |
---|
879 | item[4].Clear() |
---|
880 | item[4].Hide() |
---|
881 | ##Is this layout necessary? Had a problem w/MAC. |
---|
882 | #self.Layout() |
---|
883 | |
---|
884 | return |
---|
885 | def _selectDlg(self): |
---|
886 | """ |
---|
887 | open a dialog file to selected the customized dispersity |
---|
888 | """ |
---|
889 | import os |
---|
890 | dlg = wx.FileDialog(self, "Choose a weight file", |
---|
891 | self._default_save_location , "", "*.*", wx.OPEN) |
---|
892 | path = None |
---|
893 | if dlg.ShowModal() == wx.ID_OK: |
---|
894 | path = dlg.GetPath() |
---|
895 | dlg.Destroy() |
---|
896 | return path |
---|
897 | |
---|
898 | |
---|
899 | def _reset_context_menu(self): |
---|
900 | """ |
---|
901 | reset the context menu |
---|
902 | """ |
---|
903 | for name, state in self.state.saved_states.iteritems(): |
---|
904 | self.number_saved_state += 1 |
---|
905 | ## Add item in the context menu |
---|
906 | id = wx.NewId() |
---|
907 | self.popUpMenu.Append(id,name, 'Save model and state %g'%self.number_saved_state) |
---|
908 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
909 | |
---|
910 | |
---|
911 | def _reset_plotting_range(self, state): |
---|
912 | """ |
---|
913 | Reset the plotting range to a given state |
---|
914 | """ |
---|
915 | self.qmin.SetValue(str(state.qmin)) |
---|
916 | self.qmax.SetValue(str(state.qmax)) |
---|
917 | if self.state.npts!=None: |
---|
918 | self.npts.SetValue(str(state.npts)) |
---|
919 | |
---|
920 | |
---|
921 | def _save_typeOfmodel(self): |
---|
922 | """ |
---|
923 | save radiobutton containing the type model that can be selected |
---|
924 | """ |
---|
925 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
926 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
927 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
928 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
929 | self.state.structurebox= self.structurebox.GetCurrentSelection() |
---|
930 | self.state.formfactorbox = self.formfactorbox.GetCurrentSelection() |
---|
931 | |
---|
932 | #self._undo.Enable(True) |
---|
933 | ## post state to fit panel |
---|
934 | event = PageInfoEvent(page = self) |
---|
935 | wx.PostEvent(self.parent, event) |
---|
936 | |
---|
937 | |
---|
938 | def _save_plotting_range(self ): |
---|
939 | """ |
---|
940 | save the state of plotting range |
---|
941 | """ |
---|
942 | self.state.qmin = self.qmin_x |
---|
943 | self.state.qmax = self.qmax_x |
---|
944 | if self.npts!=None: |
---|
945 | self.state.npts= self.num_points |
---|
946 | |
---|
947 | |
---|
948 | def _onparamEnter_helper(self): |
---|
949 | """ |
---|
950 | check if values entered by the user are changed and valid to replot |
---|
951 | model |
---|
952 | use : _check_value_enter |
---|
953 | """ |
---|
954 | #self._undo.Enable(True) |
---|
955 | if self.model !=None: |
---|
956 | |
---|
957 | |
---|
958 | # Flag to register when a parameter has changed. |
---|
959 | is_modified = False |
---|
960 | is_modified =self._check_value_enter( self.fittable_param ,is_modified) |
---|
961 | is_modified =self._check_value_enter( self.fixed_param ,is_modified) |
---|
962 | is_modified =self._check_value_enter( self.parameters ,is_modified) |
---|
963 | |
---|
964 | if is_modified: |
---|
965 | #self.sizer3.Layout() |
---|
966 | self.Layout() |
---|
967 | self.Refresh() |
---|
968 | else: |
---|
969 | self.Layout() |
---|
970 | |
---|
971 | # Here we should check whether the boundaries have been modified. |
---|
972 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
973 | # set the is_modified flag to True |
---|
974 | from sans.guiframe.utils import check_value |
---|
975 | if check_value( self.qmin, self.qmax): |
---|
976 | if float(self.qmin.GetValue()) != self.qmin_x: |
---|
977 | self.qmin_x = float(self.qmin.GetValue()) |
---|
978 | is_modified = True |
---|
979 | if float(self.qmax.GetValue()) != self.qmax_x: |
---|
980 | self.qmax_x = float(self.qmax.GetValue()) |
---|
981 | is_modified = True |
---|
982 | self.fitrange = True |
---|
983 | else: |
---|
984 | self.fitrange = False |
---|
985 | if self.npts != None: |
---|
986 | if check_float(self.npts): |
---|
987 | if float(self.npts.GetValue()) != self.num_points: |
---|
988 | self.num_points = float(self.npts.GetValue()) |
---|
989 | is_modified = True |
---|
990 | else: |
---|
991 | msg= "Cannot Plot :Must enter a number!!! " |
---|
992 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
993 | |
---|
994 | |
---|
995 | ## if any value is modify draw model with new value |
---|
996 | if is_modified: |
---|
997 | self.state_change= True |
---|
998 | self._draw_model() |
---|
999 | |
---|
1000 | |
---|
1001 | def _is_modified(self, is_modified): |
---|
1002 | """ |
---|
1003 | return to self._is_modified |
---|
1004 | """ |
---|
1005 | return is_modified |
---|
1006 | |
---|
1007 | def _reset_parameters_state(self, listtorestore,statelist): |
---|
1008 | """ |
---|
1009 | Reset the parameters at the given state |
---|
1010 | """ |
---|
1011 | if len(statelist)==0 or len(listtorestore)==0 : |
---|
1012 | return |
---|
1013 | if len(statelist)!= len(listtorestore) : |
---|
1014 | return |
---|
1015 | |
---|
1016 | for j in range(len(listtorestore)): |
---|
1017 | item_page = listtorestore[j] |
---|
1018 | item_page_info = statelist[j] |
---|
1019 | ##change the state of the check box for simple parameters |
---|
1020 | if item_page[0]!=None: |
---|
1021 | item_page[0].SetValue(item_page_info[0]) |
---|
1022 | |
---|
1023 | if item_page[2]!=None: |
---|
1024 | item_page[2].SetValue(item_page_info[2]) |
---|
1025 | |
---|
1026 | if item_page[3]!=None: |
---|
1027 | ## show or hide text +/- |
---|
1028 | if item_page_info[2]: |
---|
1029 | item_page[3].Show(True) |
---|
1030 | else: |
---|
1031 | item_page[3].Hide() |
---|
1032 | if item_page[4]!=None: |
---|
1033 | ## show of hide the text crtl for fitting error |
---|
1034 | if item_page_info[4][0]: |
---|
1035 | item_page[4].Show(True) |
---|
1036 | item_page[4].SetValue(item_page_info[4][1]) |
---|
1037 | else: |
---|
1038 | item_page[3].Hide() |
---|
1039 | if item_page[5]!=None: |
---|
1040 | ## show of hide the text crtl for fitting error |
---|
1041 | if item_page_info[5][0]: |
---|
1042 | item_page[5].Show(True) |
---|
1043 | item_page[5].SetValue(item_page_info[5][1]) |
---|
1044 | else: |
---|
1045 | item_page[5].Hide() |
---|
1046 | |
---|
1047 | if item_page[6]!=None: |
---|
1048 | ## show of hide the text crtl for fitting error |
---|
1049 | if item_page_info[6][0]: |
---|
1050 | item_page[6].Show(True) |
---|
1051 | item_page[6].SetValue(item_page_info[6][1]) |
---|
1052 | else: |
---|
1053 | item_page[6].Hide() |
---|
1054 | |
---|
1055 | |
---|
1056 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
1057 | """ |
---|
1058 | copy the state of button |
---|
1059 | @param listtocopy: the list of check button to copy |
---|
1060 | @param statelist: list of state object to store the current state |
---|
1061 | """ |
---|
1062 | if len(listtocopy)==0: |
---|
1063 | return |
---|
1064 | |
---|
1065 | for item in listtocopy: |
---|
1066 | |
---|
1067 | checkbox_state = None |
---|
1068 | if item[0]!= None: |
---|
1069 | checkbox_state= item[0].GetValue() |
---|
1070 | parameter_name = item[1] |
---|
1071 | parameter_value = None |
---|
1072 | if item[2]!=None: |
---|
1073 | parameter_value = item[2].GetValue() |
---|
1074 | static_text = None |
---|
1075 | if item[3]!=None: |
---|
1076 | static_text = item[3].IsShown() |
---|
1077 | error_value = None |
---|
1078 | error_state = None |
---|
1079 | if item[4]!= None: |
---|
1080 | error_value = item[4].GetValue() |
---|
1081 | error_state = item[4].IsShown() |
---|
1082 | |
---|
1083 | min_value = None |
---|
1084 | min_state = None |
---|
1085 | if item[5]!= None: |
---|
1086 | min_value = item[5].GetValue() |
---|
1087 | min_state = item[5].IsShown() |
---|
1088 | |
---|
1089 | max_value = None |
---|
1090 | max_state = None |
---|
1091 | if item[6]!= None: |
---|
1092 | max_value = item[6].GetValue() |
---|
1093 | max_state = item[6].IsShown() |
---|
1094 | unit=None |
---|
1095 | if item[7]!=None: |
---|
1096 | unit = item[7].GetLabel() |
---|
1097 | |
---|
1098 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
1099 | static_text ,[error_state,error_value], |
---|
1100 | [min_state,min_value],[max_state , max_value],unit]) |
---|
1101 | |
---|
1102 | |
---|
1103 | |
---|
1104 | def _set_model_sizer_selection(self, model): |
---|
1105 | """ |
---|
1106 | Display the sizer according to the type of the current model |
---|
1107 | """ |
---|
1108 | if model ==None: |
---|
1109 | return |
---|
1110 | if hasattr(model ,"s_model"): |
---|
1111 | |
---|
1112 | class_name= model.s_model.__class__ |
---|
1113 | name= model.s_model.name |
---|
1114 | flag= name != "NoStructure" |
---|
1115 | if flag and (class_name in self.model_list_box["Structure Factors"]): |
---|
1116 | self.structurebox.Show() |
---|
1117 | self.text2.Show() |
---|
1118 | self.structurebox.Enable() |
---|
1119 | self.text2.Enable() |
---|
1120 | items = self.structurebox.GetItems() |
---|
1121 | self.sizer1.Layout() |
---|
1122 | self.SetScrollbars(20,20,25,65) |
---|
1123 | for i in range(len(items)): |
---|
1124 | if items[i]== str(name): |
---|
1125 | self.structurebox.SetSelection(i) |
---|
1126 | break |
---|
1127 | |
---|
1128 | if hasattr(model ,"p_model"): |
---|
1129 | class_name = model.p_model.__class__ |
---|
1130 | name = model.p_model.name |
---|
1131 | self.formfactorbox.Clear() |
---|
1132 | |
---|
1133 | for k, list in self.model_list_box.iteritems(): |
---|
1134 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1135 | self.shape_rbutton.SetValue(True) |
---|
1136 | ## fill the form factor list with new model |
---|
1137 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1138 | items = self.formfactorbox.GetItems() |
---|
1139 | ## set comboxbox to the selected item |
---|
1140 | for i in range(len(items)): |
---|
1141 | if items[i]== str(name): |
---|
1142 | self.formfactorbox.SetSelection(i) |
---|
1143 | break |
---|
1144 | return |
---|
1145 | elif k == "Shape-Independent": |
---|
1146 | self.shape_indep_rbutton.SetValue(True) |
---|
1147 | elif k == "Structure Factors": |
---|
1148 | self.struct_rbutton.SetValue(True) |
---|
1149 | else: |
---|
1150 | self.plugin_rbutton.SetValue(True) |
---|
1151 | |
---|
1152 | if class_name in list: |
---|
1153 | ## fill the form factor list with new model |
---|
1154 | self._populate_box(self.formfactorbox, list) |
---|
1155 | items = self.formfactorbox.GetItems() |
---|
1156 | ## set comboxbox to the selected item |
---|
1157 | for i in range(len(items)): |
---|
1158 | if items[i]== str(name): |
---|
1159 | self.formfactorbox.SetSelection(i) |
---|
1160 | break |
---|
1161 | break |
---|
1162 | else: |
---|
1163 | |
---|
1164 | ## Select the model from the menu |
---|
1165 | class_name = model.__class__ |
---|
1166 | name = model.name |
---|
1167 | self.formfactorbox.Clear() |
---|
1168 | items = self.formfactorbox.GetItems() |
---|
1169 | |
---|
1170 | for k, list in self.model_list_box.iteritems(): |
---|
1171 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1172 | if class_name in self.model_list_box["P(Q)*S(Q)"]: |
---|
1173 | self.structurebox.Show() |
---|
1174 | self.text2.Show() |
---|
1175 | self.structurebox.Enable() |
---|
1176 | self.structurebox.SetSelection(0) |
---|
1177 | self.text2.Enable() |
---|
1178 | else: |
---|
1179 | self.structurebox.Hide() |
---|
1180 | self.text2.Hide() |
---|
1181 | self.structurebox.Disable() |
---|
1182 | self.structurebox.SetSelection(0) |
---|
1183 | self.text2.Disable() |
---|
1184 | |
---|
1185 | self.shape_rbutton.SetValue(True) |
---|
1186 | ## fill the form factor list with new model |
---|
1187 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1188 | items = self.formfactorbox.GetItems() |
---|
1189 | ## set comboxbox to the selected item |
---|
1190 | for i in range(len(items)): |
---|
1191 | if items[i]== str(name): |
---|
1192 | self.formfactorbox.SetSelection(i) |
---|
1193 | break |
---|
1194 | return |
---|
1195 | elif k == "Shape-Independent": |
---|
1196 | self.shape_indep_rbutton.SetValue(True) |
---|
1197 | elif k == "Structure Factors": |
---|
1198 | self.struct_rbutton.SetValue(True) |
---|
1199 | else: |
---|
1200 | self.plugin_rbutton.SetValue(True) |
---|
1201 | if class_name in list: |
---|
1202 | self.structurebox.SetSelection(0) |
---|
1203 | self.structurebox.Disable() |
---|
1204 | self.text2.Disable() |
---|
1205 | ## fill the form factor list with new model |
---|
1206 | self._populate_box(self.formfactorbox, list) |
---|
1207 | items = self.formfactorbox.GetItems() |
---|
1208 | ## set comboxbox to the selected item |
---|
1209 | for i in range(len(items)): |
---|
1210 | if items[i]== str(name): |
---|
1211 | self.formfactorbox.SetSelection(i) |
---|
1212 | break |
---|
1213 | break |
---|
1214 | |
---|
1215 | |
---|
1216 | def _draw_model(self): |
---|
1217 | """ |
---|
1218 | Method to draw or refresh a plotted model. |
---|
1219 | The method will use the data member from the model page |
---|
1220 | to build a call to the fitting perspective manager. |
---|
1221 | |
---|
1222 | [Note to coder: This way future changes will be done in only one place.] |
---|
1223 | """ |
---|
1224 | if self.model !=None: |
---|
1225 | temp_smear=None |
---|
1226 | if hasattr(self, "enable_smearer"): |
---|
1227 | if self.enable_smearer.GetValue(): |
---|
1228 | temp_smear= self.smearer |
---|
1229 | |
---|
1230 | self.manager.draw_model(self.model, data=self.data, |
---|
1231 | smearer= temp_smear, |
---|
1232 | qmin=float(self.qmin_x), qmax=float(self.qmax_x), |
---|
1233 | qstep= float(self.num_points), |
---|
1234 | enable2D=self.enable2D) |
---|
1235 | |
---|
1236 | |
---|
1237 | def _set_model_sizer(self,sizer, box_sizer, title="", object=None): |
---|
1238 | """ |
---|
1239 | Use lists to fill a sizer for model info |
---|
1240 | """ |
---|
1241 | |
---|
1242 | sizer.Clear(True) |
---|
1243 | ##For MAC, this should defined here. |
---|
1244 | if box_sizer == None: |
---|
1245 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
1246 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1247 | else: |
---|
1248 | boxsizer1 = box_sizer |
---|
1249 | |
---|
1250 | #-------------------------------------------------------- |
---|
1251 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shapes', style=wx.RB_GROUP) |
---|
1252 | self.shape_indep_rbutton = wx.RadioButton(self, -1, "Shape-Independent") |
---|
1253 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure Factor ") |
---|
1254 | self.plugin_rbutton = wx.RadioButton(self, -1, "Customized Models") |
---|
1255 | |
---|
1256 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1257 | id= self.shape_rbutton.GetId() ) |
---|
1258 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1259 | id= self.shape_indep_rbutton.GetId() ) |
---|
1260 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1261 | id= self.struct_rbutton.GetId() ) |
---|
1262 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1263 | id= self.plugin_rbutton.GetId() ) |
---|
1264 | #MAC needs SetValue |
---|
1265 | self.shape_rbutton.SetValue(True) |
---|
1266 | |
---|
1267 | sizer_radiobutton = wx.GridSizer(2, 2,5, 5) |
---|
1268 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
1269 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
1270 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
1271 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
1272 | |
---|
1273 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
1274 | |
---|
1275 | self.text1 = wx.StaticText( self,-1,"" ) |
---|
1276 | self.text2 = wx.StaticText( self,-1,"P(Q)*S(Q)" ) |
---|
1277 | |
---|
1278 | |
---|
1279 | self.formfactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1280 | if self.model!=None: |
---|
1281 | self.formfactorbox.SetValue(self.model.name) |
---|
1282 | |
---|
1283 | |
---|
1284 | self.structurebox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1285 | wx.EVT_COMBOBOX(self.formfactorbox,-1, self._on_select_model) |
---|
1286 | wx.EVT_COMBOBOX(self.structurebox,-1, self._on_select_model) |
---|
1287 | |
---|
1288 | |
---|
1289 | ## fill combox box |
---|
1290 | if len(self.model_list_box)>0: |
---|
1291 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1292 | |
---|
1293 | if len(self.model_list_box)>0: |
---|
1294 | self._populate_box( self.structurebox, |
---|
1295 | self.model_list_box["Structure Factors"]) |
---|
1296 | self.structurebox.Insert("None", 0,None) |
---|
1297 | self.structurebox.SetSelection(0) |
---|
1298 | self.structurebox.Hide() |
---|
1299 | self.text2.Hide() |
---|
1300 | self.structurebox.Disable() |
---|
1301 | self.text2.Disable() |
---|
1302 | |
---|
1303 | if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]: |
---|
1304 | self.structurebox.Show() |
---|
1305 | self.text2.Show() |
---|
1306 | self.structurebox.Enable() |
---|
1307 | self.text2.Enable() |
---|
1308 | |
---|
1309 | ## check model type to show sizer |
---|
1310 | if self.model !=None: |
---|
1311 | self._set_model_sizer_selection( self.model ) |
---|
1312 | |
---|
1313 | sizer_selection.Add(self.text1) |
---|
1314 | sizer_selection.Add((5,5)) |
---|
1315 | sizer_selection.Add(self.formfactorbox) |
---|
1316 | sizer_selection.Add((5,5)) |
---|
1317 | sizer_selection.Add(self.text2) |
---|
1318 | sizer_selection.Add((5,5)) |
---|
1319 | sizer_selection.Add(self.structurebox) |
---|
1320 | sizer_selection.Add((5,5)) |
---|
1321 | |
---|
1322 | boxsizer1.Add( sizer_radiobutton ) |
---|
1323 | boxsizer1.Add( (20,20)) |
---|
1324 | boxsizer1.Add( sizer_selection ) |
---|
1325 | if object !=None: |
---|
1326 | boxsizer1.Add( (-72,-72)) |
---|
1327 | boxsizer1.Add( object, 0, wx.ALIGN_RIGHT| wx.RIGHT, 35) |
---|
1328 | boxsizer1.Add( (60,60)) |
---|
1329 | #-------------------------------------------------------- |
---|
1330 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1331 | sizer.Layout() |
---|
1332 | self.SetScrollbars(20,20,25,65) |
---|
1333 | |
---|
1334 | |
---|
1335 | def _show_combox(self, event): |
---|
1336 | """ |
---|
1337 | Show combox box associate with type of model selected |
---|
1338 | """ |
---|
1339 | ## Don't want to populate combo box again if the event comes from check box |
---|
1340 | if self.shape_rbutton.GetValue()and\ |
---|
1341 | event.GetEventObject()==self.shape_rbutton: |
---|
1342 | ##fill the combobox with form factor list |
---|
1343 | self.structurebox.SetSelection(0) |
---|
1344 | self.structurebox.Disable() |
---|
1345 | self.formfactorbox.Clear() |
---|
1346 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1347 | |
---|
1348 | if self.shape_indep_rbutton.GetValue()and\ |
---|
1349 | event.GetEventObject()==self.shape_indep_rbutton: |
---|
1350 | ##fill the combobox with shape independent factor list |
---|
1351 | self.structurebox.SetSelection(0) |
---|
1352 | self.structurebox.Disable() |
---|
1353 | self.formfactorbox.Clear() |
---|
1354 | self._populate_box( self.formfactorbox, |
---|
1355 | self.model_list_box["Shape-Independent"]) |
---|
1356 | |
---|
1357 | if self.struct_rbutton.GetValue() and\ |
---|
1358 | event.GetEventObject()==self.struct_rbutton: |
---|
1359 | ##fill the combobox with structure factor list |
---|
1360 | self.structurebox.SetSelection(0) |
---|
1361 | self.structurebox.Disable() |
---|
1362 | self.formfactorbox.Clear() |
---|
1363 | self._populate_box( self.formfactorbox, |
---|
1364 | self.model_list_box["Structure Factors"]) |
---|
1365 | |
---|
1366 | if self.plugin_rbutton.GetValue()and\ |
---|
1367 | event.GetEventObject()==self.plugin_rbutton: |
---|
1368 | |
---|
1369 | ##fill the combobox with form factor list |
---|
1370 | self.structurebox.Disable() |
---|
1371 | self.formfactorbox.Clear() |
---|
1372 | self._populate_box( self.formfactorbox, |
---|
1373 | self.model_list_box["Customized Models"]) |
---|
1374 | |
---|
1375 | self._on_select_model(event=None) |
---|
1376 | self._save_typeOfmodel() |
---|
1377 | self.sizer4_4.Layout() |
---|
1378 | self.sizer4.Layout() |
---|
1379 | self.Layout() |
---|
1380 | self.Refresh() |
---|
1381 | self.SetScrollbars(20,20,25,65) |
---|
1382 | |
---|
1383 | def _populate_box(self, combobox, list): |
---|
1384 | """ |
---|
1385 | fill combox box with dict item |
---|
1386 | @param list: contains item to fill the combox |
---|
1387 | item must model class |
---|
1388 | """ |
---|
1389 | for models in list: |
---|
1390 | model= models() |
---|
1391 | name = model.__class__.__name__ |
---|
1392 | if models.__name__!="NoStructure": |
---|
1393 | if hasattr(model, "name"): |
---|
1394 | name = model.name |
---|
1395 | combobox.Append(name,models) |
---|
1396 | |
---|
1397 | return 0 |
---|
1398 | |
---|
1399 | |
---|
1400 | def _on_select_model_helper(self): |
---|
1401 | """ |
---|
1402 | call back for model selection |
---|
1403 | """ |
---|
1404 | ## reset dictionary containing reference to dispersion |
---|
1405 | self._disp_obj_dict = {} |
---|
1406 | self.disp_cb_dict ={} |
---|
1407 | |
---|
1408 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
1409 | #For MAC |
---|
1410 | form_factor = None |
---|
1411 | if f_id >= 0: |
---|
1412 | form_factor = self.formfactorbox.GetClientData( f_id ) |
---|
1413 | |
---|
1414 | if not form_factor in self.model_list_box["multiplication"]: |
---|
1415 | self.structurebox.Hide() |
---|
1416 | self.text2.Hide() |
---|
1417 | self.structurebox.Disable() |
---|
1418 | self.structurebox.SetSelection(0) |
---|
1419 | self.text2.Disable() |
---|
1420 | else: |
---|
1421 | self.structurebox.Show() |
---|
1422 | self.text2.Show() |
---|
1423 | self.structurebox.Enable() |
---|
1424 | self.text2.Enable() |
---|
1425 | |
---|
1426 | s_id = self.structurebox.GetCurrentSelection() |
---|
1427 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
1428 | |
---|
1429 | if struct_factor !=None: |
---|
1430 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
1431 | self.model= MultiplicationModel(form_factor(),struct_factor()) |
---|
1432 | |
---|
1433 | else: |
---|
1434 | if form_factor != None: |
---|
1435 | self.model= form_factor() |
---|
1436 | else: |
---|
1437 | self.model = None |
---|
1438 | return self.model |
---|
1439 | |
---|
1440 | ## post state to fit panel |
---|
1441 | self.state.parameters =[] |
---|
1442 | self.state.model =self.model |
---|
1443 | self.disp_list =self.model.getDispParamList() |
---|
1444 | self.state.disp_list = self.disp_list |
---|
1445 | #self.sizer4_4.Layout() |
---|
1446 | self.Layout() |
---|
1447 | #self.SetScrollbars(20,20,25,65) |
---|
1448 | #self.Refresh() |
---|
1449 | |
---|
1450 | |
---|
1451 | |
---|
1452 | def _onparamEnter(self,event): |
---|
1453 | """ |
---|
1454 | when enter value on panel redraw model according to changed |
---|
1455 | """ |
---|
1456 | |
---|
1457 | tcrtl= event.GetEventObject() |
---|
1458 | |
---|
1459 | ## save current state |
---|
1460 | self.save_current_state() |
---|
1461 | if event !=None: |
---|
1462 | #self._undo.Enable(True) |
---|
1463 | event = PageInfoEvent(page = self) |
---|
1464 | wx.PostEvent(self.parent, event) |
---|
1465 | |
---|
1466 | if check_float(tcrtl): |
---|
1467 | |
---|
1468 | self._onparamEnter_helper() |
---|
1469 | |
---|
1470 | else: |
---|
1471 | msg= "Cannot Plot :Must enter a number!!! " |
---|
1472 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1473 | return |
---|
1474 | |
---|
1475 | |
---|
1476 | def _check_value_enter(self, list, modified): |
---|
1477 | """ |
---|
1478 | @param list: model parameter and panel info |
---|
1479 | each item of the list should be as follow: |
---|
1480 | item=[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1481 | """ |
---|
1482 | is_modified = modified |
---|
1483 | if len(list)==0: |
---|
1484 | return is_modified |
---|
1485 | for item in list: |
---|
1486 | try: |
---|
1487 | name = str(item[1]) |
---|
1488 | if hasattr(self,"text2_3") and self.text2_3.IsShown(): |
---|
1489 | if hasattr(self,"item[4]")and self.item[4] == None: |
---|
1490 | self.text2_3.Hide() |
---|
1491 | |
---|
1492 | ## check model parameters range |
---|
1493 | param_min= None |
---|
1494 | param_max= None |
---|
1495 | ## check minimun value |
---|
1496 | if item[5]!= None: |
---|
1497 | if item[5].GetValue().lstrip().rstrip()!="": |
---|
1498 | param_min = float(item[5].GetValue()) |
---|
1499 | is_modified = True |
---|
1500 | ## check maximum value |
---|
1501 | if item[6]!= None: |
---|
1502 | if item[6].GetValue().lstrip().rstrip()!="": |
---|
1503 | param_max = float(item[6].GetValue()) |
---|
1504 | is_modified = True |
---|
1505 | from sans.guiframe.utils import check_value |
---|
1506 | if param_min != None and param_max !=None: |
---|
1507 | if not check_value(item[5], item[6]): |
---|
1508 | msg= "Wrong Fit range entered for parameter " |
---|
1509 | msg+= "name %s of model %s "%(name, self.model.name) |
---|
1510 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1511 | if name in self.model.details.keys(): |
---|
1512 | self.model.details[name][1:]= param_min,param_max |
---|
1513 | |
---|
1514 | |
---|
1515 | ## hide statictext +/- |
---|
1516 | #if item[3]!=None and item[3].IsShown(): |
---|
1517 | # item[3].Hide() |
---|
1518 | ## hide textcrtl for error after fit |
---|
1519 | #if item[4]!=None and item[4].IsShown(): |
---|
1520 | # item[4].Clear() |
---|
1521 | # item[4].Hide() |
---|
1522 | |
---|
1523 | value= float(item[2].GetValue()) |
---|
1524 | |
---|
1525 | # If the value of the parameter has changed, |
---|
1526 | # +update the model and set the is_modified flag |
---|
1527 | if value != self.model.getParam(name): |
---|
1528 | self.model.setParam(name,value) |
---|
1529 | is_modified = True |
---|
1530 | |
---|
1531 | except: |
---|
1532 | msg= "Model Drawing Error:wrong value entered : %s"% sys.exc_value |
---|
1533 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1534 | return |
---|
1535 | return is_modified |
---|
1536 | |
---|
1537 | |
---|
1538 | def _set_dipers_Param(self, event): |
---|
1539 | """ |
---|
1540 | respond to self.enable_disp and self.disable_disp radio box. |
---|
1541 | The dispersity object is reset inside the model into Gaussian. |
---|
1542 | When the user select yes , this method display a combo box for more selection |
---|
1543 | when the user selects No,the combo box disappears. |
---|
1544 | Redraw the model with the default dispersity (Gaussian) |
---|
1545 | """ |
---|
1546 | |
---|
1547 | self._reset_dispersity() |
---|
1548 | |
---|
1549 | if self.model ==None: |
---|
1550 | self.model_disp.Hide() |
---|
1551 | self.disp_box.Hide() |
---|
1552 | self.sizer4_4.Clear(True) |
---|
1553 | return |
---|
1554 | |
---|
1555 | if self.enable_disp.GetValue(): |
---|
1556 | self.model_disp.Show(True) |
---|
1557 | self.disp_box.Show(True) |
---|
1558 | ## layout for model containing no dispersity parameters |
---|
1559 | |
---|
1560 | self.disp_list= self.model.getDispParamList() |
---|
1561 | |
---|
1562 | if len(self.disp_list)==0 and len(self.disp_cb_dict)==0: |
---|
1563 | self._layout_sizer_noDipers() |
---|
1564 | else: |
---|
1565 | ## set gaussian sizer |
---|
1566 | self._on_select_Disp(event=None) |
---|
1567 | else: |
---|
1568 | self.model_disp.Hide() |
---|
1569 | self.disp_box.Hide() |
---|
1570 | self.disp_box.SetSelection(0) |
---|
1571 | self.sizer4_4.Clear(True) |
---|
1572 | |
---|
1573 | ## post state to fit panel |
---|
1574 | self.save_current_state() |
---|
1575 | if event !=None: |
---|
1576 | #self._undo.Enable(True) |
---|
1577 | event = PageInfoEvent(page = self) |
---|
1578 | wx.PostEvent(self.parent, event) |
---|
1579 | #draw the model with the current dispersity |
---|
1580 | self._draw_model() |
---|
1581 | #self.sizer4_4.Layout() |
---|
1582 | #self.sizer5.Layout() |
---|
1583 | self.Layout() |
---|
1584 | #self.Refresh() |
---|
1585 | #self.SetScrollbars(20,20,25,65) |
---|
1586 | |
---|
1587 | |
---|
1588 | def _layout_sizer_noDipers(self): |
---|
1589 | """ |
---|
1590 | Draw a sizer with no dispersity info |
---|
1591 | """ |
---|
1592 | ix=0 |
---|
1593 | iy=1 |
---|
1594 | self.fittable_param=[] |
---|
1595 | self.fixed_param=[] |
---|
1596 | self.orientation_params_disp=[] |
---|
1597 | |
---|
1598 | self.model_disp.Hide() |
---|
1599 | self.disp_box.Hide() |
---|
1600 | self.sizer4_4.Clear(True) |
---|
1601 | model_disp = wx.StaticText(self, -1, 'No PolyDispersity for this model') |
---|
1602 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1603 | self.sizer4_4.Layout() |
---|
1604 | self.sizer4.Layout() |
---|
1605 | self.SetScrollbars(20,20,25,65) |
---|
1606 | |
---|
1607 | |
---|
1608 | def _reset_dispersity(self): |
---|
1609 | """ |
---|
1610 | put gaussian dispersity into current model |
---|
1611 | """ |
---|
1612 | if len(self.param_toFit)>0: |
---|
1613 | for item in self.fittable_param: |
---|
1614 | if item in self.param_toFit: |
---|
1615 | self.param_toFit.remove(item) |
---|
1616 | for item in self.orientation_params_disp: |
---|
1617 | if item in self.param_toFit: |
---|
1618 | self.param_toFit.remove(item) |
---|
1619 | |
---|
1620 | self.fittable_param=[] |
---|
1621 | self.fixed_param=[] |
---|
1622 | self.orientation_params_disp=[] |
---|
1623 | self.values=[] |
---|
1624 | self.weights=[] |
---|
1625 | |
---|
1626 | from sans.models.dispersion_models import GaussianDispersion, ArrayDispersion |
---|
1627 | if len(self.disp_cb_dict)==0: |
---|
1628 | self.save_current_state() |
---|
1629 | self.sizer4_4.Clear(True) |
---|
1630 | #self.sizer4_4.Layout() |
---|
1631 | #self.sizer4.Layout() |
---|
1632 | self.Layout() |
---|
1633 | #self.Refresh() |
---|
1634 | #self.SetScrollbars(20,20,25,65) |
---|
1635 | return |
---|
1636 | if (len(self.disp_cb_dict)>0) : |
---|
1637 | for p in self.disp_cb_dict: |
---|
1638 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
1639 | disp_model = GaussianDispersion() |
---|
1640 | |
---|
1641 | self._disp_obj_dict[p] = disp_model |
---|
1642 | # Set the new model as the dispersion object for the selected parameter |
---|
1643 | try: |
---|
1644 | self.model.set_dispersion(p, disp_model) |
---|
1645 | except: |
---|
1646 | |
---|
1647 | pass |
---|
1648 | # Redraw the model |
---|
1649 | #self._draw_model() |
---|
1650 | ## save state into |
---|
1651 | self.save_current_state() |
---|
1652 | #self.sizer4_4.Layout() |
---|
1653 | #self.sizer4.Layout() |
---|
1654 | self.Layout() |
---|
1655 | #self.SetScrollbars(20,20,25,65) |
---|
1656 | self.Refresh() |
---|
1657 | |
---|
1658 | |
---|
1659 | |
---|
1660 | def _on_select_Disp(self,event): |
---|
1661 | """ |
---|
1662 | allow selecting different dispersion |
---|
1663 | self.disp_list should change type later .now only gaussian |
---|
1664 | """ |
---|
1665 | n = self.disp_box.GetCurrentSelection() |
---|
1666 | name = self.disp_box.GetValue() |
---|
1667 | dispersity= self.disp_box.GetClientData(n) |
---|
1668 | |
---|
1669 | self.disp_name = name |
---|
1670 | |
---|
1671 | if name.lower() == "array": |
---|
1672 | self._set_sizer_arraydispersion() |
---|
1673 | else: |
---|
1674 | self._set_sizer_dispersion(dispersity= dispersity) |
---|
1675 | |
---|
1676 | self.state.disp_box= n |
---|
1677 | ## Redraw the model |
---|
1678 | self._draw_model() |
---|
1679 | #self._undo.Enable(True) |
---|
1680 | event = PageInfoEvent(page = self) |
---|
1681 | wx.PostEvent(self.parent, event) |
---|
1682 | |
---|
1683 | #self.sizer4_4.Layout() |
---|
1684 | #self.sizer4.Layout() |
---|
1685 | #self.Layout() |
---|
1686 | self.SetScrollbars(20,20,25,65) |
---|
1687 | #self.Refresh() |
---|
1688 | |
---|
1689 | def _set_sizer_arraydispersion(self): |
---|
1690 | """ |
---|
1691 | draw sizer with array dispersity parameters |
---|
1692 | """ |
---|
1693 | |
---|
1694 | if len(self.param_toFit)>0: |
---|
1695 | for item in self.fittable_param: |
---|
1696 | if item in self.param_toFit: |
---|
1697 | self.param_toFit.remove(item) |
---|
1698 | for item in self.orientation_params_disp: |
---|
1699 | if item in self.param_toFit: |
---|
1700 | self.param_toFit.remove(item) |
---|
1701 | for item in self.model.details.keys(): |
---|
1702 | if item in self.model.fixed: |
---|
1703 | del self.model.details [item] |
---|
1704 | |
---|
1705 | self.fittable_param=[] |
---|
1706 | self.fixed_param=[] |
---|
1707 | self.orientation_params_disp=[] |
---|
1708 | self.sizer4_4.Clear(True) |
---|
1709 | self._reset_dispersity() |
---|
1710 | ix=0 |
---|
1711 | iy=1 |
---|
1712 | disp1 = wx.StaticText(self, -1, 'Array Dispersion') |
---|
1713 | self.sizer4_4.Add(disp1,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1714 | |
---|
1715 | # Look for model parameters to which we can apply an ArrayDispersion model |
---|
1716 | # Add a check box for each parameter. |
---|
1717 | self.disp_cb_dict = {} |
---|
1718 | ix+=1 |
---|
1719 | self.noDisper_rbox = wx.RadioButton(self, -1,"None", (10, 10),style= wx.RB_GROUP) |
---|
1720 | self.Bind(wx.EVT_RADIOBUTTON,self.select_disp_angle , id=self.noDisper_rbox.GetId()) |
---|
1721 | #MAC needs SetValue |
---|
1722 | self.noDisper_rbox.SetValue(True) |
---|
1723 | self.sizer4_4.Add(self.noDisper_rbox, (iy, ix), |
---|
1724 | (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1725 | |
---|
1726 | for p in self.model.dispersion.keys(): |
---|
1727 | if not p in self.model.orientation_params: |
---|
1728 | ix+=1 |
---|
1729 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
1730 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
1731 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, id=self.disp_cb_dict[p].GetId()) |
---|
1732 | #wx.EVT_RADIOBUTTON(self, self.disp_cb_dict[p].GetId(), self.select_disp_angle) |
---|
1733 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1734 | |
---|
1735 | for p in self.model.dispersion.keys(): |
---|
1736 | if p in self.model.orientation_params: |
---|
1737 | ix+=1 |
---|
1738 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
1739 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
1740 | if not (self.enable2D or self.data.__class__.__name__ =="Data2D"): |
---|
1741 | self.disp_cb_dict[p].Hide() |
---|
1742 | #self.disp_cb_dict[p].Disable() |
---|
1743 | else: |
---|
1744 | self.disp_cb_dict[p].Show(True) |
---|
1745 | #self.disp_cb_dict[p].Enable() |
---|
1746 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, id=self.disp_cb_dict[p].GetId()) |
---|
1747 | #wx.EVT_RADIOBUTTON(self, self.disp_cb_dict[p].GetId(), self.select_disp_angle) |
---|
1748 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1749 | |
---|
1750 | |
---|
1751 | ix =0 |
---|
1752 | iy +=1 |
---|
1753 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1754 | #self.sizer4_4.Layout() |
---|
1755 | #self.sizer4.Layout() |
---|
1756 | self.Layout() |
---|
1757 | #self.SetScrollbars(20,20,25,65) |
---|
1758 | ## save state into |
---|
1759 | |
---|
1760 | self.state.orientation_params =[] |
---|
1761 | self.state.orientation_params_disp =[] |
---|
1762 | self.state.parameters =[] |
---|
1763 | self.state.fittable_param =[] |
---|
1764 | self.state.fixed_param =[] |
---|
1765 | |
---|
1766 | ## save checkbutton state and txtcrtl values |
---|
1767 | |
---|
1768 | self._copy_parameters_state(self.orientation_params, |
---|
1769 | self.state.orientation_params) |
---|
1770 | |
---|
1771 | self._copy_parameters_state(self.orientation_params_disp, |
---|
1772 | self.state.orientation_params_disp) |
---|
1773 | |
---|
1774 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
1775 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
1776 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
1777 | |
---|
1778 | |
---|
1779 | ## post state to fit panel |
---|
1780 | event = PageInfoEvent(page = self) |
---|
1781 | wx.PostEvent(self.parent, event) |
---|
1782 | |
---|
1783 | |
---|
1784 | |
---|
1785 | |
---|
1786 | |
---|
1787 | def _set_range_sizer(self, title, box_sizer=None, object1=None,object=None): |
---|
1788 | """ |
---|
1789 | Fill the Q range sizer |
---|
1790 | """ |
---|
1791 | self.sizer5.Clear(True) |
---|
1792 | #-------------------------------------------------------------- |
---|
1793 | if box_sizer == None: |
---|
1794 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
1795 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1796 | else: |
---|
1797 | #for MAC |
---|
1798 | boxsizer1 = box_sizer |
---|
1799 | |
---|
1800 | self.qmin = BasicPage.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20),style=wx.TE_PROCESS_ENTER) |
---|
1801 | self.qmin.SetValue(str(self.qmin_x)) |
---|
1802 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
1803 | #self.qmin.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1804 | #self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
1805 | #self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
1806 | |
---|
1807 | self.qmax = BasicPage.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20),style=wx.TE_PROCESS_ENTER) |
---|
1808 | self.qmax.SetValue(str(self.qmax_x)) |
---|
1809 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
1810 | #self.qmax.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1811 | #self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
1812 | #self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
1813 | |
---|
1814 | id = wx.NewId() |
---|
1815 | self.reset_qrange =wx.Button(self,id,'Reset',size=(70,23)) |
---|
1816 | self.reset_qrange.Bind(wx.EVT_BUTTON, self.on_reset_clicked,id=id) |
---|
1817 | self.reset_qrange.SetToolTipString("Reset Q range to the default values") |
---|
1818 | |
---|
1819 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
1820 | sizer= wx.GridSizer(3, 3,2, 5) |
---|
1821 | |
---|
1822 | sizer.Add(wx.StaticText(self, -1, ' Q range')) |
---|
1823 | sizer.Add(wx.StaticText(self, -1, ' Min')) |
---|
1824 | sizer.Add(wx.StaticText(self, -1, ' Max')) |
---|
1825 | sizer.Add(self.reset_qrange) |
---|
1826 | |
---|
1827 | sizer.Add(self.qmin) |
---|
1828 | sizer.Add(self.qmax) |
---|
1829 | sizer_horizontal.Add(sizer) |
---|
1830 | if object!=None: |
---|
1831 | sizer_horizontal.Add(object) |
---|
1832 | |
---|
1833 | if object1!=None: |
---|
1834 | boxsizer1.Add(object1) |
---|
1835 | boxsizer1.Add((10,10)) |
---|
1836 | boxsizer1.Add(sizer_horizontal) |
---|
1837 | ## save state |
---|
1838 | self.save_current_state() |
---|
1839 | #---------------------------------------------------------------- |
---|
1840 | self.sizer5.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1841 | self.sizer5.Layout() |
---|
1842 | #self.Layout() |
---|
1843 | #self.SetScrollbars(20,20,25,65) |
---|
1844 | |
---|
1845 | |
---|
1846 | def _fill_save_sizer(self): |
---|
1847 | """ |
---|
1848 | Draw the layout for saving option |
---|
1849 | """ |
---|
1850 | # Skipping save state functionality for release 0.9.0 |
---|
1851 | return |
---|
1852 | |
---|
1853 | self.sizer6.Clear(True) |
---|
1854 | box_description= wx.StaticBox(self, -1,"Save Model") |
---|
1855 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1856 | sizer_save = wx.BoxSizer(wx.HORIZONTAL) |
---|
1857 | |
---|
1858 | self.btSave_title = wx.StaticText(self, -1, 'Save the current Model') |
---|
1859 | self.btSave = wx.Button(self,wx.NewId(),'Save') |
---|
1860 | self.btSave.Bind(wx.EVT_BUTTON, self.onSave,id= self.btSave.GetId()) |
---|
1861 | self.btSave.SetToolTipString("Save the current Model") |
---|
1862 | |
---|
1863 | sizer_save.Add(self.btSave_title) |
---|
1864 | sizer_save.Add((20,20),0, wx.LEFT|wx.RIGHT|wx.EXPAND,45) |
---|
1865 | |
---|
1866 | sizer_save.Add(self.btSave) |
---|
1867 | |
---|
1868 | boxsizer1.Add(sizer_save) |
---|
1869 | self.sizer6.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1870 | self.sizer6.Layout() |
---|
1871 | self.SetScrollbars(20,20,25,65) |
---|
1872 | |
---|
1873 | def on_reset_clicked(self,event): |
---|
1874 | """ |
---|
1875 | #On 'Reset' button for Q range clicked |
---|
1876 | """ |
---|
1877 | ##For 3 different cases: Data2D, Data1D, and theory |
---|
1878 | if self.data.__class__.__name__ == "Data2D": |
---|
1879 | data_min= 0 |
---|
1880 | x= max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
1881 | y= max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
1882 | self.qmin_x = data_min |
---|
1883 | self.qmax_x = math.sqrt(x*x + y*y) |
---|
1884 | elif self.data.__class__.__name__ == "Data1D": |
---|
1885 | self.qmin_x = min(self.data.x) |
---|
1886 | self.qmax_x = max(self.data.x) |
---|
1887 | else: |
---|
1888 | self.qmin_x = _QMIN_DEFAULT |
---|
1889 | self.qmax_x = _QMAX_DEFAULT |
---|
1890 | self.num_points = _NPTS_DEFAULT |
---|
1891 | self.state.npts = self.num_points |
---|
1892 | |
---|
1893 | self.state.qmin = self.qmin_x |
---|
1894 | self.state.qmax = self.qmax_x |
---|
1895 | |
---|
1896 | #reset the q range values |
---|
1897 | self._reset_plotting_range(self.state) |
---|
1898 | #Re draw plot |
---|
1899 | self._draw_model() |
---|
1900 | |
---|
1901 | def on_model_help_clicked(self,event): |
---|
1902 | """ |
---|
1903 | #On 'More details' button |
---|
1904 | """ |
---|
1905 | from helpPanel import HelpWindow |
---|
1906 | |
---|
1907 | if self.model == None: |
---|
1908 | name = 'FuncHelp' |
---|
1909 | else: |
---|
1910 | name = self.model.origin_name |
---|
1911 | |
---|
1912 | frame = HelpWindow(None, -1, pageToOpen="doc/model_functions.html") |
---|
1913 | frame.Show(True) |
---|
1914 | if frame.rhelp.HasAnchor(name): |
---|
1915 | frame.rhelp.ScrollToAnchor(name) |
---|
1916 | else: |
---|
1917 | msg= "Model does not contains an available description " |
---|
1918 | msg +="Please try searching in the Help window" |
---|
1919 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1920 | |
---|