source: sasview/src/sas/sasgui/perspectives/corfunc/corfunc_panel.py @ 711e157

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 711e157 was 711e157, checked in by lewis, 8 years ago

Disable extrapolate button if no data is loaded

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