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