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

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

Refactor in preparation for 3D transform

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