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