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

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 d1e0473 was 6063b16, checked in by Gervaise Alina <gervyh@…>, 15 years ago

remember the previous directory for save option

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