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 sans.guiframe.panel_base import PanelBase |
---|
11 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
12 | from sans.guiframe.utils import format_number,check_float |
---|
13 | from sans.guiframe.events import PanelOnFocusEvent |
---|
14 | from sans.guiframe.events import StatusEvent |
---|
15 | from sans.guiframe.events import AppendBookmarkEvent |
---|
16 | import pagestate |
---|
17 | from pagestate import PageState |
---|
18 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
19 | (PreviousStateEvent, EVT_PREVIOUS_STATE) = wx.lib.newevent.NewEvent() |
---|
20 | (NextStateEvent, EVT_NEXT_STATE) = wx.lib.newevent.NewEvent() |
---|
21 | |
---|
22 | _BOX_WIDTH = 76 |
---|
23 | _QMIN_DEFAULT = 0.001 |
---|
24 | _QMAX_DEFAULT = 0.13 |
---|
25 | _NPTS_DEFAULT = 50 |
---|
26 | #Control panel width |
---|
27 | if sys.platform.count("darwin")==0: |
---|
28 | PANEL_WIDTH = 450 |
---|
29 | FONT_VARIANT = 0 |
---|
30 | ON_MAC = False |
---|
31 | else: |
---|
32 | PANEL_WIDTH = 500 |
---|
33 | FONT_VARIANT = 1 |
---|
34 | ON_MAC = True |
---|
35 | |
---|
36 | class BasicPage(ScrolledPanel, PanelBase): |
---|
37 | """ |
---|
38 | This class provide general structure of fitpanel page |
---|
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, **kwargs): |
---|
46 | """ |
---|
47 | """ |
---|
48 | ScrolledPanel.__init__(self, parent, **kwargs) |
---|
49 | PanelBase.__init__(self) |
---|
50 | self.SetupScrolling() |
---|
51 | #Set window's font size |
---|
52 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
53 | |
---|
54 | ## parent of the page |
---|
55 | self.parent = parent |
---|
56 | ## manager is the fitting plugin |
---|
57 | ## owner of the page (fitting plugin) |
---|
58 | self.event_owner = None |
---|
59 | ## current model |
---|
60 | self.model = None |
---|
61 | ## data |
---|
62 | self.data = None |
---|
63 | self.mask = None |
---|
64 | ## Q range |
---|
65 | self.qmax_x = None |
---|
66 | self.qmin_x = None |
---|
67 | self.num_points= _NPTS_DEFAULT |
---|
68 | ## total number of point: float |
---|
69 | self.npts = None |
---|
70 | ## default fitengine type |
---|
71 | self.engine_type = 'scipy' |
---|
72 | ## smear default |
---|
73 | self.smearer = None |
---|
74 | self.current_smearer = None |
---|
75 | ## 2D smear accuracy default |
---|
76 | self.smear2d_accuracy = 'Low' |
---|
77 | ## slit smear: |
---|
78 | self.dxl = None |
---|
79 | self.dxw = None |
---|
80 | ## pinhole smear |
---|
81 | self.dx_min = None |
---|
82 | self.dx_max = None |
---|
83 | |
---|
84 | self.disp_cb_dict = {} |
---|
85 | |
---|
86 | self.state = PageState(parent=parent) |
---|
87 | ## dictionary containing list of models |
---|
88 | self.model_list_box = None |
---|
89 | self.set_page_info(page_info=page_info) |
---|
90 | ## Data member to store the dispersion object created |
---|
91 | self._disp_obj_dict = {} |
---|
92 | ## selected parameters to apply dispersion |
---|
93 | self.disp_cb_dict ={} |
---|
94 | |
---|
95 | ## smearer object |
---|
96 | self.smearer = None |
---|
97 | |
---|
98 | ##list of model parameters. each item must have same length |
---|
99 | ## each item related to a given parameters |
---|
100 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
101 | self.parameters = [] |
---|
102 | # non-fittable parameter whose value is astring |
---|
103 | self.str_parameters = [] |
---|
104 | ## list of parameters to fit , must be like self.parameters |
---|
105 | self.param_toFit = [] |
---|
106 | ## list of looking like parameters but with non fittable parameters info |
---|
107 | self.fixed_param = [] |
---|
108 | ## list of looking like parameters but with fittable parameters info |
---|
109 | self.fittable_param = [] |
---|
110 | ##list of dispersion parameters |
---|
111 | self.disp_list = [] |
---|
112 | self.disp_name = "" |
---|
113 | |
---|
114 | ## list of orientation parameters |
---|
115 | self.orientation_params = [] |
---|
116 | self.orientation_params_disp = [] |
---|
117 | if self.model != None: |
---|
118 | self.disp_list = self.model.getDispParamList() |
---|
119 | |
---|
120 | ##enable model 2D draw |
---|
121 | self.enable2D = False |
---|
122 | ## check that the fit range is correct to plot the model again |
---|
123 | self.fitrange = True |
---|
124 | ## Create memento to save the current state |
---|
125 | self.state = PageState(parent=self.parent, |
---|
126 | model=self.model, data=self.data) |
---|
127 | ## flag to determine if state has change |
---|
128 | self.state_change = False |
---|
129 | ## save customized array |
---|
130 | self.values = [] |
---|
131 | self.weights = [] |
---|
132 | ## retrieve saved state |
---|
133 | self.number_saved_state = 0 |
---|
134 | ## dictionary of saved state |
---|
135 | self.saved_states = {} |
---|
136 | ## Create context menu for page |
---|
137 | self.popUpMenu = wx.Menu() |
---|
138 | #id = wx.NewId() |
---|
139 | #self._undo = wx.MenuItem(self.popUpMenu,id, "Undo", |
---|
140 | #"cancel the previous action") |
---|
141 | #self.popUpMenu.AppendItem(self._undo) |
---|
142 | #self._undo.Enable(False) |
---|
143 | #wx.EVT_MENU(self, id, self.onUndo) |
---|
144 | |
---|
145 | #id = wx.NewId() |
---|
146 | #self._redo = wx.MenuItem(self.popUpMenu,id,"Redo", |
---|
147 | #" Restore the previous action") |
---|
148 | #self.popUpMenu.AppendItem(self._redo) |
---|
149 | #self._redo.Enable(False) |
---|
150 | #wx.EVT_MENU(self, id, self.onRedo) |
---|
151 | #self.popUpMenu.AppendSeparator() |
---|
152 | #if sys.platform.count("win32")>0: |
---|
153 | id = wx.NewId() |
---|
154 | self._keep = wx.MenuItem(self.popUpMenu,id,"BookMark", |
---|
155 | " Keep the panel status to recall it later") |
---|
156 | self.popUpMenu.AppendItem(self._keep) |
---|
157 | self._keep.Enable(True) |
---|
158 | self._set_bookmark_flag(False) |
---|
159 | self._set_save_flag(False) |
---|
160 | wx.EVT_MENU(self, id, self.on_bookmark) |
---|
161 | self.popUpMenu.AppendSeparator() |
---|
162 | |
---|
163 | ## Default locations |
---|
164 | self._default_save_location = os.getcwd() |
---|
165 | ## save initial state on context menu |
---|
166 | #self.onSave(event=None) |
---|
167 | self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) |
---|
168 | |
---|
169 | ## create the basic structure of the panel with empty sizer |
---|
170 | self.define_page_structure() |
---|
171 | ## drawing Initial dispersion parameters sizer |
---|
172 | self.set_dispers_sizer() |
---|
173 | |
---|
174 | ## layout |
---|
175 | self.set_layout() |
---|
176 | |
---|
177 | def on_set_focus(self, event): |
---|
178 | """ |
---|
179 | """ |
---|
180 | if self._manager is not None: |
---|
181 | wx.PostEvent(self._manager.parent, PanelOnFocusEvent(panel=self)) |
---|
182 | |
---|
183 | class ModelTextCtrl(wx.TextCtrl): |
---|
184 | """ |
---|
185 | Text control for model and fit parameters. |
---|
186 | Binds the appropriate events for user interactions. |
---|
187 | Default callback methods can be overwritten on initialization |
---|
188 | |
---|
189 | :param kill_focus_callback: callback method for EVT_KILL_FOCUS event |
---|
190 | :param set_focus_callback: callback method for EVT_SET_FOCUS event |
---|
191 | :param mouse_up_callback: callback method for EVT_LEFT_UP event |
---|
192 | :param text_enter_callback: callback method for EVT_TEXT_ENTER event |
---|
193 | |
---|
194 | """ |
---|
195 | ## Set to True when the mouse is clicked while the whole string is selected |
---|
196 | full_selection = False |
---|
197 | ## Call back for EVT_SET_FOCUS events |
---|
198 | _on_set_focus_callback = None |
---|
199 | |
---|
200 | def __init__(self, parent, id=-1, |
---|
201 | value=wx.EmptyString, |
---|
202 | pos=wx.DefaultPosition, |
---|
203 | size=wx.DefaultSize, |
---|
204 | style=0, |
---|
205 | validator=wx.DefaultValidator, |
---|
206 | name=wx.TextCtrlNameStr, |
---|
207 | kill_focus_callback=None, |
---|
208 | set_focus_callback=None, |
---|
209 | mouse_up_callback=None, |
---|
210 | text_enter_callback = None): |
---|
211 | |
---|
212 | wx.TextCtrl.__init__(self, parent, id, value, pos, |
---|
213 | size, style, validator, name) |
---|
214 | |
---|
215 | # Bind appropriate events |
---|
216 | self._on_set_focus_callback = parent.onSetFocus \ |
---|
217 | if set_focus_callback is None else set_focus_callback |
---|
218 | self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus) |
---|
219 | self.Bind(wx.EVT_KILL_FOCUS, self._silent_kill_focus \ |
---|
220 | if kill_focus_callback is None else kill_focus_callback) |
---|
221 | self.Bind(wx.EVT_TEXT_ENTER, parent._onparamEnter \ |
---|
222 | if text_enter_callback is None else text_enter_callback) |
---|
223 | if not ON_MAC : |
---|
224 | self.Bind(wx.EVT_LEFT_UP, self._highlight_text \ |
---|
225 | if mouse_up_callback is None else mouse_up_callback) |
---|
226 | |
---|
227 | def _on_set_focus(self, event): |
---|
228 | """ |
---|
229 | Catch when the text control is set in focus to highlight the whole |
---|
230 | text if necessary |
---|
231 | |
---|
232 | :param event: mouse event |
---|
233 | |
---|
234 | """ |
---|
235 | event.Skip() |
---|
236 | self.full_selection = True |
---|
237 | return self._on_set_focus_callback(event) |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | def _highlight_text(self, event): |
---|
242 | """ |
---|
243 | Highlight text of a TextCtrl only of no text has be selected |
---|
244 | |
---|
245 | :param event: mouse event |
---|
246 | |
---|
247 | """ |
---|
248 | # Make sure the mouse event is available to other listeners |
---|
249 | event.Skip() |
---|
250 | control = event.GetEventObject() |
---|
251 | if self.full_selection: |
---|
252 | self.full_selection = False |
---|
253 | # Check that we have a TextCtrl |
---|
254 | if issubclass(control.__class__, wx.TextCtrl): |
---|
255 | # Check whether text has been selected, |
---|
256 | # if not, select the whole string |
---|
257 | (start, end) = control.GetSelection() |
---|
258 | if start==end: |
---|
259 | control.SetSelection(-1,-1) |
---|
260 | |
---|
261 | def _silent_kill_focus(self,event): |
---|
262 | """ |
---|
263 | Save the state of the page |
---|
264 | """ |
---|
265 | |
---|
266 | event.Skip() |
---|
267 | pass |
---|
268 | |
---|
269 | def set_page_info(self, page_info): |
---|
270 | """ |
---|
271 | set some page important information at once |
---|
272 | """ |
---|
273 | ##window_name |
---|
274 | self.window_name = page_info.window_name |
---|
275 | ##window_caption |
---|
276 | self.window_caption = page_info.window_caption |
---|
277 | ## manager is the fitting plugin |
---|
278 | self._manager= page_info.manager |
---|
279 | ## owner of the page (fitting plugin) |
---|
280 | self.event_owner= page_info.event_owner |
---|
281 | ## current model |
---|
282 | self.model = page_info.model |
---|
283 | ## data |
---|
284 | self.data = page_info.data |
---|
285 | ## dictionary containing list of models |
---|
286 | self.model_list_box = page_info.model_list_box |
---|
287 | ## Data member to store the dispersion object created |
---|
288 | self.populate_box(dict=self.model_list_box) |
---|
289 | |
---|
290 | def onContextMenu(self, event): |
---|
291 | """ |
---|
292 | Retrieve the state selected state |
---|
293 | """ |
---|
294 | # Skipping the save state functionality for release 0.9.0 |
---|
295 | #return |
---|
296 | |
---|
297 | pos = event.GetPosition() |
---|
298 | pos = self.ScreenToClient(pos) |
---|
299 | |
---|
300 | self.PopupMenu(self.popUpMenu, pos) |
---|
301 | |
---|
302 | |
---|
303 | def onUndo(self, event): |
---|
304 | """ |
---|
305 | Cancel the previous action |
---|
306 | """ |
---|
307 | #print "enable undo" |
---|
308 | event = PreviousStateEvent(page = self) |
---|
309 | wx.PostEvent(self.parent, event) |
---|
310 | |
---|
311 | def onRedo(self, event): |
---|
312 | """ |
---|
313 | Restore the previous action cancelled |
---|
314 | """ |
---|
315 | #print "enable redo" |
---|
316 | event = NextStateEvent(page= self) |
---|
317 | wx.PostEvent(self.parent, event) |
---|
318 | |
---|
319 | def define_page_structure(self): |
---|
320 | """ |
---|
321 | Create empty sizer for a panel |
---|
322 | """ |
---|
323 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
324 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
325 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
326 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
327 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
328 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
329 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
330 | self.sizer6 = wx.BoxSizer(wx.VERTICAL) |
---|
331 | |
---|
332 | self.sizer0.SetMinSize((PANEL_WIDTH,-1)) |
---|
333 | self.sizer1.SetMinSize((PANEL_WIDTH,-1)) |
---|
334 | self.sizer2.SetMinSize((PANEL_WIDTH,-1)) |
---|
335 | self.sizer3.SetMinSize((PANEL_WIDTH,-1)) |
---|
336 | self.sizer4.SetMinSize((PANEL_WIDTH,-1)) |
---|
337 | self.sizer5.SetMinSize((PANEL_WIDTH,-1)) |
---|
338 | self.sizer6.SetMinSize((PANEL_WIDTH,-1)) |
---|
339 | |
---|
340 | self.vbox.Add(self.sizer0) |
---|
341 | self.vbox.Add(self.sizer1) |
---|
342 | self.vbox.Add(self.sizer2) |
---|
343 | self.vbox.Add(self.sizer3) |
---|
344 | self.vbox.Add(self.sizer4) |
---|
345 | self.vbox.Add(self.sizer5) |
---|
346 | self.vbox.Add(self.sizer6) |
---|
347 | |
---|
348 | def set_layout(self): |
---|
349 | """ |
---|
350 | layout |
---|
351 | """ |
---|
352 | self.vbox.Layout() |
---|
353 | self.vbox.Fit(self) |
---|
354 | self.SetSizer(self.vbox) |
---|
355 | self.Centre() |
---|
356 | |
---|
357 | def set_owner(self,owner): |
---|
358 | """ |
---|
359 | set owner of fitpage |
---|
360 | |
---|
361 | :param owner: the class responsible of plotting |
---|
362 | |
---|
363 | """ |
---|
364 | self.event_owner = owner |
---|
365 | self.state.event_owner = owner |
---|
366 | |
---|
367 | def get_state(self): |
---|
368 | """ |
---|
369 | """ |
---|
370 | return self.state |
---|
371 | def get_data(self): |
---|
372 | """ |
---|
373 | return the current data |
---|
374 | """ |
---|
375 | return self.data |
---|
376 | |
---|
377 | def set_manager(self, manager): |
---|
378 | """ |
---|
379 | set panel manager |
---|
380 | |
---|
381 | :param manager: instance of plugin fitting |
---|
382 | |
---|
383 | """ |
---|
384 | self._manager = manager |
---|
385 | self.state.manager = manager |
---|
386 | |
---|
387 | def populate_box(self, dict): |
---|
388 | """ |
---|
389 | Store list of model |
---|
390 | |
---|
391 | :param dict: dictionary containing list of models |
---|
392 | |
---|
393 | """ |
---|
394 | self.model_list_box = dict |
---|
395 | self.state.model_list_box = self.model_list_box |
---|
396 | |
---|
397 | def initialize_combox(self): |
---|
398 | """ |
---|
399 | put default value in the combobox |
---|
400 | """ |
---|
401 | ## fill combox box |
---|
402 | if self.model_list_box is None: |
---|
403 | return |
---|
404 | if len(self.model_list_box) > 0: |
---|
405 | self._populate_box(self.formfactorbox, |
---|
406 | self.model_list_box["Shapes"]) |
---|
407 | |
---|
408 | if len(self.model_list_box) > 0: |
---|
409 | self._populate_box(self.structurebox, |
---|
410 | self.model_list_box["Structure Factors"]) |
---|
411 | self.structurebox.Insert("None", 0, None) |
---|
412 | self.structurebox.SetSelection(0) |
---|
413 | self.structurebox.Hide() |
---|
414 | self.text2.Hide() |
---|
415 | self.structurebox.Disable() |
---|
416 | self.text2.Disable() |
---|
417 | |
---|
418 | if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]: |
---|
419 | self.structurebox.Show() |
---|
420 | self.text2.Show() |
---|
421 | self.structurebox.Enable() |
---|
422 | self.text2.Enable() |
---|
423 | |
---|
424 | def set_dispers_sizer(self): |
---|
425 | """ |
---|
426 | fill sizer containing dispersity info |
---|
427 | """ |
---|
428 | self.sizer4.Clear(True) |
---|
429 | name="Polydispersity and Orientational Distribution" |
---|
430 | box_description= wx.StaticBox(self, -1,name) |
---|
431 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
432 | #---------------------------------------------------- |
---|
433 | self.disable_disp = wx.RadioButton(self, -1, 'Off', (10, 10), |
---|
434 | style=wx.RB_GROUP) |
---|
435 | self.enable_disp = wx.RadioButton(self, -1, 'On', (10, 30)) |
---|
436 | |
---|
437 | |
---|
438 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
439 | id=self.disable_disp.GetId()) |
---|
440 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
441 | id=self.enable_disp.GetId()) |
---|
442 | #MAC needs SetValue |
---|
443 | self.disable_disp.SetValue(True) |
---|
444 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
445 | sizer_dispersion.Add((20,20)) |
---|
446 | name=""#Polydispersity and \nOrientational Distribution " |
---|
447 | sizer_dispersion.Add(wx.StaticText(self,-1,name)) |
---|
448 | sizer_dispersion.Add(self.enable_disp ) |
---|
449 | sizer_dispersion.Add((20,20)) |
---|
450 | sizer_dispersion.Add(self.disable_disp ) |
---|
451 | sizer_dispersion.Add((10,10)) |
---|
452 | |
---|
453 | ## fill a sizer with the combobox to select dispersion type |
---|
454 | sizer_select_dispers = wx.BoxSizer(wx.HORIZONTAL) |
---|
455 | self.model_disp = wx.StaticText(self, -1, 'Distribution Function ') |
---|
456 | |
---|
457 | import sans.models.dispersion_models |
---|
458 | self.polydisp= sans.models.dispersion_models.models |
---|
459 | self.disp_box = wx.ComboBox(self, -1) |
---|
460 | |
---|
461 | for key, value in self.polydisp.iteritems(): |
---|
462 | name = str(key) |
---|
463 | self.disp_box.Append(name,value) |
---|
464 | self.disp_box.SetStringSelection("gaussian") |
---|
465 | wx.EVT_COMBOBOX(self.disp_box,-1, self._on_select_Disp) |
---|
466 | |
---|
467 | sizer_select_dispers.Add((10,10)) |
---|
468 | sizer_select_dispers.Add(self.model_disp) |
---|
469 | sizer_select_dispers.Add(self.disp_box,0, |
---|
470 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
471 | |
---|
472 | self.model_disp.Hide() |
---|
473 | self.disp_box.Hide() |
---|
474 | |
---|
475 | boxsizer1.Add( sizer_dispersion,0, |
---|
476 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
477 | #boxsizer1.Add( (10,10) ) |
---|
478 | boxsizer1.Add( sizer_select_dispers ) |
---|
479 | self.sizer4_4 = wx.GridBagSizer(5,5) |
---|
480 | boxsizer1.Add( self.sizer4_4 ) |
---|
481 | #----------------------------------------------------- |
---|
482 | self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
483 | self.sizer4_4.Layout() |
---|
484 | self.sizer4.Layout() |
---|
485 | self.Layout() |
---|
486 | |
---|
487 | self.Refresh() |
---|
488 | ## saving the state of enable dispersity button |
---|
489 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
490 | self.state.disable_disp= self.disable_disp.GetValue() |
---|
491 | |
---|
492 | def select_disp_angle(self, event): |
---|
493 | """ |
---|
494 | Event for when a user select a parameter to average over. |
---|
495 | |
---|
496 | :param event: radiobutton event |
---|
497 | |
---|
498 | """ |
---|
499 | self.values=[] |
---|
500 | self.weights=[] |
---|
501 | if event.GetEventObject()==self.noDisper_rbox: |
---|
502 | if self.noDisper_rbox.GetValue(): |
---|
503 | #No array dispersity apply yet |
---|
504 | self._reset_dispersity() |
---|
505 | ## Redraw the model ??? |
---|
506 | self._draw_model() |
---|
507 | # Go through the list of dispersion check boxes to identify |
---|
508 | # which one has changed |
---|
509 | for p in self.disp_cb_dict: |
---|
510 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
511 | # Catch which one of the box was just checked or unchecked. |
---|
512 | if event.GetEventObject() == self.disp_cb_dict[p]: |
---|
513 | if self.disp_cb_dict[p].GetValue() == True: |
---|
514 | |
---|
515 | ##Temp. FIX for V1.0 regarding changing checkbox |
---|
516 | #to radiobutton. |
---|
517 | ##This (self._reset_dispersity) should be removed |
---|
518 | #when the array dispersion is fixed. |
---|
519 | self._reset_dispersity() |
---|
520 | |
---|
521 | # The user wants this parameter to be averaged. |
---|
522 | # Pop up the file selection dialog. |
---|
523 | path = self._selectDlg() |
---|
524 | |
---|
525 | # If nothing was selected, just return |
---|
526 | if path is None: |
---|
527 | self.disp_cb_dict[p].SetValue(False) |
---|
528 | self.noDisper_rbox.SetValue(True) |
---|
529 | return |
---|
530 | try: |
---|
531 | self._default_save_location = os.path.dirname(path) |
---|
532 | except: |
---|
533 | pass |
---|
534 | try: |
---|
535 | self.values,self.weights = self.read_file(path) |
---|
536 | except: |
---|
537 | msg="Could not read input file" |
---|
538 | wx.PostEvent(self.parent.parent, |
---|
539 | StatusEvent(status=msg)) |
---|
540 | return |
---|
541 | |
---|
542 | # If any of the two arrays is empty, notify the user that we won't |
---|
543 | # proceed |
---|
544 | if self.values is None or self.weights is None or \ |
---|
545 | self.values ==[] or self.weights ==[]: |
---|
546 | msg = "The loaded %s distrubtion is" |
---|
547 | msg + " corrupted or empty" % p |
---|
548 | wx.PostEvent(self.parent.parent, |
---|
549 | StatusEvent(status=msg)) |
---|
550 | return |
---|
551 | |
---|
552 | # Tell the user that we are about to apply the distribution |
---|
553 | msg = "Applying loaded %s distribution: %s" % (p, path) |
---|
554 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
555 | |
---|
556 | # Create the dispersion objects |
---|
557 | from sans.models.dispersion_models import ArrayDispersion |
---|
558 | disp_model = ArrayDispersion() |
---|
559 | disp_model.set_weights(self.values, self.weights) |
---|
560 | |
---|
561 | # Store the object to make it persist outside the |
---|
562 | # scope of this method |
---|
563 | #TODO: refactor model to clean this up? |
---|
564 | self._disp_obj_dict[p] = disp_model |
---|
565 | self.state._disp_obj_dict [p]= disp_model |
---|
566 | self.state.values = [] |
---|
567 | self.state.weights = [] |
---|
568 | self.state.values = copy.deepcopy(self.values) |
---|
569 | self.state.weights = copy.deepcopy(self.weights) |
---|
570 | # Set the new model as the dispersion object for the |
---|
571 | #selected parameter |
---|
572 | self.model.set_dispersion(p, disp_model) |
---|
573 | # Store a reference to the weights in the model object |
---|
574 | #so that |
---|
575 | # it's not lost when we use the model within another thread. |
---|
576 | #TODO: total hack - fix this |
---|
577 | self.state.model= self.model.clone() |
---|
578 | |
---|
579 | self.model._persistency_dict = {} |
---|
580 | self.model._persistency_dict[p] = \ |
---|
581 | [self.values, self.weights] |
---|
582 | self.state.model._persistency_dict[p] = \ |
---|
583 | [self.values,self.weights] |
---|
584 | else: |
---|
585 | self._reset_dispersity() |
---|
586 | |
---|
587 | ## Redraw the model |
---|
588 | self._draw_model() |
---|
589 | |
---|
590 | ## post state to fit panel |
---|
591 | event = PageInfoEvent(page = self) |
---|
592 | wx.PostEvent(self.parent, event) |
---|
593 | |
---|
594 | |
---|
595 | def onResetModel(self, event): |
---|
596 | """ |
---|
597 | Reset model state |
---|
598 | """ |
---|
599 | menu = event.GetEventObject() |
---|
600 | ## post help message for the selected model |
---|
601 | msg = menu.GetHelpString(event.GetId()) |
---|
602 | msg +=" reloaded" |
---|
603 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
604 | |
---|
605 | name = menu.GetLabel(event.GetId()) |
---|
606 | self._on_select_model_helper() |
---|
607 | |
---|
608 | if name in self.saved_states.keys(): |
---|
609 | previous_state = self.saved_states[name] |
---|
610 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
611 | self.reset_page(previous_state) |
---|
612 | |
---|
613 | def on_save(self, event): |
---|
614 | """ |
---|
615 | Save the current state into file |
---|
616 | """ |
---|
617 | self.save_current_state() |
---|
618 | new_state = self.state.clone() |
---|
619 | # Ask the user the location of the file to write to. |
---|
620 | path = None |
---|
621 | dlg = wx.FileDialog(self, "Choose a file", self._default_save_location, |
---|
622 | "", "*.fitv", wx.SAVE) |
---|
623 | if dlg.ShowModal() == wx.ID_OK: |
---|
624 | path = dlg.GetPath() |
---|
625 | self._default_save_location = os.path.dirname(path) |
---|
626 | else: |
---|
627 | return None |
---|
628 | #the manager write the state into file |
---|
629 | self._manager.save_fit_state(filepath=path, fitstate=new_state) |
---|
630 | return new_state |
---|
631 | |
---|
632 | def _get_time_stamp(self): |
---|
633 | """ |
---|
634 | return time and date stings |
---|
635 | """ |
---|
636 | # date and time |
---|
637 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
638 | current_time= str(hour)+":"+str(minute)+":"+str(second) |
---|
639 | current_date= str( month)+"/"+str(day)+"/"+str(year) |
---|
640 | return current_time, current_date |
---|
641 | |
---|
642 | def on_bookmark(self, event): |
---|
643 | """ |
---|
644 | save history of the data and model |
---|
645 | """ |
---|
646 | if self.model==None: |
---|
647 | msg="Can not bookmark; Please select Data and Model first..." |
---|
648 | wx.MessageBox(msg, 'Info') |
---|
649 | return |
---|
650 | self.save_current_state() |
---|
651 | new_state = self.state.clone() |
---|
652 | ##Add model state on context menu |
---|
653 | self.number_saved_state += 1 |
---|
654 | current_time, current_date = self._get_time_stamp() |
---|
655 | #name= self.model.name+"[%g]"%self.number_saved_state |
---|
656 | name = "Fitting: %g]" % self.number_saved_state |
---|
657 | name += self.model.__class__.__name__ |
---|
658 | name += "bookmarked at %s on %s" % (current_time, current_date) |
---|
659 | self.saved_states[name]= new_state |
---|
660 | |
---|
661 | ## Add item in the context menu |
---|
662 | msg = "Model saved at %s on %s"%(current_time, current_date) |
---|
663 | ## post help message for the selected model |
---|
664 | msg +=" Saved! right click on this page to retrieve this model" |
---|
665 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
666 | |
---|
667 | #id = wx.NewId() |
---|
668 | #self.popUpMenu.Append(id,name,str(msg)) |
---|
669 | #wx.EVT_MENU(self, id, self.onResetModel) |
---|
670 | wx.PostEvent(self.parent.parent, |
---|
671 | AppendBookmarkEvent(title=name, |
---|
672 | hint=str(msg), handler=self.onResetModel)) |
---|
673 | |
---|
674 | def old_on_bookmark(self, event): |
---|
675 | """ |
---|
676 | save history of the data and model |
---|
677 | """ |
---|
678 | if self.model==None: |
---|
679 | msg="Can not bookmark; Please select Data and Model first..." |
---|
680 | wx.MessageBox(msg, 'Info') |
---|
681 | return |
---|
682 | if hasattr(self,"enable_disp"): |
---|
683 | self.state.enable_disp = copy.deepcopy(self.enable_disp.GetValue()) |
---|
684 | if hasattr(self, "disp_box"): |
---|
685 | self.state.disp_box = copy.deepcopy(self.disp_box.GetSelection()) |
---|
686 | |
---|
687 | self.state.model.name= self.model.name |
---|
688 | |
---|
689 | #Remember fit engine_type for fit panel |
---|
690 | if self.engine_type == None: |
---|
691 | self.engine_type = "scipy" |
---|
692 | if self._manager !=None: |
---|
693 | self._manager._on_change_engine(engine=self.engine_type) |
---|
694 | |
---|
695 | self.state.engine_type = self.engine_type |
---|
696 | |
---|
697 | new_state = self.state.clone() |
---|
698 | new_state.model.name = self.state.model.name |
---|
699 | |
---|
700 | new_state.enable2D = copy.deepcopy(self.enable2D) |
---|
701 | ##Add model state on context menu |
---|
702 | self.number_saved_state += 1 |
---|
703 | #name= self.model.name+"[%g]"%self.number_saved_state |
---|
704 | name= self.model.__class__.__name__+"[%g]"%self.number_saved_state |
---|
705 | self.saved_states[name]= new_state |
---|
706 | |
---|
707 | ## Add item in the context menu |
---|
708 | |
---|
709 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
710 | my_time= str(hour)+" : "+str(minute)+" : "+str(second)+" " |
---|
711 | date= str( month)+"|"+str(day)+"|"+str(year) |
---|
712 | msg= "Model saved at %s on %s"%(my_time, date) |
---|
713 | ## post help message for the selected model |
---|
714 | msg +=" Saved! right click on this page to retrieve this model" |
---|
715 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
716 | |
---|
717 | id = wx.NewId() |
---|
718 | self.popUpMenu.Append(id,name,str(msg)) |
---|
719 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
720 | |
---|
721 | def onSetFocus(self, evt): |
---|
722 | """ |
---|
723 | highlight the current textcrtl and hide the error text control shown |
---|
724 | after fitting |
---|
725 | """ |
---|
726 | return |
---|
727 | |
---|
728 | def read_file(self, path): |
---|
729 | """ |
---|
730 | Read two columns file |
---|
731 | |
---|
732 | :param path: the path to the file to read |
---|
733 | |
---|
734 | """ |
---|
735 | try: |
---|
736 | if path==None: |
---|
737 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
738 | " Selected Distribution was not loaded: %s"%path)) |
---|
739 | return None, None |
---|
740 | input_f = open(path, 'r') |
---|
741 | buff = input_f.read() |
---|
742 | lines = buff.split('\n') |
---|
743 | |
---|
744 | angles = [] |
---|
745 | weights=[] |
---|
746 | for line in lines: |
---|
747 | toks = line.split() |
---|
748 | try: |
---|
749 | angle = float(toks[0]) |
---|
750 | weight = float(toks[1]) |
---|
751 | except: |
---|
752 | # Skip non-data lines |
---|
753 | pass |
---|
754 | angles.append(angle) |
---|
755 | weights.append(weight) |
---|
756 | return numpy.array(angles), numpy.array(weights) |
---|
757 | except: |
---|
758 | raise |
---|
759 | |
---|
760 | def createMemento(self): |
---|
761 | """ |
---|
762 | return the current state of the page |
---|
763 | """ |
---|
764 | return self.state.clone() |
---|
765 | |
---|
766 | |
---|
767 | def save_current_state(self): |
---|
768 | """ |
---|
769 | Store current state |
---|
770 | """ |
---|
771 | self.state.engine_type = copy.deepcopy(self.engine_type) |
---|
772 | ## save model option |
---|
773 | if self.model!= None: |
---|
774 | self.disp_list= self.model.getDispParamList() |
---|
775 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
776 | self.state.model = self.model.clone() |
---|
777 | #save radiobutton state for model selection |
---|
778 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
779 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
780 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
781 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
782 | #model combobox |
---|
783 | self.state.structurebox = self.structurebox.GetSelection() |
---|
784 | self.state.formfactorbox = self.formfactorbox.GetSelection() |
---|
785 | |
---|
786 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
787 | self.state.values= copy.deepcopy(self.values) |
---|
788 | self.state.weights = copy.deepcopy( self.weights) |
---|
789 | ## save data |
---|
790 | self.state.data= copy.deepcopy(self.data) |
---|
791 | self.state.qmax_x = self.qmax_x |
---|
792 | self.state.qmin_x = self.qmin_x |
---|
793 | try: |
---|
794 | n = self.disp_box.GetCurrentSelection() |
---|
795 | dispersity= self.disp_box.GetClientData(n) |
---|
796 | name= dispersity.__name__ |
---|
797 | self.disp_name = name |
---|
798 | if name == "GaussianDispersion" : |
---|
799 | if hasattr(self,"cb1"): |
---|
800 | self.state.cb1= self.cb1.GetValue() |
---|
801 | except: |
---|
802 | pass |
---|
803 | |
---|
804 | if hasattr(self,"enable_disp"): |
---|
805 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
806 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
807 | |
---|
808 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
809 | if hasattr(self,"enable_smearer"): |
---|
810 | self.state.enable_smearer = \ |
---|
811 | copy.deepcopy(self.enable_smearer.GetValue()) |
---|
812 | self.state.disable_smearer = \ |
---|
813 | copy.deepcopy(self.disable_smearer.GetValue()) |
---|
814 | |
---|
815 | self.state.pinhole_smearer = \ |
---|
816 | copy.deepcopy(self.pinhole_smearer.GetValue()) |
---|
817 | self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) |
---|
818 | |
---|
819 | if hasattr(self,"disp_box"): |
---|
820 | self.state.disp_box = self.disp_box.GetSelection() |
---|
821 | |
---|
822 | if len(self.disp_cb_dict)>0: |
---|
823 | for k , v in self.disp_cb_dict.iteritems(): |
---|
824 | |
---|
825 | if v ==None : |
---|
826 | self.state.disp_cb_dict[k]= v |
---|
827 | else: |
---|
828 | try: |
---|
829 | self.state.disp_cb_dict[k]=v.GetValue() |
---|
830 | except: |
---|
831 | self.state.disp_cb_dict[k]= None |
---|
832 | |
---|
833 | if len(self._disp_obj_dict)>0: |
---|
834 | for k , v in self._disp_obj_dict.iteritems(): |
---|
835 | |
---|
836 | self.state._disp_obj_dict[k]= v |
---|
837 | |
---|
838 | |
---|
839 | self.state.values = copy.deepcopy(self.values) |
---|
840 | self.state.weights = copy.deepcopy(self.weights) |
---|
841 | ## save plotting range |
---|
842 | self._save_plotting_range() |
---|
843 | |
---|
844 | self.state.orientation_params =[] |
---|
845 | self.state.orientation_params_disp =[] |
---|
846 | self.state.parameters =[] |
---|
847 | self.state.fittable_param =[] |
---|
848 | self.state.fixed_param =[] |
---|
849 | |
---|
850 | |
---|
851 | ## save checkbutton state and txtcrtl values |
---|
852 | self._copy_parameters_state(self.orientation_params, |
---|
853 | self.state.orientation_params) |
---|
854 | self._copy_parameters_state(self.orientation_params_disp, |
---|
855 | self.state.orientation_params_disp) |
---|
856 | |
---|
857 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
858 | self._copy_parameters_state(self.fittable_param, |
---|
859 | self.state.fittable_param) |
---|
860 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
861 | #save chisqr |
---|
862 | self.state.tcChi = self.tcChi.GetValue() |
---|
863 | |
---|
864 | def save_current_state_fit(self): |
---|
865 | """ |
---|
866 | Store current state for fit_page |
---|
867 | """ |
---|
868 | ## save model option |
---|
869 | if self.model!= None: |
---|
870 | self.disp_list= self.model.getDispParamList() |
---|
871 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
872 | self.state.model = self.model.clone() |
---|
873 | if hasattr(self, "engine_type"): |
---|
874 | self.state.engine_type = copy.deepcopy(self.engine_type) |
---|
875 | |
---|
876 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
877 | self.state.values= copy.deepcopy(self.values) |
---|
878 | self.state.weights = copy.deepcopy( self.weights) |
---|
879 | ## save data |
---|
880 | self.state.data= copy.deepcopy(self.data) |
---|
881 | try: |
---|
882 | n = self.disp_box.GetCurrentSelection() |
---|
883 | dispersity= self.disp_box.GetClientData(n) |
---|
884 | name= dispersity.__name__ |
---|
885 | self.disp_name = name |
---|
886 | if name == "GaussianDispersion" : |
---|
887 | if hasattr(self,"cb1"): |
---|
888 | self.state.cb1= self.cb1.GetValue() |
---|
889 | |
---|
890 | except: |
---|
891 | pass |
---|
892 | |
---|
893 | if hasattr(self,"enable_disp"): |
---|
894 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
895 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
896 | |
---|
897 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
898 | if hasattr(self,"enable_smearer"): |
---|
899 | self.state.enable_smearer = \ |
---|
900 | copy.deepcopy(self.enable_smearer.GetValue()) |
---|
901 | self.state.disable_smearer = \ |
---|
902 | copy.deepcopy(self.disable_smearer.GetValue()) |
---|
903 | |
---|
904 | self.state.pinhole_smearer = \ |
---|
905 | copy.deepcopy(self.pinhole_smearer.GetValue()) |
---|
906 | self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) |
---|
907 | |
---|
908 | if hasattr(self,"disp_box"): |
---|
909 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
910 | |
---|
911 | if len(self.disp_cb_dict) > 0: |
---|
912 | for k, v in self.disp_cb_dict.iteritems(): |
---|
913 | |
---|
914 | if v == None : |
---|
915 | self.state.disp_cb_dict[k] = v |
---|
916 | else: |
---|
917 | try: |
---|
918 | self.state.disp_cb_dict[k] = v.GetValue() |
---|
919 | except: |
---|
920 | self.state.disp_cb_dict[k] = None |
---|
921 | |
---|
922 | if len(self._disp_obj_dict) > 0: |
---|
923 | for k , v in self._disp_obj_dict.iteritems(): |
---|
924 | |
---|
925 | self.state._disp_obj_dict[k] = v |
---|
926 | |
---|
927 | |
---|
928 | self.state.values = copy.deepcopy(self.values) |
---|
929 | self.state.weights = copy.deepcopy(self.weights) |
---|
930 | |
---|
931 | ## save plotting range |
---|
932 | self._save_plotting_range() |
---|
933 | |
---|
934 | ## save checkbutton state and txtcrtl values |
---|
935 | self._copy_parameters_state(self.orientation_params, |
---|
936 | self.state.orientation_params) |
---|
937 | self._copy_parameters_state(self.orientation_params_disp, |
---|
938 | self.state.orientation_params_disp) |
---|
939 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
940 | self._copy_parameters_state(self.fittable_param, |
---|
941 | self.state.fittable_param) |
---|
942 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
943 | |
---|
944 | |
---|
945 | def check_invalid_panel(self): |
---|
946 | """ |
---|
947 | check if the user can already perform some action with this panel |
---|
948 | """ |
---|
949 | flag = False |
---|
950 | if self.data is None: |
---|
951 | self.disable_smearer.SetValue(True) |
---|
952 | self.disable_disp.SetValue(True) |
---|
953 | msg = "Please load Data and select Model to start..." |
---|
954 | wx.MessageBox(msg, 'Info') |
---|
955 | return True |
---|
956 | |
---|
957 | |
---|
958 | def reset_page_helper(self, state): |
---|
959 | """ |
---|
960 | Use page_state and change the state of existing page |
---|
961 | |
---|
962 | :precondition: the page is already drawn or created |
---|
963 | |
---|
964 | :postcondition: the state of the underlying data change as well as the |
---|
965 | state of the graphic interface |
---|
966 | """ |
---|
967 | if state == None: |
---|
968 | #self._undo.Enable(False) |
---|
969 | return |
---|
970 | |
---|
971 | self.set_data(state.data) |
---|
972 | self.enable2D= state.enable2D |
---|
973 | self.engine_type = state.engine_type |
---|
974 | |
---|
975 | self.disp_cb_dict = state.disp_cb_dict |
---|
976 | self.disp_list = state.disp_list |
---|
977 | |
---|
978 | ## set the state of the radio box |
---|
979 | self.shape_rbutton.SetValue(state.shape_rbutton ) |
---|
980 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
981 | self.struct_rbutton.SetValue(state.struct_rbutton) |
---|
982 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
983 | |
---|
984 | ## fill model combobox |
---|
985 | self._show_combox_helper() |
---|
986 | #select the current model |
---|
987 | self.formfactorbox.Select(int(state.formfactorcombobox)) |
---|
988 | self.structurebox.SetSelection(state.structurecombobox ) |
---|
989 | if state.multi_factor != None: |
---|
990 | self.multifactorbox.SetSelection(state.multi_factor) |
---|
991 | |
---|
992 | #reset the fitting engine type |
---|
993 | self.engine_type = state.engine_type |
---|
994 | #draw the pnael according to the new model parameter |
---|
995 | self._on_select_model(event=None) |
---|
996 | |
---|
997 | if self._manager !=None: |
---|
998 | self._manager._on_change_engine(engine=self.engine_type) |
---|
999 | ## set the select all check box to the a given state |
---|
1000 | self.cb1.SetValue(state.cb1) |
---|
1001 | |
---|
1002 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
1003 | self._reset_parameters_state(self.orientation_params_disp, |
---|
1004 | state.orientation_params_disp) |
---|
1005 | self._reset_parameters_state(self.orientation_params, |
---|
1006 | state.orientation_params) |
---|
1007 | self._reset_parameters_state(self.str_parameters, |
---|
1008 | state.str_parameters) |
---|
1009 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
1010 | ## display dispersion info layer |
---|
1011 | self.enable_disp.SetValue(state.enable_disp) |
---|
1012 | self.disable_disp.SetValue(state.disable_disp) |
---|
1013 | |
---|
1014 | if hasattr(self, "disp_box"): |
---|
1015 | |
---|
1016 | self.disp_box.SetSelection(state.disp_box) |
---|
1017 | n= self.disp_box.GetCurrentSelection() |
---|
1018 | dispersity= self.disp_box.GetClientData(n) |
---|
1019 | name = dispersity.__name__ |
---|
1020 | |
---|
1021 | self._set_dipers_Param(event=None) |
---|
1022 | |
---|
1023 | if name == "ArrayDispersion": |
---|
1024 | |
---|
1025 | for item in self.disp_cb_dict.keys(): |
---|
1026 | |
---|
1027 | if hasattr(self.disp_cb_dict[item], "SetValue") : |
---|
1028 | self.disp_cb_dict[item].SetValue(\ |
---|
1029 | state.disp_cb_dict[item]) |
---|
1030 | # Create the dispersion objects |
---|
1031 | from sans.models.dispersion_models import ArrayDispersion |
---|
1032 | disp_model = ArrayDispersion() |
---|
1033 | if hasattr(state,"values")and\ |
---|
1034 | self.disp_cb_dict[item].GetValue() == True: |
---|
1035 | if len(state.values)>0: |
---|
1036 | self.values=state.values |
---|
1037 | self.weights=state.weights |
---|
1038 | disp_model.set_weights(self.values, |
---|
1039 | state.weights) |
---|
1040 | else: |
---|
1041 | self._reset_dispersity() |
---|
1042 | |
---|
1043 | self._disp_obj_dict[item] = disp_model |
---|
1044 | # Set the new model as the dispersion object |
---|
1045 | #for the selected parameter |
---|
1046 | self.model.set_dispersion(item, disp_model) |
---|
1047 | |
---|
1048 | self.model._persistency_dict[item] = \ |
---|
1049 | [state.values, state.weights] |
---|
1050 | |
---|
1051 | else: |
---|
1052 | keys = self.model.getParamList() |
---|
1053 | for item in keys: |
---|
1054 | if item in self.disp_list and \ |
---|
1055 | not self.model.details.has_key(item): |
---|
1056 | self.model.details[item] = ["", None, None] |
---|
1057 | for k,v in self.state.disp_cb_dict.iteritems(): |
---|
1058 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
1059 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
1060 | |
---|
1061 | ##plotting range restore |
---|
1062 | self._reset_plotting_range(state) |
---|
1063 | ## smearing info restore |
---|
1064 | if hasattr(self, "enable_smearer"): |
---|
1065 | ## set smearing value whether or not the data |
---|
1066 | #contain the smearing info |
---|
1067 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
1068 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
1069 | self.onSmear(event=None) |
---|
1070 | self.pinhole_smearer.SetValue(state.pinhole_smearer) |
---|
1071 | self.slit_smearer.SetValue(state.slit_smearer) |
---|
1072 | ## we have two more options for smearing |
---|
1073 | if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None) |
---|
1074 | elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None) |
---|
1075 | |
---|
1076 | ## reset state of checkbox,textcrtl and dispersity parameters value |
---|
1077 | self._reset_parameters_state(self.fittable_param,state.fittable_param) |
---|
1078 | self._reset_parameters_state(self.fixed_param,state.fixed_param) |
---|
1079 | |
---|
1080 | ## draw the model with previous parameters value |
---|
1081 | self._onparamEnter_helper() |
---|
1082 | #reset the value of chisqr when not consistent with the value computed |
---|
1083 | self.tcChi.SetValue(str(self.state.tcChi)) |
---|
1084 | ## reset context menu items |
---|
1085 | self._reset_context_menu() |
---|
1086 | |
---|
1087 | ## set the value of the current state to the state given as parameter |
---|
1088 | self.state = state.clone() |
---|
1089 | |
---|
1090 | |
---|
1091 | def old_reset_page_helper(self, state): |
---|
1092 | """ |
---|
1093 | Use page_state and change the state of existing page |
---|
1094 | |
---|
1095 | :precondition: the page is already drawn or created |
---|
1096 | |
---|
1097 | :postcondition: the state of the underlying data change as well as the |
---|
1098 | state of the graphic interface |
---|
1099 | """ |
---|
1100 | if state ==None: |
---|
1101 | #self._undo.Enable(False) |
---|
1102 | return |
---|
1103 | |
---|
1104 | self.model= state.model |
---|
1105 | self.data = state.data |
---|
1106 | if self.data !=None: |
---|
1107 | from DataLoader.qsmearing import smear_selection |
---|
1108 | self.smearer= smear_selection(self.data, self.model) |
---|
1109 | self.enable2D= state.enable2D |
---|
1110 | self.engine_type = state.engine_type |
---|
1111 | |
---|
1112 | self.disp_cb_dict = state.disp_cb_dict |
---|
1113 | self.disp_list = state.disp_list |
---|
1114 | |
---|
1115 | ## set the state of the radio box |
---|
1116 | self.shape_rbutton.SetValue(state.shape_rbutton ) |
---|
1117 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
1118 | self.struct_rbutton.SetValue(state.struct_rbutton ) |
---|
1119 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
1120 | ##draw sizer containing model parameters value for the current model |
---|
1121 | self._set_model_sizer_selection( self.model ) |
---|
1122 | self.set_model_param_sizer(self.model) |
---|
1123 | |
---|
1124 | ## reset value of combox box |
---|
1125 | self.structurebox.SetSelection(state.structurecombobox ) |
---|
1126 | self.formfactorbox.SetSelection(state.formfactorcombobox) |
---|
1127 | |
---|
1128 | |
---|
1129 | ## enable the view 2d button if this is a modelpage type |
---|
1130 | if hasattr(self,"model_view"): |
---|
1131 | if self.enable2D: |
---|
1132 | self.model_view.Disable() |
---|
1133 | else: |
---|
1134 | self.model_view.Enable() |
---|
1135 | ## set the select all check box to the a given state |
---|
1136 | if hasattr(self, "cb1"): |
---|
1137 | self.cb1.SetValue(state.cb1) |
---|
1138 | |
---|
1139 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
1140 | |
---|
1141 | self._reset_parameters_state(self.orientation_params_disp, |
---|
1142 | state.orientation_params_disp) |
---|
1143 | self._reset_parameters_state(self.orientation_params, |
---|
1144 | state.orientation_params) |
---|
1145 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
1146 | ## display dispersion info layer |
---|
1147 | self.enable_disp.SetValue(state.enable_disp) |
---|
1148 | self.disable_disp.SetValue(state.disable_disp) |
---|
1149 | |
---|
1150 | if hasattr(self, "disp_box"): |
---|
1151 | |
---|
1152 | self.disp_box.SetSelection(state.disp_box) |
---|
1153 | n= self.disp_box.GetCurrentSelection() |
---|
1154 | dispersity= self.disp_box.GetClientData(n) |
---|
1155 | name= dispersity.__name__ |
---|
1156 | |
---|
1157 | self._set_dipers_Param(event=None) |
---|
1158 | |
---|
1159 | if name=="ArrayDispersion": |
---|
1160 | |
---|
1161 | for item in self.disp_cb_dict.keys(): |
---|
1162 | |
---|
1163 | if hasattr(self.disp_cb_dict[item],"SetValue") : |
---|
1164 | self.disp_cb_dict[item].SetValue(state.disp_cb_dict[item]) |
---|
1165 | # Create the dispersion objects |
---|
1166 | from sans.models.dispersion_models import ArrayDispersion |
---|
1167 | disp_model = ArrayDispersion() |
---|
1168 | if hasattr(state,"values") and\ |
---|
1169 | self.disp_cb_dict[item].GetValue()==True: |
---|
1170 | if len(state.values) > 0: |
---|
1171 | self.values = state.values |
---|
1172 | self.weights = state.weights |
---|
1173 | disp_model.set_weights(self.values, |
---|
1174 | state.weights) |
---|
1175 | else: |
---|
1176 | self._reset_dispersity() |
---|
1177 | |
---|
1178 | self._disp_obj_dict[item] = disp_model |
---|
1179 | # Set the new model as the dispersion |
---|
1180 | #object for the selected parameter |
---|
1181 | self.model.set_dispersion(item, disp_model) |
---|
1182 | |
---|
1183 | self.model._persistency_dict[item] = [state.values, |
---|
1184 | state.weights] |
---|
1185 | |
---|
1186 | else: |
---|
1187 | keys = self.model.getParamList() |
---|
1188 | for item in keys: |
---|
1189 | if item in self.disp_list and \ |
---|
1190 | not self.model.details.has_key(item): |
---|
1191 | self.model.details[item]=["",None,None] |
---|
1192 | for k,v in self.state.disp_cb_dict.iteritems(): |
---|
1193 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
1194 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
1195 | |
---|
1196 | ##plotting range restore |
---|
1197 | self._reset_plotting_range(state) |
---|
1198 | |
---|
1199 | ## smearing info restore |
---|
1200 | if hasattr(self,"enable_smearer"): |
---|
1201 | ## set smearing value whether or not the data |
---|
1202 | #contain the smearing info |
---|
1203 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
1204 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
1205 | self.onSmear(event=None) |
---|
1206 | self.pinhole_smearer.SetValue(state.pinhole_smearer) |
---|
1207 | self.slit_smearer.SetValue(state.slit_smearer) |
---|
1208 | ## we have two more options for smearing |
---|
1209 | if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None) |
---|
1210 | elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None) |
---|
1211 | |
---|
1212 | ## reset state of checkbox,textcrtl and dispersity parameters value |
---|
1213 | self._reset_parameters_state(self.fittable_param,state.fittable_param) |
---|
1214 | self._reset_parameters_state(self.fixed_param,state.fixed_param) |
---|
1215 | |
---|
1216 | ## draw the model with previous parameters value |
---|
1217 | self._onparamEnter_helper() |
---|
1218 | |
---|
1219 | ## reset context menu items |
---|
1220 | self._reset_context_menu() |
---|
1221 | |
---|
1222 | ## set the value of the current state to the state given as parameter |
---|
1223 | self.state = state.clone() |
---|
1224 | self._draw_model() |
---|
1225 | |
---|
1226 | def _selectDlg(self): |
---|
1227 | """ |
---|
1228 | open a dialog file to selected the customized dispersity |
---|
1229 | """ |
---|
1230 | import os |
---|
1231 | dlg = wx.FileDialog(self, "Choose a weight file", |
---|
1232 | self._default_save_location , "", |
---|
1233 | "*.*", wx.OPEN) |
---|
1234 | path = None |
---|
1235 | if dlg.ShowModal() == wx.ID_OK: |
---|
1236 | path = dlg.GetPath() |
---|
1237 | dlg.Destroy() |
---|
1238 | return path |
---|
1239 | |
---|
1240 | def _reset_context_menu(self): |
---|
1241 | """ |
---|
1242 | reset the context menu |
---|
1243 | """ |
---|
1244 | for name, state in self.state.saved_states.iteritems(): |
---|
1245 | self.number_saved_state += 1 |
---|
1246 | ## Add item in the context menu |
---|
1247 | id = wx.NewId() |
---|
1248 | msg = 'Save model and state %g' % self.number_saved_state |
---|
1249 | self.popUpMenu.Append(id, name, msg) |
---|
1250 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
1251 | |
---|
1252 | def _reset_plotting_range(self, state): |
---|
1253 | """ |
---|
1254 | Reset the plotting range to a given state |
---|
1255 | """ |
---|
1256 | if self.check_invalid_panel(): |
---|
1257 | return |
---|
1258 | self.qmin.SetValue(str(state.qmin)) |
---|
1259 | self.qmax.SetValue(str(state.qmax)) |
---|
1260 | |
---|
1261 | def _save_typeOfmodel(self): |
---|
1262 | """ |
---|
1263 | save radiobutton containing the type model that can be selected |
---|
1264 | """ |
---|
1265 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
1266 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
1267 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
1268 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
1269 | self.state.structurebox= self.structurebox.GetCurrentSelection() |
---|
1270 | self.state.formfactorbox = self.formfactorbox.GetCurrentSelection() |
---|
1271 | |
---|
1272 | #self._undo.Enable(True) |
---|
1273 | ## post state to fit panel |
---|
1274 | event = PageInfoEvent(page = self) |
---|
1275 | wx.PostEvent(self.parent, event) |
---|
1276 | |
---|
1277 | def _save_plotting_range(self ): |
---|
1278 | """ |
---|
1279 | save the state of plotting range |
---|
1280 | """ |
---|
1281 | self.state.qmin = self.qmin_x |
---|
1282 | self.state.qmax = self.qmax_x |
---|
1283 | if self.npts != None: |
---|
1284 | self.state.npts = self.num_points |
---|
1285 | |
---|
1286 | def _onparamEnter_helper(self): |
---|
1287 | """ |
---|
1288 | check if values entered by the user are changed and valid to replot |
---|
1289 | model |
---|
1290 | """ |
---|
1291 | # Flag to register when a parameter has changed. |
---|
1292 | is_modified = False |
---|
1293 | self.fitrange = True |
---|
1294 | is_2Ddata = False |
---|
1295 | #self._undo.Enable(True) |
---|
1296 | # check if 2d data |
---|
1297 | if self.data.__class__.__name__ == "Data2D": |
---|
1298 | is_2Ddata = True |
---|
1299 | if self.model !=None: |
---|
1300 | try: |
---|
1301 | is_modified = self._check_value_enter(self.fittable_param, |
---|
1302 | is_modified) |
---|
1303 | is_modified = self._check_value_enter(self.fixed_param, |
---|
1304 | is_modified) |
---|
1305 | is_modified = self._check_value_enter(self.parameters, |
---|
1306 | is_modified) |
---|
1307 | except: |
---|
1308 | pass |
---|
1309 | #if is_modified: |
---|
1310 | |
---|
1311 | # Here we should check whether the boundaries have been modified. |
---|
1312 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
1313 | # set the is_modified flag to True |
---|
1314 | if self._validate_qrange(self.qmin, self.qmax): |
---|
1315 | tempmin = float(self.qmin.GetValue()) |
---|
1316 | if tempmin != self.qmin_x: |
---|
1317 | self.qmin_x = tempmin |
---|
1318 | is_modified = True |
---|
1319 | tempmax = float(self.qmax.GetValue()) |
---|
1320 | if tempmax != self.qmax_x: |
---|
1321 | self.qmax_x = tempmax |
---|
1322 | is_modified = True |
---|
1323 | |
---|
1324 | if is_2Ddata: |
---|
1325 | # set mask |
---|
1326 | is_modified = self._validate_Npts() |
---|
1327 | |
---|
1328 | else: |
---|
1329 | self.fitrange = False |
---|
1330 | |
---|
1331 | ## if any value is modify draw model with new value |
---|
1332 | if not self.fitrange: |
---|
1333 | #self.btFit.Disable() |
---|
1334 | if is_2Ddata: self.btEditMask.Disable() |
---|
1335 | else: |
---|
1336 | #self.btFit.Enable(True) |
---|
1337 | if is_2Ddata: self.btEditMask.Enable(True) |
---|
1338 | |
---|
1339 | if is_modified and self.fitrange: |
---|
1340 | self.state_change= True |
---|
1341 | self._draw_model() |
---|
1342 | self.Refresh() |
---|
1343 | return is_modified |
---|
1344 | |
---|
1345 | def _update_paramv_on_fit(self): |
---|
1346 | """ |
---|
1347 | make sure that update param values just before the fitting |
---|
1348 | """ |
---|
1349 | #flag for qmin qmax check values |
---|
1350 | flag = True |
---|
1351 | self.fitrange = True |
---|
1352 | is_modified = False |
---|
1353 | |
---|
1354 | wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
1355 | updating ... ",type="update")) |
---|
1356 | |
---|
1357 | ##So make sure that update param values on_Fit. |
---|
1358 | #self._undo.Enable(True) |
---|
1359 | if self.model !=None: |
---|
1360 | ##Check the values |
---|
1361 | self._check_value_enter( self.fittable_param ,is_modified) |
---|
1362 | self._check_value_enter( self.fixed_param ,is_modified) |
---|
1363 | self._check_value_enter( self.parameters ,is_modified) |
---|
1364 | |
---|
1365 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
1366 | # Here we should check whether the boundaries have been modified. |
---|
1367 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
1368 | # set the is_modified flag to True |
---|
1369 | self.fitrange = self._validate_qrange(self.qmin, self.qmax) |
---|
1370 | if self.fitrange: |
---|
1371 | tempmin = float(self.qmin.GetValue()) |
---|
1372 | if tempmin != self.qmin_x: |
---|
1373 | self.qmin_x = tempmin |
---|
1374 | tempmax = float(self.qmax.GetValue()) |
---|
1375 | if tempmax != self.qmax_x: |
---|
1376 | self.qmax_x = tempmax |
---|
1377 | if tempmax == tempmin: |
---|
1378 | flag = False |
---|
1379 | temp_smearer = None |
---|
1380 | if not self.disable_smearer.GetValue(): |
---|
1381 | temp_smearer= self.current_smearer |
---|
1382 | if self.slit_smearer.GetValue(): |
---|
1383 | flag = self.update_slit_smear() |
---|
1384 | elif self.pinhole_smearer.GetValue(): |
---|
1385 | flag = self.update_pinhole_smear() |
---|
1386 | else: |
---|
1387 | self._manager.set_smearer_nodraw(smearer=temp_smearer, |
---|
1388 | qmin=float(self.qmin_x), |
---|
1389 | qmax=float(self.qmax_x)) |
---|
1390 | elif not self._is_2D(): |
---|
1391 | self._manager.set_smearer(smearer=temp_smearer, |
---|
1392 | qmin=float(self.qmin_x), |
---|
1393 | qmax= float(self.qmax_x)) |
---|
1394 | index_data = ((self.qmin_x <= self.data.x)&\ |
---|
1395 | (self.data.x <= self.qmax_x)) |
---|
1396 | val = str(len(self.data.x[index_data==True])) |
---|
1397 | self.Npts_fit.SetValue(val) |
---|
1398 | flag = True |
---|
1399 | if self._is_2D(): |
---|
1400 | # only 2D case set mask |
---|
1401 | flag = self._validate_Npts() |
---|
1402 | if not flag: |
---|
1403 | return flag |
---|
1404 | else: flag = False |
---|
1405 | else: |
---|
1406 | flag = False |
---|
1407 | |
---|
1408 | #For invalid q range, disable the mask editor and fit button, vs. |
---|
1409 | if not self.fitrange: |
---|
1410 | #self.btFit.Disable() |
---|
1411 | if self._is_2D():self.btEditMask.Disable() |
---|
1412 | else: |
---|
1413 | #self.btFit.Enable(True) |
---|
1414 | if self._is_2D():self.btEditMask.Enable(True) |
---|
1415 | |
---|
1416 | if not flag: |
---|
1417 | msg = "Cannot Plot or Fit :Must select a " |
---|
1418 | msg += " model or Fitting range is not valid!!! " |
---|
1419 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
1420 | |
---|
1421 | self.save_current_state() |
---|
1422 | |
---|
1423 | return flag |
---|
1424 | |
---|
1425 | def _is_modified(self, is_modified): |
---|
1426 | """ |
---|
1427 | return to self._is_modified |
---|
1428 | """ |
---|
1429 | return is_modified |
---|
1430 | |
---|
1431 | def _reset_parameters_state(self, listtorestore, statelist): |
---|
1432 | """ |
---|
1433 | Reset the parameters at the given state |
---|
1434 | """ |
---|
1435 | if len(statelist) == 0 or len(listtorestore) == 0: |
---|
1436 | return |
---|
1437 | if len(statelist) != len(listtorestore): |
---|
1438 | return |
---|
1439 | |
---|
1440 | for j in range(len(listtorestore)): |
---|
1441 | item_page = listtorestore[j] |
---|
1442 | item_page_info = statelist[j] |
---|
1443 | ##change the state of the check box for simple parameters |
---|
1444 | if item_page[0]!=None: |
---|
1445 | item_page[0].SetValue(item_page_info[0]) |
---|
1446 | if item_page[2]!=None: |
---|
1447 | item_page[2].SetValue(item_page_info[2]) |
---|
1448 | if item_page[3]!=None: |
---|
1449 | ## show or hide text +/- |
---|
1450 | if item_page_info[2]: |
---|
1451 | item_page[3].Show(True) |
---|
1452 | else: |
---|
1453 | item_page[3].Hide() |
---|
1454 | if item_page[4]!=None: |
---|
1455 | ## show of hide the text crtl for fitting error |
---|
1456 | if item_page_info[4][0]: |
---|
1457 | item_page[4].Show(True) |
---|
1458 | item_page[4].SetValue(item_page_info[4][1]) |
---|
1459 | else: |
---|
1460 | item_page[3].Hide() |
---|
1461 | if item_page[5]!=None: |
---|
1462 | ## show of hide the text crtl for fitting error |
---|
1463 | if item_page_info[5][0]: |
---|
1464 | item_page[5].Show(True) |
---|
1465 | item_page[5].SetValue(item_page_info[5][1]) |
---|
1466 | else: |
---|
1467 | item_page[5].Hide() |
---|
1468 | |
---|
1469 | if item_page[6]!=None: |
---|
1470 | ## show of hide the text crtl for fitting error |
---|
1471 | if item_page_info[6][0]: |
---|
1472 | item_page[6].Show(True) |
---|
1473 | item_page[6].SetValue(item_page_info[6][1]) |
---|
1474 | else: |
---|
1475 | item_page[6].Hide() |
---|
1476 | |
---|
1477 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
1478 | """ |
---|
1479 | copy the state of button |
---|
1480 | |
---|
1481 | :param listtocopy: the list of check button to copy |
---|
1482 | :param statelist: list of state object to store the current state |
---|
1483 | |
---|
1484 | """ |
---|
1485 | if len(listtocopy)==0: |
---|
1486 | return |
---|
1487 | |
---|
1488 | for item in listtocopy: |
---|
1489 | |
---|
1490 | checkbox_state = None |
---|
1491 | if item[0]!= None: |
---|
1492 | checkbox_state= item[0].GetValue() |
---|
1493 | parameter_name = item[1] |
---|
1494 | parameter_value = None |
---|
1495 | if item[2]!=None: |
---|
1496 | parameter_value = item[2].GetValue() |
---|
1497 | static_text = None |
---|
1498 | if item[3]!=None: |
---|
1499 | static_text = item[3].IsShown() |
---|
1500 | error_value = None |
---|
1501 | error_state = None |
---|
1502 | if item[4]!= None: |
---|
1503 | error_value = item[4].GetValue() |
---|
1504 | error_state = item[4].IsShown() |
---|
1505 | |
---|
1506 | min_value = None |
---|
1507 | min_state = None |
---|
1508 | if item[5]!= None: |
---|
1509 | min_value = item[5].GetValue() |
---|
1510 | min_state = item[5].IsShown() |
---|
1511 | |
---|
1512 | max_value = None |
---|
1513 | max_state = None |
---|
1514 | if item[6]!= None: |
---|
1515 | max_value = item[6].GetValue() |
---|
1516 | max_state = item[6].IsShown() |
---|
1517 | unit=None |
---|
1518 | if item[7]!=None: |
---|
1519 | unit = item[7].GetLabel() |
---|
1520 | |
---|
1521 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
1522 | static_text ,[error_state, error_value], |
---|
1523 | [min_state, min_value], |
---|
1524 | [max_state, max_value], unit]) |
---|
1525 | |
---|
1526 | def _set_model_sizer_selection(self, model): |
---|
1527 | """ |
---|
1528 | Display the sizer according to the type of the current model |
---|
1529 | """ |
---|
1530 | if model == None: |
---|
1531 | return |
---|
1532 | if hasattr(model ,"s_model"): |
---|
1533 | |
---|
1534 | class_name = model.s_model.__class__ |
---|
1535 | name = model.s_model.name |
---|
1536 | flag = (name != "NoStructure") |
---|
1537 | if flag and \ |
---|
1538 | (class_name in self.model_list_box["Structure Factors"]): |
---|
1539 | self.structurebox.Show() |
---|
1540 | self.text2.Show() |
---|
1541 | self.structurebox.Enable() |
---|
1542 | self.text2.Enable() |
---|
1543 | items = self.structurebox.GetItems() |
---|
1544 | self.sizer1.Layout() |
---|
1545 | |
---|
1546 | for i in range(len(items)): |
---|
1547 | if items[i]== str(name): |
---|
1548 | self.structurebox.SetSelection(i) |
---|
1549 | break |
---|
1550 | |
---|
1551 | if hasattr(model ,"p_model"): |
---|
1552 | class_name = model.p_model.__class__ |
---|
1553 | name = model.p_model.name |
---|
1554 | self.formfactorbox.Clear() |
---|
1555 | |
---|
1556 | for k, list in self.model_list_box.iteritems(): |
---|
1557 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1558 | self.shape_rbutton.SetValue(True) |
---|
1559 | ## fill the form factor list with new model |
---|
1560 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1561 | items = self.formfactorbox.GetItems() |
---|
1562 | ## set comboxbox to the selected item |
---|
1563 | for i in range(len(items)): |
---|
1564 | if items[i]== str(name): |
---|
1565 | self.formfactorbox.SetSelection(i) |
---|
1566 | break |
---|
1567 | return |
---|
1568 | elif k == "Shape-Independent": |
---|
1569 | self.shape_indep_rbutton.SetValue(True) |
---|
1570 | elif k == "Structure Factors": |
---|
1571 | self.struct_rbutton.SetValue(True) |
---|
1572 | elif k == "Multi-Functions": |
---|
1573 | continue |
---|
1574 | else: |
---|
1575 | self.plugin_rbutton.SetValue(True) |
---|
1576 | |
---|
1577 | if class_name in list: |
---|
1578 | ## fill the form factor list with new model |
---|
1579 | self._populate_box(self.formfactorbox, list) |
---|
1580 | items = self.formfactorbox.GetItems() |
---|
1581 | ## set comboxbox to the selected item |
---|
1582 | for i in range(len(items)): |
---|
1583 | if items[i]== str(name): |
---|
1584 | self.formfactorbox.SetSelection(i) |
---|
1585 | break |
---|
1586 | break |
---|
1587 | else: |
---|
1588 | |
---|
1589 | ## Select the model from the menu |
---|
1590 | class_name = model.__class__ |
---|
1591 | name = model.name |
---|
1592 | self.formfactorbox.Clear() |
---|
1593 | items = self.formfactorbox.GetItems() |
---|
1594 | |
---|
1595 | for k, list in self.model_list_box.iteritems(): |
---|
1596 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1597 | if class_name in self.model_list_box["P(Q)*S(Q)"]: |
---|
1598 | self.structurebox.Show() |
---|
1599 | self.text2.Show() |
---|
1600 | self.structurebox.Enable() |
---|
1601 | self.structurebox.SetSelection(0) |
---|
1602 | self.text2.Enable() |
---|
1603 | else: |
---|
1604 | self.structurebox.Hide() |
---|
1605 | self.text2.Hide() |
---|
1606 | self.structurebox.Disable() |
---|
1607 | self.structurebox.SetSelection(0) |
---|
1608 | self.text2.Disable() |
---|
1609 | |
---|
1610 | self.shape_rbutton.SetValue(True) |
---|
1611 | ## fill the form factor list with new model |
---|
1612 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1613 | items = self.formfactorbox.GetItems() |
---|
1614 | ## set comboxbox to the selected item |
---|
1615 | for i in range(len(items)): |
---|
1616 | if items[i]== str(name): |
---|
1617 | self.formfactorbox.SetSelection(i) |
---|
1618 | break |
---|
1619 | return |
---|
1620 | elif k == "Shape-Independent": |
---|
1621 | self.shape_indep_rbutton.SetValue(True) |
---|
1622 | elif k == "Structure Factors": |
---|
1623 | self.struct_rbutton.SetValue(True) |
---|
1624 | elif k == "Multi-Functions": |
---|
1625 | continue |
---|
1626 | else: |
---|
1627 | self.plugin_rbutton.SetValue(True) |
---|
1628 | if class_name in list: |
---|
1629 | self.structurebox.SetSelection(0) |
---|
1630 | self.structurebox.Disable() |
---|
1631 | self.text2.Disable() |
---|
1632 | ## fill the form factor list with new model |
---|
1633 | self._populate_box(self.formfactorbox, list) |
---|
1634 | items = self.formfactorbox.GetItems() |
---|
1635 | ## set comboxbox to the selected item |
---|
1636 | for i in range(len(items)): |
---|
1637 | if items[i]== str(name): |
---|
1638 | self.formfactorbox.SetSelection(i) |
---|
1639 | break |
---|
1640 | break |
---|
1641 | |
---|
1642 | def _draw_model(self): |
---|
1643 | """ |
---|
1644 | Method to draw or refresh a plotted model. |
---|
1645 | The method will use the data member from the model page |
---|
1646 | to build a call to the fitting perspective manager. |
---|
1647 | """ |
---|
1648 | if self.check_invalid_panel(): |
---|
1649 | return |
---|
1650 | if self.model !=None: |
---|
1651 | temp_smear=None |
---|
1652 | if hasattr(self, "enable_smearer"): |
---|
1653 | if not self.disable_smearer.GetValue(): |
---|
1654 | temp_smear= self.current_smearer |
---|
1655 | |
---|
1656 | self._manager.draw_model(self.model, |
---|
1657 | data=self.data, |
---|
1658 | smearer= temp_smear, |
---|
1659 | qmin=float(self.qmin_x), |
---|
1660 | qmax=float(self.qmax_x), |
---|
1661 | qstep= float(self.num_points), |
---|
1662 | enable2D=self.enable2D) |
---|
1663 | |
---|
1664 | def _set_model_sizer(self,sizer, box_sizer, title="", object=None): |
---|
1665 | """ |
---|
1666 | Use lists to fill a sizer for model info |
---|
1667 | """ |
---|
1668 | |
---|
1669 | sizer.Clear(True) |
---|
1670 | ##For MAC, this should defined here. |
---|
1671 | if box_sizer == None: |
---|
1672 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
1673 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1674 | else: |
---|
1675 | boxsizer1 = box_sizer |
---|
1676 | sizer_buttons = wx.BoxSizer(wx.HORIZONTAL) |
---|
1677 | #-------------------------------------------------------- |
---|
1678 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shapes', style=wx.RB_GROUP) |
---|
1679 | self.shape_indep_rbutton = wx.RadioButton(self, -1, "Shape-Independent") |
---|
1680 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure Factor ") |
---|
1681 | self.plugin_rbutton = wx.RadioButton(self, -1, "Customized Models") |
---|
1682 | |
---|
1683 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1684 | id= self.shape_rbutton.GetId() ) |
---|
1685 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1686 | id= self.shape_indep_rbutton.GetId() ) |
---|
1687 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1688 | id= self.struct_rbutton.GetId() ) |
---|
1689 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1690 | id= self.plugin_rbutton.GetId() ) |
---|
1691 | #MAC needs SetValue |
---|
1692 | self.shape_rbutton.SetValue(True) |
---|
1693 | |
---|
1694 | sizer_radiobutton = wx.GridSizer(2, 2,5, 5) |
---|
1695 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
1696 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
1697 | |
---|
1698 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
1699 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
1700 | sizer_buttons.Add(sizer_radiobutton) |
---|
1701 | # detail button |
---|
1702 | if object !=None: |
---|
1703 | sizer_buttons.Add((50,0)) |
---|
1704 | sizer_buttons.Add(object) |
---|
1705 | |
---|
1706 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
1707 | mutifactor_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
1708 | |
---|
1709 | self.text1 = wx.StaticText( self,-1,"" ) |
---|
1710 | self.text2 = wx.StaticText( self,-1,"P(Q)*S(Q)" ) |
---|
1711 | self.mutifactor_text = wx.StaticText( self,-1,"No. of Shells: " ) |
---|
1712 | self.mutifactor_text1 = wx.StaticText( self,-1,"" ) |
---|
1713 | self.show_sld_button = wx.Button( self,-1,"Show SLD Profile" ) |
---|
1714 | self.show_sld_button.Bind(wx.EVT_BUTTON,self._on_show_sld) |
---|
1715 | |
---|
1716 | self.formfactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1717 | if self.model!= None: |
---|
1718 | self.formfactorbox.SetValue(self.model.name) |
---|
1719 | |
---|
1720 | self.structurebox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1721 | self.multifactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1722 | self.initialize_combox() |
---|
1723 | |
---|
1724 | wx.EVT_COMBOBOX(self.formfactorbox,-1, self._on_select_model) |
---|
1725 | wx.EVT_COMBOBOX(self.structurebox,-1, self._on_select_model) |
---|
1726 | wx.EVT_COMBOBOX(self.multifactorbox,-1, self._on_select_model) |
---|
1727 | |
---|
1728 | ## check model type to show sizer |
---|
1729 | if self.model !=None: |
---|
1730 | self._set_model_sizer_selection( self.model ) |
---|
1731 | |
---|
1732 | sizer_selection.Add(self.text1) |
---|
1733 | sizer_selection.Add((5,5)) |
---|
1734 | sizer_selection.Add(self.formfactorbox) |
---|
1735 | sizer_selection.Add((5,5)) |
---|
1736 | sizer_selection.Add(self.text2) |
---|
1737 | sizer_selection.Add((5,5)) |
---|
1738 | sizer_selection.Add(self.structurebox) |
---|
1739 | #sizer_selection.Add((5,5)) |
---|
1740 | mutifactor_selection.Add((10,5)) |
---|
1741 | mutifactor_selection.Add(self.mutifactor_text) |
---|
1742 | mutifactor_selection.Add(self.multifactorbox) |
---|
1743 | mutifactor_selection.Add((5,5)) |
---|
1744 | mutifactor_selection.Add(self.mutifactor_text1) |
---|
1745 | mutifactor_selection.Add((10,5)) |
---|
1746 | mutifactor_selection.Add(self.show_sld_button) |
---|
1747 | |
---|
1748 | boxsizer1.Add( sizer_buttons ) |
---|
1749 | boxsizer1.Add( (15,15)) |
---|
1750 | boxsizer1.Add( sizer_selection ) |
---|
1751 | boxsizer1.Add( (10,10)) |
---|
1752 | boxsizer1.Add(mutifactor_selection) |
---|
1753 | |
---|
1754 | self._set_multfactor_combobox() |
---|
1755 | self.multifactorbox.SetSelection(1) |
---|
1756 | self.show_sld_button.Hide() |
---|
1757 | #-------------------------------------------------------- |
---|
1758 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1759 | sizer.Layout() |
---|
1760 | |
---|
1761 | def _on_show_sld(self, event=None): |
---|
1762 | """ |
---|
1763 | Plot SLD profile |
---|
1764 | """ |
---|
1765 | # get profile data |
---|
1766 | x,y=self.model.getProfile() |
---|
1767 | |
---|
1768 | from danse.common.plottools import Data1D |
---|
1769 | #from sans.perspectives.theory.profile_dialog import SLDPanel |
---|
1770 | from sans.guiframe.local_perspectives.plotting.profile_dialog \ |
---|
1771 | import SLDPanel |
---|
1772 | sld_data = Data1D(x,y) |
---|
1773 | sld_data.name = 'SLD' |
---|
1774 | sld_data.axes = self.sld_axes |
---|
1775 | self.panel = SLDPanel(self, data=sld_data,axes =self.sld_axes,id =-1 ) |
---|
1776 | self.panel.ShowModal() |
---|
1777 | |
---|
1778 | def _set_multfactor_combobox(self, multiplicity=10): |
---|
1779 | """ |
---|
1780 | Set comboBox for muitfactor of CoreMultiShellModel |
---|
1781 | :param multiplicit: no. of multi-functionality |
---|
1782 | """ |
---|
1783 | # build content of the combobox |
---|
1784 | for idx in range(0,multiplicity): |
---|
1785 | self.multifactorbox.Append(str(idx),int(idx)) |
---|
1786 | #self.multifactorbox.SetSelection(1) |
---|
1787 | self._hide_multfactor_combobox() |
---|
1788 | |
---|
1789 | def _show_multfactor_combobox(self): |
---|
1790 | """ |
---|
1791 | Show the comboBox of muitfactor of CoreMultiShellModel |
---|
1792 | """ |
---|
1793 | if not self.mutifactor_text.IsShown(): |
---|
1794 | self.mutifactor_text.Show(True) |
---|
1795 | self.mutifactor_text1.Show(True) |
---|
1796 | if not self.multifactorbox.IsShown(): |
---|
1797 | self.multifactorbox.Show(True) |
---|
1798 | |
---|
1799 | def _hide_multfactor_combobox(self): |
---|
1800 | """ |
---|
1801 | Hide the comboBox of muitfactor of CoreMultiShellModel |
---|
1802 | """ |
---|
1803 | if self.mutifactor_text.IsShown(): |
---|
1804 | self.mutifactor_text.Hide() |
---|
1805 | self.mutifactor_text1.Hide() |
---|
1806 | if self.multifactorbox.IsShown(): |
---|
1807 | self.multifactorbox.Hide() |
---|
1808 | |
---|
1809 | |
---|
1810 | def _show_combox_helper(self): |
---|
1811 | """ |
---|
1812 | Fill panel's combo box according to the type of model selected |
---|
1813 | """ |
---|
1814 | if self.shape_rbutton.GetValue(): |
---|
1815 | ##fill the combobox with form factor list |
---|
1816 | self.structurebox.SetSelection(0) |
---|
1817 | self.structurebox.Disable() |
---|
1818 | self.formfactorbox.Clear() |
---|
1819 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1820 | if self.shape_indep_rbutton.GetValue(): |
---|
1821 | ##fill the combobox with shape independent factor list |
---|
1822 | self.structurebox.SetSelection(0) |
---|
1823 | self.structurebox.Disable() |
---|
1824 | self.formfactorbox.Clear() |
---|
1825 | self._populate_box( self.formfactorbox, |
---|
1826 | self.model_list_box["Shape-Independent"]) |
---|
1827 | if self.struct_rbutton.GetValue(): |
---|
1828 | ##fill the combobox with structure factor list |
---|
1829 | self.structurebox.SetSelection(0) |
---|
1830 | self.structurebox.Disable() |
---|
1831 | self.formfactorbox.Clear() |
---|
1832 | self._populate_box( self.formfactorbox, |
---|
1833 | self.model_list_box["Structure Factors"]) |
---|
1834 | if self.plugin_rbutton.GetValue(): |
---|
1835 | ##fill the combobox with form factor list |
---|
1836 | self.structurebox.Disable() |
---|
1837 | self.formfactorbox.Clear() |
---|
1838 | self._populate_box( self.formfactorbox, |
---|
1839 | self.model_list_box["Customized Models"]) |
---|
1840 | |
---|
1841 | def _show_combox(self, event=None): |
---|
1842 | """ |
---|
1843 | Show combox box associate with type of model selected |
---|
1844 | """ |
---|
1845 | if self.check_invalid_panel(): |
---|
1846 | self.shape_rbutton.SetValue(True) |
---|
1847 | return |
---|
1848 | |
---|
1849 | self._show_combox_helper() |
---|
1850 | self._on_select_model(event=None) |
---|
1851 | self._save_typeOfmodel() |
---|
1852 | self.sizer4_4.Layout() |
---|
1853 | self.sizer4.Layout() |
---|
1854 | self.Layout() |
---|
1855 | self.Refresh() |
---|
1856 | |
---|
1857 | def _populate_box(self, combobox, list): |
---|
1858 | """ |
---|
1859 | fill combox box with dict item |
---|
1860 | |
---|
1861 | :param list: contains item to fill the combox |
---|
1862 | item must model class |
---|
1863 | """ |
---|
1864 | st = time.time() |
---|
1865 | for models in list: |
---|
1866 | model= models() |
---|
1867 | name = model.__class__.__name__ |
---|
1868 | if models.__name__!="NoStructure": |
---|
1869 | if hasattr(model, "name"): |
---|
1870 | name = model.name |
---|
1871 | combobox.Append(name,models) |
---|
1872 | return 0 |
---|
1873 | |
---|
1874 | def _onQrangeEnter(self, event): |
---|
1875 | """ |
---|
1876 | Check validity of value enter in the Q range field |
---|
1877 | """ |
---|
1878 | |
---|
1879 | tcrtl= event.GetEventObject() |
---|
1880 | #Clear msg if previously shown. |
---|
1881 | msg= "" |
---|
1882 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1883 | # Flag to register when a parameter has changed. |
---|
1884 | is_modified = False |
---|
1885 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1886 | try: |
---|
1887 | value = float(tcrtl.GetValue()) |
---|
1888 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1889 | |
---|
1890 | # If qmin and qmax have been modified, update qmin and qmax |
---|
1891 | if self._validate_qrange( self.qmin, self.qmax): |
---|
1892 | tempmin = float(self.qmin.GetValue()) |
---|
1893 | if tempmin != self.qmin_x: |
---|
1894 | self.qmin_x = tempmin |
---|
1895 | tempmax = float(self.qmax.GetValue()) |
---|
1896 | if tempmax != self.qmax_x: |
---|
1897 | self.qmax_x = tempmax |
---|
1898 | else: |
---|
1899 | tcrtl.SetBackgroundColour("pink") |
---|
1900 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1901 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1902 | return |
---|
1903 | except: |
---|
1904 | tcrtl.SetBackgroundColour("pink") |
---|
1905 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1906 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1907 | return |
---|
1908 | #Check if # of points for theory model are valid(>0). |
---|
1909 | if self.npts != None: |
---|
1910 | if check_float(self.npts): |
---|
1911 | temp_npts = float(self.npts.GetValue()) |
---|
1912 | if temp_npts != self.num_points: |
---|
1913 | self.num_points = temp_npts |
---|
1914 | is_modified = True |
---|
1915 | else: |
---|
1916 | msg= "Cannot Plot :No npts in that Qrange!!! " |
---|
1917 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1918 | else: |
---|
1919 | tcrtl.SetBackgroundColour("pink") |
---|
1920 | msg= "Model Error:wrong value entered!!!" |
---|
1921 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
1922 | #self._undo.Enable(True) |
---|
1923 | self.save_current_state() |
---|
1924 | event = PageInfoEvent(page = self) |
---|
1925 | wx.PostEvent(self.parent, event) |
---|
1926 | self.state_change= False |
---|
1927 | #Draw the model for a different range |
---|
1928 | self._draw_model() |
---|
1929 | |
---|
1930 | def _on_select_model_helper(self): |
---|
1931 | """ |
---|
1932 | call back for model selection |
---|
1933 | """ |
---|
1934 | ## reset dictionary containing reference to dispersion |
---|
1935 | self._disp_obj_dict = {} |
---|
1936 | self.disp_cb_dict ={} |
---|
1937 | |
---|
1938 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
1939 | #For MAC |
---|
1940 | form_factor = None |
---|
1941 | if f_id >= 0: |
---|
1942 | form_factor = self.formfactorbox.GetClientData(f_id) |
---|
1943 | |
---|
1944 | if not form_factor in self.model_list_box["multiplication"]: |
---|
1945 | self.structurebox.Hide() |
---|
1946 | self.text2.Hide() |
---|
1947 | self.structurebox.Disable() |
---|
1948 | self.structurebox.SetSelection(0) |
---|
1949 | self.text2.Disable() |
---|
1950 | else: |
---|
1951 | self.structurebox.Show() |
---|
1952 | self.text2.Show() |
---|
1953 | self.structurebox.Enable() |
---|
1954 | self.text2.Enable() |
---|
1955 | |
---|
1956 | if form_factor != None: |
---|
1957 | # set multifactor for Mutifunctional models |
---|
1958 | if form_factor().__class__ in self.model_list_box["Multi-Functions"]: |
---|
1959 | m_id = self.multifactorbox.GetCurrentSelection() |
---|
1960 | multiplicity = form_factor().multiplicity_info[0] |
---|
1961 | self.multifactorbox.Clear() |
---|
1962 | #self.mutifactor_text.SetLabel(form_factor().details[]) |
---|
1963 | self._set_multfactor_combobox(multiplicity) |
---|
1964 | self._show_multfactor_combobox() |
---|
1965 | #ToDo: this info should be called directly from the model |
---|
1966 | text = form_factor().multiplicity_info[1]#'No. of Shells: ' |
---|
1967 | |
---|
1968 | #self.mutifactor_text.Clear() |
---|
1969 | self.mutifactor_text.SetLabel(text) |
---|
1970 | if m_id > multiplicity -1: |
---|
1971 | # default value |
---|
1972 | m_id = 1 |
---|
1973 | |
---|
1974 | self.multi_factor = self.multifactorbox.GetClientData(m_id) |
---|
1975 | if self.multi_factor == None: self.multi_factor =0 |
---|
1976 | form_factor = form_factor(int(self.multi_factor)) |
---|
1977 | self.multifactorbox.SetSelection(m_id) |
---|
1978 | # Check len of the text1 and max_multiplicity |
---|
1979 | text = '' |
---|
1980 | if form_factor.multiplicity_info[0] == len(form_factor.multiplicity_info[2]): |
---|
1981 | text = form_factor.multiplicity_info[2][self.multi_factor] |
---|
1982 | self.mutifactor_text1.SetLabel(text) |
---|
1983 | # Check if model has get sld profile. |
---|
1984 | if len(form_factor.multiplicity_info[3]) > 0: |
---|
1985 | self.sld_axes = form_factor.multiplicity_info[3] |
---|
1986 | self.show_sld_button.Show(True) |
---|
1987 | else: |
---|
1988 | self.sld_axes = "" |
---|
1989 | |
---|
1990 | else: |
---|
1991 | self._hide_multfactor_combobox() |
---|
1992 | self.show_sld_button.Hide() |
---|
1993 | form_factor = form_factor() |
---|
1994 | self.multi_factor = None |
---|
1995 | else: |
---|
1996 | self._hide_multfactor_combobox() |
---|
1997 | self.show_sld_button.Hide() |
---|
1998 | self.multi_factor = None |
---|
1999 | |
---|
2000 | s_id = self.structurebox.GetCurrentSelection() |
---|
2001 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
2002 | |
---|
2003 | if struct_factor !=None: |
---|
2004 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
2005 | self.model= MultiplicationModel(form_factor,struct_factor()) |
---|
2006 | |
---|
2007 | else: |
---|
2008 | if form_factor != None: |
---|
2009 | self.model= form_factor |
---|
2010 | else: |
---|
2011 | self.model = None |
---|
2012 | return self.model |
---|
2013 | |
---|
2014 | |
---|
2015 | ## post state to fit panel |
---|
2016 | self.state.parameters =[] |
---|
2017 | self.state.model =self.model |
---|
2018 | self.state.qmin = self.qmin_x |
---|
2019 | self.state.multi_factor = self.multi_factor |
---|
2020 | self.disp_list =self.model.getDispParamList() |
---|
2021 | self.state.disp_list = self.disp_list |
---|
2022 | self.Layout() |
---|
2023 | |
---|
2024 | def _validate_qrange(self, qmin_ctrl, qmax_ctrl): |
---|
2025 | """ |
---|
2026 | Verify that the Q range controls have valid values |
---|
2027 | and that Qmin < Qmax. |
---|
2028 | |
---|
2029 | :param qmin_ctrl: text control for Qmin |
---|
2030 | :param qmax_ctrl: text control for Qmax |
---|
2031 | |
---|
2032 | :return: True is the Q range is value, False otherwise |
---|
2033 | |
---|
2034 | """ |
---|
2035 | qmin_validity = check_float(qmin_ctrl) |
---|
2036 | qmax_validity = check_float(qmax_ctrl) |
---|
2037 | if not (qmin_validity and qmax_validity): |
---|
2038 | return False |
---|
2039 | else: |
---|
2040 | qmin = float(qmin_ctrl.GetValue()) |
---|
2041 | qmax = float(qmax_ctrl.GetValue()) |
---|
2042 | if qmin <= qmax: |
---|
2043 | #Make sure to set both colours white. |
---|
2044 | qmin_ctrl.SetBackgroundColour(wx.WHITE) |
---|
2045 | qmin_ctrl.Refresh() |
---|
2046 | qmax_ctrl.SetBackgroundColour(wx.WHITE) |
---|
2047 | qmax_ctrl.Refresh() |
---|
2048 | else: |
---|
2049 | qmin_ctrl.SetBackgroundColour("pink") |
---|
2050 | qmin_ctrl.Refresh() |
---|
2051 | qmax_ctrl.SetBackgroundColour("pink") |
---|
2052 | qmax_ctrl.Refresh() |
---|
2053 | msg= "Invalid Q range: Q min must be smaller than Q max" |
---|
2054 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
2055 | return False |
---|
2056 | return True |
---|
2057 | |
---|
2058 | def _validate_Npts(self): |
---|
2059 | """ |
---|
2060 | Validate the number of points for fitting is more than 10 points. |
---|
2061 | If valid, setvalues Npts_fit otherwise post msg. |
---|
2062 | """ |
---|
2063 | #default flag |
---|
2064 | flag = True |
---|
2065 | |
---|
2066 | # q value from qx and qy |
---|
2067 | radius= numpy.sqrt( self.data.qx_data * self.data.qx_data + |
---|
2068 | self.data.qy_data * self.data.qy_data ) |
---|
2069 | #get unmasked index |
---|
2070 | index_data = (float(self.qmin.GetValue()) <= radius) & \ |
---|
2071 | (radius <= float(self.qmax.GetValue())) |
---|
2072 | index_data = (index_data) & (self.data.mask) |
---|
2073 | index_data = (index_data) & (numpy.isfinite(self.data.data)) |
---|
2074 | |
---|
2075 | if len(index_data[index_data]) < 10: |
---|
2076 | # change the color pink. |
---|
2077 | self.qmin.SetBackgroundColour("pink") |
---|
2078 | self.qmin.Refresh() |
---|
2079 | self.qmax.SetBackgroundColour("pink") |
---|
2080 | self.qmax.Refresh() |
---|
2081 | msg= "Cannot Plot :No or too little npts in that data range!!! " |
---|
2082 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2083 | self.fitrange = False |
---|
2084 | flag = False |
---|
2085 | else: |
---|
2086 | self.Npts_fit.SetValue(str(len(self.data.mask[index_data==True]))) |
---|
2087 | self.fitrange = True |
---|
2088 | |
---|
2089 | return flag |
---|
2090 | |
---|
2091 | def _check_value_enter(self, list, modified): |
---|
2092 | """ |
---|
2093 | :param list: model parameter and panel info |
---|
2094 | :Note: each item of the list should be as follow: |
---|
2095 | item=[check button state, parameter's name, |
---|
2096 | paramater's value, string="+/-", |
---|
2097 | parameter's error of fit, |
---|
2098 | parameter's minimum value, |
---|
2099 | parrameter's maximum value , |
---|
2100 | parameter's units] |
---|
2101 | """ |
---|
2102 | is_modified = modified |
---|
2103 | if len(list)==0: |
---|
2104 | return is_modified |
---|
2105 | for item in list: |
---|
2106 | #skip angle parameters for 1D |
---|
2107 | if self.data.__class__.__name__ !="Data2D": |
---|
2108 | if item in self.orientation_params: |
---|
2109 | continue |
---|
2110 | #try: |
---|
2111 | name = str(item[1]) |
---|
2112 | |
---|
2113 | if string.find(name,".npts") ==-1 and string.find(name,".nsigmas")==-1: |
---|
2114 | ## check model parameters range |
---|
2115 | param_min= None |
---|
2116 | param_max= None |
---|
2117 | |
---|
2118 | ## check minimun value |
---|
2119 | if item[5]!= None and item[5]!= "": |
---|
2120 | if item[5].GetValue().lstrip().rstrip()!="": |
---|
2121 | try: |
---|
2122 | |
---|
2123 | param_min = float(item[5].GetValue()) |
---|
2124 | if not self._validate_qrange(item[5],item[2]): |
---|
2125 | if numpy.isfinite(param_min): |
---|
2126 | item[2].SetValue(format_number(param_min)) |
---|
2127 | |
---|
2128 | item[5].SetBackgroundColour(wx.WHITE) |
---|
2129 | item[2].SetBackgroundColour(wx.WHITE) |
---|
2130 | |
---|
2131 | except: |
---|
2132 | msg = "Wrong Fit parameter range entered " |
---|
2133 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
2134 | raise ValueError, msg |
---|
2135 | is_modified = True |
---|
2136 | ## check maximum value |
---|
2137 | if item[6]!= None and item[6]!= "": |
---|
2138 | if item[6].GetValue().lstrip().rstrip()!="": |
---|
2139 | try: |
---|
2140 | param_max = float(item[6].GetValue()) |
---|
2141 | if not self._validate_qrange(item[2],item[6]): |
---|
2142 | if numpy.isfinite(param_max): |
---|
2143 | item[2].SetValue(format_number(param_max)) |
---|
2144 | |
---|
2145 | item[6].SetBackgroundColour(wx.WHITE) |
---|
2146 | item[2].SetBackgroundColour(wx.WHITE) |
---|
2147 | except: |
---|
2148 | msg = "Wrong Fit parameter range entered " |
---|
2149 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
2150 | raise ValueError, msg |
---|
2151 | is_modified = True |
---|
2152 | |
---|
2153 | |
---|
2154 | if param_min != None and param_max !=None: |
---|
2155 | if not self._validate_qrange(item[5], item[6]): |
---|
2156 | msg= "Wrong Fit range entered for parameter " |
---|
2157 | msg+= "name %s of model %s "%(name, self.model.name) |
---|
2158 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
2159 | |
---|
2160 | if name in self.model.details.keys(): |
---|
2161 | self.model.details[name][1:3]= param_min,param_max |
---|
2162 | is_modified = True |
---|
2163 | |
---|
2164 | else: |
---|
2165 | self.model.details [name] = ["",param_min,param_max] |
---|
2166 | is_modified = True |
---|
2167 | try: |
---|
2168 | value= float(item[2].GetValue()) |
---|
2169 | |
---|
2170 | # If the value of the parameter has changed, |
---|
2171 | # +update the model and set the is_modified flag |
---|
2172 | if value != self.model.getParam(name) and numpy.isfinite(value): |
---|
2173 | self.model.setParam(name,value) |
---|
2174 | is_modified = True |
---|
2175 | except: |
---|
2176 | msg = "Wrong Fit parameter value entered " |
---|
2177 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
2178 | |
---|
2179 | return is_modified |
---|
2180 | |
---|
2181 | |
---|
2182 | def _set_dipers_Param(self, event): |
---|
2183 | """ |
---|
2184 | respond to self.enable_disp and self.disable_disp radio box. |
---|
2185 | The dispersity object is reset inside the model into Gaussian. |
---|
2186 | When the user select yes , this method display a combo box for more selection |
---|
2187 | when the user selects No,the combo box disappears. |
---|
2188 | Redraw the model with the default dispersity (Gaussian) |
---|
2189 | """ |
---|
2190 | if self.check_invalid_panel(): |
---|
2191 | return |
---|
2192 | ## On selction if no model exists. |
---|
2193 | if self.model ==None: |
---|
2194 | self.disable_disp.SetValue(True) |
---|
2195 | msg="Please select a Model first..." |
---|
2196 | wx.MessageBox(msg, 'Info') |
---|
2197 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2198 | "Polydispersion: %s"%msg)) |
---|
2199 | return |
---|
2200 | |
---|
2201 | self._reset_dispersity() |
---|
2202 | |
---|
2203 | if self.model ==None: |
---|
2204 | self.model_disp.Hide() |
---|
2205 | self.disp_box.Hide() |
---|
2206 | self.sizer4_4.Clear(True) |
---|
2207 | return |
---|
2208 | |
---|
2209 | if self.enable_disp.GetValue(): |
---|
2210 | self.model_disp.Show(True) |
---|
2211 | self.disp_box.Show(True) |
---|
2212 | ## layout for model containing no dispersity parameters |
---|
2213 | |
---|
2214 | self.disp_list= self.model.getDispParamList() |
---|
2215 | |
---|
2216 | if len(self.disp_list)==0 and len(self.disp_cb_dict)==0: |
---|
2217 | self._layout_sizer_noDipers() |
---|
2218 | else: |
---|
2219 | ## set gaussian sizer |
---|
2220 | self._on_select_Disp(event=None) |
---|
2221 | else: |
---|
2222 | self.model_disp.Hide() |
---|
2223 | self.disp_box.Hide() |
---|
2224 | self.disp_box.SetSelection(0) |
---|
2225 | self.sizer4_4.Clear(True) |
---|
2226 | |
---|
2227 | ## post state to fit panel |
---|
2228 | self.save_current_state() |
---|
2229 | if event !=None: |
---|
2230 | #self._undo.Enable(True) |
---|
2231 | event = PageInfoEvent(page = self) |
---|
2232 | wx.PostEvent(self.parent, event) |
---|
2233 | #draw the model with the current dispersity |
---|
2234 | self._draw_model() |
---|
2235 | self.sizer4_4.Layout() |
---|
2236 | self.sizer5.Layout() |
---|
2237 | self.Layout() |
---|
2238 | self.Refresh() |
---|
2239 | |
---|
2240 | |
---|
2241 | def _layout_sizer_noDipers(self): |
---|
2242 | """ |
---|
2243 | Draw a sizer with no dispersity info |
---|
2244 | """ |
---|
2245 | ix=0 |
---|
2246 | iy=1 |
---|
2247 | self.fittable_param=[] |
---|
2248 | self.fixed_param=[] |
---|
2249 | self.orientation_params_disp=[] |
---|
2250 | |
---|
2251 | self.model_disp.Hide() |
---|
2252 | self.disp_box.Hide() |
---|
2253 | self.sizer4_4.Clear(True) |
---|
2254 | text = "No polydispersity available for this model" |
---|
2255 | model_disp = wx.StaticText(self, -1, text) |
---|
2256 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2257 | self.sizer4_4.Layout() |
---|
2258 | self.sizer4.Layout() |
---|
2259 | |
---|
2260 | def _reset_dispersity(self): |
---|
2261 | """ |
---|
2262 | put gaussian dispersity into current model |
---|
2263 | """ |
---|
2264 | if len(self.param_toFit)>0: |
---|
2265 | for item in self.fittable_param: |
---|
2266 | if item in self.param_toFit: |
---|
2267 | self.param_toFit.remove(item) |
---|
2268 | |
---|
2269 | for item in self.orientation_params_disp: |
---|
2270 | if item in self.param_toFit: |
---|
2271 | self.param_toFit.remove(item) |
---|
2272 | |
---|
2273 | self.fittable_param=[] |
---|
2274 | self.fixed_param=[] |
---|
2275 | self.orientation_params_disp=[] |
---|
2276 | self.values=[] |
---|
2277 | self.weights=[] |
---|
2278 | |
---|
2279 | from sans.models.dispersion_models import GaussianDispersion, ArrayDispersion |
---|
2280 | if len(self.disp_cb_dict)==0: |
---|
2281 | self.save_current_state() |
---|
2282 | self.sizer4_4.Clear(True) |
---|
2283 | self.Layout() |
---|
2284 | |
---|
2285 | return |
---|
2286 | if (len(self.disp_cb_dict)>0) : |
---|
2287 | for p in self.disp_cb_dict: |
---|
2288 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
2289 | disp_model = GaussianDispersion() |
---|
2290 | |
---|
2291 | self._disp_obj_dict[p] = disp_model |
---|
2292 | # Set the new model as the dispersion object for the selected parameter |
---|
2293 | try: |
---|
2294 | self.model.set_dispersion(p, disp_model) |
---|
2295 | except: |
---|
2296 | |
---|
2297 | pass |
---|
2298 | |
---|
2299 | ## save state into |
---|
2300 | self.save_current_state() |
---|
2301 | self.Layout() |
---|
2302 | self.Refresh() |
---|
2303 | |
---|
2304 | def _on_select_Disp(self,event): |
---|
2305 | """ |
---|
2306 | allow selecting different dispersion |
---|
2307 | self.disp_list should change type later .now only gaussian |
---|
2308 | """ |
---|
2309 | n = self.disp_box.GetCurrentSelection() |
---|
2310 | name = self.disp_box.GetValue() |
---|
2311 | dispersity= self.disp_box.GetClientData(n) |
---|
2312 | self.disp_name = name |
---|
2313 | |
---|
2314 | if name.lower() == "array": |
---|
2315 | self._set_sizer_arraydispersion() |
---|
2316 | else: |
---|
2317 | self._set_sizer_dispersion(dispersity= dispersity) |
---|
2318 | |
---|
2319 | self.state.disp_box= n |
---|
2320 | ## Redraw the model |
---|
2321 | self._draw_model() |
---|
2322 | #self._undo.Enable(True) |
---|
2323 | event = PageInfoEvent(page = self) |
---|
2324 | wx.PostEvent(self.parent, event) |
---|
2325 | |
---|
2326 | self.sizer4_4.Layout() |
---|
2327 | self.sizer4.Layout() |
---|
2328 | |
---|
2329 | def _set_sizer_arraydispersion(self): |
---|
2330 | """ |
---|
2331 | draw sizer with array dispersity parameters |
---|
2332 | """ |
---|
2333 | |
---|
2334 | if len(self.param_toFit)>0: |
---|
2335 | for item in self.fittable_param: |
---|
2336 | if item in self.param_toFit: |
---|
2337 | self.param_toFit.remove(item) |
---|
2338 | for item in self.orientation_params_disp: |
---|
2339 | if item in self.param_toFit: |
---|
2340 | self.param_toFit.remove(item) |
---|
2341 | for item in self.model.details.keys(): |
---|
2342 | if item in self.model.fixed: |
---|
2343 | del self.model.details [item] |
---|
2344 | |
---|
2345 | self.fittable_param=[] |
---|
2346 | self.fixed_param=[] |
---|
2347 | self.orientation_params_disp=[] |
---|
2348 | self.sizer4_4.Clear(True) |
---|
2349 | self._reset_dispersity() |
---|
2350 | ix=0 |
---|
2351 | iy=1 |
---|
2352 | disp1 = wx.StaticText(self, -1, 'Array Dispersion') |
---|
2353 | self.sizer4_4.Add(disp1,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2354 | |
---|
2355 | # Look for model parameters to which we can apply an ArrayDispersion model |
---|
2356 | # Add a check box for each parameter. |
---|
2357 | self.disp_cb_dict = {} |
---|
2358 | ix+=1 |
---|
2359 | self.noDisper_rbox = wx.RadioButton(self, -1,"None", (10, 10),style= wx.RB_GROUP) |
---|
2360 | self.Bind(wx.EVT_RADIOBUTTON,self.select_disp_angle , id=self.noDisper_rbox.GetId()) |
---|
2361 | #MAC needs SetValue |
---|
2362 | self.noDisper_rbox.SetValue(True) |
---|
2363 | self.sizer4_4.Add(self.noDisper_rbox, (iy, ix), |
---|
2364 | (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2365 | |
---|
2366 | for p in self.model.dispersion.keys(): |
---|
2367 | if not p in self.model.orientation_params: |
---|
2368 | ix+=1 |
---|
2369 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
2370 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
2371 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, id=self.disp_cb_dict[p].GetId()) |
---|
2372 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2373 | |
---|
2374 | for p in self.model.dispersion.keys(): |
---|
2375 | if p in self.model.orientation_params: |
---|
2376 | ix+=1 |
---|
2377 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
2378 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
2379 | if not (self.enable2D or self.data.__class__.__name__ =="Data2D"): |
---|
2380 | self.disp_cb_dict[p].Hide() |
---|
2381 | else: |
---|
2382 | self.disp_cb_dict[p].Show(True) |
---|
2383 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, id=self.disp_cb_dict[p].GetId()) |
---|
2384 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2385 | |
---|
2386 | |
---|
2387 | ix =0 |
---|
2388 | iy +=1 |
---|
2389 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2390 | self.Layout() |
---|
2391 | |
---|
2392 | self.state.orientation_params =[] |
---|
2393 | self.state.orientation_params_disp =[] |
---|
2394 | self.state.parameters =[] |
---|
2395 | self.state.fittable_param =[] |
---|
2396 | self.state.fixed_param =[] |
---|
2397 | |
---|
2398 | ## save checkbutton state and txtcrtl values |
---|
2399 | |
---|
2400 | self._copy_parameters_state(self.orientation_params, |
---|
2401 | self.state.orientation_params) |
---|
2402 | |
---|
2403 | self._copy_parameters_state(self.orientation_params_disp, |
---|
2404 | self.state.orientation_params_disp) |
---|
2405 | |
---|
2406 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
2407 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
2408 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
2409 | |
---|
2410 | |
---|
2411 | ## post state to fit panel |
---|
2412 | event = PageInfoEvent(page = self) |
---|
2413 | wx.PostEvent(self.parent, event) |
---|
2414 | |
---|
2415 | def _set_range_sizer(self, title, box_sizer=None, object1=None,object=None): |
---|
2416 | """ |
---|
2417 | Fill the Q range sizer |
---|
2418 | """ |
---|
2419 | #2D data? default |
---|
2420 | is_2Ddata = False |
---|
2421 | |
---|
2422 | #check if it is 2D data |
---|
2423 | if self.data.__class__.__name__ == 'Data2D': |
---|
2424 | is_2Ddata = True |
---|
2425 | |
---|
2426 | self.sizer5.Clear(True) |
---|
2427 | #-------------------------------------------------------------- |
---|
2428 | if box_sizer == None: |
---|
2429 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
2430 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2431 | else: |
---|
2432 | #for MAC |
---|
2433 | boxsizer1 = box_sizer |
---|
2434 | |
---|
2435 | self.qmin = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20),style=wx.TE_PROCESS_ENTER, |
---|
2436 | text_enter_callback = self._onQrangeEnter) |
---|
2437 | self.qmin.SetValue(str(self.qmin_x)) |
---|
2438 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
2439 | |
---|
2440 | self.qmax = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20),style=wx.TE_PROCESS_ENTER, |
---|
2441 | text_enter_callback = self._onQrangeEnter) |
---|
2442 | self.qmax.SetValue(str(self.qmax_x)) |
---|
2443 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
2444 | |
---|
2445 | id = wx.NewId() |
---|
2446 | self.reset_qrange =wx.Button(self,id,'Reset',size=(77,20)) |
---|
2447 | |
---|
2448 | self.reset_qrange.Bind(wx.EVT_BUTTON, self.on_reset_clicked,id=id) |
---|
2449 | self.reset_qrange.SetToolTipString("Reset Q range to the default values") |
---|
2450 | |
---|
2451 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
2452 | sizer= wx.GridSizer(2, 4,2, 6) |
---|
2453 | |
---|
2454 | self.btEditMask = wx.Button(self,wx.NewId(),'Editor', size=(88,23)) |
---|
2455 | self.btEditMask.Bind(wx.EVT_BUTTON, self._onMask,id= self.btEditMask.GetId()) |
---|
2456 | self.btEditMask.SetToolTipString("Edit Mask.") |
---|
2457 | self.EditMask_title = wx.StaticText(self, -1, ' Masking(2D)') |
---|
2458 | |
---|
2459 | sizer.Add(wx.StaticText(self, -1, ' Q range')) |
---|
2460 | sizer.Add(wx.StaticText(self, -1, ' Min[1/A]')) |
---|
2461 | sizer.Add(wx.StaticText(self, -1, ' Max[1/A]')) |
---|
2462 | sizer.Add(self.EditMask_title) |
---|
2463 | sizer.Add(self.reset_qrange) |
---|
2464 | |
---|
2465 | sizer.Add(self.qmin) |
---|
2466 | sizer.Add(self.qmax) |
---|
2467 | sizer.Add(self.btEditMask) |
---|
2468 | |
---|
2469 | if object1!=None: |
---|
2470 | |
---|
2471 | boxsizer1.Add(object1) |
---|
2472 | boxsizer1.Add((10,10)) |
---|
2473 | boxsizer1.Add(sizer) |
---|
2474 | if object!=None: |
---|
2475 | boxsizer1.Add((10,15)) |
---|
2476 | boxsizer1.Add(object) |
---|
2477 | if is_2Ddata: |
---|
2478 | self.btEditMask.Enable() |
---|
2479 | self.EditMask_title.Enable() |
---|
2480 | else: |
---|
2481 | self.btEditMask.Disable() |
---|
2482 | self.EditMask_title.Disable() |
---|
2483 | ## save state |
---|
2484 | self.save_current_state() |
---|
2485 | #---------------------------------------------------------------- |
---|
2486 | self.sizer5.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
2487 | self.sizer5.Layout() |
---|
2488 | |
---|
2489 | def _lay_out(self): |
---|
2490 | """ |
---|
2491 | returns self.Layout |
---|
2492 | |
---|
2493 | :Note: Mac seems to like this better when self. |
---|
2494 | Layout is called after fitting. |
---|
2495 | """ |
---|
2496 | self._sleep4sec() |
---|
2497 | self.Layout() |
---|
2498 | return |
---|
2499 | |
---|
2500 | def _sleep4sec(self): |
---|
2501 | """ |
---|
2502 | sleep for 1 sec only applied on Mac |
---|
2503 | Note: This 1sec helps for Mac not to crash on self.:ayout after self._draw_model |
---|
2504 | """ |
---|
2505 | if ON_MAC == True: |
---|
2506 | time.sleep(1) |
---|
2507 | |
---|
2508 | def on_reset_clicked(self,event): |
---|
2509 | """ |
---|
2510 | On 'Reset' button for Q range clicked |
---|
2511 | """ |
---|
2512 | flag = True |
---|
2513 | if self.check_invalid_panel(): |
---|
2514 | return |
---|
2515 | ##For 3 different cases: Data2D, Data1D, and theory |
---|
2516 | if self.data.__class__.__name__ == "Data2D": |
---|
2517 | data_min= 0 |
---|
2518 | x= max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
2519 | y= max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
2520 | self.qmin_x = data_min |
---|
2521 | self.qmax_x = math.sqrt(x*x + y*y) |
---|
2522 | # check smearing |
---|
2523 | if not self.disable_smearer.GetValue(): |
---|
2524 | temp_smearer= self.current_smearer |
---|
2525 | ## set smearing value whether or not the data contain the smearing info |
---|
2526 | if self.pinhole_smearer.GetValue(): |
---|
2527 | flag = self.update_pinhole_smear() |
---|
2528 | else: |
---|
2529 | flag = True |
---|
2530 | elif self.data.__class__.__name__ != "Data2D": |
---|
2531 | self.qmin_x = min(self.data.x) |
---|
2532 | self.qmax_x = max(self.data.x) |
---|
2533 | # check smearing |
---|
2534 | if not self.disable_smearer.GetValue(): |
---|
2535 | temp_smearer= self.current_smearer |
---|
2536 | ## set smearing value whether or not the data contain the smearing info |
---|
2537 | if self.slit_smearer.GetValue(): |
---|
2538 | flag = self.update_slit_smear() |
---|
2539 | elif self.pinhole_smearer.GetValue(): |
---|
2540 | flag = self.update_pinhole_smear() |
---|
2541 | else: |
---|
2542 | flag = True |
---|
2543 | else: |
---|
2544 | self.qmin_x = _QMIN_DEFAULT |
---|
2545 | self.qmax_x = _QMAX_DEFAULT |
---|
2546 | self.num_points = _NPTS_DEFAULT |
---|
2547 | self.state.npts = self.num_points |
---|
2548 | |
---|
2549 | if flag == False: |
---|
2550 | msg= "Cannot Plot :Must enter a number!!! " |
---|
2551 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2552 | else: |
---|
2553 | # set relative text ctrs. |
---|
2554 | self.qmin.SetValue(str(self.qmin_x)) |
---|
2555 | self.qmax.SetValue(str(self.qmax_x)) |
---|
2556 | self.set_npts2fit() |
---|
2557 | # At this point, some button and variables satatus (disabled?) should be checked |
---|
2558 | # such as color that should be reset to white in case that it was pink. |
---|
2559 | self._onparamEnter_helper() |
---|
2560 | |
---|
2561 | self.save_current_state() |
---|
2562 | self.state.qmin = self.qmin_x |
---|
2563 | self.state.qmax = self.qmax_x |
---|
2564 | |
---|
2565 | #reset the q range values |
---|
2566 | self._reset_plotting_range(self.state) |
---|
2567 | #self.compute_chisqr(smearer=self.current_smearer) |
---|
2568 | #Re draw plot |
---|
2569 | self._draw_model() |
---|
2570 | |
---|
2571 | |
---|
2572 | def on_model_help_clicked(self,event): |
---|
2573 | """ |
---|
2574 | on 'More details' button |
---|
2575 | """ |
---|
2576 | from help_panel import HelpWindow |
---|
2577 | import sans.models as models |
---|
2578 | |
---|
2579 | # Get models help model_function path |
---|
2580 | path = models.get_data_path(media='media') |
---|
2581 | model_path = os.path.join(path,"model_functions.html") |
---|
2582 | if self.model == None: |
---|
2583 | name = 'FuncHelp' |
---|
2584 | else: |
---|
2585 | name = self.model.origin_name |
---|
2586 | |
---|
2587 | frame = HelpWindow(None, -1, pageToOpen=model_path) |
---|
2588 | frame.Show(True) |
---|
2589 | if frame.rhelp.HasAnchor(name): |
---|
2590 | frame.rhelp.ScrollToAnchor(name) |
---|
2591 | else: |
---|
2592 | msg= "Model does not contains an available description " |
---|
2593 | msg +="Please try searching in the Help window" |
---|
2594 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2595 | |
---|
2596 | |
---|