1 | """ |
---|
2 | This module provide GUI for the neutron scattering length density calculator |
---|
3 | |
---|
4 | """ |
---|
5 | import copy |
---|
6 | import time |
---|
7 | import sys |
---|
8 | import os |
---|
9 | import wx |
---|
10 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
11 | from sans.invariant import invariant |
---|
12 | from sans.guiframe.utils import format_number |
---|
13 | from sans.guiframe.utils import check_float |
---|
14 | from sans.guiframe.events import StatusEvent |
---|
15 | from sans.guiframe.events import AppendBookmarkEvent |
---|
16 | from .invariant_details import InvariantDetailsPanel |
---|
17 | from .invariant_details import InvariantContainer |
---|
18 | from .invariant_widgets import OutputTextCtrl |
---|
19 | from .invariant_widgets import InvTextCtrl |
---|
20 | from .invariant_state import InvariantState as IState |
---|
21 | from sans.guiframe.panel_base import PanelBase |
---|
22 | # The minimum q-value to be used when extrapolating |
---|
23 | Q_MINIMUM = 1e-5 |
---|
24 | # The maximum q-value to be used when extrapolating |
---|
25 | Q_MAXIMUM = 10 |
---|
26 | # the ratio of maximum q value/(qmax of data) to plot the theory data |
---|
27 | Q_MAXIMUM_PLOT = 3 |
---|
28 | # the number of points to consider during fit |
---|
29 | NPTS = 10 |
---|
30 | #Default value for background |
---|
31 | BACKGROUND = 0.0 |
---|
32 | #default value for the scale |
---|
33 | SCALE = 1.0 |
---|
34 | #default value of the contrast |
---|
35 | CONTRAST = 1.0 |
---|
36 | #default value of the power used for power law |
---|
37 | POWER = 4.0 |
---|
38 | #Invariant panel size |
---|
39 | _BOX_WIDTH = 76 |
---|
40 | |
---|
41 | |
---|
42 | if sys.platform.count("win32") > 0: |
---|
43 | _STATICBOX_WIDTH = 450 |
---|
44 | PANEL_WIDTH = 500 |
---|
45 | PANEL_HEIGHT = 700 |
---|
46 | FONT_VARIANT = 0 |
---|
47 | else: |
---|
48 | _STATICBOX_WIDTH = 480 |
---|
49 | PANEL_WIDTH = 530 |
---|
50 | PANEL_HEIGHT = 700 |
---|
51 | FONT_VARIANT = 1 |
---|
52 | |
---|
53 | |
---|
54 | class InvariantPanel(ScrolledPanel, PanelBase): |
---|
55 | """ |
---|
56 | Provides the Invariant GUI. |
---|
57 | """ |
---|
58 | ## Internal nickname for the window, used by the AUI manager |
---|
59 | window_name = "Invariant" |
---|
60 | ## Name to appear on the window title bar |
---|
61 | window_caption = "Invariant" |
---|
62 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
63 | CENTER_PANE = True |
---|
64 | def __init__(self, parent, data=None, manager=None, *args, **kwds): |
---|
65 | kwds["size"] = (PANEL_WIDTH, PANEL_HEIGHT) |
---|
66 | kwds["style"] = wx.FULL_REPAINT_ON_RESIZE |
---|
67 | ScrolledPanel.__init__(self, parent=parent, *args, **kwds) |
---|
68 | PanelBase.__init__(self, parent) |
---|
69 | self.SetupScrolling() |
---|
70 | #Font size |
---|
71 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
72 | #Object that receive status event |
---|
73 | self.parent = parent |
---|
74 | #plug-in using this panel |
---|
75 | self._manager = manager |
---|
76 | #Data uses for computation |
---|
77 | self._data = data |
---|
78 | self._scale = SCALE |
---|
79 | self._background = BACKGROUND |
---|
80 | self._bmark = None |
---|
81 | self.bookmark_num = 0 |
---|
82 | |
---|
83 | self._set_bookmark_menu() |
---|
84 | #Init state |
---|
85 | self.set_state() |
---|
86 | # default flags for state |
---|
87 | self.new_state = False |
---|
88 | self.is_state_data = False |
---|
89 | self.is_power_out = False |
---|
90 | |
---|
91 | #container of invariant value |
---|
92 | self.inv_container = None |
---|
93 | #Draw the panel |
---|
94 | self._do_layout() |
---|
95 | self.reset_panel() |
---|
96 | self._reset_state_list() |
---|
97 | |
---|
98 | if self.parent is not None: |
---|
99 | msg = "" |
---|
100 | wx.PostEvent(self.parent,StatusEvent(status=msg, info="info")) |
---|
101 | |
---|
102 | ## Default file location for save |
---|
103 | self._default_save_location = os.getcwd() |
---|
104 | self._set_bookmark_flag(False) |
---|
105 | |
---|
106 | def get_data(self): |
---|
107 | """ |
---|
108 | """ |
---|
109 | return self._manager.get_data() |
---|
110 | |
---|
111 | def get_state(self): |
---|
112 | """ |
---|
113 | """ |
---|
114 | return self.state |
---|
115 | |
---|
116 | def set_data(self, data): |
---|
117 | """ |
---|
118 | Set the data |
---|
119 | """ |
---|
120 | self._data = data |
---|
121 | #edit the panel |
---|
122 | if self._data is not None: |
---|
123 | self.get_state_by_num(0) |
---|
124 | data_name = self._data.name |
---|
125 | data_qmin = min (self._data.x) |
---|
126 | data_qmax = max (self._data.x) |
---|
127 | self.data_name_tcl.SetValue(str(data_name)) |
---|
128 | self.data_min_tcl.SetValue(str(data_qmin)) |
---|
129 | self.data_max_tcl.SetValue(str(data_qmax)) |
---|
130 | self.reset_panel() |
---|
131 | self.compute_invariant(event=None) |
---|
132 | self.state.file = self._data.name |
---|
133 | #Reset the list of states |
---|
134 | self.state.data = copy.deepcopy(data) |
---|
135 | self._set_save_flag(True) |
---|
136 | self._set_preview_flag(False) |
---|
137 | self._reset_state_list() |
---|
138 | self._set_bookmark_flag(True) |
---|
139 | return True |
---|
140 | |
---|
141 | def set_message(self): |
---|
142 | """ |
---|
143 | Display warning message if available |
---|
144 | """ |
---|
145 | if self.inv_container is not None: |
---|
146 | if self.inv_container.existing_warning: |
---|
147 | msg = "Warning! Computations on invariant require your " |
---|
148 | msg += "attention.\n Please click on Details button." |
---|
149 | self.hint_msg_txt.SetForegroundColour("red") |
---|
150 | |
---|
151 | wx.PostEvent(self.parent, |
---|
152 | StatusEvent(status=msg,info="warning")) |
---|
153 | else: |
---|
154 | msg = "For more information, click on Details button." |
---|
155 | self.hint_msg_txt.SetForegroundColour("black") |
---|
156 | wx.PostEvent(self.parent, |
---|
157 | StatusEvent(status=msg,info="info")) |
---|
158 | self.hint_msg_txt.SetLabel(msg) |
---|
159 | |
---|
160 | |
---|
161 | def set_manager(self, manager): |
---|
162 | """ |
---|
163 | set value for the manager |
---|
164 | """ |
---|
165 | self._manager = manager |
---|
166 | |
---|
167 | def set_state(self,state=None,data=None): |
---|
168 | """ |
---|
169 | set state when loading it from a .inv/.svs file |
---|
170 | """ |
---|
171 | |
---|
172 | if state == None and data == None: |
---|
173 | self.state = IState() |
---|
174 | elif state == None or data == None: return |
---|
175 | else: |
---|
176 | new_state = copy.deepcopy(state) |
---|
177 | self.new_state = True |
---|
178 | if not self.set_data(data): |
---|
179 | return |
---|
180 | |
---|
181 | self.state = new_state |
---|
182 | self.state.file = data.name |
---|
183 | |
---|
184 | num = self.state.saved_state['state_num'] |
---|
185 | if num > 0 : |
---|
186 | self._set_undo_flag(True) |
---|
187 | if num < len(state.state_list) - 1: |
---|
188 | self._set_redo_flag(True) |
---|
189 | |
---|
190 | # get bookmarks |
---|
191 | self.bookmark_num = len(self.state.bookmark_list) |
---|
192 | total_bookmark_num = self.bookmark_num + 1 |
---|
193 | |
---|
194 | for ind in range(1,total_bookmark_num): |
---|
195 | #bookmark_num = ind |
---|
196 | value = self.state.bookmark_list[ind] |
---|
197 | name = "%d] bookmarked at %s on %s"% (ind,value[0], value[1]) |
---|
198 | # append it to menu |
---|
199 | id = wx.NewId() |
---|
200 | self.popUpMenu.Append(id,name,str('')) |
---|
201 | wx.EVT_MENU(self, id, self._back_to_bookmark) |
---|
202 | wx.PostEvent(self.parent, AppendBookmarkEvent(title=name, |
---|
203 | hint='', |
---|
204 | handler=self._back_to_bookmark)) |
---|
205 | |
---|
206 | self.get_state_by_num(state_num=str(num)) |
---|
207 | |
---|
208 | self._get_input_list() |
---|
209 | #make sure that the data is reset (especially |
---|
210 | # when loaded from a inv file) |
---|
211 | self.state.data = self._data |
---|
212 | self._set_preview_flag(False) |
---|
213 | self.new_state = False |
---|
214 | self.is_state_data = False |
---|
215 | |
---|
216 | def clear_panel(self): |
---|
217 | """ |
---|
218 | Clear panel to defaults, used by set_state of manager |
---|
219 | """ |
---|
220 | |
---|
221 | self._data = None |
---|
222 | # default data testctrl |
---|
223 | self.hint_msg_txt.SetLabel('') |
---|
224 | data_name = '' |
---|
225 | data_qmin = '' |
---|
226 | data_qmax = '' |
---|
227 | self.data_name_tcl.SetValue(str(data_name)) |
---|
228 | self.data_min_tcl.SetValue(str(data_qmin)) |
---|
229 | self.data_max_tcl.SetValue(str(data_qmax)) |
---|
230 | #reset output textctrl |
---|
231 | self._reset_output() |
---|
232 | #reset panel |
---|
233 | self.reset_panel() |
---|
234 | #reset state w/o data |
---|
235 | self.set_state() |
---|
236 | # default flags for state |
---|
237 | self.new_state = False |
---|
238 | self.is_state_data = False |
---|
239 | self.is_power_out = False |
---|
240 | |
---|
241 | def get_background(self): |
---|
242 | """ |
---|
243 | return the background textcrtl value as a float |
---|
244 | """ |
---|
245 | background = self.background_tcl.GetValue().lstrip().rstrip() |
---|
246 | if background == "": |
---|
247 | raise ValueError, "Need a background" |
---|
248 | if check_float(self.background_tcl): |
---|
249 | return float(background) |
---|
250 | else: |
---|
251 | msg = "Receive invalid value for background : %s" % (background) |
---|
252 | raise ValueError, msg |
---|
253 | |
---|
254 | def get_scale(self): |
---|
255 | """ |
---|
256 | return the scale textcrtl value as a float |
---|
257 | """ |
---|
258 | scale = self.scale_tcl.GetValue().lstrip().rstrip() |
---|
259 | if scale == "": |
---|
260 | raise ValueError, "Need a background" |
---|
261 | if check_float(self.scale_tcl): |
---|
262 | if float(scale) <= 0.0: |
---|
263 | self.scale_tcl.SetBackgroundColour("pink") |
---|
264 | self.scale_tcl.Refresh() |
---|
265 | msg = "Receive invalid value for scale: %s" % (scale) |
---|
266 | raise ValueError, msg |
---|
267 | return float(scale) |
---|
268 | else: |
---|
269 | raise ValueError, "Receive invalid value for scale : %s" % (scale) |
---|
270 | |
---|
271 | def get_contrast(self): |
---|
272 | """ |
---|
273 | return the contrast textcrtl value as a float |
---|
274 | """ |
---|
275 | par_str = self.contrast_tcl.GetValue().strip() |
---|
276 | contrast = None |
---|
277 | if par_str !=" " and check_float(self.contrast_tcl): |
---|
278 | contrast = float(par_str) |
---|
279 | return contrast |
---|
280 | |
---|
281 | def get_extrapolation_type(self, low_q, high_q): |
---|
282 | """ |
---|
283 | """ |
---|
284 | extrapolation = None |
---|
285 | if low_q and not high_q: |
---|
286 | extrapolation = "low" |
---|
287 | elif not low_q and high_q: |
---|
288 | extrapolation = "high" |
---|
289 | elif low_q and high_q: |
---|
290 | extrapolation = "both" |
---|
291 | return extrapolation |
---|
292 | |
---|
293 | def get_porod_const(self): |
---|
294 | """ |
---|
295 | return the porod constant textcrtl value as a float |
---|
296 | """ |
---|
297 | par_str = self.porod_constant_tcl.GetValue().strip() |
---|
298 | porod_const = None |
---|
299 | if par_str != "" and check_float(self.porod_constant_tcl): |
---|
300 | porod_const = float(par_str) |
---|
301 | return porod_const |
---|
302 | |
---|
303 | def get_volume(self, inv, contrast, extrapolation): |
---|
304 | """ |
---|
305 | """ |
---|
306 | if contrast is not None: |
---|
307 | try: |
---|
308 | v, dv = inv.get_volume_fraction_with_error(contrast=contrast, |
---|
309 | extrapolation=extrapolation) |
---|
310 | self.volume_tcl.SetValue(format_number(v)) |
---|
311 | self.volume_err_tcl.SetValue(format_number(dv)) |
---|
312 | except: |
---|
313 | self.volume_tcl.SetValue(format_number(None)) |
---|
314 | self.volume_err_tcl.SetValue(format_number(None)) |
---|
315 | msg = "Error occurred computing volume " |
---|
316 | msg += " fraction: %s" % sys.exc_value |
---|
317 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
318 | info="error", |
---|
319 | type="stop")) |
---|
320 | |
---|
321 | def get_surface(self, inv, contrast, porod_const, extrapolation): |
---|
322 | """ |
---|
323 | """ |
---|
324 | if contrast is not None and porod_const is not None: |
---|
325 | try: |
---|
326 | s, ds = inv.get_surface_with_error(contrast=contrast, |
---|
327 | porod_const=porod_const, |
---|
328 | extrapolation=extrapolation) |
---|
329 | self.surface_tcl.SetValue(format_number(s)) |
---|
330 | self.surface_err_tcl.SetValue(format_number(ds)) |
---|
331 | except: |
---|
332 | self.surface_tcl.SetValue(format_number(None)) |
---|
333 | self.surface_err_tcl.SetValue(format_number(None)) |
---|
334 | msg = "Error occurred computing " |
---|
335 | msg += "specific surface: %s" % sys.exc_value |
---|
336 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", |
---|
337 | type="stop")) |
---|
338 | |
---|
339 | def get_total_qstar(self, inv, extrapolation): |
---|
340 | """ |
---|
341 | """ |
---|
342 | try: |
---|
343 | qstar_total, qstar_total_err = inv.get_qstar_with_error(extrapolation) |
---|
344 | |
---|
345 | self.invariant_total_tcl.SetValue(format_number(qstar_total)) |
---|
346 | self.invariant_total_err_tcl.SetValue(format_number(qstar_total_err)) |
---|
347 | self.inv_container.qstar_total = qstar_total |
---|
348 | self.inv_container.qstar_total_err = qstar_total_err |
---|
349 | |
---|
350 | except: |
---|
351 | self.inv_container.qstar_total = "Error" |
---|
352 | self.inv_container.qstar_total_err = "Error" |
---|
353 | self.invariant_total_tcl.SetValue(format_number(None)) |
---|
354 | self.invariant_total_err_tcl.SetValue(format_number(None)) |
---|
355 | msg = "Error occurred computing invariant using" |
---|
356 | msg += " extrapolation: %s" % sys.exc_value |
---|
357 | wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop")) |
---|
358 | |
---|
359 | def get_low_qstar(self, inv, npts_low, low_q=False): |
---|
360 | """ |
---|
361 | """ |
---|
362 | if low_q: |
---|
363 | try: |
---|
364 | qstar_low, qstar_low_err = inv.get_qstar_low() |
---|
365 | self.inv_container.qstar_low = qstar_low |
---|
366 | self.inv_container.qstar_low_err = qstar_low_err |
---|
367 | extrapolated_data = inv.get_extra_data_low(npts_in=npts_low) |
---|
368 | power_low = inv.get_extrapolation_power(range='low') |
---|
369 | if self.power_law_low.GetValue(): |
---|
370 | self.power_low_tcl.SetValue(format_number(power_low)) |
---|
371 | self._manager.plot_theory(data=extrapolated_data, |
---|
372 | name="Low-Q extrapolation") |
---|
373 | except: |
---|
374 | raise |
---|
375 | self.inv_container.qstar_low = "ERROR" |
---|
376 | self.inv_container.qstar_low_err = "ERROR" |
---|
377 | self._manager.plot_theory(name="Low-Q extrapolation") |
---|
378 | msg = "Error occurred computing low-Q " |
---|
379 | msg += "invariant: %s" % sys.exc_value |
---|
380 | wx.PostEvent(self.parent, |
---|
381 | StatusEvent(status=msg, type="stop")) |
---|
382 | else: |
---|
383 | try: |
---|
384 | self._manager.plot_theory(name="Low-Q extrapolation") |
---|
385 | except: pass |
---|
386 | |
---|
387 | def get_high_qstar(self, inv, high_q=False): |
---|
388 | """ |
---|
389 | """ |
---|
390 | if high_q: |
---|
391 | try: |
---|
392 | qmax_plot = Q_MAXIMUM_PLOT * max(self._data.x) |
---|
393 | if qmax_plot > Q_MAXIMUM: qmax_plot = Q_MAXIMUM |
---|
394 | qstar_high, qstar_high_err = inv.get_qstar_high() |
---|
395 | self.inv_container.qstar_high = qstar_high |
---|
396 | self.inv_container.qstar_high_err = qstar_high_err |
---|
397 | power_high = inv.get_extrapolation_power(range='high') |
---|
398 | self.power_high_tcl.SetValue(format_number(power_high)) |
---|
399 | high_out_data = inv.get_extra_data_high(q_end=qmax_plot, |
---|
400 | npts=500) |
---|
401 | self._manager.plot_theory(data=high_out_data, |
---|
402 | name="High-Q extrapolation") |
---|
403 | except: |
---|
404 | #raise |
---|
405 | self.inv_container.qstar_high = "ERROR" |
---|
406 | self.inv_container.qstar_high_err = "ERROR" |
---|
407 | self._manager.plot_theory(name="High-Q extrapolation") |
---|
408 | msg = "Error occurred computing high-Q " |
---|
409 | msg += "invariant: %s" % sys.exc_value |
---|
410 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
411 | type="stop")) |
---|
412 | else: |
---|
413 | try: |
---|
414 | self._manager.plot_theory(name="High-Q extrapolation") |
---|
415 | except: pass |
---|
416 | |
---|
417 | def get_qstar(self, inv): |
---|
418 | """ |
---|
419 | """ |
---|
420 | qstar, qstar_err = inv.get_qstar_with_error() |
---|
421 | self.inv_container.qstar = qstar |
---|
422 | self.inv_container.qstar_err = qstar_err |
---|
423 | |
---|
424 | def set_extrapolation_low(self, inv, low_q=False): |
---|
425 | """ |
---|
426 | return float value necessary to compute invariant a low q |
---|
427 | """ |
---|
428 | #get funtion |
---|
429 | if self.guinier.GetValue(): |
---|
430 | function_low = "guinier" |
---|
431 | # get the function |
---|
432 | power_low = None #2.0/3.0 |
---|
433 | if self.power_law_low.GetValue(): |
---|
434 | function_low = "power_law" |
---|
435 | if self.fit_enable_low.GetValue(): |
---|
436 | #set value of power_low to none to allow fitting |
---|
437 | power_low = None |
---|
438 | else: |
---|
439 | power_low = self.power_low_tcl.GetValue().lstrip().rstrip() |
---|
440 | if check_float(self.power_low_tcl): |
---|
441 | power_low = float(power_low) |
---|
442 | else: |
---|
443 | if low_q : |
---|
444 | #Raise error only when qstar at low q is requested |
---|
445 | msg = "Expect float for power at low q, " |
---|
446 | msg += " got %s" % (power_low) |
---|
447 | raise ValueError, msg |
---|
448 | |
---|
449 | #Get the number of points to extrapolated |
---|
450 | npts_low = self.npts_low_tcl.GetValue().lstrip().rstrip() |
---|
451 | if check_float(self.npts_low_tcl): |
---|
452 | npts_low = float(npts_low) |
---|
453 | else: |
---|
454 | if low_q: |
---|
455 | msg = "Expect float for number of points at low q," |
---|
456 | msg += " got %s" % (npts_low) |
---|
457 | raise ValueError, msg |
---|
458 | #Set the invariant calculator |
---|
459 | inv.set_extrapolation(range="low", npts=npts_low, |
---|
460 | function=function_low, power=power_low) |
---|
461 | return inv, npts_low |
---|
462 | |
---|
463 | |
---|
464 | def set_extrapolation_high(self, inv, high_q=False): |
---|
465 | """ |
---|
466 | return float value necessary to compute invariant a high q |
---|
467 | """ |
---|
468 | power_high = None |
---|
469 | #if self.power_law_high.GetValue(): |
---|
470 | function_high = "power_law" |
---|
471 | if self.fit_enable_high.GetValue(): |
---|
472 | #set value of power_high to none to allow fitting |
---|
473 | power_high = None |
---|
474 | else: |
---|
475 | power_high = self.power_high_tcl.GetValue().lstrip().rstrip() |
---|
476 | if check_float(self.power_high_tcl): |
---|
477 | power_high = float(power_high) |
---|
478 | else: |
---|
479 | if high_q : |
---|
480 | #Raise error only when qstar at high q is requested |
---|
481 | msg = "Expect float for power at high q," |
---|
482 | msg += " got %s" % (power_high) |
---|
483 | raise ValueError, msg |
---|
484 | |
---|
485 | npts_high = self.npts_high_tcl.GetValue().lstrip().rstrip() |
---|
486 | if check_float(self.npts_high_tcl): |
---|
487 | npts_high = float(npts_high) |
---|
488 | else: |
---|
489 | if high_q: |
---|
490 | msg = "Expect float for number of points at high q," |
---|
491 | msg += " got %s" % (npts_high) |
---|
492 | raise ValueError, msg |
---|
493 | inv.set_extrapolation(range="high", npts=npts_high, |
---|
494 | function=function_high, power=power_high) |
---|
495 | return inv, npts_high |
---|
496 | |
---|
497 | def display_details(self, event): |
---|
498 | """ |
---|
499 | open another panel for more details on invariant calculation |
---|
500 | """ |
---|
501 | panel = InvariantDetailsPanel(parent=self, |
---|
502 | qstar_container=self.inv_container) |
---|
503 | panel.ShowModal() |
---|
504 | panel.Destroy() |
---|
505 | self.button_calculate.SetFocus() |
---|
506 | |
---|
507 | def compute_invariant(self, event=None): |
---|
508 | """ |
---|
509 | compute invariant |
---|
510 | """ |
---|
511 | if self._data == None: |
---|
512 | msg = "\n\nData must be loaded first in order" |
---|
513 | msg += " to perform a compution..." |
---|
514 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
515 | # set a state for this computation for saving |
---|
516 | elif event != None: |
---|
517 | self._set_compute_state(state='compute') |
---|
518 | self._set_bookmark_flag(True) |
---|
519 | msg= "\n\nStarting a new invariant computation..." |
---|
520 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
521 | |
---|
522 | |
---|
523 | if self._data is None: |
---|
524 | return |
---|
525 | self.button_details.Enable() |
---|
526 | #clear outputs textctrl |
---|
527 | self._reset_output() |
---|
528 | try: |
---|
529 | background = self.get_background() |
---|
530 | scale = self.get_scale() |
---|
531 | except: |
---|
532 | msg = "Invariant Error: %s" % (sys.exc_value) |
---|
533 | wx.PostEvent(self.parent, StatusEvent(status=msg, type="stop")) |
---|
534 | return |
---|
535 | |
---|
536 | low_q = self.enable_low_cbox.GetValue() |
---|
537 | high_q = self.enable_high_cbox.GetValue() |
---|
538 | temp_data = copy.deepcopy(self._data) |
---|
539 | |
---|
540 | #set invariant calculator |
---|
541 | inv = invariant.InvariantCalculator(data=temp_data, |
---|
542 | background=background, |
---|
543 | scale=scale) |
---|
544 | try: |
---|
545 | inv, npts_low = self.set_extrapolation_low(inv=inv, low_q=low_q) |
---|
546 | inv, npts_high = self.set_extrapolation_high(inv=inv, high_q=high_q) |
---|
547 | except: |
---|
548 | msg = "Error occurred computing invariant: %s" % sys.exc_value |
---|
549 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
550 | info="warning",type="stop")) |
---|
551 | return |
---|
552 | #check the type of extrapolation |
---|
553 | extrapolation = self.get_extrapolation_type(low_q=low_q, high_q=high_q) |
---|
554 | |
---|
555 | #Compute invariant |
---|
556 | bkg_changed = False |
---|
557 | scale_changed = False |
---|
558 | try: |
---|
559 | self.get_qstar(inv=inv) |
---|
560 | #if scale_changed or bkg_changed: |
---|
561 | #self._manager.plot_data(data=inv.get_data()) |
---|
562 | |
---|
563 | except: |
---|
564 | msg= "Error occurred computing invariant: %s" % sys.exc_value |
---|
565 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
566 | info="warning", |
---|
567 | type="stop")) |
---|
568 | return |
---|
569 | |
---|
570 | #Compute qstar extrapolated to low q range |
---|
571 | self.get_low_qstar(inv=inv, npts_low=npts_low, low_q=low_q) |
---|
572 | #Compute qstar extrapolated to high q range |
---|
573 | self.get_high_qstar(inv=inv, high_q=high_q) |
---|
574 | #Compute qstar extrapolated to total q range and set value to txtcrtl |
---|
575 | self.get_total_qstar(inv=inv, extrapolation=extrapolation) |
---|
576 | # Parse additional parameters |
---|
577 | porod_const = self.get_porod_const() |
---|
578 | contrast = self.get_contrast() |
---|
579 | |
---|
580 | try: |
---|
581 | #Compute volume and set value to txtcrtl |
---|
582 | self.get_volume(inv=inv, contrast=contrast, |
---|
583 | extrapolation=extrapolation) |
---|
584 | #compute surface and set value to txtcrtl |
---|
585 | except: |
---|
586 | msg = "Error occurred computing invariant: %s" % sys.exc_value |
---|
587 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
588 | info="warning", |
---|
589 | type="stop")) |
---|
590 | try: |
---|
591 | self.get_surface(inv=inv, contrast=contrast, |
---|
592 | porod_const=porod_const, |
---|
593 | extrapolation=extrapolation) |
---|
594 | |
---|
595 | except: |
---|
596 | msg = "Error occurred computing invariant: %s" % sys.exc_value |
---|
597 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
598 | info="warning", |
---|
599 | type="stop")) |
---|
600 | |
---|
601 | #compute percentage of each invariant |
---|
602 | self.inv_container.compute_percentage() |
---|
603 | |
---|
604 | #display a message |
---|
605 | self.set_message() |
---|
606 | |
---|
607 | # reset power_out to default to get ready for another '_on_text' |
---|
608 | if self.is_power_out == True: |
---|
609 | self.state.container = copy.deepcopy(self.inv_container) |
---|
610 | self.state.timestamp= self._get_time_stamp() |
---|
611 | msg = self.state.__str__() |
---|
612 | self.state.set_report_string() |
---|
613 | self.is_power_out = False |
---|
614 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
615 | |
---|
616 | #enable the button_ok for more details |
---|
617 | self._set_preview_flag(True) |
---|
618 | |
---|
619 | if event != None: |
---|
620 | self._set_preview_flag(True) |
---|
621 | self._set_save_flag(True) |
---|
622 | wx.PostEvent(self.parent, |
---|
623 | StatusEvent(status='\nFinished invariant computation...')) |
---|
624 | |
---|
625 | def on_undo(self,event=None): |
---|
626 | """ |
---|
627 | Go back to the previous state |
---|
628 | |
---|
629 | : param event: undo button event |
---|
630 | """ |
---|
631 | if self.state.state_num < 0: |
---|
632 | return |
---|
633 | self.is_power_out = True |
---|
634 | # get the previous state_num |
---|
635 | pre_state_num = int(self.state.saved_state['state_num']) - 1 |
---|
636 | |
---|
637 | self.get_state_by_num(state_num=str(pre_state_num)) |
---|
638 | |
---|
639 | if float(pre_state_num) <= 0: |
---|
640 | self._set_undo_flag(False) |
---|
641 | else: |
---|
642 | self._set_undo_flag(True) |
---|
643 | self._set_redo_flag(True) |
---|
644 | self.is_power_out = False |
---|
645 | self._info_state_num() |
---|
646 | |
---|
647 | |
---|
648 | def on_redo(self,event=None): |
---|
649 | """ |
---|
650 | Go forward to the previous state |
---|
651 | |
---|
652 | : param event: redo button event |
---|
653 | """ |
---|
654 | self.is_power_out = True |
---|
655 | # get the next state_num |
---|
656 | next_state_num = int(self.state.saved_state['state_num']) + 1 |
---|
657 | |
---|
658 | self.get_state_by_num(state_num=str(next_state_num)) |
---|
659 | |
---|
660 | if float(next_state_num)+2 > len(self.state.state_list): |
---|
661 | self._set_redo_flag(False) |
---|
662 | else: |
---|
663 | self._set_redo_flag(True) |
---|
664 | |
---|
665 | self._set_undo_flag(True) |
---|
666 | self.is_power_out = False |
---|
667 | self._info_state_num() |
---|
668 | |
---|
669 | def on_preview(self, event=None): |
---|
670 | """ |
---|
671 | Invoke report dialog panel |
---|
672 | |
---|
673 | : param event: report button event |
---|
674 | """ |
---|
675 | from .report_dialog import ReportDialog |
---|
676 | |
---|
677 | self.state.set_report_string() |
---|
678 | report_html_str = self.state.report_str |
---|
679 | report_text_str = self.state.__str__() |
---|
680 | report_img = self.state.image |
---|
681 | report_list = [report_html_str,report_text_str,report_img] |
---|
682 | dialog = ReportDialog(report_list, None, -1, "") |
---|
683 | dialog.ShowModal() |
---|
684 | |
---|
685 | def get_state_by_num(self,state_num=None): |
---|
686 | """ |
---|
687 | Get the state given by number |
---|
688 | |
---|
689 | : param state_num: the given state number |
---|
690 | """ |
---|
691 | if state_num == None: |
---|
692 | return |
---|
693 | |
---|
694 | backup_state_list = copy.deepcopy(self.state.state_list) |
---|
695 | |
---|
696 | # get the previous state |
---|
697 | try: |
---|
698 | current_state = copy.deepcopy(self.state.state_list[str(state_num)]) |
---|
699 | # get the previously computed state number |
---|
700 | #(computation before the state changes happened) |
---|
701 | current_compute_num = str(current_state['compute_num']) |
---|
702 | except : |
---|
703 | raise |
---|
704 | #raise ValueError, "No such state exists in history" |
---|
705 | |
---|
706 | # get the state at pre_compute_num |
---|
707 | comp_state = copy.deepcopy(self.state.state_list[current_compute_num]) |
---|
708 | |
---|
709 | # set the parameters |
---|
710 | for key in comp_state: |
---|
711 | value = comp_state[key] |
---|
712 | try: |
---|
713 | exec "self.%s.SetValue(str(%s))" % (key, value) |
---|
714 | except TypeError: |
---|
715 | exec "self.%s.SetValue(%s)" % (key, value) |
---|
716 | except: |
---|
717 | pass |
---|
718 | |
---|
719 | self.compute_invariant(event=None) |
---|
720 | |
---|
721 | # set the input params at the state at pre_state_num |
---|
722 | for key in current_state: |
---|
723 | # set the inputs and boxes |
---|
724 | value = current_state[key] |
---|
725 | try: |
---|
726 | exec 'self.%s.SetValue(str(%s))' % (key, value) |
---|
727 | except TypeError: |
---|
728 | exec 'self.%s.SetValue(%s)' % (key, value) |
---|
729 | except: |
---|
730 | pass |
---|
731 | |
---|
732 | self._enable_high_q_section(event=None) |
---|
733 | self._enable_low_q_section(event=None) |
---|
734 | self.state.state_list = backup_state_list |
---|
735 | self.state.saved_state = current_state |
---|
736 | self.state.state_num = state_num |
---|
737 | |
---|
738 | |
---|
739 | def get_bookmark_by_num(self, num=None): |
---|
740 | """ |
---|
741 | Get the bookmark state given by number |
---|
742 | |
---|
743 | : param num: the given bookmark number |
---|
744 | |
---|
745 | """ |
---|
746 | current_state = {} |
---|
747 | comp_state = {} |
---|
748 | backup_state_list = copy.deepcopy(self.state.state_list) |
---|
749 | |
---|
750 | # get the previous state |
---|
751 | try: |
---|
752 | time,date,current_state,comp_state = self.state.bookmark_list[int(num)] |
---|
753 | except : |
---|
754 | raise ValueError, "No such bookmark exists" |
---|
755 | |
---|
756 | # set the parameters |
---|
757 | for key in comp_state: |
---|
758 | value = comp_state[key] |
---|
759 | try: |
---|
760 | exec "self.%s.SetValue(str(%s))" % (key, value) |
---|
761 | except TypeError: |
---|
762 | exec "self.%s.SetValue(%s)" % (key, value) |
---|
763 | except: |
---|
764 | pass |
---|
765 | |
---|
766 | self.compute_invariant(event=None) |
---|
767 | # set the input params at the state of pre_state_num |
---|
768 | for key in current_state: |
---|
769 | value = current_state[key] |
---|
770 | try: |
---|
771 | exec 'self.%s.SetValue(str(%s))' % (key, value) |
---|
772 | except TypeError: |
---|
773 | exec 'self.%s.SetValue(%s)' % (key, value) |
---|
774 | except: |
---|
775 | pass |
---|
776 | self.state.saved_state = copy.deepcopy(current_state) |
---|
777 | |
---|
778 | self._enable_high_q_section(event=None) |
---|
779 | self._enable_low_q_section(event=None) |
---|
780 | self.state.state_list = backup_state_list |
---|
781 | #self.state.saved_state = current_state |
---|
782 | #self.state.state_num = state_num |
---|
783 | |
---|
784 | def reset_panel(self): |
---|
785 | """ |
---|
786 | set the panel at its initial state. |
---|
787 | """ |
---|
788 | self.background_tcl.SetValue(str(BACKGROUND)) |
---|
789 | self.scale_tcl.SetValue(str(SCALE)) |
---|
790 | self.contrast_tcl.SetValue(str(CONTRAST)) |
---|
791 | self.porod_constant_tcl.SetValue('') |
---|
792 | self.npts_low_tcl.SetValue(str(NPTS)) |
---|
793 | self.enable_low_cbox.SetValue(False) |
---|
794 | self.fix_enable_low.SetValue(True) |
---|
795 | self.power_low_tcl.SetValue(str(POWER)) |
---|
796 | self.guinier.SetValue(True) |
---|
797 | self.power_low_tcl.Disable() |
---|
798 | self.enable_high_cbox.SetValue(False) |
---|
799 | self.fix_enable_high.SetValue(True) |
---|
800 | self.power_high_tcl.SetValue(str(POWER)) |
---|
801 | self.npts_high_tcl.SetValue(str(NPTS)) |
---|
802 | self.button_details.Disable() |
---|
803 | #Change the state of txtcrtl to enable/disable |
---|
804 | self._enable_low_q_section() |
---|
805 | #Change the state of txtcrtl to enable/disable |
---|
806 | self._enable_high_q_section() |
---|
807 | self._reset_output() |
---|
808 | self._set_undo_flag(False) |
---|
809 | self._set_redo_flag(False) |
---|
810 | self._set_bookmark_flag(False) |
---|
811 | self._set_preview_flag(False) |
---|
812 | self._set_save_flag(False) |
---|
813 | self.button_calculate.SetFocus() |
---|
814 | #self.SetupScrolling() |
---|
815 | |
---|
816 | def _set_state(self, event): |
---|
817 | """ |
---|
818 | Set the state list |
---|
819 | |
---|
820 | :param event: rb/cb event |
---|
821 | """ |
---|
822 | if event == None: |
---|
823 | return |
---|
824 | obj = event.GetEventObject() |
---|
825 | name = str(obj.GetName()) |
---|
826 | value = str(obj.GetValue()) |
---|
827 | rb_list = [['power_law_low','guinier'], |
---|
828 | ['fit_enable_low','fix_enable_low'], |
---|
829 | ['fit_enable_high','fix_enable_high']] |
---|
830 | |
---|
831 | try: |
---|
832 | if value == None or value.lstrip().rstrip() =='': |
---|
833 | value = 'None' |
---|
834 | exec 'self.state.%s = %s' % (name, value) |
---|
835 | exec "self.state.saved_state['%s'] = %s" % (name, value) |
---|
836 | |
---|
837 | # set the count part of radio button clicked |
---|
838 | #False for the saved_state |
---|
839 | for title,content in rb_list: |
---|
840 | if name == title: |
---|
841 | name = content |
---|
842 | value = False |
---|
843 | elif name == content: |
---|
844 | name = title |
---|
845 | value = False |
---|
846 | exec "self.state.saved_state['%s'] = %s" % (name, value) |
---|
847 | |
---|
848 | # Instead of changing the future, create a new future. |
---|
849 | max_state_num = len(self.state.state_list) - 1 |
---|
850 | self.state.saved_state['state_num'] = max_state_num |
---|
851 | |
---|
852 | self.state.saved_state['state_num'] += 1 |
---|
853 | self.state.state_num = self.state.saved_state['state_num'] |
---|
854 | self.state.state_list[str(self.state.state_num)] = self.state.clone_state()#copy.deepcopy(self.state.saved_state) |
---|
855 | except: |
---|
856 | pass |
---|
857 | |
---|
858 | self._set_undo_flag(True) |
---|
859 | self._set_redo_flag(False) |
---|
860 | #event.Skip() |
---|
861 | |
---|
862 | def _set_compute_state(self,state=None): |
---|
863 | """ |
---|
864 | Notify the compute_invariant state to self.state |
---|
865 | |
---|
866 | : param state: set 'compute' when the computation is |
---|
867 | activated by the 'compute' button, else None |
---|
868 | |
---|
869 | """ |
---|
870 | # reset the default |
---|
871 | if state != 'compute': |
---|
872 | self.new_state = False |
---|
873 | self.is_power_out = False |
---|
874 | else: |
---|
875 | self.is_power_out = True |
---|
876 | # Instead of changing the future, create a new future. |
---|
877 | max_state_num = len(self.state.state_list)-1 |
---|
878 | self.state.saved_state['state_num'] = max_state_num |
---|
879 | # A new computation is also A state |
---|
880 | #copy.deepcopy(self.state.saved_state) |
---|
881 | temp_saved_states = self.state.clone_state() |
---|
882 | temp_saved_states['state_num'] += 1 |
---|
883 | self.state.state_num = temp_saved_states['state_num'] |
---|
884 | |
---|
885 | |
---|
886 | # set the state number of the computation |
---|
887 | if state == 'compute': |
---|
888 | temp_saved_states['compute_num'] = self.state.state_num |
---|
889 | self.state.saved_state= copy.deepcopy(temp_saved_states) |
---|
890 | #copy.deepcopy(self.state.saved_state) |
---|
891 | self.state.state_list[str(self.state.state_num)] = self.state.clone_state() |
---|
892 | |
---|
893 | # A computation is a new state, so delete the states with any higher |
---|
894 | # state numbers |
---|
895 | for i in range(self.state.state_num+1,len(self.state.state_list)): |
---|
896 | try: |
---|
897 | del (self.state.state_list[str(i)]) |
---|
898 | except: |
---|
899 | pass |
---|
900 | # Enable the undo button if it was not |
---|
901 | self._set_undo_flag(True) |
---|
902 | self._set_redo_flag(False) |
---|
903 | |
---|
904 | def _reset_state_list(self, data=None): |
---|
905 | """ |
---|
906 | Reset the state_list just before data was loading: |
---|
907 | Used in 'set_current_data()' |
---|
908 | """ |
---|
909 | #if data == None: return |
---|
910 | #temp_state = self.state.clone_state() |
---|
911 | #copy.deepcopy(self.state.saved_state) |
---|
912 | # Clear the list |
---|
913 | self.state.state_list.clear() |
---|
914 | self.state.bookmark_list.clear() |
---|
915 | # Set defaults |
---|
916 | self.state.saved_state['state_num'] = 0 |
---|
917 | self.state.saved_state['compute_num'] = 0 |
---|
918 | if self._data != None: |
---|
919 | self.state.saved_state['file'] = str(self._data.name) |
---|
920 | else: |
---|
921 | self.state.saved_state['file'] = 'None' |
---|
922 | self.state.file = self.state.saved_state['file'] |
---|
923 | |
---|
924 | self.state.state_num = self.state.saved_state['state_num'] |
---|
925 | self.state.timestamp = "('00:00:00', '00/00/0000')" |
---|
926 | |
---|
927 | # Put only the current state in the list |
---|
928 | #copy.deepcopy(self.state.saved_state) |
---|
929 | self.state.state_list[str(self.state.state_num)] = self.state.clone_state() |
---|
930 | self._set_undo_flag(False) |
---|
931 | self._set_redo_flag(False) |
---|
932 | self._set_bookmark_flag(False) |
---|
933 | self._set_preview_flag(False) |
---|
934 | self._set_save_flag(False) |
---|
935 | |
---|
936 | |
---|
937 | def _on_text(self, event): |
---|
938 | """ |
---|
939 | Catch text change event to add the state to the state_list |
---|
940 | |
---|
941 | :param event: txtctr event ; assumes not None |
---|
942 | |
---|
943 | """ |
---|
944 | if self._data == None: |
---|
945 | return |
---|
946 | # check if this event is from do/undo button |
---|
947 | if self.state.saved_state['is_time_machine'] or self.new_state: |
---|
948 | #event.Skip() |
---|
949 | return |
---|
950 | |
---|
951 | # get the object |
---|
952 | obj = event.GetEventObject() |
---|
953 | name = str(obj.GetName()) |
---|
954 | value = str(obj.GetValue()) |
---|
955 | state_num = self.state.saved_state['state_num'] |
---|
956 | |
---|
957 | # text event is a new state, so delete the states with higher state_num |
---|
958 | # i.e., change the future |
---|
959 | for i in range(int(state_num)+1,len(self.state.state_list)): |
---|
960 | try: |
---|
961 | del (self.state.state_list[str(i)]) |
---|
962 | except: |
---|
963 | pass |
---|
964 | |
---|
965 | # Instead of changing the future, create a new future. |
---|
966 | #max_state_num = len(self.state.state_list)-1 |
---|
967 | #self.state.saved_state['state_num'] = max_state_num |
---|
968 | |
---|
969 | # try to add new state of the text changes in the state_list |
---|
970 | try: |
---|
971 | if value.strip() == None: value = '' |
---|
972 | exec "self.state.%s = '%s'" % (name, value) |
---|
973 | exec "self.state.saved_state['%s'] = '%s'" % (name, value) |
---|
974 | exec "self.state.input_list['%s'] = '%s'" % (name, value) |
---|
975 | if not self.is_power_out: |
---|
976 | if name != 'power_low_tcl' and name !='power_high_tcl': |
---|
977 | self.state.saved_state['state_num'] += 1 |
---|
978 | self.state.state_num = self.state.saved_state['state_num'] |
---|
979 | #copy.deepcopy(self.state.saved_state) |
---|
980 | self.state.state_list[str(self.state.state_num)] = self.state.clone_state() |
---|
981 | except: |
---|
982 | pass |
---|
983 | |
---|
984 | #event.Skip() |
---|
985 | self._set_undo_flag(True) |
---|
986 | self._set_redo_flag(False) |
---|
987 | self._set_bookmark_flag(True) |
---|
988 | self._set_preview_flag(False) |
---|
989 | |
---|
990 | def _on_out_text(self, event): |
---|
991 | """ |
---|
992 | Catch ouput text change to add the state |
---|
993 | |
---|
994 | :param event: txtctr event ; assumes not None |
---|
995 | |
---|
996 | """ |
---|
997 | # get the object |
---|
998 | obj = event.GetEventObject() |
---|
999 | name = str(obj.GetName()) |
---|
1000 | value = str(obj.GetValue()) |
---|
1001 | try: |
---|
1002 | exec "self.state.saved_state['%s'] = '%s'" % (name, value) |
---|
1003 | self.state.state_list[str(self.state.state_num)] = self.state.clone_state() |
---|
1004 | except: |
---|
1005 | pass |
---|
1006 | #if event != None: event.Skip() |
---|
1007 | |
---|
1008 | def _get_input_list(self): |
---|
1009 | """ |
---|
1010 | get input_list; called by set_state |
---|
1011 | """ |
---|
1012 | # get state num of the last compute state |
---|
1013 | compute_num = self.state.saved_state['compute_num'] |
---|
1014 | # find values and put into the input list |
---|
1015 | for key1,value1 in self.state.state_list[str(compute_num)].iteritems(): |
---|
1016 | for key,value in self.state.input_list.iteritems(): |
---|
1017 | if key == key1: |
---|
1018 | self.state.input_list[key]=value1 |
---|
1019 | break |
---|
1020 | |
---|
1021 | def _set_bookmark_menu(self): |
---|
1022 | """ |
---|
1023 | Setup 'bookmark' context menu |
---|
1024 | """ |
---|
1025 | ## Create context menu for page |
---|
1026 | self.popUpMenu = wx.Menu() |
---|
1027 | id = wx.NewId() |
---|
1028 | self._bmark = wx.MenuItem(self.popUpMenu,id,"BookMark", |
---|
1029 | " Bookmark the panel to recall it later") |
---|
1030 | self.popUpMenu.AppendItem(self._bmark) |
---|
1031 | self._bmark.Enable(True) |
---|
1032 | wx.EVT_MENU(self, id, self.on_bookmark) |
---|
1033 | self.popUpMenu.AppendSeparator() |
---|
1034 | self.Bind(wx.EVT_CONTEXT_MENU, self._on_context_menu) |
---|
1035 | |
---|
1036 | def on_bookmark(self,event): |
---|
1037 | """ |
---|
1038 | Save the panel state in memory and add the list on |
---|
1039 | the popup menu on bookmark context menu event |
---|
1040 | """ |
---|
1041 | if self._data == None: return |
---|
1042 | if event == None: return |
---|
1043 | self.bookmark_num += 1 |
---|
1044 | # date and time of the event |
---|
1045 | #year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
1046 | #my_time= str(hour)+" : "+str(minute)+" : "+str(second)+" " |
---|
1047 | #date= str( month)+"/"+str(day)+"/"+str(year) |
---|
1048 | my_time, date = self._get_time_stamp() |
---|
1049 | state_num = self.state.state_num |
---|
1050 | compute_num = self.state.saved_state['compute_num'] |
---|
1051 | # name and message of the bookmark list |
---|
1052 | msg= "State saved at %s on %s"%(my_time, date) |
---|
1053 | ## post help message for the selected model |
---|
1054 | msg +=" Right click on the panel to retrieve this state" |
---|
1055 | #wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1056 | name = "%d] bookmarked at %s on %s"%(self.bookmark_num, my_time, date) |
---|
1057 | |
---|
1058 | # append it to menu |
---|
1059 | id = wx.NewId() |
---|
1060 | self.popUpMenu.Append(id,name,str(msg)) |
---|
1061 | wx.EVT_MENU(self, id, self._back_to_bookmark) |
---|
1062 | state = self.state.clone_state() |
---|
1063 | comp_state = copy.deepcopy(self.state.state_list[str(compute_num)]) |
---|
1064 | self.state.bookmark_list[self.bookmark_num] = [my_time, date, |
---|
1065 | state,comp_state] |
---|
1066 | self.state.toXML(self, doc=None, entry_node=None) |
---|
1067 | |
---|
1068 | wx.PostEvent(self.parent,StatusEvent(status=msg,info="info")) |
---|
1069 | wx.PostEvent(self.parent, |
---|
1070 | AppendBookmarkEvent(title=name, |
---|
1071 | hint=str(msg), |
---|
1072 | handler=self._back_to_bookmark)) |
---|
1073 | |
---|
1074 | def _back_to_bookmark(self,event): |
---|
1075 | """ |
---|
1076 | Bring the panel back to the state of bookmarked requested by |
---|
1077 | context menu event |
---|
1078 | and set it as a new state |
---|
1079 | """ |
---|
1080 | self._manager.on_perspective(event) |
---|
1081 | menu = event.GetEventObject() |
---|
1082 | ## post help message for the selected model |
---|
1083 | msg = menu.GetHelpString(event.GetId()) |
---|
1084 | msg +=" reloaded" |
---|
1085 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1086 | |
---|
1087 | name= menu.GetLabel(event.GetId()) |
---|
1088 | |
---|
1089 | num,time = name.split(']') |
---|
1090 | current_state_num = self.state.state_num |
---|
1091 | self.get_bookmark_by_num(num) |
---|
1092 | state_num = int(current_state_num)+1 |
---|
1093 | |
---|
1094 | self.state.saved_state['state_num'] = state_num |
---|
1095 | #copy.deepcopy(self.state.saved_state) |
---|
1096 | self.state.state_list[str(state_num)] = self.state.clone_state() |
---|
1097 | self.state.state_num = state_num |
---|
1098 | |
---|
1099 | self._set_undo_flag(True) |
---|
1100 | self._info_bookmark_num(event) |
---|
1101 | |
---|
1102 | def _info_bookmark_num(self,event=None): |
---|
1103 | """ |
---|
1104 | print the bookmark number in info |
---|
1105 | |
---|
1106 | : event: popUpMenu event |
---|
1107 | """ |
---|
1108 | if event == None: return |
---|
1109 | # get the object |
---|
1110 | menu = event.GetEventObject() |
---|
1111 | item = menu.FindItemById(event.GetId()) |
---|
1112 | text = item.GetText() |
---|
1113 | num = text.split(']')[0] |
---|
1114 | msg = "bookmark num = %s "% num |
---|
1115 | |
---|
1116 | wx.PostEvent(self.parent, StatusEvent(status = msg )) |
---|
1117 | |
---|
1118 | def _info_state_num(self): |
---|
1119 | """ |
---|
1120 | print the current state number in info |
---|
1121 | """ |
---|
1122 | msg = "state num = " |
---|
1123 | msg += self.state.state_num |
---|
1124 | |
---|
1125 | wx.PostEvent(self.parent, StatusEvent(status = msg)) |
---|
1126 | |
---|
1127 | def _get_time_stamp(self): |
---|
1128 | """ |
---|
1129 | return time and date stings |
---|
1130 | """ |
---|
1131 | # date and time |
---|
1132 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
1133 | my_time= str(hour)+":"+str(minute)+":"+str(second) |
---|
1134 | date= str( month)+"/"+str(day)+"/"+str(year) |
---|
1135 | return my_time, date |
---|
1136 | |
---|
1137 | |
---|
1138 | def on_save(self, evt=None): |
---|
1139 | """ |
---|
1140 | Save invariant state into a file |
---|
1141 | """ |
---|
1142 | # Ask the user the location of the file to write to. |
---|
1143 | path = None |
---|
1144 | dlg = wx.FileDialog(self, "Choose a file", |
---|
1145 | self._default_save_location, "", "*.inv", wx.SAVE) |
---|
1146 | if dlg.ShowModal() == wx.ID_OK: |
---|
1147 | path = dlg.GetPath() |
---|
1148 | self._default_save_location = os.path.dirname(path) |
---|
1149 | else: |
---|
1150 | return None |
---|
1151 | |
---|
1152 | dlg.Destroy() |
---|
1153 | |
---|
1154 | self._manager.save_file(filepath=path, state=self.state) |
---|
1155 | |
---|
1156 | def _show_message(self, mssg='',msg='Warning'): |
---|
1157 | """ |
---|
1158 | Show warning message when resetting data |
---|
1159 | """ |
---|
1160 | # no message for now |
---|
1161 | return True |
---|
1162 | count_bf = self.data_cbbox.GetCount() |
---|
1163 | if count_bf > 1: |
---|
1164 | msg += "Loading a new data set will reset all the work" |
---|
1165 | msg += " done in this panel. \n\r" |
---|
1166 | mssg += "Please make sure to save it first... \n\r" |
---|
1167 | answer = wx.MessageBox(mssg, msg, |
---|
1168 | wx.CANCEL|wx.OK|wx.ICON_EXCLAMATION) |
---|
1169 | |
---|
1170 | if answer == wx.OK: |
---|
1171 | return True |
---|
1172 | else: |
---|
1173 | return False |
---|
1174 | else: True |
---|
1175 | |
---|
1176 | def _reset_output(self): |
---|
1177 | """ |
---|
1178 | clear outputs textcrtl |
---|
1179 | """ |
---|
1180 | self.invariant_total_tcl.Clear() |
---|
1181 | self.invariant_total_err_tcl.Clear() |
---|
1182 | self.volume_tcl.Clear() |
---|
1183 | self.volume_err_tcl.Clear() |
---|
1184 | self.surface_tcl.Clear() |
---|
1185 | self.surface_err_tcl.Clear() |
---|
1186 | #prepare a new container to put result of invariant |
---|
1187 | self.inv_container = InvariantContainer() |
---|
1188 | |
---|
1189 | |
---|
1190 | def _on_context_menu(self,event): |
---|
1191 | |
---|
1192 | pos = event.GetPosition() |
---|
1193 | pos = self.ScreenToClient(pos) |
---|
1194 | |
---|
1195 | self.PopupMenu(self.popUpMenu, pos) |
---|
1196 | |
---|
1197 | def _define_structure(self): |
---|
1198 | """ |
---|
1199 | Define main sizers needed for this panel |
---|
1200 | """ |
---|
1201 | ## Box sizers must be defined first before |
---|
1202 | #defining buttons/textctrls (MAC). |
---|
1203 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
1204 | #Sizer related to outputs |
---|
1205 | outputs_box = wx.StaticBox(self, -1, "Outputs") |
---|
1206 | self.outputs_sizer = wx.StaticBoxSizer(outputs_box, wx.VERTICAL) |
---|
1207 | self.outputs_sizer.SetMinSize((PANEL_WIDTH,-1)) |
---|
1208 | #Sizer related to data |
---|
1209 | data_name_box = wx.StaticBox(self, -1, "I(q) Data Source") |
---|
1210 | self.data_name_boxsizer = wx.StaticBoxSizer(data_name_box, wx.VERTICAL) |
---|
1211 | self.data_name_boxsizer.SetMinSize((PANEL_WIDTH,-1)) |
---|
1212 | self.hint_msg_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1213 | self.data_name_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1214 | |
---|
1215 | self.data_range_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1216 | #Sizer related to background and scale |
---|
1217 | self.bkg_scale_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1218 | #Sizer related to contrast and porod constant |
---|
1219 | self.contrast_porod_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1220 | #Sizer related to inputs |
---|
1221 | inputs_box = wx.StaticBox(self, -1, "Customized Inputs") |
---|
1222 | self.inputs_sizer = wx.StaticBoxSizer(inputs_box, wx.VERTICAL) |
---|
1223 | #Sizer related to extrapolation |
---|
1224 | extrapolation_box = wx.StaticBox(self, -1, "Extrapolation") |
---|
1225 | self.extrapolation_sizer = wx.StaticBoxSizer(extrapolation_box, |
---|
1226 | wx.VERTICAL) |
---|
1227 | self.extrapolation_sizer.SetMinSize((PANEL_WIDTH,-1)) |
---|
1228 | self.extrapolation_range_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1229 | self.extrapolation_low_high_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1230 | #Sizer related to extrapolation at low q range |
---|
1231 | low_q_box = wx.StaticBox(self, -1, "Low Q") |
---|
1232 | self.low_extrapolation_sizer = wx.StaticBoxSizer(low_q_box, wx.VERTICAL) |
---|
1233 | self.low_q_sizer = wx.GridBagSizer(5,5) |
---|
1234 | #Sizer related to extrapolation at low q range |
---|
1235 | high_q_box = wx.StaticBox(self, -1, "High Q") |
---|
1236 | self.high_extrapolation_sizer = wx.StaticBoxSizer(high_q_box, |
---|
1237 | wx.VERTICAL) |
---|
1238 | self.high_q_sizer = wx.GridBagSizer(5,5) |
---|
1239 | #sizer to define outputs |
---|
1240 | self.volume_surface_sizer = wx.GridBagSizer(5,5) |
---|
1241 | #Sizer related to invariant output |
---|
1242 | self.invariant_sizer = wx.GridBagSizer(5, 5) |
---|
1243 | #Sizer related to button |
---|
1244 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1245 | #Sizer related to save button |
---|
1246 | self.save_button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1247 | |
---|
1248 | def _layout_data_name(self): |
---|
1249 | """ |
---|
1250 | Draw widgets related to data's name |
---|
1251 | """ |
---|
1252 | #Sizer hint |
---|
1253 | hint_msg = "" |
---|
1254 | |
---|
1255 | self.hint_msg_txt = wx.StaticText(self, -1, hint_msg) |
---|
1256 | self.hint_msg_txt.SetForegroundColour("red") |
---|
1257 | msg = "Highlight = mouse the mouse's cursor on the data until" |
---|
1258 | msg += " the plot's color changes to yellow" |
---|
1259 | self.hint_msg_txt.SetToolTipString(msg) |
---|
1260 | self.hint_msg_sizer.Add(self.hint_msg_txt) |
---|
1261 | #Data name [string] |
---|
1262 | data_name_txt = wx.StaticText(self, -1, 'Data : ') |
---|
1263 | |
---|
1264 | self.data_name_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH*5, 20), |
---|
1265 | style=0) |
---|
1266 | self.data_name_tcl.SetToolTipString("Data's name.") |
---|
1267 | self.data_name_sizer.AddMany([(data_name_txt, 0, wx.LEFT|wx.RIGHT, 10), |
---|
1268 | (self.data_name_tcl, 0, wx.EXPAND)]) |
---|
1269 | #Data range [string] |
---|
1270 | data_range_txt = wx.StaticText(self, -1, 'Total Q Range (1/A): ') |
---|
1271 | data_min_txt = wx.StaticText(self, -1, 'Min : ') |
---|
1272 | self.data_min_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
1273 | style=0, name='data_min_tcl') |
---|
1274 | self.data_min_tcl.SetToolTipString("The minimum value of q range.") |
---|
1275 | data_max_txt = wx.StaticText(self, -1, 'Max : ') |
---|
1276 | self.data_max_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
1277 | style=0, name='data_max_tcl') |
---|
1278 | self.data_max_tcl.SetToolTipString("The maximum value of q range.") |
---|
1279 | self.data_range_sizer.AddMany([(data_range_txt, 0, wx.RIGHT, 10), |
---|
1280 | (data_min_txt, 0, wx.RIGHT, 10), |
---|
1281 | (self.data_min_tcl, 0, wx.RIGHT, 10), |
---|
1282 | (data_max_txt, 0, wx.RIGHT, 10), |
---|
1283 | (self.data_max_tcl, 0, wx.RIGHT, 10)]) |
---|
1284 | self.data_name_boxsizer.AddMany([(self.hint_msg_sizer, 0 , wx.ALL, 5), |
---|
1285 | (self.data_name_sizer, 0 , wx.ALL, 10), |
---|
1286 | (self.data_range_sizer, 0 , wx.ALL, 10)]) |
---|
1287 | |
---|
1288 | def _layout_bkg_scale(self): |
---|
1289 | """ |
---|
1290 | Draw widgets related to background and scale |
---|
1291 | """ |
---|
1292 | background_txt = wx.StaticText(self, -1, 'Background : ') |
---|
1293 | self.background_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
1294 | style=0, name='background_tcl') |
---|
1295 | wx.EVT_TEXT(self, self.background_tcl.GetId(), self._on_text) |
---|
1296 | background_hint_txt = "Background" |
---|
1297 | self.background_tcl.SetToolTipString(background_hint_txt) |
---|
1298 | background_unit_txt = wx.StaticText(self, -1, '[1/cm]') |
---|
1299 | scale_txt = wx.StaticText(self, -1, 'Scale : ') |
---|
1300 | self.scale_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0, |
---|
1301 | name='scale_tcl') |
---|
1302 | wx.EVT_TEXT(self, self.scale_tcl.GetId(), self._on_text) |
---|
1303 | scale_hint_txt = "Scale" |
---|
1304 | self.scale_tcl.SetToolTipString(scale_hint_txt) |
---|
1305 | self.bkg_scale_sizer.AddMany([(background_txt, 0, wx.LEFT, 10), |
---|
1306 | (self.background_tcl, 0, wx.LEFT, 5), |
---|
1307 | (background_unit_txt, 0, wx.LEFT, 10), |
---|
1308 | (scale_txt, 0, wx.LEFT, 70), |
---|
1309 | (self.scale_tcl, 0, wx.LEFT, 40)]) |
---|
1310 | |
---|
1311 | def _layout_contrast_porod(self): |
---|
1312 | """ |
---|
1313 | Draw widgets related to porod constant and contrast |
---|
1314 | """ |
---|
1315 | contrast_txt = wx.StaticText(self, -1, 'Contrast : ') |
---|
1316 | self.contrast_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
1317 | style=0,name='contrast_tcl') |
---|
1318 | wx.EVT_TEXT(self, self.contrast_tcl.GetId(), self._on_text) |
---|
1319 | contrast_hint_txt = "Contrast" |
---|
1320 | self.contrast_tcl.SetToolTipString(contrast_hint_txt) |
---|
1321 | contrast_unit_txt = wx.StaticText(self, -1, '[1/A^(2)]') |
---|
1322 | porod_const_txt = wx.StaticText(self, -1, 'Porod Constant:') |
---|
1323 | self.porod_constant_tcl = InvTextCtrl(self, -1, |
---|
1324 | size=(_BOX_WIDTH, 20), style=0, |
---|
1325 | name='porod_constant_tcl') |
---|
1326 | wx.EVT_TEXT(self, self.porod_constant_tcl.GetId(), self._on_text) |
---|
1327 | porod_const_hint_txt = "Porod Constant" |
---|
1328 | self.porod_constant_tcl.SetToolTipString(porod_const_hint_txt) |
---|
1329 | optional_txt = wx.StaticText(self, -1, '(Optional)') |
---|
1330 | self.contrast_porod_sizer.AddMany([(contrast_txt, 0, wx.LEFT, 10), |
---|
1331 | (self.contrast_tcl, 0, wx.LEFT, 20), |
---|
1332 | (contrast_unit_txt, 0, wx.LEFT, 10), |
---|
1333 | (porod_const_txt, 0, wx.LEFT, 50), |
---|
1334 | (self.porod_constant_tcl, 0, wx.LEFT, 0), |
---|
1335 | (optional_txt, 0, wx.LEFT, 10)]) |
---|
1336 | |
---|
1337 | def _enable_fit_power_law_low(self, event=None): |
---|
1338 | """ |
---|
1339 | Enable and disable the power value editing |
---|
1340 | """ |
---|
1341 | if event != None: |
---|
1342 | self._set_bookmark_flag(True) |
---|
1343 | self._set_preview_flag(False) |
---|
1344 | |
---|
1345 | if self.fix_enable_low.IsEnabled(): |
---|
1346 | |
---|
1347 | if self.fix_enable_low.GetValue(): |
---|
1348 | self.fit_enable_low.SetValue(False) |
---|
1349 | self.power_low_tcl.Enable() |
---|
1350 | else: |
---|
1351 | self.fit_enable_low.SetValue(True) |
---|
1352 | self.power_low_tcl.Disable() |
---|
1353 | self._set_state(event=event) |
---|
1354 | |
---|
1355 | def _enable_low_q_section(self, event=None): |
---|
1356 | """ |
---|
1357 | Disable or enable some button if the user enable low q extrapolation |
---|
1358 | """ |
---|
1359 | if event != None: |
---|
1360 | self._set_bookmark_flag(True) |
---|
1361 | self._set_preview_flag(False) |
---|
1362 | |
---|
1363 | if self.enable_low_cbox.GetValue(): |
---|
1364 | self.npts_low_tcl.Enable() |
---|
1365 | self.fix_enable_low.Enable() |
---|
1366 | self.fit_enable_low.Enable() |
---|
1367 | self.guinier.Enable() |
---|
1368 | self.power_law_low.Enable() |
---|
1369 | |
---|
1370 | else: |
---|
1371 | self.npts_low_tcl.Disable() |
---|
1372 | self.fix_enable_low.Disable() |
---|
1373 | self.fit_enable_low.Disable() |
---|
1374 | self.guinier.Disable() |
---|
1375 | self.power_law_low.Disable() |
---|
1376 | |
---|
1377 | self._enable_power_law_low() |
---|
1378 | self._enable_fit_power_law_low() |
---|
1379 | self._set_state(event=event) |
---|
1380 | self.button_calculate.SetFocus() |
---|
1381 | |
---|
1382 | def _enable_power_law_low(self, event=None): |
---|
1383 | """ |
---|
1384 | Enable editing power law section at low q range |
---|
1385 | """ |
---|
1386 | if event != None: |
---|
1387 | self._set_bookmark_flag(True) |
---|
1388 | self._set_preview_flag(False) |
---|
1389 | if self.guinier.GetValue(): |
---|
1390 | self.power_law_low.SetValue(False) |
---|
1391 | self.fix_enable_low.Disable() |
---|
1392 | self.fit_enable_low.Disable() |
---|
1393 | self.power_low_tcl.Disable() |
---|
1394 | else: |
---|
1395 | self.power_law_low.SetValue(True) |
---|
1396 | self.fix_enable_low.Enable() |
---|
1397 | self.fit_enable_low.Enable() |
---|
1398 | self.power_low_tcl.Enable() |
---|
1399 | self._enable_fit_power_law_low() |
---|
1400 | self._set_state(event=event) |
---|
1401 | |
---|
1402 | def _layout_extrapolation_low(self): |
---|
1403 | """ |
---|
1404 | Draw widgets related to extrapolation at low q range |
---|
1405 | """ |
---|
1406 | self.enable_low_cbox = wx.CheckBox(self, -1, |
---|
1407 | "Enable Extrapolate Low Q", |
---|
1408 | name='enable_low_cbox') |
---|
1409 | wx.EVT_CHECKBOX(self, self.enable_low_cbox.GetId(), |
---|
1410 | self._enable_low_q_section) |
---|
1411 | self.fix_enable_low = wx.RadioButton(self, -1, 'Fix', |
---|
1412 | (10, 10), style=wx.RB_GROUP, |
---|
1413 | name='fix_enable_low') |
---|
1414 | self.Bind(wx.EVT_RADIOBUTTON, self._enable_fit_power_law_low, |
---|
1415 | id=self.fix_enable_low.GetId()) |
---|
1416 | self.fit_enable_low = wx.RadioButton(self, -1, 'Fit', (10, 10), |
---|
1417 | name='fit_enable_low') |
---|
1418 | self.Bind(wx.EVT_RADIOBUTTON, self._enable_fit_power_law_low, |
---|
1419 | id=self.fit_enable_low.GetId()) |
---|
1420 | self.guinier = wx.RadioButton(self, -1, 'Guinier', |
---|
1421 | (10, 10), style=wx.RB_GROUP, |
---|
1422 | name='guinier') |
---|
1423 | self.Bind(wx.EVT_RADIOBUTTON, self._enable_power_law_low, |
---|
1424 | id=self.guinier.GetId()) |
---|
1425 | self.power_law_low = wx.RadioButton(self, -1, 'Power Law', |
---|
1426 | (10, 10), name='power_law_low') |
---|
1427 | self.Bind(wx.EVT_RADIOBUTTON, self._enable_power_law_low, |
---|
1428 | id=self.power_law_low.GetId()) |
---|
1429 | |
---|
1430 | npts_low_txt = wx.StaticText(self, -1, 'Npts') |
---|
1431 | self.npts_low_tcl = InvTextCtrl(self, -1, |
---|
1432 | size=(_BOX_WIDTH*2/3, -1), |
---|
1433 | name='npts_low_tcl') |
---|
1434 | wx.EVT_TEXT(self, self.npts_low_tcl.GetId(), self._on_text) |
---|
1435 | msg_hint = "Number of Q points to consider" |
---|
1436 | msg_hint +="while extrapolating the low-Q region" |
---|
1437 | self.npts_low_tcl.SetToolTipString(msg_hint) |
---|
1438 | power_txt = wx.StaticText(self, -1, 'Power') |
---|
1439 | self.power_low_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH*2/3, -1), |
---|
1440 | name='power_low_tcl') |
---|
1441 | wx.EVT_TEXT(self, self.power_low_tcl.GetId(), self._on_text) |
---|
1442 | |
---|
1443 | power_hint_txt = "Exponent to apply to the Power_law function." |
---|
1444 | self.power_low_tcl.SetToolTipString(power_hint_txt) |
---|
1445 | iy = 0 |
---|
1446 | ix = 0 |
---|
1447 | self.low_q_sizer.Add(self.enable_low_cbox,(iy, ix), (1, 5), |
---|
1448 | wx.TOP|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1449 | iy += 1 |
---|
1450 | ix = 0 |
---|
1451 | self.low_q_sizer.Add(npts_low_txt,(iy, ix), (1, 1), |
---|
1452 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1453 | ix += 1 |
---|
1454 | self.low_q_sizer.Add(self.npts_low_tcl, (iy, ix), (1,1), |
---|
1455 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1456 | iy += 1 |
---|
1457 | ix = 0 |
---|
1458 | self.low_q_sizer.Add(self.guinier,(iy, ix),(1,2), |
---|
1459 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1460 | iy += 1 |
---|
1461 | ix = 0 |
---|
1462 | self.low_q_sizer.Add(self.power_law_low,(iy, ix), (1, 2), |
---|
1463 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1464 | |
---|
1465 | # Parameter controls for power law |
---|
1466 | ix = 1 |
---|
1467 | iy += 1 |
---|
1468 | self.low_q_sizer.Add(self.fix_enable_low,(iy, ix), (1, 1), |
---|
1469 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1470 | ix += 1 |
---|
1471 | self.low_q_sizer.Add(self.fit_enable_low,(iy, ix),(1,1), |
---|
1472 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1473 | ix = 1 |
---|
1474 | iy += 1 |
---|
1475 | self.low_q_sizer.Add(power_txt,(iy, ix), (1, 1), |
---|
1476 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1477 | ix += 1 |
---|
1478 | self.low_q_sizer.Add(self.power_low_tcl, (iy, ix), (1, 1), |
---|
1479 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1480 | self.low_extrapolation_sizer.AddMany([(self.low_q_sizer, 0, |
---|
1481 | wx.BOTTOM|wx.RIGHT, 15)]) |
---|
1482 | |
---|
1483 | def _enable_fit_power_law_high(self, event=None): |
---|
1484 | """ |
---|
1485 | Enable and disable the power value editing |
---|
1486 | """ |
---|
1487 | if event != None: |
---|
1488 | self._set_bookmark_flag(True) |
---|
1489 | |
---|
1490 | self._set_preview_flag(False) |
---|
1491 | if self.fix_enable_high.IsEnabled(): |
---|
1492 | if self.fix_enable_high.GetValue(): |
---|
1493 | self.fit_enable_high.SetValue(False) |
---|
1494 | self.power_high_tcl.Enable() |
---|
1495 | else: |
---|
1496 | self.fit_enable_high.SetValue(True) |
---|
1497 | self.power_high_tcl.Disable() |
---|
1498 | self._set_state(event=event) |
---|
1499 | |
---|
1500 | def _enable_high_q_section(self, event=None): |
---|
1501 | """ |
---|
1502 | Disable or enable some button if the user enable high q extrapolation |
---|
1503 | """ |
---|
1504 | if event != None: |
---|
1505 | self._set_bookmark_flag(True) |
---|
1506 | self._set_preview_flag(False) |
---|
1507 | if self.enable_high_cbox.GetValue(): |
---|
1508 | self.npts_high_tcl.Enable() |
---|
1509 | self.power_law_high.Enable() |
---|
1510 | self.power_high_tcl.Enable() |
---|
1511 | self.fix_enable_high.Enable() |
---|
1512 | self.fit_enable_high.Enable() |
---|
1513 | else: |
---|
1514 | self.npts_high_tcl.Disable() |
---|
1515 | self.power_law_high.Disable() |
---|
1516 | self.power_high_tcl.Disable() |
---|
1517 | self.fix_enable_high.Disable() |
---|
1518 | self.fit_enable_high.Disable() |
---|
1519 | self._enable_fit_power_law_high() |
---|
1520 | self._set_state(event=event) |
---|
1521 | self.button_calculate.SetFocus() |
---|
1522 | |
---|
1523 | def _layout_extrapolation_high(self): |
---|
1524 | """ |
---|
1525 | Draw widgets related to extrapolation at high q range |
---|
1526 | """ |
---|
1527 | self.enable_high_cbox = wx.CheckBox(self, -1, |
---|
1528 | "Enable Extrapolate high-Q", |
---|
1529 | name='enable_high_cbox') |
---|
1530 | wx.EVT_CHECKBOX(self, self.enable_high_cbox.GetId(), |
---|
1531 | self._enable_high_q_section) |
---|
1532 | self.fix_enable_high = wx.RadioButton(self, -1, 'Fix', |
---|
1533 | (10, 10), style=wx.RB_GROUP, |
---|
1534 | name='fix_enable_high') |
---|
1535 | self.Bind(wx.EVT_RADIOBUTTON, self._enable_fit_power_law_high, |
---|
1536 | id=self.fix_enable_high.GetId()) |
---|
1537 | self.fit_enable_high = wx.RadioButton(self, -1, 'Fit', (10, 10), |
---|
1538 | name='fit_enable_high') |
---|
1539 | self.Bind(wx.EVT_RADIOBUTTON, self._enable_fit_power_law_high, |
---|
1540 | id=self.fit_enable_high.GetId()) |
---|
1541 | |
---|
1542 | self.power_law_high = wx.StaticText(self, -1, 'Power Law') |
---|
1543 | msg_hint ="Check to extrapolate data at high-Q" |
---|
1544 | self.power_law_high.SetToolTipString(msg_hint) |
---|
1545 | npts_high_txt = wx.StaticText(self, -1, 'Npts') |
---|
1546 | self.npts_high_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH*2/3, -1), |
---|
1547 | name='npts_high_tcl') |
---|
1548 | wx.EVT_TEXT(self, self.npts_high_tcl.GetId(), self._on_text) |
---|
1549 | msg_hint = "Number of Q points to consider" |
---|
1550 | msg_hint += "while extrapolating the high-Q region" |
---|
1551 | self.npts_high_tcl.SetToolTipString(msg_hint) |
---|
1552 | power_txt = wx.StaticText(self, -1, 'Power') |
---|
1553 | self.power_high_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH*2/3, -1), |
---|
1554 | name='power_high_tcl') |
---|
1555 | wx.EVT_TEXT(self, self.power_high_tcl.GetId(), self._on_text) |
---|
1556 | power_hint_txt = "Exponent to apply to the Power_law function." |
---|
1557 | self.power_high_tcl.SetToolTipString(power_hint_txt) |
---|
1558 | iy = 0 |
---|
1559 | ix = 0 |
---|
1560 | self.high_q_sizer.Add(self.enable_high_cbox, (iy, ix), (1, 5), |
---|
1561 | wx.TOP|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1562 | iy += 1 |
---|
1563 | ix = 0 |
---|
1564 | self.high_q_sizer.Add(npts_high_txt, (iy, ix), (1, 1), |
---|
1565 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1566 | ix += 1 |
---|
1567 | self.high_q_sizer.Add(self.npts_high_tcl, (iy, ix), (1, 1), |
---|
1568 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1569 | iy += 2 |
---|
1570 | ix = 0 |
---|
1571 | self.high_q_sizer.Add(self.power_law_high, (iy, ix),(1, 2), |
---|
1572 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1573 | |
---|
1574 | # Parameter controls for power law |
---|
1575 | ix = 1 |
---|
1576 | iy += 1 |
---|
1577 | self.high_q_sizer.Add(self.fix_enable_high,(iy, ix), (1, 1), |
---|
1578 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1579 | ix += 1 |
---|
1580 | self.high_q_sizer.Add(self.fit_enable_high,(iy, ix), (1, 1), |
---|
1581 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1582 | ix = 1 |
---|
1583 | iy += 1 |
---|
1584 | self.high_q_sizer.Add(power_txt,(iy, ix), (1, 1), |
---|
1585 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1586 | ix += 1 |
---|
1587 | self.high_q_sizer.Add(self.power_high_tcl, (iy, ix), (1, 1), |
---|
1588 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1589 | self.high_extrapolation_sizer.AddMany([(self.high_q_sizer, 0, |
---|
1590 | wx.BOTTOM|wx.RIGHT, 10)]) |
---|
1591 | |
---|
1592 | def _layout_extrapolation(self): |
---|
1593 | """ |
---|
1594 | Draw widgets related to extrapolation |
---|
1595 | """ |
---|
1596 | extra_hint = "Extrapolation Maximum Q Range [1/A]: " |
---|
1597 | extra_hint_txt = wx.StaticText(self, -1, extra_hint) |
---|
1598 | #Extrapolation range [string] |
---|
1599 | extrapolation_min_txt = wx.StaticText(self, -1, 'Min :') |
---|
1600 | self.extrapolation_min_tcl = OutputTextCtrl(self, -1, |
---|
1601 | size=(_BOX_WIDTH, 20), style=0, |
---|
1602 | name='extrapolation_min_tcl') |
---|
1603 | self.extrapolation_min_tcl.SetValue(str(Q_MINIMUM)) |
---|
1604 | hint_msg = "The minimum extrapolated q value." |
---|
1605 | self.extrapolation_min_tcl.SetToolTipString(hint_msg) |
---|
1606 | extrapolation_max_txt = wx.StaticText(self, -1, 'Max :') |
---|
1607 | self.extrapolation_max_tcl = OutputTextCtrl(self, -1, |
---|
1608 | size=(_BOX_WIDTH, 20), |
---|
1609 | style=0, |
---|
1610 | name='extrapolation_max_tcl') |
---|
1611 | self.extrapolation_max_tcl.SetValue(str(Q_MAXIMUM)) |
---|
1612 | hint_msg = "The maximum extrapolated q value." |
---|
1613 | self.extrapolation_max_tcl.SetToolTipString(hint_msg) |
---|
1614 | self.extrapolation_range_sizer.AddMany([(extra_hint_txt, 0, |
---|
1615 | wx.LEFT, 10), |
---|
1616 | (extrapolation_min_txt, 0, |
---|
1617 | wx.LEFT, 10), |
---|
1618 | (self.extrapolation_min_tcl, |
---|
1619 | 0, wx.LEFT, 10), |
---|
1620 | (extrapolation_max_txt, 0, |
---|
1621 | wx.LEFT, 10), |
---|
1622 | (self.extrapolation_max_tcl, |
---|
1623 | 0, wx.LEFT, 10), |
---|
1624 | ]) |
---|
1625 | self._layout_extrapolation_low() |
---|
1626 | self._layout_extrapolation_high() |
---|
1627 | self.extrapolation_low_high_sizer.AddMany([(self.low_extrapolation_sizer, |
---|
1628 | 0, wx.ALL, 5), |
---|
1629 | (self.high_extrapolation_sizer, |
---|
1630 | 0, wx.ALL, 5)]) |
---|
1631 | self.extrapolation_sizer.AddMany([(self.extrapolation_range_sizer, 0, |
---|
1632 | wx.RIGHT, 5), |
---|
1633 | (self.extrapolation_low_high_sizer, 0, |
---|
1634 | wx.ALL, 5)]) |
---|
1635 | |
---|
1636 | def _layout_volume_surface_sizer(self): |
---|
1637 | """ |
---|
1638 | Draw widgets related to volume and surface |
---|
1639 | """ |
---|
1640 | unit_volume = '' |
---|
1641 | unit_surface = '' |
---|
1642 | uncertainty = "+/-" |
---|
1643 | volume_txt = wx.StaticText(self, -1, 'Volume Fraction ') |
---|
1644 | self.volume_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH, -1), |
---|
1645 | name='volume_tcl') |
---|
1646 | wx.EVT_TEXT(self, self.volume_tcl.GetId(), self._on_out_text) |
---|
1647 | self.volume_tcl.SetToolTipString("Volume fraction.") |
---|
1648 | self.volume_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH, -1), |
---|
1649 | name='volume_err_tcl') |
---|
1650 | wx.EVT_TEXT(self, self.volume_err_tcl.GetId(), self._on_out_text) |
---|
1651 | hint_msg = "Uncertainty on the volume fraction." |
---|
1652 | self.volume_err_tcl.SetToolTipString(hint_msg) |
---|
1653 | volume_units_txt = wx.StaticText(self, -1, unit_volume) |
---|
1654 | |
---|
1655 | surface_txt = wx.StaticText(self, -1, 'Specific Surface') |
---|
1656 | self.surface_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH, -1), |
---|
1657 | name='surface_tcl') |
---|
1658 | wx.EVT_TEXT(self, self.surface_tcl.GetId(), self._on_out_text) |
---|
1659 | self.surface_tcl.SetToolTipString("Specific surface value.") |
---|
1660 | self.surface_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH, -1), |
---|
1661 | name='surface_err_tcl') |
---|
1662 | wx.EVT_TEXT(self, self.surface_err_tcl.GetId(), self._on_out_text) |
---|
1663 | hint_msg = "Uncertainty on the specific surface." |
---|
1664 | self.surface_err_tcl.SetToolTipString(hint_msg) |
---|
1665 | surface_units_txt = wx.StaticText(self, -1, unit_surface) |
---|
1666 | iy = 0 |
---|
1667 | ix = 0 |
---|
1668 | self.volume_surface_sizer.Add(volume_txt, (iy, ix), (1, 1), |
---|
1669 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1670 | ix += 1 |
---|
1671 | self.volume_surface_sizer.Add(self.volume_tcl, (iy, ix), (1, 1), |
---|
1672 | wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
1673 | ix += 1 |
---|
1674 | self.volume_surface_sizer.Add(wx.StaticText(self, -1, uncertainty), |
---|
1675 | (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
1676 | ix += 1 |
---|
1677 | self.volume_surface_sizer.Add(self.volume_err_tcl, (iy, ix), (1, 1), |
---|
1678 | wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
1679 | ix += 1 |
---|
1680 | self.volume_surface_sizer.Add(volume_units_txt, (iy, ix), (1, 1), |
---|
1681 | wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
1682 | iy += 1 |
---|
1683 | ix = 0 |
---|
1684 | self.volume_surface_sizer.Add(surface_txt, (iy, ix), (1, 1), |
---|
1685 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1686 | ix += 1 |
---|
1687 | self.volume_surface_sizer.Add(self.surface_tcl, (iy, ix), (1, 1), |
---|
1688 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1689 | ix += 1 |
---|
1690 | self.volume_surface_sizer.Add(wx.StaticText(self, -1, uncertainty), |
---|
1691 | (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1692 | ix += 1 |
---|
1693 | self.volume_surface_sizer.Add(self.surface_err_tcl, (iy, ix), (1, 1), |
---|
1694 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1695 | ix += 1 |
---|
1696 | self.volume_surface_sizer.Add(surface_units_txt, (iy, ix), (1, 1), |
---|
1697 | wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
1698 | |
---|
1699 | def _layout_invariant_sizer(self): |
---|
1700 | """ |
---|
1701 | Draw widgets related to invariant |
---|
1702 | """ |
---|
1703 | uncertainty = "+/-" |
---|
1704 | unit_invariant = '[1/(cm * A)]' |
---|
1705 | invariant_total_txt = wx.StaticText(self, -1, 'Invariant Total [Q*]') |
---|
1706 | self.invariant_total_tcl = OutputTextCtrl(self, -1, |
---|
1707 | size=(_BOX_WIDTH,-1), |
---|
1708 | name='invariant_total_tcl') |
---|
1709 | msg_hint = "Total invariant [Q*], including extrapolated regions." |
---|
1710 | self.invariant_total_tcl.SetToolTipString(msg_hint) |
---|
1711 | self.invariant_total_err_tcl = OutputTextCtrl(self, -1, |
---|
1712 | size=(_BOX_WIDTH,-1), |
---|
1713 | name='invariant_total_err_tcl') |
---|
1714 | hint_msg = "Uncertainty on invariant." |
---|
1715 | self.invariant_total_err_tcl.SetToolTipString(hint_msg) |
---|
1716 | invariant_total_units_txt = wx.StaticText(self, -1, unit_invariant) |
---|
1717 | |
---|
1718 | #Invariant total |
---|
1719 | iy = 0 |
---|
1720 | ix = 0 |
---|
1721 | self.invariant_sizer.Add(invariant_total_txt, (iy, ix), (1, 1), |
---|
1722 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1723 | ix += 1 |
---|
1724 | self.invariant_sizer.Add(self.invariant_total_tcl, (iy, ix), (1, 1), |
---|
1725 | wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
1726 | ix += 1 |
---|
1727 | self.invariant_sizer.Add( wx.StaticText(self, -1, uncertainty), |
---|
1728 | (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
1729 | ix += 1 |
---|
1730 | self.invariant_sizer.Add(self.invariant_total_err_tcl, (iy, ix), (1, 1), |
---|
1731 | wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
1732 | ix += 1 |
---|
1733 | self.invariant_sizer.Add(invariant_total_units_txt,(iy, ix), (1, 1), |
---|
1734 | wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
1735 | |
---|
1736 | def _layout_inputs_sizer(self): |
---|
1737 | """ |
---|
1738 | Draw widgets related to inputs |
---|
1739 | """ |
---|
1740 | self._layout_bkg_scale() |
---|
1741 | self._layout_contrast_porod() |
---|
1742 | self.inputs_sizer.AddMany([(self.bkg_scale_sizer, 0, wx.ALL, 5), |
---|
1743 | (self.contrast_porod_sizer, 0, wx.ALL, 5)]) |
---|
1744 | |
---|
1745 | def _layout_outputs_sizer(self): |
---|
1746 | """ |
---|
1747 | Draw widgets related to outputs |
---|
1748 | """ |
---|
1749 | self._layout_volume_surface_sizer() |
---|
1750 | self._layout_invariant_sizer() |
---|
1751 | static_line = wx.StaticLine(self, -1) |
---|
1752 | self.outputs_sizer.AddMany([(self.volume_surface_sizer, 0, wx.ALL, 10), |
---|
1753 | (static_line, 0, wx.EXPAND, 0), |
---|
1754 | (self.invariant_sizer, 0, wx.ALL, 10)]) |
---|
1755 | def _layout_button(self): |
---|
1756 | """ |
---|
1757 | Do the layout for the button widgets |
---|
1758 | """ |
---|
1759 | #compute button |
---|
1760 | id = wx.NewId() |
---|
1761 | self.button_calculate = wx.Button(self, id, "Compute", |
---|
1762 | name='compute_invariant') |
---|
1763 | self.button_calculate.SetToolTipString("Compute invariant") |
---|
1764 | self.Bind(wx.EVT_BUTTON, self.compute_invariant, id=id) |
---|
1765 | #detail button |
---|
1766 | id = wx.NewId() |
---|
1767 | self.button_details = wx.Button(self, id, "Details?") |
---|
1768 | hint_msg = "Details about the results of the computation" |
---|
1769 | self.button_details.SetToolTipString(hint_msg) |
---|
1770 | self.Bind(wx.EVT_BUTTON, self.display_details, id=id) |
---|
1771 | details = "Details on Invariant Total Calculations" |
---|
1772 | details_txt = wx.StaticText(self, -1, details) |
---|
1773 | self.button_sizer.AddMany([((50,10), 0 , wx.LEFT,0), |
---|
1774 | (details_txt, 0 , |
---|
1775 | wx.RIGHT|wx.BOTTOM|wx.TOP, 10), |
---|
1776 | (self.button_details, 0 , wx.ALL, 10), |
---|
1777 | (self.button_calculate, 0 , |
---|
1778 | wx.RIGHT|wx.TOP|wx.BOTTOM, 10)]) |
---|
1779 | def _do_layout(self): |
---|
1780 | """ |
---|
1781 | Draw window content |
---|
1782 | """ |
---|
1783 | self._define_structure() |
---|
1784 | self._layout_data_name() |
---|
1785 | self._layout_extrapolation() |
---|
1786 | self._layout_inputs_sizer() |
---|
1787 | self._layout_outputs_sizer() |
---|
1788 | self._layout_button() |
---|
1789 | self.main_sizer.AddMany([(self.data_name_boxsizer,0, wx.ALL, 10), |
---|
1790 | (self.outputs_sizer, 0, |
---|
1791 | wx.LEFT|wx.RIGHT|wx.BOTTOM, 10), |
---|
1792 | (self.button_sizer,0, |
---|
1793 | wx.LEFT|wx.RIGHT|wx.BOTTOM, 10), |
---|
1794 | (self.inputs_sizer, 0, |
---|
1795 | wx.LEFT|wx.RIGHT|wx.BOTTOM, 10), |
---|
1796 | (self.extrapolation_sizer, 0, |
---|
1797 | wx.LEFT|wx.RIGHT|wx.BOTTOM, 10)]) |
---|
1798 | self.SetSizer(self.main_sizer) |
---|
1799 | self.SetAutoLayout(True) |
---|
1800 | |
---|
1801 | |
---|
1802 | class InvariantDialog(wx.Dialog): |
---|
1803 | """ |
---|
1804 | """ |
---|
1805 | def __init__(self, parent=None, id=1,graph=None, |
---|
1806 | data=None, title="Invariant",base=None): |
---|
1807 | wx.Dialog.__init__(self, parent, id, title, size=(PANEL_WIDTH, |
---|
1808 | PANEL_HEIGHT)) |
---|
1809 | self.panel = InvariantPanel(self) |
---|
1810 | self.Centre() |
---|
1811 | self.Show(True) |
---|
1812 | |
---|
1813 | class InvariantWindow(wx.Frame): |
---|
1814 | """ |
---|
1815 | """ |
---|
1816 | def __init__(self, parent=None, id=1, graph=None, |
---|
1817 | data=None, title="Invariant", base=None): |
---|
1818 | |
---|
1819 | wx.Frame.__init__(self, parent, id, title, size=(PANEL_WIDTH +100, |
---|
1820 | PANEL_HEIGHT+100)) |
---|
1821 | from DataLoader.loader import Loader |
---|
1822 | self.loader = Loader() |
---|
1823 | import invariant |
---|
1824 | path = "C:/ECLPS/workspace/trunk/DataLoader/test/ascii_test_3.txt" |
---|
1825 | data= self.loader.load(path) |
---|
1826 | self.panel = InvariantPanel(self) |
---|
1827 | |
---|
1828 | data.name = data.filename |
---|
1829 | self.panel.set_data(data) |
---|
1830 | self.Centre() |
---|
1831 | self.Show(True) |
---|
1832 | |
---|
1833 | class MyApp(wx.App): |
---|
1834 | def OnInit(self): |
---|
1835 | wx.InitAllImageHandlers() |
---|
1836 | frame = InvariantWindow() |
---|
1837 | frame.Show(True) |
---|
1838 | self.SetTopWindow(frame) |
---|
1839 | |
---|
1840 | return True |
---|
1841 | |
---|
1842 | # end of class MyApp |
---|
1843 | |
---|
1844 | if __name__ == "__main__": |
---|
1845 | app = MyApp(0) |
---|
1846 | app.MainLoop() |
---|