source: sasview/guiframe/local_perspectives/plotting/Plotter2D.py @ 247cb58

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 247cb58 was 20b6760, checked in by Jae Cho <jhjcho@…>, 15 years ago

Added qx qy data as 2D input parameters

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