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.fitrange =True |
---|
1007 | is_2Ddata = False |
---|
1008 | #self._undo.Enable(True) |
---|
1009 | # check if 2d data |
---|
1010 | if self.data.__class__.__name__ =="Data2D": |
---|
1011 | is_2Ddata = True |
---|
1012 | if self.model !=None: |
---|
1013 | try: |
---|
1014 | is_modified =self._check_value_enter( self.fittable_param ,is_modified) |
---|
1015 | is_modified =self._check_value_enter( self.fixed_param ,is_modified) |
---|
1016 | is_modified =self._check_value_enter( self.parameters ,is_modified) |
---|
1017 | except: |
---|
1018 | pass |
---|
1019 | #if is_modified: |
---|
1020 | |
---|
1021 | # Here we should check whether the boundaries have been modified. |
---|
1022 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
1023 | # set the is_modified flag to True |
---|
1024 | if self._validate_qrange(self.qmin, self.qmax): |
---|
1025 | tempmin = float(self.qmin.GetValue()) |
---|
1026 | if tempmin != self.qmin_x: |
---|
1027 | self.qmin_x = tempmin |
---|
1028 | is_modified = True |
---|
1029 | tempmax = float(self.qmax.GetValue()) |
---|
1030 | if tempmax != self.qmax_x: |
---|
1031 | self.qmax_x = tempmax |
---|
1032 | is_modified = True |
---|
1033 | |
---|
1034 | if is_2Ddata: |
---|
1035 | # set mask |
---|
1036 | is_modified = self._validate_Npts() |
---|
1037 | |
---|
1038 | else: |
---|
1039 | self.fitrange = False |
---|
1040 | |
---|
1041 | ## if any value is modify draw model with new value |
---|
1042 | if not self.fitrange: |
---|
1043 | self.btFit.Disable() |
---|
1044 | if is_2Ddata: self.btEditMask.Disable() |
---|
1045 | else: |
---|
1046 | self.btFit.Enable(True) |
---|
1047 | if is_2Ddata: self.btEditMask.Enable(True) |
---|
1048 | |
---|
1049 | if is_modified and self.fitrange: |
---|
1050 | self.state_change= True |
---|
1051 | self._draw_model() |
---|
1052 | self.Refresh() |
---|
1053 | return is_modified |
---|
1054 | |
---|
1055 | def _update_paramv_on_fit(self): |
---|
1056 | """ |
---|
1057 | make sure that update param values just before the fitting |
---|
1058 | """ |
---|
1059 | #flag for qmin qmax check values |
---|
1060 | flag = True |
---|
1061 | self.fitrange = True |
---|
1062 | is_modified = False |
---|
1063 | is_2Ddata = False |
---|
1064 | |
---|
1065 | # check if 2d data |
---|
1066 | if self.data.__class__.__name__ =="Data2D": |
---|
1067 | is_2Ddata = True |
---|
1068 | |
---|
1069 | ##So make sure that update param values on_Fit. |
---|
1070 | #self._undo.Enable(True) |
---|
1071 | if self.model !=None: |
---|
1072 | ##Check the values |
---|
1073 | self._check_value_enter( self.fittable_param ,is_modified) |
---|
1074 | self._check_value_enter( self.fixed_param ,is_modified) |
---|
1075 | self._check_value_enter( self.parameters ,is_modified) |
---|
1076 | |
---|
1077 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
1078 | # Here we should check whether the boundaries have been modified. |
---|
1079 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
1080 | # set the is_modified flag to True |
---|
1081 | self.fitrange = self._validate_qrange(self.qmin, self.qmax) |
---|
1082 | if self.fitrange: |
---|
1083 | tempmin = float(self.qmin.GetValue()) |
---|
1084 | if tempmin != self.qmin_x: |
---|
1085 | self.qmin_x = tempmin |
---|
1086 | tempmax = float(self.qmax.GetValue()) |
---|
1087 | if tempmax != self.qmax_x: |
---|
1088 | self.qmax_x = tempmax |
---|
1089 | if tempmax == tempmin: |
---|
1090 | flag = False |
---|
1091 | temp_smearer = None |
---|
1092 | if not self.disable_smearer.GetValue(): |
---|
1093 | temp_smearer= self.current_smearer |
---|
1094 | if self.slit_smearer.GetValue(): |
---|
1095 | flag = self.update_slit_smear() |
---|
1096 | elif self.pinhole_smearer.GetValue(): |
---|
1097 | flag = self.update_pinhole_smear() |
---|
1098 | else: |
---|
1099 | self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
1100 | qmax= float(self.qmax_x)) |
---|
1101 | elif not is_2Ddata: |
---|
1102 | self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
1103 | qmax= float(self.qmax_x)) |
---|
1104 | index_data = ((self.qmin_x <= self.data.x)&(self.data.x <= self.qmax_x)) |
---|
1105 | self.Npts_fit.SetValue(str(len(self.data.x[index_data==True]))) |
---|
1106 | flag = True |
---|
1107 | if is_2Ddata: |
---|
1108 | # only 2D case set mask |
---|
1109 | flag = self._validate_Npts() |
---|
1110 | if not flag: |
---|
1111 | return flag |
---|
1112 | else: flag = False |
---|
1113 | else: |
---|
1114 | flag = False |
---|
1115 | |
---|
1116 | #For invalid q range, disable the mask editor and fit button, vs. |
---|
1117 | if not self.fitrange: |
---|
1118 | self.btFit.Disable() |
---|
1119 | self.btEditMask.Disable() |
---|
1120 | else: |
---|
1121 | self.btFit.Enable(True) |
---|
1122 | self.btEditMask.Enable(True) |
---|
1123 | |
---|
1124 | |
---|
1125 | if not flag: |
---|
1126 | msg= "Cannot Plot or Fit :Must select a model or Fitting range is not valid!!! " |
---|
1127 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1128 | |
---|
1129 | self.save_current_state() |
---|
1130 | |
---|
1131 | return flag |
---|
1132 | |
---|
1133 | def _is_modified(self, is_modified): |
---|
1134 | """ |
---|
1135 | return to self._is_modified |
---|
1136 | """ |
---|
1137 | return is_modified |
---|
1138 | |
---|
1139 | def _reset_parameters_state(self, listtorestore,statelist): |
---|
1140 | """ |
---|
1141 | Reset the parameters at the given state |
---|
1142 | """ |
---|
1143 | if len(statelist)==0 or len(listtorestore)==0 : |
---|
1144 | return |
---|
1145 | if len(statelist)!= len(listtorestore) : |
---|
1146 | return |
---|
1147 | |
---|
1148 | for j in range(len(listtorestore)): |
---|
1149 | item_page = listtorestore[j] |
---|
1150 | item_page_info = statelist[j] |
---|
1151 | ##change the state of the check box for simple parameters |
---|
1152 | if item_page[0]!=None: |
---|
1153 | item_page[0].SetValue(item_page_info[0]) |
---|
1154 | |
---|
1155 | if item_page[2]!=None: |
---|
1156 | item_page[2].SetValue(item_page_info[2]) |
---|
1157 | |
---|
1158 | if item_page[3]!=None: |
---|
1159 | ## show or hide text +/- |
---|
1160 | if item_page_info[2]: |
---|
1161 | item_page[3].Show(True) |
---|
1162 | else: |
---|
1163 | item_page[3].Hide() |
---|
1164 | if item_page[4]!=None: |
---|
1165 | ## show of hide the text crtl for fitting error |
---|
1166 | if item_page_info[4][0]: |
---|
1167 | item_page[4].Show(True) |
---|
1168 | item_page[4].SetValue(item_page_info[4][1]) |
---|
1169 | else: |
---|
1170 | item_page[3].Hide() |
---|
1171 | if item_page[5]!=None: |
---|
1172 | ## show of hide the text crtl for fitting error |
---|
1173 | if item_page_info[5][0]: |
---|
1174 | item_page[5].Show(True) |
---|
1175 | item_page[5].SetValue(item_page_info[5][1]) |
---|
1176 | else: |
---|
1177 | item_page[5].Hide() |
---|
1178 | |
---|
1179 | if item_page[6]!=None: |
---|
1180 | ## show of hide the text crtl for fitting error |
---|
1181 | if item_page_info[6][0]: |
---|
1182 | item_page[6].Show(True) |
---|
1183 | item_page[6].SetValue(item_page_info[6][1]) |
---|
1184 | else: |
---|
1185 | item_page[6].Hide() |
---|
1186 | |
---|
1187 | |
---|
1188 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
1189 | """ |
---|
1190 | copy the state of button |
---|
1191 | @param listtocopy: the list of check button to copy |
---|
1192 | @param statelist: list of state object to store the current state |
---|
1193 | """ |
---|
1194 | if len(listtocopy)==0: |
---|
1195 | return |
---|
1196 | |
---|
1197 | for item in listtocopy: |
---|
1198 | |
---|
1199 | checkbox_state = None |
---|
1200 | if item[0]!= None: |
---|
1201 | checkbox_state= item[0].GetValue() |
---|
1202 | parameter_name = item[1] |
---|
1203 | parameter_value = None |
---|
1204 | if item[2]!=None: |
---|
1205 | parameter_value = item[2].GetValue() |
---|
1206 | static_text = None |
---|
1207 | if item[3]!=None: |
---|
1208 | static_text = item[3].IsShown() |
---|
1209 | error_value = None |
---|
1210 | error_state = None |
---|
1211 | if item[4]!= None: |
---|
1212 | error_value = item[4].GetValue() |
---|
1213 | error_state = item[4].IsShown() |
---|
1214 | |
---|
1215 | min_value = None |
---|
1216 | min_state = None |
---|
1217 | if item[5]!= None: |
---|
1218 | min_value = item[5].GetValue() |
---|
1219 | min_state = item[5].IsShown() |
---|
1220 | |
---|
1221 | max_value = None |
---|
1222 | max_state = None |
---|
1223 | if item[6]!= None: |
---|
1224 | max_value = item[6].GetValue() |
---|
1225 | max_state = item[6].IsShown() |
---|
1226 | unit=None |
---|
1227 | if item[7]!=None: |
---|
1228 | unit = item[7].GetLabel() |
---|
1229 | |
---|
1230 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
1231 | static_text ,[error_state,error_value], |
---|
1232 | [min_state,min_value],[max_state , max_value],unit]) |
---|
1233 | |
---|
1234 | |
---|
1235 | |
---|
1236 | def _set_model_sizer_selection(self, model): |
---|
1237 | """ |
---|
1238 | Display the sizer according to the type of the current model |
---|
1239 | """ |
---|
1240 | if model ==None: |
---|
1241 | return |
---|
1242 | if hasattr(model ,"s_model"): |
---|
1243 | |
---|
1244 | class_name= model.s_model.__class__ |
---|
1245 | name= model.s_model.name |
---|
1246 | flag= name != "NoStructure" |
---|
1247 | if flag and (class_name in self.model_list_box["Structure Factors"]): |
---|
1248 | self.structurebox.Show() |
---|
1249 | self.text2.Show() |
---|
1250 | self.structurebox.Enable() |
---|
1251 | self.text2.Enable() |
---|
1252 | items = self.structurebox.GetItems() |
---|
1253 | self.sizer1.Layout() |
---|
1254 | self.SetScrollbars(20,20,25,65) |
---|
1255 | for i in range(len(items)): |
---|
1256 | if items[i]== str(name): |
---|
1257 | self.structurebox.SetSelection(i) |
---|
1258 | break |
---|
1259 | |
---|
1260 | if hasattr(model ,"p_model"): |
---|
1261 | class_name = model.p_model.__class__ |
---|
1262 | name = model.p_model.name |
---|
1263 | self.formfactorbox.Clear() |
---|
1264 | |
---|
1265 | for k, list in self.model_list_box.iteritems(): |
---|
1266 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1267 | self.shape_rbutton.SetValue(True) |
---|
1268 | ## fill the form factor list with new model |
---|
1269 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1270 | items = self.formfactorbox.GetItems() |
---|
1271 | ## set comboxbox to the selected item |
---|
1272 | for i in range(len(items)): |
---|
1273 | if items[i]== str(name): |
---|
1274 | self.formfactorbox.SetSelection(i) |
---|
1275 | break |
---|
1276 | return |
---|
1277 | elif k == "Shape-Independent": |
---|
1278 | self.shape_indep_rbutton.SetValue(True) |
---|
1279 | elif k == "Structure Factors": |
---|
1280 | self.struct_rbutton.SetValue(True) |
---|
1281 | else: |
---|
1282 | self.plugin_rbutton.SetValue(True) |
---|
1283 | |
---|
1284 | if class_name in list: |
---|
1285 | ## fill the form factor list with new model |
---|
1286 | self._populate_box(self.formfactorbox, list) |
---|
1287 | items = self.formfactorbox.GetItems() |
---|
1288 | ## set comboxbox to the selected item |
---|
1289 | for i in range(len(items)): |
---|
1290 | if items[i]== str(name): |
---|
1291 | self.formfactorbox.SetSelection(i) |
---|
1292 | break |
---|
1293 | break |
---|
1294 | else: |
---|
1295 | |
---|
1296 | ## Select the model from the menu |
---|
1297 | class_name = model.__class__ |
---|
1298 | name = model.name |
---|
1299 | self.formfactorbox.Clear() |
---|
1300 | items = self.formfactorbox.GetItems() |
---|
1301 | |
---|
1302 | for k, list in self.model_list_box.iteritems(): |
---|
1303 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1304 | if class_name in self.model_list_box["P(Q)*S(Q)"]: |
---|
1305 | self.structurebox.Show() |
---|
1306 | self.text2.Show() |
---|
1307 | self.structurebox.Enable() |
---|
1308 | self.structurebox.SetSelection(0) |
---|
1309 | self.text2.Enable() |
---|
1310 | else: |
---|
1311 | self.structurebox.Hide() |
---|
1312 | self.text2.Hide() |
---|
1313 | self.structurebox.Disable() |
---|
1314 | self.structurebox.SetSelection(0) |
---|
1315 | self.text2.Disable() |
---|
1316 | |
---|
1317 | self.shape_rbutton.SetValue(True) |
---|
1318 | ## fill the form factor list with new model |
---|
1319 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1320 | items = self.formfactorbox.GetItems() |
---|
1321 | ## set comboxbox to the selected item |
---|
1322 | for i in range(len(items)): |
---|
1323 | if items[i]== str(name): |
---|
1324 | self.formfactorbox.SetSelection(i) |
---|
1325 | break |
---|
1326 | return |
---|
1327 | elif k == "Shape-Independent": |
---|
1328 | self.shape_indep_rbutton.SetValue(True) |
---|
1329 | elif k == "Structure Factors": |
---|
1330 | self.struct_rbutton.SetValue(True) |
---|
1331 | else: |
---|
1332 | self.plugin_rbutton.SetValue(True) |
---|
1333 | if class_name in list: |
---|
1334 | self.structurebox.SetSelection(0) |
---|
1335 | self.structurebox.Disable() |
---|
1336 | self.text2.Disable() |
---|
1337 | ## fill the form factor list with new model |
---|
1338 | self._populate_box(self.formfactorbox, list) |
---|
1339 | items = self.formfactorbox.GetItems() |
---|
1340 | ## set comboxbox to the selected item |
---|
1341 | for i in range(len(items)): |
---|
1342 | if items[i]== str(name): |
---|
1343 | self.formfactorbox.SetSelection(i) |
---|
1344 | break |
---|
1345 | break |
---|
1346 | |
---|
1347 | |
---|
1348 | def _draw_model(self): |
---|
1349 | """ |
---|
1350 | Method to draw or refresh a plotted model. |
---|
1351 | The method will use the data member from the model page |
---|
1352 | to build a call to the fitting perspective manager. |
---|
1353 | |
---|
1354 | [Note to coder: This way future changes will be done in only one place.] |
---|
1355 | """ |
---|
1356 | if self.check_invalid_panel(): |
---|
1357 | return |
---|
1358 | if self.model !=None: |
---|
1359 | temp_smear=None |
---|
1360 | if hasattr(self, "enable_smearer"): |
---|
1361 | if not self.disable_smearer.GetValue(): |
---|
1362 | temp_smear= self.current_smearer |
---|
1363 | |
---|
1364 | self.manager.draw_model(self.model, data=self.data, |
---|
1365 | smearer= temp_smear, |
---|
1366 | qmin=float(self.qmin_x), |
---|
1367 | qmax=float(self.qmax_x), |
---|
1368 | qstep= float(self.num_points), |
---|
1369 | enable2D=self.enable2D) |
---|
1370 | |
---|
1371 | |
---|
1372 | def _set_model_sizer(self,sizer, box_sizer, title="", object=None): |
---|
1373 | """ |
---|
1374 | Use lists to fill a sizer for model info |
---|
1375 | """ |
---|
1376 | |
---|
1377 | sizer.Clear(True) |
---|
1378 | ##For MAC, this should defined here. |
---|
1379 | if box_sizer == None: |
---|
1380 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
1381 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1382 | else: |
---|
1383 | boxsizer1 = box_sizer |
---|
1384 | |
---|
1385 | #-------------------------------------------------------- |
---|
1386 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shapes', style=wx.RB_GROUP) |
---|
1387 | self.shape_indep_rbutton = wx.RadioButton(self, -1, "Shape-Independent") |
---|
1388 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure Factor ") |
---|
1389 | self.plugin_rbutton = wx.RadioButton(self, -1, "Customized Models") |
---|
1390 | |
---|
1391 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1392 | id= self.shape_rbutton.GetId() ) |
---|
1393 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1394 | id= self.shape_indep_rbutton.GetId() ) |
---|
1395 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1396 | id= self.struct_rbutton.GetId() ) |
---|
1397 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1398 | id= self.plugin_rbutton.GetId() ) |
---|
1399 | #MAC needs SetValue |
---|
1400 | self.shape_rbutton.SetValue(True) |
---|
1401 | |
---|
1402 | sizer_radiobutton = wx.GridSizer(2, 2,5, 5) |
---|
1403 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
1404 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
1405 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
1406 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
1407 | |
---|
1408 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
1409 | |
---|
1410 | self.text1 = wx.StaticText( self,-1,"" ) |
---|
1411 | self.text2 = wx.StaticText( self,-1,"P(Q)*S(Q)" ) |
---|
1412 | |
---|
1413 | |
---|
1414 | self.formfactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1415 | if self.model!= None: |
---|
1416 | self.formfactorbox.SetValue(self.model.name) |
---|
1417 | |
---|
1418 | self.structurebox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1419 | |
---|
1420 | self.initialize_combox() |
---|
1421 | |
---|
1422 | wx.EVT_COMBOBOX(self.formfactorbox,-1, self._on_select_model) |
---|
1423 | wx.EVT_COMBOBOX(self.structurebox,-1, self._on_select_model) |
---|
1424 | |
---|
1425 | |
---|
1426 | ## check model type to show sizer |
---|
1427 | if self.model !=None: |
---|
1428 | self._set_model_sizer_selection( self.model ) |
---|
1429 | |
---|
1430 | sizer_selection.Add(self.text1) |
---|
1431 | sizer_selection.Add((5,5)) |
---|
1432 | sizer_selection.Add(self.formfactorbox) |
---|
1433 | sizer_selection.Add((5,5)) |
---|
1434 | sizer_selection.Add(self.text2) |
---|
1435 | sizer_selection.Add((5,5)) |
---|
1436 | sizer_selection.Add(self.structurebox) |
---|
1437 | sizer_selection.Add((5,5)) |
---|
1438 | |
---|
1439 | boxsizer1.Add( sizer_radiobutton ) |
---|
1440 | boxsizer1.Add( (20,20)) |
---|
1441 | boxsizer1.Add( sizer_selection ) |
---|
1442 | if object !=None: |
---|
1443 | boxsizer1.Add( (-72,-72)) |
---|
1444 | boxsizer1.Add( object, 0, wx.ALIGN_RIGHT| wx.RIGHT, 35) |
---|
1445 | boxsizer1.Add( (60,60)) |
---|
1446 | #-------------------------------------------------------- |
---|
1447 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1448 | sizer.Layout() |
---|
1449 | self.SetScrollbars(20,20,25,65) |
---|
1450 | |
---|
1451 | |
---|
1452 | def _show_combox(self, event): |
---|
1453 | """ |
---|
1454 | Show combox box associate with type of model selected |
---|
1455 | """ |
---|
1456 | if self.check_invalid_panel(): |
---|
1457 | return |
---|
1458 | |
---|
1459 | ## Don't want to populate combo box again if the event comes from check box |
---|
1460 | if self.shape_rbutton.GetValue()and\ |
---|
1461 | event.GetEventObject()==self.shape_rbutton: |
---|
1462 | ##fill the combobox with form factor list |
---|
1463 | self.structurebox.SetSelection(0) |
---|
1464 | self.structurebox.Disable() |
---|
1465 | self.formfactorbox.Clear() |
---|
1466 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1467 | |
---|
1468 | if self.shape_indep_rbutton.GetValue()and\ |
---|
1469 | event.GetEventObject()==self.shape_indep_rbutton: |
---|
1470 | ##fill the combobox with shape independent factor list |
---|
1471 | self.structurebox.SetSelection(0) |
---|
1472 | self.structurebox.Disable() |
---|
1473 | self.formfactorbox.Clear() |
---|
1474 | self._populate_box( self.formfactorbox, |
---|
1475 | self.model_list_box["Shape-Independent"]) |
---|
1476 | |
---|
1477 | if self.struct_rbutton.GetValue() and\ |
---|
1478 | event.GetEventObject()==self.struct_rbutton: |
---|
1479 | ##fill the combobox with structure factor list |
---|
1480 | self.structurebox.SetSelection(0) |
---|
1481 | self.structurebox.Disable() |
---|
1482 | self.formfactorbox.Clear() |
---|
1483 | self._populate_box( self.formfactorbox, |
---|
1484 | self.model_list_box["Structure Factors"]) |
---|
1485 | |
---|
1486 | if self.plugin_rbutton.GetValue()and\ |
---|
1487 | event.GetEventObject()==self.plugin_rbutton: |
---|
1488 | |
---|
1489 | ##fill the combobox with form factor list |
---|
1490 | self.structurebox.Disable() |
---|
1491 | self.formfactorbox.Clear() |
---|
1492 | self._populate_box( self.formfactorbox, |
---|
1493 | self.model_list_box["Customized Models"]) |
---|
1494 | |
---|
1495 | self._on_select_model(event=None) |
---|
1496 | self._save_typeOfmodel() |
---|
1497 | self.sizer4_4.Layout() |
---|
1498 | self.sizer4.Layout() |
---|
1499 | self.Layout() |
---|
1500 | self.Refresh() |
---|
1501 | self.SetScrollbars(20,20,25,65) |
---|
1502 | |
---|
1503 | def _populate_box(self, combobox, list): |
---|
1504 | """ |
---|
1505 | fill combox box with dict item |
---|
1506 | @param list: contains item to fill the combox |
---|
1507 | item must model class |
---|
1508 | """ |
---|
1509 | for models in list: |
---|
1510 | model= models() |
---|
1511 | name = model.__class__.__name__ |
---|
1512 | if models.__name__!="NoStructure": |
---|
1513 | if hasattr(model, "name"): |
---|
1514 | name = model.name |
---|
1515 | combobox.Append(name,models) |
---|
1516 | |
---|
1517 | return 0 |
---|
1518 | |
---|
1519 | #def _onparamEnter(self,event): |
---|
1520 | """ |
---|
1521 | #when enter value on panel redraw model according to changed |
---|
1522 | """ |
---|
1523 | """ |
---|
1524 | tcrtl= event.GetEventObject() |
---|
1525 | |
---|
1526 | #Clear msg if previously shown. |
---|
1527 | msg= "" |
---|
1528 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1529 | |
---|
1530 | ## save current state |
---|
1531 | self.save_current_state() |
---|
1532 | if event !=None: |
---|
1533 | #self._undo.Enable(True) |
---|
1534 | event = PageInfoEvent(page = self) |
---|
1535 | wx.PostEvent(self.parent, event) |
---|
1536 | |
---|
1537 | if check_float(tcrtl): |
---|
1538 | |
---|
1539 | self._onparamEnter_helper() |
---|
1540 | #event.Skip() |
---|
1541 | else: |
---|
1542 | msg= "Cannot Plot :Must enter a number!!! " |
---|
1543 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1544 | #event.Skip() |
---|
1545 | return |
---|
1546 | """ |
---|
1547 | def _onQrangeEnter(self, event): |
---|
1548 | """ |
---|
1549 | Check validity of value enter in the Q range field |
---|
1550 | """ |
---|
1551 | |
---|
1552 | tcrtl= event.GetEventObject() |
---|
1553 | #Clear msg if previously shown. |
---|
1554 | msg= "" |
---|
1555 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1556 | # Flag to register when a parameter has changed. |
---|
1557 | is_modified = False |
---|
1558 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1559 | try: |
---|
1560 | value = float(tcrtl.GetValue()) |
---|
1561 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1562 | |
---|
1563 | # If qmin and qmax have been modified, update qmin and qmax |
---|
1564 | if self._validate_qrange( self.qmin, self.qmax): |
---|
1565 | tempmin = float(self.qmin.GetValue()) |
---|
1566 | if tempmin != self.qmin_x: |
---|
1567 | self.qmin_x = tempmin |
---|
1568 | tempmax = float(self.qmax.GetValue()) |
---|
1569 | if tempmax != self.qmax_x: |
---|
1570 | self.qmax_x = tempmax |
---|
1571 | else: |
---|
1572 | tcrtl.SetBackgroundColour("pink") |
---|
1573 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1574 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1575 | return |
---|
1576 | |
---|
1577 | except: |
---|
1578 | tcrtl.SetBackgroundColour("pink") |
---|
1579 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1580 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1581 | return |
---|
1582 | #Check if # of points for theory model are valid(>0). |
---|
1583 | if self.npts != None: |
---|
1584 | if check_float(self.npts): |
---|
1585 | temp_npts = float(self.npts.GetValue()) |
---|
1586 | if temp_npts != self.num_points: |
---|
1587 | self.num_points = temp_npts |
---|
1588 | is_modified = True |
---|
1589 | else: |
---|
1590 | msg= "Cannot Plot :No npts in that Qrange!!! " |
---|
1591 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1592 | |
---|
1593 | else: |
---|
1594 | tcrtl.SetBackgroundColour("pink") |
---|
1595 | msg= "Model Error:wrong value entered!!!" |
---|
1596 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1597 | |
---|
1598 | #self._undo.Enable(True) |
---|
1599 | self.save_current_state() |
---|
1600 | event = PageInfoEvent(page = self) |
---|
1601 | wx.PostEvent(self.parent, event) |
---|
1602 | self.state_change= False |
---|
1603 | #Draw the model for a different range |
---|
1604 | self._draw_model() |
---|
1605 | |
---|
1606 | def _on_select_model_helper(self): |
---|
1607 | """ |
---|
1608 | call back for model selection |
---|
1609 | """ |
---|
1610 | ## reset dictionary containing reference to dispersion |
---|
1611 | self._disp_obj_dict = {} |
---|
1612 | self.disp_cb_dict ={} |
---|
1613 | |
---|
1614 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
1615 | #For MAC |
---|
1616 | form_factor = None |
---|
1617 | if f_id >= 0: |
---|
1618 | form_factor = self.formfactorbox.GetClientData( f_id ) |
---|
1619 | |
---|
1620 | if not form_factor in self.model_list_box["multiplication"]: |
---|
1621 | self.structurebox.Hide() |
---|
1622 | self.text2.Hide() |
---|
1623 | self.structurebox.Disable() |
---|
1624 | self.structurebox.SetSelection(0) |
---|
1625 | self.text2.Disable() |
---|
1626 | else: |
---|
1627 | self.structurebox.Show() |
---|
1628 | self.text2.Show() |
---|
1629 | self.structurebox.Enable() |
---|
1630 | self.text2.Enable() |
---|
1631 | if self.data.__class__.__name__ =="Data2D": |
---|
1632 | self.smear_description_2d.Show(True) |
---|
1633 | |
---|
1634 | s_id = self.structurebox.GetCurrentSelection() |
---|
1635 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
1636 | |
---|
1637 | if struct_factor !=None: |
---|
1638 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
1639 | self.model= MultiplicationModel(form_factor(),struct_factor()) |
---|
1640 | |
---|
1641 | else: |
---|
1642 | if form_factor != None: |
---|
1643 | self.model= form_factor() |
---|
1644 | else: |
---|
1645 | self.model = None |
---|
1646 | return self.model |
---|
1647 | |
---|
1648 | ## post state to fit panel |
---|
1649 | self.state.parameters =[] |
---|
1650 | self.state.model =self.model |
---|
1651 | self.disp_list =self.model.getDispParamList() |
---|
1652 | self.state.disp_list = self.disp_list |
---|
1653 | self.Layout() |
---|
1654 | |
---|
1655 | def _validate_qrange(self, qmin_ctrl, qmax_ctrl): |
---|
1656 | """ |
---|
1657 | Verify that the Q range controls have valid values |
---|
1658 | and that Qmin < Qmax. |
---|
1659 | |
---|
1660 | @param qmin_ctrl: text control for Qmin |
---|
1661 | @param qmax_ctrl: text control for Qmax |
---|
1662 | @return: True is the Q range is value, False otherwise |
---|
1663 | """ |
---|
1664 | qmin_validity = check_float(qmin_ctrl) |
---|
1665 | qmax_validity = check_float(qmax_ctrl) |
---|
1666 | if not (qmin_validity and qmax_validity): |
---|
1667 | return False |
---|
1668 | else: |
---|
1669 | qmin = float(qmin_ctrl.GetValue()) |
---|
1670 | qmax = float(qmax_ctrl.GetValue()) |
---|
1671 | if qmin <= qmax: |
---|
1672 | #Make sure to set both colours white. |
---|
1673 | qmin_ctrl.SetBackgroundColour(wx.WHITE) |
---|
1674 | qmin_ctrl.Refresh() |
---|
1675 | qmax_ctrl.SetBackgroundColour(wx.WHITE) |
---|
1676 | qmax_ctrl.Refresh() |
---|
1677 | else: |
---|
1678 | qmin_ctrl.SetBackgroundColour("pink") |
---|
1679 | qmin_ctrl.Refresh() |
---|
1680 | qmax_ctrl.SetBackgroundColour("pink") |
---|
1681 | qmax_ctrl.Refresh() |
---|
1682 | msg= "Invalid Q range: Q min must be smaller than Q max" |
---|
1683 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
1684 | return False |
---|
1685 | return True |
---|
1686 | |
---|
1687 | def _validate_Npts(self): |
---|
1688 | """ |
---|
1689 | Validate the number of points for fitting is more than 10 points. |
---|
1690 | If valid, setvalues Npts_fit otherwise post msg. |
---|
1691 | """ |
---|
1692 | #default flag |
---|
1693 | flag = True |
---|
1694 | |
---|
1695 | # q value from qx and qy |
---|
1696 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + self.data.qy_data*self.data.qy_data ) |
---|
1697 | #get unmasked index |
---|
1698 | index_data = (float(self.qmin.GetValue()) <= radius)&(radius<= float(self.qmax.GetValue())) |
---|
1699 | index_data = (index_data)&(self.data.mask) |
---|
1700 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1701 | |
---|
1702 | if len(index_data[index_data]) < 10: |
---|
1703 | # change the color pink. |
---|
1704 | self.qmin.SetBackgroundColour("pink") |
---|
1705 | self.qmin.Refresh() |
---|
1706 | self.qmax.SetBackgroundColour("pink") |
---|
1707 | self.qmax.Refresh() |
---|
1708 | msg= "Cannot Plot :No or too little npts in that data range!!! " |
---|
1709 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1710 | self.fitrange = False |
---|
1711 | flag = False |
---|
1712 | else: |
---|
1713 | self.Npts_fit.SetValue(str(len(self.data.mask[index_data==True]))) |
---|
1714 | self.fitrange = True |
---|
1715 | |
---|
1716 | return flag |
---|
1717 | |
---|
1718 | def _check_value_enter(self, list, modified): |
---|
1719 | """ |
---|
1720 | @param list: model parameter and panel info |
---|
1721 | each item of the list should be as follow: |
---|
1722 | item=[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1723 | """ |
---|
1724 | is_modified = modified |
---|
1725 | if len(list)==0: |
---|
1726 | return is_modified |
---|
1727 | for item in list: |
---|
1728 | #skip angle parameters for 1D |
---|
1729 | if self.data.__class__.__name__ !="Data2D": |
---|
1730 | if item in self.orientation_params: |
---|
1731 | continue |
---|
1732 | #try: |
---|
1733 | name = str(item[1]) |
---|
1734 | |
---|
1735 | if string.find(name,".npts") ==-1 and string.find(name,".nsigmas")==-1: |
---|
1736 | ## check model parameters range |
---|
1737 | param_min= None |
---|
1738 | param_max= None |
---|
1739 | |
---|
1740 | ## check minimun value |
---|
1741 | if item[5]!= None and item[5]!= "": |
---|
1742 | if item[5].GetValue().lstrip().rstrip()!="": |
---|
1743 | try: |
---|
1744 | |
---|
1745 | param_min = float(item[5].GetValue()) |
---|
1746 | if not self._validate_qrange(item[5],item[2]): |
---|
1747 | if numpy.isfinite(param_min): |
---|
1748 | item[2].SetValue(format_number(param_min)) |
---|
1749 | |
---|
1750 | item[5].SetBackgroundColour(wx.WHITE) |
---|
1751 | item[2].SetBackgroundColour(wx.WHITE) |
---|
1752 | |
---|
1753 | except: |
---|
1754 | msg = "Wrong Fit parameter range entered " |
---|
1755 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
1756 | raise ValueError, msg |
---|
1757 | is_modified = True |
---|
1758 | ## check maximum value |
---|
1759 | if item[6]!= None and item[6]!= "": |
---|
1760 | if item[6].GetValue().lstrip().rstrip()!="": |
---|
1761 | try: |
---|
1762 | param_max = float(item[6].GetValue()) |
---|
1763 | if not self._validate_qrange(item[2],item[6]): |
---|
1764 | if numpy.isfinite(param_max): |
---|
1765 | item[2].SetValue(format_number(param_max)) |
---|
1766 | |
---|
1767 | item[6].SetBackgroundColour(wx.WHITE) |
---|
1768 | item[2].SetBackgroundColour(wx.WHITE) |
---|
1769 | except: |
---|
1770 | msg = "Wrong Fit parameter range entered " |
---|
1771 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
1772 | raise ValueError, msg |
---|
1773 | is_modified = True |
---|
1774 | |
---|
1775 | |
---|
1776 | if param_min != None and param_max !=None: |
---|
1777 | if not self._validate_qrange(item[5], item[6]): |
---|
1778 | msg= "Wrong Fit range entered for parameter " |
---|
1779 | msg+= "name %s of model %s "%(name, self.model.name) |
---|
1780 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
1781 | |
---|
1782 | if name in self.model.details.keys(): |
---|
1783 | self.model.details[name][1:3]= param_min,param_max |
---|
1784 | is_modified = True |
---|
1785 | |
---|
1786 | else: |
---|
1787 | self.model.details [name] = ["",param_min,param_max] |
---|
1788 | is_modified = True |
---|
1789 | try: |
---|
1790 | value= float(item[2].GetValue()) |
---|
1791 | |
---|
1792 | # If the value of the parameter has changed, |
---|
1793 | # +update the model and set the is_modified flag |
---|
1794 | if value != self.model.getParam(name) and numpy.isfinite(value): |
---|
1795 | self.model.setParam(name,value) |
---|
1796 | is_modified = True |
---|
1797 | except: |
---|
1798 | msg = "Wrong Fit parameter value entered " |
---|
1799 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
1800 | |
---|
1801 | return is_modified |
---|
1802 | |
---|
1803 | |
---|
1804 | def _set_dipers_Param(self, event): |
---|
1805 | """ |
---|
1806 | respond to self.enable_disp and self.disable_disp radio box. |
---|
1807 | The dispersity object is reset inside the model into Gaussian. |
---|
1808 | When the user select yes , this method display a combo box for more selection |
---|
1809 | when the user selects No,the combo box disappears. |
---|
1810 | Redraw the model with the default dispersity (Gaussian) |
---|
1811 | """ |
---|
1812 | if self.check_invalid_panel(): |
---|
1813 | return |
---|
1814 | self._reset_dispersity() |
---|
1815 | |
---|
1816 | if self.model ==None: |
---|
1817 | self.model_disp.Hide() |
---|
1818 | self.disp_box.Hide() |
---|
1819 | self.sizer4_4.Clear(True) |
---|
1820 | return |
---|
1821 | |
---|
1822 | if self.enable_disp.GetValue(): |
---|
1823 | self.model_disp.Show(True) |
---|
1824 | self.disp_box.Show(True) |
---|
1825 | ## layout for model containing no dispersity parameters |
---|
1826 | |
---|
1827 | self.disp_list= self.model.getDispParamList() |
---|
1828 | |
---|
1829 | if len(self.disp_list)==0 and len(self.disp_cb_dict)==0: |
---|
1830 | self._layout_sizer_noDipers() |
---|
1831 | else: |
---|
1832 | ## set gaussian sizer |
---|
1833 | self._on_select_Disp(event=None) |
---|
1834 | else: |
---|
1835 | self.model_disp.Hide() |
---|
1836 | self.disp_box.Hide() |
---|
1837 | self.disp_box.SetSelection(0) |
---|
1838 | self.sizer4_4.Clear(True) |
---|
1839 | |
---|
1840 | ## post state to fit panel |
---|
1841 | self.save_current_state() |
---|
1842 | if event !=None: |
---|
1843 | #self._undo.Enable(True) |
---|
1844 | event = PageInfoEvent(page = self) |
---|
1845 | wx.PostEvent(self.parent, event) |
---|
1846 | #draw the model with the current dispersity |
---|
1847 | self._draw_model() |
---|
1848 | self.sizer4_4.Layout() |
---|
1849 | self.sizer5.Layout() |
---|
1850 | self.Layout() |
---|
1851 | self.Refresh() |
---|
1852 | |
---|
1853 | |
---|
1854 | def _layout_sizer_noDipers(self): |
---|
1855 | """ |
---|
1856 | Draw a sizer with no dispersity info |
---|
1857 | """ |
---|
1858 | ix=0 |
---|
1859 | iy=1 |
---|
1860 | self.fittable_param=[] |
---|
1861 | self.fixed_param=[] |
---|
1862 | self.orientation_params_disp=[] |
---|
1863 | |
---|
1864 | self.model_disp.Hide() |
---|
1865 | self.disp_box.Hide() |
---|
1866 | self.sizer4_4.Clear(True) |
---|
1867 | model_disp = wx.StaticText(self, -1, 'No PolyDispersity for this model') |
---|
1868 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1869 | self.sizer4_4.Layout() |
---|
1870 | self.sizer4.Layout() |
---|
1871 | self.SetScrollbars(20,20,25,65) |
---|
1872 | |
---|
1873 | |
---|
1874 | def _reset_dispersity(self): |
---|
1875 | """ |
---|
1876 | put gaussian dispersity into current model |
---|
1877 | """ |
---|
1878 | if len(self.param_toFit)>0: |
---|
1879 | for item in self.fittable_param: |
---|
1880 | if item in self.param_toFit: |
---|
1881 | self.param_toFit.remove(item) |
---|
1882 | |
---|
1883 | for item in self.orientation_params_disp: |
---|
1884 | if item in self.param_toFit: |
---|
1885 | self.param_toFit.remove(item) |
---|
1886 | |
---|
1887 | self.fittable_param=[] |
---|
1888 | self.fixed_param=[] |
---|
1889 | self.orientation_params_disp=[] |
---|
1890 | self.values=[] |
---|
1891 | self.weights=[] |
---|
1892 | |
---|
1893 | from sans.models.dispersion_models import GaussianDispersion, ArrayDispersion |
---|
1894 | if len(self.disp_cb_dict)==0: |
---|
1895 | self.save_current_state() |
---|
1896 | self.sizer4_4.Clear(True) |
---|
1897 | self.Layout() |
---|
1898 | |
---|
1899 | return |
---|
1900 | if (len(self.disp_cb_dict)>0) : |
---|
1901 | for p in self.disp_cb_dict: |
---|
1902 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
1903 | disp_model = GaussianDispersion() |
---|
1904 | |
---|
1905 | self._disp_obj_dict[p] = disp_model |
---|
1906 | # Set the new model as the dispersion object for the selected parameter |
---|
1907 | try: |
---|
1908 | self.model.set_dispersion(p, disp_model) |
---|
1909 | except: |
---|
1910 | |
---|
1911 | pass |
---|
1912 | |
---|
1913 | ## save state into |
---|
1914 | self.save_current_state() |
---|
1915 | self.Layout() |
---|
1916 | self.Refresh() |
---|
1917 | |
---|
1918 | |
---|
1919 | def _on_select_Disp(self,event): |
---|
1920 | """ |
---|
1921 | allow selecting different dispersion |
---|
1922 | self.disp_list should change type later .now only gaussian |
---|
1923 | """ |
---|
1924 | n = self.disp_box.GetCurrentSelection() |
---|
1925 | name = self.disp_box.GetValue() |
---|
1926 | dispersity= self.disp_box.GetClientData(n) |
---|
1927 | self.disp_name = name |
---|
1928 | |
---|
1929 | if name.lower() == "array": |
---|
1930 | self._set_sizer_arraydispersion() |
---|
1931 | else: |
---|
1932 | self._set_sizer_dispersion(dispersity= dispersity) |
---|
1933 | |
---|
1934 | self.state.disp_box= n |
---|
1935 | ## Redraw the model |
---|
1936 | self._draw_model() |
---|
1937 | #self._undo.Enable(True) |
---|
1938 | event = PageInfoEvent(page = self) |
---|
1939 | wx.PostEvent(self.parent, event) |
---|
1940 | |
---|
1941 | self.sizer4_4.Layout() |
---|
1942 | self.sizer4.Layout() |
---|
1943 | self.SetScrollbars(20,20,25,65) |
---|
1944 | |
---|
1945 | def _set_sizer_arraydispersion(self): |
---|
1946 | """ |
---|
1947 | draw sizer with array dispersity parameters |
---|
1948 | """ |
---|
1949 | |
---|
1950 | if len(self.param_toFit)>0: |
---|
1951 | for item in self.fittable_param: |
---|
1952 | if item in self.param_toFit: |
---|
1953 | self.param_toFit.remove(item) |
---|
1954 | for item in self.orientation_params_disp: |
---|
1955 | if item in self.param_toFit: |
---|
1956 | self.param_toFit.remove(item) |
---|
1957 | for item in self.model.details.keys(): |
---|
1958 | if item in self.model.fixed: |
---|
1959 | del self.model.details [item] |
---|
1960 | |
---|
1961 | self.fittable_param=[] |
---|
1962 | self.fixed_param=[] |
---|
1963 | self.orientation_params_disp=[] |
---|
1964 | self.sizer4_4.Clear(True) |
---|
1965 | self._reset_dispersity() |
---|
1966 | ix=0 |
---|
1967 | iy=1 |
---|
1968 | disp1 = wx.StaticText(self, -1, 'Array Dispersion') |
---|
1969 | self.sizer4_4.Add(disp1,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1970 | |
---|
1971 | # Look for model parameters to which we can apply an ArrayDispersion model |
---|
1972 | # Add a check box for each parameter. |
---|
1973 | self.disp_cb_dict = {} |
---|
1974 | ix+=1 |
---|
1975 | self.noDisper_rbox = wx.RadioButton(self, -1,"None", (10, 10),style= wx.RB_GROUP) |
---|
1976 | self.Bind(wx.EVT_RADIOBUTTON,self.select_disp_angle , id=self.noDisper_rbox.GetId()) |
---|
1977 | #MAC needs SetValue |
---|
1978 | self.noDisper_rbox.SetValue(True) |
---|
1979 | self.sizer4_4.Add(self.noDisper_rbox, (iy, ix), |
---|
1980 | (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1981 | |
---|
1982 | for p in self.model.dispersion.keys(): |
---|
1983 | if not p in self.model.orientation_params: |
---|
1984 | ix+=1 |
---|
1985 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
1986 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
1987 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, id=self.disp_cb_dict[p].GetId()) |
---|
1988 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1989 | |
---|
1990 | for p in self.model.dispersion.keys(): |
---|
1991 | if p in self.model.orientation_params: |
---|
1992 | ix+=1 |
---|
1993 | self.disp_cb_dict[p] = wx.RadioButton(self, -1, p, (10, 10)) |
---|
1994 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
1995 | if not (self.enable2D or self.data.__class__.__name__ =="Data2D"): |
---|
1996 | self.disp_cb_dict[p].Hide() |
---|
1997 | else: |
---|
1998 | self.disp_cb_dict[p].Show(True) |
---|
1999 | self.Bind(wx.EVT_RADIOBUTTON, self.select_disp_angle, id=self.disp_cb_dict[p].GetId()) |
---|
2000 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2001 | |
---|
2002 | |
---|
2003 | ix =0 |
---|
2004 | iy +=1 |
---|
2005 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2006 | self.Layout() |
---|
2007 | |
---|
2008 | self.state.orientation_params =[] |
---|
2009 | self.state.orientation_params_disp =[] |
---|
2010 | self.state.parameters =[] |
---|
2011 | self.state.fittable_param =[] |
---|
2012 | self.state.fixed_param =[] |
---|
2013 | |
---|
2014 | ## save checkbutton state and txtcrtl values |
---|
2015 | |
---|
2016 | self._copy_parameters_state(self.orientation_params, |
---|
2017 | self.state.orientation_params) |
---|
2018 | |
---|
2019 | self._copy_parameters_state(self.orientation_params_disp, |
---|
2020 | self.state.orientation_params_disp) |
---|
2021 | |
---|
2022 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
2023 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
2024 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
2025 | |
---|
2026 | |
---|
2027 | ## post state to fit panel |
---|
2028 | event = PageInfoEvent(page = self) |
---|
2029 | wx.PostEvent(self.parent, event) |
---|
2030 | |
---|
2031 | |
---|
2032 | |
---|
2033 | |
---|
2034 | |
---|
2035 | def _set_range_sizer(self, title, box_sizer=None, object1=None,object=None): |
---|
2036 | """ |
---|
2037 | Fill the Q range sizer |
---|
2038 | """ |
---|
2039 | #2D data? default |
---|
2040 | is_2Ddata = False |
---|
2041 | |
---|
2042 | #check if it is 2D data |
---|
2043 | if self.data.__class__.__name__ == 'Data2D': |
---|
2044 | is_2Ddata = True |
---|
2045 | |
---|
2046 | self.sizer5.Clear(True) |
---|
2047 | #-------------------------------------------------------------- |
---|
2048 | if box_sizer == None: |
---|
2049 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
2050 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2051 | else: |
---|
2052 | #for MAC |
---|
2053 | boxsizer1 = box_sizer |
---|
2054 | |
---|
2055 | self.qmin = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20),style=wx.TE_PROCESS_ENTER, |
---|
2056 | text_enter_callback = self._onparamEnter) |
---|
2057 | self.qmin.SetValue(str(self.qmin_x)) |
---|
2058 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
2059 | |
---|
2060 | self.qmax = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20),style=wx.TE_PROCESS_ENTER, |
---|
2061 | text_enter_callback = self._onparamEnter) |
---|
2062 | self.qmax.SetValue(str(self.qmax_x)) |
---|
2063 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
2064 | |
---|
2065 | id = wx.NewId() |
---|
2066 | self.reset_qrange =wx.Button(self,id,'Reset',size=(77,20)) |
---|
2067 | |
---|
2068 | self.reset_qrange.Bind(wx.EVT_BUTTON, self.on_reset_clicked,id=id) |
---|
2069 | self.reset_qrange.SetToolTipString("Reset Q range to the default values") |
---|
2070 | |
---|
2071 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
2072 | sizer= wx.GridSizer(2, 4,2, 6) |
---|
2073 | |
---|
2074 | self.btEditMask = wx.Button(self,wx.NewId(),'Editor', size=(88,23)) |
---|
2075 | self.btEditMask.Bind(wx.EVT_BUTTON, self._onMask,id= self.btEditMask.GetId()) |
---|
2076 | self.btEditMask.SetToolTipString("Edit Mask.") |
---|
2077 | self.EditMask_title = wx.StaticText(self, -1, ' Masking(2D)') |
---|
2078 | |
---|
2079 | sizer.Add(wx.StaticText(self, -1, ' Q range')) |
---|
2080 | sizer.Add(wx.StaticText(self, -1, ' Min[1/A]')) |
---|
2081 | sizer.Add(wx.StaticText(self, -1, ' Max[1/A]')) |
---|
2082 | sizer.Add(self.EditMask_title) |
---|
2083 | sizer.Add(self.reset_qrange) |
---|
2084 | |
---|
2085 | sizer.Add(self.qmin) |
---|
2086 | sizer.Add(self.qmax) |
---|
2087 | sizer.Add(self.btEditMask) |
---|
2088 | |
---|
2089 | if object1!=None: |
---|
2090 | |
---|
2091 | boxsizer1.Add(object1) |
---|
2092 | boxsizer1.Add((10,10)) |
---|
2093 | boxsizer1.Add(sizer) |
---|
2094 | if object!=None: |
---|
2095 | boxsizer1.Add((10,15)) |
---|
2096 | boxsizer1.Add(object) |
---|
2097 | if is_2Ddata: |
---|
2098 | self.btEditMask.Enable() |
---|
2099 | self.EditMask_title.Enable() |
---|
2100 | else: |
---|
2101 | self.btEditMask.Disable() |
---|
2102 | self.EditMask_title.Disable() |
---|
2103 | ## save state |
---|
2104 | self.save_current_state() |
---|
2105 | #---------------------------------------------------------------- |
---|
2106 | self.sizer5.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
2107 | self.sizer5.Layout() |
---|
2108 | |
---|
2109 | |
---|
2110 | def _fill_save_sizer(self): |
---|
2111 | """ |
---|
2112 | Draw the layout for saving option |
---|
2113 | """ |
---|
2114 | # Skipping save state functionality for release 0.9.0 |
---|
2115 | return |
---|
2116 | |
---|
2117 | self.sizer6.Clear(True) |
---|
2118 | box_description= wx.StaticBox(self, -1,"Save Model") |
---|
2119 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2120 | sizer_save = wx.BoxSizer(wx.HORIZONTAL) |
---|
2121 | |
---|
2122 | self.btSave_title = wx.StaticText(self, -1, 'Save the current Model') |
---|
2123 | self.btSave = wx.Button(self,wx.NewId(),'Save') |
---|
2124 | self.btSave.Bind(wx.EVT_BUTTON, self.onSave,id= self.btSave.GetId()) |
---|
2125 | self.btSave.SetToolTipString("Save the current Model") |
---|
2126 | |
---|
2127 | sizer_save.Add(self.btSave_title) |
---|
2128 | sizer_save.Add((20,20),0, wx.LEFT|wx.RIGHT|wx.EXPAND,45) |
---|
2129 | |
---|
2130 | sizer_save.Add(self.btSave) |
---|
2131 | |
---|
2132 | boxsizer1.Add(sizer_save) |
---|
2133 | self.sizer6.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
2134 | self.sizer6.Layout() |
---|
2135 | self.SetScrollbars(20,20,25,65) |
---|
2136 | |
---|
2137 | def _lay_out(self): |
---|
2138 | """ |
---|
2139 | returns self.Layout |
---|
2140 | Note: Mac seems to like this better when self.Layout is called after fitting. |
---|
2141 | """ |
---|
2142 | self._sleep4sec() |
---|
2143 | self.Layout() |
---|
2144 | #self._sleep4sec() |
---|
2145 | return |
---|
2146 | |
---|
2147 | def _sleep4sec(self): |
---|
2148 | """ |
---|
2149 | sleep for 1 sec only applied on Mac |
---|
2150 | Note: This 1sec helps for Mac not to crash on self.:ayout after self._draw_model |
---|
2151 | """ |
---|
2152 | if ON_MAC == True: |
---|
2153 | time.sleep(1) |
---|
2154 | |
---|
2155 | def on_reset_clicked(self,event): |
---|
2156 | """ |
---|
2157 | #On 'Reset' button for Q range clicked |
---|
2158 | """ |
---|
2159 | flag = True |
---|
2160 | if self.check_invalid_panel(): |
---|
2161 | return |
---|
2162 | ##For 3 different cases: Data2D, Data1D, and theory |
---|
2163 | if self.data.__class__.__name__ == "Data2D": |
---|
2164 | data_min= 0 |
---|
2165 | x= max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
2166 | y= max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
2167 | self.qmin_x = data_min |
---|
2168 | self.qmax_x = math.sqrt(x*x + y*y) |
---|
2169 | elif self.data.__class__.__name__ != "Data2D": |
---|
2170 | self.qmin_x = min(self.data.x) |
---|
2171 | self.qmax_x = max(self.data.x) |
---|
2172 | # check smearing |
---|
2173 | if not self.disable_smearer.GetValue(): |
---|
2174 | temp_smearer= self.current_smearer |
---|
2175 | ## set smearing value whether or not the data contain the smearing info |
---|
2176 | if self.slit_smearer.GetValue(): |
---|
2177 | flag = self.update_slit_smear() |
---|
2178 | elif self.pinhole_smearer.GetValue(): |
---|
2179 | flag = self.update_pinhole_smear() |
---|
2180 | else: |
---|
2181 | flag = True |
---|
2182 | else: |
---|
2183 | self.qmin_x = _QMIN_DEFAULT |
---|
2184 | self.qmax_x = _QMAX_DEFAULT |
---|
2185 | self.num_points = _NPTS_DEFAULT |
---|
2186 | self.state.npts = self.num_points |
---|
2187 | |
---|
2188 | if flag == False: |
---|
2189 | msg= "Cannot Plot :Must enter a number!!! " |
---|
2190 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2191 | else: |
---|
2192 | # set relative text ctrs. |
---|
2193 | self.qmin.SetValue(str(self.qmin_x)) |
---|
2194 | self.qmax.SetValue(str(self.qmax_x)) |
---|
2195 | self.set_npts2fit() |
---|
2196 | # At this point, some button and variables satatus (disabled?) should be checked |
---|
2197 | # such as color that should be reset to white in case that it was pink. |
---|
2198 | self._onparamEnter_helper() |
---|
2199 | |
---|
2200 | |
---|
2201 | self.save_current_state() |
---|
2202 | self.state.qmin = self.qmin_x |
---|
2203 | self.state.qmax = self.qmax_x |
---|
2204 | |
---|
2205 | #reset the q range values |
---|
2206 | self._reset_plotting_range(self.state) |
---|
2207 | #Re draw plot |
---|
2208 | self._draw_model() |
---|
2209 | |
---|
2210 | def on_model_help_clicked(self,event): |
---|
2211 | """ |
---|
2212 | #On 'More details' button |
---|
2213 | """ |
---|
2214 | from help_panel import HelpWindow |
---|
2215 | |
---|
2216 | if self.model == None: |
---|
2217 | name = 'FuncHelp' |
---|
2218 | else: |
---|
2219 | name = self.model.origin_name |
---|
2220 | |
---|
2221 | frame = HelpWindow(None, -1, pageToOpen="media/model_functions.html") |
---|
2222 | frame.Show(True) |
---|
2223 | if frame.rhelp.HasAnchor(name): |
---|
2224 | frame.rhelp.ScrollToAnchor(name) |
---|
2225 | else: |
---|
2226 | msg= "Model does not contains an available description " |
---|
2227 | msg +="Please try searching in the Help window" |
---|
2228 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2229 | |
---|