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