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