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