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