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