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