source: sasview/guiframe/local_perspectives/plotting/Plotter2D.py @ 4e0806e8

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 4e0806e8 was 6c0568b, checked in by Gervaise Alina <gervyh@…>, 16 years ago

comment class

  • Property mode set to 100644
File size: 18.9 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
[ea290ee]13import sys, math
[0d9dae8]14import pylab
15
[1bf33c1]16import danse.common.plottools
17from danse.common.plottools.PlotPanel import PlotPanel
18from danse.common.plottools.plottables import Graph,Data1D
[0bd2cd8]19from sans.guicomm.events import EVT_NEW_PLOT
[0d9dae8]20from sans.guicomm.events import EVT_SLICER_PARS
21from sans.guicomm.events import StatusEvent ,NewPlotEvent,SlicerEvent
22from sans.guiframe.utils import PanelMenu
[1bf33c1]23from binder import BindArtist
24from Plotter1D import ModelPanel1D
[0d9dae8]25(InternalEvent, EVT_INTERNAL)   = wx.lib.newevent.NewEvent()
26
[1bf33c1]27
[0d9dae8]28
29DEFAULT_QMAX = 0.05
[1bf33c1]30DEFAULT_QSTEP = 0.001
31DEFAULT_BEAM = 0.005
[ef0c170]32BIN_WIDTH = 1.0
[0d9dae8]33
34
35
36
[1bf33c1]37class ModelPanel2D( ModelPanel1D):
38    """
39        Plot panel for use with the GUI manager
40    """
41   
42    ## Internal name for the AUI manager
43    window_name = "plotpanel"
44    ## Title to appear on top of the window
45    window_caption = "Plot Panel"
46    ## Flag to tell the GUI manager that this panel is not
47    #  tied to any perspective
48    ALWAYS_ON = True
49    ## Group ID
50    group_id = None
51   
52   
53    def __init__(self, parent, id = -1,data2d=None, color = None,\
54        dpi = None, style = wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs):
55        """
56            Initialize the panel
57        """
58        ModelPanel1D.__init__(self, parent, id = id, style = style, **kwargs)
59       
60        ## Reference to the parent window
61        self.parent = parent
[6c0568b]62        ## Dictionary containing Plottables
[1bf33c1]63        self.plots = {}
[6c0568b]64        ## Save reference of the current plotted
65        self.data2D = data2d
[1bf33c1]66        ## Unique ID (from gui_manager)
67        self.uid = None
68        ## Action IDs for internal call-backs
69        self.action_ids = {}
[6c0568b]70        ## Create Artist and bind it
[1bf33c1]71        self.connect = BindArtist(self.subplot.figure)
[6c0568b]72        ## Beam stop
[1bf33c1]73        self.beamstop_radius = DEFAULT_BEAM
[6c0568b]74        ## to set the order of lines drawn first.
[f15ed33]75        self.slicer_z = 5
[6c0568b]76        ## Reference to the current slicer
[1bf33c1]77        self.slicer = None
[6c0568b]78        ## event to send slicer info
[d468daa]79        self.Bind(EVT_INTERNAL, self._onEVT_INTERNAL)
[1bf33c1]80       
[6c0568b]81        self.axes_frozen = False
82        ## panel that contains result from slicer motion (ex: Boxsum info)
[54cc36a]83        self.panel_slicer=None
[1bf33c1]84        ## Graph       
85        self.graph = Graph()
86        self.graph.xaxis("\\rm{Q}", 'A^{-1}')
87        self.graph.yaxis("\\rm{Intensity} ","cm^{-1}")
88        self.graph.render(self)
[0d9dae8]89       
90       
[1bf33c1]91    def _onEVT_1DREPLOT(self, event):
92        """
93            Data is ready to be displayed
[4b91fd1]94           
95            #TODO: this name should be changed to something more appropriate
96            # Don't forget that changing this name will mean changing code
97            # in plotting.py
98             
[1bf33c1]99            @param event: data event
100        """
[6c0568b]101        ## Update self.data2d with the current plot
102        self.data2D = event.plot
[1bf33c1]103        #TODO: Check for existence of plot attribute
[6c0568b]104       
[1bf33c1]105        # Check whether this is a replot. If we ask for a replot
106        # and the plottable no longer exists, ignore the event.
107        if hasattr(event, "update") and event.update==True \
108            and event.plot.name not in self.plots.keys():
109            return
[ab8f936]110       
[1bf33c1]111        if hasattr(event, "reset"):
112            self._reset()
113        is_new = True
114        if event.plot.name in self.plots.keys():
115            # Check whether the class of plottable changed
116            if not event.plot.__class__==self.plots[event.plot.name].__class__:
[ab8f936]117                #overwrite a plottable using the same name
[1bf33c1]118                self.graph.delete(self.plots[event.plot.name])
119            else:
[ab8f936]120                # plottable is already draw on the panel
[1bf33c1]121                is_new = False
[ab8f936]122           
123        if is_new:
124            # a new plottable overwrites a plotted one  using the same id
125            for plottable in self.plots.itervalues():
[e48a62e]126                if hasattr(event.plot,"id"):
127                    if event.plot.id==plottable.id :
128                        self.graph.delete(plottable)
[ab8f936]129           
130            self.plots[event.plot.name] = event.plot
131            self.graph.add(self.plots[event.plot.name])
132        else:
[4b91fd1]133            # Update the plottable with the new data
134           
135            #TODO: we should have a method to do this,
136            #      something along the lines of:
137            #      plottable1.update_data_from_plottable(plottable2)
138           
139            self.plots[event.plot.name].xmin = event.plot.xmin
140            self.plots[event.plot.name].xmax = event.plot.xmax
141            self.plots[event.plot.name].ymin = event.plot.ymin
142            self.plots[event.plot.name].ymax = event.plot.ymax
143            self.plots[event.plot.name].data = event.plot.data
144            self.plots[event.plot.name].err_data = event.plot.err_data
[ac9a5f6]145            # update qmax with the new xmax of data plotted
146            self.qmax= event.plot.xmax
[4b91fd1]147           
[ac9a5f6]148        self.slicer= None
[1bf33c1]149        # Check axis labels
150        #TODO: Should re-factor this
[6c0568b]151        ## render the graph with its new content
[1bf33c1]152        self.graph.xaxis(event.plot._xaxis, event.plot._xunit)
153        self.graph.yaxis(event.plot._yaxis, event.plot._yunit)
[0690e1d]154        self.graph.title(self.data2D.name)
[1bf33c1]155        self.graph.render(self)
156        self.subplot.figure.canvas.draw_idle()
157
158
159    def onContextMenu(self, event):
160        """
161            2D plot context menu
162            @param event: wx context event
163        """
164        slicerpop = PanelMenu()
165        slicerpop.set_plots(self.plots)
166        slicerpop.set_graph(self.graph)
[9a585d0]167       
168             
169        id = wx.NewId()
170        slicerpop.Append(id, '&Save image')
171        wx.EVT_MENU(self, id, self.onSaveImage)
172       
173        id = wx.NewId()
174        slicerpop.Append(id,'&Print image', 'Print image ')
175        wx.EVT_MENU(self, id, self.onPrint)
176       
[1ce365f8]177        id = wx.NewId()
[18eba35]178        slicerpop.Append(id,'&Print Preview', 'image preview for print')
[1ce365f8]179        wx.EVT_MENU(self, id, self.onPrinterPreview)
180       
[9a585d0]181        slicerpop.AppendSeparator()
[1bf33c1]182        item_list = self.parent.get_context_menu(self.graph)
183        if (not item_list==None) and (not len(item_list)==0):
[d468daa]184               
[1bf33c1]185                for item in item_list:
186                    try:
187                        id = wx.NewId()
188                        slicerpop.Append(id, item[0], item[1])
189                        wx.EVT_MENU(self, id, item[2])
190                    except:
[6c0568b]191                        wx.PostEvent(self.parent, StatusEvent(status=\
192                        "ModelPanel1D.onContextMenu: bad menu item  %s"%sys.exc_value))
[d468daa]193                        pass
[9a585d0]194                slicerpop.AppendSeparator()
[8bd764d]195       
[ea290ee]196        id = wx.NewId()
197        slicerpop.Append(id, '&Edit Detector Parameters')
198        wx.EVT_MENU(self, id, self._onEditDetector) 
[1bf33c1]199       
200        id = wx.NewId()
201        slicerpop.Append(id, '&Perform circular average')
202        wx.EVT_MENU(self, id, self.onCircular) 
203       
204        id = wx.NewId()
[ef0c170]205        slicerpop.Append(id, '&Sector [Q view]')
[1bf33c1]206        wx.EVT_MENU(self, id, self.onSectorQ) 
207       
208        id = wx.NewId()
[ef0c170]209        slicerpop.Append(id, '&Annulus [Phi view ]')
[1bf33c1]210        wx.EVT_MENU(self, id, self.onSectorPhi) 
[92c2345]211       
[7ab9241]212        id = wx.NewId()
213        slicerpop.Append(id, '&Box Sum')
214        wx.EVT_MENU(self, id, self.onBoxSum) 
215       
[38224f10]216        id = wx.NewId()
[8a7a21b]217        slicerpop.Append(id, '&Box averaging in Qx')
218        wx.EVT_MENU(self, id, self.onBoxavgX) 
219       
220        id = wx.NewId()
221        slicerpop.Append(id, '&Box averaging in Qy')
222        wx.EVT_MENU(self, id, self.onBoxavgY) 
[6c0568b]223       
[eba08f1a]224        if self.slicer !=None :
[92c2345]225            id = wx.NewId()
226            slicerpop.Append(id, '&Clear slicer')
227            wx.EVT_MENU(self, id,  self.onClearSlicer) 
[6c0568b]228           
[eba08f1a]229            if self.slicer.__class__.__name__ !="BoxSum":
230                id = wx.NewId()
231                slicerpop.Append(id, '&Edit Slicer Parameters')
232                wx.EVT_MENU(self, id, self._onEditSlicer) 
[6c0568b]233               
[d468daa]234        slicerpop.AppendSeparator() 
[9a585d0]235       
[1bf33c1]236        id = wx.NewId()
237        slicerpop.Append(id, '&Toggle Linear/Log scale')
238        wx.EVT_MENU(self, id, self._onToggleScale) 
[d468daa]239                 
[1bf33c1]240        pos = event.GetPosition()
241        pos = self.ScreenToClient(pos)
242        self.PopupMenu(slicerpop, pos)
[8bd764d]243   
[6c0568b]244   
[ea290ee]245    def _onEditDetector(self, event):
[6d920cd]246        """
[6c0568b]247            Allow to view and edits  detector parameters
248            @param event: wx.menu event
[6d920cd]249        """
[6c0568b]250       
[ea290ee]251        import detector_dialog
[9a585d0]252        dialog = detector_dialog.DetectorDialog(self, -1,base=self.parent)
[6c0568b]253        ## info of current detector and data2D
[ea290ee]254        xnpts = len(self.data2D.x_bins)
255        ynpts = len(self.data2D.y_bins)
256        xmax = max(self.data2D.xmin, self.data2D.xmax)
257        ymax = max(self.data2D.ymin, self.data2D.ymax)
258        qmax = math.sqrt(math.pow(xmax,2)+math.pow(ymax,2))
259        beam = self.data2D.xmin
260        zmin = self.zmin_2D
261        zmax = self.zmax_2D
[6c0568b]262        ## set dialog window content
[ea290ee]263        dialog.setContent(xnpts=xnpts,ynpts=ynpts,qmax=qmax,
264                           beam=self.data2D.xmin,
265                           zmin = self.zmin_2D,
266                          zmax = self.zmax_2D)
267        if dialog.ShowModal() == wx.ID_OK:
268            evt = dialog.getContent()
269            self.zmin_2D = evt.zmin
270            self.zmax_2D = evt.zmax
271       
272        dialog.Destroy()
[6c0568b]273        ## Redraw the current image
[ea290ee]274        self.image(data= self.data2D.data,
275                   xmin= self.data2D.xmin,
276                   xmax= self.data2D.xmax,
277                   ymin= self.data2D.ymin,
278                   ymax= self.data2D.ymax,
279                   zmin= self.zmin_2D,
280                   zmax= self.zmax_2D,
281                   color=0,symbol=0,label='data2D')
282        self.subplot.figure.canvas.draw_idle()
[1bf33c1]283       
[6c0568b]284       
285 
[1bf33c1]286    def freeze_axes(self):
287        self.axes_frozen = True
288       
289    def thaw_axes(self):
290        self.axes_frozen = False
291       
292    def onMouseMotion(self,event):
293        pass
294    def onWheel(self, event):
[6c0568b]295        pass 
296     
[1bf33c1]297    def update(self, draw=True):
298        """
299            Respond to changes in the model by recalculating the
300            profiles and resetting the widgets.
301        """
302        self.draw()
303       
304       
305    def _getEmptySlicerEvent(self):
[6c0568b]306        """
307            create an empty slicervent
308        """
[1bf33c1]309        return SlicerEvent(type=None,
310                           params=None,
311                           obj_class=None)
312    def _onEVT_INTERNAL(self, event):
313        """
[6c0568b]314            Draw the slicer
315            @param event: wx.lib.newevent (SlicerEvent) containing slicer
316            parameter
[1bf33c1]317        """
318        self._setSlicer(event.slicer)
319           
320    def _setSlicer(self, slicer):
[6c0568b]321        """
322            Clear the previous slicer and create a new one.Post an internal
323            event.
324            @param slicer: slicer class to create
325        """
[1bf33c1]326       
[6c0568b]327        ## Clear current slicer
[1bf33c1]328        if not self.slicer == None: 
329            self.slicer.clear()           
[6c0568b]330        ## Create a new slicer   
[1bf33c1]331        self.slicer_z += 1
332        self.slicer = slicer(self, self.subplot, zorder=self.slicer_z)
[240c805]333        self.subplot.set_ylim(self.data2D.ymin, self.data2D.ymax)
334        self.subplot.set_xlim(self.data2D.xmin, self.data2D.xmax)
[6c0568b]335        ## Draw slicer
[1bf33c1]336        self.update()
337        self.slicer.update()
[6c0568b]338        wx.PostEvent(self.parent, StatusEvent(status=\
339                        "Plotter2D._setSlicer  %s"%self.slicer.__class__.__name__))
[1bf33c1]340        # Post slicer event
341        event = self._getEmptySlicerEvent()
342        event.type = self.slicer.__class__.__name__
[54cc36a]343       
[1bf33c1]344        event.obj_class = self.slicer.__class__
345        event.params = self.slicer.get_params()
[d468daa]346        wx.PostEvent(self, event)
[1bf33c1]347
[6c0568b]348
[1bf33c1]349    def onCircular(self, event):
350        """
351            perform circular averaging on Data2D
[6c0568b]352            @param event: wx.menu event
[1bf33c1]353        """
354       
355        from DataLoader.manipulations import CircularAverage
[6c0568b]356        ## compute the maximum radius of data2D
[216efab]357        self.qmax= max(math.fabs(self.data2D.xmax),math.fabs(self.data2D.xmin))
358        self.ymax=max(math.fabs(self.data2D.ymax),math.fabs(self.data2D.ymin))
359        self.radius= math.sqrt( math.pow(self.qmax,2)+math.pow(self.ymax,2)) 
[6c0568b]360        ##Compute beam width
[8f584c9]361        bin_width = (self.qmax +self.qmax)/100
[6c0568b]362        ## Create data1D circular average of data2D
[c73d871]363        Circle = CircularAverage( r_min=0, r_max=self.radius, bin_width=bin_width)
[1bf33c1]364        circ = Circle(self.data2D)
[6c0568b]365       
[1bf33c1]366        from sans.guiframe.dataFitting import Data1D
367        if hasattr(circ,"dxl"):
368            dxl= circ.dxl
369        else:
370            dxl= None
371        if hasattr(circ,"dxw"):
372            dxw= circ.dxw
373        else:
374            dxw= None
[ef0c170]375       
[1bf33c1]376        new_plot = Data1D(x=circ.x,y=circ.y,dy=circ.dy,dxl=dxl,dxw=dxw)
377        new_plot.name = "Circ avg "+ self.data2D.name
378        new_plot.source=self.data2D.source
[50cbace]379        #new_plot.info=self.data2D.info
[1bf33c1]380        new_plot.interactive = True
381        new_plot.detector =self.data2D.detector
[6c0568b]382        ## If the data file does not tell us what the axes are, just assume...
[8f584c9]383        new_plot.xaxis("\\rm{Q}","A^{-1}")
[1bf33c1]384        new_plot.yaxis("\\rm{Intensity} ","cm^{-1}")
385        new_plot.group_id = "Circ avg "+ self.data2D.name
[8b30e62]386        new_plot.id = "Circ avg "+ self.data2D.name
[6c0568b]387       
[1bf33c1]388        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=new_plot.name))
[ef0c170]389       
[6c0568b]390       
[1bf33c1]391    def _onEditSlicer(self, event):
[6c0568b]392        """
393            Is available only when a slicer is drawn.Create a dialog
394            window where the user can enter value to reset slicer
395            parameters.
396            @param event: wx.menu event
397        """
[1bf33c1]398        if self.slicer !=None:
399            from SlicerParameters import SlicerParameterPanel
[4f8a00c]400            dialog = SlicerParameterPanel(self, -1, "Slicer Parameters")
[1bf33c1]401            dialog.set_slicer(self.slicer.__class__.__name__,
402                            self.slicer.get_params())
403            if dialog.ShowModal() == wx.ID_OK:
404                dialog.Destroy() 
405       
[6c0568b]406       
[1bf33c1]407    def onSectorQ(self, event):
408        """
[6c0568b]409            Perform sector averaging on Q and draw sector slicer
[1bf33c1]410        """
[ef0c170]411        from SectorSlicer import SectorInteractor
[1bf33c1]412        self.onClearSlicer(event)
[d468daa]413        wx.PostEvent(self, InternalEvent(slicer= SectorInteractor))
[1bf33c1]414       
415    def onSectorPhi(self, event):
416        """
[6c0568b]417            Perform sector averaging on Phi and draw annulus slicer
[1bf33c1]418        """
[ef0c170]419        from AnnulusSlicer import AnnulusInteractor
[1bf33c1]420        self.onClearSlicer(event)
[d468daa]421        wx.PostEvent(self, InternalEvent(slicer= AnnulusInteractor))
[1bf33c1]422       
[7ab9241]423    def onBoxSum(self,event):
424        from boxSum import BoxSum
425        self.onClearSlicer(event)
[6c0568b]426                   
[54cc36a]427        self.slicer_z += 1
428        self.slicer =  BoxSum(self, self.subplot, zorder=self.slicer_z)
[6c0568b]429       
[54cc36a]430        self.subplot.set_ylim(self.data2D.ymin, self.data2D.ymax)
431        self.subplot.set_xlim(self.data2D.xmin, self.data2D.xmax)
432       
433        self.update()
434        self.slicer.update()
[6c0568b]435        ## Value used to initially set the slicer panel
436        type = self.slicer.__class__.__name__
437        params = self.slicer.get_params()
438        ## Create a new panel to display results of summation of Data2D
[8a7a21b]439        from slicerpanel import SlicerPanel
[6c0568b]440        new_panel = SlicerPanel(parent= self.parent, id= -1,
441                                    base= self, type= type,
442                                    params= params, style= wx.RAISED_BORDER)
443       
444        new_panel.window_caption=self.slicer.__class__.__name__+" "+ str(self.data2D.name)
445        new_panel.window_name = self.slicer.__class__.__name__+" "+ str(self.data2D.name)
446        ## Store a reference of the new created panel
[54cc36a]447        self.panel_slicer= new_panel
[6c0568b]448        ## save the window_caption of the new panel in the current slicer
[0bd2cd8]449        self.slicer.set_panel_name( name= new_panel.window_caption)
[6c0568b]450        ## post slicer panel to guiframe to display it
[54cc36a]451        from sans.guicomm.events import SlicerPanelEvent
452        wx.PostEvent(self.parent, SlicerPanelEvent (panel= self.panel_slicer))
[6c0568b]453
[8a7a21b]454       
455    def onBoxavgX(self,event):
[6c0568b]456        """
457            Perform 2D data averaging on Qx
458            Create a new slicer .
459            @param event: wx.menu event
460        """
[8a7a21b]461        from boxSlicer import BoxInteractorX
[38224f10]462        self.onClearSlicer(event)
[d468daa]463        wx.PostEvent(self, InternalEvent(slicer= BoxInteractorX))
464       
465       
[8a7a21b]466    def onBoxavgY(self,event):
[6c0568b]467        """
468            Perform 2D data averaging on Qy
469            Create a new slicer .
470            @param event: wx.menu event
471        """
[8a7a21b]472        from boxSlicer import BoxInteractorY
473        self.onClearSlicer(event)
[d468daa]474        wx.PostEvent(self, InternalEvent(slicer= BoxInteractorY))
[6c0568b]475       
[b319def8]476       
[1bf33c1]477    def onClearSlicer(self, event):
478        """
479            Clear the slicer on the plot
480        """
481        if not self.slicer==None:
482            self.slicer.clear()
483            self.subplot.figure.canvas.draw()
484            self.slicer = None
485       
486            # Post slicer None event
487            event = self._getEmptySlicerEvent()
[d468daa]488            wx.PostEvent(self, event)
[1bf33c1]489         
[6c0568b]490   
[1bf33c1]491    def _onToggleScale(self, event):
492        """
493            toggle pixel scale and replot image
494        """
495        if self.scale == 'log':
496            self.scale = 'linear'
497        else:
498            self.scale = 'log'
[6c0568b]499        self.image(self.data2D.data,self.xmin_2D,self.xmax_2D,self.ymin_2D,
[1bf33c1]500                   self.ymax_2D,self.zmin_2D ,self.zmax_2D )
501        wx.PostEvent(self.parent, StatusEvent(status="Image is in %s scale"%self.scale))
502       
[0f6d05f8]503        """     
504            #TODO: this name should be changed to something more appropriate
505            # Don't forget that changing this name will mean changing code
506            # in plotting.py
507             
508            # Update the plottable with the new data
509           
510            #TODO: we should have a method to do this,
511            #      something along the lines of:
512            #      plottable1.update_data_from_plottable(plottable2)
513           
514            self.plots[event.plot.name].xmin = event.plot.xmin
515            self.plots[event.plot.name].xmax = event.plot.xmax
516            self.plots[event.plot.name].ymin = event.plot.ymin
517            self.plots[event.plot.name].ymax = event.plot.ymax
518            self.plots[event.plot.name].data = event.plot.data
519            self.plots[event.plot.name].err_data = event.plot.err_data
520        """
Note: See TracBrowser for help on using the repository browser.