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