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