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

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 e73e723 was e73e723, checked in by lewis, 8 years ago

Move string conversion from calculator class to panel

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