source: sasview/prview/perspectives/pr/explore_dialog.py @ 1a3a03b

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 1a3a03b was 7116b6e0, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on documentation

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