From scipy-svn at scipy.org Fri Oct 1 06:23:35 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 1 Oct 2010 05:23:35 -0500 (CDT) Subject: [Scipy-svn] r6828 - in trunk/scipy/sparse/linalg: . eigen/lobpcg isolve Message-ID: <20101001102335.3C0E86E016@scipy.org> Author: ptvirtan Date: 2010-10-01 05:23:34 -0500 (Fri, 01 Oct 2010) New Revision: 6828 Modified: trunk/scipy/sparse/linalg/eigen/lobpcg/lobpcg.py trunk/scipy/sparse/linalg/interface.py trunk/scipy/sparse/linalg/isolve/iterative.py Log: DOC: sparse.linalg: reformat docstrings Modified: trunk/scipy/sparse/linalg/eigen/lobpcg/lobpcg.py =================================================================== --- trunk/scipy/sparse/linalg/eigen/lobpcg/lobpcg.py 2010-09-28 02:08:26 UTC (rev 6827) +++ trunk/scipy/sparse/linalg/eigen/lobpcg/lobpcg.py 2010-10-01 10:23:34 UTC (rev 6828) @@ -158,7 +158,6 @@ This function implements the Locally Optimal Block Preconditioned Conjugate Gradient Method (LOBPCG). - Parameters ---------- A : {sparse matrix, dense matrix, LinearOperator} @@ -167,44 +166,40 @@ X : array_like Initial approximation to the k eigenvectors. If A has shape=(n,n) then X should have shape shape=(n,k). - - Returns - ------- - w : array - Array of k eigenvalues - v : array - An array of k eigenvectors. V has the same shape as X. - - - Optional Parameters - ------------------- - B : {dense matrix, sparse matrix, LinearOperator} + B : {dense matrix, sparse matrix, LinearOperator}, optional the right hand side operator in a generalized eigenproblem. by default, B = Identity often called the "mass matrix" - M : {dense matrix, sparse matrix, LinearOperator} + M : {dense matrix, sparse matrix, LinearOperator}, optional preconditioner to A; by default M = Identity M should approximate the inverse of A - Y : array_like + Y : array_like, optional n-by-sizeY matrix of constraints, sizeY < n The iterations will be performed in the B-orthogonal complement of the column-space of Y. Y must be full rank. + Returns + ------- + w : array + Array of k eigenvalues + v : array + An array of k eigenvectors. V has the same shape as X. + Other Parameters ---------------- - tol : scalar + tol : scalar, optional Solver tolerance (stopping criterion) by default: tol=n*sqrt(eps) - maxiter: integer + maxiter: integer, optional maximum number of iterations by default: maxiter=min(n,20) - largest : boolean + largest : boolean, optional when True, solve for the largest eigenvalues, otherwise the smallest - verbosityLevel : integer + verbosityLevel : integer, optional controls solver output. default: verbosityLevel = 0. - retLambdaHistory : boolean + retLambdaHistory : boolean, optional whether to return eigenvalue history - retResidualNormsHistory : boolean + retResidualNormsHistory : boolean, optional whether to return history of residual norms Modified: trunk/scipy/sparse/linalg/interface.py =================================================================== --- trunk/scipy/sparse/linalg/interface.py 2010-09-28 02:08:26 UTC (rev 6827) +++ trunk/scipy/sparse/linalg/interface.py 2010-10-01 10:23:34 UTC (rev 6828) @@ -21,8 +21,8 @@ matvec : callable f(v) Returns returns A * v. - Optional Parameters - ------------------- + Other Parameters + ---------------- rmatvec : callable f(v) Returns A^H * v, where A^H is the conjugate transpose of A. matmat : callable f(V) Modified: trunk/scipy/sparse/linalg/isolve/iterative.py =================================================================== --- trunk/scipy/sparse/linalg/isolve/iterative.py 2010-09-28 02:08:26 UTC (rev 6827) +++ trunk/scipy/sparse/linalg/isolve/iterative.py 2010-10-01 10:23:34 UTC (rev 6828) @@ -21,8 +21,18 @@ b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). -Optional Parameters -------------------- +Returns +------- +x : {array, matrix} + The converged solution. +info : integer + Provides convergence information: + 0 : successful exit + >0 : convergence to tolerance not achieved, number of iterations + <0 : illegal input or breakdown + +Other Parameters +---------------- x0 : {array, matrix} Starting guess for the solution. tol : float @@ -39,26 +49,16 @@ callback : function User-supplied function to call after each iteration. It is called as callback(xk), where xk is the current solution vector. - -Outputs -------- -x : {array, matrix} - The converged solution. -info : integer - Provides convergence information: - 0 : successful exit - >0 : convergence to tolerance not achieved, number of iterations - <0 : illegal input or breakdown - -Deprecated Parameters ----------------------- xtype : {'f','d','F','D'} + This parameter is deprecated -- avoid using it. + The type of the result. If None, then it will be determined from A.dtype.char and b. If A does not have a typecode method then it will compute A.matvec(x0) to get a typecode. To save the extra computation when A does not have a typecode attribute use xtype=0 for the same type as b or use xtype='f','d','F',or 'D'. This parameter has been superceeded by LinearOperator. + """ @@ -314,8 +314,18 @@ b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). - Optional Parameters - ------------------- + Returns + ------- + x : {array, matrix} + The converged solution. + info : integer + Provides convergence information: + 0 : successful exit + >0 : convergence to tolerance not achieved, number of iterations + <0 : illegal input or breakdown + + Other Parameters + ---------------- x0 : {array, matrix} Starting guess for the solution. tol : float @@ -336,24 +346,9 @@ callback : function User-supplied function to call after each iteration. It is called as callback(rk), where rk is the current residual vector. - - Outputs - ------- - x : {array, matrix} - The converged solution. - info : integer - Provides convergence information: - 0 : successful exit - >0 : convergence to tolerance not achieved, number of iterations - <0 : illegal input or breakdown - - See Also - -------- - LinearOperator - - Deprecated Parameters - --------------------- xtype : {'f','d','F','D'} + This parameter is DEPRECATED --- avoid using it. + The type of the result. If None, then it will be determined from A.dtype.char and b. If A does not have a typecode method then it will compute A.matvec(x0) to get a typecode. To save the extra @@ -361,6 +356,10 @@ for the same type as b or use xtype='f','d','F',or 'D'. This parameter has been superceeded by LinearOperator. + See Also + -------- + LinearOperator + """ # Change 'restrt' keyword to 'restart' @@ -460,8 +459,18 @@ b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). - Optional Parameters - ------------------- + Returns + ------- + x : {array, matrix} + The converged solution. + info : integer + Provides convergence information: + 0 : successful exit + >0 : convergence to tolerance not achieved, number of iterations + <0 : illegal input or breakdown + + Other Parameters + ---------------- x0 : {array, matrix} Starting guess for the solution. tol : float @@ -479,24 +488,9 @@ callback : function User-supplied function to call after each iteration. It is called as callback(xk), where xk is the current solution vector. - - Outputs - ------- - x : {array, matrix} - The converged solution. - info : integer - Provides convergence information: - 0 : successful exit - >0 : convergence to tolerance not achieved, number of iterations - <0 : illegal input or breakdown - - See Also - -------- - LinearOperator - - Deprecated Parameters - --------------------- xtype : {'f','d','F','D'} + This parameter is DEPRECATED -- avoid using it. + The type of the result. If None, then it will be determined from A.dtype.char and b. If A does not have a typecode method then it will compute A.matvec(x0) to get a typecode. To save the extra @@ -504,6 +498,10 @@ for the same type as b or use xtype='f','d','F',or 'D'. This parameter has been superceeded by LinearOperator. + See Also + -------- + LinearOperator + """ A_ = A A,M,x,b,postprocess = make_system(A,None,x0,b,xtype) From scipy-svn at scipy.org Thu Oct 7 22:35:33 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 7 Oct 2010 21:35:33 -0500 (CDT) Subject: [Scipy-svn] hi! Message-ID: <20101008023533.E45EA6E018@scipy.org> An HTML attachment was scrubbed... URL: From scipy-svn at scipy.org Sun Oct 10 00:40:48 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 9 Oct 2010 23:40:48 -0500 (CDT) Subject: [Scipy-svn] r6829 - trunk/scipy/weave/tests Message-ID: <20101010044048.104286E004@scipy.org> Author: warren.weckesser Date: 2010-10-09 23:40:47 -0500 (Sat, 09 Oct 2010) New Revision: 6829 Modified: trunk/scipy/weave/tests/test_ast_tools.py trunk/scipy/weave/tests/test_blitz_tools.py trunk/scipy/weave/tests/test_build_tools.py trunk/scipy/weave/tests/test_c_spec.py trunk/scipy/weave/tests/test_catalog.py trunk/scipy/weave/tests/test_ext_tools.py trunk/scipy/weave/tests/test_inline_tools.py trunk/scipy/weave/tests/test_numpy_scalar_spec.py trunk/scipy/weave/tests/test_scxx_dict.py trunk/scipy/weave/tests/test_scxx_object.py trunk/scipy/weave/tests/test_scxx_sequence.py trunk/scipy/weave/tests/test_size_check.py trunk/scipy/weave/tests/test_slice_handler.py trunk/scipy/weave/tests/test_standard_array_spec.py Log: TST: weave: Don't use 'import *'. Don't use plain 'assert'. Rename a couple duplicated tests. Clean up a bit. Modified: trunk/scipy/weave/tests/test_ast_tools.py =================================================================== --- trunk/scipy/weave/tests/test_ast_tools.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_ast_tools.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -1,8 +1,8 @@ -from numpy.testing import * +from numpy.testing import TestCase, assert_equal from scipy.weave import ast_tools -from weave_test_utils import * + class TestHarvestVariables(TestCase): """ Not much testing going on here, but at least it is a flame test. Modified: trunk/scipy/weave/tests/test_blitz_tools.py =================================================================== --- trunk/scipy/weave/tests/test_blitz_tools.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_blitz_tools.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -4,11 +4,11 @@ from numpy import float32, float64, complex64, complex128, \ zeros, random, array, sum, abs, allclose -from numpy.testing import * +from numpy.testing import TestCase, dec, assert_equal, assert_ from scipy.weave import blitz_tools from scipy.weave.ast_tools import harvest_variables -from weave_test_utils import * +from weave_test_utils import empty_temp_dir, cleanup_temp_dir, remove_whitespace class TestAstToBlitzExpr(TestCase): @@ -80,7 +80,7 @@ try: # this isn't very stringent. Need to tighten this up and # learn where failures are occuring. - assert(allclose(abs(actual.ravel()),abs(desired.ravel()),1e-4,1e-6)) + assert_(allclose(abs(actual.ravel()),abs(desired.ravel()),1e-4,1e-6)) except: diff = actual-desired print diff[:4,:4] @@ -127,10 +127,12 @@ print "2nd run(numpy.numerix,compiled,speed up): %3.4f, %3.4f, " \ "%3.4f" % (standard,compiled,speed_up) cleanup_temp_dir(mod_location) + #def test_simple_2d(self): # """ result = a + b""" # expr = "result = a + b" # self.generic_2d(expr) + @dec.slow def test_5point_avg_2d_float(self): """ result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1] @@ -177,6 +179,7 @@ "+ b[1:-1,2:] + b[1:-1,:-2]) / 5." self.generic_2d(expr,complex128) + if __name__ == "__main__": import nose nose.run(argv=['', __file__]) Modified: trunk/scipy/weave/tests/test_build_tools.py =================================================================== --- trunk/scipy/weave/tests/test_build_tools.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_build_tools.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -4,7 +4,7 @@ import os, sys, tempfile, warnings -from numpy.testing import * +from numpy.testing import TestCase, assert_ from scipy.weave import build_tools @@ -16,41 +16,50 @@ def is_writable(val): return os.access(val,os.W_OK) + class TestConfigureBuildDir(TestCase): + def test_default(self): " default behavior is to return current directory " d = build_tools.configure_build_dir() if is_writable('.'): - assert(d == os.path.abspath('.')) - assert(is_writable(d)) + assert_(d == os.path.abspath('.')) + assert_(is_writable(d)) + def test_curdir(self): " make sure it handles relative values. " d = build_tools.configure_build_dir('.') if is_writable('.'): - assert(d == os.path.abspath('.')) - assert(is_writable(d)) + assert_(d == os.path.abspath('.')) + assert_(is_writable(d)) + def test_pardir(self): " make sure it handles relative values " d = build_tools.configure_build_dir('..') if is_writable('..'): - assert(d == os.path.abspath('..')) - assert(is_writable(d)) + assert_(d == os.path.abspath('..')) + assert_(is_writable(d)) + def test_bad_path(self): " bad path should return same as default (and warn) " d = build_tools.configure_build_dir('_bad_path_') d2 = build_tools.configure_build_dir() - assert(d == d2) - assert(is_writable(d)) + assert_(d == d2) + assert_(is_writable(d)) + class TestConfigureTempDir(TestConfigureBuildDir): + def test_default(self): " default behavior returns tempdir" # this'll fail if the temp directory isn't writable. d = build_tools.configure_temp_dir() - assert(d == tempfile.gettempdir()) - assert(is_writable(d)) + assert_(d == tempfile.gettempdir()) + assert_(is_writable(d)) + class TestConfigureSysArgv(TestCase): + def test_simple(self): build_dir = 'build_dir' temp_dir = 'temp_dir' @@ -59,13 +68,14 @@ build_tools.configure_sys_argv(compiler,temp_dir,build_dir) argv = sys.argv[:] bd = argv[argv.index('--build-lib')+1] - assert(bd == build_dir) + assert_(bd == build_dir) td = argv[argv.index('--build-temp')+1] - assert(td == temp_dir) + assert_(td == temp_dir) argv.index('--compiler='+compiler) build_tools.restore_sys_argv() - assert(pre_argv == sys.argv[:]) + assert_(pre_argv == sys.argv[:]) + if __name__ == "__main__": import nose nose.run(argv=['', __file__]) Modified: trunk/scipy/weave/tests/test_c_spec.py =================================================================== --- trunk/scipy/weave/tests/test_c_spec.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_c_spec.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -8,7 +8,7 @@ global test_dir test_dir = '' -from numpy.testing import * +from numpy.testing import TestCase, dec, assert_ from scipy.weave import inline_tools,ext_tools,c_spec from scipy.weave.build_tools import msvc_exists, gcc_exists @@ -48,23 +48,29 @@ class IntConverter(TestCase): + compiler = '' + @dec.slow def test_type_match_string(self): s = c_spec.int_converter() - assert( not s.type_match('string') ) + assert_( not s.type_match('string') ) + @dec.slow def test_type_match_int(self): s = c_spec.int_converter() - assert(s.type_match(5)) + assert_(s.type_match(5)) + @dec.slow def test_type_match_float(self): s = c_spec.int_converter() - assert(not s.type_match(5.)) + assert_(not s.type_match(5.)) + @dec.slow def test_type_match_complex(self): s = c_spec.int_converter() - assert(not s.type_match(5.+1j)) + assert_(not s.type_match(5.+1j)) + @dec.slow def test_var_in(self): mod_name = 'int_var_in' + self.compiler @@ -106,26 +112,33 @@ b=1 c = test(b) - assert( c == 3) + assert_( c == 3) + class FloatConverter(TestCase): + compiler = '' + @dec.slow def test_type_match_string(self): s = c_spec.float_converter() - assert( not s.type_match('string')) + assert_( not s.type_match('string')) + @dec.slow def test_type_match_int(self): s = c_spec.float_converter() - assert(not s.type_match(5)) + assert_(not s.type_match(5)) + @dec.slow def test_type_match_float(self): s = c_spec.float_converter() - assert(s.type_match(5.)) + assert_(s.type_match(5.)) + @dec.slow def test_type_match_complex(self): s = c_spec.float_converter() - assert(not s.type_match(5.+1j)) + assert_(not s.type_match(5.+1j)) + @dec.slow def test_float_var_in(self): mod_name = sys._getframe().f_code.co_name + self.compiler @@ -150,7 +163,6 @@ except TypeError: pass - @dec.slow def test_float_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler @@ -167,26 +179,33 @@ exec 'from ' + mod_name + ' import test' b=1. c = test(b) - assert( c == 3.) + assert_( c == 3.) + class ComplexConverter(TestCase): + compiler = '' + @dec.slow def test_type_match_string(self): s = c_spec.complex_converter() - assert( not s.type_match('string') ) + assert_( not s.type_match('string') ) + @dec.slow def test_type_match_int(self): s = c_spec.complex_converter() - assert(not s.type_match(5)) + assert_(not s.type_match(5)) + @dec.slow def test_type_match_float(self): s = c_spec.complex_converter() - assert(not s.type_match(5.)) + assert_(not s.type_match(5.)) + @dec.slow def test_type_match_complex(self): s = c_spec.complex_converter() - assert(s.type_match(5.+1j)) + assert_(s.type_match(5.+1j)) + @dec.slow def test_complex_var_in(self): mod_name = sys._getframe().f_code.co_name + self.compiler @@ -227,14 +246,17 @@ exec 'from ' + mod_name + ' import test' b=1.+1j c = test(b) - assert( c == 3.+3j) + assert_( c == 3.+3j) + #---------------------------------------------------------------------------- # File conversion tests #---------------------------------------------------------------------------- class FileConverter(TestCase): + compiler = '' + @dec.slow def test_py_to_file(self): import tempfile @@ -246,7 +268,8 @@ inline_tools.inline(code,['file'],compiler=self.compiler,force=1) file.close() file = open(file_name,'r') - assert(file.read() == "hello bob") + assert_(file.read() == "hello bob") + @dec.slow def test_file_to_py(self): import tempfile @@ -263,8 +286,9 @@ file.write("hello fred") file.close() file = open(file_name,'r') - assert(file.read() == "hello fred") + assert_(file.read() == "hello fred") + #---------------------------------------------------------------------------- # Instance conversion tests #---------------------------------------------------------------------------- @@ -272,12 +296,15 @@ class InstanceConverter(TestCase): pass + #---------------------------------------------------------------------------- # Callable object conversion tests #---------------------------------------------------------------------------- class CallableConverter(TestCase): + compiler='' + @dec.slow def test_call_function(self): import string @@ -295,45 +322,58 @@ actual = inline_tools.inline(code,['func','search_str','sub_str'], compiler=self.compiler,force=1) desired = func(search_str,sub_str) - assert(desired == actual) + assert_(desired == actual) + class SequenceConverter(TestCase): + compiler = '' + @dec.slow def test_convert_to_dict(self): d = {} inline_tools.inline("",['d'],compiler=self.compiler,force=1) + @dec.slow def test_convert_to_list(self): l = [] inline_tools.inline("",['l'],compiler=self.compiler,force=1) + @dec.slow def test_convert_to_string(self): s = 'hello' inline_tools.inline("",['s'],compiler=self.compiler,force=1) + @dec.slow def test_convert_to_tuple(self): t = () inline_tools.inline("",['t'],compiler=self.compiler,force=1) + class StringConverter(TestCase): + compiler = '' + @dec.slow def test_type_match_string(self): s = c_spec.string_converter() - assert( s.type_match('string') ) + assert_( s.type_match('string') ) + @dec.slow def test_type_match_int(self): s = c_spec.string_converter() - assert(not s.type_match(5)) + assert_(not s.type_match(5)) + @dec.slow def test_type_match_float(self): s = c_spec.string_converter() - assert(not s.type_match(5.)) + assert_(not s.type_match(5.)) + @dec.slow def test_type_match_complex(self): s = c_spec.string_converter() - assert(not s.type_match(5.+1j)) + assert_(not s.type_match(5.+1j)) + @dec.slow def test_var_in(self): mod_name = 'string_var_in'+self.compiler @@ -375,20 +415,25 @@ exec 'from ' + mod_name + ' import test' b='bub' c = test(b) - assert( c == 'hello') + assert_( c == 'hello') + class ListConverter(TestCase): + compiler = '' + @dec.slow def test_type_match_bad(self): s = c_spec.list_converter() objs = [{},(),'',1,1.,1+1j] for i in objs: - assert( not s.type_match(i) ) + assert_( not s.type_match(i) ) + @dec.slow def test_type_match_good(self): s = c_spec.list_converter() - assert(s.type_match([])) + assert_(s.type_match([])) + @dec.slow def test_var_in(self): mod_name = 'list_var_in'+self.compiler @@ -430,7 +475,7 @@ exec 'from ' + mod_name + ' import test' b=[1,2] c = test(b) - assert( c == ['hello']) + assert_( c == ['hello']) @dec.slow def test_speed(self): @@ -491,20 +536,25 @@ sum3 -= i t2 = time.time() print 'python:', t2 - t1 - assert( sum1 == sum2 and sum1 == sum3) + assert_( sum1 == sum2 and sum1 == sum3) + class TupleConverter(TestCase): + compiler = '' + @dec.slow def test_type_match_bad(self): s = c_spec.tuple_converter() objs = [{},[],'',1,1.,1+1j] for i in objs: - assert( not s.type_match(i) ) + assert_( not s.type_match(i) ) + @dec.slow def test_type_match_good(self): s = c_spec.tuple_converter() - assert(s.type_match((1,))) + assert_(s.type_match((1,))) + @dec.slow def test_var_in(self): mod_name = 'tuple_var_in'+self.compiler @@ -547,7 +597,7 @@ exec 'from ' + mod_name + ' import test' b=(1,2) c = test(b) - assert( c == ('hello',None)) + assert_( c == ('hello',None)) class DictConverter(TestCase): @@ -565,11 +615,13 @@ s = c_spec.dict_converter() objs = [[],(),'',1,1.,1+1j] for i in objs: - assert( not s.type_match(i) ) + assert_( not s.type_match(i) ) + @dec.slow def test_type_match_good(self): s = c_spec.dict_converter() - assert(s.type_match({})) + assert_(s.type_match({})) + @dec.slow def test_var_in(self): mod_name = 'dict_var_in'+self.compiler @@ -611,8 +663,9 @@ exec 'from ' + mod_name + ' import test' b = {'z':2} c = test(b) - assert( c['hello'] == 5) + assert_( c['hello'] == 5) + # for compiler in compilers: # for name,klass in globals().iteritems(): # if name[:4]=="Test" and name[-9:] == "Converter": @@ -739,6 +792,7 @@ # for _n in dir(): # if _n[:7]=='TestGcc': exec 'del '+_n # + if __name__ == "__main__": import nose nose.run(argv=['', __file__]) Modified: trunk/scipy/weave/tests/test_catalog.py =================================================================== --- trunk/scipy/weave/tests/test_catalog.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_catalog.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -1,11 +1,11 @@ import sys import os +from numpy.testing import TestCase, assert_ -from numpy.testing import * - from scipy.weave import catalog -from weave_test_utils import * +from weave_test_utils import clear_temp_catalog, restore_temp_catalog, \ + empty_temp_dir, cleanup_temp_dir class TestDefaultDir(TestCase): @@ -19,54 +19,64 @@ test_file.close() os.remove(name) + class TestOsDependentCatalogName(TestCase): pass + class TestCatalogPath(TestCase): + def test_default(self): in_path = catalog.default_dir() path = catalog.catalog_path(in_path) d,f = os.path.split(path) - assert(d == in_path) - assert(f == catalog.os_dependent_catalog_name()) + assert_(d == in_path) + assert_(f == catalog.os_dependent_catalog_name()) + def test_current(self): in_path = '.' path = catalog.catalog_path(in_path) d,f = os.path.split(path) - assert(d == os.path.abspath(in_path)) - assert(f == catalog.os_dependent_catalog_name()) + assert_(d == os.path.abspath(in_path)) + assert_(f == catalog.os_dependent_catalog_name()) + def test_user(path): if sys.platform != 'win32': in_path = '~' path = catalog.catalog_path(in_path) d,f = os.path.split(path) - assert(d == os.path.expanduser(in_path)) - assert(f == catalog.os_dependent_catalog_name()) + assert_(d == os.path.expanduser(in_path)) + assert_(f == catalog.os_dependent_catalog_name()) + def test_module(self): # hand it a module and see if it uses the parent directory # of the module. path = catalog.catalog_path(os.__file__) d,f = os.path.split(os.__file__) d2,f = os.path.split(path) - assert (d2 == d) + assert_(d2 == d) + def test_path(self): # use os.__file__ to get a usable directory. in_path,f = os.path.split(os.__file__) path = catalog.catalog_path(in_path) d,f = os.path.split(path) - assert (d == in_path) + assert_(d == in_path) + def test_bad_path(self): # stupid_path_name in_path = 'stupid_path_name' path = catalog.catalog_path(in_path) - assert (path is None) + assert_(path is None) + class TestGetCatalog(TestCase): """ This only tests whether new catalogs are created correctly. And whether non-existent return None correctly with read mode. Putting catalogs in the right place is all tested with catalog_dir tests. """ + def get_test_dir(self,erase = 0): # make sure tempdir catalog doesn't exist import tempfile, glob @@ -80,6 +90,7 @@ for cat_file in cat_files: os.remove(cat_file) return pardir + def remove_dir(self,d): import distutils.dir_util distutils.dir_util.remove_tree(d) @@ -88,13 +99,15 @@ pardir = self.get_test_dir(erase=1) cat = catalog.get_catalog(pardir,'r') self.remove_dir(pardir) - assert(cat is None) + assert_(cat is None) + def test_create_catalog(self): pardir = self.get_test_dir(erase=1) cat = catalog.get_catalog(pardir,'c') self.remove_dir(pardir) - assert(cat is not None) + assert_(cat is not None) + class TestCatalog(TestCase): def clear_environ(self): @@ -103,12 +116,15 @@ del os.environ['PYTHONCOMPILED'] else: self.old_PYTHONCOMPILED = None + def reset_environ(self): if self.old_PYTHONCOMPILED: os.environ['PYTHONCOMPILED'] = self.old_PYTHONCOMPILED self.old_PYTHONCOMPILED = None + def setUp(self): self.clear_environ() + def tearDown(self): self.reset_environ() @@ -116,58 +132,66 @@ q = catalog.catalog() q.set_module_directory('bob') r = q.get_module_directory() - assert (r == 'bob') + assert_(r == 'bob') + def test_clear_module_directory(self): q = catalog.catalog() r = q.get_module_directory() - assert (r is None) + assert_(r is None) q.set_module_directory('bob') r = q.clear_module_directory() - assert (r is None) + assert_(r is None) + def test_get_environ_path(self): if sys.platform == 'win32': sep = ';' else: sep = ':' os.environ['PYTHONCOMPILED'] = sep.join(('path1','path2','path3')) q = catalog.catalog() path = q.get_environ_path() - assert(path == ['path1','path2','path3']) + assert_(path == ['path1','path2','path3']) + def test_build_search_order1(self): """ MODULE in search path should be replaced by module_dir. """ q = catalog.catalog(['first','MODULE','third']) q.set_module_directory('second') order = q.build_search_order() - assert(order == ['first','second','third',catalog.default_dir()]) + assert_(order == ['first','second','third',catalog.default_dir()]) + def test_build_search_order2(self): """ MODULE in search path should be removed if module_dir==None. """ q = catalog.catalog(['first','MODULE','third']) order = q.build_search_order() - assert(order == ['first','third',catalog.default_dir()]) + assert_(order == ['first','third',catalog.default_dir()]) + def test_build_search_order3(self): """ If MODULE is absent, module_dir shouldn't be in search path. """ q = catalog.catalog(['first','second']) q.set_module_directory('third') order = q.build_search_order() - assert(order == ['first','second',catalog.default_dir()]) + assert_(order == ['first','second',catalog.default_dir()]) + def test_build_search_order4(self): """ Make sure environment variable is getting used. """ q = catalog.catalog(['first','second']) - if sys.platform == 'win32': sep = ';' - else: sep = ':' + if sys.platform == 'win32': + sep = ';' + else: + sep = ':' os.environ['PYTHONCOMPILED'] = sep.join(('MODULE','fourth','fifth')) q.set_module_directory('third') order = q.build_search_order() - assert(order == ['first','second','third','fourth','fifth',catalog.default_dir()]) + assert_(order == ['first','second','third','fourth','fifth',catalog.default_dir()]) def test_catalog_files1(self): """ Be sure we get at least one file even without specifying the path. """ q = catalog.catalog() files = q.get_catalog_files() - assert(len(files) == 1) + assert_(len(files) == 1) def test_catalog_files2(self): """ Ignore bad paths in the path. @@ -175,7 +199,7 @@ q = catalog.catalog() os.environ['PYTHONCOMPILED'] = '_some_bad_path_' files = q.get_catalog_files() - assert(len(files) == 1) + assert_(len(files) == 1) def test_get_existing_files1(self): """ Shouldn't get any files when temp doesn't exist and no path set. @@ -184,7 +208,8 @@ q = catalog.catalog() files = q.get_existing_files() restore_temp_catalog() - assert(len(files) == 0) + assert_(len(files) == 0) + def test_get_existing_files2(self): """ Shouldn't get a single file from the temp dir. """ @@ -197,7 +222,7 @@ q = catalog.catalog() files = q.get_existing_files() restore_temp_catalog() - assert(len(files) == 1) + assert_(len(files) == 1) def test_access_writable_file(self): """ There should always be a writable file -- even if it is in temp @@ -210,12 +235,15 @@ finally: f.close() os.remove(file) + def test_writable_with_bad_path(self): """ There should always be a writable file -- even if search paths contain bad values. """ - if sys.platform == 'win32': sep = ';' - else: sep = ':' + if sys.platform == 'win32': + sep = ';' + else: + sep = ':' os.environ['PYTHONCOMPILED'] = sep.join(('_bad_path_name_')) q = catalog.catalog() file = q.get_writable_file() @@ -225,6 +253,7 @@ finally: f.close() os.remove(file) + def test_writable_dir(self): """ Check that we can create a file in the writable directory """ @@ -237,14 +266,15 @@ finally: f.close() os.remove(file) + def test_unique_module_name(self): """ Check that we can create a file in the writable directory """ q = catalog.catalog() file = q.unique_module_name('bob') cfile1 = file+'.cpp' - assert(not os.path.exists(cfile1)) - #make sure it is writable + assert_(not os.path.exists(cfile1)) + # Make sure it is writable try: f = open(cfile1,'w') f.write('bob') @@ -253,8 +283,9 @@ # try again with same code fragment -- should get unique name file = q.unique_module_name('bob') cfile2 = file+'.cpp' - assert(not os.path.exists(cfile2+'.cpp')) + assert_(not os.path.exists(cfile2+'.cpp')) os.remove(cfile1) + def test_add_function_persistent1(self): """ Test persisting a function in the default catalog """ @@ -269,7 +300,7 @@ # any way to clean modules??? restore_temp_catalog() for i in funcs: - assert(i in pfuncs) + assert_(i in pfuncs) def test_add_function_ordered(self): clear_temp_catalog() @@ -315,13 +346,13 @@ # been read in from a prior catalog file (such as the defualt one). # the test should really be made so that these aren't read in, but # until I get this figured out... - #assert(funcs1 == [string.lower,string.upper]) - #assert(funcs2 == [os.chdir,os.abort,string.replace,string.find]) - #assert(funcs3 == [re.purge,re.match,os.open, + #assert_(funcs1 == [string.lower,string.upper]) + #assert_(funcs2 == [os.chdir,os.abort,string.replace,string.find]) + #assert_(funcs3 == [re.purge,re.match,os.open, # os.access,string.atoi,string.atof]) - assert(funcs1[:2] == [string.lower,string.upper]),`funcs1` - assert(funcs2[:4] == [os.chdir,os.abort,string.replace,string.find]) - assert(funcs3[:6] == [re.purge,re.match,os.open, + assert_(funcs1[:2] == [string.lower,string.upper]),`funcs1` + assert_(funcs2[:4] == [os.chdir,os.abort,string.replace,string.find]) + assert_(funcs3[:6] == [re.purge,re.match,os.open, os.access,string.atoi,string.atof]) cleanup_temp_dir(user_dir) cleanup_temp_dir(env_dir) Modified: trunk/scipy/weave/tests/test_ext_tools.py =================================================================== --- trunk/scipy/weave/tests/test_ext_tools.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_ext_tools.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -1,25 +1,28 @@ -import nose -from numpy.testing import * +from numpy.testing import TestCase, dec, assert_equal, assert_ + from scipy.weave import ext_tools, c_spec try: from scipy.weave.standard_array_spec import array_converter except ImportError: pass # requires numpy.numerix +from weave_test_utils import empty_temp_dir -from weave_test_utils import * - build_dir = empty_temp_dir() + class TestExtModule(TestCase): + #should really do some testing of where modules end up + @dec.slow def test_simple(self): """ Simplest possible module """ mod = ext_tools.ext_module('simple_ext_module') mod.compile(location = build_dir) import simple_ext_module + @dec.slow def test_multi_functions(self): mod = ext_tools.ext_module('module_multi_function') @@ -33,6 +36,7 @@ import module_multi_function module_multi_function.test() module_multi_function.test2() + @dec.slow def test_with_include(self): # decalaring variables @@ -72,7 +76,7 @@ mod.compile(location = build_dir) import ext_string_and_int c = ext_string_and_int.test(a,b) - assert(c == len(b)) + assert_(c == len(b)) @dec.slow def test_return_tuple(self): @@ -94,10 +98,13 @@ mod.compile(location = build_dir) import ext_return_tuple c,d = ext_return_tuple.test(a) - assert(c==a and d == a+1) + assert_(c==a and d == a+1) + class TestExtFunction(TestCase): + #should really do some testing of where modules end up + @dec.slow def test_simple(self): """ Simplest possible function """ @@ -110,7 +117,9 @@ import simple_ext_function simple_ext_function.test() + class TestAssignVariableTypes(TestCase): + def test_assign_variable_types(self): try: from numpy.numerix import arange, Float32, Float64 @@ -136,6 +145,7 @@ desired = [ad,bd,cd] assert_equal(actual,desired) + if __name__ == "__main__": import nose nose.run(argv=['', __file__]) Modified: trunk/scipy/weave/tests/test_inline_tools.py =================================================================== --- trunk/scipy/weave/tests/test_inline_tools.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_inline_tools.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -1,8 +1,9 @@ -from numpy import * -from numpy.testing import * +from numpy.testing import TestCase, dec, assert_ + from scipy.weave import inline_tools + class TestInline(TestCase): """ These are long running tests... @@ -19,7 +20,7 @@ return_val = PyInt_FromLong(a+1); """ result = inline_tools.inline(code,['a']) - assert(result == 4) + assert_(result == 4) ## Unfortunately, it is not always possible to catch distutils compiler ## errors, since SystemExit is used. Until that is fixed, these tests @@ -28,7 +29,7 @@ ## try: ## a = 1 ## result = inline_tools.inline(code,['a']) -## assert(1) # should've thrown a ValueError +## assert_(1) # should've thrown a ValueError ## except ValueError: ## pass @@ -36,7 +37,7 @@ ## try: ## a = 'string' ## result = inline_tools.inline(code,['a']) -## assert(1) # should've gotten an error +## assert_(1) # should've gotten an error ## except: ## # ?CompileError is the error reported, but catching it doesn't work ## pass Modified: trunk/scipy/weave/tests/test_numpy_scalar_spec.py =================================================================== --- trunk/scipy/weave/tests/test_numpy_scalar_spec.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_numpy_scalar_spec.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -9,7 +9,7 @@ test_dir = '' import numpy -from numpy.testing import * +from numpy.testing import TestCase, dec, assert_ from scipy.weave import inline_tools,ext_tools from scipy.weave.build_tools import msvc_exists, gcc_exists @@ -33,6 +33,7 @@ #---------------------------------------------------------------------------- class NumpyComplexScalarConverter(TestCase): + compiler = '' def setUp(self): @@ -40,16 +41,19 @@ @dec.slow def test_type_match_string(self): - assert( not self.converter.type_match('string') ) + assert_( not self.converter.type_match('string') ) + @dec.slow def test_type_match_int(self): - assert( not self.converter.type_match(5)) + assert_( not self.converter.type_match(5)) + @dec.slow def test_type_match_float(self): - assert( not self.converter.type_match(5.)) + assert_( not self.converter.type_match(5.)) + @dec.slow def test_type_match_complex128(self): - assert(self.converter.type_match(numpy.complex128(5.+1j))) + assert_(self.converter.type_match(numpy.complex128(5.+1j))) @dec.slow def test_complex_var_in(self): @@ -91,13 +95,13 @@ exec 'from ' + mod_name + ' import test' b=1.+1j c = test(b) - assert( c == 3.+3j) + assert_( c == 3.+3j) @dec.slow def test_inline(self): a = numpy.complex128(1+1j) result = inline_tools.inline("return_val=1.0/a;",['a']) - assert( result==.5-.5j) + assert_( result==.5-.5j) # class TestMsvcNumpyComplexScalarConverter( # TestNumpyComplexScalarConverter): @@ -150,6 +154,7 @@ for _n in dir(): if _n[:7]=='TestGcc': exec 'del '+_n + if __name__ == "__main__": import nose nose.run(argv=['', __file__]) Modified: trunk/scipy/weave/tests/test_scxx_dict.py =================================================================== --- trunk/scipy/weave/tests/test_scxx_dict.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_scxx_dict.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -3,7 +3,7 @@ import sys -from numpy.testing import * +from numpy.testing import TestCase, dec, assert_, assert_raises from scipy.weave import inline_tools @@ -21,11 +21,12 @@ return_val = val; """ res = inline_tools.inline(code) - assert sys.getrefcount(res) == 2 - assert res == {} + assert_(sys.getrefcount(res) == 2) + assert_(res == {}) class TestDictHasKey(TestCase): + @dec.slow def test_obj(self): class Foo: @@ -37,7 +38,8 @@ return_val = a.has_key(key); """ res = inline_tools.inline(code,['a','key']) - assert res + assert_(res) + @dec.slow def test_int(self): a = {} @@ -46,7 +48,8 @@ return_val = a.has_key(1234); """ res = inline_tools.inline(code,['a']) - assert res + assert_(res) + @dec.slow def test_double(self): a = {} @@ -55,7 +58,8 @@ return_val = a.has_key(1234.); """ res = inline_tools.inline(code,['a']) - assert res + assert_(res) + @dec.slow def test_complex(self): a = {} @@ -65,7 +69,7 @@ return_val = a.has_key(key); """ res = inline_tools.inline(code,['a','key']) - assert res + assert_(res) @dec.slow def test_string(self): @@ -75,7 +79,8 @@ return_val = a.has_key("b"); """ res = inline_tools.inline(code,['a']) - assert res + assert_(res) + @dec.slow def test_std_string(self): a = {} @@ -85,7 +90,8 @@ return_val = a.has_key(key_name); """ res = inline_tools.inline(code,['a','key_name']) - assert res + assert_(res) + @dec.slow def test_string_fail(self): a = {} @@ -94,8 +100,9 @@ return_val = a.has_key("c"); """ res = inline_tools.inline(code,['a']) - assert not res + assert_(not res) + class TestDictGetItemOp(TestCase): def generic_get(self,code,args=['a']): @@ -103,7 +110,7 @@ a['b'] = 12345 res = inline_tools.inline(code,args) - assert res == a['b'] + assert_(res == a['b']) @dec.slow def test_char(self): @@ -114,10 +121,7 @@ def test_char_fail(self): # We can't through a KeyError for dicts on RHS of # = but not on LHS. Not sure how to deal with this. - try: - self.generic_get('return_val = a["c"];') - except KeyError: - pass + assert_raises(KeyError, self.generic_get, 'return_val = a["c"];') @dec.slow def test_string(self): @@ -137,16 +141,15 @@ def test_obj_fail(self): # We can't through a KeyError for dicts on RHS of # = but not on LHS. Not sure how to deal with this. - try: - code = """ - py::object name = "c"; - return_val = a[name]; - """ - self.generic_get(code,['a']) - except KeyError: - pass + code = """ + py::object name = "c"; + return_val = a[name]; + """ + assert_raises(KeyError, self.generic_get, code, ['a']) + class TestDictSetOperator(TestCase): + def generic_new(self,key,val): # test that value is set correctly and that reference counts # on dict, key, and val are being handled correctly. @@ -154,12 +157,13 @@ # call once to handle mysterious addition of one ref count # on first call to inline. inline_tools.inline("a[key] = val;",['a','key','val']) - assert a[key] == val + assert_(a[key] == val) before = sys.getrefcount(a), sys.getrefcount(key), sys.getrefcount(val) inline_tools.inline("a[key] = val;",['a','key','val']) - assert a[key] == val + assert_(a[key] == val) after = sys.getrefcount(a), sys.getrefcount(key), sys.getrefcount(val) - assert before == after + assert_(before == after) + def generic_overwrite(self,key,val): a = {} overwritten = 1 @@ -168,31 +172,35 @@ # on first call to inline. before_overwritten = sys.getrefcount(overwritten) inline_tools.inline("a[key] = val;",['a','key','val']) - assert a[key] == val + assert_(a[key] == val) before = sys.getrefcount(a), sys.getrefcount(key), sys.getrefcount(val) inline_tools.inline("a[key] = val;",['a','key','val']) - assert a[key] == val + assert_(a[key] == val) after = sys.getrefcount(a), sys.getrefcount(key), sys.getrefcount(val) after_overwritten = sys.getrefcount(overwritten) - assert before == after - assert before_overwritten == after_overwritten + assert_(before == after) + assert_(before_overwritten == after_overwritten) @dec.slow def test_new_int_int(self): key,val = 1234,12345 self.generic_new(key,val) + @dec.slow def test_new_double_int(self): key,val = 1234.,12345 self.generic_new(key,val) + @dec.slow def test_new_std_string_int(self): key,val = "hello",12345 self.generic_new(key,val) + @dec.slow def test_new_complex_int(self): key,val = 1+1j,12345 self.generic_new(key,val) + @dec.slow def test_new_obj_int(self): class Foo: @@ -204,18 +212,22 @@ def test_overwrite_int_int(self): key,val = 1234,12345 self.generic_overwrite(key,val) + @dec.slow def test_overwrite_double_int(self): key,val = 1234.,12345 self.generic_overwrite(key,val) + @dec.slow def test_overwrite_std_string_int(self): key,val = "hello",12345 self.generic_overwrite(key,val) + @dec.slow def test_overwrite_complex_int(self): key,val = 1+1j,12345 self.generic_overwrite(key,val) + @dec.slow def test_overwrite_obj_int(self): class Foo: @@ -223,7 +235,9 @@ key,val = Foo(),12345 self.generic_overwrite(key,val) + class TestDictDel(TestCase): + def generic(self,key): # test that value is set correctly and that reference counts # on dict, key, are being handled correctly. after deletion, @@ -231,30 +245,35 @@ a = {} a[key] = 1 inline_tools.inline("a.del(key);",['a','key']) - assert key not in a + assert_(key not in a) a[key] = 1 before = sys.getrefcount(a), sys.getrefcount(key) inline_tools.inline("a.del(key);",['a','key']) - assert key not in a + assert_(key not in a) after = sys.getrefcount(a), sys.getrefcount(key) - assert before[0] == after[0] - assert before[1] == after[1] + 1 + assert_(before[0] == after[0]) + assert_(before[1] == after[1] + 1) + @dec.slow def test_int(self): key = 1234 self.generic(key) + @dec.slow def test_double(self): key = 1234. self.generic(key) + @dec.slow def test_std_string(self): key = "hello" self.generic(key) + @dec.slow def test_complex(self): key = 1+1j self.generic(key) + @dec.slow def test_obj(self): class Foo: @@ -262,39 +281,46 @@ key = Foo() self.generic(key) + class TestDictOthers(TestCase): + @dec.slow def test_clear(self): a = {} a["hello"] = 1 inline_tools.inline("a.clear();",['a']) - assert not a + assert_(not a) + @dec.slow def test_items(self): a = {} a["hello"] = 1 items = inline_tools.inline("return_val = a.items();",['a']) - assert items == a.items() + assert_(items == a.items()) + @dec.slow def test_values(self): a = {} a["hello"] = 1 values = inline_tools.inline("return_val = a.values();",['a']) - assert values == a.values() + assert_(values == a.values()) + @dec.slow def test_keys(self): a = {} a["hello"] = 1 keys = inline_tools.inline("return_val = a.keys();",['a']) - assert keys == a.keys() + assert_(keys == a.keys()) + @dec.slow def test_update(self): a,b = {},{} a["hello"] = 1 b["hello"] = 2 inline_tools.inline("a.update(b);",['a','b']) - assert a == b + assert_(a == b) + if __name__ == "__main__": import nose nose.run(argv=['', __file__]) Modified: trunk/scipy/weave/tests/test_scxx_object.py =================================================================== --- trunk/scipy/weave/tests/test_scxx_object.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_scxx_object.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -3,8 +3,7 @@ import sys -import nose -from numpy.testing import * +from numpy.testing import TestCase, dec, assert_equal, assert_, assert_raises from scipy.weave import inline_tools @@ -14,6 +13,7 @@ # Check that construction from basic types is allowed and have correct # reference counts #------------------------------------------------------------------------ + @dec.slow def test_int(self): # strange int value used to try and make sure refcount is 2. @@ -24,6 +24,7 @@ res = inline_tools.inline(code) assert_equal(sys.getrefcount(res),2) assert_equal(res,1001) + @dec.slow def test_float(self): code = """ @@ -33,6 +34,7 @@ res = inline_tools.inline(code) assert_equal(sys.getrefcount(res),2) assert_equal(res,1.0) + @dec.slow def test_double(self): code = """ @@ -42,6 +44,7 @@ res = inline_tools.inline(code) assert_equal(sys.getrefcount(res),2) assert_equal(res,1.0) + @dec.slow def test_complex(self): code = """ @@ -52,6 +55,7 @@ res = inline_tools.inline(code) assert_equal(sys.getrefcount(res),2) assert_equal(res,1.0+1.0j) + @dec.slow def test_string(self): code = """ @@ -77,6 +81,7 @@ #------------------------------------------------------------------------ # Check the object print protocol. #------------------------------------------------------------------------ + @dec.slow def test_stringio(self): import cStringIO @@ -104,6 +109,7 @@ class TestObjectCast(TestCase): + @dec.slow def test_int_cast(self): code = """ @@ -111,6 +117,7 @@ int raw_val __attribute__ ((unused)) = val; """ inline_tools.inline(code) + @dec.slow def test_double_cast(self): code = """ @@ -118,6 +125,7 @@ double raw_val __attribute__ ((unused)) = val; """ inline_tools.inline(code) + @dec.slow def test_float_cast(self): code = """ @@ -125,6 +133,7 @@ float raw_val __attribute__ ((unused)) = val; """ inline_tools.inline(code) + @dec.slow def test_complex_cast(self): code = """ @@ -133,6 +142,7 @@ std::complex raw_val __attribute__ ((unused)) = val; """ inline_tools.inline(code) + @dec.slow def test_string_cast(self): code = """ @@ -141,6 +151,7 @@ """ inline_tools.inline(code) + # test class used for testing python class access from C++. class Foo: def bar(self): @@ -154,7 +165,9 @@ # def __str__(self): # return "b" + class TestObjectHasattr(TestCase): + @dec.slow def test_string(self): a = Foo() @@ -163,7 +176,8 @@ return_val = a.hasattr("b"); """ res = inline_tools.inline(code,['a']) - assert res + assert_(res) + @dec.slow def test_std_string(self): a = Foo() @@ -173,7 +187,8 @@ return_val = a.hasattr(attr_name); """ res = inline_tools.inline(code,['a','attr_name']) - assert res + assert_(res) + @dec.slow def test_string_fail(self): a = Foo() @@ -182,7 +197,8 @@ return_val = a.hasattr("c"); """ res = inline_tools.inline(code,['a']) - assert not res + assert_(not res) + @dec.slow def test_inline(self): """ THIS NEEDS TO MOVE TO THE INLINE TEST SUITE @@ -213,8 +229,9 @@ return_val = a.hasattr("bar"); """ res = inline_tools.inline(code,['a']) - assert res + assert_(res) + class TestObjectAttr(TestCase): def generic_attr(self,code,args=['a']): @@ -234,10 +251,7 @@ @dec.slow def test_char_fail(self): - try: - self.generic_attr('return_val = a.attr("c");') - except AttributeError: - pass + assert_raises(AttributeError, self.generic_attr, 'return_val = a.attr("c");') @dec.slow def test_string(self): @@ -245,10 +259,7 @@ @dec.slow def test_string_fail(self): - try: - self.generic_attr('return_val = a.attr(std::string("c"));') - except AttributeError: - pass + assert_raises(AttributeError, self.generic_attr, 'return_val = a.attr(std::string("c"));') @dec.slow def test_obj(self): @@ -260,14 +271,11 @@ @dec.slow def test_obj_fail(self): - try: - code = """ - py::object name = "c"; - return_val = a.attr(name); - """ - self.generic_attr(code,['a']) - except AttributeError: - pass + code = """ + py::object name = "c"; + return_val = a.attr(name); + """ + assert_raises(AttributeError, self.generic_attr, code, ['a']) @dec.slow def test_attr_call(self): @@ -280,6 +288,7 @@ assert_equal(res,"bar results") assert_equal(first,second) + class TestObjectSetAttr(TestCase): def generic_existing(self, code, desired): @@ -298,15 +307,19 @@ @dec.slow def test_existing_char(self): self.generic_existing('a.set_attr("b","hello");',"hello") + @dec.slow def test_new_char(self): self.generic_new('a.set_attr("b","hello");',"hello") + @dec.slow def test_existing_string(self): self.generic_existing('a.set_attr("b",std::string("hello"));',"hello") + @dec.slow def test_new_string(self): self.generic_new('a.set_attr("b",std::string("hello"));',"hello") + @dec.slow def test_existing_object(self): code = """ @@ -314,6 +327,7 @@ a.set_attr("b",obj); """ self.generic_existing(code,"hello") + @dec.slow def test_new_object(self): code = """ @@ -321,6 +335,7 @@ a.set_attr("b",obj); """ self.generic_new(code,"hello") + @dec.slow def test_new_fail(self): try: @@ -335,9 +350,11 @@ @dec.slow def test_existing_int(self): self.generic_existing('a.set_attr("b",1);',1) + @dec.slow def test_existing_double(self): self.generic_existing('a.set_attr("b",1.0);',1.0) + @dec.slow def test_existing_complex(self): code = """ @@ -345,9 +362,11 @@ a.set_attr("b",obj); """ self.generic_existing(code,1+1j) + @dec.slow def test_existing_char1(self): self.generic_existing('a.set_attr("b","hello");',"hello") + @dec.slow def test_existing_string1(self): code = """ @@ -356,17 +375,20 @@ """ self.generic_existing(code,"hello") + class TestObjectDel(TestCase): + def generic(self, code): args = ['a'] a = Foo() a.b = 12345 res = inline_tools.inline(code,args) - assert not hasattr(a,"b") + assert_(not hasattr(a,"b")) @dec.slow def test_char(self): self.generic('a.del("b");') + @dec.slow def test_string(self): code = """ @@ -374,6 +396,7 @@ a.del(name); """ self.generic(code) + @dec.slow def test_object(self): code = """ @@ -382,12 +405,15 @@ """ self.generic(code) + class TestObjectCmp(TestCase): + @dec.slow def test_equal(self): a,b = 1,1 res = inline_tools.inline('return_val = (a == b);',['a','b']) assert_equal(res,(a == b)) + @dec.slow def test_equal_objects(self): class Foo: @@ -398,56 +424,67 @@ a,b = Foo(1),Foo(2) res = inline_tools.inline('return_val = (a == b);',['a','b']) assert_equal(res,(a == b)) + @dec.slow def test_lt(self): a,b = 1,2 res = inline_tools.inline('return_val = (a < b);',['a','b']) assert_equal(res,(a < b)) + @dec.slow def test_gt(self): a,b = 1,2 res = inline_tools.inline('return_val = (a > b);',['a','b']) assert_equal(res,(a > b)) + @dec.slow def test_gte(self): a,b = 1,2 res = inline_tools.inline('return_val = (a >= b);',['a','b']) assert_equal(res,(a >= b)) + @dec.slow def test_lte(self): a,b = 1,2 res = inline_tools.inline('return_val = (a <= b);',['a','b']) assert_equal(res,(a <= b)) + @dec.slow def test_not_equal(self): a,b = 1,2 res = inline_tools.inline('return_val = (a != b);',['a','b']) assert_equal(res,(a != b)) + @dec.slow def test_int(self): a = 1 res = inline_tools.inline('return_val = (a == 1);',['a']) assert_equal(res,(a == 1)) + @dec.slow def test_int2(self): a = 1 res = inline_tools.inline('return_val = (1 == a);',['a']) assert_equal(res,(a == 1)) + @dec.slow def test_unsigned_long(self): a = 1 res = inline_tools.inline('return_val = (a == (unsigned long)1);',['a']) assert_equal(res,(a == 1)) + @dec.slow def test_double(self): a = 1 res = inline_tools.inline('return_val = (a == 1.0);',['a']) assert_equal(res,(a == 1.0)) + @dec.slow def test_char(self): a = "hello" res = inline_tools.inline('return_val = (a == "hello");',['a']) assert_equal(res,(a == "hello")) + @dec.slow def test_std_string(self): a = "hello" @@ -458,7 +495,9 @@ res = inline_tools.inline(code,['a']) assert_equal(res,(a == "hello")) + class TestObjectRepr(TestCase): + @dec.slow def test_repr(self): class Foo: @@ -475,7 +514,9 @@ assert_equal(first,second) assert_equal(res,"repr return") + class TestObjectStr(TestCase): + @dec.slow def test_str(self): class Foo: @@ -493,8 +534,11 @@ print res assert_equal(res,"str return") + class TestObjectUnicode(TestCase): + # This ain't going to win awards for test of the year... + @dec.slow def test_unicode(self): class Foo: @@ -511,7 +555,9 @@ assert_equal(first,second) assert_equal(res,"unicode") + class TestObjectIsCallable(TestCase): + @dec.slow def test_true(self): class Foo: @@ -519,16 +565,19 @@ return 0 a= Foo() res = inline_tools.inline('return_val = a.is_callable();',['a']) - assert res + assert_(res) + @dec.slow def test_false(self): class Foo: pass a= Foo() res = inline_tools.inline('return_val = a.is_callable();',['a']) - assert not res + assert_(not res) + class TestObjectCall(TestCase): + @dec.slow def test_noargs(self): def Foo(): @@ -536,6 +585,7 @@ res = inline_tools.inline('return_val = Foo.call();',['Foo']) assert_equal(res,(1,2,3)) assert_equal(sys.getrefcount(res),3) # should be 2? + @dec.slow def test_args(self): def Foo(val1,val2): @@ -549,6 +599,7 @@ res = inline_tools.inline(code,['Foo']) assert_equal(res,(1,"hello")) assert_equal(sys.getrefcount(res),2) + @dec.slow def test_args_kw(self): def Foo(val1,val2,val3=1): @@ -564,6 +615,7 @@ res = inline_tools.inline(code,['Foo']) assert_equal(res,(1,"hello",3)) assert_equal(sys.getrefcount(res),2) + @dec.slow def test_noargs_with_args(self): # calling a function that does take args with args @@ -588,7 +640,9 @@ # first should == second, but the weird refcount error assert_equal(second,third) + class TestObjectMcall(TestCase): + @dec.slow def test_noargs(self): a = Foo() @@ -600,6 +654,7 @@ assert_equal(res,"bar results") second = sys.getrefcount(res) assert_equal(first,second) + @dec.slow def test_args(self): a = Foo() @@ -612,6 +667,7 @@ res = inline_tools.inline(code,['a']) assert_equal(res,(1,"hello")) assert_equal(sys.getrefcount(res),2) + @dec.slow def test_args_kw(self): a = Foo() @@ -626,6 +682,7 @@ res = inline_tools.inline(code,['a']) assert_equal(res,(1,"hello",3)) assert_equal(sys.getrefcount(res),2) + @dec.slow def test_std_noargs(self): a = Foo() @@ -638,6 +695,7 @@ assert_equal(res,"bar results") second = sys.getrefcount(res) assert_equal(first,second) + @dec.slow def test_std_args(self): a = Foo() @@ -651,6 +709,7 @@ res = inline_tools.inline(code,['a','method']) assert_equal(res,(1,"hello")) assert_equal(sys.getrefcount(res),2) + @dec.slow def test_std_args_kw(self): a = Foo() @@ -666,6 +725,7 @@ res = inline_tools.inline(code,['a','method']) assert_equal(res,(1,"hello",3)) assert_equal(sys.getrefcount(res),2) + @dec.slow def test_noargs_with_args(self): # calling a function that does take args with args @@ -689,7 +749,9 @@ # first should == second, but the weird refcount error assert_equal(second,third) + class TestObjectHash(TestCase): + @dec.slow def test_hash(self): class Foo: @@ -700,7 +762,9 @@ print 'hash:', res assert_equal(res,123) + class TestObjectIsTrue(TestCase): + @dec.slow def test_true(self): class Foo: @@ -708,13 +772,16 @@ a= Foo() res = inline_tools.inline('return_val = a.is_true();',['a']) assert_equal(res,1) + @dec.slow def test_false(self): a= None res = inline_tools.inline('return_val = a.is_true();',['a']) assert_equal(res,0) + class TestObjectType(TestCase): + @dec.slow def test_type(self): class Foo: @@ -723,7 +790,9 @@ res = inline_tools.inline('return_val = a.type();',['a']) assert_equal(res,type(a)) + class TestObjectSize(TestCase): + @dec.slow def test_size(self): class Foo: @@ -732,6 +801,7 @@ a= Foo() res = inline_tools.inline('return_val = a.size();',['a']) assert_equal(res,len(a)) + @dec.slow def test_len(self): class Foo: @@ -740,6 +810,7 @@ a= Foo() res = inline_tools.inline('return_val = a.len();',['a']) assert_equal(res,len(a)) + @dec.slow def test_length(self): class Foo: @@ -749,8 +820,12 @@ res = inline_tools.inline('return_val = a.length();',['a']) assert_equal(res,len(a)) + from UserList import UserList + + class TestObjectSetItemOpIndex(TestCase): + @dec.slow def test_list_refcount(self): a = UserList([1,2,3]) @@ -759,39 +834,48 @@ before1 = sys.getrefcount(a) after1 = sys.getrefcount(a) assert_equal(after1,before1) + @dec.slow def test_set_int(self): a = UserList([1,2,3]) inline_tools.inline("a[1] = 1234;",['a']) assert_equal(sys.getrefcount(a[1]),2) assert_equal(a[1],1234) + @dec.slow def test_set_double(self): a = UserList([1,2,3]) inline_tools.inline("a[1] = 123.0;",['a']) assert_equal(sys.getrefcount(a[1]),2) assert_equal(a[1],123.0) + @dec.slow def test_set_char(self): a = UserList([1,2,3]) inline_tools.inline('a[1] = "bubba";',['a']) assert_equal(sys.getrefcount(a[1]),2) assert_equal(a[1],'bubba') + @dec.slow - def test_set_string(self): + def test_set_string1(self): a = UserList([1,2,3]) inline_tools.inline('a[1] = std::string("sissy");',['a']) assert_equal(sys.getrefcount(a[1]),2) assert_equal(a[1],'sissy') + @dec.slow - def test_set_string(self): + def test_set_string2(self): a = UserList([1,2,3]) inline_tools.inline('a[1] = std::complex(1,1);',['a']) assert_equal(sys.getrefcount(a[1]),2) assert_equal(a[1],1+1j) + from UserDict import UserDict + + class TestObjectSetItemOpKey(TestCase): + @dec.slow def test_key_refcount(self): a = UserDict() @@ -841,6 +925,7 @@ assert_equal(sys.getrefcount(key),5) assert_equal(sys.getrefcount(a[key]),2) assert_equal(a[key],123.0) + @dec.slow def test_set_double_new(self): a = UserDict() @@ -849,6 +934,7 @@ assert_equal(sys.getrefcount(key),4) # should be 3 assert_equal(sys.getrefcount(a[key]),2) assert_equal(a[key],123.0) + @dec.slow def test_set_complex(self): a = UserDict() @@ -857,6 +943,7 @@ assert_equal(sys.getrefcount(key),4) # should be 3 assert_equal(sys.getrefcount(a[key]),2) assert_equal(a[key],1234) + @dec.slow def test_set_char(self): a = UserDict() @@ -883,6 +970,7 @@ assert_equal(sys.getrefcount(key),4) assert_equal(sys.getrefcount(a[key]),2) assert_equal(a[key],'bubba') + @dec.slow def test_set_from_member(self): a = UserDict() @@ -891,6 +979,7 @@ inline_tools.inline('a["first"] = a["second"];',['a']) assert_equal(a['first'],a['second']) + if __name__ == "__main__": import nose nose.run(argv=['', __file__]) Modified: trunk/scipy/weave/tests/test_scxx_sequence.py =================================================================== --- trunk/scipy/weave/tests/test_scxx_sequence.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_scxx_sequence.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -4,7 +4,7 @@ import time import sys -from numpy.testing import * +from numpy.testing import TestCase, dec, assert_, assert_raises from scipy.weave import inline_tools @@ -18,6 +18,7 @@ # operator[] (get) # operator[] (set) DONE + class _TestSequenceBase(TestCase): seq_type = None @@ -32,7 +33,7 @@ inline_tools.inline(" ",['a']) after = sys.getrefcount(a) #print '2nd,3rd:', before, after - assert(after == before) + assert_(after == before) @dec.slow def test_in(self): @@ -44,34 +45,34 @@ item = 1 code = "return_val = a.in(item);" res = inline_tools.inline(code,['a','item']) - assert res == 1 + assert_(res == 1) item = 0 res = inline_tools.inline(code,['a','item']) - assert res == 0 + assert_(res == 0) # check overloaded in(int val) method code = "return_val = a.in(1);" res = inline_tools.inline(code,['a']) - assert res == 1 + assert_(res == 1) code = "return_val = a.in(0);" res = inline_tools.inline(code,['a']) - assert res == 0 + assert_(res == 0) # check overloaded in(double val) method code = "return_val = a.in(3.1416);" res = inline_tools.inline(code,['a']) - assert res == 1 + assert_(res == 1) code = "return_val = a.in(3.1417);" res = inline_tools.inline(code,['a']) - assert res == 0 + assert_(res == 0) # check overloaded in(char* val) method code = 'return_val = a.in("alpha");' res = inline_tools.inline(code,['a']) - assert res == 1 + assert_(res == 1) code = 'return_val = a.in("beta");' res = inline_tools.inline(code,['a']) - assert res == 0 + assert_(res == 0) # check overloaded in(std::string val) method code = """ @@ -79,13 +80,13 @@ return_val = a.in(val); """ res = inline_tools.inline(code,['a']) - assert res == 1 + assert_(res == 1) code = """ std::string val = std::string("beta"); return_val = a.in(val); """ res = inline_tools.inline(code,['a']) - assert res == 0 + assert_(res == 0) @dec.slow def test_count(self): @@ -97,22 +98,22 @@ item = 1 code = "return_val = a.count(item);" res = inline_tools.inline(code,['a','item']) - assert res == 1 + assert_(res == 1) # check overloaded count(int val) method code = "return_val = a.count(1);" res = inline_tools.inline(code,['a']) - assert res == 1 + assert_(res == 1) # check overloaded count(double val) method code = "return_val = a.count(3.1416);" res = inline_tools.inline(code,['a']) - assert res == 1 + assert_(res == 1) # check overloaded count(char* val) method code = 'return_val = a.count("alpha");' res = inline_tools.inline(code,['a']) - assert res == 1 + assert_(res == 1) # check overloaded count(std::string val) method code = """ @@ -120,7 +121,7 @@ return_val = a.count(alpha); """ res = inline_tools.inline(code,['a']) - assert res == 1 + assert_(res == 1) @dec.slow def test_access_speed(self): @@ -179,8 +180,9 @@ inline_tools.inline(code,['a','b']) t2 = time.time() print 'weave:', t2 - t1 - assert list(b) == list(a) + assert_(list(b) == list(a)) + class TestTuple(_TestSequenceBase): seq_type = tuple @@ -189,10 +191,8 @@ # Tuples should only allow setting of variables # immediately after creation. a = (1,2,3) - try: - inline_tools.inline("a[1] = 1234;",['a']) - except TypeError: - pass + assert_raises(TypeError, inline_tools.inline, "a[1] = 1234;",['a']) + @dec.slow def test_set_item_operator_equal(self): code = """ @@ -203,9 +203,9 @@ return_val = a; """ a = inline_tools.inline(code) - assert a == (1,2,3) + assert_(a == (1,2,3)) # returned value should only have a single refcount - assert sys.getrefcount(a) == 2 + assert_(sys.getrefcount(a) == 2) @dec.slow def test_set_item_index_error(self): @@ -214,25 +214,21 @@ a[4] = 1; return_val = a; """ - try: - a = inline_tools.inline(code) - assert 0 - except IndexError: - pass + assert_raises(IndexError, inline_tools.inline, code) + @dec.slow def test_get_item_operator_index_error(self): code = """ py::tuple a(3); py::object b = a[4]; // should fail. """ - try: - a = inline_tools.inline(code) - assert 0 - except IndexError: - pass + assert_raises(IndexError, inline_tools.inline, code) + class TestList(_TestSequenceBase): + seq_type = list + @dec.slow def test_append_passed_item(self): a = [] @@ -245,12 +241,13 @@ before1 = sys.getrefcount(a) before2 = sys.getrefcount(item) inline_tools.inline("a.append(item);",['a','item']) - assert a[0] is item + assert_(a[0] is item) del a[0] after1 = sys.getrefcount(a) after2 = sys.getrefcount(item) - assert after1 == before1 - assert after2 == before2 + assert_(after1 == before1) + assert_(after2 == before2) + @dec.slow def test_append(self): a = [] @@ -262,30 +259,31 @@ # check overloaded append(int val) method inline_tools.inline("a.append(1234);",['a']) - assert sys.getrefcount(a[0]) == 2 - assert a[0] == 1234 + assert_(sys.getrefcount(a[0]) == 2) + assert_(a[0] == 1234) del a[0] # check overloaded append(double val) method inline_tools.inline("a.append(123.0);",['a']) - assert sys.getrefcount(a[0]) == 2 - assert a[0] == 123.0 + assert_(sys.getrefcount(a[0]) == 2) + assert_(a[0] == 123.0) del a[0] # check overloaded append(char* val) method inline_tools.inline('a.append("bubba");',['a']) - assert sys.getrefcount(a[0]) == 2 - assert a[0] == 'bubba' + assert_(sys.getrefcount(a[0]) == 2) + assert_(a[0] == 'bubba') del a[0] # check overloaded append(std::string val) method inline_tools.inline('a.append(std::string("sissy"));',['a']) - assert sys.getrefcount(a[0]) == 2 - assert a[0] == 'sissy' + assert_(sys.getrefcount(a[0]) == 2) + assert_(a[0] == 'sissy') del a[0] after1 = sys.getrefcount(a) - assert after1 == before1 + assert_(after1 == before1) + @dec.slow def test_insert(self): a = [1,2,3] @@ -301,30 +299,30 @@ # check overloaded insert(int ndx, int val) method inline_tools.inline("a.insert(1,1234);",['a']) - assert sys.getrefcount(a[1]) == 2 - assert a[1] == 1234 + assert_(sys.getrefcount(a[1]) == 2) + assert_(a[1] == 1234) del a[1] # check overloaded insert(int ndx, double val) method inline_tools.inline("a.insert(1,123.0);",['a']) - assert sys.getrefcount(a[1]) == 2 - assert a[1] == 123.0 + assert_(sys.getrefcount(a[1]) == 2) + assert_(a[1] == 123.0) del a[1] # check overloaded insert(int ndx, char* val) method inline_tools.inline('a.insert(1,"bubba");',['a']) - assert sys.getrefcount(a[1]) == 2 - assert a[1] == 'bubba' + assert_(sys.getrefcount(a[1]) == 2) + assert_(a[1] == 'bubba') del a[1] # check overloaded insert(int ndx, std::string val) method inline_tools.inline('a.insert(1,std::string("sissy"));',['a']) - assert sys.getrefcount(a[1]) == 2 - assert a[1] == 'sissy' + assert_(sys.getrefcount(a[1]) == 2) + assert_(a[1] == 'sissy') del a[0] after1 = sys.getrefcount(a) - assert after1 == before1 + assert_(after1 == before1) @dec.slow def test_set_item_operator_equal(self): @@ -335,18 +333,18 @@ # check overloaded insert(int ndx, int val) method inline_tools.inline("a[1] = 1234;",['a']) - assert sys.getrefcount(a[1]) == 2 - assert a[1] == 1234 + assert_(sys.getrefcount(a[1]) == 2) + assert_(a[1] == 1234) # check overloaded insert(int ndx, double val) method inline_tools.inline("a[1] = 123.0;",['a']) - assert sys.getrefcount(a[1]) == 2 - assert a[1] == 123.0 + assert_(sys.getrefcount(a[1]) == 2) + assert_(a[1] == 123.0) # check overloaded insert(int ndx, char* val) method inline_tools.inline('a[1] = "bubba";',['a']) - assert sys.getrefcount(a[1]) == 2 - assert a[1] == 'bubba' + assert_(sys.getrefcount(a[1]) == 2) + assert_(a[1] == 'bubba') # check overloaded insert(int ndx, std::string val) method code = """ @@ -354,11 +352,12 @@ a[1] = val; """ inline_tools.inline(code,['a']) - assert sys.getrefcount(a[1]) == 2 - assert a[1] == 'sissy' + assert_(sys.getrefcount(a[1]) == 2) + assert_(a[1] == 'sissy') after1 = sys.getrefcount(a) - assert after1 == before1 + assert_(after1 == before1) + @dec.slow def test_set_item_operator_equal_created(self): code = """ @@ -369,31 +368,25 @@ return_val = a; """ a = inline_tools.inline(code) - assert a == [1,2,3] + assert_(a == [1,2,3]) # returned value should only have a single refcount - assert sys.getrefcount(a) == 2 + assert_(sys.getrefcount(a) == 2) + @dec.slow def test_set_item_index_error(self): code = """ py::list a(3); a[4] = 1; """ - try: - a = inline_tools.inline(code) - assert 0 - except IndexError: - pass + assert_raises(IndexError, inline_tools.inline, code) + @dec.slow def test_get_item_index_error(self): code = """ py::list a(3); py::object o = a[4]; """ - try: - a = inline_tools.inline(code) - assert 0 - except IndexError: - pass + assert_raises(IndexError, inline_tools.inline, code) @dec.slow def test_string_add_speed(self): @@ -421,7 +414,8 @@ inline_tools.inline(code,['a','b']) t2 = time.time() print 'weave:', t2 - t1 - assert b == desired + assert_(b == desired) + @dec.slow def test_int_add_speed(self): N = 1000000 @@ -447,8 +441,9 @@ inline_tools.inline(code,['a','b']) t2 = time.time() print 'weave:', t2 - t1 - assert b == desired + assert_(b == desired) + if __name__ == "__main__": import nose nose.run(argv=['', __file__]) Modified: trunk/scipy/weave/tests/test_size_check.py =================================================================== --- trunk/scipy/weave/tests/test_size_check.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_size_check.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -1,8 +1,8 @@ import numpy as np -from numpy.testing import * +from numpy.testing import TestCase, assert_array_equal, run_module_suite from scipy.weave import size_check -from scipy.weave.ast_tools import * +from scipy.weave.ast_tools import harvest_variables empty = np.array(()) @@ -316,14 +316,14 @@ except: print 'EXPR:',expr print 'ACTUAL:',actual - print 'DEISRED:',desired + print 'DESIRED:',desired def generic_wrap(self,expr,**kw): try: x = np.array(eval(expr,kw)) try: desired = x.shape except: - desired = zeros(()) + desired = np.zeros(()) except: desired = 'failed' self.generic_check(expr,desired,**kw) @@ -365,5 +365,4 @@ if __name__ == "__main__": - import nose run_module_suite() Modified: trunk/scipy/weave/tests/test_slice_handler.py =================================================================== --- trunk/scipy/weave/tests/test_slice_handler.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_slice_handler.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -1,9 +1,10 @@ -from numpy.testing import * +from numpy.testing import TestCase, assert_equal from scipy.weave import slice_handler from scipy.weave.slice_handler import indexed_array_pattern -from scipy.weave.ast_tools import * +from scipy.weave.ast_tools import ast_to_string, find_first_pattern + class TestBuildSliceAtom(TestCase): def generic_check(self,slice_vars,desired): pos = slice_vars['pos'] @@ -117,6 +118,7 @@ return out class TestTransformSlices(TestCase): + def generic_check(self,suite_string,desired): import parser ast_list = parser.suite(suite_string).tolist() @@ -128,12 +130,13 @@ desired = replace_whitespace(desired) assert_equal(actual,desired,suite_string) - def test_simple_expr(self): + def test_simple_expr1(self): """transform a[:] to slice notation""" test ="a[:]" - desired = 'a[slice(_beg,_end,_stp)]' + desired = 'a[slice(_beg,_end)]' self.generic_check(test,desired) - def test_simple_expr(self): + + def test_simple_expr2(self): """transform a[:,:] = b[:,1:1+2:3] *(c[1-2+i:,:] - c[:,:])""" test ="a[:,:] = b[:,1:1+2:3] *(c[1-2+i:,:] - c[:,:])" desired = " a[slice(_beg,_end),slice(_beg,_end)] = "\ Modified: trunk/scipy/weave/tests/test_standard_array_spec.py =================================================================== --- trunk/scipy/weave/tests/test_standard_array_spec.py 2010-10-01 10:23:34 UTC (rev 6828) +++ trunk/scipy/weave/tests/test_standard_array_spec.py 2010-10-10 04:40:47 UTC (rev 6829) @@ -1,5 +1,5 @@ -from numpy import * -from numpy.testing import * +from numpy import arange +from numpy.testing import TestCase, assert_ from scipy.weave import standard_array_spec @@ -9,17 +9,22 @@ out = out.replace("\n","") return out + class TestArrayConverter(TestCase): + def test_type_match_string(self): s = standard_array_spec.array_converter() - assert( not s.type_match('string') ) + assert_( not s.type_match('string') ) + def test_type_match_int(self): s = standard_array_spec.array_converter() - assert(not s.type_match(5)) + assert_(not s.type_match(5)) + def test_type_match_array(self): s = standard_array_spec.array_converter() - assert(s.type_match(arange(4))) + assert_(s.type_match(arange(4))) + if __name__ == "__main__": import nose nose.run(argv=['', __file__]) From scipy-svn at scipy.org Sun Oct 10 01:33:44 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 10 Oct 2010 00:33:44 -0500 (CDT) Subject: [Scipy-svn] r6830 - trunk/scipy/io/tests Message-ID: <20101010053344.021AF6E004@scipy.org> Author: warren.weckesser Date: 2010-10-10 00:33:43 -0500 (Sun, 10 Oct 2010) New Revision: 6830 Modified: trunk/scipy/io/tests/test_mmio.py Log: TST: io: Don't use 'import *'. Remove unused imports. Modified: trunk/scipy/io/tests/test_mmio.py =================================================================== --- trunk/scipy/io/tests/test_mmio.py 2010-10-10 04:40:47 UTC (rev 6829) +++ trunk/scipy/io/tests/test_mmio.py 2010-10-10 05:33:43 UTC (rev 6830) @@ -2,12 +2,13 @@ from tempfile import mktemp from numpy import array,transpose -from numpy.testing import * -from numpy.compat import asbytes, asbytes_nested +from numpy.testing import TestCase, run_module_suite, assert_array_almost_equal, \ + assert_equal, rand import scipy.sparse from scipy.io.mmio import mminfo,mmread,mmwrite + class TestMMIOArray(TestCase): def test_simple(self): From scipy-svn at scipy.org Sun Oct 10 01:40:50 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 10 Oct 2010 00:40:50 -0500 (CDT) Subject: [Scipy-svn] r6831 - trunk/scipy/cluster/tests Message-ID: <20101010054050.7894A6E004@scipy.org> Author: warren.weckesser Date: 2010-10-10 00:40:50 -0500 (Sun, 10 Oct 2010) New Revision: 6831 Modified: trunk/scipy/cluster/tests/test_hierarchy.py Log: TST: cluster: Don't use 'import *'. Rename tests with duplicate names. Remove unused imports. Modified: trunk/scipy/cluster/tests/test_hierarchy.py =================================================================== --- trunk/scipy/cluster/tests/test_hierarchy.py 2010-10-10 05:33:43 UTC (rev 6830) +++ trunk/scipy/cluster/tests/test_hierarchy.py 2010-10-10 05:40:50 UTC (rev 6831) @@ -36,9 +36,13 @@ import os.path import numpy as np -from numpy.testing import * +from numpy.testing import TestCase, run_module_suite -from scipy.cluster.hierarchy import linkage, from_mlab_linkage, to_mlab_linkage, num_obs_linkage, inconsistent, cophenet, from_mlab_linkage, fclusterdata, fcluster, is_isomorphic, single, complete, average, weighted, centroid, median, ward, leaders, correspond, is_monotonic, maxdists, maxinconsts, maxRstat, is_valid_linkage, is_valid_im, to_tree, leaves_list +from scipy.cluster.hierarchy import linkage, from_mlab_linkage, to_mlab_linkage,\ + num_obs_linkage, inconsistent, cophenet, fclusterdata, fcluster, \ + is_isomorphic, single, complete, weighted, centroid, leaders, \ + correspond, is_monotonic, maxdists, maxinconsts, maxRstat, \ + is_valid_linkage, is_valid_im, to_tree, leaves_list from scipy.spatial.distance import squareform, pdist _tdist = np.array([[0, 662, 877, 255, 412, 996], @@ -890,12 +894,12 @@ [4, 5, 0.2, 4]], dtype=np.double) self.assertTrue(is_monotonic(Z) == False) - def test_is_monotonic_tdist_linkage(self): + def test_is_monotonic_tdist_linkage1(self): "Tests is_monotonic(Z) on clustering generated by single linkage on tdist data set. Expecting True." Z = linkage(_ytdist, 'single') self.assertTrue(is_monotonic(Z) == True) - def test_is_monotonic_tdist_linkage(self): + def test_is_monotonic_tdist_linkage2(self): "Tests is_monotonic(Z) on clustering generated by single linkage on tdist data set. Perturbing. Expecting False." Z = linkage(_ytdist, 'single') Z[2,2]=0.0 From scipy-svn at scipy.org Sun Oct 10 15:42:10 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 10 Oct 2010 14:42:10 -0500 (CDT) Subject: [Scipy-svn] r6832 - in trunk/scipy/linalg: . tests Message-ID: <20101010194210.6316F6E005@scipy.org> Author: ptvirtan Date: 2010-10-10 14:42:10 -0500 (Sun, 10 Oct 2010) New Revision: 6832 Modified: trunk/scipy/linalg/basic.py trunk/scipy/linalg/tests/test_basic.py Log: BUG: linalg: fix residuals returned from linalg.lstsq for complex matrices Modified: trunk/scipy/linalg/basic.py =================================================================== --- trunk/scipy/linalg/basic.py 2010-10-10 05:40:50 UTC (rev 6831) +++ trunk/scipy/linalg/basic.py 2010-10-10 19:42:10 UTC (rev 6832) @@ -387,7 +387,7 @@ if n < m: x1 = x[:n] if rank == n: - resids = sum(x[n:]**2, axis=0) + resids = sum(abs(x[n:])**2, axis=0) x = x1 return x, resids, rank, s Modified: trunk/scipy/linalg/tests/test_basic.py =================================================================== --- trunk/scipy/linalg/tests/test_basic.py 2010-10-10 05:40:50 UTC (rev 6831) +++ trunk/scipy/linalg/tests/test_basic.py 2010-10-10 19:42:10 UTC (rev 6832) @@ -460,9 +460,16 @@ a = [[1,2],[4,5],[3,4]] b = [1,2,3] x,res,r,s = lstsq(a,b) - #XXX: check defintion of res assert_array_almost_equal(x,direct_lstsq(a,b)) + assert_almost_equal((abs(dot(a,x) - b)**2).sum(axis=0), res) + def test_simple_overdet_complex(self): + a = [[1+2j,2],[4,5],[3,4]] + b = [1,2+4j,3] + x,res,r,s = lstsq(a,b) + assert_array_almost_equal(x,direct_lstsq(a,b,cmplx=1)) + assert_almost_equal(res, (abs(dot(a,x) - b)**2).sum(axis=0)) + def test_simple_underdet(self): a = [[1,2,3],[4,5,6]] b = [1,2] From scipy-svn at scipy.org Tue Oct 12 16:02:13 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 12 Oct 2010 15:02:13 -0500 (CDT) Subject: [Scipy-svn] r6833 - in trunk/scipy/optimize: . tests Message-ID: <20101012200213.2CB446E006@scipy.org> Author: warren.weckesser Date: 2010-10-12 15:02:12 -0500 (Tue, 12 Oct 2010) New Revision: 6833 Modified: trunk/scipy/optimize/optimize.py trunk/scipy/optimize/tests/test_optimize.py Log: BUG: optimize: Fixed formula in rosen_hess (ticket #1248)--thanks to John Gabriel. Also added docstrings to the rosen* functions. Modified: trunk/scipy/optimize/optimize.py =================================================================== --- trunk/scipy/optimize/optimize.py 2010-10-10 19:42:10 UTC (rev 6832) +++ trunk/scipy/optimize/optimize.py 2010-10-12 20:02:12 UTC (rev 6833) @@ -23,7 +23,7 @@ __docformat__ = "restructuredtext en" import numpy -from numpy import atleast_1d, eye, mgrid, argmin, zeros, shape, empty, \ +from numpy import atleast_1d, eye, mgrid, argmin, zeros, shape, \ squeeze, vectorize, asarray, absolute, sqrt, Inf, asfarray, isinf from linesearch import \ line_search_BFGS, line_search_wolfe1, line_search_wolfe2, \ @@ -64,11 +64,47 @@ else: return numpy.sum(abs(x)**ord,axis=0)**(1.0/ord) -def rosen(x): # The Rosenbrock function +def rosen(x): + """The Rosenbrock function. + + The function computed is + + sum(100.0*(x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0 + + Parameters + ---------- + x : array_like, 1D + The point at which the Rosenbrock function is to be computed. + + Returns + ------- + f : float + The value of the Rosenbrock function + + See Also + -------- + rosen_der, rosen_hess, rosen_hess_prod + """ x = asarray(x) return numpy.sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0,axis=0) def rosen_der(x): + """The derivative (i.e. gradient) of the Rosenbrock function. + + Parameters + ---------- + x : array_like, 1D + The point at which the derivative is to be computed. + + Returns + ------- + der : 1D numpy array + The gradient of the Rosenbrock function at `x`. + + See Also + -------- + rosen, rosen_hess, rosen_hess_prod + """ x = asarray(x) xm = x[1:-1] xm_m1 = x[:-2] @@ -80,16 +116,51 @@ return der def rosen_hess(x): + """The Hessian matrix of the Rosenbrock function. + + Parameters + ---------- + x : array_like, 1D + The point at which the Hessian matrix is to be computed. + + Returns + ------- + hess : 2D numpy array + The Hessian matrix of the Rosenbrock function at `x`. + + See Also + -------- + rosen, rosen_der, rosen_hess_prod + """ x = atleast_1d(x) H = numpy.diag(-400*x[:-1],1) - numpy.diag(400*x[:-1],-1) diagonal = numpy.zeros(len(x), dtype=x.dtype) - diagonal[0] = 1200*x[0]-400*x[1]+2 + diagonal[0] = 1200*x[0]**2 - 400*x[1] + 2 diagonal[-1] = 200 diagonal[1:-1] = 202 + 1200*x[1:-1]**2 - 400*x[2:] H = H + numpy.diag(diagonal) return H def rosen_hess_prod(x,p): + """Product of the Hessian matrix of the Rosenbrock function with a vector. + + Parameters + ---------- + x : array_like, 1D + The point at which the Hessian matrix is to be computed. + p : array_like, 1D, same size as `x`. + The vector to be multiplied by the Hessian matrix. + + Returns + ------- + v : 1D numpy array + The Hessian matrix of the Rosenbrock function at `x` multiplied + by the vector `p`. + + See Also + -------- + rosen, rosen_der, rosen_hess + """ x = atleast_1d(x) Hp = numpy.zeros(len(x), dtype=x.dtype) Hp[0] = (1200*x[0]**2 - 400*x[1] + 2)*p[0] - 400*x[0]*p[1] Modified: trunk/scipy/optimize/tests/test_optimize.py =================================================================== --- trunk/scipy/optimize/tests/test_optimize.py 2010-10-10 19:42:10 UTC (rev 6832) +++ trunk/scipy/optimize/tests/test_optimize.py 2010-10-12 20:02:12 UTC (rev 6833) @@ -11,7 +11,7 @@ """ from numpy.testing import assert_raises, assert_almost_equal, \ - assert_, TestCase, run_module_suite + assert_equal, assert_, TestCase, run_module_suite from scipy import optimize from numpy import array, zeros, float64, dot, log, exp, inf, sin, cos @@ -345,5 +345,17 @@ if ef > 1e-8: raise err + +class TestRosen(TestCase): + + def test_hess(self): + """Compare rosen_hess(x) times p with rosen_hess_prod(x,p) (ticket #1248)""" + x = array([3, 4, 5]) + p = array([2, 2, 2]) + hp = optimize.rosen_hess_prod(x, p) + dothp = np.dot(optimize.rosen_hess(x), p) + assert_equal(hp, dothp) + + if __name__ == "__main__": run_module_suite() From scipy-svn at scipy.org Tue Oct 12 22:29:33 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 12 Oct 2010 21:29:33 -0500 (CDT) Subject: [Scipy-svn] r6834 - in trunk/scipy/stats: . tests Message-ID: <20101013022933.62B7542E053@scipy.org> Author: warren.weckesser Date: 2010-10-12 21:29:33 -0500 (Tue, 12 Oct 2010) New Revision: 6834 Modified: trunk/scipy/stats/distributions.py trunk/scipy/stats/tests/test_distributions.py Log: BUG: stats: In a couple of the frozen distribution methods, giving a keyword argument modified the saved kwds dict. Also the frozen moment() method called the distribution's moment() method with keywords, but it doesn't take any. (Ticket #1293) Modified: trunk/scipy/stats/distributions.py =================================================================== --- trunk/scipy/stats/distributions.py 2010-10-12 20:02:12 UTC (rev 6833) +++ trunk/scipy/stats/distributions.py 2010-10-13 02:29:33 UTC (rev 6834) @@ -324,43 +324,59 @@ # Frozen RV class class rv_frozen(object): + def __init__(self, dist, *args, **kwds): self.args = args self.kwds = kwds self.dist = dist - def pdf(self,x): #raises AttributeError in frozen discrete distribution - return self.dist.pdf(x,*self.args,**self.kwds) - def cdf(self,x): - return self.dist.cdf(x,*self.args,**self.kwds) - def ppf(self,q): - return self.dist.ppf(q,*self.args,**self.kwds) - def isf(self,q): - return self.dist.isf(q,*self.args,**self.kwds) + + def pdf(self, x): #raises AttributeError in frozen discrete distribution + return self.dist.pdf(x, *self.args, **self.kwds) + + def cdf(self, x): + return self.dist.cdf(x, *self.args, **self.kwds) + + def ppf(self, q): + return self.dist.ppf(q, *self.args, **self.kwds) + + def isf(self, q): + return self.dist.isf(q, *self.args, **self.kwds) + def rvs(self, size=None): - kwds = self.kwds + kwds = self.kwds.copy() kwds.update({'size':size}) - return self.dist.rvs(*self.args,**kwds) - def sf(self,x): - return self.dist.sf(x,*self.args,**self.kwds) - def stats(self,moments='mv'): - kwds = self.kwds + return self.dist.rvs(*self.args, **kwds) + + def sf(self, x): + return self.dist.sf(x, *self.args, **self.kwds) + + def stats(self, moments='mv'): + kwds = self.kwds.copy() kwds.update({'moments':moments}) - return self.dist.stats(*self.args,**kwds) + return self.dist.stats(*self.args, **kwds) + def median(self): return self.dist.median(*self.args, **self.kwds) + def mean(self): - return self.dist.mean(*self.args,**self.kwds) + return self.dist.mean(*self.args, **self.kwds) + def var(self): return self.dist.var(*self.args, **self.kwds) + def std(self): return self.dist.std(*self.args, **self.kwds) - def moment(self,n): - return self.dist.moment(n,*self.args,**self.kwds) + + def moment(self, n): + return self.dist.moment(n, *self.args) + def entropy(self): - return self.dist.entropy(*self.args,**self.kwds) + return self.dist.entropy(*self.args, **self.kwds) + def pmf(self,k): - return self.dist.pmf(k,*self.args,**self.kwds) - def interval(self,alpha): + return self.dist.pmf(k, *self.args, **self.kwds) + + def interval(self, alpha): return self.dist.interval(alpha, *self.args, **self.kwds) Modified: trunk/scipy/stats/tests/test_distributions.py =================================================================== --- trunk/scipy/stats/tests/test_distributions.py 2010-10-12 20:02:12 UTC (rev 6833) +++ trunk/scipy/stats/tests/test_distributions.py 2010-10-13 02:29:33 UTC (rev 6834) @@ -436,5 +436,125 @@ assert_(len(vals5) == 2+len(args)) assert_(vals5[2] == args[2]) +class TestFrozen(TestCase): + """Test that a frozen distribution gives the same results as the original object. + + Only tested for the normal distribution (with loc and scale specified) and for the + gamma distribution (with a shape parameter specified). + """ + def test_norm(self): + dist = stats.norm + frozen = stats.norm(loc=10.0, scale=3.0) + + result_f = frozen.pdf(20.0) + result = dist.pdf(20.0, loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.cdf(20.0) + result = dist.cdf(20.0, loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.ppf(0.25) + result = dist.ppf(0.25, loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.isf(0.25) + result = dist.isf(0.25, loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.sf(10.0) + result = dist.sf(10.0, loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.median() + result = dist.median(loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.mean() + result = dist.mean(loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.var() + result = dist.var(loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.std() + result = dist.std(loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.entropy() + result = dist.entropy(loc=10.0, scale=3.0) + assert_equal(result_f, result) + + result_f = frozen.moment(2) + result = dist.moment(2) + assert_equal(result_f, result) + + def test_gamma(self): + a = 2.0 + dist = stats.gamma + frozen = stats.gamma(a) + + result_f = frozen.pdf(20.0) + result = dist.pdf(20.0, a) + assert_equal(result_f, result) + + result_f = frozen.cdf(20.0) + result = dist.cdf(20.0, a) + assert_equal(result_f, result) + + result_f = frozen.ppf(0.25) + result = dist.ppf(0.25, a) + assert_equal(result_f, result) + + result_f = frozen.isf(0.25) + result = dist.isf(0.25, a) + assert_equal(result_f, result) + + result_f = frozen.sf(10.0) + result = dist.sf(10.0, a) + assert_equal(result_f, result) + + result_f = frozen.median() + result = dist.median(a) + assert_equal(result_f, result) + + result_f = frozen.mean() + result = dist.mean(a) + assert_equal(result_f, result) + + result_f = frozen.var() + result = dist.var(a) + assert_equal(result_f, result) + + result_f = frozen.std() + result = dist.std(a) + assert_equal(result_f, result) + + result_f = frozen.entropy() + result = dist.entropy(a) + assert_equal(result_f, result) + + result_f = frozen.moment(2) + result = dist.moment(2, a) + assert_equal(result_f, result) + + def test_regression_02(self): + """Regression test for ticket #1293.""" + # Create a frozen distribution. + frozen = stats.lognorm(1) + # Call one of its methods that does not take any keyword arguments. + m1 = frozen.moment(2) + # Now call a method that takes a keyword argument. + s = frozen.stats(moments='mvsk') + # Call moment(2) again. + # After calling stats(), the following was raising an exception. + # So this test passes if the following does not raise an exception. + m2 = frozen.moment(2) + # The following should also be true, of course. But it is not + # the focus of this test. + assert_equal(m1, m2) + + if __name__ == "__main__": run_module_suite() From scipy-svn at scipy.org Wed Oct 13 17:44:46 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 13 Oct 2010 16:44:46 -0500 (CDT) Subject: [Scipy-svn] r6835 - in trunk/scipy: linalg sparse/linalg/eigen/lobpcg sparse/linalg/eigen/lobpcg/tests Message-ID: <20101013214446.4E5146E012@scipy.org> Author: ptvirtan Date: 2010-10-13 16:44:46 -0500 (Wed, 13 Oct 2010) New Revision: 6835 Modified: trunk/scipy/linalg/basic.py trunk/scipy/sparse/linalg/eigen/lobpcg/lobpcg.py trunk/scipy/sparse/linalg/eigen/lobpcg/tests/test_lobpcg.py Log: BUG: scipy.sparse.linalg.eigen.lobpcg: remember that linalg.inv(overwrite_a=True) does not guarantee the output is overwritten (#1304) Modified: trunk/scipy/linalg/basic.py =================================================================== --- trunk/scipy/linalg/basic.py 2010-10-13 02:29:33 UTC (rev 6834) +++ trunk/scipy/linalg/basic.py 2010-10-13 21:44:46 UTC (rev 6835) @@ -206,6 +206,8 @@ ---------- a : array-like, shape (M, M) Matrix to be inverted + overwrite_a : bool, optional + Discard data in ``a`` (may improve performance) Returns ------- Modified: trunk/scipy/sparse/linalg/eigen/lobpcg/lobpcg.py =================================================================== --- trunk/scipy/sparse/linalg/eigen/lobpcg/lobpcg.py 2010-10-13 02:29:33 UTC (rev 6834) +++ trunk/scipy/sparse/linalg/eigen/lobpcg/lobpcg.py 2010-10-13 21:44:46 UTC (rev 6835) @@ -137,7 +137,7 @@ blockVectorBV = blockVectorV # Shared data!!! gramVBV = sp.dot( blockVectorV.T, blockVectorBV ) gramVBV = sla.cholesky( gramVBV ) - sla.inv( gramVBV, overwrite_a = True ) + gramVBV = sla.inv( gramVBV, overwrite_a = True ) # gramVBV is now R^{-1}. blockVectorV = sp.dot( blockVectorV, gramVBV ) if B is not None: Modified: trunk/scipy/sparse/linalg/eigen/lobpcg/tests/test_lobpcg.py =================================================================== --- trunk/scipy/sparse/linalg/eigen/lobpcg/tests/test_lobpcg.py 2010-10-13 02:29:33 UTC (rev 6834) +++ trunk/scipy/sparse/linalg/eigen/lobpcg/tests/test_lobpcg.py 2010-10-13 21:44:46 UTC (rev 6835) @@ -5,7 +5,7 @@ import numpy from numpy.testing import assert_almost_equal, run_module_suite -from scipy import arange, ones, rand, set_printoptions, r_, diag, linalg +from scipy import arange, ones, rand, set_printoptions, r_, diag, linalg, eye from scipy.linalg import eig from scipy.sparse.linalg.eigen.lobpcg import lobpcg @@ -77,6 +77,11 @@ A,B = MikotaPair(100) compare_solutions(A,B,20) +def test_trivial(): + n = 5 + X = ones((n, 1)) + A = eye(n) + compare_solutions(A, None, n) if __name__ == "__main__": run_module_suite() From scipy-svn at scipy.org Thu Oct 14 11:36:54 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 14 Oct 2010 10:36:54 -0500 (CDT) Subject: [Scipy-svn] r6836 - trunk/scipy/ndimage Message-ID: <20101014153654.406486E00E@scipy.org> Author: stefan Date: 2010-10-14 10:36:54 -0500 (Thu, 14 Oct 2010) New Revision: 6836 Modified: trunk/scipy/ndimage/measurements.py Log: ENH: ndimage.measurements: refactor _stats. Modified: trunk/scipy/ndimage/measurements.py =================================================================== --- trunk/scipy/ndimage/measurements.py 2010-10-13 21:44:46 UTC (rev 6835) +++ trunk/scipy/ndimage/measurements.py 2010-10-14 15:36:54 UTC (rev 6836) @@ -274,12 +274,13 @@ return output -def _stats(input, labels = None, index = None, do_sum2=False): - '''returns count, sum, and optionally sum^2 by label''' +def _stats(input, labels=None, index=None, centered=False): + '''returns count, sum, and optionally (sum - centre)^2 by label''' def single_group(vals): - if do_sum2: - return vals.size, vals.sum(), (vals * vals.conjugate()).sum() + if centered: + vals_c = vals# - vals.mean() + return vals.size, vals.sum(), (vals_c * vals_c.conjugate()).sum() else: return vals.size, vals.sum() @@ -295,16 +296,25 @@ if numpy.isscalar(index): return single_group(input[labels == index]) + counts = numpy.bincount(labels.ravel()) + sums = numpy.bincount(labels.ravel(), weights=input.ravel()) + + def _sum_centered(labels): + means = sums / counts + centered_input = input# - means[labels] + return numpy.bincount(labels, + weights=(centered_input * \ + centered_input.conjugate()).ravel()) + # remap labels to unique integers if necessary, or if the largest # label is larger than the number of values. + if ((not numpy.issubdtype(labels.dtype, numpy.int)) or (labels.min() < 0) or (labels.max() > labels.size)): unique_labels, new_labels = numpy.unique1d(labels, return_inverse=True) - counts = numpy.bincount(new_labels) - sums = numpy.bincount(new_labels, weights=input.ravel()) - if do_sum2: - sums2 = numpy.bincount(new_labels, weights=(input * input.conjugate()).ravel()) + if centered: + sums_c, sums, counts = _sum_centered(new_labels) idxs = numpy.searchsorted(unique_labels, index) # make all of idxs valid @@ -313,25 +323,25 @@ else: # labels are an integer type, and there aren't too many, so # call bincount directly. - counts = numpy.bincount(labels.ravel()) - sums = numpy.bincount(labels.ravel(), weights=input.ravel()) - if do_sum2: - sums2 = numpy.bincount(labels.ravel(), weights=(input * input.conjugate()).ravel()) + if centered: + sums_c = _sum_centered(labels.ravel()) # make sure all index values are valid idxs = numpy.asanyarray(index, numpy.int).copy() found = (idxs >= 0) & (idxs < counts.size) - idxs[~ found] = 0 + idxs[~found] = 0 counts = counts[idxs] - counts[~ found] = 0 + counts[~found] = 0 sums = sums[idxs] - sums[~ found] = 0 - if not do_sum2: + sums[~found] = 0 + + if not centered: return (counts, sums) - sums2 = sums2[idxs] - sums2[~ found] = 0 - return (counts, sums, sums2) + else: + sums_c = sums_c[idxs] + sums_c[~found] = 0 + return (counts, sums, sums_c) def sum(input, labels = None, index = None): @@ -398,9 +408,9 @@ none, all values where label is greater than zero are used. """ - count, sum, sum2 = _stats(input, labels, index, do_sum2=True) + count, sum, sum_c = _stats(input, labels, index, centered=True) mean = sum / numpy.asanyarray(count).astype(numpy.float) - mean2 = sum2 / numpy.asanyarray(count).astype(numpy.float) + mean2 = sum_c / numpy.asanyarray(count).astype(numpy.float) return mean2 - (mean * mean.conjugate()) From scipy-svn at scipy.org Thu Oct 14 11:37:27 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 14 Oct 2010 10:37:27 -0500 (CDT) Subject: [Scipy-svn] r6837 - trunk/scipy/ndimage/tests Message-ID: <20101014153727.EC2216E00E@scipy.org> Author: stefan Date: 2010-10-14 10:37:27 -0500 (Thu, 14 Oct 2010) New Revision: 6837 Modified: trunk/scipy/ndimage/tests/test_ndimage.py Log: STY: Clean up ndimage tests. Modified: trunk/scipy/ndimage/tests/test_ndimage.py =================================================================== --- trunk/scipy/ndimage/tests/test_ndimage.py 2010-10-14 15:36:54 UTC (rev 6836) +++ trunk/scipy/ndimage/tests/test_ndimage.py 2010-10-14 15:37:27 UTC (rev 6837) @@ -33,11 +33,16 @@ import numpy as np from numpy import fft from numpy.testing import assert_, assert_equal, assert_array_equal, \ - TestCase, run_module_suite + TestCase, run_module_suite, \ + assert_array_almost_equal, assert_almost_equal import scipy.ndimage as ndimage eps = 1e-12 +### +### TODO: Remove this helper function and replace by functionality +### inside numpy.testing. +### def diff(a, b): if not isinstance(a, numpy.ndarray): a = numpy.asarray(a) @@ -61,7 +66,7 @@ return math.sqrt(t) -class TestNdimage(TestCase): +class TestNdimage: def setUp(self): # list of numarray data types @@ -77,42 +82,54 @@ "correlation 1" array = numpy.array([1, 2]) weights = numpy.array([2]) - true = [2, 4] + expected = [2, 4] + output = ndimage.correlate(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) + output = ndimage.convolve(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) + output = ndimage.correlate1d(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) + output = ndimage.convolve1d(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) def test_correlate02(self): "correlation 2" array = numpy.array([1, 2, 3]) kernel = numpy.array([1]) + output = ndimage.correlate(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) + output = ndimage.convolve(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) + output = ndimage.correlate1d(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) + output = ndimage.convolve1d(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) def test_correlate03(self): "correlation 3" array = numpy.array([1]) weights = numpy.array([1, 1]) - true = [2] + expected = [2] + output = ndimage.correlate(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) + output = ndimage.convolve(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) + output = ndimage.correlate1d(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) + output = ndimage.convolve1d(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) def test_correlate04(self): "correlation 4" @@ -121,13 +138,13 @@ tcov = [3, 4] weights = numpy.array([1, 1]) output = ndimage.correlate(array, weights) - self.assertTrue(diff(output, tcor) < eps) + assert_array_almost_equal(output, tcor) output = ndimage.convolve(array, weights) - self.assertTrue(diff(output, tcov) < eps) + assert_array_almost_equal(output, tcov) output = ndimage.correlate1d(array, weights) - self.assertTrue(diff(output, tcor) < eps) + assert_array_almost_equal(output, tcor) output = ndimage.convolve1d(array, weights) - self.assertTrue(diff(output, tcov) < eps) + assert_array_almost_equal(output, tcov) def test_correlate05(self): "correlation 5" @@ -136,13 +153,13 @@ tcov = [3, 5, 6] kernel = numpy.array([1, 1]) output = ndimage.correlate(array, kernel) - self.assertTrue(diff(tcor, output) < eps) + assert_array_almost_equal(tcor, output) output = ndimage.convolve(array, kernel) - self.assertTrue(diff(tcov, output) < eps) + assert_array_almost_equal(tcov, output) output = ndimage.correlate1d(array, kernel) - self.assertTrue(diff(tcor, output) < eps) + assert_array_almost_equal(tcor, output) output = ndimage.convolve1d(array, kernel) - self.assertTrue(diff(tcov, output) < eps) + assert_array_almost_equal(tcov, output) def test_correlate06(self): "correlation 6" @@ -151,27 +168,27 @@ tcov = [7, 10, 15] weights = numpy.array([1, 2, 3]) output = ndimage.correlate(array, weights) - self.assertTrue(diff(output, tcor) < eps) + assert_array_almost_equal(output, tcor) output = ndimage.convolve(array, weights) - self.assertTrue(diff(output, tcov) < eps) + assert_array_almost_equal(output, tcov) output = ndimage.correlate1d(array, weights) - self.assertTrue(diff(output, tcor) < eps) + assert_array_almost_equal(output, tcor) output = ndimage.convolve1d(array, weights) - self.assertTrue(diff(output, tcov) < eps) + assert_array_almost_equal(output, tcov) def test_correlate07(self): "correlation 7" array = numpy.array([1, 2, 3]) - true = [5, 8, 11] + expected = [5, 8, 11] weights = numpy.array([1, 2, 1]) output = ndimage.correlate(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) output = ndimage.convolve(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) output = ndimage.correlate1d(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) output = ndimage.convolve1d(array, weights) - self.assertTrue(diff(output, true) < eps) + assert_array_almost_equal(output, expected) def test_correlate08(self): "correlation 8" @@ -180,35 +197,35 @@ tcov = [3, 6, 7] weights = numpy.array([1, 2, -1]) output = ndimage.correlate(array, weights) - self.assertTrue(diff(output, tcor) < eps) + assert_array_almost_equal(output, tcor) output = ndimage.convolve(array, weights) - self.assertTrue(diff(output, tcov) < eps) + assert_array_almost_equal(output, tcov) output = ndimage.correlate1d(array, weights) - self.assertTrue(diff(output, tcor) < eps) + assert_array_almost_equal(output, tcor) output = ndimage.convolve1d(array, weights) - self.assertTrue(diff(output, tcov) < eps) + assert_array_almost_equal(output, tcov) def test_correlate09(self): "correlation 9" array = [] kernel = numpy.array([1, 1]) output = ndimage.correlate(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) output = ndimage.convolve(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) output = ndimage.correlate1d(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) output = ndimage.convolve1d(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) def test_correlate10(self): "correlation 10" array = [[]] kernel = numpy.array([[1, 1]]) output = ndimage.correlate(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) output = ndimage.convolve(array, kernel) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) def test_correlate11(self): "correlation 11" @@ -217,9 +234,9 @@ kernel = numpy.array([[1, 1], [1, 1]]) output = ndimage.correlate(array, kernel) - self.assertTrue(diff([[4, 6, 10], [10, 12, 16]], output) < eps) + assert_array_almost_equal([[4, 6, 10], [10, 12, 16]], output) output = ndimage.convolve(array, kernel) - self.assertTrue(diff([[12, 16, 18], [18, 22, 24]], output) < eps) + assert_array_almost_equal([[12, 16, 18], [18, 22, 24]], output) def test_correlate12(self): "correlation 12" @@ -228,61 +245,62 @@ kernel = numpy.array([[1, 0], [0, 1]]) output = ndimage.correlate(array, kernel) - self.assertTrue(diff([[2, 3, 5], [5, 6, 8]], output) < eps) + assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output) output = ndimage.convolve(array, kernel) - self.assertTrue(diff([[6, 8, 9], [9, 11, 12]], output) < eps) + assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output) def test_correlate13(self): "correlation 13" kernel = numpy.array([[1, 0], - [0, 1]]) + [0, 1]]) for type1 in self.types: array = numpy.array([[1, 2, 3], - [4, 5, 6]], type1) + [4, 5, 6]], type1) for type2 in self.types: output = ndimage.correlate(array, kernel, - output = type2) - error = diff([[2, 3, 5], [5, 6, 8]], output) - self.assertTrue(error < eps and output.dtype.type == type2) + output=type2) + assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output) + assert_equal(output.dtype.type, type2) + output = ndimage.convolve(array, kernel, - output = type2) - error = diff([[6, 8, 9], [9, 11, 12]], output) - self.assertTrue(error < eps and output.dtype.type == type2) + output=type2) + assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output) + assert_equal(output.dtype.type, type2) def test_correlate14(self): "correlation 14" kernel = numpy.array([[1, 0], - [0, 1]]) + [0, 1]]) for type1 in self.types: array = numpy.array([[1, 2, 3], - [4, 5, 6]], type1) + [4, 5, 6]], type1) for type2 in self.types: output = numpy.zeros(array.shape, type2) ndimage.correlate(array, kernel, - output = output) - error = diff([[2, 3, 5], [5, 6, 8]], output) - self.assertTrue(error < eps and output.dtype.type == type2) - ndimage.convolve(array, kernel, output = output) - error = diff([[6, 8, 9], [9, 11, 12]], output) - self.assertTrue(error < eps and output.dtype.type == type2) + output=output) + assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output) + assert_equal(output.dtype.type, type2) + ndimage.convolve(array, kernel, output=output) + assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output) + assert_equal(output.dtype.type, type2) + def test_correlate15(self): "correlation 15" kernel = numpy.array([[1, 0], - [0, 1]]) + [0, 1]]) for type1 in self.types: array = numpy.array([[1, 2, 3], - [4, 5, 6]], type1) + [4, 5, 6]], type1) output = ndimage.correlate(array, kernel, - output = numpy.float32) - error = diff([[2, 3, 5], [5, 6, 8]], output) - self.assertTrue(error < eps and - output.dtype.type == numpy.float32) + output=numpy.float32) + assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output) + assert_equal(output.dtype.type, numpy.float32) + output = ndimage.convolve(array, kernel, - output = numpy.float32) - error = diff([[6, 8, 9], [9, 11, 12]], output) - self.assertTrue(error < eps and - output.dtype.type == numpy.float32) + output=numpy.float32) + assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output) + assert_equal(output.dtype.type, numpy.float32) def test_correlate16(self): "correlation 16" @@ -292,15 +310,14 @@ array = numpy.array([[1, 2, 3], [4, 5, 6]], type1) output = ndimage.correlate(array, kernel, - output = numpy.float32) - error = diff([[1, 1.5, 2.5], [2.5, 3, 4]], output) - self.assertTrue(error < eps and - output.dtype.type == numpy.float32) + output=numpy.float32) + assert_array_almost_equal([[1, 1.5, 2.5], [2.5, 3, 4]], output) + assert_equal(output.dtype.type, numpy.float32) + output = ndimage.convolve(array, kernel, - output = numpy.float32) - error = diff([[3, 4, 4.5], [4.5, 5.5, 6]], output) - self.assertTrue(error < eps and - output.dtype.type == numpy.float32) + output=numpy.float32) + assert_array_almost_equal([[3, 4, 4.5], [4.5, 5.5, 6]], output) + assert_equal(output.dtype.type, numpy.float32) def test_correlate17(self): "correlation 17" @@ -308,14 +325,14 @@ tcor = [3, 5, 6] tcov = [2, 3, 5] kernel = numpy.array([1, 1]) - output = ndimage.correlate(array, kernel, origin = -1) - self.assertTrue(diff(tcor, output) < eps) - output = ndimage.convolve(array, kernel, origin = -1) - self.assertTrue(diff(tcov, output) < eps) - output = ndimage.correlate1d(array, kernel, origin = -1) - self.assertTrue(diff(tcor, output) < eps) - output = ndimage.convolve1d(array, kernel, origin = -1) - self.assertTrue(diff(tcov, output) < eps) + output = ndimage.correlate(array, kernel, origin=-1) + assert_array_almost_equal(tcor, output) + output = ndimage.convolve(array, kernel, origin=-1) + assert_array_almost_equal(tcov, output) + output = ndimage.correlate1d(array, kernel, origin=-1) + assert_array_almost_equal(tcor, output) + output = ndimage.convolve1d(array, kernel, origin=-1) + assert_array_almost_equal(tcov, output) def test_correlate18(self): "correlation 18" @@ -325,95 +342,94 @@ array = numpy.array([[1, 2, 3], [4, 5, 6]], type1) output = ndimage.correlate(array, kernel, - output = numpy.float32, - mode = 'nearest', origin = -1) - error = diff([[6, 8, 9], [9, 11, 12]], output) - self.assertTrue(error < eps and - output.dtype.type == numpy.float32) + output=numpy.float32, + mode='nearest', origin=-1) + assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output) + assert_equal(output.dtype.type, numpy.float32) + output = ndimage.convolve(array, kernel, - output = numpy.float32, mode = 'nearest', origin = -1) - error = diff([[2, 3, 5], [5, 6, 8]], output) - self.assertTrue(error < eps and - output.dtype.type == numpy.float32) + output=numpy.float32, + mode='nearest', origin=-1) + assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output) + assert_equal(output.dtype.type, numpy.float32) def test_correlate19(self): "correlation 19" kernel = numpy.array([[1, 0], - [0, 1]]) + [0, 1]]) for type1 in self.types: array = numpy.array([[1, 2, 3], - [4, 5, 6]], type1) + [4, 5, 6]], type1) output = ndimage.correlate(array, kernel, - output = numpy.float32, - mode = 'nearest', origin = [-1, 0]) - error = diff([[5, 6, 8], [8, 9, 11]], output) - self.assertTrue(error < eps and - output.dtype.type == numpy.float32) + output=numpy.float32, + mode='nearest', origin=[-1, 0]) + assert_array_almost_equal([[5, 6, 8], [8, 9, 11]], output) + assert_equal(output.dtype.type, numpy.float32) + output = ndimage.convolve(array, kernel, - output = numpy.float32, - mode = 'nearest', origin = [-1, 0]) - error = diff([[3, 5, 6], [6, 8, 9]], output) - self.assertTrue(error < eps and - output.dtype.type == numpy.float32) + output=numpy.float32, + mode='nearest', origin=[-1, 0]) + assert_array_almost_equal([[3, 5, 6], [6, 8, 9]], output) + assert_equal(output.dtype.type, numpy.float32) def test_correlate20(self): "correlation 20" weights = numpy.array([1, 2, 1]) - true = [[5, 10, 15], [7, 14, 21]] + expected = [[5, 10, 15], [7, 14, 21]] for type1 in self.types: array = numpy.array([[1, 2, 3], [2, 4, 6]], type1) for type2 in self.types: output = numpy.zeros((2, 3), type2) - ndimage.correlate1d(array, weights, axis = 0, - output = output) - self.assertTrue(diff(output, true) < eps) - ndimage.convolve1d(array, weights, axis = 0, - output = output) - self.assertTrue(diff(output, true) < eps) + ndimage.correlate1d(array, weights, axis=0, + output=output) + assert_array_almost_equal(output, expected) + ndimage.convolve1d(array, weights, axis=0, + output=output) + assert_array_almost_equal(output, expected) def test_correlate21(self): "correlation 21" array = numpy.array([[1, 2, 3], [2, 4, 6]]) - true = [[5, 10, 15], [7, 14, 21]] + expected = [[5, 10, 15], [7, 14, 21]] weights = numpy.array([1, 2, 1]) - output = ndimage.correlate1d(array, weights, axis = 0) - self.assertTrue(diff(output, true) < eps) - output = ndimage.convolve1d(array, weights, axis = 0) - self.assertTrue(diff(output, true) < eps) + output = ndimage.correlate1d(array, weights, axis=0) + assert_array_almost_equal(output, expected) + output = ndimage.convolve1d(array, weights, axis=0) + assert_array_almost_equal(output, expected) def test_correlate22(self): "correlation 22" weights = numpy.array([1, 2, 1]) - true = [[6, 12, 18], [6, 12, 18]] + expected = [[6, 12, 18], [6, 12, 18]] for type1 in self.types: array = numpy.array([[1, 2, 3], [2, 4, 6]], type1) for type2 in self.types: output = numpy.zeros((2, 3), type2) - ndimage.correlate1d(array, weights, axis = 0, - mode = 'wrap', output = output) - self.assertTrue(diff(output, true) < eps) - ndimage.convolve1d(array, weights, axis = 0, - mode = 'wrap', output = output) - self.assertTrue(diff(output, true) < eps) + ndimage.correlate1d(array, weights, axis=0, + mode='wrap', output=output) + assert_array_almost_equal(output, expected) + ndimage.convolve1d(array, weights, axis=0, + mode='wrap', output=output) + assert_array_almost_equal(output, expected) def test_correlate23(self): "correlation 23" weights = numpy.array([1, 2, 1]) - true = [[5, 10, 15], [7, 14, 21]] + expected = [[5, 10, 15], [7, 14, 21]] for type1 in self.types: array = numpy.array([[1, 2, 3], [2, 4, 6]], type1) for type2 in self.types: output = numpy.zeros((2, 3), type2) - ndimage.correlate1d(array, weights, axis = 0, - mode = 'nearest', output = output) - self.assertTrue(diff(output, true) < eps) - ndimage.convolve1d(array, weights, axis = 0, - mode = 'nearest', output = output) - self.assertTrue(diff(output, true) < eps) + ndimage.correlate1d(array, weights, axis=0, + mode='nearest', output=output) + assert_array_almost_equal(output, expected) + ndimage.convolve1d(array, weights, axis=0, + mode='nearest', output=output) + assert_array_almost_equal(output, expected) def test_correlate24(self): "correlation 24" @@ -425,12 +441,12 @@ [2, 4, 6]], type1) for type2 in self.types: output = numpy.zeros((2, 3), type2) - ndimage.correlate1d(array, weights, axis = 0, - mode = 'nearest', output = output, origin = -1) - self.assertTrue(diff(output, tcor) < eps) - ndimage.convolve1d(array, weights, axis = 0, - mode = 'nearest', output = output, origin = -1) - self.assertTrue(diff(output, tcov) < eps) + ndimage.correlate1d(array, weights, axis=0, + mode='nearest', output=output, origin=-1) + assert_array_almost_equal(output, tcor) + ndimage.convolve1d(array, weights, axis=0, + mode='nearest', output=output, origin=-1) + assert_array_almost_equal(output, tcov) def test_correlate25(self): "correlation 25" @@ -439,30 +455,30 @@ tcov = [[7, 14, 21], [8, 16, 24]] for type1 in self.types: array = numpy.array([[1, 2, 3], - [2, 4, 6]], type1) + [2, 4, 6]], type1) for type2 in self.types: output = numpy.zeros((2, 3), type2) - ndimage.correlate1d(array, weights, axis = 0, - mode = 'nearest', output = output, origin = 1) - self.assertTrue(diff(output, tcor) < eps) - ndimage.convolve1d(array, weights, axis = 0, - mode = 'nearest', output = output, origin = 1) - self.assertTrue(diff(output, tcov) < eps) + ndimage.correlate1d(array, weights, axis=0, + mode='nearest', output=output, origin=1) + assert_array_almost_equal(output, tcor) + ndimage.convolve1d(array, weights, axis=0, + mode='nearest', output=output, origin=1) + assert_array_almost_equal(output, tcov) def test_gauss01(self): "gaussian filter 1" input = numpy.array([[1, 2, 3], - [2, 4, 6]], numpy.float32) + [2, 4, 6]], numpy.float32) output = ndimage.gaussian_filter(input, 0) - self.assertTrue(diff(output, input) < eps) + assert_array_almost_equal(output, input) def test_gauss02(self): "gaussian filter 2" input = numpy.array([[1, 2, 3], - [2, 4, 6]], numpy.float32) + [2, 4, 6]], numpy.float32) output = ndimage.gaussian_filter(input, 1.0) - self.assertTrue(input.dtype == output.dtype and - input.shape == output.shape) + assert_equal(input.dtype, output.dtype) + assert_equal(input.shape, output.shape) def test_gauss03(self): "gaussian filter 3" @@ -470,21 +486,22 @@ input.shape = (100, 100) output = ndimage.gaussian_filter(input, [1.0, 1.0]) - self.assertTrue(input.dtype == output.dtype and - input.shape == output.shape and - output.sum(dtype='d') - input.sum(dtype='d') < eps and - diff(input, output) > 1.0) + assert_equal(input.dtype, output.dtype) + assert_equal(input.shape, output.shape) + assert_almost_equal(output.sum(), input.sum()) + assert_(diff(input, output) > 1.0) + def test_gauss04(self): "gaussian filter 4" input = numpy.arange(100 * 100).astype(numpy.float32) input.shape = (100, 100) otype = numpy.float64 output = ndimage.gaussian_filter(input, [1.0, 1.0], - output = otype) - self.assertTrue(output.dtype.type == numpy.float64 and - input.shape == output.shape and - diff(input, output) > 1.0) + output=otype) + assert_equal(output.dtype.type, numpy.float64) + assert_equal(input.shape, output.shape) + assert_(diff(input, output) > 1.0) def test_gauss05(self): "gaussian filter 5" @@ -492,10 +509,10 @@ input.shape = (100, 100) otype = numpy.float64 output = ndimage.gaussian_filter(input, [1.0, 1.0], - order = 1, output = otype) - self.assertTrue(output.dtype.type == numpy.float64 and - input.shape == output.shape and - diff(input, output) > 1.0) + order=1, output=otype) + assert_equal(output.dtype.type, numpy.float64) + assert_equal(input.shape, output.shape) + assert_(diff(input, output) > 1.0) def test_gauss06(self): "gaussian filter 6" @@ -503,10 +520,10 @@ input.shape = (100, 100) otype = numpy.float64 output1 = ndimage.gaussian_filter(input, [1.0, 1.0], - output = otype) + output=otype) output2 = ndimage.gaussian_filter(input, 1.0, - output = otype) - self.assertTrue(diff(output1, output2) < eps) + output=otype) + assert_array_almost_equal(output1, output2) def test_prewitt01(self): "prewitt filter 1" @@ -517,7 +534,7 @@ t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0) t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 1) output = ndimage.prewitt(array, 0) - self.assertTrue(diff(t, output) < eps) + assert_array_almost_equal(t, output) def test_prewitt02(self): @@ -530,7 +547,7 @@ t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 1) output = numpy.zeros(array.shape, type) ndimage.prewitt(array, 0, output) - self.assertTrue(diff(t, output) < eps) + assert_array_almost_equal(t, output) def test_prewitt03(self): "prewitt filter 3" @@ -541,7 +558,7 @@ t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 1) t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 0) output = ndimage.prewitt(array, 1) - self.assertTrue(diff(t, output) < eps) + assert_array_almost_equal(t, output) def test_prewitt04(self): "prewitt filter 4" @@ -551,7 +568,7 @@ [5, 6, 9, 3, 5]], type) t = ndimage.prewitt(array, -1) output = ndimage.prewitt(array, 1) - self.assertTrue(diff(t, output) < eps) + assert_array_almost_equal(t, output) def test_sobel01(self): "sobel filter 1" @@ -562,7 +579,7 @@ t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0) t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1) output = ndimage.sobel(array, 0) - self.assertTrue(diff(t, output) < eps) + assert_array_almost_equal(t, output) def test_sobel02(self): "sobel filter 2" @@ -574,7 +591,7 @@ t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1) output = numpy.zeros(array.shape, type) ndimage.sobel(array, 0, output) - self.assertTrue(diff(t, output) < eps) + assert_array_almost_equal(t, output) def test_sobel03(self): "sobel filter 3" @@ -586,7 +603,7 @@ t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 0) output = numpy.zeros(array.shape, type) output = ndimage.sobel(array, 1) - self.assertTrue(diff(t, output) < eps) + assert_array_almost_equal(t, output) def test_sobel04(self): "sobel filter 4" @@ -596,7 +613,7 @@ [5, 6, 9, 3, 5]], type) t = ndimage.sobel(array, -1) output = ndimage.sobel(array, 1) - self.assertTrue(diff(t, output) < eps) + assert_array_almost_equal(t, output) def test_laplace01(self): "laplace filter 1" @@ -607,7 +624,7 @@ tmp1 = ndimage.correlate1d(array, [1, -2, 1], 0) tmp2 = ndimage.correlate1d(array, [1, -2, 1], 1) output = ndimage.laplace(array) - self.assertTrue(diff(tmp1 + tmp2, output) < eps) + assert_array_almost_equal(tmp1 + tmp2, output) def test_laplace02(self): "laplace filter 2" @@ -618,8 +635,8 @@ tmp1 = ndimage.correlate1d(array, [1, -2, 1], 0) tmp2 = ndimage.correlate1d(array, [1, -2, 1], 1) output = numpy.zeros(array.shape, type) - ndimage.laplace(array, output = output) - self.assertTrue(diff(tmp1 + tmp2, output) < eps) + ndimage.laplace(array, output=output) + assert_array_almost_equal(tmp1 + tmp2, output) def test_gaussian_laplace01(self): "gaussian laplace filter 1" @@ -630,7 +647,7 @@ tmp1 = ndimage.gaussian_filter(array, 1.0, [2, 0]) tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 2]) output = ndimage.gaussian_laplace(array, 1.0) - self.assertTrue(diff(tmp1 + tmp2, output) < eps) + assert_array_almost_equal(tmp1 + tmp2, output) def test_gaussian_laplace02(self): "gaussian laplace filter 2" @@ -642,7 +659,7 @@ tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 2]) output = numpy.zeros(array.shape, type) ndimage.gaussian_laplace(array, 1.0, output) - self.assertTrue(diff(tmp1 + tmp2, output) < eps) + assert_array_almost_equal(tmp1 + tmp2, output) def test_generic_laplace01(self): "generic laplace filter 1" @@ -659,9 +676,9 @@ [5, 6, 9, 3, 5]], type) output = numpy.zeros(array.shape, type) tmp = ndimage.generic_laplace(array, derivative2, - extra_arguments = (1.0,), extra_keywords = {'b': 2.0}) + extra_arguments=(1.0,), extra_keywords={'b': 2.0}) ndimage.gaussian_laplace(array, 1.0, output) - self.assertTrue(diff(tmp, output) < eps) + assert_array_almost_equal(tmp, output) def test_gaussian_gradient_magnitude01(self): "gaussian gradient magnitude filter 1" @@ -673,9 +690,9 @@ tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 1]) output = ndimage.gaussian_gradient_magnitude(array, 1.0) - true = tmp1 * tmp1 + tmp2 * tmp2 - numpy.sqrt(true, true) - self.assertTrue(diff(true, output) < eps) + expected = tmp1 * tmp1 + tmp2 * tmp2 + numpy.sqrt(expected, expected) + assert_array_almost_equal(expected, output) def test_gaussian_gradient_magnitude02(self): "gaussian gradient magnitude filter 2" @@ -688,9 +705,9 @@ output = numpy.zeros(array.shape, type) ndimage.gaussian_gradient_magnitude(array, 1.0, output) - true = tmp1 * tmp1 + tmp2 * tmp2 - numpy.sqrt(true, true) - self.assertTrue(diff(true, output) < eps) + expected = tmp1 * tmp1 + tmp2 * tmp2 + numpy.sqrt(expected, expected) + assert_array_almost_equal(expected, output) def test_generic_gradient_magnitude01(self): "generic gradient magnitude 1" @@ -706,45 +723,45 @@ output, mode, cval) tmp1 = ndimage.gaussian_gradient_magnitude(array, 1.0) tmp2 = ndimage.generic_gradient_magnitude(array, - derivative, extra_arguments = (1.0,), - extra_keywords = {'b': 2.0}) - self.assertTrue(diff(tmp1, tmp2) < eps) + derivative, extra_arguments=(1.0,), + extra_keywords={'b': 2.0}) + assert_array_almost_equal(tmp1, tmp2) def test_uniform01(self): "uniform filter 1" array = numpy.array([2, 4, 6]) size = 2 output = ndimage.uniform_filter1d(array, size, - origin = -1) - self.assertTrue(diff([3, 5, 6], output) < eps) + origin=-1) + assert_array_almost_equal([3, 5, 6], output) def test_uniform02(self): "uniform filter 2" array = numpy.array([1, 2, 3]) filter_shape = [0] output = ndimage.uniform_filter(array, filter_shape) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) def test_uniform03(self): "uniform filter 3" array = numpy.array([1, 2, 3]) filter_shape = [1] output = ndimage.uniform_filter(array, filter_shape) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) def test_uniform04(self): "uniform filter 4" array = numpy.array([2, 4, 6]) filter_shape = [2] output = ndimage.uniform_filter(array, filter_shape) - self.assertTrue(diff([2, 3, 5], output) < eps) + assert_array_almost_equal([2, 3, 5], output) def test_uniform05(self): "uniform filter 5" array = [] filter_shape = [1] output = ndimage.uniform_filter(array, filter_shape) - self.assertTrue(diff([], output) < eps) + assert_array_almost_equal([], output) def test_uniform06(self): "uniform filter 6" @@ -754,37 +771,37 @@ [16, 20, 24]], type1) for type2 in self.types: output = ndimage.uniform_filter(array, - filter_shape, output = type2) - error = diff([[4, 6, 10], [10, 12, 16]], output) - self.assertTrue(error < eps and output.dtype.type == type2) + filter_shape, output=type2) + assert_array_almost_equal([[4, 6, 10], [10, 12, 16]], output) + assert_equal(output.dtype.type, type2) def test_minimum_filter01(self): "minimum filter 1" array = numpy.array([1, 2, 3, 4, 5]) filter_shape = numpy.array([2]) output = ndimage.minimum_filter(array, filter_shape) - self.assertTrue(diff([1, 1, 2, 3, 4], output) < eps) + assert_array_almost_equal([1, 1, 2, 3, 4], output) def test_minimum_filter02(self): "minimum filter 2" array = numpy.array([1, 2, 3, 4, 5]) filter_shape = numpy.array([3]) output = ndimage.minimum_filter(array, filter_shape) - self.assertTrue(diff([1, 1, 2, 3, 4], output) < eps) + assert_array_almost_equal([1, 1, 2, 3, 4], output) def test_minimum_filter03(self): "minimum filter 3" array = numpy.array([3, 2, 5, 1, 4]) filter_shape = numpy.array([2]) output = ndimage.minimum_filter(array, filter_shape) - self.assertTrue(diff([3, 2, 2, 1, 1], output) < eps) + assert_array_almost_equal([3, 2, 2, 1, 1], output) def test_minimum_filter04(self): "minimum filter 4" array = numpy.array([3, 2, 5, 1, 4]) filter_shape = numpy.array([3]) output = ndimage.minimum_filter(array, filter_shape) - self.assertTrue(diff([2, 2, 1, 1, 1], output) < eps) + assert_array_almost_equal([2, 2, 1, 1, 1], output) def test_minimum_filter05(self): "minimum filter 5" @@ -793,9 +810,9 @@ [5, 8, 3, 7, 1]]) filter_shape = numpy.array([2, 3]) output = ndimage.minimum_filter(array, filter_shape) - self.assertTrue(diff([[2, 2, 1, 1, 1], + assert_array_almost_equal([[2, 2, 1, 1, 1], [2, 2, 1, 1, 1], - [5, 3, 3, 1, 1]], output) < eps) + [5, 3, 3, 1, 1]], output) def test_minimum_filter06(self): "minimum filter 6" @@ -804,10 +821,10 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 1, 1], [1, 1, 1]] output = ndimage.minimum_filter(array, - footprint = footprint) - self.assertTrue(diff([[2, 2, 1, 1, 1], + footprint=footprint) + assert_array_almost_equal([[2, 2, 1, 1, 1], [2, 2, 1, 1, 1], - [5, 3, 3, 1, 1]], output) < eps) + [5, 3, 3, 1, 1]], output) def test_minimum_filter07(self): "minimum filter 7" @@ -816,10 +833,10 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] output = ndimage.minimum_filter(array, - footprint = footprint) - self.assertTrue(diff([[2, 2, 1, 1, 1], + footprint=footprint) + assert_array_almost_equal([[2, 2, 1, 1, 1], [2, 3, 1, 3, 1], - [5, 5, 3, 3, 1]], output) < eps) + [5, 5, 3, 3, 1]], output) def test_minimum_filter08(self): "minimum filter 8" @@ -828,10 +845,10 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] output = ndimage.minimum_filter(array, - footprint = footprint, origin = -1) - self.assertTrue(diff([[3, 1, 3, 1, 1], + footprint=footprint, origin=-1) + assert_array_almost_equal([[3, 1, 3, 1, 1], [5, 3, 3, 1, 1], - [3, 3, 1, 1, 1]], output) < eps) + [3, 3, 1, 1, 1]], output) def test_minimum_filter09(self): "minimum filter 9" @@ -840,38 +857,38 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] output = ndimage.minimum_filter(array, - footprint = footprint, origin = [-1, 0]) - self.assertTrue(diff([[2, 3, 1, 3, 1], + footprint=footprint, origin=[-1, 0]) + assert_array_almost_equal([[2, 3, 1, 3, 1], [5, 5, 3, 3, 1], - [5, 3, 3, 1, 1]], output) < eps) + [5, 3, 3, 1, 1]], output) def test_maximum_filter01(self): "maximum filter 1" array = numpy.array([1, 2, 3, 4, 5]) filter_shape = numpy.array([2]) output = ndimage.maximum_filter(array, filter_shape) - self.assertTrue(diff([1, 2, 3, 4, 5], output) < eps) + assert_array_almost_equal([1, 2, 3, 4, 5], output) def test_maximum_filter02(self): "maximum filter 2" array = numpy.array([1, 2, 3, 4, 5]) filter_shape = numpy.array([3]) output = ndimage.maximum_filter(array, filter_shape) - self.assertTrue(diff([2, 3, 4, 5, 5], output) < eps) + assert_array_almost_equal([2, 3, 4, 5, 5], output) def test_maximum_filter03(self): "maximum filter 3" array = numpy.array([3, 2, 5, 1, 4]) filter_shape = numpy.array([2]) output = ndimage.maximum_filter(array, filter_shape) - self.assertTrue(diff([3, 3, 5, 5, 4], output) < eps) + assert_array_almost_equal([3, 3, 5, 5, 4], output) def test_maximum_filter04(self): "maximum filter 4" array = numpy.array([3, 2, 5, 1, 4]) filter_shape = numpy.array([3]) output = ndimage.maximum_filter(array, filter_shape) - self.assertTrue(diff([3, 5, 5, 5, 4], output) < eps) + assert_array_almost_equal([3, 5, 5, 5, 4], output) def test_maximum_filter05(self): "maximum filter 5" @@ -880,9 +897,9 @@ [5, 8, 3, 7, 1]]) filter_shape = numpy.array([2, 3]) output = ndimage.maximum_filter(array, filter_shape) - self.assertTrue(diff([[3, 5, 5, 5, 4], + assert_array_almost_equal([[3, 5, 5, 5, 4], [7, 9, 9, 9, 5], - [8, 9, 9, 9, 7]], output) < eps) + [8, 9, 9, 9, 7]], output) def test_maximum_filter06(self): "maximum filter 6" @@ -891,10 +908,10 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 1, 1], [1, 1, 1]] output = ndimage.maximum_filter(array, - footprint = footprint) - self.assertTrue(diff([[3, 5, 5, 5, 4], + footprint=footprint) + assert_array_almost_equal([[3, 5, 5, 5, 4], [7, 9, 9, 9, 5], - [8, 9, 9, 9, 7]], output) < eps) + [8, 9, 9, 9, 7]], output) def test_maximum_filter07(self): "maximum filter 7" @@ -903,10 +920,10 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] output = ndimage.maximum_filter(array, - footprint = footprint) - self.assertTrue(diff([[3, 5, 5, 5, 4], + footprint=footprint) + assert_array_almost_equal([[3, 5, 5, 5, 4], [7, 7, 9, 9, 5], - [7, 9, 8, 9, 7]], output) < eps) + [7, 9, 8, 9, 7]], output) def test_maximum_filter08(self): "maximum filter 8" @@ -915,10 +932,10 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] output = ndimage.maximum_filter(array, - footprint = footprint, origin = -1) - self.assertTrue(diff([[7, 9, 9, 5, 5], + footprint=footprint, origin=-1) + assert_array_almost_equal([[7, 9, 9, 5, 5], [9, 8, 9, 7, 5], - [8, 8, 7, 7, 7]], output) < eps) + [8, 8, 7, 7, 7]], output) def test_maximum_filter09(self): "maximum filter 9" @@ -927,102 +944,102 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] output = ndimage.maximum_filter(array, - footprint = footprint, origin = [-1, 0]) - self.assertTrue(diff([[7, 7, 9, 9, 5], + footprint=footprint, origin=[-1, 0]) + assert_array_almost_equal([[7, 7, 9, 9, 5], [7, 9, 8, 9, 7], - [8, 8, 8, 7, 7]], output) < eps) + [8, 8, 8, 7, 7]], output) def test_rank01(self): "rank filter 1" array = numpy.array([1, 2, 3, 4, 5]) - output = ndimage.rank_filter(array, 1, size = 2) - self.assertTrue(diff(array, output) < eps) - output = ndimage.percentile_filter(array, 100, size = 2) - self.assertTrue(diff(array, output) < eps) + output = ndimage.rank_filter(array, 1, size=2) + assert_array_almost_equal(array, output) + output = ndimage.percentile_filter(array, 100, size=2) + assert_array_almost_equal(array, output) output = ndimage.median_filter(array, 2) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) def test_rank02(self): "rank filter 2" array = numpy.array([1, 2, 3, 4, 5]) - output = ndimage.rank_filter(array, 1, size = [3]) - self.assertTrue(diff(array, output) < eps) - output = ndimage.percentile_filter(array, 50, size = 3) - self.assertTrue(diff(array, output) < eps) + output = ndimage.rank_filter(array, 1, size=[3]) + assert_array_almost_equal(array, output) + output = ndimage.percentile_filter(array, 50, size=3) + assert_array_almost_equal(array, output) output = ndimage.median_filter(array, (3,)) - self.assertTrue(diff(array, output) < eps) + assert_array_almost_equal(array, output) def test_rank03(self): "rank filter 3" array = numpy.array([3, 2, 5, 1, 4]) - output = ndimage.rank_filter(array, 1, size = [2]) - self.assertTrue(diff([3, 3, 5, 5, 4], output) < eps) - output = ndimage.percentile_filter(array, 100, size = 2) - self.assertTrue(diff([3, 3, 5, 5, 4], output) < eps) + output = ndimage.rank_filter(array, 1, size=[2]) + assert_array_almost_equal([3, 3, 5, 5, 4], output) + output = ndimage.percentile_filter(array, 100, size=2) + assert_array_almost_equal([3, 3, 5, 5, 4], output) def test_rank04(self): "rank filter 4" array = numpy.array([3, 2, 5, 1, 4]) - true = [3, 3, 2, 4, 4] - output = ndimage.rank_filter(array, 1, size = 3) - self.assertTrue(diff(true, output) < eps) - output = ndimage.percentile_filter(array, 50, size = 3) - self.assertTrue(diff(true, output) < eps) - output = ndimage.median_filter(array, size = 3) - self.assertTrue(diff(true, output) < eps) + expected = [3, 3, 2, 4, 4] + output = ndimage.rank_filter(array, 1, size=3) + assert_array_almost_equal(expected, output) + output = ndimage.percentile_filter(array, 50, size=3) + assert_array_almost_equal(expected, output) + output = ndimage.median_filter(array, size=3) + assert_array_almost_equal(expected, output) def test_rank05(self): "rank filter 5" array = numpy.array([3, 2, 5, 1, 4]) - true = [3, 3, 2, 4, 4] - output = ndimage.rank_filter(array, -2, size = 3) - self.assertTrue(diff(true, output) < eps) + expected = [3, 3, 2, 4, 4] + output = ndimage.rank_filter(array, -2, size=3) + assert_array_almost_equal(expected, output) def test_rank06(self): "rank filter 6" array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]]) - true = [[2, 2, 1, 1, 1], + expected = [[2, 2, 1, 1, 1], [3, 3, 2, 1, 1], [5, 5, 3, 3, 1]] - output = ndimage.rank_filter(array, 1, size = [2, 3]) - self.assertTrue(diff(true, output) < eps) + output = ndimage.rank_filter(array, 1, size=[2, 3]) + assert_array_almost_equal(expected, output) output = ndimage.percentile_filter(array, 17, - size = (2, 3)) - self.assertTrue(diff(true, output) < eps) + size=(2, 3)) + assert_array_almost_equal(expected, output) def test_rank07(self): "rank filter 7" array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]]) - true = [[3, 5, 5, 5, 4], + expected = [[3, 5, 5, 5, 4], [5, 5, 7, 5, 4], [6, 8, 8, 7, 5]] - output = ndimage.rank_filter(array, -2, size = [2, 3]) - self.assertTrue(diff(true, output) < eps) + output = ndimage.rank_filter(array, -2, size=[2, 3]) + assert_array_almost_equal(expected, output) def test_rank08(self): "median filter 8" array = numpy.array([[3, 2, 5, 1, 4], [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]]) - true = [[3, 3, 2, 4, 4], + expected = [[3, 3, 2, 4, 4], [5, 5, 5, 4, 4], [5, 6, 7, 5, 5]] kernel = numpy.array([2, 3]) output = ndimage.percentile_filter(array, 50.0, - size = (2, 3)) - self.assertTrue(diff(true, output) < eps) - output = ndimage.rank_filter(array, 3, size = (2, 3)) - self.assertTrue(diff(true, output) < eps) - output = ndimage.median_filter(array, size = (2, 3)) - self.assertTrue(diff(true, output) < eps) + size=(2, 3)) + assert_array_almost_equal(expected, output) + output = ndimage.rank_filter(array, 3, size=(2, 3)) + assert_array_almost_equal(expected, output) + output = ndimage.median_filter(array, size=(2, 3)) + assert_array_almost_equal(expected, output) def test_rank09(self): "rank filter 9" - true = [[3, 3, 2, 4, 4], + expected = [[3, 3, 2, 4, 4], [3, 5, 2, 5, 1], [5, 5, 8, 3, 5]] footprint = [[1, 0, 1], [0, 1, 0]] @@ -1031,48 +1048,48 @@ [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) output = ndimage.rank_filter(array, 1, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) output = ndimage.percentile_filter(array, 35, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) def test_rank10(self): "rank filter 10" array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) - true = [[2, 2, 1, 1, 1], + expected = [[2, 2, 1, 1, 1], [2, 3, 1, 3, 1], [5, 5, 3, 3, 1]] footprint = [[1, 0, 1], [1, 1, 0]] output = ndimage.rank_filter(array, 0, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) output = ndimage.percentile_filter(array, 0.0, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) def test_rank11(self): "rank filter 11" array = numpy.array([[3, 2, 5, 1, 4], [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) - true = [[3, 5, 5, 5, 4], + expected = [[3, 5, 5, 5, 4], [7, 7, 9, 9, 5], [7, 9, 8, 9, 7]] footprint = [[1, 0, 1], [1, 1, 0]] output = ndimage.rank_filter(array, -1, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) output = ndimage.percentile_filter(array, 100.0, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) def test_rank12(self): "rank filter 12" - true = [[3, 3, 2, 4, 4], + expected = [[3, 3, 2, 4, 4], [3, 5, 2, 5, 1], [5, 5, 8, 3, 5]] footprint = [[1, 0, 1], [0, 1, 0]] @@ -1081,18 +1098,18 @@ [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) output = ndimage.rank_filter(array, 1, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) output = ndimage.percentile_filter(array, 50.0, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) output = ndimage.median_filter(array, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) def test_rank13(self): "rank filter 13" - true = [[5, 2, 5, 1, 1], + expected = [[5, 2, 5, 1, 1], [5, 8, 3, 5, 5], [6, 6, 5, 5, 5]] footprint = [[1, 0, 1], [0, 1, 0]] @@ -1101,12 +1118,12 @@ [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) output = ndimage.rank_filter(array, 1, - footprint = footprint, origin = -1) - self.assertTrue(diff(true, output) < eps) + footprint=footprint, origin=-1) + assert_array_almost_equal(expected, output) def test_rank14(self): "rank filter 14" - true = [[3, 5, 2, 5, 1], + expected = [[3, 5, 2, 5, 1], [5, 5, 8, 3, 5], [5, 6, 6, 5, 5]] footprint = [[1, 0, 1], [0, 1, 0]] @@ -1115,8 +1132,8 @@ [5, 8, 3, 7, 1], [5, 6, 9, 3, 5]], type) output = ndimage.rank_filter(array, 1, - footprint = footprint, origin = [-1, 0]) - self.assertTrue(diff(true, output) < eps) + footprint=footprint, origin=[-1, 0]) + assert_array_almost_equal(expected, output) def test_generic_filter1d01(self): "generic 1d filter 1" @@ -1128,89 +1145,88 @@ output[ii] += input[ii + 1] * fltr[1] output[ii] += input[ii + 2] * fltr[2] for type in self.types: - a = numpy.arange(12, dtype = type) + a = numpy.arange(12, dtype=type) a.shape = (3,4) r1 = ndimage.correlate1d(a, weights / weights.sum(), 0, - origin = -1) + origin=-1) r2 = ndimage.generic_filter1d(a, _filter_func, 3, - axis = 0, origin = -1, extra_arguments = (weights,), - extra_keywords = {'total': weights.sum()}) - self.assertTrue(diff(r1, r2) < eps) + axis=0, origin=-1, extra_arguments=(weights,), + extra_keywords={'total': weights.sum()}) + assert_array_almost_equal(r1, r2) def test_generic_filter01(self): "generic filter 1" 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): + def _filter_func(buffer, weights, total=1.0): weights = cf / total return (buffer * weights).sum() for type in self.types: - a = numpy.arange(12, dtype = type) + a = numpy.arange(12, dtype=type) a.shape = (3,4) r1 = ndimage.correlate(a, filter_ * footprint) r1 /= 5 r2 = ndimage.generic_filter(a, _filter_func, - footprint = footprint, extra_arguments = (cf,), - extra_keywords = {'total': cf.sum()}) - self.assertTrue(diff(r1, r2) < eps, - "%r\n%r" % (r1, r2)) + footprint=footprint, extra_arguments=(cf,), + extra_keywords={'total': cf.sum()}) + assert_array_almost_equal(r1, r2) def test_extend01(self): "line extension 1" array = numpy.array([1, 2, 3]) weights = numpy.array([1, 0]) - true_values = [[1, 1, 2], + expected_values = [[1, 1, 2], [3, 1, 2], [1, 1, 2], [2, 1, 2], [0, 1, 2]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate1d(array, weights, 0, - mode = mode, cval = 0) - assert_array_equal(output,true_value) + mode=mode, cval=0) + assert_array_equal(output,expected_value) def test_extend02(self): "line extension 2" array = numpy.array([1, 2, 3]) weights = numpy.array([1, 0, 0, 0, 0, 0, 0, 0]) - true_values = [[1, 1, 1], + expected_values = [[1, 1, 1], [3, 1, 2], [3, 3, 2], [1, 2, 3], [0, 0, 0]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate1d(array, weights, 0, - mode = mode, cval = 0) - assert_array_equal(output, true_value) + mode=mode, cval=0) + assert_array_equal(output, expected_value) def test_extend03(self): "line extension 3" array = numpy.array([1, 2, 3]) weights = numpy.array([0, 0, 1]) - true_values = [[2, 3, 3], + expected_values = [[2, 3, 3], [2, 3, 1], [2, 3, 3], [2, 3, 2], [2, 3, 0]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate1d(array, weights, 0, - mode = mode, cval = 0) - assert_array_equal(output, true_value) + mode=mode, cval=0) + assert_array_equal(output, expected_value) def test_extend04(self): "line extension 4" array = numpy.array([1, 2, 3]) weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1]) - true_values = [[3, 3, 3], + expected_values = [[3, 3, 3], [2, 3, 1], [2, 1, 1], [1, 2, 3], [0, 0, 0]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate1d(array, weights, 0, - mode = mode, cval = 0) - assert_array_equal(output, true_value) + mode=mode, cval=0) + assert_array_equal(output, expected_value) def test_extend05(self): @@ -1219,15 +1235,15 @@ [4, 5, 6], [7, 8, 9]]) weights = numpy.array([[1, 0], [0, 0]]) - true_values = [[[1, 1, 2], [1, 1, 2], [4, 4, 5]], + expected_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]], [[5, 4, 5], [2, 1, 2], [5, 4, 5]], [[0, 0, 0], [0, 1, 2], [0, 4, 5]]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate(array, weights, - mode = mode, cval = 0) - assert_array_equal(output, true_value) + mode=mode, cval=0) + assert_array_equal(output, expected_value) def test_extend06(self): @@ -1236,74 +1252,74 @@ [4, 5, 6], [7, 8, 9]]) weights = numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]]) - true_values = [[[5, 6, 6], [8, 9, 9], [8, 9, 9]], + expected_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]], [[5, 6, 5], [8, 9, 8], [5, 6, 5]], [[5, 6, 0], [8, 9, 0], [0, 0, 0]]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate(array, weights, - mode = mode, cval = 0) - assert_array_equal(output, true_value) + mode=mode, cval=0) + assert_array_equal(output, expected_value) def test_extend07(self): "line extension 7" array = numpy.array([1, 2, 3]) weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1]) - true_values = [[3, 3, 3], + expected_values = [[3, 3, 3], [2, 3, 1], [2, 1, 1], [1, 2, 3], [0, 0, 0]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate(array, weights, - mode = mode, cval = 0) - assert_array_equal(output, true_value) + mode=mode, cval=0) + assert_array_equal(output, expected_value) def test_extend08(self): "line extension 8" array = numpy.array([[1], [2], [3]]) weights = numpy.array([[0], [0], [0], [0], [0], [0], [0], [0], [1]]) - true_values = [[[3], [3], [3]], + expected_values = [[[3], [3], [3]], [[2], [3], [1]], [[2], [1], [1]], [[1], [2], [3]], [[0], [0], [0]]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate(array, weights, - mode = mode, cval = 0) - assert_array_equal(output, true_value) + mode=mode, cval=0) + assert_array_equal(output, expected_value) def test_extend09(self): "line extension 9" array = numpy.array([1, 2, 3]) weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1]) - true_values = [[3, 3, 3], + expected_values = [[3, 3, 3], [2, 3, 1], [2, 1, 1], [1, 2, 3], [0, 0, 0]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate(array, weights, - mode = mode, cval = 0) - assert_array_equal(output, true_value) + mode=mode, cval=0) + assert_array_equal(output, expected_value) def test_extend10(self): "line extension 10" array = numpy.array([[1], [2], [3]]) weights = numpy.array([[0], [0], [0], [0], [0], [0], [0], [0], [1]]) - true_values = [[[3], [3], [3]], + expected_values = [[[3], [3], [3]], [[2], [3], [1]], [[2], [1], [1]], [[1], [2], [3]], [[0], [0], [0]]] - for mode, true_value in zip(self.modes, true_values): + for mode, expected_value in zip(self.modes, expected_values): output = ndimage.correlate(array, weights, - mode = mode, cval = 0) - assert_array_equal(output, true_value) + mode=mode, cval=0) + assert_array_equal(output, expected_value) def test_boundaries(self): "boundary modes" @@ -1352,7 +1368,7 @@ shape[0], 0) a = fft.ifft(a, shape[1], 1) a = fft.irfft(a, shape[0], 0) - self.assertTrue(diff(ndimage.sum(a), 1.0) < eps) + assert_almost_equal(ndimage.sum(a), 1) def test_fourier_gaussian_complex01(self): "gaussian fourier filter for complex transforms 1" @@ -1366,8 +1382,7 @@ 0) a = fft.ifft(a, shape[1], 1) a = fft.ifft(a, shape[0], 0) - error = diff(ndimage.sum(a.real), 1.0) - self.assertTrue(error < eps) + assert_almost_equal(ndimage.sum(a.real), 1.0) def test_fourier_uniform_real01(self): "uniform fourier filter for real transforms 1" @@ -1381,7 +1396,7 @@ shape[0], 0) a = fft.ifft(a, shape[1], 1) a = fft.irfft(a, shape[0], 0) - self.assertTrue(diff(ndimage.sum(a), 1.0) < eps) + assert_almost_equal(ndimage.sum(a), 1.0) def test_fourier_uniform_complex01(self): "uniform fourier filter for complex transforms 1" @@ -1394,39 +1409,36 @@ a = ndimage.fourier_uniform(a, [5.0, 2.5], -1, 0) a = fft.ifft(a, shape[1], 1) a = fft.ifft(a, shape[0], 0) - error = diff(ndimage.sum(a.real), 1.0) - self.assertTrue(error < eps) + assert_almost_equal(ndimage.sum(a.real), 1.0) def test_fourier_shift_real01(self): "shift filter for real transforms 1" for shape in [(32, 16), (31, 15)]: 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) + expected = numpy.arange(shape[0] * shape[1], dtype=dtype) + expected.shape = shape + a = fft.rfft(expected, shape[0], 0) a = fft.fft(a, shape[1], 1) a = ndimage.fourier_shift(a, [1, 1], shape[0], 0) 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, numpy.zeros(shape)) - self.assertTrue(error1 < 1e-10 and error2 < 1e-10) + assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1]) + assert_array_almost_equal(a.imag, numpy.zeros(shape)) def test_fourier_shift_complex01(self): "shift filter for complex transforms 1" for shape in [(32, 16), (31, 15)]: 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) + expected = numpy.arange(shape[0] * shape[1], + dtype=type) + expected.shape = shape + a = fft.fft(expected, shape[0], 0) a = fft.fft(a, shape[1], 1) a = ndimage.fourier_shift(a, [1, 1], -1, 0) 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, numpy.zeros(shape)) - self.assertTrue(error1 < 1e-10 and error2 < 1e-10) + assert_array_almost_equal(a.real[1:, 1:], expected[:-1, :-1]) + assert_array_almost_equal(a.imag, numpy.zeros(shape)) def test_fourier_ellipsoid_real01(self): "ellipsoid fourier filter for real transforms 1" @@ -1437,10 +1449,10 @@ a = fft.rfft(a, shape[0], 0) a = fft.fft(a, shape[1], 1) a = ndimage.fourier_ellipsoid(a, [5.0, 2.5], - shape[0], 0) + shape[0], 0) a = fft.ifft(a, shape[1], 1) a = fft.irfft(a, shape[0], 0) - self.assertTrue(diff(ndimage.sum(a), 1.0) < eps) + assert_almost_equal(ndimage.sum(a), 1.0) def test_fourier_ellipsoid_complex01(self): "ellipsoid fourier filter for complex transforms 1" @@ -1454,26 +1466,23 @@ 0) a = fft.ifft(a, shape[1], 1) a = fft.ifft(a, shape[0], 0) - error = diff(ndimage.sum(a.real), 1.0) - self.assertTrue(error < eps) + assert_almost_equal(ndimage.sum(a.real), 1.0) def test_spline01(self): "spline filter 1" for type in self.types: data = numpy.ones([], type) for order in range(2, 6): - out = ndimage.spline_filter(data, order = order) - self.assertTrue(diff(out, 1)< eps and - out.dtype.type == numpy.float64) + out = ndimage.spline_filter(data, order=order) + assert_array_almost_equal(out, 1) def test_spline02(self): "spline filter 2" for type in self.types: data = numpy.array([1]) for order in range(2, 6): - out = ndimage.spline_filter(data, order = order) - self.assertTrue(diff(out, [1]) < eps and - out.dtype.type == numpy.float64) + out = ndimage.spline_filter(data, order=order) + assert_array_almost_equal(out, [1]) def test_spline03(self): "spline filter 3" @@ -1481,9 +1490,8 @@ data = numpy.ones([], type) for order in range(2, 6): out = ndimage.spline_filter(data, order, - output = type) - self.assertTrue(diff(out, 1) < eps and - out.dtype.type == type) + output=type) + assert_array_almost_equal(out, 1) def test_spline04(self): "spline filter 4" @@ -1491,18 +1499,18 @@ data = numpy.ones([4], type) for order in range(2, 6): out = ndimage.spline_filter(data, order) - self.assertTrue(diff(out, [1, 1, 1, 1]) < eps) + assert_array_almost_equal(out, [1, 1, 1, 1]) def test_spline05(self): "spline filter 5" for type in self.types: data = numpy.ones([4, 4], type) for order in range(2, 6): - out = ndimage.spline_filter(data, order = order) - self.assertTrue(diff(out, [[1, 1, 1, 1], + out = ndimage.spline_filter(data, order=order) + assert_array_almost_equal(out, [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], - [1, 1, 1, 1]]) < eps) + [1, 1, 1, 1]]) def test_geometric_transform01(self): "geometric transform 1" @@ -1513,7 +1521,7 @@ out = ndimage.geometric_transform(data, mapping, data.shape, order=order) - self.assertTrue(diff(out, [1]) < eps) + assert_array_almost_equal(out, [1]) def test_geometric_transform02(self): "geometric transform 2" @@ -1523,7 +1531,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, data.shape, order=order) - self.assertTrue(diff(out, [1, 1, 1, 1]) < eps) + assert_array_almost_equal(out, [1, 1, 1, 1]) def test_geometric_transform03(self): "geometric transform 3" @@ -1533,7 +1541,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, data.shape, order=order) - self.assertTrue(diff(out, [0, 1, 1, 1]) < eps) + assert_array_almost_equal(out, [0, 1, 1, 1]) def test_geometric_transform04(self): "geometric transform 4" @@ -1543,7 +1551,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, data.shape, order=order) - self.assertTrue(diff(out, [0, 4, 1, 3]) < eps) + assert_array_almost_equal(out, [0, 4, 1, 3]) def test_geometric_transform05(self): "geometric transform 5" @@ -1555,9 +1563,9 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, data.shape, order=order) - self.assertTrue(diff(out, [[0, 1, 1, 1], + assert_array_almost_equal(out, [[0, 1, 1, 1], [0, 1, 1, 1], - [0, 1, 1, 1]]) < eps) + [0, 1, 1, 1]]) def test_geometric_transform06(self): "geometric transform 6" @@ -1569,9 +1577,9 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, data.shape, order=order) - self.assertTrue(diff(out, [[0, 4, 1, 3], + assert_array_almost_equal(out, [[0, 4, 1, 3], [0, 7, 6, 8], - [0, 3, 5, 3]]) < eps) + [0, 3, 5, 3]]) def test_geometric_transform07(self): "geometric transform 7" @@ -1583,9 +1591,9 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, data.shape, order=order) - self.assertTrue(diff(out, [[0, 0, 0, 0], + assert_array_almost_equal(out, [[0, 0, 0, 0], [4, 1, 3, 2], - [7, 6, 8, 5]]) < eps) + [7, 6, 8, 5]]) def test_geometric_transform08(self): "geometric transform 8" @@ -1597,9 +1605,9 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, data.shape, order=order) - self.assertTrue(diff(out, [[0, 0, 0, 0], + assert_array_almost_equal(out, [[0, 0, 0, 0], [0, 4, 1, 3], - [0, 7, 6, 8]]) < eps) + [0, 7, 6, 8]]) def test_geometric_transform10(self): "geometric transform 10" @@ -1615,10 +1623,10 @@ else: filtered = data out = ndimage.geometric_transform(filtered, mapping, - data.shape, order=order, prefilter = False) - self.assertTrue(diff(out, [[0, 0, 0, 0], + data.shape, order=order, prefilter=False) + assert_array_almost_equal(out, [[0, 0, 0, 0], [0, 4, 1, 3], - [0, 7, 6, 8]]) < eps) + [0, 7, 6, 8]]) def test_geometric_transform13(self): "geometric transform 13" @@ -1628,8 +1636,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, [4], order=order) - self.assertTrue(diff(out, [1, 1, 1, 1]) < eps, - "%r" % out) + assert_array_almost_equal(out, [1, 1, 1, 1]) def test_geometric_transform14(self): "geometric transform 14" @@ -1639,7 +1646,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, [4], order=order) - self.assertTrue(diff(out, [1, 2, 3, 4]) < eps) + assert_array_almost_equal(out, [1, 2, 3, 4]) def test_geometric_transform15(self): "geometric transform 15" @@ -1649,7 +1656,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, [8], order=order) - self.assertTrue(diff(out[::2], [1, 2, 3, 4]) < eps) + assert_array_almost_equal(out[::2], [1, 2, 3, 4]) def test_geometric_transform16(self): "geometric transform 16" @@ -1661,7 +1668,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, (3, 2), order=order) - self.assertTrue(diff(out, [[1, 3], [5, 7], [9, 11]]) < eps) + assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]]) def test_geometric_transform17(self): "geometric transform 17" @@ -1673,7 +1680,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, (1, 4), order=order) - self.assertTrue(diff(out, [[1, 2, 3, 4]]) < eps) + assert_array_almost_equal(out, [[1, 2, 3, 4]]) def test_geometric_transform18(self): "geometric transform 18" @@ -1685,7 +1692,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, (1, 2), order=order) - self.assertTrue(diff(out, [[1, 3]]) < eps) + assert_array_almost_equal(out, [[1, 3]]) def test_geometric_transform19(self): "geometric transform 19" @@ -1697,7 +1704,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, (3, 8), order=order) - self.assertTrue(diff(out[..., ::2], data) < eps) + assert_array_almost_equal(out[..., ::2], data) def test_geometric_transform20(self): "geometric transform 20" @@ -1709,7 +1716,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, (6, 4), order=order) - self.assertTrue(diff(out[::2, ...], data) < eps) + assert_array_almost_equal(out[::2, ...], data) def test_geometric_transform21(self): "geometric transform 21" @@ -1721,7 +1728,7 @@ for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, (6, 8), order=order) - self.assertTrue(diff(out[::2, ::2], data) < eps) + assert_array_almost_equal(out[::2, ::2], data) def test_geometric_transform22(self): @@ -1739,7 +1746,7 @@ out = ndimage.geometric_transform(out, mapping2, (3, 4), order=order) error = diff(out, data) - self.assertTrue(diff(out, data) < eps) + assert_array_almost_equal(out, data) def test_geometric_transform23(self): "geometric transform 23" @@ -1752,7 +1759,7 @@ out = ndimage.geometric_transform(data, mapping, (2,), order=order) out = out.astype(numpy.int32) - self.assertTrue(diff(out, [5, 7]) < eps) + assert_array_almost_equal(out, [5, 7]) def test_geometric_transform24(self): "geometric transform 24" @@ -1763,9 +1770,9 @@ return (a, x[0] * b) for order in range(0, 6): out = ndimage.geometric_transform(data, mapping, - (2,), order=order, extra_arguments = (1,), - extra_keywords = {'b': 2}) - self.assertTrue(diff(out, [5, 7]) < eps) + (2,), order=order, extra_arguments=(1,), + extra_keywords={'b': 2}) + assert_array_almost_equal(out, [5, 7]) def test_map_coordinates01(self): "map coordinates 1" @@ -1776,9 +1783,9 @@ idx -= 1 for order in range(0, 6): out = ndimage.map_coordinates(data, idx, order=order) - self.assertTrue(diff(out, [[0, 0, 0, 0], + assert_array_almost_equal(out, [[0, 0, 0, 0], [0, 4, 1, 3], - [0, 7, 6, 8]]) < eps) + [0, 7, 6, 8]]) def test_map_coordinates02(self): "map coordinates 2" @@ -1791,7 +1798,7 @@ out1 = ndimage.shift(data, 0.5, order=order) out2 = ndimage.map_coordinates(data, idx, order=order) - self.assertTrue(diff(out1, out2) < eps) + assert_array_almost_equal(out1, out2) def test_affine_transform01(self): "affine_transform 1" @@ -1799,7 +1806,7 @@ for order in range(0, 6): out = ndimage.affine_transform(data, [[1]], order=order) - self.assertTrue(diff(out, [1]) < eps) + assert_array_almost_equal(out, [1]) def test_affine_transform02(self): "affine transform 2" @@ -1807,7 +1814,7 @@ for order in range(0, 6): out = ndimage.affine_transform(data, [[1]], order=order) - self.assertTrue(diff(out, [1, 1, 1, 1]) < eps) + assert_array_almost_equal(out, [1, 1, 1, 1]) def test_affine_transform03(self): "affine transform 3" @@ -1815,7 +1822,7 @@ for order in range(0, 6): out = ndimage.affine_transform(data, [[1]], -1, order=order) - self.assertTrue(diff(out, [0, 1, 1, 1]) < eps) + assert_array_almost_equal(out, [0, 1, 1, 1]) def test_affine_transform04(self): "affine transform 4" @@ -1823,7 +1830,7 @@ for order in range(0, 6): out = ndimage.affine_transform(data, [[1]], -1, order=order) - self.assertTrue(diff(out, [0, 4, 1, 3]) < eps) + assert_array_almost_equal(out, [0, 4, 1, 3]) def test_affine_transform05(self): "affine transform 5" @@ -1834,9 +1841,9 @@ out = ndimage.affine_transform(data, [[1, 0], [0, 1]], [0, -1], order=order) - self.assertTrue(diff(out, [[0, 1, 1, 1], + assert_array_almost_equal(out, [[0, 1, 1, 1], [0, 1, 1, 1], - [0, 1, 1, 1]]) < eps) + [0, 1, 1, 1]]) def test_affine_transform06(self): "affine transform 6" @@ -1847,9 +1854,9 @@ out = ndimage.affine_transform(data, [[1, 0], [0, 1]], [0, -1], order=order) - self.assertTrue(diff(out, [[0, 4, 1, 3], + assert_array_almost_equal(out, [[0, 4, 1, 3], [0, 7, 6, 8], - [0, 3, 5, 3]]) < eps) + [0, 3, 5, 3]]) def test_affine_transform07(self): "affine transform 7" @@ -1860,9 +1867,9 @@ out = ndimage.affine_transform(data, [[1, 0], [0, 1]], [-1, 0], order=order) - self.assertTrue(diff(out, [[0, 0, 0, 0], + assert_array_almost_equal(out, [[0, 0, 0, 0], [4, 1, 3, 2], - [7, 6, 8, 5]]) < eps) + [7, 6, 8, 5]]) def test_affine_transform08(self): "affine transform 8" @@ -1873,9 +1880,9 @@ out = ndimage.affine_transform(data, [[1, 0], [0, 1]], [-1, -1], order=order) - self.assertTrue(diff(out, [[0, 0, 0, 0], + assert_array_almost_equal(out, [[0, 0, 0, 0], [0, 4, 1, 3], - [0, 7, 6, 8]]) < eps) + [0, 7, 6, 8]]) def test_affine_transform09(self): "affine transform 9" @@ -1890,18 +1897,18 @@ filtered = data out = ndimage.affine_transform(filtered,[[1, 0], [0, 1]], - [-1, -1], order=order, prefilter = False) - self.assertTrue(diff(out, [[0, 0, 0, 0], + [-1, -1], order=order, prefilter=False) + assert_array_almost_equal(out, [[0, 0, 0, 0], [0, 4, 1, 3], - [0, 7, 6, 8]]) < eps) + [0, 7, 6, 8]]) def test_affine_transform10(self): "affine transform 10" data = numpy.ones([2], numpy.float64) for order in range(0, 6): out = ndimage.affine_transform(data, [[0.5]], - output_shape = (4,), order=order) - self.assertTrue(diff(out, [1, 1, 1, 0]) < eps) + output_shape=(4,), order=order) + assert_array_almost_equal(out, [1, 1, 1, 0]) def test_affine_transform11(self): "affine transform 11" @@ -1909,7 +1916,7 @@ for order in range(0, 6): out = ndimage.affine_transform(data, [[2]], 0, (4,), order=order) - self.assertTrue(diff(out, [1, 2, 3, 4]) < eps) + assert_array_almost_equal(out, [1, 2, 3, 4]) def test_affine_transform12(self): "affine transform 12" @@ -1917,7 +1924,7 @@ for order in range(0, 6): out = ndimage.affine_transform(data, [[0.5]], 0, (8,), order=order) - self.assertTrue(diff(out[::2], [1, 2, 3, 4]) < eps) + assert_array_almost_equal(out[::2], [1, 2, 3, 4]) def test_affine_transform13(self): "affine transform 13" @@ -1928,7 +1935,7 @@ out = ndimage.affine_transform(data, [[1, 0], [0, 2]], 0, (3, 2), order=order) - self.assertTrue(diff(out, [[1, 3], [5, 7], [9, 11]]) < eps) + assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]]) def test_affine_transform14(self): "affine transform 14" @@ -1939,7 +1946,7 @@ out = ndimage.affine_transform(data, [[2, 0], [0, 1]], 0, (1, 4), order=order) - self.assertTrue(diff(out, [[1, 2, 3, 4]]) < eps) + assert_array_almost_equal(out, [[1, 2, 3, 4]]) def test_affine_transform15(self): "affine transform 15" @@ -1950,7 +1957,7 @@ out = ndimage.affine_transform(data, [[2, 0], [0, 2]], 0, (1, 2), order=order) - self.assertTrue(diff(out, [[1, 3]]) < eps) + assert_array_almost_equal(out, [[1, 3]]) def test_affine_transform16(self): "affine transform 16" @@ -1961,7 +1968,7 @@ out = ndimage.affine_transform(data, [[1, 0.0], [0, 0.5]], 0, (3, 8), order=order) - self.assertTrue(diff(out[..., ::2], data) < eps) + assert_array_almost_equal(out[..., ::2], data) def test_affine_transform17(self): "affine transform 17" @@ -1972,7 +1979,7 @@ out = ndimage.affine_transform(data, [[0.5, 0], [0, 1]], 0, (6, 4), order=order) - self.assertTrue(diff(out[::2, ...], data) < eps) + assert_array_almost_equal(out[::2, ...], data) def test_affine_transform18(self): "affine transform 18" @@ -1984,7 +1991,7 @@ [[0.5, 0], [0, 0.5]], 0, (6, 8), order=order) - self.assertTrue(diff(out[::2, ::2], data) < eps) + assert_array_almost_equal(out[::2, ::2], data) def test_affine_transform19(self): "affine transform 19" @@ -2000,7 +2007,7 @@ [[2.0, 0], [0, 2.0]], 0, (3, 4), order=order) - self.assertTrue(diff(out, data) < eps) + assert_array_almost_equal(out, data) def test_affine_transform20(self): "affine transform 20" @@ -2010,7 +2017,7 @@ for order in range(0, 6): out = ndimage.affine_transform(data, [[0], [2]], 0, (2,), order=order) - self.assertTrue(diff(out, [1, 3]) < eps) + assert_array_almost_equal(out, [1, 3]) def test_affine_transform21(self): "affine transform 21" @@ -2020,35 +2027,35 @@ for order in range(0, 6): out = ndimage.affine_transform(data, [[2], [0]], 0, (2,), order=order) - self.assertTrue(diff(out, [1, 9]) < eps) + assert_array_almost_equal(out, [1, 9]) def test_shift01(self): "shift 1" data = numpy.array([1]) for order in range(0, 6): out = ndimage.shift(data, [1], order=order) - self.assertTrue(diff(out, [0]) < eps) + assert_array_almost_equal(out, [0]) def test_shift02(self): "shift 2" data = numpy.ones([4]) for order in range(0, 6): out = ndimage.shift(data, [1], order=order) - self.assertTrue(diff(out, [0, 1, 1, 1]) < eps) + assert_array_almost_equal(out, [0, 1, 1, 1]) def test_shift03(self): "shift 3" data = numpy.ones([4]) for order in range(0, 6): out = ndimage.shift(data, -1, order=order) - self.assertTrue(diff(out, [1, 1, 1, 0]) < eps) + assert_array_almost_equal(out, [1, 1, 1, 0]) def test_shift04(self): "shift 4" data = numpy.array([4, 1, 3, 2]) for order in range(0, 6): out = ndimage.shift(data, 1, order=order) - self.assertTrue(diff(out, [0, 4, 1, 3]) < eps) + assert_array_almost_equal(out, [0, 4, 1, 3]) def test_shift05(self): "shift 5" @@ -2057,9 +2064,9 @@ [1, 1, 1, 1]]) for order in range(0, 6): out = ndimage.shift(data, [0, 1], order=order) - self.assertTrue(diff(out, [[0, 1, 1, 1], + assert_array_almost_equal(out, [[0, 1, 1, 1], [0, 1, 1, 1], - [0, 1, 1, 1]]) < eps) + [0, 1, 1, 1]]) def test_shift06(self): "shift 6" @@ -2068,9 +2075,9 @@ [3, 5, 3, 6]]) for order in range(0, 6): out = ndimage.shift(data, [0, 1], order=order) - self.assertTrue(diff(out, [[0, 4, 1, 3], + assert_array_almost_equal(out, [[0, 4, 1, 3], [0, 7, 6, 8], - [0, 3, 5, 3]]) < eps) + [0, 3, 5, 3]]) def test_shift07(self): "shift 7" @@ -2079,9 +2086,9 @@ [3, 5, 3, 6]]) for order in range(0, 6): out = ndimage.shift(data, [1, 0], order=order) - self.assertTrue(diff(out, [[0, 0, 0, 0], + assert_array_almost_equal(out, [[0, 0, 0, 0], [4, 1, 3, 2], - [7, 6, 8, 5]]) < eps) + [7, 6, 8, 5]]) def test_shift08(self): @@ -2091,9 +2098,9 @@ [3, 5, 3, 6]]) for order in range(0, 6): out = ndimage.shift(data, [1, 1], order=order) - self.assertTrue(diff(out, [[0, 0, 0, 0], + assert_array_almost_equal(out, [[0, 0, 0, 0], [0, 4, 1, 3], - [0, 7, 6, 8]]) < eps) + [0, 7, 6, 8]]) def test_shift09(self): "shift 9" @@ -2107,10 +2114,10 @@ else: filtered = data out = ndimage.shift(filtered, [1, 1], order=order, - prefilter = False) - self.assertTrue(diff(out, [[0, 0, 0, 0], + prefilter=False) + assert_array_almost_equal(out, [[0, 0, 0, 0], [0, 4, 1, 3], - [0, 7, 6, 8]]) < eps) + [0, 7, 6, 8]]) def test_zoom1(self): "zoom 1" @@ -2139,55 +2146,55 @@ for order in range(0, 6): out = ndimage.affine_transform(data, [0.5, 0.5], 0, (6, 8), order=order) - self.assertTrue(diff(out[::2, ::2], data) < eps) + assert_array_almost_equal(out[::2, ::2], data) def test_rotate01(self): "rotate 1" data = numpy.array([[0, 0, 0, 0], [0, 1, 1, 0], - [0, 0, 0, 0]], dtype = numpy.float64) + [0, 0, 0, 0]], dtype=numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 0) - self.assertTrue(diff(out, data) < eps) + assert_array_almost_equal(out, data) def test_rotate02(self): "rotate 2" data = numpy.array([[0, 0, 0, 0], [0, 1, 0, 0], - [0, 0, 0, 0]], dtype = numpy.float64) - true = numpy.array([[0, 0, 0], + [0, 0, 0, 0]], dtype=numpy.float64) + expected = numpy.array([[0, 0, 0], [0, 0, 0], [0, 1, 0], - [0, 0, 0]], dtype = numpy.float64) + [0, 0, 0]], dtype=numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 90) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_rotate03(self): "rotate 3" data = numpy.array([[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], - [0, 0, 0, 0, 0]], dtype = numpy.float64) - true = numpy.array([[0, 0, 0], + [0, 0, 0, 0, 0]], dtype=numpy.float64) + expected = numpy.array([[0, 0, 0], [0, 0, 0], [0, 1, 0], [0, 1, 0], - [0, 0, 0]], dtype = numpy.float64) + [0, 0, 0]], dtype=numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 90) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_rotate04(self): "rotate 4" data = numpy.array([[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], - [0, 0, 0, 0, 0]], dtype = numpy.float64) - true = numpy.array([[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]], dtype=numpy.float64) + expected = numpy.array([[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], - [0, 0, 1, 0, 0]], dtype = numpy.float64) + [0, 0, 1, 0, 0]], dtype=numpy.float64) for order in range(0, 6): - out = ndimage.rotate(data, 90, reshape = False) - self.assertTrue(diff(out, true) < eps) + out = ndimage.rotate(data, 90, reshape=False) + assert_array_almost_equal(out, expected) def test_rotate05(self): "rotate 5" @@ -2196,16 +2203,16 @@ data[:,:,i] = numpy.array([[0,0,0], [0,1,0], [0,1,0], - [0,0,0]], dtype = numpy.float64) + [0,0,0]], dtype=numpy.float64) - true = numpy.array([[0,0,0,0], + expected = numpy.array([[0,0,0,0], [0,1,1,0], - [0,0,0,0]], dtype = numpy.float64) + [0,0,0,0]], dtype=numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 90) for i in range(3): - self.assertTrue(diff(out[:,:,i], true) < eps) + assert_array_almost_equal(out[:,:,i], expected) def test_rotate06(self): "rotate 6" @@ -2213,52 +2220,52 @@ for i in range(3): data[:,:,i] = numpy.array([[0,0,0,0], [0,1,1,0], - [0,0,0,0]], dtype = numpy.float64) + [0,0,0,0]], dtype=numpy.float64) - true = numpy.array([[0,0,0], + expected = numpy.array([[0,0,0], [0,1,0], [0,1,0], - [0,0,0]], dtype = numpy.float64) + [0,0,0]], dtype=numpy.float64) for order in range(0, 6): out = ndimage.rotate(data, 90) for i in range(3): - self.assertTrue(diff(out[:,:,i], true) < eps) + assert_array_almost_equal(out[:,:,i], expected) def test_rotate07(self): "rotate 7" data = numpy.array([[[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0]]] * 2, - dtype = numpy.float64) + dtype=numpy.float64) data = data.transpose() - true = numpy.array([[[0, 0, 0], + expected = numpy.array([[[0, 0, 0], [0, 1, 0], [0, 1, 0], [0, 0, 0], - [0, 0, 0]]] * 2, dtype = numpy.float64) - true = true.transpose([2,1,0]) + [0, 0, 0]]] * 2, dtype=numpy.float64) + expected = expected.transpose([2,1,0]) for order in range(0, 6): - out = ndimage.rotate(data, 90, axes = (0, 1)) - self.assertTrue(diff(out, true) < eps) + out = ndimage.rotate(data, 90, axes=(0, 1)) + assert_array_almost_equal(out, expected) def test_rotate08(self): "rotate 8" data = numpy.array([[[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0]]] * 2, - dtype = numpy.float64) + dtype=numpy.float64) data = data.transpose() - true = numpy.array([[[0, 0, 1, 0, 0], + expected = numpy.array([[[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]] * 2, - dtype = numpy.float64) - true = true.transpose() + dtype=numpy.float64) + expected = expected.transpose() for order in range(0, 6): - out = ndimage.rotate(data, 90, axes = (0, 1), - reshape = False) - self.assertTrue(diff(out, true) < eps) + out = ndimage.rotate(data, 90, axes=(0, 1), + reshape=False) + assert_array_almost_equal(out, expected) def test_watershed_ift01(self): "watershed_ift 1" @@ -2280,18 +2287,18 @@ [ 0, 0, 0, 0, 0, 0, 0]], numpy.int8) out = ndimage.watershed_ift(data, markers, - structure = [[1,1,1], - [1,1,1], - [1,1,1]]) - error = diff([[-1, -1, -1, -1, -1, -1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, -1, -1, -1, -1, -1, -1], - [-1, -1, -1, -1, -1, -1, -1]], out) - self.assertTrue(error < eps) + structure=[[1,1,1], + [1,1,1], + [1,1,1]]) + expected = [[-1, -1, -1, -1, -1, -1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1]] + assert_array_almost_equal(out, expected) def test_watershed_ift02(self): "watershed_ift 2" @@ -2313,15 +2320,15 @@ [ 0, 0, 0, 0, 0, 0, 0]], numpy.int8) out = ndimage.watershed_ift(data, markers) - error = diff([[-1, -1, -1, -1, -1, -1, -1], - [-1, -1, 1, 1, 1, -1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, -1, 1, 1, 1, -1, -1], - [-1, -1, -1, -1, -1, -1, -1], - [-1, -1, -1, -1, -1, -1, -1]], out) - self.assertTrue(error < eps) + expected = [[-1, -1, -1, -1, -1, -1, -1], + [-1, -1, 1, 1, 1, -1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, -1, 1, 1, 1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1]] + assert_array_almost_equal(out, expected) def test_watershed_ift03(self): "watershed_ift 3" @@ -2341,14 +2348,14 @@ [ 0, 0, 0, 0, 0, 0, -1]], numpy.int8) out = ndimage.watershed_ift(data, markers) - error = diff([[-1, -1, -1, -1, -1, -1, -1], - [-1, -1, 2, -1, 3, -1, -1], - [-1, 2, 2, 3, 3, 3, -1], - [-1, 2, 2, 3, 3, 3, -1], - [-1, 2, 2, 3, 3, 3, -1], - [-1, -1, 2, -1, 3, -1, -1], - [-1, -1, -1, -1, -1, -1, -1]], out) - self.assertTrue(error < eps) + expected = [[-1, -1, -1, -1, -1, -1, -1], + [-1, -1, 2, -1, 3, -1, -1], + [-1, 2, 2, 3, 3, 3, -1], + [-1, 2, 2, 3, 3, 3, -1], + [-1, 2, 2, 3, 3, 3, -1], + [-1, -1, 2, -1, 3, -1, -1], + [-1, -1, -1, -1, -1, -1, -1]] + assert_array_almost_equal(out, expected) def test_watershed_ift04(self): "watershed_ift 4" @@ -2360,55 +2367,55 @@ [0, 1, 1, 1, 1, 1, 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]], - numpy.int8) + [ 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]], + numpy.int8) out = ndimage.watershed_ift(data, markers, - structure = [[1,1,1], - [1,1,1], - [1,1,1]]) - error = diff([[-1, -1, -1, -1, -1, -1, -1], - [-1, 2, 2, 3, 3, 3, -1], - [-1, 2, 2, 3, 3, 3, -1], - [-1, 2, 2, 3, 3, 3, -1], - [-1, 2, 2, 3, 3, 3, -1], - [-1, 2, 2, 3, 3, 3, -1], - [-1, -1, -1, -1, -1, -1, -1]], out) - self.assertTrue(error < eps) + structure=[[1,1,1], + [1,1,1], + [1,1,1]]) + expected = [[-1, -1, -1, -1, -1, -1, -1], + [-1, 2, 2, 3, 3, 3, -1], + [-1, 2, 2, 3, 3, 3, -1], + [-1, 2, 2, 3, 3, 3, -1], + [-1, 2, 2, 3, 3, 3, -1], + [-1, 2, 2, 3, 3, 3, -1], + [-1, -1, -1, -1, -1, -1, -1]] + assert_array_almost_equal(out, expected) def test_watershed_ift05(self): "watershed_ift 5" 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]], numpy.uint8) + [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]], 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]], - numpy.int8) + [ 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]], + numpy.int8) out = ndimage.watershed_ift(data, markers, - structure = [[1,1,1], - [1,1,1], - [1,1,1]]) - error = diff([[-1, -1, -1, -1, -1, -1, -1], - [-1, 3, 3, 2, 2, 2, -1], - [-1, 3, 3, 2, 2, 2, -1], - [-1, 3, 3, 2, 2, 2, -1], - [-1, 3, 3, 2, 2, 2, -1], - [-1, 3, 3, 2, 2, 2, -1], - [-1, -1, -1, -1, -1, -1, -1]], out) - self.assertTrue(error < eps) + structure=[[1,1,1], + [1,1,1], + [1,1,1]]) + expected = [[-1, -1, -1, -1, -1, -1, -1], + [-1, 3, 3, 2, 2, 2, -1], + [-1, 3, 3, 2, 2, 2, -1], + [-1, 3, 3, 2, 2, 2, -1], + [-1, 3, 3, 2, 2, 2, -1], + [-1, 3, 3, 2, 2, 2, -1], + [-1, -1, -1, -1, -1, -1, -1]] + assert_array_almost_equal(out, expected) def test_watershed_ift06(self): "watershed_ift 6" @@ -2426,21 +2433,21 @@ [ 0, 0, 0, 0, 0, 0, 0]], numpy.int8) out = ndimage.watershed_ift(data, markers, - structure = [[1,1,1], - [1,1,1], - [1,1,1]]) - error = diff([[-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, -1, -1, -1, -1, -1, -1], - [-1, -1, -1, -1, -1, -1, -1]], out) - self.assertTrue(error < eps) + structure=[[1,1,1], + [1,1,1], + [1,1,1]]) + expected = [[-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1]] + assert_array_almost_equal(out, expected) def test_watershed_ift07(self): "watershed_ift 7" shape = (7, 6) - data = numpy.zeros(shape, dtype = numpy.uint8) + data = numpy.zeros(shape, dtype=numpy.uint8) data = data.transpose() data[...] = numpy.array([[0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], @@ -2458,53 +2465,53 @@ out = numpy.zeros(shape, dtype = numpy.int16) out = out.transpose() ndimage.watershed_ift(data, markers, - structure = [[1,1,1], - [1,1,1], - [1,1,1]], - output = out) - error = diff([[-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, 1, 1, 1, 1, 1, -1], - [-1, -1, -1, -1, -1, -1, -1], - [-1, -1, -1, -1, -1, -1, -1]], out) - self.assertTrue(error < eps) + structure=[[1,1,1], + [1,1,1], + [1,1,1]], + output=out) + expected = [[-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, 1, 1, 1, 1, 1, -1], + [-1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1]] + assert_array_almost_equal(out, expected) def test_label01(self): "label 1" data = numpy.ones([]) out, n = ndimage.label(data) - self.assertTrue(diff(out, 1) < eps and n == 1) + assert_array_almost_equal(out, 1) def test_label02(self): "label 2" data = numpy.zeros([]) out, n = ndimage.label(data) - self.assertTrue(diff(out, 0) < eps and n == 0) + assert_array_almost_equal(out, 0) def test_label03(self): "label 3" data = numpy.ones([1]) out, n = ndimage.label(data) - self.assertTrue(diff(out, [1]) < eps and n == 1) + assert_array_almost_equal(out, [1]) def test_label04(self): "label 4" data = numpy.zeros([1]) out, n = ndimage.label(data) - self.assertTrue(diff(out, [0]) < eps and n == 0) + assert_array_almost_equal(out, [0]) def test_label05(self): "label 5" data = numpy.ones([5]) out, n = ndimage.label(data) - self.assertTrue(diff(out, [1, 1, 1, 1, 1]) < eps and n == 1) + assert_array_almost_equal(out, [1, 1, 1, 1, 1]) def test_label06(self): "label 6" data = numpy.array([1, 0, 1, 1, 0, 1]) out, n = ndimage.label(data) - self.assertTrue(diff(out, [1, 0, 2, 2, 0, 3]) < eps and n == 3) + assert_array_almost_equal(out, [1, 0, 2, 2, 0, 3]) def test_label07(self): "label 7" @@ -2515,12 +2522,12 @@ [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]) out, n = ndimage.label(data) - self.assertTrue(diff(out, [[0, 0, 0, 0, 0, 0], + assert_array_almost_equal(out, [[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, 0, 0, 0, 0, 0]]) < eps and n == 0) + [0, 0, 0, 0, 0, 0]]) def test_label08(self): "label 8" @@ -2531,12 +2538,12 @@ [1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0]]) out, n = ndimage.label(data) - self.assertTrue(diff(out, [[1, 0, 0, 0, 0, 0], + assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0], [0, 0, 2, 2, 0, 0], [0, 0, 2, 2, 2, 0], [3, 3, 0, 0, 0, 0], [3, 3, 0, 0, 0, 0], - [0, 0, 0, 4, 4, 0]]) < eps and n == 4) + [0, 0, 0, 4, 4, 0]]) def test_label09(self): "label 9" @@ -2548,12 +2555,12 @@ [0, 0, 0, 1, 1, 0]]) struct = ndimage.generate_binary_structure(2, 2) out, n = ndimage.label(data, struct) - self.assertTrue(diff(out, [[1, 0, 0, 0, 0, 0], + assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0], [0, 0, 2, 2, 0, 0], [0, 0, 2, 2, 2, 0], [2, 2, 0, 0, 0, 0], [2, 2, 0, 0, 0, 0], - [0, 0, 0, 3, 3, 0]]) < eps and n == 3) + [0, 0, 0, 3, 3, 0]]) def test_label10(self): "label 10" @@ -2563,10 +2570,10 @@ [0, 0, 0, 0, 0, 0]]) struct = ndimage.generate_binary_structure(2, 2) out, n = ndimage.label(data, struct) - self.assertTrue(diff(out, [[0, 0, 0, 0, 0, 0], + assert_array_almost_equal(out, [[0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 0], [0, 1, 1, 1, 1, 0], - [0, 0, 0, 0, 0, 0]]) < eps and n == 1) + [0, 0, 0, 0, 0, 0]]) def test_label11(self): "label 11" @@ -2578,13 +2585,14 @@ [1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0]], type) out, n = ndimage.label(data) - error = diff(out, [[1, 0, 0, 0, 0, 0], - [0, 0, 2, 2, 0, 0], - [0, 0, 2, 2, 2, 0], - [3, 3, 0, 0, 0, 0], - [3, 3, 0, 0, 0, 0], - [0, 0, 0, 4, 4, 0]]) - self.assertTrue(error < eps and n == 4) + expected = [[1, 0, 0, 0, 0, 0], + [0, 0, 2, 2, 0, 0], + [0, 0, 2, 2, 2, 0], + [3, 3, 0, 0, 0, 0], + [3, 3, 0, 0, 0, 0], + [0, 0, 0, 4, 4, 0]] + assert_array_almost_equal(out, expected) + assert_equal(n, 4) def test_label12(self): "label 12" @@ -2595,12 +2603,13 @@ [0, 0, 1, 1, 1, 1], [0, 0, 0, 1, 1, 0]], type) out, n = ndimage.label(data) - error = diff(out, [[0, 0, 0, 0, 1, 1], - [0, 0, 0, 0, 0, 1], - [0, 0, 1, 0, 1, 1], - [0, 0, 1, 1, 1, 1], - [0, 0, 0, 1, 1, 0]]) - self.assertTrue(error < eps and n == 1) + expected = [[0, 0, 0, 0, 1, 1], + [0, 0, 0, 0, 0, 1], + [0, 0, 1, 0, 1, 1], + [0, 0, 1, 1, 1, 1], + [0, 0, 0, 1, 1, 0]] + assert_array_almost_equal(out, expected) + assert_equal(n, 1) def test_label13(self): "label 13" @@ -2611,60 +2620,61 @@ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], type) out, n = ndimage.label(data) - error = diff(out, [[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]]) - self.assertTrue(error < eps and n == 1) + expected = [[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]] + assert_array_almost_equal(out, expected) + assert_equal(n, 1) def test_find_objects01(self): "find_objects 1" data = numpy.ones([], dtype=int) out = ndimage.find_objects(data) - self.assertTrue(out == [()]) + assert_(out == [()]) def test_find_objects02(self): "find_objects 2" data = numpy.zeros([], dtype=int) out = ndimage.find_objects(data) - self.assertTrue(out == []) + assert_(out == []) def test_find_objects03(self): "find_objects 3" data = numpy.ones([1], dtype=int) out = ndimage.find_objects(data) - self.assertTrue(out == [(slice(0, 1, None),)]) + assert_equal(out, [(slice(0, 1, None),)]) def test_find_objects04(self): "find_objects 4" data = numpy.zeros([1], dtype=int) out = ndimage.find_objects(data) - self.assertTrue(out == []) + assert_equal(out, []) def test_find_objects05(self): "find_objects 5" data = numpy.ones([5], dtype=int) out = ndimage.find_objects(data) - self.assertTrue(out == [(slice(0, 5, None),)]) + assert_equal(out, [(slice(0, 5, None),)]) def test_find_objects06(self): "find_objects 6" data = numpy.array([1, 0, 2, 2, 0, 3]) out = ndimage.find_objects(data) - self.assertTrue(out == [(slice(0, 1, None),), - (slice(2, 4, None),), - (slice(5, 6, None),)]) + assert_equal(out, [(slice(0, 1, None),), + (slice(2, 4, None),), + (slice(5, 6, None),)]) def test_find_objects07(self): "find_objects 7" 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], - [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, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0]]) out = ndimage.find_objects(data) - self.assertTrue(out == []), + assert_equal(out, []) def test_find_objects08(self): "find_objects 8" @@ -2675,10 +2685,10 @@ [3, 3, 0, 0, 0, 0], [0, 0, 0, 4, 4, 0]]) out = ndimage.find_objects(data) - self.assertTrue(out == [(slice(0, 1, None), slice(0, 1, None)), - (slice(1, 3, None), slice(2, 5, None)), - (slice(3, 5, None), slice(0, 2, None)), - (slice(5, 6, None), slice(3, 5, None))]) + assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)), + (slice(1, 3, None), slice(2, 5, None)), + (slice(3, 5, None), slice(0, 2, None)), + (slice(5, 6, None), slice(3, 5, None))]) def test_find_objects09(self): "find_objects 9" @@ -2689,202 +2699,202 @@ [0, 0, 0, 0, 0, 0], [0, 0, 0, 4, 4, 0]]) out = ndimage.find_objects(data) - self.assertTrue(out == [(slice(0, 1, None), slice(0, 1, None)), - (slice(1, 3, None), slice(2, 5, None)), - None, - (slice(5, 6, None), slice(3, 5, None))]) + assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)), + (slice(1, 3, None), slice(2, 5, None)), + None, + (slice(5, 6, None), slice(3, 5, None))]) def test_sum01(self): "sum 1" for type in self.types: input = numpy.array([], type) output = ndimage.sum(input) - self.assertTrue(output == 0.0) + assert_equal(output, 0.0) def test_sum02(self): "sum 2" for type in self.types: input = numpy.zeros([0, 4], type) output = ndimage.sum(input) - self.assertTrue(output == 0.0) + assert_equal(output, 0.0) def test_sum03(self): "sum 3" for type in self.types: input = numpy.ones([], type) output = ndimage.sum(input) - self.assertTrue(output == 1.0) + assert_almost_equal(output, 1.0) def test_sum04(self): "sum 4" for type in self.types: input = numpy.array([1, 2], type) output = ndimage.sum(input) - self.assertTrue(output == 3.0) + assert_almost_equal(output, 3.0) def test_sum05(self): "sum 5" for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.sum(input) - self.assertTrue(output == 10.0) + assert_almost_equal(output, 10.0) def test_sum06(self): "sum 6" labels = numpy.array([], bool) for type in self.types: input = numpy.array([], type) - output = ndimage.sum(input, labels = labels) - self.assertTrue(output == 0.0) + output = ndimage.sum(input, labels=labels) + assert_equal(output, 0.0) def test_sum07(self): "sum 7" labels = numpy.ones([0, 4], bool) for type in self.types: input = numpy.zeros([0, 4], type) - output = ndimage.sum(input, labels = labels) - self.assertTrue(output == 0.0) + output = ndimage.sum(input, labels=labels) + assert_equal(output, 0.0) def test_sum08(self): "sum 8" labels = numpy.array([1, 0], bool) for type in self.types: input = numpy.array([1, 2], type) - output = ndimage.sum(input, labels = labels) - self.assertTrue(output == 1.0) + output = ndimage.sum(input, labels=labels) + assert_equal(output, 1.0) def test_sum09(self): "sum 9" labels = numpy.array([1, 0], bool) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.sum(input, labels = labels) - self.assertTrue(output == 4.0) + output = ndimage.sum(input, labels=labels) + assert_almost_equal(output, 4.0) def test_sum10(self): "sum 10" labels = numpy.array([1, 0], bool) input = numpy.array([[1, 2], [3, 4]], bool) - output = ndimage.sum(input, labels = labels) - self.assertTrue(output == 2.0) + output = ndimage.sum(input, labels=labels) + assert_almost_equal(output, 2.0) def test_sum11(self): "sum 11" labels = numpy.array([1, 2], numpy.int8) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.sum(input, labels = labels, - index = 2) - self.assertTrue(output == 6.0) + output = ndimage.sum(input, labels=labels, + index=2) + assert_almost_equal(output, 6.0) def test_sum12(self): "sum 12" labels = numpy.array([[1, 2], [2, 4]], numpy.int8) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.sum(input, labels = labels, - index = [4, 8, 2]) - self.assertTrue(numpy.all(output == [4.0, 0.0, 5.0])) + output = ndimage.sum(input, labels=labels, + index=[4, 8, 2]) + assert_array_almost_equal(output, [4.0, 0.0, 5.0]) def test_mean01(self): "mean 1" labels = numpy.array([1, 0], bool) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.mean(input, labels = labels) - self.assertTrue(output == 2.0) + output = ndimage.mean(input, labels=labels) + assert_almost_equal(output, 2.0) def test_mean02(self): "mean 2" labels = numpy.array([1, 0], bool) input = numpy.array([[1, 2], [3, 4]], bool) - output = ndimage.mean(input, labels = labels) - self.assertTrue(output == 1.0) + output = ndimage.mean(input, labels=labels) + assert_almost_equal(output, 1.0) def test_mean03(self): "mean 3" labels = numpy.array([1, 2]) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.mean(input, labels = labels, - index = 2) - self.assertTrue(output == 3.0) + output = ndimage.mean(input, labels=labels, + index=2) + assert_almost_equal(output, 3.0) def test_mean04(self): "mean 4" labels = numpy.array([[1, 2], [2, 4]], numpy.int8) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.mean(input, labels = labels, - index = [4, 8, 2]) - self.assertTrue(numpy.all(output[[0,2]] == [4.0, 2.5]) and - numpy.isnan(output[1])) + output = ndimage.mean(input, labels=labels, + index=[4, 8, 2]) + assert_array_almost_equal(output[[0,2]], [4.0, 2.5]) + assert_(numpy.isnan(output[1])) def test_minimum01(self): "minimum 1" labels = numpy.array([1, 0], bool) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.minimum(input, labels = labels) - self.assertTrue(output == 1.0) + output = ndimage.minimum(input, labels=labels) + assert_almost_equal(output, 1.0) def test_minimum02(self): "minimum 2" labels = numpy.array([1, 0], bool) input = numpy.array([[2, 2], [2, 4]], bool) - output = ndimage.minimum(input, labels = labels) - self.assertTrue(output == 1.0) + output = ndimage.minimum(input, labels=labels) + assert_almost_equal(output, 1.0) def test_minimum03(self): "minimum 3" labels = numpy.array([1, 2]) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.minimum(input, labels = labels, - index = 2) - self.assertTrue(output == 2.0) + output = ndimage.minimum(input, labels=labels, + index=2) + assert_almost_equal(output, 2.0) def test_minimum04(self): "minimum 4" labels = numpy.array([[1, 2], [2, 3]]) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.minimum(input, labels = labels, - index = [2, 3, 8]) - self.assertTrue(numpy.all(output == [2.0, 4.0, 0.0])) + output = ndimage.minimum(input, labels=labels, + index=[2, 3, 8]) + assert_array_almost_equal(output, [2.0, 4.0, 0.0]) def test_maximum01(self): "maximum 1" labels = numpy.array([1, 0], bool) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.maximum(input, labels = labels) - self.assertTrue(output == 3.0) + output = ndimage.maximum(input, labels=labels) + assert_almost_equal(output, 3.0) def test_maximum02(self): "maximum 2" labels = numpy.array([1, 0], bool) input = numpy.array([[2, 2], [2, 4]], bool) - output = ndimage.maximum(input, labels = labels) - self.assertTrue(output == 1.0) + output = ndimage.maximum(input, labels=labels) + assert_almost_equal(output, 1.0) def test_maximum03(self): "maximum 3" labels = numpy.array([1, 2]) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.maximum(input, labels = labels, - index = 2) - self.assertTrue(output == 4.0) + output = ndimage.maximum(input, labels=labels, + index=2) + assert_almost_equal(output, 4.0) def test_maximum04(self): "maximum 4" labels = numpy.array([[1, 2], [2, 3]]) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.maximum(input, labels = labels, - index = [2, 3, 8]) - self.assertTrue(numpy.all(output == [3.0, 4.0, 0.0])) + output = ndimage.maximum(input, labels=labels, + index=[2, 3, 8]) + assert_array_almost_equal(output, [3.0, 4.0, 0.0]) def test_maximum05(self): "Ticket #501" @@ -2896,27 +2906,27 @@ for type in self.types: input = numpy.array([], type) output = ndimage.variance(input) - self.assertTrue(numpy.isnan(output)) + assert_(numpy.isnan(output)) def test_variance02(self): "variance 2" for type in self.types: input = numpy.array([1], type) output = ndimage.variance(input) - self.assertTrue(float(output) == 0.0) + assert_almost_equal(output, 0.0) def test_variance03(self): "variance 3" for type in self.types: input = numpy.array([1, 3], type) output = ndimage.variance(input) - self.assertTrue(output == 1.0) + assert_almost_equal(output, 1.0) def test_variance04(self): "variance 4" input = numpy.array([1, 0], bool) output = ndimage.variance(input) - self.assertTrue(output == 0.25) + assert_almost_equal(output, 0.25) def test_variance05(self): "variance 5" @@ -2924,7 +2934,7 @@ for type in self.types: input = numpy.array([1, 3, 8], type) output = ndimage.variance(input, labels, 2) - self.assertTrue(output == 1.0) + assert_almost_equal(output, 1.0) def test_variance06(self): "variance 6" @@ -2932,34 +2942,34 @@ for type in self.types: input = numpy.array([1, 3, 8, 10, 8], type) output = ndimage.variance(input, labels, [2, 3, 4]) - self.assertTrue(numpy.all(output == [1.0, 1.0, 0.0])) + assert_array_almost_equal(output, [1.0, 1.0, 0.0]) def test_standard_deviation01(self): "standard deviation 1" for type in self.types: input = numpy.array([], type) output = ndimage.standard_deviation(input) - self.assertTrue(numpy.isnan(output)) + assert_(numpy.isnan(output)) def test_standard_deviation02(self): "standard deviation 2" for type in self.types: input = numpy.array([1], type) output = ndimage.standard_deviation(input) - self.assertTrue(float(output) == 0.0) + assert_almost_equal(output, 0.0) def test_standard_deviation03(self): "standard deviation 3" for type in self.types: input = numpy.array([1, 3], type) output = ndimage.standard_deviation(input) - self.assertTrue(output == math.sqrt(1.0)) + assert_almost_equal(output, math.sqrt(1.0)) def test_standard_deviation04(self): "standard deviation 4" input = numpy.array([1, 0], bool) output = ndimage.standard_deviation(input) - self.assertTrue(output == 0.5) + assert_almost_equal(output, 0.5) def test_standard_deviation05(self): "standard deviation 5" @@ -2967,7 +2977,7 @@ for type in self.types: input = numpy.array([1, 3, 8], type) output = ndimage.standard_deviation(input, labels, 2) - self.assertTrue(output == 1.0) + assert_almost_equal(output, 1.0) def test_standard_deviation06(self): "standard deviation 6" @@ -2976,7 +2986,7 @@ input = numpy.array([1, 3, 8, 10, 8], type) output = ndimage.standard_deviation(input, labels, [2, 3, 4]) - self.assertTrue(np.all(output == [1.0, 1.0, 0.0])) + assert_array_almost_equal(output, [1.0, 1.0, 0.0]) def test_minimum_position01(self): "minimum position 1" @@ -2984,8 +2994,8 @@ for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.minimum_position(input, - labels = labels) - self.assertTrue(output == (0, 0)) + labels=labels) + assert_equal(output, (0, 0)) def test_minimum_position02(self): "minimum position 2" @@ -2994,7 +3004,7 @@ [3, 7, 0, 2], [1, 5, 1, 1]], type) output = ndimage.minimum_position(input) - self.assertTrue(output == (1, 2)) + assert_equal(output, (1, 2)) def test_minimum_position03(self): "minimum position 3" @@ -3002,7 +3012,7 @@ [3, 7, 0, 2], [1, 5, 1, 1]], bool) output = ndimage.minimum_position(input) - self.assertTrue(output == (1, 2)) + assert_equal(output, (1, 2)) def test_minimum_position04(self): "minimum position 4" @@ -3010,7 +3020,7 @@ [3, 7, 1, 2], [1, 5, 1, 1]], bool) output = ndimage.minimum_position(input) - self.assertTrue(output == (0, 0)) + assert_equal(output, (0, 0)) def test_minimum_position05(self): "minimum position 5" @@ -3020,7 +3030,7 @@ [3, 7, 0, 2], [1, 5, 2, 3]], type) output = ndimage.minimum_position(input, labels) - self.assertTrue(output == (2, 0)) + assert_equal(output, (2, 0)) def test_minimum_position06(self): "minimum position 6" @@ -3030,7 +3040,7 @@ [3, 7, 0, 2], [1, 5, 1, 1]], type) output = ndimage.minimum_position(input, labels, 2) - self.assertTrue(output == (0, 1)) + assert_equal(output, (0, 1)) def test_minimum_position07(self): "minimum position 7" @@ -3041,7 +3051,8 @@ [1, 5, 1, 1]], type) output = ndimage.minimum_position(input, labels, [2, 3]) - self.assertTrue(output[0] == (0, 1) and output[1] == (1, 2)) + assert_equal(output[0], (0, 1)) + assert_equal(output[1], (1, 2)) def test_maximum_position01(self): "maximum position 1" @@ -3049,8 +3060,8 @@ for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) output = ndimage.maximum_position(input, - labels = labels) - self.assertTrue(output == (1, 0)) + labels=labels) + assert_equal(output, (1, 0)) def test_maximum_position02(self): "maximum position 2" @@ -3059,7 +3070,7 @@ [3, 7, 8, 2], [1, 5, 1, 1]], type) output = ndimage.maximum_position(input) - self.assertTrue(output == (1, 2)) + assert_equal(output, (1, 2)) def test_maximum_position03(self): "maximum position 3" @@ -3067,7 +3078,7 @@ [3, 7, 8, 2], [1, 5, 1, 1]], bool) output = ndimage.maximum_position(input) - self.assertTrue(output == (0, 0)) + assert_equal(output, (0, 0)) def test_maximum_position04(self): "maximum position 4" @@ -3077,7 +3088,7 @@ [3, 7, 8, 2], [1, 5, 1, 1]], type) output = ndimage.maximum_position(input, labels) - self.assertTrue(output == (1, 1)) + assert_equal(output, (1, 1)) def test_maximum_position05(self): "maximum position 5" @@ -3087,7 +3098,7 @@ [3, 7, 8, 2], [1, 5, 1, 1]], type) output = ndimage.maximum_position(input, labels, 1) - self.assertTrue(output == (0, 0)) + assert_equal(output, (0, 0)) def test_maximum_position06(self): "maximum position 6" @@ -3098,60 +3109,59 @@ [1, 5, 1, 1]], type) output = ndimage.maximum_position(input, labels, [1, 2]) - self.assertTrue(output[0] == (0, 0) and output[1] == (1, 1)) + assert_equal(output[0], (0, 0)) + assert_equal(output[1], (1, 1)) def test_extrema01(self): "extrema 1" labels = numpy.array([1, 0], bool) for type in self.types: 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) + output1 = ndimage.extrema(input, labels=labels) + output2 = ndimage.minimum(input, labels=labels) + output3 = ndimage.maximum(input, labels=labels) output4 = ndimage.minimum_position(input, - labels = labels) + labels=labels) output5 = ndimage.maximum_position(input, - labels = labels) - self.assertTrue(output1 == (output2, output3, output4, - output5)) + labels=labels) + assert_equal(output1, (output2, output3, output4, output5)) def test_extrema02(self): "extrema 2" labels = numpy.array([1, 2]) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output1 = ndimage.extrema(input, labels = labels, - index = 2) - output2 = ndimage.minimum(input, labels = labels, - index = 2) - output3 = ndimage.maximum(input, labels = labels, - index = 2) + output1 = ndimage.extrema(input, labels=labels, + index=2) + output2 = ndimage.minimum(input, labels=labels, + index=2) + output3 = ndimage.maximum(input, labels=labels, + index=2) output4 = ndimage.minimum_position(input, - labels = labels, index = 2) + labels=labels, index=2) output5 = ndimage.maximum_position(input, - labels = labels, index = 2) - self.assertTrue(output1 == (output2, output3, output4, - output5)) + labels=labels, index=2) + assert_equal(output1, (output2, output3, output4, output5)) def test_extrema03(self): "extrema 3" labels = numpy.array([[1, 2], [2, 3]]) for type in self.types: input = numpy.array([[1, 2], [3, 4]], type) - output1 = ndimage.extrema(input, labels = labels, - index = [2, 3, 8]) - output2 = ndimage.minimum(input, labels = labels, - index = [2, 3, 8]) - output3 = ndimage.maximum(input, labels = labels, - index = [2, 3, 8]) + output1 = ndimage.extrema(input, labels=labels, + index=[2, 3, 8]) + output2 = ndimage.minimum(input, labels=labels, + index=[2, 3, 8]) + output3 = ndimage.maximum(input, labels=labels, + index=[2, 3, 8]) output4 = ndimage.minimum_position(input, - labels = labels, index = [2, 3, 8]) + labels=labels, index=[2, 3, 8]) output5 = ndimage.maximum_position(input, - labels = labels, index = [2, 3, 8]) - self.assertTrue(numpy.all(output1[0] == output2)) - self.assertTrue(numpy.all(output1[1] == output3)) - self.assertTrue(numpy.all(output1[2] == output4)) - self.assertTrue(numpy.all(output1[3] == output5)) + labels=labels, index=[2, 3, 8]) + assert_array_almost_equal(output1[0], output2) + assert_array_almost_equal(output1[1], output3) + assert_array_almost_equal(output1[2], output4) + assert_array_almost_equal(output1[3], output5) def test_extrema04(self): "extrema 4" @@ -3167,164 +3177,153 @@ [1, 2]) output5 = ndimage.maximum_position(input, labels, [1, 2]) - self.assertTrue(numpy.all(output1[0] == output2)) - self.assertTrue(numpy.all(output1[1] == output3)) - self.assertTrue(numpy.all(output1[2] == output4)) - self.assertTrue(numpy.all(output1[3] == output5)) + assert_array_almost_equal(output1[0], output2) + assert_array_almost_equal(output1[1], output3) + assert_array_almost_equal(output1[2], output4) + assert_array_almost_equal(output1[3], output5) def test_center_of_mass01(self): "center of mass 1" - true = [0.0, 0.0] + expected = [0.0, 0.0] for type in self.types: input = numpy.array([[1, 0], [0, 0]], type) output = ndimage.center_of_mass(input) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_center_of_mass02(self): "center of mass 2" - true = [1, 0] + expected = [1, 0] for type in self.types: input = numpy.array([[0, 0], [1, 0]], type) output = ndimage.center_of_mass(input) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_center_of_mass03(self): "center of mass 3" - true = [0, 1] + expected = [0, 1] for type in self.types: input = numpy.array([[0, 1], [0, 0]], type) output = ndimage.center_of_mass(input) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_center_of_mass04(self): "center of mass 4" - true = [1, 1] + expected = [1, 1] for type in self.types: input = numpy.array([[0, 0], [0, 1]], type) output = ndimage.center_of_mass(input) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_center_of_mass05(self): "center of mass 5" - true = [0.5, 0.5] + expected = [0.5, 0.5] for type in self.types: input = numpy.array([[1, 1], [1, 1]], type) output = ndimage.center_of_mass(input) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_center_of_mass06(self): "center of mass 6" - true = [0.5, 0.5] + expected = [0.5, 0.5] input = numpy.array([[1, 2], [3, 1]], bool) output = ndimage.center_of_mass(input) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_center_of_mass07(self): "center of mass 7" labels = [1, 0] - true = [0.5, 0.0] + expected = [0.5, 0.0] input = numpy.array([[1, 2], [3, 1]], bool) output = ndimage.center_of_mass(input, labels) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_center_of_mass08(self): "center of mass 8" labels = [1, 2] - true = [0.5, 1.0] + expected = [0.5, 1.0] input = numpy.array([[5, 2], [3, 1]], bool) output = ndimage.center_of_mass(input, labels, 2) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) - def test_center_of_mass09(self): "center of mass 9" labels = [1, 2] - true = [(0.5, 0.0), (0.5, 1.0)] + expected = [(0.5, 0.0), (0.5, 1.0)] input = numpy.array([[1, 2], [1, 1]], bool) output = ndimage.center_of_mass(input, labels, [1, 2]) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_histogram01(self): "histogram 1" - true = numpy.ones(10) + expected = numpy.ones(10) input = numpy.arange(10) output = ndimage.histogram(input, 0, 10, 10) - e = diff(true, output) - self.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_histogram02(self): "histogram 2" labels = [1, 1, 1, 1, 2, 2, 2, 2] - true = [0, 2, 0, 1, 1] + expected = [0, 2, 0, 1, 1] 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.assertTrue(e < eps) + assert_array_almost_equal(output, expected) def test_histogram03(self): "histogram 3" labels = [1, 0, 1, 1, 2, 2, 2, 2] - true1 = [0, 1, 0, 1, 1] - true2 = [0, 0, 0, 3, 0] + expected1 = [0, 1, 0, 1, 1] + expected2 = [0, 0, 0, 3, 0] 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]) - self.assertTrue(e1 < eps and e2 < eps) + assert_array_almost_equal(output[0], expected1) + assert_array_almost_equal(output[1], expected2) + def test_distance_transform_bf01(self): "brute force distance transform 1" for type in self.types: 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], - [0, 0, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 0, 1, 1, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) - out, ft = ndimage.distance_transform_bf(data, - 'euclidean', return_indices = True) - error1 = diff([[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, 2, 4, 2, 1, 0, 0], - [0, 0, 1, 4, 8, 4, 1, 0, 0], - [0, 0, 1, 2, 4, 2, 1, 0, 0], - [0, 0, 0, 1, 1, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]], - out * out) - error2 = diff([[[0, 0, 0, 0, 0, 0, 0, 0, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1], - [2, 2, 2, 2, 1, 2, 2, 2, 2], - [3, 3, 3, 2, 1, 2, 3, 3, 3], - [4, 4, 4, 4, 6, 4, 4, 4, 4], - [5, 5, 6, 6, 7, 6, 6, 5, 5], - [6, 6, 6, 7, 7, 7, 6, 6, 6], - [7, 7, 7, 7, 7, 7, 7, 7, 7], - [8, 8, 8, 8, 8, 8, 8, 8, 8]], - [[0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 2, 4, 6, 6, 7, 8], - [0, 1, 1, 2, 4, 6, 7, 7, 8], - [0, 1, 1, 1, 6, 7, 7, 7, 8], - [0, 1, 2, 2, 4, 6, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8]]], ft) - self.assertTrue(error1 < eps and error2 < eps) + [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], + [0, 0, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) + out, ft = ndimage.distance_transform_bf(data, 'euclidean', + return_indices=True) + expected = [[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, 2, 4, 2, 1, 0, 0], + [0, 0, 1, 4, 8, 4, 1, 0, 0], + [0, 0, 1, 2, 4, 2, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]] + assert_array_almost_equal(out * out, expected) + expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1], + [2, 2, 2, 2, 1, 2, 2, 2, 2], + [3, 3, 3, 2, 1, 2, 3, 3, 3], + [4, 4, 4, 4, 6, 4, 4, 4, 4], + [5, 5, 6, 6, 7, 6, 6, 5, 5], + [6, 6, 6, 7, 7, 7, 6, 6, 6], + [7, 7, 7, 7, 7, 7, 7, 7, 7], + [8, 8, 8, 8, 8, 8, 8, 8, 8]], + [[0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 2, 4, 6, 6, 7, 8], + [0, 1, 1, 2, 4, 6, 7, 7, 8], + [0, 1, 1, 1, 6, 7, 7, 7, 8], + [0, 1, 2, 2, 4, 6, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8]]] + assert_array_almost_equal(ft, expected) + def test_distance_transform_bf02(self): "brute force distance transform 2" for type in self.types: @@ -3337,130 +3336,136 @@ [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) - out, ft = ndimage.distance_transform_bf(data, - 'cityblock', return_indices = True) - error1 = diff([[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, 2, 2, 2, 1, 0, 0], - [0, 0, 1, 2, 3, 2, 1, 0, 0], - [0, 0, 1, 2, 2, 2, 1, 0, 0], - [0, 0, 0, 1, 1, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]], out) - error2 = diff([[[0, 0, 0, 0, 0, 0, 0, 0, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1], - [2, 2, 2, 2, 1, 2, 2, 2, 2], - [3, 3, 3, 3, 1, 3, 3, 3, 3], - [4, 4, 4, 4, 7, 4, 4, 4, 4], - [5, 5, 6, 7, 7, 7, 6, 5, 5], - [6, 6, 6, 7, 7, 7, 6, 6, 6], - [7, 7, 7, 7, 7, 7, 7, 7, 7], - [8, 8, 8, 8, 8, 8, 8, 8, 8]], - [[0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 2, 4, 6, 6, 7, 8], - [0, 1, 1, 1, 4, 7, 7, 7, 8], - [0, 1, 1, 1, 4, 7, 7, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8]]], ft) - self.assertTrue(error1 < eps and error2 < eps) + out, ft = ndimage.distance_transform_bf(data, 'cityblock', + return_indices=True) + expected = [[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, 2, 2, 2, 1, 0, 0], + [0, 0, 1, 2, 3, 2, 1, 0, 0], + [0, 0, 1, 2, 2, 2, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]] + assert_array_almost_equal(out, expected) + + expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1], + [2, 2, 2, 2, 1, 2, 2, 2, 2], + [3, 3, 3, 3, 1, 3, 3, 3, 3], + [4, 4, 4, 4, 7, 4, 4, 4, 4], + [5, 5, 6, 7, 7, 7, 6, 5, 5], + [6, 6, 6, 7, 7, 7, 6, 6, 6], + [7, 7, 7, 7, 7, 7, 7, 7, 7], + [8, 8, 8, 8, 8, 8, 8, 8, 8]], + [[0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 2, 4, 6, 6, 7, 8], + [0, 1, 1, 1, 4, 7, 7, 7, 8], + [0, 1, 1, 1, 4, 7, 7, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8]]] + assert_array_almost_equal(expected, ft) + def test_distance_transform_bf03(self): "brute force distance transform 3" for type in self.types: 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], - [0, 0, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 0, 1, 1, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) - out, ft = ndimage.distance_transform_bf(data, - 'chessboard', return_indices = True) - error1 = diff([[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, 2, 1, 1, 0, 0], - [0, 0, 1, 2, 2, 2, 1, 0, 0], - [0, 0, 1, 1, 2, 1, 1, 0, 0], - [0, 0, 0, 1, 1, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]], out) - error2 = diff([[[0, 0, 0, 0, 0, 0, 0, 0, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1], - [2, 2, 2, 2, 1, 2, 2, 2, 2], - [3, 3, 4, 2, 2, 2, 4, 3, 3], - [4, 4, 5, 6, 6, 6, 5, 4, 4], - [5, 5, 6, 6, 7, 6, 6, 5, 5], - [6, 6, 6, 7, 7, 7, 6, 6, 6], - [7, 7, 7, 7, 7, 7, 7, 7, 7], - [8, 8, 8, 8, 8, 8, 8, 8, 8]], - [[0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 2, 5, 6, 6, 7, 8], - [0, 1, 1, 2, 6, 6, 7, 7, 8], - [0, 1, 1, 2, 6, 7, 7, 7, 8], - [0, 1, 2, 2, 6, 6, 7, 7, 8], - [0, 1, 2, 4, 5, 6, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8]]], ft) - self.assertTrue(error1 < eps and error2 < eps) + [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], + [0, 0, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) + out, ft = ndimage.distance_transform_bf(data, 'chessboard', + return_indices=True) + expected = [[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, 2, 1, 1, 0, 0], + [0, 0, 1, 2, 2, 2, 1, 0, 0], + [0, 0, 1, 1, 2, 1, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]] + assert_array_almost_equal(out, expected) + + expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1], + [2, 2, 2, 2, 1, 2, 2, 2, 2], + [3, 3, 4, 2, 2, 2, 4, 3, 3], + [4, 4, 5, 6, 6, 6, 5, 4, 4], + [5, 5, 6, 6, 7, 6, 6, 5, 5], + [6, 6, 6, 7, 7, 7, 6, 6, 6], + [7, 7, 7, 7, 7, 7, 7, 7, 7], + [8, 8, 8, 8, 8, 8, 8, 8, 8]], + [[0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 2, 5, 6, 6, 7, 8], + [0, 1, 1, 2, 6, 6, 7, 7, 8], + [0, 1, 1, 2, 6, 7, 7, 7, 8], + [0, 1, 2, 2, 6, 6, 7, 7, 8], + [0, 1, 2, 4, 5, 6, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8]]] + assert_array_almost_equal(ft, expected) + def test_distance_transform_bf04(self): "brute force distance transform 4" for type in self.types: 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], - [0, 0, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 0, 1, 1, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) + [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], + [0, 0, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) tdt, tft = ndimage.distance_transform_bf(data, - return_indices = 1) + return_indices=1) dts = [] fts = [] - dt = numpy.zeros(data.shape, dtype = numpy.float64) - ndimage.distance_transform_bf(data, distances = dt) + 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) + return_distances=False, return_indices=1) fts.append(ft) - ft = numpy.indices(data.shape, dtype = numpy.int32) + ft = numpy.indices(data.shape, dtype=numpy.int32) ndimage.distance_transform_bf(data, - return_distances = False, return_indices = True, indices = ft) + return_distances=False, return_indices=True, indices=ft) fts.append(ft) dt, ft = ndimage.distance_transform_bf(data, - return_indices = 1) + return_indices=1) dts.append(dt) fts.append(ft) - dt = numpy.zeros(data.shape, dtype = numpy.float64) - ft = ndimage.distance_transform_bf(data, distances = dt, - return_indices = True) + 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 = numpy.indices(data.shape, dtype = numpy.int32) + ft = numpy.indices(data.shape, dtype=numpy.int32) dt = ndimage.distance_transform_bf(data, - return_indices = True, indices = ft) + return_indices=True, indices=ft) dts.append(dt) fts.append(ft) - 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) + 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) fts.append(ft) for dt in dts: - self.assertTrue(diff(tdt, dt) < eps) + assert_array_almost_equal(tdt, dt) for ft in fts: - self.assertTrue(diff(tft, ft) < eps) + assert_array_almost_equal(tft, ft) def test_distance_transform_bf05(self): "brute force distance transform 5" @@ -3475,36 +3480,38 @@ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) out, ft = ndimage.distance_transform_bf(data, - 'euclidean', return_indices = True, sampling = [2, 2]) - error1 = diff([[0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 4, 4, 4, 0, 0, 0], - [0, 0, 4, 8, 16, 8, 4, 0, 0], - [0, 0, 4, 16, 32, 16, 4, 0, 0], - [0, 0, 4, 8, 16, 8, 4, 0, 0], - [0, 0, 0, 4, 4, 4, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]], out * out) - error2 = diff([[[0, 0, 0, 0, 0, 0, 0, 0, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1], - [2, 2, 2, 2, 1, 2, 2, 2, 2], - [3, 3, 3, 2, 1, 2, 3, 3, 3], - [4, 4, 4, 4, 6, 4, 4, 4, 4], - [5, 5, 6, 6, 7, 6, 6, 5, 5], - [6, 6, 6, 7, 7, 7, 6, 6, 6], - [7, 7, 7, 7, 7, 7, 7, 7, 7], - [8, 8, 8, 8, 8, 8, 8, 8, 8]], - [[0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 2, 4, 6, 6, 7, 8], - [0, 1, 1, 2, 4, 6, 7, 7, 8], - [0, 1, 1, 1, 6, 7, 7, 7, 8], - [0, 1, 2, 2, 4, 6, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8]]], ft) - self.assertTrue(error1 < eps and error2 < eps) + 'euclidean', return_indices=True, sampling=[2, 2]) + expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 4, 4, 4, 0, 0, 0], + [0, 0, 4, 8, 16, 8, 4, 0, 0], + [0, 0, 4, 16, 32, 16, 4, 0, 0], + [0, 0, 4, 8, 16, 8, 4, 0, 0], + [0, 0, 0, 4, 4, 4, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]] + assert_array_almost_equal(out * out, expected) + expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1], + [2, 2, 2, 2, 1, 2, 2, 2, 2], + [3, 3, 3, 2, 1, 2, 3, 3, 3], + [4, 4, 4, 4, 6, 4, 4, 4, 4], + [5, 5, 6, 6, 7, 6, 6, 5, 5], + [6, 6, 6, 7, 7, 7, 6, 6, 6], + [7, 7, 7, 7, 7, 7, 7, 7, 7], + [8, 8, 8, 8, 8, 8, 8, 8, 8]], + [[0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 2, 4, 6, 6, 7, 8], + [0, 1, 1, 2, 4, 6, 7, 7, 8], + [0, 1, 1, 1, 6, 7, 7, 7, 8], + [0, 1, 2, 2, 4, 6, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8]]] + assert_array_almost_equal(ft, expected) + def test_distance_transform_bf06(self): "brute force distance transform 6" for type in self.types: @@ -3518,37 +3525,38 @@ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) out, ft = ndimage.distance_transform_bf(data, - 'euclidean', return_indices = True, sampling = [2, 1]) - error1 = diff([[0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 1, 4, 1, 0, 0, 0], - [0, 0, 1, 4, 8, 4, 1, 0, 0], - [0, 0, 1, 4, 9, 4, 1, 0, 0], - [0, 0, 1, 4, 8, 4, 1, 0, 0], - [0, 0, 0, 1, 4, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]], out * out) - error2 = diff([[[0, 0, 0, 0, 0, 0, 0, 0, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1], - [2, 2, 2, 2, 2, 2, 2, 2, 2], - [3, 3, 3, 3, 2, 3, 3, 3, 3], - [4, 4, 4, 4, 4, 4, 4, 4, 4], - [5, 5, 5, 5, 6, 5, 5, 5, 5], - [6, 6, 6, 6, 7, 6, 6, 6, 6], - [7, 7, 7, 7, 7, 7, 7, 7, 7], - [8, 8, 8, 8, 8, 8, 8, 8, 8]], - [[0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 2, 6, 6, 6, 7, 8], - [0, 1, 1, 1, 6, 7, 7, 7, 8], - [0, 1, 1, 1, 7, 7, 7, 7, 8], - [0, 1, 1, 1, 6, 7, 7, 7, 8], - [0, 1, 2, 2, 4, 6, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8]]], ft) - self.assertTrue(error1 < eps and error2 < eps) - self.assertTrue(error1 < eps and error2 < eps) + 'euclidean', return_indices=True, sampling=[2, 1]) + expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 4, 1, 0, 0, 0], + [0, 0, 1, 4, 8, 4, 1, 0, 0], + [0, 0, 1, 4, 9, 4, 1, 0, 0], + [0, 0, 1, 4, 8, 4, 1, 0, 0], + [0, 0, 0, 1, 4, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]] + assert_array_almost_equal(out * out, expected) + expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1], + [2, 2, 2, 2, 2, 2, 2, 2, 2], + [3, 3, 3, 3, 2, 3, 3, 3, 3], + [4, 4, 4, 4, 4, 4, 4, 4, 4], + [5, 5, 5, 5, 6, 5, 5, 5, 5], + [6, 6, 6, 6, 7, 6, 6, 6, 6], + [7, 7, 7, 7, 7, 7, 7, 7, 7], + [8, 8, 8, 8, 8, 8, 8, 8, 8]], + [[0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 2, 6, 6, 6, 7, 8], + [0, 1, 1, 1, 6, 7, 7, 7, 8], + [0, 1, 1, 1, 7, 7, 7, 7, 8], + [0, 1, 1, 1, 6, 7, 7, 7, 8], + [0, 1, 2, 2, 4, 6, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8]]] + assert_array_almost_equal(ft, expected) + def test_distance_transform_cdt01(self): "chamfer type distance transform 1" for type in self.types: @@ -3562,65 +3570,67 @@ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) out, ft = ndimage.distance_transform_cdt(data, - 'cityblock', return_indices = True) + 'cityblock', return_indices=True) bf = ndimage.distance_transform_bf(data, 'cityblock') - error1 = diff(bf, out) - error2 = diff([[[0, 0, 0, 0, 0, 0, 0, 0, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1], - [2, 2, 2, 1, 1, 1, 2, 2, 2], - [3, 3, 2, 1, 1, 1, 2, 3, 3], - [4, 4, 4, 4, 1, 4, 4, 4, 4], - [5, 5, 5, 5, 7, 7, 6, 5, 5], - [6, 6, 6, 6, 7, 7, 6, 6, 6], - [7, 7, 7, 7, 7, 7, 7, 7, 7], - [8, 8, 8, 8, 8, 8, 8, 8, 8]], - [[0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 1, 1, 4, 7, 7, 7, 8], - [0, 1, 1, 1, 4, 5, 6, 7, 8], - [0, 1, 2, 2, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8],]], ft) - self.assertTrue(error1 < eps and error2 < eps) + assert_array_almost_equal(bf, out) + expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1], + [2, 2, 2, 1, 1, 1, 2, 2, 2], + [3, 3, 2, 1, 1, 1, 2, 3, 3], + [4, 4, 4, 4, 1, 4, 4, 4, 4], + [5, 5, 5, 5, 7, 7, 6, 5, 5], + [6, 6, 6, 6, 7, 7, 6, 6, 6], + [7, 7, 7, 7, 7, 7, 7, 7, 7], + [8, 8, 8, 8, 8, 8, 8, 8, 8]], + [[0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 1, 1, 4, 7, 7, 7, 8], + [0, 1, 1, 1, 4, 5, 6, 7, 8], + [0, 1, 2, 2, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8],]] + assert_array_almost_equal(ft, expected) + def test_distance_transform_cdt02(self): "chamfer type distance transform 2" for type in self.types: 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], - [0, 0, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 0, 1, 1, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) - out, ft = ndimage.distance_transform_cdt(data, - 'chessboard', return_indices = True) + [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], + [0, 0, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) + out, ft = ndimage.distance_transform_cdt(data, 'chessboard', + return_indices=True) bf = ndimage.distance_transform_bf(data, 'chessboard') - error1 = diff(bf, out) - error2 = diff([[[0, 0, 0, 0, 0, 0, 0, 0, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1], - [2, 2, 2, 1, 1, 1, 2, 2, 2], - [3, 3, 2, 2, 1, 2, 2, 3, 3], - [4, 4, 3, 2, 2, 2, 3, 4, 4], - [5, 5, 4, 6, 7, 6, 4, 5, 5], - [6, 6, 6, 6, 7, 7, 6, 6, 6], - [7, 7, 7, 7, 7, 7, 7, 7, 7], - [8, 8, 8, 8, 8, 8, 8, 8, 8]], - [[0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 2, 3, 4, 6, 7, 8], - [0, 1, 1, 2, 2, 6, 6, 7, 8], - [0, 1, 1, 1, 2, 6, 7, 7, 8], - [0, 1, 1, 2, 6, 6, 7, 7, 8], - [0, 1, 2, 2, 5, 6, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8], - [0, 1, 2, 3, 4, 5, 6, 7, 8],]], ft) - self.assertTrue(error1 < eps and error2 < eps) + assert_array_almost_equal(bf, out) + expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1], + [2, 2, 2, 1, 1, 1, 2, 2, 2], + [3, 3, 2, 2, 1, 2, 2, 3, 3], + [4, 4, 3, 2, 2, 2, 3, 4, 4], + [5, 5, 4, 6, 7, 6, 4, 5, 5], + [6, 6, 6, 6, 7, 7, 6, 6, 6], + [7, 7, 7, 7, 7, 7, 7, 7, 7], + [8, 8, 8, 8, 8, 8, 8, 8, 8]], + [[0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 2, 3, 4, 6, 7, 8], + [0, 1, 1, 2, 2, 6, 6, 7, 8], + [0, 1, 1, 1, 2, 6, 7, 7, 8], + [0, 1, 1, 2, 6, 6, 7, 7, 8], + [0, 1, 2, 2, 5, 6, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8], + [0, 1, 2, 3, 4, 5, 6, 7, 8],]] + assert_array_almost_equal(ft, expected) + def test_distance_transform_cdt03(self): "chamfer type distance transform 3" for type in self.types: @@ -3634,43 +3644,43 @@ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) tdt, tft = ndimage.distance_transform_cdt(data, - return_indices = True) + return_indices=True) dts = [] fts = [] 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) + return_distances=False, return_indices=True) fts.append(ft) - ft = numpy.indices(data.shape, dtype = numpy.int32) + ft = numpy.indices(data.shape, dtype=numpy.int32) ndimage.distance_transform_cdt(data, - return_distances = False, return_indices = True, indices = ft) + return_distances=False, return_indices=True, indices=ft) fts.append(ft) dt, ft = ndimage.distance_transform_cdt(data, - return_indices = True) + return_indices=True) dts.append(dt) fts.append(ft) - dt = numpy.zeros(data.shape, dtype = numpy.int32) - ft = ndimage.distance_transform_cdt(data, distances = dt, + 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 = numpy.indices(data.shape, dtype = numpy.int32) + ft = numpy.indices(data.shape, dtype=numpy.int32) dt = ndimage.distance_transform_cdt(data, - return_indices = True, indices = ft) + return_indices=True, indices=ft) dts.append(dt) fts.append(ft) - 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) + 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) fts.append(ft) for dt in dts: - self.assertTrue(diff(tdt, dt) < eps) + assert_array_almost_equal(tdt, dt) for ft in fts: - self.assertTrue(diff(tft, ft) < eps) + assert_array_almost_equal(tft, ft) def test_distance_transform_edt01(self): "euclidean distance transform 1" @@ -3685,18 +3695,18 @@ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) out, ft = ndimage.distance_transform_edt(data, - return_indices = True) + return_indices=True) bf = ndimage.distance_transform_bf(data, 'euclidean') + assert_array_almost_equal(bf, out) - error1 = diff(bf, out) - dt = ft - numpy.indices(ft.shape[1:], dtype = ft.dtype) + 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) + dt = numpy.add.reduce(dt, axis=0) numpy.sqrt(dt, dt) - error2 = diff(bf, dt) - self.assertTrue(error1 < eps and error2 < eps) + assert_array_almost_equal(bf, dt) + def test_distance_transform_edt02(self): "euclidean distance transform 2" for type in self.types: @@ -3710,43 +3720,43 @@ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) tdt, tft = ndimage.distance_transform_edt(data, - return_indices = True) + return_indices=True) dts = [] fts = [] - dt = numpy.zeros(data.shape, dtype = numpy.float64) - ndimage.distance_transform_edt(data, distances = dt) + 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) + return_distances=0, return_indices=True) fts.append(ft) - ft = numpy.indices(data.shape, dtype = numpy.int32) + ft = numpy.indices(data.shape, dtype=numpy.int32) ndimage.distance_transform_edt(data, - return_distances = False,return_indices = True, indices = ft) + return_distances=False,return_indices=True, indices=ft) fts.append(ft) dt, ft = ndimage.distance_transform_edt(data, - return_indices = True) + return_indices=True) dts.append(dt) fts.append(ft) - dt = numpy.zeros(data.shape, dtype = numpy.float64) - ft = ndimage.distance_transform_edt(data, distances = dt, - return_indices = True) + 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 = numpy.indices(data.shape, dtype = numpy.int32) + ft = numpy.indices(data.shape, dtype=numpy.int32) dt = ndimage.distance_transform_edt(data, - return_indices = True, indices = ft) + return_indices=True, indices=ft) dts.append(dt) fts.append(ft) - 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) + 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) fts.append(ft) for dt in dts: - self.assertTrue(diff(tdt, dt) < eps) + assert_array_almost_equal(tdt, dt) for ft in fts: - self.assertTrue(diff(tft, ft) < eps) + assert_array_almost_equal(tft, ft) def test_distance_transform_edt03(self): "euclidean distance transform 3" @@ -3761,10 +3771,10 @@ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) ref = ndimage.distance_transform_bf(data, 'euclidean', - sampling = [2, 2]) + sampling=[2, 2]) out = ndimage.distance_transform_edt(data, - sampling = [2, 2]) - self.assertTrue(diff(ref, out) < eps) + sampling=[2, 2]) + assert_array_almost_equal(ref, out) def test_distance_transform_edt4(self): @@ -3780,34 +3790,34 @@ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], type) ref = ndimage.distance_transform_bf(data, 'euclidean', - sampling = [2, 1]) + sampling=[2, 1]) out = ndimage.distance_transform_edt(data, - sampling = [2, 1]) - self.assertTrue(diff(ref, out) < eps) + sampling=[2, 1]) + assert_array_almost_equal(ref, out) def test_generate_structure01(self): "generation of a binary structure 1" struct = ndimage.generate_binary_structure(0, 1) - self.assertTrue(diff(struct, 1) < eps) + assert_array_almost_equal(struct, 1) def test_generate_structure02(self): "generation of a binary structure 2" struct = ndimage.generate_binary_structure(1, 1) - self.assertTrue(diff(struct, [1, 1, 1]) < eps) + assert_array_almost_equal(struct, [1, 1, 1]) def test_generate_structure03(self): "generation of a binary structure 3" struct = ndimage.generate_binary_structure(2, 1) - self.assertTrue(diff(struct, [[0, 1, 0], + assert_array_almost_equal(struct, [[0, 1, 0], [1, 1, 1], - [0, 1, 0]]) < eps) + [0, 1, 0]]) def test_generate_structure04(self): "generation of a binary structure 4" struct = ndimage.generate_binary_structure(2, 2) - self.assertTrue(diff(struct, [[1, 1, 1], + assert_array_almost_equal(struct, [[1, 1, 1], [1, 1, 1], - [1, 1, 1]]) < eps) + [1, 1, 1]]) def test_iterate_structure01(self): "iterating a structure 1" @@ -3815,11 +3825,11 @@ [1, 1, 1], [0, 1, 0]] out = ndimage.iterate_structure(struct, 2) - self.assertTrue(diff(out, [[0, 0, 1, 0, 0], + assert_array_almost_equal(out, [[0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [1, 1, 1, 1, 1], [0, 1, 1, 1, 0], - [0, 0, 1, 0, 0]]) < eps) + [0, 0, 1, 0, 0]]) def test_iterate_structure02(self): "iterating a structure 2" @@ -3827,11 +3837,11 @@ [1, 1], [0, 1]] out = ndimage.iterate_structure(struct, 2) - self.assertTrue(diff(out, [[0, 0, 1], + assert_array_almost_equal(out, [[0, 0, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], - [0, 0, 1]]) < eps) + [0, 0, 1]]) def test_iterate_structure03(self): "iterating a structure 3" @@ -3839,68 +3849,69 @@ [1, 1, 1], [0, 1, 0]] out = ndimage.iterate_structure(struct, 2, 1) - error = diff(out[0], [[0, 0, 1, 0, 0], - [0, 1, 1, 1, 0], - [1, 1, 1, 1, 1], - [0, 1, 1, 1, 0], - [0, 0, 1, 0, 0]]) - self.assertTrue(error < eps and out[1] == [2, 2]) + expected = [[0, 0, 1, 0, 0], + [0, 1, 1, 1, 0], + [1, 1, 1, 1, 1], + [0, 1, 1, 1, 0], + [0, 0, 1, 0, 0]] + assert_array_almost_equal(out[0], expected) + assert_equal(out[1], [2, 2]) def test_binary_erosion01(self): "binary erosion 1" for type in self.types: data = numpy.ones([], type) out = ndimage.binary_erosion(data) - self.assertTrue(diff(out, 1) < eps) + assert_array_almost_equal(out, 1) def test_binary_erosion02(self): "binary erosion 2" for type in self.types: data = numpy.ones([], type) - out = ndimage.binary_erosion(data, border_value = 1) - self.assertTrue(diff(out, 1) < eps) + out = ndimage.binary_erosion(data, border_value=1) + assert_array_almost_equal(out, 1) def test_binary_erosion03(self): "binary erosion 3" for type in self.types: data = numpy.ones([1], type) out = ndimage.binary_erosion(data) - self.assertTrue(diff(out, [0]) < eps) + assert_array_almost_equal(out, [0]) def test_binary_erosion04(self): "binary erosion 4" for type in self.types: data = numpy.ones([1], type) - out = ndimage.binary_erosion(data, border_value = 1) - self.assertTrue(diff(out, [1]) < eps) + out = ndimage.binary_erosion(data, border_value=1) + assert_array_almost_equal(out, [1]) def test_binary_erosion05(self): "binary erosion 5" for type in self.types: data = numpy.ones([3], type) out = ndimage.binary_erosion(data) - self.assertTrue(diff(out, [0, 1, 0]) < eps) + assert_array_almost_equal(out, [0, 1, 0]) def test_binary_erosion06(self): "binary erosion 6" for type in self.types: data = numpy.ones([3], type) - out = ndimage.binary_erosion(data, border_value = 1) - self.assertTrue(diff(out, [1, 1, 1]) < eps) + out = ndimage.binary_erosion(data, border_value=1) + assert_array_almost_equal(out, [1, 1, 1]) def test_binary_erosion07(self): "binary erosion 7" for type in self.types: data = numpy.ones([5], type) out = ndimage.binary_erosion(data) - self.assertTrue(diff(out, [0, 1, 1, 1, 0]) < eps) + assert_array_almost_equal(out, [0, 1, 1, 1, 0]) def test_binary_erosion08(self): "binary erosion 8" for type in self.types: data = numpy.ones([5], type) - out = ndimage.binary_erosion(data, border_value = 1) - self.assertTrue(diff(out, [1, 1, 1, 1, 1]) < eps) + out = ndimage.binary_erosion(data, border_value=1) + assert_array_almost_equal(out, [1, 1, 1, 1, 1]) def test_binary_erosion09(self): "binary erosion 9" @@ -3908,15 +3919,15 @@ data = numpy.ones([5], type) data[2] = 0 out = ndimage.binary_erosion(data) - self.assertTrue(diff(out, [0, 0, 0, 0, 0]) < eps) + assert_array_almost_equal(out, [0, 0, 0, 0, 0]) def test_binary_erosion10(self): "binary erosion 10" for type in self.types: data = numpy.ones([5], type) data[2] = 0 - out = ndimage.binary_erosion(data, border_value = 1) - self.assertTrue(diff(out, [1, 0, 0, 0, 1]) < eps) + out = ndimage.binary_erosion(data, border_value=1) + assert_array_almost_equal(out, [1, 0, 0, 0, 1]) def test_binary_erosion11(self): "binary erosion 11" @@ -3925,8 +3936,8 @@ data[2] = 0 struct = [1, 0, 1] out = ndimage.binary_erosion(data, struct, - border_value = 1) - self.assertTrue(diff(out, [1, 0, 1, 0, 1]) < eps) + border_value=1) + assert_array_almost_equal(out, [1, 0, 1, 0, 1]) def test_binary_erosion12(self): "binary erosion 12" @@ -3935,9 +3946,9 @@ data[2] = 0 struct = [1, 0, 1] out = ndimage.binary_erosion(data, struct, - border_value = 1, - origin = -1) - self.assertTrue(diff(out, [0, 1, 0, 1, 1]) < eps) + border_value=1, + origin=-1) + assert_array_almost_equal(out, [0, 1, 0, 1, 1]) def test_binary_erosion13(self): "binary erosion 13" @@ -3946,9 +3957,9 @@ data[2] = 0 struct = [1, 0, 1] out = ndimage.binary_erosion(data, struct, - border_value = 1, - origin = 1) - self.assertTrue(diff(out, [1, 1, 0, 1, 0]) < eps) + border_value=1, + origin=1) + assert_array_almost_equal(out, [1, 1, 0, 1, 0]) def test_binary_erosion14(self): "binary erosion 14" @@ -3957,8 +3968,8 @@ data[2] = 0 struct = [1, 1] out = ndimage.binary_erosion(data, struct, - border_value = 1) - self.assertTrue(diff(out, [1, 1, 0, 0, 1]) < eps) + border_value=1) + assert_array_almost_equal(out, [1, 1, 0, 0, 1]) def test_binary_erosion15(self): "binary erosion 15" @@ -3967,59 +3978,59 @@ data[2] = 0 struct = [1, 1] out = ndimage.binary_erosion(data, struct, - border_value = 1, - origin = -1) - self.assertTrue(diff(out, [1, 0, 0, 1, 1]) < eps) + border_value=1, + origin=-1) + assert_array_almost_equal(out, [1, 0, 0, 1, 1]) def test_binary_erosion16(self): "binary erosion 16" for type in self.types: data = numpy.ones([1, 1], type) - out = ndimage.binary_erosion(data, border_value = 1) - self.assertTrue(diff(out, [[1]]) < eps) + out = ndimage.binary_erosion(data, border_value=1) + assert_array_almost_equal(out, [[1]]) def test_binary_erosion17(self): "binary erosion 17" for type in self.types: data = numpy.ones([1, 1], type) out = ndimage.binary_erosion(data) - self.assertTrue(diff(out, [[0]]) < eps) + assert_array_almost_equal(out, [[0]]) def test_binary_erosion18(self): "binary erosion 18" for type in self.types: data = numpy.ones([1, 3], type) out = ndimage.binary_erosion(data) - self.assertTrue(diff(out, [[0, 0, 0]]) < eps) + assert_array_almost_equal(out, [[0, 0, 0]]) def test_binary_erosion19(self): "binary erosion 19" for type in self.types: data = numpy.ones([1, 3], type) - out = ndimage.binary_erosion(data, border_value = 1) - self.assertTrue(diff(out, [[1, 1, 1]]) < eps) + out = ndimage.binary_erosion(data, border_value=1) + assert_array_almost_equal(out, [[1, 1, 1]]) def test_binary_erosion20(self): "binary erosion 20" for type in self.types: data = numpy.ones([3, 3], type) out = ndimage.binary_erosion(data) - self.assertTrue(diff(out, [[0, 0, 0], + assert_array_almost_equal(out, [[0, 0, 0], [0, 1, 0], - [0, 0, 0]]) < eps) + [0, 0, 0]]) def test_binary_erosion21(self): "binary erosion 21" for type in self.types: data = numpy.ones([3, 3], type) - out = ndimage.binary_erosion(data, border_value = 1) - self.assertTrue(diff(out, [[1, 1, 1], + out = ndimage.binary_erosion(data, border_value=1) + assert_array_almost_equal(out, [[1, 1, 1], [1, 1, 1], - [1, 1, 1]]) < eps) + [1, 1, 1]]) def test_binary_erosion22(self): "binary erosion 22" - true = [[0, 0, 0, 0, 0, 0, 0, 0], + expected = [[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, 1, 0, 0], @@ -4036,13 +4047,13 @@ [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) - out = ndimage.binary_erosion(data, border_value = 1) - self.assertTrue(diff(out, true) < eps) + out = ndimage.binary_erosion(data, border_value=1) + assert_array_almost_equal(out, expected) def test_binary_erosion23(self): "binary erosion 23" struct = ndimage.generate_binary_structure(2, 2) - true = [[0, 0, 0, 0, 0, 0, 0, 0], + expected = [[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, 0], @@ -4060,14 +4071,14 @@ [0, 1, 1, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_erosion(data, struct, - border_value = 1) - self.assertTrue(diff(out, true) < eps) + border_value=1) + assert_array_almost_equal(out, expected) def test_binary_erosion24(self): "binary erosion 24" struct = [[0, 1], [1, 1]] - true = [[0, 0, 0, 0, 0, 0, 0, 0], + expected = [[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, 1, 1, 1], @@ -4085,15 +4096,15 @@ [0, 1, 1, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_erosion(data, struct, - border_value = 1) - self.assertTrue(diff(out, true) < eps) + border_value=1) + assert_array_almost_equal(out, expected) def test_binary_erosion25(self): "binary erosion 25" struct = [[0, 1, 0], [1, 0, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 0, 0, 0], + expected = [[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, 1, 0, 0], @@ -4111,15 +4122,15 @@ [0, 1, 1, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_erosion(data, struct, - border_value = 1) - self.assertTrue(diff(out, true) < eps) + border_value=1) + assert_array_almost_equal(out, expected) def test_binary_erosion26(self): "binary erosion 26" struct = [[0, 1, 0], [1, 0, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 0, 0, 0], + expected = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 1, 0, 0, 1], [0, 0, 1, 0, 0, 0, 0, 0], @@ -4137,15 +4148,15 @@ [0, 1, 1, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_erosion(data, struct, - border_value = 1, origin = (-1, -1)) - self.assertTrue(diff(out, true) < eps) + border_value=1, origin=(-1, -1)) + assert_array_almost_equal(out, expected) def test_binary_erosion27(self): "binary erosion 27" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 0, 0], + expected = [[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, 0, 0, 0], @@ -4160,15 +4171,15 @@ [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], bool) out = ndimage.binary_erosion(data, struct, - border_value = 1, iterations = 2) - self.assertTrue(diff(out, true) < eps) + border_value=1, iterations=2) + assert_array_almost_equal(out, expected) def test_binary_erosion28(self): "binary erosion 28" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 0, 0], + expected = [[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, 0, 0, 0], @@ -4183,16 +4194,16 @@ [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], bool) out = numpy.zeros(data.shape, bool) - ndimage.binary_erosion(data, struct, border_value = 1, - iterations = 2, output = out) - self.assertTrue(diff(out, true) < eps) + ndimage.binary_erosion(data, struct, border_value=1, + iterations=2, output=out) + assert_array_almost_equal(out, expected) def test_binary_erosion29(self): "binary erosion 29" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 0, 0], + expected = [[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, 0, 0, 0], @@ -4207,15 +4218,15 @@ [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0]], bool) out = ndimage.binary_erosion(data, struct, - border_value = 1, iterations = 3) - self.assertTrue(diff(out, true) < eps) + border_value=1, iterations=3) + assert_array_almost_equal(out, expected) def test_binary_erosion30(self): "binary erosion 30" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 0, 0], + expected = [[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, 0, 0, 0], @@ -4230,16 +4241,16 @@ [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0]], bool) out = numpy.zeros(data.shape, bool) - ndimage.binary_erosion(data, struct, border_value = 1, - iterations = 3, output = out) - self.assertTrue(diff(out, true) < eps) + ndimage.binary_erosion(data, struct, border_value=1, + iterations=3, output=out) + assert_array_almost_equal(out, expected) def test_binary_erosion31(self): "binary erosion 31" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 1, 0, 0, 0, 0], + expected = [[0, 0, 1, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 0, 1], [0, 1, 1, 1, 0, 0, 0], @@ -4254,16 +4265,16 @@ [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0]], bool) out = numpy.zeros(data.shape, bool) - ndimage.binary_erosion(data, struct, border_value = 1, - iterations = 1, output = out, origin = (-1, -1)) - self.assertTrue(diff(out, true) < eps) + ndimage.binary_erosion(data, struct, border_value=1, + iterations=1, output=out, origin=(-1, -1)) + assert_array_almost_equal(out, expected) def test_binary_erosion32(self): "binary erosion 32" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 0, 0], + expected = [[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, 0, 0, 0], @@ -4278,15 +4289,15 @@ [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], bool) out = ndimage.binary_erosion(data, struct, - border_value = 1, iterations = 2) - self.assertTrue(diff(out, true) < eps) + border_value=1, iterations=2) + assert_array_almost_equal(out, expected) def test_binary_erosion33(self): "binary erosion 33" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 1, 1], + expected = [[0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], @@ -4308,15 +4319,15 @@ [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], bool) out = ndimage.binary_erosion(data, struct, - border_value = 1, mask = mask, iterations = -1) - self.assertTrue(diff(out, true) < eps) + border_value=1, mask=mask, iterations=-1) + assert_array_almost_equal(out, expected) def test_binary_erosion34(self): "binary erosion 34" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 0, 0], + expected = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], @@ -4338,8 +4349,8 @@ [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], bool) out = ndimage.binary_erosion(data, struct, - border_value = 1, mask = mask) - self.assertTrue(diff(out, true) < eps) + border_value=1, mask=mask) + assert_array_almost_equal(out, expected) def test_binary_erosion35(self): "binary erosion 35" @@ -4367,14 +4378,14 @@ [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 1]] - true = numpy.logical_and(tmp, mask) + expected = numpy.logical_and(tmp, mask) tmp = numpy.logical_and(data, numpy.logical_not(mask)) - true = numpy.logical_or(true, tmp) + expected = numpy.logical_or(expected, tmp) out = numpy.zeros(data.shape, bool) - ndimage.binary_erosion(data, struct, border_value = 1, - iterations = 1, output = out, - origin = (-1, -1), mask = mask) - self.assertTrue(diff(out, true) < eps) + ndimage.binary_erosion(data, struct, border_value=1, + iterations=1, output=out, + origin=(-1, -1), mask=mask) + assert_array_almost_equal(out, expected) def test_binary_erosion36(self): "binary erosion 36" @@ -4405,54 +4416,54 @@ [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 = numpy.logical_and(tmp, mask) + expected = 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.assertTrue(diff(out, true) < eps) + expected = numpy.logical_or(expected, tmp) + out = ndimage.binary_erosion(data, struct, mask=mask, + border_value=1, origin=(-1, -1)) + assert_array_almost_equal(out, expected) def test_binary_dilation01(self): "binary dilation 1" for type in self.types: data = numpy.ones([], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, 1) < eps) + assert_array_almost_equal(out, 1) def test_binary_dilation02(self): "binary dilation 2" for type in self.types: data = numpy.zeros([], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, 0) < eps) + assert_array_almost_equal(out, 0) def test_binary_dilation03(self): "binary dilation 3" for type in self.types: data = numpy.ones([1], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [1]) < eps) + assert_array_almost_equal(out, [1]) def test_binary_dilation04(self): "binary dilation 4" for type in self.types: data = numpy.zeros([1], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [0]) < eps) + assert_array_almost_equal(out, [0]) def test_binary_dilation05(self): "binary dilation 5" for type in self.types: data = numpy.ones([3], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [1, 1, 1]) < eps) + assert_array_almost_equal(out, [1, 1, 1]) def test_binary_dilation06(self): "binary dilation 6" for type in self.types: data = numpy.zeros([3], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [0, 0, 0]) < eps) + assert_array_almost_equal(out, [0, 0, 0]) def test_binary_dilation07(self): "binary dilation 7" @@ -4461,7 +4472,7 @@ data = numpy.zeros([3], type) data[1] = 1 out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [1, 1, 1]) < eps) + assert_array_almost_equal(out, [1, 1, 1]) def test_binary_dilation08(self): "binary dilation 8" @@ -4470,7 +4481,7 @@ data[1] = 1 data[3] = 1 out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [1, 1, 1, 1, 1]) < eps) + assert_array_almost_equal(out, [1, 1, 1, 1, 1]) def test_binary_dilation09(self): "binary dilation 9" @@ -4478,23 +4489,23 @@ data = numpy.zeros([5], type) data[1] = 1 out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [1, 1, 1, 0, 0]) < eps) + assert_array_almost_equal(out, [1, 1, 1, 0, 0]) def test_binary_dilation10(self): "binary dilation 10" for type in self.types: data = numpy.zeros([5], type) data[1] = 1 - out = ndimage.binary_dilation(data, origin = -1) - self.assertTrue(diff(out, [0, 1, 1, 1, 0]) < eps) + out = ndimage.binary_dilation(data, origin=-1) + assert_array_almost_equal(out, [0, 1, 1, 1, 0]) def test_binary_dilation11(self): "binary dilation 11" for type in self.types: data = numpy.zeros([5], type) data[1] = 1 - out = ndimage.binary_dilation(data, origin = 1) - self.assertTrue(diff(out, [1, 1, 0, 0, 0]) < eps) + out = ndimage.binary_dilation(data, origin=1) + assert_array_almost_equal(out, [1, 1, 0, 0, 0]) def test_binary_dilation12(self): "binary dilation 12" @@ -4503,7 +4514,7 @@ data[1] = 1 struct = [1, 0, 1] out = ndimage.binary_dilation(data, struct) - self.assertTrue(diff(out, [1, 0, 1, 0, 0]) < eps) + assert_array_almost_equal(out, [1, 0, 1, 0, 0]) def test_binary_dilation13(self): "binary dilation 13" @@ -4512,8 +4523,8 @@ data[1] = 1 struct = [1, 0, 1] out = ndimage.binary_dilation(data, struct, - border_value = 1) - self.assertTrue(diff(out, [1, 0, 1, 0, 1]) < eps) + border_value=1) + assert_array_almost_equal(out, [1, 0, 1, 0, 1]) def test_binary_dilation14(self): "binary dilation 14" @@ -4522,8 +4533,8 @@ data[1] = 1 struct = [1, 0, 1] out = ndimage.binary_dilation(data, struct, - origin = -1) - self.assertTrue(diff(out, [0, 1, 0, 1, 0]) < eps) + origin=-1) + assert_array_almost_equal(out, [0, 1, 0, 1, 0]) def test_binary_dilation15(self): "binary dilation 15" @@ -4532,38 +4543,38 @@ data[1] = 1 struct = [1, 0, 1] out = ndimage.binary_dilation(data, struct, - origin = -1, border_value = 1) - self.assertTrue(diff(out, [1, 1, 0, 1, 0]) < eps) + origin=-1, border_value=1) + assert_array_almost_equal(out, [1, 1, 0, 1, 0]) def test_binary_dilation16(self): "binary dilation 16" for type in self.types: data = numpy.ones([1, 1], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [[1]]) < eps) + assert_array_almost_equal(out, [[1]]) def test_binary_dilation17(self): "binary dilation 17" for type in self.types: data = numpy.zeros([1, 1], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [[0]]) < eps) + assert_array_almost_equal(out, [[0]]) def test_binary_dilation18(self): "binary dilation 18" for type in self.types: data = numpy.ones([1, 3], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [[1, 1, 1]]) < eps) + assert_array_almost_equal(out, [[1, 1, 1]]) def test_binary_dilation19(self): "binary dilation 19" for type in self.types: data = numpy.ones([3, 3], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [[1, 1, 1], + assert_array_almost_equal(out, [[1, 1, 1], [1, 1, 1], - [1, 1, 1]]) < eps) + [1, 1, 1]]) def test_binary_dilation20(self): "binary dilation 20" @@ -4571,9 +4582,9 @@ data = numpy.zeros([3, 3], type) data[1, 1] = 1 out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, [[0, 1, 0], + assert_array_almost_equal(out, [[0, 1, 0], [1, 1, 1], - [0, 1, 0]]) < eps) + [0, 1, 0]]) def test_binary_dilation21(self): "binary dilation 21" @@ -4582,13 +4593,13 @@ data = numpy.zeros([3, 3], type) data[1, 1] = 1 out = ndimage.binary_dilation(data, struct) - self.assertTrue(diff(out, [[1, 1, 1], + assert_array_almost_equal(out, [[1, 1, 1], [1, 1, 1], - [1, 1, 1]]) < eps) + [1, 1, 1]]) def test_binary_dilation22(self): "binary dilation 22" - true = [[0, 1, 0, 0, 0, 0, 0, 0], + expected = [[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], @@ -4607,11 +4618,11 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_dilation(data) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_binary_dilation23(self): "binary dilation 23" - true = [[1, 1, 1, 1, 1, 1, 1, 1], + expected = [[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0, 0, 1], [1, 1, 0, 0, 0, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1], @@ -4629,12 +4640,12 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) - out = ndimage.binary_dilation(data, border_value = 1) - self.assertTrue(diff(out, true) < eps) + out = ndimage.binary_dilation(data, border_value=1) + assert_array_almost_equal(out, expected) def test_binary_dilation24(self): "binary dilation 24" - true = [[1, 1, 0, 0, 0, 0, 0, 0], + expected = [[1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0, 0], @@ -4652,12 +4663,12 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) - out = ndimage.binary_dilation(data, origin = (1, 1)) - self.assertTrue(diff(out, true) < eps) + out = ndimage.binary_dilation(data, origin=(1, 1)) + assert_array_almost_equal(out, expected) def test_binary_dilation25(self): "binary dilation 25" - true = [[1, 1, 0, 0, 0, 0, 1, 1], + expected = [[1, 1, 0, 0, 0, 0, 1, 1], [1, 0, 0, 0, 1, 0, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 0, 1, 1], @@ -4675,14 +4686,14 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) - out = ndimage.binary_dilation(data, origin = (1, 1), - border_value = 1) - self.assertTrue(diff(out, true) < eps) + out = ndimage.binary_dilation(data, origin=(1, 1), + border_value=1) + assert_array_almost_equal(out, expected) def test_binary_dilation26(self): "binary dilation 26" struct = ndimage.generate_binary_structure(2, 2) - true = [[1, 1, 1, 0, 0, 0, 0, 0], + expected = [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 0], @@ -4701,13 +4712,13 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_dilation(data, struct) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_binary_dilation27(self): "binary dilation 27" struct = [[0, 1], [1, 1]] - true = [[0, 1, 0, 0, 0, 0, 0, 0], + expected = [[0, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0], @@ -4726,11 +4737,11 @@ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_dilation(data, struct) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_binary_dilation28(self): "binary dilation 28" - true = [[1, 1, 1, 1], + expected = [[1, 1, 1, 1], [1, 0, 0, 1], [1, 0, 0, 1], [1, 1, 1, 1]] @@ -4740,14 +4751,14 @@ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], type) - out = ndimage.binary_dilation(data, border_value = 1) - self.assertTrue(diff(out, true) < eps) + out = ndimage.binary_dilation(data, border_value=1) + assert_array_almost_equal(out, expected) def test_binary_dilation29(self): "binary dilation 29" struct = [[0, 1], [1, 1]] - true = [[0, 0, 0, 0, 0], + expected = [[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 1, 1, 0], [0, 1, 1, 1, 0], @@ -4759,14 +4770,14 @@ [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]], bool) out = ndimage.binary_dilation(data, struct, - iterations = 2) - self.assertTrue(diff(out, true) < eps) + iterations=2) + assert_array_almost_equal(out, expected) def test_binary_dilation30(self): "binary dilation 30" struct = [[0, 1], [1, 1]] - true = [[0, 0, 0, 0, 0], + expected = [[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 1, 1, 0], [0, 1, 1, 1, 0], @@ -4778,15 +4789,15 @@ [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]], bool) out = numpy.zeros(data.shape, bool) - ndimage.binary_dilation(data, struct, iterations = 2, - output = out) - self.assertTrue(diff(out, true) < eps) + ndimage.binary_dilation(data, struct, iterations=2, + output=out) + assert_array_almost_equal(out, expected) def test_binary_dilation31(self): "binary dilation 31" struct = [[0, 1], [1, 1]] - true = [[0, 0, 0, 1, 0], + expected = [[0, 0, 0, 1, 0], [0, 0, 1, 1, 0], [0, 1, 1, 1, 0], [1, 1, 1, 1, 0], @@ -4798,14 +4809,14 @@ [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]], bool) out = ndimage.binary_dilation(data, struct, - iterations = 3) - self.assertTrue(diff(out, true) < eps) + iterations=3) + assert_array_almost_equal(out, expected) def test_binary_dilation32(self): "binary dilation 32" struct = [[0, 1], [1, 1]] - true = [[0, 0, 0, 1, 0], + expected = [[0, 0, 0, 1, 0], [0, 0, 1, 1, 0], [0, 1, 1, 1, 0], [1, 1, 1, 1, 0], @@ -4817,16 +4828,16 @@ [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]], bool) out = numpy.zeros(data.shape, bool) - ndimage.binary_dilation(data, struct, iterations = 3, - output = out) - self.assertTrue(diff(out, true) < eps) + ndimage.binary_dilation(data, struct, iterations=3, + output=out) + assert_array_almost_equal(out, expected) def test_binary_dilation33(self): "binary dilation 33" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], + expected = 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], @@ -4852,15 +4863,15 @@ [0, 0, 0, 0, 0, 0, 0, 0]], bool) out = ndimage.binary_dilation(data, struct, - iterations = -1, mask = mask, border_value = 0) - self.assertTrue(diff(out, true) < eps) + iterations=-1, mask=mask, border_value=0) + assert_array_almost_equal(out, expected) def test_binary_dilation34(self): "binary dilation 34" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 1, 0, 0, 0, 0, 0, 0], + expected = [[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, 0, 0, 0], @@ -4878,8 +4889,8 @@ [0, 0, 0, 0, 0, 0, 0, 0]], bool) data = numpy.zeros(mask.shape, bool) out = ndimage.binary_dilation(data, struct, - iterations = -1, mask = mask, border_value = 1) - self.assertTrue(diff(out, true) < eps) + iterations=-1, mask=mask, border_value=1) + assert_array_almost_equal(out, expected) def test_binary_dilation35(self): "binary dilation 35" @@ -4907,9 +4918,9 @@ [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 = numpy.logical_and(tmp, mask) + expected = numpy.logical_and(tmp, mask) tmp = numpy.logical_and(data, numpy.logical_not(mask)) - true = numpy.logical_or(true, tmp) + expected = numpy.logical_or(expected, tmp) for type in self.types: data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], @@ -4919,16 +4930,16 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) - out = ndimage.binary_dilation(data, mask = mask, - origin = (1, 1), border_value = 1) - self.assertTrue(diff(out, true) < eps) + out = ndimage.binary_dilation(data, mask=mask, + origin=(1, 1), border_value=1) + assert_array_almost_equal(out, expected) def test_binary_propagation01(self): "binary propagation 1" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0], + expected = 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], @@ -4954,15 +4965,15 @@ [0, 0, 0, 0, 0, 0, 0, 0]], bool) out = ndimage.binary_propagation(data, struct, - mask = mask, border_value = 0) - self.assertTrue(diff(out, true) < eps) + mask=mask, border_value=0) + assert_array_almost_equal(out, expected) def test_binary_propagation02(self): "binary propagation 2" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 1, 0, 0, 0, 0, 0, 0], + expected = [[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, 0, 0, 0], @@ -4980,12 +4991,12 @@ [0, 0, 0, 0, 0, 0, 0, 0]], bool) data = numpy.zeros(mask.shape, bool) out = ndimage.binary_propagation(data, struct, - mask = mask, border_value = 1) - self.assertTrue(diff(out, true) < eps) + mask=mask, border_value=1) + assert_array_almost_equal(out, expected) def test_binary_opening01(self): "binary opening 1" - true = [[0, 1, 0, 0, 0, 0, 0, 0], + expected = [[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, 0, 1, 1, 1, 0], @@ -5003,12 +5014,12 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_opening(data) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_binary_opening02(self): "binary opening 2" struct = ndimage.generate_binary_structure(2, 2) - true = [[1, 1, 1, 0, 0, 0, 0, 0], + expected = [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], @@ -5026,11 +5037,11 @@ [0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_opening(data, struct) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_binary_closing01(self): "binary closing 1" - true = [[0, 0, 0, 0, 0, 0, 0, 0], + expected = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0], @@ -5048,12 +5059,12 @@ [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_closing(data) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_binary_closing02(self): "binary closing 2" struct = ndimage.generate_binary_structure(2, 2) - true = [[0, 0, 0, 0, 0, 0, 0, 0], + expected = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0], @@ -5071,11 +5082,11 @@ [0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_closing(data, struct) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_binary_fill_holes01(self): "binary fill holes 1" - true = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], + expected = 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], @@ -5090,11 +5101,11 @@ [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) out = ndimage.binary_fill_holes(data) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_binary_fill_holes02(self): "binary fill holes 2" - true = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], + expected = 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], @@ -5109,11 +5120,11 @@ [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], bool) out = ndimage.binary_fill_holes(data) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_binary_fill_holes03(self): "binary fill holes 3" - true = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0], + expected = 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], @@ -5128,7 +5139,7 @@ [0, 0, 1, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0]], bool) out = ndimage.binary_fill_holes(data) - self.assertTrue(diff(out, true) < eps) + assert_array_almost_equal(out, expected) def test_grey_erosion01(self): "grey erosion 1" @@ -5137,10 +5148,10 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] output = ndimage.grey_erosion(array, - footprint = footprint) - self.assertTrue(diff([[2, 2, 1, 1, 1], + footprint=footprint) + assert_array_almost_equal([[2, 2, 1, 1, 1], [2, 3, 1, 3, 1], - [5, 5, 3, 3, 1]], output) < eps) + [5, 5, 3, 3, 1]], output) def test_grey_erosion02(self): "grey erosion 2" @@ -5150,10 +5161,10 @@ footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] output = ndimage.grey_erosion(array, - footprint = footprint, structure = structure) - self.assertTrue(diff([[2, 2, 1, 1, 1], + footprint=footprint, structure=structure) + assert_array_almost_equal([[2, 2, 1, 1, 1], [2, 3, 1, 3, 1], - [5, 5, 3, 3, 1]], output) < eps) + [5, 5, 3, 3, 1]], output) def test_grey_erosion03(self): "grey erosion 3" @@ -5163,10 +5174,10 @@ footprint = [[1, 0, 1], [1, 1, 0]] structure = [[1, 1, 1], [1, 1, 1]] output = ndimage.grey_erosion(array, - footprint = footprint, structure = structure) - self.assertTrue(diff([[1, 1, 0, 0, 0], + footprint=footprint, structure=structure) + assert_array_almost_equal([[1, 1, 0, 0, 0], [1, 2, 0, 2, 0], - [4, 4, 2, 2, 0]], output) < eps) + [4, 4, 2, 2, 0]], output) def test_grey_dilation01(self): "grey dilation 1" @@ -5175,10 +5186,10 @@ [5, 8, 3, 7, 1]]) footprint = [[0, 1, 1], [1, 0, 1]] output = ndimage.grey_dilation(array, - footprint = footprint) - self.assertTrue(diff([[7, 7, 9, 9, 5], + footprint=footprint) + assert_array_almost_equal([[7, 7, 9, 9, 5], [7, 9, 8, 9, 7], - [8, 8, 8, 7, 7]], output) < eps) + [8, 8, 8, 7, 7]], output) def test_grey_dilation02(self): "grey dilation 2" @@ -5188,10 +5199,10 @@ footprint = [[0, 1, 1], [1, 0, 1]] structure = [[0, 0, 0], [0, 0, 0]] output = ndimage.grey_dilation(array, - footprint = footprint, structure = structure) - self.assertTrue(diff([[7, 7, 9, 9, 5], + footprint=footprint, structure=structure) + assert_array_almost_equal([[7, 7, 9, 9, 5], [7, 9, 8, 9, 7], - [8, 8, 8, 7, 7]], output) < eps) + [8, 8, 8, 7, 7]], output) def test_grey_dilation03(self): "grey dilation 3" @@ -5201,10 +5212,10 @@ footprint = [[0, 1, 1], [1, 0, 1]] structure = [[1, 1, 1], [1, 1, 1]] output = ndimage.grey_dilation(array, - footprint = footprint, structure = structure) - self.assertTrue(diff([[8, 8, 10, 10, 6], + footprint=footprint, structure=structure) + assert_array_almost_equal([[8, 8, 10, 10, 6], [8, 10, 9, 10, 8], - [9, 9, 9, 8, 8]], output) < eps) + [9, 9, 9, 8, 8]], output) def test_grey_opening01(self): "grey opening 1" @@ -5212,11 +5223,11 @@ [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] - tmp = ndimage.grey_erosion(array, footprint = footprint) - true = ndimage.grey_dilation(tmp, footprint = footprint) + tmp = ndimage.grey_erosion(array, footprint=footprint) + expected = ndimage.grey_dilation(tmp, footprint=footprint) output = ndimage.grey_opening(array, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) def test_grey_opening02(self): @@ -5226,13 +5237,13 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] - tmp = ndimage.grey_erosion(array, footprint = footprint, - structure = structure) - true = ndimage.grey_dilation(tmp, footprint = footprint, - structure = structure) + tmp = ndimage.grey_erosion(array, footprint=footprint, + structure=structure) + expected = ndimage.grey_dilation(tmp, footprint=footprint, + structure=structure) output = ndimage.grey_opening(array, - footprint = footprint, structure = structure) - self.assertTrue(diff(true, output) < eps) + footprint=footprint, structure=structure) + assert_array_almost_equal(expected, output) def test_grey_closing01(self): "grey closing 1" @@ -5240,11 +5251,11 @@ [7, 6, 9, 3, 5], [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] - tmp = ndimage.grey_dilation(array, footprint = footprint) - true = ndimage.grey_erosion(tmp, footprint = footprint) + tmp = ndimage.grey_dilation(array, footprint=footprint) + expected = ndimage.grey_erosion(tmp, footprint=footprint) output = ndimage.grey_closing(array, - footprint = footprint) - self.assertTrue(diff(true, output) < eps) + footprint=footprint) + assert_array_almost_equal(expected, output) def test_grey_closing02(self): "grey closing 2" @@ -5253,13 +5264,13 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] - tmp = ndimage.grey_dilation(array, footprint = footprint, - structure = structure) - true = ndimage.grey_erosion(tmp, footprint = footprint, - structure = structure) + tmp = ndimage.grey_dilation(array, footprint=footprint, + structure=structure) + expected = ndimage.grey_erosion(tmp, footprint=footprint, + structure=structure) output = ndimage.grey_closing(array, - footprint = footprint, structure = structure) - self.assertTrue(diff(true, output) < eps) + footprint=footprint, structure=structure) + assert_array_almost_equal(expected, output) def test_morphological_gradient01(self): "morphological gradient 1" @@ -5269,14 +5280,14 @@ footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] tmp1 = ndimage.grey_dilation(array, - footprint = footprint, structure = structure) - tmp2 = ndimage.grey_erosion(array, footprint = footprint, - structure = structure) - true = tmp1 - tmp2 + footprint=footprint, structure=structure) + tmp2 = ndimage.grey_erosion(array, footprint=footprint, + structure=structure) + expected = tmp1 - tmp2 output = numpy.zeros(array.shape, array.dtype) ndimage.morphological_gradient(array, - footprint=footprint, structure=structure, output = output) - self.assertTrue(diff(true, output) < eps) + footprint=footprint, structure=structure, output=output) + assert_array_almost_equal(expected, output) def test_morphological_gradient02(self): "morphological gradient 2" @@ -5286,13 +5297,13 @@ footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] tmp1 = ndimage.grey_dilation(array, - footprint = footprint, structure = structure) - tmp2 = ndimage.grey_erosion(array, footprint = footprint, - structure = structure) - true = tmp1 - tmp2 + footprint=footprint, structure=structure) + tmp2 = ndimage.grey_erosion(array, footprint=footprint, + structure=structure) + expected = tmp1 - tmp2 output =ndimage.morphological_gradient(array, footprint=footprint, structure=structure) - self.assertTrue(diff(true, output) < eps) + assert_array_almost_equal(expected, output) def test_morphological_laplace01(self): "morphological laplace 1" @@ -5302,14 +5313,14 @@ footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] tmp1 = ndimage.grey_dilation(array, - footprint = footprint, structure = structure) - tmp2 = ndimage.grey_erosion(array, footprint = footprint, - structure = structure) - true = tmp1 + tmp2 - 2 * array + footprint=footprint, structure=structure) + tmp2 = ndimage.grey_erosion(array, footprint=footprint, + structure=structure) + expected = tmp1 + tmp2 - 2 * array output = numpy.zeros(array.shape, array.dtype) ndimage.morphological_laplace(array, footprint=footprint, - structure=structure, output = output) - self.assertTrue(diff(true, output) < eps) + structure=structure, output=output) + assert_array_almost_equal(expected, output) def test_morphological_laplace02(self): "morphological laplace 2" @@ -5319,13 +5330,13 @@ footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] tmp1 = ndimage.grey_dilation(array, - footprint = footprint, structure = structure) - tmp2 = ndimage.grey_erosion(array, footprint = footprint, - structure = structure) - true = tmp1 + tmp2 - 2 * array + footprint=footprint, structure=structure) + tmp2 = ndimage.grey_erosion(array, footprint=footprint, + structure=structure) + expected = tmp1 + tmp2 - 2 * array output = ndimage.morphological_laplace(array, footprint=footprint, structure=structure) - self.assertTrue(diff(true, output) < eps) + assert_array_almost_equal(expected, output) def test_white_tophat01(self): "white tophat 1" @@ -5334,13 +5345,13 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] - tmp = ndimage.grey_opening(array, footprint = footprint, - structure = structure) - true = array - tmp + tmp = ndimage.grey_opening(array, footprint=footprint, + structure=structure) + expected = array - tmp output = numpy.zeros(array.shape, array.dtype) ndimage.white_tophat(array, footprint=footprint, - structure=structure, output = output) - self.assertTrue(diff(true, output) < eps) + structure=structure, output=output) + assert_array_almost_equal(expected, output) def test_white_tophat02(self): "white tophat 2" @@ -5349,12 +5360,12 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] - tmp = ndimage.grey_opening(array, footprint = footprint, - structure = structure) - true = array - tmp + tmp = ndimage.grey_opening(array, footprint=footprint, + structure=structure) + expected = array - tmp output = ndimage.white_tophat(array, footprint=footprint, structure=structure) - self.assertTrue(diff(true, output) < eps) + assert_array_almost_equal(expected, output) def test_black_tophat01(self): "black tophat 1" @@ -5363,13 +5374,13 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] - tmp = ndimage.grey_closing(array, footprint = footprint, - structure = structure) - true = tmp - array + tmp = ndimage.grey_closing(array, footprint=footprint, + structure=structure) + expected = tmp - array output = numpy.zeros(array.shape, array.dtype) ndimage.black_tophat(array, footprint=footprint, - structure=structure, output = output) - self.assertTrue(diff(true, output) < eps) + structure=structure, output=output) + assert_array_almost_equal(expected, output) def test_black_tophat02(self): "black tophat 2" @@ -5378,19 +5389,19 @@ [5, 8, 3, 7, 1]]) footprint = [[1, 0, 1], [1, 1, 0]] structure = [[0, 0, 0], [0, 0, 0]] - tmp = ndimage.grey_closing(array, footprint = footprint, - structure = structure) - true = tmp - array + tmp = ndimage.grey_closing(array, footprint=footprint, + structure=structure) + expected = tmp - array output = ndimage.black_tophat(array, footprint=footprint, structure=structure) - self.assertTrue(diff(true, output) < eps) + assert_array_almost_equal(expected, output) def test_hit_or_miss01(self): "binary hit-or-miss transform 1" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0], + expected = [[0, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], @@ -5409,15 +5420,15 @@ [0, 0, 0, 0, 0]], type) out = numpy.zeros(data.shape, bool) ndimage.binary_hit_or_miss(data, struct, - output = out) - self.assertTrue(diff(true, out) < eps) + output=out) + assert_array_almost_equal(expected, out) def test_hit_or_miss02(self): "binary hit-or-miss transform 2" struct = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] - true = [[0, 0, 0, 0, 0, 0, 0, 0], + expected = [[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]] @@ -5427,7 +5438,7 @@ [0, 1, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_hit_or_miss(data, struct) - self.assertTrue(diff(true, out) < eps) + assert_array_almost_equal(expected, out) def test_hit_or_miss03(self): "binary hit-or-miss transform 3" @@ -5437,7 +5448,7 @@ struct2 = [[1, 1, 1], [0, 0, 0], [1, 1, 1]] - true = [[0, 0, 0, 0, 0, 1, 0, 0], + expected = [[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], @@ -5456,7 +5467,7 @@ [0, 0, 0, 0, 0, 0, 0, 0]], type) out = ndimage.binary_hit_or_miss(data, struct1, struct2) - self.assertTrue(diff(true, out) < eps) + assert_array_almost_equal(expected, out) #class NDImageTestResult(unittest.TestResult): From scipy-svn at scipy.org Thu Oct 14 11:37:58 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 14 Oct 2010 10:37:58 -0500 (CDT) Subject: [Scipy-svn] r6838 - trunk/scipy/ndimage/tests Message-ID: <20101014153758.D1A9437A4BD@scipy.org> Author: stefan Date: 2010-10-14 10:37:58 -0500 (Thu, 14 Oct 2010) New Revision: 6838 Modified: trunk/scipy/ndimage/tests/test_ndimage.py Log: STY: ndimage: Remove 'diff' helper function from test suite. Modified: trunk/scipy/ndimage/tests/test_ndimage.py =================================================================== --- trunk/scipy/ndimage/tests/test_ndimage.py 2010-10-14 15:37:27 UTC (rev 6837) +++ trunk/scipy/ndimage/tests/test_ndimage.py 2010-10-14 15:37:58 UTC (rev 6838) @@ -39,33 +39,9 @@ eps = 1e-12 -### -### TODO: Remove this helper function and replace by functionality -### inside numpy.testing. -### -def diff(a, b): - if not isinstance(a, numpy.ndarray): - a = numpy.asarray(a) - if not isinstance(b, numpy.ndarray): - b = numpy.asarray(b) - if (0 in a.shape) and (0 in b.shape): - return 0.0 - 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() - if (a.dtype == numpy.object or b.dtype == numpy.object): - t = sum([diff(c,d)**2 for c,d in zip(a,b)]) - else: - 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) +def sumsq(a, b): + return math.sqrt(((a - b)**2).sum()) - class TestNdimage: def setUp(self): @@ -490,7 +466,7 @@ assert_equal(input.shape, output.shape) assert_almost_equal(output.sum(), input.sum()) - assert_(diff(input, output) > 1.0) + assert_(sumsq(input, output) > 1.0) def test_gauss04(self): "gaussian filter 4" @@ -501,7 +477,7 @@ output=otype) assert_equal(output.dtype.type, numpy.float64) assert_equal(input.shape, output.shape) - assert_(diff(input, output) > 1.0) + assert_(sumsq(input, output) > 1.0) def test_gauss05(self): "gaussian filter 5" @@ -512,7 +488,7 @@ order=1, output=otype) assert_equal(output.dtype.type, numpy.float64) assert_equal(input.shape, output.shape) - assert_(diff(input, output) > 1.0) + assert_(sumsq(input, output) > 1.0) def test_gauss06(self): "gaussian filter 6" @@ -1742,10 +1718,9 @@ return (x[0] * 2, x[1] * 2) for order in range(0, 6): out = ndimage.geometric_transform(data, mapping1, - (6, 8), order=order) + (6, 8), order=order) out = ndimage.geometric_transform(out, mapping2, - (3, 4), order=order) - error = diff(out, data) + (3, 4), order=order) assert_array_almost_equal(out, data) def test_geometric_transform23(self): From scipy-svn at scipy.org Thu Oct 14 11:38:36 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 14 Oct 2010 10:38:36 -0500 (CDT) Subject: [Scipy-svn] r6839 - trunk/scipy/ndimage/tests Message-ID: <20101014153836.EB75E37A4BD@scipy.org> Author: stefan Date: 2010-10-14 10:38:36 -0500 (Thu, 14 Oct 2010) New Revision: 6839 Added: trunk/scipy/ndimage/tests/test_measurements.py Modified: trunk/scipy/ndimage/tests/test_ndimage.py Log: ENH: ndimage: Factor out measurement tests. Added: trunk/scipy/ndimage/tests/test_measurements.py =================================================================== --- trunk/scipy/ndimage/tests/test_measurements.py (rev 0) +++ trunk/scipy/ndimage/tests/test_measurements.py 2010-10-14 15:38:36 UTC (rev 6839) @@ -0,0 +1,824 @@ +from numpy.testing import assert_, assert_array_almost_equal, assert_equal, \ + assert_almost_equal, \ + run_module_suite +import numpy as np + +import scipy.ndimage as ndimage + +types = [np.int8, np.uint8, np.int16, + np.uint16, np.int32, np.uint32, + np.int64, np.uint64, + np.float32, np.float64] + +def test_label01(): + "label 1" + data = np.ones([]) + out, n = ndimage.label(data) + assert_array_almost_equal(out, 1) + +def test_label02(): + "label 2" + data = np.zeros([]) + out, n = ndimage.label(data) + assert_array_almost_equal(out, 0) + +def test_label03(): + "label 3" + data = np.ones([1]) + out, n = ndimage.label(data) + assert_array_almost_equal(out, [1]) + +def test_label04(): + "label 4" + data = np.zeros([1]) + out, n = ndimage.label(data) + assert_array_almost_equal(out, [0]) + +def test_label05(): + "label 5" + data = np.ones([5]) + out, n = ndimage.label(data) + assert_array_almost_equal(out, [1, 1, 1, 1, 1]) + +def test_label06(): + "label 6" + data = np.array([1, 0, 1, 1, 0, 1]) + out, n = ndimage.label(data) + assert_array_almost_equal(out, [1, 0, 2, 2, 0, 3]) + +def test_label07(): + "label 7" + data = np.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], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0]]) + out, n = ndimage.label(data) + assert_array_almost_equal(out, [[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, 0, 0, 0, 0, 0]]) + +def test_label08(): + "label 8" + data = np.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], + [1, 1, 0, 0, 0, 0], + [0, 0, 0, 1, 1, 0]]) + out, n = ndimage.label(data) + assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0], + [0, 0, 2, 2, 0, 0], + [0, 0, 2, 2, 2, 0], + [3, 3, 0, 0, 0, 0], + [3, 3, 0, 0, 0, 0], + [0, 0, 0, 4, 4, 0]]) + +def test_label09(): + "label 9" + data = np.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], + [1, 1, 0, 0, 0, 0], + [0, 0, 0, 1, 1, 0]]) + struct = ndimage.generate_binary_structure(2, 2) + out, n = ndimage.label(data, struct) + assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0], + [0, 0, 2, 2, 0, 0], + [0, 0, 2, 2, 2, 0], + [2, 2, 0, 0, 0, 0], + [2, 2, 0, 0, 0, 0], + [0, 0, 0, 3, 3, 0]]) + +def test_label10(): + "label 10" + data = np.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]]) + struct = ndimage.generate_binary_structure(2, 2) + out, n = ndimage.label(data, struct) + assert_array_almost_equal(out, [[0, 0, 0, 0, 0, 0], + [0, 1, 1, 0, 1, 0], + [0, 1, 1, 1, 1, 0], + [0, 0, 0, 0, 0, 0]]) + +def test_label11(): + "label 11" + for type in types: + data = np.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], + [1, 1, 0, 0, 0, 0], + [0, 0, 0, 1, 1, 0]], type) + out, n = ndimage.label(data) + expected = [[1, 0, 0, 0, 0, 0], + [0, 0, 2, 2, 0, 0], + [0, 0, 2, 2, 2, 0], + [3, 3, 0, 0, 0, 0], + [3, 3, 0, 0, 0, 0], + [0, 0, 0, 4, 4, 0]] + assert_array_almost_equal(out, expected) + assert_equal(n, 4) + +def test_label12(): + "label 12" + for type in types: + data = np.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], + [0, 0, 0, 1, 1, 0]], type) + out, n = ndimage.label(data) + expected = [[0, 0, 0, 0, 1, 1], + [0, 0, 0, 0, 0, 1], + [0, 0, 1, 0, 1, 1], + [0, 0, 1, 1, 1, 1], + [0, 0, 0, 1, 1, 0]] + assert_array_almost_equal(out, expected) + assert_equal(n, 1) + +def test_label13(): + "label 13" + for type in types: + data = np.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]], + type) + out, n = ndimage.label(data) + expected = [[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]] + assert_array_almost_equal(out, expected) + assert_equal(n, 1) + +def test_find_objects01(): + "find_objects 1" + data = np.ones([], dtype=int) + out = ndimage.find_objects(data) + assert_(out == [()]) + +def test_find_objects02(): + "find_objects 2" + data = np.zeros([], dtype=int) + out = ndimage.find_objects(data) + assert_(out == []) + +def test_find_objects03(): + "find_objects 3" + data = np.ones([1], dtype=int) + out = ndimage.find_objects(data) + assert_equal(out, [(slice(0, 1, None),)]) + +def test_find_objects04(): + "find_objects 4" + data = np.zeros([1], dtype=int) + out = ndimage.find_objects(data) + assert_equal(out, []) + +def test_find_objects05(): + "find_objects 5" + data = np.ones([5], dtype=int) + out = ndimage.find_objects(data) + assert_equal(out, [(slice(0, 5, None),)]) + +def test_find_objects06(): + "find_objects 6" + data = np.array([1, 0, 2, 2, 0, 3]) + out = ndimage.find_objects(data) + assert_equal(out, [(slice(0, 1, None),), + (slice(2, 4, None),), + (slice(5, 6, None),)]) + +def test_find_objects07(): + "find_objects 7" + data = np.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], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0]]) + out = ndimage.find_objects(data) + assert_equal(out, []) + +def test_find_objects08(): + "find_objects 8" + data = np.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], + [3, 3, 0, 0, 0, 0], + [0, 0, 0, 4, 4, 0]]) + out = ndimage.find_objects(data) + assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)), + (slice(1, 3, None), slice(2, 5, None)), + (slice(3, 5, None), slice(0, 2, None)), + (slice(5, 6, None), slice(3, 5, None))]) + +def test_find_objects09(): + "find_objects 9" + data = np.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], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 4, 4, 0]]) + out = ndimage.find_objects(data) + assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)), + (slice(1, 3, None), slice(2, 5, None)), + None, + (slice(5, 6, None), slice(3, 5, None))]) + +def test_sum01(): + "sum 1" + for type in types: + input = np.array([], type) + output = ndimage.sum(input) + assert_equal(output, 0.0) + +def test_sum02(): + "sum 2" + for type in types: + input = np.zeros([0, 4], type) + output = ndimage.sum(input) + assert_equal(output, 0.0) + +def test_sum03(): + "sum 3" + for type in types: + input = np.ones([], type) + output = ndimage.sum(input) + assert_almost_equal(output, 1.0) + +def test_sum04(): + "sum 4" + for type in types: + input = np.array([1, 2], type) + output = ndimage.sum(input) + assert_almost_equal(output, 3.0) + +def test_sum05(): + "sum 5" + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.sum(input) + assert_almost_equal(output, 10.0) + +def test_sum06(): + "sum 6" + labels = np.array([], bool) + for type in types: + input = np.array([], type) + output = ndimage.sum(input, labels=labels) + assert_equal(output, 0.0) + +def test_sum07(): + "sum 7" + labels = np.ones([0, 4], bool) + for type in types: + input = np.zeros([0, 4], type) + output = ndimage.sum(input, labels=labels) + assert_equal(output, 0.0) + +def test_sum08(): + "sum 8" + labels = np.array([1, 0], bool) + for type in types: + input = np.array([1, 2], type) + output = ndimage.sum(input, labels=labels) + assert_equal(output, 1.0) + +def test_sum09(): + "sum 9" + labels = np.array([1, 0], bool) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.sum(input, labels=labels) + assert_almost_equal(output, 4.0) + +def test_sum10(): + "sum 10" + labels = np.array([1, 0], bool) + input = np.array([[1, 2], [3, 4]], bool) + output = ndimage.sum(input, labels=labels) + assert_almost_equal(output, 2.0) + +def test_sum11(): + "sum 11" + labels = np.array([1, 2], np.int8) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.sum(input, labels=labels, + index=2) + assert_almost_equal(output, 6.0) + +def test_sum12(): + "sum 12" + labels = np.array([[1, 2], [2, 4]], np.int8) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.sum(input, labels=labels, + index=[4, 8, 2]) + assert_array_almost_equal(output, [4.0, 0.0, 5.0]) + +def test_mean01(): + "mean 1" + labels = np.array([1, 0], bool) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.mean(input, labels=labels) + assert_almost_equal(output, 2.0) + +def test_mean02(): + "mean 2" + labels = np.array([1, 0], bool) + input = np.array([[1, 2], [3, 4]], bool) + output = ndimage.mean(input, labels=labels) + assert_almost_equal(output, 1.0) + +def test_mean03(): + "mean 3" + labels = np.array([1, 2]) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.mean(input, labels=labels, + index=2) + assert_almost_equal(output, 3.0) + +def test_mean04(): + "mean 4" + labels = np.array([[1, 2], [2, 4]], np.int8) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.mean(input, labels=labels, + index=[4, 8, 2]) + assert_array_almost_equal(output[[0,2]], [4.0, 2.5]) + assert_(np.isnan(output[1])) + +def test_minimum01(): + "minimum 1" + labels = np.array([1, 0], bool) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.minimum(input, labels=labels) + assert_almost_equal(output, 1.0) + +def test_minimum02(): + "minimum 2" + labels = np.array([1, 0], bool) + input = np.array([[2, 2], [2, 4]], bool) + output = ndimage.minimum(input, labels=labels) + assert_almost_equal(output, 1.0) + +def test_minimum03(): + "minimum 3" + labels = np.array([1, 2]) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.minimum(input, labels=labels, + index=2) + assert_almost_equal(output, 2.0) + +def test_minimum04(): + "minimum 4" + labels = np.array([[1, 2], [2, 3]]) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.minimum(input, labels=labels, + index=[2, 3, 8]) + assert_array_almost_equal(output, [2.0, 4.0, 0.0]) + +def test_maximum01(): + "maximum 1" + labels = np.array([1, 0], bool) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.maximum(input, labels=labels) + assert_almost_equal(output, 3.0) + +def test_maximum02(): + "maximum 2" + labels = np.array([1, 0], bool) + input = np.array([[2, 2], [2, 4]], bool) + output = ndimage.maximum(input, labels=labels) + assert_almost_equal(output, 1.0) + +def test_maximum03(): + "maximum 3" + labels = np.array([1, 2]) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.maximum(input, labels=labels, + index=2) + assert_almost_equal(output, 4.0) + +def test_maximum04(): + "maximum 4" + labels = np.array([[1, 2], [2, 3]]) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.maximum(input, labels=labels, + index=[2, 3, 8]) + assert_array_almost_equal(output, [3.0, 4.0, 0.0]) + +def test_maximum05(): + "Ticket #501" + x = np.array([-3,-2,-1]) + assert_equal(ndimage.maximum(x),-1) + +def test_variance01(): + "variance 1" + for type in types: + input = np.array([], type) + output = ndimage.variance(input) + assert_(np.isnan(output)) + +def test_variance02(): + "variance 2" + for type in types: + input = np.array([1], type) + output = ndimage.variance(input) + assert_almost_equal(output, 0.0) + +def test_variance03(): + "variance 3" + for type in types: + input = np.array([1, 3], type) + output = ndimage.variance(input) + assert_almost_equal(output, 1.0) + +def test_variance04(): + "variance 4" + input = np.array([1, 0], bool) + output = ndimage.variance(input) + assert_almost_equal(output, 0.25) + +def test_variance05(): + "variance 5" + labels = [2, 2, 3] + for type in types: + input = np.array([1, 3, 8], type) + output = ndimage.variance(input, labels, 2) + assert_almost_equal(output, 1.0) + +def test_variance06(): + "variance 6" + labels = [2, 2, 3, 3, 4] + for type in types: + input = np.array([1, 3, 8, 10, 8], type) + output = ndimage.variance(input, labels, [2, 3, 4]) + assert_array_almost_equal(output, [1.0, 1.0, 0.0]) + +def test_standard_deviation01(): + "standard deviation 1" + for type in types: + input = np.array([], type) + output = ndimage.standard_deviation(input) + assert_(np.isnan(output)) + +def test_standard_deviation02(): + "standard deviation 2" + for type in types: + input = np.array([1], type) + output = ndimage.standard_deviation(input) + assert_almost_equal(output, 0.0) + +def test_standard_deviation03(): + "standard deviation 3" + for type in types: + input = np.array([1, 3], type) + output = ndimage.standard_deviation(input) + assert_almost_equal(output, np.sqrt(1.0)) + +def test_standard_deviation04(): + "standard deviation 4" + input = np.array([1, 0], bool) + output = ndimage.standard_deviation(input) + assert_almost_equal(output, 0.5) + +def test_standard_deviation05(): + "standard deviation 5" + labels = [2, 2, 3] + for type in types: + input = np.array([1, 3, 8], type) + output = ndimage.standard_deviation(input, labels, 2) + assert_almost_equal(output, 1.0) + +def test_standard_deviation06(): + "standard deviation 6" + labels = [2, 2, 3, 3, 4] + for type in types: + input = np.array([1, 3, 8, 10, 8], type) + output = ndimage.standard_deviation(input, labels, + [2, 3, 4]) + assert_array_almost_equal(output, [1.0, 1.0, 0.0]) + +def test_standard_deviation07(): + "standard deviation 7" + labels = [1] + for type in types: + input = np.array([-0.00619519], type) + output = ndimage.standard_deviation(input, labels, [1]) + assert_array_almost_equal(output, [0]) + +def test_minimum_position01(): + "minimum position 1" + labels = np.array([1, 0], bool) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.minimum_position(input, + labels=labels) + assert_equal(output, (0, 0)) + +def test_minimum_position02(): + "minimum position 2" + for type in types: + input = np.array([[5, 4, 2, 5], + [3, 7, 0, 2], + [1, 5, 1, 1]], type) + output = ndimage.minimum_position(input) + assert_equal(output, (1, 2)) + +def test_minimum_position03(): + "minimum position 3" + input = np.array([[5, 4, 2, 5], + [3, 7, 0, 2], + [1, 5, 1, 1]], bool) + output = ndimage.minimum_position(input) + assert_equal(output, (1, 2)) + +def test_minimum_position04(): + "minimum position 4" + input = np.array([[5, 4, 2, 5], + [3, 7, 1, 2], + [1, 5, 1, 1]], bool) + output = ndimage.minimum_position(input) + assert_equal(output, (0, 0)) + +def test_minimum_position05(): + "minimum position 5" + labels = [1, 2, 0, 4] + for type in types: + input = np.array([[5, 4, 2, 5], + [3, 7, 0, 2], + [1, 5, 2, 3]], type) + output = ndimage.minimum_position(input, labels) + assert_equal(output, (2, 0)) + +def test_minimum_position06(): + "minimum position 6" + labels = [1, 2, 3, 4] + for type in types: + input = np.array([[5, 4, 2, 5], + [3, 7, 0, 2], + [1, 5, 1, 1]], type) + output = ndimage.minimum_position(input, labels, 2) + assert_equal(output, (0, 1)) + +def test_minimum_position07(): + "minimum position 7" + labels = [1, 2, 3, 4] + for type in types: + input = np.array([[5, 4, 2, 5], + [3, 7, 0, 2], + [1, 5, 1, 1]], type) + output = ndimage.minimum_position(input, labels, + [2, 3]) + assert_equal(output[0], (0, 1)) + assert_equal(output[1], (1, 2)) + +def test_maximum_position01(): + "maximum position 1" + labels = np.array([1, 0], bool) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output = ndimage.maximum_position(input, + labels=labels) + assert_equal(output, (1, 0)) + +def test_maximum_position02(): + "maximum position 2" + for type in types: + input = np.array([[5, 4, 2, 5], + [3, 7, 8, 2], + [1, 5, 1, 1]], type) + output = ndimage.maximum_position(input) + assert_equal(output, (1, 2)) + +def test_maximum_position03(): + "maximum position 3" + input = np.array([[5, 4, 2, 5], + [3, 7, 8, 2], + [1, 5, 1, 1]], bool) + output = ndimage.maximum_position(input) + assert_equal(output, (0, 0)) + +def test_maximum_position04(): + "maximum position 4" + labels = [1, 2, 0, 4] + for type in types: + input = np.array([[5, 4, 2, 5], + [3, 7, 8, 2], + [1, 5, 1, 1]], type) + output = ndimage.maximum_position(input, labels) + assert_equal(output, (1, 1)) + +def test_maximum_position05(): + "maximum position 5" + labels = [1, 2, 0, 4] + for type in types: + input = np.array([[5, 4, 2, 5], + [3, 7, 8, 2], + [1, 5, 1, 1]], type) + output = ndimage.maximum_position(input, labels, 1) + assert_equal(output, (0, 0)) + +def test_maximum_position06(): + "maximum position 6" + labels = [1, 2, 0, 4] + for type in types: + input = np.array([[5, 4, 2, 5], + [3, 7, 8, 2], + [1, 5, 1, 1]], type) + output = ndimage.maximum_position(input, labels, + [1, 2]) + assert_equal(output[0], (0, 0)) + assert_equal(output[1], (1, 1)) + +def test_extrema01(): + "extrema 1" + labels = np.array([1, 0], bool) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output1 = ndimage.extrema(input, labels=labels) + output2 = ndimage.minimum(input, labels=labels) + output3 = ndimage.maximum(input, labels=labels) + output4 = ndimage.minimum_position(input, + labels=labels) + output5 = ndimage.maximum_position(input, + labels=labels) + assert_equal(output1, (output2, output3, output4, output5)) + +def test_extrema02(): + "extrema 2" + labels = np.array([1, 2]) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output1 = ndimage.extrema(input, labels=labels, + index=2) + output2 = ndimage.minimum(input, labels=labels, + index=2) + output3 = ndimage.maximum(input, labels=labels, + index=2) + output4 = ndimage.minimum_position(input, + labels=labels, index=2) + output5 = ndimage.maximum_position(input, + labels=labels, index=2) + assert_equal(output1, (output2, output3, output4, output5)) + +def test_extrema03(): + "extrema 3" + labels = np.array([[1, 2], [2, 3]]) + for type in types: + input = np.array([[1, 2], [3, 4]], type) + output1 = ndimage.extrema(input, labels=labels, + index=[2, 3, 8]) + output2 = ndimage.minimum(input, labels=labels, + index=[2, 3, 8]) + output3 = ndimage.maximum(input, labels=labels, + index=[2, 3, 8]) + output4 = ndimage.minimum_position(input, + labels=labels, index=[2, 3, 8]) + output5 = ndimage.maximum_position(input, + labels=labels, index=[2, 3, 8]) + assert_array_almost_equal(output1[0], output2) + assert_array_almost_equal(output1[1], output3) + assert_array_almost_equal(output1[2], output4) + assert_array_almost_equal(output1[3], output5) + +def test_extrema04(): + "extrema 4" + labels = [1, 2, 0, 4] + for type in types: + input = np.array([[5, 4, 2, 5], + [3, 7, 8, 2], + [1, 5, 1, 1]], type) + output1 = ndimage.extrema(input, labels, [1, 2]) + output2 = ndimage.minimum(input, labels, [1, 2]) + output3 = ndimage.maximum(input, labels, [1, 2]) + output4 = ndimage.minimum_position(input, labels, + [1, 2]) + output5 = ndimage.maximum_position(input, labels, + [1, 2]) + assert_array_almost_equal(output1[0], output2) + assert_array_almost_equal(output1[1], output3) + assert_array_almost_equal(output1[2], output4) + assert_array_almost_equal(output1[3], output5) + +def test_center_of_mass01(): + "center of mass 1" + expected = [0.0, 0.0] + for type in types: + input = np.array([[1, 0], [0, 0]], type) + output = ndimage.center_of_mass(input) + assert_array_almost_equal(output, expected) + +def test_center_of_mass02(): + "center of mass 2" + expected = [1, 0] + for type in types: + input = np.array([[0, 0], [1, 0]], type) + output = ndimage.center_of_mass(input) + assert_array_almost_equal(output, expected) + +def test_center_of_mass03(): + "center of mass 3" + expected = [0, 1] + for type in types: + input = np.array([[0, 1], [0, 0]], type) + output = ndimage.center_of_mass(input) + assert_array_almost_equal(output, expected) + +def test_center_of_mass04(): + "center of mass 4" + expected = [1, 1] + for type in types: + input = np.array([[0, 0], [0, 1]], type) + output = ndimage.center_of_mass(input) + assert_array_almost_equal(output, expected) + +def test_center_of_mass05(): + "center of mass 5" + expected = [0.5, 0.5] + for type in types: + input = np.array([[1, 1], [1, 1]], type) + output = ndimage.center_of_mass(input) + assert_array_almost_equal(output, expected) + +def test_center_of_mass06(): + "center of mass 6" + expected = [0.5, 0.5] + input = np.array([[1, 2], [3, 1]], bool) + output = ndimage.center_of_mass(input) + assert_array_almost_equal(output, expected) + +def test_center_of_mass07(): + "center of mass 7" + labels = [1, 0] + expected = [0.5, 0.0] + input = np.array([[1, 2], [3, 1]], bool) + output = ndimage.center_of_mass(input, labels) + assert_array_almost_equal(output, expected) + +def test_center_of_mass08(): + "center of mass 8" + labels = [1, 2] + expected = [0.5, 1.0] + input = np.array([[5, 2], [3, 1]], bool) + output = ndimage.center_of_mass(input, labels, 2) + assert_array_almost_equal(output, expected) + +def test_center_of_mass09(): + "center of mass 9" + labels = [1, 2] + expected = [(0.5, 0.0), (0.5, 1.0)] + input = np.array([[1, 2], [1, 1]], bool) + output = ndimage.center_of_mass(input, labels, [1, 2]) + assert_array_almost_equal(output, expected) + +def test_histogram01(): + "histogram 1" + expected = np.ones(10) + input = np.arange(10) + output = ndimage.histogram(input, 0, 10, 10) + assert_array_almost_equal(output, expected) + +def test_histogram02(): + "histogram 2" + labels = [1, 1, 1, 1, 2, 2, 2, 2] + expected = [0, 2, 0, 1, 1] + input = np.array([1, 1, 3, 4, 3, 3, 3, 3]) + output = ndimage.histogram(input, 0, 4, 5, labels, 1) + assert_array_almost_equal(output, expected) + +def test_histogram03(): + "histogram 3" + labels = [1, 0, 1, 1, 2, 2, 2, 2] + expected1 = [0, 1, 0, 1, 1] + expected2 = [0, 0, 0, 3, 0] + input = np.array([1, 1, 3, 4, 3, 5, 3, 3]) + output = ndimage.histogram(input, 0, 4, 5, labels, (1,2)) + + assert_array_almost_equal(output[0], expected1) + assert_array_almost_equal(output[1], expected2) + +if __name__ == "__main__": + run_module_suite() Modified: trunk/scipy/ndimage/tests/test_ndimage.py =================================================================== --- trunk/scipy/ndimage/tests/test_ndimage.py 2010-10-14 15:37:58 UTC (rev 6838) +++ trunk/scipy/ndimage/tests/test_ndimage.py 2010-10-14 15:38:36 UTC (rev 6839) @@ -2452,808 +2452,6 @@ [-1, -1, -1, -1, -1, -1, -1]] assert_array_almost_equal(out, expected) - def test_label01(self): - "label 1" - data = numpy.ones([]) - out, n = ndimage.label(data) - assert_array_almost_equal(out, 1) - - def test_label02(self): - "label 2" - data = numpy.zeros([]) - out, n = ndimage.label(data) - assert_array_almost_equal(out, 0) - - def test_label03(self): - "label 3" - data = numpy.ones([1]) - out, n = ndimage.label(data) - assert_array_almost_equal(out, [1]) - - def test_label04(self): - "label 4" - data = numpy.zeros([1]) - out, n = ndimage.label(data) - assert_array_almost_equal(out, [0]) - - def test_label05(self): - "label 5" - data = numpy.ones([5]) - out, n = ndimage.label(data) - assert_array_almost_equal(out, [1, 1, 1, 1, 1]) - - def test_label06(self): - "label 6" - data = numpy.array([1, 0, 1, 1, 0, 1]) - out, n = ndimage.label(data) - assert_array_almost_equal(out, [1, 0, 2, 2, 0, 3]) - - def test_label07(self): - "label 7" - 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], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0]]) - out, n = ndimage.label(data) - assert_array_almost_equal(out, [[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, 0, 0, 0, 0, 0]]) - - def test_label08(self): - "label 8" - 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], - [1, 1, 0, 0, 0, 0], - [0, 0, 0, 1, 1, 0]]) - out, n = ndimage.label(data) - assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0], - [0, 0, 2, 2, 0, 0], - [0, 0, 2, 2, 2, 0], - [3, 3, 0, 0, 0, 0], - [3, 3, 0, 0, 0, 0], - [0, 0, 0, 4, 4, 0]]) - - def test_label09(self): - "label 9" - 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], - [1, 1, 0, 0, 0, 0], - [0, 0, 0, 1, 1, 0]]) - struct = ndimage.generate_binary_structure(2, 2) - out, n = ndimage.label(data, struct) - assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0], - [0, 0, 2, 2, 0, 0], - [0, 0, 2, 2, 2, 0], - [2, 2, 0, 0, 0, 0], - [2, 2, 0, 0, 0, 0], - [0, 0, 0, 3, 3, 0]]) - - def test_label10(self): - "label 10" - 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]]) - struct = ndimage.generate_binary_structure(2, 2) - out, n = ndimage.label(data, struct) - assert_array_almost_equal(out, [[0, 0, 0, 0, 0, 0], - [0, 1, 1, 0, 1, 0], - [0, 1, 1, 1, 1, 0], - [0, 0, 0, 0, 0, 0]]) - - def test_label11(self): - "label 11" - for type in self.types: - 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], - [1, 1, 0, 0, 0, 0], - [0, 0, 0, 1, 1, 0]], type) - out, n = ndimage.label(data) - expected = [[1, 0, 0, 0, 0, 0], - [0, 0, 2, 2, 0, 0], - [0, 0, 2, 2, 2, 0], - [3, 3, 0, 0, 0, 0], - [3, 3, 0, 0, 0, 0], - [0, 0, 0, 4, 4, 0]] - assert_array_almost_equal(out, expected) - assert_equal(n, 4) - - def test_label12(self): - "label 12" - for type in self.types: - 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], - [0, 0, 0, 1, 1, 0]], type) - out, n = ndimage.label(data) - expected = [[0, 0, 0, 0, 1, 1], - [0, 0, 0, 0, 0, 1], - [0, 0, 1, 0, 1, 1], - [0, 0, 1, 1, 1, 1], - [0, 0, 0, 1, 1, 0]] - assert_array_almost_equal(out, expected) - assert_equal(n, 1) - - def test_label13(self): - "label 13" - for type in self.types: - 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]], - type) - out, n = ndimage.label(data) - expected = [[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]] - assert_array_almost_equal(out, expected) - assert_equal(n, 1) - - def test_find_objects01(self): - "find_objects 1" - data = numpy.ones([], dtype=int) - out = ndimage.find_objects(data) - assert_(out == [()]) - - def test_find_objects02(self): - "find_objects 2" - data = numpy.zeros([], dtype=int) - out = ndimage.find_objects(data) - assert_(out == []) - - def test_find_objects03(self): - "find_objects 3" - data = numpy.ones([1], dtype=int) - out = ndimage.find_objects(data) - assert_equal(out, [(slice(0, 1, None),)]) - - def test_find_objects04(self): - "find_objects 4" - data = numpy.zeros([1], dtype=int) - out = ndimage.find_objects(data) - assert_equal(out, []) - - def test_find_objects05(self): - "find_objects 5" - data = numpy.ones([5], dtype=int) - out = ndimage.find_objects(data) - assert_equal(out, [(slice(0, 5, None),)]) - - def test_find_objects06(self): - "find_objects 6" - data = numpy.array([1, 0, 2, 2, 0, 3]) - out = ndimage.find_objects(data) - assert_equal(out, [(slice(0, 1, None),), - (slice(2, 4, None),), - (slice(5, 6, None),)]) - - def test_find_objects07(self): - "find_objects 7" - 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], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0]]) - out = ndimage.find_objects(data) - assert_equal(out, []) - - def test_find_objects08(self): - "find_objects 8" - 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], - [3, 3, 0, 0, 0, 0], - [0, 0, 0, 4, 4, 0]]) - out = ndimage.find_objects(data) - assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)), - (slice(1, 3, None), slice(2, 5, None)), - (slice(3, 5, None), slice(0, 2, None)), - (slice(5, 6, None), slice(3, 5, None))]) - - def test_find_objects09(self): - "find_objects 9" - 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], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 4, 4, 0]]) - out = ndimage.find_objects(data) - assert_equal(out, [(slice(0, 1, None), slice(0, 1, None)), - (slice(1, 3, None), slice(2, 5, None)), - None, - (slice(5, 6, None), slice(3, 5, None))]) - - def test_sum01(self): - "sum 1" - for type in self.types: - input = numpy.array([], type) - output = ndimage.sum(input) - assert_equal(output, 0.0) - - def test_sum02(self): - "sum 2" - for type in self.types: - input = numpy.zeros([0, 4], type) - output = ndimage.sum(input) - assert_equal(output, 0.0) - - def test_sum03(self): - "sum 3" - for type in self.types: - input = numpy.ones([], type) - output = ndimage.sum(input) - assert_almost_equal(output, 1.0) - - def test_sum04(self): - "sum 4" - for type in self.types: - input = numpy.array([1, 2], type) - output = ndimage.sum(input) - assert_almost_equal(output, 3.0) - - def test_sum05(self): - "sum 5" - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.sum(input) - assert_almost_equal(output, 10.0) - - def test_sum06(self): - "sum 6" - labels = numpy.array([], bool) - for type in self.types: - input = numpy.array([], type) - output = ndimage.sum(input, labels=labels) - assert_equal(output, 0.0) - - def test_sum07(self): - "sum 7" - labels = numpy.ones([0, 4], bool) - for type in self.types: - input = numpy.zeros([0, 4], type) - output = ndimage.sum(input, labels=labels) - assert_equal(output, 0.0) - - def test_sum08(self): - "sum 8" - labels = numpy.array([1, 0], bool) - for type in self.types: - input = numpy.array([1, 2], type) - output = ndimage.sum(input, labels=labels) - assert_equal(output, 1.0) - - def test_sum09(self): - "sum 9" - labels = numpy.array([1, 0], bool) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.sum(input, labels=labels) - assert_almost_equal(output, 4.0) - - def test_sum10(self): - "sum 10" - labels = numpy.array([1, 0], bool) - input = numpy.array([[1, 2], [3, 4]], bool) - output = ndimage.sum(input, labels=labels) - assert_almost_equal(output, 2.0) - - def test_sum11(self): - "sum 11" - labels = numpy.array([1, 2], numpy.int8) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.sum(input, labels=labels, - index=2) - assert_almost_equal(output, 6.0) - - def test_sum12(self): - "sum 12" - labels = numpy.array([[1, 2], [2, 4]], numpy.int8) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.sum(input, labels=labels, - index=[4, 8, 2]) - assert_array_almost_equal(output, [4.0, 0.0, 5.0]) - - def test_mean01(self): - "mean 1" - labels = numpy.array([1, 0], bool) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.mean(input, labels=labels) - assert_almost_equal(output, 2.0) - - def test_mean02(self): - "mean 2" - labels = numpy.array([1, 0], bool) - input = numpy.array([[1, 2], [3, 4]], bool) - output = ndimage.mean(input, labels=labels) - assert_almost_equal(output, 1.0) - - def test_mean03(self): - "mean 3" - labels = numpy.array([1, 2]) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.mean(input, labels=labels, - index=2) - assert_almost_equal(output, 3.0) - - def test_mean04(self): - "mean 4" - labels = numpy.array([[1, 2], [2, 4]], numpy.int8) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.mean(input, labels=labels, - index=[4, 8, 2]) - assert_array_almost_equal(output[[0,2]], [4.0, 2.5]) - assert_(numpy.isnan(output[1])) - - def test_minimum01(self): - "minimum 1" - labels = numpy.array([1, 0], bool) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.minimum(input, labels=labels) - assert_almost_equal(output, 1.0) - - def test_minimum02(self): - "minimum 2" - labels = numpy.array([1, 0], bool) - input = numpy.array([[2, 2], [2, 4]], bool) - output = ndimage.minimum(input, labels=labels) - assert_almost_equal(output, 1.0) - - def test_minimum03(self): - "minimum 3" - labels = numpy.array([1, 2]) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.minimum(input, labels=labels, - index=2) - assert_almost_equal(output, 2.0) - - def test_minimum04(self): - "minimum 4" - labels = numpy.array([[1, 2], [2, 3]]) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.minimum(input, labels=labels, - index=[2, 3, 8]) - assert_array_almost_equal(output, [2.0, 4.0, 0.0]) - - def test_maximum01(self): - "maximum 1" - labels = numpy.array([1, 0], bool) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.maximum(input, labels=labels) - assert_almost_equal(output, 3.0) - - def test_maximum02(self): - "maximum 2" - labels = numpy.array([1, 0], bool) - input = numpy.array([[2, 2], [2, 4]], bool) - output = ndimage.maximum(input, labels=labels) - assert_almost_equal(output, 1.0) - - def test_maximum03(self): - "maximum 3" - labels = numpy.array([1, 2]) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.maximum(input, labels=labels, - index=2) - assert_almost_equal(output, 4.0) - - def test_maximum04(self): - "maximum 4" - labels = numpy.array([[1, 2], [2, 3]]) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.maximum(input, labels=labels, - index=[2, 3, 8]) - assert_array_almost_equal(output, [3.0, 4.0, 0.0]) - - def test_maximum05(self): - "Ticket #501" - x = numpy.array([-3,-2,-1]) - assert_equal(ndimage.maximum(x),-1) - - def test_variance01(self): - "variance 1" - for type in self.types: - input = numpy.array([], type) - output = ndimage.variance(input) - assert_(numpy.isnan(output)) - - def test_variance02(self): - "variance 2" - for type in self.types: - input = numpy.array([1], type) - output = ndimage.variance(input) - assert_almost_equal(output, 0.0) - - def test_variance03(self): - "variance 3" - for type in self.types: - input = numpy.array([1, 3], type) - output = ndimage.variance(input) - assert_almost_equal(output, 1.0) - - def test_variance04(self): - "variance 4" - input = numpy.array([1, 0], bool) - output = ndimage.variance(input) - assert_almost_equal(output, 0.25) - - def test_variance05(self): - "variance 5" - labels = [2, 2, 3] - for type in self.types: - input = numpy.array([1, 3, 8], type) - output = ndimage.variance(input, labels, 2) - assert_almost_equal(output, 1.0) - - def test_variance06(self): - "variance 6" - labels = [2, 2, 3, 3, 4] - for type in self.types: - input = numpy.array([1, 3, 8, 10, 8], type) - output = ndimage.variance(input, labels, [2, 3, 4]) - assert_array_almost_equal(output, [1.0, 1.0, 0.0]) - - def test_standard_deviation01(self): - "standard deviation 1" - for type in self.types: - input = numpy.array([], type) - output = ndimage.standard_deviation(input) - assert_(numpy.isnan(output)) - - def test_standard_deviation02(self): - "standard deviation 2" - for type in self.types: - input = numpy.array([1], type) - output = ndimage.standard_deviation(input) - assert_almost_equal(output, 0.0) - - def test_standard_deviation03(self): - "standard deviation 3" - for type in self.types: - input = numpy.array([1, 3], type) - output = ndimage.standard_deviation(input) - assert_almost_equal(output, math.sqrt(1.0)) - - def test_standard_deviation04(self): - "standard deviation 4" - input = numpy.array([1, 0], bool) - output = ndimage.standard_deviation(input) - assert_almost_equal(output, 0.5) - - def test_standard_deviation05(self): - "standard deviation 5" - labels = [2, 2, 3] - for type in self.types: - input = numpy.array([1, 3, 8], type) - output = ndimage.standard_deviation(input, labels, 2) - assert_almost_equal(output, 1.0) - - def test_standard_deviation06(self): - "standard deviation 6" - labels = [2, 2, 3, 3, 4] - for type in self.types: - input = numpy.array([1, 3, 8, 10, 8], type) - output = ndimage.standard_deviation(input, labels, - [2, 3, 4]) - assert_array_almost_equal(output, [1.0, 1.0, 0.0]) - - def test_minimum_position01(self): - "minimum position 1" - labels = numpy.array([1, 0], bool) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.minimum_position(input, - labels=labels) - assert_equal(output, (0, 0)) - - def test_minimum_position02(self): - "minimum position 2" - for type in self.types: - input = numpy.array([[5, 4, 2, 5], - [3, 7, 0, 2], - [1, 5, 1, 1]], type) - output = ndimage.minimum_position(input) - assert_equal(output, (1, 2)) - - def test_minimum_position03(self): - "minimum position 3" - input = numpy.array([[5, 4, 2, 5], - [3, 7, 0, 2], - [1, 5, 1, 1]], bool) - output = ndimage.minimum_position(input) - assert_equal(output, (1, 2)) - - def test_minimum_position04(self): - "minimum position 4" - input = numpy.array([[5, 4, 2, 5], - [3, 7, 1, 2], - [1, 5, 1, 1]], bool) - output = ndimage.minimum_position(input) - assert_equal(output, (0, 0)) - - def test_minimum_position05(self): - "minimum position 5" - labels = [1, 2, 0, 4] - for type in self.types: - input = numpy.array([[5, 4, 2, 5], - [3, 7, 0, 2], - [1, 5, 2, 3]], type) - output = ndimage.minimum_position(input, labels) - assert_equal(output, (2, 0)) - - def test_minimum_position06(self): - "minimum position 6" - labels = [1, 2, 3, 4] - for type in self.types: - input = numpy.array([[5, 4, 2, 5], - [3, 7, 0, 2], - [1, 5, 1, 1]], type) - output = ndimage.minimum_position(input, labels, 2) - assert_equal(output, (0, 1)) - - def test_minimum_position07(self): - "minimum position 7" - labels = [1, 2, 3, 4] - for type in self.types: - input = numpy.array([[5, 4, 2, 5], - [3, 7, 0, 2], - [1, 5, 1, 1]], type) - output = ndimage.minimum_position(input, labels, - [2, 3]) - assert_equal(output[0], (0, 1)) - assert_equal(output[1], (1, 2)) - - def test_maximum_position01(self): - "maximum position 1" - labels = numpy.array([1, 0], bool) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output = ndimage.maximum_position(input, - labels=labels) - assert_equal(output, (1, 0)) - - def test_maximum_position02(self): - "maximum position 2" - for type in self.types: - input = numpy.array([[5, 4, 2, 5], - [3, 7, 8, 2], - [1, 5, 1, 1]], type) - output = ndimage.maximum_position(input) - assert_equal(output, (1, 2)) - - def test_maximum_position03(self): - "maximum position 3" - input = numpy.array([[5, 4, 2, 5], - [3, 7, 8, 2], - [1, 5, 1, 1]], bool) - output = ndimage.maximum_position(input) - assert_equal(output, (0, 0)) - - def test_maximum_position04(self): - "maximum position 4" - labels = [1, 2, 0, 4] - for type in self.types: - input = numpy.array([[5, 4, 2, 5], - [3, 7, 8, 2], - [1, 5, 1, 1]], type) - output = ndimage.maximum_position(input, labels) - assert_equal(output, (1, 1)) - - def test_maximum_position05(self): - "maximum position 5" - labels = [1, 2, 0, 4] - for type in self.types: - input = numpy.array([[5, 4, 2, 5], - [3, 7, 8, 2], - [1, 5, 1, 1]], type) - output = ndimage.maximum_position(input, labels, 1) - assert_equal(output, (0, 0)) - - def test_maximum_position06(self): - "maximum position 6" - labels = [1, 2, 0, 4] - for type in self.types: - input = numpy.array([[5, 4, 2, 5], - [3, 7, 8, 2], - [1, 5, 1, 1]], type) - output = ndimage.maximum_position(input, labels, - [1, 2]) - assert_equal(output[0], (0, 0)) - assert_equal(output[1], (1, 1)) - - def test_extrema01(self): - "extrema 1" - labels = numpy.array([1, 0], bool) - for type in self.types: - 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) - output4 = ndimage.minimum_position(input, - labels=labels) - output5 = ndimage.maximum_position(input, - labels=labels) - assert_equal(output1, (output2, output3, output4, output5)) - - def test_extrema02(self): - "extrema 2" - labels = numpy.array([1, 2]) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output1 = ndimage.extrema(input, labels=labels, - index=2) - output2 = ndimage.minimum(input, labels=labels, - index=2) - output3 = ndimage.maximum(input, labels=labels, - index=2) - output4 = ndimage.minimum_position(input, - labels=labels, index=2) - output5 = ndimage.maximum_position(input, - labels=labels, index=2) - assert_equal(output1, (output2, output3, output4, output5)) - - def test_extrema03(self): - "extrema 3" - labels = numpy.array([[1, 2], [2, 3]]) - for type in self.types: - input = numpy.array([[1, 2], [3, 4]], type) - output1 = ndimage.extrema(input, labels=labels, - index=[2, 3, 8]) - output2 = ndimage.minimum(input, labels=labels, - index=[2, 3, 8]) - output3 = ndimage.maximum(input, labels=labels, - index=[2, 3, 8]) - output4 = ndimage.minimum_position(input, - labels=labels, index=[2, 3, 8]) - output5 = ndimage.maximum_position(input, - labels=labels, index=[2, 3, 8]) - assert_array_almost_equal(output1[0], output2) - assert_array_almost_equal(output1[1], output3) - assert_array_almost_equal(output1[2], output4) - assert_array_almost_equal(output1[3], output5) - - def test_extrema04(self): - "extrema 4" - labels = [1, 2, 0, 4] - for type in self.types: - input = numpy.array([[5, 4, 2, 5], - [3, 7, 8, 2], - [1, 5, 1, 1]], type) - output1 = ndimage.extrema(input, labels, [1, 2]) - output2 = ndimage.minimum(input, labels, [1, 2]) - output3 = ndimage.maximum(input, labels, [1, 2]) - output4 = ndimage.minimum_position(input, labels, - [1, 2]) - output5 = ndimage.maximum_position(input, labels, - [1, 2]) - assert_array_almost_equal(output1[0], output2) - assert_array_almost_equal(output1[1], output3) - assert_array_almost_equal(output1[2], output4) - assert_array_almost_equal(output1[3], output5) - - def test_center_of_mass01(self): - "center of mass 1" - expected = [0.0, 0.0] - for type in self.types: - input = numpy.array([[1, 0], [0, 0]], type) - output = ndimage.center_of_mass(input) - assert_array_almost_equal(output, expected) - - def test_center_of_mass02(self): - "center of mass 2" - expected = [1, 0] - for type in self.types: - input = numpy.array([[0, 0], [1, 0]], type) - output = ndimage.center_of_mass(input) - assert_array_almost_equal(output, expected) - - def test_center_of_mass03(self): - "center of mass 3" - expected = [0, 1] - for type in self.types: - input = numpy.array([[0, 1], [0, 0]], type) - output = ndimage.center_of_mass(input) - assert_array_almost_equal(output, expected) - - def test_center_of_mass04(self): - "center of mass 4" - expected = [1, 1] - for type in self.types: - input = numpy.array([[0, 0], [0, 1]], type) - output = ndimage.center_of_mass(input) - assert_array_almost_equal(output, expected) - - def test_center_of_mass05(self): - "center of mass 5" - expected = [0.5, 0.5] - for type in self.types: - input = numpy.array([[1, 1], [1, 1]], type) - output = ndimage.center_of_mass(input) - assert_array_almost_equal(output, expected) - - def test_center_of_mass06(self): - "center of mass 6" - expected = [0.5, 0.5] - input = numpy.array([[1, 2], [3, 1]], bool) - output = ndimage.center_of_mass(input) - assert_array_almost_equal(output, expected) - - def test_center_of_mass07(self): - "center of mass 7" - labels = [1, 0] - expected = [0.5, 0.0] - input = numpy.array([[1, 2], [3, 1]], bool) - output = ndimage.center_of_mass(input, labels) - assert_array_almost_equal(output, expected) - - def test_center_of_mass08(self): - "center of mass 8" - labels = [1, 2] - expected = [0.5, 1.0] - input = numpy.array([[5, 2], [3, 1]], bool) - output = ndimage.center_of_mass(input, labels, 2) - assert_array_almost_equal(output, expected) - - def test_center_of_mass09(self): - "center of mass 9" - labels = [1, 2] - expected = [(0.5, 0.0), (0.5, 1.0)] - input = numpy.array([[1, 2], [1, 1]], bool) - output = ndimage.center_of_mass(input, labels, [1, 2]) - assert_array_almost_equal(output, expected) - - def test_histogram01(self): - "histogram 1" - expected = numpy.ones(10) - input = numpy.arange(10) - output = ndimage.histogram(input, 0, 10, 10) - assert_array_almost_equal(output, expected) - - def test_histogram02(self): - "histogram 2" - labels = [1, 1, 1, 1, 2, 2, 2, 2] - expected = [0, 2, 0, 1, 1] - input = numpy.array([1, 1, 3, 4, 3, 3, 3, 3]) - output = ndimage.histogram(input, 0, 4, 5, labels, 1) - assert_array_almost_equal(output, expected) - - def test_histogram03(self): - "histogram 3" - labels = [1, 0, 1, 1, 2, 2, 2, 2] - expected1 = [0, 1, 0, 1, 1] - expected2 = [0, 0, 0, 3, 0] - input = numpy.array([1, 1, 3, 4, 3, 5, 3, 3]) - output = ndimage.histogram(input, 0, 4, 5, labels, (1,2)) - - assert_array_almost_equal(output[0], expected1) - assert_array_almost_equal(output[1], expected2) - def test_distance_transform_bf01(self): "brute force distance transform 1" for type in self.types: From scipy-svn at scipy.org Thu Oct 14 11:39:06 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 14 Oct 2010 10:39:06 -0500 (CDT) Subject: [Scipy-svn] r6840 - trunk/scipy/ndimage Message-ID: <20101014153906.AF6ED37A4BD@scipy.org> Author: stefan Date: 2010-10-14 10:39:06 -0500 (Thu, 14 Oct 2010) New Revision: 6840 Modified: trunk/scipy/ndimage/measurements.py Log: BUG: ndimage: Integer check must take uints into account. Modified: trunk/scipy/ndimage/measurements.py =================================================================== --- trunk/scipy/ndimage/measurements.py 2010-10-14 15:38:36 UTC (rev 6839) +++ trunk/scipy/ndimage/measurements.py 2010-10-14 15:39:06 UTC (rev 6840) @@ -309,8 +309,8 @@ # remap labels to unique integers if necessary, or if the largest # label is larger than the number of values. - if ((not numpy.issubdtype(labels.dtype, numpy.int)) or - (labels.min() < 0) or (labels.max() > labels.size)): + if not numpy.issubdtype(labels.dtype, (numpy.int, np.unsignedinteger)) or \ + (labels.min() < 0) or (labels.max() > labels.size): unique_labels, new_labels = numpy.unique1d(labels, return_inverse=True) if centered: From scipy-svn at scipy.org Thu Oct 14 11:39:40 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 14 Oct 2010 10:39:40 -0500 (CDT) Subject: [Scipy-svn] r6841 - trunk/scipy/ndimage Message-ID: <20101014153940.4B59537A4BD@scipy.org> Author: stefan Date: 2010-10-14 10:39:40 -0500 (Thu, 14 Oct 2010) New Revision: 6841 Modified: trunk/scipy/ndimage/measurements.py Log: ENH: ndimage: Smaller round-off error when computing variance. Modified: trunk/scipy/ndimage/measurements.py =================================================================== --- trunk/scipy/ndimage/measurements.py 2010-10-14 15:39:06 UTC (rev 6840) +++ trunk/scipy/ndimage/measurements.py 2010-10-14 15:39:40 UTC (rev 6841) @@ -279,7 +279,7 @@ def single_group(vals): if centered: - vals_c = vals# - vals.mean() + vals_c = vals - vals.mean() return vals.size, vals.sum(), (vals_c * vals_c.conjugate()).sum() else: return vals.size, vals.sum() @@ -301,7 +301,7 @@ def _sum_centered(labels): means = sums / counts - centered_input = input# - means[labels] + centered_input = input - means[labels] return numpy.bincount(labels, weights=(centered_input * \ centered_input.conjugate()).ravel()) @@ -408,11 +408,9 @@ none, all values where label is greater than zero are used. """ - count, sum, sum_c = _stats(input, labels, index, centered=True) - mean = sum / numpy.asanyarray(count).astype(numpy.float) - mean2 = sum_c / numpy.asanyarray(count).astype(numpy.float) + count, sum, sum_c_sq = _stats(input, labels, index, centered=True) - return mean2 - (mean * mean.conjugate()) + return sum_c_sq / np.asanyarray(count).astype(float) def standard_deviation(input, labels = None, index = None): """Calculate the standard deviation of the values of an array at labels. From scipy-svn at scipy.org Sat Oct 16 20:22:39 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 16 Oct 2010 19:22:39 -0500 (CDT) Subject: [Scipy-svn] r6842 - trunk/scipy/special Message-ID: <20101017002239.EB172AE1A1@scipy.org> Author: warren.weckesser Date: 2010-10-16 19:22:38 -0500 (Sat, 16 Oct 2010) New Revision: 6842 Modified: trunk/scipy/special/basic.py Log: BUG: special: sinc(0) should not cause a warning. Modified: trunk/scipy/special/basic.py =================================================================== --- trunk/scipy/special/basic.py 2010-10-14 15:39:40 UTC (rev 6841) +++ trunk/scipy/special/basic.py 2010-10-17 00:22:38 UTC (rev 6842) @@ -11,8 +11,13 @@ def sinc(x): """Returns sin(pi*x)/(pi*x) at all points of array x. """ - w = asarray(asarray(x)*pi) - return where(x==0, 1.0, sin(w)/w) + w = pi * asarray(x) + # w might contain 0, and so temporarily turn off warnings + # while calculating sin(w)/w. + old_settings = seterr(all='ignore') + s = sin(w) / w + seterr(**old_settings) + return where(x==0, 1.0, s) def diric(x,n): """Returns the periodic sinc function also called the dirichlet function: From scipy-svn at scipy.org Sat Oct 16 20:36:33 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 16 Oct 2010 19:36:33 -0500 (CDT) Subject: [Scipy-svn] r6843 - trunk/scipy/special Message-ID: <20101017003633.3258737C40B@scipy.org> Author: warren.weckesser Date: 2010-10-16 19:36:33 -0500 (Sat, 16 Oct 2010) New Revision: 6843 Modified: trunk/scipy/special/basic.py Log: ENH: special: update 'raise' statements in basic.py Modified: trunk/scipy/special/basic.py =================================================================== --- trunk/scipy/special/basic.py 2010-10-17 00:22:38 UTC (rev 6842) +++ trunk/scipy/special/basic.py 2010-10-17 00:36:33 UTC (rev 6843) @@ -68,7 +68,7 @@ See jn_zeros, jnp_zeros to get separated arrays of zeros. """ if not isscalar(nt) or (floor(nt)!=nt) or (nt>1200): - raise ValueError, "Number must be integer <= 1200." + raise ValueError("Number must be integer <= 1200.") nt = int(nt) n,m,t,zo = specfun.jdzo(nt) return zo[:nt],n[:nt],m[:nt],t[:nt] @@ -80,11 +80,11 @@ See jn_zeros, jnp_zeros, yn_zeros, ynp_zeros to get separate arrays. """ if not (isscalar(nt) and isscalar(n)): - raise ValueError, "Arguments must be scalars." + raise ValueError("Arguments must be scalars.") if (floor(n)!=n) or (floor(nt)!=nt): - raise ValueError, "Arguments must be integers." + raise ValueError("Arguments must be integers.") if (nt <=0): - raise ValueError, "nt > 0" + raise ValueError("nt > 0") return specfun.jyzo(abs(n),nt) def jn_zeros(n,nt): @@ -109,7 +109,7 @@ of Y0'(z0) = -Y1(z0) at each zero. """ if not isscalar(nt) or (floor(nt)!=nt) or (nt <=0): - raise ValueError, "Arguments must be scalar positive integer." + raise ValueError("Arguments must be scalar positive integer.") kf = 0 kc = (complex != 1) return specfun.cyzo(nt,kf,kc) @@ -119,7 +119,7 @@ of Y1'(z1) = Y0(z1) at each zero. """ if not isscalar(nt) or (floor(nt)!=nt) or (nt <=0): - raise ValueError, "Arguments must be scalar positive integer." + raise ValueError("Arguments must be scalar positive integer.") kf = 1 kc = (complex != 1) return specfun.cyzo(nt,kf,kc) @@ -129,7 +129,7 @@ of Y1(z1') at each zero. """ if not isscalar(nt) or (floor(nt)!=nt) or (nt <=0): - raise ValueError, "Arguments must be scalar positive integer." + raise ValueError("Arguments must be scalar positive integer.") kf = 2 kc = (complex != 1) return specfun.cyzo(nt,kf,kc) @@ -150,7 +150,7 @@ """Return the nth derivative of Jv(z) with respect to z. """ if not isinstance(n,types.IntType) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if n == 0: return jv(v,z) else: @@ -161,7 +161,7 @@ """Return the nth derivative of Yv(z) with respect to z. """ if not isinstance(n,types.IntType) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if n == 0: return yv(v,z) else: @@ -172,7 +172,7 @@ """Return the nth derivative of Kv(z) with respect to z. """ if not isinstance(n,types.IntType) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if n == 0: return kv(v,z) else: @@ -182,7 +182,7 @@ """Return the nth derivative of Iv(z) with respect to z. """ if not isinstance(n,types.IntType) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if n == 0: return iv(v,z) else: @@ -192,7 +192,7 @@ """Return the nth derivative of H1v(z) with respect to z. """ if not isinstance(n,types.IntType) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if n == 0: return hankel1(v,z) else: @@ -203,7 +203,7 @@ """Return the nth derivative of H2v(z) with respect to z. """ if not isinstance(n,types.IntType) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if n == 0: return hankel2(v,z) else: @@ -215,9 +215,9 @@ all orders up to and including n. """ if not (isscalar(n) and isscalar(z)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if (n < 1): n1 = 1 else: n1 = n if iscomplex(z): @@ -231,9 +231,9 @@ all orders up to and including n. """ if not (isscalar(n) and isscalar(z)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if (n < 1): n1 = 1 else: n1 = n if iscomplex(z) or less(z,0): @@ -247,9 +247,9 @@ derivatives for all orders up to and including n. """ if not (isscalar(n) and isscalar(z)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if (n < 1): n1 = 1 else: n1 = n if iscomplex(z) or less(z,0): @@ -264,9 +264,9 @@ all orders up to and including n. """ if not (isscalar(n) and isscalar(z)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if (n < 1): n1 = 1 else: n1 = n if iscomplex(z): @@ -280,9 +280,9 @@ all orders up to and including n. """ if not (isscalar(n) and isscalar(z)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if (n < 1): n1 = 1 else: n1 = n if iscomplex(z) or less(z,0): @@ -296,9 +296,9 @@ derivatives for all orders up to and including n. """ if not (isscalar(n) and isscalar(z)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if iscomplex(z) or less(z,0): nm,In,Inp,kn,knp = specfun.csphik(n,z) else: @@ -311,9 +311,9 @@ derivative for all orders up to and including n. """ if not (isscalar(n) and isscalar(x)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if (n == 0): n1 = 1 else: n1 = n nm,jn,jnp = specfun.rctj(n1,x) @@ -324,9 +324,9 @@ derivative for all orders up to and including n. """ if not (isscalar(n) and isscalar(x)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if (n == 0): n1 = 1 else: n1 = n nm,jn,jnp = specfun.rcty(n1,x) @@ -385,21 +385,21 @@ """Compute nt complex zeros of the error function erf(z). """ if (floor(nt)!=nt) or (nt<=0) or not isscalar(nt): - raise ValueError, "Argument must be positive scalar integer." + raise ValueError("Argument must be positive scalar integer.") return specfun.cerzo(nt) def fresnelc_zeros(nt): """Compute nt complex zeros of the cosine fresnel integral C(z). """ if (floor(nt)!=nt) or (nt<=0) or not isscalar(nt): - raise ValueError, "Argument must be positive scalar integer." + raise ValueError("Argument must be positive scalar integer.") return specfun.fcszo(1,nt) def fresnels_zeros(nt): """Compute nt complex zeros of the sine fresnel integral S(z). """ if (floor(nt)!=nt) or (nt<=0) or not isscalar(nt): - raise ValueError, "Argument must be positive scalar integer." + raise ValueError("Argument must be positive scalar integer.") return specfun.fcszo(2,nt) def fresnel_zeros(nt): @@ -407,7 +407,7 @@ S(z) and C(z). """ if (floor(nt)!=nt) or (nt<=0) or not isscalar(nt): - raise ValueError, "Argument must be positive scalar integer." + raise ValueError("Argument must be positive scalar integer.") return specfun.fcszo(2,nt), specfun.fcszo(1,nt) def hyp0f1(v,z): @@ -445,11 +445,11 @@ modified mathieu functions. """ if not (isscalar(m) and isscalar(q)): - raise ValueError, "m and q must be scalars." + raise ValueError("m and q must be scalars.") if (q < 0): - raise ValueError, "q >=0" + raise ValueError("q >=0") if (m != floor(m)) or (m<0): - raise ValueError, "m must be an integer >=0." + raise ValueError("m must be an integer >=0.") if (q <= 1): qm = 7.5+56.1*sqrt(q)-134.7*q+90.7*sqrt(q)*q @@ -472,11 +472,11 @@ modified mathieu functions. """ if not (isscalar(m) and isscalar(q)): - raise ValueError, "m and q must be scalars." + raise ValueError("m and q must be scalars.") if (q < 0): - raise ValueError, "q >=0" + raise ValueError("q >=0") if (m != floor(m)) or (m<=0): - raise ValueError, "m must be an integer > 0" + raise ValueError("m must be an integer > 0") if (q <= 1): qm = 7.5+56.1*sqrt(q)-134.7*q+90.7*sqrt(q)*q @@ -521,11 +521,11 @@ Derivatives for all orders 0..m and degrees 0..n """ if not isscalar(m) or (abs(m)>n): - raise ValueError, "m must be <= n." + raise ValueError("m must be <= n.") if not isscalar(n) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if not isscalar(z): - raise ValueError, "z must be scalar." + raise ValueError("z must be scalar.") if (m < 0): mp = -m mf,nf = mgrid[0:mp+1,0:n+1] @@ -554,11 +554,11 @@ z can be complex. """ if not isscalar(m) or (m<0): - raise ValueError, "m must be a non-negative integer." + raise ValueError("m must be a non-negative integer.") if not isscalar(n) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if not isscalar(z): - raise ValueError, "z must be scalar." + raise ValueError("z must be scalar.") m = int(m) n = int(n) @@ -577,7 +577,7 @@ """Return an array of the Bernoulli numbers B0..Bn """ if not isscalar(n) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") n = int(n) if (n < 2): n1 = 2 else: n1 = n @@ -587,7 +587,7 @@ """Return an array of the Euler numbers E0..En (inclusive) """ if not isscalar(n) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") n = int(n) if (n < 2): n1 = 2 else: n1 = n @@ -600,9 +600,9 @@ See also special.legendre for polynomial class. """ if not (isscalar(n) and isscalar(z)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if (n < 1): n1 = 1 else: n1 = n if iscomplex(z): @@ -618,9 +618,9 @@ Qn(z) and derivatives for all degrees from 0 to n (inclusive). """ if not (isscalar(n) and isscalar(z)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (n!= floor(n)) or (n<0): - raise ValueError, "n must be a non-negative integer." + raise ValueError("n must be a non-negative integer.") if (n < 1): n1 = 1 else: n1 = n if iscomplex(z): @@ -642,7 +642,7 @@ """ kf = 1 if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be a positive integer scalar." + raise ValueError("nt must be a positive integer scalar.") return specfun.airyzo(nt,kf) def bi_zeros(nt): @@ -658,7 +658,7 @@ """ kf = 2 if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be a positive integer scalar." + raise ValueError("nt must be a positive integer scalar.") return specfun.airyzo(nt,kf) def lmbda(v,x): @@ -666,9 +666,9 @@ and their derivatives. Lv0(x)..Lv(x) are computed with v0=v-int(v). """ if not (isscalar(v) and isscalar(x)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (v<0): - raise ValueError, "argument must be > 0." + raise ValueError("argument must be > 0.") n = int(v) v0 = v - n if (n < 1): n1 = 1 @@ -685,7 +685,7 @@ their derivatives for Dv0(x)..Dv(x) with v0=v-int(v). """ if not (isscalar(v) and isscalar(x)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") n = int(v) v0 = v-n if (n < 1): n1=1 @@ -699,7 +699,7 @@ their derivatives for Dv0(x)..Dv(x) with v0=v-int(v). """ if not (isscalar(v) and isscalar(x)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") n = int(v) v0 = v-n if (n <= 1): n1=1 @@ -713,9 +713,9 @@ their derivatives for D0(z)..Dn(z). """ if not (isscalar(n) and isscalar(z)): - raise ValueError, "arguments must be scalars." + raise ValueError("arguments must be scalars.") if (floor(n)!=n): - raise ValueError, "n must be an integer." + raise ValueError("n must be an integer.") if (abs(n) <= 1): n1 = 1 else: @@ -727,56 +727,56 @@ """Compute nt zeros of the kelvin function ber x """ if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be positive integer scalar." + raise ValueError("nt must be positive integer scalar.") return specfun.klvnzo(nt,1) def bei_zeros(nt): """Compute nt zeros of the kelvin function bei x """ if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be positive integer scalar." + raise ValueError("nt must be positive integer scalar.") return specfun.klvnzo(nt,2) def ker_zeros(nt): """Compute nt zeros of the kelvin function ker x """ if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be positive integer scalar." + raise ValueError("nt must be positive integer scalar.") return specfun.klvnzo(nt,3) def kei_zeros(nt): """Compute nt zeros of the kelvin function kei x """ if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be positive integer scalar." + raise ValueError("nt must be positive integer scalar.") return specfun.klvnzo(nt,4) def berp_zeros(nt): """Compute nt zeros of the kelvin function ber' x """ if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be positive integer scalar." + raise ValueError("nt must be positive integer scalar.") return specfun.klvnzo(nt,5) def beip_zeros(nt): """Compute nt zeros of the kelvin function bei' x """ if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be positive integer scalar." + raise ValueError("nt must be positive integer scalar.") return specfun.klvnzo(nt,6) def kerp_zeros(nt): """Compute nt zeros of the kelvin function ker' x """ if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be positive integer scalar." + raise ValueError("nt must be positive integer scalar.") return specfun.klvnzo(nt,7) def keip_zeros(nt): """Compute nt zeros of the kelvin function kei' x """ if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be positive integer scalar." + raise ValueError("nt must be positive integer scalar.") return specfun.klvnzo(nt,8) def kelvin_zeros(nt): @@ -786,7 +786,7 @@ (ber, bei, ker, kei, ber', bei', ker', kei') """ if not isscalar(nt) or (floor(nt)!=nt) or (nt<=0): - raise ValueError, "nt must be positive integer scalar." + raise ValueError("nt must be positive integer scalar.") return specfun.klvnzo(nt,1), \ specfun.klvnzo(nt,2), \ specfun.klvnzo(nt,3), \ @@ -802,11 +802,11 @@ parameter c. """ if not (isscalar(m) and isscalar(n) and isscalar(c)): - raise ValueError, "Arguments must be scalars." + raise ValueError("Arguments must be scalars.") if (n!=floor(n)) or (m!=floor(m)): - raise ValueError, "Modes must be integers." + raise ValueError("Modes must be integers.") if (n-m > 199): - raise ValueError, "Difference between n and m is too large." + raise ValueError("Difference between n and m is too large.") maxL = n-m+1 return specfun.segv(m,n,c,1)[1][:maxL] @@ -816,11 +816,11 @@ parameter c. """ if not (isscalar(m) and isscalar(n) and isscalar(c)): - raise ValueError, "Arguments must be scalars." + raise ValueError("Arguments must be scalars.") if (n!=floor(n)) or (m!=floor(m)): - raise ValueError, "Modes must be integers." + raise ValueError("Modes must be integers.") if (n-m > 199): - raise ValueError, "Difference between n and m is too large." + raise ValueError("Difference between n and m is too large.") maxL = n-m+1 return specfun.segv(m,n,c,-1)[1][:maxL] From scipy-svn at scipy.org Tue Oct 19 09:08:48 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Oct 2010 08:08:48 -0500 (CDT) Subject: [Scipy-svn] r6846 - trunk/scipy/stats Message-ID: <20101019130848.9D66E37C9F6@scipy.org> Author: warren.weckesser Date: 2010-10-19 08:08:47 -0500 (Tue, 19 Oct 2010) New Revision: 6846 Modified: trunk/scipy/stats/distributions.py Log: BUG: Fix names and extradoc for the fisk distribution. Modified: trunk/scipy/stats/distributions.py =================================================================== --- trunk/scipy/stats/distributions.py 2010-10-17 15:14:21 UTC (rev 6845) +++ trunk/scipy/stats/distributions.py 2010-10-19 13:08:47 UTC (rev 6846) @@ -2159,11 +2159,13 @@ return burr_gen._stats(self, c, 1.0) def _entropy(self, c): return 2 - log(c) -fisk = fisk_gen(a=0.0, name='fink', longname="A funk", +fisk = fisk_gen(a=0.0, name='fisk', longname="Fisk", shapes='c', extradoc=""" -Fink distribution. +Fisk distribution. +Also known as the log-logistic distribution. + Burr distribution with d=1. """ ) From scipy-svn at scipy.org Tue Oct 19 09:31:18 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Oct 2010 08:31:18 -0500 (CDT) Subject: [Scipy-svn] r6847 - trunk/scipy/stats Message-ID: <20101019133118.07A541B8AD0@scipy.org> Author: warren.weckesser Date: 2010-10-19 08:31:17 -0500 (Tue, 19 Oct 2010) New Revision: 6847 Modified: trunk/scipy/stats/distributions.py Log: BUG: stats: Fix typo in dlaplace extradoc. Modified: trunk/scipy/stats/distributions.py =================================================================== --- trunk/scipy/stats/distributions.py 2010-10-19 13:08:47 UTC (rev 6846) +++ trunk/scipy/stats/distributions.py 2010-10-19 13:31:17 UTC (rev 6847) @@ -6075,7 +6075,7 @@ Discrete Laplacian distribution. -dlapacle.pmf(k,a) = tanh(a/2) * exp(-a*abs(k)) +dlaplace.pmf(k,a) = tanh(a/2) * exp(-a*abs(k)) for a > 0. """ ) From scipy-svn at scipy.org Tue Oct 19 12:20:43 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Oct 2010 11:20:43 -0500 (CDT) Subject: [Scipy-svn] r6848 - in trunk/scipy/stats: . tests Message-ID: <20101019162043.A9B6EAE194@scipy.org> Author: warren.weckesser Date: 2010-10-19 11:20:43 -0500 (Tue, 19 Oct 2010) New Revision: 6848 Modified: trunk/scipy/stats/distributions.py trunk/scipy/stats/tests/test_distributions.py Log: BUG: stats: The method _construct_default_doc did not properly handle the default value of the extradoc keyword (ticket #1316). Modified: trunk/scipy/stats/distributions.py =================================================================== --- trunk/scipy/stats/distributions.py 2010-10-19 13:31:17 UTC (rev 6847) +++ trunk/scipy/stats/distributions.py 2010-10-19 16:20:43 UTC (rev 6848) @@ -971,6 +971,10 @@ def _construct_default_doc(self, longname=None, extradoc=None): """Construct instance docstring from the default template.""" + if longname is None: + longname = 'A' + if extradoc is None: + extradoc = '' if extradoc.startswith('\n\n'): extradoc = extradoc[2:] self.__doc__ = ''.join(['%s continuous random variable.'%longname, Modified: trunk/scipy/stats/tests/test_distributions.py =================================================================== --- trunk/scipy/stats/tests/test_distributions.py 2010-10-19 13:31:17 UTC (rev 6847) +++ trunk/scipy/stats/tests/test_distributions.py 2010-10-19 16:20:43 UTC (rev 6848) @@ -556,5 +556,12 @@ assert_equal(m1, m2) +def test_regression_ticket_1316(): + """Regression test for ticket #1316.""" + # The following was raising an exception, because _construct_default_doc() + # did not handle the default keyword extradoc=None. See ticket #1316. + g = stats.distributions.gamma_gen(name='gamma') + + if __name__ == "__main__": run_module_suite() From scipy-svn at scipy.org Tue Oct 19 19:53:15 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Oct 2010 18:53:15 -0500 (CDT) Subject: [Scipy-svn] r6849 - trunk Message-ID: <20101019235315.1E27937C9F5@scipy.org> Author: ptvirtan Date: 2010-10-19 18:53:14 -0500 (Tue, 19 Oct 2010) New Revision: 6849 Modified: trunk/setup.py Log: BUG: setup.py: add a missing import Modified: trunk/setup.py =================================================================== --- trunk/setup.py 2010-10-19 16:20:43 UTC (rev 6848) +++ trunk/setup.py 2010-10-19 23:53:14 UTC (rev 6849) @@ -22,6 +22,7 @@ import sys import warnings import subprocess +import shutil import re CLASSIFIERS = """\ From scipy-svn at scipy.org Tue Oct 19 20:05:10 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 19 Oct 2010 19:05:10 -0500 (CDT) Subject: [Scipy-svn] r6850 - trunk Message-ID: <20101020000510.5854A37C9F5@scipy.org> Author: ptvirtan Date: 2010-10-19 19:05:10 -0500 (Tue, 19 Oct 2010) New Revision: 6850 Modified: trunk/TEST_COMMIT Log: TEST: test commit, kicking git mirror Modified: trunk/TEST_COMMIT =================================================================== --- trunk/TEST_COMMIT 2010-10-19 23:53:14 UTC (rev 6849) +++ trunk/TEST_COMMIT 2010-10-20 00:05:10 UTC (rev 6850) @@ -1,2 +1,2 @@ cdavid: OK3 -ptvirtan: OK2 +ptvirtan: OK3 From scipy-svn at scipy.org Sat Oct 23 10:40:54 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 23 Oct 2010 09:40:54 -0500 (CDT) Subject: [Scipy-svn] r6851 - trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC Message-ID: <20101023144054.30FF437D4A6@scipy.org> Author: ptvirtan Date: 2010-10-23 09:40:53 -0500 (Sat, 23 Oct 2010) New Revision: 6851 Modified: trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/cneupd.f trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/dneupd.f trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/sneupd.f trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/zneupd.f Log: BUG: sparse/arpack: apply patch from Debian bug:572935 -- fixes a crash inside ARPACK (#1313) Modified: trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/cneupd.f =================================================================== --- trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/cneupd.f 2010-10-20 00:05:10 UTC (rev 6850) +++ trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/cneupd.f 2010-10-23 14:40:53 UTC (rev 6851) @@ -301,7 +301,7 @@ & invsub, iuptri, iwev , j , ldh , ldq , & mode , msglvl, ritz , wr , k , irz , & ibd , outncv, iq , np , numcnv, jj , - & ishift + & ishift, nconv2 Complex & rnorm, temp, vl(1) Real @@ -592,9 +592,13 @@ call ctrsen('None' , 'V' , select , & ncv , workl(iuptri), ldh , & workl(invsub), ldq , workl(iheig), - & nconv , conds , sep , + & nconv2 , conds , sep , & workev , ncv , ierr) c + if (nconv2 .lt. nconv) then + nconv = nconv2 + end if + if (ierr .eq. 1) then info = 1 go to 9000 Modified: trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/dneupd.f =================================================================== --- trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/dneupd.f 2010-10-20 00:05:10 UTC (rev 6850) +++ trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/dneupd.f 2010-10-23 14:40:53 UTC (rev 6851) @@ -353,7 +353,7 @@ & mode , msglvl, outncv, ritzr , & ritzi , wri , wrr , irr , & iri , ibd , ishift, numcnv , - & np , jj + & np , jj , nconv2 logical reord Double precision & conds , rnorm, sep , temp, @@ -661,16 +661,21 @@ & workl(iuptri), ldh , & workl(invsub), ldq , & workl(iheigr), workl(iheigi), - & nconv , conds , + & nconv2 , conds , & sep , workl(ihbds) , & ncv , iwork , & 1 , ierr) c + if (nconv2 .lt. nconv) then + nconv = nconv2 + end if + if (ierr .eq. 1) then info = 1 go to 9000 end if c + if (msglvl .gt. 2) then call dvout (logfil, ncv, workl(iheigr), ndigit, & '_neupd: Real part of the eigenvalues of H--reordered') Modified: trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/sneupd.f =================================================================== --- trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/sneupd.f 2010-10-20 00:05:10 UTC (rev 6850) +++ trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/sneupd.f 2010-10-23 14:40:53 UTC (rev 6851) @@ -353,7 +353,7 @@ & mode , msglvl, outncv, ritzr , & ritzi , wri , wrr , irr , & iri , ibd , ishift, numcnv , - & np , jj + & np , jj , nconv2 logical reord Real & conds , rnorm, sep , temp, @@ -661,11 +661,15 @@ & workl(iuptri), ldh , & workl(invsub), ldq , & workl(iheigr), workl(iheigi), - & nconv , conds , + & nconv2 , conds , & sep , workl(ihbds) , & ncv , iwork , & 1 , ierr) c + if (nconv2 .lt. nconv) then + nconv = nconv2 + end if + if (ierr .eq. 1) then info = 1 go to 9000 Modified: trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/zneupd.f =================================================================== --- trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/zneupd.f 2010-10-20 00:05:10 UTC (rev 6850) +++ trunk/scipy/sparse/linalg/eigen/arpack/ARPACK/SRC/zneupd.f 2010-10-23 14:40:53 UTC (rev 6851) @@ -301,7 +301,7 @@ & invsub, iuptri, iwev , j , ldh , ldq , & mode , msglvl, ritz , wr , k , irz , & ibd , outncv, iq , np , numcnv, jj , - & ishift + & ishift, nconv2 Complex*16 & rnorm, temp, vl(1) Double precision @@ -592,9 +592,13 @@ call ztrsen('None' , 'V' , select , & ncv , workl(iuptri), ldh , & workl(invsub), ldq , workl(iheig), - & nconv , conds , sep , + & nconv2 , conds , sep , & workev , ncv , ierr) c + if (nconv2 .lt. nconv) then + nconv = nconv2 + end if + if (ierr .eq. 1) then info = 1 go to 9000 From scipy-svn at scipy.org Sat Oct 23 10:41:17 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 23 Oct 2010 09:41:17 -0500 (CDT) Subject: [Scipy-svn] r6852 - trunk/scipy/sparse/linalg/eigen/arpack Message-ID: <20101023144117.F041E37D4A6@scipy.org> Author: ptvirtan Date: 2010-10-23 09:41:16 -0500 (Sat, 23 Oct 2010) New Revision: 6852 Modified: trunk/scipy/sparse/linalg/eigen/arpack/arpack.py Log: BUG: sparse/arpack: fix ARPACK overwriting the initial vector (#1313) Modified: trunk/scipy/sparse/linalg/eigen/arpack/arpack.py =================================================================== --- trunk/scipy/sparse/linalg/eigen/arpack/arpack.py 2010-10-23 14:40:53 UTC (rev 6851) +++ trunk/scipy/sparse/linalg/eigen/arpack/arpack.py 2010-10-23 14:41:16 UTC (rev 6852) @@ -68,7 +68,8 @@ raise ValueError("matrix type must be 'f', 'd', 'F', or 'D'") if v0 is not None: - self.resid = v0 + # ARPACK overwrites its initial resid, make a copy + self.resid = np.array(v0, copy=True) info = 1 else: self.resid = np.zeros(n, tp) From scipy-svn at scipy.org Sat Oct 23 10:41:40 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 23 Oct 2010 09:41:40 -0500 (CDT) Subject: [Scipy-svn] r6853 - trunk/scipy/sparse/linalg/eigen/arpack/tests Message-ID: <20101023144140.AC1B937D4A6@scipy.org> Author: ptvirtan Date: 2010-10-23 09:41:40 -0500 (Sat, 23 Oct 2010) New Revision: 6853 Modified: trunk/scipy/sparse/linalg/eigen/arpack/tests/test_arpack.py Log: TST: sparse/arpack: make ARPACK tests deterministic by setting an initial vector (#1313) Modified: trunk/scipy/sparse/linalg/eigen/arpack/tests/test_arpack.py =================================================================== --- trunk/scipy/sparse/linalg/eigen/arpack/tests/test_arpack.py 2010-10-23 14:41:16 UTC (rev 6852) +++ trunk/scipy/sparse/linalg/eigen/arpack/tests/test_arpack.py 2010-10-23 14:41:40 UTC (rev 6853) @@ -52,6 +52,13 @@ [ 0., 0., 0., 0., 1., -1.], [-1., -1., -1., -1., -1., 5.]]) + S1['v0']= array([0.39574246391875789, + 0.00086496039750016962, + -0.9227205789982591, + -0.9165671495278005, + 0.1175963848841306, + -0.29962625203712179]) + S1['eval']=array([0,1,2,2,5,6]) self.symmetric.append(S1) @@ -63,15 +70,18 @@ [ 1., 6., 1., 0., -4.], [ 2., -6., 4., 9., -3]]) + N1['v0'] = array([0.39574246391875789, + 0.00086496039750016962, + -0.9227205789982591, + -0.9165671495278005, + 0.1175963848841306]) + N1['eval']=\ array([ -5.4854094033782888+0.0j, -2.2169058544873783+8.5966096591588261j, -2.2169058544873783-8.5966096591588261j, 4.4596105561765107+3.8007839204319454j, 4.4596105561765107-3.8007839204319454j],'D') - - - self.nonsymmetric.append(N1) @@ -93,10 +103,12 @@ high=range(len(eval))[-h:] return eval[low+high] - def eval_evec(self,d,typ,k,which,**kwds): + def eval_evec(self,d,typ,k,which,v0=None): a=d['mat'].astype(typ) + if v0 == None: + v0 = d['v0'] exact_eval=self.get_exact_eval(d,typ,k,which) - eval,evec=eigen_symmetric(a,k,which=which,**kwds) + eval,evec=eigen_symmetric(a,k,which=which,v0=v0) # check eigenvalues assert_array_almost_equal(eval,exact_eval,decimal=_ndigits[typ]) # check eigenvectors A*evec=eval*evec @@ -132,14 +144,16 @@ if which=='SM' or which=='SR': return ind[:k] - def eval_evec(self,d,typ,k,which): + def eval_evec(self,d,typ,k,which,v0=None): a=d['mat'].astype(typ) + if v0 == None: + v0 = d['v0'] # get exact eigenvalues exact_eval=d['eval'].astype(typ) ind=self.sort_choose(exact_eval,typ,k,which) exact_eval=exact_eval[ind] # compute eigenvalues - eval,evec=eigen(a,k,which=which) + eval,evec=eigen(a,k,which=which,v0=v0) ind=self.sort_choose(eval,typ,k,which) eval=eval[ind] evec=evec[:,ind] @@ -156,7 +170,7 @@ k=2 for typ in 'FD': for which in ['LM','SM','LR','SR']: - self.eval_evec(self.symmetric[0],typ,k,which) + self.eval_evec(self.symmetric[0],typ,k,which) class TestEigenNonSymmetric(TestArpack): @@ -178,14 +192,16 @@ return ind[:k] - def eval_evec(self,d,typ,k,which,**kwds): + def eval_evec(self,d,typ,k,which,v0=None): a=d['mat'].astype(typ) + if v0 == None: + v0 = d['v0'] # get exact eigenvalues exact_eval=d['eval'].astype(typ.upper()) ind=self.sort_choose(exact_eval,typ,k,which) exact_eval=exact_eval[ind] # compute eigenvalues - eval,evec=eigen(a,k,which=which,**kwds) + eval,evec=eigen(a,k,which=which,v0=v0) ind=self.sort_choose(eval,typ,k,which) eval=eval[ind] evec=evec[:,ind] @@ -235,8 +251,10 @@ if which in ['SR','SI','SM']: return ind[:k] - def eval_evec(self,d,typ,k,which): + def eval_evec(self,d,typ,k,which,v0=None): a=d['mat'].astype(typ) + if v0 == None: + v0 = d['v0'] # get exact eigenvalues exact_eval=d['eval'].astype(typ.upper()) ind=self.sort_choose(exact_eval,typ,k,which) @@ -247,7 +265,7 @@ # compute eigenvalues - eval,evec=eigen(a,k,which=which) + eval,evec=eigen(a,k,which=which,v0=v0) ind=self.sort_choose(eval,typ,k,which) eval=eval[ind] evec=evec[:,ind] From scipy-svn at scipy.org Sat Oct 23 10:42:01 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 23 Oct 2010 09:42:01 -0500 (CDT) Subject: [Scipy-svn] r6854 - trunk/scipy/sparse/linalg/eigen/arpack Message-ID: <20101023144201.500AD37D4A6@scipy.org> Author: ptvirtan Date: 2010-10-23 09:42:01 -0500 (Sat, 23 Oct 2010) New Revision: 6854 Modified: trunk/scipy/sparse/linalg/eigen/arpack/arpack.py Log: BUG: sparse/arpack: fix off-by-two in check for ncv Modified: trunk/scipy/sparse/linalg/eigen/arpack/arpack.py =================================================================== --- trunk/scipy/sparse/linalg/eigen/arpack/arpack.py 2010-10-23 14:41:40 UTC (rev 6853) +++ trunk/scipy/sparse/linalg/eigen/arpack/arpack.py 2010-10-23 14:42:01 UTC (rev 6854) @@ -82,9 +82,6 @@ ncv = 2 * k + 1 ncv = min(ncv, n) - if ncv > n or ncv < k: - raise ValueError("ncv must be k<=ncv<=n, ncv=%s" % ncv) - self.v = np.zeros((n, ncv), tp) # holds Ritz vectors self.iparam = np.zeros(11, "int") @@ -119,6 +116,9 @@ _ArpackParams.__init__(self, n, k, tp, matvec, sigma, ncv, v0, maxiter, which, tol) + if self.ncv > n or self.ncv <= k: + raise ValueError("ncv must be k n or self.ncv <= k+1: + raise ValueError("ncv must be k+1 Author: warren.weckesser Date: 2010-10-24 14:19:41 -0500 (Sun, 24 Oct 2010) New Revision: 6855 Modified: trunk/scipy/sparse/info.py Log: DOC-BUG: sparse: Fixed Example 1 in the sparse module docstring (info.py) Modified: trunk/scipy/sparse/info.py =================================================================== --- trunk/scipy/sparse/info.py 2010-10-23 14:42:01 UTC (rev 6854) +++ trunk/scipy/sparse/info.py 2010-10-24 19:19:41 UTC (rev 6855) @@ -33,10 +33,12 @@ --------- Construct a 1000x1000 lil_matrix and add some values to it: ->>> from scipy import sparse, linsolve ->>> from numpy import linalg +>>> from scipy.sparse import lil_matrix +>>> from scipy.sparse.linalg import spsolve +>>> from numpy.linalg import solve, norm >>> from numpy.random import rand ->>> A = sparse.lil_matrix((1000, 1000)) + +>>> A = lil_matrix((1000, 1000)) >>> A[0, :100] = rand(100) >>> A[1, 100:200] = A[0, :100] >>> A.setdiag(rand(1000)) @@ -45,16 +47,16 @@ >>> A = A.tocsr() >>> b = rand(1000) ->>> x = linsolve.spsolve(A, b) +>>> x = spsolve(A, b) Convert it to a dense matrix and solve, and check that the result is the same: ->>> x_ = linalg.solve(A.todense(), b) +>>> x_ = solve(A.todense(), b) Now we can compute norm of the error with: ->>> err = linalg.norm(x-x_) +>>> err = norm(x-x_) >>> err < 1e-10 True From scipy-svn at scipy.org Mon Oct 25 16:10:08 2010 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 25 Oct 2010 15:10:08 -0500 (CDT) Subject: [Scipy-svn] r6856 - trunk/doc/release Message-ID: <20101025201008.14C5037CA92@scipy.org> Author: ptvirtan Date: 2010-10-25 15:10:06 -0500 (Mon, 25 Oct 2010) New Revision: 6856 Modified: trunk/doc/release/0.9.0-notes.rst Log: DOC: update release notes Modified: trunk/doc/release/0.9.0-notes.rst =================================================================== --- trunk/doc/release/0.9.0-notes.rst 2010-10-24 19:19:41 UTC (rev 6855) +++ trunk/doc/release/0.9.0-notes.rst 2010-10-25 20:10:06 UTC (rev 6856) @@ -37,10 +37,8 @@ Python 3 ======== -.. note:: This is the goal, we aren't there yet! +Scipy 0.9.0 has full support for Python 3. -Scipy 0.9.0 is the first release to have full support for Python 3. - Deprecated features =================== @@ -102,5 +100,13 @@ The ``scipy.optimize.nonlin`` module was completely rewritten, and some of the functions were deprecated (see above). + +New linear algebra routines (``scipy.linalg``) +---------------------------------------------- + +Scipy now contains routines for effectively solving triangular +equation systems (``scipy.linalg.solve_triangular``). + + Removed features ================