source: sasview/guiframe/local_perspectives/plotting/Plotter1D.py @ d0d2ab5

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 d0d2ab5 was 3cc533e, checked in by Gervaise Alina <gervyh@…>, 16 years ago

show linear fit option on context menu

  • Property mode set to 100644
File size: 17.0 KB
RevLine 
[1bf33c1]1"""
2This software was developed by the University of Tennessee as part of the
3Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4project funded by the US National Science Foundation.
5
6See the license text in license.txt
7
8copyright 2008, University of Tennessee
9"""
10
11
12import wx
13import sys
[d72cee0]14import pylab, time
[0d9dae8]15
[1bf33c1]16import danse.common.plottools
17from danse.common.plottools.PlotPanel import PlotPanel
[18eba35]18from danse.common.plottools.plottables import Graph,Data1D,Theory1D,Data1D
[1bf33c1]19from sans.guicomm.events import EVT_NEW_PLOT
[18eba35]20from sans.guicomm.events import StatusEvent ,NewPlotEvent,SlicerEvent,ErrorDataEvent
[0d9dae8]21from sans.guiframe.utils import PanelMenu
[1bf33c1]22
23from binder import BindArtist
24
[0d9dae8]25
26DEFAULT_QMAX = 0.05
[1bf33c1]27DEFAULT_QSTEP = 0.001
28DEFAULT_BEAM = 0.005
29BIN_WIDTH =1
30
[0d9dae8]31
[1bf33c1]32class ModelPanel1D(PlotPanel):
33    """
34        Plot panel for use with the GUI manager
35    """
36   
37    ## Internal name for the AUI manager
38    window_name = "plotpanel"
39    ## Title to appear on top of the window
40    window_caption = "Plot Panel"
41    ## Flag to tell the GUI manager that this panel is not
42    #  tied to any perspective
43    ALWAYS_ON = True
44    ## Group ID
45    group_id = None
46   
47    def __init__(self, parent, id = -1, color = None,\
48        dpi = None, style = wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs):
49        """
50            Initialize the panel
51        """
52        PlotPanel.__init__(self, parent, id = id, style = style, **kwargs)
53       
54        ## Reference to the parent window
55        self.parent = parent
56        ## Plottables
57        self.plots = {}
[869c368]58        ## save errors dy  for each data plotted
59        self.err_dy={}
[6c0568b]60        ## flag to determine if the hide or show context menu item should
61        ## be displayed
[18eba35]62        self.errors_hide=0
[1bf33c1]63        ## Unique ID (from gui_manager)
64        self.uid = None
65        ## Action IDs for internal call-backs
66        self.action_ids = {}
67        ## Graph       
68        self.graph = Graph()
69        self.graph.xaxis("\\rm{Q}", 'A^{-1}')
70        self.graph.yaxis("\\rm{Intensity} ","cm^{-1}")
71        self.graph.render(self)
72   
[6c0568b]73   
[1bf33c1]74    def _reset(self):
75        """
76            Resets internal data and graph
77        """   
78        self.graph.reset()
79        self.plots      = {}
80        self.action_ids = {}
81   
[6c0568b]82   
[1bf33c1]83    def _onEVT_1DREPLOT(self, event):
84        """
85            Data is ready to be displayed
86            @param event: data event
87        """
[ffd23b5]88       
[1bf33c1]89        #TODO: Check for existence of plot attribute
90        # Check whether this is a replot. If we ask for a replot
91        # and the plottable no longer exists, ignore the event.
92        if hasattr(event, "update") and event.update==True \
93            and event.plot.name not in self.plots.keys():
94            return
95       
96        if hasattr(event, "reset"):
97            self._reset()
98       
99        is_new = True
100        if event.plot.name in self.plots.keys():
101            # Check whether the class of plottable changed
102            if not event.plot.__class__==self.plots[event.plot.name].__class__:
[ab8f936]103                #overwrite a plottable using the same name
[1bf33c1]104                self.graph.delete(self.plots[event.plot.name])
105            else:
[ab8f936]106                # plottable is already draw on the panel
[1bf33c1]107                is_new = False
[ffd23b5]108       
[1bf33c1]109        if is_new:
[ab8f936]110            # a new plottable overwrites a plotted one  using the same id
111            for plottable in self.plots.itervalues():
[e48a62e]112                if hasattr(event.plot,"id"):
113                    if event.plot.id==plottable.id :
114                        self.graph.delete(plottable)
[ab8f936]115           
[1bf33c1]116            self.plots[event.plot.name] = event.plot
117            self.graph.add(self.plots[event.plot.name])
118        else:
[ab8f936]119            #replot the graph
[1bf33c1]120            self.plots[event.plot.name].x = event.plot.x   
121            self.plots[event.plot.name].y = event.plot.y   
122            self.plots[event.plot.name].dy = event.plot.dy 
123            if hasattr(event.plot, 'dx') and hasattr(self.plots[event.plot.name], 'dx'):
124                self.plots[event.plot.name].dx = event.plot.dx   
125 
126        #TODO: Should re-factor this
[6c0568b]127        ## for all added plot the option to hide error show be displayed first
128        self.errors_hide = 0
129        ## Set axis labels
[1bf33c1]130        self.graph.xaxis(event.plot._xaxis, event.plot._xunit)
131        self.graph.yaxis(event.plot._yaxis, event.plot._yunit)
[6c0568b]132        ## Set the view scale for all plots
[1bf33c1]133        self._onEVT_FUNC_PROPERTY()
[6c0568b]134        ## render the graph
[1bf33c1]135        self.graph.render(self)
136        self.subplot.figure.canvas.draw_idle()
[869c368]137       
[6c0568b]138       
[1bf33c1]139    def onLeftDown(self,event): 
[6c0568b]140        """
141            left button down and ready to drag
142            Display the position of the mouse on the statusbar
143        """
[1bf33c1]144        PlotPanel.onLeftDown(self, event)
145        ax = event.inaxes
146        if ax != None:
147            position = "x: %8.3g    y: %8.3g" % (event.xdata, event.ydata)
148            wx.PostEvent(self.parent, StatusEvent(status=position))
149
[6c0568b]150
[1bf33c1]151    def _onRemove(self, event):
152        """
[6c0568b]153            Remove a plottable from the graph and render the graph
154            @param event: Menu event
[1bf33c1]155        """
[6c0568b]156        ## Check if there is a selected graph to remove
[1bf33c1]157        if not self.graph.selected_plottable == None:
158            self.graph.delete(self.plots[self.graph.selected_plottable])
159            del self.plots[self.graph.selected_plottable]
160            self.graph.render(self)
161            self.subplot.figure.canvas.draw_idle()   
162           
163
164    def onContextMenu(self, event):
165        """
166            1D plot context menu
167            @param event: wx context event
168        """
169        slicerpop = PanelMenu()
170        slicerpop.set_plots(self.plots)
171        slicerpop.set_graph(self.graph)
172               
[9a585d0]173        # Various plot options
174        id = wx.NewId()
175        slicerpop.Append(id,'&Save image', 'Save image as PNG')
176        wx.EVT_MENU(self, id, self.onSaveImage)
177       
178        id = wx.NewId()
179        slicerpop.Append(id,'&Print image', 'Print image ')
[18eba35]180        wx.EVT_MENU(self, id, self.onPrint)
181         
182        id = wx.NewId()
183        slicerpop.Append(id,'&Print Preview', 'image preview for print')
184        wx.EVT_MENU(self, id, self.onPrinterPreview)
[9a585d0]185           
186        slicerpop.AppendSeparator()
187        item_list = self.parent.get_context_menu(self.graph)
[6c0568b]188       
[9a585d0]189        if (not item_list==None) and (not len(item_list)==0):
190            for item in item_list:
191                try:
192                    id = wx.NewId()
193                    slicerpop.Append(id, item[0], item[1])
194                    wx.EVT_MENU(self, id, item[2])
195                except:
[6c0568b]196                    wx.PostEvent(self.parent, StatusEvent(status=\
197                        "ModelPanel1D.onContextMenu: bad menu item  %s"%sys.exc_value))
[9a585d0]198                    pass
199            slicerpop.AppendSeparator()
[6c0568b]200       
[1bf33c1]201        if self.graph.selected_plottable in self.plots:
202            plot = self.plots[self.graph.selected_plottable]
203            id = wx.NewId()
204            name = plot.name
[6c0568b]205           
[1bf33c1]206            slicerpop.Append(id, "&Save %s points" % name)
207            self.action_ids[str(id)] = plot
208            wx.EVT_MENU(self, id, self._onSave)
[6c0568b]209           
[8bd764d]210            id = wx.NewId()
211            slicerpop.Append(id, "&Save %s canSAS XML" % name)
212            self.action_ids[str(id)] = plot
213            wx.EVT_MENU(self, id, self._onSaveXML)
[6c0568b]214           
[1bf33c1]215            id = wx.NewId()
216            slicerpop.Append(id, "Remove %s curve" % name)
217            self.action_ids[str(id)] = plot
218            wx.EVT_MENU(self, id, self._onRemove)
[9a585d0]219            slicerpop.AppendSeparator()
[1bf33c1]220            # Option to hide
221            #TODO: implement functionality to hide a plottable (legend click)
[d468daa]222       
[1bf33c1]223        if self.graph.selected_plottable in self.plots:
[18eba35]224            if self.plots[self.graph.selected_plottable].name in self.err_dy.iterkeys()\
225                and self.errors_hide==1:
[6c0568b]226               
[1bf33c1]227                id = wx.NewId()
[18eba35]228                slicerpop.Append(id, '&Show errors to data')
[1bf33c1]229                wx.EVT_MENU(self, id, self._on_add_errors)
[18eba35]230               
231            elif self.plots[self.graph.selected_plottable].__class__.__name__=="Data1D"\
232                and self.errors_hide==0:
[6c0568b]233                   
[dd66fbd]234                    id = wx.NewId()
[18eba35]235                    slicerpop.Append(id, '&Hide Error bars')
[dd66fbd]236                    wx.EVT_MENU(self, id, self._on_remove_errors)
[3cc533e]237           
238            id = wx.NewId()
239            slicerpop.Append(id, '&Linear fit')
240            wx.EVT_MENU(self, id, self.onFitting)
[1bf33c1]241               
[9a585d0]242            slicerpop.AppendSeparator()
[6c0568b]243       
[1bf33c1]244        id = wx.NewId()
245        slicerpop.Append(id, '&Change scale')
246        wx.EVT_MENU(self, id, self._onProperties)
[6c0568b]247       
[1bf33c1]248        id = wx.NewId()
249        slicerpop.Append(id, '&Reset Graph')
[d468daa]250        wx.EVT_MENU(self, id, self.onResetGraph) 
[6d920cd]251       
[1bf33c1]252        pos = event.GetPosition()
253        pos = self.ScreenToClient(pos)
254        self.PopupMenu(slicerpop, pos)
[18eba35]255       
256       
[869c368]257    def _on_remove_errors(self, evt):
[6c0568b]258        """
259            Save name and dy of data in dictionary self.err_dy
260            Create a new data1D with the same x, y
261            vector and dy with zeros.
262            post self.err_dy as event (ErrorDataEvent) for any object
263            which wants to reconstruct the initial data.
264            @param evt: Menu event
265        """
[869c368]266        if not self.graph.selected_plottable == None:
[6c0568b]267            ## store existing dy
[869c368]268            name =self.plots[self.graph.selected_plottable].name
269            dy = self.plots[self.graph.selected_plottable].dy
270            self.err_dy[name]= dy
[6c0568b]271            ## Create a new dy for a new plottable
[18eba35]272            import numpy
273            dy= numpy.zeros(len(self.plots[self.graph.selected_plottable].y))
274            new_plot = Data1D(self.plots[self.graph.selected_plottable].x,
[869c368]275                              self.plots[self.graph.selected_plottable].y,
[18eba35]276                              dy=dy)
[869c368]277            new_plot.interactive = True
[6c0568b]278            self.errors_hide = 1
[869c368]279            new_plot.name = self.plots[self.graph.selected_plottable].name
280            if hasattr(self.plots[self.graph.selected_plottable], "group_id"):
281                new_plot.group_id = self.plots[self.graph.selected_plottable].group_id
282                new_plot.id = self.plots[self.graph.selected_plottable].id
283            else:
284                new_plot.group_id = str(time.time())
285                new_plot.id = str(time.time())
286            label, unit = self.plots[self.graph.selected_plottable].get_xaxis()
287            new_plot.xaxis(label, unit)
288            label, unit = self.plots[self.graph.selected_plottable].get_yaxis()
289            new_plot.yaxis(label, unit)
[6c0568b]290            ## save the color of the selected plottable before it is deleted
[869c368]291            color=self.graph.plottables[self.plots[self.graph.selected_plottable]]
[18eba35]292            self.graph.delete(self.plots[self.graph.selected_plottable])
[6c0568b]293            ## add newly created plottable to the graph with the save color
[dd66fbd]294            self.graph.add(new_plot,color)
[6c0568b]295            ## transforming the view of the new data into the same of the previous data
[869c368]296            self._onEVT_FUNC_PROPERTY()
[6c0568b]297            ## save the plot
[869c368]298            self.plots[self.graph.selected_plottable]=new_plot
[6c0568b]299            ## Render the graph
[869c368]300            self.graph.render(self)
301            self.subplot.figure.canvas.draw_idle() 
[18eba35]302           
303            event = ErrorDataEvent(err_dy=self.err_dy)
304            wx.PostEvent(self.parent, event)
[1bf33c1]305   
[6c0568b]306   
[1bf33c1]307    def _on_add_errors(self, evt):
308        """
[6c0568b]309            create a new data1D witht the errors saved in self.err_dy
310            to show errors of the plot.
[1bf33c1]311            Compute reasonable errors for a data set without
312            errors and transorm the plottable to a Data1D
[6c0568b]313            @param evt: Menu event
[1bf33c1]314        """
315        import math
316        import numpy
317        import time
318       
319        if not self.graph.selected_plottable == None:
[6c0568b]320            ##Reset the flag to display the hide option on the context menu
321            self.errors_hide = 0
322            ## restore dy
[1bf33c1]323            length = len(self.plots[self.graph.selected_plottable].x)
324            dy = numpy.zeros(length)
[869c368]325            selected_plot= self.plots[self.graph.selected_plottable]
326            try:
327                dy = self.err_dy[selected_plot.name]
328            except:
329                for i in range(length):
330                    dy[i] = math.sqrt(self.plots[self.graph.selected_plottable].y[i])     
[6c0568b]331            ## Create a new plottable data1D
[1bf33c1]332            new_plot = Data1D(self.plots[self.graph.selected_plottable].x,
333                              self.plots[self.graph.selected_plottable].y,
334                              dy=dy)
335            new_plot.interactive = True
[6c0568b]336           
[1bf33c1]337            new_plot.name = self.plots[self.graph.selected_plottable].name
338            if hasattr(self.plots[self.graph.selected_plottable], "group_id"):
339                new_plot.group_id = self.plots[self.graph.selected_plottable].group_id
[d1dd9d4]340                new_plot.id = self.plots[self.graph.selected_plottable].id
[1bf33c1]341            else:
342                new_plot.group_id = str(time.time())
[d1dd9d4]343                new_plot.id = str(time.time())
[1bf33c1]344           
345            label, unit = self.plots[self.graph.selected_plottable].get_xaxis()
346            new_plot.xaxis(label, unit)
347            label, unit = self.plots[self.graph.selected_plottable].get_yaxis()
348            new_plot.yaxis(label, unit)
[6c0568b]349            ## save the color of the selected plottable before it is deleted
[18eba35]350            color=self.graph.plottables[self.plots[self.graph.selected_plottable]]
351            self.graph.delete(self.plots[self.graph.selected_plottable])
[6c0568b]352            ## add newly created plottable to the graph with the save color
[18eba35]353            self.graph.add(new_plot, color)
[6c0568b]354            ## transforming the view of the new data into the same of the previous data
[ffd23b5]355            self._onEVT_FUNC_PROPERTY()
[6c0568b]356            ## save the plot
[1bf33c1]357            self.plots[self.graph.selected_plottable]=new_plot
[6c0568b]358            ## render the graph with its new content
[1bf33c1]359            self.graph.render(self)
[8bd764d]360            self.subplot.figure.canvas.draw_idle() 
361               
[6c0568b]362               
[8bd764d]363    def _onSaveXML(self, evt):
[6c0568b]364        """
365            Save 1D  Data to  XML file
366            @param evt: Menu event
367        """
[8bd764d]368        import os
369        id = str(evt.GetId())
370        if id in self.action_ids:         
371            path = None
372            dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.xml", wx.SAVE)
373            if dlg.ShowModal() == wx.ID_OK:
374                path = dlg.GetPath()
375                mypath = os.path.basename(path)
376            dlg.Destroy()
377           
378            if not path == None:
379                out = open(path, 'w')
380                from DataLoader.readers import cansas_reader
381                reader = cansas_reader.Reader()
382                datainfo= self.plots[self.graph.selected_plottable].info
383                reader.write( path, datainfo)
384            return 
[6c0568b]385                     
[8bd764d]386               
387               
[1bf33c1]388    def _onSave(self, evt):
389        """
390            Save a data set to a text file
391            @param evt: Menu event
392        """
393        import os
394        id = str(evt.GetId())
395        if id in self.action_ids:         
396           
397            path = None
398            dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.txt", wx.SAVE)
399            if dlg.ShowModal() == wx.ID_OK:
400                path = dlg.GetPath()
401                mypath = os.path.basename(path)
[6c0568b]402               
[1bf33c1]403            dlg.Destroy()
404           
405            if not path == None:
406                out = open(path, 'w')
407                has_errors = True
408                if self.action_ids[id].dy==None or self.action_ids[id].dy==[]:
409                    has_errors = False
410                   
411                # Sanity check
412                if has_errors:
413                    try:
414                        if len(self.action_ids[id].y) != len(self.action_ids[id].dy):
[6c0568b]415   
[1bf33c1]416                            has_errors = False
417                    except:
418                        has_errors = False
419               
420                if has_errors:
421                    out.write("<X>   <Y>   <dY>\n")
422                else:
423                    out.write("<X>   <Y>\n")
424                   
425                for i in range(len(self.action_ids[id].x)):
426                    if has_errors:
427                        out.write("%g  %g  %g\n" % (self.action_ids[id].x[i], 
428                                                    self.action_ids[id].y[i],
429                                                    self.action_ids[id].dy[i]))
430                    else:
431                        out.write("%g  %g\n" % (self.action_ids[id].x[i], 
432                                                self.action_ids[id].y[i]))
433                       
434                out.close()
435   
[6c0568b]436   
[1bf33c1]437       
Note: See TracBrowser for help on using the repository browser.