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