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