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