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
Line 
1import wx
2import sys
3import numpy as np
4from wx.lib.scrolledpanel import ScrolledPanel
5from sas.sasgui.guiframe.events import PlotQrangeEvent
6from sas.sasgui.guiframe.events import StatusEvent
7from sas.sasgui.guiframe.panel_base import PanelBase
8from sas.sasgui.guiframe.utils import check_float
9from sas.sasgui.guiframe.dataFitting import Data1D
10from sas.sasgui.perspectives.invariant.invariant_widgets import OutputTextCtrl
11from sas.sasgui.perspectives.invariant.invariant_widgets import InvTextCtrl
12from sas.sasgui.perspectives.fitting.basepage import ModelTextCtrl
13from sas.sasgui.perspectives.corfunc.corfunc_state import CorfuncState
14import sas.sasgui.perspectives.corfunc.corfunc
15from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator
16from sas.sasgui.guiframe.documentation_window import DocumentationWindow
17from plot_labels import *
18
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: ",
24    'A': "Polydispersity: ",
25    'fill': "Local Crystallinity: "
26}
27
28if sys.platform.count("win32") > 0:
29    _STATICBOX_WIDTH = 350
30    PANEL_WIDTH = 400
31    PANEL_HEIGHT = 700
32    FONT_VARIANT = 0
33else:
34    _STATICBOX_WIDTH = 390
35    PANEL_WIDTH = 430
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
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._extrapolate_btn = None
63        self._transform_btn = None
64        self._extract_btn = None
65        self.qmin = 0
66        self.qmax = (0, 0)
67        self.background = 0
68        self.extracted_params = None
69        self.transform_type = 'fourier'
70        # Dictionary for saving refs to text boxes used to display output data
71        self._output_boxes = None
72        self.state = None
73        self._do_layout()
74        self._disable_inputs()
75        self.set_state()
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)
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)
82        self._background_input.Bind(wx.EVT_TEXT, self._on_enter_input)
83
84    def set_state(self, state=None, data=None):
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        """
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
98        self.set_data(data, set_qrange=False)
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))
103        if self.state.background is not None:
104            self.set_background(self.state.background)
105        if self.state.is_extrapolated:
106            self.compute_extrapolation()
107        else:
108            return
109        if self.state.is_transformed:
110            self.compute_transform()
111        else:
112            return
113        if self.state.outputs is not None and self.state.outputs != {}:
114            self.set_extracted_params(self.state.outputs, reset=True)
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])
124        state.set_saved_state('background_tcl', self.background)
125        state.outputs = self.extracted_params
126        if self._data is not None:
127            state.file = self._data.title
128            state.data = self._data
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
133        self.state = state
134
135        return self.state
136
137    def onSetFocus(self, evt):
138        if evt is not None:
139            evt.Skip()
140        self._validate_inputs()
141
142    def set_data(self, data=None, set_qrange=True):
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        """
148        if data is None:
149            return
150        self._enable_inputs()
151        self._transform_btn.Disable()
152        self._extract_btn.Disable()
153        self._data_name_box.SetValue(str(data.title))
154        self._data = data
155        self._calculator.set_data(data)
156        # Reset the outputs
157        self.set_extracted_params(None, reset=True)
158        if self._manager is not None:
159            self._manager.clear_data()
160            self._manager.show_data(self._data, IQ_DATA_LABEL, reset=True)
161
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))
168            self.set_background(self._calculator.compute_background(self.qmax))
169
170    def get_data(self):
171        return self._data
172
173    def radio_changed(self, event=None):
174        if event is not None:
175            self.transform_type = event.GetEventObject().GetName()
176
177    def compute_extrapolation(self, event=None):
178        """
179        Compute and plot the extrapolated data.
180        Called when Extrapolate button is pressed.
181        """
182        if not self._validate_inputs:
183            msg = "Invalid Q range entered."
184            wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
185            return
186        self._calculator.set_data(self._data)
187        self._calculator.lowerq = self.qmin
188        self._calculator.upperq = self.qmax
189        self._calculator.background = self.background
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
198        # TODO: Find way to set xlim and ylim so full range of data can be
199        # plotted but zoomed in
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)
207        # Update state of the GUI
208        self._transform_btn.Enable()
209        self._extract_btn.Disable()
210        self.set_extracted_params(reset=True)
211
212    def compute_transform(self, event=None):
213        """
214        Compute and plot the transformed data.
215        Called when Transform button is pressed.
216        """
217        if not self._calculator.transform_isrunning():
218            self._calculator.compute_transform(self._extrapolated_data,
219                self.transform_type, background=self.background,
220                completefn=self.transform_complete,
221                updatefn=self.transform_update)
222
223            self._transform_btn.SetLabel("Stop Tansform")
224        else:
225            self._calculator.stop_transform()
226            self.transform_update("Transform cancelled.")
227            self._transform_btn.SetLabel("Tansform")
228
229    def transform_update(self, msg=""):
230        """
231        Called from FourierThread to update on status of calculation
232        """
233        wx.PostEvent(self._manager.parent,
234            StatusEvent(status=msg))
235
236    def transform_complete(self, transform=None):
237        """
238        Called from FourierThread when calculation has completed
239        """
240        self._transform_btn.SetLabel("Tansform")
241        if transform is None:
242            msg = "Error calculating Transform."
243            if self.transform_type == 'hilbert':
244                msg = "Not yet implemented"
245            wx.PostEvent(self._manager.parent,
246                StatusEvent(status=msg, info="Error"))
247            self._extract_btn.Disable()
248            return
249        self._transformed_data = transform
250        import numpy as np
251        plot_x = transform.x[np.where(transform.x <= 200)]
252        plot_y = transform.y[np.where(transform.x <= 200)]
253        self._manager.show_data(Data1D(plot_x, plot_y), TRANSFORM_LABEL)
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()
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,
268                StatusEvent(status=msg, info="Error"))
269            return
270        self.set_extracted_params(params)
271
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
280    def save_project(self, doc=None):
281        """
282        Return an XML node containing the state of the panel
283
284        :param doc: Am xml node to attach the project state to (optional)
285        """
286        data = self._data
287        state = self.get_state()
288        if data is not None:
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
299
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
309    def set_background(self, bg):
310        self.background = bg
311        self._background_input.SetValue(str(bg))
312        self._calculator.background = bg
313
314    def set_extracted_params(self, params=None, reset=False):
315        self.extracted_params = params
316        error = False
317        if params is None:
318            if not reset: error = True
319            for key in OUTPUT_STRINGS.keys():
320                self._output_boxes[key].SetValue('-')
321        else:
322            if len(params) < len(OUTPUT_STRINGS):
323                # Not all parameters were calculated
324                error = True
325            for key, value in params.iteritems():
326                rounded = self._round_sig_figs(value, 6)
327                self._output_boxes[key].SetValue(rounded)
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
333
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
342
343    def _compute_background(self, event=None):
344        self.set_background(self._calculator.compute_background(self.qmax))
345
346    def _on_enter_input(self, event=None):
347        """
348        Read values from input boxes and save to memory.
349        """
350        if event is not None: event.Skip()
351        if not self._validate_inputs():
352            return
353        self.qmin = float(self._qmin_input.GetValue())
354        new_qmax1 = float(self._qmax1_input.GetValue())
355        new_qmax2 = float(self._qmax2_input.GetValue())
356        self.qmax = (new_qmax1, new_qmax2)
357        self.background = float(self._background_input.GetValue())
358        self._calculator.background = self.background
359        if event is not None:
360            active_ctrl = event.GetEventObject()
361            if active_ctrl == self._background_input:
362                self._manager.show_data(self._data, IQ_DATA_LABEL,
363                    reset=False, active_ctrl=active_ctrl)
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
370        self.plot_qrange(active=event.GetEventObject(),
371            leftdown=event.LeftDown())
372
373    def _validate_inputs(self):
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
383        background_valid = check_float(self._background_input)
384        msg = ""
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
399            elif background > self._data.y.max():
400                msg = "background must be less than highest I"
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):
412            self._qmin_input.SetBackgroundColour(wx.WHITE)
413            self._qmax1_input.SetBackgroundColour(wx.WHITE)
414            self._qmax2_input.SetBackgroundColour(wx.WHITE)
415            self._background_input.SetBackgroundColour(wx.WHITE)
416        self._qmin_input.Refresh()
417        self._qmax1_input.Refresh()
418        self._qmax2_input.Refresh()
419        self._background_input.Refresh()
420        return (qmin_valid and qmax_valid and background_valid)
421
422    def _do_layout(self):
423        """
424        Draw the window content
425        """
426        vbox = wx.GridBagSizer(0,0)
427
428        # I(q) data box
429        databox = wx.StaticBox(self, -1, "I(Q) Data Source")
430        databox_sizer = wx.StaticBoxSizer(databox, wx.VERTICAL)
431
432        file_sizer = wx.GridBagSizer(5, 5)
433
434        file_name_label = wx.StaticText(self, -1, "Name:")
435        file_sizer.Add(file_name_label, (0, 0), (1, 1),
436            wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
437
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
451        qbox = wx.StaticBox(self, -1, "Input Parameters")
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))
467        q_sizer.Add(qrange_label, (1,0), (1,1), wx.LEFT | wx.EXPAND, 5)
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
474        qmin_lower = OutputTextCtrl(self, -1, size=(75, 20), value="0.0")
475        self._qmin_input = ModelTextCtrl(self, -1, size=(75, 20),
476                        style=wx.TE_PROCESS_ENTER, name='qmin_input',
477                        text_enter_callback=self._on_enter_input)
478        self._qmin_input.SetToolTipString(("Values with q < qmin will be used "
479            "for Guinier back extrapolation"))
480
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)
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
494        self._qmax1_input = ModelTextCtrl(self, -1, size=(75, 20),
495            style=wx.TE_PROCESS_ENTER, name="qmax1_input",
496            text_enter_callback=self._on_enter_input)
497        self._qmax1_input.SetToolTipString(qmax_tooltip)
498        self._qmax2_input = ModelTextCtrl(self, -1, size=(75, 20),
499            style=wx.TE_PROCESS_ENTER, name="qmax2_input",
500            text_enter_callback=self._on_enter_input)
501        self._qmax2_input.SetToolTipString(qmax_tooltip)
502
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
511        self._background_input = ModelTextCtrl(self, -1, size=(75,20),
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)
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
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
549        # Output data
550        outputbox = wx.StaticBox(self, -1, "Output Parameters")
551        outputbox_sizer = wx.StaticBoxSizer(outputbox, wx.VERTICAL)
552
553        output_sizer = wx.GridBagSizer(5, 5)
554
555        self._output_boxes = dict()
556        i = 0
557        for key, value in OUTPUT_STRINGS.iteritems():
558            # Create a label and a text box for each poperty
559            label = wx.StaticText(self, -1, value)
560            output_box = OutputTextCtrl(self, wx.NewId(),
561                value="-", style=wx.ALIGN_CENTER_HORIZONTAL)
562            # Save the ID of each of the text boxes for accessing after the
563            # output data has been calculated
564            self._output_boxes[key] = output_box
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)
568            i += 1
569
570        outputbox_sizer.Add(output_sizer, wx.TOP, 0)
571
572        vbox.Add(outputbox_sizer, (3, 0), (1, 1),
573            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
574
575        # Controls
576        controlbox = wx.StaticBox(self, -1, "Controls")
577        controlbox_sizer = wx.StaticBoxSizer(controlbox, wx.VERTICAL)
578
579        controls_sizer = wx.BoxSizer(wx.VERTICAL)
580
581        self._extrapolate_btn = wx.Button(self, wx.NewId(), "Extrapolate")
582        self._transform_btn = wx.Button(self, wx.NewId(), "Transform")
583        self._extract_btn = wx.Button(self, wx.NewId(), "Compute Parameters")
584        help_btn = wx.Button(self, -1, "HELP")
585
586        self._transform_btn.Disable()
587        self._extract_btn.Disable()
588
589        self._extrapolate_btn.Bind(wx.EVT_BUTTON, self.compute_extrapolation)
590        self._transform_btn.Bind(wx.EVT_BUTTON, self.compute_transform)
591        self._extract_btn.Bind(wx.EVT_BUTTON, self.extract_parameters)
592        help_btn.Bind(wx.EVT_BUTTON, self.on_help)
593
594        controls_sizer.Add(self._extrapolate_btn, wx.CENTER | wx.EXPAND)
595        controls_sizer.Add(self._transform_btn, wx.CENTER | wx.EXPAND)
596        controls_sizer.Add(self._extract_btn, wx.CENTER | wx.EXPAND)
597        controls_sizer.Add(help_btn, wx.CENTER | wx.EXPAND)
598
599        controlbox_sizer.Add(controls_sizer, wx.TOP | wx.EXPAND, 0)
600        vbox.Add(controlbox_sizer, (4, 0), (1, 1),
601            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
602
603
604        self.SetSizer(vbox)
605
606    def _disable_inputs(self):
607        """
608        Disable all input fields
609        """
610        self._qmin_input.Disable()
611        self._qmax1_input.Disable()
612        self._qmax2_input.Disable()
613        self._background_input.Disable()
614        self._extrapolate_btn.Disable()
615
616    def _enable_inputs(self):
617        """
618        Enable all input fields
619        """
620        self._qmin_input.Enable()
621        self._qmax1_input.Enable()
622        self._qmax2_input.Enable()
623        self._background_input.Enable()
624        self._extrapolate_btn.Enable()
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.