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