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

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

Allow inputting of qmin and qmax, and plot them on the graph as vertical lines

  • Property mode set to 100644
File size: 11.6 KB
Line 
1import wx
2import sys
3from wx.lib.scrolledpanel import ScrolledPanel
4from sas.sasgui.guiframe.events import PlotQrangeEvent
5from sas.sasgui.guiframe.events import StatusEvent
6from sas.sasgui.guiframe.panel_base import PanelBase
7from sas.sasgui.guiframe.utils import check_float
8from sas.sasgui.perspectives.invariant.invariant_widgets import OutputTextCtrl
9from sas.sasgui.perspectives.invariant.invariant_widgets import InvTextCtrl
10from sas.sasgui.perspectives.fitting.basepage import ModelTextCtrl
11
12if sys.platform.count("win32") > 0:
13    _STATICBOX_WIDTH = 350
14    PANEL_WIDTH = 400
15    PANEL_HEIGHT = 700
16    FONT_VARIANT = 0
17else:
18    _STATICBOX_WIDTH = 390
19    PANEL_WIDTH = 430
20    PANEL_HEIGHT = 700
21    FONT_VARIANT = 1
22
23class CorfuncPanel(ScrolledPanel,PanelBase):
24    window_name = "Correlation Function"
25    window_caption = "Correlation Function"
26    CENTER_PANE = True
27
28    def __init__(self, parent, data=None, manager=None, *args, **kwds):
29        kwds["size"] = (PANEL_WIDTH, PANEL_HEIGHT)
30        kwds["style"] = wx.FULL_REPAINT_ON_RESIZE
31        ScrolledPanel.__init__(self, parent=parent, *args, **kwds)
32        PanelBase.__init__(self, parent)
33        self.SetupScrolling()
34        self.SetWindowVariant(variant=FONT_VARIANT)
35        self._manager = manager
36        self._data = data # The data to be analysed
37        self._data_name_box = None # Text box to show name of file
38        self._qmin_input = None
39        self._qmax1_input = None
40        self._qmax2_input = None
41        self._qmin = 0
42        self._qmax = (0, 0)
43        # Dictionary for saving IDs of text boxes used to display output data
44        self._output_ids = None
45        self.state = None
46        self.set_state()
47        self._do_layout()
48        self._qmin_input.Bind(wx.EVT_TEXT, self._on_enter_qrange)
49        self._qmax1_input.Bind(wx.EVT_TEXT, self._on_enter_qrange)
50        self._qmax2_input.Bind(wx.EVT_TEXT, self._on_enter_qrange)
51
52    def set_state(self, state=None, data=None):
53        # TODO: Implement state restoration
54        return False
55
56    def onSetFocus(self, evt):
57        if evt is not None:
58            evt.Skip()
59        self._validate_qrange()
60
61    def _set_data(self, data=None):
62        """
63        Update the GUI to reflect new data that has been loaded in
64
65        :param data: The data that has been loaded
66        """
67        self._data_name_box.SetValue(str(data.name))
68        self._data = data
69        if self._manager is not None:
70            self._manager.show_data(data=data, reset=True)
71        lower = data.x[-1]*0.05
72        upper1 = data.x[-1] - lower*5
73        upper2 = data.x[-1]
74        self.set_qmin(lower)
75        self.set_qmax((upper1, upper2))
76
77
78    def _on_enter_qrange(self, event):
79        """
80        Read values from input boxes and save to memory.
81        """
82        event.Skip()
83        if not self._validate_qrange():
84            return
85        new_qmin = float(self._qmin_input.GetValue())
86        new_qmax1 = float(self._qmax1_input.GetValue())
87        new_qmax2 = float(self._qmax2_input.GetValue())
88        self.qmin = new_qmin
89        self.qmax = (new_qmax1, new_qmax2)
90        data_id = self._manager.data_id
91        from sas.sasgui.perspectives.corfunc.corfunc import GROUP_ID_IQ_DATA
92        group_id = GROUP_ID_IQ_DATA
93        wx.PostEvent(self._manager.parent, PlotQrangeEvent(
94            ctrl=[self._qmin_input, self._qmax1_input, self._qmax2_input],
95            active=event.GetEventObject(), id=data_id, group_id=group_id,
96            leftdown=False))
97
98    def set_qmin(self, qmin):
99        self.qmin = qmin
100        self._qmin_input.SetValue(str(qmin))
101
102    def set_qmax(self, qmax):
103        self.qmax = qmax
104        self._qmax1_input.SetValue(str(qmax[0]))
105        self._qmax2_input.SetValue(str(qmax[1]))
106
107    def _validate_qrange(self):
108        """
109        Check that the values for qmin and qmax in the input boxes are valid
110        """
111        if self._data is None:
112            return False
113        qmin_valid = check_float(self._qmin_input)
114        qmax1_valid = check_float(self._qmax1_input)
115        qmax2_valid = check_float(self._qmax2_input)
116        qmax_valid = qmax1_valid and qmax2_valid
117        if not (qmin_valid and qmax_valid):
118            if not qmin_valid:
119                self._qmin_input.SetBackgroundColour('pink')
120                self._qmin_input.Refresh()
121            if not qmax1_valid:
122                self._qmax1_input.SetBackgroundColour('pink')
123                self._qmax1_input.Refresh()
124            if not qmax2_valid:
125                self._qmax2_input.SetBackgroundColour('pink')
126                self._qmax2_input.Refresh()
127            return False
128        qmin = float(self._qmin_input.GetValue())
129        qmax1 = float(self._qmax1_input.GetValue())
130        qmax2 = float(self._qmax2_input.GetValue())
131        msg = ""
132        if not qmin > self._data.x.min():
133            msg = "qmin must be greater than the lowest q value"
134            qmin_valid = False
135        elif qmax2 < qmax1:
136            "qmax1 must be less than qmax2"
137            qmax_valid = False
138        elif qmin > qmax1:
139            "qmin must be less than qmax"
140            qmin_valid = False
141        # import pdb; pdb.set_trace()
142        if not (qmin_valid and qmax_valid):
143            if not qmin_valid:
144                self._qmin_input.SetBackgroundColour('pink')
145            if not qmax_valid:
146                self._qmax1_input.SetBackgroundColour('pink')
147                self._qmax2_input.SetBackgroundColour('pink')
148            wx.PostEvent(self._manager.parent, StatusEvent(status=msg))
149        else:
150            self._qmin_input.SetBackgroundColour(wx.WHITE)
151            self._qmax1_input.SetBackgroundColour(wx.WHITE)
152            self._qmax2_input.SetBackgroundColour(wx.WHITE)
153        self._qmin_input.Refresh()
154        self._qmax1_input.Refresh()
155        self._qmax2_input.Refresh()
156        return (qmin_valid and qmax_valid)
157
158    def _do_layout(self):
159        """
160        Draw the window content
161        """
162        vbox = wx.GridBagSizer(0,0)
163
164        # I(q) data box
165        databox = wx.StaticBox(self, -1, "I(Q) Data Source")
166        databox_sizer = wx.StaticBoxSizer(databox, wx.VERTICAL)
167
168        file_sizer = wx.GridBagSizer(5, 5)
169
170        file_name_label = wx.StaticText(self, -1, "Name:")
171        file_sizer.Add(file_name_label, (0, 0), (1, 1),
172            wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
173
174        self._data_name_box = OutputTextCtrl(self, -1,
175            size=(300,20))
176        file_sizer.Add(self._data_name_box, (0, 1), (1, 1),
177            wx.CENTER | wx.ADJUST_MINSIZE, 15)
178
179        file_sizer.AddSpacer((1, 25), pos=(0,2))
180        databox_sizer.Add(file_sizer, wx.TOP, 15)
181
182        vbox.Add(databox_sizer, (0, 0), (1, 1),
183            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE | wx.TOP, 15)
184
185
186        # Parameters
187        qbox = wx.StaticBox(self, -1, "Parameters")
188        qbox_sizer = wx.StaticBoxSizer(qbox, wx.VERTICAL)
189        qbox_sizer.SetMinSize((_STATICBOX_WIDTH, 75))
190
191        q_sizer = wx.GridBagSizer(5, 5)
192
193        # Explanation
194        explanation_txt = ("Corfunc will use all values in the lower range for"
195            " Guinier back extrapolation, and all values in the upper range "
196            "for Porod forward extrapolation.")
197        explanation_label = wx.StaticText(self, -1, explanation_txt,
198            size=(_STATICBOX_WIDTH, 60))
199
200        q_sizer.Add(explanation_label, (0,0), (1,4), wx.LEFT | wx.EXPAND, 5)
201
202        qrange_label = wx.StaticText(self, -1, "Q Range:", size=(50,20))
203        q_sizer.Add(qrange_label, (1,0), (1,1), wx.LEFT | wx.EXPAND, 5)
204
205        # Lower Q Range
206        qmin_label = wx.StaticText(self, -1, "Lower:", size=(50,20))
207        qmin_dash_label = wx.StaticText(self, -1, "-", size=(10,20),
208            style=wx.ALIGN_CENTER_HORIZONTAL)
209
210        qmin_lower = OutputTextCtrl(self, -1, size=(50, 20), value="0.0")
211        self._qmin_input = ModelTextCtrl(self, -1, size=(50, 20),
212                        style=wx.TE_PROCESS_ENTER, name='qmin_input',
213                        text_enter_callback=self._on_enter_qrange)
214        self._qmin_input.SetToolTipString(("Values with q < qmin will be used "
215            "for Guinier back extrapolation"))
216
217        q_sizer.Add(qmin_label, (2, 0), (1, 1), wx.LEFT | wx.EXPAND, 5)
218        q_sizer.Add(qmin_lower, (2, 1), (1, 1), wx.LEFT, 5)
219        q_sizer.Add(qmin_dash_label, (2, 2), (1, 1), wx.CENTER | wx.EXPAND, 5)
220        q_sizer.Add(self._qmin_input, (2, 3), (1, 1), wx.LEFT, 5)
221
222        # Upper Q range
223        qmax_tooltip = ("Values with qmax1 < q < qmax2 will be used for Porod"
224            " forward extrapolation")
225
226        qmax_label = wx.StaticText(self, -1, "Upper:", size=(50,20))
227        qmax_dash_label = wx.StaticText(self, -1, "-", size=(10,20),
228            style=wx.ALIGN_CENTER_HORIZONTAL)
229
230        self._qmax1_input = ModelTextCtrl(self, -1, size=(50, 20),
231            style=wx.TE_PROCESS_ENTER, name="qmax1_input",
232            text_enter_callback=self._on_enter_qrange)
233        self._qmax1_input.SetToolTipString(qmax_tooltip)
234        self._qmax2_input = ModelTextCtrl(self, -1, size=(50, 20),
235            style=wx.TE_PROCESS_ENTER, name="qmax2_input",
236            text_enter_callback=self._on_enter_qrange)
237        self._qmax2_input.SetToolTipString(qmax_tooltip)
238
239        q_sizer.Add(qmax_label, (3, 0), (1, 1), wx.LEFT | wx.EXPAND, 5)
240        q_sizer.Add(self._qmax1_input, (3, 1), (1, 1), wx.LEFT, 5)
241        q_sizer.Add(qmax_dash_label, (3, 2), (1, 1), wx.CENTER | wx.EXPAND, 5)
242        q_sizer.Add(self._qmax2_input, (3,3), (1, 1), wx.LEFT, 5)
243
244        qbox_sizer.Add(q_sizer, wx.TOP, 0)
245
246        vbox.Add(qbox_sizer, (1, 0), (1, 1),
247            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
248
249        # Output data
250        outputbox = wx.StaticBox(self, -1, "Output Measuments")
251        outputbox_sizer = wx.StaticBoxSizer(outputbox, wx.VERTICAL)
252
253        output_sizer = wx.GridBagSizer(5, 5)
254
255        label_strings = [
256            "Long Period (A): ",
257            "Average Hard Block Thickness (A): ",
258            "Average Interface Thickness (A): ",
259            "Average Core Thickness: ",
260            "PolyDispersity: ",
261            "Filling Fraction: "
262        ]
263        self._output_ids = dict()
264        for i in range(len(label_strings)):
265            # Create a label and a text box for each poperty
266            label = wx.StaticText(self, -1, label_strings[i])
267            output_box = OutputTextCtrl(self, wx.NewId(), size=(50, 20),
268                value="-", style=wx.ALIGN_CENTER_HORIZONTAL)
269            # Save the ID of each of the text boxes for accessing after the
270            # output data has been calculated
271            self._output_ids[label_strings[i]] = output_box.GetId()
272            output_sizer.Add(label, (i, 0), (1, 1), wx.LEFT | wx.EXPAND, 15)
273            output_sizer.Add(output_box, (i, 2), (1, 1),
274                wx.RIGHT | wx.EXPAND, 15)
275
276        outputbox_sizer.Add(output_sizer, wx.TOP, 0)
277
278        vbox.Add(outputbox_sizer, (2, 0), (1, 1),
279            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
280
281        # Controls
282        controlbox = wx.StaticBox(self, -1, "Controls")
283        controlbox_sizer = wx.StaticBoxSizer(controlbox, wx.VERTICAL)
284
285        controls_sizer = wx.BoxSizer(wx.VERTICAL)
286
287        extrapolate_btn = wx.Button(self, wx.NewId(), "Extrapolate")
288        transform_btn = wx.Button(self, wx.NewId(), "Transform")
289        compute_btn = wx.Button(self, wx.NewId(), "Compute Measuments")
290
291        controls_sizer.Add(extrapolate_btn, wx.CENTER | wx.EXPAND)
292        controls_sizer.Add(transform_btn, wx.CENTER | wx.EXPAND)
293        controls_sizer.Add(compute_btn, wx.CENTER | wx.EXPAND)
294
295        controlbox_sizer.Add(controls_sizer, wx.TOP | wx.EXPAND, 0)
296        vbox.Add(controlbox_sizer, (3, 0), (1, 1),
297            wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
298
299        self.SetSizer(vbox)
Note: See TracBrowser for help on using the repository browser.