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