source: sasview/plottools/examples/test_panel.py @ c080e542

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 c080e542 was 82a54b8, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

adding plottools Part 2

  • Property mode set to 100644
File size: 6.7 KB
Line 
1"""
2    Test application that uses plottools
3    This application uses most of the functionality required by the SANS group
4    and should be treated as a test-case representative of that use.
5   
6    The following is a checklist of functionality to look for while testing:
7   
8    1- Start the application: the graph should be empty with a white background.
9        The status bar should read "This is a data viewer".
10    2- From the "File" menu, click "Add data": Three data sets should be plotted:
11        - A line called Theory curve, in blue.
12        - A series of green points with errors above that line, called Data set 1.
13        - A series of red points with errors below that line, called Data set 2.
14    3- Hovering over any plotted data will highlight the whole data set or line in yellow.
15    4- Left-clicking on the graph and dragging will drag the graph.
16    5- Using the mouse wheel will zoom in and out of the graph.
17    6- Right-clicking on the graph when no curve is highlighted will
18        pop up the context menu:
19        - "Save image" will pop up a save dialog to save an image.
20        - "Reset graph" will reset the graph to its original appearance after it
21            has been dragged around or zoomed in or out.
22        - "Change scale" will pop up a scale dialog with which the axes can
23            be transformed. Each transformation choice should work.
24    7- Right-clicking on the graph when a curve is highlighted will
25        pop up the context menu describe above, to which an item called
26        "Linear fit to [name of the data set]" is added.
27   
28        Selecting a linear fit to data sets 1 or 2 will pop up a fit dialog.
29        After fitting, the fit result will appear will be shown in the dialog
30        and the fit curve will appear on the plot and should follow the data.
31   
32        It is possible to interact with the graph when the fit dialog is up.
33       
34        Selecting a linear fit to the theory curve will produce a message
35        on the status bar saying: "Can't fit a theory curve"
36     
37"""
38import wx
39from danse.common.plottools.PlotPanel import PlotPanel
40from danse.common.plottools.plottables import Plottable, Graph, Data1D, Theory1D
41import  sys
42import numpy
43import random, math
44
45
46class SANSplotpanel(PlotPanel):
47   
48    def __init__(self, parent, id = -1, color = None,\
49        dpi = None, **kwargs):
50        PlotPanel.__init__(self, parent, id=id, color=color, dpi=dpi, **kwargs)
51       
52        # Keep track of the parent Frame
53        self.parent = parent
54       
55        # Internal list of plottable names (because graph
56        # doesn't have a dictionary of handles for the plottables)
57        self.plots = {}
58       
59    def add_plottable(self, plot):
60        self.plots[plot.name] = plot
61       
62        self.graph.add(plot)
63        self.graph.xaxis('\\rm{q} ', 'A^{-1}')
64        self.graph.yaxis("\\rm{Intensity} ","cm^{-1}")
65        self.onResetGraph(None)
66               
67    def plottable_selected(self, id):
68        PlotPanel.plottable_selected(self, id)
69        if id is not None:
70            self.parent.SetStatusText("Hovering over %s" % self.graph.selected_plottable)
71        else:
72            self.parent.SetStatusText("")
73   
74 
75       
76    def onFitting(self, event):
77        if self.graph.selected_plottable is not None:
78            if self.plots[self.graph.selected_plottable].__class__.__name__ == 'Data1D':
79                PlotPanel.onFitting(self, event)     
80            else:
81                self.parent.SetStatusText("Can't fit a theory curve")
82
83class ViewerFrame(wx.Frame):
84    """
85        Add comment
86    """
87    def __init__(self, parent, id, title):
88        """
89            comment
90            @param parent: parent panel/container
91        """
92        # Initialize the Frame object
93        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(950,850))
94       
95        # Panel for 1D plot
96        self.plotpanel    = SANSplotpanel(self, -1, style=wx.RAISED_BORDER)
97
98        # Set up the menu
99        self._setup_menus()
100     
101        # Set up the layout
102        self._setup_layout()
103               
104        # Register the close event so it calls our own method
105        wx.EVT_CLOSE(self, self._onClose)
106       
107       
108    def _setup_layout(self):
109        """
110            Set up the layout
111        """
112        # Status bar
113        self.sb = self.CreateStatusBar()
114        self.SetStatusText("This is a data viewer")
115       
116        # Two columns of panels
117        sizer = wx.GridBagSizer(0,0)
118       
119        sizer.Add(self.plotpanel, (0,0), (1,1),   flag=wx.EXPAND | wx.ALL, border=0)
120        sizer.AddGrowableRow(0)
121        sizer.AddGrowableCol(0)
122   
123        self.SetSizer(sizer)
124        self.Centre()
125               
126    def _setup_menus(self):
127        """
128            Set up the application menus
129        """
130       
131        # Menu
132        menubar = wx.MenuBar()
133        # File menu
134        filemenu = wx.Menu()
135       
136        # Quit
137        id = wx.NewId()
138        filemenu.Append(id,'&Quit', 'Exit')
139        wx.EVT_MENU(self, id, self._onClose)
140       
141        # New plot
142        id = wx.NewId()
143        filemenu.Append(id,'&Add data', 'Add a new curve to the plot')
144        wx.EVT_MENU(self, id, self._add_data)
145 
146        # Add sub menus
147        menubar.Append(filemenu, '&File')
148         
149        self.SetMenuBar(menubar)
150
151    def _onClose(self, event):
152        """
153        """
154        wx.Exit()
155        sys.exit()
156           
157    def _add_data(self, event):
158        data_len = 50
159        x  = numpy.zeros(data_len)
160        y  = numpy.zeros(data_len)
161        x2  = numpy.zeros(data_len)
162        y2  = numpy.zeros(data_len)
163        dy2  = numpy.zeros(data_len)
164        x3  = numpy.zeros(data_len)
165        y3  = numpy.zeros(data_len)
166        dy3  = numpy.zeros(data_len)
167        for i in range(len(x)):
168            x[i] = i
169            x2[i] = i-0.1+0.2*random.random()
170            x3[i] = i-0.1+0.2*random.random()
171            y[i] = 0.5*(i+random.random())
172            y2[i] = i+random.random()
173            dy2[i] = math.sqrt(y2[i]) 
174            y3[i] = 0.3*(i+random.random())
175            dy3[i] = math.sqrt(y3[i]) 
176           
177        newplot = Data1D(x=x, y=y)
178        newplot.name = "Theory curve"
179        newplot.symbol = 13
180        self.plotpanel.add_plottable(newplot)
181       
182       
183        newplot = Data1D(x=x2, y=y2, dy=dy2)
184        newplot.name = "Data set 1"
185        self.plotpanel.add_plottable(newplot)
186       
187        newplot = Data1D(x=x3, y=y3, dy=dy3)
188        newplot.name = "Data set 2"
189        self.plotpanel.add_plottable(newplot)
190           
191 
192class ViewApp(wx.App):
193    def OnInit(self):
194        frame = ViewerFrame(None, -1, 'testView')   
195        frame.Show(True)
196        self.SetTopWindow(frame)
197       
198        return True
199       
200
201if __name__ == "__main__": 
202    app = ViewApp(0)
203    app.MainLoop()       
204           
Note: See TracBrowser for help on using the repository browser.