Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/customgui_base.py

    r2697 r2799  
    11import os
     2import weakref
    23import matplotlib, numpy
    34from asap.logging import asaplog, asaplog_post_dec
     
    1314class CustomToolbarCommon:
    1415    def __init__(self,parent):
    15         self.plotter = parent
     16        self.plotter = weakref.ref(parent)
    1617        #self.figmgr=self.plotter._plotter.figmgr
     18
     19    def _get_plotter(self):
     20        # check for the validity of the plotter and
     21        # return the plotter class instance if its valid.
     22        if self.plotter() is None:
     23            raise RuntimeError, "Internal Error. The plotter has been destroyed."
     24        else:
     25            return self.plotter()
    1726
    1827    ### select the nearest spectrum in pick radius
     
    2938        if event.button != 1:
    3039            return
     40
     41        # check for the validity of plotter and get the plotter
     42        theplotter = self._get_plotter()
    3143
    3244        xclick = event.xdata
     
    6274        del pind, inds, xlin, ylin
    6375        # Spectra are Picked
    64         theplot = self.plotter._plotter
     76        theplot = theplotter._plotter
    6577        thetoolbar = self.figmgr.toolbar
    6678        thecanvas = self.figmgr.canvas
     
    154166            return
    155167
     168        # check for the validity of plotter and get the plotter
     169        theplotter = self._get_plotter()
     170
    156171        self._thisregion = {'axes': event.inaxes,'xs': event.x,
    157172                            'worldx': [event.xdata,event.xdata],
     
    160175        self.xdataold = event.xdata
    161176
    162         self.plotter._plotter.register('button_press',None)
    163         self.plotter._plotter.register('motion_notify', self._xspan_draw)
    164         self.plotter._plotter.register('button_press', self._xspan_end)
     177        theplotter._plotter.register('button_press',None)
     178        theplotter._plotter.register('motion_notify', self._xspan_draw)
     179        theplotter._plotter.register('button_press', self._xspan_end)
    165180
    166181    def _xspan_draw(self,event):
     
    203218            xdataend = self.xdataold
    204219
     220        # check for the validity of plotter and get the plotter
     221        theplotter = self._get_plotter()
     222
    205223        self._thisregion['worldx'][1] = xdataend
    206224        # print statistics of spectra in subplot
     
    208226       
    209227        # release event
    210         self.plotter._plotter.register('button_press',None)
    211         self.plotter._plotter.register('motion_notify',None)
     228        theplotter._plotter.register('button_press',None)
     229        theplotter._plotter.register('motion_notify',None)
    212230        # Clear up region selection
    213231        self._thisregion = None
     
    215233        self.xold = None
    216234        # finally recover region selection event
    217         self.plotter._plotter.register('button_press',self._single_mask)
     235        theplotter._plotter.register('button_press',self._single_mask)
    218236
    219237    def _subplot_stats(self,selection):
     
    321339    ### actual plotting of the new page
    322340    def _new_page(self,goback=False):
     341        # check for the validity of plotter and get the plotter
     342        theplotter = self._get_plotter()
     343
    323344        top = None
    324         header = self.plotter._headtext
     345        header = theplotter._headtext
    325346        reset = False
    326347        doheader = (isinstance(header['textobj'],list) and \
    327348                    len(header['textobj']) > 0)
    328349        if doheader:
    329             top = self.plotter._plotter.figure.subplotpars.top
     350            top = theplotter._plotter.figure.subplotpars.top
    330351            fontsize = header['textobj'][0].get_fontproperties().get_size()
    331         if self.plotter._startrow <= 0:
     352        if theplotter._startrow <= 0:
    332353            msg = "The page counter is reset due to chages of plot settings. "
    333354            msg += "Plotting from the first page."
     
    342363                if header.has_key('selstr'):
    343364                    selstr = header['selstr']
    344             self.plotter._reset_header()
    345 
    346         self.plotter._plotter.hold()
     365            theplotter._reset_header()
     366
     367        theplotter._plotter.hold()
    347368        if goback:
    348369            self._set_prevpage_counter()
    349         #self.plotter._plotter.clear()
    350         self.plotter._plot(self.plotter._data)
     370        #theplotter._plotter.clear()
     371        theplotter._plot(theplotter._data)
    351372        pagenum = self._get_pagenum()
    352373        self.set_pagecounter(pagenum)
     
    354375        #if header['textobj']:
    355376        if doheader and pagenum == 1:
    356             if top and top != self.plotter._margins[3]:
     377            if top and top != theplotter._margins[3]:
    357378                # work around for sdplot in CASA. complete checking in future?
    358                 self.plotter._plotter.figure.subplots_adjust(top=top)
     379                theplotter._plotter.figure.subplots_adjust(top=top)
    359380            if reset:
    360                 self.plotter.print_header(plot=True,fontsize=fontsize,selstr=selstr, extrastr=extrastr)
     381                theplotter.print_header(plot=True,fontsize=fontsize,selstr=selstr, extrastr=extrastr)
    361382            else:
    362                 self.plotter._header_plot(header['string'],fontsize=fontsize)
    363         self.plotter._plotter.release()
    364         self.plotter._plotter.tidy()
    365         self.plotter._plotter.show(hardrefresh=False)
     383                theplotter._header_plot(header['string'],fontsize=fontsize)
     384        theplotter._plotter.release()
     385        theplotter._plotter.tidy()
     386        theplotter._plotter.show(hardrefresh=False)
    366387        del top
    367388
    368389    ### calculate the panel ID and start row to plot the previous page
    369390    def _set_prevpage_counter(self):
     391        # check for the validity of plotter and get the plotter
     392        theplotter = self._get_plotter()
     393
    370394        # set row and panel counters to those of the 1st panel of previous page
    371395        maxpanel = 16
    372396        # the ID of the last panel in current plot
    373         lastpanel = self.plotter._ipanel
     397        lastpanel = theplotter._ipanel
    374398        # the number of current subplots
    375         currpnum = len(self.plotter._plotter.subplots)
     399        currpnum = len(theplotter._plotter.subplots)
    376400        # the nuber of previous subplots
    377401        prevpnum = None
    378         if self.plotter._rows and self.plotter._cols:
     402        if theplotter._rows and theplotter._cols:
    379403            # when user set layout
    380             prevpnum = self.plotter._rows*self.plotter._cols
     404            prevpnum = theplotter._rows*theplotter._cols
    381405        else:
    382406            # no user specification
     
    385409        start_ipanel = max(lastpanel-currpnum-prevpnum+1, 0)
    386410        # set the pannel ID of the last panel of prev-prev page
    387         self.plotter._ipanel = start_ipanel-1
    388         if self.plotter._panelling == 'r':
    389             self.plotter._startrow = start_ipanel
     411        theplotter._ipanel = start_ipanel-1
     412        if theplotter._panelling == 'r':
     413            theplotter._startrow = start_ipanel
    390414        else:
    391415            # the start row number of the next panel
    392             self.plotter._startrow = self.plotter._panelrows[start_ipanel]
     416            theplotter._startrow = theplotter._panelrows[start_ipanel]
    393417        del lastpanel,currpnum,prevpnum,start_ipanel
    394418
     
    405429
    406430    def _get_pagenum(self):
     431        # check for the validity of plotter and get the plotter
     432        theplotter = self._get_plotter()
     433       
    407434        # get the ID of last panel in the current page
    408         idlastpanel = self.plotter._ipanel
     435        idlastpanel = theplotter._ipanel
    409436        # max panels in a page
    410         ppp = self.plotter._plotter.rows*self.plotter._plotter.cols
     437        ppp = theplotter._plotter.rows*theplotter._plotter.cols
    411438        return int(idlastpanel/ppp)+1
    412439
     
    683710class CustomFlagToolbarCommon:
    684711    def __init__(self,parent):
    685         self.plotter=parent
     712        self.plotter=weakref.ref(parent)
    686713        #self.figmgr=self.plotter._plotter.figmgr
    687714        self._selregions = {}
     
    691718        self.xdataold=None
    692719
     720    def _get_plotter(self):
     721        # check for the validity of the plotter and
     722        # return the plotter class instance if its valid.
     723        if self.plotter() is None:
     724            raise RuntimeError, "Internal Error. The plotter has been destroyed."
     725        else:
     726            return self.plotter()
     727
    693728    ### select the nearest spectrum in pick radius
    694729    ###    and display spectral value on the toolbar.
     
    704739        if event.button != 1:
    705740            return
     741
     742        # check for the validity of plotter and get the plotter
     743        theplotter = self._get_plotter()
    706744
    707745        xclick = event.xdata
     
    737775        del pind, inds, xlin, ylin
    738776        # Spectra are Picked
    739         theplot = self.plotter._plotter
     777        theplot = theplotter._plotter
    740778        thetoolbar = self.figmgr.toolbar
    741779        thecanvas = self.figmgr.canvas
     
    819857        if event.button != 1 or event.inaxes == None:
    820858            return
     859        # check for the validity of plotter and get the plotter
     860        theplotter = self._get_plotter()
    821861        # this row resolution assumes row panelling
    822862        irow = int(self._getrownum(event.inaxes))
     
    829869        self._thisregion = {'axes': event.inaxes,'xs': event.x,
    830870                            'worldx': [event.xdata,event.xdata]}
    831         self.plotter._plotter.register('button_press',None)
     871        theplotter._plotter.register('button_press',None)
    832872        self.xold = event.x
    833873        self.xdataold = event.xdata
    834         self.plotter._plotter.register('motion_notify', self._xspan_draw)
    835         self.plotter._plotter.register('button_press', self._xspan_end)
     874        theplotter._plotter.register('motion_notify', self._xspan_draw)
     875        theplotter._plotter.register('button_press', self._xspan_end)
    836876
    837877    def _xspan_draw(self,event):
     
    882922        self._thisregion['axes'].set_xlim(axlimx)
    883923       
    884         self.plotter._plotter.canvas.draw()
     924        # check for the validity of plotter and get the plotter
     925        theplotter = self._get_plotter()
     926
     927        theplotter._plotter.canvas.draw()
    885928        self._polygons.append(pregion)
    886929        srow = self._getrownum(self._thisregion['axes'])
     
    895938
    896939        # release event
    897         self.plotter._plotter.register('button_press',None)
    898         self.plotter._plotter.register('motion_notify',None)
     940        theplotter._plotter.register('button_press',None)
     941        theplotter._plotter.register('motion_notify',None)
    899942        # Clear up region selection
    900943        self._thisregion = None
     
    902945        self.xold = None
    903946        # finally recover region selection event
    904         self.plotter._plotter.register('button_press',self._add_region)
     947        theplotter._plotter.register('button_press',self._add_region)
    905948
    906949    ### add panels to selections
     
    911954        if event.button != 1 or event.inaxes == None:
    912955            return
     956        # check for the validity of plotter and get the plotter
     957        theplotter = self._get_plotter()
     958
    913959        selax = event.inaxes
    914960        # this row resolution assumes row panelling
     
    919965        shadow = Rectangle((0,0),1,1,facecolor='0.7',transform=selax.transAxes,visible=True)
    920966        self._polygons.append(selax.add_patch(shadow))
    921         #self.plotter._plotter.show(False)
    922         self.plotter._plotter.canvas.draw()
     967        #theplotter._plotter.show(False)
     968        theplotter._plotter.canvas.draw()
    923969        asaplog.push("row "+str(irow)+" is selected")
    924970        ## check for region selection of the spectra and overwrite it.
     
    9561002            asaplog.push("Invalid panel specification")
    9571003            asaplog.post('ERROR')
    958         strow = self._getrownum(self.plotter._plotter.subplots[0]['axes'])
    959         enrow = self._getrownum(self.plotter._plotter.subplots[-1]['axes'])
     1004
     1005        # check for the validity of plotter and get the plotter
     1006        theplotter = self._get_plotter()
     1007
     1008        strow = self._getrownum(theplotter._plotter.subplots[0]['axes'])
     1009        enrow = self._getrownum(theplotter._plotter.subplots[-1]['axes'])
    9601010        for irow in range(int(strow),int(enrow)+1):
    9611011            if regions.has_key(str(irow)):
    962                 ax = self.plotter._plotter.subplots[irow - int(strow)]['axes']
     1012                ax = theplotter._plotter.subplots[irow - int(strow)]['axes']
    9631013                mlist = regions.pop(str(irow))
    9641014                # WORKAROUND for the issue axvspan started to reset xlim.
     
    9701020                del ax,mlist,axlimx
    9711021            if irow in panels:
    972                 ax = self.plotter._plotter.subplots[irow - int(strow)]['axes']
     1022                ax = theplotter._plotter.subplots[irow - int(strow)]['axes']
    9731023                shadow = Rectangle((0,0),1,1,facecolor='0.7',
    9741024                                   transform=ax.transAxes,visible=True)
    9751025                self._polygons.append(ax.add_patch(shadow))
    9761026                del ax,shadow
    977         self.plotter._plotter.canvas.draw()
     1027        theplotter._plotter.canvas.draw()
    9781028        del regions,panels,strow,enrow
    9791029
     
    9831033            for shadow in self._polygons:
    9841034                shadow.remove()
    985             if refresh: self.plotter._plotter.canvas.draw()
     1035            if refresh:
     1036                # check for the validity of plotter and get the plotter
     1037                theplotter = self._get_plotter()
     1038                theplotter._plotter.canvas.draw()
    9861039        self._polygons = []
    9871040
     
    10071060            asaplog.post('WARN')
    10081061            return
     1062
    10091063        self._pause_buttons(operation="start",msg="Flagging data...")
    10101064        self._flag_operation(rows=self._selpanels,
     
    10151069        asaplog.push(sout)
    10161070        del sout
    1017         self.plotter._ismodified = True
     1071        # check for the validity of plotter and get the plotter
     1072        theplotter = self._get_plotter()
     1073
     1074        theplotter._ismodified = True
    10181075        self._clearup_selections(refresh=False)
    10191076        self._plot_page(pagemode="current")
     
    10361093        asaplog.push(sout)
    10371094        del sout
    1038         self.plotter._ismodified = True
     1095
     1096        # check for the validity of plotter and get the plotter
     1097        theplotter = self._get_plotter()
     1098        theplotter._ismodified = True
    10391099        self._clearup_selections(refresh=False)
    10401100        self._plot_page(pagemode="current")
     
    10441104    @asaplog_post_dec
    10451105    def _flag_operation(self,rows=None,regions=None,unflag=False):
    1046         scan = self.plotter._data
     1106        # check for the validity of plotter and get the plotter
     1107        theplotter = self._get_plotter()
     1108
     1109        scan = theplotter._data
    10471110        if not scan:
    10481111            asaplog.post()
     
    10791142    @asaplog_post_dec
    10801143    def _selected_stats(self,rows=None,regions=None):
    1081         scan = self.plotter._data
     1144        # check for the validity of plotter and get the plotter
     1145        theplotter = self._get_plotter()
     1146
     1147        scan = theplotter._data
    10821148        if not scan:
    10831149            asaplog.post()
     
    11641230    ### actual plotting of the new page
    11651231    def _plot_page(self,pagemode="next"):
    1166         if self.plotter._startrow <= 0:
     1232        # check for the validity of plotter and get the plotter
     1233        theplotter = self._get_plotter()
     1234        if theplotter._startrow <= 0:
    11671235            msg = "The page counter is reset due to chages of plot settings. "
    11681236            msg += "Plotting from the first page."
     
    11721240            goback = False
    11731241
    1174         self.plotter._plotter.hold()
    1175         #self.plotter._plotter.legend(1)
     1242        theplotter._plotter.hold()
     1243        #theplotter._plotter.legend(1)
    11761244        self._set_plot_counter(pagemode)
    1177         self.plotter._plot(self.plotter._data)
     1245        theplotter._plot(theplotter._data)
    11781246        self.set_pagecounter(self._get_pagenum())
    1179         self.plotter._plotter.release()
    1180         self.plotter._plotter.tidy()
    1181         self.plotter._plotter.show(hardrefresh=False)
     1247        theplotter._plotter.release()
     1248        theplotter._plotter.tidy()
     1249        theplotter._plotter.show(hardrefresh=False)
    11821250
    11831251    ### calculate the panel ID and start row to plot a page
     
    11941262            # nothing necessary to plot the next page
    11951263            return
     1264
     1265        # check for the validity of plotter and get the plotter
     1266        theplotter = self._get_plotter()
     1267
    11961268        # set row and panel counters to those of the 1st panel of previous page
    11971269        maxpanel = 25
    11981270        # the ID of the last panel in current plot
    1199         lastpanel = self.plotter._ipanel
     1271        lastpanel = theplotter._ipanel
    12001272        # the number of current subplots
    1201         currpnum = len(self.plotter._plotter.subplots)
     1273        currpnum = len(theplotter._plotter.subplots)
    12021274
    12031275        # the nuber of previous subplots
     
    12081280            ## previous page
    12091281            prevpnum = None
    1210             if self.plotter._rows and self.plotter._cols:
     1282            if theplotter._rows and theplotter._cols:
    12111283                # when user set layout
    1212                 prevpnum = self.plotter._rows*self.plotter._cols
     1284                prevpnum = theplotter._rows*theplotter._cols
    12131285            else:
    12141286                # no user specification
     
    12181290
    12191291        # set the pannel ID of the last panel of the prev(-prev) page
    1220         self.plotter._ipanel = start_ipanel-1
    1221         if self.plotter._panelling == 'r':
    1222             self.plotter._startrow = start_ipanel
     1292        theplotter._ipanel = start_ipanel-1
     1293        if theplotter._panelling == 'r':
     1294            theplotter._startrow = start_ipanel
    12231295        else:
    12241296            # the start row number of the next panel
    1225             self.plotter._startrow = self.plotter._panelrows[start_ipanel]
     1297            theplotter._startrow = theplotter._panelrows[start_ipanel]
    12261298        del lastpanel,currpnum,start_ipanel
    12271299
     
    12381310
    12391311    def _get_pagenum(self):
     1312        # check for the validity of plotter and get the plotter
     1313        theplotter = self._get_plotter()
    12401314        # get the ID of last panel in the current page
    1241         idlastpanel = self.plotter._ipanel
     1315        idlastpanel = theplotter._ipanel
    12421316        # max panels in a page
    1243         ppp = self.plotter._plotter.rows*self.plotter._plotter.cols
     1317        ppp = theplotter._plotter.rows*theplotter._plotter.cols
    12441318        return int(idlastpanel/ppp)+1
    12451319
Note: See TracChangeset for help on using the changeset viewer.