1 | import wx |
---|
2 | import sys |
---|
3 | import numpy as np |
---|
4 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
5 | from sas.sasgui.guiframe.events import PlotQrangeEvent |
---|
6 | from sas.sasgui.guiframe.events import StatusEvent |
---|
7 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
8 | from sas.sasgui.guiframe.utils import check_float |
---|
9 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
10 | from sas.sasgui.perspectives.invariant.invariant_widgets import OutputTextCtrl |
---|
11 | from sas.sasgui.perspectives.invariant.invariant_widgets import InvTextCtrl |
---|
12 | from sas.sasgui.perspectives.fitting.basepage import ModelTextCtrl |
---|
13 | from sas.sasgui.perspectives.corfunc.corfunc_state import CorfuncState |
---|
14 | import sas.sasgui.perspectives.corfunc.corfunc |
---|
15 | from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator |
---|
16 | from sas.sasgui.guiframe.documentation_window import DocumentationWindow |
---|
17 | from plot_labels import * |
---|
18 | |
---|
19 | OUTPUT_STRINGS = { |
---|
20 | 'max': "Long Period (A): ", |
---|
21 | 'Lc': "Average Hard Block Thickness (A): ", |
---|
22 | 'dtr': "Average Interface Thickness (A): ", |
---|
23 | 'd0': "Average Core Thickness: ", |
---|
24 | 'A': "Polydispersity: ", |
---|
25 | 'fill': "Local Crystallinity: " |
---|
26 | } |
---|
27 | |
---|
28 | if sys.platform.count("win32") > 0: |
---|
29 | _STATICBOX_WIDTH = 350 |
---|
30 | PANEL_WIDTH = 400 |
---|
31 | PANEL_HEIGHT = 700 |
---|
32 | FONT_VARIANT = 0 |
---|
33 | else: |
---|
34 | _STATICBOX_WIDTH = 390 |
---|
35 | PANEL_WIDTH = 430 |
---|
36 | PANEL_HEIGHT = 700 |
---|
37 | FONT_VARIANT = 1 |
---|
38 | |
---|
39 | class CorfuncPanel(ScrolledPanel,PanelBase): |
---|
40 | window_name = "Correlation Function" |
---|
41 | window_caption = "Correlation Function" |
---|
42 | CENTER_PANE = True |
---|
43 | |
---|
44 | def __init__(self, parent, data=None, manager=None, *args, **kwds): |
---|
45 | kwds["size"] = (PANEL_WIDTH, PANEL_HEIGHT) |
---|
46 | kwds["style"] = wx.FULL_REPAINT_ON_RESIZE |
---|
47 | ScrolledPanel.__init__(self, parent=parent, *args, **kwds) |
---|
48 | PanelBase.__init__(self, parent) |
---|
49 | self.SetupScrolling() |
---|
50 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
51 | self._manager = manager |
---|
52 | # The data with no correction for background values |
---|
53 | self._data = data # The data to be analysed (corrected fr background) |
---|
54 | self._extrapolated_data = None # The extrapolated data set |
---|
55 | self._transformed_data = None # Fourier trans. of the extrapolated data |
---|
56 | self._calculator = CorfuncCalculator() |
---|
57 | self._data_name_box = None # Text box to show name of file |
---|
58 | self._background_input = None |
---|
59 | self._qmin_input = None |
---|
60 | self._qmax1_input = None |
---|
61 | self._qmax2_input = None |
---|
62 | self._transform_btn = None |
---|
63 | self._extract_btn = None |
---|
64 | self.qmin = 0 |
---|
65 | self.qmax = (0, 0) |
---|
66 | self.background = 0 |
---|
67 | self.extracted_params = None |
---|
68 | self.transform_type = 'fourier' |
---|
69 | # Dictionary for saving refs to text boxes used to display output data |
---|
70 | self._output_boxes = None |
---|
71 | self.state = None |
---|
72 | self._do_layout() |
---|
73 | self._disable_inputs() |
---|
74 | self.set_state() |
---|
75 | self._qmin_input.Bind(wx.EVT_TEXT, self._on_enter_input) |
---|
76 | self._qmax1_input.Bind(wx.EVT_TEXT, self._on_enter_input) |
---|
77 | self._qmax2_input.Bind(wx.EVT_TEXT, self._on_enter_input) |
---|
78 | self._qmin_input.Bind(wx.EVT_MOUSE_EVENTS, self._on_click_qrange) |
---|
79 | self._qmax1_input.Bind(wx.EVT_MOUSE_EVENTS, self._on_click_qrange) |
---|
80 | self._qmax2_input.Bind(wx.EVT_MOUSE_EVENTS, self._on_click_qrange) |
---|
81 | self._background_input.Bind(wx.EVT_TEXT, self._on_enter_input) |
---|
82 | |
---|
83 | def set_state(self, state=None, data=None): |
---|
84 | """ |
---|
85 | Set the state of the panel. If no state is provided, the panel will |
---|
86 | be set to the default state. |
---|
87 | |
---|
88 | :param state: A CorfuncState object |
---|
89 | :param data: A Data1D object |
---|
90 | """ |
---|
91 | if state is None: |
---|
92 | self.state = CorfuncState() |
---|
93 | else: |
---|
94 | self.state = state |
---|
95 | if data is not None: |
---|
96 | self.state.data = data |
---|
97 | self.set_data(data, set_qrange=False) |
---|
98 | if self.state.qmin is not None: |
---|
99 | self.set_qmin(self.state.qmin) |
---|
100 | if self.state.qmax is not None and self.state.qmax != (None, None): |
---|
101 | self.set_qmax(tuple(self.state.qmax)) |
---|
102 | if self.state.background is not None: |
---|
103 | self.set_background(self.state.background) |
---|
104 | if self.state.is_extrapolated: |
---|
105 | self.compute_extrapolation() |
---|
106 | else: |
---|
107 | return |
---|
108 | if self.state.is_transformed: |
---|
109 | self.compute_transform() |
---|
110 | else: |
---|
111 | return |
---|
112 | if self.state.outputs is not None and self.state.outputs != {}: |
---|
113 | self.set_extracted_params(self.state.outputs, reset=True) |
---|
114 | |
---|
115 | def get_state(self): |
---|
116 | """ |
---|
117 | Return the state of the panel |
---|
118 | """ |
---|
119 | state = CorfuncState() |
---|
120 | state.set_saved_state('qmin_tcl', self.qmin) |
---|
121 | state.set_saved_state('qmax1_tcl', self.qmax[0]) |
---|
122 | state.set_saved_state('qmax2_tcl', self.qmax[1]) |
---|
123 | state.set_saved_state('background_tcl', self.background) |
---|
124 | state.outputs = self.extracted_params |
---|
125 | if self._data is not None: |
---|
126 | state.file = self._data.title |
---|
127 | state.data = self._data |
---|
128 | if self._extrapolated_data is not None: |
---|
129 | state.is_extrapolated = True |
---|
130 | if self._transformed_data is not None: |
---|
131 | state.is_transformed = True |
---|
132 | self.state = state |
---|
133 | |
---|
134 | return self.state |
---|
135 | |
---|
136 | def onSetFocus(self, evt): |
---|
137 | if evt is not None: |
---|
138 | evt.Skip() |
---|
139 | self._validate_inputs() |
---|
140 | |
---|
141 | def set_data(self, data=None, set_qrange=True): |
---|
142 | """ |
---|
143 | Update the GUI to reflect new data that has been loaded in |
---|
144 | |
---|
145 | :param data: The data that has been loaded |
---|
146 | """ |
---|
147 | if data is None: |
---|
148 | return |
---|
149 | self._enable_inputs() |
---|
150 | self._transform_btn.Disable() |
---|
151 | self._extract_btn.Disable() |
---|
152 | self._data_name_box.SetValue(str(data.title)) |
---|
153 | self._data = data |
---|
154 | self._calculator.set_data(data) |
---|
155 | # Reset the outputs |
---|
156 | self.set_extracted_params(None, reset=True) |
---|
157 | if self._manager is not None: |
---|
158 | self._manager.clear_data() |
---|
159 | self._manager.show_data(self._data, IQ_DATA_LABEL, reset=True) |
---|
160 | |
---|
161 | if set_qrange: |
---|
162 | lower = data.x[-1]*0.05 |
---|
163 | upper1 = data.x[-1] - lower*5 |
---|
164 | upper2 = data.x[-1] |
---|
165 | self.set_qmin(lower) |
---|
166 | self.set_qmax((upper1, upper2)) |
---|
167 | self.set_background(self._calculator.compute_background(self.qmax)) |
---|
168 | |
---|
169 | def get_data(self): |
---|
170 | return self._data |
---|
171 | |
---|
172 | def radio_changed(self, event=None): |
---|
173 | if event is not None: |
---|
174 | self.transform_type = event.GetEventObject().GetName() |
---|
175 | |
---|
176 | def compute_extrapolation(self, event=None): |
---|
177 | """ |
---|
178 | Compute and plot the extrapolated data. |
---|
179 | Called when Extrapolate button is pressed. |
---|
180 | """ |
---|
181 | if not self._validate_inputs: |
---|
182 | msg = "Invalid Q range entered." |
---|
183 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
184 | return |
---|
185 | self._calculator.set_data(self._data) |
---|
186 | self._calculator.lowerq = self.qmin |
---|
187 | self._calculator.upperq = self.qmax |
---|
188 | self._calculator.background = self.background |
---|
189 | try: |
---|
190 | self._extrapolated_data = self._calculator.compute_extrapolation() |
---|
191 | except: |
---|
192 | msg = "Error extrapolating data." |
---|
193 | wx.PostEvent(self._manager.parent, |
---|
194 | StatusEvent(status=msg, info="Error")) |
---|
195 | self._transform_btn.Disable() |
---|
196 | return |
---|
197 | # TODO: Find way to set xlim and ylim so full range of data can be |
---|
198 | # plotted but zoomed in |
---|
199 | maxq = self._data.x.max() |
---|
200 | mask = self._extrapolated_data.x <= maxq |
---|
201 | numpts = len(self._extrapolated_data.x[mask]) + 250 |
---|
202 | plot_x = self._extrapolated_data.x[0:numpts] |
---|
203 | plot_y = self._extrapolated_data.y[0:numpts] |
---|
204 | to_plot = Data1D(plot_x, plot_y) |
---|
205 | self._manager.show_data(to_plot, IQ_EXTRAPOLATED_DATA_LABEL) |
---|
206 | # Update state of the GUI |
---|
207 | self._transform_btn.Enable() |
---|
208 | self._extract_btn.Disable() |
---|
209 | self.set_extracted_params(reset=True) |
---|
210 | |
---|
211 | def compute_transform(self, event=None): |
---|
212 | """ |
---|
213 | Compute and plot the transformed data. |
---|
214 | Called when Transform button is pressed. |
---|
215 | """ |
---|
216 | if not self._calculator.transform_isrunning(): |
---|
217 | self._calculator.compute_transform(self._extrapolated_data, |
---|
218 | self.transform_type, background=self.background, |
---|
219 | completefn=self.transform_complete, |
---|
220 | updatefn=self.transform_update) |
---|
221 | |
---|
222 | self._transform_btn.SetLabel("Stop Tansform") |
---|
223 | else: |
---|
224 | self._calculator.stop_transform() |
---|
225 | self.transform_update("Transform cancelled.") |
---|
226 | self._transform_btn.SetLabel("Tansform") |
---|
227 | |
---|
228 | def transform_update(self, msg=""): |
---|
229 | """ |
---|
230 | Called from FourierThread to update on status of calculation |
---|
231 | """ |
---|
232 | wx.PostEvent(self._manager.parent, |
---|
233 | StatusEvent(status=msg)) |
---|
234 | |
---|
235 | def transform_complete(self, transform=None): |
---|
236 | """ |
---|
237 | Called from FourierThread when calculation has completed |
---|
238 | """ |
---|
239 | self._transform_btn.SetLabel("Tansform") |
---|
240 | if transform is None: |
---|
241 | msg = "Error calculating Transform." |
---|
242 | if self.transform_type == 'hilbert': |
---|
243 | msg = "Not yet implemented" |
---|
244 | wx.PostEvent(self._manager.parent, |
---|
245 | StatusEvent(status=msg, info="Error")) |
---|
246 | self._extract_btn.Disable() |
---|
247 | return |
---|
248 | self._transformed_data = transform |
---|
249 | import numpy as np |
---|
250 | plot_x = transform.x[np.where(transform.x <= 200)] |
---|
251 | plot_y = transform.y[np.where(transform.x <= 200)] |
---|
252 | self._manager.show_data(Data1D(plot_x, plot_y), TRANSFORM_LABEL) |
---|
253 | # Only enable extract params button if a fourier trans. has been done |
---|
254 | if self.transform_type == 'fourier': |
---|
255 | self._extract_btn.Enable() |
---|
256 | else: |
---|
257 | self._extract_btn.Disable() |
---|
258 | |
---|
259 | def extract_parameters(self, event=None): |
---|
260 | try: |
---|
261 | params = self._calculator.extract_parameters(self._transformed_data) |
---|
262 | except: |
---|
263 | params = None |
---|
264 | if params is None: |
---|
265 | msg = "Error extracting parameters." |
---|
266 | wx.PostEvent(self._manager.parent, |
---|
267 | StatusEvent(status=msg, info="Error")) |
---|
268 | return |
---|
269 | self.set_extracted_params(params) |
---|
270 | |
---|
271 | def on_help(self, event=None): |
---|
272 | """ |
---|
273 | Show the corfunc documentation |
---|
274 | """ |
---|
275 | tree_location = "user/sasgui/perspectives/corfunc/corfunc_help.html" |
---|
276 | doc_viewer = DocumentationWindow(self, -1, tree_location, "", |
---|
277 | "Correlation Function Help") |
---|
278 | |
---|
279 | def save_project(self, doc=None): |
---|
280 | """ |
---|
281 | Return an XML node containing the state of the panel |
---|
282 | |
---|
283 | :param doc: Am xml node to attach the project state to (optional) |
---|
284 | """ |
---|
285 | data = self._data |
---|
286 | state = self.get_state() |
---|
287 | if data is not None: |
---|
288 | new_doc, sasentry = self._manager.state_reader._to_xml_doc(data) |
---|
289 | new_doc = state.toXML(doc=new_doc, entry_node=sasentry) |
---|
290 | if new_doc is not None: |
---|
291 | if doc is not None and hasattr(doc, "firstChild"): |
---|
292 | child = new_doc.getElementsByTagName("SASentry") |
---|
293 | for item in child: |
---|
294 | doc.firstChild.appendChild(item) |
---|
295 | else: |
---|
296 | doc = new_doc |
---|
297 | return doc |
---|
298 | |
---|
299 | def set_qmin(self, qmin): |
---|
300 | self.qmin = qmin |
---|
301 | self._qmin_input.SetValue(str(qmin)) |
---|
302 | |
---|
303 | def set_qmax(self, qmax): |
---|
304 | self.qmax = qmax |
---|
305 | self._qmax1_input.SetValue(str(qmax[0])) |
---|
306 | self._qmax2_input.SetValue(str(qmax[1])) |
---|
307 | |
---|
308 | def set_background(self, bg): |
---|
309 | self.background = bg |
---|
310 | self._background_input.SetValue(str(bg)) |
---|
311 | self._calculator.background = bg |
---|
312 | |
---|
313 | def set_extracted_params(self, params=None, reset=False): |
---|
314 | self.extracted_params = params |
---|
315 | error = False |
---|
316 | if params is None: |
---|
317 | if not reset: error = True |
---|
318 | for key in OUTPUT_STRINGS.keys(): |
---|
319 | self._output_boxes[key].SetValue('-') |
---|
320 | else: |
---|
321 | if len(params) < len(OUTPUT_STRINGS): |
---|
322 | # Not all parameters were calculated |
---|
323 | error = True |
---|
324 | for key, value in params.iteritems(): |
---|
325 | rounded = self._round_sig_figs(value, 6) |
---|
326 | self._output_boxes[key].SetValue(rounded) |
---|
327 | if error: |
---|
328 | msg = 'Not all parameters were able to be calculated' |
---|
329 | wx.PostEvent(self._manager.parent, StatusEvent( |
---|
330 | status=msg, info='error')) |
---|
331 | |
---|
332 | |
---|
333 | def plot_qrange(self, active=None, leftdown=False): |
---|
334 | if active is None: |
---|
335 | active = self._qmin_input |
---|
336 | wx.PostEvent(self._manager.parent, PlotQrangeEvent( |
---|
337 | ctrl=[self._qmin_input, self._qmax1_input, self._qmax2_input], |
---|
338 | active=active, id=IQ_DATA_LABEL, is_corfunc=True, |
---|
339 | group_id=GROUP_ID_IQ_DATA, leftdown=leftdown)) |
---|
340 | |
---|
341 | |
---|
342 | def _compute_background(self, event=None): |
---|
343 | self.set_background(self._calculator.compute_background(self.qmax)) |
---|
344 | |
---|
345 | def _on_enter_input(self, event=None): |
---|
346 | """ |
---|
347 | Read values from input boxes and save to memory. |
---|
348 | """ |
---|
349 | if event is not None: event.Skip() |
---|
350 | if not self._validate_inputs(): |
---|
351 | return |
---|
352 | self.qmin = float(self._qmin_input.GetValue()) |
---|
353 | new_qmax1 = float(self._qmax1_input.GetValue()) |
---|
354 | new_qmax2 = float(self._qmax2_input.GetValue()) |
---|
355 | self.qmax = (new_qmax1, new_qmax2) |
---|
356 | self.background = float(self._background_input.GetValue()) |
---|
357 | self._calculator.background = self.background |
---|
358 | if event is not None: |
---|
359 | active_ctrl = event.GetEventObject() |
---|
360 | if active_ctrl == self._background_input: |
---|
361 | self._manager.show_data(self._data, IQ_DATA_LABEL, |
---|
362 | reset=False, active_ctrl=active_ctrl) |
---|
363 | |
---|
364 | def _on_click_qrange(self, event=None): |
---|
365 | if event is None: |
---|
366 | return |
---|
367 | event.Skip() |
---|
368 | if not self._validate_inputs(): return |
---|
369 | self.plot_qrange(active=event.GetEventObject(), |
---|
370 | leftdown=event.LeftDown()) |
---|
371 | |
---|
372 | def _validate_inputs(self): |
---|
373 | """ |
---|
374 | Check that the values for qmin and qmax in the input boxes are valid |
---|
375 | """ |
---|
376 | if self._data is None: |
---|
377 | return False |
---|
378 | qmin_valid = check_float(self._qmin_input) |
---|
379 | qmax1_valid = check_float(self._qmax1_input) |
---|
380 | qmax2_valid = check_float(self._qmax2_input) |
---|
381 | qmax_valid = qmax1_valid and qmax2_valid |
---|
382 | background_valid = check_float(self._background_input) |
---|
383 | msg = "" |
---|
384 | if (qmin_valid and qmax_valid and background_valid): |
---|
385 | qmin = float(self._qmin_input.GetValue()) |
---|
386 | qmax1 = float(self._qmax1_input.GetValue()) |
---|
387 | qmax2 = float(self._qmax2_input.GetValue()) |
---|
388 | background = float(self._background_input.GetValue()) |
---|
389 | if not qmin > self._data.x.min(): |
---|
390 | msg = "qmin must be greater than the lowest q value" |
---|
391 | qmin_valid = False |
---|
392 | elif qmax2 < qmax1: |
---|
393 | msg = "qmax1 must be less than qmax2" |
---|
394 | qmax_valid = False |
---|
395 | elif qmin > qmax1: |
---|
396 | msg = "qmin must be less than qmax" |
---|
397 | qmin_valid = False |
---|
398 | elif background > self._data.y.max(): |
---|
399 | msg = "background must be less than highest I" |
---|
400 | background_valid = False |
---|
401 | if not qmin_valid: |
---|
402 | self._qmin_input.SetBackgroundColour('pink') |
---|
403 | if not qmax_valid: |
---|
404 | self._qmax1_input.SetBackgroundColour('pink') |
---|
405 | self._qmax2_input.SetBackgroundColour('pink') |
---|
406 | if not background_valid: |
---|
407 | self._background_input.SetBackgroundColour('pink') |
---|
408 | if msg != "": |
---|
409 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
410 | if (qmin_valid and qmax_valid and background_valid): |
---|
411 | self._qmin_input.SetBackgroundColour(wx.WHITE) |
---|
412 | self._qmax1_input.SetBackgroundColour(wx.WHITE) |
---|
413 | self._qmax2_input.SetBackgroundColour(wx.WHITE) |
---|
414 | self._background_input.SetBackgroundColour(wx.WHITE) |
---|
415 | self._qmin_input.Refresh() |
---|
416 | self._qmax1_input.Refresh() |
---|
417 | self._qmax2_input.Refresh() |
---|
418 | self._background_input.Refresh() |
---|
419 | return (qmin_valid and qmax_valid and background_valid) |
---|
420 | |
---|
421 | def _do_layout(self): |
---|
422 | """ |
---|
423 | Draw the window content |
---|
424 | """ |
---|
425 | vbox = wx.GridBagSizer(0,0) |
---|
426 | |
---|
427 | # I(q) data box |
---|
428 | databox = wx.StaticBox(self, -1, "I(Q) Data Source") |
---|
429 | databox_sizer = wx.StaticBoxSizer(databox, wx.VERTICAL) |
---|
430 | |
---|
431 | file_sizer = wx.GridBagSizer(5, 5) |
---|
432 | |
---|
433 | file_name_label = wx.StaticText(self, -1, "Name:") |
---|
434 | file_sizer.Add(file_name_label, (0, 0), (1, 1), |
---|
435 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
436 | |
---|
437 | self._data_name_box = OutputTextCtrl(self, -1, |
---|
438 | size=(300,20)) |
---|
439 | file_sizer.Add(self._data_name_box, (0, 1), (1, 1), |
---|
440 | wx.CENTER | wx.ADJUST_MINSIZE, 15) |
---|
441 | |
---|
442 | file_sizer.AddSpacer((1, 25), pos=(0,2)) |
---|
443 | databox_sizer.Add(file_sizer, wx.TOP, 15) |
---|
444 | |
---|
445 | vbox.Add(databox_sizer, (0, 0), (1, 1), |
---|
446 | wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE | wx.TOP, 15) |
---|
447 | |
---|
448 | |
---|
449 | # Parameters |
---|
450 | qbox = wx.StaticBox(self, -1, "Input Parameters") |
---|
451 | qbox_sizer = wx.StaticBoxSizer(qbox, wx.VERTICAL) |
---|
452 | qbox_sizer.SetMinSize((_STATICBOX_WIDTH, 75)) |
---|
453 | |
---|
454 | q_sizer = wx.GridBagSizer(5, 5) |
---|
455 | |
---|
456 | # Explanation |
---|
457 | explanation_txt = ("Corfunc will use all values in the lower range for" |
---|
458 | " Guinier back extrapolation, and all values in the upper range " |
---|
459 | "for Porod forward extrapolation.") |
---|
460 | explanation_label = wx.StaticText(self, -1, explanation_txt, |
---|
461 | size=(_STATICBOX_WIDTH, 60)) |
---|
462 | |
---|
463 | q_sizer.Add(explanation_label, (0,0), (1,4), wx.LEFT | wx.EXPAND, 5) |
---|
464 | |
---|
465 | qrange_label = wx.StaticText(self, -1, "Q Range:", size=(50,20)) |
---|
466 | q_sizer.Add(qrange_label, (1,0), (1,1), wx.LEFT | wx.EXPAND, 5) |
---|
467 | |
---|
468 | # Lower Q Range |
---|
469 | qmin_label = wx.StaticText(self, -1, "Lower:", size=(50,20)) |
---|
470 | qmin_dash_label = wx.StaticText(self, -1, "-", size=(10,20), |
---|
471 | style=wx.ALIGN_CENTER_HORIZONTAL) |
---|
472 | |
---|
473 | qmin_lower = OutputTextCtrl(self, -1, size=(75, 20), value="0.0") |
---|
474 | self._qmin_input = ModelTextCtrl(self, -1, size=(75, 20), |
---|
475 | style=wx.TE_PROCESS_ENTER, name='qmin_input', |
---|
476 | text_enter_callback=self._on_enter_input) |
---|
477 | self._qmin_input.SetToolTipString(("Values with q < qmin will be used " |
---|
478 | "for Guinier back extrapolation")) |
---|
479 | |
---|
480 | q_sizer.Add(qmin_label, (2, 0), (1, 1), wx.LEFT | wx.EXPAND, 5) |
---|
481 | q_sizer.Add(qmin_lower, (2, 1), (1, 1), wx.LEFT, 5) |
---|
482 | q_sizer.Add(qmin_dash_label, (2, 2), (1, 1), wx.CENTER | wx.EXPAND, 5) |
---|
483 | q_sizer.Add(self._qmin_input, (2, 3), (1, 1), wx.LEFT, 5) |
---|
484 | |
---|
485 | # Upper Q range |
---|
486 | qmax_tooltip = ("Values with qmax1 < q < qmax2 will be used for Porod" |
---|
487 | " forward extrapolation") |
---|
488 | |
---|
489 | qmax_label = wx.StaticText(self, -1, "Upper:", size=(50,20)) |
---|
490 | qmax_dash_label = wx.StaticText(self, -1, "-", size=(10,20), |
---|
491 | style=wx.ALIGN_CENTER_HORIZONTAL) |
---|
492 | |
---|
493 | self._qmax1_input = ModelTextCtrl(self, -1, size=(75, 20), |
---|
494 | style=wx.TE_PROCESS_ENTER, name="qmax1_input", |
---|
495 | text_enter_callback=self._on_enter_input) |
---|
496 | self._qmax1_input.SetToolTipString(qmax_tooltip) |
---|
497 | self._qmax2_input = ModelTextCtrl(self, -1, size=(75, 20), |
---|
498 | style=wx.TE_PROCESS_ENTER, name="qmax2_input", |
---|
499 | text_enter_callback=self._on_enter_input) |
---|
500 | self._qmax2_input.SetToolTipString(qmax_tooltip) |
---|
501 | |
---|
502 | q_sizer.Add(qmax_label, (3, 0), (1, 1), wx.LEFT | wx.EXPAND, 5) |
---|
503 | q_sizer.Add(self._qmax1_input, (3, 1), (1, 1), wx.LEFT, 5) |
---|
504 | q_sizer.Add(qmax_dash_label, (3, 2), (1, 1), wx.CENTER | wx.EXPAND, 5) |
---|
505 | q_sizer.Add(self._qmax2_input, (3,3), (1, 1), wx.LEFT, 5) |
---|
506 | |
---|
507 | background_label = wx.StaticText(self, -1, "Background:", size=(80,20)) |
---|
508 | q_sizer.Add(background_label, (4,0), (1,1), wx.LEFT | wx.EXPAND, 5) |
---|
509 | |
---|
510 | self._background_input = ModelTextCtrl(self, -1, size=(75,20), |
---|
511 | style=wx.TE_PROCESS_ENTER, name='background_input', |
---|
512 | text_enter_callback=self._on_enter_input) |
---|
513 | self._background_input.SetToolTipString(("A background value to " |
---|
514 | "subtract from all intensity values")) |
---|
515 | q_sizer.Add(self._background_input, (4,1), (1,1), |
---|
516 | wx.RIGHT, 5) |
---|
517 | |
---|
518 | background_button = wx.Button(self, wx.NewId(), "Calculate", |
---|
519 | size=(75, 20)) |
---|
520 | background_button.Bind(wx.EVT_BUTTON, self._compute_background) |
---|
521 | q_sizer.Add(background_button, (4, 2), (1, 1), wx.RIGHT, 5) |
---|
522 | |
---|
523 | qbox_sizer.Add(q_sizer, wx.TOP, 0) |
---|
524 | |
---|
525 | vbox.Add(qbox_sizer, (1, 0), (1, 1), |
---|
526 | wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
527 | |
---|
528 | # Transform type |
---|
529 | transform_box = wx.StaticBox(self, -1, "Transform Type") |
---|
530 | transform_sizer = wx.StaticBoxSizer(transform_box, wx.VERTICAL) |
---|
531 | |
---|
532 | radio_sizer = wx.GridBagSizer(5,5) |
---|
533 | |
---|
534 | fourier_btn = wx.RadioButton(self, -1, "Fourier", name='fourier', |
---|
535 | style=wx.RB_GROUP) |
---|
536 | hilbert_btn = wx.RadioButton(self, -1, "Hilbert", name='hilbert') |
---|
537 | |
---|
538 | fourier_btn.Bind(wx.EVT_RADIOBUTTON, self.radio_changed) |
---|
539 | hilbert_btn.Bind(wx.EVT_RADIOBUTTON, self.radio_changed) |
---|
540 | |
---|
541 | radio_sizer.Add(fourier_btn, (0,0), (1,1), wx.LEFT | wx.EXPAND) |
---|
542 | radio_sizer.Add(hilbert_btn, (0,1), (1,1), wx.RIGHT | wx.EXPAND) |
---|
543 | |
---|
544 | transform_sizer.Add(radio_sizer, wx.TOP, 0) |
---|
545 | vbox.Add(transform_sizer, (2, 0), (1, 1), |
---|
546 | wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
547 | |
---|
548 | # Output data |
---|
549 | outputbox = wx.StaticBox(self, -1, "Output Parameters") |
---|
550 | outputbox_sizer = wx.StaticBoxSizer(outputbox, wx.VERTICAL) |
---|
551 | |
---|
552 | output_sizer = wx.GridBagSizer(5, 5) |
---|
553 | |
---|
554 | self._output_boxes = dict() |
---|
555 | i = 0 |
---|
556 | for key, value in OUTPUT_STRINGS.iteritems(): |
---|
557 | # Create a label and a text box for each poperty |
---|
558 | label = wx.StaticText(self, -1, value) |
---|
559 | output_box = OutputTextCtrl(self, wx.NewId(), |
---|
560 | value="-", style=wx.ALIGN_CENTER_HORIZONTAL) |
---|
561 | # Save the ID of each of the text boxes for accessing after the |
---|
562 | # output data has been calculated |
---|
563 | self._output_boxes[key] = output_box |
---|
564 | output_sizer.Add(label, (i, 0), (1, 1), wx.LEFT | wx.EXPAND, 15) |
---|
565 | output_sizer.Add(output_box, (i, 2), (1, 1), |
---|
566 | wx.RIGHT | wx.EXPAND, 15) |
---|
567 | i += 1 |
---|
568 | |
---|
569 | outputbox_sizer.Add(output_sizer, wx.TOP, 0) |
---|
570 | |
---|
571 | vbox.Add(outputbox_sizer, (3, 0), (1, 1), |
---|
572 | wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
573 | |
---|
574 | # Controls |
---|
575 | controlbox = wx.StaticBox(self, -1, "Controls") |
---|
576 | controlbox_sizer = wx.StaticBoxSizer(controlbox, wx.VERTICAL) |
---|
577 | |
---|
578 | controls_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
579 | |
---|
580 | extrapolate_btn = wx.Button(self, wx.NewId(), "Extrapolate") |
---|
581 | self._transform_btn = wx.Button(self, wx.NewId(), "Transform") |
---|
582 | self._extract_btn = wx.Button(self, wx.NewId(), "Compute Parameters") |
---|
583 | help_btn = wx.Button(self, -1, "HELP") |
---|
584 | |
---|
585 | self._transform_btn.Disable() |
---|
586 | self._extract_btn.Disable() |
---|
587 | |
---|
588 | extrapolate_btn.Bind(wx.EVT_BUTTON, self.compute_extrapolation) |
---|
589 | self._transform_btn.Bind(wx.EVT_BUTTON, self.compute_transform) |
---|
590 | self._extract_btn.Bind(wx.EVT_BUTTON, self.extract_parameters) |
---|
591 | help_btn.Bind(wx.EVT_BUTTON, self.on_help) |
---|
592 | |
---|
593 | controls_sizer.Add(extrapolate_btn, wx.CENTER | wx.EXPAND) |
---|
594 | controls_sizer.Add(self._transform_btn, wx.CENTER | wx.EXPAND) |
---|
595 | controls_sizer.Add(self._extract_btn, wx.CENTER | wx.EXPAND) |
---|
596 | controls_sizer.Add(help_btn, wx.CENTER | wx.EXPAND) |
---|
597 | |
---|
598 | controlbox_sizer.Add(controls_sizer, wx.TOP | wx.EXPAND, 0) |
---|
599 | vbox.Add(controlbox_sizer, (4, 0), (1, 1), |
---|
600 | wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
601 | |
---|
602 | |
---|
603 | self.SetSizer(vbox) |
---|
604 | |
---|
605 | def _disable_inputs(self): |
---|
606 | """ |
---|
607 | Disable all input fields |
---|
608 | """ |
---|
609 | self._qmin_input.Disable() |
---|
610 | self._qmax1_input.Disable() |
---|
611 | self._qmax2_input.Disable() |
---|
612 | self._background_input.Disable() |
---|
613 | |
---|
614 | def _enable_inputs(self): |
---|
615 | """ |
---|
616 | Enable all input fields |
---|
617 | """ |
---|
618 | self._qmin_input.Enable() |
---|
619 | self._qmax1_input.Enable() |
---|
620 | self._qmax2_input.Enable() |
---|
621 | self._background_input.Enable() |
---|
622 | |
---|
623 | def _round_sig_figs(self, x, sigfigs): |
---|
624 | """ |
---|
625 | Round a number to a given number of significant figures. |
---|
626 | |
---|
627 | :param x: The value to round |
---|
628 | :param sigfigs: How many significant figures to round to |
---|
629 | :return rounded_str: x rounded to the given number of significant |
---|
630 | figures, as a string |
---|
631 | """ |
---|
632 | # Index of first significant digit |
---|
633 | significant_digit = -int(np.floor(np.log10(np.abs(x)))) |
---|
634 | # Number of digits required for correct number of sig figs |
---|
635 | digits = significant_digit + (sigfigs - 1) |
---|
636 | rounded = np.round(x, decimals=digits) |
---|
637 | rounded_str = "{1:.{0}f}".format(sigfigs -1 + significant_digit, |
---|
638 | rounded) |
---|
639 | return rounded_str |
---|