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