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