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