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