|
From: <lee...@us...> - 2009-08-09 19:25:56
|
Revision: 7439
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7439&view=rev
Author: leejjoon
Date: 2009-08-09 19:25:49 +0000 (Sun, 09 Aug 2009)
Log Message:
-----------
reorganization of AxesImage and BboxImage
Modified Paths:
--------------
trunk/matplotlib/lib/matplotlib/image.py
Modified: trunk/matplotlib/lib/matplotlib/image.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/image.py 2009-08-09 18:50:15 UTC (rev 7438)
+++ trunk/matplotlib/lib/matplotlib/image.py 2009-08-09 19:25:49 UTC (rev 7439)
@@ -26,7 +26,7 @@
from matplotlib.transforms import BboxBase
-class AxesImage(martist.Artist, cm.ScalarMappable):
+class _AxesImageBase(martist.Artist, cm.ScalarMappable):
zorder = 1
# map interpolation strings to module constants
_interpd = {
@@ -62,7 +62,6 @@
norm = None,
interpolation=None,
origin=None,
- extent=None,
filternorm=1,
filterrad=4.0,
resample = False,
@@ -87,7 +86,6 @@
if origin is None: origin = rcParams['image.origin']
self.origin = origin
- self._extent = extent
self.set_filternorm(filternorm)
self.set_filterrad(filterrad)
self._filterrad = filterrad
@@ -126,108 +124,9 @@
self._rgbacache = None
cm.ScalarMappable.changed(self)
-
def make_image(self, magnification=1.0):
- if self._A is None:
- raise RuntimeError('You must first set the image array or the image attribute')
+ raise RuntimeError('The make_image method must be overridden.')
- xmin, xmax, ymin, ymax = self.get_extent()
- dxintv = xmax-xmin
- dyintv = ymax-ymin
-
- # the viewport scale factor
- sx = dxintv/self.axes.viewLim.width
- sy = dyintv/self.axes.viewLim.height
- numrows, numcols = self._A.shape[:2]
- if sx > 2:
- x0 = (self.axes.viewLim.x0-xmin)/dxintv * numcols
- ix0 = max(0, int(x0 - self._filterrad))
- x1 = (self.axes.viewLim.x1-xmin)/dxintv * numcols
- ix1 = min(numcols, int(x1 + self._filterrad))
- xslice = slice(ix0, ix1)
- xmin_old = xmin
- xmin = xmin_old + ix0*dxintv/numcols
- xmax = xmin_old + ix1*dxintv/numcols
- dxintv = xmax - xmin
- sx = dxintv/self.axes.viewLim.width
- else:
- xslice = slice(0, numcols)
-
- if sy > 2:
- y0 = (self.axes.viewLim.y0-ymin)/dyintv * numrows
- iy0 = max(0, int(y0 - self._filterrad))
- y1 = (self.axes.viewLim.y1-ymin)/dyintv * numrows
- iy1 = min(numrows, int(y1 + self._filterrad))
- if self.origin == 'upper':
- yslice = slice(numrows-iy1, numrows-iy0)
- else:
- yslice = slice(iy0, iy1)
- ymin_old = ymin
- ymin = ymin_old + iy0*dyintv/numrows
- ymax = ymin_old + iy1*dyintv/numrows
- dyintv = ymax - ymin
- sy = dyintv/self.axes.viewLim.height
- else:
- yslice = slice(0, numrows)
-
- if xslice != self._oldxslice or yslice != self._oldyslice:
- self._imcache = None
- self._oldxslice = xslice
- self._oldyslice = yslice
-
- if self._imcache is None:
- if self._A.dtype == np.uint8 and len(self._A.shape) == 3:
- im = _image.frombyte(self._A[yslice,xslice,:], 0)
- im.is_grayscale = False
- else:
- if self._rgbacache is None:
- x = self.to_rgba(self._A, self._alpha)
- self._rgbacache = x
- else:
- x = self._rgbacache
- im = _image.fromarray(x[yslice,xslice], 0)
- if len(self._A.shape) == 2:
- im.is_grayscale = self.cmap.is_gray()
- else:
- im.is_grayscale = False
- self._imcache = im
-
- if self.origin=='upper':
- im.flipud_in()
- else:
- im = self._imcache
-
- fc = self.axes.patch.get_facecolor()
- bg = mcolors.colorConverter.to_rgba(fc, 0)
- im.set_bg( *bg)
-
- # image input dimensions
- im.reset_matrix()
- numrows, numcols = im.get_size()
-
- im.set_interpolation(self._interpd[self._interpolation])
-
- im.set_resample(self._resample)
-
- # the viewport translation
- tx = (xmin-self.axes.viewLim.x0)/dxintv * numcols
- ty = (ymin-self.axes.viewLim.y0)/dyintv * numrows
-
- l, b, r, t = self.axes.bbox.extents
- widthDisplay = (round(r) + 0.5) - (round(l) - 0.5)
- heightDisplay = (round(t) + 0.5) - (round(b) - 0.5)
- widthDisplay *= magnification
- heightDisplay *= magnification
- im.apply_translation(tx, ty)
-
- # resize viewport to display
- rx = widthDisplay / numcols
- ry = heightDisplay / numrows
- im.apply_scaling(rx*sx, ry*sy)
- im.resize(int(widthDisplay+0.5), int(heightDisplay+0.5),
- norm=self._filternorm, radius=self._filterrad)
- return im
-
@allow_rasterization
def draw(self, renderer, *args, **kwargs):
if not self.get_visible(): return
@@ -314,20 +213,6 @@
- def set_extent(self, extent):
- """
- extent is data axes (left, right, bottom, top) for making image plots
- """
- self._extent = extent
-
- xmin, xmax, ymin, ymax = extent
- corners = (xmin, ymin), (xmax, ymax)
- self.axes.update_datalim(corners)
- if self.axes._autoscaleXon:
- self.axes.set_xlim((xmin, xmax))
- if self.axes._autoscaleYon:
- self.axes.set_ylim((ymin, ymax))
-
def get_interpolation(self):
"""
Return the interpolation method the image uses when resizing.
@@ -367,19 +252,6 @@
'return the image resample boolean'
return self._resample
- def get_extent(self):
- 'get the image extent: left, right, bottom, top'
- if self._extent is not None:
- return self._extent
- else:
- sz = self.get_size()
- #print 'sz', sz
- numrows, numcols = sz
- if self.origin == 'upper':
- return (-0.5, numcols-0.5, numrows-0.5, -0.5)
- else:
- return (-0.5, numcols-0.5, -0.5, numrows-0.5)
-
def set_filternorm(self, filternorm):
"""
Set whether the resize filter norms the weights -- see
@@ -412,6 +284,182 @@
return self._filterrad
+
+class AxesImage(_AxesImageBase):
+ def __str__(self):
+ return "AxesImage(%g,%g;%gx%g)" % tuple(self.axes.bbox.bounds)
+
+ def __init__(self, ax,
+ cmap = None,
+ norm = None,
+ interpolation=None,
+ origin=None,
+ extent=None,
+ filternorm=1,
+ filterrad=4.0,
+ resample = False,
+ **kwargs
+ ):
+
+ """
+ interpolation and cmap default to their rc settings
+
+ cmap is a colors.Colormap instance
+ norm is a colors.Normalize instance to map luminance to 0-1
+
+ extent is data axes (left, right, bottom, top) for making image plots
+ registered with data plots. Default is to label the pixel
+ centers with the zero-based row and column indices.
+
+ Additional kwargs are matplotlib.artist properties
+
+ """
+
+ self._extent = extent
+
+ _AxesImageBase.__init__(self, ax,
+ cmap = cmap,
+ norm = norm,
+ interpolation=interpolation,
+ origin=origin,
+ filternorm=filternorm,
+ filterrad=filterrad,
+ resample = resample,
+ **kwargs
+ )
+
+
+ def make_image(self, magnification=1.0):
+ if self._A is None:
+ raise RuntimeError('You must first set the image array or the image attribute')
+
+ xmin, xmax, ymin, ymax = self.get_extent()
+ dxintv = xmax-xmin
+ dyintv = ymax-ymin
+
+ # the viewport scale factor
+ sx = dxintv/self.axes.viewLim.width
+ sy = dyintv/self.axes.viewLim.height
+ numrows, numcols = self._A.shape[:2]
+ if sx > 2:
+ x0 = (self.axes.viewLim.x0-xmin)/dxintv * numcols
+ ix0 = max(0, int(x0 - self._filterrad))
+ x1 = (self.axes.viewLim.x1-xmin)/dxintv * numcols
+ ix1 = min(numcols, int(x1 + self._filterrad))
+ xslice = slice(ix0, ix1)
+ xmin_old = xmin
+ xmin = xmin_old + ix0*dxintv/numcols
+ xmax = xmin_old + ix1*dxintv/numcols
+ dxintv = xmax - xmin
+ sx = dxintv/self.axes.viewLim.width
+ else:
+ xslice = slice(0, numcols)
+
+ if sy > 2:
+ y0 = (self.axes.viewLim.y0-ymin)/dyintv * numrows
+ iy0 = max(0, int(y0 - self._filterrad))
+ y1 = (self.axes.viewLim.y1-ymin)/dyintv * numrows
+ iy1 = min(numrows, int(y1 + self._filterrad))
+ if self.origin == 'upper':
+ yslice = slice(numrows-iy1, numrows-iy0)
+ else:
+ yslice = slice(iy0, iy1)
+ ymin_old = ymin
+ ymin = ymin_old + iy0*dyintv/numrows
+ ymax = ymin_old + iy1*dyintv/numrows
+ dyintv = ymax - ymin
+ sy = dyintv/self.axes.viewLim.height
+ else:
+ yslice = slice(0, numrows)
+
+ if xslice != self._oldxslice or yslice != self._oldyslice:
+ self._imcache = None
+ self._oldxslice = xslice
+ self._oldyslice = yslice
+
+ if self._imcache is None:
+ if self._A.dtype == np.uint8 and len(self._A.shape) == 3:
+ im = _image.frombyte(self._A[yslice,xslice,:], 0)
+ im.is_grayscale = False
+ else:
+ if self._rgbacache is None:
+ x = self.to_rgba(self._A, self._alpha)
+ self._rgbacache = x
+ else:
+ x = self._rgbacache
+ im = _image.fromarray(x[yslice,xslice], 0)
+ if len(self._A.shape) == 2:
+ im.is_grayscale = self.cmap.is_gray()
+ else:
+ im.is_grayscale = False
+ self._imcache = im
+
+ if self.origin=='upper':
+ im.flipud_in()
+ else:
+ im = self._imcache
+
+ fc = self.axes.patch.get_facecolor()
+ bg = mcolors.colorConverter.to_rgba(fc, 0)
+ im.set_bg( *bg)
+
+ # image input dimensions
+ im.reset_matrix()
+ numrows, numcols = im.get_size()
+
+ im.set_interpolation(self._interpd[self._interpolation])
+
+ im.set_resample(self._resample)
+
+ # the viewport translation
+ tx = (xmin-self.axes.viewLim.x0)/dxintv * numcols
+ ty = (ymin-self.axes.viewLim.y0)/dyintv * numrows
+
+ l, b, r, t = self.axes.bbox.extents
+ widthDisplay = (round(r) + 0.5) - (round(l) - 0.5)
+ heightDisplay = (round(t) + 0.5) - (round(b) - 0.5)
+ widthDisplay *= magnification
+ heightDisplay *= magnification
+ im.apply_translation(tx, ty)
+
+ # resize viewport to display
+ rx = widthDisplay / numcols
+ ry = heightDisplay / numrows
+ im.apply_scaling(rx*sx, ry*sy)
+ im.resize(int(widthDisplay+0.5), int(heightDisplay+0.5),
+ norm=self._filternorm, radius=self._filterrad)
+ return im
+
+
+ def set_extent(self, extent):
+ """
+ extent is data axes (left, right, bottom, top) for making image plots
+ """
+ self._extent = extent
+
+ xmin, xmax, ymin, ymax = extent
+ corners = (xmin, ymin), (xmax, ymax)
+ self.axes.update_datalim(corners)
+ if self.axes._autoscaleXon:
+ self.axes.set_xlim((xmin, xmax))
+ if self.axes._autoscaleYon:
+ self.axes.set_ylim((ymin, ymax))
+
+ def get_extent(self):
+ 'get the image extent: left, right, bottom, top'
+ if self._extent is not None:
+ return self._extent
+ else:
+ sz = self.get_size()
+ #print 'sz', sz
+ numrows, numcols = sz
+ if self.origin == 'upper':
+ return (-0.5, numcols-0.5, numrows-0.5, -0.5)
+ else:
+ return (-0.5, numcols-0.5, -0.5, numrows-0.5)
+
+
+
class NonUniformImage(AxesImage):
def __init__(self, ax, **kwargs):
"""
@@ -747,7 +795,7 @@
_png.write_png(buffer, cols, rows, fname)
-class BboxImage(AxesImage):
+class BboxImage(_AxesImageBase):
"""
The Image class whose size is determined by the given bbox.
"""
@@ -770,16 +818,16 @@
kwargs are an optional list of Artist keyword args
"""
- AxesImage.__init__(self, ax=None,
- cmap = cmap,
- norm = norm,
- interpolation=interpolation,
- origin=origin,
- filternorm=filternorm,
- filterrad=filterrad,
- resample = resample,
- **kwargs
- )
+ _AxesImageBase.__init__(self, ax=None,
+ cmap = cmap,
+ norm = norm,
+ interpolation=interpolation,
+ origin=origin,
+ filternorm=filternorm,
+ filterrad=filterrad,
+ resample = resample,
+ **kwargs
+ )
self.bbox = bbox
@@ -842,11 +890,6 @@
else:
im = self._imcache
- if 0:
- fc = self.axes.patch.get_facecolor()
- bg = mcolors.colorConverter.to_rgba(fc, 0)
- im.set_bg( *bg)
-
# image input dimensions
im.reset_matrix()
@@ -859,7 +902,6 @@
heightDisplay = (round(t) + 0.5) - (round(b) - 0.5)
widthDisplay *= magnification
heightDisplay *= magnification
- #im.apply_translation(tx, ty)
numrows, numcols = self._A.shape[:2]
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|