1 | |
---|
2 | import sys |
---|
3 | import os |
---|
4 | import wx |
---|
5 | import numpy |
---|
6 | import time |
---|
7 | import copy |
---|
8 | import math |
---|
9 | import string |
---|
10 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
11 | from sans.guiframe.panel_base import PanelBase |
---|
12 | from sans.guiframe.utils import format_number,check_float |
---|
13 | from sans.guiframe.events import PanelOnFocusEvent |
---|
14 | from sans.guiframe.events import StatusEvent |
---|
15 | from sans.guiframe.events import AppendBookmarkEvent |
---|
16 | from sans.guiframe.dataFitting import Data2D |
---|
17 | from sans.guiframe.dataFitting import Data1D |
---|
18 | from sans.guiframe.dataFitting import check_data_validity |
---|
19 | from sans.dataloader.data_info import Detector |
---|
20 | from sans.dataloader.data_info import Source |
---|
21 | import pagestate |
---|
22 | from pagestate import PageState |
---|
23 | |
---|
24 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
25 | (PreviousStateEvent, EVT_PREVIOUS_STATE) = wx.lib.newevent.NewEvent() |
---|
26 | (NextStateEvent, EVT_NEXT_STATE) = wx.lib.newevent.NewEvent() |
---|
27 | |
---|
28 | _BOX_WIDTH = 76 |
---|
29 | _QMIN_DEFAULT = 0.0005 |
---|
30 | _QMAX_DEFAULT = 0.5 |
---|
31 | _NPTS_DEFAULT = 50 |
---|
32 | #Control panel width |
---|
33 | if sys.platform.count("win32")> 0: |
---|
34 | PANEL_WIDTH = 450 |
---|
35 | FONT_VARIANT = 0 |
---|
36 | ON_MAC = False |
---|
37 | else: |
---|
38 | PANEL_WIDTH = 500 |
---|
39 | FONT_VARIANT = 1 |
---|
40 | ON_MAC = True |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | class BasicPage(ScrolledPanel, PanelBase): |
---|
45 | """ |
---|
46 | This class provide general structure of fitpanel page |
---|
47 | """ |
---|
48 | ## Internal name for the AUI manager |
---|
49 | window_name = "Fit Page" |
---|
50 | ## Title to appear on top of the window |
---|
51 | window_caption = "Fit Page " |
---|
52 | |
---|
53 | def __init__(self, parent,color='blue', **kwargs): |
---|
54 | """ |
---|
55 | """ |
---|
56 | ScrolledPanel.__init__(self, parent, **kwargs) |
---|
57 | PanelBase.__init__(self, parent) |
---|
58 | self.SetupScrolling() |
---|
59 | #Set window's font size |
---|
60 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
61 | |
---|
62 | self.SetBackgroundColour(color) |
---|
63 | ## parent of the page |
---|
64 | self.parent = parent |
---|
65 | ## manager is the fitting plugin |
---|
66 | ## owner of the page (fitting plugin) |
---|
67 | self.event_owner = None |
---|
68 | ## current model |
---|
69 | self.model = None |
---|
70 | self.index_model = None |
---|
71 | ## data |
---|
72 | self.data = None |
---|
73 | #list of available data |
---|
74 | self.data_list = [] |
---|
75 | self.mask = None |
---|
76 | self.uid = wx.NewId() |
---|
77 | self.graph_id = None |
---|
78 | #Q range for data set |
---|
79 | self.qmin_data_set = numpy.inf |
---|
80 | self.qmax_data_set = None |
---|
81 | self.npts_data_set = 0 |
---|
82 | ## Q range |
---|
83 | self.qmin = None |
---|
84 | self.qmax = None |
---|
85 | self.qmax_x = _QMAX_DEFAULT |
---|
86 | self.qmin_x = _QMIN_DEFAULT |
---|
87 | self.npts_x = _NPTS_DEFAULT |
---|
88 | ## total number of point: float |
---|
89 | self.npts = None |
---|
90 | ## default fitengine type |
---|
91 | self.engine_type = 'scipy' |
---|
92 | ## smear default |
---|
93 | self.current_smearer = None |
---|
94 | ## 2D smear accuracy default |
---|
95 | self.smear2d_accuracy = 'Low' |
---|
96 | ## slit smear: |
---|
97 | self.dxl = None |
---|
98 | self.dxw = None |
---|
99 | ## pinhole smear |
---|
100 | self.dx_min = None |
---|
101 | self.dx_max = None |
---|
102 | |
---|
103 | self.disp_cb_dict = {} |
---|
104 | |
---|
105 | self.state = PageState(parent=parent) |
---|
106 | ## dictionary containing list of models |
---|
107 | self.model_list_box = {} |
---|
108 | |
---|
109 | ## Data member to store the dispersion object created |
---|
110 | self._disp_obj_dict = {} |
---|
111 | ## selected parameters to apply dispersion |
---|
112 | self.disp_cb_dict ={} |
---|
113 | ## smearer object |
---|
114 | self.enable2D = False |
---|
115 | self.is_mac = ON_MAC |
---|
116 | |
---|
117 | ##list of model parameters. each item must have same length |
---|
118 | ## each item related to a given parameters |
---|
119 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
120 | self.parameters = [] |
---|
121 | # non-fittable parameter whose value is astring |
---|
122 | self.str_parameters = [] |
---|
123 | ## list of parameters to fit , must be like self.parameters |
---|
124 | self.param_toFit = [] |
---|
125 | ## list of looking like parameters but with non fittable parameters info |
---|
126 | self.fixed_param = [] |
---|
127 | ## list of looking like parameters but with fittable parameters info |
---|
128 | self.fittable_param = [] |
---|
129 | ##list of dispersion parameters |
---|
130 | self.disp_list = [] |
---|
131 | self.disp_name = "" |
---|
132 | |
---|
133 | ## list of orientation parameters |
---|
134 | self.orientation_params = [] |
---|
135 | self.orientation_params_disp = [] |
---|
136 | if self.model != None: |
---|
137 | self.disp_list = self.model.getDispParamList() |
---|
138 | self.temp_multi_functional = False |
---|
139 | ##enable model 2D draw |
---|
140 | self.enable2D = False |
---|
141 | ## check that the fit range is correct to plot the model again |
---|
142 | self.fitrange = True |
---|
143 | ## Create memento to save the current state |
---|
144 | self.state = PageState(parent=self.parent, |
---|
145 | model=self.model, data=self.data) |
---|
146 | ## flag to determine if state has change |
---|
147 | self.state_change = False |
---|
148 | ## save customized array |
---|
149 | self.values = [] |
---|
150 | self.weights = [] |
---|
151 | ## retrieve saved state |
---|
152 | self.number_saved_state = 0 |
---|
153 | ## dictionary of saved state |
---|
154 | self.saved_states = {} |
---|
155 | ## Create context menu for page |
---|
156 | self.popUpMenu = wx.Menu() |
---|
157 | |
---|
158 | id = wx.NewId() |
---|
159 | self._keep = wx.MenuItem(self.popUpMenu,id,"Add bookmark", |
---|
160 | " Keep the panel status to recall it later") |
---|
161 | self.popUpMenu.AppendItem(self._keep) |
---|
162 | self._keep.Enable(False) |
---|
163 | self._set_bookmark_flag(False) |
---|
164 | self._set_save_flag(False) |
---|
165 | wx.EVT_MENU(self, id, self.on_bookmark) |
---|
166 | self.popUpMenu.AppendSeparator() |
---|
167 | |
---|
168 | ## Default locations |
---|
169 | self._default_save_location = os.getcwd() |
---|
170 | ## save initial state on context menu |
---|
171 | #self.onSave(event=None) |
---|
172 | self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) |
---|
173 | |
---|
174 | # bind key event |
---|
175 | self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down) |
---|
176 | |
---|
177 | ## create the basic structure of the panel with empty sizer |
---|
178 | self.define_page_structure() |
---|
179 | ## drawing Initial dispersion parameters sizer |
---|
180 | self.set_dispers_sizer() |
---|
181 | |
---|
182 | ## layout |
---|
183 | self.set_layout() |
---|
184 | |
---|
185 | def set_index_model(self, index): |
---|
186 | """ |
---|
187 | Index related to this page |
---|
188 | """ |
---|
189 | self.index_model = index |
---|
190 | |
---|
191 | def create_default_data(self): |
---|
192 | """ |
---|
193 | Given the user selection, creates a 1D or 2D data |
---|
194 | Only when the page is on theory mode. |
---|
195 | """ |
---|
196 | if not hasattr(self, "model_view"): |
---|
197 | return |
---|
198 | toggle_mode_on = self.model_view.IsEnabled() |
---|
199 | if toggle_mode_on: |
---|
200 | if self.enable2D and not check_data_validity(self.data): |
---|
201 | self._create_default_2d_data() |
---|
202 | else: |
---|
203 | self._create_default_1d_data() |
---|
204 | |
---|
205 | def _create_default_1d_data(self): |
---|
206 | """ |
---|
207 | Create default data for fitting perspective |
---|
208 | Only when the page is on theory mode. |
---|
209 | :warning: This data is never plotted. |
---|
210 | |
---|
211 | """ |
---|
212 | x = numpy.linspace(start=self.qmin_x, stop=self.qmax_x, |
---|
213 | num=self.npts_x, endpoint=True) |
---|
214 | self.data = Data1D(x=x) |
---|
215 | self.data.xaxis('\\rm{Q}',"A^{-1}") |
---|
216 | self.data.yaxis('\\rm{Intensity}', "cm^{-1}") |
---|
217 | self.data.is_data = False |
---|
218 | self.data.id = str(self.uid) + " data" |
---|
219 | self.data.group_id = str(self.uid) + " Model1D" |
---|
220 | |
---|
221 | def _create_default_2d_data(self): |
---|
222 | """ |
---|
223 | Create 2D data by default |
---|
224 | Only when the page is on theory mode. |
---|
225 | :warning: This data is never plotted. |
---|
226 | """ |
---|
227 | self.data = Data2D() |
---|
228 | qmax = self.qmax_x / math.sqrt(2) |
---|
229 | self.data.xaxis('\\rm{Q_{x}}', 'A^{-1}') |
---|
230 | self.data.yaxis('\\rm{Q_{y}}', 'A^{-1}') |
---|
231 | self.data.is_data = False |
---|
232 | self.data.id = str(self.uid) + " data" |
---|
233 | self.data.group_id = str(self.uid) + " Model2D" |
---|
234 | ## Default values |
---|
235 | self.data.detector.append(Detector()) |
---|
236 | index = len(self.data.detector) - 1 |
---|
237 | self.data.detector[index].distance = 8000 # mm |
---|
238 | self.data.source.wavelength= 6 # A |
---|
239 | self.data.detector[index].pixel_size.x = 5 # mm |
---|
240 | self.data.detector[index].pixel_size.y = 5 # mm |
---|
241 | self.data.detector[index].beam_center.x = qmax |
---|
242 | self.data.detector[index].beam_center.y = qmax |
---|
243 | ## create x_bins and y_bins of the model 2D |
---|
244 | pixel_width_x = self.data.detector[index].pixel_size.x |
---|
245 | pixel_width_y = self.data.detector[index].pixel_size.y |
---|
246 | center_x = self.data.detector[index].beam_center.x/pixel_width_x |
---|
247 | center_y = self.data.detector[index].beam_center.y/pixel_width_y |
---|
248 | # theory default: assume the beam |
---|
249 | #center is located at the center of sqr detector |
---|
250 | xmax = qmax |
---|
251 | xmin = -qmax |
---|
252 | ymax = qmax |
---|
253 | ymin = -qmax |
---|
254 | qstep = self.npts_x |
---|
255 | |
---|
256 | x = numpy.linspace(start=xmin, stop=xmax, num=qstep, endpoint=True) |
---|
257 | y = numpy.linspace(start=ymin, stop=ymax, num=qstep, endpoint=True) |
---|
258 | ## use data info instead |
---|
259 | new_x = numpy.tile(x, (len(y), 1)) |
---|
260 | new_y = numpy.tile(y, (len(x), 1)) |
---|
261 | new_y = new_y.swapaxes(0,1) |
---|
262 | # all data reuire now in 1d array |
---|
263 | qx_data = new_x.flatten() |
---|
264 | qy_data = new_y.flatten() |
---|
265 | q_data = numpy.sqrt(qx_data*qx_data + qy_data*qy_data) |
---|
266 | # set all True (standing for unmasked) as default |
---|
267 | mask = numpy.ones(len(qx_data), dtype=bool) |
---|
268 | # calculate the range of qx and qy: this way, |
---|
269 | # it is a little more independent |
---|
270 | x_size = xmax - xmin |
---|
271 | y_size = ymax - ymin |
---|
272 | # store x and y bin centers in q space |
---|
273 | x_bins = x |
---|
274 | y_bins = y |
---|
275 | # bin size: x- & y-directions |
---|
276 | xstep = x_size/len(x_bins-1) |
---|
277 | ystep = y_size/len(y_bins-1) |
---|
278 | |
---|
279 | self.data.source = Source() |
---|
280 | self.data.data = numpy.ones(len(mask)) |
---|
281 | self.data.err_data = numpy.ones(len(mask)) |
---|
282 | self.data.qx_data = qx_data |
---|
283 | self.data.qy_data = qy_data |
---|
284 | self.data.q_data = q_data |
---|
285 | self.data.mask = mask |
---|
286 | self.data.x_bins = x_bins |
---|
287 | self.data.y_bins = y_bins |
---|
288 | # max and min taking account of the bin sizes |
---|
289 | self.data.xmin = xmin |
---|
290 | self.data.xmax = xmax |
---|
291 | self.data.ymin = ymin |
---|
292 | self.data.ymax = ymax |
---|
293 | |
---|
294 | def on_set_focus(self, event): |
---|
295 | """ |
---|
296 | On Set Focus, update guimanger and menu |
---|
297 | """ |
---|
298 | if self._manager is not None: |
---|
299 | wx.PostEvent(self._manager.parent, PanelOnFocusEvent(panel=self)) |
---|
300 | self.on_tap_focus() |
---|
301 | |
---|
302 | def on_tap_focus(self): |
---|
303 | """ |
---|
304 | Update menu1 on cliking the page tap |
---|
305 | """ |
---|
306 | if self._manager.menu1 != None: |
---|
307 | chain_menu = self._manager.menu1.FindItemById(\ |
---|
308 | self._manager.id_reset_flag) |
---|
309 | chain_menu.Enable(self.batch_on) |
---|
310 | sim_menu = self._manager.menu1.FindItemById(self._manager.id_simfit) |
---|
311 | sim_menu.Enable(not self.batch_on and self.data.is_data\ |
---|
312 | and (self.model!=None)) |
---|
313 | |
---|
314 | class ModelTextCtrl(wx.TextCtrl): |
---|
315 | """ |
---|
316 | Text control for model and fit parameters. |
---|
317 | Binds the appropriate events for user interactions. |
---|
318 | Default callback methods can be overwritten on initialization |
---|
319 | |
---|
320 | :param kill_focus_callback: callback method for EVT_KILL_FOCUS event |
---|
321 | :param set_focus_callback: callback method for EVT_SET_FOCUS event |
---|
322 | :param mouse_up_callback: callback method for EVT_LEFT_UP event |
---|
323 | :param text_enter_callback: callback method for EVT_TEXT_ENTER event |
---|
324 | |
---|
325 | """ |
---|
326 | ## Set to True when the mouse is clicked while the whole string is selected |
---|
327 | full_selection = False |
---|
328 | ## Call back for EVT_SET_FOCUS events |
---|
329 | _on_set_focus_callback = None |
---|
330 | |
---|
331 | def __init__(self, parent, id=-1, |
---|
332 | value=wx.EmptyString, |
---|
333 | pos=wx.DefaultPosition, |
---|
334 | size=wx.DefaultSize, |
---|
335 | style=0, |
---|
336 | validator=wx.DefaultValidator, |
---|
337 | name=wx.TextCtrlNameStr, |
---|
338 | kill_focus_callback=None, |
---|
339 | set_focus_callback=None, |
---|
340 | mouse_up_callback=None, |
---|
341 | text_enter_callback = None): |
---|
342 | |
---|
343 | wx.TextCtrl.__init__(self, parent, id, value, pos, |
---|
344 | size, style, validator, name) |
---|
345 | |
---|
346 | # Bind appropriate events |
---|
347 | self._on_set_focus_callback = parent.onSetFocus \ |
---|
348 | if set_focus_callback is None else set_focus_callback |
---|
349 | self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus) |
---|
350 | self.Bind(wx.EVT_KILL_FOCUS, self._silent_kill_focus \ |
---|
351 | if kill_focus_callback is None else kill_focus_callback) |
---|
352 | self.Bind(wx.EVT_TEXT_ENTER, parent._onparamEnter \ |
---|
353 | if text_enter_callback is None else text_enter_callback) |
---|
354 | if not ON_MAC : |
---|
355 | self.Bind(wx.EVT_LEFT_UP, self._highlight_text \ |
---|
356 | if mouse_up_callback is None else mouse_up_callback) |
---|
357 | |
---|
358 | def _on_set_focus(self, event): |
---|
359 | """ |
---|
360 | Catch when the text control is set in focus to highlight the whole |
---|
361 | text if necessary |
---|
362 | |
---|
363 | :param event: mouse event |
---|
364 | |
---|
365 | """ |
---|
366 | event.Skip() |
---|
367 | self.full_selection = True |
---|
368 | return self._on_set_focus_callback(event) |
---|
369 | |
---|
370 | |
---|
371 | |
---|
372 | def _highlight_text(self, event): |
---|
373 | """ |
---|
374 | Highlight text of a TextCtrl only of no text has be selected |
---|
375 | |
---|
376 | :param event: mouse event |
---|
377 | |
---|
378 | """ |
---|
379 | # Make sure the mouse event is available to other listeners |
---|
380 | event.Skip() |
---|
381 | control = event.GetEventObject() |
---|
382 | if self.full_selection: |
---|
383 | self.full_selection = False |
---|
384 | # Check that we have a TextCtrl |
---|
385 | if issubclass(control.__class__, wx.TextCtrl): |
---|
386 | # Check whether text has been selected, |
---|
387 | # if not, select the whole string |
---|
388 | (start, end) = control.GetSelection() |
---|
389 | if start==end: |
---|
390 | control.SetSelection(-1,-1) |
---|
391 | |
---|
392 | def _silent_kill_focus(self,event): |
---|
393 | """ |
---|
394 | Save the state of the page |
---|
395 | """ |
---|
396 | |
---|
397 | event.Skip() |
---|
398 | pass |
---|
399 | |
---|
400 | def set_page_info(self, page_info): |
---|
401 | """ |
---|
402 | set some page important information at once |
---|
403 | """ |
---|
404 | ##window_name |
---|
405 | self.window_name = page_info.window_name |
---|
406 | ##window_caption |
---|
407 | self.window_caption = page_info.window_caption |
---|
408 | ## manager is the fitting plugin |
---|
409 | self._manager= page_info.manager |
---|
410 | ## owner of the page (fitting plugin) |
---|
411 | self.event_owner= page_info.event_owner |
---|
412 | ## current model |
---|
413 | self.model = page_info.model |
---|
414 | ## data |
---|
415 | self.data = page_info.data |
---|
416 | ## dictionary containing list of models |
---|
417 | self.model_list_box = page_info.model_list_box |
---|
418 | ## Data member to store the dispersion object created |
---|
419 | self.populate_box(dict=self.model_list_box) |
---|
420 | |
---|
421 | def onContextMenu(self, event): |
---|
422 | """ |
---|
423 | Retrieve the state selected state |
---|
424 | """ |
---|
425 | # Skipping the save state functionality for release 0.9.0 |
---|
426 | #return |
---|
427 | |
---|
428 | pos = event.GetPosition() |
---|
429 | pos = self.ScreenToClient(pos) |
---|
430 | |
---|
431 | self.PopupMenu(self.popUpMenu, pos) |
---|
432 | |
---|
433 | |
---|
434 | def onUndo(self, event): |
---|
435 | """ |
---|
436 | Cancel the previous action |
---|
437 | """ |
---|
438 | event = PreviousStateEvent(page = self) |
---|
439 | wx.PostEvent(self.parent, event) |
---|
440 | |
---|
441 | def onRedo(self, event): |
---|
442 | """ |
---|
443 | Restore the previous action cancelled |
---|
444 | """ |
---|
445 | event = NextStateEvent(page= self) |
---|
446 | wx.PostEvent(self.parent, event) |
---|
447 | |
---|
448 | def define_page_structure(self): |
---|
449 | """ |
---|
450 | Create empty sizer for a panel |
---|
451 | """ |
---|
452 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
453 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
454 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
455 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
456 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
457 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
458 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
459 | self.sizer6 = wx.BoxSizer(wx.VERTICAL) |
---|
460 | |
---|
461 | self.sizer0.SetMinSize((PANEL_WIDTH,-1)) |
---|
462 | self.sizer1.SetMinSize((PANEL_WIDTH,-1)) |
---|
463 | self.sizer2.SetMinSize((PANEL_WIDTH,-1)) |
---|
464 | self.sizer3.SetMinSize((PANEL_WIDTH,-1)) |
---|
465 | self.sizer4.SetMinSize((PANEL_WIDTH,-1)) |
---|
466 | self.sizer5.SetMinSize((PANEL_WIDTH,-1)) |
---|
467 | self.sizer6.SetMinSize((PANEL_WIDTH,-1)) |
---|
468 | |
---|
469 | self.vbox.Add(self.sizer0) |
---|
470 | self.vbox.Add(self.sizer1) |
---|
471 | self.vbox.Add(self.sizer2) |
---|
472 | self.vbox.Add(self.sizer3) |
---|
473 | self.vbox.Add(self.sizer4) |
---|
474 | self.vbox.Add(self.sizer5) |
---|
475 | self.vbox.Add(self.sizer6) |
---|
476 | |
---|
477 | def set_layout(self): |
---|
478 | """ |
---|
479 | layout |
---|
480 | """ |
---|
481 | self.vbox.Layout() |
---|
482 | self.vbox.Fit(self) |
---|
483 | self.SetSizer(self.vbox) |
---|
484 | self.Centre() |
---|
485 | |
---|
486 | def set_owner(self,owner): |
---|
487 | """ |
---|
488 | set owner of fitpage |
---|
489 | |
---|
490 | :param owner: the class responsible of plotting |
---|
491 | |
---|
492 | """ |
---|
493 | self.event_owner = owner |
---|
494 | self.state.event_owner = owner |
---|
495 | |
---|
496 | def get_state(self): |
---|
497 | """ |
---|
498 | """ |
---|
499 | return self.state |
---|
500 | |
---|
501 | def get_data(self): |
---|
502 | """ |
---|
503 | return the current data |
---|
504 | """ |
---|
505 | return self.data |
---|
506 | |
---|
507 | def get_data_list(self): |
---|
508 | """ |
---|
509 | return the current data |
---|
510 | """ |
---|
511 | return self.data_list |
---|
512 | |
---|
513 | def set_manager(self, manager): |
---|
514 | """ |
---|
515 | set panel manager |
---|
516 | |
---|
517 | :param manager: instance of plugin fitting |
---|
518 | |
---|
519 | """ |
---|
520 | self._manager = manager |
---|
521 | self.state.manager = manager |
---|
522 | |
---|
523 | def populate_box(self, dict): |
---|
524 | """ |
---|
525 | Store list of model |
---|
526 | |
---|
527 | :param dict: dictionary containing list of models |
---|
528 | |
---|
529 | """ |
---|
530 | self.model_list_box = dict |
---|
531 | self.state.model_list_box = self.model_list_box |
---|
532 | self.initialize_combox() |
---|
533 | |
---|
534 | def initialize_combox(self): |
---|
535 | """ |
---|
536 | put default value in the combobox |
---|
537 | """ |
---|
538 | ## fill combox box |
---|
539 | if self.model_list_box is None: |
---|
540 | return |
---|
541 | if len(self.model_list_box) > 0: |
---|
542 | self._populate_box(self.formfactorbox, |
---|
543 | self.model_list_box["Shapes"]) |
---|
544 | |
---|
545 | if len(self.model_list_box) > 0: |
---|
546 | self._populate_box(self.structurebox, |
---|
547 | self.model_list_box["Structure Factors"]) |
---|
548 | self.structurebox.Insert("None", 0, None) |
---|
549 | self.structurebox.SetSelection(0) |
---|
550 | self.structurebox.Hide() |
---|
551 | self.text2.Hide() |
---|
552 | self.structurebox.Disable() |
---|
553 | self.text2.Disable() |
---|
554 | |
---|
555 | if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]: |
---|
556 | self.structurebox.Show() |
---|
557 | self.text2.Show() |
---|
558 | self.structurebox.Enable() |
---|
559 | self.text2.Enable() |
---|
560 | |
---|
561 | def set_dispers_sizer(self): |
---|
562 | """ |
---|
563 | fill sizer containing dispersity info |
---|
564 | """ |
---|
565 | self.sizer4.Clear(True) |
---|
566 | name="Polydispersity and Orientational Distribution" |
---|
567 | box_description= wx.StaticBox(self, -1,name) |
---|
568 | box_description.SetForegroundColour(wx.BLUE) |
---|
569 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
570 | #---------------------------------------------------- |
---|
571 | self.disable_disp = wx.RadioButton(self, -1, 'Off', (10, 10), |
---|
572 | style=wx.RB_GROUP) |
---|
573 | self.enable_disp = wx.RadioButton(self, -1, 'On', (10, 30)) |
---|
574 | # best size for MAC and PC |
---|
575 | if ON_MAC: |
---|
576 | size_q = (30, 20) |
---|
577 | else: |
---|
578 | size_q = (20, 15) |
---|
579 | self.disp_help_bt = wx.Button(self,wx.NewId(),'?', |
---|
580 | style = wx.BU_EXACTFIT, |
---|
581 | size=size_q) |
---|
582 | self.disp_help_bt.Bind(wx.EVT_BUTTON, |
---|
583 | self.on_pd_help_clicked,id= self.disp_help_bt.GetId()) |
---|
584 | self.disp_help_bt.SetToolTipString("Helps for Polydispersion.") |
---|
585 | |
---|
586 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
587 | id=self.disable_disp.GetId()) |
---|
588 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, |
---|
589 | id=self.enable_disp.GetId()) |
---|
590 | #MAC needs SetValue |
---|
591 | self.disable_disp.SetValue(True) |
---|
592 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
593 | sizer_dispersion.Add((20,20)) |
---|
594 | name=""#Polydispersity and \nOrientational Distribution " |
---|
595 | sizer_dispersion.Add(wx.StaticText(self,-1,name)) |
---|
596 | sizer_dispersion.Add(self.enable_disp ) |
---|
597 | sizer_dispersion.Add((20,20)) |
---|
598 | sizer_dispersion.Add(self.disable_disp ) |
---|
599 | sizer_dispersion.Add((25,20)) |
---|
600 | sizer_dispersion.Add(self.disp_help_bt) |
---|
601 | |
---|
602 | ## fill a sizer for dispersion |
---|
603 | boxsizer1.Add( sizer_dispersion,0, |
---|
604 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
605 | #boxsizer1.Add( (10,10) ) |
---|
606 | #boxsizer1.Add( sizer_select_dispers ) |
---|
607 | self.sizer4_4 = wx.GridBagSizer(6,5) |
---|
608 | |
---|
609 | boxsizer1.Add( self.sizer4_4 ) |
---|
610 | #----------------------------------------------------- |
---|
611 | self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
612 | self.sizer4_4.Layout() |
---|
613 | self.sizer4.Layout() |
---|
614 | self.Layout() |
---|
615 | |
---|
616 | self.Refresh() |
---|
617 | ## saving the state of enable dispersity button |
---|
618 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
619 | self.state.disable_disp= self.disable_disp.GetValue() |
---|
620 | self.SetupScrolling() |
---|
621 | |
---|
622 | |
---|
623 | def onResetModel(self, event): |
---|
624 | """ |
---|
625 | Reset model state |
---|
626 | """ |
---|
627 | menu = event.GetEventObject() |
---|
628 | ## post help message for the selected model |
---|
629 | msg = menu.GetHelpString(event.GetId()) |
---|
630 | msg +=" reloaded" |
---|
631 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
632 | self.Show(False) |
---|
633 | name = menu.GetLabel(event.GetId()) |
---|
634 | self._on_select_model_helper() |
---|
635 | if name in self.saved_states.keys(): |
---|
636 | previous_state = self.saved_states[name] |
---|
637 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
638 | |
---|
639 | self.reset_page(previous_state) |
---|
640 | self.Show(True) |
---|
641 | |
---|
642 | def on_preview(self, event): |
---|
643 | """ |
---|
644 | Report the current fit results |
---|
645 | """ |
---|
646 | # Get plot image from plotpanel |
---|
647 | images, canvases = self.get_images() |
---|
648 | # get the report dialog |
---|
649 | self.state.report(images, canvases) |
---|
650 | |
---|
651 | |
---|
652 | def on_save(self, event): |
---|
653 | """ |
---|
654 | Save the current state into file |
---|
655 | """ |
---|
656 | self.save_current_state() |
---|
657 | new_state = self.state.clone() |
---|
658 | # Ask the user the location of the file to write to. |
---|
659 | path = None |
---|
660 | if self.parent != None: |
---|
661 | self._default_save_location = \ |
---|
662 | self.parent.parent._default_save_location |
---|
663 | dlg = wx.FileDialog(self, "Choose a file", self._default_save_location, |
---|
664 | self.window_caption, "*.fitv", wx.SAVE) |
---|
665 | |
---|
666 | if dlg.ShowModal() == wx.ID_OK: |
---|
667 | path = dlg.GetPath() |
---|
668 | self._default_save_location = os.path.dirname(path) |
---|
669 | self.parent.parent._default_save_location =\ |
---|
670 | self._default_save_location |
---|
671 | else: |
---|
672 | return None |
---|
673 | # MAC always needs the extension for saving |
---|
674 | extens = ".fitv" |
---|
675 | # Make sure the ext included in the file name |
---|
676 | fName = os.path.splitext(path)[0] + extens |
---|
677 | #the manager write the state into file |
---|
678 | self._manager.save_fit_state(filepath=fName, fitstate=new_state) |
---|
679 | return new_state |
---|
680 | |
---|
681 | def on_copy(self, event): |
---|
682 | """ |
---|
683 | Copy Parameter values to the clipboad |
---|
684 | """ |
---|
685 | if event != None: |
---|
686 | event.Skip() |
---|
687 | # It seems MAC needs wxCallAfter |
---|
688 | wx.CallAfter(self.get_copy) |
---|
689 | |
---|
690 | # messages depending on the flag |
---|
691 | #self._copy_info(None) |
---|
692 | |
---|
693 | def on_paste(self, event): |
---|
694 | """ |
---|
695 | Paste Parameter values to the panel if possible |
---|
696 | """ |
---|
697 | if event != None: |
---|
698 | event.Skip() |
---|
699 | # It seems MAC needs wxCallAfter for the setvalues |
---|
700 | # for multiple textctrl items, otherwise it tends to crash once a while. |
---|
701 | wx.CallAfter(self.get_paste) |
---|
702 | # messages depending on the flag |
---|
703 | #self._copy_info(True) |
---|
704 | |
---|
705 | def _copy_info(self, flag): |
---|
706 | """ |
---|
707 | Send event dpemding on flag |
---|
708 | |
---|
709 | : Param flag: flag that distinguish event |
---|
710 | """ |
---|
711 | # messages depending on the flag |
---|
712 | if flag == None: |
---|
713 | msg = " Parameter values are copied to the clipboard..." |
---|
714 | infor = 'warning' |
---|
715 | elif flag: |
---|
716 | msg = " Parameter values are pasted from the clipboad..." |
---|
717 | infor = "warning" |
---|
718 | else: |
---|
719 | msg = "Error was occured " |
---|
720 | msg += ": No valid parameter values to paste from the clipboard..." |
---|
721 | infor = "error" |
---|
722 | # inform msg to wx |
---|
723 | wx.PostEvent( self.parent.parent, |
---|
724 | StatusEvent(status= msg, info=infor)) |
---|
725 | |
---|
726 | def _get_time_stamp(self): |
---|
727 | """ |
---|
728 | return time and date stings |
---|
729 | """ |
---|
730 | # date and time |
---|
731 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
732 | current_time= str(hour)+":"+str(minute)+":"+str(second) |
---|
733 | current_date= str( month)+"/"+str(day)+"/"+str(year) |
---|
734 | return current_time, current_date |
---|
735 | |
---|
736 | def on_bookmark(self, event): |
---|
737 | """ |
---|
738 | save history of the data and model |
---|
739 | """ |
---|
740 | if self.model==None: |
---|
741 | msg="Can not bookmark; Please select Data and Model first..." |
---|
742 | wx.MessageBox(msg, 'Info') |
---|
743 | return |
---|
744 | self.save_current_state() |
---|
745 | new_state = self.state.clone() |
---|
746 | ##Add model state on context menu |
---|
747 | self.number_saved_state += 1 |
---|
748 | current_time, current_date = self._get_time_stamp() |
---|
749 | #name= self.model.name+"[%g]"%self.number_saved_state |
---|
750 | name = "Fitting: %g]" % self.number_saved_state |
---|
751 | name += self.model.__class__.__name__ |
---|
752 | name += "bookmarked at %s on %s" % (current_time, current_date) |
---|
753 | self.saved_states[name]= new_state |
---|
754 | |
---|
755 | ## Add item in the context menu |
---|
756 | msg = "Model saved at %s on %s"%(current_time, current_date) |
---|
757 | ## post help message for the selected model |
---|
758 | msg +=" Saved! right click on this page to retrieve this model" |
---|
759 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
760 | |
---|
761 | id = wx.NewId() |
---|
762 | self.popUpMenu.Append(id,name,str(msg)) |
---|
763 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
764 | wx.PostEvent(self.parent.parent, |
---|
765 | AppendBookmarkEvent(title=name, |
---|
766 | hint=str(msg), handler=self._back_to_bookmark)) |
---|
767 | def _back_to_bookmark(self, event): |
---|
768 | """ |
---|
769 | Back to bookmark |
---|
770 | """ |
---|
771 | self._manager.on_perspective(event) |
---|
772 | self.onResetModel(event) |
---|
773 | def old_on_bookmark(self, event): |
---|
774 | """ |
---|
775 | save history of the data and model |
---|
776 | """ |
---|
777 | if self.model==None: |
---|
778 | msg="Can not bookmark; Please select Data and Model first..." |
---|
779 | wx.MessageBox(msg, 'Info') |
---|
780 | return |
---|
781 | if hasattr(self,"enable_disp"): |
---|
782 | self.state.enable_disp = copy.deepcopy(self.enable_disp.GetValue()) |
---|
783 | if hasattr(self, "disp_box"): |
---|
784 | self.state.disp_box = copy.deepcopy(self.disp_box.GetSelection()) |
---|
785 | |
---|
786 | self.state.model.name= self.model.name |
---|
787 | |
---|
788 | #Remember fit engine_type for fit panel |
---|
789 | if self.engine_type == None: |
---|
790 | self.engine_type = "scipy" |
---|
791 | if self._manager !=None: |
---|
792 | self._manager._on_change_engine(engine=self.engine_type) |
---|
793 | |
---|
794 | self.state.engine_type = self.engine_type |
---|
795 | |
---|
796 | new_state = self.state.clone() |
---|
797 | new_state.model.name = self.state.model.name |
---|
798 | |
---|
799 | new_state.enable2D = copy.deepcopy(self.enable2D) |
---|
800 | ##Add model state on context menu |
---|
801 | self.number_saved_state += 1 |
---|
802 | #name= self.model.name+"[%g]"%self.number_saved_state |
---|
803 | name= self.model.__class__.__name__+"[%g]"%self.number_saved_state |
---|
804 | self.saved_states[name]= new_state |
---|
805 | |
---|
806 | ## Add item in the context menu |
---|
807 | |
---|
808 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
809 | my_time= str(hour)+" : "+str(minute)+" : "+str(second)+" " |
---|
810 | date= str( month)+"|"+str(day)+"|"+str(year) |
---|
811 | msg= "Model saved at %s on %s"%(my_time, date) |
---|
812 | ## post help message for the selected model |
---|
813 | msg +=" Saved! right click on this page to retrieve this model" |
---|
814 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
815 | |
---|
816 | id = wx.NewId() |
---|
817 | self.popUpMenu.Append(id,name,str(msg)) |
---|
818 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
819 | |
---|
820 | def onSetFocus(self, evt): |
---|
821 | """ |
---|
822 | highlight the current textcrtl and hide the error text control shown |
---|
823 | after fitting |
---|
824 | """ |
---|
825 | return |
---|
826 | |
---|
827 | def read_file(self, path): |
---|
828 | """ |
---|
829 | Read two columns file |
---|
830 | |
---|
831 | :param path: the path to the file to read |
---|
832 | |
---|
833 | """ |
---|
834 | try: |
---|
835 | if path==None: |
---|
836 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
837 | " Selected Distribution was not loaded: %s"%path)) |
---|
838 | return None, None |
---|
839 | input_f = open(path, 'r') |
---|
840 | buff = input_f.read() |
---|
841 | lines = buff.split('\n') |
---|
842 | |
---|
843 | angles = [] |
---|
844 | weights=[] |
---|
845 | for line in lines: |
---|
846 | toks = line.split() |
---|
847 | try: |
---|
848 | angle = float(toks[0]) |
---|
849 | weight = float(toks[1]) |
---|
850 | except: |
---|
851 | # Skip non-data lines |
---|
852 | pass |
---|
853 | angles.append(angle) |
---|
854 | weights.append(weight) |
---|
855 | return numpy.array(angles), numpy.array(weights) |
---|
856 | except: |
---|
857 | raise |
---|
858 | |
---|
859 | def createMemento(self): |
---|
860 | """ |
---|
861 | return the current state of the page |
---|
862 | """ |
---|
863 | return self.state.clone() |
---|
864 | |
---|
865 | |
---|
866 | def save_current_state(self): |
---|
867 | """ |
---|
868 | Store current state |
---|
869 | """ |
---|
870 | self.state.engine_type = copy.deepcopy(self.engine_type) |
---|
871 | ## save model option |
---|
872 | if self.model!= None: |
---|
873 | self.disp_list= self.model.getDispParamList() |
---|
874 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
875 | self.state.model = self.model.clone() |
---|
876 | #save radiobutton state for model selection |
---|
877 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
878 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
879 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
880 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
881 | #model combobox |
---|
882 | self.state.structurebox = self.structurebox.GetSelection() |
---|
883 | self.state.formfactorbox = self.formfactorbox.GetSelection() |
---|
884 | |
---|
885 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
886 | self.state.values= copy.deepcopy(self.values) |
---|
887 | self.state.weights = copy.deepcopy( self.weights) |
---|
888 | ## save data |
---|
889 | self.state.data= copy.deepcopy(self.data) |
---|
890 | self.state.qmax_x = self.qmax_x |
---|
891 | self.state.qmin_x = self.qmin_x |
---|
892 | self.state.dI_noweight = copy.deepcopy(self.dI_noweight.GetValue()) |
---|
893 | self.state.dI_didata = copy.deepcopy(self.dI_didata.GetValue()) |
---|
894 | self.state.dI_sqrdata = copy.deepcopy(self.dI_sqrdata.GetValue()) |
---|
895 | self.state.dI_idata = copy.deepcopy(self.dI_idata.GetValue()) |
---|
896 | if hasattr(self,"enable_disp"): |
---|
897 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
898 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
899 | |
---|
900 | self.state.smearer = copy.deepcopy(self.current_smearer) |
---|
901 | if hasattr(self,"enable_smearer"): |
---|
902 | self.state.enable_smearer = \ |
---|
903 | copy.deepcopy(self.enable_smearer.GetValue()) |
---|
904 | self.state.disable_smearer = \ |
---|
905 | copy.deepcopy(self.disable_smearer.GetValue()) |
---|
906 | |
---|
907 | self.state.pinhole_smearer = \ |
---|
908 | copy.deepcopy(self.pinhole_smearer.GetValue()) |
---|
909 | self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) |
---|
910 | |
---|
911 | if len(self._disp_obj_dict)>0: |
---|
912 | for k , v in self._disp_obj_dict.iteritems(): |
---|
913 | self.state._disp_obj_dict[k]= v |
---|
914 | |
---|
915 | |
---|
916 | self.state.values = copy.deepcopy(self.values) |
---|
917 | self.state.weights = copy.deepcopy(self.weights) |
---|
918 | ## save plotting range |
---|
919 | self._save_plotting_range() |
---|
920 | |
---|
921 | self.state.orientation_params = [] |
---|
922 | self.state.orientation_params_disp = [] |
---|
923 | self.state.parameters = [] |
---|
924 | self.state.fittable_param = [] |
---|
925 | self.state.fixed_param = [] |
---|
926 | self.state.str_parameters = [] |
---|
927 | |
---|
928 | |
---|
929 | ## save checkbutton state and txtcrtl values |
---|
930 | self._copy_parameters_state(self.str_parameters, |
---|
931 | self.state.str_parameters) |
---|
932 | self._copy_parameters_state(self.orientation_params, |
---|
933 | self.state.orientation_params) |
---|
934 | self._copy_parameters_state(self.orientation_params_disp, |
---|
935 | self.state.orientation_params_disp) |
---|
936 | |
---|
937 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
938 | self._copy_parameters_state(self.fittable_param, |
---|
939 | self.state.fittable_param) |
---|
940 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
941 | #save chisqr |
---|
942 | self.state.tcChi = self.tcChi.GetValue() |
---|
943 | |
---|
944 | def save_current_state_fit(self): |
---|
945 | """ |
---|
946 | Store current state for fit_page |
---|
947 | """ |
---|
948 | ## save model option |
---|
949 | if self.model!= None: |
---|
950 | self.disp_list= self.model.getDispParamList() |
---|
951 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
952 | self.state.model = self.model.clone() |
---|
953 | if hasattr(self, "engine_type"): |
---|
954 | self.state.engine_type = copy.deepcopy(self.engine_type) |
---|
955 | |
---|
956 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
957 | self.state.values= copy.deepcopy(self.values) |
---|
958 | self.state.weights = copy.deepcopy( self.weights) |
---|
959 | ## save data |
---|
960 | self.state.data= copy.deepcopy(self.data) |
---|
961 | |
---|
962 | if hasattr(self,"enable_disp"): |
---|
963 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
964 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
965 | |
---|
966 | self.state.smearer = copy.deepcopy(self.current_smearer) |
---|
967 | if hasattr(self,"enable_smearer"): |
---|
968 | self.state.enable_smearer = \ |
---|
969 | copy.deepcopy(self.enable_smearer.GetValue()) |
---|
970 | self.state.disable_smearer = \ |
---|
971 | copy.deepcopy(self.disable_smearer.GetValue()) |
---|
972 | |
---|
973 | self.state.pinhole_smearer = \ |
---|
974 | copy.deepcopy(self.pinhole_smearer.GetValue()) |
---|
975 | self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) |
---|
976 | self.state.dI_noweight = copy.deepcopy(self.dI_noweight.GetValue()) |
---|
977 | self.state.dI_didata = copy.deepcopy(self.dI_didata.GetValue()) |
---|
978 | self.state.dI_sqrdata = copy.deepcopy(self.dI_sqrdata.GetValue()) |
---|
979 | self.state.dI_idata = copy.deepcopy(self.dI_idata.GetValue()) |
---|
980 | if hasattr(self,"disp_box"): |
---|
981 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
982 | |
---|
983 | if len(self.disp_cb_dict) > 0: |
---|
984 | for k, v in self.disp_cb_dict.iteritems(): |
---|
985 | |
---|
986 | if v == None : |
---|
987 | self.state.disp_cb_dict[k] = v |
---|
988 | else: |
---|
989 | try: |
---|
990 | self.state.disp_cb_dict[k] = v.GetValue() |
---|
991 | except: |
---|
992 | self.state.disp_cb_dict[k] = None |
---|
993 | |
---|
994 | if len(self._disp_obj_dict) > 0: |
---|
995 | for k , v in self._disp_obj_dict.iteritems(): |
---|
996 | |
---|
997 | self.state._disp_obj_dict[k] = v |
---|
998 | |
---|
999 | |
---|
1000 | self.state.values = copy.deepcopy(self.values) |
---|
1001 | self.state.weights = copy.deepcopy(self.weights) |
---|
1002 | |
---|
1003 | ## save plotting range |
---|
1004 | self._save_plotting_range() |
---|
1005 | |
---|
1006 | ## save checkbutton state and txtcrtl values |
---|
1007 | self._copy_parameters_state(self.orientation_params, |
---|
1008 | self.state.orientation_params) |
---|
1009 | self._copy_parameters_state(self.orientation_params_disp, |
---|
1010 | self.state.orientation_params_disp) |
---|
1011 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
1012 | self._copy_parameters_state(self.fittable_param, |
---|
1013 | self.state.fittable_param) |
---|
1014 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
1015 | |
---|
1016 | |
---|
1017 | def check_invalid_panel(self): |
---|
1018 | """ |
---|
1019 | check if the user can already perform some action with this panel |
---|
1020 | """ |
---|
1021 | flag = False |
---|
1022 | if self.data is None: |
---|
1023 | self.disable_smearer.SetValue(True) |
---|
1024 | self.disable_disp.SetValue(True) |
---|
1025 | msg = "Please load Data and select Model to start..." |
---|
1026 | wx.MessageBox(msg, 'Info') |
---|
1027 | return True |
---|
1028 | |
---|
1029 | def set_model_state(self, state): |
---|
1030 | """ |
---|
1031 | reset page given a model state |
---|
1032 | """ |
---|
1033 | self.disp_cb_dict = state.disp_cb_dict |
---|
1034 | self.disp_list = state.disp_list |
---|
1035 | |
---|
1036 | ## set the state of the radio box |
---|
1037 | self.shape_rbutton.SetValue(state.shape_rbutton ) |
---|
1038 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
1039 | self.struct_rbutton.SetValue(state.struct_rbutton) |
---|
1040 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
1041 | |
---|
1042 | ## fill model combobox |
---|
1043 | self._show_combox_helper() |
---|
1044 | #select the current model |
---|
1045 | self.formfactorbox.Select(int(state.formfactorcombobox)) |
---|
1046 | self.structurebox.SetSelection(state.structurecombobox ) |
---|
1047 | if state.multi_factor != None: |
---|
1048 | self.multifactorbox.SetSelection(state.multi_factor) |
---|
1049 | |
---|
1050 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
1051 | self._reset_parameters_state(self.orientation_params_disp, |
---|
1052 | state.orientation_params_disp) |
---|
1053 | self._reset_parameters_state(self.orientation_params, |
---|
1054 | state.orientation_params) |
---|
1055 | self._reset_parameters_state(self.str_parameters, |
---|
1056 | state.str_parameters) |
---|
1057 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
1058 | ## display dispersion info layer |
---|
1059 | self.enable_disp.SetValue(state.enable_disp) |
---|
1060 | self.disable_disp.SetValue(state.disable_disp) |
---|
1061 | |
---|
1062 | if hasattr(self, "disp_box"): |
---|
1063 | |
---|
1064 | self.disp_box.SetSelection(state.disp_box) |
---|
1065 | n= self.disp_box.GetCurrentSelection() |
---|
1066 | dispersity= self.disp_box.GetClientData(n) |
---|
1067 | name = dispersity.__name__ |
---|
1068 | |
---|
1069 | self._set_dipers_Param(event=None) |
---|
1070 | |
---|
1071 | if name == "ArrayDispersion": |
---|
1072 | |
---|
1073 | for item in self.disp_cb_dict.keys(): |
---|
1074 | |
---|
1075 | if hasattr(self.disp_cb_dict[item], "SetValue") : |
---|
1076 | self.disp_cb_dict[item].SetValue(\ |
---|
1077 | state.disp_cb_dict[item]) |
---|
1078 | # Create the dispersion objects |
---|
1079 | from sans.models.dispersion_models import ArrayDispersion |
---|
1080 | disp_model = ArrayDispersion() |
---|
1081 | if hasattr(state,"values")and\ |
---|
1082 | self.disp_cb_dict[item].GetValue() == True: |
---|
1083 | if len(state.values)>0: |
---|
1084 | self.values=state.values |
---|
1085 | self.weights=state.weights |
---|
1086 | disp_model.set_weights(self.values, |
---|
1087 | state.weights) |
---|
1088 | else: |
---|
1089 | self._reset_dispersity() |
---|
1090 | |
---|
1091 | self._disp_obj_dict[item] = disp_model |
---|
1092 | # Set the new model as the dispersion object |
---|
1093 | #for the selected parameter |
---|
1094 | self.model.set_dispersion(item, disp_model) |
---|
1095 | |
---|
1096 | self.model._persistency_dict[item] = \ |
---|
1097 | [state.values, state.weights] |
---|
1098 | |
---|
1099 | else: |
---|
1100 | keys = self.model.getParamList() |
---|
1101 | for item in keys: |
---|
1102 | if item in self.disp_list and \ |
---|
1103 | not self.model.details.has_key(item): |
---|
1104 | self.model.details[item] = ["", None, None] |
---|
1105 | for k,v in self.state.disp_cb_dict.iteritems(): |
---|
1106 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
1107 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
1108 | ## smearing info restore |
---|
1109 | if hasattr(self, "enable_smearer"): |
---|
1110 | ## set smearing value whether or not the data |
---|
1111 | #contain the smearing info |
---|
1112 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
1113 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
1114 | self.onSmear(event=None) |
---|
1115 | self.pinhole_smearer.SetValue(state.pinhole_smearer) |
---|
1116 | self.slit_smearer.SetValue(state.slit_smearer) |
---|
1117 | |
---|
1118 | self.dI_noweight.SetValue(state.dI_noweight) |
---|
1119 | self.dI_didata.SetValue(state.dI_didata) |
---|
1120 | self.dI_sqrdata.SetValue(state.dI_sqrdata) |
---|
1121 | self.dI_idata.SetValue(state.dI_idata) |
---|
1122 | |
---|
1123 | ## we have two more options for smearing |
---|
1124 | if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None) |
---|
1125 | elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None) |
---|
1126 | |
---|
1127 | ## reset state of checkbox,textcrtl and dispersity parameters value |
---|
1128 | self._reset_parameters_state(self.fittable_param,state.fittable_param) |
---|
1129 | self._reset_parameters_state(self.fixed_param,state.fixed_param) |
---|
1130 | |
---|
1131 | ## draw the model with previous parameters value |
---|
1132 | self._onparamEnter_helper() |
---|
1133 | self.select_param(event=None) |
---|
1134 | #Save state_fit |
---|
1135 | self.save_current_state_fit() |
---|
1136 | self._lay_out() |
---|
1137 | self.Refresh() |
---|
1138 | |
---|
1139 | def reset_page_helper(self, state): |
---|
1140 | """ |
---|
1141 | Use page_state and change the state of existing page |
---|
1142 | |
---|
1143 | :precondition: the page is already drawn or created |
---|
1144 | |
---|
1145 | :postcondition: the state of the underlying data change as well as the |
---|
1146 | state of the graphic interface |
---|
1147 | """ |
---|
1148 | if state == None: |
---|
1149 | #self._undo.Enable(False) |
---|
1150 | return |
---|
1151 | # set data, etc. from the state |
---|
1152 | # reset page between theory and fitting from bookmarking |
---|
1153 | #if state.data == None: |
---|
1154 | # data = None |
---|
1155 | #else: |
---|
1156 | data = state.data |
---|
1157 | |
---|
1158 | #if data != None: |
---|
1159 | |
---|
1160 | if data == None: |
---|
1161 | data_min = state.qmin |
---|
1162 | data_max = state.qmax |
---|
1163 | self.qmin_x = data_min |
---|
1164 | self.qmax_x = data_max |
---|
1165 | #self.minimum_q.SetValue(str(data_min)) |
---|
1166 | #self.maximum_q.SetValue(str(data_max)) |
---|
1167 | self.qmin.SetValue(str(data_min)) |
---|
1168 | self.qmax.SetValue(str(data_max)) |
---|
1169 | |
---|
1170 | self.state.data = data |
---|
1171 | self.state.qmin = self.qmin_x |
---|
1172 | self.state.qmax = self.qmax_x |
---|
1173 | else: |
---|
1174 | self.set_data(data) |
---|
1175 | |
---|
1176 | self.enable2D= state.enable2D |
---|
1177 | self.engine_type = state.engine_type |
---|
1178 | |
---|
1179 | self.disp_cb_dict = state.disp_cb_dict |
---|
1180 | self.disp_list = state.disp_list |
---|
1181 | |
---|
1182 | ## set the state of the radio box |
---|
1183 | self.shape_rbutton.SetValue(state.shape_rbutton ) |
---|
1184 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
1185 | self.struct_rbutton.SetValue(state.struct_rbutton) |
---|
1186 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
1187 | |
---|
1188 | ## fill model combobox |
---|
1189 | self._show_combox_helper() |
---|
1190 | #select the current model |
---|
1191 | self.formfactorbox.Select(int(state.formfactorcombobox)) |
---|
1192 | self.structurebox.SetSelection(state.structurecombobox ) |
---|
1193 | if state.multi_factor != None: |
---|
1194 | self.multifactorbox.SetSelection(state.multi_factor) |
---|
1195 | |
---|
1196 | #reset the fitting engine type |
---|
1197 | self.engine_type = state.engine_type |
---|
1198 | #draw the pnael according to the new model parameter |
---|
1199 | self._on_select_model(event=None) |
---|
1200 | # take care of 2D button |
---|
1201 | if data == None and self.model_view.IsEnabled(): |
---|
1202 | if self.enable2D: |
---|
1203 | self.model_view.SetLabel("2D Mode") |
---|
1204 | else: |
---|
1205 | self.model_view.SetLabel("1D Mode") |
---|
1206 | # else: |
---|
1207 | |
---|
1208 | if self._manager !=None: |
---|
1209 | self._manager._on_change_engine(engine=self.engine_type) |
---|
1210 | ## set the select all check box to the a given state |
---|
1211 | self.cb1.SetValue(state.cb1) |
---|
1212 | |
---|
1213 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
1214 | self._reset_parameters_state(self.orientation_params_disp, |
---|
1215 | state.orientation_params_disp) |
---|
1216 | self._reset_parameters_state(self.orientation_params, |
---|
1217 | state.orientation_params) |
---|
1218 | self._reset_parameters_state(self.str_parameters, |
---|
1219 | state.str_parameters) |
---|
1220 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
1221 | ## display dispersion info layer |
---|
1222 | self.enable_disp.SetValue(state.enable_disp) |
---|
1223 | self.disable_disp.SetValue(state.disable_disp) |
---|
1224 | # If the polydispersion is ON |
---|
1225 | if state.enable_disp: |
---|
1226 | # reset dispersion according the state |
---|
1227 | self._set_dipers_Param(event=None) |
---|
1228 | self._reset_page_disp_helper(state) |
---|
1229 | ##plotting range restore |
---|
1230 | self._reset_plotting_range(state) |
---|
1231 | ## smearing info restore |
---|
1232 | if hasattr(self, "enable_smearer"): |
---|
1233 | ## set smearing value whether or not the data |
---|
1234 | #contain the smearing info |
---|
1235 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
1236 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
1237 | self.onSmear(event=None) |
---|
1238 | self.pinhole_smearer.SetValue(state.pinhole_smearer) |
---|
1239 | self.slit_smearer.SetValue(state.slit_smearer) |
---|
1240 | try: |
---|
1241 | self.dI_noweight.SetValue(state.dI_noweight) |
---|
1242 | self.dI_didata.SetValue(state.dI_didata) |
---|
1243 | self.dI_sqrdata.SetValue(state.dI_sqrdata) |
---|
1244 | self.dI_idata.SetValue(state.dI_idata) |
---|
1245 | except: |
---|
1246 | # to support older state file formats |
---|
1247 | self.dI_noweight.SetValue(False) |
---|
1248 | self.dI_didata.SetValue(True) |
---|
1249 | self.dI_sqrdata.SetValue(False) |
---|
1250 | self.dI_idata.SetValue(False) |
---|
1251 | |
---|
1252 | ## we have two more options for smearing |
---|
1253 | if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None) |
---|
1254 | elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None) |
---|
1255 | |
---|
1256 | ## reset state of checkbox,textcrtl and dispersity parameters value |
---|
1257 | self._reset_parameters_state(self.fittable_param,state.fittable_param) |
---|
1258 | self._reset_parameters_state(self.fixed_param,state.fixed_param) |
---|
1259 | |
---|
1260 | ## draw the model with previous parameters value |
---|
1261 | self._onparamEnter_helper() |
---|
1262 | #reset the value of chisqr when not consistent with the value computed |
---|
1263 | self.tcChi.SetValue(str(self.state.tcChi)) |
---|
1264 | ## reset context menu items |
---|
1265 | self._reset_context_menu() |
---|
1266 | |
---|
1267 | ## set the value of the current state to the state given as parameter |
---|
1268 | self.state = state.clone() |
---|
1269 | |
---|
1270 | def _reset_page_disp_helper(self, state): |
---|
1271 | """ |
---|
1272 | Help to rest page for dispersions |
---|
1273 | """ |
---|
1274 | keys = self.model.getParamList() |
---|
1275 | for item in keys: |
---|
1276 | if item in self.disp_list and \ |
---|
1277 | not self.model.details.has_key(item): |
---|
1278 | self.model.details[item] = ["", None, None] |
---|
1279 | #for k,v in self.state.disp_cb_dict.iteritems(): |
---|
1280 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
1281 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
1282 | self.values = copy.deepcopy(state.values) |
---|
1283 | self.weights = copy.deepcopy(state.weights) |
---|
1284 | |
---|
1285 | for key, disp in state._disp_obj_dict.iteritems(): |
---|
1286 | # From saved file, disp_model can not be sent in model obj. |
---|
1287 | # it will be sent as a string here, then converted to model object. |
---|
1288 | if disp.__class__.__name__ == 'str': |
---|
1289 | com_str = "from sans.models.dispersion_models " |
---|
1290 | com_str += "import %s as disp_func" |
---|
1291 | exec com_str % disp |
---|
1292 | disp_model = disp_func() |
---|
1293 | else: |
---|
1294 | disp_model = disp |
---|
1295 | |
---|
1296 | self._disp_obj_dict[key] = disp_model |
---|
1297 | param_name = key.split('.')[0] |
---|
1298 | # Try to set dispersion only when available |
---|
1299 | # for eg., pass the orient. angles for 1D Cal |
---|
1300 | try: |
---|
1301 | self.model.set_dispersion(param_name, disp_model) |
---|
1302 | self.model._persistency_dict[key] = \ |
---|
1303 | [state.values, state.weights] |
---|
1304 | except: |
---|
1305 | pass |
---|
1306 | selection = self._find_polyfunc_selection(disp_model) |
---|
1307 | for list in self.fittable_param: |
---|
1308 | if list[1] == key and list[7] != None: |
---|
1309 | list[7].SetSelection(selection) |
---|
1310 | # For the array disp_model, set the values and weights |
---|
1311 | if selection == 1: |
---|
1312 | disp_model.set_weights(self.values[key], |
---|
1313 | self.weights[key]) |
---|
1314 | try: |
---|
1315 | # Diables all fittable params for array |
---|
1316 | list[0].SetValue(False) |
---|
1317 | list[0].Disable() |
---|
1318 | list[2].Disable() |
---|
1319 | list[5].Disable() |
---|
1320 | list[6].Disable() |
---|
1321 | except: |
---|
1322 | pass |
---|
1323 | # For array, disable all fixed params |
---|
1324 | if selection == 1: |
---|
1325 | for item in self.fixed_param: |
---|
1326 | if item[1].split(".")[0] == key.split(".")[0]: |
---|
1327 | # try it and pass it for the orientation for 1D |
---|
1328 | try: |
---|
1329 | item[2].Disable() |
---|
1330 | except: |
---|
1331 | pass |
---|
1332 | |
---|
1333 | # Make sure the check box updated when all checked |
---|
1334 | if self.cb1.GetValue(): |
---|
1335 | self.select_all_param(None) |
---|
1336 | |
---|
1337 | def _selectDlg(self): |
---|
1338 | """ |
---|
1339 | open a dialog file to selected the customized dispersity |
---|
1340 | """ |
---|
1341 | import os |
---|
1342 | if self.parent != None: |
---|
1343 | self._default_save_location = \ |
---|
1344 | self.parent.parent._default_save_location |
---|
1345 | dlg = wx.FileDialog(self, "Choose a weight file", |
---|
1346 | self._default_save_location , "", |
---|
1347 | "*.*", wx.OPEN) |
---|
1348 | path = None |
---|
1349 | if dlg.ShowModal() == wx.ID_OK: |
---|
1350 | path = dlg.GetPath() |
---|
1351 | dlg.Destroy() |
---|
1352 | return path |
---|
1353 | |
---|
1354 | def _reset_context_menu(self): |
---|
1355 | """ |
---|
1356 | reset the context menu |
---|
1357 | """ |
---|
1358 | for name, state in self.state.saved_states.iteritems(): |
---|
1359 | self.number_saved_state += 1 |
---|
1360 | ## Add item in the context menu |
---|
1361 | id = wx.NewId() |
---|
1362 | msg = 'Save model and state %g' % self.number_saved_state |
---|
1363 | self.popUpMenu.Append(id, name, msg) |
---|
1364 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
1365 | |
---|
1366 | def _reset_plotting_range(self, state): |
---|
1367 | """ |
---|
1368 | Reset the plotting range to a given state |
---|
1369 | """ |
---|
1370 | # if self.check_invalid_panel(): |
---|
1371 | # return |
---|
1372 | self.qmin.SetValue(str(state.qmin)) |
---|
1373 | self.qmax.SetValue(str(state.qmax)) |
---|
1374 | |
---|
1375 | def _save_typeOfmodel(self): |
---|
1376 | """ |
---|
1377 | save radiobutton containing the type model that can be selected |
---|
1378 | """ |
---|
1379 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
1380 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
1381 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
1382 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
1383 | self.state.structurebox= self.structurebox.GetCurrentSelection() |
---|
1384 | self.state.formfactorbox = self.formfactorbox.GetCurrentSelection() |
---|
1385 | |
---|
1386 | #self._undo.Enable(True) |
---|
1387 | ## post state to fit panel |
---|
1388 | event = PageInfoEvent(page = self) |
---|
1389 | wx.PostEvent(self.parent, event) |
---|
1390 | |
---|
1391 | def _save_plotting_range(self ): |
---|
1392 | """ |
---|
1393 | save the state of plotting range |
---|
1394 | """ |
---|
1395 | self.state.qmin = self.qmin_x |
---|
1396 | self.state.qmax = self.qmax_x |
---|
1397 | self.state.npts = self.npts_x |
---|
1398 | |
---|
1399 | def _onparamEnter_helper(self): |
---|
1400 | """ |
---|
1401 | check if values entered by the user are changed and valid to replot |
---|
1402 | model |
---|
1403 | """ |
---|
1404 | # Flag to register when a parameter has changed. |
---|
1405 | is_modified = False |
---|
1406 | self.fitrange = True |
---|
1407 | is_2Ddata = False |
---|
1408 | #self._undo.Enable(True) |
---|
1409 | # check if 2d data |
---|
1410 | if self.data.__class__.__name__ == "Data2D": |
---|
1411 | is_2Ddata = True |
---|
1412 | if self.model !=None: |
---|
1413 | try: |
---|
1414 | is_modified = self._check_value_enter(self.fittable_param, |
---|
1415 | is_modified) |
---|
1416 | is_modified = self._check_value_enter(self.fixed_param, |
---|
1417 | is_modified) |
---|
1418 | is_modified = self._check_value_enter(self.parameters, |
---|
1419 | is_modified) |
---|
1420 | except: |
---|
1421 | pass |
---|
1422 | #if is_modified: |
---|
1423 | |
---|
1424 | # Here we should check whether the boundaries have been modified. |
---|
1425 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
1426 | # set the is_modified flag to True |
---|
1427 | if self._validate_qrange(self.qmin, self.qmax): |
---|
1428 | tempmin = float(self.qmin.GetValue()) |
---|
1429 | if tempmin != self.qmin_x: |
---|
1430 | self.qmin_x = tempmin |
---|
1431 | is_modified = True |
---|
1432 | tempmax = float(self.qmax.GetValue()) |
---|
1433 | if tempmax != self.qmax_x: |
---|
1434 | self.qmax_x = tempmax |
---|
1435 | is_modified = True |
---|
1436 | |
---|
1437 | if is_2Ddata: |
---|
1438 | # set mask |
---|
1439 | is_modified = self._validate_Npts() |
---|
1440 | |
---|
1441 | else: |
---|
1442 | self.fitrange = False |
---|
1443 | ## if any value is modify draw model with new value |
---|
1444 | if not self.fitrange: |
---|
1445 | #self.btFit.Disable() |
---|
1446 | if is_2Ddata: self.btEditMask.Disable() |
---|
1447 | else: |
---|
1448 | #self.btFit.Enable(True) |
---|
1449 | if is_2Ddata: self.btEditMask.Enable(True) |
---|
1450 | if is_modified and self.fitrange: |
---|
1451 | #if self.data == None: |
---|
1452 | # Theory case: need to get npts value to draw |
---|
1453 | self.npts_x = float(self.Npts_total.GetValue()) |
---|
1454 | self.create_default_data() |
---|
1455 | self.state_change= True |
---|
1456 | self._draw_model() |
---|
1457 | self.Refresh() |
---|
1458 | return is_modified |
---|
1459 | |
---|
1460 | def _update_paramv_on_fit(self): |
---|
1461 | """ |
---|
1462 | make sure that update param values just before the fitting |
---|
1463 | """ |
---|
1464 | #flag for qmin qmax check values |
---|
1465 | flag = True |
---|
1466 | self.fitrange = True |
---|
1467 | is_modified = False |
---|
1468 | |
---|
1469 | #wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
1470 | #updating ... ",type="update")) |
---|
1471 | |
---|
1472 | ##So make sure that update param values on_Fit. |
---|
1473 | #self._undo.Enable(True) |
---|
1474 | if self.model !=None: |
---|
1475 | ##Check the values |
---|
1476 | self._check_value_enter( self.fittable_param ,is_modified) |
---|
1477 | self._check_value_enter( self.fixed_param ,is_modified) |
---|
1478 | self._check_value_enter( self.parameters ,is_modified) |
---|
1479 | |
---|
1480 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
1481 | # Here we should check whether the boundaries have been modified. |
---|
1482 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
1483 | # set the is_modified flag to True |
---|
1484 | self.fitrange = self._validate_qrange(self.qmin, self.qmax) |
---|
1485 | if self.fitrange: |
---|
1486 | tempmin = float(self.qmin.GetValue()) |
---|
1487 | if tempmin != self.qmin_x: |
---|
1488 | self.qmin_x = tempmin |
---|
1489 | tempmax = float(self.qmax.GetValue()) |
---|
1490 | if tempmax != self.qmax_x: |
---|
1491 | self.qmax_x = tempmax |
---|
1492 | if tempmax == tempmin: |
---|
1493 | flag = False |
---|
1494 | temp_smearer = None |
---|
1495 | if not self.disable_smearer.GetValue(): |
---|
1496 | temp_smearer= self.current_smearer |
---|
1497 | if self.slit_smearer.GetValue(): |
---|
1498 | flag = self.update_slit_smear() |
---|
1499 | elif self.pinhole_smearer.GetValue(): |
---|
1500 | flag = self.update_pinhole_smear() |
---|
1501 | else: |
---|
1502 | self._manager.set_smearer(smearer=temp_smearer, |
---|
1503 | uid=self.uid, |
---|
1504 | fid=self.data.id, |
---|
1505 | qmin=float(self.qmin_x), |
---|
1506 | qmax=float(self.qmax_x), |
---|
1507 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
1508 | draw=False) |
---|
1509 | elif not self._is_2D(): |
---|
1510 | self._manager.set_smearer(smearer=temp_smearer, |
---|
1511 | qmin=float(self.qmin_x), |
---|
1512 | uid=self.uid, |
---|
1513 | fid=self.data.id, |
---|
1514 | qmax= float(self.qmax_x), |
---|
1515 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
1516 | draw=False) |
---|
1517 | if self.data != None: |
---|
1518 | index_data = ((self.qmin_x <= self.data.x)&\ |
---|
1519 | (self.data.x <= self.qmax_x)) |
---|
1520 | val = str(len(self.data.x[index_data==True])) |
---|
1521 | self.Npts_fit.SetValue(val) |
---|
1522 | else: |
---|
1523 | # No data in the panel |
---|
1524 | try: |
---|
1525 | self.npts_x = float(self.Npts_total.GetValue()) |
---|
1526 | except: |
---|
1527 | flag = False |
---|
1528 | return flag |
---|
1529 | flag = True |
---|
1530 | if self._is_2D(): |
---|
1531 | # only 2D case set mask |
---|
1532 | flag = self._validate_Npts() |
---|
1533 | if not flag: |
---|
1534 | return flag |
---|
1535 | else: flag = False |
---|
1536 | else: |
---|
1537 | flag = False |
---|
1538 | |
---|
1539 | #For invalid q range, disable the mask editor and fit button, vs. |
---|
1540 | if not self.fitrange: |
---|
1541 | #self.btFit.Disable() |
---|
1542 | if self._is_2D(): |
---|
1543 | self.btEditMask.Disable() |
---|
1544 | else: |
---|
1545 | #self.btFit.Enable(True) |
---|
1546 | if self._is_2D() and self.data != None: |
---|
1547 | self.btEditMask.Enable(True) |
---|
1548 | |
---|
1549 | if not flag: |
---|
1550 | msg = "Cannot Plot or Fit :Must select a " |
---|
1551 | msg += " model or Fitting range is not valid!!! " |
---|
1552 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
1553 | |
---|
1554 | self.save_current_state() |
---|
1555 | |
---|
1556 | return flag |
---|
1557 | |
---|
1558 | def _is_modified(self, is_modified): |
---|
1559 | """ |
---|
1560 | return to self._is_modified |
---|
1561 | """ |
---|
1562 | return is_modified |
---|
1563 | |
---|
1564 | def _reset_parameters_state(self, listtorestore, statelist): |
---|
1565 | """ |
---|
1566 | Reset the parameters at the given state |
---|
1567 | """ |
---|
1568 | if len(statelist) == 0 or len(listtorestore) == 0: |
---|
1569 | return |
---|
1570 | if len(statelist) != len(listtorestore): |
---|
1571 | return |
---|
1572 | |
---|
1573 | for j in range(len(listtorestore)): |
---|
1574 | item_page = listtorestore[j] |
---|
1575 | item_page_info = statelist[j] |
---|
1576 | ##change the state of the check box for simple parameters |
---|
1577 | if item_page[0]!=None: |
---|
1578 | item_page[0].SetValue(item_page_info[0]) |
---|
1579 | if item_page[2]!=None: |
---|
1580 | item_page[2].SetValue(item_page_info[2]) |
---|
1581 | if item_page[2].__class__.__name__ == "ComboBox": |
---|
1582 | if self.model.fun_list.has_key(item_page_info[2]): |
---|
1583 | fun_val = self.model.fun_list[item_page_info[2]] |
---|
1584 | self.model.setParam(item_page_info[1],fun_val) |
---|
1585 | if item_page[3]!=None: |
---|
1586 | ## show or hide text +/- |
---|
1587 | if item_page_info[2]: |
---|
1588 | item_page[3].Show(True) |
---|
1589 | else: |
---|
1590 | item_page[3].Hide() |
---|
1591 | if item_page[4]!=None: |
---|
1592 | ## show of hide the text crtl for fitting error |
---|
1593 | if item_page_info[4][0]: |
---|
1594 | item_page[4].Show(True) |
---|
1595 | item_page[4].SetValue(item_page_info[4][1]) |
---|
1596 | else: |
---|
1597 | item_page[3].Hide() |
---|
1598 | if item_page[5]!=None: |
---|
1599 | ## show of hide the text crtl for fitting error |
---|
1600 | item_page[5].Show(item_page_info[5][0]) |
---|
1601 | item_page[5].SetValue(item_page_info[5][1]) |
---|
1602 | |
---|
1603 | if item_page[6]!=None: |
---|
1604 | ## show of hide the text crtl for fitting error |
---|
1605 | item_page[6].Show(item_page_info[6][0]) |
---|
1606 | item_page[6].SetValue(item_page_info[6][1]) |
---|
1607 | |
---|
1608 | |
---|
1609 | def _reset_strparam_state(self, listtorestore, statelist): |
---|
1610 | """ |
---|
1611 | Reset the string parameters at the given state |
---|
1612 | """ |
---|
1613 | if len(statelist) == 0: |
---|
1614 | return |
---|
1615 | |
---|
1616 | listtorestore = copy.deepcopy(statelist) |
---|
1617 | |
---|
1618 | for j in range(len(listtorestore)): |
---|
1619 | item_page = listtorestore[j] |
---|
1620 | item_page_info = statelist[j] |
---|
1621 | ##change the state of the check box for simple parameters |
---|
1622 | |
---|
1623 | if item_page[0] != None: |
---|
1624 | item_page[0].SetValue(format_number(item_page_info[0], True)) |
---|
1625 | |
---|
1626 | if item_page[2] != None: |
---|
1627 | param_name = item_page_info[1] |
---|
1628 | value = item_page_info[2] |
---|
1629 | selection = value |
---|
1630 | if self.model.fun_list.has_key(value): |
---|
1631 | selection = self.model.fun_list[value] |
---|
1632 | item_page[2].SetValue(selection) |
---|
1633 | self.model.setParam(param_name, selection) |
---|
1634 | |
---|
1635 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
1636 | """ |
---|
1637 | copy the state of button |
---|
1638 | |
---|
1639 | :param listtocopy: the list of check button to copy |
---|
1640 | :param statelist: list of state object to store the current state |
---|
1641 | |
---|
1642 | """ |
---|
1643 | if len(listtocopy)==0: |
---|
1644 | return |
---|
1645 | |
---|
1646 | for item in listtocopy: |
---|
1647 | |
---|
1648 | checkbox_state = None |
---|
1649 | if item[0]!= None: |
---|
1650 | checkbox_state= item[0].GetValue() |
---|
1651 | parameter_name = item[1] |
---|
1652 | parameter_value = None |
---|
1653 | if item[2]!=None: |
---|
1654 | parameter_value = item[2].GetValue() |
---|
1655 | static_text = None |
---|
1656 | if item[3]!=None: |
---|
1657 | static_text = item[3].IsShown() |
---|
1658 | error_value = None |
---|
1659 | error_state = None |
---|
1660 | if item[4]!= None: |
---|
1661 | error_value = item[4].GetValue() |
---|
1662 | error_state = item[4].IsShown() |
---|
1663 | |
---|
1664 | min_value = None |
---|
1665 | min_state = None |
---|
1666 | if item[5]!= None: |
---|
1667 | min_value = item[5].GetValue() |
---|
1668 | min_state = item[5].IsShown() |
---|
1669 | |
---|
1670 | max_value = None |
---|
1671 | max_state = None |
---|
1672 | if item[6]!= None: |
---|
1673 | max_value = item[6].GetValue() |
---|
1674 | max_state = item[6].IsShown() |
---|
1675 | unit=None |
---|
1676 | if item[7]!=None: |
---|
1677 | unit = item[7].GetLabel() |
---|
1678 | |
---|
1679 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
1680 | static_text ,[error_state, error_value], |
---|
1681 | [min_state, min_value], |
---|
1682 | [max_state, max_value], unit]) |
---|
1683 | |
---|
1684 | def _set_model_sizer_selection(self, model): |
---|
1685 | """ |
---|
1686 | Display the sizer according to the type of the current model |
---|
1687 | """ |
---|
1688 | if model == None: |
---|
1689 | return |
---|
1690 | if hasattr(model ,"s_model"): |
---|
1691 | |
---|
1692 | class_name = model.s_model.__class__ |
---|
1693 | name = model.s_model.name |
---|
1694 | flag = (name != "NoStructure") |
---|
1695 | if flag and \ |
---|
1696 | (class_name in self.model_list_box["Structure Factors"]): |
---|
1697 | self.structurebox.Show() |
---|
1698 | self.text2.Show() |
---|
1699 | self.structurebox.Enable() |
---|
1700 | self.text2.Enable() |
---|
1701 | items = self.structurebox.GetItems() |
---|
1702 | self.sizer1.Layout() |
---|
1703 | |
---|
1704 | for i in range(len(items)): |
---|
1705 | if items[i]== str(name): |
---|
1706 | self.structurebox.SetSelection(i) |
---|
1707 | break |
---|
1708 | |
---|
1709 | if hasattr(model ,"p_model"): |
---|
1710 | class_name = model.p_model.__class__ |
---|
1711 | name = model.p_model.name |
---|
1712 | self.formfactorbox.Clear() |
---|
1713 | |
---|
1714 | for k, list in self.model_list_box.iteritems(): |
---|
1715 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1716 | self.shape_rbutton.SetValue(True) |
---|
1717 | ## fill the form factor list with new model |
---|
1718 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1719 | items = self.formfactorbox.GetItems() |
---|
1720 | ## set comboxbox to the selected item |
---|
1721 | for i in range(len(items)): |
---|
1722 | if items[i]== str(name): |
---|
1723 | self.formfactorbox.SetSelection(i) |
---|
1724 | break |
---|
1725 | return |
---|
1726 | elif k == "Shape-Independent": |
---|
1727 | self.shape_indep_rbutton.SetValue(True) |
---|
1728 | elif k == "Structure Factors": |
---|
1729 | self.struct_rbutton.SetValue(True) |
---|
1730 | elif k == "Multi-Functions": |
---|
1731 | continue |
---|
1732 | else: |
---|
1733 | self.plugin_rbutton.SetValue(True) |
---|
1734 | |
---|
1735 | if class_name in list: |
---|
1736 | ## fill the form factor list with new model |
---|
1737 | self._populate_box(self.formfactorbox, list) |
---|
1738 | items = self.formfactorbox.GetItems() |
---|
1739 | ## set comboxbox to the selected item |
---|
1740 | for i in range(len(items)): |
---|
1741 | if items[i]== str(name): |
---|
1742 | self.formfactorbox.SetSelection(i) |
---|
1743 | break |
---|
1744 | break |
---|
1745 | else: |
---|
1746 | |
---|
1747 | ## Select the model from the menu |
---|
1748 | class_name = model.__class__ |
---|
1749 | name = model.name |
---|
1750 | self.formfactorbox.Clear() |
---|
1751 | items = self.formfactorbox.GetItems() |
---|
1752 | |
---|
1753 | for k, list in self.model_list_box.iteritems(): |
---|
1754 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1755 | if class_name in self.model_list_box["P(Q)*S(Q)"]: |
---|
1756 | self.structurebox.Show() |
---|
1757 | self.text2.Show() |
---|
1758 | self.structurebox.Enable() |
---|
1759 | self.structurebox.SetSelection(0) |
---|
1760 | self.text2.Enable() |
---|
1761 | else: |
---|
1762 | self.structurebox.Hide() |
---|
1763 | self.text2.Hide() |
---|
1764 | self.structurebox.Disable() |
---|
1765 | self.structurebox.SetSelection(0) |
---|
1766 | self.text2.Disable() |
---|
1767 | |
---|
1768 | self.shape_rbutton.SetValue(True) |
---|
1769 | ## fill the form factor list with new model |
---|
1770 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1771 | items = self.formfactorbox.GetItems() |
---|
1772 | ## set comboxbox to the selected item |
---|
1773 | for i in range(len(items)): |
---|
1774 | if items[i]== str(name): |
---|
1775 | self.formfactorbox.SetSelection(i) |
---|
1776 | break |
---|
1777 | return |
---|
1778 | elif k == "Shape-Independent": |
---|
1779 | self.shape_indep_rbutton.SetValue(True) |
---|
1780 | elif k == "Structure Factors": |
---|
1781 | self.struct_rbutton.SetValue(True) |
---|
1782 | elif k == "Multi-Functions": |
---|
1783 | continue |
---|
1784 | else: |
---|
1785 | self.plugin_rbutton.SetValue(True) |
---|
1786 | if class_name in list: |
---|
1787 | self.structurebox.SetSelection(0) |
---|
1788 | self.structurebox.Disable() |
---|
1789 | self.text2.Disable() |
---|
1790 | ## fill the form factor list with new model |
---|
1791 | self._populate_box(self.formfactorbox, list) |
---|
1792 | items = self.formfactorbox.GetItems() |
---|
1793 | ## set comboxbox to the selected item |
---|
1794 | for i in range(len(items)): |
---|
1795 | if items[i]== str(name): |
---|
1796 | self.formfactorbox.SetSelection(i) |
---|
1797 | break |
---|
1798 | break |
---|
1799 | |
---|
1800 | def _draw_model(self, update_chisqr=True, source='model'): |
---|
1801 | """ |
---|
1802 | Method to draw or refresh a plotted model. |
---|
1803 | The method will use the data member from the model page |
---|
1804 | to build a call to the fitting perspective manager. |
---|
1805 | |
---|
1806 | :param chisqr: update chisqr value [bool] |
---|
1807 | """ |
---|
1808 | #if self.check_invalid_panel(): |
---|
1809 | # return |
---|
1810 | if self.model !=None: |
---|
1811 | temp_smear=None |
---|
1812 | if hasattr(self, "enable_smearer"): |
---|
1813 | if not self.disable_smearer.GetValue(): |
---|
1814 | temp_smear= self.current_smearer |
---|
1815 | # compute weight for the current data |
---|
1816 | from .utils import get_weight |
---|
1817 | flag = self.get_weight_flag() |
---|
1818 | weight = get_weight(data=self.data, is2d=self._is_2D(), flag=flag) |
---|
1819 | toggle_mode_on = self.model_view.IsEnabled() |
---|
1820 | is_2d = self._is_2D() |
---|
1821 | self._manager.draw_model(self.model, |
---|
1822 | data=self.data, |
---|
1823 | smearer= temp_smear, |
---|
1824 | qmin=float(self.qmin_x), |
---|
1825 | qmax=float(self.qmax_x), |
---|
1826 | page_id=self.uid, |
---|
1827 | toggle_mode_on=toggle_mode_on, |
---|
1828 | state = self.state, |
---|
1829 | enable2D=is_2d, |
---|
1830 | update_chisqr=update_chisqr, |
---|
1831 | source='model', |
---|
1832 | weight=weight) |
---|
1833 | |
---|
1834 | |
---|
1835 | def _on_show_sld(self, event=None): |
---|
1836 | """ |
---|
1837 | Plot SLD profile |
---|
1838 | """ |
---|
1839 | # get profile data |
---|
1840 | x,y=self.model.getProfile() |
---|
1841 | |
---|
1842 | from danse.common.plottools import Data1D |
---|
1843 | #from sans.perspectives.theory.profile_dialog import SLDPanel |
---|
1844 | from sans.guiframe.local_perspectives.plotting.profile_dialog \ |
---|
1845 | import SLDPanel |
---|
1846 | sld_data = Data1D(x,y) |
---|
1847 | sld_data.name = 'SLD' |
---|
1848 | sld_data.axes = self.sld_axes |
---|
1849 | self.panel = SLDPanel(self, data=sld_data, axes =self.sld_axes,id =-1) |
---|
1850 | self.panel.ShowModal() |
---|
1851 | |
---|
1852 | def _set_multfactor_combobox(self, multiplicity=10): |
---|
1853 | """ |
---|
1854 | Set comboBox for muitfactor of CoreMultiShellModel |
---|
1855 | :param multiplicit: no. of multi-functionality |
---|
1856 | """ |
---|
1857 | # build content of the combobox |
---|
1858 | for idx in range(0,multiplicity): |
---|
1859 | self.multifactorbox.Append(str(idx),int(idx)) |
---|
1860 | #self.multifactorbox.SetSelection(1) |
---|
1861 | self._hide_multfactor_combobox() |
---|
1862 | |
---|
1863 | def _show_multfactor_combobox(self): |
---|
1864 | """ |
---|
1865 | Show the comboBox of muitfactor of CoreMultiShellModel |
---|
1866 | """ |
---|
1867 | if not self.mutifactor_text.IsShown(): |
---|
1868 | self.mutifactor_text.Show(True) |
---|
1869 | self.mutifactor_text1.Show(True) |
---|
1870 | if not self.multifactorbox.IsShown(): |
---|
1871 | self.multifactorbox.Show(True) |
---|
1872 | |
---|
1873 | def _hide_multfactor_combobox(self): |
---|
1874 | """ |
---|
1875 | Hide the comboBox of muitfactor of CoreMultiShellModel |
---|
1876 | """ |
---|
1877 | if self.mutifactor_text.IsShown(): |
---|
1878 | self.mutifactor_text.Hide() |
---|
1879 | self.mutifactor_text1.Hide() |
---|
1880 | if self.multifactorbox.IsShown(): |
---|
1881 | self.multifactorbox.Hide() |
---|
1882 | |
---|
1883 | |
---|
1884 | def _show_combox_helper(self): |
---|
1885 | """ |
---|
1886 | Fill panel's combo box according to the type of model selected |
---|
1887 | """ |
---|
1888 | if self.shape_rbutton.GetValue(): |
---|
1889 | ##fill the combobox with form factor list |
---|
1890 | self.structurebox.SetSelection(0) |
---|
1891 | self.structurebox.Disable() |
---|
1892 | self.formfactorbox.Clear() |
---|
1893 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1894 | if self.shape_indep_rbutton.GetValue(): |
---|
1895 | ##fill the combobox with shape independent factor list |
---|
1896 | self.structurebox.SetSelection(0) |
---|
1897 | self.structurebox.Disable() |
---|
1898 | self.formfactorbox.Clear() |
---|
1899 | self._populate_box( self.formfactorbox, |
---|
1900 | self.model_list_box["Shape-Independent"]) |
---|
1901 | if self.struct_rbutton.GetValue(): |
---|
1902 | ##fill the combobox with structure factor list |
---|
1903 | self.structurebox.SetSelection(0) |
---|
1904 | self.structurebox.Disable() |
---|
1905 | self.formfactorbox.Clear() |
---|
1906 | self._populate_box( self.formfactorbox, |
---|
1907 | self.model_list_box["Structure Factors"]) |
---|
1908 | if self.plugin_rbutton.GetValue(): |
---|
1909 | ##fill the combobox with form factor list |
---|
1910 | self.structurebox.Disable() |
---|
1911 | self.formfactorbox.Clear() |
---|
1912 | self._populate_box( self.formfactorbox, |
---|
1913 | self.model_list_box["Customized Models"]) |
---|
1914 | |
---|
1915 | def _show_combox(self, event=None): |
---|
1916 | """ |
---|
1917 | Show combox box associate with type of model selected |
---|
1918 | """ |
---|
1919 | #if self.check_invalid_panel(): |
---|
1920 | # self.shape_rbutton.SetValue(True) |
---|
1921 | # return |
---|
1922 | self.Show(False) |
---|
1923 | self._show_combox_helper() |
---|
1924 | self._on_select_model(event=None) |
---|
1925 | self.Show(True) |
---|
1926 | self._save_typeOfmodel() |
---|
1927 | self.sizer4_4.Layout() |
---|
1928 | self.sizer4.Layout() |
---|
1929 | self.Layout() |
---|
1930 | self.Refresh() |
---|
1931 | |
---|
1932 | def _populate_box(self, combobox, list): |
---|
1933 | """ |
---|
1934 | fill combox box with dict item |
---|
1935 | |
---|
1936 | :param list: contains item to fill the combox |
---|
1937 | item must model class |
---|
1938 | """ |
---|
1939 | for models in list: |
---|
1940 | model= models() |
---|
1941 | name = model.__class__.__name__ |
---|
1942 | if models.__name__!="NoStructure": |
---|
1943 | if hasattr(model, "name"): |
---|
1944 | name = model.name |
---|
1945 | combobox.Append(name,models) |
---|
1946 | return 0 |
---|
1947 | |
---|
1948 | def _onQrangeEnter(self, event): |
---|
1949 | """ |
---|
1950 | Check validity of value enter in the Q range field |
---|
1951 | |
---|
1952 | """ |
---|
1953 | tcrtl = event.GetEventObject() |
---|
1954 | #Clear msg if previously shown. |
---|
1955 | msg = "" |
---|
1956 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
1957 | # Flag to register when a parameter has changed. |
---|
1958 | is_modified = False |
---|
1959 | if tcrtl.GetValue().lstrip().rstrip() != "": |
---|
1960 | try: |
---|
1961 | value = float(tcrtl.GetValue()) |
---|
1962 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1963 | # If qmin and qmax have been modified, update qmin and qmax |
---|
1964 | if self._validate_qrange(self.qmin, self.qmax): |
---|
1965 | tempmin = float(self.qmin.GetValue()) |
---|
1966 | if tempmin != self.qmin_x: |
---|
1967 | self.qmin_x = tempmin |
---|
1968 | tempmax = float(self.qmax.GetValue()) |
---|
1969 | if tempmax != self.qmax_x: |
---|
1970 | self.qmax_x = tempmax |
---|
1971 | else: |
---|
1972 | tcrtl.SetBackgroundColour("pink") |
---|
1973 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
1974 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
1975 | return |
---|
1976 | except: |
---|
1977 | tcrtl.SetBackgroundColour("pink") |
---|
1978 | msg = "Model Error:wrong value entered : %s" % sys.exc_value |
---|
1979 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
1980 | return |
---|
1981 | #Check if # of points for theory model are valid(>0). |
---|
1982 | if self.npts != None: |
---|
1983 | if check_float(self.npts): |
---|
1984 | temp_npts = float(self.npts.GetValue()) |
---|
1985 | if temp_npts != self.num_points: |
---|
1986 | self.num_points = temp_npts |
---|
1987 | is_modified = True |
---|
1988 | else: |
---|
1989 | msg = "Cannot Plot :No npts in that Qrange!!! " |
---|
1990 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
1991 | else: |
---|
1992 | tcrtl.SetBackgroundColour("pink") |
---|
1993 | msg = "Model Error:wrong value entered!!!" |
---|
1994 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
1995 | #self._undo.Enable(True) |
---|
1996 | self.save_current_state() |
---|
1997 | event = PageInfoEvent(page=self) |
---|
1998 | wx.PostEvent(self.parent, event) |
---|
1999 | self.state_change = False |
---|
2000 | #Draw the model for a different range |
---|
2001 | self.create_default_data() |
---|
2002 | self._draw_model() |
---|
2003 | |
---|
2004 | def _theory_qrange_enter(self, event): |
---|
2005 | """ |
---|
2006 | Check validity of value enter in the Q range field |
---|
2007 | """ |
---|
2008 | |
---|
2009 | tcrtl= event.GetEventObject() |
---|
2010 | #Clear msg if previously shown. |
---|
2011 | msg= "" |
---|
2012 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2013 | # Flag to register when a parameter has changed. |
---|
2014 | is_modified = False |
---|
2015 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
2016 | try: |
---|
2017 | value = float(tcrtl.GetValue()) |
---|
2018 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
2019 | |
---|
2020 | # If qmin and qmax have been modified, update qmin and qmax |
---|
2021 | if self._validate_qrange(self.theory_qmin, self.theory_qmax): |
---|
2022 | tempmin = float(self.theory_qmin.GetValue()) |
---|
2023 | if tempmin != self.theory_qmin_x: |
---|
2024 | self.theory_qmin_x = tempmin |
---|
2025 | tempmax = float(self.theory_qmax.GetValue()) |
---|
2026 | if tempmax != self.qmax_x: |
---|
2027 | self.theory_qmax_x = tempmax |
---|
2028 | else: |
---|
2029 | tcrtl.SetBackgroundColour("pink") |
---|
2030 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
2031 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg )) |
---|
2032 | return |
---|
2033 | except: |
---|
2034 | tcrtl.SetBackgroundColour("pink") |
---|
2035 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
2036 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg )) |
---|
2037 | return |
---|
2038 | #Check if # of points for theory model are valid(>0). |
---|
2039 | if self.Npts_total.IsEditable() : |
---|
2040 | if check_float(self.Npts_total): |
---|
2041 | temp_npts = float(self.Npts_total.GetValue()) |
---|
2042 | if temp_npts != self.num_points: |
---|
2043 | self.num_points = temp_npts |
---|
2044 | is_modified = True |
---|
2045 | else: |
---|
2046 | msg= "Cannot Plot :No npts in that Qrange!!! " |
---|
2047 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2048 | else: |
---|
2049 | tcrtl.SetBackgroundColour("pink") |
---|
2050 | msg = "Model Error:wrong value entered!!!" |
---|
2051 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
2052 | #self._undo.Enable(True) |
---|
2053 | self.save_current_state() |
---|
2054 | event = PageInfoEvent(page = self) |
---|
2055 | wx.PostEvent(self.parent, event) |
---|
2056 | self.state_change= False |
---|
2057 | #Draw the model for a different range |
---|
2058 | self.create_default_data() |
---|
2059 | self._draw_model() |
---|
2060 | |
---|
2061 | def _on_select_model_helper(self): |
---|
2062 | """ |
---|
2063 | call back for model selection |
---|
2064 | """ |
---|
2065 | ## reset dictionary containing reference to dispersion |
---|
2066 | self._disp_obj_dict = {} |
---|
2067 | self.disp_cb_dict ={} |
---|
2068 | self.temp_multi_functional = False |
---|
2069 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
2070 | #For MAC |
---|
2071 | form_factor = None |
---|
2072 | if f_id >= 0: |
---|
2073 | form_factor = self.formfactorbox.GetClientData(f_id) |
---|
2074 | |
---|
2075 | if not form_factor in self.model_list_box["multiplication"]: |
---|
2076 | self.structurebox.Hide() |
---|
2077 | self.text2.Hide() |
---|
2078 | self.structurebox.Disable() |
---|
2079 | self.structurebox.SetSelection(0) |
---|
2080 | self.text2.Disable() |
---|
2081 | else: |
---|
2082 | self.structurebox.Show() |
---|
2083 | self.text2.Show() |
---|
2084 | self.structurebox.Enable() |
---|
2085 | self.text2.Enable() |
---|
2086 | |
---|
2087 | if form_factor != None: |
---|
2088 | # set multifactor for Mutifunctional models |
---|
2089 | if form_factor().__class__ in self.model_list_box["Multi-Functions"]: |
---|
2090 | m_id = self.multifactorbox.GetCurrentSelection() |
---|
2091 | multiplicity = form_factor().multiplicity_info[0] |
---|
2092 | self.multifactorbox.Clear() |
---|
2093 | #self.mutifactor_text.SetLabel(form_factor().details[]) |
---|
2094 | self._set_multfactor_combobox(multiplicity) |
---|
2095 | self._show_multfactor_combobox() |
---|
2096 | #ToDo: this info should be called directly from the model |
---|
2097 | text = form_factor().multiplicity_info[1]#'No. of Shells: ' |
---|
2098 | |
---|
2099 | #self.mutifactor_text.Clear() |
---|
2100 | self.mutifactor_text.SetLabel(text) |
---|
2101 | if m_id > multiplicity -1: |
---|
2102 | # default value |
---|
2103 | m_id = 1 |
---|
2104 | |
---|
2105 | self.multi_factor = self.multifactorbox.GetClientData(m_id) |
---|
2106 | if self.multi_factor == None: self.multi_factor =0 |
---|
2107 | form_factor = form_factor(int(self.multi_factor)) |
---|
2108 | self.multifactorbox.SetSelection(m_id) |
---|
2109 | # Check len of the text1 and max_multiplicity |
---|
2110 | text = '' |
---|
2111 | if form_factor.multiplicity_info[0] == len(form_factor.multiplicity_info[2]): |
---|
2112 | text = form_factor.multiplicity_info[2][self.multi_factor] |
---|
2113 | self.mutifactor_text1.SetLabel(text) |
---|
2114 | # Check if model has get sld profile. |
---|
2115 | if len(form_factor.multiplicity_info[3]) > 0: |
---|
2116 | self.sld_axes = form_factor.multiplicity_info[3] |
---|
2117 | self.show_sld_button.Show(True) |
---|
2118 | else: |
---|
2119 | self.sld_axes = "" |
---|
2120 | |
---|
2121 | else: |
---|
2122 | self._hide_multfactor_combobox() |
---|
2123 | self.show_sld_button.Hide() |
---|
2124 | form_factor = form_factor() |
---|
2125 | self.multi_factor = None |
---|
2126 | else: |
---|
2127 | self._hide_multfactor_combobox() |
---|
2128 | self.show_sld_button.Hide() |
---|
2129 | self.multi_factor = None |
---|
2130 | |
---|
2131 | s_id = self.structurebox.GetCurrentSelection() |
---|
2132 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
2133 | |
---|
2134 | if struct_factor !=None: |
---|
2135 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
2136 | self.model= MultiplicationModel(form_factor,struct_factor()) |
---|
2137 | # multifunctional form factor |
---|
2138 | if len(form_factor.non_fittable) > 0: |
---|
2139 | self.temp_multi_functional = True |
---|
2140 | else: |
---|
2141 | if form_factor != None: |
---|
2142 | self.model= form_factor |
---|
2143 | else: |
---|
2144 | self.model = None |
---|
2145 | return self.model |
---|
2146 | |
---|
2147 | ## post state to fit panel |
---|
2148 | self.state.parameters =[] |
---|
2149 | self.state.model =self.model |
---|
2150 | self.state.qmin = self.qmin_x |
---|
2151 | self.state.multi_factor = self.multi_factor |
---|
2152 | self.disp_list =self.model.getDispParamList() |
---|
2153 | self.state.disp_list = self.disp_list |
---|
2154 | self.on_set_focus(None) |
---|
2155 | self.Layout() |
---|
2156 | |
---|
2157 | def _validate_qrange(self, qmin_ctrl, qmax_ctrl): |
---|
2158 | """ |
---|
2159 | Verify that the Q range controls have valid values |
---|
2160 | and that Qmin < Qmax. |
---|
2161 | |
---|
2162 | :param qmin_ctrl: text control for Qmin |
---|
2163 | :param qmax_ctrl: text control for Qmax |
---|
2164 | |
---|
2165 | :return: True is the Q range is value, False otherwise |
---|
2166 | |
---|
2167 | """ |
---|
2168 | qmin_validity = check_float(qmin_ctrl) |
---|
2169 | qmax_validity = check_float(qmax_ctrl) |
---|
2170 | if not (qmin_validity and qmax_validity): |
---|
2171 | return False |
---|
2172 | else: |
---|
2173 | qmin = float(qmin_ctrl.GetValue()) |
---|
2174 | qmax = float(qmax_ctrl.GetValue()) |
---|
2175 | if qmin < qmax: |
---|
2176 | #Make sure to set both colours white. |
---|
2177 | qmin_ctrl.SetBackgroundColour(wx.WHITE) |
---|
2178 | qmin_ctrl.Refresh() |
---|
2179 | qmax_ctrl.SetBackgroundColour(wx.WHITE) |
---|
2180 | qmax_ctrl.Refresh() |
---|
2181 | else: |
---|
2182 | qmin_ctrl.SetBackgroundColour("pink") |
---|
2183 | qmin_ctrl.Refresh() |
---|
2184 | qmax_ctrl.SetBackgroundColour("pink") |
---|
2185 | qmax_ctrl.Refresh() |
---|
2186 | msg= "Invalid Q range: Q min must be smaller than Q max" |
---|
2187 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
2188 | return False |
---|
2189 | return True |
---|
2190 | |
---|
2191 | def _validate_Npts(self): |
---|
2192 | """ |
---|
2193 | Validate the number of points for fitting is more than 10 points. |
---|
2194 | If valid, setvalues Npts_fit otherwise post msg. |
---|
2195 | """ |
---|
2196 | #default flag |
---|
2197 | flag = True |
---|
2198 | # Theory |
---|
2199 | if self.data == None and self.enable2D: |
---|
2200 | return flag |
---|
2201 | for data in self.data_list: |
---|
2202 | # q value from qx and qy |
---|
2203 | radius= numpy.sqrt( data.qx_data * data.qx_data + |
---|
2204 | data.qy_data * data.qy_data ) |
---|
2205 | #get unmasked index |
---|
2206 | index_data = (float(self.qmin.GetValue()) <= radius) & \ |
---|
2207 | (radius <= float(self.qmax.GetValue())) |
---|
2208 | index_data = (index_data) & (data.mask) |
---|
2209 | index_data = (index_data) & (numpy.isfinite(data.data)) |
---|
2210 | |
---|
2211 | if len(index_data[index_data]) < 10: |
---|
2212 | # change the color pink. |
---|
2213 | self.qmin.SetBackgroundColour("pink") |
---|
2214 | self.qmin.Refresh() |
---|
2215 | self.qmax.SetBackgroundColour("pink") |
---|
2216 | self.qmax.Refresh() |
---|
2217 | msg= "Npts of Data Error :No or too little npts of %s."% data.name |
---|
2218 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2219 | self.fitrange = False |
---|
2220 | flag = False |
---|
2221 | else: |
---|
2222 | self.Npts_fit.SetValue(str(len(index_data[index_data==True]))) |
---|
2223 | self.fitrange = True |
---|
2224 | |
---|
2225 | return flag |
---|
2226 | |
---|
2227 | def _validate_Npts_1D(self): |
---|
2228 | """ |
---|
2229 | Validate the number of points for fitting is more than 5 points. |
---|
2230 | If valid, setvalues Npts_fit otherwise post msg. |
---|
2231 | """ |
---|
2232 | #default flag |
---|
2233 | flag = True |
---|
2234 | # Theory |
---|
2235 | if self.data == None: |
---|
2236 | return flag |
---|
2237 | for data in self.data_list: |
---|
2238 | # q value from qx and qy |
---|
2239 | radius= data.x |
---|
2240 | #get unmasked index |
---|
2241 | index_data = (float(self.qmin.GetValue()) <= radius) & \ |
---|
2242 | (radius <= float(self.qmax.GetValue())) |
---|
2243 | index_data = (index_data) & (numpy.isfinite(data.y)) |
---|
2244 | |
---|
2245 | if len(index_data[index_data]) < 5: |
---|
2246 | # change the color pink. |
---|
2247 | self.qmin.SetBackgroundColour("pink") |
---|
2248 | self.qmin.Refresh() |
---|
2249 | self.qmax.SetBackgroundColour("pink") |
---|
2250 | self.qmax.Refresh() |
---|
2251 | msg= "Npts of Data Error :No or too little npts of %s."% data.name |
---|
2252 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2253 | self.fitrange = False |
---|
2254 | flag = False |
---|
2255 | else: |
---|
2256 | self.Npts_fit.SetValue(str(len(index_data[index_data==True]))) |
---|
2257 | self.fitrange = True |
---|
2258 | |
---|
2259 | return flag |
---|
2260 | |
---|
2261 | |
---|
2262 | |
---|
2263 | def _check_value_enter(self, list, modified): |
---|
2264 | """ |
---|
2265 | :param list: model parameter and panel info |
---|
2266 | :Note: each item of the list should be as follow: |
---|
2267 | item=[check button state, parameter's name, |
---|
2268 | paramater's value, string="+/-", |
---|
2269 | parameter's error of fit, |
---|
2270 | parameter's minimum value, |
---|
2271 | parrameter's maximum value , |
---|
2272 | parameter's units] |
---|
2273 | """ |
---|
2274 | is_modified = modified |
---|
2275 | if len(list)==0: |
---|
2276 | return is_modified |
---|
2277 | for item in list: |
---|
2278 | #skip angle parameters for 1D |
---|
2279 | if not self.enable2D:#self.data.__class__.__name__ !="Data2D": |
---|
2280 | if item in self.orientation_params: |
---|
2281 | continue |
---|
2282 | #try: |
---|
2283 | name = str(item[1]) |
---|
2284 | |
---|
2285 | if string.find(name,".npts") ==-1 and \ |
---|
2286 | string.find(name,".nsigmas")==-1: |
---|
2287 | ## check model parameters range |
---|
2288 | param_min= None |
---|
2289 | param_max= None |
---|
2290 | |
---|
2291 | ## check minimun value |
---|
2292 | if item[5]!= None and item[5]!= "": |
---|
2293 | if item[5].GetValue().lstrip().rstrip()!="": |
---|
2294 | try: |
---|
2295 | |
---|
2296 | param_min = float(item[5].GetValue()) |
---|
2297 | if not self._validate_qrange(item[5],item[2]): |
---|
2298 | if numpy.isfinite(param_min): |
---|
2299 | item[2].SetValue(format_number(param_min)) |
---|
2300 | |
---|
2301 | item[5].SetBackgroundColour(wx.WHITE) |
---|
2302 | item[2].SetBackgroundColour(wx.WHITE) |
---|
2303 | |
---|
2304 | except: |
---|
2305 | msg = "Wrong Fit parameter range entered " |
---|
2306 | wx.PostEvent(self.parent.parent, |
---|
2307 | StatusEvent(status = msg)) |
---|
2308 | raise ValueError, msg |
---|
2309 | is_modified = True |
---|
2310 | ## check maximum value |
---|
2311 | if item[6]!= None and item[6]!= "": |
---|
2312 | if item[6].GetValue().lstrip().rstrip()!="": |
---|
2313 | try: |
---|
2314 | param_max = float(item[6].GetValue()) |
---|
2315 | if not self._validate_qrange(item[2],item[6]): |
---|
2316 | if numpy.isfinite(param_max): |
---|
2317 | item[2].SetValue(format_number(param_max)) |
---|
2318 | |
---|
2319 | item[6].SetBackgroundColour(wx.WHITE) |
---|
2320 | item[2].SetBackgroundColour(wx.WHITE) |
---|
2321 | except: |
---|
2322 | msg = "Wrong Fit parameter range entered " |
---|
2323 | wx.PostEvent(self.parent.parent, |
---|
2324 | StatusEvent(status = msg)) |
---|
2325 | raise ValueError, msg |
---|
2326 | is_modified = True |
---|
2327 | |
---|
2328 | |
---|
2329 | if param_min != None and param_max !=None: |
---|
2330 | if not self._validate_qrange(item[5], item[6]): |
---|
2331 | msg= "Wrong Fit range entered for parameter " |
---|
2332 | msg+= "name %s of model %s "%(name, self.model.name) |
---|
2333 | wx.PostEvent(self.parent.parent, |
---|
2334 | StatusEvent(status = msg)) |
---|
2335 | |
---|
2336 | if name in self.model.details.keys(): |
---|
2337 | self.model.details[name][1:3] = param_min, param_max |
---|
2338 | is_modified = True |
---|
2339 | |
---|
2340 | else: |
---|
2341 | self.model.details [name] = ["", param_min, param_max] |
---|
2342 | is_modified = True |
---|
2343 | try: |
---|
2344 | # Check if the textctr is enabled |
---|
2345 | if item[2].IsEnabled(): |
---|
2346 | value= float(item[2].GetValue()) |
---|
2347 | item[2].SetBackgroundColour("white") |
---|
2348 | # If the value of the parameter has changed, |
---|
2349 | # +update the model and set the is_modified flag |
---|
2350 | if value != self.model.getParam(name) and \ |
---|
2351 | numpy.isfinite(value): |
---|
2352 | self.model.setParam(name, value) |
---|
2353 | |
---|
2354 | except: |
---|
2355 | item[2].SetBackgroundColour("pink") |
---|
2356 | msg = "Wrong Fit parameter value entered " |
---|
2357 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg)) |
---|
2358 | |
---|
2359 | return is_modified |
---|
2360 | |
---|
2361 | |
---|
2362 | def _set_dipers_Param(self, event): |
---|
2363 | """ |
---|
2364 | respond to self.enable_disp and self.disable_disp radio box. |
---|
2365 | The dispersity object is reset inside the model into Gaussian. |
---|
2366 | When the user select yes , this method display a combo box for more selection |
---|
2367 | when the user selects No,the combo box disappears. |
---|
2368 | Redraw the model with the default dispersity (Gaussian) |
---|
2369 | """ |
---|
2370 | #if self.check_invalid_panel(): |
---|
2371 | # return |
---|
2372 | ## On selction if no model exists. |
---|
2373 | if self.model ==None: |
---|
2374 | self.disable_disp.SetValue(True) |
---|
2375 | msg="Please select a Model first..." |
---|
2376 | wx.MessageBox(msg, 'Info') |
---|
2377 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2378 | "Polydispersion: %s"%msg)) |
---|
2379 | return |
---|
2380 | |
---|
2381 | self._reset_dispersity() |
---|
2382 | |
---|
2383 | if self.model ==None: |
---|
2384 | self.model_disp.Hide() |
---|
2385 | self.sizer4_4.Clear(True) |
---|
2386 | return |
---|
2387 | |
---|
2388 | if self.enable_disp.GetValue(): |
---|
2389 | ## layout for model containing no dispersity parameters |
---|
2390 | |
---|
2391 | self.disp_list= self.model.getDispParamList() |
---|
2392 | |
---|
2393 | if len(self.disp_list)==0 and len(self.disp_cb_dict)==0: |
---|
2394 | self._layout_sizer_noDipers() |
---|
2395 | else: |
---|
2396 | ## set gaussian sizer |
---|
2397 | self._on_select_Disp(event=None) |
---|
2398 | else: |
---|
2399 | self.sizer4_4.Clear(True) |
---|
2400 | |
---|
2401 | ## post state to fit panel |
---|
2402 | self.save_current_state() |
---|
2403 | if event !=None: |
---|
2404 | #self._undo.Enable(True) |
---|
2405 | event = PageInfoEvent(page = self) |
---|
2406 | wx.PostEvent(self.parent, event) |
---|
2407 | #draw the model with the current dispersity |
---|
2408 | self._draw_model() |
---|
2409 | self.sizer4_4.Layout() |
---|
2410 | self.sizer5.Layout() |
---|
2411 | self.Layout() |
---|
2412 | self.Refresh() |
---|
2413 | |
---|
2414 | |
---|
2415 | def _layout_sizer_noDipers(self): |
---|
2416 | """ |
---|
2417 | Draw a sizer with no dispersity info |
---|
2418 | """ |
---|
2419 | ix=0 |
---|
2420 | iy=1 |
---|
2421 | self.fittable_param=[] |
---|
2422 | self.fixed_param=[] |
---|
2423 | self.orientation_params_disp=[] |
---|
2424 | |
---|
2425 | self.sizer4_4.Clear(True) |
---|
2426 | text = "No polydispersity available for this model" |
---|
2427 | text = "No polydispersity available for this model" |
---|
2428 | model_disp = wx.StaticText(self, -1, text) |
---|
2429 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), |
---|
2430 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
2431 | self.sizer4_4.Layout() |
---|
2432 | self.sizer4.Layout() |
---|
2433 | |
---|
2434 | def _reset_dispersity(self): |
---|
2435 | """ |
---|
2436 | put gaussian dispersity into current model |
---|
2437 | """ |
---|
2438 | if len(self.param_toFit)>0: |
---|
2439 | for item in self.fittable_param: |
---|
2440 | if item in self.param_toFit: |
---|
2441 | self.param_toFit.remove(item) |
---|
2442 | |
---|
2443 | for item in self.orientation_params_disp: |
---|
2444 | if item in self.param_toFit: |
---|
2445 | self.param_toFit.remove(item) |
---|
2446 | |
---|
2447 | self.fittable_param=[] |
---|
2448 | self.fixed_param=[] |
---|
2449 | self.orientation_params_disp=[] |
---|
2450 | self.values={} |
---|
2451 | self.weights={} |
---|
2452 | |
---|
2453 | from sans.models.dispersion_models import GaussianDispersion, ArrayDispersion |
---|
2454 | if len(self.disp_cb_dict)==0: |
---|
2455 | self.save_current_state() |
---|
2456 | self.sizer4_4.Clear(True) |
---|
2457 | self.Layout() |
---|
2458 | |
---|
2459 | return |
---|
2460 | if (len(self.disp_cb_dict)>0) : |
---|
2461 | for p in self.disp_cb_dict: |
---|
2462 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
2463 | disp_model = GaussianDispersion() |
---|
2464 | |
---|
2465 | self._disp_obj_dict[p] = disp_model |
---|
2466 | # Set the new model as the dispersion object for the selected parameter |
---|
2467 | try: |
---|
2468 | self.model.set_dispersion(p, disp_model) |
---|
2469 | except: |
---|
2470 | |
---|
2471 | pass |
---|
2472 | |
---|
2473 | ## save state into |
---|
2474 | self.save_current_state() |
---|
2475 | self.Layout() |
---|
2476 | self.Refresh() |
---|
2477 | |
---|
2478 | def _on_select_Disp(self,event): |
---|
2479 | """ |
---|
2480 | allow selecting different dispersion |
---|
2481 | self.disp_list should change type later .now only gaussian |
---|
2482 | """ |
---|
2483 | self._set_sizer_dispersion() |
---|
2484 | |
---|
2485 | ## Redraw the model |
---|
2486 | self._draw_model() |
---|
2487 | #self._undo.Enable(True) |
---|
2488 | event = PageInfoEvent(page = self) |
---|
2489 | wx.PostEvent(self.parent, event) |
---|
2490 | |
---|
2491 | self.sizer4_4.Layout() |
---|
2492 | self.sizer4.Layout() |
---|
2493 | self.SetupScrolling() |
---|
2494 | |
---|
2495 | def _on_disp_func(self, event=None): |
---|
2496 | """ |
---|
2497 | Select a distribution function for the polydispersion |
---|
2498 | |
---|
2499 | :Param event: ComboBox event |
---|
2500 | """ |
---|
2501 | # get ready for new event |
---|
2502 | if event != None: |
---|
2503 | event.Skip() |
---|
2504 | # Get event object |
---|
2505 | disp_box = event.GetEventObject() |
---|
2506 | |
---|
2507 | # Try to select a Distr. function |
---|
2508 | try: |
---|
2509 | disp_box.SetBackgroundColour("white") |
---|
2510 | selection = disp_box.GetCurrentSelection() |
---|
2511 | param_name = disp_box.Name.split('.')[0] |
---|
2512 | disp_name = disp_box.GetValue() |
---|
2513 | dispersity= disp_box.GetClientData(selection) |
---|
2514 | |
---|
2515 | #disp_model = GaussianDispersion() |
---|
2516 | disp_model = dispersity() |
---|
2517 | # Get param names to reset the values of the param |
---|
2518 | name1 = param_name + ".width" |
---|
2519 | name2 = param_name + ".npts" |
---|
2520 | name3 = param_name + ".nsigmas" |
---|
2521 | # Check Disp. function whether or not it is 'array' |
---|
2522 | if disp_name.lower() == "array": |
---|
2523 | value2= "" |
---|
2524 | value3= "" |
---|
2525 | value1 = self._set_array_disp(name=name1, disp=disp_model) |
---|
2526 | else: |
---|
2527 | self._del_array_values(name1) |
---|
2528 | #self._reset_array_disp(param_name) |
---|
2529 | self._disp_obj_dict[name1] = disp_model |
---|
2530 | self.model.set_dispersion(param_name, disp_model) |
---|
2531 | self.state._disp_obj_dict[name1]= disp_model |
---|
2532 | |
---|
2533 | value1= str(format_number(self.model.getParam(name1), True)) |
---|
2534 | value2= str(format_number(self.model.getParam(name2))) |
---|
2535 | value3= str(format_number(self.model.getParam(name3))) |
---|
2536 | # Reset fittable polydispersin parameter value |
---|
2537 | for item in self.fittable_param: |
---|
2538 | if item[1] == name1: |
---|
2539 | item[2].SetValue(value1) |
---|
2540 | item[5].SetValue("") |
---|
2541 | item[6].SetValue("") |
---|
2542 | # Disable for array |
---|
2543 | if disp_name.lower() == "array": |
---|
2544 | item[0].SetValue(False) |
---|
2545 | item[0].Disable() |
---|
2546 | item[2].Disable() |
---|
2547 | item[3].Show(False) |
---|
2548 | item[4].Show(False) |
---|
2549 | item[5].Disable() |
---|
2550 | item[6].Disable() |
---|
2551 | else: |
---|
2552 | item[0].Enable() |
---|
2553 | item[2].Enable() |
---|
2554 | item[5].Enable() |
---|
2555 | item[6].Enable() |
---|
2556 | break |
---|
2557 | # Reset fixed polydispersion params |
---|
2558 | for item in self.fixed_param: |
---|
2559 | if item[1] == name2: |
---|
2560 | item[2].SetValue(value2) |
---|
2561 | # Disable Npts for array |
---|
2562 | if disp_name.lower() == "array": |
---|
2563 | item[2].Disable() |
---|
2564 | else: |
---|
2565 | item[2].Enable() |
---|
2566 | if item[1] == name3: |
---|
2567 | item[2].SetValue(value3) |
---|
2568 | # Disable Nsigs for array |
---|
2569 | if disp_name.lower() == "array": |
---|
2570 | item[2].Disable() |
---|
2571 | else: |
---|
2572 | item[2].Enable() |
---|
2573 | |
---|
2574 | # Make sure the check box updated when all checked |
---|
2575 | if self.cb1.GetValue(): |
---|
2576 | #self.select_all_param(None) |
---|
2577 | self.get_all_checked_params() |
---|
2578 | |
---|
2579 | # update params |
---|
2580 | self._update_paramv_on_fit() |
---|
2581 | # draw |
---|
2582 | self._draw_model() |
---|
2583 | self.Refresh() |
---|
2584 | except: |
---|
2585 | # Error msg |
---|
2586 | msg = "Error occurred:" |
---|
2587 | msg += " Could not select the distribution function..." |
---|
2588 | msg += " Please select another distribution function." |
---|
2589 | disp_box.SetBackgroundColour("pink") |
---|
2590 | # Focus on Fit button so that users can see the pinky box |
---|
2591 | self.btFit.SetFocus() |
---|
2592 | wx.PostEvent(self.parent.parent, |
---|
2593 | StatusEvent(status=msg, info="error")) |
---|
2594 | |
---|
2595 | |
---|
2596 | def _set_array_disp(self, name=None, disp=None): |
---|
2597 | """ |
---|
2598 | Set array dispersion |
---|
2599 | |
---|
2600 | :param name: name of the parameter for the dispersion to be set |
---|
2601 | :param disp: the polydisperion object |
---|
2602 | """ |
---|
2603 | # The user wants this parameter to be averaged. |
---|
2604 | # Pop up the file selection dialog. |
---|
2605 | path = self._selectDlg() |
---|
2606 | # Array data |
---|
2607 | values = [] |
---|
2608 | weights = [] |
---|
2609 | # If nothing was selected, just return |
---|
2610 | if path is None: |
---|
2611 | self.disp_cb_dict[name].SetValue(False) |
---|
2612 | #self.noDisper_rbox.SetValue(True) |
---|
2613 | return |
---|
2614 | self._default_save_location = os.path.dirname(path) |
---|
2615 | if self.parent != None: |
---|
2616 | self.parent.parent._default_save_location =\ |
---|
2617 | self._default_save_location |
---|
2618 | |
---|
2619 | basename = os.path.basename(path) |
---|
2620 | values,weights = self.read_file(path) |
---|
2621 | |
---|
2622 | # If any of the two arrays is empty, notify the user that we won't |
---|
2623 | # proceed |
---|
2624 | if len(self.param_toFit)>0: |
---|
2625 | if name in self.param_toFit: |
---|
2626 | self.param_toFit.remove(name) |
---|
2627 | |
---|
2628 | # Tell the user that we are about to apply the distribution |
---|
2629 | msg = "Applying loaded %s distribution: %s" % (name, path) |
---|
2630 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
2631 | self._set_array_disp_model(name=name, disp=disp, |
---|
2632 | values=values, weights=weights) |
---|
2633 | return basename |
---|
2634 | |
---|
2635 | def _set_array_disp_model(self, name=None, disp=None, |
---|
2636 | values=[], weights=[]): |
---|
2637 | """ |
---|
2638 | Set array dispersion model |
---|
2639 | |
---|
2640 | :param name: name of the parameter for the dispersion to be set |
---|
2641 | :param disp: the polydisperion object |
---|
2642 | """ |
---|
2643 | disp.set_weights(values, weights) |
---|
2644 | self._disp_obj_dict[name] = disp |
---|
2645 | self.model.set_dispersion(name.split('.')[0], disp) |
---|
2646 | self.state._disp_obj_dict[name]= disp |
---|
2647 | self.values[name] = values |
---|
2648 | self.weights[name] = weights |
---|
2649 | # Store the object to make it persist outside the |
---|
2650 | # scope of this method |
---|
2651 | #TODO: refactor model to clean this up? |
---|
2652 | self.state.values = {} |
---|
2653 | self.state.weights = {} |
---|
2654 | self.state.values = copy.deepcopy(self.values) |
---|
2655 | self.state.weights = copy.deepcopy(self.weights) |
---|
2656 | |
---|
2657 | # Set the new model as the dispersion object for the |
---|
2658 | #selected parameter |
---|
2659 | #self.model.set_dispersion(p, disp_model) |
---|
2660 | # Store a reference to the weights in the model object |
---|
2661 | #so that |
---|
2662 | # it's not lost when we use the model within another thread. |
---|
2663 | #TODO: total hack - fix this |
---|
2664 | self.state.model= self.model.clone() |
---|
2665 | self.model._persistency_dict[name.split('.')[0]] = \ |
---|
2666 | [values, weights] |
---|
2667 | self.state.model._persistency_dict[name.split('.')[0]] = \ |
---|
2668 | [values,weights] |
---|
2669 | |
---|
2670 | |
---|
2671 | def _del_array_values(self, name=None): |
---|
2672 | """ |
---|
2673 | Reset array dispersion |
---|
2674 | |
---|
2675 | :param name: name of the parameter for the dispersion to be set |
---|
2676 | """ |
---|
2677 | # Try to delete values and weight of the names array dic if exists |
---|
2678 | try: |
---|
2679 | del self.values[name] |
---|
2680 | del self.weights[name] |
---|
2681 | # delete all other dic |
---|
2682 | del self.state.values[name] |
---|
2683 | del self.state.weights[name] |
---|
2684 | del self.model._persistency_dict[name.split('.')[0]] |
---|
2685 | del self.state.model._persistency_dict[name.split('.')[0]] |
---|
2686 | except: |
---|
2687 | pass |
---|
2688 | |
---|
2689 | def _lay_out(self): |
---|
2690 | """ |
---|
2691 | returns self.Layout |
---|
2692 | |
---|
2693 | :Note: Mac seems to like this better when self. |
---|
2694 | Layout is called after fitting. |
---|
2695 | """ |
---|
2696 | self._sleep4sec() |
---|
2697 | self.Layout() |
---|
2698 | return |
---|
2699 | |
---|
2700 | def _sleep4sec(self): |
---|
2701 | """ |
---|
2702 | sleep for 1 sec only applied on Mac |
---|
2703 | Note: This 1sec helps for Mac not to crash on self.:ayout after self._draw_model |
---|
2704 | """ |
---|
2705 | if ON_MAC == True: |
---|
2706 | time.sleep(1) |
---|
2707 | |
---|
2708 | def _find_polyfunc_selection(self, disp_func = None): |
---|
2709 | """ |
---|
2710 | FInd Comboox selection from disp_func |
---|
2711 | |
---|
2712 | :param disp_function: dispersion distr. function |
---|
2713 | """ |
---|
2714 | # List of the poly_model name in the combobox |
---|
2715 | list = ["RectangleDispersion", "ArrayDispersion", |
---|
2716 | "LogNormalDispersion", "GaussianDispersion", |
---|
2717 | "SchulzDispersion"] |
---|
2718 | |
---|
2719 | # Find the selection |
---|
2720 | try: |
---|
2721 | selection = list.index(disp_func.__class__.__name__) |
---|
2722 | return selection |
---|
2723 | except: |
---|
2724 | return 3 |
---|
2725 | |
---|
2726 | def on_reset_clicked(self,event): |
---|
2727 | """ |
---|
2728 | On 'Reset' button for Q range clicked |
---|
2729 | """ |
---|
2730 | flag = True |
---|
2731 | #if self.check_invalid_panel(): |
---|
2732 | # return |
---|
2733 | ##For 3 different cases: Data2D, Data1D, and theory |
---|
2734 | if self.model == None: |
---|
2735 | msg="Please select a model first..." |
---|
2736 | wx.MessageBox(msg, 'Info') |
---|
2737 | flag = False |
---|
2738 | return |
---|
2739 | |
---|
2740 | elif self.data.__class__.__name__ == "Data2D": |
---|
2741 | data_min= 0 |
---|
2742 | x= max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
2743 | y= max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
2744 | self.qmin_x = data_min |
---|
2745 | self.qmax_x = math.sqrt(x*x + y*y) |
---|
2746 | #self.data.mask = numpy.ones(len(self.data.data),dtype=bool) |
---|
2747 | # check smearing |
---|
2748 | if not self.disable_smearer.GetValue(): |
---|
2749 | temp_smearer= self.current_smearer |
---|
2750 | ## set smearing value whether or not the data contain the smearing info |
---|
2751 | if self.pinhole_smearer.GetValue(): |
---|
2752 | flag = self.update_pinhole_smear() |
---|
2753 | else: |
---|
2754 | flag = True |
---|
2755 | |
---|
2756 | elif self.data == None: |
---|
2757 | self.qmin_x = _QMIN_DEFAULT |
---|
2758 | self.qmax_x = _QMAX_DEFAULT |
---|
2759 | self.num_points = _NPTS_DEFAULT |
---|
2760 | self.state.npts = self.num_points |
---|
2761 | |
---|
2762 | elif self.data.__class__.__name__ != "Data2D": |
---|
2763 | self.qmin_x = min(self.data.x) |
---|
2764 | self.qmax_x = max(self.data.x) |
---|
2765 | # check smearing |
---|
2766 | if not self.disable_smearer.GetValue(): |
---|
2767 | temp_smearer= self.current_smearer |
---|
2768 | ## set smearing value whether or not the data contain the smearing info |
---|
2769 | if self.slit_smearer.GetValue(): |
---|
2770 | flag = self.update_slit_smear() |
---|
2771 | elif self.pinhole_smearer.GetValue(): |
---|
2772 | flag = self.update_pinhole_smear() |
---|
2773 | else: |
---|
2774 | flag = True |
---|
2775 | else: |
---|
2776 | flag = False |
---|
2777 | |
---|
2778 | if flag == False: |
---|
2779 | msg= "Cannot Plot :Must enter a number!!! " |
---|
2780 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2781 | else: |
---|
2782 | # set relative text ctrs. |
---|
2783 | self.qmin.SetValue(str(self.qmin_x)) |
---|
2784 | self.qmax.SetValue(str(self.qmax_x)) |
---|
2785 | self.set_npts2fit() |
---|
2786 | # At this point, some button and variables satatus (disabled?) should be checked |
---|
2787 | # such as color that should be reset to white in case that it was pink. |
---|
2788 | self._onparamEnter_helper() |
---|
2789 | |
---|
2790 | self.save_current_state() |
---|
2791 | self.state.qmin = self.qmin_x |
---|
2792 | self.state.qmax = self.qmax_x |
---|
2793 | |
---|
2794 | #reset the q range values |
---|
2795 | self._reset_plotting_range(self.state) |
---|
2796 | #self.compute_chisqr(smearer=self.current_smearer) |
---|
2797 | #Re draw plot |
---|
2798 | self._draw_model() |
---|
2799 | |
---|
2800 | def get_images(self): |
---|
2801 | """ |
---|
2802 | Get the images of the plots corresponding this panel for report |
---|
2803 | |
---|
2804 | : return graphs: list of figures |
---|
2805 | : TODO: Move to guiframe |
---|
2806 | """ |
---|
2807 | # set list of graphs |
---|
2808 | graphs = [] |
---|
2809 | canvases = [] |
---|
2810 | # call gui_manager |
---|
2811 | gui_manager = self.parent.parent |
---|
2812 | # loops through the panels [dic] |
---|
2813 | for item1, item2 in gui_manager.plot_panels.iteritems(): |
---|
2814 | data_title = self.data.group_id |
---|
2815 | data_name = str(self.data.name).split(" [")[0] |
---|
2816 | # try to get all plots belonging to this control panel |
---|
2817 | try: |
---|
2818 | title = '' |
---|
2819 | # check titles (main plot) |
---|
2820 | if hasattr(item2,"data2D"): |
---|
2821 | title = item2.data2D.title |
---|
2822 | # and data_names (model plot[2D], and residuals) |
---|
2823 | if item2.group_id == data_title or \ |
---|
2824 | item2.group_id.count("res" + str(self.graph_id)) or \ |
---|
2825 | item2.group_id.count(str(self.uid)) > 0: |
---|
2826 | #panel = gui_manager._mgr.GetPane(item2.window_name) |
---|
2827 | # append to the list |
---|
2828 | graphs.append(item2.figure) |
---|
2829 | canvases.append(item2.canvas) |
---|
2830 | except: |
---|
2831 | # Not for control panels |
---|
2832 | pass |
---|
2833 | # return the list of graphs |
---|
2834 | return graphs, canvases |
---|
2835 | |
---|
2836 | def on_model_help_clicked(self,event): |
---|
2837 | """ |
---|
2838 | on 'More details' button |
---|
2839 | """ |
---|
2840 | from help_panel import HelpWindow |
---|
2841 | from sans.models import get_data_path |
---|
2842 | |
---|
2843 | # Get models help model_function path |
---|
2844 | path = get_data_path(media='media') |
---|
2845 | model_path = os.path.join(path,"model_functions.html") |
---|
2846 | if self.model == None: |
---|
2847 | name = 'FuncHelp' |
---|
2848 | else: |
---|
2849 | name = self.formfactorbox.GetValue() |
---|
2850 | #name = self.model.__class__.__name__ |
---|
2851 | frame = HelpWindow(None, -1, pageToOpen=model_path) |
---|
2852 | frame.Show(True) |
---|
2853 | if frame.rhelp.HasAnchor(name): |
---|
2854 | frame.rhelp.ScrollToAnchor(name) |
---|
2855 | else: |
---|
2856 | msg= "Model does not contains an available description " |
---|
2857 | msg +="Please try searching in the Help window" |
---|
2858 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
2859 | |
---|
2860 | def on_pd_help_clicked(self, event): |
---|
2861 | """ |
---|
2862 | Button event for PD help |
---|
2863 | """ |
---|
2864 | from help_panel import HelpWindow |
---|
2865 | import sans.models as models |
---|
2866 | |
---|
2867 | # Get models help model_function path |
---|
2868 | path = models.get_data_path(media='media') |
---|
2869 | pd_path = os.path.join(path,"pd_help.html") |
---|
2870 | |
---|
2871 | frame = HelpWindow(None, -1, pageToOpen=pd_path) |
---|
2872 | frame.Show(True) |
---|
2873 | |
---|
2874 | def on_left_down(self, event): |
---|
2875 | """ |
---|
2876 | Get key stroke event |
---|
2877 | """ |
---|
2878 | # Figuring out key combo: Cmd for copy, Alt for paste |
---|
2879 | if event.CmdDown() and event.ShiftDown(): |
---|
2880 | flag = self.get_paste() |
---|
2881 | elif event.CmdDown(): |
---|
2882 | flag = self.get_copy() |
---|
2883 | else: |
---|
2884 | event.Skip() |
---|
2885 | return |
---|
2886 | # make event free |
---|
2887 | event.Skip() |
---|
2888 | |
---|
2889 | def get_copy(self): |
---|
2890 | """ |
---|
2891 | Get copy params to clipboard |
---|
2892 | """ |
---|
2893 | content = self.get_copy_params() |
---|
2894 | flag = self.set_clipboard(content) |
---|
2895 | self._copy_info(flag) |
---|
2896 | return flag |
---|
2897 | |
---|
2898 | def get_copy_params(self): |
---|
2899 | """ |
---|
2900 | Get the string copies of the param names and values in the tap |
---|
2901 | """ |
---|
2902 | content = 'sansview_parameter_values:' |
---|
2903 | # Do it if params exist |
---|
2904 | if self.parameters !=[]: |
---|
2905 | |
---|
2906 | # go through the parameters |
---|
2907 | string = self._get_copy_helper(self.parameters, |
---|
2908 | self.orientation_params) |
---|
2909 | content += string |
---|
2910 | |
---|
2911 | # go through the fittables |
---|
2912 | string = self._get_copy_helper(self.fittable_param, |
---|
2913 | self.orientation_params_disp) |
---|
2914 | content += string |
---|
2915 | |
---|
2916 | # go through the fixed params |
---|
2917 | string = self._get_copy_helper(self.fixed_param, |
---|
2918 | self.orientation_params_disp) |
---|
2919 | content += string |
---|
2920 | |
---|
2921 | # go through the str params |
---|
2922 | string = self._get_copy_helper(self.str_parameters, |
---|
2923 | self.orientation_params) |
---|
2924 | content += string |
---|
2925 | return content |
---|
2926 | else: |
---|
2927 | return False |
---|
2928 | |
---|
2929 | def set_clipboard(self, content=None): |
---|
2930 | """ |
---|
2931 | Put the string to the clipboard |
---|
2932 | """ |
---|
2933 | if not content: |
---|
2934 | return False |
---|
2935 | if wx.TheClipboard.Open(): |
---|
2936 | wx.TheClipboard.SetData(wx.TextDataObject(str(content))) |
---|
2937 | data = wx.TextDataObject() |
---|
2938 | success = wx.TheClipboard.GetData(data) |
---|
2939 | text = data.GetText() |
---|
2940 | wx.TheClipboard.Close() |
---|
2941 | return True |
---|
2942 | return None |
---|
2943 | |
---|
2944 | def _get_copy_helper(self, param, orient_param): |
---|
2945 | """ |
---|
2946 | Helping get value and name of the params |
---|
2947 | |
---|
2948 | : param param: parameters |
---|
2949 | : param orient_param = oritational params |
---|
2950 | : return content: strings [list] [name,value:....] |
---|
2951 | """ |
---|
2952 | content = '' |
---|
2953 | # go through the str params |
---|
2954 | for item in param: |
---|
2955 | disfunc = '' |
---|
2956 | try: |
---|
2957 | if item[7].__class__.__name__ == 'ComboBox': |
---|
2958 | disfunc = str(item[7].GetValue()) |
---|
2959 | except: |
---|
2960 | pass |
---|
2961 | |
---|
2962 | # 2D |
---|
2963 | if self.data.__class__.__name__== "Data2D": |
---|
2964 | try: |
---|
2965 | check = item[0].GetValue() |
---|
2966 | except: |
---|
2967 | check = None |
---|
2968 | name = item[1] |
---|
2969 | value = item[2].GetValue() |
---|
2970 | # 1D |
---|
2971 | else: |
---|
2972 | ## for 1D all parameters except orientation |
---|
2973 | if not item[1] in orient_param: |
---|
2974 | try: |
---|
2975 | check = item[0].GetValue() |
---|
2976 | except: |
---|
2977 | check = None |
---|
2978 | name = item[1] |
---|
2979 | value = item[2].GetValue() |
---|
2980 | |
---|
2981 | # add to the content |
---|
2982 | if disfunc != '': |
---|
2983 | |
---|
2984 | disfunc = ',' + disfunc |
---|
2985 | # TODO: to support array func for copy/paste |
---|
2986 | try: |
---|
2987 | if disfunc.count('array') > 0: |
---|
2988 | disfunc += ',' |
---|
2989 | for val in self.values[name]: |
---|
2990 | disfunc += ' ' + str(val) |
---|
2991 | disfunc += ',' |
---|
2992 | for weight in self.weights[name]: |
---|
2993 | disfunc += ' ' + str(weight) |
---|
2994 | except: |
---|
2995 | pass |
---|
2996 | #if disfunc.count('array') == 0: |
---|
2997 | content += name + ',' + str(check) + ',' + value + disfunc + ':' |
---|
2998 | |
---|
2999 | return content |
---|
3000 | |
---|
3001 | def get_clipboard(self): |
---|
3002 | """ |
---|
3003 | Get strings in the clipboard |
---|
3004 | """ |
---|
3005 | text = "" |
---|
3006 | # Get text from the clip board |
---|
3007 | if wx.TheClipboard.Open(): |
---|
3008 | if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): |
---|
3009 | data = wx.TextDataObject() |
---|
3010 | # get wx dataobject |
---|
3011 | success = wx.TheClipboard.GetData(data) |
---|
3012 | # get text |
---|
3013 | text = data.GetText() |
---|
3014 | # close clipboard |
---|
3015 | wx.TheClipboard.Close() |
---|
3016 | |
---|
3017 | return text |
---|
3018 | |
---|
3019 | def get_paste(self): |
---|
3020 | """ |
---|
3021 | Paste params from the clipboard |
---|
3022 | """ |
---|
3023 | text = self.get_clipboard() |
---|
3024 | flag = self.get_paste_params(text) |
---|
3025 | self._copy_info(flag) |
---|
3026 | return flag |
---|
3027 | |
---|
3028 | def get_paste_params(self, text=''): |
---|
3029 | """ |
---|
3030 | Get the string copies of the param names and values in the tap |
---|
3031 | """ |
---|
3032 | context = {} |
---|
3033 | # put the text into dictionary |
---|
3034 | lines = text.split(':') |
---|
3035 | if lines[0] != 'sansview_parameter_values': |
---|
3036 | self._copy_info(False) |
---|
3037 | return False |
---|
3038 | for line in lines[1:-1]: |
---|
3039 | if len(line) != 0: |
---|
3040 | item =line.split(',') |
---|
3041 | check = item[1] |
---|
3042 | name = item[0] |
---|
3043 | value = item[2] |
---|
3044 | # Transfer the text to content[dictionary] |
---|
3045 | context[name] = [check, value] |
---|
3046 | # ToDo: PlugIn this poly disp function for pasting |
---|
3047 | try: |
---|
3048 | poly_func = item[3] |
---|
3049 | context[name].append(poly_func) |
---|
3050 | try: |
---|
3051 | # take the vals and weights for array |
---|
3052 | array_values = item[4].split(' ') |
---|
3053 | array_weights = item[5].split(' ') |
---|
3054 | val = [float(a_val) for a_val in array_values[1:]] |
---|
3055 | weit = [float(a_weit) for a_weit in array_weights[1:]] |
---|
3056 | |
---|
3057 | context[name].append(val) |
---|
3058 | context[name].append(weit) |
---|
3059 | except: |
---|
3060 | raise |
---|
3061 | except: |
---|
3062 | poly_func = '' |
---|
3063 | context[name].append(poly_func) |
---|
3064 | |
---|
3065 | # Do it if params exist |
---|
3066 | if self.parameters != []: |
---|
3067 | # go through the parameters |
---|
3068 | self._get_paste_helper(self.parameters, |
---|
3069 | self.orientation_params, context) |
---|
3070 | |
---|
3071 | # go through the fittables |
---|
3072 | self._get_paste_helper(self.fittable_param, |
---|
3073 | self.orientation_params_disp, |
---|
3074 | context) |
---|
3075 | |
---|
3076 | # go through the fixed params |
---|
3077 | self._get_paste_helper(self.fixed_param, |
---|
3078 | self.orientation_params_disp, context) |
---|
3079 | |
---|
3080 | # go through the str params |
---|
3081 | self._get_paste_helper(self.str_parameters, |
---|
3082 | self.orientation_params, context) |
---|
3083 | |
---|
3084 | return True |
---|
3085 | return None |
---|
3086 | |
---|
3087 | def _get_paste_helper(self, param, orient_param, content): |
---|
3088 | """ |
---|
3089 | Helping set values of the params |
---|
3090 | |
---|
3091 | : param param: parameters |
---|
3092 | : param orient_param: oritational params |
---|
3093 | : param content: dictionary [ name, value: name1.value1,...] |
---|
3094 | """ |
---|
3095 | # go through the str params |
---|
3096 | for item in param: |
---|
3097 | # 2D |
---|
3098 | if self.data.__class__.__name__== "Data2D": |
---|
3099 | name = item[1] |
---|
3100 | if name in content.keys(): |
---|
3101 | check = content[name][0] |
---|
3102 | pd = content[name][1] |
---|
3103 | if name.count('.') > 0: |
---|
3104 | try: |
---|
3105 | float(pd) |
---|
3106 | except: |
---|
3107 | #continue |
---|
3108 | if not pd and pd != '': |
---|
3109 | continue |
---|
3110 | item[2].SetValue(str(pd)) |
---|
3111 | if item in self.fixed_param and pd == '': |
---|
3112 | # Only array func has pd == '' case. |
---|
3113 | item[2].Enable(False) |
---|
3114 | if item[2].__class__.__name__ == "ComboBox": |
---|
3115 | if self.model.fun_list.has_key(content[name][1]): |
---|
3116 | fun_val = self.model.fun_list[content[name][1]] |
---|
3117 | self.model.setParam(name,fun_val) |
---|
3118 | |
---|
3119 | value = content[name][1:] |
---|
3120 | self._paste_poly_help(item, value) |
---|
3121 | if check == 'True': |
---|
3122 | is_true = True |
---|
3123 | elif check == 'False': |
---|
3124 | is_true = False |
---|
3125 | else: |
---|
3126 | is_true = None |
---|
3127 | if is_true != None: |
---|
3128 | item[0].SetValue(is_true) |
---|
3129 | # 1D |
---|
3130 | else: |
---|
3131 | ## for 1D all parameters except orientation |
---|
3132 | if not item[1] in orient_param: |
---|
3133 | name = item[1] |
---|
3134 | if name in content.keys(): |
---|
3135 | check = content[name][0] |
---|
3136 | # Avoid changing combox content which needs special care |
---|
3137 | value = content[name][1:] |
---|
3138 | pd = value[0] |
---|
3139 | if name.count('.') > 0: |
---|
3140 | try: |
---|
3141 | pd = float(pd) |
---|
3142 | except: |
---|
3143 | #continue |
---|
3144 | if not pd and pd != '': |
---|
3145 | continue |
---|
3146 | item[2].SetValue(str(pd)) |
---|
3147 | if item in self.fixed_param and pd == '': |
---|
3148 | # Only array func has pd == '' case. |
---|
3149 | item[2].Enable(False) |
---|
3150 | if item[2].__class__.__name__ == "ComboBox": |
---|
3151 | if self.model.fun_list.has_key(value[0]): |
---|
3152 | fun_val = self.model.fun_list[value[0]] |
---|
3153 | self.model.setParam(name,fun_val) |
---|
3154 | # save state |
---|
3155 | #self._copy_parameters_state(self.str_parameters, |
---|
3156 | # self.state.str_parameters) |
---|
3157 | self._paste_poly_help(item, value) |
---|
3158 | if check == 'True': |
---|
3159 | is_true = True |
---|
3160 | elif check == 'False': |
---|
3161 | is_true = False |
---|
3162 | else: |
---|
3163 | is_true = None |
---|
3164 | if is_true != None: |
---|
3165 | item[0].SetValue(is_true) |
---|
3166 | |
---|
3167 | def _paste_poly_help(self, item, value): |
---|
3168 | """ |
---|
3169 | Helps get paste for poly function |
---|
3170 | |
---|
3171 | :param item: Gui param items |
---|
3172 | :param value: the values for parameter ctrols |
---|
3173 | """ |
---|
3174 | is_array = False |
---|
3175 | if len(value[1]) > 0: |
---|
3176 | # Only for dispersion func.s |
---|
3177 | try: |
---|
3178 | item[7].SetValue(value[1]) |
---|
3179 | selection = item[7].GetCurrentSelection() |
---|
3180 | name = item[7].Name |
---|
3181 | param_name = name.split('.')[0] |
---|
3182 | disp_name = item[7].GetValue() |
---|
3183 | dispersity= item[7].GetClientData(selection) |
---|
3184 | disp_model = dispersity() |
---|
3185 | # Only for array disp |
---|
3186 | try: |
---|
3187 | pd_vals = numpy.array(value[2]) |
---|
3188 | pd_weights = numpy.array(value[3]) |
---|
3189 | if len(pd_vals) > 0 and len(pd_vals) > 0: |
---|
3190 | if len(pd_vals) == len(pd_weights): |
---|
3191 | self._set_disp_array_cb(item=item) |
---|
3192 | self._set_array_disp_model(name=name, |
---|
3193 | disp=disp_model, |
---|
3194 | values=pd_vals, |
---|
3195 | weights=pd_weights) |
---|
3196 | is_array = True |
---|
3197 | except: |
---|
3198 | pass |
---|
3199 | if not is_array: |
---|
3200 | self._disp_obj_dict[name] = disp_model |
---|
3201 | self.model.set_dispersion(name, |
---|
3202 | disp_model) |
---|
3203 | self.state._disp_obj_dict[name] = \ |
---|
3204 | disp_model |
---|
3205 | self.model.set_dispersion(param_name, disp_model) |
---|
3206 | self.state.values = self.values |
---|
3207 | self.state.weights = self.weights |
---|
3208 | self.model._persistency_dict[param_name] = \ |
---|
3209 | [state.values, state.weights] |
---|
3210 | |
---|
3211 | except: |
---|
3212 | pass |
---|
3213 | |
---|
3214 | def _set_disp_array_cb(self, item): |
---|
3215 | """ |
---|
3216 | Set cb for array disp |
---|
3217 | """ |
---|
3218 | item[0].SetValue(False) |
---|
3219 | item[0].Enable(False) |
---|
3220 | item[2].Enable(False) |
---|
3221 | item[3].Show(False) |
---|
3222 | item[4].Show(False) |
---|
3223 | item[5].SetValue('') |
---|
3224 | item[5].Enable(False) |
---|
3225 | item[6].SetValue('') |
---|
3226 | item[6].Enable(False) |
---|
3227 | |
---|
3228 | |
---|