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