source: sasview/src/sas/perspectives/pr/explore_dialog.py @ 27f8dac

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.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 27f8dac was 79492222, checked in by krzywon, 10 years ago

Changed the file and folder names to remove all SANS references.

  • Property mode set to 100644
File size: 16.8 KB
Line 
1
2################################################################################
3#This software was developed by the University of Tennessee as part of the
4#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
5#project funded by the US National Science Foundation.
6#
7#See the license text in license.txt
8#
9#copyright 2009, University of Tennessee
10################################################################################
11
12"""
13Dialog panel to explore the P(r) inversion results for a range
14of D_max value. User picks a number of points and a range of
15distances, then can toggle between inversion outputs and see
16their distribution as a function of D_max.
17"""
18   
19
20import wx
21import numpy
22import math
23import logging
24import sys
25
26# Avoid Matplotlib complaining about the lack of legend on the plot
27import warnings
28warnings.simplefilter("ignore")
29
30# Import plotting classes
31from sas.plottools.PlotPanel import PlotPanel
32from sas.plottools import Data1D as Model1D
33from sas.guiframe.gui_style import GUIFRAME_ID
34from sas.plottools.plottables import Graph
35
36from pr_widgets import PrTextCtrl
37
38# Default number of points on the output plot
39DEFAULT_NPTS = 10
40# Default output parameter to plot
41DEFAULT_OUTPUT = 'Chi2/dof'
42
43class OutputPlot(PlotPanel):
44    """
45    Plot panel used to show the selected results as a function
46    of D_max
47    """
48    def __init__(self, d_min, d_max, parent, id = -1, color = None,\
49        dpi = None, style = wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs):
50        """
51        Initialization. The parameters added to PlotPanel are:
52       
53        :param d_min: Minimum value of D_max to explore
54        :param d_max: Maximum value of D_max to explore
55       
56        """
57        PlotPanel.__init__(self, parent, id = id, style = style, **kwargs)
58
59        self.parent = parent
60        self.min = d_min
61        self.max = d_max
62        self.npts = DEFAULT_NPTS
63
64        step = (self.max-self.min)/(self.npts-1)
65        self.x = numpy.arange(self.min, self.max+step*0.01, step)
66        dx = numpy.zeros(len(self.x))
67        y = numpy.ones(len(self.x))
68        dy = numpy.zeros(len(self.x))
69 
70        # Plot area
71        self.plot = Model1D(self.x, y=y, dy=dy)
72        self.plot.name = DEFAULT_OUTPUT
73        self.plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM
74
75        # Graph       
76        self.graph = Graph()
77        self.graph.xaxis("\\rm{D_{max}}", 'A')
78        self.graph.yaxis("\\rm{%s}" % DEFAULT_OUTPUT,"")
79        self.graph.add(self.plot)
80        self.graph.render(self)
81       
82        self.toolbar.DeleteToolByPos(0)
83        self.toolbar.DeleteToolByPos(8)
84        self.toolbar.Realize()
85
86    def onContextMenu(self, event):
87        """
88        Default context menu for the plot panel
89       
90        :TODO: Would be nice to add printing and log/linear scales.
91            The current verison of plottools no longer plays well with
92            plots outside of guiframe. Guiframe team needs to fix this.
93        """
94        # Slicer plot popup menu
95        id = wx.NewId()
96        slicerpop = wx.Menu()
97        slicerpop.Append(id, '&Save image', 'Save image as PNG')
98        wx.EVT_MENU(self, id, self.onSaveImage)
99       
100        id = wx.NewId()
101        slicerpop.AppendSeparator()
102        slicerpop.Append(id, '&Reset Graph')
103        wx.EVT_MENU(self, id, self.onResetGraph)
104       
105        pos = event.GetPosition()
106        pos = self.ScreenToClient(pos)
107        self.PopupMenu(slicerpop, pos)
108
109class Results:
110    """
111    Class to hold the inversion output parameters
112    as a function of D_max
113    """
114    def __init__(self):
115        """
116        Initialization. Create empty arrays
117        and dictionary of labels.
118        """
119        # Array of output for each inversion
120        self.chi2 = []
121        self.osc = []
122        self.pos = []
123        self.pos_err = []
124        self.rg = []
125        self.iq0 = []
126        self.bck = []
127        self.d_max = []
128       
129        # Dictionary of outputs
130        self.outputs = {}
131        self.outputs['Chi2/dof'] = ["\chi^2/dof", "a.u.", self.chi2]   
132        self.outputs['Oscillation parameter'] = ["Osc", "a.u.", self.osc]   
133        self.outputs['Positive fraction'] = ["P^+", "a.u.", self.pos]   
134        self.outputs['1-sigma positive fraction'] = ["P^+_{1\ \sigma}", 
135                                                     "a.u.", self.pos_err]   
136        self.outputs['Rg'] = ["R_g", "A", self.rg]   
137        self.outputs['I(q=0)'] = ["I(q=0)", "1/A", self.iq0]   
138        self.outputs['Background'] = ["Bck", "1/A", self.bck] 
139       
140class ExploreDialog(wx.Dialog):
141    """
142    The explorer dialog box. This dialog is meant to be
143    invoked by the InversionControl class.
144    """
145   
146    def __init__(self, pr_state, nfunc, *args, **kwds):
147        """
148        Initialization. The parameters added to Dialog are:
149       
150        :param pr_state: sas.pr.invertor.Invertor object
151        :param nfunc: Number of terms in the expansion
152       
153        """
154        kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE
155        wx.Dialog.__init__(self, *args, **kwds)
156       
157        # Initialize Results object
158        self.results = Results()
159       
160        self.pr_state  = pr_state
161        self._default_min = 0.9*self.pr_state.d_max
162        self._default_max = 1.1*self.pr_state.d_max
163        self.nfunc     = nfunc
164       
165        # Control for number of points
166        self.npts_ctl = PrTextCtrl(self, -1, style=wx.TE_PROCESS_ENTER,
167                                   size=(60,20))
168        # Control for the minimum value of D_max
169        self.dmin_ctl = PrTextCtrl(self, -1, style=wx.TE_PROCESS_ENTER,
170                                   size=(60,20))
171        # Control for the maximum value of D_max
172        self.dmax_ctl = PrTextCtrl(self, -1, style=wx.TE_PROCESS_ENTER,
173                                   size=(60,20))
174
175        # Output selection box for the y axis
176        self.output_box = None
177
178        # Create the plot object
179        self.plotpanel = OutputPlot(self._default_min, self._default_max,
180                                    self, -1, style=wx.RAISED_BORDER)
181       
182        # Create the layout of the dialog
183        self.__do_layout()
184        self.Fit()
185       
186        # Calculate exploration results
187        self._recalc()
188        # Graph the default output curve
189        self._plot_output()
190       
191    class Event:
192        """
193        Class that holds the content of the form
194        """
195        ## Number of points to be plotted
196        npts   = 0
197        ## Minimum value of D_max
198        dmin   = 0
199        ## Maximum value of D_max
200        dmax   = 0
201               
202    def _get_values(self, event=None):
203        """
204        Invoked when the user changes a value of the form.
205        Check that the values are of the right type.
206       
207        :return: ExploreDialog.Event object if the content is good,
208            None otherwise
209        """
210        # Flag to make sure that all values are good
211        flag = True
212       
213        # Empty ExploreDialog.Event content
214        content_event = self.Event()
215       
216        # Read each text control and make sure the type is valid
217        # Let the user know if a type is invalid by changing the
218        # background color of the control.
219        try:
220            content_event.npts = int(self.npts_ctl.GetValue())
221            self.npts_ctl.SetBackgroundColour(wx.WHITE)
222            self.npts_ctl.Refresh()
223        except:
224            flag = False
225            self.npts_ctl.SetBackgroundColour("pink")
226            self.npts_ctl.Refresh()
227           
228        try:
229            content_event.dmin = float(self.dmin_ctl.GetValue())
230            self.dmin_ctl.SetBackgroundColour(wx.WHITE)
231            self.dmin_ctl.Refresh()
232        except:
233            flag = False
234            self.dmin_ctl.SetBackgroundColour("pink")
235            self.dmin_ctl.Refresh()
236       
237        try:
238            content_event.dmax = float(self.dmax_ctl.GetValue())
239            self.dmax_ctl.SetBackgroundColour(wx.WHITE)
240            self.dmax_ctl.Refresh()
241        except:
242            flag = False
243            self.dmax_ctl.SetBackgroundColour("pink")
244            self.dmax_ctl.Refresh()
245       
246        # If the content of the form is valid, return the content,
247        # otherwise return None
248        if flag:
249            if event is not None:
250                event.Skip(True)
251            return content_event
252        else:
253            return None
254   
255    def _plot_output(self, event=None):
256        """
257        Invoked when a new output type is selected for plotting,
258        or when a new computation is finished.
259        """
260        # Get the output type selection
261        output_type = self.output_box.GetString(self.output_box.GetSelection())
262       
263        # If the selected output type is part of the results ojbect,
264        # display the results.
265        # Note: by design, the output type should always be part of the
266        #       results object.
267        if self.results.outputs.has_key(output_type): 
268            self.plotpanel.plot.x = self.results.d_max
269            self.plotpanel.plot.y = self.results.outputs[output_type][2]
270            self.plotpanel.plot.name = '_nolegend_'
271            y_label = "\\rm{%s}" % self.results.outputs[output_type][0]
272            self.plotpanel.graph.yaxis(y_label,
273                                       self.results.outputs[output_type][1])
274           
275            # Redraw
276            self.plotpanel.graph.render(self.plotpanel)
277            self.plotpanel.subplot.figure.canvas.draw_idle()
278        else:
279            msg =  "ExploreDialog: the Results object's dictionary "
280            msg += "does not contain "
281            msg += "the [%s] output type. This must be indicative of "
282            msg += "a change in the " % str(output_type)
283            msg += "ExploreDialog code."
284            logging.error(msg)
285       
286    def __do_layout(self):
287        """
288        Do the layout of the dialog
289        """
290        # Dialog box properties
291        self.SetTitle("D_max Explorer")
292        self.SetSize((600, 595))
293       
294        sizer_main = wx.BoxSizer(wx.VERTICAL)
295        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
296        sizer_params = wx.GridBagSizer(5, 5)
297
298        iy = 0
299        ix = 0
300        label_npts  = wx.StaticText(self, -1, "Npts")
301        sizer_params.Add(label_npts, (iy, ix), (1, 1), 
302                         wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
303        ix += 1
304        sizer_params.Add(self.npts_ctl,   (iy, ix), (1, 1), 
305                         wx.EXPAND|wx.ADJUST_MINSIZE, 0)
306        self.npts_ctl.SetValue("%g" % DEFAULT_NPTS)
307        self.npts_ctl.Bind(wx.EVT_KILL_FOCUS, self._recalc)
308       
309        ix += 1
310        label_dmin   = wx.StaticText(self, -1, "Min Distance [A]")
311        sizer_params.Add(label_dmin, (iy, ix), (1, 1),
312                         wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
313        ix += 1
314        sizer_params.Add(self.dmin_ctl, (iy, ix), (1, 1),
315                         wx.EXPAND|wx.ADJUST_MINSIZE, 0)
316        self.dmin_ctl.SetValue(str(self._default_min))
317        self.dmin_ctl.Bind(wx.EVT_KILL_FOCUS, self._recalc)
318       
319        ix += 1
320        label_dmax = wx.StaticText(self, -1, "Max Distance [A]")
321        sizer_params.Add(label_dmax, (iy, ix), (1, 1),
322                         wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
323        ix += 1
324        sizer_params.Add(self.dmax_ctl,   (iy, ix), (1, 1),
325                         wx.EXPAND|wx.ADJUST_MINSIZE, 0)
326        self.dmax_ctl.SetValue(str(self._default_max))
327        self.dmax_ctl.Bind(wx.EVT_KILL_FOCUS, self._recalc)
328
329
330        # Ouput selection box
331        selection_msg = wx.StaticText(self, -1, "Select a dependent variable:")
332        self.output_box = wx.ComboBox(self, -1)
333        for item in self.results.outputs.keys():
334            self.output_box.Append(item, "")
335        self.output_box.SetStringSelection(DEFAULT_OUTPUT)
336       
337        output_sizer = wx.GridBagSizer(5, 5)
338        output_sizer.Add(selection_msg, (0, 0), (1, 1),
339                         wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
340        output_sizer.Add(self.output_box, (0, 1), (1, 2),
341                         wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
342       
343        wx.EVT_COMBOBOX(self.output_box, -1, self._plot_output) 
344        sizer_main.Add(output_sizer, 0, wx.EXPAND | wx.ALL, 10)
345       
346        sizer_main.Add(self.plotpanel, 0, wx.EXPAND | wx.ALL, 10)
347        sizer_main.SetItemMinSize(self.plotpanel, 400, 400)
348       
349        sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 10)
350        static_line_3 = wx.StaticLine(self, -1)
351        sizer_main.Add(static_line_3, 0, wx.EXPAND, 0)
352       
353        # Bottom area with the close button
354        sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
355        button_OK = wx.Button(self, wx.ID_OK, "Close")
356        sizer_button.Add(button_OK, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)
357       
358        sizer_main.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
359        self.SetAutoLayout(True)
360        self.SetSizer(sizer_main)
361        self.Layout()
362        self.Centre()
363       
364        # Bind the Enter key to recalculation
365        self.Bind(wx.EVT_TEXT_ENTER, self._recalc)
366
367    def set_plot_unfocus(self):
368        """
369        Not implemented
370        """
371        pass
372   
373    def _recalc(self, event=None):
374        """
375        Invoked when the user changed a value on the form.
376        Process the form and compute the output to be plottted.
377        """
378        # Get the content of the form
379        content = self._get_values()
380        # If the content of the form is invalid, return and do nothing
381        if content is None:
382            return
383       
384        # Results object to store the computation outputs.
385        results = Results()
386       
387        # Loop over d_max values
388        for i in range(content.npts): 
389            temp = (content.dmax - content.dmin)/(content.npts - 1.0)   
390            d = content.dmin + i * temp
391               
392            self.pr_state.d_max = d
393            try:
394                out, cov = self.pr_state.invert(self.nfunc)   
395           
396                # Store results
397                iq0 = self.pr_state.iq0(out)
398                rg = self.pr_state.rg(out)
399                pos = self.pr_state.get_positive(out)
400                pos_err = self.pr_state.get_pos_err(out, cov)
401                osc = self.pr_state.oscillations(out)
402           
403                results.d_max.append(self.pr_state.d_max)
404                results.bck.append(self.pr_state.background)
405                results.chi2.append(self.pr_state.chi2)
406                results.iq0.append(iq0)
407                results.rg.append(rg)
408                results.pos.append(pos)
409                results.pos_err.append(pos_err)
410                results.osc.append(osc)           
411            except:
412                # This inversion failed, skip this D_max value
413                msg = "ExploreDialog: inversion failed "
414                msg += "for D_max=%s\n%s" % (str(d), sys.exc_value)
415                logging.error(msg)
416           
417        self.results = results           
418         
419        # Plot the selected output
420        self._plot_output()   
421           
422##### testing code ############################################################
423"""
424Example: ::
425
426class MyApp(wx.App):
427   
428    #Test application used to invoke the ExploreDialog for testing
429   
430    def OnInit(self):
431        from inversion_state import Reader
432        from sas.pr.invertor import Invertor
433        wx.InitAllImageHandlers()
434       
435        def call_back(state, datainfo=None):
436           
437            #Dummy call-back method used by the P(r)
438            #file reader.
439           
440            print state
441           
442        # Load test data
443        # This would normally be loaded by the application
444        # of the P(r) plug-in.
445        r = Reader(call_back)
446        data = r.read("test_pr_data.prv")
447        pr_state = data.meta_data['prstate']
448       
449        # Create Invertor object for the data
450        # This would normally be assembled by the P(r) plug-in
451        pr = Invertor()
452        pr.d_max = pr_state.d_max
453        pr.alpha = pr_state.alpha
454        pr.q_min = pr_state.qmin
455        pr.q_max = pr_state.qmax
456        pr.x = data.x
457        pr.y = data.y
458        pr.err = data.y*0.1
459        pr.has_bck = False
460        pr.slit_height = pr_state.height
461        pr.slit_width = pr_state.width
462       
463        # Create the dialog
464        dialog = ExploreDialog(pr, 10, None, -1, "")
465        self.SetTopWindow(dialog)
466        dialog.ShowModal()
467        dialog.Destroy()
468
469        return 1
470
471if __name__ == "__main__":
472    app = MyApp(0)
473    app.MainLoop()
474   
475""" 
Note: See TracBrowser for help on using the repository browser.