source: sasview/src/sas/sasgui/perspectives/corfunc/corfunc_panel.py @ 412c509

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 412c509 was 412c509, checked in by lewis, 7 years ago

Compute 3D correlation function as well as 1D

  • Property mode set to 100644
File size: 31.6 KB
Line 
1import wx
2import sys
3import os
4import numpy as np
5from wx.lib.scrolledpanel import ScrolledPanel
6from sas.sasgui.guiframe.events import PlotQrangeEvent
7from sas.sasgui.guiframe.events import StatusEvent
8from sas.sasgui.guiframe.events import PanelOnFocusEvent
9from sas.sasgui.guiframe.panel_base import PanelBase
10from sas.sasgui.guiframe.utils import check_float
11from sas.sasgui.guiframe.dataFitting import Data1D
12from sas.sasgui.perspectives.invariant.invariant_widgets import OutputTextCtrl
13from sas.sasgui.perspectives.invariant.invariant_widgets import InvTextCtrl
14from sas.sasgui.perspectives.fitting.basepage import ModelTextCtrl
15from sas.sasgui.perspectives.corfunc.corfunc_state import CorfuncState
16import sas.sasgui.perspectives.corfunc.corfunc
17from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator
18from sas.sasgui.guiframe.documentation_window import DocumentationWindow
19from plot_labels import *
20
21OUTPUT_STRINGS = {
22    'max': "Long Period (A): ",
23    'Lc': "Average Hard Block Thickness (A): ",
24    'dtr': "Average Interface Thickness (A): ",
25    'd0': "Average Core Thickness: ",
26    'A': "Polydispersity: ",
27    'fill': "Local Crystallinity: "
28}
29
30if sys.platform.count("win32") > 0:
31    _STATICBOX_WIDTH = 350
32    PANEL_WIDTH = 400
33    PANEL_HEIGHT = 700
34    FONT_VARIANT = 0
35else:
36    _STATICBOX_WIDTH = 390
37    PANEL_WIDTH = 430
38    PANEL_HEIGHT = 700
39    FONT_VARIANT = 1
40
41class CorfuncPanel(ScrolledPanel,PanelBase):
42    window_name = "Correlation Function"
43    window_caption = "Correlation Function"
44    CENTER_PANE = True
45
46    def __init__(self, parent, data=None, manager=None, *args, **kwds):
47        kwds["size"] = (PANEL_WIDTH, PANEL_HEIGHT)
48        kwds["style"] = wx.FULL_REPAINT_ON_RESIZE
49        ScrolledPanel.__init__(self, parent=parent, *args, **kwds)
50        PanelBase.__init__(self, parent)
51        self.SetupScrolling()
52        self.SetWindowVariant(variant=FONT_VARIANT)
53        self._manager = manager
54        # The data with no correction for background values
55        self._data = data # The data to be analysed (corrected fr background)
56        self._extrapolated_data = None # The extrapolated data set
57        # Callable object of class CorfuncCalculator._Interpolator representing
58        # the extrapolated and interpolated data
59        self._extrapolated_fn = None
60        self._transformed_data = None # Fourier trans. of the extrapolated data
61        self._calculator = CorfuncCalculator()
62        self._data_name_box = None # Text box to show name of file
63        self._background_input = None
64        self._qmin_input = None
65        self._qmax1_input = None
66        self._qmax2_input = None
67        self._extrapolate_btn = None
68        self._transform_btn = None
69        self._extract_btn = None
70        self.qmin = 0
71        self.qmax = (0, 0)
72        self.background = 0
73        self.extracted_params = None
74        self.transform_type = 'fourier'
75        self._extrapolation_outputs = {}
76        # Dictionary for saving refs to text boxes used to display output data
77        self._output_boxes = None
78        self.state = None
79        self._do_layout()
80        self._disable_inputs()
81        self.set_state()
82        self._qmin_input.Bind(wx.EVT_TEXT, self._on_enter_input)
83        self._qmax1_input.Bind(wx.EVT_TEXT, self._on_enter_input)
84        self._qmax2_input.Bind(wx.EVT_TEXT, self._on_enter_input)
85        self._qmin_input.Bind(wx.EVT_MOUSE_EVENTS, self._on_click_qrange)
86        self._qmax1_input.Bind(wx.EVT_MOUSE_EVENTS, self._on_click_qrange)
87        self._qmax2_input.Bind(wx.EVT_MOUSE_EVENTS, self._on_click_qrange)
88        self._background_input.Bind(wx.EVT_TEXT, self._on_enter_input)
89
90    def set_state(self, state=None, data=None):
91        """
92        Set the state of the panel. If no state is provided, the panel will
93        be set to the default state.
94
95        :param state: A CorfuncState object
96        :param data: A Data1D object
97        """
98        if state is None:
99            self.state = CorfuncState()
100        else:
101            self.state = state
102        if data is not None:
103            self.state.data = data
104        self.set_data(data, set_qrange=False)
105        if self.state.qmin is not None:
106            self.set_qmin(self.state.qmin)
107        if self.state.qmax is not None and self.state.qmax != (None, None):
108            self.set_qmax(tuple(self.state.qmax))
109        if self.state.background is not None:
110            self.set_background(self.state.background)
111        if self.state.is_extrapolated:
112            self.compute_extrapolation()
113        else:
114            return
115        if self.state.is_transformed:
116            self.transform_type = self.state.transform_type
117            self.compute_transform()
118        else:
119            return
120        if self.state.outputs is not None and self.state.outputs != {}:
121            self.set_extracted_params(self.state.outputs, reset=True)
122
123    def get_state(self):
124        """
125        Return the state of the panel
126        """
127        state = CorfuncState()
128        state.set_saved_state('qmin_tcl', self.qmin)
129        state.set_saved_state('qmax1_tcl', self.qmax[0])
130        state.set_saved_state('qmax2_tcl', self.qmax[1])
131        state.set_saved_state('background_tcl', self.background)
132        state.outputs = self.extracted_params
133        if self._data is not None:
134            state.file = self._data.title
135            state.data = self._data
136        if self._extrapolated_data is not None:
137            state.is_extrapolated = True
138        if self._transformed_data is not None:
139            state.is_transformed = True
140            state.transform_type = self.transform_type
141        self.state = state
142
143        return self.state
144
145    def onSetFocus(self, evt):
146        if evt is not None:
147            evt.Skip()
148        self._validate_inputs()
149
150    def set_data(self, data=None, set_qrange=True):
151        """
152        Update the GUI to reflect new data that has been loaded in
153
154        :param data: The data that has been loaded
155        """
156        if data is None:
157            self._disable_inputs()
158            # Reset outputs
159            self.set_extracted_params(reset=True)
160            self.set_extrapolation_params()
161            self._data = None
162            return
163        self._enable_inputs()
164        self._transform_btn.Disable()
165        self._extract_btn.Disable()
166        self._data_name_box.SetValue(str(data.title))
167        self._data = data
168        self._calculator.set_data(data)
169        # Reset the outputs
170        self.set_extracted_params(None, reset=True)
171        if self._manager is not None:
172            self._manager.clear_data()
173            self._manager.show_data(self._data, IQ_DATA_LABEL, reset=True)
174
175        if set_qrange:
176            lower = data.x[-1]*0.05
177            upper1 = data.x[-1] - lower*5
178            upper2 = data.x[-1]
179            self.set_qmin(lower)
180            self.set_qmax((upper1, upper2))
181            self._compute_background()
182
183    def get_data(self):
184        return self._data
185
186    def radio_changed(self, event=None):
187        """
188        Called when the "Transform type" radio button are changed
189        """
190        if event is not None:
191            self.transform_type = event.GetEventObject().GetName()
192
193    def compute_extrapolation(self, event=None):
194        """
195        Compute and plot the extrapolated data.
196        Called when Extrapolate button is pressed.
197        """
198        if not self._validate_inputs:
199            msg = "Invalid Q range entered."
200            wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
201            return
202
203        warning_msg = ""
204        if self.background < 0:
205            warning_msg += "Negative background value entered."
206        if any((self._data.y - self.background) < 0):
207            if warning_msg != "":
208                warning_msg += "\n"
209            warning_msg += "Background value results in negative Intensity values."
210        if warning_msg != "":
211            self._background_input.SetBackgroundColour('yellow')
212            wx.PostEvent(self._manager.parent, StatusEvent(status=warning_msg, info='warning'))
213        else:
214            self._background_input.SetBackgroundColour(wx.WHITE)
215        self._background_input.Refresh()
216
217        self._calculator.set_data(self._data)
218        self._calculator.lowerq = self.qmin
219        self._calculator.upperq = self.qmax
220        self._calculator.background = self.background
221
222        try:
223            params, self._extrapolated_data, self._extrapolated_fn = \
224                self._calculator.compute_extrapolation()
225        except Exception as e:
226            msg = "Error extrapolating data:\n"
227            msg += str(e)
228            wx.PostEvent(self._manager.parent,
229                StatusEvent(status=msg, info="error"))
230            self._transform_btn.Disable()
231            return
232        self._manager.show_data(self._extrapolated_data, IQ_EXTRAPOLATED_DATA_LABEL)
233        # Update state of the GUI
234        self._transform_btn.Enable()
235        self._extract_btn.Disable()
236        self.set_extracted_params(reset=True)
237        self.set_extrapolation_params(params)
238
239    def compute_transform(self, event=None):
240        """
241        Compute and plot the transformed data.
242        Called when Transform button is pressed.
243        """
244        if not self._calculator.transform_isrunning():
245            self._calculator.compute_transform(self._extrapolated_data,
246                self.transform_type, background=self.background,
247                extrap_fn=self._extrapolated_fn,
248                completefn=self.transform_complete,
249                updatefn=self.transform_update)
250
251            self._transform_btn.SetLabel("Stop Transform")
252        else:
253            self._calculator.stop_transform()
254            self.transform_update("Transform cancelled.")
255            self._transform_btn.SetLabel("Transform")
256
257    def transform_update(self, msg=""):
258        """
259        Called from FourierThread to update on status of calculation
260        """
261        wx.PostEvent(self._manager.parent,
262            StatusEvent(status=msg))
263
264    def transform_complete(self, transforms=None):
265        """
266        Called from FourierThread when calculation has completed
267        """
268        self._transform_btn.SetLabel("Transform")
269        if transforms is None:
270            msg = "Error calculating Transform."
271            if self.transform_type == 'hilbert':
272                msg = "Not yet implemented"
273            wx.PostEvent(self._manager.parent,
274                StatusEvent(status=msg, info="Error"))
275            self._extract_btn.Disable()
276            return
277        self._transformed_data = transforms
278        (transform1, transform3) = transforms
279        import numpy as np
280        plot_x = transform1.x[np.where(transform1.x <= 200)]
281        plot_y = transform1.y[np.where(transform1.x <= 200)]
282        self._manager.show_data(Data1D(plot_x, plot_y), TRANSFORM_LABEL1)
283        plot_x = transform3.x[np.where(transform3.x <= 200)]
284        plot_y = transform3.y[np.where(transform3.x <= 200)]
285        self._manager.show_data(Data1D(plot_x, plot_y), TRANSFORM_LABEL3)
286        # Only enable extract params button if a fourier trans. has been done
287        if self.transform_type == 'fourier':
288            self._extract_btn.Enable()
289        else:
290            self._extract_btn.Disable()
291
292    def extract_parameters(self, event=None):
293        """
294        Called when "Extract Parameters" is clicked
295        """
296        try:
297            params = self._calculator.extract_parameters(self._transformed_data[0])
298        except:
299            params = None
300        if params is None:
301            msg = "Error extracting parameters."
302            wx.PostEvent(self._manager.parent,
303                StatusEvent(status=msg, info="Error"))
304            return
305        self.set_extracted_params(params)
306
307    def on_help(self, event=None):
308        """
309        Show the corfunc documentation
310        """
311        tree_location = "user/sasgui/perspectives/corfunc/corfunc_help.html"
312        doc_viewer = DocumentationWindow(self, -1, tree_location, "",
313                                          "Correlation Function Help")
314
315    def get_save_flag(self):
316        if self._data is not None:
317            return True
318        return False
319
320    def on_set_focus(self, event=None):
321        if self._manager.parent is not None:
322            wx.PostEvent(self._manager.parent, PanelOnFocusEvent(panel=self))
323
324    def on_save(self, event=None):
325        """
326        Save corfunc state into a file
327        """
328        # Ask the user the location of the file to write to.
329        path = None
330        default_save_location = os.getcwd()
331        if self._manager.parent is not None:
332            default_save_location = self._manager.parent.get_save_location()
333
334        dlg = wx.FileDialog(self, "Choose a file",
335                            default_save_location, \
336                            self.window_caption, "*.crf", wx.SAVE)
337        if dlg.ShowModal() == wx.ID_OK:
338            path = dlg.GetPath()
339            default_save_location = os.path.dirname(path)
340            if self._manager.parent is not None:
341                self._manager.parent._default_save_location = default_save_location
342        else:
343            return None
344
345        dlg.Destroy()
346        # MAC always needs the extension for saving
347        extens = ".crf"
348        # Make sure the ext included in the file name
349        f_name = os.path.splitext(path)[0] + extens
350        self._manager.state_reader.write(f_name, self._data, self.get_state())
351
352    def save_project(self, doc=None):
353        """
354        Return an XML node containing the state of the panel
355
356        :param doc: Am xml node to attach the project state to (optional)
357        """
358        data = self._data
359        state = self.get_state()
360        if data is not None:
361            new_doc, sasentry = self._manager.state_reader._to_xml_doc(data)
362            new_doc = state.toXML(doc=new_doc, entry_node=sasentry)
363            if new_doc is not None:
364                if doc is not None and hasattr(doc, "firstChild"):
365                    child = new_doc.getElementsByTagName("SASentry")
366                    for item in child:
367                        doc.firstChild.appendChild(item)
368                else:
369                    doc = new_doc
370        return doc
371
372    def set_qmin(self, qmin):
373        self.qmin = qmin
374        self._qmin_input.SetValue(str(qmin))
375
376    def set_qmax(self, qmax):
377        self.qmax = qmax
378        self._qmax1_input.SetValue(str(qmax[0]))
379        self._qmax2_input.SetValue(str(qmax[1]))
380
381    def set_background(self, bg):
382        self.background = bg
383        self._background_input.SetValue(str(bg))
384        self._calculator.background = bg
385
386    def set_extrapolation_params(self, params=None):
387        """
388        Displays the value of the parameters calculated in the extrapolation
389        """
390        if params is None:
391            # Reset outputs
392            for output in self._extrapolation_outputs.values():
393                output.SetValue('-')
394            return
395        for key, value in params.iteritems():
396            output = self._extrapolation_outputs[key]
397            rounded = self._round_sig_figs(value, 6)
398            output.SetValue(rounded)
399
400
401    def set_extracted_params(self, params=None, reset=False):
402        """
403        Displays the values of the parameters extracted from the Fourier
404        transform
405        """
406        self.extracted_params = params
407        error = False
408        if params is None:
409            if not reset: error = True
410            for output in self._output_boxes.values():
411                output.SetValue('-')
412        else:
413            if len(params) < len(OUTPUT_STRINGS):
414                # Not all parameters were calculated
415                error = True
416            for key, value in params.iteritems():
417                rounded = self._round_sig_figs(value, 6)
418                self._output_boxes[key].SetValue(rounded)
419        if error:
420            msg = 'Not all parameters were able to be calculated'
421            wx.PostEvent(self._manager.parent, StatusEvent(
422                status=msg, info='error'))
423
424    def plot_qrange(self, active=None, leftdown=False):
425        if active is None:
426            active = self._qmin_input
427        wx.PostEvent(self._manager.parent, PlotQrangeEvent(
428            ctrl=[self._qmin_input, self._qmax1_input, self._qmax2_input],
429            active=active, id=IQ_DATA_LABEL, is_corfunc=True,
430            group_id=GROUP_ID_IQ_DATA, leftdown=leftdown))
431
432
433    def _compute_background(self, event=None):
434        """
435        Compute the background level based on the position of the upper q bars
436        """
437        if event is not None:
438            event.Skip()
439        self._on_enter_input()
440        try:
441            bg = self._calculator.compute_background(self.qmax)
442            self.set_background(bg)
443        except Exception as e:
444            msg = "Error computing background level:\n"
445            msg += str(e)
446            wx.PostEvent(self._manager.parent,
447                StatusEvent(status=msg, info="error"))
448
449    def _on_enter_input(self, event=None):
450        """
451        Read values from input boxes and save to memory.
452        """
453        if event is not None: event.Skip()
454        if not self._validate_inputs():
455            return
456        self.qmin = float(self._qmin_input.GetValue())
457        new_qmax1 = float(self._qmax1_input.GetValue())
458        new_qmax2 = float(self._qmax2_input.GetValue())
459        self.qmax = (new_qmax1, new_qmax2)
460        self.background = float(self._background_input.GetValue())
461        self._calculator.background = self.background
462        if event is not None:
463            active_ctrl = event.GetEventObject()
464            if active_ctrl == self._background_input:
465                self._manager.show_data(self._data, IQ_DATA_LABEL,
466                    reset=False, active_ctrl=active_ctrl)
467
468    def _on_click_qrange(self, event=None):
469        if event is None:
470            return
471        event.Skip()
472        if not self._validate_inputs(): return
473        self.plot_qrange(active=event.GetEventObject(),
474            leftdown=event.LeftDown())
475
476    def _validate_inputs(self):
477        """
478        Check that the values for qmin and qmax in the input boxes are valid
479        """
480        if self._data is None:
481            return False
482        qmin_valid = check_float(self._qmin_input)
483        qmax1_valid = check_float(self._qmax1_input)
484        qmax2_valid = check_float(self._qmax2_input)
485        qmax_valid = qmax1_valid and qmax2_valid
486        background_valid = check_float(self._background_input)
487        msg = ""
488        if (qmin_valid and qmax_valid and background_valid):
489            qmin = float(self._qmin_input.GetValue())
490            qmax1 = float(self._qmax1_input.GetValue())
491            qmax2 = float(self._qmax2_input.GetValue())
492            background = float(self._background_input.GetValue())
493            if not qmin > self._data.x.min():
494                msg = "qmin must be greater than the lowest q value"
495                qmin_valid = False
496            elif qmax2 < qmax1:
497                msg = "qmax1 must be less than qmax2"
498                qmax_valid = False
499            elif qmin > qmax1:
500                msg = "qmin must be less than qmax"
501                qmin_valid = False
502            elif background > self._data.y.max():
503                msg = "background must be less than highest I"
504                background_valid = False
505        if not qmin_valid:
506            self._qmin_input.SetBackgroundColour('pink')
507        if not qmax_valid:
508            self._qmax1_input.SetBackgroundColour('pink')
509            self._qmax2_input.SetBackgroundColour('pink')
510        if not background_valid:
511            self._background_input.SetBackgroundColour('pink')
512            if msg != "":
513                wx.PostEvent(self._manager.parent, StatusEvent(status=msg))
514        if (qmin_valid and qmax_valid and background_valid):
515            self._qmin_input.SetBackgroundColour(wx.WHITE)
516            self._qmax1_input.SetBackgroundColour(wx.WHITE)
517            self._qmax2_input.SetBackgroundColour(wx.WHITE)
518            self._background_input.SetBackgroundColour(wx.WHITE)
519        self._qmin_input.Refresh()
520        self._qmax1_input.Refresh()
521        self._qmax2_input.Refresh()
522        self._background_input.Refresh()
523        return (qmin_valid and qmax_valid and background_valid)
524
525    def _do_layout(self):
526        """
527        Draw the window content
528        """
529        vbox = wx.GridBagSizer(0,0)
530
531        # I(q) data box
532        databox = wx.StaticBox(self, -1, "I(Q) Data Source")
533        databox_sizer = wx.StaticBoxSizer(databox, wx.VERTICAL)
534
535        file_sizer = wx.GridBagSizer(5, 5)
536
537        y = 0
538
539        file_name_label = wx.StaticText(self, -1, "Name:")
540        file_sizer.Add(file_name_label, (0, 0), (1, 1),
541            wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
542
543        self._data_name_box = OutputTextCtrl(self, -1,
544            size=(300,20))
545        file_sizer.Add(self._data_name_box, (0, 1), (1, 1),
546            wx.CENTER | wx.ADJUST_MINSIZE, 15)
547
548        file_sizer.AddSpacer((1, 25), pos=(0,2))
549        databox_sizer.Add(file_sizer, wx.TOP, 15)
550
551        vbox.Add(databox_sizer, (y, 0), (1, 1),
552            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE | wx.TOP, 15)
553        y += 1
554
555        # Parameters
556        qbox = wx.StaticBox(self, -1, "Input Parameters")
557        qbox_sizer = wx.StaticBoxSizer(qbox, wx.VERTICAL)
558        qbox_sizer.SetMinSize((_STATICBOX_WIDTH, 75))
559
560        q_sizer = wx.GridBagSizer(5, 5)
561
562        # Explanation
563        explanation_txt = ("Corfunc will use all values in the lower range for"
564            " Guinier back extrapolation, and all values in the upper range "
565            "for Porod forward extrapolation.")
566        explanation_label = wx.StaticText(self, -1, explanation_txt,
567            size=(_STATICBOX_WIDTH, 60))
568
569        q_sizer.Add(explanation_label, (0,0), (1,4), wx.LEFT | wx.EXPAND, 5)
570
571        qrange_label = wx.StaticText(self, -1, "Q Range:", size=(50,20))
572        q_sizer.Add(qrange_label, (1,0), (1,1), wx.LEFT | wx.EXPAND, 5)
573
574        # Lower Q Range
575        qmin_label = wx.StaticText(self, -1, "Lower:", size=(50,20))
576        qmin_dash_label = wx.StaticText(self, -1, "-", size=(10,20),
577            style=wx.ALIGN_CENTER_HORIZONTAL)
578
579        qmin_lower = OutputTextCtrl(self, -1, size=(75, 20), value="0.0")
580        self._qmin_input = ModelTextCtrl(self, -1, size=(75, 20),
581                        style=wx.TE_PROCESS_ENTER, name='qmin_input',
582                        text_enter_callback=self._on_enter_input)
583        self._qmin_input.SetToolTipString(("Values with q < qmin will be used "
584            "for Guinier back extrapolation"))
585
586        q_sizer.Add(qmin_label, (2, 0), (1, 1), wx.LEFT | wx.EXPAND, 5)
587        q_sizer.Add(qmin_lower, (2, 1), (1, 1), wx.LEFT, 5)
588        q_sizer.Add(qmin_dash_label, (2, 2), (1, 1), wx.CENTER | wx.EXPAND, 5)
589        q_sizer.Add(self._qmin_input, (2, 3), (1, 1), wx.LEFT, 5)
590
591        # Upper Q range
592        qmax_tooltip = ("Values with qmax1 < q < qmax2 will be used for Porod"
593            " forward extrapolation")
594
595        qmax_label = wx.StaticText(self, -1, "Upper:", size=(50,20))
596        qmax_dash_label = wx.StaticText(self, -1, "-", size=(10,20),
597            style=wx.ALIGN_CENTER_HORIZONTAL)
598
599        self._qmax1_input = ModelTextCtrl(self, -1, size=(75, 20),
600            style=wx.TE_PROCESS_ENTER, name="qmax1_input",
601            text_enter_callback=self._on_enter_input)
602        self._qmax1_input.SetToolTipString(qmax_tooltip)
603        self._qmax2_input = ModelTextCtrl(self, -1, size=(75, 20),
604            style=wx.TE_PROCESS_ENTER, name="qmax2_input",
605            text_enter_callback=self._on_enter_input)
606        self._qmax2_input.SetToolTipString(qmax_tooltip)
607
608        q_sizer.Add(qmax_label, (3, 0), (1, 1), wx.LEFT | wx.EXPAND, 5)
609        q_sizer.Add(self._qmax1_input, (3, 1), (1, 1), wx.LEFT, 5)
610        q_sizer.Add(qmax_dash_label, (3, 2), (1, 1), wx.CENTER | wx.EXPAND, 5)
611        q_sizer.Add(self._qmax2_input, (3,3), (1, 1), wx.LEFT, 5)
612
613        qbox_sizer.Add(q_sizer, wx.TOP, 0)
614
615        vbox.Add(qbox_sizer, (y, 0), (1, 1),
616            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
617        y += 1
618
619        extrapolation_box = wx.StaticBox(self, -1, "Extrapolation Parameters")
620        extrapolation_sizer = wx.StaticBoxSizer(extrapolation_box, wx.VERTICAL)
621        params_sizer = wx.GridBagSizer(5, 5)
622
623        guinier_label = wx.StaticText(self, -1, "Guinier:")
624        params_sizer.Add(guinier_label, (0, 0), (1,1),
625            wx.ALL | wx.EXPAND | wx.ADJUST_MINSIZE, 5)
626
627        a_label = wx.StaticText(self, -1, "A: ")
628        params_sizer.Add(a_label, (1, 0), (1, 1), wx.LEFT | wx.EXPAND, 15)
629
630        a_output = OutputTextCtrl(self, wx.NewId(),
631            value="-", style=wx.ALIGN_CENTER_HORIZONTAL)
632        params_sizer.Add(a_output, (1, 1), (1, 1), wx.RIGHT | wx.EXPAND, 15)
633        self._extrapolation_outputs['A'] = a_output
634
635        b_label = wx.StaticText(self, -1, "B: ")
636        params_sizer.Add(b_label, (2, 0), (1, 1), wx.LEFT | wx.EXPAND, 15)
637
638        b_output = OutputTextCtrl(self, wx.NewId(),
639            value="-", style=wx.ALIGN_CENTER_HORIZONTAL)
640        params_sizer.Add(b_output, (2, 1), (1, 1), wx.RIGHT | wx.EXPAND, 15)
641        self._extrapolation_outputs['B'] = b_output
642
643        porod_label = wx.StaticText(self, -1, "Porod: ")
644        params_sizer.Add(porod_label, (0, 2), (1, 1),
645            wx.ALL | wx.EXPAND | wx.ADJUST_MINSIZE, 5)
646
647        k_label = wx.StaticText(self, -1, "K: ")
648        params_sizer.Add(k_label, (1, 2), (1, 1), wx.LEFT | wx.EXPAND, 15)
649
650        k_output = OutputTextCtrl(self, wx.NewId(),
651            value="-", style=wx.ALIGN_CENTER_HORIZONTAL)
652        params_sizer.Add(k_output, (1, 3), (1, 1), wx.RIGHT | wx.EXPAND, 15)
653        self._extrapolation_outputs['K'] = k_output
654
655        sigma_label = wx.StaticText(self, -1, u'\u03C3: ')
656        params_sizer.Add(sigma_label, (2, 2), (1, 1), wx.LEFT | wx.EXPAND, 15)
657
658        sigma_output = OutputTextCtrl(self, wx.NewId(),
659            value="-", style=wx.ALIGN_CENTER_HORIZONTAL)
660        params_sizer.Add(sigma_output, (2, 3), (1, 1), wx.RIGHT | wx.EXPAND, 15)
661        self._extrapolation_outputs['sigma'] = sigma_output
662
663        bg_label = wx.StaticText(self, -1, "Bg: ")
664        params_sizer.Add(bg_label, (3, 2), (1, 1), wx.LEFT | wx.EXPAND, 15)
665
666        self._background_input = ModelTextCtrl(self, -1, value="0.0",
667            style=wx.TE_PROCESS_ENTER | wx.TE_CENTRE, name='background_input',
668            text_enter_callback=self._on_enter_input)
669        self._background_input.SetToolTipString(("A background value to "
670            "subtract from all intensity values"))
671        params_sizer.Add(self._background_input, (3, 3), (1, 1), wx.RIGHT | wx.EXPAND, 15)
672
673        background_button = wx.Button(self, wx.NewId(), "Calculate Bg",
674            size=(75, -1))
675        background_button.Bind(wx.EVT_BUTTON, self._compute_background)
676        params_sizer.Add(background_button, (4,3), (1, 1), wx.EXPAND | wx.RIGHT, 15)
677
678        extrapolation_sizer.Add(params_sizer)
679        vbox.Add(extrapolation_sizer, (y, 0), (1, 1),
680            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
681        y += 1
682
683        # Transform type
684        transform_box = wx.StaticBox(self, -1, "Transform Type")
685        transform_sizer = wx.StaticBoxSizer(transform_box, wx.VERTICAL)
686
687        radio_sizer = wx.GridBagSizer(5,5)
688
689        fourier_btn = wx.RadioButton(self, -1, "Fourier", name='fourier',
690            style=wx.RB_GROUP)
691        hilbert_btn = wx.RadioButton(self, -1, "Hilbert", name='hilbert')
692
693        fourier_btn.Bind(wx.EVT_RADIOBUTTON, self.radio_changed)
694        hilbert_btn.Bind(wx.EVT_RADIOBUTTON, self.radio_changed)
695
696        radio_sizer.Add(fourier_btn, (0,0), (1,1), wx.LEFT | wx.EXPAND)
697        radio_sizer.Add(hilbert_btn, (0,1), (1,1), wx.RIGHT | wx.EXPAND)
698
699        transform_sizer.Add(radio_sizer, wx.TOP, 0)
700        vbox.Add(transform_sizer, (y, 0), (1, 1),
701            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
702        y += 1
703
704        # Output data
705        outputbox = wx.StaticBox(self, -1, "Output Parameters")
706        outputbox_sizer = wx.StaticBoxSizer(outputbox, wx.VERTICAL)
707
708        output_sizer = wx.GridBagSizer(5, 5)
709
710        self._output_boxes = dict()
711        i = 0
712        for key, value in OUTPUT_STRINGS.iteritems():
713            # Create a label and a text box for each poperty
714            label = wx.StaticText(self, -1, value)
715            output_box = OutputTextCtrl(self, wx.NewId(),
716                value="-", style=wx.ALIGN_CENTER_HORIZONTAL)
717            # Save the ID of each of the text boxes for accessing after the
718            # output data has been calculated
719            self._output_boxes[key] = output_box
720            output_sizer.Add(label, (i, 0), (1, 1), wx.LEFT | wx.EXPAND, 15)
721            output_sizer.Add(output_box, (i, 2), (1, 1),
722                wx.RIGHT | wx.EXPAND, 15)
723            i += 1
724
725        outputbox_sizer.Add(output_sizer, wx.TOP, 0)
726
727        vbox.Add(outputbox_sizer, (y, 0), (1, 1),
728            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
729        y += 1
730
731        # Controls
732        controlbox = wx.StaticBox(self, -1, "Controls")
733        controlbox_sizer = wx.StaticBoxSizer(controlbox, wx.VERTICAL)
734
735        controls_sizer = wx.BoxSizer(wx.VERTICAL)
736
737        self._extrapolate_btn = wx.Button(self, wx.NewId(), "Extrapolate")
738        self._transform_btn = wx.Button(self, wx.NewId(), "Transform")
739        self._extract_btn = wx.Button(self, wx.NewId(), "Compute Parameters")
740        help_btn = wx.Button(self, -1, "HELP")
741
742        self._transform_btn.Disable()
743        self._extract_btn.Disable()
744
745        self._extrapolate_btn.Bind(wx.EVT_BUTTON, self.compute_extrapolation)
746        self._transform_btn.Bind(wx.EVT_BUTTON, self.compute_transform)
747        self._extract_btn.Bind(wx.EVT_BUTTON, self.extract_parameters)
748        help_btn.Bind(wx.EVT_BUTTON, self.on_help)
749
750        controls_sizer.Add(self._extrapolate_btn, wx.CENTER | wx.EXPAND)
751        controls_sizer.Add(self._transform_btn, wx.CENTER | wx.EXPAND)
752        controls_sizer.Add(self._extract_btn, wx.CENTER | wx.EXPAND)
753        controls_sizer.Add(help_btn, wx.CENTER | wx.EXPAND)
754
755        controlbox_sizer.Add(controls_sizer, wx.TOP | wx.EXPAND, 0)
756        vbox.Add(controlbox_sizer, (y, 0), (1, 1),
757            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
758
759
760        self.SetSizer(vbox)
761
762    def _disable_inputs(self):
763        """
764        Disable all input fields
765        """
766        self._qmin_input.Disable()
767        self._qmax1_input.Disable()
768        self._qmax2_input.Disable()
769        self._background_input.Disable()
770        self._extrapolate_btn.Disable()
771
772    def _enable_inputs(self):
773        """
774        Enable all input fields
775        """
776        self._qmin_input.Enable()
777        self._qmax1_input.Enable()
778        self._qmax2_input.Enable()
779        self._background_input.Enable()
780        self._extrapolate_btn.Enable()
781
782    def _round_sig_figs(self, x, sigfigs):
783        """
784        Round a number to a given number of significant figures.
785
786        :param x: The value to round
787        :param sigfigs: How many significant figures to round to
788        :return rounded_str: x rounded to the given number of significant
789            figures, as a string
790        """
791        rounded_str = ""
792        try:
793            # Index of first significant digit
794            significant_digit = -int(np.floor(np.log10(np.abs(x))))
795
796            if np.abs(significant_digit > 4):
797                # Use scientific notation if x > 1e5 or x < 1e4
798                rounded_str = "{1:.{0}E}".format(sigfigs-1, x)
799            else:
800                # Format as a standard decimal
801                # Number of digits required for correct number of sig figs
802                digits = significant_digit + (sigfigs - 1)
803                rounded = np.round(x, decimals=digits)
804                rounded_str = "{1:.{0}f}".format(sigfigs -1  + significant_digit,
805                    rounded)
806        except:
807            # Method for finding significant_digit fails if x is 0 (since log10(0)=inf)
808            if x == 0.0:
809                rounded_str = "0.0"
810            else:
811                rounded_str = "-"
812
813        return rounded_str
Note: See TracBrowser for help on using the repository browser.