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