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 | return |
---|
424 | try: |
---|
425 | self._default_save_location = os.path.dirname(path) |
---|
426 | except: |
---|
427 | pass |
---|
428 | try: |
---|
429 | self.values,self.weights = self.read_file(path) |
---|
430 | except: |
---|
431 | msg="Could not read input file" |
---|
432 | wx.PostEvent(self.parent, StatusEvent(status= msg)) |
---|
433 | return |
---|
434 | |
---|
435 | # If any of the two arrays is empty, notify the user that we won't |
---|
436 | # proceed |
---|
437 | if self.values is None or self.weights is None or \ |
---|
438 | self.values ==[] or self.weights ==[]: |
---|
439 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
440 | "The loaded %s distrubtion is corrupted or empty" % p)) |
---|
441 | return |
---|
442 | |
---|
443 | # Tell the user that we are about to apply the distribution |
---|
444 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
445 | "Applying loaded %s distribution: %s" % (p, path))) |
---|
446 | |
---|
447 | # Create the dispersion objects |
---|
448 | from sans.models.dispersion_models import ArrayDispersion |
---|
449 | disp_model = ArrayDispersion() |
---|
450 | disp_model.set_weights(self.values, self.weights) |
---|
451 | |
---|
452 | # Store the object to make it persist outside the scope of this method |
---|
453 | #TODO: refactor model to clean this up? |
---|
454 | self._disp_obj_dict[p] = disp_model |
---|
455 | self.state._disp_obj_dict [p]= disp_model |
---|
456 | self.state.values=[] |
---|
457 | self.state.weights=[] |
---|
458 | self.state.values = copy.deepcopy(self.values) |
---|
459 | self.state.weights = copy.deepcopy(self.weights) |
---|
460 | # Set the new model as the dispersion object for the selected parameter |
---|
461 | self.model.set_dispersion(p, disp_model) |
---|
462 | # Store a reference to the weights in the model object so that |
---|
463 | # it's not lost when we use the model within another thread. |
---|
464 | #TODO: total hack - fix this |
---|
465 | self.state.model= self.model.clone() |
---|
466 | |
---|
467 | self.model._persistency_dict = {} |
---|
468 | self.model._persistency_dict[p] = [self.values, self.weights] |
---|
469 | self.state.model._persistency_dict[p] = [self.values, self.weights] |
---|
470 | else: |
---|
471 | self._reset_dispersity() |
---|
472 | |
---|
473 | ## Redraw the model |
---|
474 | self._draw_model() |
---|
475 | |
---|
476 | ## post state to fit panel |
---|
477 | event = PageInfoEvent(page = self) |
---|
478 | wx.PostEvent(self.parent, event) |
---|
479 | |
---|
480 | |
---|
481 | def onResetModel(self, event): |
---|
482 | """ |
---|
483 | Reset model state |
---|
484 | """ |
---|
485 | ## post help message for the selected model |
---|
486 | msg = self.popUpMenu.GetHelpString(event.GetId()) |
---|
487 | msg +=" reloaded" |
---|
488 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
489 | |
---|
490 | name= self.popUpMenu.GetLabel(event.GetId()) |
---|
491 | self._on_select_model_helper() |
---|
492 | |
---|
493 | if name in self.saved_states.keys(): |
---|
494 | previous_state = self.saved_states[name] |
---|
495 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
496 | self.reset_page(previous_state) |
---|
497 | |
---|
498 | def onSave(self, event): |
---|
499 | """ |
---|
500 | save history of the data and model |
---|
501 | """ |
---|
502 | if self.model==None: |
---|
503 | return |
---|
504 | if hasattr(self,"enable_disp"): |
---|
505 | self.state.enable_disp = copy.deepcopy(self.enable_disp.GetValue()) |
---|
506 | if hasattr(self, "disp_box"): |
---|
507 | self.state.disp_box = copy.deepcopy(self.disp_box.GetSelection()) |
---|
508 | |
---|
509 | self.state.model.name= self.model.name |
---|
510 | |
---|
511 | new_state = self.state.clone() |
---|
512 | new_state.model.name = self.state.model.name |
---|
513 | |
---|
514 | new_state.enable2D = copy.deepcopy(self.enable2D) |
---|
515 | ##Add model state on context menu |
---|
516 | self.number_saved_state += 1 |
---|
517 | #name= self.model.name+"[%g]"%self.number_saved_state |
---|
518 | name= self.model.__class__.__name__+"[%g]"%self.number_saved_state |
---|
519 | self.saved_states[name]= new_state |
---|
520 | |
---|
521 | ## Add item in the context menu |
---|
522 | |
---|
523 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
524 | my_time= str(hour)+" : "+str(minute)+" : "+str(second)+" " |
---|
525 | date= str( month)+"|"+str(day)+"|"+str(year) |
---|
526 | msg= "Model saved at %s on %s"%(my_time, date) |
---|
527 | ## post help message for the selected model |
---|
528 | msg +=" Saved! right click on this page to retrieve this model" |
---|
529 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
530 | |
---|
531 | id = wx.NewId() |
---|
532 | self.popUpMenu.Append(id,name,str(msg)) |
---|
533 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
534 | |
---|
535 | def onSetFocus(self, evt): |
---|
536 | """ |
---|
537 | highlight the current textcrtl and hide the error text control shown |
---|
538 | after fitting |
---|
539 | :Not implemented. |
---|
540 | """ |
---|
541 | return |
---|
542 | |
---|
543 | def read_file(self, path): |
---|
544 | """ |
---|
545 | Read two columns file |
---|
546 | @param path: the path to the file to read |
---|
547 | """ |
---|
548 | try: |
---|
549 | if path==None: |
---|
550 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
551 | " Selected Distribution was not loaded: %s"%path)) |
---|
552 | return None, None |
---|
553 | input_f = open(path, 'r') |
---|
554 | buff = input_f.read() |
---|
555 | lines = buff.split('\n') |
---|
556 | |
---|
557 | angles = [] |
---|
558 | weights=[] |
---|
559 | for line in lines: |
---|
560 | toks = line.split() |
---|
561 | try: |
---|
562 | angle = float(toks[0]) |
---|
563 | weight = float(toks[1]) |
---|
564 | except: |
---|
565 | # Skip non-data lines |
---|
566 | pass |
---|
567 | angles.append(angle) |
---|
568 | weights.append(weight) |
---|
569 | return numpy.array(angles), numpy.array(weights) |
---|
570 | except: |
---|
571 | raise |
---|
572 | |
---|
573 | |
---|
574 | def createMemento(self): |
---|
575 | """ |
---|
576 | return the current state of the page |
---|
577 | """ |
---|
578 | return self.state.clone() |
---|
579 | |
---|
580 | |
---|
581 | def save_current_state(self): |
---|
582 | """ |
---|
583 | Store current state |
---|
584 | """ |
---|
585 | ## save model option |
---|
586 | if self.model!= None: |
---|
587 | self.disp_list= self.model.getDispParamList() |
---|
588 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
589 | self.state.model = self.model.clone() |
---|
590 | |
---|
591 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
592 | self.state.values= copy.deepcopy(self.values) |
---|
593 | self.state.weights = copy.deepcopy( self.weights) |
---|
594 | ## save data |
---|
595 | self.state.data= copy.deepcopy(self.data) |
---|
596 | try: |
---|
597 | n = self.disp_box.GetCurrentSelection() |
---|
598 | dispersity= self.disp_box.GetClientData(n) |
---|
599 | name= dispersity.__name__ |
---|
600 | self.disp_name = name |
---|
601 | if name == "GaussianDispersion" : |
---|
602 | if hasattr(self,"cb1"): |
---|
603 | self.state.cb1= self.cb1.GetValue() |
---|
604 | except: |
---|
605 | pass |
---|
606 | |
---|
607 | if hasattr(self,"enable_disp"): |
---|
608 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
609 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
610 | |
---|
611 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
612 | if hasattr(self,"enable_smearer"): |
---|
613 | self.state.enable_smearer = copy.deepcopy(self.enable_smearer.GetValue()) |
---|
614 | self.state.disable_smearer = copy.deepcopy(self.disable_smearer.GetValue()) |
---|
615 | |
---|
616 | if hasattr(self,"disp_box"): |
---|
617 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
618 | |
---|
619 | if len(self.disp_cb_dict)>0: |
---|
620 | for k , v in self.disp_cb_dict.iteritems(): |
---|
621 | |
---|
622 | if v ==None : |
---|
623 | self.state.disp_cb_dict[k]= v |
---|
624 | else: |
---|
625 | try: |
---|
626 | self.state.disp_cb_dict[k]=v.GetValue() |
---|
627 | except: |
---|
628 | self.state.disp_cb_dict[k]= None |
---|
629 | |
---|
630 | if len(self._disp_obj_dict)>0: |
---|
631 | for k , v in self._disp_obj_dict.iteritems(): |
---|
632 | |
---|
633 | self.state._disp_obj_dict[k]= v |
---|
634 | |
---|
635 | |
---|
636 | self.state.values = copy.deepcopy(self.values) |
---|
637 | self.state.weights = copy.deepcopy(self.weights) |
---|
638 | ## save plotting range |
---|
639 | self._save_plotting_range() |
---|
640 | |
---|
641 | self.state.orientation_params =[] |
---|
642 | self.state.orientation_params_disp =[] |
---|
643 | self.state.parameters =[] |
---|
644 | self.state.fittable_param =[] |
---|
645 | self.state.fixed_param =[] |
---|
646 | |
---|
647 | |
---|
648 | ## save checkbutton state and txtcrtl values |
---|
649 | self._copy_parameters_state(self.orientation_params, |
---|
650 | self.state.orientation_params) |
---|
651 | self._copy_parameters_state(self.orientation_params_disp, |
---|
652 | self.state.orientation_params_disp) |
---|
653 | |
---|
654 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
655 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
656 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
657 | |
---|
658 | |
---|
659 | def save_current_state_fit(self): |
---|
660 | """ |
---|
661 | Store current state for fit_page |
---|
662 | """ |
---|
663 | ## save model option |
---|
664 | if self.model!= None: |
---|
665 | self.disp_list= self.model.getDispParamList() |
---|
666 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
667 | self.state.model = self.model.clone() |
---|
668 | #if hasattr(self,self.engine_type)> 0: |
---|
669 | #self.state.engine_type = self.engine_type.clone() |
---|
670 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
671 | self.state.values= copy.deepcopy(self.values) |
---|
672 | self.state.weights = copy.deepcopy( self.weights) |
---|
673 | ## save data |
---|
674 | self.state.data= copy.deepcopy(self.data) |
---|
675 | try: |
---|
676 | n = self.disp_box.GetCurrentSelection() |
---|
677 | dispersity= self.disp_box.GetClientData(n) |
---|
678 | name= dispersity.__name__ |
---|
679 | self.disp_name = name |
---|
680 | if name == "GaussianDispersion" : |
---|
681 | if hasattr(self,"cb1"): |
---|
682 | self.state.cb1= self.cb1.GetValue() |
---|
683 | |
---|
684 | except: |
---|
685 | pass |
---|
686 | |
---|
687 | if hasattr(self,"enable_disp"): |
---|
688 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
689 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
690 | |
---|
691 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
692 | if hasattr(self,"enable_smearer"): |
---|
693 | self.state.enable_smearer = copy.deepcopy(self.enable_smearer.GetValue()) |
---|
694 | self.state.disable_smearer = copy.deepcopy(self.disable_smearer.GetValue()) |
---|
695 | |
---|
696 | if hasattr(self,"disp_box"): |
---|
697 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
698 | |
---|
699 | if len(self.disp_cb_dict)>0: |
---|
700 | for k , v in self.disp_cb_dict.iteritems(): |
---|
701 | |
---|
702 | if v ==None : |
---|
703 | self.state.disp_cb_dict[k]= v |
---|
704 | else: |
---|
705 | try: |
---|
706 | self.state.disp_cb_dict[k]=v.GetValue() |
---|
707 | except: |
---|
708 | self.state.disp_cb_dict[k]= None |
---|
709 | |
---|
710 | if len(self._disp_obj_dict)>0: |
---|
711 | for k , v in self._disp_obj_dict.iteritems(): |
---|
712 | |
---|
713 | self.state._disp_obj_dict[k]= v |
---|
714 | |
---|
715 | |
---|
716 | self.state.values = copy.deepcopy(self.values) |
---|
717 | self.state.weights = copy.deepcopy(self.weights) |
---|
718 | |
---|
719 | ## save plotting range |
---|
720 | self._save_plotting_range() |
---|
721 | |
---|
722 | ## save checkbutton state and txtcrtl values |
---|
723 | self._copy_parameters_state(self.orientation_params, |
---|
724 | self.state.orientation_params) |
---|
725 | self._copy_parameters_state(self.orientation_params_disp, |
---|
726 | self.state.orientation_params_disp) |
---|
727 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
728 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
729 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
730 | |
---|
731 | |
---|
732 | def reset_page_helper(self, state): |
---|
733 | """ |
---|
734 | Use page_state and change the state of existing page |
---|
735 | @precondition: the page is already drawn or created |
---|
736 | @postcondition: the state of the underlying data change as well as the |
---|
737 | state of the graphic interface |
---|
738 | """ |
---|
739 | if state ==None: |
---|
740 | #self._undo.Enable(False) |
---|
741 | return |
---|
742 | |
---|
743 | self.model= state.model |
---|
744 | self.data = state.data |
---|
745 | if self.data !=None: |
---|
746 | from DataLoader.qsmearing import smear_selection |
---|
747 | self.smearer= smear_selection( self.data ) |
---|
748 | self.enable2D= state.enable2D |
---|
749 | self.engine_type = state.engine_type |
---|
750 | |
---|
751 | self.disp_cb_dict = state.disp_cb_dict |
---|
752 | self.disp_list = state.disp_list |
---|
753 | |
---|
754 | ## set the state of the radio box |
---|
755 | self.shape_rbutton.SetValue(state.shape_rbutton ) |
---|
756 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
757 | self.struct_rbutton.SetValue(state.struct_rbutton ) |
---|
758 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
759 | ##draw sizer containing model parameters value for the current model |
---|
760 | self._set_model_sizer_selection( self.model ) |
---|
761 | self.set_model_param_sizer(self.model) |
---|
762 | |
---|
763 | ## reset value of combox box |
---|
764 | self.structurebox.SetSelection(state.structurecombobox ) |
---|
765 | self.formfactorbox.SetSelection(state.formfactorcombobox) |
---|
766 | |
---|
767 | |
---|
768 | ## enable the view 2d button if this is a modelpage type |
---|
769 | if hasattr(self,"model_view"): |
---|
770 | if self.enable2D: |
---|
771 | self.model_view.Disable() |
---|
772 | else: |
---|
773 | self.model_view.Enable() |
---|
774 | ## set the select all check box to the a given state |
---|
775 | if hasattr(self, "cb1"): |
---|
776 | self.cb1.SetValue(state.cb1) |
---|
777 | |
---|
778 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
779 | |
---|
780 | self._reset_parameters_state(self.orientation_params_disp, |
---|
781 | state.orientation_params_disp) |
---|
782 | self._reset_parameters_state(self.orientation_params, |
---|
783 | state.orientation_params) |
---|
784 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
785 | ## display dispersion info layer |
---|
786 | self.enable_disp.SetValue(state.enable_disp) |
---|
787 | self.disable_disp.SetValue(state.disable_disp) |
---|
788 | |
---|
789 | if hasattr(self, "disp_box"): |
---|
790 | |
---|
791 | self.disp_box.SetSelection(state.disp_box) |
---|
792 | n= self.disp_box.GetCurrentSelection() |
---|
793 | dispersity= self.disp_box.GetClientData(n) |
---|
794 | name= dispersity.__name__ |
---|
795 | |
---|
796 | self._set_dipers_Param(event=None) |
---|
797 | |
---|
798 | if name=="ArrayDispersion": |
---|
799 | |
---|
800 | for item in self.disp_cb_dict.keys(): |
---|
801 | |
---|
802 | if hasattr(self.disp_cb_dict[item],"SetValue") : |
---|
803 | self.disp_cb_dict[item].SetValue(state.disp_cb_dict[item]) |
---|
804 | # Create the dispersion objects |
---|
805 | from sans.models.dispersion_models import ArrayDispersion |
---|
806 | disp_model = ArrayDispersion() |
---|
807 | if hasattr(state,"values")and self.disp_cb_dict[item].GetValue()==True: |
---|
808 | if len(state.values)>0: |
---|
809 | self.values=state.values |
---|
810 | self.weights=state.weights |
---|
811 | disp_model.set_weights(self.values, state.weights) |
---|
812 | else: |
---|
813 | self._reset_dispersity() |
---|
814 | |
---|
815 | self._disp_obj_dict[item] = disp_model |
---|
816 | # Set the new model as the dispersion object for the selected parameter |
---|
817 | self.model.set_dispersion(item, disp_model) |
---|
818 | |
---|
819 | self.model._persistency_dict[item] = [state.values, state.weights] |
---|
820 | |
---|
821 | else: |
---|
822 | keys = self.model.getParamList() |
---|
823 | for item in keys: |
---|
824 | if item in self.disp_list and not self.model.details.has_key(item): |
---|
825 | self.model.details[item]=["",None,None] |
---|
826 | for k,v in self.state.disp_cb_dict.iteritems(): |
---|
827 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
828 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
829 | |
---|
830 | ##plotting range restore |
---|
831 | self._reset_plotting_range(state) |
---|
832 | |
---|
833 | ## smearing info restore |
---|
834 | if hasattr(self,"enable_smearer"): |
---|
835 | ## set smearing value whether or not the data contain the smearing info |
---|
836 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
837 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
838 | self.onSmear(event=None) |
---|
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 _selectDlg(self): |
---|
855 | """ |
---|
856 | open a dialog file to selected the customized dispersity |
---|
857 | """ |
---|
858 | import os |
---|
859 | dlg = wx.FileDialog(self, "Choose a weight file", |
---|
860 | self._default_save_location , "", "*.*", wx.OPEN) |
---|
861 | path = None |
---|
862 | if dlg.ShowModal() == wx.ID_OK: |
---|
863 | path = dlg.GetPath() |
---|
864 | dlg.Destroy() |
---|
865 | return path |
---|
866 | |
---|
867 | |
---|
868 | def _reset_context_menu(self): |
---|
869 | """ |
---|
870 | reset the context menu |
---|
871 | """ |
---|
872 | for name, state in self.state.saved_states.iteritems(): |
---|
873 | self.number_saved_state += 1 |
---|
874 | ## Add item in the context menu |
---|
875 | id = wx.NewId() |
---|
876 | self.popUpMenu.Append(id,name, 'Save model and state %g'%self.number_saved_state) |
---|
877 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
878 | |
---|
879 | |
---|
880 | def _reset_plotting_range(self, state): |
---|
881 | """ |
---|
882 | Reset the plotting range to a given state |
---|
883 | """ |
---|
884 | self.qmin.SetValue(str(state.qmin)) |
---|
885 | self.qmax.SetValue(str(state.qmax)) |
---|
886 | if self.state.npts!=None: |
---|
887 | self.npts.SetValue(str(state.npts)) |
---|
888 | |
---|
889 | |
---|
890 | def _save_typeOfmodel(self): |
---|
891 | """ |
---|
892 | save radiobutton containing the type model that can be selected |
---|
893 | """ |
---|
894 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
895 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
896 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
897 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
898 | self.state.structurebox= self.structurebox.GetCurrentSelection() |
---|
899 | self.state.formfactorbox = self.formfactorbox.GetCurrentSelection() |
---|
900 | |
---|
901 | #self._undo.Enable(True) |
---|
902 | ## post state to fit panel |
---|
903 | event = PageInfoEvent(page = self) |
---|
904 | wx.PostEvent(self.parent, event) |
---|
905 | |
---|
906 | |
---|
907 | def _save_plotting_range(self ): |
---|
908 | """ |
---|
909 | save the state of plotting range |
---|
910 | """ |
---|
911 | self.state.qmin = self.qmin_x |
---|
912 | self.state.qmax = self.qmax_x |
---|
913 | if self.npts!=None: |
---|
914 | self.state.npts= self.num_points |
---|
915 | |
---|
916 | |
---|
917 | def _onparamEnter_helper(self): |
---|
918 | """ |
---|
919 | check if values entered by the user are changed and valid to replot |
---|
920 | model |
---|
921 | use : _check_value_enter |
---|
922 | """ |
---|
923 | # Flag to register when a parameter has changed. |
---|
924 | is_modified = False |
---|
925 | #self._undo.Enable(True) |
---|
926 | if self.model !=None: |
---|
927 | try: |
---|
928 | is_modified =self._check_value_enter( self.fittable_param ,is_modified) |
---|
929 | is_modified =self._check_value_enter( self.fixed_param ,is_modified) |
---|
930 | is_modified =self._check_value_enter( self.parameters ,is_modified) |
---|
931 | except: |
---|
932 | pass |
---|
933 | #if is_modified: |
---|
934 | # Here we should check whether the boundaries have been modified. |
---|
935 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
936 | # set the is_modified flag to True |
---|
937 | if self._validate_qrange(self.qmin, self.qmax): |
---|
938 | tempmin = float(self.qmin.GetValue()) |
---|
939 | if tempmin != self.qmin_x: |
---|
940 | self.qmin_x = tempmin |
---|
941 | is_modified = True |
---|
942 | tempmax = float(self.qmax.GetValue()) |
---|
943 | if tempmax != self.qmax_x: |
---|
944 | self.qmax_x = tempmax |
---|
945 | is_modified = True |
---|
946 | self.fitrange = True |
---|
947 | else: |
---|
948 | self.fitrange = False |
---|
949 | if self.npts != None: |
---|
950 | if check_float(self.npts): |
---|
951 | temp_npts = float(self.npts.GetValue()) |
---|
952 | if temp_npts != self.num_points: |
---|
953 | self.num_points = temp_npts |
---|
954 | is_modified = True |
---|
955 | else: |
---|
956 | msg= "Cannot Plot :Must enter a number!!! " |
---|
957 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
958 | |
---|
959 | |
---|
960 | ## if any value is modify draw model with new value |
---|
961 | if is_modified: |
---|
962 | self.state_change= True |
---|
963 | self._draw_model() |
---|
964 | self.Refresh() |
---|
965 | return is_modified |
---|
966 | |
---|
967 | |
---|
968 | def _is_modified(self, is_modified): |
---|
969 | """ |
---|
970 | return to self._is_modified |
---|
971 | """ |
---|
972 | return is_modified |
---|
973 | |
---|
974 | def _reset_parameters_state(self, listtorestore,statelist): |
---|
975 | """ |
---|
976 | Reset the parameters at the given state |
---|
977 | """ |
---|
978 | if len(statelist)==0 or len(listtorestore)==0 : |
---|
979 | return |
---|
980 | if len(statelist)!= len(listtorestore) : |
---|
981 | return |
---|
982 | |
---|
983 | for j in range(len(listtorestore)): |
---|
984 | item_page = listtorestore[j] |
---|
985 | item_page_info = statelist[j] |
---|
986 | ##change the state of the check box for simple parameters |
---|
987 | if item_page[0]!=None: |
---|
988 | item_page[0].SetValue(item_page_info[0]) |
---|
989 | |
---|
990 | if item_page[2]!=None: |
---|
991 | item_page[2].SetValue(item_page_info[2]) |
---|
992 | |
---|
993 | if item_page[3]!=None: |
---|
994 | ## show or hide text +/- |
---|
995 | if item_page_info[2]: |
---|
996 | item_page[3].Show(True) |
---|
997 | else: |
---|
998 | item_page[3].Hide() |
---|
999 | if item_page[4]!=None: |
---|
1000 | ## show of hide the text crtl for fitting error |
---|
1001 | if item_page_info[4][0]: |
---|
1002 | item_page[4].Show(True) |
---|
1003 | item_page[4].SetValue(item_page_info[4][1]) |
---|
1004 | else: |
---|
1005 | item_page[3].Hide() |
---|
1006 | if item_page[5]!=None: |
---|
1007 | ## show of hide the text crtl for fitting error |
---|
1008 | if item_page_info[5][0]: |
---|
1009 | item_page[5].Show(True) |
---|
1010 | item_page[5].SetValue(item_page_info[5][1]) |
---|
1011 | else: |
---|
1012 | item_page[5].Hide() |
---|
1013 | |
---|
1014 | if item_page[6]!=None: |
---|
1015 | ## show of hide the text crtl for fitting error |
---|
1016 | if item_page_info[6][0]: |
---|
1017 | item_page[6].Show(True) |
---|
1018 | item_page[6].SetValue(item_page_info[6][1]) |
---|
1019 | else: |
---|
1020 | item_page[6].Hide() |
---|
1021 | |
---|
1022 | |
---|
1023 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
1024 | """ |
---|
1025 | copy the state of button |
---|
1026 | @param listtocopy: the list of check button to copy |
---|
1027 | @param statelist: list of state object to store the current state |
---|
1028 | """ |
---|
1029 | if len(listtocopy)==0: |
---|
1030 | return |
---|
1031 | |
---|
1032 | for item in listtocopy: |
---|
1033 | |
---|
1034 | checkbox_state = None |
---|
1035 | if item[0]!= None: |
---|
1036 | checkbox_state= item[0].GetValue() |
---|
1037 | parameter_name = item[1] |
---|
1038 | parameter_value = None |
---|
1039 | if item[2]!=None: |
---|
1040 | parameter_value = item[2].GetValue() |
---|
1041 | static_text = None |
---|
1042 | if item[3]!=None: |
---|
1043 | static_text = item[3].IsShown() |
---|
1044 | error_value = None |
---|
1045 | error_state = None |
---|
1046 | if item[4]!= None: |
---|
1047 | error_value = item[4].GetValue() |
---|
1048 | error_state = item[4].IsShown() |
---|
1049 | |
---|
1050 | min_value = None |
---|
1051 | min_state = None |
---|
1052 | if item[5]!= None: |
---|
1053 | min_value = item[5].GetValue() |
---|
1054 | min_state = item[5].IsShown() |
---|
1055 | |
---|
1056 | max_value = None |
---|
1057 | max_state = None |
---|
1058 | if item[6]!= None: |
---|
1059 | max_value = item[6].GetValue() |
---|
1060 | max_state = item[6].IsShown() |
---|
1061 | unit=None |
---|
1062 | if item[7]!=None: |
---|
1063 | unit = item[7].GetLabel() |
---|
1064 | |
---|
1065 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
1066 | static_text ,[error_state,error_value], |
---|
1067 | [min_state,min_value],[max_state , max_value],unit]) |
---|
1068 | |
---|
1069 | |
---|
1070 | |
---|
1071 | def _set_model_sizer_selection(self, model): |
---|
1072 | """ |
---|
1073 | Display the sizer according to the type of the current model |
---|
1074 | """ |
---|
1075 | if model ==None: |
---|
1076 | return |
---|
1077 | if hasattr(model ,"s_model"): |
---|
1078 | |
---|
1079 | class_name= model.s_model.__class__ |
---|
1080 | name= model.s_model.name |
---|
1081 | flag= name != "NoStructure" |
---|
1082 | if flag and (class_name in self.model_list_box["Structure Factors"]): |
---|
1083 | self.structurebox.Show() |
---|
1084 | self.text2.Show() |
---|
1085 | self.structurebox.Enable() |
---|
1086 | self.text2.Enable() |
---|
1087 | items = self.structurebox.GetItems() |
---|
1088 | self.sizer1.Layout() |
---|
1089 | self.SetScrollbars(20,20,25,65) |
---|
1090 | for i in range(len(items)): |
---|
1091 | if items[i]== str(name): |
---|
1092 | self.structurebox.SetSelection(i) |
---|
1093 | break |
---|
1094 | |
---|
1095 | if hasattr(model ,"p_model"): |
---|
1096 | class_name = model.p_model.__class__ |
---|
1097 | name = model.p_model.name |
---|
1098 | self.formfactorbox.Clear() |
---|
1099 | |
---|
1100 | for k, list in self.model_list_box.iteritems(): |
---|
1101 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1102 | self.shape_rbutton.SetValue(True) |
---|
1103 | ## fill the form factor list with new model |
---|
1104 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1105 | items = self.formfactorbox.GetItems() |
---|
1106 | ## set comboxbox to the selected item |
---|
1107 | for i in range(len(items)): |
---|
1108 | if items[i]== str(name): |
---|
1109 | self.formfactorbox.SetSelection(i) |
---|
1110 | break |
---|
1111 | return |
---|
1112 | elif k == "Shape-Independent": |
---|
1113 | self.shape_indep_rbutton.SetValue(True) |
---|
1114 | elif k == "Structure Factors": |
---|
1115 | self.struct_rbutton.SetValue(True) |
---|
1116 | else: |
---|
1117 | self.plugin_rbutton.SetValue(True) |
---|
1118 | |
---|
1119 | if class_name in list: |
---|
1120 | ## fill the form factor list with new model |
---|
1121 | self._populate_box(self.formfactorbox, list) |
---|
1122 | items = self.formfactorbox.GetItems() |
---|
1123 | ## set comboxbox to the selected item |
---|
1124 | for i in range(len(items)): |
---|
1125 | if items[i]== str(name): |
---|
1126 | self.formfactorbox.SetSelection(i) |
---|
1127 | break |
---|
1128 | break |
---|
1129 | else: |
---|
1130 | |
---|
1131 | ## Select the model from the menu |
---|
1132 | class_name = model.__class__ |
---|
1133 | name = model.name |
---|
1134 | self.formfactorbox.Clear() |
---|
1135 | items = self.formfactorbox.GetItems() |
---|
1136 | |
---|
1137 | for k, list in self.model_list_box.iteritems(): |
---|
1138 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1139 | if class_name in self.model_list_box["P(Q)*S(Q)"]: |
---|
1140 | self.structurebox.Show() |
---|
1141 | self.text2.Show() |
---|
1142 | self.structurebox.Enable() |
---|
1143 | self.structurebox.SetSelection(0) |
---|
1144 | self.text2.Enable() |
---|
1145 | else: |
---|
1146 | self.structurebox.Hide() |
---|
1147 | self.text2.Hide() |
---|
1148 | self.structurebox.Disable() |
---|
1149 | self.structurebox.SetSelection(0) |
---|
1150 | self.text2.Disable() |
---|
1151 | |
---|
1152 | self.shape_rbutton.SetValue(True) |
---|
1153 | ## fill the form factor list with new model |
---|
1154 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
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 | return |
---|
1162 | elif k == "Shape-Independent": |
---|
1163 | self.shape_indep_rbutton.SetValue(True) |
---|
1164 | elif k == "Structure Factors": |
---|
1165 | self.struct_rbutton.SetValue(True) |
---|
1166 | else: |
---|
1167 | self.plugin_rbutton.SetValue(True) |
---|
1168 | if class_name in list: |
---|
1169 | self.structurebox.SetSelection(0) |
---|
1170 | self.structurebox.Disable() |
---|
1171 | self.text2.Disable() |
---|
1172 | ## fill the form factor list with new model |
---|
1173 | self._populate_box(self.formfactorbox, list) |
---|
1174 | items = self.formfactorbox.GetItems() |
---|
1175 | ## set comboxbox to the selected item |
---|
1176 | for i in range(len(items)): |
---|
1177 | if items[i]== str(name): |
---|
1178 | self.formfactorbox.SetSelection(i) |
---|
1179 | break |
---|
1180 | break |
---|
1181 | |
---|
1182 | |
---|
1183 | def _draw_model(self): |
---|
1184 | """ |
---|
1185 | Method to draw or refresh a plotted model. |
---|
1186 | The method will use the data member from the model page |
---|
1187 | to build a call to the fitting perspective manager. |
---|
1188 | |
---|
1189 | [Note to coder: This way future changes will be done in only one place.] |
---|
1190 | """ |
---|
1191 | if self.model !=None: |
---|
1192 | temp_smear=None |
---|
1193 | if hasattr(self, "enable_smearer"): |
---|
1194 | if self.enable_smearer.GetValue(): |
---|
1195 | temp_smear= self.smearer |
---|
1196 | |
---|
1197 | self.manager.draw_model(self.model, data=self.data, |
---|
1198 | smearer= temp_smear, |
---|
1199 | qmin=float(self.qmin_x), |
---|
1200 | qmax=float(self.qmax_x), |
---|
1201 | qstep= float(self.num_points), |
---|
1202 | enable2D=self.enable2D) |
---|
1203 | |
---|
1204 | |
---|
1205 | def _set_model_sizer(self,sizer, box_sizer, title="", object=None): |
---|
1206 | """ |
---|
1207 | Use lists to fill a sizer for model info |
---|
1208 | """ |
---|
1209 | |
---|
1210 | sizer.Clear(True) |
---|
1211 | ##For MAC, this should defined here. |
---|
1212 | if box_sizer == None: |
---|
1213 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
1214 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1215 | else: |
---|
1216 | boxsizer1 = box_sizer |
---|
1217 | |
---|
1218 | #-------------------------------------------------------- |
---|
1219 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shapes', style=wx.RB_GROUP) |
---|
1220 | self.shape_indep_rbutton = wx.RadioButton(self, -1, "Shape-Independent") |
---|
1221 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure Factor ") |
---|
1222 | self.plugin_rbutton = wx.RadioButton(self, -1, "Customized Models") |
---|
1223 | |
---|
1224 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1225 | id= self.shape_rbutton.GetId() ) |
---|
1226 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1227 | id= self.shape_indep_rbutton.GetId() ) |
---|
1228 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1229 | id= self.struct_rbutton.GetId() ) |
---|
1230 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1231 | id= self.plugin_rbutton.GetId() ) |
---|
1232 | #MAC needs SetValue |
---|
1233 | self.shape_rbutton.SetValue(True) |
---|
1234 | |
---|
1235 | sizer_radiobutton = wx.GridSizer(2, 2,5, 5) |
---|
1236 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
1237 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
1238 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
1239 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
1240 | |
---|
1241 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
1242 | |
---|
1243 | self.text1 = wx.StaticText( self,-1,"" ) |
---|
1244 | self.text2 = wx.StaticText( self,-1,"P(Q)*S(Q)" ) |
---|
1245 | |
---|
1246 | |
---|
1247 | self.formfactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1248 | if self.model!=None: |
---|
1249 | self.formfactorbox.SetValue(self.model.name) |
---|
1250 | |
---|
1251 | |
---|
1252 | self.structurebox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1253 | wx.EVT_COMBOBOX(self.formfactorbox,-1, self._on_select_model) |
---|
1254 | wx.EVT_COMBOBOX(self.structurebox,-1, self._on_select_model) |
---|
1255 | |
---|
1256 | |
---|
1257 | ## fill combox box |
---|
1258 | if len(self.model_list_box)>0: |
---|
1259 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1260 | |
---|
1261 | if len(self.model_list_box)>0: |
---|
1262 | self._populate_box( self.structurebox, |
---|
1263 | self.model_list_box["Structure Factors"]) |
---|
1264 | self.structurebox.Insert("None", 0,None) |
---|
1265 | self.structurebox.SetSelection(0) |
---|
1266 | self.structurebox.Hide() |
---|
1267 | self.text2.Hide() |
---|
1268 | self.structurebox.Disable() |
---|
1269 | self.text2.Disable() |
---|
1270 | |
---|
1271 | if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]: |
---|
1272 | self.structurebox.Show() |
---|
1273 | self.text2.Show() |
---|
1274 | self.structurebox.Enable() |
---|
1275 | self.text2.Enable() |
---|
1276 | |
---|
1277 | ## check model type to show sizer |
---|
1278 | if self.model !=None: |
---|
1279 | self._set_model_sizer_selection( self.model ) |
---|
1280 | |
---|
1281 | sizer_selection.Add(self.text1) |
---|
1282 | sizer_selection.Add((5,5)) |
---|
1283 | sizer_selection.Add(self.formfactorbox) |
---|
1284 | sizer_selection.Add((5,5)) |
---|
1285 | sizer_selection.Add(self.text2) |
---|
1286 | sizer_selection.Add((5,5)) |
---|
1287 | sizer_selection.Add(self.structurebox) |
---|
1288 | sizer_selection.Add((5,5)) |
---|
1289 | |
---|
1290 | boxsizer1.Add( sizer_radiobutton ) |
---|
1291 | boxsizer1.Add( (20,20)) |
---|
1292 | boxsizer1.Add( sizer_selection ) |
---|
1293 | if object !=None: |
---|
1294 | boxsizer1.Add( (-72,-72)) |
---|
1295 | boxsizer1.Add( object, 0, wx.ALIGN_RIGHT| wx.RIGHT, 35) |
---|
1296 | boxsizer1.Add( (60,60)) |
---|
1297 | #-------------------------------------------------------- |
---|
1298 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1299 | sizer.Layout() |
---|
1300 | self.SetScrollbars(20,20,25,65) |
---|
1301 | |
---|
1302 | |
---|
1303 | def _show_combox(self, event): |
---|
1304 | """ |
---|
1305 | Show combox box associate with type of model selected |
---|
1306 | """ |
---|
1307 | ## Don't want to populate combo box again if the event comes from check box |
---|
1308 | if self.shape_rbutton.GetValue()and\ |
---|
1309 | event.GetEventObject()==self.shape_rbutton: |
---|
1310 | ##fill the combobox with form factor list |
---|
1311 | self.structurebox.SetSelection(0) |
---|
1312 | self.structurebox.Disable() |
---|
1313 | self.formfactorbox.Clear() |
---|
1314 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1315 | |
---|
1316 | if self.shape_indep_rbutton.GetValue()and\ |
---|
1317 | event.GetEventObject()==self.shape_indep_rbutton: |
---|
1318 | ##fill the combobox with shape independent factor list |
---|
1319 | self.structurebox.SetSelection(0) |
---|
1320 | self.structurebox.Disable() |
---|
1321 | self.formfactorbox.Clear() |
---|
1322 | self._populate_box( self.formfactorbox, |
---|
1323 | self.model_list_box["Shape-Independent"]) |
---|
1324 | |
---|
1325 | if self.struct_rbutton.GetValue() and\ |
---|
1326 | event.GetEventObject()==self.struct_rbutton: |
---|
1327 | ##fill the combobox with structure factor list |
---|
1328 | self.structurebox.SetSelection(0) |
---|
1329 | self.structurebox.Disable() |
---|
1330 | self.formfactorbox.Clear() |
---|
1331 | self._populate_box( self.formfactorbox, |
---|
1332 | self.model_list_box["Structure Factors"]) |
---|
1333 | |
---|
1334 | if self.plugin_rbutton.GetValue()and\ |
---|
1335 | event.GetEventObject()==self.plugin_rbutton: |
---|
1336 | |
---|
1337 | ##fill the combobox with form factor list |
---|
1338 | self.structurebox.Disable() |
---|
1339 | self.formfactorbox.Clear() |
---|
1340 | self._populate_box( self.formfactorbox, |
---|
1341 | self.model_list_box["Customized Models"]) |
---|
1342 | |
---|
1343 | self._on_select_model(event=None) |
---|
1344 | self._save_typeOfmodel() |
---|
1345 | self.sizer4_4.Layout() |
---|
1346 | self.sizer4.Layout() |
---|
1347 | self.Layout() |
---|
1348 | self.Refresh() |
---|
1349 | self.SetScrollbars(20,20,25,65) |
---|
1350 | |
---|
1351 | def _populate_box(self, combobox, list): |
---|
1352 | """ |
---|
1353 | fill combox box with dict item |
---|
1354 | @param list: contains item to fill the combox |
---|
1355 | item must model class |
---|
1356 | """ |
---|
1357 | for models in list: |
---|
1358 | model= models() |
---|
1359 | name = model.__class__.__name__ |
---|
1360 | if models.__name__!="NoStructure": |
---|
1361 | if hasattr(model, "name"): |
---|
1362 | name = model.name |
---|
1363 | combobox.Append(name,models) |
---|
1364 | |
---|
1365 | return 0 |
---|
1366 | |
---|
1367 | def _onparamEnter(self,event): |
---|
1368 | """ |
---|
1369 | when enter value on panel redraw model according to changed |
---|
1370 | """ |
---|
1371 | tcrtl= event.GetEventObject() |
---|
1372 | |
---|
1373 | #Clear msg if previously shown. |
---|
1374 | msg= "" |
---|
1375 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1376 | |
---|
1377 | ## save current state |
---|
1378 | self.save_current_state() |
---|
1379 | if event !=None: |
---|
1380 | #self._undo.Enable(True) |
---|
1381 | event = PageInfoEvent(page = self) |
---|
1382 | wx.PostEvent(self.parent, event) |
---|
1383 | |
---|
1384 | if check_float(tcrtl): |
---|
1385 | |
---|
1386 | self._onparamEnter_helper() |
---|
1387 | #event.Skip() |
---|
1388 | else: |
---|
1389 | msg= "Cannot Plot :Must enter a number!!! " |
---|
1390 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1391 | #event.Skip() |
---|
1392 | return |
---|
1393 | |
---|
1394 | def _onQrangeEnter(self, event): |
---|
1395 | """ |
---|
1396 | Check validity of value enter in the Q range field |
---|
1397 | """ |
---|
1398 | |
---|
1399 | tcrtl= event.GetEventObject() |
---|
1400 | #Clear msg if previously shown. |
---|
1401 | msg= "" |
---|
1402 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1403 | # Flag to register when a parameter has changed. |
---|
1404 | is_modified = False |
---|
1405 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1406 | try: |
---|
1407 | value = float(tcrtl.GetValue()) |
---|
1408 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1409 | |
---|
1410 | # If qmin and qmax have been modified, update qmin and qmax |
---|
1411 | if self._validate_qrange( self.qmin, self.qmax): |
---|
1412 | tempmin = float(self.qmin.GetValue()) |
---|
1413 | if tempmin != self.qmin_x: |
---|
1414 | self.qmin_x = tempmin |
---|
1415 | tempmax = float(self.qmax.GetValue()) |
---|
1416 | if tempmax != self.qmax_x: |
---|
1417 | self.qmax_x = tempmax |
---|
1418 | else: |
---|
1419 | tcrtl.SetBackgroundColour("pink") |
---|
1420 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1421 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1422 | return |
---|
1423 | |
---|
1424 | except: |
---|
1425 | tcrtl.SetBackgroundColour("pink") |
---|
1426 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1427 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1428 | return |
---|
1429 | #Check if # of points for theory model are valid(>0). |
---|
1430 | if self.npts != None: |
---|
1431 | if check_float(self.npts): |
---|
1432 | temp_npts = float(self.npts.GetValue()) |
---|
1433 | if temp_npts != self.num_points: |
---|
1434 | self.num_points = temp_npts |
---|
1435 | is_modified = True |
---|
1436 | else: |
---|
1437 | msg= "Cannot Plot :No npts in that Qrange!!! " |
---|
1438 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1439 | |
---|
1440 | else: |
---|
1441 | tcrtl.SetBackgroundColour("pink") |
---|
1442 | msg= "Model Error:wrong value entered!!!" |
---|
1443 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1444 | |
---|
1445 | #self._undo.Enable(True) |
---|
1446 | self.save_current_state() |
---|
1447 | event = PageInfoEvent(page = self) |
---|
1448 | wx.PostEvent(self.parent, event) |
---|
1449 | self.state_change= False |
---|
1450 | #Draw the model for a different range |
---|
1451 | self._draw_model() |
---|
1452 | |
---|
1453 | def _on_select_model_helper(self): |
---|
1454 | """ |
---|
1455 | call back for model selection |
---|
1456 | """ |
---|
1457 | ## reset dictionary containing reference to dispersion |
---|
1458 | self._disp_obj_dict = {} |
---|
1459 | self.disp_cb_dict ={} |
---|
1460 | |
---|
1461 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
1462 | #For MAC |
---|
1463 | form_factor = None |
---|
1464 | if f_id >= 0: |
---|
1465 | form_factor = self.formfactorbox.GetClientData( f_id ) |
---|
1466 | |
---|
1467 | if not form_factor in self.model_list_box["multiplication"]: |
---|
1468 | self.structurebox.Hide() |
---|
1469 | self.text2.Hide() |
---|
1470 | self.structurebox.Disable() |
---|
1471 | self.structurebox.SetSelection(0) |
---|
1472 | self.text2.Disable() |
---|
1473 | else: |
---|
1474 | self.structurebox.Show() |
---|
1475 | self.text2.Show() |
---|
1476 | self.structurebox.Enable() |
---|
1477 | self.text2.Enable() |
---|
1478 | |
---|
1479 | s_id = self.structurebox.GetCurrentSelection() |
---|
1480 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
1481 | |
---|
1482 | if struct_factor !=None: |
---|
1483 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
1484 | self.model= MultiplicationModel(form_factor(),struct_factor()) |
---|
1485 | |
---|
1486 | else: |
---|
1487 | if form_factor != None: |
---|
1488 | self.model= form_factor() |
---|
1489 | else: |
---|
1490 | self.model = None |
---|
1491 | return self.model |
---|
1492 | |
---|
1493 | ## post state to fit panel |
---|
1494 | self.state.parameters =[] |
---|
1495 | self.state.model =self.model |
---|
1496 | self.disp_list =self.model.getDispParamList() |
---|
1497 | self.state.disp_list = self.disp_list |
---|
1498 | self.Layout() |
---|
1499 | |
---|
1500 | def _validate_qrange(self, qmin_ctrl, qmax_ctrl): |
---|
1501 | """ |
---|
1502 | Verify that the Q range controls have valid values |
---|
1503 | and that Qmin < Qmax. |
---|
1504 | |
---|
1505 | @param qmin_ctrl: text control for Qmin |
---|
1506 | @param qmax_ctrl: text control for Qmax |
---|
1507 | @return: True is the Q range is value, False otherwise |
---|
1508 | """ |
---|
1509 | qmin_validity = check_float(qmin_ctrl) |
---|
1510 | qmax_validity = check_float(qmax_ctrl) |
---|
1511 | if not (qmin_validity and qmax_validity): |
---|
1512 | return False |
---|
1513 | else: |
---|
1514 | qmin = float(qmin_ctrl.GetValue()) |
---|
1515 | qmax = float(qmax_ctrl.GetValue()) |
---|
1516 | if qmin < qmax: |
---|
1517 | #Make sure to set both colours white. |
---|
1518 | qmin_ctrl.SetBackgroundColour(wx.WHITE) |
---|
1519 | qmin_ctrl.Refresh() |
---|
1520 | qmax_ctrl.SetBackgroundColour(wx.WHITE) |
---|
1521 | qmax_ctrl.Refresh() |
---|
1522 | else: |
---|
1523 | qmin_ctrl.SetBackgroundColour("pink") |
---|
1524 | qmin_ctrl.Refresh() |
---|
1525 | qmax_ctrl.SetBackgroundColour("pink") |
---|
1526 | qmax_ctrl.Refresh() |
---|
1527 | msg= "Invalid Q range: Q min must be smaller than Q max" |
---|
1528 | wx.PostEvent(self.parent, StatusEvent(status = msg)) |
---|
1529 | return False |
---|
1530 | return True |
---|
1531 | |
---|
1532 | def _check_value_enter(self, list, modified): |
---|
1533 | """ |
---|
1534 | @param list: model parameter and panel info |
---|
1535 | each item of the list should be as follow: |
---|
1536 | item=[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1537 | """ |
---|
1538 | is_modified = modified |
---|
1539 | if len(list)==0: |
---|
1540 | return is_modified |
---|
1541 | for item in list: |
---|
1542 | #skip angle parameters for 1D |
---|
1543 | if not self.enable2D: |
---|
1544 | if item in self.orientation_params: |
---|
1545 | continue |
---|
1546 | #try: |
---|
1547 | name = str(item[1]) |
---|
1548 | |
---|
1549 | try: |
---|
1550 | value= float(item[2].GetValue()) |
---|
1551 | # If the value of the parameter has changed, |
---|
1552 | # +update the model and set the is_modified flag |
---|
1553 | if value != self.model.getParam(name) and numpy.isfinite(value): |
---|
1554 | self.model.setParam(name,value) |
---|
1555 | is_modified = True |
---|
1556 | except: |
---|
1557 | msg = "Wrong parameter value entered " |
---|
1558 | wx.PostEvent(self.parent, StatusEvent(status = msg)) |
---|
1559 | |
---|
1560 | return is_modified |
---|
1561 | |
---|
1562 | |
---|
1563 | def _set_dipers_Param(self, event): |
---|
1564 | """ |
---|
1565 | respond to self.enable_disp and self.disable_disp radio box. |
---|
1566 | The dispersity object is reset inside the model into Gaussian. |
---|
1567 | When the user select yes , this method display a combo box for more selection |
---|
1568 | when the user selects No,the combo box disappears. |
---|
1569 | Redraw the model with the default dispersity (Gaussian) |
---|
1570 | """ |
---|
1571 | |
---|
1572 | self._reset_dispersity() |
---|
1573 | |
---|
1574 | if self.model ==None: |
---|
1575 | self.model_disp.Hide() |
---|
1576 | self.disp_box.Hide() |
---|
1577 | self.sizer4_4.Clear(True) |
---|
1578 | return |
---|
1579 | |
---|
1580 | if self.enable_disp.GetValue(): |
---|
1581 | self.model_disp.Show(True) |
---|
1582 | self.disp_box.Show(True) |
---|
1583 | ## layout for model containing no dispersity parameters |
---|
1584 | |
---|
1585 | self.disp_list= self.model.getDispParamList() |
---|
1586 | |
---|
1587 | if len(self.disp_list)==0 and len(self.disp_cb_dict)==0: |
---|
1588 | self._layout_sizer_noDipers() |
---|
1589 | else: |
---|
1590 | ## set gaussian sizer |
---|
1591 | self._on_select_Disp(event=None) |
---|
1592 | else: |
---|
1593 | self.model_disp.Hide() |
---|
1594 | self.disp_box.Hide() |
---|
1595 | self.disp_box.SetSelection(0) |
---|
1596 | self.sizer4_4.Clear(True) |
---|
1597 | |
---|
1598 | ## post state to fit panel |
---|
1599 | self.save_current_state() |
---|
1600 | if event !=None: |
---|
1601 | #self._undo.Enable(True) |
---|
1602 | event = PageInfoEvent(page = self) |
---|
1603 | wx.PostEvent(self.parent, event) |
---|
1604 | #draw the model with the current dispersity |
---|
1605 | self._draw_model() |
---|
1606 | self.sizer4_4.Layout() |
---|
1607 | self.sizer5.Layout() |
---|
1608 | self.Layout() |
---|
1609 | self.Refresh() |
---|
1610 | |
---|
1611 | |
---|
1612 | def _layout_sizer_noDipers(self): |
---|
1613 | """ |
---|
1614 | Draw a sizer with no dispersity info |
---|
1615 | """ |
---|
1616 | ix=0 |
---|
1617 | iy=1 |
---|
1618 | self.fittable_param=[] |
---|
1619 | self.fixed_param=[] |
---|
1620 | self.orientation_params_disp=[] |
---|
1621 | |
---|
1622 | self.model_disp.Hide() |
---|
1623 | self.disp_box.Hide() |
---|
1624 | self.sizer4_4.Clear(True) |
---|
1625 | model_disp = wx.StaticText(self, -1, 'No PolyDispersity for this model') |
---|
1626 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1627 | self.sizer4_4.Layout() |
---|
1628 | self.sizer4.Layout() |
---|
1629 | self.SetScrollbars(20,20,25,65) |
---|
1630 | |
---|
1631 | |
---|
1632 | def _reset_dispersity(self): |
---|
1633 | """ |
---|
1634 | put gaussian dispersity into current model |
---|
1635 | """ |
---|
1636 | if len(self.param_toFit)>0: |
---|
1637 | for item in self.fittable_param: |
---|
1638 | if item in self.param_toFit: |
---|
1639 | self.param_toFit.remove(item) |
---|
1640 | |
---|
1641 | for item in self.orientation_params_disp: |
---|
1642 | if item in self.param_toFit: |
---|
1643 | self.param_toFit.remove(item) |
---|
1644 | |
---|
1645 | self.fittable_param=[] |
---|
1646 | self.fixed_param=[] |
---|
1647 | self.orientation_params_disp=[] |
---|
1648 | self.values=[] |
---|
1649 | self.weights=[] |
---|
1650 | |
---|
1651 | from sans.models.dispersion_models import GaussianDispersion, ArrayDispersion |
---|
1652 | if len(self.disp_cb_dict)==0: |
---|
1653 | self.save_current_state() |
---|
1654 | self.sizer4_4.Clear(True) |
---|
1655 | self.Layout() |
---|
1656 | |
---|
1657 | return |
---|
1658 | if (len(self.disp_cb_dict)>0) : |
---|
1659 | for p in self.disp_cb_dict: |
---|
1660 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
1661 | disp_model = GaussianDispersion() |
---|
1662 | |
---|
1663 | self._disp_obj_dict[p] = disp_model |
---|
1664 | # Set the new model as the dispersion object for the selected parameter |
---|
1665 | try: |
---|
1666 | self.model.set_dispersion(p, disp_model) |
---|
1667 | except: |
---|
1668 | |
---|
1669 | pass |
---|
1670 | |
---|
1671 | ## save state into |
---|
1672 | self.save_current_state() |
---|
1673 | self.Layout() |
---|
1674 | self.Refresh() |
---|
1675 | |
---|
1676 | |
---|
1677 | def _on_select_Disp(self,event): |
---|
1678 | """ |
---|
1679 | allow selecting different dispersion |
---|
1680 | self.disp_list should change type later .now only gaussian |
---|
1681 | """ |
---|
1682 | n = self.disp_box.GetCurrentSelection() |
---|
1683 | name = self.disp_box.GetValue() |
---|
1684 | dispersity= self.disp_box.GetClientData(n) |
---|
1685 | self.disp_name = name |
---|
1686 | |
---|
1687 | if name.lower() == "array": |
---|
1688 | self._set_sizer_arraydispersion() |
---|
1689 | else: |
---|
1690 | self._set_sizer_dispersion(dispersity= dispersity) |
---|
1691 | |
---|
1692 | self.state.disp_box= n |
---|
1693 | ## Redraw the model |
---|
1694 | self._draw_model() |
---|
1695 | #self._undo.Enable(True) |
---|
1696 | event = PageInfoEvent(page = self) |
---|
1697 | wx.PostEvent(self.parent, event) |
---|
1698 | |
---|
1699 | self.sizer4_4.Layout() |
---|
1700 | self.sizer4.Layout() |
---|
1701 | self.SetScrollbars(20,20,25,65) |
---|
1702 | |
---|
1703 | def _set_sizer_arraydispersion(self): |
---|
1704 | """ |
---|
1705 | draw sizer with array dispersity parameters |
---|
1706 | """ |
---|
1707 | |
---|
1708 | if len(self.param_toFit)>0: |
---|
1709 | for item in self.fittable_param: |
---|
1710 | if item in self.param_toFit: |
---|
1711 | self.param_toFit.remove(item) |
---|
1712 | for item in self.orientation_params_disp: |
---|
1713 | if item in self.param_toFit: |
---|
1714 | self.param_toFit.remove(item) |
---|
1715 | for item in self.model.details.keys(): |
---|
1716 | if item in self.model.fixed: |
---|
1717 | del self.model.details [item] |
---|
1718 | |
---|
1719 | self.fittable_param=[] |
---|
1720 | self.fixed_param=[] |
---|
1721 | self.orientation_params_disp=[] |
---|
1722 | self.sizer4_4.Clear(True) |
---|
1723 | self._reset_dispersity() |
---|
1724 | ix=0 |
---|
1725 | iy=1 |
---|
1726 | disp1 = wx.StaticText(self, -1, 'Array Dispersion') |
---|
1727 | self.sizer4_4.Add(disp1,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1728 | |
---|
1729 | # Look for model parameters to which we can apply an ArrayDispersion model |
---|
1730 | # Add a check box for each parameter. |
---|
1731 | self.disp_cb_dict = {} |
---|
1732 | ix+=1 |
---|
1733 | self.noDisper_rbox = wx.RadioButton(self, -1,"None", (10, 10),style= wx.RB_GROUP) |
---|
1734 | self.Bind(wx.EVT_RADIOBUTTON,self.select_disp_angle , id=self.noDisper_rbox.GetId()) |
---|
1735 | #MAC needs SetValue |
---|
1736 | self.noDisper_rbox.SetValue(True) |
---|
1737 | self.sizer4_4.Add(self.noDisper_rbox, (iy, ix), |
---|
1738 | (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1739 | |
---|
1740 | for p in self.model.dispersion.keys(): |
---|
1741 | if not p in self.model.orientation_params: |
---|
1742 | ix+=1 |
---|
1743 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
1744 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
1745 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, id=self.disp_cb_dict[p].GetId()) |
---|
1746 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1747 | |
---|
1748 | for p in self.model.dispersion.keys(): |
---|
1749 | if p in self.model.orientation_params: |
---|
1750 | ix+=1 |
---|
1751 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
1752 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
1753 | if not (self.enable2D or self.data.__class__.__name__ =="Data2D"): |
---|
1754 | self.disp_cb_dict[p].Hide() |
---|
1755 | else: |
---|
1756 | self.disp_cb_dict[p].Show(True) |
---|
1757 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, id=self.disp_cb_dict[p].GetId()) |
---|
1758 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1759 | |
---|
1760 | |
---|
1761 | ix =0 |
---|
1762 | iy +=1 |
---|
1763 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1764 | self.Layout() |
---|
1765 | |
---|
1766 | self.state.orientation_params =[] |
---|
1767 | self.state.orientation_params_disp =[] |
---|
1768 | self.state.parameters =[] |
---|
1769 | self.state.fittable_param =[] |
---|
1770 | self.state.fixed_param =[] |
---|
1771 | |
---|
1772 | ## save checkbutton state and txtcrtl values |
---|
1773 | |
---|
1774 | self._copy_parameters_state(self.orientation_params, |
---|
1775 | self.state.orientation_params) |
---|
1776 | |
---|
1777 | self._copy_parameters_state(self.orientation_params_disp, |
---|
1778 | self.state.orientation_params_disp) |
---|
1779 | |
---|
1780 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
1781 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
1782 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
1783 | |
---|
1784 | |
---|
1785 | ## post state to fit panel |
---|
1786 | event = PageInfoEvent(page = self) |
---|
1787 | wx.PostEvent(self.parent, event) |
---|
1788 | |
---|
1789 | |
---|
1790 | |
---|
1791 | |
---|
1792 | |
---|
1793 | def _set_range_sizer(self, title, box_sizer=None, object1=None,object=None): |
---|
1794 | """ |
---|
1795 | Fill the Q range sizer |
---|
1796 | """ |
---|
1797 | self.sizer5.Clear(True) |
---|
1798 | #-------------------------------------------------------------- |
---|
1799 | if box_sizer == None: |
---|
1800 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
1801 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1802 | else: |
---|
1803 | #for MAC |
---|
1804 | boxsizer1 = box_sizer |
---|
1805 | |
---|
1806 | self.qmin = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20),style=wx.TE_PROCESS_ENTER, |
---|
1807 | kill_focus_callback = self._onQrangeEnter, |
---|
1808 | text_enter_callback = self._onQrangeEnter) |
---|
1809 | self.qmin.SetValue(str(self.qmin_x)) |
---|
1810 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
1811 | |
---|
1812 | self.qmax = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20),style=wx.TE_PROCESS_ENTER, |
---|
1813 | kill_focus_callback = self._onQrangeEnter, |
---|
1814 | text_enter_callback = self._onQrangeEnter) |
---|
1815 | self.qmax.SetValue(str(self.qmax_x)) |
---|
1816 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
1817 | |
---|
1818 | id = wx.NewId() |
---|
1819 | self.reset_qrange =wx.Button(self,id,'Reset',size=(70,23)) |
---|
1820 | |
---|
1821 | self.reset_qrange.Bind(wx.EVT_BUTTON, self.on_reset_clicked,id=id) |
---|
1822 | self.reset_qrange.SetToolTipString("Reset Q range to the default values") |
---|
1823 | |
---|
1824 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
1825 | sizer= wx.GridSizer(3, 3,2, 5) |
---|
1826 | |
---|
1827 | sizer.Add(wx.StaticText(self, -1, ' Q range')) |
---|
1828 | sizer.Add(wx.StaticText(self, -1, ' Min')) |
---|
1829 | sizer.Add(wx.StaticText(self, -1, ' Max')) |
---|
1830 | sizer.Add(self.reset_qrange) |
---|
1831 | |
---|
1832 | sizer.Add(self.qmin) |
---|
1833 | sizer.Add(self.qmax) |
---|
1834 | sizer_horizontal.Add(sizer) |
---|
1835 | if object!=None: |
---|
1836 | sizer_horizontal.Add(object) |
---|
1837 | |
---|
1838 | if object1!=None: |
---|
1839 | boxsizer1.Add(object1) |
---|
1840 | boxsizer1.Add((10,10)) |
---|
1841 | boxsizer1.Add(sizer_horizontal) |
---|
1842 | ## save state |
---|
1843 | self.save_current_state() |
---|
1844 | #---------------------------------------------------------------- |
---|
1845 | self.sizer5.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1846 | self.sizer5.Layout() |
---|
1847 | |
---|
1848 | |
---|
1849 | def _fill_save_sizer(self): |
---|
1850 | """ |
---|
1851 | Draw the layout for saving option |
---|
1852 | """ |
---|
1853 | # Skipping save state functionality for release 0.9.0 |
---|
1854 | return |
---|
1855 | |
---|
1856 | self.sizer6.Clear(True) |
---|
1857 | box_description= wx.StaticBox(self, -1,"Save Model") |
---|
1858 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1859 | sizer_save = wx.BoxSizer(wx.HORIZONTAL) |
---|
1860 | |
---|
1861 | self.btSave_title = wx.StaticText(self, -1, 'Save the current Model') |
---|
1862 | self.btSave = wx.Button(self,wx.NewId(),'Save') |
---|
1863 | self.btSave.Bind(wx.EVT_BUTTON, self.onSave,id= self.btSave.GetId()) |
---|
1864 | self.btSave.SetToolTipString("Save the current Model") |
---|
1865 | |
---|
1866 | sizer_save.Add(self.btSave_title) |
---|
1867 | sizer_save.Add((20,20),0, wx.LEFT|wx.RIGHT|wx.EXPAND,45) |
---|
1868 | |
---|
1869 | sizer_save.Add(self.btSave) |
---|
1870 | |
---|
1871 | boxsizer1.Add(sizer_save) |
---|
1872 | self.sizer6.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1873 | self.sizer6.Layout() |
---|
1874 | self.SetScrollbars(20,20,25,65) |
---|
1875 | |
---|
1876 | def _lay_out(self): |
---|
1877 | """ |
---|
1878 | returns self.Layout |
---|
1879 | Note: Mac seems to like this better when self.Layout is called after fitting. |
---|
1880 | """ |
---|
1881 | self._sleep4sec() |
---|
1882 | self.Layout() |
---|
1883 | #self._sleep4sec() |
---|
1884 | return |
---|
1885 | |
---|
1886 | def _sleep4sec(self): |
---|
1887 | """ |
---|
1888 | sleep for 1 sec only applied on Mac |
---|
1889 | Note: This 1sec helps for Mac not to crash on self.:ayout after self._draw_model |
---|
1890 | """ |
---|
1891 | if ON_MAC == True: |
---|
1892 | time.sleep(1) |
---|
1893 | |
---|
1894 | def on_reset_clicked(self,event): |
---|
1895 | """ |
---|
1896 | #On 'Reset' button for Q range clicked |
---|
1897 | """ |
---|
1898 | ##For 3 different cases: Data2D, Data1D, and theory |
---|
1899 | if self.data.__class__.__name__ == "Data2D": |
---|
1900 | data_min= 0 |
---|
1901 | x= max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
1902 | y= max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
1903 | self.qmin_x = data_min |
---|
1904 | self.qmax_x = math.sqrt(x*x + y*y) |
---|
1905 | elif self.data.__class__.__name__ == "Data1D": |
---|
1906 | self.qmin_x = min(self.data.x) |
---|
1907 | self.qmax_x = max(self.data.x) |
---|
1908 | else: |
---|
1909 | self.qmin_x = _QMIN_DEFAULT |
---|
1910 | self.qmax_x = _QMAX_DEFAULT |
---|
1911 | self.num_points = _NPTS_DEFAULT |
---|
1912 | self.state.npts = self.num_points |
---|
1913 | |
---|
1914 | |
---|
1915 | self.state.qmin = self.qmin_x |
---|
1916 | self.state.qmax = self.qmax_x |
---|
1917 | |
---|
1918 | #reset the q range values |
---|
1919 | self._reset_plotting_range(self.state) |
---|
1920 | #Re draw plot |
---|
1921 | self._draw_model() |
---|
1922 | |
---|
1923 | def on_model_help_clicked(self,event): |
---|
1924 | """ |
---|
1925 | #On 'More details' button |
---|
1926 | """ |
---|
1927 | from help_panel import HelpWindow |
---|
1928 | |
---|
1929 | if self.model == None: |
---|
1930 | name = 'FuncHelp' |
---|
1931 | else: |
---|
1932 | name = self.model.origin_name |
---|
1933 | |
---|
1934 | frame = HelpWindow(None, -1, pageToOpen="doc/model_functions.html") |
---|
1935 | frame.Show(True) |
---|
1936 | if frame.rhelp.HasAnchor(name): |
---|
1937 | frame.rhelp.ScrollToAnchor(name) |
---|
1938 | else: |
---|
1939 | msg= "Model does not contains an available description " |
---|
1940 | msg +="Please try searching in the Help window" |
---|
1941 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1942 | |
---|