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