From scipy-svn at scipy.org Sat Sep 2 18:09:26 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 2 Sep 2006 17:09:26 -0500 (CDT) Subject: [Scipy-svn] r2186 - trunk/Lib/interpolate Message-ID: <20060902220926.D0FB239C069@new.scipy.org> Author: stefan Date: 2006-09-02 17:09:19 -0500 (Sat, 02 Sep 2006) New Revision: 2186 Modified: trunk/Lib/interpolate/fitpack.py Log: Fix previous patch to use int32 instead of intc. Modified: trunk/Lib/interpolate/fitpack.py =================================================================== --- trunk/Lib/interpolate/fitpack.py 2006-08-30 15:18:55 UTC (rev 2185) +++ trunk/Lib/interpolate/fitpack.py 2006-09-02 22:09:19 UTC (rev 2186) @@ -33,7 +33,7 @@ __version__ = "$Revision$"[10:-1] import _fitpack from numpy import atleast_1d, array, ones, zeros, sqrt, ravel, transpose, \ - dot, sin, cos, pi, arange, empty, intc + dot, sin, cos, pi, arange, empty, int32 myasarray = atleast_1d # Try to replace _fitpack interface with @@ -101,7 +101,7 @@ An error occured""",TypeError]} _parcur_cache = {'t': array([],float), 'wrk': array([],float), - 'iwrk':array([],intc), 'u': array([],float),'ub':0,'ue':1} + 'iwrk':array([],int32), 'u': array([],float),'ub':0,'ue':1} def splprep(x,w=None,u=None,ub=None,ue=None,k=3,task=0,s=None,t=None, full_output=0,nest=None,per=0,quiet=1): @@ -169,7 +169,7 @@ """ if task<=0: _parcur_cache = {'t': array([],float), 'wrk': array([],float), - 'iwrk':array([],intc),'u': array([],float), + 'iwrk':array([],int32),'u': array([],float), 'ub':0,'ue':1} x=myasarray(x) idim,m=x.shape @@ -245,7 +245,7 @@ return tcku _curfit_cache = {'t': array([],float), 'wrk': array([],float), - 'iwrk':array([],intc)} + 'iwrk':array([],int32)} def splrep(x,y,w=None,xb=None,xe=None,k=3,task=0,s=1e-3,t=None, full_output=0,per=0,quiet=1): """Find the B-spline representation of 1-D curve. @@ -353,7 +353,7 @@ _curfit_cache['t'] = t if task <= 0: _curfit_cache['wrk'] = empty((m*(k+1)+nest*(7+3*k),),float) - _curfit_cache['iwrk'] = empty((nest,),intc) + _curfit_cache['iwrk'] = empty((nest,),int32) try: t=_curfit_cache['t'] wrk=_curfit_cache['wrk'] @@ -532,7 +532,7 @@ # full_output=0,nest=None,per=0,quiet=1): _surfit_cache = {'tx': array([],float),'ty': array([],float), - 'wrk': array([],float), 'iwrk':array([],intc)} + 'wrk': array([],float), 'iwrk':array([],int32)} def bisplrep(x,y,z,w=None,xb=None,xe=None,yb=None,ye=None,kx=3,ky=3,task=0, s=None,eps=1e-16,tx=None,ty=None,full_output=0, nxest=None,nyest=None,quiet=1): From scipy-svn at scipy.org Tue Sep 5 03:45:33 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 5 Sep 2006 02:45:33 -0500 (CDT) Subject: [Scipy-svn] r2187 - trunk/Lib Message-ID: <20060905074533.EBE8639C03A@new.scipy.org> Author: oliphant Date: 2006-09-05 02:45:29 -0500 (Tue, 05 Sep 2006) New Revision: 2187 Modified: trunk/Lib/__init__.py Log: Alter default for printing warnings. Modified: trunk/Lib/__init__.py =================================================================== --- trunk/Lib/__init__.py 2006-09-02 22:09:19 UTC (rev 2186) +++ trunk/Lib/__init__.py 2006-09-05 07:45:29 UTC (rev 2187) @@ -51,7 +51,7 @@ # Load scipy packages, their global_symbols, set up __doc__ string. from numpy._import_tools import PackageLoader import os as _os -SCIPY_IMPORT_VERBOSE = int(_os.environ.get('SCIPY_IMPORT_VERBOSE','0')) +SCIPY_IMPORT_VERBOSE = int(_os.environ.get('SCIPY_IMPORT_VERBOSE','-1')) del _os pkgload = PackageLoader() pkgload(verbose=SCIPY_IMPORT_VERBOSE,postpone=True) From scipy-svn at scipy.org Tue Sep 5 03:48:25 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 5 Sep 2006 02:48:25 -0500 (CDT) Subject: [Scipy-svn] r2188 - tags Message-ID: <20060905074825.0E75C39C03A@new.scipy.org> Author: oliphant Date: 2006-09-05 02:48:12 -0500 (Tue, 05 Sep 2006) New Revision: 2188 Added: tags/0.5.1/ Log: made a copy Copied: tags/0.5.1 (from rev 2187, trunk) From scipy-svn at scipy.org Tue Sep 5 03:59:55 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 5 Sep 2006 02:59:55 -0500 (CDT) Subject: [Scipy-svn] r2189 - tags/0.5.1/Lib Message-ID: <20060905075955.52C4D39C03A@new.scipy.org> Author: oliphant Date: 2006-09-05 02:59:42 -0500 (Tue, 05 Sep 2006) New Revision: 2189 Modified: tags/0.5.1/Lib/version.py Log: Fix version number Modified: tags/0.5.1/Lib/version.py =================================================================== --- tags/0.5.1/Lib/version.py 2006-09-05 07:48:12 UTC (rev 2188) +++ tags/0.5.1/Lib/version.py 2006-09-05 07:59:42 UTC (rev 2189) @@ -1,5 +1,5 @@ version = '0.5.1' -release=False +release=True if not release: import os From scipy-svn at scipy.org Tue Sep 5 04:01:30 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 5 Sep 2006 03:01:30 -0500 (CDT) Subject: [Scipy-svn] r2190 - trunk/Lib Message-ID: <20060905080130.B3C5439C03A@new.scipy.org> Author: oliphant Date: 2006-09-05 03:01:10 -0500 (Tue, 05 Sep 2006) New Revision: 2190 Modified: trunk/Lib/version.py Log: Update version to 0.5.2 Modified: trunk/Lib/version.py =================================================================== --- trunk/Lib/version.py 2006-09-05 07:59:42 UTC (rev 2189) +++ trunk/Lib/version.py 2006-09-05 08:01:10 UTC (rev 2190) @@ -1,4 +1,4 @@ -version = '0.5.1' +version = '0.5.2' release=False if not release: From scipy-svn at scipy.org Tue Sep 5 20:46:25 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 5 Sep 2006 19:46:25 -0500 (CDT) Subject: [Scipy-svn] r2191 - trunk/Lib/signal Message-ID: <20060906004625.7783C39C018@new.scipy.org> Author: stefan Date: 2006-09-05 19:46:12 -0500 (Tue, 05 Sep 2006) New Revision: 2191 Modified: trunk/Lib/signal/filter_design.py Log: filter_design's ellipap should specify coefficients as an array in call to optimize. Modified: trunk/Lib/signal/filter_design.py =================================================================== --- trunk/Lib/signal/filter_design.py 2006-09-05 08:01:10 UTC (rev 2190) +++ trunk/Lib/signal/filter_design.py 2006-09-06 00:46:12 UTC (rev 2191) @@ -1045,7 +1045,7 @@ else: krat = N*val[0] / val[1] - m = optimize.fmin(kratio, 0.5, args=(krat,), maxfun=250, maxiter=250, + m = optimize.fmin(kratio, [0.5], args=(krat,), maxfun=250, maxiter=250, disp=0) if m < 0 or m > 1: m = optimize.fminbound(kratio, 0, 1, args=(krat,), maxfun=250, From scipy-svn at scipy.org Fri Sep 8 06:41:05 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 8 Sep 2006 05:41:05 -0500 (CDT) Subject: [Scipy-svn] r2192 - trunk/Lib/sandbox/svm Message-ID: <20060908104105.6EF0E39C080@new.scipy.org> Author: fullung Date: 2006-09-08 05:40:47 -0500 (Fri, 08 Sep 2006) New Revision: 2192 Modified: trunk/Lib/sandbox/svm/__init__.py trunk/Lib/sandbox/svm/kernel.py Log: Documentation. Modified: trunk/Lib/sandbox/svm/__init__.py =================================================================== --- trunk/Lib/sandbox/svm/__init__.py 2006-09-06 00:46:12 UTC (rev 2191) +++ trunk/Lib/sandbox/svm/__init__.py 2006-09-08 10:40:47 UTC (rev 2192) @@ -1,18 +1,62 @@ """ A Support Vector Machine, this module defines the following classes: -- `CSVCModel`, a model for C-SV classification -- `NuSVCModel`, a model for nu-SV classification -- `EpsilonSVRModel`, a model for epsilon-SV regression -- `NuSVRModel`, a model for nu-SV regression -- `OneClassModel`, a model for distribution estimation (one-class SVM) +- `LibSvmCClassificationModel`, a model for C-SV classification +- `LibSvmNuClassificationModel`, a model for nu-SV classification +- `LibSvmEpsilonRegressionModel`, a model for epsilon-SV regression +- `LibSvmNuRegressionModel`, a model for nu-SV regression +- `LibSvmOneClassModel`, a model for distribution estimation + (one-class SVM) +Kernel classes: +- `LinearKernel`, a linear kernel +- `PolynomialKernel`, a polynomial kernel +- `RBFKernel`, a radial basis function kernel +- `SigmoidKernel`, a sigmoid kernel +- `CustomKernel`, a kernel that wraps any callable + +Dataset classes: + +- `LibSvmClassificationDataSet`, a dataset for training classification + models +- `LibSvmRegressionDataSet`, a dataset for training regression models +- `LibSvmOneClassDataSet`, a dataset for training distribution + estimation (one-class SVM) models +- `LibSvmTestDataSet`, a dataset for testing with any model + +Data type classes: + +- `svm_node_dtype`, the libsvm data type for its arrays + How To Use This Module ====================== (See the individual classes, methods, and attributes for details.) 1. Import it: ``import svm`` or ``from svm import ...``. + +2. Create a training dataset for your problem:: + + traindata = LibSvmClassificationDataSet(labels, x) + traindata = LibSvmRegressionDataSet(y, x) + traindata = LibSvmOneClassDataSet(x) + + where x is sequence of NumPy arrays containing scalars or + svm_node_dtype entries. + +3. Create a test dataset:: + + testdata = LibSvmTestDataSet(u) + +4. Create a model and fit it to the training data:: + + model = LibSvmCClassificationModel(kernel) + results = model.fit(traindata) + +5. Use the results to make predictions with the test data:: + + p = results.predict(testdata) + v = results.predict_values(testdata) """ from classification import * Modified: trunk/Lib/sandbox/svm/kernel.py =================================================================== --- trunk/Lib/sandbox/svm/kernel.py 2006-09-06 00:46:12 UTC (rev 2191) +++ trunk/Lib/sandbox/svm/kernel.py 2006-09-08 10:40:47 UTC (rev 2192) @@ -11,6 +11,7 @@ ] class LinearKernel: + """Linear kernel: u'*v.""" def __init__(self): self.kernel_type = libsvm.LINEAR @@ -23,6 +24,8 @@ return self class PolynomialKernel: + """Polynomial kernel: (gamma*u'*v + coef0)^degree.""" + def __init__(self, degree, gamma, coef0): self.kernel_type = libsvm.POLY self.degree = degree @@ -51,6 +54,8 @@ 'model compaction for PolynomialKernel not implemented' class RBFKernel: + """Radial basis function kernel: exp(-gamma*|u-v|^2)""" + def __init__(self, gamma): self.kernel_type = libsvm.RBF self.gamma = gamma @@ -75,6 +80,8 @@ 'model compaction for RBFKernel not implemented' class SigmoidKernel: + """Sigmoid kernel: tanh(gamma*u'*v + coef0)""" + def __init__(self, gamma, coef0): self.kernel_type = libsvm.SIGMOID self.gamma = gamma @@ -94,6 +101,8 @@ 'model compaction for SigmoidKernel not implemented' class CustomKernel: + """Custom kernel: any callable""" + def __init__(self, f): self.kernel_type = libsvm.PRECOMPUTED self.f = f From scipy-svn at scipy.org Fri Sep 8 12:14:47 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 8 Sep 2006 11:14:47 -0500 (CDT) Subject: [Scipy-svn] r2193 - trunk/Lib/sandbox/svm Message-ID: <20060908161447.EC22739C053@new.scipy.org> Author: fullung Date: 2006-09-08 11:14:39 -0500 (Fri, 08 Sep 2006) New Revision: 2193 Modified: trunk/Lib/sandbox/svm/libsvm.py Log: Load library built with distutils. Modified: trunk/Lib/sandbox/svm/libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/libsvm.py 2006-09-08 10:40:47 UTC (rev 2192) +++ trunk/Lib/sandbox/svm/libsvm.py 2006-09-08 16:14:39 UTC (rev 2193) @@ -1,13 +1,15 @@ import inspect from ctypes import * +import distutils.sysconfig import numpy as N __all__ = [ 'svm_node_dtype' ] -_libsvm = N.ctypeslib.load_library('libsvm_', __file__) +so_ext = distutils.sysconfig.get_config_vars('SO')[0] +_libsvm = N.ctypeslib.load_library('libsvm_%s' % so_ext, __file__) svm_node_dtype = \ N.dtype({'names' : ['index', 'value'], From scipy-svn at scipy.org Fri Sep 8 12:18:46 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 8 Sep 2006 11:18:46 -0500 (CDT) Subject: [Scipy-svn] r2194 - trunk/Lib/sandbox/svm Message-ID: <20060908161846.EEA0A39C01C@new.scipy.org> Author: fullung Date: 2006-09-08 11:18:38 -0500 (Fri, 08 Sep 2006) New Revision: 2194 Modified: trunk/Lib/sandbox/svm/libsvm.py Log: Comment about our interesting library filename. Modified: trunk/Lib/sandbox/svm/libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/libsvm.py 2006-09-08 16:14:39 UTC (rev 2193) +++ trunk/Lib/sandbox/svm/libsvm.py 2006-09-08 16:18:38 UTC (rev 2194) @@ -8,6 +8,9 @@ 'svm_node_dtype' ] +# load libsvm library from the shared library/DLL using the file name +# extension for Python extensions, because we are currently using +# distutils to build the shared library/DLL as a Python extension so_ext = distutils.sysconfig.get_config_vars('SO')[0] _libsvm = N.ctypeslib.load_library('libsvm_%s' % so_ext, __file__) From scipy-svn at scipy.org Fri Sep 8 12:23:24 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 8 Sep 2006 11:23:24 -0500 (CDT) Subject: [Scipy-svn] r2195 - trunk/Lib/sandbox/svm Message-ID: <20060908162324.0523F39C01C@new.scipy.org> Author: fullung Date: 2006-09-08 11:23:16 -0500 (Fri, 08 Sep 2006) New Revision: 2195 Modified: trunk/Lib/sandbox/svm/setup.py Log: Include tests with build. Modified: trunk/Lib/sandbox/svm/setup.py =================================================================== --- trunk/Lib/sandbox/svm/setup.py 2006-09-08 16:18:38 UTC (rev 2194) +++ trunk/Lib/sandbox/svm/setup.py 2006-09-08 16:23:16 UTC (rev 2195) @@ -9,6 +9,7 @@ ('LIBSVM_DLL', None)], sources=[join('libsvm-2.82', 'svm.cpp')], depends=[join('libsvm-2.82', 'svm.h')]) + config.add_data_dir('tests') return config if __name__ == '__main__': From scipy-svn at scipy.org Tue Sep 12 04:37:38 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 12 Sep 2006 03:37:38 -0500 (CDT) Subject: [Scipy-svn] r2196 - trunk/Lib/signal Message-ID: <20060912083738.9D78A39C01D@new.scipy.org> Author: stefan Date: 2006-09-12 03:37:24 -0500 (Tue, 12 Sep 2006) New Revision: 2196 Modified: trunk/Lib/signal/ltisys.py Log: Use interp1d in ltsys. Modified: trunk/Lib/signal/ltisys.py =================================================================== --- trunk/Lib/signal/ltisys.py 2006-09-08 16:23:16 UTC (rev 2195) +++ trunk/Lib/signal/ltisys.py 2006-09-12 08:37:24 UTC (rev 2196) @@ -7,7 +7,7 @@ from filter_design import tf2zpk, zpk2tf, normalize import numpy from numpy import product, zeros, \ - array, dot, transpose, arange, ones + array, dot, transpose, arange, ones, nan_to_num from numpy.oldnumeric import Float import scipy.interpolate as interpolate import scipy.integrate as integrate @@ -330,10 +330,10 @@ # for each output point directly integrate assume zero-order hold # or linear interpolation. - ufunc = interpolate.linear_1d(T, U, axis=0, bounds_error=0, fill_value=0) + ufunc = interpolate.interp1d(T, U, kind='linear', axis=0, bounds_error=False) def fprime(x, t, sys, ufunc): - return dot(sys.A,x) + squeeze(dot(sys.B,ufunc([t]))) + return dot(sys.A,x) + squeeze(dot(sys.B,nan_to_num(ufunc([t])))) xout = integrate.odeint(fprime, X0, T, args=(sys, ufunc)) yout = dot(sys.C,transpose(xout)) + dot(sys.D,transpose(U)) From scipy-svn at scipy.org Tue Sep 12 16:49:37 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 12 Sep 2006 15:49:37 -0500 (CDT) Subject: [Scipy-svn] r2197 - trunk/Lib/sandbox/odr Message-ID: <20060912204937.232C339C040@new.scipy.org> Author: rkern Date: 2006-09-12 15:49:35 -0500 (Tue, 12 Sep 2006) New Revision: 2197 Modified: trunk/Lib/sandbox/odr/models.py trunk/Lib/sandbox/odr/odrpack.py Log: Fix odr for numpy. Modified: trunk/Lib/sandbox/odr/models.py =================================================================== --- trunk/Lib/sandbox/odr/models.py 2006-09-12 08:37:24 UTC (rev 2196) +++ trunk/Lib/sandbox/odr/models.py 2006-09-12 20:49:35 UTC (rev 2197) @@ -2,23 +2,23 @@ """ from scipy.sandbox.odr.odrpack import Model -import numpy as sb +import numpy as np from types import * -def _lin_fcn(B, x, sum=sb.sum): +def _lin_fcn(B, x, sum=np.sum): a, b = B[0], B[1:] b.shape = (b.shape[0], 1) return a + sum(x*b,axis=0) -def _lin_fjb(B, x, concatenate=sb.concatenate, Float=sb.Float, - ones=sb.ones, ravel=sb.ravel): - a = ones((x.shape[-1],), Float) +def _lin_fjb(B, x, concatenate=np.concatenate, + ones=np.ones, ravel=np.ravel): + a = ones((x.shape[-1],), float) res = concatenate((a, ravel(x))) res.shape = (B.shape[-1], x.shape[-1]) return res -def _lin_fjd(B, x, repeat=sb.repeat): +def _lin_fjd(B, x, repeat=np.repeat): b = B[1:] b = repeat(b, (x.shape[-1],)*b.shape[-1],axis=0) b.shape = x.shape @@ -34,21 +34,21 @@ else: m = 1 - return ones((m + 1,), Float) + return np.ones((m + 1,), float) -def _poly_fcn(B, x, powers, power=sb.power, sum=sb.sum): +def _poly_fcn(B, x, powers, power=np.power, sum=np.sum): a, b = B[0], B[1:] b.shape = (b.shape[0], 1) return a + sum(b * power(x, powers),axis=0) -def _poly_fjacb(B, x, powers, power=sb.power, - concatenate=sb.concatenate, Float=sb.Float, ones=sb.ones): - res = concatenate((ones((x.shape[-1],), Float), power(x, powers).flat)) +def _poly_fjacb(B, x, powers, power=np.power, + concatenate=np.concatenate, ones=np.ones): + res = concatenate((ones((x.shape[-1],), float), power(x, powers).flat)) res.shape = (B.shape[-1], x.shape[-1]) return res -def _poly_fjacd(B, x, powers, power=sb.power, sum=sb.sum): +def _poly_fjacd(B, x, powers, power=np.power, sum=np.sum): b = B[1:] b.shape = (b.shape[0], 1) @@ -56,15 +56,14 @@ return sum(b * power(x, powers-1),axis=0) -def _exp_fcn(B, x, exp=sb.exp): +def _exp_fcn(B, x, exp=np.exp): return B[0] + exp(B[1] * x) -def _exp_fjd(B, x, exp=sb.exp): +def _exp_fjd(B, x, exp=np.exp): return B[1] * exp(B[1] * x) -def _exp_fjb(B, x, exp=sb.exp, concatenate=sb.concatenate, ones=sb.ones, - Float=sb.Float): - res = concatenate((ones((x.shape[-1],), sb.Float), x * exp(B[1] * x))) +def _exp_fjb(B, x, exp=np.exp, concatenate=np.concatenate, ones=np.ones): + res = concatenate((ones((x.shape[-1],), float), x * exp(B[1] * x))) res.shape = (2, x.shape[-1]) return res @@ -94,7 +93,7 @@ if type(order) is int: order = range(1, order+1) - powers = sb.asarray(order) + powers = np.asarray(order) powers.shape = (len(powers), 1) len_beta = len(powers) + 1 @@ -102,7 +101,7 @@ def _poly_est(data, len_beta=len_beta): # Eh. Ignore data and return all ones. - return sb.ones((len_beta,), sb.Float) + return np.ones((len_beta,), float) return Model(_poly_fcn, fjacd=_poly_fjd, fjacb=_poly_fjb, estimate=_poly_est, extra_args=(powers,), @@ -120,10 +119,10 @@ return x*B[0] + B[1] def _unilin_fjd(B, x): - return sb.ones(x.shape, sb.Float) * B[0] + return np.ones(x.shape, float) * B[0] -def _unilin_fjb(B, x, cat=sb.concatenate): - _ret = cat((x,sb.ones(x.shape, sb.Float))) +def _unilin_fjb(B, x, cat=np.concatenate): + _ret = cat((x,np.ones(x.shape, float))) _ret.shape = (2,) + x.shape return _ret @@ -137,8 +136,8 @@ def _quad_fjd(B, x): return 2*x*B[0] + B[1] -def _quad_fjb(B,x,cat=sb.concatenate): - _ret = cat((x*x, x, sb.ones(x.shape, Float))) +def _quad_fjb(B,x,cat=np.concatenate): + _ret = cat((x*x, x, np.ones(x.shape, float))) _ret.shape = (3,) + x.shape return _ret Modified: trunk/Lib/sandbox/odr/odrpack.py =================================================================== --- trunk/Lib/sandbox/odr/odrpack.py 2006-09-12 08:37:24 UTC (rev 2196) +++ trunk/Lib/sandbox/odr/odrpack.py 2006-09-12 20:49:35 UTC (rev 2197) @@ -110,6 +110,7 @@ robert.kern at gmail.com """ +import numpy from scipy.sandbox.odr import __odrpack from types import NoneType @@ -854,7 +855,7 @@ 5*q + q*(p+m) + ldwe*ld2we*q) if isinstance(self.work, numpy.ndarray) and self.work.shape == (lwork,)\ - and self.work.dtype == numpy.Float: + and self.work.dtype.str.endswith('f8'): # the existing array is fine return else: From scipy-svn at scipy.org Thu Sep 14 05:32:00 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 14 Sep 2006 04:32:00 -0500 (CDT) Subject: [Scipy-svn] r2198 - in trunk/Lib/io: . tests tests/data Message-ID: <20060914093200.8A38839C076@new.scipy.org> Author: stefan Date: 2006-09-14 04:29:55 -0500 (Thu, 14 Sep 2006) New Revision: 2198 Added: trunk/Lib/io/bytestream.py trunk/Lib/io/mio4.py trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py trunk/Lib/io/tests/data/japanese_utf8.txt trunk/Lib/io/tests/data/test3dmatrix_6.1_SOL2.mat trunk/Lib/io/tests/data/test3dmatrix_7.1_GLNX86.mat trunk/Lib/io/tests/data/testcell_6.1_SOL2.mat trunk/Lib/io/tests/data/testcell_7.1_GLNX86.mat trunk/Lib/io/tests/data/testcellnest_6.1_SOL2.mat trunk/Lib/io/tests/data/testcellnest_7.1_GLNX86.mat trunk/Lib/io/tests/data/testcomplex_4.2c_SOL2.mat trunk/Lib/io/tests/data/testcomplex_6.1_SOL2.mat trunk/Lib/io/tests/data/testcomplex_7.1_GLNX86.mat trunk/Lib/io/tests/data/testdouble_4.2c_SOL2.mat trunk/Lib/io/tests/data/testdouble_6.1_SOL2.mat trunk/Lib/io/tests/data/testdouble_7.1_GLNX86.mat trunk/Lib/io/tests/data/testmatrix_4.2c_SOL2.mat trunk/Lib/io/tests/data/testmatrix_6.1_SOL2.mat trunk/Lib/io/tests/data/testmatrix_7.1_GLNX86.mat trunk/Lib/io/tests/data/testminus_4.2c_SOL2.mat trunk/Lib/io/tests/data/testminus_6.1_SOL2.mat trunk/Lib/io/tests/data/testminus_7.1_GLNX86.mat trunk/Lib/io/tests/data/testmulti_4.2c_SOL2.mat trunk/Lib/io/tests/data/testmulti_7.1_GLNX86.mat trunk/Lib/io/tests/data/testobject_6.1_SOL2.mat trunk/Lib/io/tests/data/testobject_7.1_GLNX86.mat trunk/Lib/io/tests/data/testonechar_4.2c_SOL2.mat trunk/Lib/io/tests/data/testonechar_6.1_SOL2.mat trunk/Lib/io/tests/data/testonechar_7.1_GLNX86.mat trunk/Lib/io/tests/data/testsparse_4.2c_SOL2.mat trunk/Lib/io/tests/data/testsparse_6.1_SOL2.mat trunk/Lib/io/tests/data/testsparse_7.1_GLNX86.mat trunk/Lib/io/tests/data/testsparsecomplex_4.2c_SOL2.mat trunk/Lib/io/tests/data/testsparsecomplex_6.1_SOL2.mat trunk/Lib/io/tests/data/testsparsecomplex_7.1_GLNX86.mat trunk/Lib/io/tests/data/teststring_4.2c_SOL2.mat trunk/Lib/io/tests/data/teststring_6.1_SOL2.mat trunk/Lib/io/tests/data/teststring_7.1_GLNX86.mat trunk/Lib/io/tests/data/teststringarray_4.2c_SOL2.mat trunk/Lib/io/tests/data/teststringarray_6.1_SOL2.mat trunk/Lib/io/tests/data/teststringarray_7.1_GLNX86.mat trunk/Lib/io/tests/data/teststruct_6.1_SOL2.mat trunk/Lib/io/tests/data/teststruct_7.1_GLNX86.mat trunk/Lib/io/tests/data/teststructarr_6.1_SOL2.mat trunk/Lib/io/tests/data/teststructarr_7.1_GLNX86.mat trunk/Lib/io/tests/data/teststructnest_6.1_SOL2.mat trunk/Lib/io/tests/data/teststructnest_7.1_GLNX86.mat trunk/Lib/io/tests/data/testunicode_7.1_GLNX86.mat trunk/Lib/io/tests/gen_unittests.m trunk/Lib/io/tests/gen_unittests4.m trunk/Lib/io/tests/save_test.m Modified: trunk/Lib/io/mio.py trunk/Lib/io/tests/data/test3dmatrix_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testcell_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testcellnest_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testcomplex_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testdouble_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testmatrix_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testminus_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testobject_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testonechar_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testsparse_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testsparsecomplex_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/teststring_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/teststringarray_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/teststruct_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/teststructarr_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/teststructnest_6.5.1_GLNX86.mat trunk/Lib/io/tests/test_mio.py Log: Improved support for reading MATLAB files (contributed by Matthew Brett). Added: trunk/Lib/io/bytestream.py =================================================================== --- trunk/Lib/io/bytestream.py 2006-09-12 20:49:35 UTC (rev 2197) +++ trunk/Lib/io/bytestream.py 2006-09-14 09:29:55 UTC (rev 2198) @@ -0,0 +1,69 @@ +# Author: Matthew Brett + +''' File-like interfact for memmapped array ''' + +from numpy import * + +class ByteStream(object): + ''' Overlays file-like interface on memmapped array + + This may speed up array reading from files + + @byte_array - uint array or string containing bytes + ''' + + def __init__(self, byte_array): + if isinstance(byte_array, ndarray): + if not byte_array.dtype == uint8: + raise ValueError, 'Need uint8 byte array as array input' + self.bytes = byte_array + elif isinstance(byte_array, basestring): + self.bytes = ndarray( + shape=(len(byte_array)), + dtype=uint8, + buffer=byte_array) + else: + raise ValueError, "Need string or byte array as input" + self.array_len = len(byte_array) + self.seek(0) + + # current file position + def get_pos(self): + return self._pos + def set_pos(self, offset): + if offset < 0: + raise IOError, 'Invalid argument' + self._pos = offset + pos = property(get_pos, + set_pos, + None, + 'get/set current position') + + def seek(self, offset, whence=0): + """ Method emulates seek method of file objects """ + if whence == 0: + self.pos = offset + elif whence == 1: # seek relative to the current position + self.pos += offset + elif whence == 2: # relative to end + self.pos = self.array_len + offset + else: + raise ValueError, 'Invalid value %d for whence parameter' % whence + + def tell(self): + return self.pos + + def read(self, num_bytes=-1): + if num_bytes < 0: + num_bytes = self.array_len + if self.pos >= self.array_len: + return [] + next_pos = min(self.pos + num_bytes, self.array_len) + res = self.bytes[self.pos:next_pos] + self.pos = next_pos + return res + + def write(self, data): + assert False, 'Not implemented' + + Modified: trunk/Lib/io/mio.py =================================================================== --- trunk/Lib/io/mio.py 2006-09-12 20:49:35 UTC (rev 2197) +++ trunk/Lib/io/mio.py 2006-09-14 09:29:55 UTC (rev 2198) @@ -1,790 +1,46 @@ -## Automatically adapted for scipy Oct 05, 2005 by convertcode.py +# Authors: Travis Oliphant, Matthew Brett -# Author: Travis Oliphant +""" +Module for reading and writing matlab .mat files +""" -import struct, os, sys -import types -from tempfile import mkstemp -import zlib +import os +import sys -from numpy import array, asarray, empty, obj2sctype, product, reshape, \ - squeeze, transpose, zeros, vstack, ndarray, shape, diff, where, uint8, \ - atleast_1d -import numpyio +from numpy import * -try: - import scipy.sparse - have_sparse = 1 -except ImportError: - have_sparse = 0 +from bytestream import ByteStream +from mio4 import MatFile4Reader, MatFile4Writer +from mio5 import MatFile5Reader -LittleEndian = (sys.byteorder == 'little') -_unit_imag = {'f': array(1j,'F'), 'd': 1j} - -__all__ = ['fopen','loadmat','savemat'] - -def getsize_type(mtype): - if mtype in ['B','uchar','byte','unsigned char','integer*1', 'int8']: - mtype = 'B' - elif mtype in ['S1', 'char', 'char*1']: - mtype = 'B' - elif mtype in ['b', 'schar', 'signed char']: - mtype = 'b' - elif mtype in ['h','short','int16','integer*2']: - mtype = 'h' - elif mtype in ['H','ushort','uint16','unsigned short']: - mtype = 'H' - elif mtype in ['i','int']: - mtype = 'i' - elif mtype in ['I','uint','uint32','unsigned int']: - mtype = 'I' - elif mtype in ['u4','int32','integer*4']: - mtype = 'u4' - elif mtype in ['f','float','float32','real*4', 'real']: - mtype = 'f' - elif mtype in ['d','double','float64','real*8', 'double precision']: - mtype = 'd' - elif mtype in ['F','complex float','complex*8','complex64']: - mtype = 'F' - elif mtype in ['D','complex*16','complex128','complex','complex double']: - mtype = 'D' - else: - mtype = obj2sctype(mtype) - - newarr = empty((1,),mtype) - return newarr.itemsize, newarr.dtype.char - -class fopen(object): - """Class for reading and writing binary files into numpy arrays. - - Inputs: - - file_name -- The complete path name to the file to open. - permission -- Open the file with given permissions: ('r', 'H', 'a') - for reading, writing, or appending. This is the same - as the mode argument in the builtin open command. - format -- The byte-ordering of the file: - (['native', 'n'], ['ieee-le', 'l'], ['ieee-be', 'B']) for - native, little-endian, or big-endian respectively. - - Attributes (Read only): - - bs -- non-zero if byte-swapping is performed on read and write. - format -- 'native', 'ieee-le', or 'ieee-be' - closed -- non-zero if the file is closed. - mode -- permissions with which this file was opened - name -- name of the file - """ - -# Methods: -# -# read -- read data from file and return numpy array -# write -- write to file from numpy array -# fort_read -- read Fortran-formatted binary data from the file. -# fort_write -- write Fortran-formatted binary data to the file. -# rewind -- rewind to beginning of file -# size -- get size of file -# seek -- seek to some position in the file -# tell -- return current position in file -# close -- close the file - - def __init__(self,file_name,permission='rb',format='n'): - if 'b' not in permission: permission += 'b' - if isinstance(file_name, basestring): - self.file = file(file_name, permission) - elif isinstance(file_name, file) and not file_name.closed: - # first argument is an open file - self.file = file_name - else: - raise TypeError, 'Need filename or open file as input' - self.setformat(format) - self.zbuffer = None - - def __del__(self): - try: - self.file.close() - except: - pass - - def close(self): - self.file.close() - - def seek(self, *args): - self.file.seek(*args) - - def tell(self): - return self.file.tell() - - def raw_read(self, size=-1): - """Read raw bytes from file as string.""" - return self.file.read(size) - - def raw_write(self, str): - """Write string to file as raw bytes.""" - return self.file.write(str) - - def setformat(self, format): - """Set the byte-order of the file.""" - if format in ['native','n','default']: - self.bs = False - self.format = 'native' - elif format in ['ieee-le','l','little-endian','le']: - self.bs = not LittleEndian - self.format = 'ieee-le' - elif format in ['ieee-be','B','big-endian','be']: - self.bs = LittleEndian - self.format = 'ieee-be' - else: - raise ValueError, "Unrecognized format: " + format - return - - def write(self,data,mtype=None,bs=None): - """Write to open file object the flattened numpy array data. - - Inputs: - - data -- the numpy array to write. - mtype -- a string indicating the binary type to write. - The default is the type of data. If necessary a cast is made. - unsigned byte : 'B', 'uchar', 'byte' 'unsigned char', 'int8', - 'integer*1' - character : 'S1', 'char', 'char*1' - signed char : 'b', 'schar', 'signed char' - short : 'h', 'short', 'int16', 'integer*2' - unsigned short : 'H', 'ushort','uint16','unsigned short' - int : 'i', 'int' - unsigned int : 'I', 'uint32','uint','unsigned int' - int32 : 'u4', 'int32', 'integer*4' - float : 'f', 'float', 'float32', 'real*4' - double : 'd', 'double', 'float64', 'real*8' - complex float : 'F', 'complex float', 'complex*8', 'complex64' - complex double : 'D', 'complex', 'complex double', 'complex*16', - 'complex128' - """ - if bs is None: - bs = self.bs - else: - bs = (bs == 1) - if isinstance(data, str): - N, buf = len(data), buffer(data) - data = ndarray(shape=(N,),dtype='B',buffer=buf) - else: - data = asarray(data) - if mtype is None: - mtype = data.dtype.char - howmany,mtype = getsize_type(mtype) - count = product(data.shape,axis=0) - numpyio.fwrite(self.file,count,data,mtype,bs) - return - - fwrite = write - - def read(self,count,stype,rtype=None,bs=None,c_is_b=0): - """Read data from file and return it in a numpy array. - - Inputs: - - count -- an integer specifying the number of elements of type - stype to read or a tuple indicating the shape of - the output array. - stype -- The data type of the stored data (see fwrite method). - rtype -- The type of the output array. Same as stype if None. - bs -- Whether or not to byteswap (or use self.bs if None) - c_is_b --- If non-zero then the count is an integer - specifying the total number of bytes to read - (must be a multiple of the size of stype). - - Outputs: (output,) - - output -- a numpy array of type rtype. - """ - if bs is None: - bs = self.bs - else: - bs = (bs == 1) - howmany,stype = getsize_type(stype) - shape = None - if c_is_b: - if count % howmany != 0: - raise ValueError, "When c_is_b is non-zero then " \ - "count is bytes\nand must be multiple of basic size." - count = count / howmany - elif type(count) in [types.TupleType, types.ListType]: - shape = list(count) - # allow -1 to specify unknown dimension size as in reshape - minus_ones = shape.count(-1) - if minus_ones == 0: - count = product(shape,axis=0) - elif minus_ones == 1: - now = self.tell() - self.seek(0,2) - end = self.tell() - self.seek(now) - remaining_bytes = end - now - know_dimensions_size = -product(count,axis=0) * getsize_type(stype)[0] - unknown_dimension_size, illegal = divmod(remaining_bytes, - know_dimensions_size) - if illegal: - raise ValueError("unknown dimension doesn't match filesize") - shape[shape.index(-1)] = unknown_dimension_size - count = product(shape,axis=0) - else: - raise ValueError( - "illegal count; can only specify one unknown dimension") - shape = tuple(shape) - if rtype is None: - rtype = stype - else: - howmany,rtype = getsize_type(rtype) - if count == 0: - return zeros(0,rtype) - retval = numpyio.fread(self.file, count, stype, rtype, bs) - if shape is not None: - retval = resize(retval, shape) - return retval - - fread = read - - def rewind(self,howmany=None): - """Rewind a file to its beginning or by a specified amount. - """ - if howmany is None: - self.seek(0) - else: - self.seek(-howmany,1) - - def size(self): - """Return the size of the file. - """ - try: - sz = self.thesize - except AttributeError: - curpos = self.tell() - self.seek(0,2) - sz = self.tell() - self.seek(curpos) - self.thesize = sz - return sz - - def fort_write(self,fmt,*args): - """Write a Fortran binary record. - - Inputs: - - fmt -- If a string then it represents the same format string as - used by struct.pack. The remaining arguments are passed - to struct.pack. - - If fmt is an array, then this array will be written as - a Fortran record using the output type args[0]. - - *args -- Arguments representing data to write. - """ - if self.format == 'ieee-le': - nfmt = " 0: - sz,mtype = getsize_type(args[0]) - else: - sz,mtype = getsize_type(fmt.dtype.char) - count = product(fmt.shape,axis=0) - strlen = struct.pack(nfmt,count*sz) - self.write(strlen) - numpyio.fwrite(self.file,count,fmt,mtype,self.bs) - self.write(strlen) - else: - raise TypeError, "Unknown type in first argument" - - def fort_read(self,fmt,dtype=None): - """Read a Fortran binary record. - - Inputs: - - fmt -- If dtype is not given this represents a struct.pack - format string to interpret the next record. Otherwise this - argument is ignored. - dtype -- If dtype is not None, then read in the next record as - an array of type dtype. - - Outputs: (data,) - - data -- If dtype is None, then data is a tuple containing the output - of struct.unpack on the next Fortan record. - If dtype is a datatype string, then the next record is - read in as a 1-D array of type datatype. - """ - lookup_dict = {'ieee-le':"<",'ieee-be':">",'native':''} - if dtype is None: - fmt = lookup_dict[self.format] + fmt - numbytes = struct.calcsize(fmt) - nn = struct.calcsize("i"); - if (self.raw_read(nn) == ''): - raise ValueError, "Unexpected end of file..." - strdata = self.raw_read(numbytes) - if strdata == '': - raise ValueError, "Unexpected end of file..." - data = struct.unpack(fmt,strdata) - if (self.raw_read(nn) == ''): - raise ValueError, "Unexpected end of file..." - return data - else: # Ignore format string and read in next record as an array. - fmt = lookup_dict[self.format] + "i" - nn = struct.calcsize(fmt) - nbytestr = self.raw_read(nn) - if nbytestr == '': - raise ValueError, "Unexpected end of file..." - nbytes = struct.unpack(fmt,nbytestr)[0] - howmany, dtype = getsize_type(dtype) - ncount = nbytes / howmany - if ncount*howmany != nbytes: - self.rewind(4) - raise ValueError, "A mismatch between the type requested and the data stored." - if ncount < 0: - raise ValueError, "Negative number of bytes to read:\n file is probably not opened with correct endian-ness." - if ncount == 0: - raise ValueError, "End of file? Zero-bytes to read." - retval = numpyio.fread(self.file, ncount, dtype, dtype, self.bs) - if len(retval) == 1: - retval = retval[0] - if (self.raw_read(nn) == ''): - raise ValueError, "Unexpected end of file..." - return retval - - -class CompressedFopen(fopen): - """ File container for temporary buffer to decompress data """ - def __init__(self, *args, **kwargs): - fd, fname = mkstemp() - super(CompressedFopen, self).__init__( - os.fdopen(fd, 'w+b'), *args, **kwargs) - self.file_name = fname - - def fill(self, bytes): - """ Uncompress buffer in @bytes and write to file """ - self.rewind() - self.raw_write(zlib.decompress(bytes)) - self.rewind() - - def __del__(self): - try: - self.file.truncate(0) - except: - pass - try: - self.close() - except: - pass - try: - os.remove(self.file_name) - except: - pass - -#### MATLAB Version 5 Support ########### - -# Portions of code borrowed and (heavily) adapted -# from matfile.py by Heiko Henkelmann - -## Notice in matfile.py file - -# Copyright (c) 2003 Heiko Henkelmann - -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. - - -class mat_struct: # dummy structure holder - pass - -class mat_obj: # dummy object holder - pass - -miINT8 = 1 -miUINT8 = 2 -miINT16 = 3 -miUINT16 = 4 -miINT32 = 5 -miUINT32 = 6 -miSINGLE = 7 -miDOUBLE = 9 -miINT64 =12 -miUINT64 = 13 -miMATRIX = 14 -miCOMPRESSED = 15 -miUTF8 = 16 -miUTF16 = 17 -miUTF32 = 18 - -miNumbers = ( - miINT8, - miUINT8, - miINT16, - miUINT16, - miINT32, - miUINT32, - miSINGLE, - miDOUBLE, - miINT64, - miUINT64, - ) - -miDataTypes = { - miINT8 : ('miINT8', 1,'b'), - miUINT8 : ('miUINT8', 1,'B'), - miINT16 : ('miINT16', 2,'h'), - miUINT16 :('miUINT16',2,'H'), - miINT32 : ('miINT32',4,'u4'), - miUINT32 : ('miUINT32',4,'I'), - miSINGLE : ('miSINGLE',4,'f'), - miDOUBLE : ('miDOUBLE',8,'d'), - miINT64 : ('miINT64',8,'q'), - miUINT64 : ('miUINT64',8,'Q'), - miMATRIX : ('miMATRIX',0,None), - miUTF8 : ('miUTF8',1,'b'), - miUTF16 : ('miUTF16',2,'h'), - miUTF32 : ('miUTF32',4,'u4'), - } - -''' Before release v7.1 (release 14) matlab used the system default -character encoding scheme padded out to 16-bits. Release 14 and later -use Unicode. When saving character data, matlab R14 checks if it can -be encoded in 7-bit ascii, and saves in that format if so.''' -miCodecs = { - miUINT8: 'ascii', - miUINT16: sys.getdefaultencoding(), - miUTF8: 'utf8', - miUTF16: 'utf16', - miUTF32: 'utf32', - } - -mxCELL_CLASS = 1 -mxSTRUCT_CLASS = 2 -mxOBJECT_CLASS = 3 -mxCHAR_CLASS = 4 -mxSPARSE_CLASS = 5 -mxDOUBLE_CLASS = 6 -mxSINGLE_CLASS = 7 -mxINT8_CLASS = 8 -mxUINT8_CLASS = 9 -mxINT16_CLASS = 10 -mxUINT16_CLASS = 11 -mxINT32_CLASS = 12 -mxUINT32_CLASS = 13 - -mxArrays = ( - mxCHAR_CLASS, - mxDOUBLE_CLASS, - mxSINGLE_CLASS, - mxINT8_CLASS, - mxUINT8_CLASS, - mxINT16_CLASS, - mxUINT16_CLASS, - mxINT32_CLASS, - mxUINT32_CLASS, - ) +def mat_reader_factory(file_name, append_mat=True): + """Create reader for matlab format files -def _parse_header(fid, hdict): - correct_endian = (ord('M')<<8) + ord('I') - # if this number is read no BS - fid.seek(126) # skip to endian detector - endian_test = fid.read(1,'int16') - if (endian_test == correct_endian): openstr = 'n' - else: # must byteswap - if LittleEndian: - openstr = 'B' - else: openstr = 'l' - fid.setformat(openstr) # change byte-order if necessary - fid.rewind() - hdict['__header__'] = fid.raw_read(124).strip(' \t\n\000') - vers = fid.read(1,'int16') - hdict['__version__'] = '%d.%d' % (vers >> 8, vers & 0xFF) - fid.seek(2,1) # move to start of data - return + If name not a full path name, search for the file on the sys.path + list and use the first one found (the current directory is + searched first). -def _skip_padding(fid, numbytes, rowsize): - """ Skip to next row or @rowsize after previous read of @numbytes """ - mod = numbytes % rowsize - if mod: - skip = rowsize-mod - fid.seek(skip,1) - -def _parse_array_flags(fid): - # first 8 bytes are always miUINT32 and 8 --- just a check - dtype, nbytes = fid.read(2,'I') - if (dtype != miUINT32) or (nbytes != 8): - raise IOError, "Invalid MAT file. Perhaps a byte-order problem." - - # read array flags. - rawflags = fid.read(2,'I') - class_ = rawflags[0] & 0xFF - flags = (rawflags[0] & 0xFFFF) >> 8 - # Global and logical fields are currently ignored - if (flags & 8): cmplx = 1 - else: cmplx = 0 - if class_ == mxSPARSE_CLASS: - nzmax = rawflags[1] - else: - nzmax = None - return class_, cmplx, nzmax - -def _parse_mimatrix(fid,bytes): - dclass, cmplx, nzmax =_parse_array_flags(fid) - dims = _get_element(fid) - name = _get_element(fid).tostring() - tupdims = tuple(dims[::-1]) - if dclass in mxArrays: - result, unused, dtype =_get_element(fid, return_name_dtype=True) - if dclass == mxCHAR_CLASS: - en = miCodecs[dtype] - try: - " ".encode(en) - except LookupError: - raise TypeError, 'Character encoding %s not supported' % en - if dtype == miUINT16: - char_len = len(" ".encode(en)) - len(" ".encode(en)) - if char_len == 1: # Need to downsample from 16 bit - result = result.astype(uint8) - elif char_len != 2: - raise TypeError, 'miUNIT16 type cannot use >2 bytes encoding' - result = squeeze(transpose(reshape(result,tupdims))) - dims = result.shape - if len(dims) >= 2: # return array of strings - n_dims = dims[:-1] - string_arr = reshape(result, (product(n_dims,axis=0), dims[-1])) - result = empty(n_dims, dtype=object) - for i in range(0, n_dims[-1]): - result[...,i] = string_arr[i].tostring().decode(en) - else: # return string - result = result.tostring().decode(en) - else: - if cmplx: - imag =_get_element(fid) - try: - result = result + _unit_imag[imag.dtype.char] * imag - except KeyError: - result = result + 1j*imag - result = squeeze(transpose(reshape(result,tupdims))) - - elif dclass == mxCELL_CLASS: - length = product(dims,axis=0) - result = empty(length, dtype=object) - for i in range(length): - result[i] = _get_element(fid) - result = squeeze(transpose(reshape(result,tupdims))) - if not result.shape: - result = result.item() - - elif dclass == mxSTRUCT_CLASS: - length = product(dims,axis=0) - result = zeros(length, object) - namelength = _get_element(fid) - # get field names - names = _get_element(fid) - splitnames = [names[i:i+namelength] for i in \ - xrange(0,len(names),namelength)] - fieldnames = [x.tostring().strip('\x00') - for x in splitnames] - for i in range(length): - result[i] = mat_struct() - for element in fieldnames: - result[i].__dict__[element] = _get_element(fid) - result = squeeze(transpose(reshape(result,tupdims))) - if not result.shape: - result = result.item() - - # object is like a structure with but with a class name - elif dclass == mxOBJECT_CLASS: - class_name = _get_element(fid).tostring() - length = product(dims,axis=0) - result = zeros(length, object) - namelength = _get_element(fid) - # get field names - names = _get_element(fid) - splitnames = [names[i:i+namelength] for i in \ - xrange(0,len(names),namelength)] - fieldnames = [x.tostring().strip('\x00') - for x in splitnames] - for i in range(length): - result[i] = mat_obj() - result[i]._classname = class_name - for element in fieldnames: - result[i].__dict__[element] = _get_element(fid) - result = squeeze(transpose(reshape(result,tupdims))) - if not result.shape: - result = result.item() - - elif dclass == mxSPARSE_CLASS: - rowind = _get_element(fid) - colind = _get_element(fid) - res = _get_element(fid) - if cmplx: - imag = _get_element(fid) - try: - res = res + _unit_imag[imag.dtype.char] * imag - except (KeyError,AttributeError): - res = res + 1j*imag - ''' From the matlab API documentation, last found here: - http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ - @rowind are simply the row indices for all the (@res) non-zero - entries in the sparse array. @rowind has nzmax entries, so - may well have more entries than len(@res), the actual number - of non-zero entries, but @rowind[len(res):] can be discarded - and should be 0. @colind has length (number of columns + 1), - and is such that, if D = diff(@colind), D[j] gives the number - of non-zero entries in column j. Because @rowind values are - stored in column order, this gives the column corresponding to - each @rowind - ''' - cols = empty((len(res)), dtype=rowind.dtype) - col_counts = diff(colind) - start_row = 0 - for i in where(col_counts)[0]: - end_row = start_row + col_counts[i] - cols[start_row:end_row] = i - start_row = end_row - ij = vstack((rowind[:len(res)], cols)) - if have_sparse: - result = scipy.sparse.csc_matrix((res,ij), [dims[0],dims[1]]) - else: - result = (dims, ij, res) - - return result, name - -# Return a Python object for the element -def _get_element(fid, return_name_dtype=False): - """ Return a python object from next element in @fid - - @fid - fopen object for matfile - @return_name_dtype - if True, return tuple of (element, name, dtype) - if False, return element only - """ - name = None - test = fid.raw_read(1) - if len(test) == 0: # nothing left - raise EOFError - else: - fid.rewind(1) - # get the data tag - raw_tag = int(fid.read(1,'I')) - - # check for small data element format - numbytes = raw_tag >> 16 - if numbytes > 0: # small data element format - if numbytes > 4: - raise IOError, "Problem with MAT file: " \ - "too many bytes in small data element format." - dtype = int(raw_tag & 0xFFFF) - el = fid.read(numbytes,miDataTypes[dtype][2],c_is_b=1) - fid.seek(4-numbytes,1) # skip padding - else: - # otherwise parse tag - dtype = raw_tag - numbytes = fid.read(1,'I') - - if dtype == miCOMPRESSED: # compressed data type - if not fid.zbuffer: - fid.zbuffer = CompressedFopen(format=fid.format) - fid.zbuffer.fill(fid.raw_read(numbytes)) - _skip_padding(fid, numbytes, 8) - return _get_element(fid.zbuffer, return_name_dtype) - if dtype != miMATRIX: # therefore basic data type - try: - el = fid.read(numbytes,miDataTypes[dtype][2],c_is_b=1) - except KeyError: - raise ValueError, "Unknown data type" - _skip_padding(fid, numbytes, 8) - else: - # handle miMatrix type - el, name = _parse_mimatrix(fid,numbytes) - - if return_name_dtype: - return el, name, dtype - return el - -def _loadv5(fid,basename): - # return a dictionary from a Matlab version 5-7.1 file - # always contains the variable __header__ - mdict = {} - _parse_header(fid,mdict) - var = 0 - while 1: # file pointer to start of next data - try: - var = var + 1 - el, varname, unused = _get_element(fid, return_name_dtype=True) - if varname is None: - varname = '%s_%04d' % (basename,var) - mdict[varname] = el - except EOFError: - break - return mdict - -### END MATLAB v5 support ############# - -def loadmat(name, mdict=None, appendmat=1, basename='raw'): - """Load the MATLAB(tm) mat file. - - If name is a full path name load it in. Otherwise search for the file - on the sys.path list and load the first one found (the current directory - is searched first). - v4 (Level 1.0), v6 and v7.1 matfiles are supported. Inputs: - name -- name of the mat file (don't need .mat extension if appendmat=1) - dict -- the dictionary to insert into. If none the variables will be - returned in a dictionary. - appendmat -- non-zero to append the .mat extension to the end of the + file_name -- name of the mat file (don't need .mat extension if append_mat=True) + append_mat -- True to append the .mat extension to the end of the given filename. - basename -- for MATLAB(tm) v5 matfiles raw data will have this basename. - - Outputs: - - If dict is None, then a dictionary of names and objects representing the - stored arrays is returned. - """ - - if appendmat and name[-4:] == ".mat": - name = name[:-4] - if os.sep in name: - full_name = name - if appendmat: - full_name = name + ".mat" + """ + if append_mat and file_name[-4:] == ".mat": + file_name = file_name[:-4] + if os.sep in file_name: + full_file_name = file_name + if append_mat: + full_name = file_name + ".mat" else: full_name = None - junk,name = os.path.split(name) + junk,file_name = os.path.split(file_name) for path in sys.path: - test_name = os.path.join(path,name) - if appendmat: + test_name = os.path.join(path,file_name) + if append_mat: test_name += ".mat" try: fid = open(test_name,'rb') @@ -794,143 +50,40 @@ except IOError: pass if full_name is None: - raise IOError, "%s not found on the path." % name + raise IOError, "%s not found on the path." % file_name - fid = fopen(full_name,'rb') - test_vals = fid.fread(4,'byte') + byte_stream = ByteStream(memmap(full_name)) + MR = MatFile4Reader(byte_stream) + if MR.format_looks_right(): + return MR + return MatFile5Reader(byte_stream) - if not (0 in test_vals): # MATLAB version 5 format - fid.rewind() - thisdict = _loadv5(fid,basename) - if mdict is not None: - mdict.update(thisdict) - return - else: - return thisdict - - # The remainder of this function is the v4 codepath - testtype = struct.unpack('i',test_vals.tostring()) - # Check to see if the number is positive and less than 5000. - if testtype[0] < 0 or testtype[0] > 4999: - # wrong byte-order - if LittleEndian: - format = 'ieee-be' - else: - format = 'ieee-le' - else: # otherwise we are O.K. - if LittleEndian: - format = 'ieee-le' - else: - format = 'ieee-be' +def loadmat(file_name, mdict=None, appendmat=True, basename='raw'): + ''' Load Matlab(tm) file - fid.setformat(format) - - length = fid.size() - fid.rewind() # back to the begining - - defnames = [] - thisdict = {} - while 1: - if (fid.tell() == length): - break - header = fid.fread(5,'int') - if len(header) != 5: - fid.close() - print "Warning: Read error in file." - break - M,rest = divmod(int(header[0]),1000) # int is for workaround numpy 0.9.9 bug - O,rest = divmod(rest,100) - P,rest = divmod(rest,10) - T = rest - - if (M > 1): - fid.close() - raise ValueError, "Unsupported binary format." - if (O != 0): - fid.close() - raise ValuError, "Hundreds digit of first integer should be zero." - - if (T not in [0,1]): - fid.close() - raise ValueError, "Cannot handle sparse matrices, yet." - - storage = {0:'d',1:'f',2:'i',3:'h',4:'H',5:'B'}[P] - - varname = fid.fread(header[-1],'char')[:-1] - varname = varname.tostring() - defnames.append(varname) - numels = header[1]*header[2] - if T == 0: # Text data - data = atleast_1d(fid.fread(numels,storage)) - if header[3]: # imaginary data - data2 = fid.fread(numels,storage) - if data.dtype.char == 'f' and data2.dtype.char == 'f': - new = empty(data.shape,'F') - new.real = data - new.imag = data2 - data = new - del(new) - del(data2) - if len(data) > 1: - data=data.reshape((header[2], header[1]) ) - thisdict[varname] = transpose(squeeze(data)) - else: - thisdict[varname] = data - else: - data = atleast_1d(fid.fread(numels,storage,'char')) - if len(data) > 1: - data=data.reshape((header[2], header[1])) - thisdict[varname] = transpose(squeeze(data)) - else: - thisdict[varname] = data - - fid.close() + See docs for mat_reader_factory for details of input options + ''' + MR = mat_reader_factory(file_name, basename) + matfile_dict = MR.get_variables() if mdict is not None: - print "Names defined = ", defnames - mdict.update(thisdict) + mdict.update(matfile_dict) else: - return thisdict + mdict = matfile_dict + return mdict - -def savemat(filename, mdict): +def savemat(file_name, mdict, appendmat=True): """Save a dictionary of names and arrays into the MATLAB-style .mat file. This saves the arrayobjects in the given dictionary to a matlab Version 4 style .mat file. + @appendmat - if true, appends '.mat' extension to filename, if not present """ - storage = {'D':0,'d':0,'F':1,'f':1,'u4':2,'i':2,'h':3,'B':5} - if filename[-4:] != ".mat": - filename = filename + ".mat" - fid = fopen(filename,'wb') - M = not LittleEndian - O = 0 - for variable in mdict.keys(): - var = mdict[variable] - if not isinstance(var, ndarray): - continue - if var.dtype.char == 'S1': - T = 1 - else: - T = 0 - if var.dtype.char == 'b': - var = var.astype('h') - P = storage[var.dtype.char] - fid.fwrite([M*1000+O*100+P*10+T],'int') - - if len(var.shape) == 1: - var=var.reshape((len(var), 1)) - var = transpose(var) - - if len(var.shape) > 2: - var=var.reshape((product(var.shape[:-1],axis=0), var.shape[-1])) - - imagf = var.dtype.char in ['F', 'D'] - fid.fwrite([var.shape[1], var.shape[0], imagf, len(variable)+1],'int') - fid.fwrite(variable+'\x00','char') - if imagf: - fid.fwrite(var.real) - fid.fwrite(var.imag) - else: - fid.fwrite(var) - fid.close() - return + if appendmat and file_name[-4:] != ".mat": + file_name = file_name + ".mat" + file_stream = open(file_name, 'wb') + MW = MatFile4Writer(file_stream) + MW.put_variables(mdict) + file_stream.close() + +if __name__ == '__main__': + D = savemat('test.mat', {'a': 1}) Added: trunk/Lib/io/mio4.py =================================================================== --- trunk/Lib/io/mio4.py 2006-09-12 20:49:35 UTC (rev 2197) +++ trunk/Lib/io/mio4.py 2006-09-14 09:29:55 UTC (rev 2198) @@ -0,0 +1,268 @@ +''' Classes for read / write of matlab 4 files +''' + +from numpy import * + +from miobase import * + +miDOUBLE = 0 +miSINGLE = 1 +miINT32 = 2 +miINT16 = 3 +miUINT16 = 4 +miUINT8 = 5 + +mdtypes_template = { + miDOUBLE: 'f8', + miSINGLE: 'f4', + miINT32: 'i4', + miINT16: 'i2', + miUINT16: 'u2', + miUINT8: 'u1', + 'header': [('mopt', 'i4'), + ('mrows', 'i4'), + ('ncols', 'i4'), + ('imagf', 'i4'), + ('namlen', 'i4')], + 'U1': 'U1', + } + +np_to_mtypes = { + 'f8': miDOUBLE, + 'c16': miDOUBLE, + 'f4': miSINGLE, + 'c8': miSINGLE, + 'i4': miINT32, + 'i2': miINT16, + 'u2': miUINT16, + 'u1': miUINT8, + 'S1': miUINT8, + } + +# matrix classes +mxFULL_CLASS = 0 +mxCHAR_CLASS = 1 +mxSPARSE_CLASS = 2 + +order_codes = { + 0: '<', + 1: '>', + 2: 'VAX D-float', #! + 3: 'VAX G-float', + 4: 'Cray', #!! + } + +class Mat4Header(object): + ''' Place holder for Mat4 header ''' + pass + +class Mat4ArrayReader(MatArrayReader): + ''' Class for reading Mat4 arrays + ''' + + def __init__(self, *args, **kwargs): + super(Mat4ArrayReader,self).__init__(*args, **kwargs) + self._getter_classes = { + mxFULL_CLASS: Mat4FullGetter, + mxCHAR_CLASS: Mat4CharGetter, + mxSPARSE_CLASS: Mat4SparseGetter, + } + + def read_header(self): + ''' Read and return Mat4 matrix header + + Defines: + next_position - start position of next matrix + name + dtype - numpy dtype of matrix + mclass - matlab code for class of matrix + dims - shape of matrix as stored (see sparse reader) + is_complex - True if data are complex + is_char - True if these are char data + ''' + header = Mat4Header() + data = self.read_array(self.dtypes['header']) + header.name = self.read_ztstring(data['namlen']) + if data['mopt'] < 0 or data['mopt'] > 5000: + ValueError, 'Mat 4 mopt wrong format, byteswapping problem?' + M,rest = divmod(data['mopt'], 1000) + O,rest = divmod(rest,100) + P,rest = divmod(rest,10) + T = rest + if O != 0: + raise ValueError, 'O in MOPT integer should be 0, wrong format?' + header.dtype = self.dtypes[P] + header.mclass = T + header.is_char = None + header.is_numeric = None + header.original_dtype = None + header.dims = (data['mrows'], data['ncols']) + header.is_complex = data['imagf'] == 1 + remaining_bytes = header.dtype.itemsize * product(header.dims) + if header.is_complex and not header.mclass == mxSPARSE_CLASS: + remaining_bytes *= 2 + header.next_position = self.mat_stream.tell() + remaining_bytes + return header + + def matrix_getter_factory(self): + header = self.read_header() + return self._getter_classes[header.mclass](self, header) + + +class Mat4MatrixGetter(MatMatrixGetter): + + # Mat4 variables never global or logical + is_global = False + is_logical = False + + def read_hdr_array(self, *args, **kwargs): + ''' Mat4 read array always uses header dtype and dims ''' + return self.read_array( + self.header.dtype, self.dims, *args, **kwargs) + + +class Mat4FullGetter(Mat4MatrixGetter): + def get_raw_array(self): + self.header.is_numeric = True + if self.header.is_complex: + # avoid array copy to save memory + res = self.read_hdr_array(copy=False) + res_j = self.read_hdr_array(copy=False) + return res + (res_j * 1j) + else: + return self.read_hdr_array() + + +class Mat4CharGetter(Mat4MatrixGetter): + def get_raw_array(self): + self.header.is_char = True + arr = self.read_hdr_array().astype(uint8) + # ascii to unicode + S = arr.tostring().decode('ascii') + return ndarray(shape=self.dims, + dtype=dtype('U1'), + buffer = array(S)).copy() + + +class Mat4SparseGetter(Mat4MatrixGetter): + ''' Read sparse matrix type + + Matlab 4 real sparse arrays are saved in a N+1 by 3 array format, + where N is the number of non-zero values. Column 1 values [0:N] + are the (1-based) row indices of the each non-zero value, column 2 + [0:N] are the column indices, column 3 [0:N] are the (real) + values. The last values [-1:0:2] of the rows, column indices are + shape[0] and shape[1] respectively of the output matrix. The last + value for the values column is a padding 0. mrows and ncols values + from the header give the shape of the stored matrix, here [N+1, + 3]. Complex data is saved as a 4 column matrix, where the fourth + column contains the imaginary component of the data; the last + value is again 0 + ''' + def get_raw_array(self): + res = self.read_hdr_array() + tmp = res[:-1,:] + dims = res[-1,0:2] + ij = transpose(tmp[:,0:2]) - 1 # for matlab 1-based indexing + vals = tmp[:,2] + if res.shape[1] == 4: + self.header.is_complex = True + vals = vals + res[:-1,3] * 1j + if have_sparse: + return scipy.sparse.csc_matrix((vals,ij), dims) + return (dims, ij, vals) + + +class MatFile4Reader(MatFileReader): + ''' Reader for Mat4 files ''' + def __init__(self, mat_stream, *args, **kwargs): + self._array_reader = Mat4ArrayReader( + mat_stream, + None, + None, + ) + super(MatFile4Reader, self).__init__(mat_stream, *args, **kwargs) + self._array_reader.processor_func = self.processor_func + + def set_dtypes(self): + self.dtypes = self.convert_dtypes(mdtypes_template) + self._array_reader.dtypes = self.dtypes + + def matrix_getter_factory(self): + return self._array_reader.matrix_getter_factory() + + def format_looks_right(self): + # Matlab 4 files have a zero somewhere in first 4 bytes + self.mat_stream.seek(0) + mopt_bytes = self.read_bytes(4) + self.mat_stream.seek(0) + return 0 in mopt_bytes + + def guess_byte_order(self): + self.mat_stream.seek(0) + mopt = self.read_array(dtype('i4')) + self.mat_stream.seek(0) + if mopt < 0 or mopt > 5000: + return ByteOrder.swapped_code + return ByteOrder.native_code + + +class MatFile4Writer(MatFileWriter): + codec = 'ascii' + + def arr_to_matrix(self, arr): + ''' Convert numeric array to matlab format ''' + dts = arr.dtype.str[1:] + if not dts in np_to_mtypes: + arr = arr.astype('f8') + return atleast_2d(arr) + + def matrix_header(self, var, name): + ''' Return header for matrix array ''' + header = empty((), mdtypes_template['header']) + dt = var.dtype.str[1:] + M = not ByteOrder.little_endian + O = 0 + P = np_to_mtypes[dt] + T = dt == 'S1' # could also be sparse -> 2 + header['mopt'] = M*1000+O*100+P*10+T + dims = var.shape + header['mrows'] = dims[0] + header['ncols'] = dims[1] + header['imagf'] = var.dtype.kind == 'c' + header['namlen'] = len(name) + 1 + return header + + def put_variable(self, var, name): + arr = array(var) + if arr.dtype.hasobject: + raise TypeError, 'Cannot save object arrays in Mat4' + if have_sparse: + if scipy.sparse.issparse(arr): + raise TypeError, 'Cannot save sparse arrays yet' + if arr.dtype.kind in ('U', 'S'): + arr = self.str_to_chars(arr) + else: + arr = self.arr_to_matrix(arr) + dims = arr.shape + if len(dims) > 2: + dims = [product(dims[:-1]), dims[-1]] + arr = reshape(arr, dims) + if arr.dtype.kind == 'U': + # Recode unicode to ascii + dt = 'U' + str(product(dims)) + st_arr = ndarray(shape=(), dtype=dt, buffer=arr) + st = st_arr.item().encode('ascii') + arr = ndarray(shape=dims, dtype='S1', buffer=st) + header = self.matrix_header(arr, name) + self.write_bytes(header) + self.write_string(name + '\0') + if header['imagf']: + self.write_bytes(arr.real) + self.write_bytes(arr.imag) + else: + self.write_bytes(arr) + + def put_variables(self, mdict): + for name, var in mdict.items(): + self.put_variable(var, name) Added: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-09-12 20:49:35 UTC (rev 2197) +++ trunk/Lib/io/mio5.py 2006-09-14 09:29:55 UTC (rev 2198) @@ -0,0 +1,543 @@ +''' Classes for read / write of matlab 5 files +''' + +# Small fragments of current code adapted from matfile.py by Heiko +# Henkelmann + +## Notice in matfile.py file + +# Copyright (c) 2003 Heiko Henkelmann + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to +# deal in the Software without restriction, including without limitation the +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + +import zlib +from copy import copy as pycopy + +from numpy import * + +from bytestream import ByteStream +from miobase import * + +miINT8 = 1 +miUINT8 = 2 +miINT16 = 3 +miUINT16 = 4 +miINT32 = 5 +miUINT32 = 6 +miSINGLE = 7 +miDOUBLE = 9 +miINT64 = 12 +miUINT64 = 13 +miMATRIX = 14 +miCOMPRESSED = 15 +miUTF8 = 16 +miUTF16 = 17 +miUTF32 = 18 + +mxCELL_CLASS = 1 +mxSTRUCT_CLASS = 2 +mxOBJECT_CLASS = 3 +mxCHAR_CLASS = 4 +mxSPARSE_CLASS = 5 +mxDOUBLE_CLASS = 6 +mxSINGLE_CLASS = 7 +mxINT8_CLASS = 8 +mxUINT8_CLASS = 9 +mxINT16_CLASS = 10 +mxUINT16_CLASS = 11 +mxINT32_CLASS = 12 +mxUINT32_CLASS = 13 + +mdtypes_template = { + miINT8: 'i1', + miUINT8: 'u1', + miINT16: 'i2', + miUINT16: 'u2', + miINT32: 'i4', + miUINT32: 'u4', + miSINGLE: 'f4', + miDOUBLE: 'f8', + miINT64: 'i8', + miUINT64: 'u8', + miUTF8: 'u1', + miUTF16: 'u2', + miUTF32: 'u4', + 'file_header': [('description', 'S116'), + ('subsystem_offset', 'i8'), + ('version', 'u2'), + ('endian_test', 'S2')], + 'tag_mdtype': 'u4', + 'tag_byte_count': 'u4', + 'array_flags': [('data_type', 'u4'), + ('byte_count', 'u4'), + ('flags_class','u4'), + ('nzmax', 'u4')], + 'U1': 'U1', + } + +mclass_dtypes_template = { + mxINT8_CLASS: 'i1', + mxUINT8_CLASS: 'u1', + mxINT16_CLASS: 'i2', + mxUINT16_CLASS: 'u2', + mxINT32_CLASS: 'i4', + mxUINT32_CLASS: 'u4', + mxSINGLE_CLASS: 'f4', + mxDOUBLE_CLASS: 'f8', + } + +''' Before release v7.1 (release 14) matlab used the system default +character encoding scheme padded out to 16-bits. Release 14 and later +use Unicode. When saving character data, matlab R14 checks if it can +be encoded in 7-bit ascii, and saves in that format if so.''' + +codecs_template = { + miUTF8: {'codec': 'utf_8', 'width': 1}, + miUTF16: {'codec': 'utf_16', 'width': 2}, + miUTF32: {'codec': 'utf_32','width': 4}, + } + +miUINT16_codec = sys.getdefaultencoding() + +mx_numbers = ( + mxDOUBLE_CLASS, + mxSINGLE_CLASS, + mxINT8_CLASS, + mxUINT8_CLASS, + mxINT16_CLASS, + mxUINT16_CLASS, + mxINT32_CLASS, + mxUINT32_CLASS, + ) + +class mat_struct(object): + ''' Placeholder for holding read data from structs ''' + pass + +class mat_obj(object): + ''' Placeholder for holding read data from objects ''' + pass + +class Mat5Tag(object): + ''' Placeholder for holding tag information ''' + pass + +class Mat5Header(object): + ''' Placeholder for Mat5 header ''' + pass + +class Mat5ArrayFlags(object): + ''' Place holder for array flags ''' + pass + + +class Mat5Arrayreader(MatArrayReader): + ''' Class to get Mat5 arrays + + Provides element reader functions, header reader, matrix reader + factory function + ''' + + def __init__(self, mat_stream, dtypes, processor_func, codecs, class_dtypes): + super(Mat5Arrayreader, self).__init__(mat_stream, + dtypes, + processor_func, + ) + self.codecs = codecs + self.class_dtypes = class_dtypes + + def read_tag(self): + tag = Mat5Tag() + # Check for small data element first + tag.mdtype = int(self.read_array(self.dtypes['tag_mdtype'])) + byte_count = tag.mdtype >> 16 + if byte_count: # small data element format + if byte_count > 4: + raise ValueError, 'Too many bytes for sde format' + tag.byte_count = byte_count + tag.mdtype = tag.mdtype & 0xFFFF + tag.skip = 4 - byte_count + return tag + tag.byte_count = self.read_array( + self.dtypes['tag_byte_count']) + tag.skip = tag.byte_count % 8 and 8 - tag.byte_count % 8 + return tag + + def read_element(self, copy=True): + tag = self.read_tag() + if tag.mdtype == miMATRIX: + header = self.read_header(tag) + return self.header_to_getter(header).get_array() + if tag.mdtype in self.codecs: # encoded char data + raw_str = self.read_bytes(tag.byte_count) + codec = self.codecs[tag.mdtype] + if not codec: + raise TypeError, 'Do not support encoding %d' % tag.mdtype + el = raw_str.tostring().decode(codec) + else: # numeric data + try: + dt = self.dtypes[tag.mdtype] + except KeyError: + raise TypeError, 'Do not know matlab data code %d' % tag.mdtype + el_count = tag.byte_count / dt.itemsize + el = self.read_array(dt, a_shape=(el_count), copy=copy) + if tag.skip: + self.mat_stream.seek(tag.skip, 1) + return el + + def read_header(self, tag): + ''' Read header from Mat5 matrix + + Defines: + next_position - start position of next matrix + name + dtype - numpy dtype of matrix + mclass - matlab code for class of matrix + dims - shape of matrix as stored (see sparse reader) + is_complex - True if data are complex + is_char - True if these are char data + is_global - is a global variable in matlab workspace + is_numeric - is basic numeric matrix + original_dtype - data type when saved from matlab + ''' + if not tag.mdtype == miMATRIX: + raise TypeError, \ + 'Expecting miMATRIX type here, got %d' % tag.mdtype + header = Mat5Header() + header.next_position = (self.mat_stream.pos + + tag.byte_count + + tag.skip) + header.flags = self.read_array_flags() + header.is_complex = header.flags.is_complex + header.is_global = header.flags.is_global + header.is_logical = header.flags.is_logical + header.mclass = header.flags.mclass + header.is_numeric = None + header.original_dtype = None + header.is_char = None + header.dims = self.read_element() + header.name = self.read_element().tostring() + return header + + def read_array_flags(self): + flags = Mat5ArrayFlags() + af = self.read_array(self.dtypes['array_flags']) + flags_class = af['flags_class'] + flags.mclass = flags_class & 0xFF + flags.is_logical = flags_class >> 9 & 1 + flags.is_global = flags_class >> 10 & 1 + flags.is_complex = flags_class >> 11 & 1 + flags.nzmax = af['nzmax'] + return flags + + def matrix_getter_factory(self): + ''' Returns reader for next matrix ''' + tag = self.read_tag() + if tag.mdtype == miCOMPRESSED: + return Mat5ZArrayreader(self, tag).matrix_getter_factory() + header = self.read_header(tag) + return self.header_to_getter(header) + + def header_to_getter(self, header): + mc = header.mclass + if mc in mx_numbers: + return Mat5NumericMatrixGetter(self, header) + if mc == mxSPARSE_CLASS: + return Mat5SparseMatrixGetter(self, header) + if mc == mxCHAR_CLASS: + return Mat5CharMatrixGetter(self, header) + if mc == mxCELL_CLASS: + return Mat5CellMatrixGetter(self, header) + if mc == mxSTRUCT_CLASS: + return Mat5StructMatrixGetter(self, header) + if mc == mxOBJECT_CLASS: + return Mat5ObjectMatrixGetter(self, header) + raise TypeError, 'No reader for class code %s' % mc + + +class Mat5ZArrayreader(Mat5Arrayreader): + ''' Getter for compressed arrays + + Reads and uncompresses gzipped stream on init, providing wrapper + for this new sub-stream. Sets next_position for main stream to + allow skipping over this variable (although we have to read and + uncompress the whole thing anyway to get the name) + ''' + def __init__(self, array_reader, tag): + '''Reads and uncompresses gzipped stream''' + data = array_reader.read_bytes(tag.byte_count) + if tag.skip: + array_reader.mat_stream.seek(tag.skip, 1) + super(Mat5ZArrayreader, self).__init__( + ByteStream(zlib.decompress(data.tostring())), + array_reader.dtypes, + array_reader.processor_func, + array_reader.codecs, + array_reader.class_dtypes) + self.next_position = array_reader.mat_stream.tell() + + def header_to_getter(self, header): + ''' Set next_position to current position in parent stream ''' + header.next_position = self.next_position + return super(Mat5ZArrayreader, self).header_to_getter(header) + + +class Mat5MatrixGetter(MatMatrixGetter): + ''' Base class for getting Mat5 matrices + + Gets current read information from passed array_reader + ''' + + def __init__(self, array_reader, header): + ''' Accepts @array_reader and @header ''' + super(Mat5MatrixGetter, self).__init__(array_reader, header) + self.class_dtypes = array_reader.class_dtypes + self.codecs = array_reader.codecs + self.is_global = header.is_global + + def read_tag(self): + return self.array_reader.read_tag() + + def read_element(self, *args, **kwargs): + return self.array_reader.read_element(*args, **kwargs) + + +class Mat5NumericMatrixGetter(Mat5MatrixGetter): + def get_raw_array(self): + self.header.is_numeric = True + self.header.original_dtype = self.class_dtypes[self.header.mclass] + if self.header.is_complex: + # avoid array copy to save memory + res = self.read_element(copy=False) + res_j = self.read_element(copy=False) + res = res + (res_j * 1j) + else: + res = self.read_element() + return ndarray(shape=self.dims, + dtype=res.dtype, + buffer=res, + order='F') + + +class Mat5SparseMatrixGetter(Mat5MatrixGetter): + def get_raw_array(self): + rowind = self.read_element() + colind = self.read_element() + if self.header.is_complex: + # avoid array copy to save memory + res = self.read_element(copy=False) + res_j = self.read_element(copy=False) + res = res + (res_j * 1j) + else: + res = self.read_element() + ''' From the matlab API documentation, last found here: + http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ + @rowind are simply the row indices for all the (@res) non-zero + entries in the sparse array. @rowind has nzmax entries, so + may well have more entries than len(@res), the actual number + of non-zero entries, but @rowind[len(res):] can be discarded + and should be 0. @colind has length (number of columns + 1), + and is such that, if D = diff(@colind), D[j] gives the number + of non-zero entries in column j. Because @rowind values are + stored in column order, this gives the column corresponding to + each @rowind + ''' + cols = empty((len(res)), dtype=rowind.dtype) + col_counts = diff(colind) + start_row = 0 + for i in where(col_counts)[0]: + end_row = start_row + col_counts[i] + cols[start_row:end_row] = i + start_row = end_row + ij = vstack((rowind[:len(res)], cols)) + if have_sparse: + result = scipy.sparse.csc_matrix((res,ij), + self.dims) + else: + result = (dims, ij, res) + return result + + +class Mat5CharMatrixGetter(Mat5MatrixGetter): + def get_raw_array(self): + self.header.is_char = True + res = self.read_element() + # Convert non-string types to unicode + if isinstance(res, ndarray): + if res.dtype.type == uint16: + codec = miUINT16_codec + if self.codecs['uint16_len'] == 1: + res = res.astype(uint8) + elif res.dtype.type in (uint8, int8): + codec = 'ascii' + else: + raise TypeError, 'Did not expect type %s' % res.dtype + res = res.tostring().decode(codec) + return ndarray(shape=self.dims, + dtype=dtype('U1'), + buffer=array(res), + order='F').copy() + + +class Mat5CellMatrixGetter(Mat5MatrixGetter): + def get_raw_array(self): + # Account for fortran indexing of cells + tupdims = tuple(self.dims[::-1]) + length = product(self.dims) + result = empty(length, dtype=object) + for i in range(length): + result[i] = self.get_item() + result = transpose(reshape(result,tupdims)) + return result + + def get_item(self): + return self.read_element() + + +class Mat5StructMatrixGetter(Mat5CellMatrixGetter): + obj_template = mat_struct() + def get_raw_array(self): + namelength = self.read_element() + # get field names + names = self.read_element() + splitnames = [names[i:i+namelength] for i in \ + xrange(0,len(names),namelength)] + self.obj_template._fieldnames = [x.tostring().strip('\x00') + for x in splitnames] + return super(Mat5StructMatrixGetter, self).get_raw_array() + + def get_item(self): + item = pycopy(self.obj_template) + for element in item._fieldnames: + item.__dict__[element] = self.read_element() + return item + + +class Mat5ObjectMatrixGetter(Mat5StructMatrixGetter): + obj_template = mat_obj() + def get_raw_array(self): + self.obj_template._classname = self.read_element().tostring() + return super(Mat5ObjectMatrixGetter, self).get_raw_array() + + +class MatFile5Reader(MatFileReader): + ''' Reader for Mat 5 mat files + + Adds the following attribute to base class + + @uint16_codec - char codec to use for uint16 char arrays + (defaults to system default codec) + ''' + + def __init__(self, + mat_stream, + byte_order=None, + base_name='raw', + matlab_compatible=False, + squeeze_me=True, + chars_as_strings=True, + uint16_codec=None + ): + self.codecs = {} + self._array_reader = Mat5Arrayreader( + mat_stream, + None, + None, + None, + None, + ) + super(MatFile5Reader, self).__init__( + mat_stream, + byte_order, + base_name, + matlab_compatible, + squeeze_me, + chars_as_strings) + self._array_reader.processor_func = self.processor_func + self.uint16_codec = uint16_codec + + def get_uint16_codec(self): + return self._uint16_codec + def set_uint16_codec(self, uint16_codec): + if not uint16_codec: + uint16_codec = sys.getdefaultencoding() + # Set length of miUINT16 char encoding + self.codecs['uint16_len'] = len(" ".encode(uint16_codec)) \ + - len(" ".encode(uint16_codec)) + self.codecs['uint16_codec'] = uint16_codec + self._array_reader.codecs = self.codecs + self._uint16_codec = uint16_codec + uint16_codec = property(get_uint16_codec, + set_uint16_codec, + None, + 'get/set uint16_codec') + + def set_dtypes(self): + ''' Set dtypes and codecs ''' + self.dtypes = self.convert_dtypes(mdtypes_template) + self.class_dtypes = self.convert_dtypes(mclass_dtypes_template) + codecs = {} + postfix = self.order_code == '<' and '_le' or '_be' + for k, v in codecs_template.items(): + codec = v['codec'] + try: + " ".encode(codec) + except LookupError: + codecs[k] = None + continue + if v['width'] > 1: + codec += postfix + codecs[k] = codec + self.codecs.update(codecs) + self.update_array_reader() + + def update_array_reader(self): + self._array_reader.codecs = self.codecs + self._array_reader.dtypes = self.dtypes + self._array_reader.class_dtypes = self.class_dtypes + + def matrix_getter_factory(self): + return self._array_reader.matrix_getter_factory() + + def guess_byte_order(self): + self.mat_stream.seek(126) + mi = self.read_bytes(2).tostring() + self.mat_stream.seek(0) + return mi == 'IM' and '<' or '>' + + def file_header(self): + ''' Read in mat 5 file header ''' + hdict = {} + hdr = self.read_array(self.dtypes['file_header']) + hdict['__header__'] = hdr['description'].strip(' \t\n\000') + v_major = hdr['version'] >> 8 + v_minor = hdr['version'] & 0xFF + hdict['__version__'] = '%d.%d' % (v_major, v_minor) + return hdict + + def format_looks_right(self): + # Matlab 4 files have a zero somewhere in first 4 bytes + self.mat_stream.seek(0) + mopt_bytes = self.read_bytes(4) + self.mat_stream.seek(0) + return 0 not in mopt_bytes + + +class Mat5Writer(MatFileWriter): + pass Added: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-12 20:49:35 UTC (rev 2197) +++ trunk/Lib/io/miobase.py 2006-09-14 09:29:55 UTC (rev 2198) @@ -0,0 +1,337 @@ +# Authors: Travis Oliphant, Matthew Brett + +""" +Base classes for matlab file stream reading +""" + +import sys + +from numpy import * + +try: + import scipy.sparse + have_sparse = 1 +except ImportError: + have_sparse = 0 + + +class ByteOrder(object): + ''' Namespace for byte ordering ''' + little_endian = sys.byteorder == 'little' + native_code = little_endian and '<' or '>' + swapped_code = little_endian and '>' or '<' + + def to_numpy_code(code): + if code is None: + return ByteOrder.native_code + if code in ('little', '<', 'l', 'L'): + return '<' + elif code in ('BIG', '>', 'B', 'b'): + return '>' + elif code in ('native', '='): + return ByteOrder.native_code + elif code in ('swapped'): + return ByteOrder.swapped_code + else: + raise ValueError, 'We cannot handle byte order %s' % byte_order + to_numpy_code = staticmethod(to_numpy_code) + + +class MatStreamAgent(object): + ''' Base object for readers / getters from mat file streams + + Attaches to initialized stream + + Base class for "getters" - which do store state of what they are + reading on itialization, and therefore need to be initialized + before each read, and "readers" which do not store state, and only + need to be initialized once on object creation + + Implements common array reading functions + + Inputs @mat_steam - MatFileReader object + ''' + + def __init__(self, mat_stream): + self.mat_stream = mat_stream + + def read_bytes(self, num_bytes): + ''' Get next block of data of length @num_bytes ''' + res = self.mat_stream.read(num_bytes) + # Allow stream to return strings instead of bytes + if isinstance(res, basestring): + res = ndarray(shape=(len(res)), + dtype=uint8, + buffer=res) + return res + + def read_array(self, a_dtype, a_shape=(), copy=True): + ''' Generic get of byte stream data of known type and shape + + Inputs + @a_dtype - dtype of array + @a_shape - shape of desired array + @copy - copies array if True + (buffer is usually read only) + a_dtype is assumed to be correct endianness + ''' + num_bytes = a_dtype.itemsize * product(a_shape) + data = self.read_bytes(num_bytes) + arr = ndarray(shape=a_shape, + dtype=a_dtype, + buffer=data, + order='F') + if copy: + arr = arr.copy() + return arr + + def read_ztstring(self, num_bytes): + return self.read_bytes(num_bytes).tostring().strip('\x00') + + +class MatFileReader(MatStreamAgent): + """ Base object for reading mat files + + @initialized byte stream object - file io interface object + @byte_order - byte order ('native', 'little', 'BIG') + in ('native', '=') + or in ('little', '<') + or in ('BIG', '>') + @base_name - base name for unnamed variables + @matlab_compatible - return arrays as matlab saved them + @squeeze_me - whether to squeeze unit dimensions or not + @chars_as_strings - whether to convert char arrays to string arrays + + To make this class functional, you will need to override the + following methods: + + set_dtypes - sets data types defs from byte order + matrix_getter_factory - gives object to fetch next matrix from stream + format_looks_right - returns True if format looks correct for + this file type (Mat4, Mat5) + guess_byte_order - guesses file byte order from file + """ + + def __init__(self, mat_stream, + byte_order=None, + base_name='raw', + matlab_compatible=False, + squeeze_me=True, + chars_as_strings=True, + ): + # Initialize stream + self.mat_stream = mat_stream + self.dtypes = {} + if not byte_order: + byte_order = self.guess_byte_order() + self.order_code = byte_order # sets dtypes and other things too + self.base_name = base_name + self.squeeze_me = squeeze_me + self.chars_as_strings = chars_as_strings + self.matlab_compatible = matlab_compatible + self.processor_func = self.get_processor_func() + + # matlab_compatible property sets squeeze_me and chars_as_strings + def get_matlab_compatible(self): + return self._matlab_compatible + def set_matlab_compatible(self, matlab_compatible): + self._matlab_compatible = matlab_compatible + if matlab_compatible: + self.squeeze_me = False + self.char_as_strings = False + matlab_compatible = property(get_matlab_compatible, + set_matlab_compatible, + None, + 'get/set matlab_compatible property') + + def get_order_code(self): + return self._order_code + def set_order_code(self, order_code): + order_code = ByteOrder.to_numpy_code(order_code) + self._order_code = order_code + self.set_dtypes() + order_code = property(get_order_code, + set_order_code, + None, + 'get/set order code') + + def set_dtypes(self): + assert False, 'Not implemented' + + def convert_dtypes(self, dtype_template): + dtypes = dtype_template.copy() + for k in dtypes: + dtypes[k] = dtype(dtypes[k]).newbyteorder( + self.order_code) + return dtypes + + def matrix_getter_factory(self): + assert False, 'Not implemented' + + def format_looks_right(self): + "Return True if the format looks right for this object" + assert False, 'Not implemented' + + def file_header(self): + return {} + + def guess_byte_order(self): + assert 0, 'Not implemented' + + def get_processor_func(self): + ''' Processing to apply to read matrices + + Function applies options to matrices. We have to pass this + function into the reader routines because Matlab 5 matrices + occur as submatrices - in cell arrays, structs and objects - + so we will not see these in the main variable getting routine + here. + ''' + + def func(arr, header): + if header.is_char and self.chars_as_strings: + # Convert char array to string or array of strings + dims = arr.shape + if len(dims) >= 2: # return array of strings + dtt = self.order_code + 'U' + n_dims = dims[:-1] + str_arr = reshape(arr, + (product(n_dims), + dims[-1])) + arr = empty(n_dims, dtype=object) + for i in range(0, n_dims[-1]): + arr[...,i] = self.chars_to_str(str_arr[i]) + else: # return string + arr = self.chars_to_str(arr) + if self.matlab_compatible: + # Apply options to replicate matlab's load into workspace + if header.is_logical: + arr = arr.astype(bool) + elif header.is_numeric: + # Cast as original matlab type + if header.original_dtype: + arr = arr.astype(header.original_dtype) + if self.squeeze_me: + arr = squeeze(arr) + if not arr.shape: # 0d coverted to scalar + arr = arr.item() + return arr + return func + + def chars_to_str(self, str_arr): + ''' Convert string array to string ''' + dt = dtype('U' + str(product(str_arr.shape))) + return ndarray(shape=(), + dtype = dt, + buffer = str_arr.copy()).item() + + def get_variables(self, variable_names=None): + ''' get variables from stream as dictionary + + @variable_names - optional list of variable names to get + + If variable_names is None, then get all variables in file + ''' + self.mat_stream.seek(0) + mdict = self.file_header() + mdict['__globals__'] = [] + while not self.end_of_stream(): + getter = self.matrix_getter_factory() + name = getter.name + if variable_names and name not in variable_names: + getter.to_next() + continue + res = getter.get_array() + mdict[name] = res + if getter.is_global: + mdict['__globals__'].append(name) + if variable_names: + variable_names.remove(name) + if not variable_names: + break + return mdict + + def end_of_stream(self): + b = self.read_bytes(1) + self.mat_stream.seek(-1,1) + return len(b) == 0 + +class MatMatrixGetter(MatStreamAgent): + """ Base class for matrix getters + + Getters are stateful versions of agents, and record state of + current read on initialization, so need to be created for each + read - one-shot objects. + + MatrixGetters are initialized with the content of the matrix + header + + Accepts + @array_reader - array reading object (see below) + @header - header for matrix being read + """ + + def __init__(self, array_reader, header): + super(MatMatrixGetter, self).__init__(array_reader.mat_stream) + self.array_reader = array_reader + self.dtypes = array_reader.dtypes + self.header = header + self.name = header.name + self.next_position = header.next_position + self.dims = header.dims + self.data_position = self.mat_stream.tell() + + def get_array(self): + ''' Gets an array from matrix, and applies any necessary processing ''' + if not self.mat_stream.tell() == self.data_position: + self.mat_stream.seek(self.data_position) + arr = self.get_raw_array() + return self.array_reader.processor_func(arr, self.header) + + def get_raw_array(self): + assert False, 'Not implemented' + + def to_next(self): + self.mat_stream.seek(self.next_position) + + +class MatArrayReader(MatStreamAgent): + ''' Base class for array readers + + The array_reader contains information about the current reading + process, such as byte ordered dtypes and the processing function + to apply to matrices as they are read, as well as routines for + reading matrix compenents. + ''' + + def __init__(self, mat_stream, dtypes, processor_func): + self.mat_stream = mat_stream + self.dtypes = dtypes + self.processor_func = processor_func + + def matrix_getter_factory(self): + assert False, 'Not implemented' + + +class MatFileWriter(object): + ''' Base type for writing mat files ''' + def __init__(self, file_stream): + self.file_stream = file_stream + + def str_to_chars(self, arr): + ''' Converts string array to matlab char array ''' + dims = list(arr.shape) + if not dims: + dims = [1] + dims.append(int(arr.dtype.str[2:])) + num_els = product(dims) + dt = dtype(arr.dtype.kind + '1') + return ndarray(shape=dims, dtype=dt, buffer=arr) + + def write_bytes(self, arr): + arr.dtype.newbyteorder(ByteOrder.native_code) + s = arr.tostring(order='F') + self.file_stream.write(s) + + def write_string(self, s): + self.file_stream.write(s) Added: trunk/Lib/io/tests/data/japanese_utf8.txt =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/japanese_utf8.txt ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/test3dmatrix_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/test3dmatrix_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/test3dmatrix_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/test3dmatrix_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/test3dmatrix_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testcell_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testcell_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testcell_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testcell_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testcell_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testcellnest_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testcellnest_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testcellnest_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testcellnest_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testcellnest_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testcomplex_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testcomplex_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testcomplex_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testcomplex_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testcomplex_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testcomplex_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testcomplex_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testdouble_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testdouble_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testdouble_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testdouble_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testdouble_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testdouble_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testdouble_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testmatrix_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testmatrix_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testmatrix_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testmatrix_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testmatrix_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testmatrix_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testmatrix_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testminus_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testminus_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testminus_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testminus_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testminus_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testminus_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testminus_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testmulti_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testmulti_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testmulti_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testmulti_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testobject_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testobject_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testobject_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testobject_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testobject_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testonechar_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testonechar_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testonechar_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testonechar_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testonechar_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testonechar_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testonechar_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testsparse_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testsparse_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testsparse_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testsparse_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testsparse_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testsparse_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testsparse_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testsparsecomplex_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testsparsecomplex_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testsparsecomplex_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testsparsecomplex_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/testsparsecomplex_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/testsparsecomplex_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testsparsecomplex_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/teststring_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststring_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/teststring_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststring_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/teststring_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/teststring_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststring_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/teststringarray_4.2c_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststringarray_4.2c_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/teststringarray_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststringarray_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/teststringarray_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/teststringarray_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststringarray_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/teststruct_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststruct_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/teststruct_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/teststruct_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststruct_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/teststructarr_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststructarr_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/teststructarr_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/teststructarr_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststructarr_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/teststructnest_6.1_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststructnest_6.1_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Lib/io/tests/data/teststructnest_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Added: trunk/Lib/io/tests/data/teststructnest_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/teststructnest_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testunicode_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testunicode_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/gen_unittests.m =================================================================== --- trunk/Lib/io/tests/gen_unittests.m 2006-09-12 20:49:35 UTC (rev 2197) +++ trunk/Lib/io/tests/gen_unittests.m 2006-09-14 09:29:55 UTC (rev 2198) @@ -0,0 +1,88 @@ +% Generates mat files for loadmat unit tests +% This is the version for matlab 5 and higher +% Uses save_test.m function + +% work out matlab version and file suffix for test files +global FILEPREFIX FILESUFFIX +FILEPREFIX = [fullfile(pwd, 'data') filesep]; +temp = ver('MATLAB'); +mlv = temp.Version; +FILESUFFIX = ['_' mlv '_' computer '.mat']; + +% basic double array +save_test('testdouble', 0:pi/4:2*pi); + +% string +save_test('teststring', '"Do nine men interpret?" "Nine men," I nod.') + +% complex +theta = 0:pi/4:2*pi; +save_test('testcomplex', cos(theta) + 1j*sin(theta)); + +% asymmetric array to check indexing +a = zeros(3, 5); +a(:,1) = [1:3]'; +a(1,:) = 1:5; + +% 2D matrix +save_test('testmatrix', a); + +% minus number - tests signed int +save_test('testminus', -1); + +% single character +save_test('testonechar', 'r'); + +% string array +save_test('teststringarray', ['one '; 'two '; 'three']); + +% sparse array +save_test('testsparse', sparse(a)); + +% sparse complex array +b = sparse(a); +b(1,1) = b(1,1) + j; +save_test('testsparsecomplex', b); + +% Two variables in same file +save([FILEPREFIX 'testmulti' FILESUFFIX], 'a', 'theta') + +% struct +save_test('teststruct', ... + struct('stringfield','Rats live on no evil star.',... + 'doublefield',[sqrt(2) exp(1) pi],... + 'complexfield',(1+1j)*[sqrt(2) exp(1) pi])); + +% cell +save_test('testcell', ... + {['This cell contains this string and 3 arrays of increasing' ... + ' length'], 1., 1.:2., 1.:3.}); + +% 3D matrix +save_test('test3dmatrix', reshape(1:24,[2 3 4])) + +% nested cell array +save_test('testcellnest', {1, {2, 3, {4, 5}}}); + +% nested struct +save_test('teststructnest', struct('one', 1, 'two', ... + struct('three', 'number 3'))); + +% array of struct +save_test('teststructarr', [struct('one', 1, 'two', 2) ... + struct('one', 'number 1', 'two', 'number 2')]); + +% matlab object +save_test('testobject', inline('x')) + +% array of matlab objects +%save_test('testobjarr', [inline('x') inline('y')]) + +% unicode test +if str2num(mlv) > 7 % function added 7.0.1 + fid = fopen([FILEPREFIX 'japanese_utf8.txt']); + from_japan = fread(fid, 'uint8')'; + fclose(fid); + save_test('testunicode', native2unicode(from_japan, 'utf-8')); +end + \ No newline at end of file Added: trunk/Lib/io/tests/gen_unittests4.m =================================================================== --- trunk/Lib/io/tests/gen_unittests4.m 2006-09-12 20:49:35 UTC (rev 2197) +++ trunk/Lib/io/tests/gen_unittests4.m 2006-09-14 09:29:55 UTC (rev 2198) @@ -0,0 +1,50 @@ +% Generates mat files for loadmat unit tests +% Uses save_test.m function +% This is the version for matlab 4 + +% work out matlab version and file suffix for test files +global FILEPREFIX FILESUFFIX +sepchar = '/'; +if strcmp(computer, 'PCWIN'), sepchar = '\'; end +FILEPREFIX = [pwd sepchar 'data' sepchar]; +mlv = version; +FILESUFFIX = ['_' mlv '_' computer '.mat']; + +% basic double array +save_test('testdouble', 0:pi/4:2*pi); + +% string +save_test('teststring', '"Do nine men interpret?" "Nine men," I nod.') + +% complex +theta = 0:pi/4:2*pi; +save_test('testcomplex', cos(theta) + 1j*sin(theta)); + +% asymmetric array to check indexing +a = zeros(3, 5); +a(:,1) = [1:3]'; +a(1,:) = 1:5; + +% 2D matrix +save_test('testmatrix', a); + +% minus number - tests signed int +save_test('testminus', -1); + +% single character +save_test('testonechar', 'r'); + +% string array +save_test('teststringarray', ['one '; 'two '; 'three']); + +% sparse array +save_test('testsparse', sparse(a)); + +% sparse complex array +b = sparse(a); +b(1,1) = b(1,1) + j; +save_test('testsparsecomplex', b); + +% Two variables in same file +save([FILEPREFIX 'testmulti' FILESUFFIX], 'a', 'theta') + Added: trunk/Lib/io/tests/save_test.m =================================================================== --- trunk/Lib/io/tests/save_test.m 2006-09-12 20:49:35 UTC (rev 2197) +++ trunk/Lib/io/tests/save_test.m 2006-09-14 09:29:55 UTC (rev 2198) @@ -0,0 +1,6 @@ +function save_test(test_name, v) +% saves variable passed in m with filename from prefix + +global FILEPREFIX FILESUFFIX +eval([test_name ' = v;']); +save([FILEPREFIX test_name FILESUFFIX], test_name) \ No newline at end of file Modified: trunk/Lib/io/tests/test_mio.py =================================================================== --- trunk/Lib/io/tests/test_mio.py 2006-09-12 20:49:35 UTC (rev 2197) +++ trunk/Lib/io/tests/test_mio.py 2006-09-14 09:29:55 UTC (rev 2198) @@ -2,6 +2,7 @@ import os from glob import glob +from tempfile import mkstemp from numpy.testing import set_package_path, restore_path, ScipyTestCase, ScipyTest from numpy.testing import assert_equal, assert_array_almost_equal from numpy import arange, array, eye, pi, cos, exp, sin, sqrt, ndarray, \ @@ -9,7 +10,9 @@ import scipy.sparse as SP set_package_path() -from scipy.io.mio import loadmat, mat_obj, mat_struct +from scipy.io.mio import loadmat, savemat +from scipy.io.mio5 import mat_obj, mat_struct +from scipy.io.mio4 import MatFile4Writer restore_path() try: # Python 2.3 support @@ -17,16 +20,19 @@ except: pass +test_data_path = os.path.join(os.path.dirname(__file__), './data') + class test_mio_array(ScipyTestCase): def __init__(self, *args, **kwargs): super(test_mio_array, self).__init__(*args, **kwargs) - self.test_data_path = os.path.join(os.path.dirname(__file__), './data') def _check_level(self, label, expected, actual): - """ Check one level of a potentially nested dictionary / list """ + """ Check one level of a potentially nested object / list """ # object array is returned from cell array in mat file + typex = type(expected) + typac = type(actual) if isinstance(expected, ndarray) and expected.dtype.hasobject == 1: - assert type(expected) is type(actual), "Different types at %s" % label + assert typex is typac, "Different types at %s" % label assert len(expected) == len(actual), "Different list lengths at %s" % label for i, ev in enumerate(expected): level_label = "%s, [%d], " % (label, i) @@ -34,17 +40,17 @@ return # object, as container for matlab structs and objects elif isinstance(expected, mat_struct) or isinstance(expected, mat_obj): - assert isinstance(actual, type(expected)), \ - "Different types %s and %s at %s" % label + assert isinstance(actual, typex), \ + "Different types %s and %s at %s" % (typex, typac, label) ex_fields = dir(expected) ac_fields = dir(actual) for k in ex_fields: if k.startswith('__') and k.endswith('__'): continue - assert k in ac_fields, "Missing field at %s" % label + assert k in ac_fields, "Missing property at %s" % label ev = expected.__dict__[k] v = actual.__dict__[k] - level_label = "%s, field %s, " % (label, k) + level_label = "%s, property %s, " % (label, k) self._check_level(level_label, ev, v) return # hoping this is a single value, which might be an array @@ -55,32 +61,43 @@ err_msg = label, decimal = 5) elif isinstance(expected, ndarray): - assert isinstance(actual, ndarray), "Expected ndarray at %s" % label - assert_array_almost_equal(actual, expected, err_msg=label, decimal = 5) + if expected.shape: # allow scalar and 0d array comparisons + assert isinstance(actual, ndarray), "Expected ndarray at %s" % label + assert_array_almost_equal(actual, expected, err_msg=label, decimal=5) else: - assert isinstance(expected, type(actual)), \ - "Types %s and %s do not match at %s" % (type(expected), type(actual), label) + assert isinstance(expected, typac), \ + "Types %s and %s do not match at %s" % (typex, typac, label) assert_equal(actual, expected, err_msg=label) - def _check_case(self, name, case): - filt = os.path.join(self.test_data_path, 'test%s_*.mat' % name) - files = glob(filt) - assert files, "No files for test %s using filter %s" % (name, filt) - for f in files: - matdict = loadmat(f) - label = "Test '%s', file:%s" % (name, f) + def _check_case(self, name, files, case): + for file_name in files: + matdict = loadmat(file_name) + label = "test %s; file %s" % (name, file_name) for k, expected in case.items(): k_label = "%s, variable %s" % (label, k) assert k in matdict, "Missing key at %s" % k_label self._check_level(k_label, expected, matdict[k]) - # Add the actual tests dynamically, with given parameters - def _make_check_case(name, expected): + # Add the load tests dynamically, with given parameters + def _make_check_case(name, files, expected): def cc(self): - self._check_case(name, expected) + self._check_case(name, files, expected) cc.__doc__ = "check loadmat case %s" % name return cc + # Add the round trip tests dynamically, with given parameters + def _make_rt_check_case(name, expected): + def cc(self): + (fd, fname) = mkstemp('.mat') + file_stream = os.fdopen(fd, 'wb') + MW = MatFile4Writer(file_stream) + MW.put_variables(expected) + file_stream.close() + self._check_case(name, [fname], expected) + os.remove(fname) + cc.__doc__ = "check loadmat case %s" % name + return cc + # Define cases to test theta = pi/4*arange(9,dtype=float) case_table = [ @@ -100,7 +117,7 @@ 'expected': {'testcell': array([u'This cell contains this string and 3 arrays of '+\ 'increasing length', - array([1]), array([1,2]), array([1,2,3])], + array(1), array([1,2]), array([1,2,3])], dtype=object)} }) st = mat_struct() @@ -133,8 +150,13 @@ 'expected': {'testsparsecomplex': SP.csc_matrix(B)}, }) case_table.append( + {'name': 'multi', + 'expected': {'theta': theta, + 'a': A}, + }) + case_table.append( {'name': 'minus', - 'expected': {'testminus': array([-1])}, + 'expected': {'testminus': array(-1)}, }) case_table.append( {'name': 'onechar', @@ -144,58 +166,83 @@ {'name': 'stringarray', 'expected': {'teststringarray': array([u'one ', u'two ', u'three'], dtype=object)}, }) + a = array([array(1), + array([array(2), array(3), + array([array(4), array(5)], + dtype=object)], + dtype=object)], + dtype=object) case_table.append( {'name': 'cellnest', - 'expected': {'testcellnest': array([array([1]), - array([array([2]), array([3]), - array([array([4]), array([5])], - dtype=object)], - dtype=object)], - dtype=object)}, + 'expected': {'testcellnest': a}, }) st = mat_struct() - st.one = array([1]) + st.one = array(1) st.two = mat_struct() st.two.three = u'number 3' case_table.append( {'name': 'structnest', 'expected': {'teststructnest': st} }) - a = empty((2), dtype=object) - a[0], a[1] = mat_struct(), mat_struct() - a[0].one = array([1]) - a[0].two = array([2]) + a = array([mat_struct(), mat_struct()]) + a[0].one = array(1) + a[0].two = array(2) a[1].one = u'number 1' a[1].two = u'number 2' case_table.append( {'name': 'structarr', 'expected': {'teststructarr': a} }) - a = mat_obj() a._classname = 'inline' a.expr = u'x' a.inputExpr = u' x = INLINE_INPUTS_{1};' a.args = u'x' - a.isEmpty = array([0]) - a.numArgs = array([1]) - a.version = array([1]) + a.isEmpty = array(0) + a.numArgs = array(1) + a.version = array(1) case_table.append( {'name': 'object', 'expected': {'testobject': a} }) - + u_str = file( + os.path.join(test_data_path, 'japanese_utf8.txt'), + 'rb').read().decode('utf-8') case_table.append( - {'name': 'vec', - 'expected': {'fit_params': array([1.27661364061704e+09,7.51130255826677e-03]), - 'xdot_filt': array([8.11154474752301e-13,1.28504039006994e-11])} - }) - - # add tests + {'name': 'unicode', + 'expected': {'testunicode': u_str} + }) + # add load tests for case in case_table: name = case['name'] expected = case['expected'] - exec 'check_%s = _make_check_case(name, expected)' % name + filt = os.path.join(test_data_path, 'test%s_*.mat' % name) + files = glob(filt) + assert files, "No files for test %s using filter %s" % (name, filt) + exec 'check_%s = _make_check_case(name, files, expected)' % name + # round trip tests + case_table = [ + {'name': 'double', + 'expected': {'testdouble': theta} + }] + case_table.append( + {'name': 'string', + 'expected': {'teststring': u'"Do nine men interpret?" "Nine men," I nod.'}, + }) + case_table.append( + {'name': 'complex', + 'expected': {'testcomplex': cos(theta) + 1j*sin(theta)} + }) + case_table.append( + {'name': 'multi', + 'expected': {'theta': theta, + 'a': A}, + }) + for case in case_table: + name = case['name'] + '_round_trip' + expected = case['expected'] + exec 'check_%s = _make_rt_check_case(name, expected)' % name + if __name__ == "__main__": ScipyTest().run() From scipy-svn at scipy.org Fri Sep 15 16:24:29 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 15 Sep 2006 15:24:29 -0500 (CDT) Subject: [Scipy-svn] r2199 - trunk/Lib/ndimage/src Message-ID: <20060915202429.8E6F739C016@new.scipy.org> Author: stefan Date: 2006-09-15 15:24:08 -0500 (Fri, 15 Sep 2006) New Revision: 2199 Modified: trunk/Lib/ndimage/src/nd_image.c trunk/Lib/ndimage/src/ni_filters.c trunk/Lib/ndimage/src/ni_filters.h Log: Fix ndimage for 64-bit systems (by Matthew Brett). Modified: trunk/Lib/ndimage/src/nd_image.c =================================================================== --- trunk/Lib/ndimage/src/nd_image.c 2006-09-14 09:29:55 UTC (rev 2198) +++ trunk/Lib/ndimage/src/nd_image.c 2006-09-15 20:24:08 UTC (rev 2199) @@ -305,7 +305,7 @@ long origin, filter_size; double cval; - if (!PyArg_ParseTuple(args, "O&OiiO&idlOO", NI_ObjectToInputArray, + if (!PyArg_ParseTuple(args, "O&OliO&idlOO", NI_ObjectToInputArray, &input, &fnc, &filter_size, &axis, NI_ObjectToOutputArray, &output, &mode, &cval, &origin, &extra_arguments, &extra_keywords)) goto exit; Modified: trunk/Lib/ndimage/src/ni_filters.c =================================================================== --- trunk/Lib/ndimage/src/ni_filters.c 2006-09-14 09:29:55 UTC (rev 2198) +++ trunk/Lib/ndimage/src/ni_filters.c 2006-09-15 20:24:08 UTC (rev 2199) @@ -274,9 +274,9 @@ } int -NI_UniformFilter1D(PyArrayObject *input, maybelong filter_size, +NI_UniformFilter1D(PyArrayObject *input, long filter_size, int axis, PyArrayObject *output, NI_ExtendMode mode, - double cval, maybelong origin) + double cval, long origin) { maybelong lines, kk, ll, length, size1, size2; int more; @@ -336,9 +336,9 @@ } int -NI_MinOrMaxFilter1D(PyArrayObject *input, maybelong filter_size, +NI_MinOrMaxFilter1D(PyArrayObject *input, long filter_size, int axis, PyArrayObject *output, NI_ExtendMode mode, - double cval, maybelong origin, int minimum) + double cval, long origin, int minimum) { maybelong lines, kk, jj, ll, length, size1, size2; int more; @@ -705,8 +705,8 @@ int NI_GenericFilter1D(PyArrayObject *input, int (*function)(double*, maybelong, double*, maybelong, void*), - void* data, maybelong filter_size, int axis, PyArrayObject *output, - NI_ExtendMode mode, double cval, maybelong origin) + void* data, long filter_size, int axis, PyArrayObject *output, + NI_ExtendMode mode, double cval, long origin) { int more; maybelong ii, lines, length, size1, size2; Modified: trunk/Lib/ndimage/src/ni_filters.h =================================================================== --- trunk/Lib/ndimage/src/ni_filters.h 2006-09-14 09:29:55 UTC (rev 2198) +++ trunk/Lib/ndimage/src/ni_filters.h 2006-09-15 20:24:08 UTC (rev 2199) @@ -36,18 +36,18 @@ NI_ExtendMode, double, maybelong); int NI_Correlate(PyArrayObject*, PyArrayObject*, PyArrayObject*, NI_ExtendMode, double, maybelong*); -int NI_UniformFilter1D(PyArrayObject*, maybelong, int, PyArrayObject*, - NI_ExtendMode, double, maybelong); -int NI_MinOrMaxFilter1D(PyArrayObject*, maybelong, int, PyArrayObject*, - NI_ExtendMode, double, maybelong, int); +int NI_UniformFilter1D(PyArrayObject*, long, int, PyArrayObject*, + NI_ExtendMode, double, long); +int NI_MinOrMaxFilter1D(PyArrayObject*, long, int, PyArrayObject*, + NI_ExtendMode, double, long, int); int NI_MinOrMaxFilter(PyArrayObject*, PyArrayObject*, PyArrayObject*, PyArrayObject*, NI_ExtendMode, double, maybelong*, int); int NI_RankFilter(PyArrayObject*, int, PyArrayObject*, PyArrayObject*, NI_ExtendMode, double, maybelong*); int NI_GenericFilter1D(PyArrayObject*, int (*)(double*, maybelong, - double*, maybelong, void*), void*, maybelong, int, - PyArrayObject*, NI_ExtendMode, double, maybelong); + double*, maybelong, void*), void*, long, int, + PyArrayObject*, NI_ExtendMode, double, long); int NI_GenericFilter(PyArrayObject*, int (*)(double*, maybelong, double*, void*), void*, PyArrayObject*, PyArrayObject*, NI_ExtendMode, double, maybelong*); From scipy-svn at scipy.org Mon Sep 18 03:34:49 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 18 Sep 2006 02:34:49 -0500 (CDT) Subject: [Scipy-svn] r2200 - trunk/Lib/io Message-ID: <20060918073449.AB6AD39C049@new.scipy.org> Author: stefan Date: 2006-09-18 02:34:13 -0500 (Mon, 18 Sep 2006) New Revision: 2200 Modified: trunk/Lib/io/bytestream.py trunk/Lib/io/mio4.py trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py Log: Code cleanup, minor bugfixes in mio (by Matthew Brett). Modified: trunk/Lib/io/bytestream.py =================================================================== --- trunk/Lib/io/bytestream.py 2006-09-15 20:24:08 UTC (rev 2199) +++ trunk/Lib/io/bytestream.py 2006-09-18 07:34:13 UTC (rev 2200) @@ -57,7 +57,7 @@ if num_bytes < 0: num_bytes = self.array_len if self.pos >= self.array_len: - return [] + return array([], dtype=uint8) next_pos = min(self.pos + num_bytes, self.array_len) res = self.bytes[self.pos:next_pos] self.pos = next_pos Modified: trunk/Lib/io/mio4.py =================================================================== --- trunk/Lib/io/mio4.py 2006-09-15 20:24:08 UTC (rev 2199) +++ trunk/Lib/io/mio4.py 2006-09-18 07:34:13 UTC (rev 2200) @@ -53,9 +53,31 @@ } class Mat4Header(object): - ''' Place holder for Mat4 header ''' - pass + ''' Place holder for Mat4 header + Defines: + next_position - start position of next matrix + name + dims - shape of matrix as stored (see sparse reader) + dtype - numpy dtype of matrix + mclass - matlab code for class of matrix + is_char - True if these are char data + is_numeric - True if these are numeric data + is_complex - True if data are complex + original_dtype - data type in matlab workspace + ''' + def __init__(self): + self.next_position = None + self.name = '' + self.dims = () + self.dtype = None + self.mclass = None + self.is_char = None + self.is_numeric = None + self.is_complex = None + self.original_dtype = None + + class Mat4ArrayReader(MatArrayReader): ''' Class for reading Mat4 arrays ''' @@ -70,15 +92,6 @@ def read_header(self): ''' Read and return Mat4 matrix header - - Defines: - next_position - start position of next matrix - name - dtype - numpy dtype of matrix - mclass - matlab code for class of matrix - dims - shape of matrix as stored (see sparse reader) - is_complex - True if data are complex - is_char - True if these are char data ''' header = Mat4Header() data = self.read_array(self.dtypes['header']) @@ -93,9 +106,6 @@ raise ValueError, 'O in MOPT integer should be 0, wrong format?' header.dtype = self.dtypes[P] header.mclass = T - header.is_char = None - header.is_numeric = None - header.original_dtype = None header.dims = (data['mrows'], data['ncols']) header.is_complex = data['imagf'] == 1 remaining_bytes = header.dtype.itemsize * product(header.dims) @@ -124,6 +134,7 @@ class Mat4FullGetter(Mat4MatrixGetter): def get_raw_array(self): self.header.is_numeric = True + self.header.original_dtype = dtype(float64) if self.header.is_complex: # avoid array copy to save memory res = self.read_hdr_array(copy=False) @@ -160,6 +171,7 @@ value is again 0 ''' def get_raw_array(self): + self.header.original_dtype = dtype(float64) res = self.read_hdr_array() tmp = res[:-1,:] dims = res[-1,0:2] Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-09-15 20:24:08 UTC (rev 2199) +++ trunk/Lib/io/mio5.py 2006-09-18 07:34:13 UTC (rev 2200) @@ -139,15 +139,40 @@ pass class Mat5Header(object): - ''' Placeholder for Mat5 header ''' - pass + ''' Placeholder for Mat5 header + Defines: + next_position - start position of next matrix + name + dtype - numpy dtype of matrix + mclass - matlab code for class of matrix + dims - shape of matrix as stored (see sparse reader) + is_complex - True if data are complex + is_char - True if these are char data + is_global - is a global variable in matlab workspace + is_numeric - is basic numeric matrix + original_dtype - data type when saved from matlab + ''' + def __init__(self): + self.next_position = None + self.is_empty = False + self.flags = None + self.is_complex = False + self.is_global = False + self.is_logical = False + self.mclass = 0 + self.is_numeric = None + self.original_dtype = None + self.is_char = None + self.dims = () + self.name = '' + class Mat5ArrayFlags(object): ''' Place holder for array flags ''' pass -class Mat5Arrayreader(MatArrayReader): +class Mat5ArrayReader(MatArrayReader): ''' Class to get Mat5 arrays Provides element reader functions, header reader, matrix reader @@ -155,7 +180,7 @@ ''' def __init__(self, mat_stream, dtypes, processor_func, codecs, class_dtypes): - super(Mat5Arrayreader, self).__init__(mat_stream, + super(Mat5ArrayReader, self).__init__(mat_stream, dtypes, processor_func, ) @@ -173,10 +198,10 @@ tag.byte_count = byte_count tag.mdtype = tag.mdtype & 0xFFFF tag.skip = 4 - byte_count - return tag - tag.byte_count = self.read_array( - self.dtypes['tag_byte_count']) - tag.skip = tag.byte_count % 8 and 8 - tag.byte_count % 8 + else: # standard tag format + tag.byte_count = self.read_array( + self.dtypes['tag_byte_count']) + tag.skip = tag.byte_count % 8 and 8 - tag.byte_count % 8 return tag def read_element(self, copy=True): @@ -203,34 +228,26 @@ def read_header(self, tag): ''' Read header from Mat5 matrix - - Defines: - next_position - start position of next matrix - name - dtype - numpy dtype of matrix - mclass - matlab code for class of matrix - dims - shape of matrix as stored (see sparse reader) - is_complex - True if data are complex - is_char - True if these are char data - is_global - is a global variable in matlab workspace - is_numeric - is basic numeric matrix - original_dtype - data type when saved from matlab ''' if not tag.mdtype == miMATRIX: raise TypeError, \ 'Expecting miMATRIX type here, got %d' % tag.mdtype header = Mat5Header() + # Note - there is no skip value for the miMATRIX type; the + # number of bytes field in the tag points to the next variable + # in the file. This is always aligned to 8 byte boundaries + # (except for the miCOMPRESSED type) header.next_position = (self.mat_stream.pos + - tag.byte_count + - tag.skip) + tag.byte_count) + # Apparently an empty miMATRIX can contain no bytes + header.is_empty = tag.byte_count == 0 + if header.is_empty: + return header header.flags = self.read_array_flags() header.is_complex = header.flags.is_complex header.is_global = header.flags.is_global header.is_logical = header.flags.is_logical header.mclass = header.flags.mclass - header.is_numeric = None - header.original_dtype = None - header.is_char = None header.dims = self.read_element() header.name = self.read_element().tostring() return header @@ -250,11 +267,13 @@ ''' Returns reader for next matrix ''' tag = self.read_tag() if tag.mdtype == miCOMPRESSED: - return Mat5ZArrayreader(self, tag).matrix_getter_factory() + return Mat5ZArrayReader(self, tag).matrix_getter_factory() header = self.read_header(tag) return self.header_to_getter(header) def header_to_getter(self, header): + if header.is_empty: + return Mat5EmptyMatrixGetter(self, header) mc = header.mclass if mc in mx_numbers: return Mat5NumericMatrixGetter(self, header) @@ -271,7 +290,7 @@ raise TypeError, 'No reader for class code %s' % mc -class Mat5ZArrayreader(Mat5Arrayreader): +class Mat5ZArrayReader(Mat5ArrayReader): ''' Getter for compressed arrays Reads and uncompresses gzipped stream on init, providing wrapper @@ -282,9 +301,7 @@ def __init__(self, array_reader, tag): '''Reads and uncompresses gzipped stream''' data = array_reader.read_bytes(tag.byte_count) - if tag.skip: - array_reader.mat_stream.seek(tag.skip, 1) - super(Mat5ZArrayreader, self).__init__( + super(Mat5ZArrayReader, self).__init__( ByteStream(zlib.decompress(data.tostring())), array_reader.dtypes, array_reader.processor_func, @@ -295,7 +312,7 @@ def header_to_getter(self, header): ''' Set next_position to current position in parent stream ''' header.next_position = self.next_position - return super(Mat5ZArrayreader, self).header_to_getter(header) + return super(Mat5ZArrayReader, self).header_to_getter(header) class Mat5MatrixGetter(MatMatrixGetter): @@ -316,8 +333,14 @@ def read_element(self, *args, **kwargs): return self.array_reader.read_element(*args, **kwargs) + +class Mat5EmptyMatrixGetter(Mat5MatrixGetter): + ''' Dummy class to return empty array for empty matrix ''' + def get_raw_array(self): + return array([[]]) + class Mat5NumericMatrixGetter(Mat5MatrixGetter): def get_raw_array(self): self.header.is_numeric = True @@ -455,7 +478,7 @@ uint16_codec=None ): self.codecs = {} - self._array_reader = Mat5Arrayreader( + self._array_reader = Mat5ArrayReader( mat_stream, None, None, Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-15 20:24:08 UTC (rev 2199) +++ trunk/Lib/io/miobase.py 2006-09-18 07:34:13 UTC (rev 2200) @@ -76,6 +76,8 @@ a_dtype is assumed to be correct endianness ''' num_bytes = a_dtype.itemsize * product(a_shape) + if not num_bytes: + return array([], dtype=a_dtype) data = self.read_bytes(num_bytes) arr = ndarray(shape=a_shape, dtype=a_dtype, From scipy-svn at scipy.org Tue Sep 19 08:44:56 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Sep 2006 07:44:56 -0500 (CDT) Subject: [Scipy-svn] r2201 - trunk/Lib/interpolate Message-ID: <20060919124456.3352339C09D@new.scipy.org> Author: stefan Date: 2006-09-19 07:44:07 -0500 (Tue, 19 Sep 2006) New Revision: 2201 Modified: trunk/Lib/interpolate/fitpack.py Log: Fix spelling mistakes in fitpack. Modified: trunk/Lib/interpolate/fitpack.py =================================================================== --- trunk/Lib/interpolate/fitpack.py 2006-09-18 07:34:13 UTC (rev 2200) +++ trunk/Lib/interpolate/fitpack.py 2006-09-19 12:44:07 UTC (rev 2201) @@ -48,8 +48,8 @@ The spline is weighted least-squares polynomial of degree k. fp gives the upper bound fp0 for the smoothing factor s""",None], 1:["""\ - The required storage space exceeds the available strorage space. - Probably causes: data (x,y) size is too small or smoothing parameter s is too small (fp>s).""",ValueError], + The required storage space exceeds the available storage space. + Probable causes: data (x,y) size is too small or smoothing parameter s is too small (fp>s).""",ValueError], 2:["""\ A theoretically impossible results when finding a smoothin spline with fp = s. Probably causes: s too small. (abs(fp-s)/s>0.001)""",ValueError], @@ -73,8 +73,8 @@ Warning. The coefficients of the spline have been computed as the minimal norm least-squares solution of a rank deficient system.""",None], 1:["""\ - The required storage space exceeds the available strorage space. - Probably causes: nxest or nyest to small or s is too small. (fp>s)""",ValueError], + The required storage space exceeds the available storage space. + Probably causes: nxest or nyest too small or s is too small. (fp>s)""",ValueError], 2:["""\ A theoretically impossible results when finding a smoothin spline with fp = s. Probably causes: s too small or badly chosen eps. @@ -94,7 +94,7 @@ 10:["""\ Error on input data""",ValueError], 11:["""\ - rwrk2 to small, i.e. there is not enough workspace for computing + rwrk2 too small, i.e. there is not enough workspace for computing the minimal least-squares solution of a rank deficient system of linear equations.""",ValueError], 'unknown':["""\ From scipy-svn at scipy.org Tue Sep 19 11:17:26 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Sep 2006 10:17:26 -0500 (CDT) Subject: [Scipy-svn] r2202 - trunk/Lib/io Message-ID: <20060919151726.8D5C139C1C3@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-19 10:16:02 -0500 (Tue, 19 Sep 2006) New Revision: 2202 Modified: trunk/Lib/io/mio.py trunk/Lib/io/mio4.py trunk/Lib/io/miobase.py Log: Fix to permission error on test; fixes to byte swapping on save; refactoring for Mat4 save routines; minor bugfixes to (untested) original dtype recasting Modified: trunk/Lib/io/mio.py =================================================================== --- trunk/Lib/io/mio.py 2006-09-19 12:44:07 UTC (rev 2201) +++ trunk/Lib/io/mio.py 2006-09-19 15:16:02 UTC (rev 2202) @@ -52,7 +52,7 @@ if full_name is None: raise IOError, "%s not found on the path." % file_name - byte_stream = ByteStream(memmap(full_name)) + byte_stream = ByteStream(memmap(full_name, mode='r')) MR = MatFile4Reader(byte_stream) if MR.format_looks_right(): return MR Modified: trunk/Lib/io/mio4.py =================================================================== --- trunk/Lib/io/mio4.py 2006-09-19 12:44:07 UTC (rev 2201) +++ trunk/Lib/io/mio4.py 2006-09-19 15:16:02 UTC (rev 2202) @@ -38,7 +38,7 @@ 'u1': miUINT8, 'S1': miUINT8, } - + # matrix classes mxFULL_CLASS = 0 mxCHAR_CLASS = 1 @@ -134,13 +134,14 @@ class Mat4FullGetter(Mat4MatrixGetter): def get_raw_array(self): self.header.is_numeric = True - self.header.original_dtype = dtype(float64) if self.header.is_complex: + self.header.original_dtype = dtype(complex128) # avoid array copy to save memory res = self.read_hdr_array(copy=False) res_j = self.read_hdr_array(copy=False) return res + (res_j * 1j) else: + self.header.original_dtype = dtype(float64) return self.read_hdr_array() @@ -162,13 +163,15 @@ where N is the number of non-zero values. Column 1 values [0:N] are the (1-based) row indices of the each non-zero value, column 2 [0:N] are the column indices, column 3 [0:N] are the (real) - values. The last values [-1:0:2] of the rows, column indices are + values. The last values [-1,0:2] of the rows, column indices are shape[0] and shape[1] respectively of the output matrix. The last value for the values column is a padding 0. mrows and ncols values from the header give the shape of the stored matrix, here [N+1, 3]. Complex data is saved as a 4 column matrix, where the fourth column contains the imaginary component of the data; the last - value is again 0 + value is again 0. Complex sparse data do _not_ have the header + imagf field set to True; the fact that the data are complex is + only detectable because there are 4 storage columns ''' def get_raw_array(self): self.header.original_dtype = dtype(float64) @@ -219,62 +222,94 @@ return ByteOrder.native_code -class MatFile4Writer(MatFileWriter): - codec = 'ascii' - - def arr_to_matrix(self, arr): - ''' Convert numeric array to matlab format ''' - dts = arr.dtype.str[1:] - if not dts in np_to_mtypes: - arr = arr.astype('f8') - return atleast_2d(arr) - - def matrix_header(self, var, name): - ''' Return header for matrix array ''' +class Mat4MatrixWriter(MatStreamWriter): + + def write_header(self, P, T, dims, imagf): + ''' Write header for data type, matrix class, dims, complex flag ''' header = empty((), mdtypes_template['header']) - dt = var.dtype.str[1:] M = not ByteOrder.little_endian O = 0 - P = np_to_mtypes[dt] - T = dt == 'S1' # could also be sparse -> 2 - header['mopt'] = M*1000+O*100+P*10+T - dims = var.shape + header['mopt'] = (M * 1000 + + O * 100 + + P * 10 + + T) header['mrows'] = dims[0] header['ncols'] = dims[1] - header['imagf'] = var.dtype.kind == 'c' - header['namlen'] = len(name) + 1 - return header - - def put_variable(self, var, name): - arr = array(var) - if arr.dtype.hasobject: - raise TypeError, 'Cannot save object arrays in Mat4' - if have_sparse: - if scipy.sparse.issparse(arr): - raise TypeError, 'Cannot save sparse arrays yet' - if arr.dtype.kind in ('U', 'S'): - arr = self.str_to_chars(arr) - else: - arr = self.arr_to_matrix(arr) - dims = arr.shape + header['imagf'] = imagf + header['namlen'] = len(self.name) + 1 + self.write_bytes(header) + self.write_string(self.name + '\0') + + def arr_to_2d(self): + self.arr = atleast_2d(self.arr) + dims = self.arr.shape if len(dims) > 2: dims = [product(dims[:-1]), dims[-1]] - arr = reshape(arr, dims) - if arr.dtype.kind == 'U': + self.arr = reshape(self.arr, dims) + + def write(self): + assert False, 'Not implemented' + + +class Mat4NumericWriter(Mat4MatrixWriter): + + def write(self): + self.arr_to_2d() + imagf = self.arr.dtype.kind == 'c' + try: + P = np_to_mtypes[self.arr.dtype.str[1:]] + except KeyError: + if imagf: + self.arr = self.arr.astype('c128') + else: + self.arr = self.arr.astype('f8') + P = miDOUBLE + self.write_header(P, 0, self.arr.shape, imagf) + if imagf: + self.write_bytes(self.arr.real) + self.write_bytes(self.arr.imag) + else: + self.write_bytes(self.arr) + + +class Mat4CharWriter(Mat4MatrixWriter): + + def write(self): + self.arr_to_chars() + self.arr_to_2d() + dims = self.arr.shape + self.write_header(miUINT8, 1, dims, 0) + if self.arr.dtype.kind == 'U': # Recode unicode to ascii - dt = 'U' + str(product(dims)) - st_arr = ndarray(shape=(), dtype=dt, buffer=arr) + n_chars = product(dims) + st_arr = ndarray(shape=(), + dtype=self.arr_dtype_number(n_chars), + buffer=self.arr) st = st_arr.item().encode('ascii') - arr = ndarray(shape=dims, dtype='S1', buffer=st) - header = self.matrix_header(arr, name) - self.write_bytes(header) - self.write_string(name + '\0') - if header['imagf']: - self.write_bytes(arr.real) - self.write_bytes(arr.imag) - else: - self.write_bytes(arr) - + self.arr = ndarray(shape=dims, dtype='S1', buffer=st) + self.write_bytes(self.arr) + + +def matrix_writer_factory(stream, arr, name): + ''' Factory function to return matrix writer given variable to write + @stream - file or file-like stream to write to + @arr - array to write + @name - name in matlab workspace + ''' + arr = array(arr) + if arr.dtype.hasobject: + raise TypeError, 'Cannot save object arrays in Mat4' + if have_sparse: + if scipy.sparse.issparse(arr): + raise TypeError, 'Cannot save sparse arrays yet' + if arr.dtype.kind in ('U', 'S'): + return Mat4CharWriter(stream, arr, name) + else: + return Mat4NumericWriter(stream, arr, name) + + +class MatFile4Writer(MatFileWriter): + def put_variables(self, mdict): for name, var in mdict.items(): - self.put_variable(var, name) + matrix_writer_factory(self.file_stream, var, name).write() Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-19 12:44:07 UTC (rev 2201) +++ trunk/Lib/io/miobase.py 2006-09-19 15:16:02 UTC (rev 2202) @@ -257,7 +257,8 @@ b = self.read_bytes(1) self.mat_stream.seek(-1,1) return len(b) == 0 - + + class MatMatrixGetter(MatStreamAgent): """ Base class for matrix getters @@ -315,25 +316,38 @@ assert False, 'Not implemented' -class MatFileWriter(object): - ''' Base type for writing mat files ''' - def __init__(self, file_stream): +class MatStreamWriter(object): + ''' Base object for writing to mat files ''' + def __init__(self, file_stream, arr, name): self.file_stream = file_stream + self.arr = arr + dt = self.arr.dtype + if not dt.isnative: + self.arr = self.arr.astype(dt.newbyteorder('=')) + self.name = name + + def arr_dtype_number(self, num): + ''' Return dtype for given number of items per element''' + return dtype(self.arr.dtype.str[:2] + str(num)) - def str_to_chars(self, arr): + def arr_to_chars(self): ''' Converts string array to matlab char array ''' - dims = list(arr.shape) + dims = list(self.arr.shape) if not dims: dims = [1] - dims.append(int(arr.dtype.str[2:])) - num_els = product(dims) - dt = dtype(arr.dtype.kind + '1') - return ndarray(shape=dims, dtype=dt, buffer=arr) + dims.append(int(self.arr.dtype.str[2:])) + self.arr = ndarray(shape=dims, + dtype=self.arr_dtype_number(1), + buffer=self.arr) def write_bytes(self, arr): - arr.dtype.newbyteorder(ByteOrder.native_code) - s = arr.tostring(order='F') - self.file_stream.write(s) + self.file_stream.write(arr.tostring(order='F')) def write_string(self, s): self.file_stream.write(s) + + +class MatFileWriter(object): + ''' Base class for Mat file writers ''' + def __init__(self, file_stream): + self.file_stream = file_stream From scipy-svn at scipy.org Tue Sep 19 18:14:48 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Sep 2006 17:14:48 -0500 (CDT) Subject: [Scipy-svn] r2203 - in trunk/Lib/io: . tests Message-ID: <20060919221448.60DC739C1E4@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-19 17:14:27 -0500 (Tue, 19 Sep 2006) New Revision: 2203 Modified: trunk/Lib/io/mio.py trunk/Lib/io/mio4.py trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py trunk/Lib/io/tests/test_mio.py Log: Added mat4 sparse save; separated mat4 and mat5 tests; docstring edits Modified: trunk/Lib/io/mio.py =================================================================== --- trunk/Lib/io/mio.py 2006-09-19 15:16:02 UTC (rev 2202) +++ trunk/Lib/io/mio.py 2006-09-19 22:14:27 UTC (rev 2203) @@ -1,7 +1,7 @@ # Authors: Travis Oliphant, Matthew Brett """ -Module for reading and writing matlab .mat files +Module for reading and writing matlab (TM) .mat files """ import os @@ -15,7 +15,7 @@ def mat_reader_factory(file_name, append_mat=True): - """Create reader for matlab format files + """Create reader for matlab (TM) .mat format files If name not a full path name, search for the file on the sys.path list and use the first one found (the current directory is @@ -25,7 +25,8 @@ Inputs: - file_name -- name of the mat file (don't need .mat extension if append_mat=True) + file_name -- name of the mat file (don't need .mat extension + if append_mat=True) append_mat -- True to append the .mat extension to the end of the given filename. """ @@ -74,8 +75,9 @@ def savemat(file_name, mdict, appendmat=True): """Save a dictionary of names and arrays into the MATLAB-style .mat file. - This saves the arrayobjects in the given dictionary to a matlab Version 4 - style .mat file. + This saves the arrayobjects in the given dictionary to a matlab + Version 4 style .mat file. + @appendmat - if true, appends '.mat' extension to filename, if not present """ if appendmat and file_name[-4:] != ".mat": Modified: trunk/Lib/io/mio4.py =================================================================== --- trunk/Lib/io/mio4.py 2006-09-19 15:16:02 UTC (rev 2202) +++ trunk/Lib/io/mio4.py 2006-09-19 22:14:27 UTC (rev 2203) @@ -1,4 +1,4 @@ -''' Classes for read / write of matlab 4 files +''' Classes for read / write of matlab (TM) 4 files ''' from numpy import * @@ -60,11 +60,11 @@ name dims - shape of matrix as stored (see sparse reader) dtype - numpy dtype of matrix - mclass - matlab code for class of matrix + mclass - matlab (TM) code for class of matrix is_char - True if these are char data is_numeric - True if these are numeric data is_complex - True if data are complex - original_dtype - data type in matlab workspace + original_dtype - data type in matlab (TM) workspace ''' def __init__(self): self.next_position = None @@ -159,26 +159,26 @@ class Mat4SparseGetter(Mat4MatrixGetter): ''' Read sparse matrix type - Matlab 4 real sparse arrays are saved in a N+1 by 3 array format, - where N is the number of non-zero values. Column 1 values [0:N] - are the (1-based) row indices of the each non-zero value, column 2 - [0:N] are the column indices, column 3 [0:N] are the (real) - values. The last values [-1,0:2] of the rows, column indices are - shape[0] and shape[1] respectively of the output matrix. The last - value for the values column is a padding 0. mrows and ncols values - from the header give the shape of the stored matrix, here [N+1, - 3]. Complex data is saved as a 4 column matrix, where the fourth - column contains the imaginary component of the data; the last - value is again 0. Complex sparse data do _not_ have the header - imagf field set to True; the fact that the data are complex is - only detectable because there are 4 storage columns + Matlab (TM) 4 real sparse arrays are saved in a N+1 by 3 array + format, where N is the number of non-zero values. Column 1 values + [0:N] are the (1-based) row indices of the each non-zero value, + column 2 [0:N] are the column indices, column 3 [0:N] are the + (real) values. The last values [-1,0:2] of the rows, column + indices are shape[0] and shape[1] respectively of the output + matrix. The last value for the values column is a padding 0. mrows + and ncols values from the header give the shape of the stored + matrix, here [N+1, 3]. Complex data is saved as a 4 column + matrix, where the fourth column contains the imaginary component; + the last value is again 0. Complex sparse data do _not_ have the + header imagf field set to True; the fact that the data are complex + is only detectable because there are 4 storage columns ''' def get_raw_array(self): self.header.original_dtype = dtype(float64) res = self.read_hdr_array() tmp = res[:-1,:] dims = res[-1,0:2] - ij = transpose(tmp[:,0:2]) - 1 # for matlab 1-based indexing + ij = transpose(tmp[:,0:2]) - 1 # for 1-based indexing vals = tmp[:,2] if res.shape[1] == 4: self.header.is_complex = True @@ -207,7 +207,7 @@ return self._array_reader.matrix_getter_factory() def format_looks_right(self): - # Matlab 4 files have a zero somewhere in first 4 bytes + # Mat4 files have a zero somewhere in first 4 bytes self.mat_stream.seek(0) mopt_bytes = self.read_bytes(4) self.mat_stream.seek(0) @@ -224,8 +224,15 @@ class Mat4MatrixWriter(MatStreamWriter): - def write_header(self, P, T, dims, imagf): - ''' Write header for data type, matrix class, dims, complex flag ''' + def write_header(self, P=0, T=0, imagf=0, dims=None): + ''' Write header for given data options + @P - mat4 data type + @T - mat4 matrix class + @imagf - complex flag + @dims - matrix dimensions + ''' + if dims is None: + dims = self.arr.shape header = empty((), mdtypes_template['header']) M = not ByteOrder.little_endian O = 0 @@ -264,7 +271,9 @@ else: self.arr = self.arr.astype('f8') P = miDOUBLE - self.write_header(P, 0, self.arr.shape, imagf) + self.write_header(P=P, + T=mxFULL_CLASS, + imagf=imagf) if imagf: self.write_bytes(self.arr.real) self.write_bytes(self.arr.imag) @@ -278,7 +287,8 @@ self.arr_to_chars() self.arr_to_2d() dims = self.arr.shape - self.write_header(miUINT8, 1, dims, 0) + self.write_header(P=miUINT8, + T=mxCHAR_CLASS) if self.arr.dtype.kind == 'U': # Recode unicode to ascii n_chars = product(dims) @@ -290,18 +300,45 @@ self.write_bytes(self.arr) +class Mat4SparseWriter(Mat4MatrixWriter): + + def write(self): + ''' Sparse matrices are 2D + See docstring for Mat4SparseGetter + ''' + imagf = self.arr.dtype.kind == 'c' + N = self.arr.nnz + ijd = zeros((N+1, 3+imagf), dtype='f8') + for i in range(N): + ijd[i,0], ijd[i,1] = self.arr.rowcol(i) + ijd[:-1,0:2] += 1 # 1 based indexing + if imagf: + ijd[:-1,2] = self.arr.data.real + ijd[:-1,3] = self.arr.data.imag + else: + ijd[:-1,2] = self.arr.data + ijd[-1,0:2] = self.arr.shape + self.write_header(P=miDOUBLE, + T=mxSPARSE_CLASS, + dims=ijd.shape) + self.write_bytes(ijd) + + def matrix_writer_factory(stream, arr, name): ''' Factory function to return matrix writer given variable to write @stream - file or file-like stream to write to @arr - array to write - @name - name in matlab workspace + @name - name in matlab (TM) workspace ''' + if have_sparse: + if scipy.sparse.issparse(arr): + return Mat4SparseWriter(stream, arr, name) arr = array(arr) if arr.dtype.hasobject: raise TypeError, 'Cannot save object arrays in Mat4' if have_sparse: if scipy.sparse.issparse(arr): - raise TypeError, 'Cannot save sparse arrays yet' + return Mat4SparseWriter(stream, arr, name) if arr.dtype.kind in ('U', 'S'): return Mat4CharWriter(stream, arr, name) else: Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-09-19 15:16:02 UTC (rev 2202) +++ trunk/Lib/io/mio5.py 2006-09-19 22:14:27 UTC (rev 2203) @@ -1,4 +1,4 @@ -''' Classes for read / write of matlab 5 files +''' Classes for read / write of matlab (TM) 5 files ''' # Small fragments of current code adapted from matfile.py by Heiko @@ -102,10 +102,10 @@ mxDOUBLE_CLASS: 'f8', } -''' Before release v7.1 (release 14) matlab used the system default -character encoding scheme padded out to 16-bits. Release 14 and later -use Unicode. When saving character data, matlab R14 checks if it can -be encoded in 7-bit ascii, and saves in that format if so.''' +''' Before release v7.1 (release 14) matlab (TM) used the system +default character encoding scheme padded out to 16-bits. Release 14 +and later use Unicode. When saving character data, R14 checks if it +can be encoded in 7-bit ascii, and saves in that format if so.''' codecs_template = { miUTF8: {'codec': 'utf_8', 'width': 1}, @@ -145,13 +145,13 @@ next_position - start position of next matrix name dtype - numpy dtype of matrix - mclass - matlab code for class of matrix + mclass - matlab (TM) code for class of matrix dims - shape of matrix as stored (see sparse reader) is_complex - True if data are complex is_char - True if these are char data - is_global - is a global variable in matlab workspace + is_global - is a global variable in matlab (TM) workspace is_numeric - is basic numeric matrix - original_dtype - data type when saved from matlab + original_dtype - data type when saved from matlab (TM) ''' def __init__(self): self.next_position = None @@ -219,7 +219,8 @@ try: dt = self.dtypes[tag.mdtype] except KeyError: - raise TypeError, 'Do not know matlab data code %d' % tag.mdtype + raise TypeError, 'Do not know matlab (TM) data code %d' \ + % tag.mdtype el_count = tag.byte_count / dt.itemsize el = self.read_array(dt, a_shape=(el_count), copy=copy) if tag.skip: @@ -369,7 +370,7 @@ res = res + (res_j * 1j) else: res = self.read_element() - ''' From the matlab API documentation, last found here: + ''' From the matlab (TM) API documentation, last found here: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ @rowind are simply the row indices for all the (@res) non-zero entries in the sparse array. @rowind has nzmax entries, so @@ -555,7 +556,7 @@ return hdict def format_looks_right(self): - # Matlab 4 files have a zero somewhere in first 4 bytes + # Mat4 files have a zero somewhere in first 4 bytes self.mat_stream.seek(0) mopt_bytes = self.read_bytes(4) self.mat_stream.seek(0) Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-19 15:16:02 UTC (rev 2202) +++ trunk/Lib/io/miobase.py 2006-09-19 22:14:27 UTC (rev 2203) @@ -1,7 +1,7 @@ # Authors: Travis Oliphant, Matthew Brett """ -Base classes for matlab file stream reading +Base classes for matlab (TM) file stream reading """ import sys @@ -100,7 +100,7 @@ or in ('little', '<') or in ('BIG', '>') @base_name - base name for unnamed variables - @matlab_compatible - return arrays as matlab saved them + @matlab_compatible - return arrays as matlab (TM) saved them @squeeze_me - whether to squeeze unit dimensions or not @chars_as_strings - whether to convert char arrays to string arrays @@ -184,7 +184,7 @@ ''' Processing to apply to read matrices Function applies options to matrices. We have to pass this - function into the reader routines because Matlab 5 matrices + function into the reader routines because Mat5 matrices occur as submatrices - in cell arrays, structs and objects - so we will not see these in the main variable getting routine here. @@ -206,11 +206,12 @@ else: # return string arr = self.chars_to_str(arr) if self.matlab_compatible: - # Apply options to replicate matlab's load into workspace + # Apply options to replicate matlab's (TM) + # load into workspace if header.is_logical: arr = arr.astype(bool) elif header.is_numeric: - # Cast as original matlab type + # Cast as original matlab (TM) type if header.original_dtype: arr = arr.astype(header.original_dtype) if self.squeeze_me: @@ -331,7 +332,7 @@ return dtype(self.arr.dtype.str[:2] + str(num)) def arr_to_chars(self): - ''' Converts string array to matlab char array ''' + ''' Convert string array to char array ''' dims = list(self.arr.shape) if not dims: dims = [1] Modified: trunk/Lib/io/tests/test_mio.py =================================================================== --- trunk/Lib/io/tests/test_mio.py 2006-09-19 15:16:02 UTC (rev 2202) +++ trunk/Lib/io/tests/test_mio.py 2006-09-19 22:14:27 UTC (rev 2203) @@ -100,79 +100,80 @@ # Define cases to test theta = pi/4*arange(9,dtype=float) - case_table = [ + case_table4 = [ {'name': 'double', 'expected': {'testdouble': theta} }] - case_table.append( + case_table4.append( {'name': 'string', 'expected': {'teststring': u'"Do nine men interpret?" "Nine men," I nod.'}, }) - case_table.append( + case_table4.append( {'name': 'complex', 'expected': {'testcomplex': cos(theta) + 1j*sin(theta)} }) - case_table.append( - {'name': 'cell', - 'expected': {'testcell': - array([u'This cell contains this string and 3 arrays of '+\ - 'increasing length', - array(1), array([1,2]), array([1,2,3])], - dtype=object)} - }) - st = mat_struct() - st.stringfield = u'Rats live on no evil star.' - st.doublefield = array([sqrt(2),exp(1),pi]) - st.complexfield = (1+1j)*array([sqrt(2),exp(1),pi]) - case_table.append( - {'name': 'struct', - 'expected': {'teststruct': st} - }) A = zeros((3,5)) A[0] = range(1,6) A[:,0] = range(1,4) - case_table.append( + case_table4.append( {'name': 'matrix', 'expected': {'testmatrix': A}, }) - case_table.append( - {'name': '3dmatrix', - 'expected': {'test3dmatrix': transpose(reshape(range(1,25), (4,3,2)))} - }) - case_table.append( + case_table4.append( {'name': 'sparse', 'expected': {'testsparse': SP.csc_matrix(A)}, }) B = A.astype(complex) B[0,0] += 1j - case_table.append( + case_table4.append( {'name': 'sparsecomplex', 'expected': {'testsparsecomplex': SP.csc_matrix(B)}, }) - case_table.append( + case_table4.append( {'name': 'multi', 'expected': {'theta': theta, 'a': A}, }) - case_table.append( + case_table4.append( {'name': 'minus', 'expected': {'testminus': array(-1)}, }) - case_table.append( + case_table4.append( {'name': 'onechar', 'expected': {'testonechar': u'r'}, }) - case_table.append( + case_table5 = [ + {'name': 'cell', + 'expected': {'testcell': + array([u'This cell contains this string and 3 arrays of '+\ + 'increasing length', + array(1), array([1,2]), array([1,2,3])], + dtype=object)} + }] + case_table5.append( {'name': 'stringarray', - 'expected': {'teststringarray': array([u'one ', u'two ', u'three'], dtype=object)}, + 'expected': {'teststringarray': array( + [u'one ', u'two ', u'three'], dtype=object)}, }) + case_table5.append( + {'name': '3dmatrix', + 'expected': {'test3dmatrix': transpose(reshape(range(1,25), (4,3,2)))} + }) + st = mat_struct() + st.stringfield = u'Rats live on no evil star.' + st.doublefield = array([sqrt(2),exp(1),pi]) + st.complexfield = (1+1j)*array([sqrt(2),exp(1),pi]) + case_table5.append( + {'name': 'struct', + 'expected': {'teststruct': st} + }) a = array([array(1), array([array(2), array(3), array([array(4), array(5)], dtype=object)], dtype=object)], dtype=object) - case_table.append( + case_table5.append( {'name': 'cellnest', 'expected': {'testcellnest': a}, }) @@ -180,7 +181,7 @@ st.one = array(1) st.two = mat_struct() st.two.three = u'number 3' - case_table.append( + case_table5.append( {'name': 'structnest', 'expected': {'teststructnest': st} }) @@ -189,7 +190,7 @@ a[0].two = array(2) a[1].one = u'number 1' a[1].two = u'number 2' - case_table.append( + case_table5.append( {'name': 'structarr', 'expected': {'teststructarr': a} }) @@ -201,19 +202,19 @@ a.isEmpty = array(0) a.numArgs = array(1) a.version = array(1) - case_table.append( + case_table5.append( {'name': 'object', 'expected': {'testobject': a} }) u_str = file( os.path.join(test_data_path, 'japanese_utf8.txt'), 'rb').read().decode('utf-8') - case_table.append( + case_table5.append( {'name': 'unicode', 'expected': {'testunicode': u_str} }) # add load tests - for case in case_table: + for case in case_table4 + case_table5: name = case['name'] expected = case['expected'] filt = os.path.join(test_data_path, 'test%s_*.mat' % name) @@ -221,24 +222,7 @@ assert files, "No files for test %s using filter %s" % (name, filt) exec 'check_%s = _make_check_case(name, files, expected)' % name # round trip tests - case_table = [ - {'name': 'double', - 'expected': {'testdouble': theta} - }] - case_table.append( - {'name': 'string', - 'expected': {'teststring': u'"Do nine men interpret?" "Nine men," I nod.'}, - }) - case_table.append( - {'name': 'complex', - 'expected': {'testcomplex': cos(theta) + 1j*sin(theta)} - }) - case_table.append( - {'name': 'multi', - 'expected': {'theta': theta, - 'a': A}, - }) - for case in case_table: + for case in case_table4: name = case['name'] + '_round_trip' expected = case['expected'] exec 'check_%s = _make_rt_check_case(name, expected)' % name From scipy-svn at scipy.org Tue Sep 19 19:42:11 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Sep 2006 18:42:11 -0500 (CDT) Subject: [Scipy-svn] r2204 - trunk/Lib/signal Message-ID: <20060919234211.AE44339C017@new.scipy.org> Author: stefan Date: 2006-09-19 18:41:55 -0500 (Tue, 19 Sep 2006) New Revision: 2204 Modified: trunk/Lib/signal/ltisys.py Log: Do not modify shape of input arguments to lsim and lsim2. Modified: trunk/Lib/signal/ltisys.py =================================================================== --- trunk/Lib/signal/ltisys.py 2006-09-19 22:14:27 UTC (rev 2203) +++ trunk/Lib/signal/ltisys.py 2006-09-19 23:41:55 UTC (rev 2204) @@ -315,7 +315,7 @@ U = atleast_1d(U) T = atleast_1d(T) if len(U.shape) == 1: - U.shape = (U.shape[0],1) + U = U.reshape((U.shape[0],1)) sU = U.shape if len(T.shape) != 1: raise ValueError, "T must be a rank-1 array." @@ -382,7 +382,7 @@ U = atleast_1d(U) T = atleast_1d(T) if len(U.shape) == 1: - U.shape = (U.shape[0],1) + U = U.reshape((U.shape[0],1)) sU = U.shape if len(T.shape) != 1: raise ValueError, "T must be a rank-1 array." From scipy-svn at scipy.org Tue Sep 19 20:04:06 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Sep 2006 19:04:06 -0500 (CDT) Subject: [Scipy-svn] r2205 - trunk/Lib/stats Message-ID: <20060920000406.9102739C0DB@new.scipy.org> Author: oliphant Date: 2006-09-19 19:04:02 -0500 (Tue, 19 Sep 2006) New Revision: 2205 Modified: trunk/Lib/stats/distributions.py trunk/Lib/stats/stats.py Log: Fix-up SciPy Modified: trunk/Lib/stats/distributions.py =================================================================== --- trunk/Lib/stats/distributions.py 2006-09-19 23:41:55 UTC (rev 2204) +++ trunk/Lib/stats/distributions.py 2006-09-20 00:04:02 UTC (rev 2205) @@ -4166,7 +4166,8 @@ min = 0 U = random(size=size) val = floor((max-min)*U + min) - return arr(val).astype(int) + return arr(val).astype(int)[()] # return array scalar if needed + def _entropy(self, min, max): return log(max-min) randint = randint_gen(name='randint',longname='A discrete uniform '\ Modified: trunk/Lib/stats/stats.py =================================================================== --- trunk/Lib/stats/stats.py 2006-09-19 23:41:55 UTC (rev 2204) +++ trunk/Lib/stats/stats.py 2006-09-20 00:04:02 UTC (rev 2205) @@ -194,7 +194,7 @@ import scipy as sp # Grab this useful utility routine -from numpy.core.numeric import _move_axis_to_0 +from numpy.core.numeric import rollaxis import _support @@ -430,7 +430,7 @@ """ a, axis = _chk_asarray(a, axis) if axis != 0: - a = _move_axis_to_0(a, axis) + a = rollaxis(a, axis, 0) return np.median(a) def mode(a, axis=0): From scipy-svn at scipy.org Thu Sep 21 00:24:56 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 20 Sep 2006 23:24:56 -0500 (CDT) Subject: [Scipy-svn] r2206 - in trunk/Lib/sandbox/odr: . tests Message-ID: <20060921042456.B869439C120@new.scipy.org> Author: rkern Date: 2006-09-20 23:24:53 -0500 (Wed, 20 Sep 2006) New Revision: 2206 Added: trunk/Lib/sandbox/odr/tests/ trunk/Lib/sandbox/odr/tests/test_odrpack.py Log: Some tests for ODRPACK. Added: trunk/Lib/sandbox/odr/tests/test_odrpack.py =================================================================== --- trunk/Lib/sandbox/odr/tests/test_odrpack.py 2006-09-20 00:04:02 UTC (rev 2205) +++ trunk/Lib/sandbox/odr/tests/test_odrpack.py 2006-09-21 04:24:53 UTC (rev 2206) @@ -0,0 +1,317 @@ + +# Standard library imports. +import cPickle +import unittest + +# Scipy imports. +import numpy as np +from numpy import pi +from numpy.testing import assert_array_almost_equal +from scipy.sandbox.odr import Data, Model, ODR, RealData, odr_stop + + +class ODRTestCase(unittest.TestCase): + + # Explicit Example + + def explicit_fcn(self, B, x): + ret = B[0] + B[1] * np.power(np.exp(B[2]*x) - 1.0, 2) + return ret + + def explicit_fjd(self, B, x): + eBx = np.exp(B[2]*x) + ret = B[1] * 2.0 * (eBx-1.0) * B[2] * eBx + return ret + + def explicit_fjb(self, B, x): + eBx = np.exp(B[2]*x) + res = np.vstack([np.ones(x.shape[-1]), + np.power(eBx-1.0, 2), + B[1]*2.0*(eBx-1.0)*eBx*x]) + return res + + def test_explicit(self): + explicit_mod = Model( + self.explicit_fcn, + fjacb=self.explicit_fjb, + fjacd=self.explicit_fjd, + meta=dict(name='Sample Explicit Model', + ref='ODRPACK UG, pg. 39'), + ) + explicit_dat = Data([0.,0.,5.,7.,7.5,10.,16.,26.,30.,34.,34.5,100.], + [1265.,1263.6,1258.,1254.,1253.,1249.8,1237.,1218.,1220.6, + 1213.8,1215.5,1212.]) + explicit_odr = ODR(explicit_dat, explicit_mod, beta0=[1500.0, -50.0, -0.1], + ifixx=[0,0,1,1,1,1,1,1,1,1,1,0]) + explicit_odr.set_job(deriv=2) + + out = explicit_odr.run() + assert_array_almost_equal( + out.beta, + np.array([ 1.2646548050648876e+03, -5.4018409956678255e+01, + -8.7849712165253724e-02]), + ) + assert_array_almost_equal( + out.sd_beta, + np.array([ 1.0349270280543437, 1.583997785262061 , 0.0063321988657267]), + ) + assert_array_almost_equal( + out.cov_beta, + np.array([[ 4.4949592379003039e-01, -3.7421976890364739e-01, + -8.0978217468468912e-04], + [ -3.7421976890364739e-01, 1.0529686462751804e+00, + -1.9453521827942002e-03], + [ -8.0978217468468912e-04, -1.9453521827942002e-03, + 1.6827336938454476e-05]]), + ) + + + # Implicit Example + + def implicit_fcn(self, B, x): + return (B[2]*np.power(x[0]-B[0], 2) + + 2.0*B[3]*(x[0]-B[0])*(x[1]-B[1]) + + B[4]*np.power(x[1]-B[1], 2) - 1.0) + + def test_implicit(self): + implicit_mod = Model( + self.implicit_fcn, + implicit=1, + meta=dict(name='Sample Implicit Model', + ref='ODRPACK UG, pg. 49'), + ) + implicit_dat = Data([ + [0.5,1.2,1.6,1.86,2.12,2.36,2.44,2.36,2.06,1.74,1.34,0.9,-0.28, + -0.78,-1.36,-1.9,-2.5,-2.88,-3.18,-3.44], + [-0.12,-0.6,-1.,-1.4,-2.54,-3.36,-4.,-4.75,-5.25,-5.64,-5.97,-6.32, + -6.44,-6.44,-6.41,-6.25,-5.88,-5.5,-5.24,-4.86]], + 1) + implicit_odr = ODR(implicit_dat, implicit_mod, + beta0=[-1.0, -3.0, 0.09, 0.02, 0.08]) + + out = implicit_odr.run() + assert_array_almost_equal( + out.beta, + np.array([-0.9993809167281279, -2.9310484652026476, 0.0875730502693354, + 0.0162299708984738, 0.0797537982976416]), + ) + assert_array_almost_equal( + out.sd_beta, + np.array([ 0.1113840353364371, 0.1097673310686467, 0.0041060738314314, + 0.0027500347539902, 0.0034962501532468]), + ) + assert_array_almost_equal( + out.cov_beta, + np.array([[ 2.1089274602333052e+00, -1.9437686411979040e+00, + 7.0263550868344446e-02, -4.7175267373474862e-02, + 5.2515575927380355e-02], + [ -1.9437686411979040e+00, 2.0481509222414456e+00, + -6.1600515853057307e-02, 4.6268827806232933e-02, + -5.8822307501391467e-02], + [ 7.0263550868344446e-02, -6.1600515853057307e-02, + 2.8659542561579308e-03, -1.4628662260014491e-03, + 1.4528860663055824e-03], + [ -4.7175267373474862e-02, 4.6268827806232933e-02, + -1.4628662260014491e-03, 1.2855592885514335e-03, + -1.2692942951415293e-03], + [ 5.2515575927380355e-02, -5.8822307501391467e-02, + 1.4528860663055824e-03, -1.2692942951415293e-03, + 2.0778813389755596e-03]]), + ) + + + # Multi-variable Example + + def multi_fcn(self, B, x): + if (x < 0.0).any(): + raise odr_stop + theta = pi*B[3]/2. + ctheta = np.cos(theta) + stheta = np.sin(theta) + omega = np.power(2.*pi*x*np.exp(-B[2]), B[3]) + phi = np.arctan2((omega*stheta), (1.0 + omega*ctheta)) + r = (B[0] - B[1]) * np.power(np.sqrt(np.power(1.0 + omega*ctheta, 2) + + np.power(omega*stheta, 2)), -B[4]) + ret = np.vstack([B[1] + r*np.cos(B[4]*phi), + r*np.sin(B[4]*phi)]) + return ret + + def test_multi(self): + multi_mod = Model( + self.multi_fcn, + meta=dict(name='Sample Multi-Response Model', + ref='ODRPACK UG, pg. 56'), + ) + + multi_x = np.array([30.0, 50.0, 70.0, 100.0, 150.0, 200.0, 300.0, 500.0, + 700.0, 1000.0, 1500.0, 2000.0, 3000.0, 5000.0, 7000.0, 10000.0, + 15000.0, 20000.0, 30000.0, 50000.0, 70000.0, 100000.0, 150000.0]) + multi_y = np.array([ + [4.22, 4.167, 4.132, 4.038, 4.019, 3.956, 3.884, 3.784, 3.713, + 3.633, 3.54, 3.433, 3.358, 3.258, 3.193, 3.128, 3.059, 2.984, + 2.934, 2.876, 2.838, 2.798, 2.759], + [0.136, 0.167, 0.188, 0.212, 0.236, 0.257, 0.276, 0.297, 0.309, + 0.311, 0.314, 0.311, 0.305, 0.289, 0.277, 0.255, 0.24, 0.218, + 0.202, 0.182, 0.168, 0.153, 0.139], + ]) + n = len(multi_x) + multi_we = np.zeros((2, 2, n), dtype=float) + multi_ifixx = np.ones(n, dtype=int) + multi_delta = np.zeros(n, dtype=float) + + multi_we[0,0,:] = 559.6 + multi_we[1,0,:] = multi_we[0,1,:] = -1634.0 + multi_we[1,1,:] = 8397.0 + + for i in range(n): + if multi_x[i] < 100.0: + multi_ifixx[i] = 0 + elif multi_x[i] <= 150.0: + pass # defaults are fine + elif multi_x[i] <= 1000.0: + multi_delta[i] = 25.0 + elif multi_x[i] <= 10000.0: + multi_delta[i] = 560.0 + elif multi_x[i] <= 100000.0: + multi_delta[i] = 9500.0 + else: + multi_delta[i] = 144000.0 + if multi_x[i] == 100.0 or multi_x[i] == 150.0: + multi_we[:,:,i] = 0.0 + + multi_dat = Data(multi_x, multi_y, wd=1e-4/np.power(multi_x, 2), + we=multi_we) + multi_odr = ODR(multi_dat, multi_mod, beta0=[4.,2.,7.,.4,.5], + delta0=multi_delta, ifixx=multi_ifixx) + multi_odr.set_job(deriv=1, del_init=1) + + out = multi_odr.run() + assert_array_almost_equal( + out.beta, + np.array([ 4.3799880305938963, 2.4333057577497703, 8.0028845899503978, + 0.5101147161764654, 0.5173902330489161]), + ) + assert_array_almost_equal( + out.sd_beta, + np.array([ 0.0130625231081944, 0.0130499785273277, 0.1167085962217757, + 0.0132642749596149, 0.0288529201353984]), + ) + assert_array_almost_equal( + out.cov_beta, + np.array([[ 0.0064918418231375, 0.0036159705923791, 0.0438637051470406, + -0.0058700836512467, 0.011281212888768 ], + [ 0.0036159705923791, 0.0064793789429006, 0.0517610978353126, + -0.0051181304940204, 0.0130726943624117], + [ 0.0438637051470406, 0.0517610978353126, 0.5182263323095322, + -0.0563083340093696, 0.1269490939468611], + [-0.0058700836512467, -0.0051181304940204, -0.0563083340093696, + 0.0066939246261263, -0.0140184391377962], + [ 0.011281212888768 , 0.0130726943624117, 0.1269490939468611, + -0.0140184391377962, 0.0316733013820852]]), + ) + + + # Pearson's Data + # K. Pearson, Philosophical Magazine, 2, 559 (1901) + + def pearson_fcn(self, B, x): + return B[0] + B[1]*x + + def test_pearson(self): + p_x = np.array([0.,.9,1.8,2.6,3.3,4.4,5.2,6.1,6.5,7.4]) + p_y = np.array([5.9,5.4,4.4,4.6,3.5,3.7,2.8,2.8,2.4,1.5]) + p_sx = np.array([.03,.03,.04,.035,.07,.11,.13,.22,.74,1.]) + p_sy = np.array([1.,.74,.5,.35,.22,.22,.12,.12,.1,.04]) + + p_dat = RealData(p_x, p_y, sx=p_sx, sy=p_sy) + + # Reverse the data to test invariance of results + pr_dat = RealData(p_y, p_x, sx=p_sy, sy=p_sx) + + p_mod = Model(self.pearson_fcn, meta=dict(name='Uni-linear Fit')) + + p_odr = ODR(p_dat, p_mod, beta0=[1.,1.]) + pr_odr = ODR(pr_dat, p_mod, beta0=[1.,1.]) + + out = p_odr.run() + assert_array_almost_equal( + out.beta, + np.array([ 5.4767400299231674, -0.4796082367610305]), + ) + assert_array_almost_equal( + out.sd_beta, + np.array([ 0.3590121690702467, 0.0706291186037444]), + ) + assert_array_almost_equal( + out.cov_beta, + np.array([[ 0.0854275622946333, -0.0161807025443155], + [-0.0161807025443155, 0.003306337993922 ]]), + ) + + rout = pr_odr.run() + assert_array_almost_equal( + rout.beta, + np.array([ 11.4192022410781231, -2.0850374506165474]), + ) + assert_array_almost_equal( + rout.sd_beta, + np.array([ 0.9820231665657161, 0.3070515616198911]), + ) + assert_array_almost_equal( + rout.cov_beta, + np.array([[ 0.6391799462548782, -0.1955657291119177], + [-0.1955657291119177, 0.0624888159223392]]), + ) + + # Lorentz Peak + # The data is taken from one of the undergraduate physics labs I performed. + + def lorentz(self, beta, x): + return (beta[0]*beta[1]*beta[2] / np.sqrt(np.power(x*x - + beta[2]*beta[2], 2.0) + np.power(beta[1]*x, 2.0))) + + def test_lorentz(self): + l_sy = np.array([.29]*18) + l_sx = np.array([.000972971,.000948268,.000707632,.000706679, + .000706074, .000703918,.000698955,.000456856, + .000455207,.000662717,.000654619,.000652694, + .000000859202,.00106589,.00106378,.00125483, .00140818,.00241839]) + + l_dat = RealData( + [3.9094, 3.85945, 3.84976, 3.84716, 3.84551, 3.83964, 3.82608, + 3.78847, 3.78163, 3.72558, 3.70274, 3.6973, 3.67373, 3.65982, + 3.6562, 3.62498, 3.55525, 3.41886], + [652, 910.5, 984, 1000, 1007.5, 1053, 1160.5, 1409.5, 1430, 1122, + 957.5, 920, 777.5, 709.5, 698, 578.5, 418.5, 275.5], + sx=l_sx, + sy=l_sy, + ) + l_mod = Model(self.lorentz, meta=dict(name='Lorentz Peak')) + l_odr = ODR(l_dat, l_mod, beta0=(1000., .1, 3.8)) + + out = l_odr.run() + assert_array_almost_equal( + out.beta, + np.array([ 1.4306780846149925e+03, 1.3390509034538309e-01, + 3.7798193600109009e+00]), + ) + assert_array_almost_equal( + out.sd_beta, + np.array([ 7.3621186811330963e-01, 3.5068899941471650e-04, + 2.4451209281408992e-04]), + ) + assert_array_almost_equal( + out.cov_beta, + np.array([[ 2.4714409064597873e-01, -6.9067261911110836e-05, + -3.1236953270424990e-05], + [ -6.9067261911110836e-05, 5.6077531517333009e-08, + 3.6133261832722601e-08], + [ -3.1236953270424990e-05, 3.6133261832722601e-08, + 2.7261220025171730e-08]]), + ) + + +if __name__ == '__main__': + unittest.main() + +#### EOF ####################################################################### Property changes on: trunk/Lib/sandbox/odr/tests/test_odrpack.py ___________________________________________________________________ Name: svn:eol-style + native From scipy-svn at scipy.org Thu Sep 21 01:19:16 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 21 Sep 2006 00:19:16 -0500 (CDT) Subject: [Scipy-svn] r2207 - in trunk/Lib/sandbox/odr: . tests Message-ID: <20060921051916.B118239C030@new.scipy.org> Author: rkern Date: 2006-09-21 00:19:03 -0500 (Thu, 21 Sep 2006) New Revision: 2207 Modified: trunk/Lib/sandbox/odr/__init__.py trunk/Lib/sandbox/odr/models.py trunk/Lib/sandbox/odr/odrpack.py trunk/Lib/sandbox/odr/tests/test_odrpack.py Log: Some reformatting of the docstrings to please my eye. Also a few bugfixes along the way. Modified: trunk/Lib/sandbox/odr/__init__.py =================================================================== --- trunk/Lib/sandbox/odr/__init__.py 2006-09-21 04:24:53 UTC (rev 2206) +++ trunk/Lib/sandbox/odr/__init__.py 2006-09-21 05:19:03 UTC (rev 2207) @@ -1,56 +1,50 @@ -"""Orthogonal Distance Regression +""" Orthogonal Distance Regression -Anti-Copyright -============== - -I hereby release this code into the PUBLIC DOMAIN AS IS. There is no -support, warranty, or guarantee. I will gladly accept comments, bug -reports, and patches, however. - - Introduction ============ -Why Orthogonal Distance Regression (ODR)? Sometimes one has measurement -errors in the explanatory variable, not just the response variable. Ordinary -Least Squares (OLS) fitting procedures treat the data for explanatory -variables as fixed. Furthermore, OLS procedures require that the response -variable be an explicit function of the explanatory variables; sometimes -making the equation explicit is unwieldy and introduces errors. ODR can handle -both of these cases with ease and can even reduce to the OLS case if necessary. +Why Orthogonal Distance Regression (ODR)? Sometimes one has measurement errors +in the explanatory variable, not just the response variable. Ordinary Least +Squares (OLS) fitting procedures treat the data for explanatory variables as +fixed. Furthermore, OLS procedures require that the response variable be an +explicit function of the explanatory variables; sometimes making the equation +explicit is unwieldy and introduces errors. ODR can handle both of these cases +with ease and can even reduce to the OLS case if necessary. ODRPACK is a FORTRAN-77 library for performing ODR with possibly non-linear fitting functions. It uses a modified trust-region Levenberg-Marquardt-type algorithm to estimate the function parameters. The fitting functions are -provided by Python functions operating on NumPy arrays. The required -derivatives may be provided by Python functions as well or may be numerically -estimated. ODRPACK can do explicit or implicit ODR fits or can do OLS. Input -and output variables may be multi-dimensional. Weights can be provided to -account for different variances of the observations (even covariances between -dimensions of the variables). +provided by Python functions operating on NumPy arrays. The required derivatives +may be provided by Python functions as well or may be numerically estimated. +ODRPACK can do explicit or implicit ODR fits or can do OLS. Input and output +variables may be multi-dimensional. Weights can be provided to account for +different variances of the observations (even covariances between dimensions of +the variables). -odr provides two interfaces: a single function and a set of high-level -classes that wrap that function. Please refer to their docstrings for more -information. While the docstring of the function, odr, does not have a full -explanation of its arguments, the classes do, and the arguments with the same -name usually have the same requirements. Furthermore, it is highly suggested -that one at least skim the ODRPACK User's Guide. Know Thy Algorithm. +odr provides two interfaces: a single function and a set of high-level classes +that wrap that function. Please refer to their docstrings for more information. +While the docstring of the function, odr, does not have a full explanation of +its arguments, the classes do, and the arguments with the same name usually have +the same requirements. Furthermore, it is highly suggested that one at least +skim the ODRPACK User's Guide. Know Thy Algorithm. Use === See the docstrings of odr.odrpack and the functions and classes for -usage instructions. Also check the 'doc' subdirectory of the distribution -for the ODRPACK User's Guide. +usage instructions. The ODRPACK User's Guide is also quite helpful. It can be +found on one of the ODRPACK's original author's website: + http://www.boulder.nist.gov/mcsd/Staff/JRogers/odrpack.html + Robert Kern -kern at caltech.edu +robert.kern at gmail.com """ -__version__ = '0.6' -__author__ = 'Robert Kern ' -__date__ = '2002-11-09' +__version__ = '0.7' +__author__ = 'Robert Kern ' +__date__ = '2006-09-21' import odrpack from odrpack import odr ,\ @@ -64,3 +58,5 @@ __all__ = ['odr', 'odr_error', 'odr_stop', 'Data', 'RealData', 'Model', 'Output', 'ODR', 'odrpack'] + +#### EOF ####################################################################### Modified: trunk/Lib/sandbox/odr/models.py =================================================================== --- trunk/Lib/sandbox/odr/models.py 2006-09-21 04:24:53 UTC (rev 2206) +++ trunk/Lib/sandbox/odr/models.py 2006-09-21 05:19:03 UTC (rev 2207) @@ -1,26 +1,26 @@ -"""Collection of Model instances for use with the odrpack fitting package. +""" Collection of Model instances for use with the odrpack fitting package. """ +# Scipy imports. from scipy.sandbox.odr.odrpack import Model import numpy as np -from types import * -def _lin_fcn(B, x, sum=np.sum): + +def _lin_fcn(B, x): a, b = B[0], B[1:] b.shape = (b.shape[0], 1) - return a + sum(x*b,axis=0) + return a + (x*b).sum(axis=0) -def _lin_fjb(B, x, concatenate=np.concatenate, - ones=np.ones, ravel=np.ravel): - a = ones((x.shape[-1],), float) - res = concatenate((a, ravel(x))) +def _lin_fjb(B, x): + a = np.ones(x.shape[-1], float) + res = np.concatenate((a, x.ravel())) res.shape = (B.shape[-1], x.shape[-1]) return res -def _lin_fjd(B, x, repeat=np.repeat): +def _lin_fjd(B, x): b = B[1:] - b = repeat(b, (x.shape[-1],)*b.shape[-1],axis=0) + b = np.repeat(b, (x.shape[-1],)*b.shape[-1],axis=0) b.shape = x.shape return b @@ -36,40 +36,40 @@ return np.ones((m + 1,), float) -def _poly_fcn(B, x, powers, power=np.power, sum=np.sum): +def _poly_fcn(B, x, powers): a, b = B[0], B[1:] b.shape = (b.shape[0], 1) - return a + sum(b * power(x, powers),axis=0) + return a + np.sum(b * np.power(x, powers), axis=0) -def _poly_fjacb(B, x, powers, power=np.power, - concatenate=np.concatenate, ones=np.ones): - res = concatenate((ones((x.shape[-1],), float), power(x, powers).flat)) +def _poly_fjacb(B, x, powers): + res = np.concatenate((np.ones(x.shape[-1], float), np.power(x, + powers).flat)) res.shape = (B.shape[-1], x.shape[-1]) return res -def _poly_fjacd(B, x, powers, power=np.power, sum=np.sum): +def _poly_fjacd(B, x, powers): b = B[1:] b.shape = (b.shape[0], 1) b = b * powers - return sum(b * power(x, powers-1),axis=0) + return np.sum(b * np.power(x, powers-1),axis=0) -def _exp_fcn(B, x, exp=np.exp): - return B[0] + exp(B[1] * x) +def _exp_fcn(B, x): + return B[0] + np.exp(B[1] * x) -def _exp_fjd(B, x, exp=np.exp): - return B[1] * exp(B[1] * x) +def _exp_fjd(B, x): + return B[1] * np.exp(B[1] * x) -def _exp_fjb(B, x, exp=np.exp, concatenate=np.concatenate, ones=np.ones): - res = concatenate((ones((x.shape[-1],), float), x * exp(B[1] * x))) +def _exp_fjb(B, x): + res = np.concatenate((np.ones(x.shape[-1], float), x * np.exp(B[1] * x))) res.shape = (2, x.shape[-1]) return res def _exp_est(data): # Eh. - return array([1., 1.]) + return np.array([1., 1.]) multilinear = Model(_lin_fcn, fjacb=_lin_fjb, fjacd=_lin_fjd, estimate=_lin_est, @@ -78,32 +78,35 @@ 'TeXequ':'$y=\\beta_0 + \sum_{i=1}^m \\beta_i x_i$'}) def polynomial(order): - """Factory function for a general polynomial model. + """ Factory function for a general polynomial model. -The argument "order" can be either an integer, where it becomes the -order of the polynomial to fit, or it can be a sequence of numbers to -explicitly determine the powers in the polynomial. + Parameters + ---------- + order : int or sequence + If an integer, it becomes the order of the polynomial to fit. If + a sequence of numbers, then these are the explicit powers in the + polynomial. + A constant term (power 0) is always included, so don't include 0. + Thus, polynomial(n) is equivalent to polynomial(range(1, n+1)). -Oh yeah, a constant is always included, so don't include 0. + Returns + ------- + model : Model instance + """ -Thus, polynomial(n) is equivalent to polynomial(range(1, n+1)). + powers = np.asarray(order) + if powers.shape == (): + # Scalar. + powers = np.arange(1, powers + 1) - polynomial(order)""" - - if type(order) is int: - order = range(1, order+1) - - powers = np.asarray(order) powers.shape = (len(powers), 1) - len_beta = len(powers) + 1 def _poly_est(data, len_beta=len_beta): # Eh. Ignore data and return all ones. - return np.ones((len_beta,), float) - return Model(_poly_fcn, fjacd=_poly_fjd, fjacb=_poly_fjb, + return Model(_poly_fcn, fjacd=_poly_fjacd, fjacb=_poly_fjacb, estimate=_poly_est, extra_args=(powers,), meta={'name': 'Sorta-general Polynomial', 'equ':'y = B_0 + Sum[i=1..%s, B_i * (x**i)]' % (len_beta-1), @@ -121,8 +124,8 @@ def _unilin_fjd(B, x): return np.ones(x.shape, float) * B[0] -def _unilin_fjb(B, x, cat=np.concatenate): - _ret = cat((x,np.ones(x.shape, float))) +def _unilin_fjb(B, x): + _ret = np.concatenate((x, np.ones(x.shape, float))) _ret.shape = (2,) + x.shape return _ret @@ -136,8 +139,8 @@ def _quad_fjd(B, x): return 2*x*B[0] + B[1] -def _quad_fjb(B,x,cat=np.concatenate): - _ret = cat((x*x, x, np.ones(x.shape, float))) +def _quad_fjb(B, x): + _ret = np.concatenate((x*x, x, np.ones(x.shape, float))) _ret.shape = (3,) + x.shape return _ret @@ -154,3 +157,5 @@ estimate=_quad_est, meta={'name': 'Quadratic', 'equ': 'y = B_0*x**2 + B_1*x + B_2', 'TeXequ': '$y = \\beta_0 x^2 + \\beta_1 x + \\beta_2'}) + +#### EOF ####################################################################### Modified: trunk/Lib/sandbox/odr/odrpack.py =================================================================== --- trunk/Lib/sandbox/odr/odrpack.py 2006-09-21 04:24:53 UTC (rev 2206) +++ trunk/Lib/sandbox/odr/odrpack.py 2006-09-21 05:19:03 UTC (rev 2207) @@ -1,25 +1,5 @@ -"""Python wrappers for Orthogonal Distance Regression (ODRPACK) +""" Python wrappers for Orthogonal Distance Regression (ODRPACK). -Functions -========= - -odr() -- calls the fitting subroutine. For your sake and mine, use the class - wrappers. - -report_error(info) -- converts the return code from ODRPACK's routine to - an English string or list of strings - - -Exceptions -========== - -odr_error -- error sometimes raised inside odr() and can be raised in the - fitting functions to tell ODRPACK to halt the procedure - -odr_stop -- error to raise in fitting functions to tell ODRPACK that the data or - parameters given are invalid - - Classes ======= @@ -34,6 +14,15 @@ ODR -- collects all data and runs the fitting routine +Exceptions +========== + +odr_error -- error sometimes raised inside odr() and can be raised in the + fitting functions to tell ODRPACK to halt the procedure + +odr_stop -- error to raise in fitting functions to tell ODRPACK that the data or + parameters given are invalid + Use === @@ -112,29 +101,41 @@ import numpy from scipy.sandbox.odr import __odrpack -from types import NoneType odr = __odrpack.odr odr_error = __odrpack.odr_error odr_stop = __odrpack.odr_stop + def _conv(obj): - """Convert an object to the preferred form for input to the odr routine.""" + """ Convert an object to the preferred form for input to the odr routine. + """ - t = type(obj) - - if t in (float, int, long, NoneType): - # scalar or None, pass through + if obj is None: return obj else: - # try to pass it through scipy's asarray - return numpy.asarray(obj) + obj = numpy.asarray(obj) + if obj.shape == (): + # Scalar. + return obj.dtype.type(obj) + else: + return obj + def report_error(info): - """Interprets the return code of the odr routine. + """ Interprets the return code of the odr routine. - report_error(info) --> ["reason1", "reason2", ...] -""" + Parameters + ---------- + info : int + The return code of the odr routine. + + Returns + ------- + problems : list(str) + A list of messages about why the odr() routine stopped. + """ + stopreason = ('Blank', 'Sum of squares convergence', 'Parameter convergence', @@ -198,89 +199,81 @@ else: return [stopreason] + class Data(object): - """The Data class stores the data to fit. + """ The Data class stores the data to fit. - Data(x, y, we=None, wd=None, fix=None, meta={}) + Each argument is attached to the member of the instance of the same name. + The structures of x and y are described in the Model class docstring. If + y is an integer, then the Data instance can only be used to fit with + implicit models where the dimensionality of the response is equal to the + specified value of y. The structures of wd and we are described below. meta + is an freeform dictionary for application-specific use. -Each argument is attached to the member of the instance of the same name. -The structures of x and y are described in the Model class docstring. If -y is an integer, then the Data instance can only be used to fit with implicit -models where the dimensionality of the response is equal to the specified -value of y. The structures of wd and we are described below. meta is an -freeform dictionary for application-specific use. + we weights the effect a deviation in the response variable has on the fit. + wd weights the effect a deviation in the input variable has on the fit. To + handle multidimensional inputs and responses easily, the structure of these + arguments has the n'th dimensional axis first. These arguments heavily use + the structured arguments feature of ODRPACK to conveniently and flexibly + support all options. See the ODRPACK User's Guide for a full explanation of + how these weights are used in the algorithm. Basically, a higher value of + the weight for a particular data point makes a deviation at that point more + detrimental to the fit. -we weights the effect a deviation in the response variable has on the fit. -wd weights the effect a deviation in the input variable has on the fit. To -handle multidimensional inputs and responses easily, the structure of these -arguments has the n'th dimensional axis first. These arguments heavily use -the structured arguments feature of ODRPACK to conveniently and flexibly -support all options. See the ODRPACK User's Guide for a full explanation of -how these weights are used in the algorithm. Basically, a higher value of the -weight for a particular data point makes a deviation at that point more -detrimental to the fit. + we -- if we is a scalar, then that value is used for all data points (and + all dimensions of the response variable). - we -- if we is a scalar, then that value is used for all data points - (and all dimensions of the response variable). + If we is a rank-1 array of length q (the dimensionality of the response + variable), then this vector is the diagonal of the covariant weighting + matrix for all data points. - If we is a rank-1 array of length q (the dimensionality of the - response variable), then this vector is the diagonal of the covariant - weighting matrix for all data points. - If we is a rank-1 array of length n (the number of data points), then the i'th element is the weight for the i'th response variable observation (single-dimensional only). - If we is a rank-2 array of shape (q, q), then this is the full - covariant weighting matrix broadcast to each observation. + If we is a rank-2 array of shape (q, q), then this is the full covariant + weighting matrix broadcast to each observation. - If we is a rank-2 array of shape (q, n), then we[:,i] is the - diagonal of the covariant weighting matrix for the i'th observation. + If we is a rank-2 array of shape (q, n), then we[:,i] is the diagonal of + the covariant weighting matrix for the i'th observation. - If we is a rank-3 array of shape (q, q, n), then we[:,:,i] is - the full specification of the covariant weighting matrix for each - observation. + If we is a rank-3 array of shape (q, q, n), then we[:,:,i] is the full + specification of the covariant weighting matrix for each observation. If the fit is implicit, then only a positive scalar value is used. - wd -- if wd is a scalar, then that value is used for all data points - (and all dimensions of the input variable). If wd = 0, then - the covariant weighting matrix for each observation is set to the - identity matrix (so each dimension of each observation has the - same weight). + wd -- if wd is a scalar, then that value is used for all data points + (and all dimensions of the input variable). If wd = 0, then the + covariant weighting matrix for each observation is set to the identity + matrix (so each dimension of each observation has the same weight). - If wd is a rank-1 array of length m (the dimensionality of the - input variable), then this vector is the diagonal of the covariant - weighting matrix for all data points. + If wd is a rank-1 array of length m (the dimensionality of the input + variable), then this vector is the diagonal of the covariant weighting + matrix for all data points. If wd is a rank-1 array of length n (the number of data points), then - the i'th element is the weight for the i'th input variable - observation (single-dimensional only). + the i'th element is the weight for the i'th input variable observation + (single-dimensional only). - If wd is a rank-2 array of shape (m, m), then this is the full - covariant weighting matrix broadcast to each observation. + If wd is a rank-2 array of shape (m, m), then this is the full covariant + weighting matrix broadcast to each observation. - If wd is a rank-2 array of shape (m, n), then wd[:,i] is the - diagonal of the covariant weighting matrix for the i'th observation. + If wd is a rank-2 array of shape (m, n), then wd[:,i] is the diagonal of + the covariant weighting matrix for the i'th observation. - If wd is a rank-3 array of shape (m, m, n), then wd[:,:,i] is - the full specification of the covariant weighting matrix for each - observation. + If wd is a rank-3 array of shape (m, m, n), then wd[:,:,i] is the full + specification of the covariant weighting matrix for each observation. - fix -- fix is the same as ifixx in the class ODR. It is an array of integers - with the same shape as data.x that determines which input - observations are treated as fixed. One can use a sequence of length m - (the dimensionality of the input observations) to fix some dimensions - for all observations. A value of 0 fixes the observation, a value > 0 - makes it free. + fix -- fix is the same as ifixx in the class ODR. It is an array of integers + with the same shape as data.x that determines which input observations + are treated as fixed. One can use a sequence of length m (the + dimensionality of the input observations) to fix some dimensions for all + observations. A value of 0 fixes the observation, a value > 0 makes it + free. - meta -- optional, freeform dictionary for metadata + meta -- optional, freeform dictionary for metadata + """ -Data has one method:: - - set_meta(**kwds) -- updates the metadata dictionary conveniently with - keyword arguments.""" - def __init__(self, x, y=None, we=None, wd=None, fix=None, meta={}): self.x = _conv(x) self.y = _conv(y) @@ -289,16 +282,23 @@ self.fix = _conv(fix) self.meta = meta + def set_meta(self, **kwds): - """Update the metadata dictionary with the keywords and data provided -here. - E.g. data.set_meta(lab="Ph 7; Lab 26", title="Ag110 + Ag108 Decay") + """ Update the metadata dictionary with the keywords and data provided + by keywords. - set_meta(**kwds)""" + Example + ------- + data.set_meta(lab="Ph 7; Lab 26", title="Ag110 + Ag108 Decay") + """ self.meta.update(kwds) + def __getattr__(self, attr): + """ Dispatch aatribute access to the metadata dictionary. + """ + if attr in self.meta.keys(): return self.meta[attr] else: @@ -306,35 +306,38 @@ class RealData(Data): - """The RealData class stores the weightings as actual standard deviations -and/or covariances. The weights needed for ODRPACK are generated on-the-fly -with __getattr__ trickery. + """ The RealData class stores the weightings as actual standard deviations + and/or covariances. -sx and sy are standard deviations of x and y and are converted to weights by -dividing 1.0 by their squares. + The weights needed for ODRPACK are generated on-the-fly with __getattr__ + trickery. - E.g. wd = 1./numpy.power(sx, 2) + sx and sy are standard deviations of x and y and are converted to weights by + dividing 1.0 by their squares. -covx and covy are arrays of covariance matrices and are converted to weights by -performing a matrix inversion on each observation's covariance matrix. + E.g. wd = 1./numpy.power(sx, 2) - E.g. we[i] = scipy.linalg.inv(covy[i]) # i in range(len(covy)) - # if covy.shape == (n,q,q) + covx and covy are arrays of covariance matrices and are converted to weights + by performing a matrix inversion on each observation's covariance matrix. -These arguments follow the same structured argument conventions as wd and we -only restricted by their natures: sx and sy can't be rank-3, but covx and covy -can be. + E.g. we[i] = scipy.linalg.inv(covy[i]) # i in range(len(covy)) + # if covy.shape == (n,q,q) -Only set *either* sx or covx (not both). Setting both will raise an exception. -Same with sy and covy. + These arguments follow the same structured argument conventions as wd and we + only restricted by their natures: sx and sy can't be rank-3, but covx and + covy can be. -The argument and member fix is the same as Data.fix and ODR.ifixx: - It is an array of integers with the same shape as data.x that determines which - input observations are treated as fixed. One can use a sequence of length m - (the dimensionality of the input observations) to fix some dimensions for all - observations. A value of 0 fixes the observation, a value > 0 makes it free. -""" + Only set *either* sx or covx (not both). Setting both will raise an + exception. Same with sy and covy. + The argument and member fix is the same as Data.fix and ODR.ifixx: + It is an array of integers with the same shape as data.x that determines + which input observations are treated as fixed. One can use a sequence of + length m (the dimensionality of the input observations) to fix some + dimensions for all observations. A value of 0 fixes the observation, + a value > 0 makes it free. + """ + def __init__(self, x, y=None, sx=None, sy=None, covx=None, covy=None, fix=None, meta={}): if (sx is not None) and (covx is not None): @@ -342,7 +345,7 @@ if (sy is not None) and (covy is not None): raise ValueError, "cannot set both sy and covy" - # set flags for __getattr__ + # Set flags for __getattr__ self._ga_flags = {} if sx is not None: self._ga_flags['wd'] = 'sx' @@ -362,22 +365,21 @@ self.fix = _conv(fix) self.meta = meta + def _sd2wt(self, sd): - """Convert standard deviation to weights.""" + """ Convert standard deviation to weights. + """ return 1./numpy.power(sd, 2) def _cov2wt(self, cov): - """Convert covariance matrix(-ices) to weights.""" + """ Convert covariance matrix(-ices) to weights. + """ - try: - from scipy import linalg - except ImportError: - raise odr_error("could not import module LinearAlgebra; " - "cannot compute covariant weightings") + from scipy import linalg if len(cov.shape) == 2: - return linalg.inverse(cov) + return linalg.inv(cov) else: weights = numpy.zeros(cov.shape, float) @@ -386,6 +388,7 @@ return weights + def __getattr__(self, attr): lookup_tbl = {('wd', 'sx'): (self._sd2wt, self.sx), ('wd', 'covx'): (self._cov2wt, self.covx), @@ -406,81 +409,74 @@ else: return None + class Model(object): - """The Model class stores information about the function you wish -to fit. It stores the function itself, at the least, and optionally -stores functions which compute the Jacobians used during fitting. Also, -one can provide a function that will provide reasonable starting values -for the fit parameters possibly given the set of data. + """ The Model class stores information about the function you wish to fit. - Model(fcn, fjacb=None, fjacd=None, extra_args=None, - estimate=None, implicit=0, meta={}) + It stores the function itself, at the least, and optionally stores functions + which compute the Jacobians used during fitting. Also, one can provide + a function that will provide reasonable starting values for the fit + parameters possibly given the set of data. -The initialization method stores these into members of the -same name. + The initialization method stores these into members of the + same name. - fcn -- fit function: fcn(beta, x) --> y + fcn -- fit function: fcn(beta, x) --> y - fjacb -- Jacobian of fcn wrt the fit parameters beta: - fjacb(beta, x) --> @f_i(x,B)/@B_j + fjacb -- Jacobian of fcn wrt the fit parameters beta: + fjacb(beta, x) --> @f_i(x,B)/@B_j - fjacd -- Jacobian of fcn wrt the (possibly multidimensional) input variable: - fjacd(beta, x) --> @f_i(x,B)/@x_j + fjacd -- Jacobian of fcn wrt the (possibly multidimensional) input variable: + fjacd(beta, x) --> @f_i(x,B)/@x_j - extra_args -- if specified, extra_args should be a tuple of extra arguments - to pass to fcn, fjacb, and fjacd. Each will be called - like the following: apply(fcn, (beta, x) + extra_args) + extra_args -- if specified, extra_args should be a tuple of extra + arguments to pass to fcn, fjacb, and fjacd. Each will be called like + the following: apply(fcn, (beta, x) + extra_args) - estimate -- provide estimates of the fit parameters from the data: - estimate(data) --> estbeta + estimate -- provide estimates of the fit parameters from the data: + estimate(data) --> estbeta - implicit -- boolean variable which, if TRUE, specifies that the model - is implicit; i.e fcn(beta, x) ~= 0 and there is no y data - to fit against. + implicit -- boolean variable which, if TRUE, specifies that the model + is implicit; i.e fcn(beta, x) ~= 0 and there is no y data to fit + against. - meta -- an optional, freeform dictionary of metadata for the model + meta -- an optional, freeform dictionary of metadata for the model -Note that the fcn, fjacb, and fjacd operate on NumPy arrays and return -a NumPy array. estimate takes an instance of the Data class. + Note that the fcn, fjacb, and fjacd operate on NumPy arrays and return + a NumPy array. estimate takes an instance of the Data class. -Model has one method: + Here are the rules for the shapes of the argument and return arrays: - set_meta(**kwds) -- conveniently update the metadata dictionary via the - keyword arguments. + x -- if the input data is single-dimensional, then x is rank-1 + array; i.e. x = array([1, 2, 3, ...]); x.shape = (n,) + If the input data is multi-dimensional, then x is a rank-2 array; i.e. + x = array([[1, 2, ...], [2, 4, ...]]); x.shape = (m, n) In all cases, it + has the same shape as the input data array passed to odr(). m is the + dimensionality of the input data, n is the number of observations. -Here are the rules for the shapes of the argument and return arrays: + y -- if the response variable is single-dimensional, then y is a rank-1 + array; i.e. y = array([2, 4, ...]); y.shape = (n,) + If the response variable is multi-dimensional, then y is a rank-2 array; + i.e. y = array([[2, 4, ...], [3, 6, ...]]); y.shape = (q, n) where q is + the dimensionality of the response variable. - x -- if the input data is single-dimensional, then x is rank-1 - array; i.e. x = array([1, 2, 3, ...]); x.shape = (n,) - If the input data is multi-dimensional, then x is a rank-2 array; - i.e. x = array([[1, 2, ...], [2, 4, ...]]); x.shape = (m, n) - In all cases, it has the same shape as the input data array passed - to odr(). m is the dimensionality of the input data, n is the - number of observations. + beta -- rank-1 array of length p where p is the number of parameters; + i.e. beta = array([B_1, B_2, ..., B_p]) - y -- if the response variable is single-dimensional, then y is a rank-1 - array; i.e. y = array([2, 4, ...]); y.shape = (n,) - If the response variable is multi-dimensional, then y is a rank-2 - array; i.e. y = array([[2, 4, ...], [3, 6, ...]]); y.shape = (q, n) - where q is the dimensionality of the response variable. + fjacb -- if the response variable is multi-dimensional, then the return + array's shape is (q, p, n) such that + fjacb(x,beta)[l,k,i] = @f_l(X,B)/@B_k evaluated at the i'th data point. + If q == 1, then the return array is only rank-2 and with shape (p, n). - beta -- rank-1 array of length p where p is the number of parameters; - i.e. beta = array([B_1, B_2, ..., B_p]) + fjacd -- as with fjacb, only the return array's shape is (q, m, n) such that + fjacd(x,beta)[l,j,i] = @f_l(X,B)/@X_j at the i'th data point. + If q == 1, then the return array's shape is (m, n). If m == 1, the shape + is (q, n). If m == q == 1, the shape is (n,). + """ - fjacb -- if the response variable is multi-dimensional, then the return - array's shape is (q, p, n) such that - fjacb(x,beta)[l,k,i] = @f_l(X,B)/@B_k evaluated at the i'th data - point. - If q == 1, then the return array is only rank-2 and with shape - (p, n). + def __init__(self, fcn, fjacb=None, fjacd=None, + extra_args=None, estimate=None, implicit=0, meta=None): - fjacd -- as with fjacb, only the return array's shape is (q, m, n) such that - fjacd(x,beta)[l,j,i] = @f_l(X,B)/@X_j at the i'th data point. - If q == 1, then the return array's shape is (m, n). If m == 1, - the shape is (q, n). If m == q == 1, the shape is (n,).""" - - def __init__(self, fcn, fjacb=None, fjacd=None, - extra_args=None, estimate=None, implicit=0, meta=None): self.fcn = fcn self.fjacb = fjacb self.fjacd = fjacd @@ -493,75 +489,82 @@ self.implicit = implicit self.meta = meta + def set_meta(self, **kwds): - """Update the metadata dictionary with the keywords and data provided -here. - E.g. set_meta(name="Exponential", equation="y = a exp(b x) + c") + """ Update the metadata dictionary with the keywords and data provided + here. - set_meta(**kwds)""" + Example + ------- + set_meta(name="Exponential", equation="y = a exp(b x) + c") + """ self.meta.update(kwds) + def __getattr__(self, attr): + """ Dispatch attribute access to the metadata. + """ + if attr in self.meta.keys(): return self.meta[attr] else: raise AttributeError, "'%s' not in metadata" % attr + class Output(object): - """The Output class stores the output of an ODR run. + """ The Output class stores the output of an ODR run. -Takes one argument for initialization: the return value from the -function odr(). + Takes one argument for initialization: the return value from the + function odr(). - Output(output) + Attributes + ---------- + beta -- estimated parameter values [beta.shape == (q,)] -Members: + sd_beta -- standard errors of the estimated parameters + [sd_beta.shape == (p,)] - beta -- estimated parameter values [beta.shape == (q,)] + cov_beta -- covariance matrix of the estimated parameters + [cov_beta.shape == (p, p)] - sd_beta -- standard errors of the estimated parameters - [sd_beta.shape == (p,)] + Optional Attributes + ------------------- + Present if odr() was run with "full_output=1". - cov_beta -- covariance matrix of the estimated parameters - [cov_beta.shape == (p, p)] + delta -- array of estimated errors in input variables + [delta.shape == data.x.shape] -Optional Members (if odr() was run with "full_output=1": + eps -- array of estimated errors in response variables + [eps.shape == data.y.shape] - delta -- array of estimated errors in input variables - [delta.shape == data.x.shape] + xplus -- array of x + delta [xplus.shape == data.x.shape] - eps -- array of estimated errors in response variables - [eps.shape == data.y.shape] + y -- array of y = fcn(x + delta) [y.shape == data.y.shape] - xplus -- array of x + delta [xplus.shape == data.x.shape] + res_var -- residual variance [scalar] - y -- array of y = fcn(x + delta) [y.shape == data.y.shape] + sum_sqare -- sum of squares error [scalar] - res_var -- residual variance [scalar] + sum_square_delta -- sum of squares of delta error [scalar] - sum_sqare -- sum of squares error [scalar] + sum_square_eps -- sum of squares of eps error [scalar] - sum_square_delta -- sum of squares of delta error [scalar] + inv_condnum -- inverse condition number [scalar] (cf. ODRPACK UG p. 77) - sum_square_eps -- sum of squares of eps error [scalar] + rel_error -- relative error in function values computed within fcn [scalar] - inv_condnum -- inverse condition number [scalar] (cf. ODRPACK UG p. 77) + work -- final work array [array] - rel_error -- relative error in function values computed within fcn [scalar] + work_ind -- indices into work for drawing out values [dictionary] + (cf. ODRPACK UG p. 83) - work -- final work array [array] + info -- reason for returning (as output by ODRPACK) [integer] + (cf. ODRPACK UG p. 38) - work_ind -- indices into work for drawing out values [dictionary] - (cf. ODRPACK UG p. 83) + stopreason -- "info" interpreted into English [list of strings] + """ - info -- reason for returning (as output by ODRPACK) [integer] - (cf. ODRPACK UG p. 38) - - stopreason -- "info" interpreted into English [list of strings] - -""" - def __init__(self, output): self.beta = output[0] self.sd_beta = output[1] @@ -572,8 +575,9 @@ self.__dict__.update(output[3]) self.stopreason = report_error(self.info) + def pprint(self): - """Pretty-print important results. + """ Pretty-print important results. """ print 'Beta:', self.beta @@ -586,118 +590,110 @@ for r in self.stopreason: print ' %s' % r + class ODR(object): - """The ODR class gathers all information and coordinates the running -of the main fitting routine. + """ The ODR class gathers all information and coordinates the running of the + main fitting routine. -Members of instances of the ODR class have the same names as the arguments -to the initialization routine. Descriptions are given below. + Members of instances of the ODR class have the same names as the arguments + to the initialization routine. - Required: - data -- instance of the Data class + Parameters + ---------- + Required: + data -- instance of the Data class - model -- instance of the Model class + model -- instance of the Model class - beta0 -- a rank-1 sequence of initial parameter values. Optional if - model provides an "estimate" function to estimate these values. + beta0 -- a rank-1 sequence of initial parameter values. Optional if + model provides an "estimate" function to estimate these values. - Optional: - delta0 -- a (double-precision) float array to hold the initial values of - the errors in the input variables. Must be same shape as data.x . + Optional: + delta0 -- a (double-precision) float array to hold the initial values of + the errors in the input variables. Must be same shape as data.x . - ifixb -- sequence of integers with the same length as beta0 that determines - which parameters are held fixed. A value of 0 fixes the parameter, - a value > 0 makes the parameter free. + ifixb -- sequence of integers with the same length as beta0 that determines + which parameters are held fixed. A value of 0 fixes the parameter, + a value > 0 makes the parameter free. - ifixx -- an array of integers with the same shape as data.x that determines - which input observations are treated as fixed. One can use a - sequence of length m (the dimensionality of the input observations) - to fix some dimensions for all observations. A value of 0 fixes the - observation, a value > 0 makes it free. + ifixx -- an array of integers with the same shape as data.x that determines + which input observations are treated as fixed. One can use a sequence of + length m (the dimensionality of the input observations) to fix some + dimensions for all observations. A value of 0 fixes the observation, + a value > 0 makes it free. - job -- an integer telling ODRPACK what tasks to perform. See p. 31 of the - ODRPACK User's Guide if you absolutely must set the value here. Use - the method set_job post-initialization for a more readable interface. + job -- an integer telling ODRPACK what tasks to perform. See p. 31 of the + ODRPACK User's Guide if you absolutely must set the value here. Use the + method set_job post-initialization for a more readable interface. - iprint -- an integer telling ODRPACK what to print. See pp. 33-34 of the - ODRPACK User's Guide if you absolutely must set the value here. Use - the method set_iprint post-initialization for a more readable - interface. + iprint -- an integer telling ODRPACK what to print. See pp. 33-34 of the + ODRPACK User's Guide if you absolutely must set the value here. Use the + method set_iprint post-initialization for a more readable interface. - errfile -- string with the filename to print ODRPACK errors to. *Do Not Open - This File Yourself!* + errfile -- string with the filename to print ODRPACK errors to. *Do Not Open + This File Yourself!* - rptfile -- string with the filename to print ODRPACK summaries to. *Do Not - Open This File Yourself!* + rptfile -- string with the filename to print ODRPACK summaries to. *Do Not + Open This File Yourself!* - ndigit -- integer specifying the number of reliable digits in the computation - of the function. + ndigit -- integer specifying the number of reliable digits in the computation + of the function. - taufac -- float specifying the initial trust region. The default value is 1. - The initial trust region is equal to taufac times the length of the - first computed Gauss-Newton step. taufac must be less than 1. + taufac -- float specifying the initial trust region. The default value is 1. + The initial trust region is equal to taufac times the length of the + first computed Gauss-Newton step. taufac must be less than 1. - sstol -- float specifying the tolerance for convergence based on the relative - change in the sum-of-squares. The default value is eps**(1/2) where - eps is the smallest value such that 1 + eps > 1 for double precision - computation on the machine. sstol must be less than 1. + sstol -- float specifying the tolerance for convergence based on the relative + change in the sum-of-squares. The default value is eps**(1/2) where eps + is the smallest value such that 1 + eps > 1 for double precision + computation on the machine. sstol must be less than 1. - partol -- float specifying the tolerance for convergence based on the relative - change in the estimated parameters. The default value is eps**(2/3) - for explicit models and eps**(1/3) for implicit models. partol must - be less than 1. + partol -- float specifying the tolerance for convergence based on the relative + change in the estimated parameters. The default value is eps**(2/3) for + explicit models and eps**(1/3) for implicit models. partol must be less + than 1. - maxit -- integer specifying the maximum number of iterations to perform. For - first runs, maxit is the total number of iterations performed and - defaults to 50. For restarts, maxit is the number of additional - iterations to perform and defaults to 10. + maxit -- integer specifying the maximum number of iterations to perform. For + first runs, maxit is the total number of iterations performed and + defaults to 50. For restarts, maxit is the number of additional + iterations to perform and defaults to 10. - stpb -- sequence (len(stpb) == len(beta0)) of relative step sizes to compute - finite difference derivatives wrt the parameters. + stpb -- sequence (len(stpb) == len(beta0)) of relative step sizes to compute + finite difference derivatives wrt the parameters. - stpd -- array (stpd.shape == data.x.shape or stpd.shape == (m,)) of relative - step sizes to compute finite difference derivatives wrt the input - variable errors. If stpd is a rank-1 array with length m (the - dimensionality of the input variable), then the values are broadcast - to all observations. + stpd -- array (stpd.shape == data.x.shape or stpd.shape == (m,)) of relative + step sizes to compute finite difference derivatives wrt the input + variable errors. If stpd is a rank-1 array with length m (the + dimensionality of the input variable), then the values are broadcast to + all observations. - sclb -- sequence (len(stpb) == len(beta0)) of scaling factors for the - parameters. The purpose of these scaling factors are to scale all of - the parameters to around unity. Normally appropriate scaling factors - are computed if this argument is not specified. Specify them yourself - if the automatic procedure goes awry. + sclb -- sequence (len(stpb) == len(beta0)) of scaling factors for the + parameters. The purpose of these scaling factors are to scale all of + the parameters to around unity. Normally appropriate scaling factors are + computed if this argument is not specified. Specify them yourself if the + automatic procedure goes awry. - scld -- array (scld.shape == data.x.shape or scld.shape == (m,)) of scaling - factors for the *errors* in the input variables. Again, these factors - are automatically computed if you do not provide them. If - scld.shape == (m,), then the scaling factors are broadcast to all - observations. + scld -- array (scld.shape == data.x.shape or scld.shape == (m,)) of scaling + factors for the *errors* in the input variables. Again, these factors + are automatically computed if you do not provide them. If scld.shape == + (m,), then the scaling factors are broadcast to all observations. - work -- array to hold the double-valued working data for ODRPACK. When - restarting, takes the value of self.output.work . + work -- array to hold the double-valued working data for ODRPACK. When + restarting, takes the value of self.output.work . - iwork -- array to hold the integer-valued working data for ODRPACK. When - restarting, takes the value of self.output.iwork . + iwork -- array to hold the integer-valued working data for ODRPACK. When + restarting, takes the value of self.output.iwork . - Other Members (not supplied as initialization arguments): - output -- an instance if the Output class containing all of the returned - data from an invocation of ODR.run() or ODR.restart() + Other Members (not supplied as initialization arguments): + output -- an instance if the Output class containing all of the returned + data from an invocation of ODR.run() or ODR.restart() + """ - Methods (see docstrings): - set_job -- specifies the tasks to perform. + def __init__(self, data, model, beta0=None, delta0=None, ifixb=None, + ifixx=None, job=None, iprint=None, errfile=None, rptfile=None, + ndigit=None, taufac=None, sstol=None, partol=None, maxit=None, + stpb=None, stpd=None, sclb=None, scld=None, work=None, iwork=None): - set_iprint -- specifies what reports to make. - - run -- runs the fitting routine once everything is set up. - - restart -- restarts a previous run. -""" - - def __init__(self, data, model, beta0=None, delta0=None, ifixb=None, - ifixx=None, job=None, iprint=None, errfile=None, rptfile=None, - ndigit=None, taufac=None, sstol=None, partol=None, maxit=None, - stpb=None, stpd=None, sclb=None, scld=None, work=None, - iwork=None): self.data = data self.model = model @@ -705,8 +701,9 @@ if self.model.estimate is not None: self.beta0 = _conv(self.model.estimate(self.data)) else: - raise ValueError, \ + raise ValueError( "must specify beta0 or provide an estimater with the model" + ) else: self.beta0 = _conv(beta0) @@ -734,26 +731,25 @@ self._check() def _check(self): - """Check the inputs for consistency, but don't bother -checking things that the builtin function odr will check. + """ Check the inputs for consistency, but don't bother checking things + that the builtin function odr will check. + """ - _check()""" - x_s = list(self.data.x.shape) if isinstance(self.data.y, numpy.ndarray): y_s = list(self.data.y.shape) if self.model.implicit: - raise odr_error, "an implicit model cannot use response data" + raise odr_error("an implicit model cannot use response data") else: # implicit model with q == self.data.y y_s = [self.data.y, x_s[-1]] if not self.model.implicit: - raise odr_error, "an explicit model needs response data" + raise odr_error("an explicit model needs response data") self.set_job(fit_type=1) if x_s[-1] != y_s[-1]: - raise odr_error, "number of observations do not match" + raise odr_error("number of observations do not match") n = x_s[-1] @@ -793,33 +789,34 @@ arglist = (self.beta0, self.data.x) if self.model.extra_args is not None: arglist = arglist + self.model.extra_args - res = apply(self.model.fcn, arglist) + res = self.model.fcn(*arglist) if res.shape not in fcn_perms: - raise odr_error, "fcn does not output %s-shaped array" % `y_s` + print res.shape + print fcn_perms + raise odr_error("fcn does not output %s-shaped array" % y_s) if self.model.fjacd is not None: - res = apply(self.model.fjacd, arglist) + res = self.model.fjacd(*arglist) if res.shape not in fjacd_perms: - raise odr_error, \ - "fjacd does not output %s-shaped array" % `(q, m, n)` + raise odr_error( + "fjacd does not output %s-shaped array" % (q, m, n)) if self.model.fjacb is not None: - res = apply(self.model.fjacb, arglist) + res = self.model.fjacb(*arglist) if res.shape not in fjacb_perms: - raise odr_error, \ - "fjacb does not output %s-shaped array" % `(q, p, n)` + raise odr_error( + "fjacb does not output %s-shaped array" % (q, p, n)) # check shape of delta0 if self.delta0 is not None and self.delta0.shape != self.data.x.shape: - raise odr_error, \ - "delta0 is not a %s-shaped array" % `self.data.x.shape` + raise odr_error( + "delta0 is not a %s-shaped array" % self.data.x.shape) def _gen_work(self): - """Generate a suitable work array if one does not already exist. + """ Generate a suitable work array if one does not already exist. + """ - _gen_work()""" - n = self.data.x.shape[-1] p = self.beta0.shape[0] @@ -861,50 +858,52 @@ else: self.work = numpy.zeros((lwork,), float) + def set_job(self, fit_type=None, deriv=None, var_calc=None, - del_init=None, restart=None): - """Sets the "job" parameter is a hopefully comprehensible way. + del_init=None, restart=None): + """ Sets the "job" parameter is a hopefully comprehensible way. -If an argument is not specified, then the value is left as is. The default -value from class initialization is for all of these options set to 0. + If an argument is not specified, then the value is left as is. The + default value from class initialization is for all of these options set + to 0. -______________________________________________________________________________ -Parameter Value Meaning ---------- ----- ------- -fit_type 0 explicit ODR - 1 implicit ODR - 2 ordinary least-squares + _______________________________________________________________________ + Parameter Value Meaning + --------- ----- ------- + fit_type 0 explicit ODR + 1 implicit ODR + 2 ordinary least-squares -deriv 0 forward finite differences - 1 central finite differences - 2 user-supplied derivatives (Jacobians) with results checked - by ODRPACK - 3 user-supplied derivatives, no checking + deriv 0 forward finite differences + 1 central finite differences + 2 user-supplied derivatives (Jacobians) with results + checked by ODRPACK + 3 user-supplied derivatives, no checking -var_calc 0 calculate asymptotic covariance matrix and fit parameter - uncertainties (V_B, s_B) using derivatives recomputed - at the final solution - 1 calculate V_B and s_B using derivatives from last iteration - 2 do not calculate V_B and s_B + var_calc 0 calculate asymptotic covariance matrix and fit + parameter uncertainties (V_B, s_B) using derivatives + recomputed at the final solution + 1 calculate V_B and s_B using derivatives from last + iteration + 2 do not calculate V_B and s_B -del_init 0 initial input variable offsets set to 0 - 1 initial offsets provided by user in variable "work" + del_init 0 initial input variable offsets set to 0 + 1 initial offsets provided by user in variable "work" -restart 0 fit is not a restart - 1 fit is a restart -______________________________________________________________________________ + restart 0 fit is not a restart + 1 fit is a restart + _______________________________________________________________________ -The permissible values are different from those given on pg. 31 of the -ODRPACK User's Guide only in that one cannot specify numbers greater than the -last value for each variable. + The permissible values are different from those given on pg. 31 of the + ODRPACK User's Guide only in that one cannot specify numbers greater than the + last value for each variable. -If one does not supply functions to compute the Jacobians, the fitting -procedure will change deriv to 0, finite differences, as a default. To -initialize the input variable offsets by yourself, set del_init to 1 and -put the offsets into the "work" variable correctly. + If one does not supply functions to compute the Jacobians, the fitting + procedure will change deriv to 0, finite differences, as a default. To + initialize the input variable offsets by yourself, set del_init to 1 and + put the offsets into the "work" variable correctly. + """ - set_job(fit_type=None, deriv=None, var_calc=None, del_init=None, restart=None) -""" if self.job is None: job_l = [0, 0, 0, 0, 0] else: @@ -928,36 +927,33 @@ self.job = (job_l[0]*10000 + job_l[1]*1000 + job_l[2]*100 + job_l[3]*10 + job_l[4]) + def set_iprint(self, init=None, so_init=None, - iter=None, so_iter=None, iter_step=None, - final=None, so_final=None): - """Set the iprint parameter for the printing of computation reports. + iter=None, so_iter=None, iter_step=None, final=None, so_final=None): + """ Set the iprint parameter for the printing of computation reports. -If any of the arguments are specified here, then they are set in the iprint -member. If iprint is not set manually or with this method, then ODRPACK -defaults to no printing. If no filename is specified with the member rptfile, -then ODRPACK prints to stdout. One can tell ODRPACK to print to stdout -in addition to the specified filename by setting the so_* arguments to this -function, but one cannot specify to print to stdout but not a file since -one can do that by not specifying a rptfile filename. + If any of the arguments are specified here, then they are set in the + iprint member. If iprint is not set manually or with this method, then + ODRPACK defaults to no printing. If no filename is specified with the + member rptfile, then ODRPACK prints to stdout. One can tell ODRPACK to + print to stdout in addition to the specified filename by setting the + so_* arguments to this function, but one cannot specify to print to + stdout but not a file since one can do that by not specifying a rptfile + filename. -There are three reports: initialization, iteration, and final reports. -They are represented by the arguments init, iter, and final respectively. -The permissible values are 0, 1, and 2 representing "no report", "short -report", and "long report" respectively. + There are three reports: initialization, iteration, and final reports. + They are represented by the arguments init, iter, and final + respectively. The permissible values are 0, 1, and 2 representing "no + report", "short report", and "long report" respectively. -The argument iter_step (0 <= iter_step <= 9) specifies how often to make the -iteration report; the report will be made for every iter_step'th iteration -starting with iteration one. If iter_step == 0, then no iteration report -is made, regardless of the other arguments. + The argument iter_step (0 <= iter_step <= 9) specifies how often to make + the iteration report; the report will be made for every iter_step'th + iteration starting with iteration one. If iter_step == 0, then no + iteration report is made, regardless of the other arguments. -If the rptfile is None, then any so_* arguments supplied will raise an -exception. - - set_iprint(init=None, so_init=None, - iter=None, so_iter=None, iter_step=None, - final=None, so_final=None) -""" + If the rptfile is None, then any so_* arguments supplied will raise an + exception. + """ if self.iprint is None: self.iprint = 0 @@ -976,12 +972,12 @@ [1, 2], # short, long [2, 2]] # long, long - if self.rptfile is None and \ - (so_init is not None or - so_iter is not None or - so_final is not None): - raise odr_error, \ - "no rptfile specified, cannot output to stdout twice" + if (self.rptfile is None and + (so_init is not None or + so_iter is not None or + so_final is not None)): + raise odr_error( + "no rptfile specified, cannot output to stdout twice") iprint_l = ip2arg[ip[0]] + ip2arg[ip[1]] + ip2arg[ip[3]] @@ -1008,12 +1004,16 @@ self.iprint = ip[0]*1000 + ip[1]*100 + ip[2]*10 + ip[3] + def run(self): - """Run the fitting routine with all of the information given. + """ Run the fitting routine with all of the information given. -Returns an Output instance and assigns it to the member self.output . + Returns + ------- + output : Output instance + This object is also assigned to the attribute .output . + """ - run()""" args = (self.model.fcn, self.beta0, self.data.y, self.data.x) kwds = {'full_output': 1} kwd_l = ['ifixx', 'ifixb', 'job', 'iprint', 'errfile', 'rptfile', @@ -1029,7 +1029,6 @@ self.work[:len(d0)] = d0 # set the kwds from other objects explicitly - if self.model.fjacb is not None: kwds['fjacb'] = self.model.fjacb if self.model.fjacd is not None: @@ -1041,9 +1040,7 @@ if self.model.extra_args is not None: kwds['extra_args'] = self.model.extra_args - # implicitly set kwds from self's members - for attr in kwd_l: obj = getattr(self, attr) if obj is not None: @@ -1053,12 +1050,20 @@ return self.output + def restart(self, iter=None): - """Restarts the run with iter more iterations. + """ Restarts the run with iter more iterations. -ODRPACK's default for the number of new iterations is 10. + Parameters + ---------- + iter : int, optional + ODRPACK's default for the number of new iterations is 10. - restart(iter=None)""" + Returns + ------- + output : Output instance + This object is also assigned to the attribute .output . + """ if self.output is None: raise odr_error, "cannot restart: run() has not been called before" @@ -1070,3 +1075,5 @@ self.maxit = iter return self.run() + +#### EOF ####################################################################### Modified: trunk/Lib/sandbox/odr/tests/test_odrpack.py =================================================================== --- trunk/Lib/sandbox/odr/tests/test_odrpack.py 2006-09-21 04:24:53 UTC (rev 2206) +++ trunk/Lib/sandbox/odr/tests/test_odrpack.py 2006-09-21 05:19:03 UTC (rev 2207) @@ -85,7 +85,8 @@ -0.78,-1.36,-1.9,-2.5,-2.88,-3.18,-3.44], [-0.12,-0.6,-1.,-1.4,-2.54,-3.36,-4.,-4.75,-5.25,-5.64,-5.97,-6.32, -6.44,-6.44,-6.41,-6.25,-5.88,-5.5,-5.24,-4.86]], - 1) + 1, + ) implicit_odr = ODR(implicit_dat, implicit_mod, beta0=[-1.0, -3.0, 0.09, 0.02, 0.08]) From scipy-svn at scipy.org Thu Sep 21 05:27:30 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 21 Sep 2006 04:27:30 -0500 (CDT) Subject: [Scipy-svn] r2208 - in trunk/Lib/linsolve/umfpack: . tests Message-ID: <20060921092730.A9DFE39C11A@new.scipy.org> Author: rc Date: 2006-09-21 04:26:58 -0500 (Thu, 21 Sep 2006) New Revision: 2208 Modified: trunk/Lib/linsolve/umfpack/info.py trunk/Lib/linsolve/umfpack/tests/test_umfpack.py trunk/Lib/linsolve/umfpack/umfpack.i trunk/Lib/linsolve/umfpack/umfpack.py Log: merged lu() method added by Nathan Bell Modified: trunk/Lib/linsolve/umfpack/info.py =================================================================== --- trunk/Lib/linsolve/umfpack/info.py 2006-09-21 05:19:03 UTC (rev 2207) +++ trunk/Lib/linsolve/umfpack/info.py 2006-09-21 09:26:58 UTC (rev 2208) @@ -42,6 +42,7 @@ include_dirs = /UFsparse/UMFPACK/Include, /UFsparse/UFconfig umfpack_libs = umfpack + Examples: ========= @@ -97,6 +98,36 @@ # Print all statistics. umfpack.report_info() +-or- + +# Get LU factors and permutation matrices of a matrix. +L, U, P, Q, R, do_recip = umfpack.lu( mtx ) + +Then: + L - Lower triangular m-by-min(m,n) CSR matrix + U - Upper triangular min(m,n)-by-n CSC matrix + P - Vector of row permuations + Q - Vector of column permuations + R - Vector of diagonal row scalings + do_recip - boolean + + For a given matrix A, the decomposition satisfies: + LU = PRAQ when do_recip is true + LU = P(R^-1)AQ when do_recip is false + +Description of arguments of UmfpackContext solution methods: +============================================= +This holds for: umfpack(), umfpack.linsolve(), umfpack.solve() + + sys - one of UMFPACK system description constants, like + UMFPACK_A, UMFPACK_At, see umfSys list and UMFPACK + docs + mtx - sparse matrix (CSR or CSC) + rhs - right hand side vector + autoTranspose - automatically changes 'sys' to the + transposed type, if 'mtx' is in CSR, since UMFPACK + assumes CSC internally + Setting control parameters: =========================== Assuming this module imported as um: @@ -113,6 +144,8 @@ -- Author: Robert Cimrman + +Other contributors: Nathan Bell (lu() method wrappers) """ postpone_import = 1 Modified: trunk/Lib/linsolve/umfpack/tests/test_umfpack.py =================================================================== --- trunk/Lib/linsolve/umfpack/tests/test_umfpack.py 2006-09-21 05:19:03 UTC (rev 2207) +++ trunk/Lib/linsolve/umfpack/tests/test_umfpack.py 2006-09-21 09:26:58 UTC (rev 2208) @@ -10,8 +10,12 @@ import random from numpy.testing import * set_package_path() -from scipy import linsolve +from scipy import linsolve, rand, matrix, diag, eye from scipy.sparse import csc_matrix, dok_matrix, spdiags + +import numpy as nm +import scipy.linsolve.umfpack as um + restore_path() class test_solvers(ScipyTestCase): @@ -67,5 +71,65 @@ self.b = array([1, 2, 3, 4, 5]) + +class test_factorization(ScipyTestCase): + """Tests factorizing a sparse linear system""" + + def check_complex_lu(self): + """Getting factors of complex matrix""" + umfpack = um.UmfpackContext("zi") + + for A in self.complex_matrices: + umfpack.numeric(A) + + (L,U,P,Q,R,do_recip) = umfpack.lu(A) + + L = L.todense() + U = U.todense() + A = A.todense() + if not do_recip: R = 1.0/R + R = matrix(diag(R)) + P = eye(A.shape[0])[P,:] + Q = eye(A.shape[1])[:,Q] + + assert_array_almost_equal(P*R*A*Q,L*U) + + def check_real_lu(self): + """Getting factors of real matrix""" + umfpack = um.UmfpackContext("di") + + for A in self.real_matrices: + umfpack.numeric(A) + + (L,U,P,Q,R,do_recip) = umfpack.lu(A) + + L = L.todense() + U = U.todense() + A = A.todense() + if not do_recip: R = 1.0/R + R = matrix(diag(R)) + P = eye(A.shape[0])[P,:] + Q = eye(A.shape[1])[:,Q] + + assert_array_almost_equal(P*R*A*Q,L*U) + + + def setUp(self): + random.seed(0) #make tests repeatable + self.real_matrices = [] + self.real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]], + [0, 1], 5, 5)) + self.real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]], + [0, 1], 4, 5)) + self.real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]], + [0, 2], 5, 5)) + self.real_matrices.append(csc_matrix(rand(3,3))) + self.real_matrices.append(csc_matrix(rand(5,4))) + self.real_matrices.append(csc_matrix(rand(4,5))) + + self.complex_matrices = [x.astype(nm.complex128) + for x in self.real_matrices] + + if __name__ == "__main__": ScipyTest().run() Modified: trunk/Lib/linsolve/umfpack/umfpack.i =================================================================== --- trunk/Lib/linsolve/umfpack/umfpack.i 2006-09-21 05:19:03 UTC (rev 2207) +++ trunk/Lib/linsolve/umfpack/umfpack.i 2006-09-21 09:26:58 UTC (rev 2208) @@ -220,3 +220,49 @@ %include %include + + + +/* + * wnbell - attempt to get L,U,P,Q out + */ +%include "typemaps.i" +%apply int *OUTPUT { + int *lnz, + int *unz, + int *n_row, + int *n_col, + int *nz_udiag +}; +%apply long *OUTPUT { + long *lnz, + long *unz, + long *n_row, + long *n_col, + long *nz_udiag +}; +%include + + +ARRAY_IN( double, double, DOUBLE ) +%apply double *array { + double Lx [ ], + double Lz [ ], + double Ux [ ], + double Uz [ ], + double Dx [ ], + double Dz [ ], + double Rs [ ] +}; + +ARRAY_IN( int, int, INT ) +%apply int *array { + int Lp [ ], + int Lj [ ], + int Up [ ], + int Ui [ ], + int P [ ], + int Q [ ] +}; +%apply int *OUTPUT { int *do_recip}; +%include Modified: trunk/Lib/linsolve/umfpack/umfpack.py =================================================================== --- trunk/Lib/linsolve/umfpack/umfpack.py 2006-09-21 05:19:03 UTC (rev 2207) +++ trunk/Lib/linsolve/umfpack/umfpack.py 2006-09-21 09:26:58 UTC (rev 2208) @@ -467,7 +467,19 @@ # 21.12.2005 # 01.03.2006 def solve( self, sys, mtx, rhs, autoTranspose = False ): - """Solution of system of linear equation using the Numeric object.""" + """ + Solution of system of linear equation using the Numeric object. + + Arguments: + sys - one of UMFPACK system description constants, like + UMFPACK_A, UMFPACK_At, see umfSys list and UMFPACK + docs + mtx - sparse matrix (CSR or CSC) + rhs - right hand side vector + autoTranspose - automatically changes 'sys' to the + transposed type, if 'mtx' is in CSR, since UMFPACK + assumes CSC internally + """ if sys not in umfSys: raise ValueError, 'sys must be in' % umfSys @@ -526,9 +538,21 @@ # 30.11.2005, c # 01.12.2005 def linsolve( self, sys, mtx, rhs, autoTranspose = False ): - """One-shot solution of system of linear equation. Reuses Numeric - object if possible.""" + """ + One-shot solution of system of linear equation. Reuses Numeric object + if possible. + Arguments: + sys - one of UMFPACK system description constants, like + UMFPACK_A, UMFPACK_At, see umfSys list and UMFPACK + docs + mtx - sparse matrix (CSR or CSC) + rhs - right hand side vector + autoTranspose - automatically changes 'sys' to the + transposed type, if 'mtx' is in CSR, since UMFPACK + assumes CSC internally + """ + # print self.family if sys not in umfSys: raise ValueError, 'sys must be in' % umfSys @@ -548,10 +572,116 @@ # 30.11.2005, c # 01.12.2005 def __call__( self, sys, mtx, rhs, autoTranspose = False ): - """Uses solve() or linsolve() depending on the presence of - the Numeric object.""" + """ + Uses solve() or linsolve() depending on the presence of the Numeric + object. + Arguments: + sys - one of UMFPACK system description constants, like + UMFPACK_A, UMFPACK_At, see umfSys list and UMFPACK + docs + mtx - sparse matrix (CSR or CSC) + rhs - right hand side vector + autoTranspose - automatically changes 'sys' to the + transposed type, if 'mtx' is in CSR, since UMFPACK + assumes CSC internally + """ + if self._numeric is not None: return self.solve( sys, mtx, rhs, autoTranspose ) else: return self.linsolve( sys, mtx, rhs, autoTranspose ) + + ## + # 21.09.2006, added by Nathan Bell + def lu( self, mtx ): + """ + Returns an LU decomposition of an m-by-n matrix in the form + (L, U, P, Q, R, do_recip): + + L - Lower triangular m-by-min(m,n) CSR matrix + U - Upper triangular min(m,n)-by-n CSC matrix + P - Vector of row permuations + Q - Vector of column permuations + R - Vector of diagonal row scalings + do_recip - boolean + + For a given matrix A, the decomposition satisfies: + LU = PRAQ when do_recip is true + LU = P(R^-1)AQ when do_recip is false + """ + + #this should probably be changed + mtx = mtx.tocsc() + self.numeric( mtx ) + + #first find out how much space to reserve + (status, lnz, unz, n_row, n_col, nz_udiag)\ + = self.funs.get_lunz( self._numeric ) + + if status != UMFPACK_OK: + raise RuntimeError, '%s failed with %s' % (self.funs.get_lunz, + umfStatus[status]) + + #allocate storage for decomposition data + i_type = mtx.indptr.dtype + + Lp = nm.zeros( (n_row+1,), dtype = i_type ) + Lj = nm.zeros( (lnz,), dtype = i_type ) + Lx = nm.zeros( (lnz,), dtype = nm.double ) + + Up = nm.zeros( (n_col+1,), dtype = i_type ) + Ui = nm.zeros( (unz,), dtype = i_type ) + Ux = nm.zeros( (unz,), dtype = nm.double ) + + P = nm.zeros( (n_row,), dtype = i_type ) + Q = nm.zeros( (n_col,), dtype = i_type ) + + Dx = nm.zeros( (min(n_row,n_col),), dtype = nm.double ) + + Rs = nm.zeros( (n_row,), dtype = nm.double ) + + if self.isReal: + (status,do_recip) = self.funs.get_numeric( Lp,Lj,Lx,Up,Ui,Ux, + P,Q,Dx,Rs, + self._numeric ) + + if status != UMFPACK_OK: + raise RuntimeError, '%s failed with %s'\ + % (self.funs.get_numeric, umfStatus[status]) + + L = sp.csr_matrix((Lx,Lj,Lp),(n_row,min(n_row,n_col))) + U = sp.csc_matrix((Ux,Ui,Up),(min(n_row,n_col),n_col)) + R = Rs + + return (L,U,P,Q,R,do_recip) + + else: + #allocate additional storage for imaginary parts + Lz = nm.zeros( (lnz,), dtype = nm.double ) + Uz = nm.zeros( (unz,), dtype = nm.double ) + Dz = nm.zeros( (min(n_row,n_col),), dtype = nm.double ) + + (status,do_recip) = self.funs.get_numeric(Lp,Lj,Lx,Lz,Up,Ui,Ux,Uz, + P,Q,Dx,Dz,Rs, + self._numeric) + + if status != UMFPACK_OK: + raise RuntimeError, '%s failed with %s'\ + % (self.funs.get_numeric, umfStatus[status]) + + + Lxz = nm.zeros( (lnz,), dtype = nm.complex128 ) + Uxz = nm.zeros( (unz,), dtype = nm.complex128 ) + Dxz = nm.zeros( (min(n_row,n_col),), dtype = nm.complex128 ) + + Lxz.real,Lxz.imag = Lx,Lz + Uxz.real,Uxz.imag = Ux,Uz + Dxz.real,Dxz.imag = Dx,Dz + + L = sp.csr_matrix((Lxz,Lj,Lp),(n_row,min(n_row,n_col))) + U = sp.csc_matrix((Uxz,Ui,Up),(min(n_row,n_col),n_col)) + R = Rs + + return (L,U,P,Q,R,do_recip) + From scipy-svn at scipy.org Thu Sep 21 12:22:41 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 21 Sep 2006 11:22:41 -0500 (CDT) Subject: [Scipy-svn] r2209 - trunk/Lib/io Message-ID: <20060921162241.4028539C116@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-21 11:21:58 -0500 (Thu, 21 Sep 2006) New Revision: 2209 Modified: trunk/Lib/io/mio.py trunk/Lib/io/mio4.py trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py Log: Bug fix for recursive structs/objects, optimizations and cleanups for speed Modified: trunk/Lib/io/mio.py =================================================================== --- trunk/Lib/io/mio.py 2006-09-21 09:26:58 UTC (rev 2208) +++ trunk/Lib/io/mio.py 2006-09-21 16:21:58 UTC (rev 2209) @@ -9,7 +9,6 @@ from numpy import * -from bytestream import ByteStream from mio4 import MatFile4Reader, MatFile4Writer from mio5 import MatFile5Reader @@ -53,7 +52,7 @@ if full_name is None: raise IOError, "%s not found on the path." % file_name - byte_stream = ByteStream(memmap(full_name, mode='r')) + byte_stream = open(full_name, 'rb') MR = MatFile4Reader(byte_stream) if MR.format_looks_right(): return MR Modified: trunk/Lib/io/mio4.py =================================================================== --- trunk/Lib/io/mio4.py 2006-09-21 09:26:58 UTC (rev 2208) +++ trunk/Lib/io/mio4.py 2006-09-21 16:21:58 UTC (rev 2209) @@ -94,7 +94,7 @@ ''' Read and return Mat4 matrix header ''' header = Mat4Header() - data = self.read_array(self.dtypes['header']) + data = self.read_dtype(self.dtypes['header']) header.name = self.read_ztstring(data['namlen']) if data['mopt'] < 0 or data['mopt'] > 5000: ValueError, 'Mat 4 mopt wrong format, byteswapping problem?' @@ -125,10 +125,23 @@ is_global = False is_logical = False - def read_hdr_array(self, *args, **kwargs): - ''' Mat4 read array always uses header dtype and dims ''' - return self.read_array( - self.header.dtype, self.dims, *args, **kwargs) + def read_array(self, copy=True): + ''' Mat4 read array always uses header dtype and dims + @copy - copies array if True + (buffer is usually read only) + a_dtype is assumed to be correct endianness + ''' + dt = self.header.dtype + num_bytes = dt.itemsize + for d in self.dims: + num_bytes *= d + arr = ndarray(shape=self.dims, + dtype=dt, + buffer=self.mat_stream.read(num_bytes), + order='F') + if copy: + arr = arr.copy() + return arr class Mat4FullGetter(Mat4MatrixGetter): @@ -137,18 +150,18 @@ if self.header.is_complex: self.header.original_dtype = dtype(complex128) # avoid array copy to save memory - res = self.read_hdr_array(copy=False) - res_j = self.read_hdr_array(copy=False) + res = self.read_array(copy=False) + res_j = self.read_array(copy=False) return res + (res_j * 1j) else: self.header.original_dtype = dtype(float64) - return self.read_hdr_array() + return self.read_array() class Mat4CharGetter(Mat4MatrixGetter): def get_raw_array(self): self.header.is_char = True - arr = self.read_hdr_array().astype(uint8) + arr = self.read_array().astype(uint8) # ascii to unicode S = arr.tostring().decode('ascii') return ndarray(shape=self.dims, @@ -175,7 +188,7 @@ ''' def get_raw_array(self): self.header.original_dtype = dtype(float64) - res = self.read_hdr_array() + res = self.read_array() tmp = res[:-1,:] dims = res[-1,0:2] ij = transpose(tmp[:,0:2]) - 1 # for 1-based indexing @@ -209,13 +222,15 @@ def format_looks_right(self): # Mat4 files have a zero somewhere in first 4 bytes self.mat_stream.seek(0) - mopt_bytes = self.read_bytes(4) + mopt_bytes = ndarray(shape=(4,), + dtype=uint8, + buffer = self.mat_stream.read(4)) self.mat_stream.seek(0) return 0 in mopt_bytes def guess_byte_order(self): self.mat_stream.seek(0) - mopt = self.read_array(dtype('i4')) + mopt = self.read_dtype(dtype('i4')) self.mat_stream.seek(0) if mopt < 0 or mopt > 5000: return ByteOrder.swapped_code Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-09-21 09:26:58 UTC (rev 2208) +++ trunk/Lib/io/mio5.py 2006-09-21 16:21:58 UTC (rev 2209) @@ -28,10 +28,9 @@ import zlib from copy import copy as pycopy - +from cStringIO import StringIO from numpy import * -from bytestream import ByteStream from miobase import * miINT8 = 1 @@ -82,8 +81,7 @@ ('subsystem_offset', 'i8'), ('version', 'u2'), ('endian_test', 'S2')], - 'tag_mdtype': 'u4', - 'tag_byte_count': 'u4', + 'tag_full': [('mdtype', 'u4'), ('byte_count', 'u4')], 'array_flags': [('data_type', 'u4'), ('byte_count', 'u4'), ('flags_class','u4'), @@ -134,10 +132,6 @@ ''' Placeholder for holding read data from objects ''' pass -class Mat5Tag(object): - ''' Placeholder for holding tag information ''' - pass - class Mat5Header(object): ''' Placeholder for Mat5 header @@ -156,7 +150,6 @@ def __init__(self): self.next_position = None self.is_empty = False - self.flags = None self.is_complex = False self.is_global = False self.is_logical = False @@ -167,11 +160,7 @@ self.dims = () self.name = '' -class Mat5ArrayFlags(object): - ''' Place holder for array flags ''' - pass - class Mat5ArrayReader(MatArrayReader): ''' Class to get Mat5 arrays @@ -187,94 +176,79 @@ self.codecs = codecs self.class_dtypes = class_dtypes - def read_tag(self): - tag = Mat5Tag() - # Check for small data element first - tag.mdtype = int(self.read_array(self.dtypes['tag_mdtype'])) - byte_count = tag.mdtype >> 16 + def read_element(self, copy=True): + raw_tag = self.mat_stream.read(8) + tag = ndarray(shape=(), + dtype=self.dtypes['tag_full'], + buffer = raw_tag) + mdtype = tag['mdtype'] + byte_count = mdtype >> 16 if byte_count: # small data element format if byte_count > 4: raise ValueError, 'Too many bytes for sde format' - tag.byte_count = byte_count - tag.mdtype = tag.mdtype & 0xFFFF - tag.skip = 4 - byte_count - else: # standard tag format - tag.byte_count = self.read_array( - self.dtypes['tag_byte_count']) - tag.skip = tag.byte_count % 8 and 8 - tag.byte_count % 8 - return tag - - def read_element(self, copy=True): - tag = self.read_tag() - if tag.mdtype == miMATRIX: - header = self.read_header(tag) - return self.header_to_getter(header).get_array() - if tag.mdtype in self.codecs: # encoded char data - raw_str = self.read_bytes(tag.byte_count) - codec = self.codecs[tag.mdtype] + mdtype = mdtype & 0xFFFF + dt = self.dtypes[mdtype] + el_count = byte_count / dt.itemsize + return ndarray(shape=(el_count,), + dtype=dt, + buffer=raw_tag[4:]) + byte_count = tag['byte_count'] + if mdtype == miMATRIX: + return self.getter_from_bytes(byte_count).get_array() + if mdtype in self.codecs: # encoded char data + raw_str = self.mat_stream.read(byte_count) + codec = self.codecs[mdtype] if not codec: - raise TypeError, 'Do not support encoding %d' % tag.mdtype - el = raw_str.tostring().decode(codec) + raise TypeError, 'Do not support encoding %d' % mdtype + el = raw_str.decode(codec) else: # numeric data - try: - dt = self.dtypes[tag.mdtype] - except KeyError: - raise TypeError, 'Do not know matlab (TM) data code %d' \ - % tag.mdtype - el_count = tag.byte_count / dt.itemsize - el = self.read_array(dt, a_shape=(el_count), copy=copy) - if tag.skip: - self.mat_stream.seek(tag.skip, 1) + dt = self.dtypes[mdtype] + el_count = byte_count / dt.itemsize + el = ndarray(shape=(el_count,), + dtype=dt, + buffer=self.mat_stream.read(byte_count)) + if copy: + el = el.copy() + mod8 = byte_count % 8 + skip = mod8 and 8 - mod8 + if skip: + self.mat_stream.seek(skip, 1) return el - def read_header(self, tag): + def read_header(self): ''' Read header from Mat5 matrix ''' - if not tag.mdtype == miMATRIX: - raise TypeError, \ - 'Expecting miMATRIX type here, got %d' % tag.mdtype header = Mat5Header() - # Note - there is no skip value for the miMATRIX type; the - # number of bytes field in the tag points to the next variable - # in the file. This is always aligned to 8 byte boundaries - # (except for the miCOMPRESSED type) - header.next_position = (self.mat_stream.pos + - tag.byte_count) - # Apparently an empty miMATRIX can contain no bytes - header.is_empty = tag.byte_count == 0 - if header.is_empty: - return header - header.flags = self.read_array_flags() - header.is_complex = header.flags.is_complex - header.is_global = header.flags.is_global - header.is_logical = header.flags.is_logical - header.mclass = header.flags.mclass + af = self.read_dtype(self.dtypes['array_flags']) + flags_class = af['flags_class'] + header.mclass = flags_class & 0xFF + header.is_logical = flags_class >> 9 & 1 + header.is_global = flags_class >> 10 & 1 + header.is_complex = flags_class >> 11 & 1 + header.nzmax = af['nzmax'] header.dims = self.read_element() header.name = self.read_element().tostring() return header - def read_array_flags(self): - flags = Mat5ArrayFlags() - af = self.read_array(self.dtypes['array_flags']) - flags_class = af['flags_class'] - flags.mclass = flags_class & 0xFF - flags.is_logical = flags_class >> 9 & 1 - flags.is_global = flags_class >> 10 & 1 - flags.is_complex = flags_class >> 11 & 1 - flags.nzmax = af['nzmax'] - return flags - def matrix_getter_factory(self): ''' Returns reader for next matrix ''' - tag = self.read_tag() - if tag.mdtype == miCOMPRESSED: - return Mat5ZArrayReader(self, tag).matrix_getter_factory() - header = self.read_header(tag) - return self.header_to_getter(header) - - def header_to_getter(self, header): - if header.is_empty: + tag = self.read_dtype(self.dtypes['tag_full']) + mdtype = tag['mdtype'] + byte_count = tag['byte_count'] + if mdtype == miCOMPRESSED: + return Mat5ZArrayReader(self, byte_count).matrix_getter_factory() + if not mdtype == miMATRIX: + raise TypeError, \ + 'Expecting miMATRIX type here, got %d' % mdtype + return self.getter_from_bytes(byte_count) + + def getter_from_bytes(self, byte_count): + # Apparently an empty miMATRIX can contain no bytes + if not byte_count: return Mat5EmptyMatrixGetter(self, header) + next_pos = self.mat_stream.tell() + byte_count + header = self.read_header() + header.next_position = next_pos mc = header.mclass if mc in mx_numbers: return Mat5NumericMatrixGetter(self, header) @@ -299,22 +273,28 @@ allow skipping over this variable (although we have to read and uncompress the whole thing anyway to get the name) ''' - def __init__(self, array_reader, tag): + def __init__(self, array_reader, byte_count): '''Reads and uncompresses gzipped stream''' - data = array_reader.read_bytes(tag.byte_count) + data = array_reader.mat_stream.read(byte_count) super(Mat5ZArrayReader, self).__init__( - ByteStream(zlib.decompress(data.tostring())), + StringIO(zlib.decompress(data)), array_reader.dtypes, array_reader.processor_func, array_reader.codecs, array_reader.class_dtypes) - self.next_position = array_reader.mat_stream.tell() + self._next_position = array_reader.mat_stream.tell() - def header_to_getter(self, header): - ''' Set next_position to current position in parent stream ''' - header.next_position = self.next_position - return super(Mat5ZArrayReader, self).header_to_getter(header) + def getter_from_bytes(self, byte_count): + ''' Set next_position to current position in parent stream + self.next_position is only used by the get_variables routine + of the main file reading loop, so must refer to the position + in the main stream, not the compressed stream. + ''' + getter = super(Mat5ZArrayReader, self).getter_from_bytes(byte_count) + getter.next_position = self._next_position + return getter + class Mat5MatrixGetter(MatMatrixGetter): ''' Base class for getting Mat5 matrices @@ -329,9 +309,6 @@ self.codecs = array_reader.codecs self.is_global = header.is_global - def read_tag(self): - return self.array_reader.read_tag() - def read_element(self, *args, **kwargs): return self.array_reader.read_element(*args, **kwargs) @@ -422,7 +399,7 @@ class Mat5CellMatrixGetter(Mat5MatrixGetter): def get_raw_array(self): # Account for fortran indexing of cells - tupdims = tuple(self.dims[::-1]) + tupdims = tuple(self.dims[::-1]) length = product(self.dims) result = empty(length, dtype=object) for i in range(length): @@ -435,7 +412,10 @@ class Mat5StructMatrixGetter(Mat5CellMatrixGetter): - obj_template = mat_struct() + def __init__(self, *args, **kwargs): + super(Mat5StructMatrixGetter, self).__init__(*args, **kwargs) + self.obj_template = mat_struct() + def get_raw_array(self): namelength = self.read_element() # get field names @@ -454,7 +434,10 @@ class Mat5ObjectMatrixGetter(Mat5StructMatrixGetter): - obj_template = mat_obj() + def __init__(self, *args, **kwargs): + super(Mat5StructMatrixGetter, self).__init__(*args, **kwargs) + self.obj_template = mat_obj() + def get_raw_array(self): self.obj_template._classname = self.read_element().tostring() return super(Mat5ObjectMatrixGetter, self).get_raw_array() @@ -541,14 +524,14 @@ def guess_byte_order(self): self.mat_stream.seek(126) - mi = self.read_bytes(2).tostring() + mi = self.mat_stream.read(2) self.mat_stream.seek(0) return mi == 'IM' and '<' or '>' def file_header(self): ''' Read in mat 5 file header ''' hdict = {} - hdr = self.read_array(self.dtypes['file_header']) + hdr = self.read_dtype(self.dtypes['file_header']) hdict['__header__'] = hdr['description'].strip(' \t\n\000') v_major = hdr['version'] >> 8 v_minor = hdr['version'] & 0xFF @@ -558,7 +541,9 @@ def format_looks_right(self): # Mat4 files have a zero somewhere in first 4 bytes self.mat_stream.seek(0) - mopt_bytes = self.read_bytes(4) + mopt_bytes = ndarray(shape=(4,), + dtype=uint8, + buffer = self.mat_stream.read(4)) self.mat_stream.seek(0) return 0 not in mopt_bytes Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-21 09:26:58 UTC (rev 2208) +++ trunk/Lib/io/miobase.py 2006-09-21 16:21:58 UTC (rev 2209) @@ -55,40 +55,22 @@ def __init__(self, mat_stream): self.mat_stream = mat_stream - def read_bytes(self, num_bytes): - ''' Get next block of data of length @num_bytes ''' - res = self.mat_stream.read(num_bytes) - # Allow stream to return strings instead of bytes - if isinstance(res, basestring): - res = ndarray(shape=(len(res)), - dtype=uint8, - buffer=res) - return res - - def read_array(self, a_dtype, a_shape=(), copy=True): - ''' Generic get of byte stream data of known type and shape + def read_dtype(self, a_dtype): + ''' Generic get of byte stream data of known type Inputs @a_dtype - dtype of array - @a_shape - shape of desired array - @copy - copies array if True - (buffer is usually read only) a_dtype is assumed to be correct endianness ''' - num_bytes = a_dtype.itemsize * product(a_shape) - if not num_bytes: - return array([], dtype=a_dtype) - data = self.read_bytes(num_bytes) - arr = ndarray(shape=a_shape, + num_bytes = a_dtype.itemsize + arr = ndarray(shape=(), dtype=a_dtype, - buffer=data, + buffer=self.mat_stream.read(num_bytes), order='F') - if copy: - arr = arr.copy() return arr def read_ztstring(self, num_bytes): - return self.read_bytes(num_bytes).tostring().strip('\x00') + return self.mat_stream.read(num_bytes).strip('\x00') class MatFileReader(MatStreamAgent): @@ -255,7 +237,7 @@ return mdict def end_of_stream(self): - b = self.read_bytes(1) + b = self.mat_stream.read(1) self.mat_stream.seek(-1,1) return len(b) == 0 From scipy-svn at scipy.org Thu Sep 21 18:19:59 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 21 Sep 2006 17:19:59 -0500 (CDT) Subject: [Scipy-svn] r2210 - in trunk/Lib/fftpack: . src Message-ID: <20060921221959.4FF5D39C14E@new.scipy.org> Author: cookedm Date: 2006-09-21 17:19:54 -0500 (Thu, 21 Sep 2006) New Revision: 2210 Modified: trunk/Lib/fftpack/setup.py trunk/Lib/fftpack/src/fftpack.h trunk/Lib/fftpack/src/zfft.c trunk/Lib/fftpack/src/zfftnd.c Log: #268: patch from jtravs for Intel MKL support for fft. Also, replace using fft_opt_info in setup.py with explicitly checking for each of the FFT packages that we support. Modified: trunk/Lib/fftpack/setup.py =================================================================== --- trunk/Lib/fftpack/setup.py 2006-09-21 16:21:58 UTC (rev 2209) +++ trunk/Lib/fftpack/setup.py 2006-09-21 22:19:54 UTC (rev 2210) @@ -8,8 +8,17 @@ from numpy.distutils.misc_util import Configuration from numpy.distutils.system_info import get_info config = Configuration('fftpack',parent_package, top_path) - fft_opt_info = get_info('fft_opt') + djbfft_info = {} + mkl_info = get_info('mkl') + if mkl_info: + mkl_info.setdefault('define_macros', []).append(('SCIPY_MKL_H', None)) + fft_opt_info = mkl_info + else: + fft_opt_info = get_info('fftw3') or get_info('fftw2') \ + or get_info('dfftw') + djbfft_info = get_info('djbfft') + config.add_data_dir('tests') config.add_library('dfftpack', @@ -27,7 +36,7 @@ config.add_extension('convolve', sources = ['convolve.pyf','src/convolve.c'], libraries = ['dfftpack'], - extra_info = fft_opt_info + extra_info = [fft_opt_info, djbfft_info], ) return config Modified: trunk/Lib/fftpack/src/fftpack.h =================================================================== --- trunk/Lib/fftpack/src/fftpack.h 2006-09-21 16:21:58 UTC (rev 2209) +++ trunk/Lib/fftpack/src/fftpack.h 2006-09-21 22:19:54 UTC (rev 2210) @@ -39,6 +39,11 @@ #include #endif +#ifdef SCIPY_MKL_H +#define WITH_MKL +#include +#endif + #ifdef SCIPY_FFTW3_H #define WITH_FFTW3 #include Modified: trunk/Lib/fftpack/src/zfft.c =================================================================== --- trunk/Lib/fftpack/src/zfft.c 2006-09-21 16:21:58 UTC (rev 2209) +++ trunk/Lib/fftpack/src/zfft.c 2006-09-21 22:19:54 UTC (rev 2210) @@ -19,6 +19,7 @@ #endif /**************** DJBFFT *****************************/ +#ifndef WITH_MKL #ifdef WITH_DJBFFT GEN_CACHE(zdjbfft,(int n) ,unsigned int* f; @@ -33,8 +34,20 @@ free(caches_zdjbfft[id].ptr); ,10) #endif +#endif -#if defined WITH_FFTW3 +/**************** INTEL MKL **************************/ +#ifdef WITH_MKL +GEN_CACHE(zmklfft,(int n) + ,DFTI_DESCRIPTOR_HANDLE desc_handle; + ,(caches_zmklfft[i].n==n) + ,DftiCreateDescriptor(&caches_zmklfft[id].desc_handle, DFTI_DOUBLE, DFTI_COMPLEX, 1, (long)n); + DftiCommitDescriptor(caches_zmklfft[id].desc_handle); + ,DftiFreeDescriptor(&caches_zmklfft[id].desc_handle); + ,10) + +/**************** FFTW3 *****************************/ +#elif defined WITH_FFTW3 /* *don't cache anything */ @@ -69,10 +82,14 @@ #ifdef WITH_FFTWORK destroy_zfftwork_caches(); #endif +#ifndef WITH_MKL #ifdef WITH_DJBFFT destroy_zdjbfft_caches(); #endif -#ifdef WITH_FFTW3 +#endif +#ifdef WITH_MKL + destroy_zmklfft_caches(); +#elif defined WITH_FFTW3 #elif defined WITH_FFTW destroy_zfftw_caches(); #else @@ -85,28 +102,36 @@ int n,int direction,int howmany,int normalize) { int i; complex_double *ptr = inout; +#ifndef WITH_MKL #ifdef WITH_FFTW3 fftw_complex *ptrm = NULL; #endif #if defined(WITH_FFTW) || defined(WITH_FFTW3) fftw_plan plan = NULL; +#endif +#endif +#if defined WITH_MKL + DFTI_DESCRIPTOR_HANDLE desc_handle; #else double* wsave = NULL; #endif #ifdef WITH_FFTWORK coef_dbl* coef = NULL; #endif +#ifndef WITH_MKL #ifdef WITH_DJBFFT int j; complex_double *ptrc = NULL; unsigned int *f = NULL; #endif +#endif #ifdef WITH_FFTWORK if (ispow2le2e30(n)) { i = get_cache_id_zfftwork(n); coef = caches_zfftwork[i].coef; } else #endif +#ifndef WITH_MKL #ifdef WITH_DJBFFT switch (n) { case 2:;case 4:;case 8:;case 16:;case 32:;case 64:;case 128:;case 256:; @@ -117,7 +142,10 @@ } if (f==0) #endif -#ifdef WITH_FFTW3 +#endif +#ifdef WITH_MKL + desc_handle = caches_zmklfft[get_cache_id_zmklfft(n)].desc_handle; +#elif defined WITH_FFTW3 #elif defined WITH_FFTW plan = caches_zfftw[get_cache_id_zfftw(n,direction)].plan; #else @@ -133,6 +161,7 @@ fft_for_cplx_flt((cplx_dbl*)ptr,coef,n); } else #endif +#ifndef WITH_MKL #ifdef WITH_DJBFFT if (f!=NULL) { memcpy(ptrc,ptr,2*n*sizeof(double)); @@ -146,7 +175,10 @@ for (j=0;j0?FFTW_FORWARD:FFTW_BACKWARD), FFTW_ESTIMATE); @@ -167,6 +199,7 @@ fft_bak_cplx_flt((cplx_dbl*)ptr,coef,n); } else #endif +#ifndef WITH_MKL #ifdef WITH_DJBFFT if (f!=NULL) { for (j=0;j0?FFTW_FORWARD:FFTW_BACKWARD), FFTW_ESTIMATE); @@ -197,9 +233,10 @@ default: fprintf(stderr,"zfft: invalid direction=%d\n",direction); } - + if (normalize) { ptr = inout; +#ifndef WITH_MKL #ifdef WITH_DJBFFT if (f!=NULL) { for (i=0;i=0;--i) { *((double*)(ptr)) /= n; *((double*)(ptr++)+1) /= n; Modified: trunk/Lib/fftpack/src/zfftnd.c =================================================================== --- trunk/Lib/fftpack/src/zfftnd.c 2006-09-21 16:21:58 UTC (rev 2209) +++ trunk/Lib/fftpack/src/zfftnd.c 2006-09-21 22:19:54 UTC (rev 2210) @@ -5,13 +5,7 @@ */ #include "fftpack.h" -#ifdef WITH_FFTW3 -/* Don't worry about caching for fftw3 - plans take specific arrays and - * keeping around a lot of memory for such a small speed up isn't - * worth it. - */ -#elif defined WITH_FFTW -/**************** FFTW *****************************/ +#if defined(WITH_FFTW) || defined(WITH_MKL) static int equal_dims(int rank,int *dims1,int *dims2) { int i; @@ -20,6 +14,43 @@ return 0; return 1; } +#endif +/**************** INTEL MKL **************************/ +#ifdef WITH_MKL +long* convert_dims(int n, int *dims) +{ + long * ndim; + int i; + ndim = (long*)malloc(sizeof(long)*n); + for(i=0;i=0;--i) { + *((double*)(ptr)) /= sz; + *((double*)(ptr++)+1) /= sz; + } + } +#elif defined WITH_FFTW3 plan = fftw_plan_many_dft(rank,dims,howmany, (fftw_complex*)ptr,NULL,1,sz, (fftw_complex*)ptr,NULL,1,sz, From scipy-svn at scipy.org Thu Sep 21 18:26:50 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 21 Sep 2006 17:26:50 -0500 (CDT) Subject: [Scipy-svn] r2211 - trunk/Lib/fftpack/src Message-ID: <20060921222650.A8B4239C148@new.scipy.org> Author: cookedm Date: 2006-09-21 17:26:48 -0500 (Thu, 21 Sep 2006) New Revision: 2211 Modified: trunk/Lib/fftpack/src/zfft.c Log: #1: patch from David Cournapeau for speeding up fft of complex arrays using fftw3 This copies the array into a cached one that has been allocated with fftw_malloc, and has a plan created for it. Modified: trunk/Lib/fftpack/src/zfft.c =================================================================== --- trunk/Lib/fftpack/src/zfft.c 2006-09-21 22:19:54 UTC (rev 2210) +++ trunk/Lib/fftpack/src/zfft.c 2006-09-21 22:26:48 UTC (rev 2211) @@ -48,9 +48,22 @@ /**************** FFTW3 *****************************/ #elif defined WITH_FFTW3 -/* - *don't cache anything - */ +GEN_CACHE(zfftw,(int n,int d) + ,int direction; + fftw_plan plan; + fftw_complex* ptr; + ,((caches_zfftw[i].n==n) && + (caches_zfftw[i].direction==d)) + ,caches_zfftw[id].direction = d; + caches_zfftw[id].ptr = fftw_malloc(sizeof(fftw_complex)*(n)); + caches_zfftw[id].plan = fftw_plan_dft_1d(n, caches_zfftw[id].ptr, + caches_zfftw[id].ptr, + (d>0?FFTW_FORWARD:FFTW_BACKWARD), + FFTW_ESTIMATE); + ,fftw_destroy_plan(caches_zfftw[id].plan); + fftw_free(caches_zfftw[id].ptr); + ,10) + #elif defined WITH_FFTW /**************** FFTW2 *****************************/ GEN_CACHE(zfftw,(int n,int d) @@ -90,6 +103,7 @@ #ifdef WITH_MKL destroy_zmklfft_caches(); #elif defined WITH_FFTW3 + destroy_zfftw_caches(); #elif defined WITH_FFTW destroy_zfftw_caches(); #else @@ -146,6 +160,7 @@ #ifdef WITH_MKL desc_handle = caches_zmklfft[get_cache_id_zmklfft(n)].desc_handle; #elif defined WITH_FFTW3 + plan = caches_zfftw[get_cache_id_zfftw(n,direction)].plan; #elif defined WITH_FFTW plan = caches_zfftw[get_cache_id_zfftw(n,direction)].plan; #else @@ -179,11 +194,10 @@ #ifdef WITH_MKL DftiComputeForward(desc_handle, (double *)ptr); #elif defined WITH_FFTW3 - plan = fftw_plan_dft_1d(n, (fftw_complex*)ptr, (fftw_complex*)ptr, - (direction>0?FFTW_FORWARD:FFTW_BACKWARD), - FFTW_ESTIMATE); - fftw_execute(plan); - fftw_destroy_plan(plan); + ptrm = caches_zfftw[get_cache_id_zfftw(n,direction)].ptr; + memcpy(ptrm, ptr, sizeof(double)*2*n); + fftw_execute(plan); + memcpy(ptr, ptrm, sizeof(double)*2*n); #elif defined WITH_FFTW fftw_one(plan,(fftw_complex*)ptr,NULL); #else @@ -217,11 +231,10 @@ #ifdef WITH_MKL DftiComputeBackward(desc_handle, (double *)ptr); #elif defined WITH_FFTW3 - plan = fftw_plan_dft_1d(n, (fftw_complex*)ptr, (fftw_complex*)ptr, - (direction>0?FFTW_FORWARD:FFTW_BACKWARD), - FFTW_ESTIMATE); - fftw_execute(plan); - fftw_destroy_plan(plan); + ptrm = caches_zfftw[get_cache_id_zfftw(n,direction)].ptr; + memcpy(ptrm, ptr, sizeof(double)*2*n); + fftw_execute(plan); + memcpy(ptr, ptrm, sizeof(double)*2*n); #elif defined WITH_FFTW fftw_one(plan,(fftw_complex*)ptr,NULL); #else @@ -233,7 +246,7 @@ default: fprintf(stderr,"zfft: invalid direction=%d\n",direction); } - + if (normalize) { ptr = inout; #ifndef WITH_MKL From scipy-svn at scipy.org Fri Sep 22 08:20:34 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 22 Sep 2006 07:20:34 -0500 (CDT) Subject: [Scipy-svn] r2212 - in trunk/Lib/io: . tests Message-ID: <20060922122034.3E44339C08A@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-22 07:20:28 -0500 (Fri, 22 Sep 2006) New Revision: 2212 Modified: trunk/Lib/io/mio4.py trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py trunk/Lib/io/tests/gen_unittests.m trunk/Lib/io/tests/test_mio.py Log: More slight optimizations, bug fix for empty matrices, unit test for empty matrices Modified: trunk/Lib/io/mio4.py =================================================================== --- trunk/Lib/io/mio4.py 2006-09-21 22:26:48 UTC (rev 2211) +++ trunk/Lib/io/mio4.py 2006-09-22 12:20:28 UTC (rev 2212) @@ -52,50 +52,16 @@ 4: 'Cray', #!! } -class Mat4Header(object): - ''' Place holder for Mat4 header - Defines: - next_position - start position of next matrix - name - dims - shape of matrix as stored (see sparse reader) - dtype - numpy dtype of matrix - mclass - matlab (TM) code for class of matrix - is_char - True if these are char data - is_numeric - True if these are numeric data - is_complex - True if data are complex - original_dtype - data type in matlab (TM) workspace - ''' - def __init__(self): - self.next_position = None - self.name = '' - self.dims = () - self.dtype = None - self.mclass = None - self.is_char = None - self.is_numeric = None - self.is_complex = None - self.original_dtype = None - - class Mat4ArrayReader(MatArrayReader): ''' Class for reading Mat4 arrays ''' - def __init__(self, *args, **kwargs): - super(Mat4ArrayReader,self).__init__(*args, **kwargs) - self._getter_classes = { - mxFULL_CLASS: Mat4FullGetter, - mxCHAR_CLASS: Mat4CharGetter, - mxSPARSE_CLASS: Mat4SparseGetter, - } - - def read_header(self): - ''' Read and return Mat4 matrix header - ''' - header = Mat4Header() + def matrix_getter_factory(self): + ''' Read header, return matrix getter ''' data = self.read_dtype(self.dtypes['header']) - header.name = self.read_ztstring(data['namlen']) + header = {} + header['name'] = self.read_ztstring(data['namlen']) if data['mopt'] < 0 or data['mopt'] > 5000: ValueError, 'Mat 4 mopt wrong format, byteswapping problem?' M,rest = divmod(data['mopt'], 1000) @@ -104,21 +70,24 @@ T = rest if O != 0: raise ValueError, 'O in MOPT integer should be 0, wrong format?' - header.dtype = self.dtypes[P] - header.mclass = T - header.dims = (data['mrows'], data['ncols']) - header.is_complex = data['imagf'] == 1 - remaining_bytes = header.dtype.itemsize * product(header.dims) - if header.is_complex and not header.mclass == mxSPARSE_CLASS: + header['dtype'] = self.dtypes[P] + header['mclass'] = T + header['dims'] = (data['mrows'], data['ncols']) + header['is_complex'] = data['imagf'] == 1 + remaining_bytes = header['dtype'].itemsize * product(header['dims']) + if header['is_complex'] and not header['mclass'] == mxSPARSE_CLASS: remaining_bytes *= 2 - header.next_position = self.mat_stream.tell() + remaining_bytes - return header + header['next_position'] = self.mat_stream.tell() + remaining_bytes + if T == mxFULL_CLASS: + return Mat4FullGetter(self, header) + elif T == mxCHAR_CLASS: + return Mat4CharGetter(self, header) + elif T == mxSPARSE_CLASS: + return Mat4SparseGetter(self, header) + else: + raise TypeError, 'No reader for class code %s' % T - def matrix_getter_factory(self): - header = self.read_header() - return self._getter_classes[header.mclass](self, header) - class Mat4MatrixGetter(MatMatrixGetter): # Mat4 variables never global or logical @@ -131,11 +100,12 @@ (buffer is usually read only) a_dtype is assumed to be correct endianness ''' - dt = self.header.dtype + dt = self.header['dtype'] + dims = self.header['dims'] num_bytes = dt.itemsize - for d in self.dims: + for d in dims: num_bytes *= d - arr = ndarray(shape=self.dims, + arr = ndarray(shape=dims, dtype=dt, buffer=self.mat_stream.read(num_bytes), order='F') @@ -145,26 +115,28 @@ class Mat4FullGetter(Mat4MatrixGetter): + def __init__(self, array_reader, header): + super(Mat4FullGetter, self).__init__(array_reader, header) + if header['is_complex']: + self.mat_dtype = dtype(complex128) + else: + self.mat_dtype = dtype(float64) + def get_raw_array(self): - self.header.is_numeric = True - if self.header.is_complex: - self.header.original_dtype = dtype(complex128) + if self.header['is_complex']: # avoid array copy to save memory res = self.read_array(copy=False) res_j = self.read_array(copy=False) return res + (res_j * 1j) - else: - self.header.original_dtype = dtype(float64) - return self.read_array() + return self.read_array() class Mat4CharGetter(Mat4MatrixGetter): def get_raw_array(self): - self.header.is_char = True arr = self.read_array().astype(uint8) # ascii to unicode S = arr.tostring().decode('ascii') - return ndarray(shape=self.dims, + return ndarray(shape=self.header['dims'], dtype=dtype('U1'), buffer = array(S)).copy() @@ -187,14 +159,12 @@ is only detectable because there are 4 storage columns ''' def get_raw_array(self): - self.header.original_dtype = dtype(float64) res = self.read_array() tmp = res[:-1,:] dims = res[-1,0:2] ij = transpose(tmp[:,0:2]) - 1 # for 1-based indexing vals = tmp[:,2] if res.shape[1] == 4: - self.header.is_complex = True vals = vals + res[:-1,3] * 1j if have_sparse: return scipy.sparse.csc_matrix((vals,ij), dims) Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-09-21 22:26:48 UTC (rev 2211) +++ trunk/Lib/io/mio5.py 2006-09-22 12:20:28 UTC (rev 2212) @@ -132,35 +132,6 @@ ''' Placeholder for holding read data from objects ''' pass -class Mat5Header(object): - ''' Placeholder for Mat5 header - - Defines: - next_position - start position of next matrix - name - dtype - numpy dtype of matrix - mclass - matlab (TM) code for class of matrix - dims - shape of matrix as stored (see sparse reader) - is_complex - True if data are complex - is_char - True if these are char data - is_global - is a global variable in matlab (TM) workspace - is_numeric - is basic numeric matrix - original_dtype - data type when saved from matlab (TM) - ''' - def __init__(self): - self.next_position = None - self.is_empty = False - self.is_complex = False - self.is_global = False - self.is_logical = False - self.mclass = 0 - self.is_numeric = None - self.original_dtype = None - self.is_char = None - self.dims = () - self.name = '' - - class Mat5ArrayReader(MatArrayReader): ''' Class to get Mat5 arrays @@ -210,26 +181,10 @@ if copy: el = el.copy() mod8 = byte_count % 8 - skip = mod8 and 8 - mod8 - if skip: - self.mat_stream.seek(skip, 1) + if mod8: + self.mat_stream.seek(8 - mod8, 1) return el - def read_header(self): - ''' Read header from Mat5 matrix - ''' - header = Mat5Header() - af = self.read_dtype(self.dtypes['array_flags']) - flags_class = af['flags_class'] - header.mclass = flags_class & 0xFF - header.is_logical = flags_class >> 9 & 1 - header.is_global = flags_class >> 10 & 1 - header.is_complex = flags_class >> 11 & 1 - header.nzmax = af['nzmax'] - header.dims = self.read_element() - header.name = self.read_element().tostring() - return header - def matrix_getter_factory(self): ''' Returns reader for next matrix ''' tag = self.read_dtype(self.dtypes['tag_full']) @@ -243,13 +198,22 @@ return self.getter_from_bytes(byte_count) def getter_from_bytes(self, byte_count): + ''' Return matrix getter for current stream position ''' # Apparently an empty miMATRIX can contain no bytes if not byte_count: - return Mat5EmptyMatrixGetter(self, header) - next_pos = self.mat_stream.tell() + byte_count - header = self.read_header() - header.next_position = next_pos - mc = header.mclass + return Mat5EmptyMatrixGetter(self) + af = self.read_dtype(self.dtypes['array_flags']) + header = {} + flags_class = af['flags_class'] + header['next_position'] = self.mat_stream.tell() + byte_count + mc = flags_class & 0xFF + header['mclass'] = mc + header['is_logical'] = flags_class >> 9 & 1 + header['is_global'] = flags_class >> 10 & 1 + header['is_complex'] = flags_class >> 11 & 1 + header['nzmax'] = af['nzmax'] + header['dims'] = self.read_element() + header['name'] = self.read_element().tostring() if mc in mx_numbers: return Mat5NumericMatrixGetter(self, header) if mc == mxSPARSE_CLASS: @@ -292,7 +256,7 @@ in the main stream, not the compressed stream. ''' getter = super(Mat5ZArrayReader, self).getter_from_bytes(byte_count) - getter.next_position = self._next_position + getter.header['next_position'] = self._next_position return getter @@ -303,34 +267,49 @@ ''' def __init__(self, array_reader, header): - ''' Accepts @array_reader and @header ''' super(Mat5MatrixGetter, self).__init__(array_reader, header) self.class_dtypes = array_reader.class_dtypes self.codecs = array_reader.codecs - self.is_global = header.is_global + self.is_global = header['is_global'] + self.mat_dtype = None def read_element(self, *args, **kwargs): return self.array_reader.read_element(*args, **kwargs) class Mat5EmptyMatrixGetter(Mat5MatrixGetter): - ''' Dummy class to return empty array for empty matrix ''' + ''' Dummy class to return empty array for empty matrix + ''' + def __init__(self, array_reader): + self.array_reader = array_reader + self.mat_stream = array_reader.mat_stream + self.data_position = self.mat_stream.tell() + self.header = {} + self.is_global = False + self.mat_dtype = 'f8' + def get_raw_array(self): return array([[]]) class Mat5NumericMatrixGetter(Mat5MatrixGetter): + + def __init__(self, array_reader, header): + super(Mat5NumericMatrixGetter, self).__init__(array_reader, header) + if header['is_logical']: + self.mat_dtype = dtype('bool') + else: + self.mat_dtype = self.class_dtypes[header['mclass']] + def get_raw_array(self): - self.header.is_numeric = True - self.header.original_dtype = self.class_dtypes[self.header.mclass] - if self.header.is_complex: + if self.header['is_complex']: # avoid array copy to save memory res = self.read_element(copy=False) res_j = self.read_element(copy=False) res = res + (res_j * 1j) else: res = self.read_element() - return ndarray(shape=self.dims, + return ndarray(shape=self.header['dims'], dtype=res.dtype, buffer=res, order='F') @@ -340,7 +319,7 @@ def get_raw_array(self): rowind = self.read_element() colind = self.read_element() - if self.header.is_complex: + if self.header['is_complex']: # avoid array copy to save memory res = self.read_element(copy=False) res_j = self.read_element(copy=False) @@ -369,7 +348,7 @@ ij = vstack((rowind[:len(res)], cols)) if have_sparse: result = scipy.sparse.csc_matrix((res,ij), - self.dims) + self.header['dims']) else: result = (dims, ij, res) return result @@ -377,7 +356,6 @@ class Mat5CharMatrixGetter(Mat5MatrixGetter): def get_raw_array(self): - self.header.is_char = True res = self.read_element() # Convert non-string types to unicode if isinstance(res, ndarray): @@ -390,7 +368,7 @@ else: raise TypeError, 'Did not expect type %s' % res.dtype res = res.tostring().decode(codec) - return ndarray(shape=self.dims, + return ndarray(shape=self.header['dims'], dtype=dtype('U1'), buffer=array(res), order='F').copy() @@ -399,8 +377,8 @@ class Mat5CellMatrixGetter(Mat5MatrixGetter): def get_raw_array(self): # Account for fortran indexing of cells - tupdims = tuple(self.dims[::-1]) - length = product(self.dims) + tupdims = tuple(self.header['dims'][::-1]) + length = product(tupdims) result = empty(length, dtype=object) for i in range(length): result[i] = self.get_item() Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-21 22:26:48 UTC (rev 2211) +++ trunk/Lib/io/miobase.py 2006-09-22 12:20:28 UTC (rev 2212) @@ -15,6 +15,13 @@ have_sparse = 0 +def small_product(arr): + ''' Faster than product for small arrays ''' + res = 1 + for e in arr: + res *= e + return res + class ByteOrder(object): ''' Namespace for byte ordering ''' little_endian = sys.byteorder == 'little' @@ -170,17 +177,26 @@ occur as submatrices - in cell arrays, structs and objects - so we will not see these in the main variable getting routine here. + + The read array is the first argument. + The getter, passed as second argument to the function, must + define properties, iff matlab_compatible option is True: + + mat_dtype - data type when loaded into matlab (tm) + (None for no conversion) + + func returns the processed array ''' - def func(arr, header): - if header.is_char and self.chars_as_strings: + def func(arr, getter): + if arr.dtype.kind == 'U' and self.chars_as_strings: # Convert char array to string or array of strings dims = arr.shape if len(dims) >= 2: # return array of strings dtt = self.order_code + 'U' n_dims = dims[:-1] str_arr = reshape(arr, - (product(n_dims), + (small_product(n_dims), dims[-1])) arr = empty(n_dims, dtype=object) for i in range(0, n_dims[-1]): @@ -190,22 +206,20 @@ if self.matlab_compatible: # Apply options to replicate matlab's (TM) # load into workspace - if header.is_logical: - arr = arr.astype(bool) - elif header.is_numeric: - # Cast as original matlab (TM) type - if header.original_dtype: - arr = arr.astype(header.original_dtype) + if getter.mat_dtype: + arr = arr.astype(getter.mat_dtype) if self.squeeze_me: arr = squeeze(arr) - if not arr.shape: # 0d coverted to scalar + if not arr.size: + arr = array([]) + elif not arr.shape: # 0d coverted to scalar arr = arr.item() return arr return func def chars_to_str(self, str_arr): ''' Convert string array to string ''' - dt = dtype('U' + str(product(str_arr.shape))) + dt = dtype('U' + str(small_product(str_arr.shape))) return ndarray(shape=(), dtype = dt, buffer = str_arr.copy()).item() @@ -254,7 +268,7 @@ Accepts @array_reader - array reading object (see below) - @header - header for matrix being read + @header - header dictionary for matrix being read """ def __init__(self, array_reader, header): @@ -262,9 +276,7 @@ self.array_reader = array_reader self.dtypes = array_reader.dtypes self.header = header - self.name = header.name - self.next_position = header.next_position - self.dims = header.dims + self.name = header['name'] self.data_position = self.mat_stream.tell() def get_array(self): @@ -272,13 +284,13 @@ if not self.mat_stream.tell() == self.data_position: self.mat_stream.seek(self.data_position) arr = self.get_raw_array() - return self.array_reader.processor_func(arr, self.header) + return self.array_reader.processor_func(arr, self) def get_raw_array(self): assert False, 'Not implemented' def to_next(self): - self.mat_stream.seek(self.next_position) + self.mat_stream.seek(self.header['next_position']) class MatArrayReader(MatStreamAgent): Modified: trunk/Lib/io/tests/gen_unittests.m =================================================================== --- trunk/Lib/io/tests/gen_unittests.m 2006-09-21 22:26:48 UTC (rev 2211) +++ trunk/Lib/io/tests/gen_unittests.m 2006-09-22 12:20:28 UTC (rev 2212) @@ -47,6 +47,7 @@ % Two variables in same file save([FILEPREFIX 'testmulti' FILESUFFIX], 'a', 'theta') + % struct save_test('teststruct', ... struct('stringfield','Rats live on no evil star.',... @@ -58,6 +59,9 @@ {['This cell contains this string and 3 arrays of increasing' ... ' length'], 1., 1.:2., 1.:3.}); +% Empty cells in two cell matrices +save_test('testemptycell', {1, 2, [], [], 3}); + % 3D matrix save_test('test3dmatrix', reshape(1:24,[2 3 4])) Modified: trunk/Lib/io/tests/test_mio.py =================================================================== --- trunk/Lib/io/tests/test_mio.py 2006-09-21 22:26:48 UTC (rev 2211) +++ trunk/Lib/io/tests/test_mio.py 2006-09-22 12:20:28 UTC (rev 2212) @@ -151,6 +151,12 @@ dtype=object)} }] case_table5.append( + {'name': 'emptycell', + 'expected': {'testemptycell': + array([array(1), array(2), array([]), + array([]), array(3)], dtype=object)} + }) + case_table5.append( {'name': 'stringarray', 'expected': {'teststringarray': array( [u'one ', u'two ', u'three'], dtype=object)}, From scipy-svn at scipy.org Fri Sep 22 09:00:27 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 22 Sep 2006 08:00:27 -0500 (CDT) Subject: [Scipy-svn] r2213 - trunk/Lib/io/tests/data Message-ID: <20060922130027.BCC3A39C08F@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-22 08:00:18 -0500 (Fri, 22 Sep 2006) New Revision: 2213 Added: trunk/Lib/io/tests/data/testemptycell_5.3_SOL2.mat trunk/Lib/io/tests/data/testemptycell_6.5.1_GLNX86.mat trunk/Lib/io/tests/data/testemptycell_7.1_GLNX86.mat Log: Added empty cell test files Added: trunk/Lib/io/tests/data/testemptycell_5.3_SOL2.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testemptycell_5.3_SOL2.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testemptycell_6.5.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testemptycell_6.5.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/Lib/io/tests/data/testemptycell_7.1_GLNX86.mat =================================================================== (Binary files differ) Property changes on: trunk/Lib/io/tests/data/testemptycell_7.1_GLNX86.mat ___________________________________________________________________ Name: svn:mime-type + application/octet-stream From scipy-svn at scipy.org Fri Sep 22 09:03:31 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 22 Sep 2006 08:03:31 -0500 (CDT) Subject: [Scipy-svn] r2214 - trunk/Lib/io Message-ID: <20060922130331.76DD339C08F@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-22 08:03:22 -0500 (Fri, 22 Sep 2006) New Revision: 2214 Modified: trunk/Lib/io/mio4.py trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py Log: Removed some more unnecessary file oprations to improved speed Modified: trunk/Lib/io/mio4.py =================================================================== --- trunk/Lib/io/mio4.py 2006-09-22 13:00:18 UTC (rev 2213) +++ trunk/Lib/io/mio4.py 2006-09-22 13:03:22 UTC (rev 2214) @@ -77,15 +77,17 @@ remaining_bytes = header['dtype'].itemsize * product(header['dims']) if header['is_complex'] and not header['mclass'] == mxSPARSE_CLASS: remaining_bytes *= 2 - header['next_position'] = self.mat_stream.tell() + remaining_bytes + next_pos = self.mat_stream.tell() + remaining_bytes if T == mxFULL_CLASS: - return Mat4FullGetter(self, header) + getter = Mat4FullGetter(self, header) elif T == mxCHAR_CLASS: - return Mat4CharGetter(self, header) + getter = Mat4CharGetter(self, header) elif T == mxSPARSE_CLASS: - return Mat4SparseGetter(self, header) + getter = Mat4SparseGetter(self, header) else: raise TypeError, 'No reader for class code %s' % T + getter.next_position = next_pos + return getter class Mat4MatrixGetter(MatMatrixGetter): Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-09-22 13:00:18 UTC (rev 2213) +++ trunk/Lib/io/mio5.py 2006-09-22 13:03:22 UTC (rev 2214) @@ -165,7 +165,7 @@ buffer=raw_tag[4:]) byte_count = tag['byte_count'] if mdtype == miMATRIX: - return self.getter_from_bytes(byte_count).get_array() + return self.current_getter().get_array() if mdtype in self.codecs: # encoded char data raw_str = self.mat_stream.read(byte_count) codec = self.codecs[mdtype] @@ -186,26 +186,31 @@ return el def matrix_getter_factory(self): - ''' Returns reader for next matrix ''' + ''' Returns reader for next matrix at top level ''' tag = self.read_dtype(self.dtypes['tag_full']) mdtype = tag['mdtype'] byte_count = tag['byte_count'] + next_pos = self.mat_stream.tell() + byte_count if mdtype == miCOMPRESSED: - return Mat5ZArrayReader(self, byte_count).matrix_getter_factory() - if not mdtype == miMATRIX: + getter = Mat5ZArrayReader(self, byte_count).matrix_getter_factory() + elif not mdtype == miMATRIX: raise TypeError, \ 'Expecting miMATRIX type here, got %d' % mdtype - return self.getter_from_bytes(byte_count) + elif not byte_count: # an empty miMATRIX can contain no bytes + getter = Mat5EmptyMatrixGetter(self) + else: + getter = self.current_getter() + getter.next_position = next_pos + return getter + + def current_getter(self): + ''' Return matrix getter for current stream position - def getter_from_bytes(self, byte_count): - ''' Return matrix getter for current stream position ''' - # Apparently an empty miMATRIX can contain no bytes - if not byte_count: - return Mat5EmptyMatrixGetter(self) + Returns matrix getters at top level and sub levels + ''' af = self.read_dtype(self.dtypes['array_flags']) header = {} flags_class = af['flags_class'] - header['next_position'] = self.mat_stream.tell() + byte_count mc = flags_class & 0xFF header['mclass'] = mc header['is_logical'] = flags_class >> 9 & 1 @@ -233,9 +238,7 @@ ''' Getter for compressed arrays Reads and uncompresses gzipped stream on init, providing wrapper - for this new sub-stream. Sets next_position for main stream to - allow skipping over this variable (although we have to read and - uncompress the whole thing anyway to get the name) + for this new sub-stream. ''' def __init__(self, array_reader, byte_count): '''Reads and uncompresses gzipped stream''' @@ -246,19 +249,7 @@ array_reader.processor_func, array_reader.codecs, array_reader.class_dtypes) - self._next_position = array_reader.mat_stream.tell() - def getter_from_bytes(self, byte_count): - ''' Set next_position to current position in parent stream - - self.next_position is only used by the get_variables routine - of the main file reading loop, so must refer to the position - in the main stream, not the compressed stream. - ''' - getter = super(Mat5ZArrayReader, self).getter_from_bytes(byte_count) - getter.header['next_position'] = self._next_position - return getter - class Mat5MatrixGetter(MatMatrixGetter): ''' Base class for getting Mat5 matrices Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-22 13:00:18 UTC (rev 2213) +++ trunk/Lib/io/miobase.py 2006-09-22 13:03:22 UTC (rev 2214) @@ -277,12 +277,9 @@ self.dtypes = array_reader.dtypes self.header = header self.name = header['name'] - self.data_position = self.mat_stream.tell() def get_array(self): ''' Gets an array from matrix, and applies any necessary processing ''' - if not self.mat_stream.tell() == self.data_position: - self.mat_stream.seek(self.data_position) arr = self.get_raw_array() return self.array_reader.processor_func(arr, self) @@ -290,7 +287,7 @@ assert False, 'Not implemented' def to_next(self): - self.mat_stream.seek(self.header['next_position']) + self.mat_stream.seek(self.next_position) class MatArrayReader(MatStreamAgent): From scipy-svn at scipy.org Fri Sep 22 10:18:31 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 22 Sep 2006 09:18:31 -0500 (CDT) Subject: [Scipy-svn] r2215 - trunk/Lib/io Message-ID: <20060922141831.9BD8B39C1ED@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-22 09:18:28 -0500 (Fri, 22 Sep 2006) New Revision: 2215 Modified: trunk/Lib/io/mio5.py Log: And refix for empty matrices, when compressed Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-09-22 13:03:22 UTC (rev 2214) +++ trunk/Lib/io/mio5.py 2006-09-22 14:18:28 UTC (rev 2215) @@ -162,10 +162,10 @@ el_count = byte_count / dt.itemsize return ndarray(shape=(el_count,), dtype=dt, - buffer=raw_tag[4:]) + buffer=raw_tag[4:4+byte_count]) byte_count = tag['byte_count'] if mdtype == miMATRIX: - return self.current_getter().get_array() + return self.current_getter(byte_count).get_array() if mdtype in self.codecs: # encoded char data raw_str = self.mat_stream.read(byte_count) codec = self.codecs[mdtype] @@ -196,18 +196,18 @@ elif not mdtype == miMATRIX: raise TypeError, \ 'Expecting miMATRIX type here, got %d' % mdtype - elif not byte_count: # an empty miMATRIX can contain no bytes - getter = Mat5EmptyMatrixGetter(self) else: - getter = self.current_getter() + getter = self.current_getter(byte_count) getter.next_position = next_pos return getter - def current_getter(self): + def current_getter(self, byte_count): ''' Return matrix getter for current stream position Returns matrix getters at top level and sub levels ''' + if not byte_count: # an empty miMATRIX can contain no bytes + return Mat5EmptyMatrixGetter(self) af = self.read_dtype(self.dtypes['array_flags']) header = {} flags_class = af['flags_class'] From scipy-svn at scipy.org Fri Sep 22 14:44:36 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 22 Sep 2006 13:44:36 -0500 (CDT) Subject: [Scipy-svn] r2216 - trunk/Lib/io Message-ID: <20060922184436.C6FC739C0BE@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-22 13:44:31 -0500 (Fri, 22 Sep 2006) New Revision: 2216 Modified: trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py Log: Tiny fix for indexing error with some numpy versions Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-09-22 14:18:28 UTC (rev 2215) +++ trunk/Lib/io/mio5.py 2006-09-22 18:44:31 UTC (rev 2216) @@ -162,7 +162,7 @@ el_count = byte_count / dt.itemsize return ndarray(shape=(el_count,), dtype=dt, - buffer=raw_tag[4:4+byte_count]) + buffer=raw_tag[4:]) byte_count = tag['byte_count'] if mdtype == miMATRIX: return self.current_getter(byte_count).get_array() Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-22 14:18:28 UTC (rev 2215) +++ trunk/Lib/io/miobase.py 2006-09-22 18:44:31 UTC (rev 2216) @@ -120,7 +120,6 @@ self.squeeze_me = squeeze_me self.chars_as_strings = chars_as_strings self.matlab_compatible = matlab_compatible - self.processor_func = self.get_processor_func() # matlab_compatible property sets squeeze_me and chars_as_strings def get_matlab_compatible(self): @@ -130,6 +129,7 @@ if matlab_compatible: self.squeeze_me = False self.char_as_strings = False + self.processor_func = self.get_processor_func() matlab_compatible = property(get_matlab_compatible, set_matlab_compatible, None, From scipy-svn at scipy.org Fri Sep 22 15:48:50 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 22 Sep 2006 14:48:50 -0500 (CDT) Subject: [Scipy-svn] r2217 - trunk/Lib/stats Message-ID: <20060922194850.95FDF39C04C@new.scipy.org> Author: oliphant Date: 2006-09-22 14:48:40 -0500 (Fri, 22 Sep 2006) New Revision: 2217 Modified: trunk/Lib/stats/stats.py Log: Eliminate spurious lines. Modified: trunk/Lib/stats/stats.py =================================================================== --- trunk/Lib/stats/stats.py 2006-09-22 18:44:31 UTC (rev 2216) +++ trunk/Lib/stats/stats.py 2006-09-22 19:48:40 UTC (rev 2217) @@ -256,8 +256,6 @@ Norig = x.shape[axis] factor = 1.0-sum(isnan(x),axis)*1.0/Norig - # XXX: this line is quite clearly wrong - n = N-sum(isnan(x),axis) putmask(x,isnan(x),0) return stats.mean(x,axis)/factor @@ -270,8 +268,6 @@ n = Norig - sum(isnan(x),axis)*1.0 factor = n/Norig - # XXX: this line is quite clearly wrong - n = N-sum(isnan(x),axis) putmask(x,isnan(x),0) m1 = stats.mean(x,axis) m1c = m1/factor From scipy-svn at scipy.org Sat Sep 23 07:47:50 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 23 Sep 2006 06:47:50 -0500 (CDT) Subject: [Scipy-svn] r2218 - trunk/Lib/io Message-ID: <20060923114750.EAD4D39C049@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-23 06:47:47 -0500 (Sat, 23 Sep 2006) New Revision: 2218 Modified: trunk/Lib/io/miobase.py Log: Fixes to hidden features of individual variable loading, matlab_compatible loading Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-22 19:48:40 UTC (rev 2217) +++ trunk/Lib/io/miobase.py 2006-09-23 11:47:47 UTC (rev 2218) @@ -117,24 +117,44 @@ byte_order = self.guess_byte_order() self.order_code = byte_order # sets dtypes and other things too self.base_name = base_name - self.squeeze_me = squeeze_me - self.chars_as_strings = chars_as_strings + self._squeeze_me = squeeze_me + self._chars_as_strings = chars_as_strings self.matlab_compatible = matlab_compatible # matlab_compatible property sets squeeze_me and chars_as_strings def get_matlab_compatible(self): return self._matlab_compatible - def set_matlab_compatible(self, matlab_compatible): - self._matlab_compatible = matlab_compatible - if matlab_compatible: - self.squeeze_me = False - self.char_as_strings = False + def set_matlab_compatible(self, m_l_c): + self._matlab_compatible = m_l_c + if m_l_c: + self._squeeze_me = False + self._chars_as_strings = False self.processor_func = self.get_processor_func() matlab_compatible = property(get_matlab_compatible, set_matlab_compatible, None, 'get/set matlab_compatible property') + def get_squeeze_me(self): + return self._squeeze_me + def set_squeeze_me(self, squeeze_me): + self._squeeze_me = squeeze_me + self.processor_func = self.get_processor_func() + squeeze_me = property(get_squeeze_me, + set_squeeze_me, + None, + 'get/set squeeze me property') + + def get_chars_as_strings(self): + return self._chars_as_strings + def set_chars_as_strings(self, chars_as_strings): + self._chars_as_strings = chars_as_strings + self.processor_func = self.get_processor_func() + chars_as_strings = property(get_chars_as_strings, + set_chars_as_strings, + None, + 'get/set squeeze me property') + def get_order_code(self): return self._order_code def set_order_code(self, order_code): @@ -206,7 +226,7 @@ if self.matlab_compatible: # Apply options to replicate matlab's (TM) # load into workspace - if getter.mat_dtype: + if getter.mat_dtype is not None: arr = arr.astype(getter.mat_dtype) if self.squeeze_me: arr = squeeze(arr) @@ -231,6 +251,8 @@ If variable_names is None, then get all variables in file ''' + if isinstance(variable_names, basestring): + variable_names = [variable_names] self.mat_stream.seek(0) mdict = self.file_header() mdict['__globals__'] = [] From scipy-svn at scipy.org Sat Sep 23 17:56:54 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 23 Sep 2006 16:56:54 -0500 (CDT) Subject: [Scipy-svn] r2219 - in trunk/Lib: integrate interpolate optimize Message-ID: <20060923215654.6D7A339C03E@new.scipy.org> Author: rkern Date: 2006-09-23 16:56:21 -0500 (Sat, 23 Sep 2006) New Revision: 2219 Modified: trunk/Lib/integrate/ode.py trunk/Lib/integrate/odepack.py trunk/Lib/integrate/quadpack.py trunk/Lib/integrate/quadrature.py trunk/Lib/interpolate/fitpack.py trunk/Lib/interpolate/fitpack2.py trunk/Lib/interpolate/info.py trunk/Lib/optimize/anneal.py trunk/Lib/optimize/cobyla.py trunk/Lib/optimize/lbfgsb.py trunk/Lib/optimize/minpack.py trunk/Lib/optimize/optimize.py trunk/Lib/optimize/tnc.py trunk/Lib/optimize/zeros.py Log: Add cross-reference information to docstrings. Modified: trunk/Lib/integrate/ode.py =================================================================== --- trunk/Lib/integrate/ode.py 2006-09-23 11:47:47 UTC (rev 2218) +++ trunk/Lib/integrate/ode.py 2006-09-23 21:56:21 UTC (rev 2219) @@ -118,6 +118,10 @@ return def jac(t,y[,arg1,..]): return + +See also: + odeint - an integrator with a simpler interface based on lsoda from ODEPACK + quad - for finding the area under a curve """ def __init__(self,f,jac=None): Modified: trunk/Lib/integrate/odepack.py =================================================================== --- trunk/Lib/integrate/odepack.py 2006-09-23 11:47:47 UTC (rev 2218) +++ trunk/Lib/integrate/odepack.py 2006-09-23 21:56:21 UTC (rev 2219) @@ -108,6 +108,9 @@ mxordn -- maximum order to be allowed for the nonstiff (Adams) method. mxords -- maximum order to be allowed for the stiff (BDF) method. + See also: + ode - a more object-oriented integrator based on VODE + quad - for finding the area under a curve """ if ml is None: Modified: trunk/Lib/integrate/quadpack.py =================================================================== --- trunk/Lib/integrate/quadpack.py 2006-09-23 11:47:47 UTC (rev 2218) +++ trunk/Lib/integrate/quadpack.py 2006-09-23 21:56:21 UTC (rev 2219) @@ -174,6 +174,13 @@ wopts -- Optional input for reusing Chebyshev moments. maxp1 -- An upper bound on the number of Chebyshev moments. + See also: + dblquad, tplquad - double and triple integrals + fixed_quad - fixed-order Gaussian quadrature + quadrature - adaptive Gaussian quadrature + odeint, ode - ODE integrators + simps, trapz, romb - integrators for sampled data + scipy.special - for coefficients and roots of orthogonal polynomials """ if type(args) != type(()): args = (args,) if (weight is None): @@ -332,6 +339,14 @@ y -- the resultant integral. abserr -- an estimate of the error. + See also: + quad - single integral + tplquad - triple integral + fixed_quad - fixed-order Gaussian quadrature + quadrature - adaptive Gaussian quadrature + odeint, ode - ODE integrators + simps, trapz, romb - integrators for sampled data + scipy.special - for coefficients and roots of orthogonal polynomials """ return quad(_infunc,a,b,(func,gfun,hfun,args),epsabs=epsabs,epsrel=epsrel) @@ -372,5 +387,13 @@ y -- the resultant integral. abserr -- an estimate of the error. + See also: + quad - single integral + dblquad - double integral + fixed_quad - fixed-order Gaussian quadrature + quadrature - adaptive Gaussian quadrature + odeint, ode - ODE integrators + simps, trapz, romb - integrators for sampled data + scipy.special - for coefficients and roots of orthogonal polynomials """ return dblquad(_infunc2,a,b,gfun,hfun,(func,qfun,rfun,args),epsabs=epsabs,epsrel=epsrel) Modified: trunk/Lib/integrate/quadrature.py =================================================================== --- trunk/Lib/integrate/quadrature.py 2006-09-23 11:47:47 UTC (rev 2218) +++ trunk/Lib/integrate/quadrature.py 2006-09-23 21:56:21 UTC (rev 2219) @@ -29,11 +29,19 @@ val -- Gaussian quadrature approximation to the integral. + See also: + + quad - adaptive quadrature using QUADPACK + dblquad, tplquad - double and triple integrals + romberg - adaptive Romberg quadrature + quadrature - adaptive Gaussian quadrature + romb, simps, trapz - integrators for sampled data + cumtrapz - cumulative integration for sampled data + ode, odeint - ODE integrators """ [x,w] = p_roots(n) x = real(x) - ainf, binf = map(isinf,(a,b)) - if ainf or binf: + ainf, binf = map(isinf,(a,b)) if ainf or binf: raise ValueError, "Gaussian quadrature is only available for " \ "finite limits." y = (b-a)*(x+1)/2.0 + a @@ -83,6 +91,15 @@ val -- Gaussian quadrature approximation (within tolerance) to integral. err -- Difference between last two estimates of the integral. + See also: + + romberg - adaptive Romberg quadrature + fixed_quad - fixed-order Gaussian quadrature + quad - adaptive quadrature using QUADPACK + dblquad, tplquad - double and triple integrals + romb, simps, trapz - integrators for sampled data + cumtrapz - cumulative integration for sampled data + ode, odeint - ODE integrators """ err = 100.0 val = err @@ -108,6 +125,17 @@ """Cumulatively integrate y(x) using samples along the given axis and the composite trapezoidal rule. If x is None, spacing given by dx is assumed. + + See also: + + quad - adaptive quadrature using QUADPACK + romberg - adaptive Romberg quadrature + quadrature - adaptive Gaussian quadrature + fixed_quad - fixed-order Gaussian quadrature + dblquad, tplquad - double and triple integrals + romb, trapz - integrators for sampled data + cumtrapz - cumulative integration for sampled data + ode, odeint - ODE integrators """ y = asarray(y) if x is None: @@ -172,6 +200,17 @@ exact if the function is a polynomial of order 3 or less. If the samples are not equally spaced, then the result is exact only if the function is a polynomial of order 2 or less. + + See also: + + quad - adaptive quadrature using QUADPACK + romberg - adaptive Romberg quadrature + quadrature - adaptive Gaussian quadrature + fixed_quad - fixed-order Gaussian quadrature + dblquad, tplquad - double and triple integrals + romb, trapz - integrators for sampled data + cumtrapz - cumulative integration for sampled data + ode, odeint - ODE integrators """ y = asarray(y) nd = len(y.shape) @@ -231,6 +270,17 @@ """Uses Romberg integration to integrate y(x) using N samples along the given axis which are assumed equally spaced with distance dx. The number of samples must be 1 + a non-negative power of two: N=2**k + 1 + + See also: + + quad - adaptive quadrature using QUADPACK + romberg - adaptive Romberg quadrature + quadrature - adaptive Gaussian quadrature + fixed_quad - fixed-order Gaussian quadrature + dblquad, tplquad - double and triple integrals + simps, trapz - integrators for sampled data + cumtrapz - cumulative integration for sampled data + ode, odeint - ODE integrators """ y = asarray(y) nd = len(y.shape) @@ -361,6 +411,16 @@ the triangular array of the intermediate results will be printed. If |vec_func| is True (default is False), then |function| is assumed to support vector arguments. + + See also: + + quad - adaptive quadrature using QUADPACK + quadrature - adaptive Gaussian quadrature + fixed_quad - fixed-order Gaussian quadrature + dblquad, tplquad - double and triple integrals + romb, simps, trapz - integrators for sampled data + cumtrapz - cumulative integration for sampled data + ode, odeint - ODE integrators """ if isinf(a) or isinf(b): raise ValueError("Romberg integration only available for finite limits.") Modified: trunk/Lib/interpolate/fitpack.py =================================================================== --- trunk/Lib/interpolate/fitpack.py 2006-09-23 11:47:47 UTC (rev 2218) +++ trunk/Lib/interpolate/fitpack.py 2006-09-23 21:56:21 UTC (rev 2219) @@ -1,8 +1,8 @@ #!/usr/bin/env python """ fitpack (dierckx in netlib) --- A Python-C wrapper to FITPACK (by P. Dierckx). - FITPACK is a collection of FORTRAN programs for CURVE and SURFACE - FITTING with SPLINES and TENSOR PRODUCT SPLINES. + FITPACK is a collection of FORTRAN programs for curve and surface + fitting with splines and tensor product splines. See http://www.cs.kuleuven.ac.be/cwis/research/nalag/research/topics/fitpack.html @@ -28,8 +28,8 @@ For bivariate splines: profil, regrid, parsur, surev """ -__all__ = ['splrep', 'splprep', 'splev', 'splint', 'sproot', - 'spalde', 'bisplrep', 'bisplev'] +__all__ = ['splrep', 'splprep', 'splev', 'splint', 'sproot', 'spalde', + 'bisplrep', 'bisplev'] __version__ = "$Revision$"[10:-1] import _fitpack from numpy import atleast_1d, array, ones, zeros, sqrt, ravel, transpose, \ @@ -166,6 +166,12 @@ Remarks: SEE splev for evaluation of the spline and its derivatives. + + See also: + splrep, splev, sproot, spalde, splint - evaluation, roots, integral + bisplrep, bisplev - bivariate splines + UnivariateSpline, BivariateSpline - an alternative wrapping + of the FITPACK functions """ if task<=0: _parcur_cache = {'t': array([],float), 'wrk': array([],float), @@ -319,6 +325,11 @@ y2 = splev(x2, tck) plot(x, y, 'o', x2, y2) + See also: + splprep, splev, sproot, spalde, splint - evaluation, roots, integral + bisplrep, bisplev - bivariate splines + UnivariateSpline, BivariateSpline - an alternative wrapping + of the FITPACK functions """ if task<=0: _curfit_cache = {} @@ -414,6 +425,12 @@ y -- an array of values representing the spline function or curve. If tck was returned from splrep, then this is a list of arrays representing the curve in N-dimensional space. + + See also: + splprep, splrep, sproot, spalde, splint - evaluation, roots, integral + bisplrep, bisplev - bivariate splines + UnivariateSpline, BivariateSpline - an alternative wrapping + of the FITPACK functions """ t,c,k=tck try: @@ -449,6 +466,12 @@ wrk -- An array containing the integrals of the normalized B-splines defined on the set of knots. + + See also: + splprep, splrep, sproot, spalde, splev - evaluation, roots, integral + bisplrep, bisplev - bivariate splines + UnivariateSpline, BivariateSpline - an alternative wrapping + of the FITPACK functions """ t,c,k=tck try: c[0][0];return _ntlist(map(lambda c,a=a,b=b,t=t,k=k:splint(a,b,[t,c,k]),c)) @@ -475,6 +498,12 @@ Outputs: (zeros, ) zeros -- An array giving the roots of the spline. + + See also: + splprep, splrep, splint, spalde, splev - evaluation, roots, integral + bisplrep, bisplev - bivariate splines + UnivariateSpline, BivariateSpline - an alternative wrapping + of the FITPACK functions """ t,c,k=tck if k==4: t=t[1:-1] @@ -510,6 +539,12 @@ results -- An array (or a list of arrays) containing all derivatives up to order k inclusive for each point x. + + See also: + splprep, splrep, splint, sproot, splev - evaluation, roots, integral + bisplrep, bisplev - bivariate splines + UnivariateSpline, BivariateSpline - an alternative wrapping + of the FITPACK functions """ t,c,k=tck try: @@ -588,6 +623,11 @@ SEE bisplev to evaluate the value of the B-spline given its tck representation. + + See also: + splprep, splrep, splint, sproot, splev - evaluation, roots, integral + UnivariateSpline, BivariateSpline - an alternative wrapping + of the FITPACK functions """ x,y,z=map(myasarray,[x,y,z]) x,y,z=map(ravel,[x,y,z]) # ensure 1-d arrays. @@ -686,6 +726,15 @@ vals -- The B-pline or its derivative evaluated over the set formed by the cross-product of x and y. + + Remarks: + + SEE bisprep to generate the tck representation. + + See also: + splprep, splrep, splint, sproot, splev - evaluation, roots, integral + UnivariateSpline, BivariateSpline - an alternative wrapping + of the FITPACK functions """ tx,ty,c,kx,ky=tck if not (0<=dx x2: @@ -1169,6 +1267,26 @@ Uses inverse parabolic interpolation when possible to speed up convergence of golden section method. + + See also: + + fmin, fmin_powell, fmin_cg, + fmin_bfgs, fmin_ncg -- multivariate local optimizers + leastsq -- nonlinear least squares minimizer + + fmin_l_bfgs_b, fmin_tnc, + fmin_cobyla -- constrained multivariate optimizers + + anneal, brute -- global optimizers + + fminbound, brent, golden, bracket -- local scalar minimizers + + fsolve -- n-dimenstional root-finding + + brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding + + fixed_point -- scalar fixed-point finder + """ _mintol = 1.0e-11 _cg = 0.3819660 @@ -1267,6 +1385,26 @@ (see bracket) Uses analog of bisection method to decrease the bracketed interval. + + See also: + + fmin, fmin_powell, fmin_cg, + fmin_bfgs, fmin_ncg -- multivariate local optimizers + leastsq -- nonlinear least squares minimizer + + fmin_l_bfgs_b, fmin_tnc, + fmin_cobyla -- constrained multivariate optimizers + + anneal, brute -- global optimizers + + fminbound, brent, golden, bracket -- local scalar minimizers + + fsolve -- n-dimenstional root-finding + + brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding + + fixed_point -- scalar fixed-point finder + """ if brack is None: xa,xb,xc,fa,fb,fc,funcalls = bracket(func, args=args) @@ -1433,6 +1571,25 @@ disp -- non-zero to print convergence messages. retall -- non-zero to return a list of the solution at each iteration + See also: + + fmin, fmin_powell, fmin_cg, + fmin_bfgs, fmin_ncg -- multivariate local optimizers + leastsq -- nonlinear least squares minimizer + + fmin_l_bfgs_b, fmin_tnc, + fmin_cobyla -- constrained multivariate optimizers + + anneal, brute -- global optimizers + + fminbound, brent, golden, bracket -- local scalar minimizers + + fsolve -- n-dimenstional root-finding + + brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding + + fixed_point -- scalar fixed-point finder + """ # we need to use a mutable object here that we can update in the # wrapper function @@ -1557,6 +1714,26 @@ grid -- tuple with same length as x0 representing the evaluation grid Jout -- Function values over grid: Jout = func(*grid) + + See also: + + fmin, fmin_powell, fmin_cg, + fmin_bfgs, fmin_ncg -- multivariate local optimizers + leastsq -- nonlinear least squares minimizer + + fmin_l_bfgs_b, fmin_tnc, + fmin_cobyla -- constrained multivariate optimizers + + anneal, brute -- global optimizers + + fminbound, brent, golden, bracket -- local scalar minimizers + + fsolve -- n-dimenstional root-finding + + brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding + + fixed_point -- scalar fixed-point finder + """ N = len(ranges) if N > 40: Modified: trunk/Lib/optimize/tnc.py =================================================================== --- trunk/Lib/optimize/tnc.py 2006-09-23 11:47:47 UTC (rev 2218) +++ trunk/Lib/optimize/tnc.py 2006-09-23 21:56:21 UTC (rev 2219) @@ -54,6 +54,7 @@ } HUGE_VAL=1e500 # No standard representation of Infinity in Python 2.3.3 + # FIXME: can we use inf now that we have numpy and IEEE floats? EINVAL = -2 # Invalid parameters (n<1) INFEASIBLE = -1 # Infeasible (low > up) @@ -149,6 +150,26 @@ x : the solution (a list of floats) nfeval : the number of function evaluations rc : return code (corresponding message in optimize.tnc.RCSTRINGS) + + See also: + + fmin, fmin_powell, fmin_cg, + fmin_bfgs, fmin_ncg -- multivariate local optimizers + leastsq -- nonlinear least squares minimizer + + fmin_l_bfgs_b, fmin_tnc, + fmin_cobyla -- constrained multivariate optimizers + + anneal, brute -- global optimizers + + fminbound, brent, golden, bracket -- local scalar minimizers + + fsolve -- n-dimenstional root-finding + + brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding + + fixed_point -- scalar fixed-point finder + """ n = len(x0) Modified: trunk/Lib/optimize/zeros.py =================================================================== --- trunk/Lib/optimize/zeros.py 2006-09-23 11:47:47 UTC (rev 2218) +++ trunk/Lib/optimize/zeros.py 2006-09-23 21:56:21 UTC (rev 2219) @@ -70,6 +70,26 @@ is the root, and r is a RootResults object containing information about the convergence. In particular, r.converged is True if the the routine converged. + + See also: + + fmin, fmin_powell, fmin_cg, + fmin_bfgs, fmin_ncg -- multivariate local optimizers + leastsq -- nonlinear least squares minimizer + + fmin_l_bfgs_b, fmin_tnc, + fmin_cobyla -- constrained multivariate optimizers + + anneal, brute -- global optimizers + + fminbound, brent, golden, bracket -- local scalar minimizers + + fsolve -- n-dimenstional root-finding + + brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding + + fixed_point -- scalar fixed-point finder + """ if type(args) != type(()) : args = (args,) @@ -113,6 +133,26 @@ is the root, and r is a RootResults object containing information about the convergence. In particular, r.converged is True if the the routine converged. + + See also: + + fmin, fmin_powell, fmin_cg, + fmin_bfgs, fmin_ncg -- multivariate local optimizers + leastsq -- nonlinear least squares minimizer + + fmin_l_bfgs_b, fmin_tnc, + fmin_cobyla -- constrained multivariate optimizers + + anneal, brute -- global optimizers + + fminbound, brent, golden, bracket -- local scalar minimizers + + fsolve -- n-dimenstional root-finding + + brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding + + fixed_point -- scalar fixed-point finder + """ if type(args) != type(()) : args = (args,) @@ -157,6 +197,26 @@ is the root, and r is a RootResults object containing information about the convergence. In particular, r.converged is True if the the routine converged. + + See also: + + fmin, fmin_powell, fmin_cg, + fmin_bfgs, fmin_ncg -- multivariate local optimizers + leastsq -- nonlinear least squares minimizer + + fmin_l_bfgs_b, fmin_tnc, + fmin_cobyla -- constrained multivariate optimizers + + anneal, brute -- global optimizers + + fminbound, brent, golden, bracket -- local scalar minimizers + + fsolve -- n-dimenstional root-finding + + brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding + + fixed_point -- scalar fixed-point finder + """ if type(args) != type(()) : args = (args,) @@ -201,6 +261,26 @@ is the root, and r is a RootResults object containing information about the convergence. In particular, r.converged is True if the the routine converged. + + See also: + + fmin, fmin_powell, fmin_cg, + fmin_bfgs, fmin_ncg -- multivariate local optimizers + leastsq -- nonlinear least squares minimizer + + fmin_l_bfgs_b, fmin_tnc, + fmin_cobyla -- constrained multivariate optimizers + + anneal, brute -- global optimizers + + fminbound, brent, golden, bracket -- local scalar minimizers + + fsolve -- n-dimenstional root-finding + + brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding + + fixed_point -- scalar fixed-point finder + """ if type(args) != type(()) : args = (args,) From scipy-svn at scipy.org Sat Sep 23 18:50:21 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 23 Sep 2006 17:50:21 -0500 (CDT) Subject: [Scipy-svn] r2220 - in trunk/Lib/interpolate: . tests Message-ID: <20060923225021.01DC439C03E@new.scipy.org> Author: rkern Date: 2006-09-23 17:50:16 -0500 (Sat, 23 Sep 2006) New Revision: 2220 Modified: trunk/Lib/interpolate/interpolate.py trunk/Lib/interpolate/tests/test_interpolate.py Log: Cleaned up interp1d and added tests for it. Modified: trunk/Lib/interpolate/interpolate.py =================================================================== --- trunk/Lib/interpolate/interpolate.py 2006-09-23 21:56:21 UTC (rev 2219) +++ trunk/Lib/interpolate/interpolate.py 2006-09-23 22:50:16 UTC (rev 2220) @@ -1,14 +1,15 @@ -""" Class for interpolating values - - !! Need to find argument for keeping initialize. If it isn't - !! found, get rid of it! +""" Classes for interpolating values. """ +# !! Need to find argument for keeping initialize. If it isn't +# !! found, get rid of it! + __all__ = ['interp1d', 'interp2d'] from numpy import shape, sometrue, rank, array, transpose, \ swapaxes, searchsorted, clip, take, ones, putmask, less, greater, \ logical_or, atleast_1d, atleast_2d +import numpy as np import fitpack @@ -18,50 +19,81 @@ all = sometrue(all,axis=0) return all -class interp2d: - def __init__(self, x, y, z, kind='linear', - copy=True, bounds_error=False, fill_value=None): +class interp2d(object): + """ Interpolate over a 2D grid. + + See Also + -------- + bisplrep, bisplev - spline interpolation based on FITPACK + BivariateSpline - a more recent wrapper of the FITPACK routines + """ + + def __init__(self, x, y, z, kind='linear', copy=True, bounds_error=False, + fill_value=np.nan): + """ Initialize a 2D interpolator. + + Parameters + ---------- + x : 1D array or 2D meshgrid array + y : 1D array or 2D meshgrid array + Arrays defining the coordinates of a 2D grid. + z : 2D array + The values of the interpolated function on the grid points. + kind : 'linear', 'cubic', 'quintic' + The kind of interpolation to use. + copy : bool + If True, then data is copied, otherwise only a reference is held. + bounds_error : bool + If True, when interoplated values are requested outside of the + domain of the input data, an error is raised. + If False, then fill_value is used. + fill_value : number + If provided, the value to use for points outside of the + interpolation domain. Defaults to NaN. + + Raises + ------ + ValueError when inputs are invalid. + """ - Input: - x,y - 1-d arrays defining 2-d grid (or 2-d meshgrid arrays) - z - 2-d array of grid values - kind - interpolation type ('linear', 'cubic', 'quintic') - copy - if true then data is copied into class, otherwise only a - reference is held. - bounds_error - if true, then when out_of_bounds occurs, an error is - raised otherwise, the output is filled with - fill_value. - fill_value - if None, then NaN, otherwise the value to fill in - outside defined region. - """ + self.x = atleast_1d(x).copy() self.y = atleast_1d(y).copy() if rank(self.x) > 2 or rank(self.y) > 2: - raise ValueError, "One of the input arrays is not 1-d or 2-d." + raise ValueError("One of the input arrays is not 1-d or 2-d.") if rank(self.x) == 2: self.x = self.x[:,0] if rank(self.y) == 2: self.y = self.y[0] self.z = array(z,copy=True) if rank(z) != 2: - raise ValueError, "Grid values is not a 2-d array." + raise ValueError("Grid values is not a 2-d array.") try: kx = ky = {'linear' : 1, 'cubic' : 3, 'quintic' : 5}[kind] - except: - raise ValueError, "Unsupported interpolation type." + except KeyError: + raise ValueError("Unsupported interpolation type.") self.tck = fitpack.bisplrep(x, y, z, kx=kx, ky=ky, s=0.) def __call__(self,x,y,dx=0,dy=0): + """ Interpolate the function. + + Parameters + ---------- + x : 1D array + y : 1D array + The points to interpolate. + dx : int >= 0, < kx + dy : int >= 0, < ky + The order of partial derivatives in x and y, respectively. + + Returns + ------- + z : 2D array with shape (len(y), len(x)) + The interpolated values. """ - Input: - x,y - 1-d arrays defining points to interpolate. - dx,dy - order of partial derivatives in x and y, respectively. - 0<=dx(x_new). + def __call__(self, x_new): + """ Find linearly interpolated y_new = f(x_new). - Inputs: - x_new -- New independent variables. + Parameters + ---------- + x_new : number or array + New independent variable(s). - Outputs: - y_new -- Linearly interpolated values corresponding to x_new. + Returns + ------- + y_new : number or array + Linearly interpolated value(s) corresponding to x_new. """ + # 1. Handle values in x_new that are outside of x. Throw error, # or return a list of mask array indicating the outofbounds values. # The behavior is set by the bounds_error variable. x_new = atleast_1d(x_new) out_of_bounds = self._check_bounds(x_new) + # 2. Find where in the orignal data, the values to interpolate # would be inserted. - # Note: If x_new[n] = x[m], then m is returned by searchsorted. - x_new_indices = searchsorted(self.x,x_new) + # Note: If x_new[n] == x[m], then m is returned by searchsorted. + x_new_indices = searchsorted(self.x, x_new) + # 3. Clip x_new_indices so that they are within the range of # self.x indices and at least 1. Removes mis-interpolation # of x_new[n] = x[0] - x_new_indices = clip(x_new_indices,1,len(self.x)-1).astype(int) + x_new_indices = x_new_indices.clip(1, len(self.x)-1).astype(int) + # 4. Calculate the slope of regions that each x_new value falls in. - lo = x_new_indices - 1; hi = x_new_indices + lo = x_new_indices - 1 + hi = x_new_indices - # !! take(,axis=0) should default to the last axis (IMHO) and remove - # !! the extra argument. - x_lo = take(self.x,lo,axis=self.interp_axis) - x_hi = take(self.x,hi,axis=self.interp_axis); - y_lo = take(self.y,lo,axis=self.interp_axis) - y_hi = take(self.y,hi,axis=self.interp_axis); - slope = (y_hi-y_lo)/(x_hi-x_lo) + x_lo = self.x[lo] + x_hi = self.x[hi] + y_lo = self.y[..., lo] + y_hi = self.y[..., hi] + + # Note that the following two expressions rely on the specifics of the + # broadcasting semantics. + slope = (y_hi-y_lo) / (x_hi-x_lo) + # 5. Calculate the actual value for each entry in x_new. y_new = slope*(x_new-x_lo) + y_lo - # 6. Fill any values that were out of bounds with NaN - # !! Need to think about how to do this efficiently for - # !! mutli-dimensional Cases. - yshape = y_new.shape - y_new = y_new.ravel() - new_shape = list(yshape) - new_shape[self.interp_axis] = 1 - sec_shape = [1]*len(new_shape) - sec_shape[self.interp_axis] = len(out_of_bounds) - out_of_bounds.shape = sec_shape - new_out = ones(new_shape)*out_of_bounds - putmask(y_new, new_out.ravel(), self.fill_value) - y_new.shape = yshape - # Rotate the values of y_new back so that they coorespond to the - # correct x_new values. - result = swapaxes(y_new,self.interp_axis,self.axis) - try: - len(x_new) - return result - except TypeError: - return result[0] + + # 6. Fill any values that were out of bounds with fill_value. + y_new[..., out_of_bounds] = self.fill_value + + # Rotate the values of y_new back so that they correspond to the + # correct x_new values. For N-D x_new, take the last N axes from y_new + # and insert them where self.axis was in the list of axes. + nx = len(x_new.shape) + ny = len(y_new.shape) + axes = range(ny - nx) + axes[self.axis:self.axis] = range(ny - nx, ny) + result = y_new.transpose(axes) + return result - def _check_bounds(self,x_new): - # If self.bounds_error = 1, we raise an error if any x_new values + def _check_bounds(self, x_new): + """ Check the inputs for being in the bounds of the interpolated data. + + Parameters + ---------- + x_new : array + + Returns + ------- + out_of_bounds : bool array + The mask on x_new of values that are out of the bounds. + """ + + # If self.bounds_error is True, we raise an error if any x_new values # fall outside the range of x. Otherwise, we return an array indicating # which values are outside the boundary region. - # !! Needs some work for multi-dimensional x !! - below_bounds = less(x_new,self.x[0]) - above_bounds = greater(x_new,self.x[-1]) - # Note: sometrue has been redefined to handle length 0 arrays + below_bounds = x_new < self.x[0] + above_bounds = x_new > self.x[-1] + # !! Could provide more information about which values are out of bounds - if self.bounds_error and sometrue(below_bounds,axis=0): - raise ValueError, " A value in x_new is below the"\ - " interpolation range." - if self.bounds_error and sometrue(above_bounds,axis=0): - raise ValueError, " A value in x_new is above the"\ - " interpolation range." - # !! Should we emit a warning if some values are out of bounds. + if self.bounds_error and below_bounds.any(): + raise ValueError("A value in x_new is below the interpolation " + "range.") + if self.bounds_error and above_bounds.any(): + raise ValueError("A value in x_new is above the interpolation " + "range.") + + # !! Should we emit a warning if some values are out of bounds? # !! matlab does not. - out_of_bounds = logical_or(below_bounds,above_bounds) + out_of_bounds = logical_or(below_bounds, above_bounds) return out_of_bounds - def model_error(self,x_new,y_new): - # How well do x_new,yy points fit the model? - # Return an array of error values. - pass - - -#assumes module test_xxx is in python path -#def test(): -# test_module = 'test_' + __name__ # __name__ is name of this module -# test_string = 'import %s;reload(%s);%s.test()' % ((test_module,)*3) -# exec(test_string) - -#if __name__ == '__main__': -# test() Modified: trunk/Lib/interpolate/tests/test_interpolate.py =================================================================== --- trunk/Lib/interpolate/tests/test_interpolate.py 2006-09-23 21:56:21 UTC (rev 2219) +++ trunk/Lib/interpolate/tests/test_interpolate.py 2006-09-23 22:50:16 UTC (rev 2220) @@ -1,9 +1,12 @@ from numpy.testing import * from numpy import mgrid, pi, sin, ogrid +import numpy as np + set_package_path() from interpolate import interp1d, interp2d restore_path() + class test_interp2d(ScipyTestCase): def test_interp2d(self): y, x = mgrid[0:pi:20j, 0:pi:21j] @@ -14,5 +17,182 @@ v,u = ogrid[0:pi:24j, 0:pi:25j] assert_almost_equal(I(u.ravel(), v.ravel()), sin(v+u), decimal=2) + +class test_interp1d(ScipyTestCase): + + def setUp(self): + self.x10 = np.arange(10.) + self.y10 = np.arange(10.) + self.x25 = self.x10.reshape((2,5)) + self.x2 = np.arange(2.) + self.y2 = np.arange(2.) + self.x1 = np.array([0.]) + self.y1 = np.array([0.]) + + self.y210 = np.arange(20.).reshape((2, 10)) + self.y102 = np.arange(20.).reshape((10, 2)) + + self.fill_value = -100.0 + + def test_validation(self): + """ Make sure that appropriate exceptions are raised when invalid values + are given to the constructor. + """ + + # Only kind='linear' is implemented. + self.assertRaises(NotImplementedError, interp1d, self.x10, self.y10, kind='cubic') + interp1d(self.x10, self.y10, kind='linear') + + # x array must be 1D. + self.assertRaises(ValueError, interp1d, self.x25, self.y10) + + # y array cannot be a scalar. + self.assertRaises(ValueError, interp1d, self.x10, np.array(0)) + + # Check for x and y arrays having the same length. + self.assertRaises(ValueError, interp1d, self.x10, self.y2) + self.assertRaises(ValueError, interp1d, self.x2, self.y10) + self.assertRaises(ValueError, interp1d, self.x10, self.y102) + interp1d(self.x10, self.y210) + interp1d(self.x10, self.y102, axis=0) + + # Check for x and y having at least 1 element. + self.assertRaises(ValueError, interp1d, self.x1, self.y10) + self.assertRaises(ValueError, interp1d, self.x10, self.y1) + self.assertRaises(ValueError, interp1d, self.x1, self.y1) + + + def test_init(self): + """ Check that the attributes are initialized appropriately by the + constructor. + """ + + self.assert_(interp1d(self.x10, self.y10).copy) + self.assert_(not interp1d(self.x10, self.y10, copy=False).copy) + self.assert_(interp1d(self.x10, self.y10).bounds_error) + self.assert_(not interp1d(self.x10, self.y10, bounds_error=False).bounds_error) + self.assert_(np.isnan(interp1d(self.x10, self.y10).fill_value)) + self.assertEqual( + interp1d(self.x10, self.y10, fill_value=3.0).fill_value, + 3.0, + ) + self.assertEqual( + interp1d(self.x10, self.y10).axis, + 0, + ) + self.assertEqual( + interp1d(self.x10, self.y210).axis, + 1, + ) + self.assertEqual( + interp1d(self.x10, self.y102, axis=0).axis, + 0, + ) + assert_array_equal( + interp1d(self.x10, self.y10).x, + self.x10, + ) + assert_array_equal( + interp1d(self.x10, self.y10).y, + self.y10, + ) + assert_array_equal( + interp1d(self.x10, self.y210).y, + self.y210, + ) + + + def test_linear(self): + """ Check the actual implementation of linear interpolation. + """ + + interp10 = interp1d(self.x10, self.y10) + assert_array_almost_equal( + interp10(self.x10), + self.y10, + ) + assert_array_almost_equal( + interp10(1.2), + np.array([1.2]), + ) + assert_array_almost_equal( + interp10([2.4, 5.6, 6.0]), + np.array([2.4, 5.6, 6.0]), + ) + + + def test_bounds(self): + """ Test that our handling of out-of-bounds input is correct. + """ + + extrap10 = interp1d(self.x10, self.y10, fill_value=self.fill_value, + bounds_error=False) + assert_array_equal( + extrap10(11.2), + np.array([self.fill_value]), + ) + assert_array_equal( + extrap10(-3.4), + np.array([self.fill_value]), + ) + assert_array_equal( + extrap10._check_bounds(np.array([-1.0, 0.0, 5.0, 9.0, 11.0])), + np.array([True, False, False, False, True]), + ) + + raises_bounds_error = interp1d(self.x10, self.y10, bounds_error=True) + self.assertRaises(ValueError, raises_bounds_error, -1.0) + self.assertRaises(ValueError, raises_bounds_error, 11.0) + raises_bounds_error([0.0, 5.0, 9.0]) + + + def test_nd(self): + """ Check the behavior when the inputs and outputs are multidimensional. + """ + + # Multidimensional input. + interp10 = interp1d(self.x10, self.y10) + assert_array_almost_equal( + interp10(np.array([[3.4, 5.6], [2.4, 7.8]])), + np.array([[3.4, 5.6], [2.4, 7.8]]), + ) + + # Multidimensional outputs. + interp210 = interp1d(self.x10, self.y210) + assert_array_almost_equal( + interp210(1.5), + np.array([[1.5], [11.5]]), + ) + assert_array_almost_equal( + interp210(np.array([1.5, 2.4])), + np.array([[1.5, 2.4], + [11.5, 12.4]]), + ) + + interp102 = interp1d(self.x10, self.y102, axis=0) + assert_array_almost_equal( + interp102(1.5), + np.array([[3.0, 4.0]]), + ) + assert_array_almost_equal( + interp102(np.array([1.5, 2.4])), + np.array([[3.0, 4.0], + [4.8, 5.8]]), + ) + + # Both at the same time! + x_new = np.array([[3.4, 5.6], [2.4, 7.8]]) + assert_array_almost_equal( + interp210(x_new), + np.array([[[3.4, 5.6], [2.4, 7.8]], + [[13.4, 15.6], [12.4, 17.8]]]), + ) + assert_array_almost_equal( + interp102(x_new), + np.array([[[6.8, 7.8], [11.2, 12.2]], + [[4.8, 5.8], [15.6, 16.6]]]), + ) + + if __name__ == "__main__": ScipyTest().run() From scipy-svn at scipy.org Sun Sep 24 00:41:40 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 23 Sep 2006 23:41:40 -0500 (CDT) Subject: [Scipy-svn] r2221 - trunk/Lib/special Message-ID: <20060924044140.5C3DD39C023@new.scipy.org> Author: rkern Date: 2006-09-23 23:41:36 -0500 (Sat, 23 Sep 2006) New Revision: 2221 Modified: trunk/Lib/special/basic.py Log: Fix #229 Modified: trunk/Lib/special/basic.py =================================================================== --- trunk/Lib/special/basic.py 2006-09-23 22:50:16 UTC (rev 2220) +++ trunk/Lib/special/basic.py 2006-09-24 04:41:36 UTC (rev 2221) @@ -329,13 +329,13 @@ return jn[:(n+1)],jnp[:(n+1)] def _sph_harmonic(m,n,theta,phi): - """inputs of (m,n,theta,phi) returns spherical harmonic of order - m,n (|m|<=n) and argument theta and phi: Y^m_n(theta,phi) + """Spherical harmonic of order m,n (|m|<=n) and argument theta and phi: + Y^m_n(theta,phi) """ x = cos(phi) m,n = int(m), int(n) Pmn,Pmnd = lpmn(m,n,x) - val = Pmn[m,n] + val = Pmn[-1, -1] val *= sqrt((2*n+1)/4.0/pi) val *= exp(0.5*(gammaln(n-m+1)-gammaln(n+m+1))) val *= exp(1j*m*theta) From scipy-svn at scipy.org Sun Sep 24 01:12:33 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 00:12:33 -0500 (CDT) Subject: [Scipy-svn] r2222 - trunk Message-ID: <20060924051233.2CEED39C198@new.scipy.org> Author: rkern Date: 2006-09-24 00:12:27 -0500 (Sun, 24 Sep 2006) New Revision: 2222 Added: trunk/new_manifest.sh Modified: trunk/MANIFEST.in Log: Include sandbox files in MANIFEST.in so they will be packaged into source distributions. Modified: trunk/MANIFEST.in =================================================================== --- trunk/MANIFEST.in 2006-09-24 04:41:36 UTC (rev 2221) +++ trunk/MANIFEST.in 2006-09-24 05:12:27 UTC (rev 2222) @@ -1,6 +1,77 @@ -#prune scipy_core +# This automatically generated by new_manifest.sh +# +# Note: for files required to build extensions, you probably want to +# place them in the Extension depend list, rather than in this file. +# include setup.py include *.txt include Lib/*.py -# No more files here! They should be probably added -# to Extension depend argument list. \ No newline at end of file +# +# The following files are in the sandbox and wouldn't normally get included in sdist: +# +include Lib/sandbox/ann/* +include Lib/sandbox/ann/data/* +include Lib/sandbox/arraysetops/* +include Lib/sandbox/arraysetops/tests/* +include Lib/sandbox/buildgrid/* +include Lib/sandbox/constants/* +include Lib/sandbox/cow/* +include Lib/sandbox/delaunay/* +include Lib/sandbox/delaunay/tests/* +include Lib/sandbox/exmplpackage/* +include Lib/sandbox/exmplpackage/tests/* +include Lib/sandbox/exmplpackage/yyy/* +include Lib/sandbox/exmplpackage/yyy/tests/* +include Lib/sandbox/fdfpack/* +include Lib/sandbox/fdfpack/src/* +include Lib/sandbox/fdfpack/tests/* +include Lib/sandbox/fdfpack/utils/* +include Lib/sandbox/ga/* +include Lib/sandbox/gplt/* +include Lib/sandbox/image/* +include Lib/sandbox/models/* +include Lib/sandbox/models/family/* +include Lib/sandbox/models/robust/* +include Lib/sandbox/models/tests/* +include Lib/sandbox/montecarlo/* +include Lib/sandbox/montecarlo/src/* +include Lib/sandbox/montecarlo/tests/* +include Lib/sandbox/netcdf/* +include Lib/sandbox/newoptimize/* +include Lib/sandbox/numexpr/* +include Lib/sandbox/numexpr/tests/* +include Lib/sandbox/odr/* +include Lib/sandbox/odr/odrpack/* +include Lib/sandbox/plt/* +include Lib/sandbox/pysparse/* +include Lib/sandbox/pysparse/Tools/* +include Lib/sandbox/pysparse/amd/* +include Lib/sandbox/pysparse/docs/* +include Lib/sandbox/pysparse/examples/* +include Lib/sandbox/pysparse/examples/poisson_test/* +include Lib/sandbox/pysparse/include/* +include Lib/sandbox/pysparse/include/pysparse/* +include Lib/sandbox/pysparse/lib/* +include Lib/sandbox/pysparse/src/* +include Lib/sandbox/pysparse/superlu/* +include Lib/sandbox/pysparse/tests/* +include Lib/sandbox/pysparse/umfpack/* +include Lib/sandbox/rkern/* +include Lib/sandbox/stats/* +include Lib/sandbox/svm/* +include Lib/sandbox/svm/libsvm-2.82/* +include Lib/sandbox/svm/tests/* +include Lib/sandbox/umfpack/* +include Lib/sandbox/umfpack/umfpack/* +include Lib/sandbox/xplt/* +include Lib/sandbox/xplt/gistdata/* +include Lib/sandbox/xplt/pygist/* +include Lib/sandbox/xplt/src/* +include Lib/sandbox/xplt/src/g/* +include Lib/sandbox/xplt/src/gist/* +include Lib/sandbox/xplt/src/play/* +include Lib/sandbox/xplt/src/play/all/* +include Lib/sandbox/xplt/src/play/mac/* +include Lib/sandbox/xplt/src/play/unix/* +include Lib/sandbox/xplt/src/play/win/* +include Lib/sandbox/xplt/src/play/x11/* Added: trunk/new_manifest.sh =================================================================== --- trunk/new_manifest.sh 2006-09-24 04:41:36 UTC (rev 2221) +++ trunk/new_manifest.sh 2006-09-24 05:12:27 UTC (rev 2222) @@ -0,0 +1,29 @@ +#!/bin/sh + +# This little script re-generates MANIFEST.in to include files that +# wouldn't otherwise get picked up by distutils. This is relevant when +# building source distributions using "python setup.py sdist", which +# would otherwise leave these files out. + +MANIFEST_IN=MANIFEST.in +SANDBOX_DIR="Lib/sandbox" + +cat < $MANIFEST_IN +# This automatically generated by new_manifest.sh +# +# Note: for files required to build extensions, you probably want to +# place them in the Extension depend list, rather than in this file. +# +include setup.py +include *.txt +include Lib/*.py +# +# The following files are in the sandbox and wouldn't normally get included in sdist: +# +EOF + +for i in `svn ls -R $SANDBOX_DIR`; do + if test -d $SANDBOX_DIR/$i; then + echo "include $SANDBOX_DIR/$i*" >> $MANIFEST_IN + fi +done Property changes on: trunk/new_manifest.sh ___________________________________________________________________ Name: svn:executable + * From scipy-svn at scipy.org Sun Sep 24 01:28:44 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 00:28:44 -0500 (CDT) Subject: [Scipy-svn] r2223 - trunk/Lib/stats Message-ID: <20060924052844.CA94939C198@new.scipy.org> Author: rkern Date: 2006-09-24 00:28:38 -0500 (Sun, 24 Sep 2006) New Revision: 2223 Modified: trunk/Lib/stats/distributions.py Log: Fix 251 Modified: trunk/Lib/stats/distributions.py =================================================================== --- trunk/Lib/stats/distributions.py 2006-09-24 05:12:27 UTC (rev 2222) +++ trunk/Lib/stats/distributions.py 2006-09-24 05:28:38 UTC (rev 2223) @@ -924,7 +924,7 @@ return special.btdtri(a,b,q) def _stats(self, a, b): mn = a *1.0 / (a + b) - var = (a*b*1.0)*(a+b+1.0)/(a+b)**2.0 + var = (a*b*1.0)/(a+b+1.0)/(a+b)**2.0 g1 = 2.0*(b-a)*sqrt((1.0+a+b)/(a*b)) / (2+a+b) g2 = 6.0*(a**3 + a**2*(1-2*b) + b**2*(1+b) - 2*a*b*(2+b)) g2 /= a*b*(a+b+2)*(a+b+3) From scipy-svn at scipy.org Sun Sep 24 01:49:12 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 00:49:12 -0500 (CDT) Subject: [Scipy-svn] r2224 - trunk/Lib/sandbox/delaunay Message-ID: <20060924054912.D288939C023@new.scipy.org> Author: rkern Date: 2006-09-24 00:49:03 -0500 (Sun, 24 Sep 2006) New Revision: 2224 Modified: trunk/Lib/sandbox/delaunay/_delaunay.cpp Log: Fix #254 Modified: trunk/Lib/sandbox/delaunay/_delaunay.cpp =================================================================== --- trunk/Lib/sandbox/delaunay/_delaunay.cpp 2006-09-24 05:28:38 UTC (rev 2223) +++ trunk/Lib/sandbox/delaunay/_delaunay.cpp 2006-09-24 05:49:03 UTC (rev 2224) @@ -362,7 +362,7 @@ PyArrayObject *planes, *x, *y, *nodes, *neighbors; - if (!PyArg_ParseTuple(args, "ddlddldOOOOO", &x0, &x1, &xsteps, &y0, &y1, &ysteps, + if (!PyArg_ParseTuple(args, "ddiddidOOOOO", &x0, &x1, &xsteps, &y0, &y1, &ysteps, &defvalue, &pyplanes, &pyx, &pyy, &pynodes, &pyneighbors)) { return NULL; } @@ -524,12 +524,11 @@ PyObject *pyx, *pyy, *pyz, *pycenters, *pynodes, *pyneighbors, *grid; PyArrayObject *x, *y, *z, *centers, *nodes, *neighbors; double x0, x1, y0, y1, defvalue; - double *grid_ptr; int xsteps, ysteps; int npoints, ntriangles; intp dims[2]; - if (!PyArg_ParseTuple(args, "ddlddldOOOOOO", &x0, &x1, &xsteps, + if (!PyArg_ParseTuple(args, "ddiddidOOOOOO", &x0, &x1, &xsteps, &y0, &y1, &ysteps, &defvalue, &pyx, &pyy, &pyz, &pycenters, &pynodes, &pyneighbors)) { return NULL; @@ -579,7 +578,6 @@ dims[1] = xsteps; grid = PyArray_SimpleNew(2, dims, PyArray_DOUBLE); if (!grid) {CLEANUP} // goto fail; - grid_ptr = (double*)PyArray_DATA(grid); NaturalNeighbors nn(npoints, ntriangles, (double*)PyArray_DATA(x), (double*)PyArray_DATA(y), From scipy-svn at scipy.org Sun Sep 24 02:50:17 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 01:50:17 -0500 (CDT) Subject: [Scipy-svn] r2225 - in trunk/Lib/stats: . tests Message-ID: <20060924065017.39C2139C023@new.scipy.org> Author: rkern Date: 2006-09-24 01:50:14 -0500 (Sun, 24 Sep 2006) New Revision: 2225 Modified: trunk/Lib/stats/distributions.py trunk/Lib/stats/stats.py trunk/Lib/stats/tests/test_stats.py Log: Use modern numpy idioms. Remove ANOVA tests. Modified: trunk/Lib/stats/distributions.py =================================================================== --- trunk/Lib/stats/distributions.py 2006-09-24 05:49:03 UTC (rev 2224) +++ trunk/Lib/stats/distributions.py 2006-09-24 06:50:14 UTC (rev 2225) @@ -4161,13 +4161,10 @@ If max is None, then range is >=0 and < min """ - if max is None: - max = min - min = 0 - U = random(size=size) - val = floor((max-min)*U + min) - return arr(val).astype(int)[()] # return array scalar if needed + # Return an array scalar if needed. + return arr(mtrand.randint(min, max, size))[()] + def _entropy(self, min, max): return log(max-min) randint = randint_gen(name='randint',longname='A discrete uniform '\ Modified: trunk/Lib/stats/stats.py =================================================================== --- trunk/Lib/stats/stats.py 2006-09-24 05:49:03 UTC (rev 2224) +++ trunk/Lib/stats/stats.py 2006-09-24 06:50:14 UTC (rev 2225) @@ -182,20 +182,16 @@ ## changed name of skewness and askewness to skew and askew ## fixed (a)histogram (which sometimes counted points 1D arrays + a = np.ravel(a) # flatten any >1D arrays if (defaultlimits != None): lowerreallimit = defaultlimits[0] upperreallimit = defaultlimits[1] binsize = (upperreallimit-lowerreallimit) / float(numbins) else: - Min = minimum.reduce(a) - Max = maximum.reduce(a) + Min = a.min() + Max = a.max() estbinwidth = float(Max - Min)/float(numbins - 1) binsize = (Max-Min+estbinwidth)/float(numbins) lowerreallimit = Min - binsize/2.0 #lower real limit,1st bin @@ -1027,7 +1025,7 @@ Returns: array of cumfreq bin values, lowerreallimit, binsize, extrapoints """ h,l,b,e = histogram(a,numbins,defaultreallimits) - cumhist = cumsum(h*1,axis=0) + cumhist = np.cumsum(h*1, axis=0) return cumhist,l,b,e @@ -1060,12 +1058,12 @@ """ TINY = 1e-10 k = len(args) - n = zeros(k,Float) - v = zeros(k,Float) - m = zeros(k,Float) + n = zeros(k) + v = zeros(k) + m = zeros(k) nargs = [] for i in range(k): - nargs.append(args[i].astype(Float)) + nargs.append(args[i].astype(float)) n[i] = float(len(nargs[i])) v[i] = var(nargs[i]) m[i] = mean(nargs[i],None) @@ -1092,7 +1090,7 @@ an integer (the axis over which to operate) """ a, axis = _chk_asarray(a, axis) - mn = expand_dims(mean(a, axis), axis) + mn = np.expand_dims(mean(a, axis), axis) deviations = a - mn n = a.shape[axis] svar = ss(deviations,axis) / float(n) @@ -1104,7 +1102,7 @@ array (i.e., using N). Axis can equal None (ravel array first), an integer (the axis over which to operate). """ - return sqrt(samplevar(a,axis)) + return np.sqrt(samplevar(a,axis)) def signaltonoise(instack, axis=0): @@ -1117,7 +1115,7 @@ """ m = mean(instack,axis) sd = samplestd(instack,axis) - return where(equal(sd,0),0,m/sd) + return np.where(sd == 0, 0, m/sd) def var(a, axis=0, bias=False): @@ -1127,7 +1125,7 @@ integer (the axis over which to operate). """ a, axis = _chk_asarray(a, axis) - mn = expand_dims(mean(a,axis),axis) + mn = np.expand_dims(mean(a,axis),axis) deviations = a - mn n = a.shape[axis] vals = ss(deviations,axis)/(n-1.0) @@ -1142,7 +1140,7 @@ the passed array (i.e., N-1). Axis can equal None (ravel array first), or an integer (the axis over which to operate). """ - return sqrt(var(a,axis,bias)) + return np.sqrt(var(a,axis,bias)) def stderr(a, axis=0): @@ -1152,7 +1150,7 @@ first), or an integer (the axis over which to operate). """ a, axis = _chk_asarray(a, axis) - return std(a,axis) / float(sqrt(a.shape[axis])) + return std(a,axis) / float(np.sqrt(a.shape[axis])) def sem(a, axis=0): @@ -1163,7 +1161,7 @@ """ a, axis = _chk_asarray(a, axis) n = a.shape[axis] - s = samplestd(a,axis) / sqrt(n-1) + s = samplestd(a,axis) / np.sqrt(n-1) return s @@ -1212,13 +1210,12 @@ Returns: a, with values threshmax replaced with newval """ a = asarray(a) - mask = zeros(a.shape) + mask = zeros(a.shape, dtype=bool) if threshmin != None: - mask = mask + where(less(a,threshmin),1,0) + mask |= (a < threshmin) if threshmax != None: - mask = mask + where(greater(a,threshmax),1,0) - mask = clip(mask,0,1) - return where(mask,newval,a) + mask |= (a > threshmax) + return np.where(mask, newval, a) def trimboth(a, proportiontocut): @@ -1262,7 +1259,7 @@ """Return mean with proportiontocut chopped from each of the lower and upper tails. """ - newa = trimboth(sort(a),proportiontocut) + newa = trimboth(np.sort(a),proportiontocut) return mean(newa,axis=0) @@ -1296,8 +1293,8 @@ else: y = asarray(y) if rowvar: - m = transpose(m) - y = transpose(y) + m = np.transpose(m) + y = np.transpose(y) N = m.shape[0] if (y.shape[0] != N): raise ValueError, "x and y must have the same number of observations." @@ -1307,7 +1304,7 @@ fact = N*1.0 else: fact = N-1.0 - val = squeeze(dot(transpose(m),conjugate(y))) / fact + val = np.squeeze(np.dot(np.transpose(m),np.conjugate(y))) / fact return val def corrcoef(x, y=None, rowvar=False, bias=True): @@ -1321,11 +1318,11 @@ observations in the columns. """ if y is not None: - x = transpose([x,y]) + x = np.transpose([x,y]) y = None c = cov(x, y, rowvar=rowvar, bias=bias) - d = diag(c) - return c/sqrt(multiply.outer(d,d)) + d = np.diag(c) + return c/np.sqrt(np.multiply.outer(d,d)) @@ -1339,8 +1336,8 @@ Returns: f-value, probability """ na = len(args) # ANOVA on 'na' groups, each in it's own array - tmp = map(array,args) - alldata = concatenate(args) + tmp = map(np.array,args) + alldata = np.concatenate(args) bign = len(alldata) sstot = ss(alldata)-(square_of_sums(alldata)/float(bign)) ssbn = 0 @@ -1582,20 +1579,20 @@ xmean = mean(x,None) ymean = mean(y,None) xm,ym = x-xmean, y-ymean - r_num = add.reduce(xm*ym) - r_den = math.sqrt(ss(xm)*ss(ym)) + r_num = np.add.reduce(xm*ym) + r_den = np.sqrt(ss(xm)*ss(ym)) if r_den == 0.0: r = 0.0 else: r = r_num / r_den if (r > 1.0): r = 1.0 # from numerical error - #z = 0.5*math.log((1.0+r+TINY)/(1.0-r+TINY)) + #z = 0.5*log((1.0+r+TINY)/(1.0-r+TINY)) df = n-2 - t = r*math.sqrt(df/((1.0-r+TINY)*(1.0+r+TINY))) + t = r*np.sqrt(df/((1.0-r+TINY)*(1.0+r+TINY))) prob = betai(0.5*df,0.5,df/(df+t*t)) slope = r_num / ss(xm) intercept = ymean - slope*xmean - sterrest = math.sqrt(1-r*r)*samplestd(y) + sterrest = np.sqrt(1-r*r)*samplestd(y) return slope, intercept, r, prob, sterrest @@ -1616,7 +1613,7 @@ n = len(a) df = n-1 svar = ((n-1)*v) / float(df) - t = (x-popmean)/math.sqrt(svar*(1.0/n)) + t = (x-popmean)/np.sqrt(svar*(1.0/n)) prob = betai(0.5*df,0.5,df/(df+t*t)) return t,prob @@ -1640,14 +1637,14 @@ n2 = b.shape[axis] df = n1+n2-2 svar = ((n1-1)*v1+(n2-1)*v2) / float(df) - zerodivproblem = equal(svar,0) - t = (x1-x2)/sqrt(svar*(1.0/n1 + 1.0/n2)) # N-D COMPUTATION HERE!!!!!! - t = where(zerodivproblem,1.0,t) # replace NaN t-values with 1.0 + zerodivproblem = svar == 0 + t = (x1-x2)/np.sqrt(svar*(1.0/n1 + 1.0/n2)) # N-D COMPUTATION HERE!!!!!! + t = np.where(zerodivproblem, 1.0, t) # replace NaN t-values with 1.0 probs = betai(0.5*df,0.5,float(df)/(df+t*t)) - if not isscalar(t): - probs = reshape(probs,t.shape) - if not isscalar(probs) and len(probs) == 1: + if not np.isscalar(t): + probs = probs.reshape(t.shape) + if not np.isscalar(probs) and len(probs) == 1: probs = probs[0] return t, probs @@ -1672,15 +1669,15 @@ df = float(n-1) d = (a-b).astype('d') - denom = sqrt((n*add.reduce(d*d,axis) - add.reduce(d,axis)**2) /df) - zerodivproblem = equal(denom,0) - t = add.reduce(d, axis) / denom # N-D COMPUTATION HERE!!!!!! - t = where(zerodivproblem,1.0,t) # replace NaN t-values with 1.0 - t = where(zerodivproblem,1.0,t) # replace NaN t-values with 1.0 + denom = np.sqrt((n*np.add.reduce(d*d,axis) - np.add.reduce(d,axis)**2) /df) + zerodivproblem = denom == 0 + t = np.add.reduce(d, axis) / denom # N-D COMPUTATION HERE!!!!!! + t = np.where(zerodivproblem, 1.0, t) # replace NaN t-values with 1.0 + t = np.where(zerodivproblem, 1.0, t) # replace NaN t-values with 1.0 probs = betai(0.5*df,0.5,float(df)/(df+t*t)) - if not isscalar(t): - probs = reshape(probs,t.shape) - if not isscalar(probs) and len(probs) == 1: + if not np.isscalar(t): + probs = np.reshape(probs, t.shape) + if not np.isscalar(probs) and len(probs) == 1: probs = probs[0] return t, probs @@ -1707,12 +1704,12 @@ cdf = getattr(scipy.stats, cdf).cdf if callable(rvs): kwds = {'size':N} - vals = sort(rvs(*args,**kwds)) + vals = np.sort(rvs(*args,**kwds)) else: - vals = sort(rvs) + vals = np.sort(rvs) N = len(vals) cdfvals = cdf(vals, *args) - D1 = amax(abs(cdfvals - arange(1.0,N+1)/N)) + D1 = np.absolute(cdfvals - np.arange(1.0, N+1)/N).max() # D2 = amax(abs(cdfvals - arange(0.0,N)/N)) # D = max(D1,D2) D = D1 @@ -1730,9 +1727,9 @@ f_obs = asarray(f_obs) k = len(f_obs) if f_exp is None: - f_exp = array([sum(f_obs,axis=0)/float(k)] * len(f_obs),Float) - f_exp = f_exp.astype(Float) - chisq = add.reduce((f_obs-f_exp)**2 / f_exp) + f_exp = array([sum(f_obs,axis=0)/float(k)] * len(f_obs),float) + f_exp = f_exp.astype(float) + chisq = np.add.reduce((f_obs-f_exp)**2 / f_exp) return chisq, chisqprob(chisq, k-1) @@ -1747,15 +1744,15 @@ data1, data2 = map(asarray, (data1, data2)) j1 = 0 # zeros(data1.shape[1:]) TRIED TO MAKE THIS UFUNC-LIKE j2 = 0 # zeros(data2.shape[1:]) - fn1 = 0.0 # zeros(data1.shape[1:],Float) - fn2 = 0.0 # zeros(data2.shape[1:],Float) + fn1 = 0.0 # zeros(data1.shape[1:],float) + fn2 = 0.0 # zeros(data2.shape[1:],float) n1 = data1.shape[0] n2 = data2.shape[0] en1 = n1*1 en2 = n2*1 - d = zeros(data1.shape[1:],Float) - data1 = sort(data1,0) - data2 = sort(data2,0) + d = zeros(data1.shape[1:]) + data1 = np.sort(data1,0) + data2 = np.sort(data2,0) while j1 < n1 and j2 < n2: d1=data1[j1] d2=data2[j2] @@ -1769,8 +1766,8 @@ if abs(dt) > abs(d): d = dt try: - en = math.sqrt(en1*en2/float(en1+en2)) - prob = ksprob((en+0.12+0.11/en)*fabs(d)) + en = np.sqrt(en1*en2/float(en1+en2)) + prob = ksprob((en+0.12+0.11/en)*np.fabs(d)) except: prob = 1.0 return d, prob @@ -1790,17 +1787,17 @@ y = asarray(y) n1 = len(x) n2 = len(y) - ranked = rankdata(concatenate((x,y))) + ranked = rankdata(np.concatenate((x,y))) rankx = ranked[0:n1] # get the x-ranks #ranky = ranked[n1:] # the rest are y-ranks u1 = n1*n2 + (n1*(n1+1))/2.0 - sum(rankx,axis=0) # calc U for x u2 = n1*n2 - u1 # remainder is U for y bigu = max(u1,u2) smallu = min(u1,u2) - T = math.sqrt(tiecorrect(ranked)) # correction factor for tied scores + T = np.sqrt(tiecorrect(ranked)) # correction factor for tied scores if T == 0: raise ValueError, 'All numbers are identical in amannwhitneyu' - sd = math.sqrt(T*n1*n2*(n1+n2+1)/12.0) + sd = np.sqrt(T*n1*n2*(n1+n2+1)/12.0) z = abs((bigu-n1*n2/2.0) / sd) # normal approximation for prob calc return smallu, 1.0 - zprob(z) @@ -1837,16 +1834,16 @@ Returns: z-statistic, two-tailed p-value """ - x,y = map(asarray, (x, y)) + x,y = map(np.asarray, (x, y)) n1 = len(x) n2 = len(y) - alldata = concatenate((x,y)) + alldata = np.concatenate((x,y)) ranked = rankdata(alldata) x = ranked[:n1] y = ranked[n1:] s = sum(x,axis=0) expected = n1*(n1+n2+1) / 2.0 - z = (s - expected) / math.sqrt(n1*n2*(n1+n2+1)/12.0) + z = (s - expected) / np.sqrt(n1*n2*(n1+n2+1)/12.0) prob = 2*(1.0 -zprob(abs(z))) return z, prob @@ -1902,7 +1899,7 @@ raise ValueError, '\nLess than 3 levels. Friedman test not appropriate.\n' n = len(args[0]) data = apply(_support.abut,args) - data = data.astype(Float) + data = data.astype(float) for i in range(len(data)): data[i] = rankdata(data[i]) ssbn = sum(sum(args,1)**2,axis=0) @@ -1984,18 +1981,18 @@ p = _support.unique(para) x = zeros((n,len(p))) # design matrix for l in range(len(p)): - x[:,l] = equal(para,p[l]) + x[:,l] = para == p[l] # fixme: normal equations are bad. Use linalg.lstsq instead. - b = dot(dot(linalg.inv(dot(transpose(x),x)), # i.e., b=inv(X'X)X'Y - transpose(x)),data) + b = dot(dot(linalg.inv(dot(np.transpose(x),x)), # i.e., b=inv(X'X)X'Y + np.transpose(x)),data) diffs = (data - dot(x,b)) - s_sq = 1./(n-len(p)) * dot(transpose(diffs), diffs) + s_sq = 1./(n-len(p)) * dot(np.transpose(diffs), diffs) if len(p) == 2: # ttest_ind c = array([1,-1]) df = n-2 fact = sum(1.0/sum(x,0),axis=0) # i.e., 1/n1 + 1/n2 + 1/n3 ... - t = dot(c,b) / sqrt(s_sq*fact) + t = dot(c,b) / np.sqrt(s_sq*fact) probs = betai(0.5*df,0.5,float(df)/(df+t*t)) return t, probs @@ -2014,7 +2011,7 @@ if (a-1)**2 + (b-1)**2 == 5: q = 1 else: - q = math.sqrt( ((a-1)**2*(b-1)**2 - 2) / ((a-1)**2 + (b-1)**2 -5) ) + q = np.sqrt( ((a-1)**2*(b-1)**2 - 2) / ((a-1)**2 + (b-1)**2 -5) ) n_um = (1 - lmbda**(1.0/q))*(a-1)*(b-1) d_en = lmbda**(1.0/q) / (n_um*q - 0.5*(a-1)*(b-1) + 1) return n_um / d_en @@ -2078,7 +2075,7 @@ """ a, axis = _chk_asarray(a, axis) s = sum(a,axis) - if not isscalar(s): + if not np.isscalar(s): return s.astype(float)*s else: return float(s)*s Modified: trunk/Lib/stats/tests/test_stats.py =================================================================== --- trunk/Lib/stats/tests/test_stats.py 2006-09-24 05:49:03 UTC (rev 2224) +++ trunk/Lib/stats/tests/test_stats.py 2006-09-24 06:50:14 UTC (rev 2225) @@ -109,10 +109,11 @@ def check_meanX(self): y = scipy.stats.mean(X) - assert_almost_equal(y,5.0) + assert_almost_equal(y, 5.0) + def check_stdX(self): y = scipy.stats.std(X) - assert_almost_equal(y,2.738612788) + assert_almost_equal(y, 2.738612788) def check_tmeanX(self): y = scipy.stats.tmean(X, (2, 8), (True, True)) @@ -128,60 +129,60 @@ def check_meanZERO(self): y = scipy.stats.mean(ZERO) - assert_almost_equal(y,0.0) + assert_almost_equal(y, 0.0) def check_stdZERO(self): y = scipy.stats.std(ZERO) - assert_almost_equal(y,0.0) + assert_almost_equal(y, 0.0) ## Really need to write these tests to handle missing values properly ## def check_meanMISS(self): ## y = scipy.stats.mean(MISS) -## assert_almost_equal(y,0.0) +## assert_almost_equal(y, 0.0) ## ## def check_stdMISS(self): ## y = scipy.stats.stdev(MISS) -## assert_almost_equal(y,0.0) +## assert_almost_equal(y, 0.0) def check_meanBIG(self): y = scipy.stats.mean(BIG) - assert_almost_equal(y,99999995.00) + assert_almost_equal(y, 99999995.00) def check_stdBIG(self): y = scipy.stats.std(BIG) - assert_almost_equal(y,2.738612788) + assert_almost_equal(y, 2.738612788) def check_meanLITTLE(self): y = scipy.stats.mean(LITTLE) - assert_approx_equal(y,0.999999950) + assert_approx_equal(y, 0.999999950) def check_stdLITTLE(self): y = scipy.stats.std(LITTLE) - assert_approx_equal(y,2.738612788e-8) + assert_approx_equal(y, 2.738612788e-8) def check_meanHUGE(self): y = scipy.stats.mean(HUGE) - assert_approx_equal(y,5.00000e+12) + assert_approx_equal(y, 5.00000e+12) def check_stdHUGE(self): y = scipy.stats.std(HUGE) - assert_approx_equal(y,2.738612788e12) + assert_approx_equal(y, 2.738612788e12) def check_meanTINY(self): y = scipy.stats.mean(TINY) - assert_almost_equal(y,0.0) + assert_almost_equal(y, 0.0) def check_stdTINY(self): y = scipy.stats.std(TINY) - assert_almost_equal(y,0.0) + assert_almost_equal(y, 0.0) def check_meanROUND(self): y = scipy.stats.mean(ROUND) - assert_approx_equal(y,4.500000000) + assert_approx_equal(y, 4.500000000) def check_stdROUND(self): y = scipy.stats.std(ROUND) - assert_approx_equal(y,2.738612788) + assert_approx_equal(y, 2.738612788) class test_corr(ScipyTestCase): """ W.II.D. Compute a correlation matrix on all the variables. @@ -425,77 +426,6 @@ assert_almost_equal(intercept,0.0) assert_almost_equal(r,0.0) -anovadata = ([[1,'A1','B1',2], - [2,'A1','B1',1], - [3,'A1','B1',3], - [4,'A1','B1',2], - [5,'A1','B2',3], - [6,'A1','B2',4], - [7,'A1','B2',5], - [8,'A2','B1',4], - [9,'A2','B1',6], - [10,'A2','B1',5], - [11,'A1','B2',2], - [12,'A1','B2',4], - [13,'A1','B2',5]]) - -##class test_anova(ScipyTestCase): -## """ W.V.A. Simple contrasts. -## -## The following data contain an unbalanced design with a significant -## interaction. A least squares analysis shows the main efefct for A -## is not significant, but this test is not particularlrly meaningful -## because of the interaction. Test, therefore, the simple constrast -## between A1 and A2 within B1. Then test A1 vs. A2 within B2. Both -## tests should use the same residual error term (separate t-tests are -## unacceptable). Several widely used mainframe programs fail this -## test. Unless the program can constrast any terms in a model (not -## just main effects), it cannot handle this frequently encountered -## type of problem. -## B1 B2 -## --------------------- -## | 2 | 3 | -## A1 | 1 | 4 | -## | 3 | 5 | -## | 2 | | -## --------------------- -## | 4 | 2 | -## A2 | 6 | 4 | -## | 5 | 5 | -## --------------------- -## The numbers in Wilkinson's statistic quiz are from SYSTAT output, -## not certified to be the correct values. -## """ -## y=scipy.stats.anova(anovadata) -## for i in [0,len(y)]: -## if (y[i][0]=='A'): -## assert_approx_equal(y[i][1],5.689,3) -## """ Sum Squares - A""" -## assert_equal(y[i][2],1) -## """ Degrees Freedom - A """ -## assert_approx_equal(y[i][3],5.689,3) -## """ Mean Squares - A""" -## assert_approx_equal(y[i][4],4.800,3) -## """ F ratio - A""" -## if (y[i][0]=='B'): -## assert_approx_equal(y[i][1],0.356,2) -## """ Sum Squares - B """ -## assert_equal(y[i][2],1) -## """ Degrees Freedom - B """ -## assert_approx_equal(y[i][3],0.356,2) -## """ Mean Squares - B """ -## assert_approx_equal(y[i][4],0.300,2) -## """ F ratio - B """ -## if (y[i][0]=='AB'): -## assert_approx_equal(y[i][1],8.889,3) -## """ Sum Squares - AB """ -## assert_equal(y[i][2],1) -## """ Degrees Freedom - AB """ -## assert_approx_equal(y[i][3],8.889,3) -## """ Mean Squares - AB """ -## assert_approx_equal(y[i][4],7.500,3) -## """ F ratio - AB """ - # Utility def compare_results(res,desired): From scipy-svn at scipy.org Sun Sep 24 03:10:51 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 02:10:51 -0500 (CDT) Subject: [Scipy-svn] r2226 - in trunk/Lib: integrate linalg sandbox/stats signal tests Message-ID: <20060924071051.B051639C023@new.scipy.org> Author: oliphant Date: 2006-09-24 02:09:58 -0500 (Sun, 24 Sep 2006) New Revision: 2226 Modified: trunk/Lib/integrate/quadrature.py trunk/Lib/linalg/decomp.py trunk/Lib/sandbox/stats/anova.py trunk/Lib/signal/filter_design.py trunk/Lib/signal/ltisys.py trunk/Lib/tests/test_handy.py Log: Eliminate use of c_ (use r_['-1', instead). Fix missing line-feed in quadrature.py Modified: trunk/Lib/integrate/quadrature.py =================================================================== --- trunk/Lib/integrate/quadrature.py 2006-09-24 06:50:14 UTC (rev 2225) +++ trunk/Lib/integrate/quadrature.py 2006-09-24 07:09:58 UTC (rev 2226) @@ -41,7 +41,8 @@ """ [x,w] = p_roots(n) x = real(x) - ainf, binf = map(isinf,(a,b)) if ainf or binf: + ainf, binf = map(isinf,(a,b)) + if ainf or binf: raise ValueError, "Gaussian quadrature is only available for " \ "finite limits." y = (b-a)*(x+1)/2.0 + a Modified: trunk/Lib/linalg/decomp.py =================================================================== --- trunk/Lib/linalg/decomp.py 2006-09-24 06:50:14 UTC (rev 2225) +++ trunk/Lib/linalg/decomp.py 2006-09-24 07:09:58 UTC (rev 2226) @@ -27,7 +27,6 @@ cast = numpy.cast r_ = numpy.r_ -c_ = numpy.c_ _I = cast['F'](1j) def _make_complex_eigvecs(w,vin,cmplx_tcode): @@ -521,7 +520,7 @@ typ = part.dtype.char MorN = len(s) if MorN == M: - return c_[part,zeros((M,N-M),typ)] + return r_['-1',part,zeros((M,N-M),typ)] elif MorN == N: return r_[part,zeros((M-N,N),typ)] else: Modified: trunk/Lib/sandbox/stats/anova.py =================================================================== --- trunk/Lib/sandbox/stats/anova.py 2006-09-24 06:50:14 UTC (rev 2225) +++ trunk/Lib/sandbox/stats/anova.py 2006-09-24 07:09:58 UTC (rev 2226) @@ -356,7 +356,7 @@ ## Calc grand average (ga,axis=0), used for ALL effects ga = sum((sourceMarray*sourceNarray)/ - sum(sourceNarray),axis=0),axis=0)) + sum(sourceNarray,axis=0),axis=0) ga = reshape(ga,ones(len(Marray.shape))) ## If GRAND interaction, use harmonic mean of ALL cell Ns Modified: trunk/Lib/signal/filter_design.py =================================================================== --- trunk/Lib/signal/filter_design.py 2006-09-24 06:50:14 UTC (rev 2225) +++ trunk/Lib/signal/filter_design.py 2006-09-24 07:09:58 UTC (rev 2226) @@ -6,7 +6,7 @@ import numpy from numpy.core.umath import * from numpy import atleast_1d, poly, polyval, roots, imag, real, asarray,\ - allclose, resize, pi, concatenate, absolute, logspace, c_ + allclose, resize, pi, concatenate, absolute, logspace from numpy import mintypecode, select from scipy import special, optimize, linalg from scipy.misc import comb @@ -22,7 +22,7 @@ if len(ep) == 0: ep = atleast_1d(-1000)+0j - ez = c_[numpy.compress(ep.imag >=0, ep,axis=-1), numpy.compress((abs(tz) < 1e5) & (tz.imag >=0),tz,axis=-1)] + ez = r_['-1',numpy.compress(ep.imag >=0, ep,axis=-1), numpy.compress((abs(tz) < 1e5) & (tz.imag >=0),tz,axis=-1)] integ = abs(ez) < 1e-10 hfreq = numpy.around(numpy.log10(numpy.max(3*abs(ez.real + integ)+1.5*ez.imag))+0.5) Modified: trunk/Lib/signal/ltisys.py =================================================================== --- trunk/Lib/signal/ltisys.py 2006-09-24 06:50:14 UTC (rev 2225) +++ trunk/Lib/signal/ltisys.py 2006-09-24 07:09:58 UTC (rev 2226) @@ -12,7 +12,7 @@ import scipy.interpolate as interpolate import scipy.integrate as integrate import scipy.linalg as linalg -from numpy import r_, c_, eye, real, atleast_1d, atleast_2d, poly, \ +from numpy import r_, eye, real, atleast_1d, atleast_2d, poly, \ squeeze, diag, asarray def tf2ss(num, den): @@ -46,7 +46,7 @@ array([], Float) # pad numerator to have same number of columns has denominator - num = c_[zeros((num.shape[0],K-M),num.dtype.char), num] + num = r_['-1',zeros((num.shape[0],K-M),num.dtype.char), num] if num.shape[-1] > 0: D = num[:,0] Modified: trunk/Lib/tests/test_handy.py =================================================================== --- trunk/Lib/tests/test_handy.py 2006-09-24 06:50:14 UTC (rev 2225) +++ trunk/Lib/tests/test_handy.py 2006-09-24 07:09:58 UTC (rev 2226) @@ -41,13 +41,13 @@ b = ones(5) c = r_[b,0,0,b] assert_array_equal(c,[1,1,1,1,1,0,0,1,1,1,1,1]) - c = c_[b,0,0,b] + c = r_['-1',b,0,0,b] assert_array_equal(c,[1,1,1,1,1,0,0,1,1,1,1,1]) def check_2d(self): b = rand(5,5) c = rand(5,5) - d = c_[b,c] # append columns + d = r_['-1',b,c] # append columns assert(d.shape == (5,10)) assert_array_equal(d[:,:5],b) assert_array_equal(d[:,5:],c) From scipy-svn at scipy.org Sun Sep 24 03:11:24 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 02:11:24 -0500 (CDT) Subject: [Scipy-svn] r2227 - trunk/Lib/signal Message-ID: <20060924071124.60C9F39C023@new.scipy.org> Author: rkern Date: 2006-09-24 02:11:23 -0500 (Sun, 24 Sep 2006) New Revision: 2227 Modified: trunk/Lib/signal/waveforms.py Log: Fix #193. Modified: trunk/Lib/signal/waveforms.py =================================================================== --- trunk/Lib/signal/waveforms.py 2006-09-24 07:09:58 UTC (rev 2226) +++ trunk/Lib/signal/waveforms.py 2006-09-24 07:11:23 UTC (rev 2227) @@ -1,5 +1,3 @@ -## Automatically adapted for scipy Oct 21, 2005 by convertcode.py - # Author: Travis Oliphant # 2003 @@ -143,16 +141,22 @@ t -- array to evaluate waveform at f0, f1, t1 -- frequency (in Hz) of waveform is f0 at t=0 and f1 at t=t1 + Alternatively, if f0 is an array, then it forms the coefficients of + a polynomial (c.f. numpy.polval()) in t. The values in f1, t1, + method, and qshape are ignored. method -- linear, quadratic, or logarithmic frequency sweep - phi -- optional phase + phi -- optional phase in degrees qshape -- shape parameter for quadratic curve: concave or convex """ - phi /= 360 - if size(f0) > 1: # Polynomial type + + # Convert to radians. + phi *= pi / 180 + if size(f0) > 1: + # We were given a polynomial. return cos(2*pi*polyval(polyint(f0),t)+phi) if method in ['linear','lin','li']: beta = (f1-f0)/t1 - f = f0+beta*t + phase_angle = 2*pi * (f0*t + 0.5*beta*t*t) elif method in ['quadratic','quad','q']: if qshape == 'concave': mxf = max(f0,f1) @@ -162,14 +166,20 @@ mxf = max(f0,f1) mnf = min(f0,f1) f1,f0 = mnf, mxf + else: + raise ValueError("qshape must be either 'concave' or 'convex' but " + "a value of %r was given." % qshape) beta = (f1-f0)/t1/t1 - f = f0+beta*t*t + phase_angle = 2*pi * (f0*t + beta*t*t*t/3) elif method in ['logarithmic','log','lo']: if f1 <= f0: - raise ValueError, \ - "For a logarithmic sweep, f1=%f must be larger than f0=%f." \ - % (f1, f0) + raise ValueError( + "For a logarithmic sweep, f1=%f must be larger than f0=%f." + % (f1, f0)) beta = log10(f1-f0)/t1 - f = f0+pow(10,beta*t) + phase_angle = 2*pi * (f0*t + pow(10,beta*t)/(beta*log(10))) + else: + raise ValueError("method must be 'linear', 'quadratic', or " + "'logarithmic' but a value of %r was given." % method) - return cos(2*pi*f*t+phi) + return cos(phase_angle + phi) From scipy-svn at scipy.org Sun Sep 24 03:57:54 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 02:57:54 -0500 (CDT) Subject: [Scipy-svn] r2228 - in trunk/Lib/linalg: . tests Message-ID: <20060924075754.A76BA39C1A1@new.scipy.org> Author: rkern Date: 2006-09-24 02:57:51 -0500 (Sun, 24 Sep 2006) New Revision: 2228 Modified: trunk/Lib/linalg/decomp.py trunk/Lib/linalg/generic_flapack.pyf trunk/Lib/linalg/tests/test_decomp.py Log: Add 'economy' QR decomposition from patch in #220 Modified: trunk/Lib/linalg/decomp.py =================================================================== --- trunk/Lib/linalg/decomp.py 2006-09-24 07:11:23 UTC (rev 2227) +++ trunk/Lib/linalg/decomp.py 2006-09-24 07:57:51 UTC (rev 2228) @@ -6,10 +6,10 @@ # additions by Travis Oliphant, March 2002 # additions by Eric Jones, June 2002 # additions by Johannes Loehnert, June 2006 +# additions by Bart Vandereycken, June 2006 - __all__ = ['eig','eigh','eig_banded','eigvals','eigvalsh', 'eigvals_banded', - 'lu','svd','svdvals','diagsvd','cholesky','qr', + 'lu','svd','svdvals','diagsvd','cholesky','qr','qr_old', 'schur','rsf2csf','lu_factor','cho_factor','cho_solve','orth', 'hessenberg'] @@ -17,7 +17,7 @@ import basic from warnings import warn -from lapack import get_lapack_funcs +from lapack import get_lapack_funcs, find_best_lapack_type from blas import get_blas_funcs from flinalg import get_flinalg_funcs from scipy.linalg import calc_lwork @@ -581,8 +581,90 @@ raise TypeError, msg return b +def qr(a,overwrite_a=0,lwork=None,econ=False,mode='qr'): + """QR decomposition of an M x N matrix a. -def qr(a,overwrite_a=0,lwork=None): + Description: + + Find a unitary matrix, q, and an upper-trapezoidal matrix r + such that q * r = a + + Inputs: + + a -- the matrix + overwrite_a=0 -- if non-zero then discard the contents of a, + i.e. a is used as a work array if possible. + + lwork=None -- >= shape(a)[1]. If None (or -1) compute optimal + work array size. + econ=False -- computes the skinny or economy-size QR decomposition + only useful when M>N + mode='qr' -- if 'qr' then return both q and r; if 'r' then just return r + + Outputs: + q,r - if mode=='qr' + r - if mode=='r' + + """ + a1 = asarray_chkfinite(a) + if len(a1.shape) != 2: + raise ValueError("expected 2D array") + M, N = a1.shape + overwrite_a = overwrite_a or (_datanotshared(a1,a)) + + geqrf, = get_lapack_funcs(('geqrf',),(a1,)) + if lwork is None or lwork == -1: + # get optimal work array + qr,tau,work,info = geqrf(a1,lwork=-1,overwrite_a=1) + lwork = work[0] + + qr,tau,work,info = geqrf(a1,lwork=lwork,overwrite_a=overwrite_a) + if info<0: + raise ValueError("illegal value in %-th argument of internal geqrf" + % -info) + + if not econ or Mgeqrf + subroutine orgqr(m,n,k,a,tau,work,lwork,info) + + ! q,work,info = orgqr(a,lwork=3*n,overwrite_a=0) + ! Generates an M-by-N real matrix Q with orthonormal columns, + ! which is defined as the first N columns of a product of K elementary + ! reflectors of order M (e.g. output of geqrf) + + callstatement (*f2py_func)(&m,&n,&k,a,&m,tau,work,&lwork,&info) + callprotoargument int*,int*,int*,*,int*,*,*,int*,int* + + integer intent(hide),depend(a):: m = shape(a,0) + integer intent(hide),depend(a):: n = shape(a,1) + integer intent(hide),depend(tau):: k = shape(tau,0) + dimension(m,n),intent(in,out,copy,out=q) :: a + dimension(k),intent(in) :: tau + + integer optional,intent(in),depend(n),check(lwork>=n||lwork==-1) :: lwork=3*n + dimension(MAX(lwork,1)),intent(out),depend(lwork) :: work + integer intent(out) :: info + end subroutine orgqr + + subroutine ungqr(m,n,k,a,tau,work,lwork,info) + + ! q,work,info = ungqr(a,lwork=3*n,overwrite_a=0) + ! Generates an M-by-N complex matrix Q with unitary columns, + ! which is defined as the first N columns of a product of K elementary + ! reflectors of order M (e.g. output of geqrf) + + callstatement (*f2py_func)(&m,&n,&k,a,&m,tau,work,&lwork,&info) + callprotoargument int*,int*,int*,*,int*,*,*,int*,int* + + integer intent(hide),depend(a):: m = shape(a,0) + integer intent(hide),depend(a):: n = shape(a,1) + integer intent(hide),depend(tau):: k = shape(tau,0) + dimension(m,n),intent(in,out,copy,out=q) :: a + dimension(k),intent(in) :: tau + + integer optional,intent(in),depend(n),check(lwork>=n||lwork==-1) :: lwork=3*n + dimension(MAX(lwork,1)),intent(out),depend(lwork) :: work + integer intent(out) :: info + end subroutine ungqr ++ subroutine geev(compute_vl,compute_vr,n,a,wr,wi,vl,ldvl,vr,ldvr,work,lwork,info) ! wr,wi,vl,vr,info = geev(a,compute_vl=1,compute_vr=1,lwork=4*n,overwrite_a=0) Modified: trunk/Lib/linalg/tests/test_decomp.py =================================================================== --- trunk/Lib/linalg/tests/test_decomp.py 2006-09-24 07:11:23 UTC (rev 2227) +++ trunk/Lib/linalg/tests/test_decomp.py 2006-09-24 07:57:51 UTC (rev 2228) @@ -599,6 +599,28 @@ assert_array_almost_equal(dot(transpose(q),q),identity(3)) assert_array_almost_equal(dot(q,r),a) + def check_simple_trap(self): + a = [[8,2,3],[2,9,3]] + q,r = qr(a) + assert_array_almost_equal(dot(transpose(q),q),identity(2)) + assert_array_almost_equal(dot(q,r),a) + + def check_simple_tall(self): + # full version + a = [[8,2],[2,9],[5,3]] + q,r = qr(a) + assert_array_almost_equal(dot(transpose(q),q),identity(3)) + assert_array_almost_equal(dot(q,r),a) + + def check_simple_tall_e(self): + # economy version + a = [[8,2],[2,9],[5,3]] + q,r = qr(a,econ=True) + assert_array_almost_equal(dot(transpose(q),q),identity(2)) + assert_array_almost_equal(dot(q,r),a) + assert_equal(q.shape, (3,2)) + assert_equal(r.shape, (2,2)) + def check_simple_complex(self): a = [[3,3+4j,5],[5,2,2+7j],[3,2,7]] q,r = qr(a) @@ -613,6 +635,37 @@ assert_array_almost_equal(dot(transpose(q),q),identity(n)) assert_array_almost_equal(dot(q,r),a) + def check_random_tall(self): + # full version + m = 200 + n = 100 + for k in range(2): + a = random([m,n]) + q,r = qr(a) + assert_array_almost_equal(dot(transpose(q),q),identity(m)) + assert_array_almost_equal(dot(q,r),a) + + def check_random_tall_e(self): + # economy version + m = 200 + n = 100 + for k in range(2): + a = random([m,n]) + q,r = qr(a,econ=True) + assert_array_almost_equal(dot(transpose(q),q),identity(n)) + assert_array_almost_equal(dot(q,r),a) + assert_equal(q.shape, (m,n)) + assert_equal(r.shape, (n,n)) + + def check_random_trap(self): + m = 100 + n = 200 + for k in range(2): + a = random([m,n]) + q,r = qr(a) + assert_array_almost_equal(dot(transpose(q),q),identity(m)) + assert_array_almost_equal(dot(q,r),a) + def check_random_complex(self): n = 20 for k in range(2): From scipy-svn at scipy.org Sun Sep 24 04:08:57 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 03:08:57 -0500 (CDT) Subject: [Scipy-svn] r2229 - trunk/Lib/signal Message-ID: <20060924080857.4885839C14C@new.scipy.org> Author: rkern Date: 2006-09-24 03:08:41 -0500 (Sun, 24 Sep 2006) New Revision: 2229 Modified: trunk/Lib/signal/ltisys.py Log: Remove use of typecodes. Modified: trunk/Lib/signal/ltisys.py =================================================================== --- trunk/Lib/signal/ltisys.py 2006-09-24 07:57:51 UTC (rev 2228) +++ trunk/Lib/signal/ltisys.py 2006-09-24 08:08:41 UTC (rev 2229) @@ -1,14 +1,11 @@ -## Automatically adapted for scipy Oct 21, 2005 by convertcode.py - # # Author: Travis Oliphant 2001 # from filter_design import tf2zpk, zpk2tf, normalize import numpy -from numpy import product, zeros, \ - array, dot, transpose, arange, ones, nan_to_num -from numpy.oldnumeric import Float +from numpy import product, zeros, array, dot, transpose, arange, ones, \ + nan_to_num import scipy.interpolate as interpolate import scipy.integrate as integrate import scipy.linalg as linalg @@ -36,25 +33,25 @@ num, den = normalize(num, den) # Strips zeros, checks arrays nn = len(num.shape) if nn == 1: - num = asarray([num],num.dtype.char) + num = asarray([num], num.dtype) M = num.shape[1] K = len(den) if (M > K): raise ValueError, "Improper transfer function." if (M == 0 or K == 0): # Null system - return array([],Float), array([], Float), array([], Float), \ - array([], Float) + return array([],float), array([], float), array([], float), \ + array([], float) # pad numerator to have same number of columns has denominator - num = r_['-1',zeros((num.shape[0],K-M),num.dtype.char), num] + num = r_['-1',zeros((num.shape[0],K-M), num.dtype), num] if num.shape[-1] > 0: D = num[:,0] else: - D = array([],Float) + D = array([],float) if K == 1: - return array([], Float), array([], Float), array([], Float), D + return array([], float), array([], float), array([], float), D frow = -array([den[1:]]) A = r_[frow, eye(K-2, K-1)] @@ -150,7 +147,7 @@ num_states = A.shape[0] type_test = A[:,0] + B[:,0] + C[0,:] + D - num = numpy.zeros((nout, num_states+1),type_test.dtype.char) + num = numpy.zeros((nout, num_states+1), type_test.dtype) for k in range(nout): Ck = atleast_2d(C[k,:]) num[k] = poly(A - dot(B,Ck)) + (D[k]-1)*den @@ -325,7 +322,7 @@ raise ValueError, "System does not define that many inputs." if X0 is None: - X0 = zeros(sys.B.shape[0],sys.A.dtype.char) + X0 = zeros(sys.B.shape[0],sys.A.dtype) # for each output point directly integrate assume zero-order hold # or linear interpolation. @@ -392,9 +389,9 @@ raise ValueError, "System does not define that many inputs." if X0 is None: - X0 = zeros(sys.B.shape[0],sys.A.dtype.char) + X0 = zeros(sys.B.shape[0], sys.A.dtype) - xout = zeros((len(T),sys.B.shape[0]),sys.A.dtype.char) + xout = zeros((len(T),sys.B.shape[0]), sys.A.dtype) xout[0] = X0 A = sys.A AT, BT = transpose(sys.A), transpose(sys.B) @@ -402,10 +399,10 @@ lam, v = linalg.eig(A) vt = transpose(v) vti = linalg.inv(vt) - GT = dot(dot(vti,diag(numpy.exp(dt*lam))),vt).astype(xout.dtype.char) + GT = dot(dot(vti,diag(numpy.exp(dt*lam))),vt).astype(xout.dtype) ATm1 = linalg.inv(AT) ATm2 = dot(ATm1,ATm1) - I = eye(A.shape[0],dtype=A.dtype.char) + I = eye(A.shape[0],dtype=A.dtype) GTmI = GT-I F1T = dot(dot(BT,GTmI),ATm1) if interp: @@ -415,7 +412,7 @@ dt1 = T[k] - T[k-1] if dt1 != dt: dt = dt1 - GT = dot(dot(vti,diag(numpy.exp(dt*lam))),vt).astype(xout.dtype.char) + GT = dot(dot(vti,diag(numpy.exp(dt*lam))),vt).astype(xout.dtype) GTmI = GT-I F1T = dot(dot(BT,GTmI),ATm1) if interp: @@ -460,13 +457,13 @@ vals = linalg.eigvals(sys.A) tc = 1.0/min(abs(real(vals))) T = arange(0,7*tc,7*tc / float(N)) - h = zeros(T.shape, sys.A.dtype.char) + h = zeros(T.shape, sys.A.dtype) s,v = linalg.eig(sys.A) vi = linalg.inv(v) C = sys.C for k in range(len(h)): es = diag(numpy.exp(s*T[k])) - eA = (dot(dot(v,es),vi)).astype(h.dtype.char) + eA = (dot(dot(v,es),vi)).astype(h.dtype) h[k] = squeeze(dot(dot(C,eA),B)) return T, h @@ -497,6 +494,6 @@ vals = linalg.eigvals(sys.A) tc = 1.0/min(abs(real(vals))) T = arange(0,7*tc,7*tc / float(N)) - U = ones(T.shape, sys.A.dtype.char) + U = ones(T.shape, sys.A.dtype) vals = lsim(sys, U, T, X0=X0) return vals[0], vals[1] From scipy-svn at scipy.org Sun Sep 24 04:24:25 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 03:24:25 -0500 (CDT) Subject: [Scipy-svn] r2230 - trunk/Lib/special Message-ID: <20060924082425.EC85239C1AB@new.scipy.org> Author: rkern Date: 2006-09-24 03:24:24 -0500 (Sun, 24 Sep 2006) New Revision: 2230 Modified: trunk/Lib/special/orthogonal.py Log: Use modern numpy idioms. Modified: trunk/Lib/special/orthogonal.py =================================================================== --- trunk/Lib/special/orthogonal.py 2006-09-24 08:08:41 UTC (rev 2229) +++ trunk/Lib/special/orthogonal.py 2006-09-24 08:24:24 UTC (rev 2230) @@ -1,5 +1,3 @@ -## Automatically adapted for scipy Oct 05, 2005 by convertcode.py - #!/usr/bin/env python # # Author: Travis Oliphant 2000 @@ -60,21 +58,25 @@ """ from __future__ import nested_scopes -from numpy import * -from numpy.oldnumeric import take + +# Scipy imports. +import numpy as np +from numpy import all, any, exp, inf, pi, sqrt +from scipy.linalg import eig + +# Local imports. import _cephes as cephes _gam = cephes.gamma -from scipy.linalg import eig def poch(z,m): """Pochhammer symbol (z)_m = (z)(z+1)....(z+m-1) = gamma(z+m)/gamma(z)""" return _gam(z+m) / _gam(z) -class orthopoly1d(poly1d): +class orthopoly1d(np.poly1d): def __init__(self, roots, weights=None, hn=1.0, kn=1.0, wfunc=None, limits=None, monic=0): - poly1d.__init__(self, roots, r=1) + np.poly1d.__init__(self, roots, r=1) equiv_weights = [weights[k] / wfunc(roots[k]) for k in range(len(roots))] - self.__dict__['weights'] = array(zip(roots,weights,equiv_weights)) + self.__dict__['weights'] = np.array(zip(roots,weights,equiv_weights)) self.__dict__['weight_func'] = wfunc self.__dict__['limits'] = limits mu = sqrt(hn) @@ -88,7 +90,7 @@ def gen_roots_and_weights(n,an_func,sqrt_bn_func,mu): """[x,w] = gen_roots_and_weights(n,an_func,sqrt_bn_func,mu) - Returns the roots (x) of an nth order orthogonal polynomail, + Returns the roots (x) of an nth order orthogonal polynomial, and weights (w) to use in appropriate Gaussian quadrature with that orthogonal polynomial. @@ -99,14 +101,16 @@ sqrt_bn_func(n) should return sqrt(B_n) mu ( = h_0 ) is the integral of the weight over the orthogonal interval """ - nn = arange(1.0,n) + nn = np.arange(1.0,n) sqrt_bn = sqrt_bn_func(nn) - an = an_func(concatenate(([0],nn))) - [x,v] = eig((diag(an)+diag(sqrt_bn,1)+diag(sqrt_bn,-1))) + an = an_func(np.concatenate(([0], nn))) + x, v = eig((np.diagflat(an) + + np.diagflat(sqrt_bn,1) + + np.diagflat(sqrt_bn,-1))) answer = [] - sortind = argsort(real(x)) - answer.append(take(x,sortind,axis=0)) - answer.append(take(mu*v[0]**2,sortind,axis=0)) + sortind = x.real.argsort() + answer.append(x[sortind]) + answer.append((mu*v[0]**2)[sortind]) return answer # Jacobi Polynomials 1 P^(alpha,beta)_n(x) @@ -118,17 +122,17 @@ function (1-x)**alpha (1+x)**beta with alpha,beta > -1. """ if any(alpha <= -1) or any(beta <= -1): - raise ValueError, "alpha and beta must be greater than -1." + raise ValueError("alpha and beta must be greater than -1.") assert(n>0), "n must be positive." (p,q) = (alpha,beta) # from recurrence relations sbn_J = lambda k: 2.0/(2.0*k+p+q)*sqrt((k+p)*(k+q)/(2*k+q+p+1)) * \ - (where(k==1,1.0,sqrt(k*(k+p+q)/(2.0*k+p+q-1)))) + (np.where(k==1,1.0,sqrt(k*(k+p+q)/(2.0*k+p+q-1)))) if any(p == q): # XXX any or all??? an_J = lambda k: 0.0*k else: - an_J = lambda k: where(k==0,(q-p)/(p+q+2.0), + an_J = lambda k: np.where(k==0,(q-p)/(p+q+2.0), (q*q - p*p)/((2.0*k+p+q)*(2.0*k+p+q+2))) g = cephes.gamma mu0 = 2.0**(p+q+1)*g(p+1)*g(q+1)/(g(p+q+2)) @@ -145,7 +149,8 @@ """ assert(n>=0), "n must be nonnegative" wfunc = lambda x: (1-x)**alpha * (1+x)**beta - if n==0: return orthopoly1d([],[],1.0,1.0,wfunc,(-1,1),monic) + if n==0: + return orthopoly1d([],[],1.0,1.0,wfunc,(-1,1),monic) x,w,mu = j_roots(n,alpha,beta,mu=1) ab1 = alpha+beta+1.0 hn = 2**ab1/(2*n+ab1)*_gam(n+alpha+1) @@ -164,17 +169,17 @@ function (1-x)**(p-q) x**(q-1) with p-q > -1 and q > 0. """ # from recurrence relation - if not ( any( (p1 - q1) > -1 ) and any( q1 > 0 ) ): - raise ValueError, "(p - q) > -1 and q > 0 please." + if not ( any((p1 - q1) > -1) and any(q1 > 0) ): + raise ValueError("(p - q) > -1 and q > 0 please.") if (n <= 0): - raise ValueError, "n must be positive." + raise ValueError("n must be positive.") p,q = p1,q1 - sbn_Js = lambda k: sqrt(where(k==1,q*(p-q+1.0)/(p+2.0), \ + sbn_Js = lambda k: sqrt(np.where(k==1,q*(p-q+1.0)/(p+2.0), \ k*(k+q-1.0)*(k+p-1.0)*(k+p-q) \ / ((2.0*k+p-2) * (2.0*k+p))))/(2*k+p-1.0) - an_Js = lambda k: where(k==0,q/(p+1.0),(2.0*k*(k+p)+q*(p-1.0)) / ((2.0*k+p+1.0)*(2*k+p-1.0))) + an_Js = lambda k: np.where(k==0,q/(p+1.0),(2.0*k*(k+p)+q*(p-1.0)) / ((2.0*k+p+1.0)*(2*k+p-1.0))) # could also use definition # Gn(p,q,x) = constant_n * P^(p-q,q-1)_n(2x-1) @@ -201,9 +206,10 @@ (1-x)**(p-q) (x)**(q-1) with p>q-1 and q > 0. """ if (n<0): - raise ValueError, "n must be nonnegative" + raise ValueError("n must be nonnegative") wfunc = lambda x: (1.0-x)**(p-q) * (x)**(q-1.) - if n==0: return orthopoly1d([],[],1.0,1.0,wfunc,(-1,1),monic) + if n==0: + return orthopoly1d([],[],1.0,1.0,wfunc,(-1,1),monic) n1 = n x,w,mu0 = js_roots(n1,p,q,mu=1) hn = _gam(n+1)*_gam(n+q)*_gam(n+p)*_gam(n+p-q+1) @@ -222,7 +228,7 @@ [0,inf] with weighting function exp(-x) x**alpha with alpha > -1. """ if not all(alpha > -1): - raise ValueError, "alpha > -1" + raise ValueError("alpha > -1") assert(n>0), "n must be positive." (p,q) = (alpha,0.0) sbn_La = lambda k: -sqrt(k*(k + p)) # from recurrence relation @@ -240,7 +246,7 @@ exp(-x) x**alpha with alpha > -1 """ if any(alpha <= -1): - raise ValueError, "alpha must be > -1" + raise ValueError("alpha must be > -1") assert(n>=0), "n must be nonnegative" if n==0: n1 = n+1 else: n1 = n @@ -378,7 +384,7 @@ """ assert(n>0), "n must be positive." # from recurrence relation - sbn_J = lambda k: where(k==1,sqrt(2)/2.0,0.5) + sbn_J = lambda k: np.where(k==1,sqrt(2)/2.0,0.5) an_J = lambda k: 0.0*k g = cephes.gamma mu0 = pi @@ -394,7 +400,8 @@ """ assert(n>=0), "n must be nonnegative" wfunc = lambda x: 1.0/sqrt(1-x*x) - if n==0: return orthopoly1d([],[],pi,1.0,wfunc,(-1,1),monic) + if n==0: + return orthopoly1d([],[],pi,1.0,wfunc,(-1,1),monic) n1 = n x,w,mu = t_roots(n1,mu=1) hn = pi/2 From scipy-svn at scipy.org Sun Sep 24 04:29:00 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 03:29:00 -0500 (CDT) Subject: [Scipy-svn] r2231 - trunk/Lib/stats/tests Message-ID: <20060924082900.0457F39C1AB@new.scipy.org> Author: rkern Date: 2006-09-24 03:28:59 -0500 (Sun, 24 Sep 2006) New Revision: 2231 Modified: trunk/Lib/stats/tests/test_distributions.py Log: Use modern numpy idioms. Modified: trunk/Lib/stats/tests/test_distributions.py =================================================================== --- trunk/Lib/stats/tests/test_distributions.py 2006-09-24 08:24:24 UTC (rev 2230) +++ trunk/Lib/stats/tests/test_distributions.py 2006-09-24 08:28:59 UTC (rev 2231) @@ -6,7 +6,7 @@ from numpy.testing import * set_package_path() -import numpy.oldnumeric as numpy +import numpy from numpy import typecodes import stats restore_path() From scipy-svn at scipy.org Sun Sep 24 05:05:17 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 04:05:17 -0500 (CDT) Subject: [Scipy-svn] r2232 - in trunk/Lib/ndimage: . tests Message-ID: <20060924090517.F262D39C04E@new.scipy.org> Author: rkern Date: 2006-09-24 04:05:13 -0500 (Sun, 24 Sep 2006) New Revision: 2232 Modified: trunk/Lib/ndimage/_ni_support.py trunk/Lib/ndimage/filters.py trunk/Lib/ndimage/fourier.py trunk/Lib/ndimage/interpolation.py trunk/Lib/ndimage/measurements.py trunk/Lib/ndimage/morphology.py trunk/Lib/ndimage/tests/test_ndimage.py Log: Drop use of oldnumeric. Modified: trunk/Lib/ndimage/_ni_support.py =================================================================== --- trunk/Lib/ndimage/_ni_support.py 2006-09-24 08:28:59 UTC (rev 2231) +++ trunk/Lib/ndimage/_ni_support.py 2006-09-24 09:05:13 UTC (rev 2232) @@ -29,7 +29,7 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import types -import numpy.oldnumeric as numarray +import numpy def _extend_mode_to_code(mode): """Convert an extension mode to the corresponding integer code. @@ -75,14 +75,14 @@ if shape is None: shape = input.shape if output is None: - output = numarray.zeros(shape, dtype = input.dtype) + output = numpy.zeros(shape, dtype = input.dtype) return_value = output - elif type(output) in [type(types.TypeType), type(numarray.zeros((4,)).dtype)]: - output = numarray.zeros(shape, dtype = output) + elif type(output) in [type(types.TypeType), type(numpy.zeros((4,)).dtype)]: + output = numpy.zeros(shape, dtype = output) return_value = output elif type(output) is types.StringType: - output = numarray.typeDict[output] - output = numarray.zeros(shape, dtype = output) + output = numpy.typeDict[output] + output = numpy.zeros(shape, dtype = output) return_value = output else: if output.shape != shape: Modified: trunk/Lib/ndimage/filters.py =================================================================== --- trunk/Lib/ndimage/filters.py 2006-09-24 08:28:59 UTC (rev 2231) +++ trunk/Lib/ndimage/filters.py 2006-09-24 09:05:13 UTC (rev 2232) @@ -29,7 +29,7 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import math -import numpy.oldnumeric as numarray +import numpy import _ni_support import _nd_image @@ -40,11 +40,11 @@ The lines of the array along the given axis are correlated with the given weights. The weights parameter must be a one-dimensional sequence of numbers.""" - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' output, return_value = _ni_support._get_output(output, input) - weights = numarray.asarray(weights, dtype = numarray.Float64) + weights = numpy.asarray(weights, dtype = numpy.float64) if weights.ndim != 1 or weights.shape[0] < 1: raise RuntimeError, 'no filter weights given' if not weights.flags.contiguous: @@ -142,7 +142,7 @@ because intermediate results may be stored with insufficient precision. """ - input = numarray.asarray(input) + input = numpy.asarray(input) output, return_value = _ni_support._get_output(output, input) orders = _ni_support._normalize_sequence(order, input.ndim) sigmas = _ni_support._normalize_sequence(sigma, input.ndim) @@ -161,7 +161,7 @@ def prewitt(input, axis = -1, output = None, mode = "reflect", cval = 0.0): """Calculate a Prewitt filter. """ - input = numarray.asarray(input) + input = numpy.asarray(input) axis = _ni_support._check_axis(axis, input.ndim) output, return_value = _ni_support._get_output(output, input) correlate1d(input, [-1, 0, 1], axis, output, mode, cval, 0) @@ -173,7 +173,7 @@ def sobel(input, axis = -1, output = None, mode = "reflect", cval = 0.0): """Calculate a Sobel filter. """ - input = numarray.asarray(input) + input = numpy.asarray(input) axis = _ni_support._check_axis(axis, input.ndim) output, return_value = _ni_support._get_output(output, input) correlate1d(input, [-1, 0, 1], axis, output, mode, cval, 0) @@ -197,7 +197,7 @@ extra arguments and keywords that are passed to derivative2 at each call. """ - input = numarray.asarray(input) + input = numpy.asarray(input) output, return_value = _ni_support._get_output(output, input) axes = range(input.ndim) if len(axes) > 0: @@ -228,7 +228,7 @@ axis as a sequence, or as a single number, in which case it is equal for all axes.. """ - input = numarray.asarray(input) + input = numpy.asarray(input) def derivative2(input, axis, output, mode, cval, sigma): order = [0] * input.ndim order[axis] = 2 @@ -252,19 +252,19 @@ extra arguments and keywords that are passed to derivative2 at each call. """ - input = numarray.asarray(input) + input = numpy.asarray(input) output, return_value = _ni_support._get_output(output, input) axes = range(input.ndim) if len(axes) > 0: derivative(input, axes[0], output, mode, cval, *extra_arguments, **extra_keywords) - numarray.multiply(output, output, output) + numpy.multiply(output, output, output) for ii in range(1, len(axes)): tmp = derivative(input, axes[ii], output.dtype, mode, cval, *extra_arguments, **extra_keywords) - numarray.multiply(tmp, tmp, tmp) + numpy.multiply(tmp, tmp, tmp) output += tmp - numarray.sqrt(output, output) + numpy.sqrt(output, output) else: output[...] = input[...] return return_value @@ -278,7 +278,7 @@ axis as a sequence, or as a single number, in which case it is equal for all axes.. """ - input = numarray.asarray(input) + input = numpy.asarray(input) def derivative(input, axis, output, mode, cval, sigma): order = [0] * input.ndim order[axis] = 1 @@ -288,11 +288,11 @@ def _correlate_or_convolve(input, weights, output, mode, cval, origin, convolution): - input = numarray.asarray(input) - if numarray.iscomplexobj(int): + input = numpy.asarray(input) + if numpy.iscomplexobj(int): raise TypeError, 'Complex type not supported' origins = _ni_support._normalize_sequence(origin, input.ndim) - weights = numarray.asarray(weights, dtype = numarray.Float64) + weights = numpy.asarray(weights, dtype = numpy.float64) wshape = [ii for ii in weights.shape if ii > 0] if len(wshape) != input.ndim: raise RuntimeError, 'filter weights array has incorrect shape.' @@ -336,8 +336,8 @@ The lines of the array along the given axis are filtered with a uniform filter of given size.""" - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' axis = _ni_support._check_axis(axis, input.ndim) if size < 1: @@ -364,7 +364,7 @@ with a limited precision, the results may be imprecise because intermediate results may be stored with insufficient precision. """ - input = numarray.asarray(input) + input = numpy.asarray(input) output, return_value = _ni_support._get_output(output, input) sizes = _ni_support._normalize_sequence(size, input.ndim) origins = _ni_support._normalize_sequence(origin, input.ndim) @@ -386,8 +386,8 @@ The lines of the array along the given axis are filtered with a minimum filter of given size.""" - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' axis = _ni_support._check_axis(axis, input.ndim) if size < 1: @@ -406,8 +406,8 @@ The lines of the array along the given axis are filtered with a maximum filter of given size.""" - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' axis = _ni_support._check_axis(axis, input.ndim) if size < 1: @@ -428,24 +428,24 @@ raise RuntimeError, "no footprint provided" separable= True else: - footprint = numarray.asarray(footprint) + footprint = numpy.asarray(footprint) footprint = footprint.astype(bool) - if numarray.alltrue(numarray.ravel(footprint),axis=0): + if numpy.alltrue(numpy.ravel(footprint),axis=0): size = footprint.shape footprint = None separable = True else: separable = False else: - structure = numarray.asarray(structure, dtype = numarray.Float64) + structure = numpy.asarray(structure, dtype = numpy.float64) separable = False if footprint is None: - footprint = numarray.ones(structure.shape, bool) + footprint = numpy.ones(structure.shape, bool) else: - footprint = numarray.asarray(footprint) + footprint = numpy.asarray(footprint) footprint = footprint.astype(bool) - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' output, return_value = _ni_support._get_output(output, input) origins = _ni_support._normalize_sequence(origin, input.ndim) @@ -512,17 +512,17 @@ def _rank_filter(input, rank, size = None, footprint = None, output = None, mode = "reflect", cval = 0.0, origin = 0, operation = 'rank'): - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' origins = _ni_support._normalize_sequence(origin, input.ndim) if footprint == None: if size == None: raise RuntimeError, "no footprint or filter size provided" sizes = _ni_support._normalize_sequence(size, input.ndim) - footprint = numarray.ones(sizes, dtype = bool) + footprint = numpy.ones(sizes, dtype = bool) else: - footprint = numarray.asarray(footprint, dtype = bool) + footprint = numpy.asarray(footprint, dtype = bool) fshape = [ii for ii in footprint.shape if ii > 0] if len(fshape) != input.ndim: raise RuntimeError, 'filter footprint array has incorrect shape.' @@ -531,7 +531,7 @@ raise ValueError, 'invalid origin' if not footprint.flags.contiguous: footprint = footprint.copy() - filter_size = numarray.where(footprint, 1, 0).sum() + filter_size = numpy.where(footprint, 1, 0).sum() if operation == 'median': rank = filter_size // 2 elif operation == 'percentile': @@ -617,8 +617,8 @@ the value when mode is equal to 'constant'. The extra_arguments and extra_keywords arguments can be used to pass extra arguments and keywords that are passed to the function at each call.""" - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' output, return_value = _ni_support._get_output(output, input) if filter_size < 1: @@ -648,17 +648,17 @@ value when mode is equal to 'constant'. The extra_arguments and extra_keywords arguments can be used to pass extra arguments and keywords that are passed to the function at each call.""" - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' origins = _ni_support._normalize_sequence(origin, input.ndim) if footprint == None: if size == None: raise RuntimeError, "no footprint or filter size provided" sizes = _ni_support._normalize_sequence(size, input.ndim) - footprint = numarray.ones(size, dtype = bool) + footprint = numpy.ones(size, dtype = bool) else: - footprint = numarray.asarray(footprint) + footprint = numpy.asarray(footprint) footprint = footprint.astype(bool) fshape = [ii for ii in footprint.shape if ii > 0] if len(fshape) != input.ndim: Modified: trunk/Lib/ndimage/fourier.py =================================================================== --- trunk/Lib/ndimage/fourier.py 2006-09-24 08:28:59 UTC (rev 2231) +++ trunk/Lib/ndimage/fourier.py 2006-09-24 09:05:13 UTC (rev 2232) @@ -30,23 +30,23 @@ import types import math -import numpy.oldnumeric as numarray +import numpy import _ni_support import _nd_image def _get_output_fourier(output, input): if output == None: - if input.dtype.type in [numarray.complex64, numarray.complex128, - numarray.float32]: - output = numarray.zeros(input.shape, dtype = input.dtype) + if input.dtype.type in [numpy.complex64, numpy.complex128, + numpy.float32]: + output = numpy.zeros(input.shape, dtype = input.dtype) else: - output = numarray.zeros(input.shape, dtype = numarray.Float64) + output = numpy.zeros(input.shape, dtype = numpy.float64) return_value = output elif type(output) is types.TypeType: - if output not in [numarray.complex64, numarray.complex128, - numarray.float32, numarray.float64]: + if output not in [numpy.complex64, numpy.complex128, + numpy.float32, numpy.float64]: raise RuntimeError, "output type not supported" - output = numarray.zeros(input.shape, dtype = output) + output = numpy.zeros(input.shape, dtype = output) return_value = output else: if output.shape != input.shape: @@ -56,15 +56,15 @@ def _get_output_fourier_complex(output, input): if output == None: - if input.dtype.type in [numarray.complex64, numarray.complex128]: - output = numarray.zeros(input.shape, dtype = input.dtype) + if input.dtype.type in [numpy.complex64, numpy.complex128]: + output = numpy.zeros(input.shape, dtype = input.dtype) else: - output = numarray.zeros(input.shape, dtype = numarray.Complex64) + output = numpy.zeros(input.shape, dtype = numpy.Complex64) return_value = output elif type(output) is types.TypeType: - if output not in [numarray.complex64, numarray.complex128]: + if output not in [numpy.complex64, numpy.complex128]: raise RuntimeError, "output type not supported" - output = numarray.zeros(input.shape, dtype = output) + output = numpy.zeros(input.shape, dtype = output) return_value = output else: if output.shape != input.shape: @@ -83,11 +83,11 @@ direction. The axis of the real transform is given by the axis parameter. """ - input = numarray.asarray(input) + input = numpy.asarray(input) output, return_value = _get_output_fourier(output, input) axis = _ni_support._check_axis(axis, input.ndim) sigmas = _ni_support._normalize_sequence(sigma, input.ndim) - sigmas = numarray.asarray(sigmas, dtype = numarray.Float64) + sigmas = numpy.asarray(sigmas, dtype = numpy.float64) if not sigmas.flags.contiguous: sigmas = sigmas.copy() @@ -105,11 +105,11 @@ direction. The axis of the real transform is given by the axis parameter. """ - input = numarray.asarray(input) + input = numpy.asarray(input) output, return_value = _get_output_fourier(output, input) axis = _ni_support._check_axis(axis, input.ndim) sizes = _ni_support._normalize_sequence(size, input.ndim) - sizes = numarray.asarray(sizes, dtype = numarray.Float64) + sizes = numpy.asarray(sizes, dtype = numpy.float64) if not sizes.flags.contiguous: sizes = sizes.copy() _nd_image.fourier_filter(input, sizes, n, axis, output, 1) @@ -127,11 +127,11 @@ parameter. This function is implemented for arrays of rank 1, 2, or 3. """ - input = numarray.asarray(input) + input = numpy.asarray(input) output, return_value = _get_output_fourier(output, input) axis = _ni_support._check_axis(axis, input.ndim) sizes = _ni_support._normalize_sequence(size, input.ndim) - sizes = numarray.asarray(sizes, dtype = numarray.Float64) + sizes = numpy.asarray(sizes, dtype = numpy.float64) if not sizes.flags.contiguous: sizes = sizes.copy() _nd_image.fourier_filter(input, sizes, n, axis, output, 2) @@ -148,11 +148,11 @@ direction. The axis of the real transform is given by the axis parameter. """ - input = numarray.asarray(input) + input = numpy.asarray(input) output, return_value = _get_output_fourier_complex(output, input) axis = _ni_support._check_axis(axis, input.ndim) shifts = _ni_support._normalize_sequence(shift, input.ndim) - shifts = numarray.asarray(shifts, dtype = numarray.Float64) + shifts = numpy.asarray(shifts, dtype = numpy.float64) if not shifts.flags.contiguous: shifts = shifts.copy() _nd_image.fourier_shift(input, shifts, n, axis, output) Modified: trunk/Lib/ndimage/interpolation.py =================================================================== --- trunk/Lib/ndimage/interpolation.py 2006-09-24 08:28:59 UTC (rev 2231) +++ trunk/Lib/ndimage/interpolation.py 2006-09-24 09:05:13 UTC (rev 2232) @@ -30,12 +30,12 @@ import types import math -import numpy.oldnumeric as numarray +import numpy import _ni_support import _nd_image -def spline_filter1d(input, order = 3, axis = -1, output = numarray.float64, +def spline_filter1d(input, order = 3, axis = -1, output = numpy.float64, output_type = None): """Calculates a one-dimensional spline filter along the given axis. @@ -44,20 +44,20 @@ """ if order < 0 or order > 5: raise RuntimeError, 'spline order not supported' - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' output, return_value = _ni_support._get_output(output, input, output_type) if order in [0, 1]: - output[...] = numarray.array(input) + output[...] = numpy.array(input) else: axis = _ni_support._check_axis(axis, input.ndim) _nd_image.spline_filter1d(input, order, axis, output) return return_value -def spline_filter(input, order = 3, output = numarray.float64, +def spline_filter(input, order = 3, output = numpy.float64, output_type = None): """Multi-dimensional spline filter. @@ -69,8 +69,8 @@ """ if order < 2 or order > 5: raise RuntimeError, 'spline order not supported' - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' output, return_value = _ni_support._get_output(output, input, output_type) @@ -119,8 +119,8 @@ """ if order < 0 or order > 5: raise RuntimeError, 'spline order not supported' - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if output_shape == None: output_shape = input.shape @@ -128,7 +128,7 @@ raise RuntimeError, 'input and output rank must be > 0' mode = _ni_support._extend_mode_to_code(mode) if prefilter and order > 1: - filtered = spline_filter(input, order, output = numarray.float64) + filtered = spline_filter(input, order, output = numpy.float64) else: filtered = input output, return_value = _ni_support._get_output(output, input, @@ -177,11 +177,11 @@ """ if order < 0 or order > 5: raise RuntimeError, 'spline order not supported' - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' - coordinates = numarray.asarray(coordinates) - if numarray.iscomplexobj(coordinates): + coordinates = numpy.asarray(coordinates) + if numpy.iscomplexobj(coordinates): raise TypeError, 'Complex type not supported' output_shape = coordinates.shape[1:] if input.ndim < 1 or len(output_shape) < 1: @@ -190,7 +190,7 @@ raise RuntimeError, 'invalid shape for coordinate array' mode = _ni_support._extend_mode_to_code(mode) if prefilter and order > 1: - filtered = spline_filter(input, order, output = numarray.float64) + filtered = spline_filter(input, order, output = numpy.float64) else: filtered = input output, return_value = _ni_support._get_output(output, input, @@ -222,8 +222,8 @@ """ if order < 0 or order > 5: raise RuntimeError, 'spline order not supported' - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if output_shape == None: output_shape = input.shape @@ -231,12 +231,12 @@ raise RuntimeError, 'input and output rank must be > 0' mode = _ni_support._extend_mode_to_code(mode) if prefilter and order > 1: - filtered = spline_filter(input, order, output = numarray.float64) + filtered = spline_filter(input, order, output = numpy.float64) else: filtered = input output, return_value = _ni_support._get_output(output, input, output_type, shape = output_shape) - matrix = numarray.asarray(matrix, dtype = numarray.float64) + matrix = numpy.asarray(matrix, dtype = numpy.float64) if matrix.ndim not in [1, 2] or matrix.shape[0] < 1: raise RuntimeError, 'no proper affine matrix provided' if matrix.shape[0] != input.ndim: @@ -246,7 +246,7 @@ if not matrix.flags.contiguous: matrix = matrix.copy() offset = _ni_support._normalize_sequence(offset, input.ndim) - offset = numarray.asarray(offset, dtype = numarray.float64) + offset = numpy.asarray(offset, dtype = numpy.float64) if offset.ndim != 1 or offset.shape[0] < 1: raise RuntimeError, 'no proper offset provided' if not offset.flags.contiguous: @@ -272,21 +272,21 @@ """ if order < 0 or order > 5: raise RuntimeError, 'spline order not supported' - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if input.ndim < 1: raise RuntimeError, 'input and output rank must be > 0' mode = _ni_support._extend_mode_to_code(mode) if prefilter and order > 1: - filtered = spline_filter(input, order, output = numarray.float64) + filtered = spline_filter(input, order, output = numpy.float64) else: filtered = input output, return_value = _ni_support._get_output(output, input, output_type) shift = _ni_support._normalize_sequence(shift, input.ndim) shift = [-ii for ii in shift] - shift = numarray.asarray(shift, dtype = numarray.float64) + shift = numpy.asarray(shift, dtype = numpy.float64) if not shift.flags.contiguous: shift = shift.copy() _nd_image.zoom_shift(filtered, None, shift, output, order, mode, cval) @@ -305,14 +305,14 @@ """ if order < 0 or order > 5: raise RuntimeError, 'spline order not supported' - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if input.ndim < 1: raise RuntimeError, 'input and output rank must be > 0' mode = _ni_support._extend_mode_to_code(mode) if prefilter and order > 1: - filtered = spline_filter(input, order, output = numarray.float64) + filtered = spline_filter(input, order, output = numpy.float64) else: filtered = input zoom = _ni_support._normalize_sequence(zoom, input.ndim) @@ -320,7 +320,7 @@ zoom = [1.0 / ii for ii in zoom] output, return_value = _ni_support._get_output(output, input, output_type, shape = output_shape) - zoom = numarray.asarray(zoom, dtype = numarray.float64) + zoom = numpy.asarray(zoom, dtype = numpy.float64) if not zoom.flags.contiguous: zoom = shift.copy() _nd_image.zoom_shift(filtered, zoom, None, output, order, mode, cval) @@ -351,7 +351,7 @@ filtered before interpolation, if False it is assumed that the input is already filtered. """ - input = numarray.asarray(input) + input = numpy.asarray(input) axes = list(axes) rank = input.ndim if axes[0] < 0: @@ -362,36 +362,36 @@ raise RuntimeError, 'invalid rotation plane specified' if axes[0] > axes[1]: axes = axes[1], axes[0] - angle = numarray.pi / 180 * angle + angle = numpy.pi / 180 * angle m11 = math.cos(angle) m12 = math.sin(angle) m21 = -math.sin(angle) m22 = math.cos(angle) - matrix = numarray.array([[m11, m12], - [m21, m22]], dtype = numarray.float64) + matrix = numpy.array([[m11, m12], + [m21, m22]], dtype = numpy.float64) iy = input.shape[axes[0]] ix = input.shape[axes[1]] if reshape: - mtrx = numarray.array([[ m11, -m21], - [-m12, m22]], dtype = numarray.float64) + mtrx = numpy.array([[ m11, -m21], + [-m12, m22]], dtype = numpy.float64) minc = [0, 0] maxc = [0, 0] - coor = numarray.dot(mtrx, [0, ix]) + coor = numpy.dot(mtrx, [0, ix]) minc, maxc = _minmax(coor, minc, maxc) - coor = numarray.dot(mtrx, [iy, 0]) + coor = numpy.dot(mtrx, [iy, 0]) minc, maxc = _minmax(coor, minc, maxc) - coor = numarray.dot(mtrx, [iy, ix]) + coor = numpy.dot(mtrx, [iy, ix]) minc, maxc = _minmax(coor, minc, maxc) oy = int(maxc[0] - minc[0] + 0.5) ox = int(maxc[1] - minc[1] + 0.5) else: oy = input.shape[axes[0]] ox = input.shape[axes[1]] - offset = numarray.zeros((2,), dtype = numarray.float64) + offset = numpy.zeros((2,), dtype = numpy.float64) offset[0] = float(oy) / 2.0 - 0.5 offset[1] = float(ox) / 2.0 - 0.5 - offset = numarray.dot(matrix, offset) - tmp = numarray.zeros((2,), dtype = numarray.float64) + offset = numpy.dot(matrix, offset) + tmp = numpy.zeros((2,), dtype = numpy.float64) tmp[0] = float(iy) / 2.0 - 0.5 tmp[1] = float(ix) / 2.0 - 0.5 offset = tmp - offset @@ -406,7 +406,7 @@ order, mode, cval, prefilter) else: coordinates = [] - size = numarray.product(input.shape,axis=0) + size = numpy.product(input.shape,axis=0) size /= input.shape[axes[0]] size /= input.shape[axes[1]] for ii in range(input.ndim): Modified: trunk/Lib/ndimage/measurements.py =================================================================== --- trunk/Lib/ndimage/measurements.py 2006-09-24 08:28:59 UTC (rev 2231) +++ trunk/Lib/ndimage/measurements.py 2006-09-24 09:05:13 UTC (rev 2232) @@ -30,7 +30,6 @@ import types import math -import numpy.oldnumeric as numarray import numpy import _ni_support import _nd_image @@ -46,12 +45,12 @@ of objects found. If an output array is provided only the number of objects found is returned. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if structure == None: structure = morphology.generate_binary_structure(input.ndim, 1) - structure = numarray.asarray(structure, dtype = bool) + structure = numpy.asarray(structure, dtype = bool) if structure.ndim != input.ndim: raise RuntimeError, 'structure and input must have equal rank' for ii in structure.shape: @@ -60,10 +59,10 @@ if not structure.flags.contiguous: structure = structure.copy() if isinstance(output, numpy.ndarray): - if output.dtype.type != numarray.int32: - raise RuntimeError, 'output type must be Int32' + if output.dtype.type != numpy.int32: + raise RuntimeError, 'output type must be int32' else: - output = numarray.Int32 + output = numpy.int32 output, return_value = _ni_support._get_output(output, input) max_label = _nd_image.label(input, structure, output) if return_value == None: @@ -81,8 +80,8 @@ gives the largest object number that is searched for, otherwise all are returned. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if max_label < 1: max_label = input.max() @@ -95,11 +94,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -114,11 +113,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -133,11 +132,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -166,11 +165,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -185,11 +184,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -201,7 +200,7 @@ """Convert a linear index to a position""" if len(shape) > 0: pos = [] - stride = numarray.multiply.reduce(shape) + stride = numpy.multiply.reduce(shape) for size in shape: stride = stride // size pos.append(index // stride) @@ -218,11 +217,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -241,11 +240,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -265,11 +264,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -293,11 +292,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -315,11 +314,11 @@ label numbers of the objects to be measured. If index is None, all values are used where labels is larger than zero. """ - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if labels != None: - labels = numarray.asarray(labels) + labels = numpy.asarray(labels) labels = _broadcast(labels, input.shape) if labels.shape != input.shape: @@ -340,12 +339,12 @@ provided an element is generated iwth a squared connecitiviy equal to one. An output array can optionally be provided. """ - input = numarray.asarray(input) - if input.dtype.type not in [numarray.uint8, numarray.uint16]: + input = numpy.asarray(input) + if input.dtype.type not in [numpy.uint8, numpy.uint16]: raise TypeError, 'only 8 and 16 unsigned inputs are supported' if structure == None: structure = morphology.generate_binary_structure(input.ndim, 1) - structure = numarray.asarray(structure, dtype = bool) + structure = numpy.asarray(structure, dtype = bool) if structure.ndim != input.ndim: raise RuntimeError, 'structure and input must have equal rank' for ii in structure.shape: @@ -353,7 +352,7 @@ raise RuntimeError, 'structure dimensions must be equal to 3' if not structure.flags.contiguous: structure = structure.copy() - markers = numarray.asarray(markers) + markers = numpy.asarray(markers) if input.shape != markers.shape: raise RuntimeError, 'input and markers must have equal shape' @@ -380,7 +379,7 @@ def _broadcast(arr, sshape): """Return broadcast view of arr, else return None.""" ashape = arr.shape - return_value = numarray.zeros(sshape, arr.dtype) + return_value = numpy.zeros(sshape, arr.dtype) # Just return arr if they have the same shape if sshape == ashape: return arr Modified: trunk/Lib/ndimage/morphology.py =================================================================== --- trunk/Lib/ndimage/morphology.py 2006-09-24 08:28:59 UTC (rev 2231) +++ trunk/Lib/ndimage/morphology.py 2006-09-24 09:05:13 UTC (rev 2232) @@ -28,7 +28,6 @@ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import numpy.oldnumeric as numarray import numpy import _ni_support import _nd_image @@ -37,7 +36,7 @@ def _center_is_true(structure, origin): - structure = numarray.array(structure) + structure = numpy.array(structure) coor = tuple([oo + ss // 2 for ss, oo in zip(structure.shape, origin)]) return bool(structure[coor]) @@ -49,7 +48,7 @@ not, a tuple of the iterated structure and the modified origin is returned. """ - structure = numarray.asarray(structure) + structure = numpy.asarray(structure) if iterations < 2: return structure.copy() ni = iterations - 1 @@ -57,7 +56,7 @@ pos = [ni * (structure.shape[ii] / 2) for ii in range(len(shape))] slc = [slice(pos[ii], pos[ii] + structure.shape[ii], None) for ii in range(len(shape))] - out = numarray.zeros(shape, bool) + out = numpy.zeros(shape, bool) out[slc] = structure != 0 out = binary_dilation(out, structure, iterations = ni) if origin is None: @@ -77,39 +76,39 @@ connectivity = 1 if rank < 1: if connectivity < 1: - return numarray.array(0, dtype = bool) + return numpy.array(0, dtype = bool) else: - return numarray.array(1, dtype = bool) - output = numarray.zeros([3] * rank, bool) - output = numarray.fabs(numarray.indices([3] * rank) - 1) - output = numarray.add.reduce(output, 0) - return numarray.asarray(output <= connectivity, dtype = bool) + return numpy.array(1, dtype = bool) + output = numpy.zeros([3] * rank, bool) + output = numpy.fabs(numpy.indices([3] * rank) - 1) + output = numpy.add.reduce(output, 0) + return numpy.asarray(output <= connectivity, dtype = bool) def _binary_erosion(input, structure, iterations, mask, output, border_value, origin, invert, brute_force): - input = numarray.asarray(input) - if numarray.iscomplexobj(input): + input = numpy.asarray(input) + if numpy.iscomplexobj(input): raise TypeError, 'Complex type not supported' if structure is None: structure = generate_binary_structure(input.ndim, 1) else: - structure = numarray.asarray(structure) + structure = numpy.asarray(structure) structure = structure.astype(bool) if structure.ndim != input.ndim: raise RuntimeError, 'structure rank must equal input rank' if not structure.flags.contiguous: structure = structure.copy() - if numarray.product(structure.shape,axis=0) < 1: + if numpy.product(structure.shape,axis=0) < 1: raise RuntimeError, 'structure must not be empty' if mask is not None: - mask = numarray.asarray(mask) + mask = numpy.asarray(mask) if mask.shape != input.shape: raise RuntimeError, 'mask and input must have equal sizes' origin = _ni_support._normalize_sequence(origin, input.ndim) cit = _center_is_true(structure, origin) if isinstance(output, numpy.ndarray): - if numarray.iscomplexobj(output): + if numpy.iscomplexobj(output): raise TypeError, 'Complex output type not supported' else: output = bool @@ -130,8 +129,8 @@ if not structure.shape[ii] & 1: origin[ii] -= 1 if mask != None: - msk = numarray.asarray(mask) - msk = mask.astype(numarray.Int8) + msk = numpy.asarray(mask) + msk = mask.astype(numpy.int8) if msk is mask: msk = mask.copy() mask = msk @@ -141,11 +140,11 @@ origin, invert, coordinate_list) return return_value else: - tmp_in = numarray.zeros(input.shape, bool) + tmp_in = numpy.zeros(input.shape, bool) if return_value == None: tmp_out = output else: - tmp_out = numarray.zeros(input.shape, bool) + tmp_out = numpy.zeros(input.shape, bool) if not iterations & 1: tmp_in, tmp_out = tmp_out, tmp_in changed = _nd_image.binary_erosion(input, structure, mask, @@ -190,11 +189,11 @@ elements with a true value at the corresponding mask element are modified at each iteration. """ - input = numarray.asarray(input) + input = numpy.asarray(input) if structure == None: structure = generate_binary_structure(input.ndim, 1) origin = _ni_support._normalize_sequence(origin, input.ndim) - structure = numarray.asarray(structure) + structure = numpy.asarray(structure) structure = structure[tuple([slice(None, None, -1)] * structure.ndim)] for ii in range(len(origin)): @@ -215,7 +214,7 @@ to one. The iterations parameter gives the number of times the erosions and then the dilations are done. """ - input = numarray.asarray(input) + input = numpy.asarray(input) if structure is None: rank = input.ndim structure = generate_binary_structure(rank, 1) @@ -235,7 +234,7 @@ to one. The iterations parameter gives the number of times the dilations and then the erosions are done. """ - input = numarray.asarray(input) + input = numpy.asarray(input) if structure is None: rank = input.ndim structure = generate_binary_structure(rank, 1) @@ -257,11 +256,11 @@ element. If the origin for the second structure is equal to None it is set equal to the origin of the first. """ - input = numarray.asarray(input) + input = numpy.asarray(input) if structure1 is None: structure1 = generate_binary_structure(input.ndim, 1) if structure2 is None: - structure2 = numarray.logical_not(structure1) + structure2 = numpy.logical_not(structure1) origin1 = _ni_support._normalize_sequence(origin1, input.ndim) if origin2 is None: origin2 = origin1 @@ -274,11 +273,11 @@ result = _binary_erosion(input, structure2, 1, None, output, 0, origin2, 1, False) if inplace: - numarray.logical_not(output, output) - numarray.logical_and(tmp1, output, output) + numpy.logical_not(output, output) + numpy.logical_and(tmp1, output, output) else: - numarray.logical_not(result, result) - return numarray.logical_and(tmp1, result) + numpy.logical_not(result, result) + return numpy.logical_and(tmp1, result) def binary_propagation(input, structure = None, mask = None, output = None, border_value = 0, origin = 0): @@ -305,16 +304,16 @@ provided an element is generated with a squared connectivity equal to one. """ - mask = numarray.logical_not(input) - tmp = numarray.zeros(mask.shape, bool) + mask = numpy.logical_not(input) + tmp = numpy.zeros(mask.shape, bool) inplace = isinstance(output, numpy.ndarray) if inplace: binary_dilation(tmp, structure, -1, mask, output, 1, origin) - numarray.logical_not(output, output) + numpy.logical_not(output, output) else: output = binary_dilation(tmp, structure, -1, mask, None, 1, origin) - numarray.logical_not(output, output) + numpy.logical_not(output, output) return output def grey_erosion(input, size = None, footprint = None, structure = None, @@ -342,14 +341,14 @@ value when mode is equal to 'constant'. """ if structure is not None: - structure = numarray.asarray(structure) + structure = numpy.asarray(structure) structure = structure[tuple([slice(None, None, -1)] * structure.ndim)] if footprint is not None: - footprint = numarray.asarray(footprint) + footprint = numpy.asarray(footprint) footprint = footprint[tuple([slice(None, None, -1)] * footprint.ndim)] - input = numarray.asarray(input) + input = numpy.asarray(input) origin = _ni_support._normalize_sequence(origin, input.ndim) for ii in range(len(origin)): origin[ii] = -origin[ii] @@ -411,7 +410,7 @@ if isinstance(output, numpy.ndarray): grey_erosion(input, size, footprint, structure, output, mode, cval, origin) - return numarray.subtract(tmp, output, output) + return numpy.subtract(tmp, output, output) else: return (tmp - grey_erosion(input, size, footprint, structure, None, mode, cval, origin)) @@ -433,17 +432,17 @@ if isinstance(output, numpy.ndarray): grey_erosion(input, size, footprint, structure, output, mode, cval, origin) - numarray.add(tmp1, output, output) + numpy.add(tmp1, output, output) del tmp1 - numarray.subtract(output, input, output) - return numarray.subtract(output, input, output) + numpy.subtract(output, input, output) + return numpy.subtract(output, input, output) else: tmp2 = grey_erosion(input, size, footprint, structure, None, mode, cval, origin) - numarray.add(tmp1, tmp2, tmp2) + numpy.add(tmp1, tmp2, tmp2) del tmp1 - numarray.subtract(tmp2, input, tmp2) - numarray.subtract(tmp2, input, tmp2) + numpy.subtract(tmp2, input, tmp2) + numpy.subtract(tmp2, input, tmp2) return tmp2 @@ -463,7 +462,7 @@ grey_dilation(tmp, size, footprint, structure, output, mode, cval, origin) del tmp - return numarray.subtract(input, output, output) + return numpy.subtract(input, output, output) else: tmp = grey_dilation(tmp, size, footprint, structure, None, mode, cval, origin) @@ -487,7 +486,7 @@ grey_erosion(tmp, size, footprint, structure, output, mode, cval, origin) del tmp - return numarray.subtract(output, input, output) + return numpy.subtract(output, input, output) else: tmp = grey_erosion(tmp, size, footprint, structure, None, mode, cval, origin) @@ -524,17 +523,17 @@ chessboard algorithms. the distances and indices arguments can be used to give optional - output arrays that must be of the correct size and type (Float64 - and Int32). + output arrays that must be of the correct size and type (float64 + and int32). """ if (not return_distances) and (not return_indices): msg = 'at least one of distances/indices must be specified' raise RuntimeError, msg - tmp1 = numarray.asarray(input) != 0 + tmp1 = numpy.asarray(input) != 0 struct = generate_binary_structure(tmp1.ndim, tmp1.ndim) tmp2 = binary_dilation(tmp1, struct) - tmp2 = numarray.logical_xor(tmp1, tmp2) - tmp1 = tmp1.astype(numarray.Int8) - tmp2.astype(numarray.Int8) + tmp2 = numpy.logical_xor(tmp1, tmp2) + tmp1 = tmp1.astype(numpy.int8) - tmp2.astype(numpy.int8) del tmp2 metric = metric.lower() if metric == 'euclidean': @@ -547,44 +546,44 @@ raise RuntimeError, 'distance metric not supported' if sampling != None: sampling = _ni_support._normalize_sequence(sampling, tmp1.ndim) - sampling = numarray.asarray(sampling, dtype = numarray.Float64) + sampling = numpy.asarray(sampling, dtype = numpy.float64) if not sampling.flags.contiguous: sampling = sampling.copy() if return_indices: - ft = numarray.zeros(tmp1.shape, dtype = numarray.Int32) + ft = numpy.zeros(tmp1.shape, dtype = numpy.int32) else: ft = None if return_distances: if distances == None: if metric == 1: - dt = numarray.zeros(tmp1.shape, dtype = numarray.Float64) + dt = numpy.zeros(tmp1.shape, dtype = numpy.float64) else: - dt = numarray.zeros(tmp1.shape, dtype = numarray.UInt32) + dt = numpy.zeros(tmp1.shape, dtype = numpy.uint32) else: if distances.shape != tmp1.shape: raise RuntimeError, 'distances array has wrong shape' if metric == 1: - if distances.dtype.type != numarray.float64: - raise RuntimeError, 'distances array must be Float64' + if distances.dtype.type != numpy.float64: + raise RuntimeError, 'distances array must be float64' else: - if distances.dtype.type != numarray.uint32: - raise RuntimeError, 'distances array must be UInt32' + if distances.dtype.type != numpy.uint32: + raise RuntimeError, 'distances array must be uint32' dt = distances else: dt = None _nd_image.distance_transform_bf(tmp1, metric, sampling, dt, ft) if return_indices: if isinstance(indices, numpy.ndarray): - if indices.dtype.type != numarray.int32: - raise RuntimeError, 'indices must of Int32 type' + if indices.dtype.type != numpy.int32: + raise RuntimeError, 'indices must of int32 type' if indices.shape != (tmp1.ndim,) + tmp1.shape: raise RuntimeError, 'indices has wrong shape' tmp2 = indices else: - tmp2 = numarray.indices(tmp1.shape, dtype = numarray.Int32) - ft = numarray.ravel(ft) + tmp2 = numpy.indices(tmp1.shape, dtype = numpy.int32) + ft = numpy.ravel(ft) for ii in range(tmp2.shape[0]): - rtmp = numarray.ravel(tmp2[ii, ...])[ft] + rtmp = numpy.ravel(tmp2[ii, ...])[ft] rtmp.shape = tmp1.shape tmp2[ii, ...] = rtmp ft = tmp2 @@ -624,14 +623,14 @@ must be returned. The distances and indices arguments can be used to give optional - output arrays that must be of the correct size and type (both Int32). + output arrays that must be of the correct size and type (both int32). """ if (not return_distances) and (not return_indices): msg = 'at least one of distances/indices must be specified' raise RuntimeError, msg ft_inplace = isinstance(indices, numpy.ndarray) dt_inplace = isinstance(distances, numpy.ndarray) - input = numarray.asarray(input) + input = numpy.asarray(input) if structure == 'cityblock': rank = input.ndim structure = generate_binary_structure(rank, 1) @@ -640,7 +639,7 @@ structure = generate_binary_structure(rank, rank) else: try: - structure = numarray.asarray(structure) + structure = numpy.asarray(structure) except: raise RuntimeError, 'invalid structure provided' for s in structure.shape: @@ -649,18 +648,18 @@ if not structure.flags.contiguous: structure = structure.copy() if dt_inplace: - if distances.dtype.type != numarray.int32: - raise RuntimeError, 'distances must be of Int32 type' + if distances.dtype.type != numpy.int32: + raise RuntimeError, 'distances must be of int32 type' if distances.shape != input.shape: raise RuntimeError, 'distances has wrong shape' dt = distances - dt[...] = numarray.where(input, -1, 0).astype(numarray.Int32) + dt[...] = numpy.where(input, -1, 0).astype(numpy.int32) else: - dt = numarray.where(input, -1, 0).astype(numarray.Int32) + dt = numpy.where(input, -1, 0).astype(numpy.int32) rank = dt.ndim if return_indices: - sz = numarray.product(dt.shape,axis=0) - ft = numarray.arange(sz, dtype = numarray.Int32) + sz = numpy.product(dt.shape,axis=0) + ft = numpy.arange(sz, dtype = numpy.int32) ft.shape = dt.shape else: ft = None @@ -672,17 +671,17 @@ dt = dt[tuple([slice(None, None, -1)] * rank)] if return_indices: ft = ft[tuple([slice(None, None, -1)] * rank)] - ft = numarray.ravel(ft) + ft = numpy.ravel(ft) if ft_inplace: - if indices.dtype.type != numarray.int32: - raise RuntimeError, 'indices must of Int32 type' + if indices.dtype.type != numpy.int32: + raise RuntimeError, 'indices must of int32 type' if indices.shape != (dt.ndim,) + dt.shape: raise RuntimeError, 'indices has wrong shape' tmp = indices else: - tmp = numarray.indices(dt.shape, dtype = numarray.Int32) + tmp = numpy.indices(dt.shape, dtype = numpy.int32) for ii in range(tmp.shape[0]): - rtmp = numarray.ravel(tmp[ii, ...])[ft] + rtmp = numpy.ravel(tmp[ii, ...])[ft] rtmp.shape = dt.shape tmp[ii, ...] = rtmp ft = tmp @@ -720,8 +719,8 @@ to be equal along all axes. the distances and indices arguments can be used to give optional - output arrays that must be of the correct size and type (Float64 - and Int32). + output arrays that must be of the correct size and type (float64 + and int32). """ if (not return_distances) and (not return_indices): msg = 'at least one of distances/indices must be specified' @@ -729,41 +728,41 @@ ft_inplace = isinstance(indices, numpy.ndarray) dt_inplace = isinstance(distances, numpy.ndarray) # calculate the feature transform - input = numarray.where(input, 1, 0).astype(numarray.Int8) + input = numpy.where(input, 1, 0).astype(numpy.int8) if sampling is not None: sampling = _ni_support._normalize_sequence(sampling, input.ndim) - sampling = numarray.asarray(sampling, dtype = numarray.Float64) + sampling = numpy.asarray(sampling, dtype = numpy.float64) if not sampling.flags.contiguous: sampling = sampling.copy() if ft_inplace: ft = indices if ft.shape != (input.ndim,) + input.shape: raise RuntimeError, 'indices has wrong shape' - if ft.dtype.type != numarray.int32: - raise RuntimeError, 'indices must be of Int32 type' + if ft.dtype.type != numpy.int32: + raise RuntimeError, 'indices must be of int32 type' else: - ft = numarray.zeros((input.ndim,) + input.shape, - dtype = numarray.Int32) + ft = numpy.zeros((input.ndim,) + input.shape, + dtype = numpy.int32) _nd_image.euclidean_feature_transform(input, sampling, ft) # if requested, calculate the distance transform if return_distances: - dt = ft - numarray.indices(input.shape, dtype = ft.dtype) - dt = dt.astype(numarray.Float64) + dt = ft - numpy.indices(input.shape, dtype = ft.dtype) + dt = dt.astype(numpy.float64) if sampling is not None: for ii in range(len(sampling)): dt[ii, ...] *= sampling[ii] - numarray.multiply(dt, dt, dt) + numpy.multiply(dt, dt, dt) if dt_inplace: - dt = numarray.add.reduce(dt, axis = 0) + dt = numpy.add.reduce(dt, axis = 0) if distances.shape != dt.shape: raise RuntimeError, 'indices has wrong shape' - if distances.dtype.type != numarray.float64: - raise RuntimeError, 'indices must be of Float64 type' - numarray.sqrt(dt, distances) + if distances.dtype.type != numpy.float64: + raise RuntimeError, 'indices must be of float64 type' + numpy.sqrt(dt, distances) del dt else: - dt = numarray.add.reduce(dt, axis = 0) - dt = numarray.sqrt(dt) + dt = numpy.add.reduce(dt, axis = 0) + dt = numpy.sqrt(dt) # construct and return the result result = [] if return_distances and not dt_inplace: Modified: trunk/Lib/ndimage/tests/test_ndimage.py =================================================================== --- trunk/Lib/ndimage/tests/test_ndimage.py 2006-09-24 08:28:59 UTC (rev 2231) +++ trunk/Lib/ndimage/tests/test_ndimage.py 2006-09-24 09:05:13 UTC (rev 2232) @@ -31,7 +31,6 @@ import sys import unittest import math -import numpy.oldnumeric as numarray import numpy from numpy import fft from numpy.testing import * @@ -44,21 +43,21 @@ def diff(a, b): if not isinstance(a, numpy.ndarray): - a = numarray.asarray(a) + a = numpy.asarray(a) if not isinstance(b, numpy.ndarray): - b = numarray.asarray(b) + b = numpy.asarray(b) if (0 in a.shape) and (0 in b.shape): return 0.0 - if (a.dtype in [numarray.complex64, numarray.complex128] or - b.dtype in [numarray.complex64, numarray.complex128]): - a = numarray.asarray(a, numarray.complex128) - b = numarray.asarray(b, numarray.complex128) + if (a.dtype in [numpy.complex64, numpy.complex128] or + b.dtype in [numpy.complex64, numpy.complex128]): + a = numpy.asarray(a, numpy.complex128) + b = numpy.asarray(b, numpy.complex128) t = ((a.real - b.real)**2).sum() + ((a.imag - b.imag)**2).sum() else: - a = numarray.asarray(a) - a = a.astype(numarray.float64) - b = numarray.asarray(b) - b = b.astype(numarray.float64) + a = numpy.asarray(a) + a = a.astype(numpy.float64) + b = numpy.asarray(b) + b = b.astype(numpy.float64) t = ((a - b)**2).sum() return math.sqrt(t) @@ -67,20 +66,20 @@ def setUp(self): # list of numarray data types - self.types = [numarray.int8, numarray.uint8, numarray.int16, - numarray.uint16, numarray.int32, numarray.uint32, - numarray.int64, numarray.uint64, - numarray.float32, numarray.float64] + self.types = [numpy.int8, numpy.uint8, numpy.int16, + numpy.uint16, numpy.int32, numpy.uint32, + numpy.int64, numpy.uint64, + numpy.float32, numpy.float64] ## if numinclude.hasUInt64: -## self.types.append(numarray.UInt64) +## self.types.append(numpy.UInt64) # list of boundary modes: self.modes = ['nearest', 'wrap', 'reflect', 'constant'] def test_correlate01(self): "correlation 1" - array = numarray.array([1, 2]) - weights = numarray.array([2]) + array = numpy.array([1, 2]) + weights = numpy.array([2]) true = [2, 4] output = ndimage.correlate(array, weights) self.failUnless(diff(output, true) < eps) @@ -93,8 +92,8 @@ def test_correlate02(self): "correlation 2" - array = numarray.array([1, 2, 3]) - kernel = numarray.array([1]) + array = numpy.array([1, 2, 3]) + kernel = numpy.array([1]) output = ndimage.correlate(array, kernel) self.failUnless(diff(array, output) < eps) output = ndimage.convolve(array, kernel) @@ -106,8 +105,8 @@ def test_correlate03(self): "correlation 3" - array = numarray.array([1]) - weights = numarray.array([1, 1]) + array = numpy.array([1]) + weights = numpy.array([1, 1]) true = [2] output = ndimage.correlate(array, weights) self.failUnless(diff(output, true) < eps) @@ -120,10 +119,10 @@ def test_correlate04(self): "correlation 4" - array = numarray.array([1, 2]) + array = numpy.array([1, 2]) tcor = [2, 3] tcov = [3, 4] - weights = numarray.array([1, 1]) + weights = numpy.array([1, 1]) output = ndimage.correlate(array, weights) self.failUnless(diff(output, tcor) < eps) output = ndimage.convolve(array, weights) @@ -135,10 +134,10 @@ def test_correlate05(self): "correlation 5" - array = numarray.array([1, 2, 3]) + array = numpy.array([1, 2, 3]) tcor = [2, 3, 5] tcov = [3, 5, 6] - kernel = numarray.array([1, 1]) + kernel = numpy.array([1, 1]) output = ndimage.correlate(array, kernel) self.failUnless(diff(tcor, output) < eps) output = ndimage.convolve(array, kernel) @@ -150,10 +149,10 @@ def test_correlate06(self): "correlation 6" - array = numarray.array([1, 2, 3]) + array = numpy.array([1, 2, 3]) tcor = [9, 14, 17] tcov = [7, 10, 15] - weights = numarray.array([1, 2, 3]) + weights = numpy.array([1, 2, 3]) output = ndimage.correlate(array, weights) self.failUnless(diff(output, tcor) < eps) output = ndimage.convolve(array, weights) @@ -165,9 +164,9 @@ def test_correlate07(self): "correlation 7" - array = numarray.array([1, 2, 3]) + array = numpy.array([1, 2, 3]) true = [5, 8, 11] - weights = numarray.array([1, 2, 1]) + weights = numpy.array([1, 2, 1]) output = ndimage.correlate(array, weights) self.failUnless(diff(output, true) < eps) output = ndimage.convolve(array, weights) @@ -179,10 +178,10 @@ def test_correlate08(self): "correlation 8" - array = numarray.array([1, 2, 3]) + array = numpy.array([1, 2, 3]) tcor = [1, 2, 5] tcov = [3, 6, 7] - weights = numarray.array([1, 2, -1]) + weights = numpy.array([1, 2, -1]) output = ndimage.correlate(array, weights) self.failUnless(diff(output, tcor) < eps) output = ndimage.convolve(array, weights) @@ -195,7 +194,7 @@ def test_correlate09(self): "correlation 9" array = [] - kernel = numarray.array([1, 1]) + kernel = numpy.array([1, 1]) output = ndimage.correlate(array, kernel) self.failUnless(diff(array, output) < eps) output = ndimage.convolve(array, kernel) @@ -208,7 +207,7 @@ def test_correlate10(self): "correlation 10" array = [[]] - kernel = numarray.array([[1, 1]]) + kernel = numpy.array([[1, 1]]) output = ndimage.correlate(array, kernel) self.failUnless(diff(array, output) < eps) output = ndimage.convolve(array, kernel) @@ -216,9 +215,9 @@ def test_correlate11(self): "correlation 11" - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6]]) - kernel = numarray.array([[1, 1], + kernel = numpy.array([[1, 1], [1, 1]]) output = ndimage.correlate(array, kernel) self.failUnless(diff([[4, 6, 10], [10, 12, 16]], output) < eps) @@ -227,9 +226,9 @@ def test_correlate12(self): "correlation 12" - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6]]) - kernel = numarray.array([[1, 0], + kernel = numpy.array([[1, 0], [0, 1]]) output = ndimage.correlate(array, kernel) self.failUnless(diff([[2, 3, 5], [5, 6, 8]], output) < eps) @@ -238,10 +237,10 @@ def test_correlate13(self): "correlation 13" - kernel = numarray.array([[1, 0], + kernel = numpy.array([[1, 0], [0, 1]]) for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6]], type1) for type2 in self.types: output = ndimage.correlate(array, kernel, @@ -255,13 +254,13 @@ def test_correlate14(self): "correlation 14" - kernel = numarray.array([[1, 0], + kernel = numpy.array([[1, 0], [0, 1]]) for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6]], type1) for type2 in self.types: - output = numarray.zeros(array.shape, type2) + output = numpy.zeros(array.shape, type2) ndimage.correlate(array, kernel, output = output) error = diff([[2, 3, 5], [5, 6, 8]], output) @@ -272,46 +271,46 @@ def test_correlate15(self): "correlation 15" - kernel = numarray.array([[1, 0], + kernel = numpy.array([[1, 0], [0, 1]]) for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6]], type1) output = ndimage.correlate(array, kernel, - output = numarray.float32) + output = numpy.float32) error = diff([[2, 3, 5], [5, 6, 8]], output) self.failUnless(error < eps and - output.dtype.type == numarray.float32) + output.dtype.type == numpy.float32) output = ndimage.convolve(array, kernel, - output = numarray.float32) + output = numpy.float32) error = diff([[6, 8, 9], [9, 11, 12]], output) self.failUnless(error < eps and - output.dtype.type == numarray.float32) + output.dtype.type == numpy.float32) def test_correlate16(self): "correlation 16" - kernel = numarray.array([[0.5, 0 ], + kernel = numpy.array([[0.5, 0 ], [0, 0.5]]) for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6]], type1) output = ndimage.correlate(array, kernel, - output = numarray.float32) + output = numpy.float32) error = diff([[1, 1.5, 2.5], [2.5, 3, 4]], output) self.failUnless(error < eps and - output.dtype.type == numarray.float32) + output.dtype.type == numpy.float32) output = ndimage.convolve(array, kernel, - output = numarray.float32) + output = numpy.float32) error = diff([[3, 4, 4.5], [4.5, 5.5, 6]], output) self.failUnless(error < eps and - output.dtype.type == numarray.float32) + output.dtype.type == numpy.float32) def test_correlate17(self): "correlation 17" - array = numarray.array([1, 2, 3]) + array = numpy.array([1, 2, 3]) tcor = [3, 5, 6] tcov = [2, 3, 5] - kernel = numarray.array([1, 1]) + kernel = numpy.array([1, 1]) output = ndimage.correlate(array, kernel, origin = -1) self.failUnless(diff(tcor, output) < eps) output = ndimage.convolve(array, kernel, origin = -1) @@ -323,52 +322,52 @@ def test_correlate18(self): "correlation 18" - kernel = numarray.array([[1, 0], + kernel = numpy.array([[1, 0], [0, 1]]) for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6]], type1) output = ndimage.correlate(array, kernel, - output = numarray.float32, + output = numpy.float32, mode = 'nearest', origin = -1) error = diff([[6, 8, 9], [9, 11, 12]], output) self.failUnless(error < eps and - output.dtype.type == numarray.float32) + output.dtype.type == numpy.float32) output = ndimage.convolve(array, kernel, - output = numarray.float32, mode = 'nearest', origin = -1) + output = numpy.float32, mode = 'nearest', origin = -1) error = diff([[2, 3, 5], [5, 6, 8]], output) self.failUnless(error < eps and - output.dtype.type == numarray.float32) + output.dtype.type == numpy.float32) def test_correlate19(self): "correlation 19" - kernel = numarray.array([[1, 0], + kernel = numpy.array([[1, 0], [0, 1]]) for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6]], type1) output = ndimage.correlate(array, kernel, - output = numarray.float32, + output = numpy.float32, mode = 'nearest', origin = [-1, 0]) error = diff([[5, 6, 8], [8, 9, 11]], output) self.failUnless(error < eps and - output.dtype.type == numarray.float32) + output.dtype.type == numpy.float32) output = ndimage.convolve(array, kernel, - output = numarray.float32, + output = numpy.float32, mode = 'nearest', origin = [-1, 0]) error = diff([[3, 5, 6], [6, 8, 9]], output) self.failUnless(error < eps and - output.dtype.type == numarray.float32) + output.dtype.type == numpy.float32) def test_correlate20(self): "correlation 20" - weights = numarray.array([1, 2, 1]) + weights = numpy.array([1, 2, 1]) true = [[5, 10, 15], [7, 14, 21]] for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [2, 4, 6]], type1) for type2 in self.types: - output = numarray.zeros((2, 3), type2) + output = numpy.zeros((2, 3), type2) ndimage.correlate1d(array, weights, axis = 0, output = output) self.failUnless(diff(output, true) < eps) @@ -378,10 +377,10 @@ def test_correlate21(self): "correlation 21" - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [2, 4, 6]]) true = [[5, 10, 15], [7, 14, 21]] - weights = numarray.array([1, 2, 1]) + weights = numpy.array([1, 2, 1]) output = ndimage.correlate1d(array, weights, axis = 0) self.failUnless(diff(output, true) < eps) output = ndimage.convolve1d(array, weights, axis = 0) @@ -389,13 +388,13 @@ def test_correlate22(self): "correlation 22" - weights = numarray.array([1, 2, 1]) + weights = numpy.array([1, 2, 1]) true = [[6, 12, 18], [6, 12, 18]] for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [2, 4, 6]], type1) for type2 in self.types: - output = numarray.zeros((2, 3), type2) + output = numpy.zeros((2, 3), type2) ndimage.correlate1d(array, weights, axis = 0, mode = 'wrap', output = output) self.failUnless(diff(output, true) < eps) @@ -405,13 +404,13 @@ def test_correlate23(self): "correlation 23" - weights = numarray.array([1, 2, 1]) + weights = numpy.array([1, 2, 1]) true = [[5, 10, 15], [7, 14, 21]] for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [2, 4, 6]], type1) for type2 in self.types: - output = numarray.zeros((2, 3), type2) + output = numpy.zeros((2, 3), type2) ndimage.correlate1d(array, weights, axis = 0, mode = 'nearest', output = output) self.failUnless(diff(output, true) < eps) @@ -421,14 +420,14 @@ def test_correlate24(self): "correlation 24" - weights = numarray.array([1, 2, 1]) + weights = numpy.array([1, 2, 1]) tcor = [[7, 14, 21], [8, 16, 24]] tcov = [[4, 8, 12], [5, 10, 15]] for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [2, 4, 6]], type1) for type2 in self.types: - output = numarray.zeros((2, 3), type2) + output = numpy.zeros((2, 3), type2) ndimage.correlate1d(array, weights, axis = 0, mode = 'nearest', output = output, origin = -1) self.failUnless(diff(output, tcor) < eps) @@ -438,14 +437,14 @@ def test_correlate25(self): "correlation 25" - weights = numarray.array([1, 2, 1]) + weights = numpy.array([1, 2, 1]) tcor = [[4, 8, 12], [5, 10, 15]] tcov = [[7, 14, 21], [8, 16, 24]] for type1 in self.types: - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [2, 4, 6]], type1) for type2 in self.types: - output = numarray.zeros((2, 3), type2) + output = numpy.zeros((2, 3), type2) ndimage.correlate1d(array, weights, axis = 0, mode = 'nearest', output = output, origin = 1) self.failUnless(diff(output, tcor) < eps) @@ -455,22 +454,22 @@ def test_gauss01(self): "gaussian filter 1" - input = numarray.array([[1, 2, 3], - [2, 4, 6]], numarray.float32) + input = numpy.array([[1, 2, 3], + [2, 4, 6]], numpy.float32) output = ndimage.gaussian_filter(input, 0) self.failUnless(diff(output, input) < eps) def test_gauss02(self): "gaussian filter 2" - input = numarray.array([[1, 2, 3], - [2, 4, 6]], numarray.float32) + input = numpy.array([[1, 2, 3], + [2, 4, 6]], numpy.float32) output = ndimage.gaussian_filter(input, 1.0) self.failUnless(input.dtype == output.dtype and input.shape == output.shape) def test_gauss03(self): "gaussian filter 3" - input = numarray.arange(100 * 100).astype(numarray.float32) + input = numpy.arange(100 * 100).astype(numpy.float32) input.shape = (100, 100) output = ndimage.gaussian_filter(input, [1.0, 1.0]) @@ -481,31 +480,31 @@ def test_gauss04(self): "gaussian filter 4" - input = numarray.arange(100 * 100).astype(numarray.float32) + input = numpy.arange(100 * 100).astype(numpy.float32) input.shape = (100, 100) - otype = numarray.float64 + otype = numpy.float64 output = ndimage.gaussian_filter(input, [1.0, 1.0], output = otype) - self.failUnless(output.dtype.type == numarray.float64 and + self.failUnless(output.dtype.type == numpy.float64 and input.shape == output.shape and diff(input, output) > 1.0) def test_gauss05(self): "gaussian filter 5" - input = numarray.arange(100 * 100).astype(numarray.float32) + input = numpy.arange(100 * 100).astype(numpy.float32) input.shape = (100, 100) - otype = numarray.float64 + otype = numpy.float64 output = ndimage.gaussian_filter(input, [1.0, 1.0], order = 1, output = otype) - self.failUnless(output.dtype.type == numarray.float64 and + self.failUnless(output.dtype.type == numpy.float64 and input.shape == output.shape and diff(input, output) > 1.0) def test_gauss06(self): "gaussian filter 6" - input = numarray.arange(100 * 100).astype(numarray.float32) + input = numpy.arange(100 * 100).astype(numpy.float32) input.shape = (100, 100) - otype = numarray.float64 + otype = numpy.float64 output1 = ndimage.gaussian_filter(input, [1.0, 1.0], output = otype) output2 = ndimage.gaussian_filter(input, 1.0, @@ -515,7 +514,7 @@ def test_prewitt01(self): "prewitt filter 1" for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0) @@ -527,19 +526,19 @@ def test_prewitt02(self): "prewitt filter 2" for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0) t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 1) - output = numarray.zeros(array.shape, type) + output = numpy.zeros(array.shape, type) ndimage.prewitt(array, 0, output) self.failUnless(diff(t, output) < eps) def test_prewitt03(self): "prewitt filter 3" for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 1) @@ -550,7 +549,7 @@ def test_prewitt04(self): "prewitt filter 4" for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) t = ndimage.prewitt(array, -1) @@ -560,7 +559,7 @@ def test_sobel01(self): "sobel filter 1" for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0) @@ -571,31 +570,31 @@ def test_sobel02(self): "sobel filter 2" for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0) t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1) - output = numarray.zeros(array.shape, type) + output = numpy.zeros(array.shape, type) ndimage.sobel(array, 0, output) self.failUnless(diff(t, output) < eps) def test_sobel03(self): "sobel filter 3" for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 1) t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 0) - output = numarray.zeros(array.shape, type) + output = numpy.zeros(array.shape, type) output = ndimage.sobel(array, 1) self.failUnless(diff(t, output) < eps) def test_sobel04(self): "sobel filter 4" for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) t = ndimage.sobel(array, -1) @@ -604,8 +603,8 @@ def test_laplace01(self): "laplace filter 1" - for type in [numarray.int32, numarray.float32, numarray.float64]: - array = numarray.array([[3, 2, 5, 1, 4], + for type in [numpy.int32, numpy.float32, numpy.float64]: + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) * 100 tmp1 = ndimage.correlate1d(array, [1, -2, 1], 0) @@ -615,20 +614,20 @@ def test_laplace02(self): "laplace filter 2" - for type in [numarray.int32, numarray.float32, numarray.float64]: - array = numarray.array([[3, 2, 5, 1, 4], + for type in [numpy.int32, numpy.float32, numpy.float64]: + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) * 100 tmp1 = ndimage.correlate1d(array, [1, -2, 1], 0) tmp2 = ndimage.correlate1d(array, [1, -2, 1], 1) - output = numarray.zeros(array.shape, type) + output = numpy.zeros(array.shape, type) ndimage.laplace(array, output = output) self.failUnless(diff(tmp1 + tmp2, output) < eps) def test_gaussian_laplace01(self): "gaussian laplace filter 1" - for type in [numarray.int32, numarray.float32, numarray.float64]: - array = numarray.array([[3, 2, 5, 1, 4], + for type in [numpy.int32, numpy.float32, numpy.float64]: + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) * 100 tmp1 = ndimage.gaussian_filter(array, 1.0, [2, 0]) @@ -638,13 +637,13 @@ def test_gaussian_laplace02(self): "gaussian laplace filter 2" - for type in [numarray.int32, numarray.float32, numarray.float64]: - array = numarray.array([[3, 2, 5, 1, 4], + for type in [numpy.int32, numpy.float32, numpy.float64]: + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) * 100 tmp1 = ndimage.gaussian_filter(array, 1.0, [2, 0]) tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 2]) - output = numarray.zeros(array.shape, type) + output = numpy.zeros(array.shape, type) ndimage.gaussian_laplace(array, 1.0, output) self.failUnless(diff(tmp1 + tmp2, output) < eps) @@ -652,16 +651,16 @@ "generic laplace filter 1" def derivative2(input, axis, output, mode, cval, a, b): sigma = [a, b / 2.0] - input = numarray.asarray(input) + input = numpy.asarray(input) order = [0] * input.ndim order[axis] = 2 return ndimage.gaussian_filter(input, sigma, order, output, mode, cval) for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) - output = numarray.zeros(array.shape, type) + output = numpy.zeros(array.shape, type) tmp = ndimage.generic_laplace(array, derivative2, extra_arguments = (1.0,), extra_keywords = {'b': 2.0}) ndimage.gaussian_laplace(array, 1.0, output) @@ -669,8 +668,8 @@ def test_gaussian_gradient_magnitude01(self): "gaussian gradient magnitude filter 1" - for type in [numarray.int32, numarray.float32, numarray.float64]: - array = numarray.array([[3, 2, 5, 1, 4], + for type in [numpy.int32, numpy.float32, numpy.float64]: + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) * 100 tmp1 = ndimage.gaussian_filter(array, 1.0, [1, 0]) @@ -678,32 +677,32 @@ output = ndimage.gaussian_gradient_magnitude(array, 1.0) true = tmp1 * tmp1 + tmp2 * tmp2 - numarray.sqrt(true, true) + numpy.sqrt(true, true) self.failUnless(diff(true, output) < eps) def test_gaussian_gradient_magnitude02(self): "gaussian gradient magnitude filter 2" - for type in [numarray.int32, numarray.float32, numarray.float64]: - array = numarray.array([[3, 2, 5, 1, 4], + for type in [numpy.int32, numpy.float32, numpy.float64]: + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) * 100 tmp1 = ndimage.gaussian_filter(array, 1.0, [1, 0]) tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 1]) - output = numarray.zeros(array.shape, type) + output = numpy.zeros(array.shape, type) ndimage.gaussian_gradient_magnitude(array, 1.0, output) true = tmp1 * tmp1 + tmp2 * tmp2 - numarray.sqrt(true, true) + numpy.sqrt(true, true) self.failUnless(diff(true, output) < eps) def test_generic_gradient_magnitude01(self): "generic gradient magnitude 1" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], - [5, 6, 9, 3, 5]], numarray.float64) + [5, 6, 9, 3, 5]], numpy.float64) def derivative(input, axis, output, mode, cval, a, b): sigma = [a, b / 2.0] - input = numarray.asarray(input) + input = numpy.asarray(input) order = [0] * input.ndim order[axis] = 1 return ndimage.gaussian_filter(input, sigma, order, @@ -716,7 +715,7 @@ def test_uniform01(self): "uniform filter 1" - array = numarray.array([2, 4, 6]) + array = numpy.array([2, 4, 6]) size = 2 output = ndimage.uniform_filter1d(array, size, origin = -1) @@ -724,21 +723,21 @@ def test_uniform02(self): "uniform filter 2" - array = numarray.array([1, 2, 3]) + array = numpy.array([1, 2, 3]) filter_shape = [0] output = ndimage.uniform_filter(array, filter_shape) self.failUnless(diff(array, output) < eps) def test_uniform03(self): "uniform filter 3" - array = numarray.array([1, 2, 3]) + array = numpy.array([1, 2, 3]) filter_shape = [1] output = ndimage.uniform_filter(array, filter_shape) self.failUnless(diff(array, output) < eps) def test_uniform04(self): "uniform filter 4" - array = numarray.array([2, 4, 6]) + array = numpy.array([2, 4, 6]) filter_shape = [2] output = ndimage.uniform_filter(array, filter_shape) self.failUnless(diff([2, 3, 5], output) < eps) @@ -754,7 +753,7 @@ "uniform filter 6" filter_shape = [2, 2] for type1 in self.types: - array = numarray.array([[4, 8, 12], + array = numpy.array([[4, 8, 12], [16, 20, 24]], type1) for type2 in self.types: output = ndimage.uniform_filter(array, @@ -764,38 +763,38 @@ def test_minimum_filter01(self): "minimum filter 1" - array = numarray.array([1, 2, 3, 4, 5]) - filter_shape = numarray.array([2]) + array = numpy.array([1, 2, 3, 4, 5]) + filter_shape = numpy.array([2]) output = ndimage.minimum_filter(array, filter_shape) self.failUnless(diff([1, 1, 2, 3, 4], output) < eps) def test_minimum_filter02(self): "minimum filter 2" - array = numarray.array([1, 2, 3, 4, 5]) - filter_shape = numarray.array([3]) + array = numpy.array([1, 2, 3, 4, 5]) + filter_shape = numpy.array([3]) output = ndimage.minimum_filter(array, filter_shape) self.failUnless(diff([1, 1, 2, 3, 4], output) < eps) def test_minimum_filter03(self): "minimum filter 3" - array = numarray.array([3, 2, 5, 1, 4]) - filter_shape = numarray.array([2]) + array = numpy.array([3, 2, 5, 1, 4]) + filter_shape = numpy.array([2]) output = ndimage.minimum_filter(array, filter_shape) self.failUnless(diff([3, 2, 2, 1, 1], output) < eps) def test_minimum_filter04(self): "minimum filter 4" - array = numarray.array([3, 2, 5, 1, 4]) - filter_shape = numarray.array([3]) + array = numpy.array([3, 2, 5, 1, 4]) + filter_shape = numpy.array([3]) output = ndimage.minimum_filter(array, filter_shape) self.failUnless(diff([2, 2, 1, 1, 1], output) < eps) def test_minimum_filter05(self): "minimum filter 5" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) - filter_shape = numarray.array([2, 3]) + filter_shape = numpy.array([2, 3]) output = ndimage.minimum_filter(array, filter_shape) self.failUnless(diff([[2, 2, 1, 1, 1], [2, 2, 1, 1, 1], @@ -803,7 +802,7 @@ def test_minimum_filter06(self): "minimum filter 6" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 1, 1], [1, 1, 1]] @@ -815,7 +814,7 @@ def test_minimum_filter07(self): "minimum filter 7" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -827,7 +826,7 @@ def test_minimum_filter08(self): "minimum filter 8" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -839,7 +838,7 @@ def test_minimum_filter09(self): "minimum filter 9" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -851,38 +850,38 @@ def test_maximum_filter01(self): "maximum filter 1" - array = numarray.array([1, 2, 3, 4, 5]) - filter_shape = numarray.array([2]) + array = numpy.array([1, 2, 3, 4, 5]) + filter_shape = numpy.array([2]) output = ndimage.maximum_filter(array, filter_shape) self.failUnless(diff([1, 2, 3, 4, 5], output) < eps) def test_maximum_filter02(self): "maximum filter 2" - array = numarray.array([1, 2, 3, 4, 5]) - filter_shape = numarray.array([3]) + array = numpy.array([1, 2, 3, 4, 5]) + filter_shape = numpy.array([3]) output = ndimage.maximum_filter(array, filter_shape) self.failUnless(diff([2, 3, 4, 5, 5], output) < eps) def test_maximum_filter03(self): "maximum filter 3" - array = numarray.array([3, 2, 5, 1, 4]) - filter_shape = numarray.array([2]) + array = numpy.array([3, 2, 5, 1, 4]) + filter_shape = numpy.array([2]) output = ndimage.maximum_filter(array, filter_shape) self.failUnless(diff([3, 3, 5, 5, 4], output) < eps) def test_maximum_filter04(self): "maximum filter 4" - array = numarray.array([3, 2, 5, 1, 4]) - filter_shape = numarray.array([3]) + array = numpy.array([3, 2, 5, 1, 4]) + filter_shape = numpy.array([3]) output = ndimage.maximum_filter(array, filter_shape) self.failUnless(diff([3, 5, 5, 5, 4], output) < eps) def test_maximum_filter05(self): "maximum filter 5" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) - filter_shape = numarray.array([2, 3]) + filter_shape = numpy.array([2, 3]) output = ndimage.maximum_filter(array, filter_shape) self.failUnless(diff([[3, 5, 5, 5, 4], [7, 9, 9, 9, 5], @@ -890,7 +889,7 @@ def test_maximum_filter06(self): "maximum filter 6" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 1, 1], [1, 1, 1]] @@ -902,7 +901,7 @@ def test_maximum_filter07(self): "maximum filter 7" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -914,7 +913,7 @@ def test_maximum_filter08(self): "maximum filter 8" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -926,7 +925,7 @@ def test_maximum_filter09(self): "maximum filter 9" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -938,7 +937,7 @@ def test_rank01(self): "rank filter 1" - array = numarray.array([1, 2, 3, 4, 5]) + array = numpy.array([1, 2, 3, 4, 5]) output = ndimage.rank_filter(array, 1, size = 2) self.failUnless(diff(array, output) < eps) output = ndimage.percentile_filter(array, 100, size = 2) @@ -948,7 +947,7 @@ def test_rank02(self): "rank filter 2" - array = numarray.array([1, 2, 3, 4, 5]) + array = numpy.array([1, 2, 3, 4, 5]) output = ndimage.rank_filter(array, 1, size = [3]) self.failUnless(diff(array, output) < eps) output = ndimage.percentile_filter(array, 50, size = 3) @@ -958,7 +957,7 @@ def test_rank03(self): "rank filter 3" - array = numarray.array([3, 2, 5, 1, 4]) + array = numpy.array([3, 2, 5, 1, 4]) output = ndimage.rank_filter(array, 1, size = [2]) self.failUnless(diff([3, 3, 5, 5, 4], output) < eps) output = ndimage.percentile_filter(array, 100, size = 2) @@ -966,7 +965,7 @@ def test_rank04(self): "rank filter 4" - array = numarray.array([3, 2, 5, 1, 4]) + array = numpy.array([3, 2, 5, 1, 4]) true = [3, 3, 2, 4, 4] output = ndimage.rank_filter(array, 1, size = 3) self.failUnless(diff(true, output) < eps) @@ -977,14 +976,14 @@ def test_rank05(self): "rank filter 5" - array = numarray.array([3, 2, 5, 1, 4]) + array = numpy.array([3, 2, 5, 1, 4]) true = [3, 3, 2, 4, 4] output = ndimage.rank_filter(array, -2, size = 3) self.failUnless(diff(true, output) < eps) def test_rank06(self): "rank filter 6" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]]) true = [[2, 2, 1, 1, 1], @@ -998,7 +997,7 @@ def test_rank07(self): "rank filter 7" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]]) true = [[3, 5, 5, 5, 4], @@ -1009,13 +1008,13 @@ def test_rank08(self): "median filter 8" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]]) true = [[3, 3, 2, 4, 4], [5, 5, 5, 4, 4], [5, 6, 7, 5, 5]] - kernel = numarray.array([2, 3]) + kernel = numpy.array([2, 3]) output = ndimage.percentile_filter(array, 50.0, size = (2, 3)) self.failUnless(diff(true, output) < eps) @@ -1031,7 +1030,7 @@ [5, 5, 8, 3, 5]] footprint = [[1, 0, 1], [0, 1, 0]] for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) output = ndimage.rank_filter(array, 1, @@ -1043,7 +1042,7 @@ def test_rank10(self): "rank filter 10" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) true = [[2, 2, 1, 1, 1], @@ -1059,7 +1058,7 @@ def test_rank11(self): "rank filter 11" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) true = [[3, 5, 5, 5, 4], @@ -1081,7 +1080,7 @@ [5, 5, 8, 3, 5]] footprint = [[1, 0, 1], [0, 1, 0]] for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) output = ndimage.rank_filter(array, 1, @@ -1101,7 +1100,7 @@ [6, 6, 5, 5, 5]] footprint = [[1, 0, 1], [0, 1, 0]] for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) output = ndimage.rank_filter(array, 1, @@ -1115,7 +1114,7 @@ [5, 6, 6, 5, 5]] footprint = [[1, 0, 1], [0, 1, 0]] for type in self.types: - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) output = ndimage.rank_filter(array, 1, @@ -1124,7 +1123,7 @@ def test_generic_filter1d01(self): "generic 1d filter 1" - weights = numarray.array([1.1, 2.2, 3.3]) + weights = numpy.array([1.1, 2.2, 3.3]) def _filter_func(input, output, fltr, total): fltr = fltr / total for ii in range(input.shape[0] - 2): @@ -1132,7 +1131,7 @@ output[ii] += input[ii + 1] * fltr[1] output[ii] += input[ii + 2] * fltr[2] for type in self.types: - a = numarray.arange(12, dtype = type) + a = numpy.arange(12, dtype = type) a.shape = (3,4) r1 = ndimage.correlate1d(a, weights / weights.sum(), 0, origin = -1) @@ -1143,14 +1142,14 @@ def test_generic_filter01(self): "generic filter 1" - filter = numarray.array([[1.0, 2.0], [3.0, 4.0]]) - footprint = numarray.array([[1, 0], [0, 1]]) - cf = numarray.array([1., 4.]) + filter = numpy.array([[1.0, 2.0], [3.0, 4.0]]) + footprint = numpy.array([[1, 0], [0, 1]]) + cf = numpy.array([1., 4.]) def _filter_func(buffer, weights, total = 1.0): weights = cf / total return (buffer * weights).sum() for type in self.types: - a = numarray.arange(12, dtype = type) + a = numpy.arange(12, dtype = type) a.shape = (3,4) r1 = ndimage.correlate(a, filter * footprint) / 5 r2 = ndimage.generic_filter(a, _filter_func, @@ -1160,8 +1159,8 @@ def test_extend01(self): "line extension 1" - array = numarray.array([1, 2, 3]) - weights = numarray.array([1, 0]) + array = numpy.array([1, 2, 3]) + weights = numpy.array([1, 0]) true_values = [[1, 1, 2], [3, 1, 2], [1, 1, 2], @@ -1173,8 +1172,8 @@ def test_extend02(self): "line extension 2" - array = numarray.array([1, 2, 3]) - weights = numarray.array([1, 0, 0, 0, 0, 0, 0, 0]) + array = numpy.array([1, 2, 3]) + weights = numpy.array([1, 0, 0, 0, 0, 0, 0, 0]) true_values = [[1, 1, 1], [3, 1, 2], [3, 3, 2], @@ -1186,8 +1185,8 @@ def test_extend03(self): "line extension 3" - array = numarray.array([1, 2, 3]) - weights = numarray.array([0, 0, 1]) + array = numpy.array([1, 2, 3]) + weights = numpy.array([0, 0, 1]) true_values = [[2, 3, 3], [2, 3, 1], [2, 3, 3], @@ -1199,8 +1198,8 @@ def test_extend04(self): "line extension 4" - array = numarray.array([1, 2, 3]) - weights = numarray.array([0, 0, 0, 0, 0, 0, 0, 0, 1]) + array = numpy.array([1, 2, 3]) + weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1]) true_values = [[3, 3, 3], [2, 3, 1], [2, 1, 1], @@ -1213,10 +1212,10 @@ def test_extend05(self): "line extension 5" - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - weights = numarray.array([[1, 0], [0, 0]]) + weights = numpy.array([[1, 0], [0, 0]]) true_values = [[[1, 1, 2], [1, 1, 2], [4, 4, 5]], [[9, 7, 8], [3, 1, 2], [6, 4, 5]], [[1, 1, 2], [1, 1, 2], [4, 4, 5]], @@ -1229,10 +1228,10 @@ def test_extend06(self): "line extension 6" - array = numarray.array([[1, 2, 3], + array = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - weights = numarray.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]]) + weights = numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]]) true_values = [[[5, 6, 6], [8, 9, 9], [8, 9, 9]], [[5, 6, 4], [8, 9, 7], [2, 3, 1]], [[5, 6, 6], [8, 9, 9], [8, 9, 9]], @@ -1245,8 +1244,8 @@ def test_extend07(self): "line extension 7" - array = numarray.array([1, 2, 3]) - weights = numarray.array([0, 0, 0, 0, 0, 0, 0, 0, 1]) + array = numpy.array([1, 2, 3]) + weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1]) true_values = [[3, 3, 3], [2, 3, 1], [2, 1, 1], @@ -1258,8 +1257,8 @@ def test_extend08(self): "line extension 8" - array = numarray.array([[1], [2], [3]]) - weights = numarray.array([[0], [0], [0], [0], [0], [0], [0], + array = numpy.array([[1], [2], [3]]) + weights = numpy.array([[0], [0], [0], [0], [0], [0], [0], [0], [1]]) true_values = [[[3], [3], [3]], [[2], [3], [1]], @@ -1272,8 +1271,8 @@ def test_extend09(self): "line extension 9" - array = numarray.array([1, 2, 3]) - weights = numarray.array([0, 0, 0, 0, 0, 0, 0, 0, 1]) + array = numpy.array([1, 2, 3]) + weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1]) true_values = [[3, 3, 3], [2, 3, 1], [2, 1, 1], @@ -1285,8 +1284,8 @@ def test_extend10(self): "line extension 10" - array = numarray.array([[1], [2], [3]]) - weights = numarray.array([[0], [0], [0], [0], [0], [0], [0], + array = numpy.array([[1], [2], [3]]) + weights = numpy.array([[0], [0], [0], [0], [0], [0], [0], [0], [1]]) true_values = [[[3], [3], [3]], [[2], [3], [1]], @@ -1300,8 +1299,8 @@ def test_fourier_gaussian_real01(self): "gaussian fourier filter for real transforms 1" for shape in [(32, 16), (31, 15)]: - for type in [numarray.float32, numarray.float64]: - a = numarray.zeros(shape, type) + for type in [numpy.float32, numpy.float64]: + a = numpy.zeros(shape, type) a[0, 0] = 1.0 a = fft.rfft(a, shape[0], 0) a = fft.fft(a, shape[1], 1) @@ -1314,8 +1313,8 @@ def test_fourier_gaussian_complex01(self): "gaussian fourier filter for complex transforms 1" for shape in [(32, 16), (31, 15)]: - for type in [numarray.complex64, numarray.complex128]: - a = numarray.zeros(shape, type) + for type in [numpy.complex64, numpy.complex128]: + a = numpy.zeros(shape, type) a[0, 0] = 1.0 a = fft.fft(a, shape[0], 0) a = fft.fft(a, shape[1], 1) @@ -1329,8 +1328,8 @@ def test_fourier_uniform_real01(self): "uniform fourier filter for real transforms 1" for shape in [(32, 16), (31, 15)]: - for type in [numarray.float32, numarray.float64]: - a = numarray.zeros(shape, type) + for type in [numpy.float32, numpy.float64]: + a = numpy.zeros(shape, type) a[0, 0] = 1.0 a = fft.rfft(a, shape[0], 0) a = fft.fft(a, shape[1], 1) @@ -1343,8 +1342,8 @@ def test_fourier_uniform_complex01(self): "uniform fourier filter for complex transforms 1" for shape in [(32, 16), (31, 15)]: - for type in [numarray.complex64, numarray.complex128]: - a = numarray.zeros(shape, type) + for type in [numpy.complex64, numpy.complex128]: + a = numpy.zeros(shape, type) a[0, 0] = 1.0 a = fft.fft(a, shape[0], 0) a = fft.fft(a, shape[1], 1) @@ -1357,8 +1356,8 @@ def test_fourier_shift_real01(self): "shift filter for real transforms 1" for shape in [(32, 16), (31, 15)]: - for dtype in [numarray.float32, numarray.float64]: - true = numarray.arange(shape[0] * shape[1], dtype = dtype) + for dtype in [numpy.float32, numpy.float64]: + true = numpy.arange(shape[0] * shape[1], dtype = dtype) true.shape = shape a = fft.rfft(true, shape[0], 0) a = fft.fft(a, shape[1], 1) @@ -1366,14 +1365,14 @@ a = fft.ifft(a, shape[1], 1) a = fft.irfft(a, shape[0], 0) error1 = diff(a[1:, 1:], true[:-1, :-1]) - error2 = diff(a.imag, numarray.zeros(shape)) + error2 = diff(a.imag, numpy.zeros(shape)) self.failUnless(error1 < 1e-10 and error2 < 1e-10) def test_fourier_shift_complex01(self): "shift filter for complex transforms 1" for shape in [(32, 16), (31, 15)]: - for type in [numarray.complex64, numarray.complex128]: - true = numarray.arange(shape[0] * shape[1], + for type in [numpy.complex64, numpy.complex128]: + true = numpy.arange(shape[0] * shape[1], dtype = type) true.shape = shape a = fft.fft(true, shape[0], 0) @@ -1382,14 +1381,14 @@ a = fft.ifft(a, shape[1], 1) a = fft.ifft(a, shape[0], 0) error1 = diff(a.real[1:, 1:], true[:-1, :-1]) - error2 = diff(a.imag, numarray.zeros(shape)) + error2 = diff(a.imag, numpy.zeros(shape)) self.failUnless(error1 < 1e-10 and error2 < 1e-10) def test_fourier_ellipsoid_real01(self): "ellipsoid fourier filter for real transforms 1" for shape in [(32, 16), (31, 15)]: - for type in [numarray.float32, numarray.float64]: - a = numarray.zeros(shape, type) + for type in [numpy.float32, numpy.float64]: + a = numpy.zeros(shape, type) a[0, 0] = 1.0 a = fft.rfft(a, shape[0], 0) a = fft.fft(a, shape[1], 1) @@ -1402,8 +1401,8 @@ def test_fourier_ellipsoid_complex01(self): "ellipsoid fourier filter for complex transforms 1" for shape in [(32, 16), (31, 15)]: - for type in [numarray.complex64, numarray.complex128]: - a = numarray.zeros(shape, type) + for type in [numpy.complex64, numpy.complex128]: + a = numpy.zeros(shape, type) a[0, 0] = 1.0 a = fft.fft(a, shape[0], 0) a = fft.fft(a, shape[1], 1) @@ -1417,25 +1416,25 @@ def test_spline01(self): "spline filter 1" for type in self.types: - data = numarray.ones([], type) + data = numpy.ones([], type) for order in range(2, 6): out = ndimage.spline_filter(data, order = order) self.failUnless(diff(out, 1)< eps and - out.dtype.type == numarray.float64) + out.dtype.type == numpy.float64) def test_spline02(self): "spline filter 2" for type in self.types: - data = numarray.array([1]) + data = numpy.array([1]) for order in range(2, 6): out = ndimage.spline_filter(data, order = order) self.failUnless(diff(out, [1]) < eps and - out.dtype.type == numarray.float64) + out.dtype.type == numpy.float64) def test_spline03(self): "spline filter 3" for type in self.types: - data = numarray.ones([], type) + data = numpy.ones([], type) for order in range(2, 6): out = ndimage.spline_filter(data, order, output = type) @@ -1445,7 +1444,7 @@ def test_spline04(self): "spline filter 4" for type in self.types: - data = numarray.ones([4], type) + data = numpy.ones([4], type) for order in range(2, 6): out = ndimage.spline_filter(data, order) self.failUnless(diff(out, [1, 1, 1, 1]) < eps) @@ -1453,7 +1452,7 @@ def test_spline05(self): "spline filter 5" for type in self.types: - data = numarray.ones([4, 4], type) + data = numpy.ones([4, 4], type) for order in range(2, 6): out = ndimage.spline_filter(data, order = order) self.failUnless(diff(out, [[1, 1, 1, 1], @@ -1463,7 +1462,7 @@ def test_geometric_transform01(self): "geometric transform 1" - data = numarray.array([1]) + data = numpy.array([1]) def mapping(x): return x for order in range(0, 6): @@ -1474,7 +1473,7 @@ def test_geometric_transform02(self): "geometric transform 2" - data = numarray.ones([4]) + data = numpy.ones([4]) def mapping(x): return x for order in range(0, 6): @@ -1484,7 +1483,7 @@ def test_geometric_transform03(self): "geometric transform 3" - data = numarray.ones([4]) + data = numpy.ones([4]) def mapping(x): return (x[0] - 1,) for order in range(0, 6): @@ -1494,7 +1493,7 @@ def test_geometric_transform04(self): "geometric transform 4" - data = numarray.array([4, 1, 3, 2]) + data = numpy.array([4, 1, 3, 2]) def mapping(x): return (x[0] - 1,) for order in range(0, 6): @@ -1504,7 +1503,7 @@ def test_geometric_transform05(self): "geometric transform 5" - data = numarray.array([[1, 1, 1, 1], + data = numpy.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) def mapping(x): @@ -1518,7 +1517,7 @@ def test_geometric_transform06(self): "geometric transform 6" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) def mapping(x): @@ -1532,7 +1531,7 @@ def test_geometric_transform07(self): "geometric transform 7" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) def mapping(x): @@ -1546,7 +1545,7 @@ def test_geometric_transform08(self): "geometric transform 8" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) def mapping(x): @@ -1560,7 +1559,7 @@ def test_geometric_transform10(self): "geometric transform 10" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) def mapping(x): @@ -1579,7 +1578,7 @@ def test_geometric_transform13(self): "geometric transform 13" - data = numarray.ones([2], numarray.float64) + data = numpy.ones([2], numpy.float64) def mapping(x): return (x[0] / 2,) for order in range(0, 6): @@ -1682,9 +1681,9 @@ def test_geometric_transform22(self): "geometric transform 22" - data = numarray.array([[1, 2, 3, 4], + data = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], - [9, 10, 11, 12]], numarray.float64) + [9, 10, 11, 12]], numpy.float64) def mapping1(x): return (x[0] / 2, x[1] / 2) def mapping2(x): @@ -1707,7 +1706,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, (2,), order=order) - out = out.astype(numarray.int32) + out = out.astype(numpy.int32) self.failUnless(diff(out, [5, 7]) < eps) def test_geometric_transform24(self): @@ -1725,10 +1724,10 @@ def test_map_coordinates01(self): "map coordinates 1" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) - idx = numarray.indices(data.shape) + idx = numpy.indices(data.shape) idx -= 1 for order in range(0, 6): out = ndimage.map_coordinates(data, idx, order=order) @@ -1738,10 +1737,10 @@ def test_map_coordinates02(self): "map coordinates 2" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) - idx = numarray.indices(data.shape, numarray.float64) + idx = numpy.indices(data.shape, numpy.float64) idx -= 0.5 for order in range(0, 6): out1 = ndimage.shift(data, 0.5, order=order) @@ -1751,7 +1750,7 @@ def test_affine_transform01(self): "affine_transform 1" - data = numarray.array([1]) + data = numpy.array([1]) for order in range(0, 6): out = ndimage.affine_transform(data, [[1]], order=order) @@ -1759,7 +1758,7 @@ def test_affine_transform02(self): "affine transform 2" - data = numarray.ones([4]) + data = numpy.ones([4]) for order in range(0, 6): out = ndimage.affine_transform(data, [[1]], order=order) @@ -1767,7 +1766,7 @@ def test_affine_transform03(self): "affine transform 3" - data = numarray.ones([4]) + data = numpy.ones([4]) for order in range(0, 6): out = ndimage.affine_transform(data, [[1]], -1, order=order) @@ -1775,7 +1774,7 @@ def test_affine_transform04(self): "affine transform 4" - data = numarray.array([4, 1, 3, 2]) + data = numpy.array([4, 1, 3, 2]) for order in range(0, 6): out = ndimage.affine_transform(data, [[1]], -1, order=order) @@ -1783,7 +1782,7 @@ def test_affine_transform05(self): "affine transform 5" - data = numarray.array([[1, 1, 1, 1], + data = numpy.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) for order in range(0, 6): @@ -1796,7 +1795,7 @@ def test_affine_transform06(self): "affine transform 6" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) for order in range(0, 6): @@ -1809,7 +1808,7 @@ def test_affine_transform07(self): "affine transform 7" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) for order in range(0, 6): @@ -1822,7 +1821,7 @@ def test_affine_transform08(self): "affine transform 8" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) for order in range(0, 6): @@ -1835,7 +1834,7 @@ def test_affine_transform09(self): "affine transform 9" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) for order in range(0, 6): @@ -1853,7 +1852,7 @@ def test_affine_transform10(self): "affine transform 10" - data = numarray.ones([2], numarray.float64) + data = numpy.ones([2], numpy.float64) for order in range(0, 6): out = ndimage.affine_transform(data, [[0.5]], output_shape = (4,), order=order) @@ -1944,9 +1943,9 @@ def test_affine_transform19(self): "affine transform 19" - data = numarray.array([[1, 2, 3, 4], + data = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], - [9, 10, 11, 12]], numarray.float64) + [9, 10, 11, 12]], numpy.float64) for order in range(0, 6): out = ndimage.affine_transform(data, [[0.5, 0], @@ -1980,35 +1979,35 @@ def test_shift01(self): "shift 1" - data = numarray.array([1]) + data = numpy.array([1]) for order in range(0, 6): out = ndimage.shift(data, [1], order=order) self.failUnless(diff(out, [0]) < eps) def test_shift02(self): "shift 2" - data = numarray.ones([4]) + data = numpy.ones([4]) for order in range(0, 6): out = ndimage.shift(data, [1], order=order) self.failUnless(diff(out, [0, 1, 1, 1]) < eps) def test_shift03(self): "shift 3" - data = numarray.ones([4]) + data = numpy.ones([4]) for order in range(0, 6): out = ndimage.shift(data, -1, order=order) self.failUnless(diff(out, [1, 1, 1, 0]) < eps) def test_shift04(self): "shift 4" - data = numarray.array([4, 1, 3, 2]) + data = numpy.array([4, 1, 3, 2]) for order in range(0, 6): out = ndimage.shift(data, 1, order=order) self.failUnless(diff(out, [0, 4, 1, 3]) < eps) def test_shift05(self): "shift 5" - data = numarray.array([[1, 1, 1, 1], + data = numpy.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) for order in range(0, 6): @@ -2019,7 +2018,7 @@ def test_shift06(self): "shift 6" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) for order in range(0, 6): @@ -2030,7 +2029,7 @@ def test_shift07(self): "shift 7" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) for order in range(0, 6): @@ -2042,7 +2041,7 @@ def test_shift08(self): "shift 8" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) for order in range(0, 6): @@ -2053,7 +2052,7 @@ def test_shift09(self): "shift 9" - data = numarray.array([[4, 1, 3, 2], + data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]) for order in range(0, 6): @@ -2070,7 +2069,7 @@ def test_zoom01(self): "zoom 1" - data = numarray.ones([2], numarray.float64) + data = numpy.ones([2], numpy.float64) for order in range(0, 6): out = ndimage.zoom(data, 2.0, order=order) self.failUnless(diff(out, [1, 1, 1, 1]) < eps) @@ -2145,9 +2144,9 @@ def test_zoom10(self): "zoom 10" - data = numarray.array([[1, 2, 3, 4], + data = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], - [9, 10, 11, 12]], numarray.float64) + [9, 10, 11, 12]], numpy.float64) for order in range(0, 6): out = ndimage.zoom(data, [2, 2], order=order) out = ndimage.zoom(out, [0.5, 0.5], order=order) @@ -2165,93 +2164,93 @@ def test_rotate01(self): "rotate 1" - data = numarray.array([[0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0], [0, 1, 1, 0], - [0, 0, 0, 0]], dtype = numarray.float64) + [0, 0, 0, 0]], dtype = numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 0) self.failUnless(diff(out, data) < eps) def test_rotate02(self): "rotate 2" - data = numarray.array([[0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0], [0, 1, 0, 0], - [0, 0, 0, 0]], dtype = numarray.float64) - true = numarray.array([[0, 0, 0], + [0, 0, 0, 0]], dtype = numpy.float64) + true = numpy.array([[0, 0, 0], [0, 0, 0], [0, 1, 0], - [0, 0, 0]], dtype = numarray.float64) + [0, 0, 0]], dtype = numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 90) self.failUnless(diff(out, true) < eps) def test_rotate03(self): "rotate 3" - data = numarray.array([[0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], - [0, 0, 0, 0, 0]], dtype = numarray.float64) - true = numarray.array([[0, 0, 0], + [0, 0, 0, 0, 0]], dtype = numpy.float64) + true = numpy.array([[0, 0, 0], [0, 0, 0], [0, 1, 0], [0, 1, 0], - [0, 0, 0]], dtype = numarray.float64) + [0, 0, 0]], dtype = numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 90) self.failUnless(diff(out, true) < eps) def test_rotate04(self): "rotate 4" - data = numarray.array([[0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], - [0, 0, 0, 0, 0]], dtype = numarray.float64) - true = numarray.array([[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]], dtype = numpy.float64) + true = numpy.array([[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], - [0, 0, 1, 0, 0]], dtype = numarray.float64) + [0, 0, 1, 0, 0]], dtype = numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 90, reshape = False) self.failUnless(diff(out, true) < eps) def test_rotate05(self): "rotate 5" - data = numarray.array([[[0, 0, 0, 0, 0], + data = numpy.array([[[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0]]] * 3, - dtype = numarray.float64) - true = numarray.array([[[0, 0, 0], + dtype = numpy.float64) + true = numpy.array([[[0, 0, 0], [0, 0, 0], [0, 1, 0], [0, 1, 0], - [0, 0, 0]]] * 3, dtype = numarray.float64) + [0, 0, 0]]] * 3, dtype = numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 90) self.failUnless(diff(out, true) < eps) def test_rotate06(self): "rotate 6" - data = numarray.array([[[0, 0, 0, 0, 0], + data = numpy.array([[[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0]]] * 3, - dtype = numarray.float64) - true = numarray.array([[[0, 0, 0, 0, 0], + dtype = numpy.float64) + true = numpy.array([[[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]] * 3, - dtype = numarray.float64) + dtype = numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 90, reshape = False) self.failUnless(diff(out, true) < eps) def test_rotate07(self): "rotate 7" - data = numarray.array([[[0, 0, 0, 0, 0], + data = numpy.array([[[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0]]] * 2, - dtype = numarray.float64) + dtype = numpy.float64) data = data.transpose() - true = numarray.array([[[0, 0, 0], + true = numpy.array([[[0, 0, 0], [0, 1, 0], [0, 1, 0], [0, 0, 0], - [0, 0, 0]]] * 2, dtype = numarray.float64) + [0, 0, 0]]] * 2, dtype = numpy.float64) true = true.transpose([2,1,0]) for order in range(0, 6): @@ -2260,15 +2259,15 @@ def test_rotate08(self): "rotate 8" - data = numarray.array([[[0, 0, 0, 0, 0], + data = numpy.array([[[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0]]] * 2, - dtype = numarray.float64) + dtype = numpy.float64) data = data.transpose() - true = numarray.array([[[0, 0, 1, 0, 0], + true = numpy.array([[[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]] * 2, - dtype = numarray.float64) + dtype = numpy.float64) true = true.transpose() for order in range(0, 6): out = ndimage.rotate(data, 90, axes = (0, 1), @@ -2277,15 +2276,15 @@ def test_watershed_ift01(self): "watershed_ift 1" - data = numarray.array([[0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0]], numarray.uint8) - markers = numarray.array([[ -1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], numpy.uint8) + markers = numpy.array([[ -1, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 1, 0, 0, 0], @@ -2293,7 +2292,7 @@ [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]], - numarray.int8) + numpy.int8) out = ndimage.watershed_ift(data, markers, structure = [[1,1,1], [1,1,1], @@ -2310,15 +2309,15 @@ def test_watershed_ift02(self): "watershed_ift 2" - data = numarray.array([[0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0]], numarray.uint8) - markers = numarray.array([[ -1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], numpy.uint8) + markers = numpy.array([[ -1, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 1, 0, 0, 0], @@ -2326,7 +2325,7 @@ [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]], - numarray.int8) + numpy.int8) out = ndimage.watershed_ift(data, markers) error = diff([[-1, -1, -1, -1, -1, -1, -1], [-1, -1, 1, 1, 1, -1, -1], @@ -2340,21 +2339,21 @@ def test_watershed_ift03(self): "watershed_ift 3" - data = numarray.array([[0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], - [0, 0, 0, 0, 0, 0, 0]], numarray.uint8) - markers = numarray.array([[ 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], numpy.uint8) + markers = numpy.array([[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 2, 0, 3, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, -1]], - numarray.int8) + numpy.int8) out = ndimage.watershed_ift(data, markers) error = diff([[-1, -1, -1, -1, -1, -1, -1], [-1, -1, 2, -1, 3, -1, -1], @@ -2367,21 +2366,21 @@ def test_watershed_ift04(self): "watershed_ift 4" - data = numarray.array([[0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], - [0, 0, 0, 0, 0, 0, 0]], numarray.uint8) - markers = numarray.array([[ 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], numpy.uint8) + markers = numpy.array([[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 2, 0, 3, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, -1]], - numarray.int8) + numpy.int8) out = ndimage.watershed_ift(data, markers, structure = [[1,1,1], [1,1,1], @@ -2397,21 +2396,21 @@ def test_watershed_ift05(self): "watershed_ift 5" - data = numarray.array([[0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], - [0, 0, 0, 0, 0, 0, 0]], numarray.uint8) - markers = numarray.array([[ 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], numpy.uint8) + markers = numpy.array([[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 3, 0, 2, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, -1]], - numarray.int8) + numpy.int8) out = ndimage.watershed_ift(data, markers, structure = [[1,1,1], [1,1,1], @@ -2427,19 +2426,19 @@ def test_watershed_ift06(self): "watershed_ift 6" - data = numarray.array([[0, 1, 0, 0, 0, 1, 0], + data = numpy.array([[0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0]], numarray.uint8) - markers = numarray.array([[ -1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], numpy.uint8) + markers = numpy.array([[ -1, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 1, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]], - numarray.int8) + numpy.int8) out = ndimage.watershed_ift(data, markers, structure = [[1,1,1], [1,1,1], @@ -2455,22 +2454,22 @@ def test_watershed_ift07(self): "watershed_ift 7" shape = (7, 6) - data = numarray.zeros(shape, dtype = numarray.uint8) + data = numpy.zeros(shape, dtype = numpy.uint8) data = data.transpose() - data[...] = numarray.array([[0, 1, 0, 0, 0, 1, 0], + data[...] = numpy.array([[0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0]], numarray.uint8) - markers = numarray.array([[-1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], numpy.uint8) + markers = numpy.array([[-1, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 1, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]], - numarray.int8) - out = numarray.zeros(shape, dtype = numarray.int16) + numpy.int8) + out = numpy.zeros(shape, dtype = numpy.int16) out = out.transpose() ndimage.watershed_ift(data, markers, structure = [[1,1,1], @@ -2487,43 +2486,43 @@ def test_label01(self): "label 1" - data = numarray.ones([]) + data = numpy.ones([]) out, n = ndimage.label(data) self.failUnless(diff(out, 1) < eps and n == 1) def test_label02(self): "label 2" - data = numarray.zeros([]) + data = numpy.zeros([]) out, n = ndimage.label(data) self.failUnless(diff(out, 0) < eps and n == 0) def test_label03(self): "label 3" - data = numarray.ones([1]) + data = numpy.ones([1]) out, n = ndimage.label(data) self.failUnless(diff(out, [1]) < eps and n == 1) def test_label04(self): "label 4" - data = numarray.zeros([1]) + data = numpy.zeros([1]) out, n = ndimage.label(data) self.failUnless(diff(out, [0]) < eps and n == 0) def test_label05(self): "label 5" - data = numarray.ones([5]) + data = numpy.ones([5]) out, n = ndimage.label(data) self.failUnless(diff(out, [1, 1, 1, 1, 1]) < eps and n == 1) def test_label06(self): "label 6" - data = numarray.array([1, 0, 1, 1, 0, 1]) + data = numpy.array([1, 0, 1, 1, 0, 1]) out, n = ndimage.label(data) self.failUnless(diff(out, [1, 0, 2, 2, 0, 3]) < eps and n == 3) def test_label07(self): "label 7" - data = numarray.array([[0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], @@ -2539,7 +2538,7 @@ def test_label08(self): "label 8" - data = numarray.array([[1, 0, 0, 0, 0, 0], + data = numpy.array([[1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 0], @@ -2555,7 +2554,7 @@ def test_label09(self): "label 9" - data = numarray.array([[1, 0, 0, 0, 0, 0], + data = numpy.array([[1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 0], @@ -2572,7 +2571,7 @@ def test_label10(self): "label 10" - data = numarray.array([[0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 0], [0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0]]) @@ -2586,7 +2585,7 @@ def test_label11(self): "label 11" for type in self.types: - data = numarray.array([[1, 0, 0, 0, 0, 0], + data = numpy.array([[1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 0], @@ -2604,7 +2603,7 @@ def test_label12(self): "label 12" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 1, 1], + data = numpy.array([[0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 1, 1], [0, 0, 1, 1, 1, 1], @@ -2620,7 +2619,7 @@ def test_label13(self): "label 13" for type in self.types: - data = numarray.array([[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1], + data = numpy.array([[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1], [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], @@ -2634,37 +2633,37 @@ def test_find_objects01(self): "find_objects 1" - data = numarray.ones([]) + data = numpy.ones([], dtype=int) out = ndimage.find_objects(data) self.failUnless(out == [()]) def test_find_objects02(self): "find_objects 2" - data = numarray.zeros([]) + data = numpy.zeros([], dtype=int) out = ndimage.find_objects(data) self.failUnless(out == []) def test_find_objects03(self): "find_objects 3" - data = numarray.ones([1]) + data = numpy.ones([1], dtype=int) out = ndimage.find_objects(data) self.failUnless(out == [(slice(0, 1, None),)]) def test_find_objects04(self): "find_objects 4" - data = numarray.zeros([1]) + data = numpy.zeros([1], dtype=int) out = ndimage.find_objects(data) self.failUnless(out == []) def test_find_objects05(self): "find_objects 5" - data = numarray.ones([5]) + data = numpy.ones([5], dtype=int) out = ndimage.find_objects(data) self.failUnless(out == [(slice(0, 5, None),)]) def test_find_objects06(self): "find_objects 6" - data = numarray.array([1, 0, 2, 2, 0, 3]) + data = numpy.array([1, 0, 2, 2, 0, 3]) out = ndimage.find_objects(data) self.failUnless(out == [(slice(0, 1, None),), (slice(2, 4, None),), @@ -2672,7 +2671,7 @@ def test_find_objects07(self): "find_objects 7" - data = numarray.array([[0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], @@ -2683,7 +2682,7 @@ def test_find_objects08(self): "find_objects 8" - data = numarray.array([[1, 0, 0, 0, 0, 0], + data = numpy.array([[1, 0, 0, 0, 0, 0], [0, 0, 2, 2, 0, 0], [0, 0, 2, 2, 2, 0], [3, 3, 0, 0, 0, 0], @@ -2697,7 +2696,7 @@ def test_find_objects09(self): "find_objects 9" - data = numarray.array([[1, 0, 0, 0, 0, 0], + data = numpy.array([[1, 0, 0, 0, 0, 0], [0, 0, 2, 2, 0, 0], [0, 0, 2, 2, 2, 0], [0, 0, 0, 0, 0, 0], @@ -2712,190 +2711,190 @@ def test_sum01(self): "sum 1" for type in self.types: - input = numarray.array([], type) + input = numpy.array([], type) output = ndimage.sum(input) self.failUnless(output == 0.0) def test_sum02(self): "sum 2" for type in self.types: - input = numarray.zeros([0, 4], type) + input = numpy.zeros([0, 4], type) output = ndimage.sum(input) self.failUnless(output == 0.0) def test_sum03(self): "sum 3" for type in self.types: - input = numarray.ones([], type) + input = numpy.ones([], type) output = ndimage.sum(input) self.failUnless(output == 1.0) def test_sum04(self): "sum 4" for type in self.types: - input = numarray.array([1, 2], type) + input = numpy.array([1, 2], type) output = ndimage.sum(input) self.failUnless(output == 3.0) def test_sum05(self): "sum 5" for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.sum(input) self.failUnless(output == 10.0) def test_sum06(self): "sum 6" - labels = numarray.array([], bool) + labels = numpy.array([], bool) for type in self.types: - input = numarray.array([], type) + input = numpy.array([], type) output = ndimage.sum(input, labels = labels) self.failUnless(output == 0.0) def test_sum07(self): "sum 7" - labels = numarray.ones([0, 4], bool) + labels = numpy.ones([0, 4], bool) for type in self.types: - input = numarray.zeros([0, 4], type) + input = numpy.zeros([0, 4], type) output = ndimage.sum(input, labels = labels) self.failUnless(output == 0.0) def test_sum08(self): "sum 8" - labels = numarray.array([1, 0], bool) + labels = numpy.array([1, 0], bool) for type in self.types: - input = numarray.array([1, 2], type) + input = numpy.array([1, 2], type) output = ndimage.sum(input, labels = labels) self.failUnless(output == 1.0) def test_sum09(self): "sum 9" - labels = numarray.array([1, 0], bool) + labels = numpy.array([1, 0], bool) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.sum(input, labels = labels) self.failUnless(output == 4.0) def test_sum10(self): "sum 10" - labels = numarray.array([1, 0], bool) - input = numarray.array([[1, 2], [3, 4]], bool) + labels = numpy.array([1, 0], bool) + input = numpy.array([[1, 2], [3, 4]], bool) output = ndimage.sum(input, labels = labels) self.failUnless(output == 2.0) def test_sum11(self): "sum 11" - labels = numarray.array([1, 2], numarray.int8) + labels = numpy.array([1, 2], numpy.int8) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.sum(input, labels = labels, index = 2) self.failUnless(output == 6.0) def test_sum12(self): "sum 12" - labels = numarray.array([[1, 2], [2, 4]], numarray.int8) + labels = numpy.array([[1, 2], [2, 4]], numpy.int8) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.sum(input, labels = labels, index = [4, 8, 2]) self.failUnless(output == [4.0, 0.0, 5.0]) def test_mean01(self): "mean 1" - labels = numarray.array([1, 0], bool) + labels = numpy.array([1, 0], bool) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.mean(input, labels = labels) self.failUnless(output == 2.0) def test_mean02(self): "mean 2" - labels = numarray.array([1, 0], bool) - input = numarray.array([[1, 2], [3, 4]], bool) + labels = numpy.array([1, 0], bool) + input = numpy.array([[1, 2], [3, 4]], bool) output = ndimage.mean(input, labels = labels) self.failUnless(output == 1.0) def test_mean03(self): "mean 3" - labels = numarray.array([1, 2]) + labels = numpy.array([1, 2]) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.mean(input, labels = labels, index = 2) self.failUnless(output == 3.0) def test_mean04(self): "mean 4" - labels = numarray.array([[1, 2], [2, 4]], numarray.int8) + labels = numpy.array([[1, 2], [2, 4]], numpy.int8) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.mean(input, labels = labels, index = [4, 8, 2]) self.failUnless(output == [4.0, 0.0, 2.5]) def test_minimum01(self): "minimum 1" - labels = numarray.array([1, 0], bool) + labels = numpy.array([1, 0], bool) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.minimum(input, labels = labels) self.failUnless(output == 1.0) def test_minimum02(self): "minimum 2" - labels = numarray.array([1, 0], bool) - input = numarray.array([[2, 2], [2, 4]], bool) + labels = numpy.array([1, 0], bool) + input = numpy.array([[2, 2], [2, 4]], bool) output = ndimage.minimum(input, labels = labels) self.failUnless(output == 1.0) def test_minimum03(self): "minimum 3" - labels = numarray.array([1, 2]) + labels = numpy.array([1, 2]) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.minimum(input, labels = labels, index = 2) self.failUnless(output == 2.0) def test_minimum04(self): "minimum 4" - labels = numarray.array([[1, 2], [2, 3]]) + labels = numpy.array([[1, 2], [2, 3]]) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.minimum(input, labels = labels, index = [2, 3, 8]) self.failUnless(output == [2.0, 4.0, 0.0]) def test_maximum01(self): "maximum 1" - labels = numarray.array([1, 0], bool) + labels = numpy.array([1, 0], bool) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.maximum(input, labels = labels) self.failUnless(output == 3.0) def test_maximum02(self): "maximum 2" - labels = numarray.array([1, 0], bool) - input = numarray.array([[2, 2], [2, 4]], bool) + labels = numpy.array([1, 0], bool) + input = numpy.array([[2, 2], [2, 4]], bool) output = ndimage.maximum(input, labels = labels) self.failUnless(output == 1.0) def test_maximum03(self): "maximum 3" - labels = numarray.array([1, 2]) + labels = numpy.array([1, 2]) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.maximum(input, labels = labels, index = 2) self.failUnless(output == 4.0) def test_maximum04(self): "maximum 4" - labels = numarray.array([[1, 2], [2, 3]]) + labels = numpy.array([[1, 2], [2, 3]]) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.maximum(input, labels = labels, index = [2, 3, 8]) self.failUnless(output == [3.0, 4.0, 0.0]) @@ -2903,27 +2902,27 @@ def test_variance01(self): "variance 1" for type in self.types: - input = numarray.array([], type) + input = numpy.array([], type) output = ndimage.variance(input) self.failUnless(float(output) == 0.0) def test_variance02(self): "variance 2" for type in self.types: - input = numarray.array([1], type) + input = numpy.array([1], type) output = ndimage.variance(input) self.failUnless(float(output) == 0.0) def test_variance03(self): "variance 3" for type in self.types: - input = numarray.array([1, 3], type) + input = numpy.array([1, 3], type) output = ndimage.variance(input) self.failUnless(output == 2.0) def test_variance04(self): "variance 4" - input = numarray.array([1, 0], bool) + input = numpy.array([1, 0], bool) output = ndimage.variance(input) self.failUnless(output == 0.5) @@ -2931,7 +2930,7 @@ "variance 5" labels = [2, 2, 3] for type in self.types: - input = numarray.array([1, 3, 8], type) + input = numpy.array([1, 3, 8], type) output = ndimage.variance(input, labels, 2) self.failUnless(output == 2.0) @@ -2939,34 +2938,34 @@ "variance 6" labels = [2, 2, 3, 3, 4] for type in self.types: - input = numarray.array([1, 3, 8, 10, 8], type) + input = numpy.array([1, 3, 8, 10, 8], type) output = ndimage.variance(input, labels, [2, 3, 4]) self.failUnless(output == [2.0, 2.0, 0.0]) def test_standard_deviation01(self): "standard deviation 1" for type in self.types: - input = numarray.array([], type) + input = numpy.array([], type) output = ndimage.standard_deviation(input) self.failUnless(float(output) == 0.0) def test_standard_deviation02(self): "standard deviation 2" for type in self.types: - input = numarray.array([1], type) + input = numpy.array([1], type) output = ndimage.standard_deviation(input) self.failUnless(float(output) == 0.0) def test_standard_deviation03(self): "standard deviation 3" for type in self.types: - input = numarray.array([1, 3], type) + input = numpy.array([1, 3], type) output = ndimage.standard_deviation(input) self.failUnless(output == math.sqrt(2.0)) def test_standard_deviation04(self): "standard deviation 4" - input = numarray.array([1, 0], bool) + input = numpy.array([1, 0], bool) output = ndimage.standard_deviation(input) self.failUnless(output == math.sqrt(0.5)) @@ -2974,7 +2973,7 @@ "standard deviation 5" labels = [2, 2, 3] for type in self.types: - input = numarray.array([1, 3, 8], type) + input = numpy.array([1, 3, 8], type) output = ndimage.standard_deviation(input, labels, 2) self.failUnless(output == math.sqrt(2.0)) @@ -2982,7 +2981,7 @@ "standard deviation 6" labels = [2, 2, 3, 3, 4] for type in self.types: - input = numarray.array([1, 3, 8, 10, 8], type) + input = numpy.array([1, 3, 8, 10, 8], type) output = ndimage.standard_deviation(input, labels, [2, 3, 4]) self.failUnless(output == [math.sqrt(2.0), math.sqrt(2.0), @@ -2990,9 +2989,9 @@ def test_minimum_position01(self): "minimum position 1" - labels = numarray.array([1, 0], bool) + labels = numpy.array([1, 0], bool) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.minimum_position(input, labels = labels) self.failUnless(output == (0, 0)) @@ -3000,7 +2999,7 @@ def test_minimum_position02(self): "minimum position 2" for type in self.types: - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 0, 2], [1, 5, 1, 1]], type) output = ndimage.minimum_position(input) @@ -3008,7 +3007,7 @@ def test_minimum_position03(self): "minimum position 3" - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 0, 2], [1, 5, 1, 1]], bool) output = ndimage.minimum_position(input) @@ -3016,7 +3015,7 @@ def test_minimum_position04(self): "minimum position 4" - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 1, 2], [1, 5, 1, 1]], bool) output = ndimage.minimum_position(input) @@ -3026,7 +3025,7 @@ "minimum position 5" labels = [1, 2, 0, 4] for type in self.types: - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 0, 2], [1, 5, 2, 3]], type) output = ndimage.minimum_position(input, labels) @@ -3036,7 +3035,7 @@ "minimum position 6" labels = [1, 2, 3, 4] for type in self.types: - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 0, 2], [1, 5, 1, 1]], type) output = ndimage.minimum_position(input, labels, 2) @@ -3046,7 +3045,7 @@ "minimum position 7" labels = [1, 2, 3, 4] for type in self.types: - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 0, 2], [1, 5, 1, 1]], type) output = ndimage.minimum_position(input, labels, @@ -3055,9 +3054,9 @@ def test_maximum_position01(self): "maximum position 1" - labels = numarray.array([1, 0], bool) + labels = numpy.array([1, 0], bool) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.maximum_position(input, labels = labels) self.failUnless(output == (1, 0)) @@ -3065,7 +3064,7 @@ def test_maximum_position02(self): "maximum position 2" for type in self.types: - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 8, 2], [1, 5, 1, 1]], type) output = ndimage.maximum_position(input) @@ -3073,7 +3072,7 @@ def test_maximum_position03(self): "maximum position 3" - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 8, 2], [1, 5, 1, 1]], bool) output = ndimage.maximum_position(input) @@ -3083,7 +3082,7 @@ "maximum position 4" labels = [1, 2, 0, 4] for type in self.types: - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 8, 2], [1, 5, 1, 1]], type) output = ndimage.maximum_position(input, labels) @@ -3093,7 +3092,7 @@ "maximum position 5" labels = [1, 2, 0, 4] for type in self.types: - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 8, 2], [1, 5, 1, 1]], type) output = ndimage.maximum_position(input, labels, 1) @@ -3103,7 +3102,7 @@ "maximum position 6" labels = [1, 2, 0, 4] for type in self.types: - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 8, 2], [1, 5, 1, 1]], type) output = ndimage.maximum_position(input, labels, @@ -3112,9 +3111,9 @@ def test_extrema01(self): "extrema 1" - labels = numarray.array([1, 0], bool) + labels = numpy.array([1, 0], bool) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output1 = ndimage.extrema(input, labels = labels) output2 = ndimage.minimum(input, labels = labels) output3 = ndimage.maximum(input, labels = labels) @@ -3127,9 +3126,9 @@ def test_extrema02(self): "extrema 2" - labels = numarray.array([1, 2]) + labels = numpy.array([1, 2]) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output1 = ndimage.extrema(input, labels = labels, index = 2) output2 = ndimage.minimum(input, labels = labels, @@ -3145,9 +3144,9 @@ def test_extrema03(self): "extrema 3" - labels = numarray.array([[1, 2], [2, 3]]) + labels = numpy.array([[1, 2], [2, 3]]) for type in self.types: - input = numarray.array([[1, 2], [3, 4]], type) + input = numpy.array([[1, 2], [3, 4]], type) output1 = ndimage.extrema(input, labels = labels, index = [2, 3, 8]) output2 = ndimage.minimum(input, labels = labels, @@ -3165,7 +3164,7 @@ "extrema 4" labels = [1, 2, 0, 4] for type in self.types: - input = numarray.array([[5, 4, 2, 5], + input = numpy.array([[5, 4, 2, 5], [3, 7, 8, 2], [1, 5, 1, 1]], type) output1 = ndimage.extrema(input, labels, [1, 2]) @@ -3182,7 +3181,7 @@ "center of mass 1" true = [0.0, 0.0] for type in self.types: - input = numarray.array([[1, 0], [0, 0]], type) + input = numpy.array([[1, 0], [0, 0]], type) output = ndimage.center_of_mass(input) e = diff(true, output) self.failUnless(e < eps) @@ -3191,7 +3190,7 @@ "center of mass 2" true = [1, 0] for type in self.types: - input = numarray.array([[0, 0], [1, 0]], type) + input = numpy.array([[0, 0], [1, 0]], type) output = ndimage.center_of_mass(input) e = diff(true, output) self.failUnless(e < eps) @@ -3200,7 +3199,7 @@ "center of mass 3" true = [0, 1] for type in self.types: - input = numarray.array([[0, 1], [0, 0]], type) + input = numpy.array([[0, 1], [0, 0]], type) output = ndimage.center_of_mass(input) e = diff(true, output) self.failUnless(e < eps) @@ -3209,7 +3208,7 @@ "center of mass 4" true = [1, 1] for type in self.types: - input = numarray.array([[0, 0], [0, 1]], type) + input = numpy.array([[0, 0], [0, 1]], type) output = ndimage.center_of_mass(input) e = diff(true, output) self.failUnless(e < eps) @@ -3218,7 +3217,7 @@ "center of mass 5" true = [0.5, 0.5] for type in self.types: - input = numarray.array([[1, 1], [1, 1]], type) + input = numpy.array([[1, 1], [1, 1]], type) output = ndimage.center_of_mass(input) e = diff(true, output) self.failUnless(e < eps) @@ -3226,7 +3225,7 @@ def test_center_of_mass06(self): "center of mass 6" true = [0.5, 0.5] - input = numarray.array([[1, 2], [3, 1]], bool) + input = numpy.array([[1, 2], [3, 1]], bool) output = ndimage.center_of_mass(input) e = diff(true, output) self.failUnless(e < eps) @@ -3235,7 +3234,7 @@ "center of mass 7" labels = [1, 0] true = [0.5, 0.0] - input = numarray.array([[1, 2], [3, 1]], bool) + input = numpy.array([[1, 2], [3, 1]], bool) output = ndimage.center_of_mass(input, labels) e = diff(true, output) self.failUnless(e < eps) @@ -3244,7 +3243,7 @@ "center of mass 8" labels = [1, 2] true = [0.5, 1.0] - input = numarray.array([[5, 2], [3, 1]], bool) + input = numpy.array([[5, 2], [3, 1]], bool) output = ndimage.center_of_mass(input, labels, 2) e = diff(true, output) self.failUnless(e < eps) @@ -3254,15 +3253,15 @@ "center of mass 9" labels = [1, 2] true = [(0.5, 0.0), (0.5, 1.0)] - input = numarray.array([[1, 2], [1, 1]], bool) + input = numpy.array([[1, 2], [1, 1]], bool) output = ndimage.center_of_mass(input, labels, [1, 2]) e = diff(true, output) self.failUnless(e < eps) def test_histogram01(self): "histogram 1" - true = numarray.ones(10) - input = numarray.arange(10) + true = numpy.ones(10) + input = numpy.arange(10) output = ndimage.histogram(input, 0, 10, 10) e = diff(true, output) self.failUnless(e < eps) @@ -3271,7 +3270,7 @@ "histogram 2" labels = [1, 1, 1, 1, 2, 2, 2, 2] true = [0, 2, 0, 1, 0] - input = numarray.array([1, 1, 3, 4, 3, 3, 3, 3]) + input = numpy.array([1, 1, 3, 4, 3, 3, 3, 3]) output = ndimage.histogram(input, 0, 4, 5, labels, 1) e = diff(true, output) self.failUnless(e < eps) @@ -3281,7 +3280,7 @@ labels = [1, 0, 1, 1, 2, 2, 2, 2] true1 = [0, 1, 0, 1, 0] true2 = [0, 0, 0, 3, 0] - input = numarray.array([1, 1, 3, 4, 3, 5, 3, 3]) + input = numpy.array([1, 1, 3, 4, 3, 5, 3, 3]) output = ndimage.histogram(input, 0, 4, 5, labels, (1,2)) e1 = diff(true1, output[0]) e2 = diff(true2, output[1]) @@ -3290,7 +3289,7 @@ def test_distance_transform_bf01(self): "brute force distance transform 1" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3334,7 +3333,7 @@ def test_distance_transform_bf02(self): "brute force distance transform 2" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3377,7 +3376,7 @@ def test_distance_transform_bf03(self): "brute force distance transform 3" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3420,7 +3419,7 @@ def test_distance_transform_bf04(self): "brute force distance transform 4" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3433,13 +3432,13 @@ return_indices = 1) dts = [] fts = [] - dt = numarray.zeros(data.shape, dtype = numarray.float64) + dt = numpy.zeros(data.shape, dtype = numpy.float64) ndimage.distance_transform_bf(data, distances = dt) dts.append(dt) ft = ndimage.distance_transform_bf(data, return_distances = False, return_indices = 1) fts.append(ft) - ft = numarray.indices(data.shape, dtype = numarray.int32) + ft = numpy.indices(data.shape, dtype = numpy.int32) ndimage.distance_transform_bf(data, return_distances = False, return_indices = True, indices = ft) fts.append(ft) @@ -3447,18 +3446,18 @@ return_indices = 1) dts.append(dt) fts.append(ft) - dt = numarray.zeros(data.shape, dtype = numarray.float64) + dt = numpy.zeros(data.shape, dtype = numpy.float64) ft = ndimage.distance_transform_bf(data, distances = dt, return_indices = True) dts.append(dt) fts.append(ft) - ft = numarray.indices(data.shape, dtype = numarray.int32) + ft = numpy.indices(data.shape, dtype = numpy.int32) dt = ndimage.distance_transform_bf(data, return_indices = True, indices = ft) dts.append(dt) fts.append(ft) - dt = numarray.zeros(data.shape, dtype = numarray.float64) - ft = numarray.indices(data.shape, dtype = numarray.int32) + dt = numpy.zeros(data.shape, dtype = numpy.float64) + ft = numpy.indices(data.shape, dtype = numpy.int32) ndimage.distance_transform_bf(data, distances = dt, return_indices = True, indices = ft) dts.append(dt) @@ -3471,7 +3470,7 @@ def test_distance_transform_bf05(self): "brute force distance transform 5" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3514,7 +3513,7 @@ def test_distance_transform_bf06(self): "brute force distance transform 6" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3558,7 +3557,7 @@ def test_distance_transform_cdt01(self): "chamfer type distance transform 1" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3594,7 +3593,7 @@ def test_distance_transform_cdt02(self): "chamfer type distance transform 2" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3630,7 +3629,7 @@ def test_distance_transform_cdt03(self): "chamfer type distance transform 3" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3643,13 +3642,13 @@ return_indices = True) dts = [] fts = [] - dt = numarray.zeros(data.shape, dtype = numarray.int32) + dt = numpy.zeros(data.shape, dtype = numpy.int32) ndimage.distance_transform_cdt(data, distances = dt) dts.append(dt) ft = ndimage.distance_transform_cdt(data, return_distances = False, return_indices = True) fts.append(ft) - ft = numarray.indices(data.shape, dtype = numarray.int32) + ft = numpy.indices(data.shape, dtype = numpy.int32) ndimage.distance_transform_cdt(data, return_distances = False, return_indices = True, indices = ft) fts.append(ft) @@ -3657,18 +3656,18 @@ return_indices = True) dts.append(dt) fts.append(ft) - dt = numarray.zeros(data.shape, dtype = numarray.int32) + dt = numpy.zeros(data.shape, dtype = numpy.int32) ft = ndimage.distance_transform_cdt(data, distances = dt, return_indices = True) dts.append(dt) fts.append(ft) - ft = numarray.indices(data.shape, dtype = numarray.int32) + ft = numpy.indices(data.shape, dtype = numpy.int32) dt = ndimage.distance_transform_cdt(data, return_indices = True, indices = ft) dts.append(dt) fts.append(ft) - dt = numarray.zeros(data.shape, dtype = numarray.int32) - ft = numarray.indices(data.shape, dtype = numarray.int32) + dt = numpy.zeros(data.shape, dtype = numpy.int32) + ft = numpy.indices(data.shape, dtype = numpy.int32) ndimage.distance_transform_cdt(data, distances = dt, return_indices = True, indices = ft) dts.append(dt) @@ -3681,7 +3680,7 @@ def test_distance_transform_edt01(self): "euclidean distance transform 1" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3695,18 +3694,18 @@ bf = ndimage.distance_transform_bf(data, 'euclidean') error1 = diff(bf, out) - dt = ft - numarray.indices(ft.shape[1:], dtype = ft.dtype) - dt = dt.astype(numarray.float64) - numarray.multiply(dt, dt, dt) - dt = numarray.add.reduce(dt, axis = 0) - numarray.sqrt(dt, dt) + dt = ft - numpy.indices(ft.shape[1:], dtype = ft.dtype) + dt = dt.astype(numpy.float64) + numpy.multiply(dt, dt, dt) + dt = numpy.add.reduce(dt, axis = 0) + numpy.sqrt(dt, dt) error2 = diff(bf, dt) self.failUnless(error1 < eps and error2 < eps) def test_distance_transform_edt02(self): "euclidean distance transform 2" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3719,13 +3718,13 @@ return_indices = True) dts = [] fts = [] - dt = numarray.zeros(data.shape, dtype = numarray.float64) + dt = numpy.zeros(data.shape, dtype = numpy.float64) ndimage.distance_transform_edt(data, distances = dt) dts.append(dt) ft = ndimage.distance_transform_edt(data, return_distances = 0, return_indices = True) fts.append(ft) - ft = numarray.indices(data.shape, dtype = numarray.int32) + ft = numpy.indices(data.shape, dtype = numpy.int32) ndimage.distance_transform_edt(data, return_distances = False,return_indices = True, indices = ft) fts.append(ft) @@ -3733,18 +3732,18 @@ return_indices = True) dts.append(dt) fts.append(ft) - dt = numarray.zeros(data.shape, dtype = numarray.float64) + dt = numpy.zeros(data.shape, dtype = numpy.float64) ft = ndimage.distance_transform_edt(data, distances = dt, return_indices = True) dts.append(dt) fts.append(ft) - ft = numarray.indices(data.shape, dtype = numarray.int32) + ft = numpy.indices(data.shape, dtype = numpy.int32) dt = ndimage.distance_transform_edt(data, return_indices = True, indices = ft) dts.append(dt) fts.append(ft) - dt = numarray.zeros(data.shape, dtype = numarray.float64) - ft = numarray.indices(data.shape, dtype = numarray.int32) + dt = numpy.zeros(data.shape, dtype = numpy.float64) + ft = numpy.indices(data.shape, dtype = numpy.int32) ndimage.distance_transform_edt(data, distances = dt, return_indices = True, indices = ft) dts.append(dt) @@ -3757,7 +3756,7 @@ def test_distance_transform_edt03(self): "euclidean distance transform 3" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3776,7 +3775,7 @@ def test_distance_transform_edt4(self): "euclidean distance transform 4" for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0], @@ -3855,63 +3854,63 @@ def test_binary_erosion01(self): "binary erosion 1" for type in self.types: - data = numarray.ones([], type) + data = numpy.ones([], type) out = ndimage.binary_erosion(data) self.failUnless(diff(out, 1) < eps) def test_binary_erosion02(self): "binary erosion 2" for type in self.types: - data = numarray.ones([], type) + data = numpy.ones([], type) out = ndimage.binary_erosion(data, border_value = 1) self.failUnless(diff(out, 1) < eps) def test_binary_erosion03(self): "binary erosion 3" for type in self.types: - data = numarray.ones([1], type) + data = numpy.ones([1], type) out = ndimage.binary_erosion(data) self.failUnless(diff(out, [0]) < eps) def test_binary_erosion04(self): "binary erosion 4" for type in self.types: - data = numarray.ones([1], type) + data = numpy.ones([1], type) out = ndimage.binary_erosion(data, border_value = 1) self.failUnless(diff(out, [1]) < eps) def test_binary_erosion05(self): "binary erosion 5" for type in self.types: - data = numarray.ones([3], type) + data = numpy.ones([3], type) out = ndimage.binary_erosion(data) self.failUnless(diff(out, [0, 1, 0]) < eps) def test_binary_erosion06(self): "binary erosion 6" for type in self.types: - data = numarray.ones([3], type) + data = numpy.ones([3], type) out = ndimage.binary_erosion(data, border_value = 1) self.failUnless(diff(out, [1, 1, 1]) < eps) def test_binary_erosion07(self): "binary erosion 7" for type in self.types: - data = numarray.ones([5], type) + data = numpy.ones([5], type) out = ndimage.binary_erosion(data) self.failUnless(diff(out, [0, 1, 1, 1, 0]) < eps) def test_binary_erosion08(self): "binary erosion 8" for type in self.types: - data = numarray.ones([5], type) + data = numpy.ones([5], type) out = ndimage.binary_erosion(data, border_value = 1) self.failUnless(diff(out, [1, 1, 1, 1, 1]) < eps) def test_binary_erosion09(self): "binary erosion 9" for type in self.types: - data = numarray.ones([5], type) + data = numpy.ones([5], type) data[2] = 0 out = ndimage.binary_erosion(data) self.failUnless(diff(out, [0, 0, 0, 0, 0]) < eps) @@ -3919,7 +3918,7 @@ def test_binary_erosion10(self): "binary erosion 10" for type in self.types: - data = numarray.ones([5], type) + data = numpy.ones([5], type) data[2] = 0 out = ndimage.binary_erosion(data, border_value = 1) self.failUnless(diff(out, [1, 0, 0, 0, 1]) < eps) @@ -3927,7 +3926,7 @@ def test_binary_erosion11(self): "binary erosion 11" for type in self.types: - data = numarray.ones([5], type) + data = numpy.ones([5], type) data[2] = 0 struct = [1, 0, 1] out = ndimage.binary_erosion(data, struct, @@ -3937,7 +3936,7 @@ def test_binary_erosion12(self): "binary erosion 12" for type in self.types: - data = numarray.ones([5], type) + data = numpy.ones([5], type) data[2] = 0 struct = [1, 0, 1] out = ndimage.binary_erosion(data, struct, @@ -3948,7 +3947,7 @@ def test_binary_erosion13(self): "binary erosion 13" for type in self.types: - data = numarray.ones([5], type) + data = numpy.ones([5], type) data[2] = 0 struct = [1, 0, 1] out = ndimage.binary_erosion(data, struct, @@ -3959,7 +3958,7 @@ def test_binary_erosion14(self): "binary erosion 14" for type in self.types: - data = numarray.ones([5], type) + data = numpy.ones([5], type) data[2] = 0 struct = [1, 1] out = ndimage.binary_erosion(data, struct, @@ -3969,7 +3968,7 @@ def test_binary_erosion15(self): "binary erosion 15" for type in self.types: - data = numarray.ones([5], type) + data = numpy.ones([5], type) data[2] = 0 struct = [1, 1] out = ndimage.binary_erosion(data, struct, @@ -3980,35 +3979,35 @@ def test_binary_erosion16(self): "binary erosion 16" for type in self.types: - data = numarray.ones([1, 1], type) + data = numpy.ones([1, 1], type) out = ndimage.binary_erosion(data, border_value = 1) self.failUnless(diff(out, [[1]]) < eps) def test_binary_erosion17(self): "binary erosion 17" for type in self.types: - data = numarray.ones([1, 1], type) + data = numpy.ones([1, 1], type) out = ndimage.binary_erosion(data) self.failUnless(diff(out, [[0]]) < eps) def test_binary_erosion18(self): "binary erosion 18" for type in self.types: - data = numarray.ones([1, 3], type) + data = numpy.ones([1, 3], type) out = ndimage.binary_erosion(data) self.failUnless(diff(out, [[0, 0, 0]]) < eps) def test_binary_erosion19(self): "binary erosion 19" for type in self.types: - data = numarray.ones([1, 3], type) + data = numpy.ones([1, 3], type) out = ndimage.binary_erosion(data, border_value = 1) self.failUnless(diff(out, [[1, 1, 1]]) < eps) def test_binary_erosion20(self): "binary erosion 20" for type in self.types: - data = numarray.ones([3, 3], type) + data = numpy.ones([3, 3], type) out = ndimage.binary_erosion(data) self.failUnless(diff(out, [[0, 0, 0], [0, 1, 0], @@ -4017,7 +4016,7 @@ def test_binary_erosion21(self): "binary erosion 21" for type in self.types: - data = numarray.ones([3, 3], type) + data = numpy.ones([3, 3], type) out = ndimage.binary_erosion(data, border_value = 1) self.failUnless(diff(out, [[1, 1, 1], [1, 1, 1], @@ -4034,7 +4033,7 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1], @@ -4057,7 +4056,7 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1], @@ -4082,7 +4081,7 @@ [0, 0, 1, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1], @@ -4108,7 +4107,7 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 1, 1, 1, 0, 1, 1], @@ -4134,7 +4133,7 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 1, 1, 1, 0, 1, 1], @@ -4158,7 +4157,7 @@ [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0], @@ -4181,14 +4180,14 @@ [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], bool) - out = numarray.zeros(data.shape, bool) + out = numpy.zeros(data.shape, bool) ndimage.binary_erosion(data, struct, border_value = 1, iterations = 2, output = out) self.failUnless(diff(out, true) < eps) @@ -4205,7 +4204,7 @@ [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 1, 0, 0, 0], + data = numpy.array([[0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1], @@ -4228,14 +4227,14 @@ [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 1, 0, 0, 0], + data = numpy.array([[0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0]], bool) - out = numarray.zeros(data.shape, bool) + out = numpy.zeros(data.shape, bool) ndimage.binary_erosion(data, struct, border_value = 1, iterations = 3, output = out) self.failUnless(diff(out, true) < eps) @@ -4252,14 +4251,14 @@ [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 1]] - data = numarray.array([[0, 0, 0, 1, 0, 0, 0], + data = numpy.array([[0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0]], bool) - out = numarray.zeros(data.shape, bool) + out = numpy.zeros(data.shape, bool) ndimage.binary_erosion(data, struct, border_value = 1, iterations = 1, output = out, origin = (-1, -1)) self.failUnless(diff(out, true) < eps) @@ -4276,7 +4275,7 @@ [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0], @@ -4306,7 +4305,7 @@ [1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1]] - data = numarray.array([[0, 0, 0, 0, 0, 1, 1], + data = numpy.array([[0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 0, 0, 1], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], @@ -4336,7 +4335,7 @@ [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0], @@ -4359,7 +4358,7 @@ [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 1, 0, 0, 0], + data = numpy.array([[0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1], @@ -4373,10 +4372,10 @@ [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 1]] - true = numarray.logical_and(tmp, mask) - tmp = numarray.logical_and(data, numarray.logical_not(mask)) - true = numarray.logical_or(true, tmp) - out = numarray.zeros(data.shape, bool) + true = numpy.logical_and(tmp, mask) + tmp = numpy.logical_and(data, numpy.logical_not(mask)) + true = numpy.logical_or(true, tmp) + out = numpy.zeros(data.shape, bool) ndimage.binary_erosion(data, struct, border_value = 1, iterations = 1, output = out, origin = (-1, -1), mask = mask) @@ -4403,7 +4402,7 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1]] - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 1, 1, 1, 0, 1, 1], @@ -4411,9 +4410,9 @@ [0, 1, 0, 1, 1, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) - true = numarray.logical_and(tmp, mask) - tmp = numarray.logical_and(data, numarray.logical_not(mask)) - true = numarray.logical_or(true, tmp) + true = numpy.logical_and(tmp, mask) + tmp = numpy.logical_and(data, numpy.logical_not(mask)) + true = numpy.logical_or(true, tmp) out = ndimage.binary_erosion(data, struct, mask = mask, border_value = 1, origin = (-1, -1)) self.failUnless(diff(out, true) < eps) @@ -4421,42 +4420,42 @@ def test_binary_dilation01(self): "binary dilation 1" for type in self.types: - data = numarray.ones([], type) + data = numpy.ones([], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, 1) < eps) def test_binary_dilation02(self): "binary dilation 2" for type in self.types: - data = numarray.zeros([], type) + data = numpy.zeros([], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, 0) < eps) def test_binary_dilation03(self): "binary dilation 3" for type in self.types: - data = numarray.ones([1], type) + data = numpy.ones([1], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, [1]) < eps) def test_binary_dilation04(self): "binary dilation 4" for type in self.types: - data = numarray.zeros([1], type) + data = numpy.zeros([1], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, [0]) < eps) def test_binary_dilation05(self): "binary dilation 5" for type in self.types: - data = numarray.ones([3], type) + data = numpy.ones([3], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, [1, 1, 1]) < eps) def test_binary_dilation06(self): "binary dilation 6" for type in self.types: - data = numarray.zeros([3], type) + data = numpy.zeros([3], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, [0, 0, 0]) < eps) @@ -4464,7 +4463,7 @@ "binary dilation 7" struct = ndimage.generate_binary_structure(1, 1) for type in self.types: - data = numarray.zeros([3], type) + data = numpy.zeros([3], type) data[1] = 1 out = ndimage.binary_dilation(data) self.failUnless(diff(out, [1, 1, 1]) < eps) @@ -4472,7 +4471,7 @@ def test_binary_dilation08(self): "binary dilation 8" for type in self.types: - data = numarray.zeros([5], type) + data = numpy.zeros([5], type) data[1] = 1 data[3] = 1 out = ndimage.binary_dilation(data) @@ -4481,7 +4480,7 @@ def test_binary_dilation09(self): "binary dilation 9" for type in self.types: - data = numarray.zeros([5], type) + data = numpy.zeros([5], type) data[1] = 1 out = ndimage.binary_dilation(data) self.failUnless(diff(out, [1, 1, 1, 0, 0]) < eps) @@ -4489,7 +4488,7 @@ def test_binary_dilation10(self): "binary dilation 10" for type in self.types: - data = numarray.zeros([5], type) + data = numpy.zeros([5], type) data[1] = 1 out = ndimage.binary_dilation(data, origin = -1) self.failUnless(diff(out, [0, 1, 1, 1, 0]) < eps) @@ -4497,7 +4496,7 @@ def test_binary_dilation11(self): "binary dilation 11" for type in self.types: - data = numarray.zeros([5], type) + data = numpy.zeros([5], type) data[1] = 1 out = ndimage.binary_dilation(data, origin = 1) self.failUnless(diff(out, [1, 1, 0, 0, 0]) < eps) @@ -4505,7 +4504,7 @@ def test_binary_dilation12(self): "binary dilation 12" for type in self.types: - data = numarray.zeros([5], type) + data = numpy.zeros([5], type) data[1] = 1 struct = [1, 0, 1] out = ndimage.binary_dilation(data, struct) @@ -4514,7 +4513,7 @@ def test_binary_dilation13(self): "binary dilation 13" for type in self.types: - data = numarray.zeros([5], type) + data = numpy.zeros([5], type) data[1] = 1 struct = [1, 0, 1] out = ndimage.binary_dilation(data, struct, @@ -4524,7 +4523,7 @@ def test_binary_dilation14(self): "binary dilation 14" for type in self.types: - data = numarray.zeros([5], type) + data = numpy.zeros([5], type) data[1] = 1 struct = [1, 0, 1] out = ndimage.binary_dilation(data, struct, @@ -4534,7 +4533,7 @@ def test_binary_dilation15(self): "binary dilation 15" for type in self.types: - data = numarray.zeros([5], type) + data = numpy.zeros([5], type) data[1] = 1 struct = [1, 0, 1] out = ndimage.binary_dilation(data, struct, @@ -4544,28 +4543,28 @@ def test_binary_dilation16(self): "binary dilation 16" for type in self.types: - data = numarray.ones([1, 1], type) + data = numpy.ones([1, 1], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, [[1]]) < eps) def test_binary_dilation17(self): "binary dilation 17" for type in self.types: - data = numarray.zeros([1, 1], type) + data = numpy.zeros([1, 1], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, [[0]]) < eps) def test_binary_dilation18(self): "binary dilation 18" for type in self.types: - data = numarray.ones([1, 3], type) + data = numpy.ones([1, 3], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, [[1, 1, 1]]) < eps) def test_binary_dilation19(self): "binary dilation 19" for type in self.types: - data = numarray.ones([3, 3], type) + data = numpy.ones([3, 3], type) out = ndimage.binary_dilation(data) self.failUnless(diff(out, [[1, 1, 1], [1, 1, 1], @@ -4574,7 +4573,7 @@ def test_binary_dilation20(self): "binary dilation 20" for type in self.types: - data = numarray.zeros([3, 3], type) + data = numpy.zeros([3, 3], type) data[1, 1] = 1 out = ndimage.binary_dilation(data) self.failUnless(diff(out, [[0, 1, 0], @@ -4585,7 +4584,7 @@ "binary dilation 21" struct = ndimage.generate_binary_structure(2, 2) for type in self.types: - data = numarray.zeros([3, 3], type) + data = numpy.zeros([3, 3], type) data[1, 1] = 1 out = ndimage.binary_dilation(data, struct) self.failUnless(diff(out, [[1, 1, 1], @@ -4604,7 +4603,7 @@ [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4627,7 +4626,7 @@ [1, 1, 1, 1, 1, 1, 1, 1]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4650,7 +4649,7 @@ [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4673,7 +4672,7 @@ [1, 1, 1, 1, 1, 1, 1, 1]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4698,7 +4697,7 @@ [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4723,7 +4722,7 @@ [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4742,7 +4741,7 @@ [1, 1, 1, 1]] for type in self.types: - data = numarray.array([[0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], type) @@ -4759,7 +4758,7 @@ [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0], @@ -4778,12 +4777,12 @@ [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]], bool) - out = numarray.zeros(data.shape, bool) + out = numpy.zeros(data.shape, bool) ndimage.binary_dilation(data, struct, iterations = 2, output = out) self.failUnless(diff(out, true) < eps) @@ -4798,7 +4797,7 @@ [1, 1, 1, 1, 0], [0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0], @@ -4817,12 +4816,12 @@ [1, 1, 1, 1, 0], [0, 0, 0, 0, 0]] - data = numarray.array([[0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]], bool) - out = numarray.zeros(data.shape, bool) + out = numpy.zeros(data.shape, bool) ndimage.binary_dilation(data, struct, iterations = 3, output = out) self.failUnless(diff(out, true) < eps) @@ -4832,7 +4831,7 @@ struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + true = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0], @@ -4840,7 +4839,7 @@ [0, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) - mask = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 1, 1, 0, 0], @@ -4848,7 +4847,7 @@ [0, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) - data = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], @@ -4874,7 +4873,7 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] - mask = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4882,7 +4881,7 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) - data = numarray.zeros(mask.shape, bool) + data = numpy.zeros(mask.shape, bool) out = ndimage.binary_dilation(data, struct, iterations = -1, mask = mask, border_value = 1) self.failUnless(diff(out, true) < eps) @@ -4897,7 +4896,7 @@ [0, 1, 0, 0, 1, 0, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1]] - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4913,11 +4912,11 @@ [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] - true = numarray.logical_and(tmp, mask) - tmp = numarray.logical_and(data, numarray.logical_not(mask)) - true = numarray.logical_or(true, tmp) + true = numpy.logical_and(tmp, mask) + tmp = numpy.logical_and(data, numpy.logical_not(mask)) + true = numpy.logical_or(true, tmp) for type in self.types: - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4934,7 +4933,7 @@ struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + true = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0], @@ -4942,7 +4941,7 @@ [0, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) - mask = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 1, 1, 0, 0], @@ -4950,7 +4949,7 @@ [0, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) - data = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], @@ -4976,7 +4975,7 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] - mask = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], @@ -4984,7 +4983,7 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) - data = numarray.zeros(mask.shape, bool) + data = numpy.zeros(mask.shape, bool) out = ndimage.binary_propagation(data, struct, mask = mask, border_value = 1) self.failUnless(diff(out, true) < eps) @@ -5000,7 +4999,7 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0], @@ -5023,7 +5022,7 @@ [0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[1, 1, 1, 0, 0, 0, 0, 0], + data = numpy.array([[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 0], @@ -5045,7 +5044,7 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 1, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0], @@ -5068,7 +5067,7 @@ [0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[1, 1, 1, 0, 0, 0, 0, 0], + data = numpy.array([[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 0], @@ -5081,14 +5080,14 @@ def test_binary_fill_holes01(self): "binary fill holes 1" - true = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + true = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0], @@ -5100,14 +5099,14 @@ def test_binary_fill_holes02(self): "binary fill holes 2" - true = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + true = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0], @@ -5119,14 +5118,14 @@ def test_binary_fill_holes03(self): "binary fill holes 3" - true = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + true = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 1, 1, 1], [0, 1, 1, 1, 0, 1, 1, 1], [0, 1, 1, 1, 0, 1, 1, 1], [0, 0, 1, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0]], bool) - data = numarray.array([[0, 0, 0, 0, 0, 0, 0, 0], + data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 1], @@ -5138,7 +5137,7 @@ def test_grey_erosion01(self): "grey erosion 1" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5150,7 +5149,7 @@ def test_grey_erosion02(self): "grey erosion 2" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5163,7 +5162,7 @@ def test_grey_erosion03(self): "grey erosion 3" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5176,7 +5175,7 @@ def test_grey_dilation01(self): "grey dilation 1" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[0, 1, 1], [1, 0, 1]] @@ -5188,7 +5187,7 @@ def test_grey_dilation02(self): "grey dilation 2" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[0, 1, 1], [1, 0, 1]] @@ -5201,7 +5200,7 @@ def test_grey_dilation03(self): "grey dilation 3" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[0, 1, 1], [1, 0, 1]] @@ -5214,7 +5213,7 @@ def test_grey_opening01(self): "grey opening 1" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5227,7 +5226,7 @@ def test_grey_opening02(self): "grey opening 2" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5242,7 +5241,7 @@ def test_grey_closing01(self): "grey closing 1" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5254,7 +5253,7 @@ def test_grey_closing02(self): "grey closing 2" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5269,7 +5268,7 @@ def test_morphological_gradient01(self): "morphological gradient 1" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5279,14 +5278,14 @@ tmp2 = ndimage.grey_erosion(array, footprint = footprint, structure = structure) true = tmp1 - tmp2 - output = numarray.zeros(array.shape, array.dtype) + output = numpy.zeros(array.shape, array.dtype) ndimage.morphological_gradient(array, footprint=footprint, structure=structure, output = output) self.failUnless(diff(true, output) < eps) def test_morphological_gradient02(self): "morphological gradient 2" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5302,7 +5301,7 @@ def test_morphological_laplace01(self): "morphological laplace 1" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5312,14 +5311,14 @@ tmp2 = ndimage.grey_erosion(array, footprint = footprint, structure = structure) true = tmp1 + tmp2 - 2 * array - output = numarray.zeros(array.shape, array.dtype) + output = numpy.zeros(array.shape, array.dtype) ndimage.morphological_laplace(array, footprint=footprint, structure=structure, output = output) self.failUnless(diff(true, output) < eps) def test_morphological_laplace02(self): "morphological laplace 2" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5335,7 +5334,7 @@ def test_white_tophat01(self): "white tophat 1" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5343,14 +5342,14 @@ tmp = ndimage.grey_opening(array, footprint = footprint, structure = structure) true = array - tmp - output = numarray.zeros(array.shape, array.dtype) + output = numpy.zeros(array.shape, array.dtype) ndimage.white_tophat(array, footprint=footprint, structure=structure, output = output) self.failUnless(diff(true, output) < eps) def test_white_tophat02(self): "white tophat 2" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5364,7 +5363,7 @@ def test_black_tophat01(self): "black tophat 1" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5372,14 +5371,14 @@ tmp = ndimage.grey_closing(array, footprint = footprint, structure = structure) true = tmp - array - output = numarray.zeros(array.shape, array.dtype) + output = numpy.zeros(array.shape, array.dtype) ndimage.black_tophat(array, footprint=footprint, structure=structure, output = output) self.failUnless(diff(true, output) < eps) def test_black_tophat02(self): "black tophat 2" - array = numarray.array([[3, 2, 5, 1, 4], + array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] @@ -5405,7 +5404,7 @@ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 1, 0, 0, 0], + data = numpy.array([[0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [0, 1, 0, 1, 1], [0, 0, 1, 1, 1], @@ -5413,7 +5412,7 @@ [0, 1, 1, 1, 1], [0, 1, 1, 1, 1], [0, 0, 0, 0, 0]], type) - out = numarray.zeros(data.shape, bool) + out = numpy.zeros(data.shape, bool) ndimage.binary_hit_or_miss(data, struct, output = out) self.failUnless(diff(true, out) < eps) @@ -5428,7 +5427,7 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 1, 0, 0, 1, 1, 1, 0], + data = numpy.array([[0, 1, 0, 0, 1, 1, 1, 0], [1, 1, 1, 0, 0, 1, 0, 0], [0, 1, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) @@ -5452,7 +5451,7 @@ [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for type in self.types: - data = numarray.array([[0, 1, 0, 0, 1, 1, 1, 0], + data = numpy.array([[0, 1, 0, 0, 1, 1, 1, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 0], From scipy-svn at scipy.org Sun Sep 24 05:30:51 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 24 Sep 2006 04:30:51 -0500 (CDT) Subject: [Scipy-svn] r2233 - in trunk/Lib: integrate interpolate io misc optimize signal sparse stats weave weave/examples Message-ID: <20060924093051.5EC0539C0BF@new.scipy.org> Author: rkern Date: 2006-09-24 04:30:41 -0500 (Sun, 24 Sep 2006) New Revision: 2233 Modified: trunk/Lib/integrate/ode.py trunk/Lib/interpolate/fitpack2.py trunk/Lib/io/array_import.py trunk/Lib/io/dumbdbm_patched.py trunk/Lib/misc/ppimport.py trunk/Lib/optimize/anneal.py trunk/Lib/signal/ltisys.py trunk/Lib/sparse/sparse.py trunk/Lib/stats/distributions.py trunk/Lib/weave/accelerate_tools.py trunk/Lib/weave/base_info.py trunk/Lib/weave/base_spec.py trunk/Lib/weave/bytecodecompiler.py trunk/Lib/weave/catalog.py trunk/Lib/weave/dumbdbm_patched.py trunk/Lib/weave/examples/object.py trunk/Lib/weave/ext_tools.py trunk/Lib/weave/size_check.py Log: Inherit from object. Modified: trunk/Lib/integrate/ode.py =================================================================== --- trunk/Lib/integrate/ode.py 2006-09-24 09:05:13 UTC (rev 2232) +++ trunk/Lib/integrate/ode.py 2006-09-24 09:30:41 UTC (rev 2233) @@ -94,7 +94,7 @@ from numpy import asarray, array, zeros, sin, int32, isscalar import re, sys -class ode: +class ode(object): """\ ode - a generic interface class to numeric integrators. It has the following methods: @@ -208,7 +208,7 @@ return cl return -class IntegratorBase: +class IntegratorBase(object): runner = None # runner is None => integrator is not available success = None # success==1 if integrator was called successfully Modified: trunk/Lib/interpolate/fitpack2.py =================================================================== --- trunk/Lib/interpolate/fitpack2.py 2006-09-24 09:05:13 UTC (rev 2232) +++ trunk/Lib/interpolate/fitpack2.py 2006-09-24 09:30:41 UTC (rev 2233) @@ -49,7 +49,7 @@ xb= haveArgument: Modified: trunk/Lib/weave/catalog.py =================================================================== --- trunk/Lib/weave/catalog.py 2006-09-24 09:05:13 UTC (rev 2232) +++ trunk/Lib/weave/catalog.py 2006-09-24 09:30:41 UTC (rev 2233) @@ -282,7 +282,7 @@ sh = shelve.open(catalog_file,mode) return sh -class catalog: +class catalog(object): """ Stores information about compiled functions both in cache and on disk. catalog stores (code, list_of_function) pairs so that all the functions Modified: trunk/Lib/weave/dumbdbm_patched.py =================================================================== --- trunk/Lib/weave/dumbdbm_patched.py 2006-09-24 09:05:13 UTC (rev 2232) +++ trunk/Lib/weave/dumbdbm_patched.py 2006-09-24 09:30:41 UTC (rev 2233) @@ -30,7 +30,7 @@ error = IOError # For anydbm -class _Database: +class _Database(object): def __init__(self, file): self._dirfile = file + '.dir' Modified: trunk/Lib/weave/examples/object.py =================================================================== --- trunk/Lib/weave/examples/object.py 2006-09-24 09:05:13 UTC (rev 2232) +++ trunk/Lib/weave/examples/object.py 2006-09-24 09:30:41 UTC (rev 2233) @@ -9,7 +9,7 @@ # get/set attribute and call methods example #---------------------------------------------------------------------------- -class foo: +class foo(object): def __init__(self): self.val = 1 def inc(self,amount): Modified: trunk/Lib/weave/ext_tools.py =================================================================== --- trunk/Lib/weave/ext_tools.py 2006-09-24 09:05:13 UTC (rev 2232) +++ trunk/Lib/weave/ext_tools.py 2006-09-24 09:30:41 UTC (rev 2233) @@ -6,7 +6,7 @@ import converters import base_spec -class ext_function_from_specs: +class ext_function_from_specs(object): def __init__(self,name,code_block,arg_specs): self.name = name self.arg_specs = base_spec.arg_spec_list(arg_specs) @@ -179,7 +179,7 @@ import base_info -class ext_module: +class ext_module(object): def __init__(self,name,compiler=''): standard_info = converters.standard_info self.name = name Modified: trunk/Lib/weave/size_check.py =================================================================== --- trunk/Lib/weave/size_check.py 2006-09-24 09:05:13 UTC (rev 2232) +++ trunk/Lib/weave/size_check.py 2006-09-24 09:30:41 UTC (rev 2233) @@ -101,7 +101,7 @@ raise ValueError, "frames are not aligned" return res -class dummy_array: +class dummy_array(object): def __init__(self,ary,ary_is_shape = 0,name=None): self.name = name if ary_is_shape: From scipy-svn at scipy.org Tue Sep 26 11:25:40 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 26 Sep 2006 10:25:40 -0500 (CDT) Subject: [Scipy-svn] r2234 - trunk/Lib/stats Message-ID: <20060926152540.5D5CB39C060@new.scipy.org> Author: stefan Date: 2006-09-26 10:25:09 -0500 (Tue, 26 Sep 2006) New Revision: 2234 Modified: trunk/Lib/stats/stats.py Log: Use numpy's sum in stats. Closes ticket #271. Modified: trunk/Lib/stats/stats.py =================================================================== --- trunk/Lib/stats/stats.py 2006-09-24 09:30:41 UTC (rev 2233) +++ trunk/Lib/stats/stats.py 2006-09-26 15:25:09 UTC (rev 2234) @@ -186,7 +186,7 @@ import warnings # Scipy imports. -from numpy import array, asarray, dot, ma, zeros +from numpy import array, asarray, dot, ma, zeros, sum import scipy.special as special import scipy.linalg as linalg import numpy as np From scipy-svn at scipy.org Tue Sep 26 12:59:29 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 26 Sep 2006 11:59:29 -0500 (CDT) Subject: [Scipy-svn] r2235 - trunk/Lib/stats Message-ID: <20060926165929.684B539C17E@new.scipy.org> Author: rkern Date: 2006-09-26 11:59:28 -0500 (Tue, 26 Sep 2006) New Revision: 2235 Modified: trunk/Lib/stats/kde.py trunk/Lib/stats/stats.py Log: sum -> np.sum Modified: trunk/Lib/stats/kde.py =================================================================== --- trunk/Lib/stats/kde.py 2006-09-26 15:25:09 UTC (rev 2234) +++ trunk/Lib/stats/kde.py 2006-09-26 16:59:28 UTC (rev 2235) @@ -17,63 +17,60 @@ # #------------------------------------------------------------------------------- -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - +# Standard library imports. import warnings +# Scipy imports. from scipy import linalg, special -from numpy.oldnumeric import atleast_2d, reshape, zeros, newaxis, dot, exp, pi, sqrt, \ - ravel, Float, take, power, atleast_1d, squeeze, transpose +from numpy import atleast_2d, reshape, zeros, newaxis, dot, exp, pi, sqrt, \ + ravel, power, atleast_1d, squeeze, sum, transpose from numpy.random import randint, multivariate_normal +# Local imports. import stats import mvn __all__ = ['gaussian_kde', - ] +] -#------------------------------------------------------------------------------- -# 'gaussian_kde' class: -#------------------------------------------------------------------------------- class gaussian_kde(object): - """ + """ Representation of a kernel-density estimate using Gaussian kernels. + Parameters ---------- dataset : (# of dims, # of data)-array - datapoints to estimate from + datapoints to estimate from Members ------- d : int - number of dimensions + number of dimensions n : int - number of datapoints + number of datapoints Methods ------- kde.evaluate(points) : array - evaluate the estimated pdf on a provided set of points + evaluate the estimated pdf on a provided set of points kde(points) : array - same as kde.evaluate(points) + same as kde.evaluate(points) kde.integrate_gaussian(mean, cov) : float - multiply pdf with a specified gaussian and integrate over the whole domain + multiply pdf with a specified Gaussian and integrate over the whole domain kde.integrate_box_1d(low, high) : float - integrate pdf (1D only) between two bounds + integrate pdf (1D only) between two bounds kde.integrate_box(low_bounds, high_bounds) : float - integrate pdf over a rectangular space between low_bounds and high_bounds + integrate pdf over a rectangular space between low_bounds and high_bounds kde.integrate_kde(other_kde) : float - integrate two kernel density estimates multiplied together + integrate two kernel density estimates multiplied together Internal Methods ---------------- kde.covariance_factor() : float - computes the coefficient that multiplies the data covariance matrix to - obtain the kernel covariance matrix. Set this method to kde.scotts_factor - or kde.silverman_factor (or subclass to provide your own). The default is - scotts_factor. + computes the coefficient that multiplies the data covariance matrix to + obtain the kernel covariance matrix. Set this method to + kde.scotts_factor or kde.silverman_factor (or subclass to provide your + own). The default is scotts_factor. """ def __init__(self, dataset): @@ -82,11 +79,26 @@ self.d, self.n = self.dataset.shape self._compute_covariance() - + + def evaluate(self, points): """Evaluate the estimated pdf on a set of points. - points.shape == (# of dimensions, # of points) + Parameters + ---------- + points : (# of dimensions, # of points)-array + Alternatively, a (# of dimensions,) vector can be passed in and + treated as a single point. + + Returns + ------- + values : (# of points,)-array + The values at each point. + + Raises + ------ + ValueError if the dimensionality of the input points is different than + the dimensionality of the KDE. """ points = atleast_2d(points).astype(self.dataset.dtype) @@ -96,6 +108,7 @@ if d == 1 and m == self.d: # points was passed in as a row vector points = reshape(points, (self.d, 1)) + m = 1 else: msg = "points have dimension %s, dataset has dimension %s" % (d, self.d) @@ -126,21 +139,25 @@ __call__ = evaluate def integrate_gaussian(self, mean, cov): - """Multiply estimated density by a multivariate gaussian and integrate + """Multiply estimated density by a multivariate Gaussian and integrate over the wholespace. Parameters ---------- mean : vector - the mean of the gaussian + the mean of the Gaussian cov : matrix - the covariance matrix of the gaussian + the covariance matrix of the Gaussian Returns ------- result : scalar - the value of the integral + the value of the integral + Raises + ------ + ValueError if the mean or covariance of the input Gaussian differs from + the KDE's dimensionality. """ mean = atleast_1d(squeeze(mean)) @@ -170,19 +187,23 @@ Parameters ---------- low : scalar - lower bound of integration + lower bound of integration high : scalar - upper bound of integration + upper bound of integration Returns ------- value : scalar - the result of the integral + the result of the integral + + Raises + ------ + ValueError if the KDE is over more than one dimension. """ if self.d != 1: raise ValueError("integrate_box_1d() only handles 1D pdfs") - stdev = ravel(sqrt(self.covariance))[0] + stdev = np.ravel(sqrt(self.covariance))[0] normalized_low = ravel((low - self.dataset)/stdev) normalized_high = ravel((high - self.dataset)/stdev) @@ -198,16 +219,16 @@ Parameters ---------- low_bounds : vector - lower bounds of integration + lower bounds of integration high_bounds : vector - upper bounds of integration + upper bounds of integration maxpts=None : int - maximum number of points to use for integration + maximum number of points to use for integration Returns ------- value : scalar - the result of the integral + the result of the integral """ if maxpts is not None: extra_kwds = {'maxpts': maxpts} @@ -229,13 +250,17 @@ Parameters ---------- - other : GaussianKDE instance - the other kde + other : gaussian_kde instance + the other kde Returns ------- value : scalar - the result of the integral + the result of the integral + + Raises + ------ + ValueError if the KDEs have different dimensionality. """ if other.d != self.d: @@ -268,22 +293,24 @@ Parameters ---------- - size : int=None - if None, then size = self.n; otherwise, the number of samples to draw + size : int, optional + The number of samples to draw. + If not provided, then the size is the same as the underlying + dataset. Returns ------- dataset : (self.d, size)-array - sampled dataset + sampled dataset """ if size is None: size = self.n - norm = transpose(multivariate_normal(zeros((self.d,), Float), + norm = transpose(multivariate_normal(zeros((self.d,), float), self.covariance, size=size)) indices = randint(0, self.n, size=size) - means = take(self.dataset, indices, axis=1) + means = self.dataset[:,indices] return means + norm @@ -294,13 +321,16 @@ def silverman_factor(self): return power(self.n*(self.d+2.0)/4.0, -1./(self.d+4)) + # This can be replaced with silverman_factor if one wants to use Silverman's + # rule for choosing the bandwidth of the kernels. covariance_factor = scotts_factor def _compute_covariance(self): - """Computes the covariance matrix for each gaussian kernel using + """Computes the covariance matrix for each Gaussian kernel using covariance_factor """ self.factor = self.covariance_factor() self.covariance = atleast_2d(stats.cov(self.dataset, rowvar=1) * self.factor * self.factor) self.inv_cov = linalg.inv(self.covariance) + self._norm_factor = sqrt(linalg.det(2*pi*self.covariance)) * self.n Modified: trunk/Lib/stats/stats.py =================================================================== --- trunk/Lib/stats/stats.py 2006-09-26 15:25:09 UTC (rev 2234) +++ trunk/Lib/stats/stats.py 2006-09-26 16:59:28 UTC (rev 2235) @@ -246,7 +246,7 @@ x, axis = _chk_asarray(x,axis) x = x.copy() Norig = x.shape[axis] - factor = 1.0-sum(np.isnan(x),axis)*1.0/Norig + factor = 1.0-np.sum(np.isnan(x),axis)*1.0/Norig x[np.isnan(x)] = 0 return np.mean(x,axis)/factor @@ -1727,7 +1727,7 @@ f_obs = asarray(f_obs) k = len(f_obs) if f_exp is None: - f_exp = array([sum(f_obs,axis=0)/float(k)] * len(f_obs),float) + f_exp = array([np.sum(f_obs,axis=0)/float(k)] * len(f_obs),float) f_exp = f_exp.astype(float) chisq = np.add.reduce((f_obs-f_exp)**2 / f_exp) return chisq, chisqprob(chisq, k-1) @@ -1790,7 +1790,7 @@ ranked = rankdata(np.concatenate((x,y))) rankx = ranked[0:n1] # get the x-ranks #ranky = ranked[n1:] # the rest are y-ranks - u1 = n1*n2 + (n1*(n1+1))/2.0 - sum(rankx,axis=0) # calc U for x + u1 = n1*n2 + (n1*(n1+1))/2.0 - np.sum(rankx,axis=0) # calc U for x u2 = n1*n2 - u1 # remainder is U for y bigu = max(u1,u2) smallu = min(u1,u2) @@ -1841,7 +1841,7 @@ ranked = rankdata(alldata) x = ranked[:n1] y = ranked[n1:] - s = sum(x,axis=0) + s = np.sum(x,axis=0) expected = n1*(n1+n2+1) / 2.0 z = (s - expected) / np.sqrt(n1*n2*(n1+n2+1)/12.0) prob = 2*(1.0 -zprob(abs(z))) @@ -1871,10 +1871,10 @@ del ranked[0:n[i]] rsums = [] for i in range(len(args)): - rsums.append(sum(args[i],axis=0)**2) + rsums.append(np.sum(args[i],axis=0)**2) rsums[i] = rsums[i] / float(n[i]) - ssbn = sum(rsums,axis=0) - totaln = sum(n,axis=0) + ssbn = np.sum(rsums,axis=0) + totaln = np.sum(n,axis=0) h = 12.0 / (totaln*(totaln+1)) * ssbn - 3*(totaln+1) df = len(args) - 1 if T == 0: @@ -1902,7 +1902,7 @@ data = data.astype(float) for i in range(len(data)): data[i] = rankdata(data[i]) - ssbn = sum(sum(args,1)**2,axis=0) + ssbn = np.sum(np.sum(args,1)**2,axis=0) chisq = 12.0 / (k*n*(k+1)) * ssbn - 3*n*(k+1) return chisq, chisqprob(chisq,k-1) @@ -1991,7 +1991,7 @@ if len(p) == 2: # ttest_ind c = array([1,-1]) df = n-2 - fact = sum(1.0/sum(x,0),axis=0) # i.e., 1/n1 + 1/n2 + 1/n3 ... + fact = np.sum(1.0/np.sum(x,0),axis=0) # i.e., 1/n1 + 1/n2 + 1/n3 ... t = dot(c,b) / np.sqrt(s_sq*fact) probs = betai(0.5*df,0.5,float(df)/(df+t*t)) return t, probs @@ -2074,7 +2074,7 @@ Returns: the square of the sum over axis. """ a, axis = _chk_asarray(a, axis) - s = sum(a,axis) + s = np.sum(a,axis) if not np.isscalar(s): return s.astype(float)*s else: From scipy-svn at scipy.org Fri Sep 29 07:36:40 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 29 Sep 2006 06:36:40 -0500 (CDT) Subject: [Scipy-svn] r2236 - trunk/Lib/io Message-ID: <20060929113640.55BB339C01C@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-09-29 06:36:34 -0500 (Fri, 29 Sep 2006) New Revision: 2236 Removed: trunk/Lib/io/bytestream.py Modified: trunk/Lib/io/mio4.py trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py Log: Cleanups to processor function, drafting mat5 writing Deleted: trunk/Lib/io/bytestream.py =================================================================== --- trunk/Lib/io/bytestream.py 2006-09-26 16:59:28 UTC (rev 2235) +++ trunk/Lib/io/bytestream.py 2006-09-29 11:36:34 UTC (rev 2236) @@ -1,69 +0,0 @@ -# Author: Matthew Brett - -''' File-like interfact for memmapped array ''' - -from numpy import * - -class ByteStream(object): - ''' Overlays file-like interface on memmapped array - - This may speed up array reading from files - - @byte_array - uint array or string containing bytes - ''' - - def __init__(self, byte_array): - if isinstance(byte_array, ndarray): - if not byte_array.dtype == uint8: - raise ValueError, 'Need uint8 byte array as array input' - self.bytes = byte_array - elif isinstance(byte_array, basestring): - self.bytes = ndarray( - shape=(len(byte_array)), - dtype=uint8, - buffer=byte_array) - else: - raise ValueError, "Need string or byte array as input" - self.array_len = len(byte_array) - self.seek(0) - - # current file position - def get_pos(self): - return self._pos - def set_pos(self, offset): - if offset < 0: - raise IOError, 'Invalid argument' - self._pos = offset - pos = property(get_pos, - set_pos, - None, - 'get/set current position') - - def seek(self, offset, whence=0): - """ Method emulates seek method of file objects """ - if whence == 0: - self.pos = offset - elif whence == 1: # seek relative to the current position - self.pos += offset - elif whence == 2: # relative to end - self.pos = self.array_len + offset - else: - raise ValueError, 'Invalid value %d for whence parameter' % whence - - def tell(self): - return self.pos - - def read(self, num_bytes=-1): - if num_bytes < 0: - num_bytes = self.array_len - if self.pos >= self.array_len: - return array([], dtype=uint8) - next_pos = min(self.pos + num_bytes, self.array_len) - res = self.bytes[self.pos:next_pos] - self.pos = next_pos - return res - - def write(self, data): - assert False, 'Not implemented' - - Modified: trunk/Lib/io/mio4.py =================================================================== --- trunk/Lib/io/mio4.py 2006-09-26 16:59:28 UTC (rev 2235) +++ trunk/Lib/io/mio4.py 2006-09-29 11:36:34 UTC (rev 2236) @@ -323,9 +323,6 @@ arr = array(arr) if arr.dtype.hasobject: raise TypeError, 'Cannot save object arrays in Mat4' - if have_sparse: - if scipy.sparse.issparse(arr): - return Mat4SparseWriter(stream, arr, name) if arr.dtype.kind in ('U', 'S'): return Mat4CharWriter(stream, arr, name) else: Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-09-26 16:59:28 UTC (rev 2235) +++ trunk/Lib/io/mio5.py 2006-09-29 11:36:34 UTC (rev 2236) @@ -517,5 +517,193 @@ return 0 not in mopt_bytes -class Mat5Writer(MatFileWriter): - pass +class Mat5MatrixWriter(MatStreamWriter): + + def write_header(self, mclass, + is_global, + is_complex=False, + is_logical=False, + nzmax=0): + ''' Write header for given data options + @mclass - mat5 matrix class + @is_global - True if matrix is global + @is_complex - True is matrix is complex + @is_logical - True if matrix is logical + ''' + dims = self.arr.shape + header = empty((), mdtypes_template['header']) + M = not ByteOrder.little_endian + O = 0 + header['mopt'] = (M * 1000 + + O * 100 + + P * 10 + + T) + header['mrows'] = dims[0] + header['ncols'] = dims[1] + header['imagf'] = imagf + header['namlen'] = len(self.name) + 1 + self.write_bytes(header) + self.write_string(self.name + '\0') + + def write(self): + assert False, 'Not implemented' + + +class Mat5NumericWriter(Mat5MatrixWriter): + + def write(self): + # identify matlab type for array + # make at least 2d + # write miMATRIX tag + # write array flags (complex, global, logical, class, nzmax) + # dimensions + # array name + # maybe downcast array to smaller matlab type + # write real + # write imaginary + # put padded length in miMATRIX tag + pass + + +class Mat5CharWriter(Mat5MatrixWriter): + + def write(self): + self.arr_to_chars() + self.arr_to_2d() + dims = self.arr.shape + self.write_header(P=miUINT8, + T=mxCHAR_CLASS) + if self.arr.dtype.kind == 'U': + # Recode unicode to ascii + n_chars = product(dims) + st_arr = ndarray(shape=(), + dtype=self.arr_dtype_number(n_chars), + buffer=self.arr) + st = st_arr.item().encode('ascii') + self.arr = ndarray(shape=dims, dtype='S1', buffer=st) + self.write_bytes(self.arr) + + +class Mat5SparseWriter(Mat5MatrixWriter): + + def write(self): + ''' Sparse matrices are 2D + See docstring for Mat5SparseGetter + ''' + imagf = self.arr.dtype.kind == 'c' + N = self.arr.nnz + ijd = zeros((N+1, 3+imagf), dtype='f8') + for i in range(N): + ijd[i,0], ijd[i,1] = self.arr.rowcol(i) + ijd[:-1,0:2] += 1 # 1 based indexing + if imagf: + ijd[:-1,2] = self.arr.data.real + ijd[:-1,3] = self.arr.data.imag + else: + ijd[:-1,2] = self.arr.data + ijd[-1,0:2] = self.arr.shape + self.write_header(P=miDOUBLE, + T=mxSPARSE_CLASS, + dims=ijd.shape) + self.write_bytes(ijd) + + +def matrix_writer_factory(stream, arr, name, unicode_strings=False, is_global=False): + ''' Factory function to return matrix writer given variable to write + @stream - file or file-like stream to write to + @arr - array to write + @name - name in matlab (TM) workspace + ''' + if have_sparse: + if scipy.sparse.issparse(arr): + return Mat5SparseWriter(stream, arr, name, is_global) + arr = array(arr) + if arr.dtype.hasobject: + types, arr_type = classify_mobjects(arr) + if arr_type == 'c': + return Mat5CellWriter(stream, arr, name, is_global, types) + elif arr_type == 's': + return Mat5StructWriter(stream, arr, name, is_global) + elif arr_type == 'o': + return Mat5ObjectWriter(stream, arr, name, is_global) + if arr.dtype.kind in ('U', 'S'): + if unicode_strings: + return Mat5UniCharWriter(stream, arr, name, is_global) + else: + return Mat5IntCharWriter(stream, arr, name, is_global) + else: + return Mat5NumericWriter(stream, arr, name, is_global) + +def classify_mobjects(objarr): + ''' Function to classify objects passed for writing + returns + types - S1 array of same shape as objarr with codes for each object + i - invalid object + a - ndarray + s - matlab struct + o - matlab object + arr_type - one of + c - cell array + s - struct array + o - object array + ''' + N = objarr.size + types = empty((N,), dtype='S1') + types[:] = 'i' + type_set = set() + flato = objarr.flat + for i in range(N): + obj = flato[i] + if isinstance(obj, ndarray): + types[i] = 'a' + continue + try: + fns = tuple(obj._fieldnames) + except AttributeError: + continue + try: + cn = obj._classname + except AttributeError: + types[i] = 's' + type_set.add(fns) + continue + types[i] = 'o' + type_set.add((cn, fns)) + arr_type = 'c' + if len(set(types))==1 and len(type_set) == 1: + arr_type = types[0] + return types.reshape(objarr.shape), arr_type + + +class MatFile5Writer(MatFileWriter): + ''' Class for writing mat5 files ''' + def __init__(self, file_stream, + do_compression=False, + unicode_strings=False, + global_vars=None): + super(MatFile5Writer, self).__init__(file_stream) + self.do_compression = do_compression + self.unicode_strings = unicode_strings + if global_vars: + self.global_vars = global_vars + else: + self.global_vars = [] + + def put_variables(self, mdict): + for name, var in mdict.items(): + is_global = name in self.global_vars + stream = StringIO() + matrix_writer_factory(stream, + var, + name, + is_global, + self.unicode_strings, + ).write() + if self.do_compression: + str = zlib.compress(stream.getvalue()) + tag = empty((), mdtypes_template['tag_full']) + tag['mdtype'] = miCOMPRESSED + tag['byte_count'] = len(str) + self.file_stream.write(tag.tostring() + str) + else: + self.file_stream.write(stream.getvalue()) Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-09-26 16:59:28 UTC (rev 2235) +++ trunk/Lib/io/miobase.py 2006-09-29 11:36:34 UTC (rev 2236) @@ -88,8 +88,9 @@ in ('native', '=') or in ('little', '<') or in ('BIG', '>') - @base_name - base name for unnamed variables - @matlab_compatible - return arrays as matlab (TM) saved them + @base_name - base name for unnamed variables (unused in code) + @mat_dtype - return arrays in same dtype as loaded into matlab + (instead of the dtype with which they are saved) @squeeze_me - whether to squeeze unit dimensions or not @chars_as_strings - whether to convert char arrays to string arrays @@ -106,7 +107,7 @@ def __init__(self, mat_stream, byte_order=None, base_name='raw', - matlab_compatible=False, + mat_dtype=False, squeeze_me=True, chars_as_strings=True, ): @@ -119,22 +120,26 @@ self.base_name = base_name self._squeeze_me = squeeze_me self._chars_as_strings = chars_as_strings - self.matlab_compatible = matlab_compatible + self._mat_dtype = mat_dtype + self.processor_func = self.get_processor_func() - # matlab_compatible property sets squeeze_me and chars_as_strings - def get_matlab_compatible(self): - return self._matlab_compatible - def set_matlab_compatible(self, m_l_c): - self._matlab_compatible = m_l_c - if m_l_c: - self._squeeze_me = False - self._chars_as_strings = False + def set_matlab_compatible(self): + ''' Sets options to return arrays as matlab (tm) loads them ''' + self._mat_dtype = True + self._squeeze_me = False + self._chars_as_strings = False self.processor_func = self.get_processor_func() - matlab_compatible = property(get_matlab_compatible, - set_matlab_compatible, - None, - 'get/set matlab_compatible property') + def get_mat_dtype(self): + return self._mat_dtype + def set_mat_dtype(self, mat_dtype): + self._mat_dtype = mat_dtype + self.processor_func = self.get_processor_func() + mat_dtype = property(get_mat_dtype, + set_mat_dtype, + None, + 'get/set mat_dtype property') + def get_squeeze_me(self): return self._squeeze_me def set_squeeze_me(self, squeeze_me): @@ -151,9 +156,9 @@ self._chars_as_strings = chars_as_strings self.processor_func = self.get_processor_func() chars_as_strings = property(get_chars_as_strings, - set_chars_as_strings, - None, - 'get/set squeeze me property') + set_chars_as_strings, + None, + 'get/set squeeze me property') def get_order_code(self): return self._order_code @@ -200,7 +205,7 @@ The read array is the first argument. The getter, passed as second argument to the function, must - define properties, iff matlab_compatible option is True: + define properties, iff mat_dtype option is True: mat_dtype - data type when loaded into matlab (tm) (None for no conversion) @@ -223,7 +228,7 @@ arr[...,i] = self.chars_to_str(str_arr[i]) else: # return string arr = self.chars_to_str(arr) - if self.matlab_compatible: + if self.mat_dtype: # Apply options to replicate matlab's (TM) # load into workspace if getter.mat_dtype is not None: From scipy-svn at scipy.org Fri Sep 29 12:47:47 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 29 Sep 2006 11:47:47 -0500 (CDT) Subject: [Scipy-svn] r2237 - trunk/Lib/maxentropy Message-ID: <20060929164747.62E5539C02D@new.scipy.org> Author: rkern Date: 2006-09-29 11:47:46 -0500 (Fri, 29 Sep 2006) New Revision: 2237 Modified: trunk/Lib/maxentropy/maxentropy.py trunk/Lib/maxentropy/maxentutils.py Log: __future__ imports must come before any code in 2.5, apparently. Modified: trunk/Lib/maxentropy/maxentropy.py =================================================================== --- trunk/Lib/maxentropy/maxentropy.py 2006-09-29 11:36:34 UTC (rev 2236) +++ trunk/Lib/maxentropy/maxentropy.py 2006-09-29 16:47:46 UTC (rev 2237) @@ -3,6 +3,9 @@ # Copyright: Ed Schofield, 2003-2006 # License: BSD-style (see LICENSE.txt in main source directory) +# Future imports must come before any code in 2.5 +from __future__ import division + __author__ = "Ed Schofield" __version__ = '2.1' __changelog__ = """ @@ -66,7 +69,6 @@ """ -from __future__ import division import math, types, cPickle import numpy from scipy import optimize, sparse Modified: trunk/Lib/maxentropy/maxentutils.py =================================================================== --- trunk/Lib/maxentropy/maxentutils.py 2006-09-29 11:36:34 UTC (rev 2236) +++ trunk/Lib/maxentropy/maxentutils.py 2006-09-29 16:47:46 UTC (rev 2237) @@ -11,10 +11,12 @@ License: BSD-style (see LICENSE.txt in main source directory) """ +# Future imports must come before any code in 2.5 +from __future__ import division + __author__ = "Ed Schofield" __version__ = '2.0' -from __future__ import division import random, math, bisect, cmath import numpy from numpy import log, exp, asarray, ndarray From scipy-svn at scipy.org Sat Sep 30 19:39:37 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 30 Sep 2006 18:39:37 -0500 (CDT) Subject: [Scipy-svn] r2238 - in trunk/Lib/stats: . tests Message-ID: <20060930233937.21E9E39C012@new.scipy.org> Author: stefan Date: 2006-09-30 18:39:22 -0500 (Sat, 30 Sep 2006) New Revision: 2238 Modified: trunk/Lib/stats/stats.py trunk/Lib/stats/tests/test_stats.py Log: For stats.threshold: update doc, add test, optimise. Modified: trunk/Lib/stats/stats.py =================================================================== --- trunk/Lib/stats/stats.py 2006-09-29 16:47:46 UTC (rev 2237) +++ trunk/Lib/stats/stats.py 2006-09-30 23:39:22 UTC (rev 2238) @@ -1203,19 +1203,24 @@ ##################################### def threshold(a, threshmin=None, threshmax=None, newval=0): - """ -Like numpy.clip() except that values threshmax are replaced -by newval instead of by threshmin/threshmax (respectively). + """Clip array to a given value. + +Similar to numpy.clip(), except that values less than threshmin or +greater than threshmax are replaced by newval, instead of by +threshmin and threshmax respectively. -Returns: a, with values threshmax replaced with newval +Returns: a, with values less than threshmin or greater than threshmax + replaced with newval + """ - a = asarray(a) + a = asarray(a).copy() mask = zeros(a.shape, dtype=bool) - if threshmin != None: - mask |= (a < threshmin) - if threshmax != None: + if threshmin is not None: + mask = (a < threshmin) + if threshmax is not None: mask |= (a > threshmax) - return np.where(mask, newval, a) + a[mask] = newval + return a def trimboth(a, proportiontocut): Modified: trunk/Lib/stats/tests/test_stats.py =================================================================== --- trunk/Lib/stats/tests/test_stats.py 2006-09-29 16:47:46 UTC (rev 2237) +++ trunk/Lib/stats/tests/test_stats.py 2006-09-30 23:39:22 UTC (rev 2238) @@ -759,5 +759,16 @@ y = scipy.stats.kurtosis(self.testcase,0,0) assert_approx_equal(y,1.64) +class test_threshold(ScipyTestCase): + def check_basic(self): + a = [-1,2,3,4,5,-1,-2] + assert_array_equal(stats.threshold(a),a) + assert_array_equal(stats.threshold(a,3,None,0), + [0,0,3,4,5,0,0]) + assert_array_equal(stats.threshold(a,None,3,0), + [-1,2,3,0,0,-1,-2]) + assert_array_equal(stats.threshold(a,2,4,0), + [0,2,3,4,0,0,0]) + if __name__ == "__main__": ScipyTest().run()