Changeset 1153


Ignore:
Timestamp:
08/21/06 10:48:45 (18 years ago)
Author:
mar637
Message:

lots of changes to support soft refresh, for things like text overlays, linecatlogs etc. reworked plot_lines to to auto-peak detection. added forwarding functions to matplotlib.axes. drawing functions

Location:
trunk/python
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asaplotbase.py

    r1147 r1153  
    454454        if what[-6:] == 'colour': what = what[:-6] + 'color'
    455455
    456         newargs = {}
    457 
    458         for k, v in kwargs.iteritems():
    459             k = k.lower()
    460             if k == 'colour': k = 'color'
    461             newargs[k] = v
    462 
    463         getattr(self.axes, "set_%s"%what)(*args, **newargs)
    464 
    465         self.show()
     456        key = "colour"
     457        if kwargs.has_key(key):
     458            val = kwargs.pop(key)
     459            kwargs["color"] = val
     460
     461        getattr(self.axes, "set_%s"%what)(*args, **kwargs)
     462
     463        self.show(hardrefresh=False)
    466464
    467465
     
    485483
    486484        getattr(self.figure, "set_%s"%what)(*args, **newargs)
    487         self.show()
     485        self.show(hardrefresh=False)
    488486
    489487
     
    552550                    self.attributes[k] = v
    553551
    554         if redraw: self.show()
     552        if redraw: self.show(hardrefresh=False)
    555553
    556554
     
    613611            for i in range(nplots):
    614612                self.subplots.append({})
    615                 self.subplots[i]['axes'] = self.figure.add_subplot(rows,
     613                self.subplots[i]['lines'] = []
     614                if not ganged:
     615                    self.subplots[i]['axes'] = self.figure.add_subplot(rows,
    616616                                                cols, i+1)
    617                 self.subplots[i]['lines'] = []
    618 
    619                 if ganged:
     617                else:
     618                    if i == 0:
     619                        self.subplots[i]['axes'] = self.figure.add_subplot(rows,
     620                                                cols, i+1)
     621                    else:
     622                        self.subplots[i]['axes'] = self.figure.add_subplot(rows,
     623                                                cols, i+1,
     624                                                sharex=self.subplots[0]['axes'],
     625                                                sharey=self.subplots[0]['axes'])
    620626                    # Suppress tick labelling for interior subplots.
    621627                    if i <= (rows-1)*cols - 1:
     
    623629                            # Suppress x-labels for frames width
    624630                            # adjacent frames
    625                             self.subplots[i]['axes'].xaxis.set_major_locator(NullLocator())
     631                            for tick in self.subplots[i]['axes'].xaxis.majorTicks:
     632                                tick.label1On = False
    626633                            self.subplots[i]['axes'].xaxis.label.set_visible(False)
    627634                    if i%cols:
     
    631638                        self.subplots[i]['axes'].yaxis.label.set_visible(False)
    632639                    # disable the first tick of [1:ncol-1] of the last row
    633                     if (nplots-cols) < i <= nplots-1:
    634                         self.subplots[i]['axes'].xaxis.set_major_formatter(MyFormatter())
     640                    #if i+1 < nplots:
     641                    #    self.subplots[i]['axes'].xaxis.majorTicks[0].label1On = False
    635642                self.rows = rows
    636643                self.cols = cols
    637644            self.subplot(0)
    638645
     646    def tidy(self):
     647        # this needs to be exceuted after the first "refresh"
     648        nplots = len(self.subplots)
     649        if nplots == 1: return
     650        for i in xrange(nplots):
     651            ax = self.subplots[i]['axes']
     652            if i%self.cols:
     653                ax.xaxis.majorTicks[0].label1On = False
     654            else:
     655                if i != 0:
     656                    ax.yaxis.majorTicks[-1].label1On = False
     657
     658
    639659    def set_title(self, title=None):
    640660        """
     
    648668
    649669
    650     def show(self):
     670    def show(self, hardrefresh=True):
    651671        """
    652672        Show graphics dependent on the current buffering state.
    653673        """
     674        if not hardrefresh: return
    654675        if not self.buffering:
    655676            if self.loc is not None:
  • trunk/python/asaplotgui.py

    r1084 r1153  
    2626        v = vars()
    2727        del v['self']
    28        
     28
    2929        asaplotbase.__init__(self, **v)
    3030        self.window = Tk.Tk()
     
    3838        # Simply instantiating this is enough to get a working toolbar.
    3939        self.figmgr = FigureManagerTkAgg(self.canvas, 1, self.window)
    40         self.window.wm_title('ASAPlot graphics window')
     40        self.window.wm_title('ASAP Plotter - Tk')
    4141
    4242        self.events = {'button_press':None,
     
    4444                       'motion_notify':None}
    4545
    46         matplotlib.interactive = True
    47         self.buffering = buffering
     46        matplotlib.rcParams["interactive"] = True
     47        #self.buffering = buffering
    4848
    4949        self.canvas.show()
     
    177177
    178178
    179     def show(self):
     179    def show(self, hardrefresh=True):
    180180        """
    181181        Show graphics dependent on the current buffering state.
    182182        """
    183183        if not self.buffering:
    184             asaplotbase.show(self)
     184            if hardrefresh:
     185                asaplotbase.show(self)
    185186            self.window.wm_deiconify()
    186             self.canvas.draw()
     187            self.canvas.show()
    187188
    188189    def terminate(self):
  • trunk/python/asaplotgui_gtk.py

    r1089 r1153  
    88import matplotlib
    99from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
     10from matplotlib.backends.backend_gtkagg import FigureManagerGTKAgg
    1011matplotlib.use("GTkAgg")
    1112matplotlib.rcParams['toolbar'] = 'toolbar2'
     13from matplotlib.backends.backend_gtk import NavigationToolbar2GTK as NavigationToolbar
    1214
    1315class asaplotgui(asaplotbase):
     
    2729
    2830        asaplotbase.__init__(self, **v)
    29         self.window = gtk.Window()
     31        matplotlib.interactive = True
     32        self.canvas = FigureCanvas(self.figure)
     33        # Simply instantiating this is enough to get a working toolbar.
     34        self.figmgr = FigureManagerGTKAgg(self.canvas, 1)
    3035        def dest_callback(val):
    3136            self.is_dead = True
    32             self.window.destroy()
     37            self.figmgr.window.destroy()
     38        self.window = self.figmgr.window
    3339        self.window.connect("destroy", dest_callback )
    34         self.window.set_default_size(800,600)
    35         self.subwin = gtk.ScrolledWindow()
    36         self.window.add(self.subwin)
    37         self.subwin.set_border_width(10)
    38         self.canvas = FigureCanvas(self.figure)
    39         # Simply instantiating this is enough to get a working toolbar.
    40         self.figmgr = None#FigureManagerTkAgg(self.canvas, 1, self.window)
    41         self.window.set_title('ASAPlot graphics window')
    42 
     40        self.window.set_title('ASAP Plotter - GTK')
    4341        self.events = {'button_press':None,
    4442                       'button_release':None,
    4543                       'motion_notify':None}
    4644
    47         self.subwin.add_with_viewport(self.canvas)
    48         matplotlib.interactive = True
    4945        self.buffering = buffering
    50         self.canvas.set_size_request(800,600)
    51 
    52         self.canvas.show()
     46        matplotlib.rcParams['interactive'] = True
     47        #self.canvas.set_size_request(800,600)
     48
     49        #self.canvas.show()
    5350
    5451    def map(self):
     
    180177
    181178
    182     def show(self):
     179    def show(self, hardrefresh=True):
    183180        """
    184181        Show graphics dependent on the current buffering state.
    185182        """
    186183        if not self.buffering:
    187             asaplotbase.show(self)
     184            if hardrefresh:
     185                asaplotbase.show(self, hardrefresh)
    188186            self.window.deiconify()
    189187            self.canvas.draw()
  • trunk/python/asapplotter.py

    r1148 r1153  
    11from asap import rcParams, print_log, selector
    22from asap import NUM
     3import matplotlib.axes
    34
    45class asapplotter:
     
    9596            self._plotter.set_limits(ylim=self._minmaxy)
    9697        self._plotter.release()
     98        self._plotter.tidy()
     99        self._plotter.show(hardrefresh=False)
    97100        print_log()
    98101        return
     102
     103
     104    # forwards to matplotlib axes
     105    def text(self, *args, **kwargs):
     106        self._axes_callback("text", *args, **kwargs)
     107    text. __doc__ = matplotlib.axes.Axes.text.__doc__
     108    def arrow(self, *args, **kwargs):
     109        self._axes_callback("arrow", *args, **kwargs)
     110    arrow. __doc__ = matplotlib.axes.Axes.arrow.__doc__
     111    def axvline(self, *args, **kwargs):
     112        self._axes_callback("axvline", *args, **kwargs)
     113    axvline. __doc__ = matplotlib.axes.Axes.axvline.__doc__
     114    def axhline(self, *args, **kwargs):
     115        self._axes_callback("axhline", *args, **kwargs)
     116    axhline. __doc__ = matplotlib.axes.Axes.axhline.__doc__
     117    def axvspan(self, *args, **kwargs):
     118        self._axes_callback("axvspan", *args, **kwargs)
     119        # hack to preventy mpl from redrawing the patch
     120        # it seem to convert the patch into lines on every draw.
     121        # This doesn't happen in a test script???
     122        del self._plotter.axes.patches[-1]
     123    axvspan. __doc__ = matplotlib.axes.Axes.axvspan.__doc__
     124    def axhspan(self, *args, **kwargs):
     125        self._axes_callback("ahvspan", *args, **kwargs)
     126        # hack to preventy mpl from redrawing the patch
     127        # it seem to convert the patch into lines on every draw.
     128        # This doesn't happen in a test script???
     129        del self._plotter.axes.patches[-1]
     130    axhspan. __doc__ = matplotlib.axes.Axes.axhspan.__doc__
     131
     132    def _axes_callback(self, axesfunc, *args, **kwargs):
     133        panel = 0
     134        if kwargs.has_key("panel"):
     135            panel = kwargs.pop("panel")
     136        coords = None
     137        if kwargs.has_key("coords"):
     138            coords = kwargs.pop("coords")
     139            if coords.lower() == 'world':
     140                kwargs["transform"] = self._plotter.axes.transData
     141            elif coords.lower() == 'relative':
     142                kwargs["transform"] = self._plotter.axes.transAxes
     143        self._plotter.subplot(panel)
     144        self._plotter.axes.set_autoscale_on(False)
     145        getattr(self._plotter.axes, axesfunc)(*args, **kwargs)
     146        self._plotter.show(False)
     147        self._plotter.axes.set_autoscale_on(True)
     148    # end matplotlib.axes fowarding functions
    99149
    100150    def set_mode(self, stacking=None, panelling=None):
     
    357407        if self._data: self.plot(self._data)
    358408
    359     def plot_lines(self, linecat=None, offset=0.0, peak=5.0, rotate=0.0,
     409    def plot_lines(self, linecat=None, offset=0.0, deltachan=10, rotate=0.0,
    360410                   location=None):
    361411        """
     
    365415        if not isinstance(linecat, linecatalog): return
    366416        if not self._data.get_unit().endswith("GHz"): return
    367         self._plotter.hold()
     417        #self._plotter.hold()
     418        from matplotlib.numerix import ma
    368419        for j in range(len(self._plotter.subplots)):
    369420            self._plotter.subplot(j)
    370421            lims = self._plotter.axes.get_xlim()
    371             for i in range(linecat.nrow()):
    372                 freq = linecat.get_frequency(i)/1000.0 + offset
     422            for row in range(linecat.nrow()):
     423                freq = linecat.get_frequency(row)/1000.0 + offset
    373424                if lims[0] < freq < lims[1]:
    374425                    if location is None:
    375426                        loc = 'bottom'
    376                         if i%2: loc='top'
     427                        if row%2: loc='top'
    377428                    else: loc = location
    378                     self._plotter.vline_with_label(freq, peak, linecat.get_name(i),
     429                    maxys = []
     430                    for line in self._plotter.axes.lines:
     431                        v = line._x
     432                        asc = v[0] < v[-1]
     433
     434                        idx = None
     435                        if not asc:
     436                            if v[len(v)-1] <= freq <= v[0]:
     437                                i = len(v)-1
     438                                while i>=0 and v[i] < freq:
     439                                    idx = i
     440                                    i-=1
     441                        else:
     442                           if v[0] <= freq <= v[len(v)-1]:
     443                                i = 0
     444                                while  i<len(v) and v[i] < freq:
     445                                    idx = i
     446                                    i+=1
     447                        if idx is not None:
     448                            lower = idx - deltachan
     449                            upper = idx + deltachan
     450                            if lower < 0: lower = 0
     451                            if upper > len(v): upper = len(v)
     452                            s = slice(lower, upper)
     453                            y = line._y_orig[s]
     454                            maxys.append(ma.maximum(y))
     455                    peak = max(maxys)
     456                    self._plotter.vline_with_label(freq, peak, linecat.get_name(row),
    379457                                             location=loc, rotate=rotate)
    380         self._plotter.release()
     458        #        self._plotter.release()
     459        self._plotter.show(hardrefresh=False)
     460
    381461
    382462    def save(self, filename=None, orientation=None, dpi=None):
     
    619699
    620700    def _get_label(self, scan, row, mode, userlabel=None):
     701        if isinstance(userlabel, list) and len(userlabel) == 0:
     702            userlabel = " "
    621703        pms = dict(zip(self._selection.get_pols(),self._selection.get_poltypes()))
    622704        if len(pms):
     
    630712             't': scan._gettime(row) }
    631713        return userlabel or d[mode]
     714
  • trunk/python/linecatalog.py

    r1150 r1153  
    103103              row:        the row to retrieve
    104104        """
    105         freq = lcbase.get_frequency(row)
    106         name = lcbase.get_name(row)
     105        freq = lcbase.get_frequency(self, row)
     106        name = lcbase.get_name(self, row)
    107107        return (freq, name)
     108
     109    def __len__(self):
     110        return self.nrow()
     111
     112    def __getitem__(self, k):
     113        if k < 0: k = self.nrow()-k
     114        return self.get_row(k)
     115
     116
     117
     118
     119
  • trunk/python/scantable.py

    r1145 r1153  
    55from asap import selector
    66from asap import NUM
     7from asap import linecatalog
    78
    89class scantable(Scantable):
     
    707708        E.g. 'freqs=[1e9, 2e9]'  would mean IF 0 gets restfreq 1e9 and
    708709        IF 1 gets restfreq 2e9.
    709         You can also specify the frequencies via known line names
    710         from the built-in Lovas table.
     710        You can also specify the frequencies via a linecatalog/
     711
    711712        Parameters:
    712713            freqs:   list of rest frequency values or string idenitfiers
     
    738739        t = type(freqs)
    739740        if isinstance(freqs, int) or isinstance(freqs, float):
    740             self._setrestfreqs(freqs, unit)
     741            self._setrestfreqs(freqs, "",unit)
    741742        elif isinstance(freqs, list) or isinstance(freqs, tuple):
    742743            if isinstance(freqs[-1], int) or isinstance(freqs[-1], float):
     
    746747                    sel.set_ifs([i])
    747748                    self._setselection(sel)
    748                     self._setrestfreqs(freqs[i], unit)
     749                    self._setrestfreqs(freqs[i], "",unit)
    749750                self._setselection(savesel)
    750751            elif isinstance(freqs[-1], str):
    751752                # not yet implemented
    752753                pass
     754        elif isinstance(freqs, linecatalog):
     755            sel = selector()
     756            savesel = self._getselection()
     757            for i in xrange(freqs.nrow()):
     758                sel.set_ifs([i])
     759                self._setselection(sel)
     760                self._setrestfreqs(freqs.get_frequency(i),
     761                                   freqs.get_name(i), "MHz")
     762                # ensure that we are not iterating past nIF
     763                if i == self.nif()-1: break
     764            self._setselection(savesel)
    753765        else:
    754766            return
Note: See TracChangeset for help on using the changeset viewer.