def _initialize_x_y(self, z, origin, extent): ''' Return X, Y arrays such that contour(Z) will match imshow(Z) if origin is not None. The center of pixel Z[i,j] depends on origin: if origin is None, x = j, y = i; if origin is 'lower', x = j + 0.5, y = i + 0.5; if origin is 'upper', x = j + 0.5, y = Nrows - i - 0.5 If extent is not None, x and y will be scaled to match, as in imshow. ''' if len(shape(z)) != 2: raise TypeError("Input must be a 2D array.") else: Ny, Nx = shape(z) if origin is None: return meshgrid(arange(Nx), arange(Ny)) if extent is None: x0, x1, y0, y1 = (0, Nx, 0, Ny) else: x0, x1, y0, y1 = extent dx = float(x1 - x0) / Nx dy = float(y1 - y0) / Ny x = x0 + (arange(Nx) + 0.5) * dx y = y0 + (arange(Ny) + 0.5) * dy if origin == 'upper': y = y[::-1] return meshgrid(x, y)
def _check_xyz(self, args): ''' For functions like contour, check that the dimensions of the input arrays match; if x and y are 1D, convert them to 2D using meshgrid. Possible change: I think we should make and use an ArgumentError Exception class (here and elsewhere). Add checking for everything being the same numerix flavor? ''' x,y,z = args if len(shape(z)) != 2: raise TypeError("Input z must be a 2D array.") else: Ny, Nx = shape(z) if shape(x) == shape(z) and shape(y) == shape(z): return x,y,z if len(shape(x)) != 1 or len(shape(y)) != 1: raise TypeError("Inputs x and y must be 1D or 2D.") nx, = shape(x) ny, = shape(y) if nx != Nx or ny != Ny: raise TypeError("Length of x must be number of columns in z,\n" + "and length of y must be number of rows.") x,y = meshgrid(x,y) return x,y,z
def _check_xyz(self, args): ''' For functions like contour, check that the dimensions of the input arrays match; if x and y are 1D, convert them to 2D using meshgrid. Possible change: I think we should make and use an ArgumentError Exception class (here and elsewhere). ''' x, y, z = args if len(shape(z)) != 2: raise TypeError("Input z must be a 2D array.") else: Ny, Nx = shape(z) if shape(x) == shape(z) and shape(y) == shape(z): return x, y, z if len(shape(x)) != 1 or len(shape(y)) != 1: raise TypeError("Inputs x and y must be 1D or 2D.") nx, = shape(x) ny, = shape(y) if nx != Nx or ny != Ny: raise TypeError("Length of x must be number of columns in z,\n" + "and length of y must be number of rows.") x, y = meshgrid(x, y) return x, y, z
def _initialize_x_y(self, z): ''' Return X, Y arrays such that contour(Z) will match imshow(Z) if origin is not None. The center of pixel Z[i,j] depends on origin: if origin is None, x = j, y = i; if origin is 'lower', x = j + 0.5, y = i + 0.5; if origin is 'upper', x = j + 0.5, y = Nrows - i - 0.5 If extent is not None, x and y will be scaled to match, as in imshow. ''' if len(shape(z)) != 2: raise TypeError("Input must be a 2D array.") else: Ny, Nx = shape(z) if self.origin is None: return meshgrid(arange(Nx), arange(Ny)) if self.extent is None: x0,x1,y0,y1 = (0, Nx, 0, Ny) else: x0,x1,y0,y1 = self.extent dx = float(x1 - x0)/Nx dy = float(y1 - y0)/Ny x = x0 + (arange(Nx) + 0.5) * dx y = y0 + (arange(Ny) + 0.5) * dy if self.origin == 'upper': y = y[::-1] return meshgrid(x,y)
def rem(x, y): """ Remainder after division. rem(x,y) is equivalent to x - y.*fix(x./y) in case y is not zero. By convention, rem(x,0) returns None. We keep the convention by Matlab: "The input x and y must be real arrays of the same size, or real scalars." """ x, y = asarray(x), asarray(y) if numerix.shape(x) == numerix.shape(y) or numerix.shape(y) == (): try: return x - y * fix(x / y) except OverflowError: return None raise RuntimeError('Dimension error')
def _contour_args(self, filled, origin, extent, *args): if filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z, origin, extent) elif Nargs <=4: x,y,z = self._check_xyz(args[:3]) else: raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn)) z = ma.asarray(z) # Convert to native masked array format if necessary. if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7, filled) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg, filled) elif iterable(level_arg) and len(shape(level_arg)) == 1: lev = array([float(fl) for fl in level_arg]) else: raise TypeError("Last %s arg must give levels; see help(%s)" % (fn,fn)) if filled and len(lev) < 2: raise ValueError("Filled contours require at least 2 levels.") self.ax.set_xlim((ma.minimum(x), ma.maximum(x))) self.ax.set_ylim((ma.minimum(y), ma.maximum(y))) # Workaround for cntr.c bug wrt masked interior regions: #if filled: # z = ma.masked_array(z.filled(-1e38)) # It's not clear this is any better than the original bug. return (x, y, z, lev)
def fix(x): """ Rounds towards zero. x_rounded = fix(x) rounds the elements of x to the nearest integers towards zero. For negative numbers is equivalent to ceil and for positive to floor. """ dim = numerix.shape(x) if MLab.rank(x)==2: y = numerix.reshape(x,(1,dim[0]*dim[1]))[0] y = y.tolist() elif MLab.rank(x)==1: y = x else: y = [x] for i in range(len(y)): if y[i]>0: y[i] = numerix.floor(y[i]) else: y[i] = numerix.ceil(y[i]) if MLab.rank(x)==2: x = numerix.reshape(y,dim) elif MLab.rank(x)==0: x = y[0] return x
def _contour_args(self, filled, badmask, origin, extent, *args): if filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z, origin, extent) elif Nargs <=4: x,y,z = self._check_xyz(args[:3]) else: raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn)) z = asarray(z) # Convert to native array format if necessary. if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7, filled, badmask) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg, filled, badmask) elif iterable(level_arg) and len(shape(level_arg)) == 1: lev = array([float(fl) for fl in level_arg]) else: raise TypeError("Last %s arg must give levels; see help(%s)" % (fn,fn)) rx = ravel(x) ry = ravel(y) self.ax.set_xlim((min(rx), max(rx))) self.ax.set_ylim((min(ry), max(ry))) return (x, y, z, lev)
def _contour_args(self, filled, badmask, origin, extent, *args): if filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z, origin, extent) elif Nargs <= 4: x, y, z = self._check_xyz(args[:3]) else: raise TypeError("Too many arguments to %s; see help(%s)" % (fn, fn)) z = asarray(z) # Convert to native array format if necessary. if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7, filled, badmask) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg, filled, badmask) elif iterable(level_arg) and len(shape(level_arg)) == 1: lev = array([float(fl) for fl in level_arg]) else: raise TypeError("Last %s arg must give levels; see help(%s)" % (fn, fn)) rx = ravel(x) ry = ravel(y) self.ax.set_xlim((min(rx), max(rx))) self.ax.set_ylim((min(ry), max(ry))) return (x, y, z, lev)
def orth(A): """ Orthogonalization procedure by Matlab. The description is taken from its help: Q = ORTH(A) is an orthonormal basis for the range of A. That is, Q'*Q = I, the columns of Q span the same space as the columns of A, and the number of columns of Q is the rank of A. """ A = array(A) U,S,V = numerix.mlab.svd(A) m,n = numerix.shape(A) if m > 1: s = S elif m == 1: s = S[0] else: s = 0 tol = numerix.mlab.max((m,n)) * numerix.mlab.max(s) * _eps_approx r = asum(s > tol) Q = take(U,range(r),1) return Q
def fix(x): """ Rounds towards zero. x_rounded = fix(x) rounds the elements of x to the nearest integers towards zero. For negative numbers is equivalent to ceil and for positive to floor. """ dim = numerix.shape(x) if numerix.mlab.rank(x)==2: y = reshape(x,(1,dim[0]*dim[1]))[0] y = y.tolist() elif numerix.mlab.rank(x)==1: y = x else: y = [x] for i in range(len(y)): if y[i]>0: y[i] = floor(y[i]) else: y[i] = ceil(y[i]) if numerix.mlab.rank(x)==2: x = reshape(y,dim) elif numerix.mlab.rank(x)==0: x = y[0] return x
def rem(x,y): """ Remainder after division. rem(x,y) is equivalent to x - y.*fix(x./y) in case y is not zero. By convention, rem(x,0) returns None. We keep the convention by Matlab: "The input x and y must be real arrays of the same size, or real scalars." """ x,y = asarray(x),asarray(y) if numerix.shape(x) == numerix.shape(y) or numerix.shape(y) == (): try: return x - y * fix(x/y) except OverflowError: return None raise RuntimeError('Dimension error')
def orth(A): """ Orthogonalization procedure by Matlab. The description is taken from its help: Q = ORTH(A) is an orthonormal basis for the range of A. That is, Q'*Q = I, the columns of Q span the same space as the columns of A, and the number of columns of Q is the rank of A. """ A = numerix.array(A) U,S,V = MLab.svd(A) m,n = numerix.shape(A) if m > 1: s = S elif m == 1: s = S[0] else: s = 0 tol = MLab.max((m,n)) * MLab.max(s) * _eps_approx r = MLab.sum(s > tol) Q = numerix.take(U,range(r),1) return Q
def _contour_args(self, *args): if self.filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z) elif Nargs <=4: x,y,z = self._check_xyz(args[:3]) else: raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn)) z = ma.asarray(z) # Convert to native masked array format if necessary. if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg) elif iterable(level_arg) and len(shape(level_arg)) == 1: lev = array([float(fl) for fl in level_arg]) else: raise TypeError("Last %s arg must give levels; see help(%s)" % (fn,fn)) if self.filled and len(lev) < 2: raise ValueError("Filled contours require at least 2 levels.") # Workaround for cntr.c bug wrt masked interior regions: #if filled: # z = ma.masked_array(z.filled(-1e38)) # It's not clear this is any better than the original bug. self.levels = lev self.layers = self.levels # contour: a line is a thin layer if self.filled: self.layers = 0.5 * (self.levels[:-1] + self.levels[1:]) return (x, y, z)
def _contour_args(self, *args): if self.filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z) elif Nargs <=4: x,y,z = self._check_xyz(args[:3]) else: raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn)) z = ma.asarray(z) # Convert to native masked array format if necessary. self.zmax = ma.maximum(z) self.zmin = ma.minimum(z) self._auto = False if self.levels is None: if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg) elif iterable(level_arg) and len(shape(level_arg)) == 1: lev = array([float(fl) for fl in level_arg]) else: raise TypeError("Last %s arg must give levels; see help(%s)" % (fn,fn)) if self.filled and len(lev) < 2: raise ValueError("Filled contours require at least 2 levels.") # Workaround for cntr.c bug wrt masked interior regions: #if filled: # z = ma.masked_array(z.filled(-1e38)) # It's not clear this is any better than the original bug. self.levels = lev #if self._auto and self.extend in ('both', 'min', 'max'): # raise TypeError("Auto level selection is inconsistent " # + "with use of 'extend' kwarg") self._levels = list(self.levels) if self.extend in ('both', 'min'): self._levels.insert(0, self.zmin - 1) if self.extend in ('both', 'max'): self._levels.append(self.zmax + 1) self._levels = asarray(self._levels) self.vmin = amin(self.levels) # alternative would be self.layers self.vmax = amax(self.levels) if self.extend in ('both', 'min') or self.clip_ends: self.vmin = 2 * self.levels[0] - self.levels[1] if self.extend in ('both', 'max') or self.clip_ends: self.vmax = 2 * self.levels[-1] - self.levels[-2] self.layers = self._levels # contour: a line is a thin layer if self.filled: self.layers = 0.5 * (self._levels[:-1] + self._levels[1:]) if self.extend in ('both', 'min') or self.clip_ends: self.layers[0] = 0.5 * (self.vmin + self._levels[1]) if self.extend in ('both', 'max') or self.clip_ends: self.layers[-1] = 0.5 * (self.vmax + self._levels[-2]) return (x, y, z)
def _initialize_x_y(self, z): ''' Return X, Y arrays such that contour(Z) will match imshow(Z) if origin is not None. The center of pixel Z[i,j] depends on origin: if origin is None, x = j, y = i; if origin is 'lower', x = j + 0.5, y = i + 0.5; if origin is 'upper', x = j + 0.5, y = Nrows - i - 0.5 If extent is not None, x and y will be scaled to match, as in imshow. If origin is None and extent is not None, then extent will give the minimum and maximum values of x and y. ''' if len(shape(z)) != 2: raise TypeError("Input must be a 2D array.") else: Ny, Nx = shape(z) if self.origin is None: # Not for image-matching. if self.extent is None: return meshgrid(arange(Nx), arange(Ny)) else: x0, x1, y0, y1 = self.extent x = linspace(x0, x1, Nx) y = linspace(y0, y1, Ny) return meshgrid(x, y) # Match image behavior: if self.extent is None: x0, x1, y0, y1 = (0, Nx, 0, Ny) else: x0, x1, y0, y1 = self.extent dx = float(x1 - x0) / Nx dy = float(y1 - y0) / Ny x = x0 + (arange(Nx) + 0.5) * dx y = y0 + (arange(Ny) + 0.5) * dy if self.origin == 'upper': y = y[::-1] return meshgrid(x, y)
def rank(x): """ Returns the rank of a matrix. The rank is understood here as the an estimation of the number of linearly independent rows or columns (depending on the size of the matrix). Note that numerix.mlab.rank() is not equivalent to Matlab's rank. This function is! """ x = asarray(x) u,s,v = numerix.mlab.svd(x) # maxabs = numerix.mlab.max(numerix.absolute(s)) is also possible. maxabs = norm(x) maxdim = numerix.mlab.max(numerix.shape(x)) tol = maxabs*maxdim*_eps_approx r = s>tol return asum(r)
def rank(x): """ Returns the rank of a matrix. The rank is understood here as the an estimation of the number of linearly independent rows or columns (depending on the size of the matrix). Note that MLab.rank() is not equivalent to Matlab's rank. This function is! """ x = numerix.asarray(x) u,s,v = MLab.svd(x) # maxabs = MLab.max(numerix.absolute(s)) is also possible. maxabs = norm(x) maxdim = MLab.max(numerix.shape(x)) tol = maxabs*maxdim*_eps_approx r = s>tol return MLab.sum(r)
def _initialize_reg_tri(self, z, badmask): ''' Initialize two arrays used by the low-level contour algorithm. This is temporary code; most of the reg initialization should be done in c. For each masked point, we need to mark as missing the four regions with that point as a corner. ''' imax, jmax = shape(z) nreg = jmax * (imax + 1) + 1 reg = ones((1, nreg), typecode='i') reg[0, :jmax + 1] = 0 reg[0, -jmax:] = 0 for j in range(0, nreg, jmax): reg[0, j] = 0 if badmask is not None: for i in range(imax): for j in range(jmax): if badmask[i, j]: ii = i * jmax + j if ii < nreg: reg[0, ii] = 0 ii += 1 if ii < nreg: reg[0, ii] = 0 ii += jmax if ii < nreg: reg[0, ii] = 0 ii -= 1 if ii < nreg: reg[0, ii] = 0 triangle = zeros((imax, jmax), typecode='s') return reg, triangle
def _initialize_reg_tri(self, z, badmask): ''' Initialize two arrays used by the low-level contour algorithm. This is temporary code; most of the reg initialization should be done in c. For each masked point, we need to mark as missing the four regions with that point as a corner. ''' imax, jmax = shape(z) nreg = jmax*(imax+1)+1 reg = ones((1, nreg), typecode = 'i') reg[0,:jmax+1]=0 reg[0,-jmax:]=0 for j in range(0, nreg, jmax): reg[0,j]=0 if badmask is not None: for i in range(imax): for j in range(jmax): if badmask[i,j]: ii = i*jmax+j if ii < nreg: reg[0,ii] = 0 ii += 1 if ii < nreg: reg[0,ii] = 0 ii += jmax if ii < nreg: reg[0,ii] = 0 ii -= 1 if ii < nreg: reg[0,ii] = 0 triangle = zeros((imax,jmax), typecode='s') return reg, triangle
def _contour_args(self, *args): if self.filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z) elif Nargs <= 4: x, y, z = self._check_xyz(args[:3]) else: raise TypeError("Too many arguments to %s; see help(%s)" % (fn, fn)) z = ma.asarray( z) # Convert to native masked array format if necessary. self.zmax = ma.maximum(z) self.zmin = ma.minimum(z) self._auto = False if self.levels is None: if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg) elif iterable(level_arg) and len(shape(level_arg)) == 1: lev = array([float(fl) for fl in level_arg]) else: raise TypeError( "Last %s arg must give levels; see help(%s)" % (fn, fn)) if self.filled and len(lev) < 2: raise ValueError("Filled contours require at least 2 levels.") # Workaround for cntr.c bug wrt masked interior regions: #if filled: # z = ma.masked_array(z.filled(-1e38)) # It's not clear this is any better than the original bug. self.levels = lev #if self._auto and self.extend in ('both', 'min', 'max'): # raise TypeError("Auto level selection is inconsistent " # + "with use of 'extend' kwarg") self._levels = list(self.levels) if self.extend in ('both', 'min'): self._levels.insert(0, self.zmin - 1) if self.extend in ('both', 'max'): self._levels.append(self.zmax + 1) self._levels = asarray(self._levels) self.vmin = amin(self.levels) # alternative would be self.layers self.vmax = amax(self.levels) if self.extend in ('both', 'min') or self.clip_ends: self.vmin = 2 * self.levels[0] - self.levels[1] if self.extend in ('both', 'max') or self.clip_ends: self.vmax = 2 * self.levels[-1] - self.levels[-2] self.layers = self._levels # contour: a line is a thin layer if self.filled: self.layers = 0.5 * (self._levels[:-1] + self._levels[1:]) if self.extend in ('both', 'min') or self.clip_ends: self.layers[0] = 0.5 * (self.vmin + self._levels[1]) if self.extend in ('both', 'max') or self.clip_ends: self.layers[-1] = 0.5 * (self.vmax + self._levels[-2]) return (x, y, z)