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