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