source: sasview/guiframe/local_perspectives/plotting/Plotter1D.py @ 531a811c

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 531a811c was 3b69ca6, checked in by Gervaise Alina <gervyh@…>, 15 years ago

hide and show error ,store dxl and dxw value

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