diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-09-02 13:21:48 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-09-03 05:22:10 -0600 |
commit | d2c44c5a012d0a5db7d10a2586c8e9c58045d04b (patch) | |
tree | b93306d17c5460a68d7e7fb29e6be729ad2a28dd | |
parent | 57245e4570fa84f2e40a6fa1cc22716d279f8c00 (diff) | |
download | numpy-d2c44c5a012d0a5db7d10a2586c8e9c58045d04b.tar.gz |
STY: Make numpy/lib/test/*.py PEP8 compliant.
Run autopep8 over the test files in numpy/lib/test and make fixes
to the result.
Also remove Python5 workaround.
-rw-r--r-- | numpy/lib/tests/test__datasource.py | 14 | ||||
-rw-r--r-- | numpy/lib/tests/test__iotools.py | 59 | ||||
-rw-r--r-- | numpy/lib/tests/test_arraypad.py | 705 | ||||
-rw-r--r-- | numpy/lib/tests/test_arraysetops.py | 111 | ||||
-rw-r--r-- | numpy/lib/tests/test_financial.py | 10 | ||||
-rw-r--r-- | numpy/lib/tests/test_format.py | 38 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 319 | ||||
-rw-r--r-- | numpy/lib/tests/test_index_tricks.py | 82 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 239 | ||||
-rw-r--r-- | numpy/lib/tests/test_nanfunctions.py | 28 | ||||
-rw-r--r-- | numpy/lib/tests/test_polynomial.py | 14 | ||||
-rw-r--r-- | numpy/lib/tests/test_recfunctions.py | 381 | ||||
-rw-r--r-- | numpy/lib/tests/test_regression.py | 33 | ||||
-rw-r--r-- | numpy/lib/tests/test_shape_base.py | 81 | ||||
-rw-r--r-- | numpy/lib/tests/test_stride_tricks.py | 10 | ||||
-rw-r--r-- | numpy/lib/tests/test_twodim_base.py | 192 | ||||
-rw-r--r-- | numpy/lib/tests/test_type_check.py | 6 | ||||
-rw-r--r-- | numpy/lib/tests/test_ufunclike.py | 4 | ||||
-rw-r--r-- | numpy/lib/tests/test_utils.py | 6 |
19 files changed, 1246 insertions, 1086 deletions
diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py index 8d3e32b1b..f61d3086b 100644 --- a/numpy/lib/tests/test__datasource.py +++ b/numpy/lib/tests/test__datasource.py @@ -17,6 +17,7 @@ else: from urlparse import urlparse from urllib2 import URLError + def urlopen_stub(url, data=None): '''Stub to replace urlopen for testing.''' if url == valid_httpurl(): @@ -28,12 +29,14 @@ def urlopen_stub(url, data=None): # setup and teardown old_urlopen = None + def setup(): global old_urlopen old_urlopen = urllib_request.urlopen urllib_request.urlopen = urlopen_stub + def teardown(): urllib_request.urlopen = old_urlopen @@ -57,6 +60,7 @@ def valid_textfile(filedir): os.close(fd) return path + def invalid_textfile(filedir): # Generate and return an invalid filename. fd, path = mkstemp(suffix='.txt', prefix='dstmp_', dir=filedir) @@ -64,24 +68,31 @@ def invalid_textfile(filedir): os.remove(path) return path + def valid_httpurl(): return http_path+http_file + def invalid_httpurl(): return http_fakepath+http_fakefile + def valid_baseurl(): return http_path + def invalid_baseurl(): return http_fakepath + def valid_httpfile(): return http_file + def invalid_httpfile(): return http_fakefile + class TestDataSourceOpen(TestCase): def setUp(self): self.tmpdir = mkdtemp() @@ -259,7 +270,7 @@ class TestRepositoryAbspath(TestCase): def test_ValidHTTP(self): scheme, netloc, upath, pms, qry, frg = urlparse(valid_httpurl()) - local_path = os.path.join(self.repos._destpath, netloc, \ + local_path = os.path.join(self.repos._destpath, netloc, upath.strip(os.sep).strip('/')) filepath = self.repos.abspath(valid_httpfile()) self.assertEqual(local_path, filepath) @@ -313,6 +324,7 @@ class TestRepositoryExists(TestCase): tmpfile = valid_textfile(local_path) assert_(self.repos.exists(tmpfile)) + class TestOpenFunc(TestCase): def setUp(self): self.tmpdir = mkdtemp() diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py index 421616ccd..d564121d1 100644 --- a/numpy/lib/tests/test__iotools.py +++ b/numpy/lib/tests/test__iotools.py @@ -5,14 +5,17 @@ import time from datetime import date import numpy as np -from numpy.lib._iotools import LineSplitter, NameValidator, StringConverter, \ - has_nested_fields, easy_dtype, flatten_dtype +from numpy.lib._iotools import ( + LineSplitter, NameValidator, StringConverter, + has_nested_fields, easy_dtype, flatten_dtype + ) from numpy.testing import * from numpy.compat import asbytes, asbytes_nested + class TestLineSplitter(TestCase): "Tests the LineSplitter class." - # + def test_no_delimiter(self): "Test LineSplitter w/o delimiter" strg = asbytes(" 1 2 3 4 5 # test") @@ -71,11 +74,11 @@ class TestLineSplitter(TestCase): test = LineSplitter((6, 6, 9))(strg) assert_equal(test, asbytes_nested(['1', '3 4', '5 6'])) - #------------------------------------------------------------------------------- + class TestNameValidator(TestCase): - # + def test_case_sensitivity(self): "Test case sensitivity" names = ['A', 'a', 'b', 'c'] @@ -87,14 +90,14 @@ class TestNameValidator(TestCase): assert_equal(test, ['A', 'A_1', 'B', 'C']) test = NameValidator(case_sensitive='lower').validate(names) assert_equal(test, ['a', 'a_1', 'b', 'c']) - # + def test_excludelist(self): "Test excludelist" names = ['dates', 'data', 'Other Data', 'mask'] validator = NameValidator(excludelist=['dates', 'data', 'mask']) test = validator.validate(names) assert_equal(test, ['dates_', 'data_', 'Other_Data', 'mask_']) - # + def test_missing_names(self): "Test validate missing names" namelist = ('a', 'b', 'c') @@ -106,7 +109,7 @@ class TestNameValidator(TestCase): assert_equal(validator(namelist), ['a', 'b', 'f0']) namelist = ('', 'f0', '') assert_equal(validator(namelist), ['f1', 'f0', 'f2']) - # + def test_validate_nb_names(self): "Test validate nb names" namelist = ('a', 'b', 'c') @@ -114,7 +117,7 @@ class TestNameValidator(TestCase): assert_equal(validator(namelist, nbfields=1), ('a',)) assert_equal(validator(namelist, nbfields=5, defaultfmt="g%i"), ['a', 'b', 'c', 'g0', 'g1']) - # + def test_validate_wo_names(self): "Test validate no names" namelist = None @@ -122,26 +125,25 @@ class TestNameValidator(TestCase): assert_(validator(namelist) is None) assert_equal(validator(namelist, nbfields=3), ['f0', 'f1', 'f2']) - - - #------------------------------------------------------------------------------- + def _bytes_to_date(s): if sys.version_info[0] >= 3: return date(*time.strptime(s.decode('latin1'), "%Y-%m-%d")[:3]) else: return date(*time.strptime(s, "%Y-%m-%d")[:3]) + class TestStringConverter(TestCase): "Test StringConverter" - # + def test_creation(self): "Test creation of a StringConverter" converter = StringConverter(int, -99999) assert_equal(converter._status, 1) assert_equal(converter.default, -99999) - # + def test_upgrade(self): "Tests the upgrade method." converter = StringConverter() @@ -154,7 +156,7 @@ class TestStringConverter(TestCase): assert_equal(converter._status, 3) converter.upgrade(asbytes('a')) assert_equal(converter._status, len(converter._mapper) - 1) - # + def test_missing(self): "Tests the use of missing values." converter = StringConverter(missing_values=(asbytes('missing'), @@ -168,7 +170,7 @@ class TestStringConverter(TestCase): converter('miss') except ValueError: pass - # + def test_upgrademapper(self): "Tests updatemapper" dateparser = _bytes_to_date @@ -180,37 +182,39 @@ class TestStringConverter(TestCase): assert_equal(test, date(2009, 1, 1)) test = convert(asbytes('')) assert_equal(test, date(2000, 1, 1)) - # + def test_string_to_object(self): "Make sure that string-to-object functions are properly recognized" conv = StringConverter(_bytes_to_date) assert_equal(conv._mapper[-2][0](0), 0j) assert_(hasattr(conv, 'default')) - # + def test_keep_default(self): "Make sure we don't lose an explicit default" converter = StringConverter(None, missing_values=asbytes(''), - default= -999) + default=-999) converter.upgrade(asbytes('3.14159265')) assert_equal(converter.default, -999) assert_equal(converter.type, np.dtype(float)) # - converter = StringConverter(None, missing_values=asbytes(''), default=0) + converter = StringConverter( + None, missing_values=asbytes(''), default=0) converter.upgrade(asbytes('3.14159265')) assert_equal(converter.default, 0) assert_equal(converter.type, np.dtype(float)) - # + def test_keep_default_zero(self): "Check that we don't lose a default of 0" converter = StringConverter(int, default=0, missing_values=asbytes("N/A")) assert_equal(converter.default, 0) - # + def test_keep_missing_values(self): "Check that we're not losing missing values" converter = StringConverter(int, default=0, missing_values=asbytes("N/A")) - assert_equal(converter.missing_values, set(asbytes_nested(['', 'N/A']))) + assert_equal( + converter.missing_values, set(asbytes_nested(['', 'N/A']))) def test_int64_dtype(self): "Check that int64 integer types can be specified" @@ -226,10 +230,9 @@ class TestStringConverter(TestCase): val = asbytes("9223372043271415339") assert_(converter(val) == 9223372043271415339) -#------------------------------------------------------------------------------- class TestMiscFunctions(TestCase): - # + def test_has_nested_dtype(self): "Test has_nested_dtype" ndtype = np.dtype(np.float) @@ -292,9 +295,9 @@ class TestMiscFunctions(TestCase): np.dtype([(_, float) for _ in ('a', 'b', 'c')])) # As simple dtype w/o names (but multiple fields) ndtype = np.dtype(float) - assert_equal(easy_dtype(ndtype, names=['', '', ''], defaultfmt="f%02i"), - np.dtype([(_, float) for _ in ('f00', 'f01', 'f02')])) - + assert_equal( + easy_dtype(ndtype, names=['', '', ''], defaultfmt="f%02i"), + np.dtype([(_, float) for _ in ('f00', 'f01', 'f02')])) def test_flatten_dtype(self): "Testing flatten_dtype" diff --git a/numpy/lib/tests/test_arraypad.py b/numpy/lib/tests/test_arraypad.py index ec96f5c8c..e07f856bb 100644 --- a/numpy/lib/tests/test_arraypad.py +++ b/numpy/lib/tests/test_arraypad.py @@ -13,209 +13,219 @@ class TestStatistic(TestCase): def test_check_mean_stat_length(self): a = np.arange(100).astype('f') a = pad(a, ((25, 20), ), 'mean', stat_length=((2, 3), )) - b = np.array([ - 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, - 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, - 0.5, 0.5, 0.5, 0.5, 0.5, - - 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., - 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., - 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., - 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., - 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., - 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., - 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., - 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., - 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., - - 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., - 98., 98., 98., 98., 98., 98., 98., 98., 98., 98.]) + b = np.array( + [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, + 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, + 0.5, 0.5, 0.5, 0.5, 0.5, + + 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., + 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., + 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., + 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., + 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., + 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., + 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., + 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., + 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., + 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., + + 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., + 98., 98., 98., 98., 98., 98., 98., 98., 98., 98. + ]) assert_array_equal(a, b) def test_check_maximum_1(self): a = np.arange(100) a = pad(a, (25, 20), 'maximum') - b = np.array([ - 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, - 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, - 99, 99, 99, 99, 99, - - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, - 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, - 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, - 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, - 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, - - 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, - 99, 99, 99, 99, 99, 99, 99, 99, 99, 99]) + b = np.array( + [99, 99, 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, + + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, + 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, + 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, + + 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, 99, 99] + ) assert_array_equal(a, b) def test_check_maximum_2(self): a = np.arange(100) + 1 a = pad(a, (25, 20), 'maximum') - b = np.array([ - 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, - 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, - 100, 100, 100, 100, 100, - - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, - 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, - 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, - 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, - 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, - 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, - 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, - 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, - 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, - - 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, - 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]) + b = np.array( + [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, + 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, + 100, 100, 100, 100, 100, + + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, + 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, + 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, + 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, + 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, + + 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, + 100, 100, 100, 100, 100, 100, 100, 100, 100, 100] + ) assert_array_equal(a, b) def test_check_minimum_1(self): a = np.arange(100) a = pad(a, (25, 20), 'minimum') - b = 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, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, - 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, - 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, - 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, - 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, - - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + b = 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, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, + 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, + 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, + + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + ) assert_array_equal(a, b) def test_check_minimum_2(self): a = np.arange(100) + 2 a = pad(a, (25, 20), 'minimum') - b = np.array([ - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, - - 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, - 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, - 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, - 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, - 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, - 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, - 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, - 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, - - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) + b = np.array( + [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, + + 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, + 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, + 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, + 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, + 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, + 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, + 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, + + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] + ) assert_array_equal(a, b) def test_check_median(self): a = np.arange(100).astype('f') a = pad(a, (25, 20), 'median') - b = np.array([ - 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, - 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, - 49.5, 49.5, 49.5, 49.5, 49.5, - - 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., - 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., - 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., - 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., - 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., - 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., - 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., - 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., - 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., - - 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, - 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]) + b = np.array( + [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, + 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, + 49.5, 49.5, 49.5, 49.5, 49.5, + + 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., + 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., + 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., + 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., + 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., + 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., + 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., + 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., + 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., + 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., + + 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, + 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5] + ) assert_array_equal(a, b) def test_check_median_01(self): a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]]) a = pad(a, 1, 'median') - b = np.array([ - [4, 4, 5, 4, 4], + b = np.array( + [[4, 4, 5, 4, 4], - [3, 3, 1, 4, 3], - [5, 4, 5, 9, 5], - [8, 9, 8, 2, 8], + [3, 3, 1, 4, 3], + [5, 4, 5, 9, 5], + [8, 9, 8, 2, 8], - [4, 4, 5, 4, 4]]) + [4, 4, 5, 4, 4]] + ) assert_array_equal(a, b) def test_check_median_02(self): a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]]) a = pad(a.T, 1, 'median').T - b = np.array([ - [5, 4, 5, 4, 5], + b = np.array( + [[5, 4, 5, 4, 5], - [3, 3, 1, 4, 3], - [5, 4, 5, 9, 5], - [8, 9, 8, 2, 8], + [3, 3, 1, 4, 3], + [5, 4, 5, 9, 5], + [8, 9, 8, 2, 8], - [5, 4, 5, 4, 5]]) + [5, 4, 5, 4, 5]] + ) assert_array_equal(a, b) def test_check_mean_shape_one(self): a = [[4, 5, 6]] a = pad(a, (5, 7), 'mean', stat_length=2) - b = np.array([ - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], - [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6]]) + b = np.array( + [[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], + [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6]] + ) assert_array_equal(a, b) def test_check_mean_2(self): a = np.arange(100).astype('f') a = pad(a, (25, 20), 'mean') - b = np.array([ - 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, - 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, - 49.5, 49.5, 49.5, 49.5, 49.5, - - 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., - 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., - 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., - 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., - 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., - 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., - 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., - 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., - 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., - - 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, - 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]) + b = np.array( + [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, + 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, + 49.5, 49.5, 49.5, 49.5, 49.5, + + 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., + 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., + 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., + 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., + 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., + 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., + 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., + 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., + 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., + 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., + + 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, + 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5] + ) assert_array_equal(a, b) @@ -223,23 +233,25 @@ class TestConstant(TestCase): def test_check_constant(self): a = np.arange(100) a = pad(a, (25, 20), 'constant', constant_values=(10, 20)) - b = np.array([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, - - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, - 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, - 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, - 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, - 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, - - 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, - 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]) + b = np.array( + [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, + + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, + 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, + 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, + + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20] + ) assert_array_equal(a, b) @@ -247,24 +259,25 @@ class TestLinearRamp(TestCase): def test_check_simple(self): a = np.arange(100).astype('f') a = pad(a, (25, 20), 'linear_ramp', end_values=(4, 5)) - b = np.array([ - 4.00, 3.84, 3.68, 3.52, 3.36, 3.20, 3.04, 2.88, 2.72, 2.56, - 2.40, 2.24, 2.08, 1.92, 1.76, 1.60, 1.44, 1.28, 1.12, 0.96, - 0.80, 0.64, 0.48, 0.32, 0.16, - - 0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00, - 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, - 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, - 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, - 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, - 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, - 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, - 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, - 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, - 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, - - 94.3, 89.6, 84.9, 80.2, 75.5, 70.8, 66.1, 61.4, 56.7, 52.0, - 47.3, 42.6, 37.9, 33.2, 28.5, 23.8, 19.1, 14.4, 9.7, 5.]) + b = np.array( + [4.00, 3.84, 3.68, 3.52, 3.36, 3.20, 3.04, 2.88, 2.72, 2.56, + 2.40, 2.24, 2.08, 1.92, 1.76, 1.60, 1.44, 1.28, 1.12, 0.96, + 0.80, 0.64, 0.48, 0.32, 0.16, + + 0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00, + 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, + 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, + 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, + 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, + 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, + 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, + 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, + 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, + 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, + + 94.3, 89.6, 84.9, 80.2, 75.5, 70.8, 66.1, 61.4, 56.7, 52.0, + 47.3, 42.6, 37.9, 33.2, 28.5, 23.8, 19.1, 14.4, 9.7, 5.] + ) assert_array_almost_equal(a, b, decimal=5) @@ -272,67 +285,70 @@ class TestReflect(TestCase): def test_check_simple(self): a = np.arange(100) a = pad(a, (25, 20), 'reflect') - b = np.array([ - 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, - 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, - 5, 4, 3, 2, 1, - - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, - 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, - 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, - 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, - 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, - - 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, - 88, 87, 86, 85, 84, 83, 82, 81, 80, 79]) + b = np.array( + [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, + 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, + 5, 4, 3, 2, 1, + + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, + 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, + 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, + + 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, + 88, 87, 86, 85, 84, 83, 82, 81, 80, 79] + ) assert_array_equal(a, b) def test_check_large_pad(self): a = [[4, 5, 6], [6, 7, 8]] a = pad(a, (5, 7), 'reflect') - b = np.array([ - [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], - - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], - - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]) + b = np.array( + [[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], + + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], + + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]] + ) assert_array_equal(a, b) def test_check_shape(self): a = [[4, 5, 6]] a = pad(a, (5, 7), 'reflect') - b = np.array([ - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], - [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]) + b = np.array( + [[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], + [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]] + ) assert_array_equal(a, b) def test_check_01(self): @@ -355,83 +371,85 @@ class TestWrap(TestCase): def test_check_simple(self): a = np.arange(100) a = pad(a, (25, 20), 'wrap') - b = np.array([ - 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, - 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, - 95, 96, 97, 98, 99, - - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, - 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, - 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, - 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, - 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, - - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) + b = np.array( + [75, 76, 77, 78, 79, 80, 81, 82, 83, 84, + 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, + 95, 96, 97, 98, 99, + + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, + 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, + 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, + + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] + ) assert_array_equal(a, b) def test_check_large_pad(self): a = np.arange(12) a = np.reshape(a, (3, 4)) a = pad(a, (10, 12), 'wrap') - b = np.array([ - [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, - 11, 8, 9, 10, 11, 8, 9, 10, 11], - [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, - 3, 0, 1, 2, 3, 0, 1, 2, 3], - [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, - 7, 4, 5, 6, 7, 4, 5, 6, 7], - [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, - 11, 8, 9, 10, 11, 8, 9, 10, 11], - [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, - 3, 0, 1, 2, 3, 0, 1, 2, 3], - [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, - 7, 4, 5, 6, 7, 4, 5, 6, 7], - [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, - 11, 8, 9, 10, 11, 8, 9, 10, 11], - [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, - 3, 0, 1, 2, 3, 0, 1, 2, 3], - [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, - 7, 4, 5, 6, 7, 4, 5, 6, 7], - [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, - 11, 8, 9, 10, 11, 8, 9, 10, 11], - - [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, - 3, 0, 1, 2, 3, 0, 1, 2, 3], - [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, - 7, 4, 5, 6, 7, 4, 5, 6, 7], - [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, - 11, 8, 9, 10, 11, 8, 9, 10, 11], - - [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, - 3, 0, 1, 2, 3, 0, 1, 2, 3], - [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, - 7, 4, 5, 6, 7, 4, 5, 6, 7], - [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, - 11, 8, 9, 10, 11, 8, 9, 10, 11], - [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, - 3, 0, 1, 2, 3, 0, 1, 2, 3], - [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, - 7, 4, 5, 6, 7, 4, 5, 6, 7], - [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, - 11, 8, 9, 10, 11, 8, 9, 10, 11], - [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, - 3, 0, 1, 2, 3, 0, 1, 2, 3], - [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, - 7, 4, 5, 6, 7, 4, 5, 6, 7], - [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, - 11, 8, 9, 10, 11, 8, 9, 10, 11], - [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, - 3, 0, 1, 2, 3, 0, 1, 2, 3], - [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, - 7, 4, 5, 6, 7, 4, 5, 6, 7], - [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, - 11, 8, 9, 10, 11, 8, 9, 10, 11]]) + b = np.array( + [[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, + 11, 8, 9, 10, 11, 8, 9, 10, 11], + [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, + 3, 0, 1, 2, 3, 0, 1, 2, 3], + [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, + 7, 4, 5, 6, 7, 4, 5, 6, 7], + [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, + 11, 8, 9, 10, 11, 8, 9, 10, 11], + [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, + 3, 0, 1, 2, 3, 0, 1, 2, 3], + [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, + 7, 4, 5, 6, 7, 4, 5, 6, 7], + [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, + 11, 8, 9, 10, 11, 8, 9, 10, 11], + [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, + 3, 0, 1, 2, 3, 0, 1, 2, 3], + [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, + 7, 4, 5, 6, 7, 4, 5, 6, 7], + [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, + 11, 8, 9, 10, 11, 8, 9, 10, 11], + + [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, + 3, 0, 1, 2, 3, 0, 1, 2, 3], + [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, + 7, 4, 5, 6, 7, 4, 5, 6, 7], + [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, + 11, 8, 9, 10, 11, 8, 9, 10, 11], + + [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, + 3, 0, 1, 2, 3, 0, 1, 2, 3], + [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, + 7, 4, 5, 6, 7, 4, 5, 6, 7], + [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, + 11, 8, 9, 10, 11, 8, 9, 10, 11], + [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, + 3, 0, 1, 2, 3, 0, 1, 2, 3], + [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, + 7, 4, 5, 6, 7, 4, 5, 6, 7], + [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, + 11, 8, 9, 10, 11, 8, 9, 10, 11], + [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, + 3, 0, 1, 2, 3, 0, 1, 2, 3], + [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, + 7, 4, 5, 6, 7, 4, 5, 6, 7], + [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, + 11, 8, 9, 10, 11, 8, 9, 10, 11], + [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, + 3, 0, 1, 2, 3, 0, 1, 2, 3], + [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, + 7, 4, 5, 6, 7, 4, 5, 6, 7], + [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, + 11, 8, 9, 10, 11, 8, 9, 10, 11]] + ) assert_array_equal(a, b) def test_check_01(self): @@ -450,19 +468,21 @@ class TestStatLen(TestCase): a = np.arange(30) a = np.reshape(a, (6, 5)) a = pad(a, ((2, 3), (3, 2)), mode='mean', stat_length=(3,)) - b = np.array([[6, 6, 6, 5, 6, 7, 8, 9, 8, 8], - [6, 6, 6, 5, 6, 7, 8, 9, 8, 8], - - [1, 1, 1, 0, 1, 2, 3, 4, 3, 3], - [6, 6, 6, 5, 6, 7, 8, 9, 8, 8], - [11, 11, 11, 10, 11, 12, 13, 14, 13, 13], - [16, 16, 16, 15, 16, 17, 18, 19, 18, 18], - [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], - [26, 26, 26, 25, 26, 27, 28, 29, 28, 28], - - [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], - [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], - [21, 21, 21, 20, 21, 22, 23, 24, 23, 23]]) + b = np.array( + [[6, 6, 6, 5, 6, 7, 8, 9, 8, 8], + [6, 6, 6, 5, 6, 7, 8, 9, 8, 8], + + [1, 1, 1, 0, 1, 2, 3, 4, 3, 3], + [6, 6, 6, 5, 6, 7, 8, 9, 8, 8], + [11, 11, 11, 10, 11, 12, 13, 14, 13, 13], + [16, 16, 16, 15, 16, 17, 18, 19, 18, 18], + [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], + [26, 26, 26, 25, 26, 27, 28, 29, 28, 28], + + [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], + [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], + [21, 21, 21, 20, 21, 22, 23, 24, 23, 23]] + ) assert_array_equal(a, b) @@ -470,19 +490,20 @@ class TestEdge(TestCase): def test_check_simple(self): a = np.arange(12) a = np.reshape(a, (4, 3)) - a = pad(a, ((2, 3), (3, 2)), 'edge' ) - b = np.array([ - [0, 0, 0, 0, 1, 2, 2, 2], - [0, 0, 0, 0, 1, 2, 2, 2], - - [0, 0, 0, 0, 1, 2, 2, 2], - [3, 3, 3, 3, 4, 5, 5, 5], - [6, 6, 6, 6, 7, 8, 8, 8], - [9, 9, 9, 9, 10, 11, 11, 11], - - [9, 9, 9, 9, 10, 11, 11, 11], - [9, 9, 9, 9, 10, 11, 11, 11], - [9, 9, 9, 9, 10, 11, 11, 11]]) + a = pad(a, ((2, 3), (3, 2)), 'edge') + b = np.array( + [[0, 0, 0, 0, 1, 2, 2, 2], + [0, 0, 0, 0, 1, 2, 2, 2], + + [0, 0, 0, 0, 1, 2, 2, 2], + [3, 3, 3, 3, 4, 5, 5, 5], + [6, 6, 6, 6, 7, 8, 8, 8], + [9, 9, 9, 9, 10, 11, 11, 11], + + [9, 9, 9, 9, 10, 11, 11, 11], + [9, 9, 9, 9, 10, 11, 11, 11], + [9, 9, 9, 9, 10, 11, 11, 11]] + ) assert_array_equal(a, b) @@ -500,21 +521,21 @@ class ValueError1(TestCase): arr = np.reshape(arr, (6, 5)) kwargs = dict(mode='mean', stat_length=(3, )) assert_raises(ValueError, pad, arr, ((2, 3), (3, 2), (4, 5)), - **kwargs) + **kwargs) def test_check_negative_stat_length(self): arr = np.arange(30) arr = np.reshape(arr, (6, 5)) kwargs = dict(mode='mean', stat_length=(-3, )) assert_raises(ValueError, pad, arr, ((2, 3), (3, 2)), - **kwargs) + **kwargs) def test_check_negative_pad_width(self): arr = np.arange(30) arr = np.reshape(arr, (6, 5)) kwargs = dict(mode='mean', stat_length=(3, )) assert_raises(ValueError, pad, arr, ((-2, 3), (3, 2)), - **kwargs) + **kwargs) class ValueError2(TestCase): @@ -523,7 +544,7 @@ class ValueError2(TestCase): arr = np.reshape(arr, (6, 5)) kwargs = dict(mode='mean', stat_length=(3, )) assert_raises(ValueError, pad, arr, ((2, 3, 4), (3, 2)), - **kwargs) + **kwargs) class ValueError3(TestCase): @@ -532,7 +553,7 @@ class ValueError3(TestCase): arr = np.reshape(arr, (6, 5)) kwargs = dict(mode='mean', stat_length=(3, )) assert_raises(ValueError, pad, arr, ((-2, 3), (3, 2)), - **kwargs) + **kwargs) if __name__ == "__main__": diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 7e8a7a6f3..5934ca05a 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -9,10 +9,10 @@ from numpy.lib.arraysetops import * import warnings -class TestSetOps(TestCase): +class TestSetOps(TestCase): - def test_unique( self ): + def test_unique(self): def check_all(a, b, i1, i2, dt): msg = "check values failed for type '%s'" % dt @@ -65,47 +65,46 @@ class TestSetOps(TestCase): bb = np.array(list(zip(b, b)), dt) check_all(aa, bb, i1, i2, dt) - - def test_intersect1d( self ): + def test_intersect1d(self): # unique inputs - a = np.array( [5, 7, 1, 2] ) - b = np.array( [2, 4, 3, 1, 5] ) + a = np.array([5, 7, 1, 2]) + b = np.array([2, 4, 3, 1, 5]) - ec = np.array( [1, 2, 5] ) - c = intersect1d( a, b, assume_unique=True ) - assert_array_equal( c, ec ) + ec = np.array([1, 2, 5]) + c = intersect1d(a, b, assume_unique=True) + assert_array_equal(c, ec) # non-unique inputs - a = np.array( [5, 5, 7, 1, 2] ) - b = np.array( [2, 1, 4, 3, 3, 1, 5] ) + a = np.array([5, 5, 7, 1, 2]) + b = np.array([2, 1, 4, 3, 3, 1, 5]) - ed = np.array( [1, 2, 5] ) - c = intersect1d( a, b ) - assert_array_equal( c, ed ) + ed = np.array([1, 2, 5]) + c = intersect1d(a, b) + assert_array_equal(c, ed) assert_array_equal([], intersect1d([], [])) - def test_setxor1d( self ): - a = np.array( [5, 7, 1, 2] ) - b = np.array( [2, 4, 3, 1, 5] ) + def test_setxor1d(self): + a = np.array([5, 7, 1, 2]) + b = np.array([2, 4, 3, 1, 5]) - ec = np.array( [3, 4, 7] ) - c = setxor1d( a, b ) - assert_array_equal( c, ec ) + ec = np.array([3, 4, 7]) + c = setxor1d(a, b) + assert_array_equal(c, ec) - a = np.array( [1, 2, 3] ) - b = np.array( [6, 5, 4] ) + a = np.array([1, 2, 3]) + b = np.array([6, 5, 4]) - ec = np.array( [1, 2, 3, 4, 5, 6] ) - c = setxor1d( a, b ) - assert_array_equal( c, ec ) + ec = np.array([1, 2, 3, 4, 5, 6]) + c = setxor1d(a, b) + assert_array_equal(c, ec) - a = np.array( [1, 8, 2, 3] ) - b = np.array( [6, 5, 4, 8] ) + a = np.array([1, 8, 2, 3]) + b = np.array([6, 5, 4, 8]) - ec = np.array( [1, 2, 3, 4, 5, 6] ) - c = setxor1d( a, b ) - assert_array_equal( c, ec ) + ec = np.array([1, 2, 3, 4, 5, 6]) + c = setxor1d(a, b) + assert_array_equal(c, ec) assert_array_equal([], setxor1d([], [])) @@ -181,7 +180,7 @@ class TestSetOps(TestCase): assert_array_equal(in1d([], []), []) - def test_in1d_char_array( self ): + def test_in1d_char_array(self): a = np.array(['a', 'b', 'c', 'd', 'e', 'c', 'e', 'b']) b = np.array(['a', 'c']) @@ -212,29 +211,29 @@ class TestSetOps(TestCase): assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec) - def test_union1d( self ): - a = np.array( [5, 4, 7, 1, 2] ) - b = np.array( [2, 4, 3, 3, 2, 1, 5] ) + def test_union1d(self): + a = np.array([5, 4, 7, 1, 2]) + b = np.array([2, 4, 3, 3, 2, 1, 5]) - ec = np.array( [1, 2, 3, 4, 5, 7] ) - c = union1d( a, b ) - assert_array_equal( c, ec ) + ec = np.array([1, 2, 3, 4, 5, 7]) + c = union1d(a, b) + assert_array_equal(c, ec) assert_array_equal([], union1d([], [])) - def test_setdiff1d( self ): - a = np.array( [6, 5, 4, 7, 1, 2, 7, 4] ) - b = np.array( [2, 4, 3, 3, 2, 1, 5] ) + def test_setdiff1d(self): + a = np.array([6, 5, 4, 7, 1, 2, 7, 4]) + b = np.array([2, 4, 3, 3, 2, 1, 5]) - ec = np.array( [6, 7] ) - c = setdiff1d( a, b ) - assert_array_equal( c, ec ) + ec = np.array([6, 7]) + c = setdiff1d(a, b) + assert_array_equal(c, ec) - a = np.arange( 21 ) - b = np.arange( 19 ) - ec = np.array( [19, 20] ) - c = setdiff1d( a, b ) - assert_array_equal( c, ec ) + a = np.arange(21) + b = np.arange(19) + ec = np.array([19, 20]) + c = setdiff1d(a, b) + assert_array_equal(c, ec) assert_array_equal([], setdiff1d([], [])) @@ -243,15 +242,15 @@ class TestSetOps(TestCase): b = np.array(['a', 'b', 's']) assert_array_equal(setdiff1d(a, b), np.array(['c'])) - def test_manyways( self ): - a = np.array( [5, 7, 1, 2, 8] ) - b = np.array( [9, 8, 2, 4, 3, 1, 5] ) + def test_manyways(self): + a = np.array([5, 7, 1, 2, 8]) + b = np.array([9, 8, 2, 4, 3, 1, 5]) - c1 = setxor1d( a, b ) - aux1 = intersect1d( a, b ) - aux2 = union1d( a, b ) - c2 = setdiff1d( aux2, aux1 ) - assert_array_equal( c1, c2 ) + c1 = setxor1d(a, b) + aux1 = intersect1d(a, b) + aux2 = union1d(a, b) + c2 = setdiff1d(aux2, aux1) + assert_array_equal(c1, c2) if __name__ == "__main__": diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py index 1894da8cb..6b7c6ef53 100644 --- a/numpy/lib/tests/test_financial.py +++ b/numpy/lib/tests/test_financial.py @@ -3,6 +3,7 @@ from __future__ import division, absolute_import, print_function from numpy.testing import * import numpy as np + class TestFinancial(TestCase): def test_rate(self): assert_almost_equal(np.rate(10, 0, -3500, 10000), @@ -40,8 +41,9 @@ class TestFinancial(TestCase): 50.0, 1) def test_npv(self): - assert_almost_equal(np.npv(0.05, [-15000, 1500, 2500, 3500, 4500, 6000]), - 122.89, 2) + assert_almost_equal( + np.npv(0.05, [-15000, 1500, 2500, 3500, 4500, 6000]), + 122.89, 2) def test_mirr(self): val = [-4500, -800, 800, 800, 600, 600, 800, 800, 700, 3000] @@ -122,7 +124,7 @@ class TestFinancial(TestCase): def test_broadcast(self): assert_almost_equal(np.nper(0.075, -2000, 0, 100000., [0, 1]), - [ 21.5449442, 20.76156441], 4) + [21.5449442, 20.76156441], 4) assert_almost_equal(np.ipmt(0.1/12, list(range(5)), 24, 2000), [-17.29165168, -16.66666667, -16.03647345, @@ -133,7 +135,7 @@ class TestFinancial(TestCase): -76.88882405, -77.52956425], 4) assert_almost_equal(np.ppmt(0.1/12, list(range(5)), 24, 2000, 0, - [0, 0, 1, 'end', 'begin']), + [0, 0, 1, 'end', 'begin']), [-74.998201, -75.62318601, -75.62318601, -76.88882405, -76.88882405], 4) diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index 694dc4591..81b672839 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -291,10 +291,13 @@ from numpy.compat import asbytes, asbytes_nested tempdir = None # Module-level setup. + + def setup_module(): global tempdir tempdir = tempfile.mkdtemp() + def teardown_module(): global tempdir if tempdir is not None and os.path.isdir(tempdir): @@ -394,8 +397,10 @@ NbufferT = [ # x Info color info y z # value y2 Info2 name z2 Name Value # name value y3 z3 - ([3, 2], (6j, 6., ('nn', [6j, 4j], [6., 4.], [1, 2]), 'NN', True), 'cc', ('NN', 6j), [[6., 4.], [6., 4.]], 8), - ([4, 3], (7j, 7., ('oo', [7j, 5j], [7., 5.], [2, 1]), 'OO', False), 'dd', ('OO', 7j), [[7., 5.], [7., 5.]], 9), + ([3, 2], (6j, 6., ('nn', [6j, 4j], [6., 4.], [1, 2]), 'NN', True), + 'cc', ('NN', 6j), [[6., 4.], [6., 4.]], 8), + ([4, 3], (7j, 7., ('oo', [7j, 5j], [7., 5.], [2, 1]), 'OO', False), + 'dd', ('OO', 7j), [[7., 5.], [7., 5.]], 9), ] record_arrays = [ @@ -405,6 +410,7 @@ record_arrays = [ np.array(NbufferT, dtype=np.dtype(Ndescr).newbyteorder('>')), ] + def roundtrip(arr): f = BytesIO() format.write_array(f, arr) @@ -412,6 +418,7 @@ def roundtrip(arr): arr2 = format.read_array(f2) return arr2 + def assert_equal(o1, o2): assert_(o1 == o2) @@ -421,6 +428,7 @@ def test_roundtrip(): arr2 = roundtrip(arr) yield assert_array_equal, arr, arr2 + def test_memmap_roundtrip(): # XXX: test crashes nose on windows. Fix this if not (sys.platform == 'win32' or sys.platform == 'cygwin'): @@ -437,9 +445,10 @@ def test_memmap_roundtrip(): finally: fp.close() - fortran_order = (arr.flags.f_contiguous and not arr.flags.c_contiguous) + fortran_order = ( + arr.flags.f_contiguous and not arr.flags.c_contiguous) ma = format.open_memmap(mfn, mode='w+', dtype=arr.dtype, - shape=arr.shape, fortran_order=fortran_order) + shape=arr.shape, fortran_order=fortran_order) ma[...] = arr del ma @@ -501,31 +510,36 @@ malformed_magic = asbytes_nested([ '', ]) + def test_read_magic_bad_magic(): for magic in malformed_magic: f = BytesIO(magic) yield raises(ValueError)(format.read_magic), f + def test_read_version_1_0_bad_magic(): for magic in bad_version_magic + malformed_magic: f = BytesIO(magic) yield raises(ValueError)(format.read_array), f + def test_bad_magic_args(): assert_raises(ValueError, format.magic, -1, 1) assert_raises(ValueError, format.magic, 256, 1) assert_raises(ValueError, format.magic, 1, -1) assert_raises(ValueError, format.magic, 1, 256) + def test_large_header(): s = BytesIO() - d = {'a':1,'b':2} + d = {'a': 1, 'b': 2} format.write_array_header_1_0(s, d) s = BytesIO() - d = {'a':1,'b':2,'c':'x'*256*256} + d = {'a': 1, 'b': 2, 'c': 'x'*256*256} assert_raises(ValueError, format.write_array_header_1_0, s, d) + def test_bad_header(): # header of length less than 2 should fail s = BytesIO() @@ -538,16 +552,16 @@ def test_bad_header(): assert_raises(ValueError, format.read_array_header_1_0, s) # headers without the exact keys required should fail - d = {"shape":(1, 2), - "descr":"x"} + d = {"shape": (1, 2), + "descr": "x"} s = BytesIO() format.write_array_header_1_0(s, d) assert_raises(ValueError, format.read_array_header_1_0, s) - d = {"shape":(1, 2), - "fortran_order":False, - "descr":"x", - "extrakey":-1} + d = {"shape": (1, 2), + "fortran_order": False, + "descr": "x", + "extrakey": -1} s = BytesIO() format.write_array_header_1_0(s, d) assert_raises(ValueError, format.read_array_header_1_0, s) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 360b6ab66..61113ae7a 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1,6 +1,7 @@ from __future__ import division, absolute_import, print_function import warnings + import numpy as np from numpy.testing import ( run_module_suite, TestCase, assert_, assert_equal, assert_array_equal, @@ -71,6 +72,7 @@ class TestCopy(TestCase): assert_(not a_fort_copy.flags.c_contiguous) assert_(a_fort_copy.flags.f_contiguous) + class TestAverage(TestCase): def test_basic(self): y1 = np.array([1, 2, 3]) @@ -185,7 +187,8 @@ class TestInsert(TestCase): #assert_equal(insert(a, np.array([True]*4), 9), [9,1,9,2,9,3,9]) with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', FutureWarning) - assert_equal(insert(a, np.array([True]*4), 9), [1, 9, 9, 9, 9, 2, 3]) + assert_equal( + insert(a, np.array([True]*4), 9), [1, 9, 9, 9, 9, 2, 3]) assert_(w[0].category is FutureWarning) def test_multidim(self): @@ -199,8 +202,9 @@ class TestInsert(TestCase): a = np.array([[1, 1], [2, 2], [3, 3]]) b = np.arange(1, 4).repeat(3).reshape(3, 3) - c = np.concatenate((a[:, 0:1], np.arange(1, 4).repeat(3).reshape(3, 3).T, - a[:, 1:2]), axis=1) + c = np.concatenate( + (a[:, 0:1], np.arange(1, 4).repeat(3).reshape(3, 3).T, + a[:, 1:2]), axis=1) assert_equal(insert(a, [1], [[1], [2], [3]], axis=1), b) assert_equal(insert(a, [1], [1, 2, 3], axis=1), c) # scalars behave differently, in this case exactly opposite: @@ -209,18 +213,18 @@ class TestInsert(TestCase): a = np.arange(4).reshape(2, 2) assert_equal(insert(a[:, :1], 1, a[:, 1], axis=1), a) - assert_equal(insert(a[:1,:], 1, a[1,:], axis=0), a) + assert_equal(insert(a[:1, :], 1, a[1, :], axis=0), a) # negative axis value a = np.arange(24).reshape((2, 3, 4)) - assert_equal(insert(a, 1, a[:,:, 3], axis=-1), - insert(a, 1, a[:,:, 3], axis=2)) - assert_equal(insert(a, 1, a[:, 2,:], axis=-2), - insert(a, 1, a[:, 2,:], axis=1)) + assert_equal(insert(a, 1, a[:, :, 3], axis=-1), + insert(a, 1, a[:, :, 3], axis=2)) + assert_equal(insert(a, 1, a[:, 2, :], axis=-2), + insert(a, 1, a[:, 2, :], axis=1)) # invalid axis value - assert_raises(IndexError, insert, a, 1, a[:, 2,:], axis=3) - assert_raises(IndexError, insert, a, 1, a[:, 2,:], axis=-4) + assert_raises(IndexError, insert, a, 1, a[:, 2, :], axis=3) + assert_raises(IndexError, insert, a, 1, a[:, 2, :], axis=-4) def test_0d(self): # This is an error in the future @@ -248,6 +252,7 @@ class TestInsert(TestCase): np.insert([0, 1, 2], x, [3, 4, 5]) assert_equal(x, np.array([1, 1, 1])) + class TestAmax(TestCase): def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6.0] @@ -278,7 +283,7 @@ class TestPtp(TestCase): [4, 10.0, 5.0], [8, 3.0, 2.0]] assert_equal(np.ptp(b, axis=0), [5.0, 7.0, 7.0]) - assert_equal(np.ptp(b, axis= -1), [6.0, 6.0, 6.0]) + assert_equal(np.ptp(b, axis=-1), [6.0, 6.0, 6.0]) class TestCumsum(TestCase): @@ -286,17 +291,19 @@ class TestCumsum(TestCase): ba = [1, 2, 10, 11, 6, 5, 4] ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]] for ctype in [np.int8, np.uint8, np.int16, np.uint16, np.int32, - np.uint32, np.float32, np.float64, np.complex64, np.complex128]: + np.uint32, np.float32, np.float64, np.complex64, np.complex128]: a = np.array(ba, ctype) a2 = np.array(ba2, ctype) - tgt = np.array([1, 3, 13, 24, 30, 35, 39], ctype) + tgt = np.array([1, 3, 13, 24, 30, 35, 39], ctype) assert_array_equal(np.cumsum(a, axis=0), tgt) - tgt = np.array([[1, 2, 3, 4], [6, 8, 10, 13], [16, 11, 14, 18]], ctype) + tgt = np.array( + [[1, 2, 3, 4], [6, 8, 10, 13], [16, 11, 14, 18]], ctype) assert_array_equal(np.cumsum(a2, axis=0), tgt) - tgt = np.array([[1, 3, 6, 10], [5, 11, 18, 27], [10, 13, 17, 22]], ctype) + tgt = np.array( + [[1, 3, 6, 10], [5, 11, 18, 27], [10, 13, 17, 22]], ctype) assert_array_equal(np.cumsum(a2, axis=1), tgt) @@ -315,9 +322,9 @@ class TestProd(TestCase): else: assert_equal(np.prod(a, axis=0), 26400) assert_array_equal(np.prod(a2, axis=0), - np.array([50, 36, 84, 180], ctype)) - assert_array_equal(np.prod(a2, axis= -1), - np.array([24, 1890, 600], ctype)) + np.array([50, 36, 84, 180], ctype)) + assert_array_equal(np.prod(a2, axis=-1), + np.array([24, 1890, 600], ctype)) class TestCumprod(TestCase): @@ -325,7 +332,7 @@ class TestCumprod(TestCase): ba = [1, 2, 10, 11, 6, 5, 4] ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]] for ctype in [np.int16, np.uint16, np.int32, np.uint32, - np.float32, np.float64, np.complex64, np.complex128]: + np.float32, np.float64, np.complex64, np.complex128]: a = np.array(ba, ctype) a2 = np.array(ba2, ctype) if ctype in ['1', 'b']: @@ -333,14 +340,17 @@ class TestCumprod(TestCase): self.assertRaises(ArithmeticError, cumprod, a2, 1) self.assertRaises(ArithmeticError, cumprod, a) else: - assert_array_equal(np.cumprod(a, axis= -1), - np.array([1, 2, 20, 220, 1320, 6600, 26400], ctype)) + assert_array_equal(np.cumprod(a, axis=-1), + np.array([1, 2, 20, 220, + 1320, 6600, 26400], ctype)) assert_array_equal(np.cumprod(a2, axis=0), - np.array([[ 1, 2, 3, 4], [ 5, 12, 21, 36], - [50, 36, 84, 180]], ctype)) - assert_array_equal(np.cumprod(a2, axis= -1), - np.array([[ 1, 2, 6, 24], [ 5, 30, 210, 1890], - [10, 30, 120, 600]], ctype)) + np.array([[1, 2, 3, 4], + [5, 12, 21, 36], + [50, 36, 84, 180]], ctype)) + assert_array_equal(np.cumprod(a2, axis=-1), + np.array([[1, 2, 6, 24], + [5, 30, 210, 1890], + [10, 30, 120, 600]], ctype)) class TestDiff(TestCase): @@ -355,10 +365,10 @@ class TestDiff(TestCase): def test_nd(self): x = 20 * rand(10, 20, 30) - out1 = x[:,:, 1:] - x[:,:, :-1] - out2 = out1[:,:, 1:] - out1[:,:, :-1] - out3 = x[1:,:,:] - x[:-1,:,:] - out4 = out3[1:,:,:] - out3[:-1,:,:] + out1 = x[:, :, 1:] - x[:, :, :-1] + out2 = out1[:, :, 1:] - out1[:, :, :-1] + out3 = x[1:, :, :] - x[:-1, :, :] + out4 = out3[1:, :, :] - out3[:-1, :, :] assert_array_equal(diff(x), out1) assert_array_equal(diff(x, n=2), out2) assert_array_equal(diff(x, axis=0), out3) @@ -378,10 +388,10 @@ class TestDelete(TestCase): if not isinstance(indices, (slice, int, long, np.integer)): indices = np.asarray(indices, dtype=np.intp) indices = indices[(indices >= 0) & (indices < 5)] - assert_array_equal(setxor1d(a_del, self.a[indices,]), self.a, + assert_array_equal(setxor1d(a_del, self.a[indices, ]), self.a, err_msg=msg) - xor = setxor1d(nd_a_del[0,:, 0], self.nd_a[0, indices, 0]) - assert_array_equal(xor, self.nd_a[0,:, 0], err_msg=msg) + xor = setxor1d(nd_a_del[0, :, 0], self.nd_a[0, indices, 0]) + assert_array_equal(xor, self.nd_a[0, :, 0], err_msg=msg) def test_slices(self): lims = [-6, -2, 0, 1, 2, 4, 5] @@ -428,6 +438,7 @@ class TestDelete(TestCase): assert_(isinstance(delete(a, slice(1, 2)), SubClass)) assert_(isinstance(delete(a, slice(1, -2)), SubClass)) + class TestGradient(TestCase): def test_basic(self): v = [[1, 1], [3, 4]] @@ -450,17 +461,24 @@ class TestGradient(TestCase): def test_datetime64(self): # Make sure gradient() can handle special types like datetime64 - x = np.array(['1910-08-16', '1910-08-11', '1910-08-10', '1910-08-12', - '1910-10-12', '1910-12-12', '1912-12-12'], - dtype='datetime64[D]') - dx = np.array([ -5, -3, 0, 31, 61, 396, 731], dtype='timedelta64[D]') + x = np.array( + ['1910-08-16', '1910-08-11', '1910-08-10', '1910-08-12', + '1910-10-12', '1910-12-12', '1912-12-12'], + dtype='datetime64[D]') + dx = np.array( + [-5, -3, 0, 31, 61, 396, 731], + dtype='timedelta64[D]') assert_array_equal(gradient(x), dx) assert_(dx.dtype == np.dtype('timedelta64[D]')) def test_timedelta64(self): # Make sure gradient() can handle special types like timedelta64 - x = np.array([-5, -3, 10, 12, 61, 321, 300], dtype='timedelta64[D]') - dx = np.array([ 2, 7, 7, 25, 154, 119, -21], dtype='timedelta64[D]') + x = np.array( + [-5, -3, 10, 12, 61, 321, 300], + dtype='timedelta64[D]') + dx = np.array( + [2, 7, 7, 25, 154, 119, -21], + dtype='timedelta64[D]') assert_array_equal(gradient(x), dx) assert_(dx.dtype == np.dtype('timedelta64[D]')) @@ -468,10 +486,12 @@ class TestGradient(TestCase): class TestAngle(TestCase): def test_basic(self): x = [1 + 3j, np.sqrt(2) / 2.0 + 1j * np.sqrt(2) / 2, - 1, 1j, -1, -1j, 1 - 3j, -1 + 3j] + 1, 1j, -1, -1j, 1 - 3j, -1 + 3j] y = angle(x) - yo = [np.arctan(3.0 / 1.0), np.arctan(1.0), 0, np.pi / 2, np.pi, -np.pi / 2.0, - - np.arctan(3.0 / 1.0), np.pi - np.arctan(3.0 / 1.0)] + yo = [ + np.arctan(3.0 / 1.0), + np.arctan(1.0), 0, np.pi / 2, np.pi, -np.pi / 2.0, + -np.arctan(3.0 / 1.0), np.pi - np.arctan(3.0 / 1.0)] z = angle(x, deg=1) zo = np.array(yo) * 180 / np.pi assert_array_almost_equal(y, yo, 11) @@ -555,6 +575,7 @@ class TestVectorize(TestCase): def test_keywords(self): import math + def foo(a, b=1): return a + b f = vectorize(foo) @@ -579,6 +600,7 @@ class TestVectorize(TestCase): def test_keywords2_ticket_2100(self): r"""Test kwarg support: enhancement ticket 2100""" import math + def foo(a, b=1): return a + b f = vectorize(foo) @@ -640,6 +662,7 @@ class TestVectorize(TestCase): """Regression test for issue 1156""" class Foo: b = 2 + def bar(self, a): return a**self.b assert_array_equal(vectorize(Foo().bar)(np.arange(9)), @@ -660,7 +683,7 @@ class TestVectorize(TestCase): def test_string_ticket_1892(self): """Test vectorization over strings: issue 1892.""" - f = np.vectorize(lambda x:x) + f = np.vectorize(lambda x: x) s = '0123456789'*10 assert_equal(s, f(s)) #z = f(np.array([s,s])) @@ -669,6 +692,7 @@ class TestVectorize(TestCase): def test_cache(self): """Ensure that vectorized func called exactly once per argument.""" _calls = [0] + @vectorize def f(x): _calls[0] += 1 @@ -678,6 +702,7 @@ class TestVectorize(TestCase): assert_array_equal(f(x), x*x) assert_equal(_calls[0], len(x)) + class TestDigitize(TestCase): def test_forward(self): x = np.arange(-6, 5) @@ -778,18 +803,18 @@ class TestTrapz(TestCase): wz[0] /= 2 wz[-1] /= 2 - q = x[:, None, None] + y[None,:, None] + z[None, None,:] + q = x[:, None, None] + y[None, :, None] + z[None, None, :] qx = (q * wx[:, None, None]).sum(axis=0) - qy = (q * wy[None,:, None]).sum(axis=1) - qz = (q * wz[None, None,:]).sum(axis=2) + qy = (q * wy[None, :, None]).sum(axis=1) + qz = (q * wz[None, None, :]).sum(axis=2) # n-d `x` r = trapz(q, x=x[:, None, None], axis=0) assert_almost_equal(r, qx) - r = trapz(q, x=y[None,:, None], axis=1) + r = trapz(q, x=y[None, :, None], axis=1) assert_almost_equal(r, qy) - r = trapz(q, x=z[None, None,:], axis=2) + r = trapz(q, x=z[None, None, :], axis=2) assert_almost_equal(r, qz) # 1-d `x` @@ -807,7 +832,7 @@ class TestTrapz(TestCase): y = x * x mask = x == 2 ym = np.ma.array(y, mask=mask) - r = 13.0 # sum(0.5 * (0 + 1) * 1.0 + 0.5 * (9 + 16)) + r = 13.0 # sum(0.5 * (0 + 1) * 1.0 + 0.5 * (9 + 16)) assert_almost_equal(trapz(ym, x), r) xm = np.ma.array(x, mask=mask) @@ -842,6 +867,7 @@ class TestSinc(TestCase): assert_array_equal(y1, y2) assert_array_equal(y1, y3) + class TestHistogram(TestCase): def setUp(self): pass @@ -863,7 +889,7 @@ class TestHistogram(TestCase): def test_one_bin(self): # Ticket 632 hist, edges = histogram([1, 2, 3, 4], [1, 2]) - assert_array_equal(hist, [2,]) + assert_array_equal(hist, [2, ]) assert_array_equal(edges, [1, 2]) assert_raises(ValueError, histogram, [1, 2], bins=0) h, e = histogram([1, 2], bins=1) @@ -909,7 +935,8 @@ class TestHistogram(TestCase): # Taken from a bug report from N. Becker on the numpy-discussion # mailing list Aug. 6, 2010. - counts, dmy = np.histogram([1, 2, 3, 4], [0.5, 1.5, np.inf], density=True) + counts, dmy = np.histogram( + [1, 2, 3, 4], [0.5, 1.5, np.inf], density=True) assert_equal(counts, [.25, 0]) def test_outliers(self): @@ -970,12 +997,14 @@ class TestHistogram(TestCase): # Check with integer weights wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1]) assert_array_equal(wa, [4, 5, 0, 1]) - wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1], normed=True) + wa, wb = histogram( + [1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1], normed=True) assert_array_almost_equal(wa, np.array([4, 5, 0, 1]) / 10. / 3. * 4) # Check weights with non-uniform bin widths - a, b = histogram(np.arange(9), [0, 1, 3, 6, 10], \ - weights=[2, 1, 1, 1, 1, 1, 1, 1, 1], density=True) + a, b = histogram( + np.arange(9), [0, 1, 3, 6, 10], + weights=[2, 1, 1, 1, 1, 1, 1, 1, 1], density=True) assert_almost_equal(a, [.2, .1, .1, .075]) def test_empty(self): @@ -986,30 +1015,35 @@ class TestHistogram(TestCase): class TestHistogramdd(TestCase): def test_simple(self): - x = np.array([[-.5, .5, 1.5], [-.5, 1.5, 2.5], [-.5, 2.5, .5], \ - [.5, .5, 1.5], [.5, 1.5, 2.5], [.5, 2.5, 2.5]]) - H, edges = histogramdd(x, (2, 3, 3), range=[[-1, 1], [0, 3], [0, 3]]) - answer = np.array([[[0, 1, 0], [0, 0, 1], [1, 0, 0]], [[0, 1, 0], [0, 0, 1], - [0, 0, 1]]]) + x = np.array([[-.5, .5, 1.5], [-.5, 1.5, 2.5], [-.5, 2.5, .5], + [.5, .5, 1.5], [.5, 1.5, 2.5], [.5, 2.5, 2.5]]) + H, edges = histogramdd(x, (2, 3, 3), + range=[[-1, 1], [0, 3], [0, 3]]) + answer = np.array([[[0, 1, 0], [0, 0, 1], [1, 0, 0]], + [[0, 1, 0], [0, 0, 1], [0, 0, 1]]]) assert_array_equal(H, answer) + # Check normalization ed = [[-2, 0, 2], [0, 1, 2, 3], [0, 1, 2, 3]] H, edges = histogramdd(x, bins=ed, normed=True) assert_(np.all(H == answer / 12.)) + # Check that H has the correct shape. - H, edges = histogramdd(x, (2, 3, 4), range=[[-1, 1], [0, 3], [0, 4]], - normed=True) - answer = np.array([[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]], [[0, 1, 0, 0], - [0, 0, 1, 0], [0, 0, 1, 0]]]) + H, edges = histogramdd(x, (2, 3, 4), + range=[[-1, 1], [0, 3], [0, 4]], + normed=True) + answer = np.array([[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]], + [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]]]) assert_array_almost_equal(H, answer / 6., 4) # Check that a sequence of arrays is accepted and H has the correct # shape. z = [np.squeeze(y) for y in split(x, 3, axis=1)] - H, edges = histogramdd(z, bins=(4, 3, 2), range=[[-2, 2], [0, 3], [0, 2]]) + H, edges = histogramdd( + z, bins=(4, 3, 2), range=[[-2, 2], [0, 3], [0, 2]]) answer = np.array([[[0, 0], [0, 0], [0, 0]], - [[0, 1], [0, 0], [1, 0]], - [[0, 1], [0, 0], [0, 0]], - [[0, 0], [0, 0], [0, 0]]]) + [[0, 1], [0, 0], [1, 0]], + [[0, 1], [0, 0], [0, 0]], + [[0, 0], [0, 0], [0, 0]]]) assert_array_equal(H, answer) Z = np.zeros((5, 5, 5)) @@ -1020,7 +1054,7 @@ class TestHistogramdd(TestCase): def test_shape_3d(self): # All possible permutations for bins of different lengths in 3D. bins = ((5, 4, 6), (6, 4, 5), (5, 6, 4), (4, 6, 5), (6, 5, 4), - (4, 5, 6)) + (4, 5, 6)) r = rand(10, 3) for b in bins: H, edges = histogramdd(r, b) @@ -1029,11 +1063,11 @@ class TestHistogramdd(TestCase): def test_shape_4d(self): # All possible permutations for bins of different lengths in 4D. bins = ((7, 4, 5, 6), (4, 5, 7, 6), (5, 6, 4, 7), (7, 6, 5, 4), - (5, 7, 6, 4), (4, 6, 7, 5), (6, 5, 7, 4), (7, 5, 4, 6), - (7, 4, 6, 5), (6, 4, 7, 5), (6, 7, 5, 4), (4, 6, 5, 7), - (4, 7, 5, 6), (5, 4, 6, 7), (5, 7, 4, 6), (6, 7, 4, 5), - (6, 5, 4, 7), (4, 7, 6, 5), (4, 5, 6, 7), (7, 6, 4, 5), - (5, 4, 7, 6), (5, 6, 7, 4), (6, 4, 5, 7), (7, 5, 6, 4)) + (5, 7, 6, 4), (4, 6, 7, 5), (6, 5, 7, 4), (7, 5, 4, 6), + (7, 4, 6, 5), (6, 4, 7, 5), (6, 7, 5, 4), (4, 6, 5, 7), + (4, 7, 5, 6), (5, 4, 6, 7), (5, 7, 4, 6), (6, 7, 4, 5), + (6, 5, 4, 7), (4, 7, 6, 5), (4, 5, 6, 7), (7, 6, 4, 5), + (5, 4, 7, 6), (5, 6, 7, 4), (6, 4, 5, 7), (7, 5, 6, 4)) r = rand(10, 4) for b in bins: @@ -1058,19 +1092,20 @@ class TestHistogramdd(TestCase): def test_empty(self): a, b = histogramdd([[], []], bins=([0, 1], [0, 1])) - assert_array_max_ulp(a, np.array([[ 0.]])) + assert_array_max_ulp(a, np.array([[0.]])) a, b = np.histogramdd([[], [], []], bins=2) assert_array_max_ulp(a, np.zeros((2, 2, 2))) - def test_bins_errors(self): """There are two ways to specify bins. Check for the right errors when mixing those.""" x = np.arange(8).reshape(2, 4) assert_raises(ValueError, np.histogramdd, x, bins=[-1, 2, 4, 5]) assert_raises(ValueError, np.histogramdd, x, bins=[1, 0.99, 1, 1]) - assert_raises(ValueError, np.histogramdd, x, bins=[1, 1, 1, [1, 2, 2, 3]]) - assert_raises(ValueError, np.histogramdd, x, bins=[1, 1, 1, [1, 2, 3, -3]]) + assert_raises( + ValueError, np.histogramdd, x, bins=[1, 1, 1, [1, 2, 2, 3]]) + assert_raises( + ValueError, np.histogramdd, x, bins=[1, 1, 1, [1, 2, 3, -3]]) assert_(np.histogramdd(x, bins=[1, 1, 1, [1, 2, 3, 4]])) def test_inf_edges(self): @@ -1114,27 +1149,25 @@ class TestCheckFinite(TestCase): class TestCorrCoef(TestCase): - A = np.array([[ 0.15391142, 0.18045767, 0.14197213], - [ 0.70461506, 0.96474128, 0.27906989], - [ 0.9297531, 0.32296769, 0.19267156]]) - B = np.array([[ 0.10377691, 0.5417086, 0.49807457], - [ 0.82872117, 0.77801674, 0.39226705], - [ 0.9314666, 0.66800209, 0.03538394]]) - res1 = np.array([[ 1., 0.9379533, -0.04931983], - [ 0.9379533, 1., 0.30007991], - [-0.04931983, 0.30007991, 1. ]]) - res2 = np.array([[ 1., 0.9379533, -0.04931983, - 0.30151751, 0.66318558, 0.51532523], - [ 0.9379533, 1., 0.30007991, - - 0.04781421, 0.88157256, 0.78052386], - [-0.04931983, 0.30007991, 1., - - 0.96717111, 0.71483595, 0.83053601], - [ 0.30151751, -0.04781421, -0.96717111, - 1., -0.51366032, -0.66173113], - [ 0.66318558, 0.88157256, 0.71483595, - - 0.51366032, 1., 0.98317823], - [ 0.51532523, 0.78052386, 0.83053601, - - 0.66173113, 0.98317823, 1. ]]) + A = np.array( + [[0.15391142, 0.18045767, 0.14197213], + [0.70461506, 0.96474128, 0.27906989], + [0.9297531, 0.32296769, 0.19267156]]) + B = np.array( + [[0.10377691, 0.5417086, 0.49807457], + [0.82872117, 0.77801674, 0.39226705], + [0.9314666, 0.66800209, 0.03538394]]) + res1 = np.array( + [[1., 0.9379533, -0.04931983], + [0.9379533, 1., 0.30007991], + [-0.04931983, 0.30007991, 1.]]) + res2 = np.array( + [[1., 0.9379533, -0.04931983, 0.30151751, 0.66318558, 0.51532523], + [0.9379533, 1., 0.30007991, -0.04781421, 0.88157256, 0.78052386], + [-0.04931983, 0.30007991, 1., -0.96717111, 0.71483595, 0.83053601], + [0.30151751, -0.04781421, -0.96717111, 1., -0.51366032, -0.66173113], + [0.66318558, 0.88157256, 0.71483595, -0.51366032, 1., 0.98317823], + [0.51532523, 0.78052386, 0.83053601, -0.66173113, 0.98317823, 1.]]) def test_simple(self): assert_almost_equal(corrcoef(self.A), self.res1) @@ -1152,7 +1185,7 @@ class TestCorrCoef(TestCase): class TestCov(TestCase): def test_basic(self): x = np.array([[0, 2], [1, 1], [2, 0]]).T - assert_allclose(np.cov(x), np.array([[ 1., -1.], [-1., 1.]])) + assert_allclose(np.cov(x), np.array([[1., -1.], [-1., 1.]])) def test_empty(self): assert_equal(cov(np.array([])).size, 0) @@ -1161,34 +1194,42 @@ class TestCov(TestCase): class Test_I0(TestCase): def test_simple(self): - assert_almost_equal(i0(0.5), np.array(1.0634833707413234)) - A = np.array([ 0.49842636, 0.6969809, 0.22011976, 0.0155549]) - assert_almost_equal(i0(A), - np.array([ 1.06307822, 1.12518299, 1.01214991, 1.00006049])) - B = np.array([[ 0.827002, 0.99959078], - [ 0.89694769, 0.39298162], - [ 0.37954418, 0.05206293], - [ 0.36465447, 0.72446427], - [ 0.48164949, 0.50324519]]) - assert_almost_equal(i0(B), - np.array([[ 1.17843223, 1.26583466], - [ 1.21147086, 1.0389829 ], - [ 1.03633899, 1.00067775], - [ 1.03352052, 1.13557954], - [ 1.0588429, 1.06432317]])) + assert_almost_equal( + i0(0.5), + np.array(1.0634833707413234)) + + A = np.array([0.49842636, 0.6969809, 0.22011976, 0.0155549]) + assert_almost_equal( + i0(A), + np.array([1.06307822, 1.12518299, 1.01214991, 1.00006049])) + + B = np.array([[0.827002, 0.99959078], + [0.89694769, 0.39298162], + [0.37954418, 0.05206293], + [0.36465447, 0.72446427], + [0.48164949, 0.50324519]]) + assert_almost_equal( + i0(B), + np.array([[1.17843223, 1.26583466], + [1.21147086, 1.03898290], + [1.03633899, 1.00067775], + [1.03352052, 1.13557954], + [1.05884290, 1.06432317]])) class TestKaiser(TestCase): def test_simple(self): - assert_almost_equal(kaiser(0, 1.0), np.array([])) assert_(np.isfinite(kaiser(1, 1.0))) - assert_almost_equal(kaiser(2, 1.0), np.array([ 0.78984831, 0.78984831])) + assert_almost_equal(kaiser(0, 1.0), + np.array([])) + assert_almost_equal(kaiser(2, 1.0), + np.array([0.78984831, 0.78984831])) assert_almost_equal(kaiser(5, 1.0), - np.array([ 0.78984831, 0.94503323, 1., - 0.94503323, 0.78984831])) + np.array([0.78984831, 0.94503323, 1., + 0.94503323, 0.78984831])) assert_almost_equal(kaiser(5, 1.56789), - np.array([ 0.58285404, 0.88409679, 1., - 0.88409679, 0.58285404])) + np.array([0.58285404, 0.88409679, 1., + 0.88409679, 0.58285404])) def test_int_beta(self): kaiser(3, 4) @@ -1196,26 +1237,27 @@ class TestKaiser(TestCase): class TestMsort(TestCase): def test_simple(self): - A = np.array([[ 0.44567325, 0.79115165, 0.5490053 ], - [ 0.36844147, 0.37325583, 0.96098397], - [ 0.64864341, 0.52929049, 0.39172155]]) - assert_almost_equal(msort(A), - np.array([[ 0.36844147, 0.37325583, 0.39172155], - [ 0.44567325, 0.52929049, 0.5490053 ], - [ 0.64864341, 0.79115165, 0.96098397]])) + A = np.array([[0.44567325, 0.79115165, 0.54900530], + [0.36844147, 0.37325583, 0.96098397], + [0.64864341, 0.52929049, 0.39172155]]) + assert_almost_equal( + msort(A), + np.array([[0.36844147, 0.37325583, 0.39172155], + [0.44567325, 0.52929049, 0.54900530], + [0.64864341, 0.79115165, 0.96098397]])) class TestMeshgrid(TestCase): def test_simple(self): [X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7]) assert_(np.all(X == np.array([[1, 2, 3], - [1, 2, 3], - [1, 2, 3], - [1, 2, 3]]))) + [1, 2, 3], + [1, 2, 3], + [1, 2, 3]]))) assert_(np.all(Y == np.array([[4, 4, 4], - [5, 5, 5], - [6, 6, 6], - [7, 7, 7]]))) + [5, 5, 5], + [6, 6, 6], + [7, 7, 7]]))) def test_single_input(self): assert_raises(ValueError, meshgrid, np.arange(5)) @@ -1268,7 +1310,6 @@ class TestPiecewise(TestCase): x = piecewise([0, 0], [np.array([1, 0])], [1]) assert_array_equal(x, [1, 0]) - x = piecewise([0, 0], [[False, True]], [lambda x:-1]) assert_array_equal(x, [0, -1]) @@ -1390,6 +1431,7 @@ def compare_results(res, desired): def test_percentile_list(): assert_equal(np.percentile([1, 2, 3], 0), 1) + def test_percentile_out(): x = np.array([1, 2, 3]) y = np.zeros((3,)) @@ -1459,9 +1501,12 @@ class TestMedian(TestCase): assert_allclose(np.median(a2.copy(), overwrite_input=True), 2.5) assert_allclose(np.median(a2.copy(), overwrite_input=True, axis=0), [1.5, 2.5, 3.5]) - assert_allclose(np.median(a2.copy(), overwrite_input=True, axis=1), [1, 4]) - assert_allclose(np.median(a2.copy(), overwrite_input=True, axis=None), 2.5) - assert_allclose(np.median(a3.copy(), overwrite_input=True, axis=0), [3, 4]) + assert_allclose( + np.median(a2.copy(), overwrite_input=True, axis=1), [1, 4]) + assert_allclose( + np.median(a2.copy(), overwrite_input=True, axis=None), 2.5) + assert_allclose( + np.median(a3.copy(), overwrite_input=True, axis=0), [3, 4]) assert_allclose(np.median(a3.T.copy(), overwrite_input=True, axis=1), [3, 4]) diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index 9002331ce..b4152fafa 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -2,9 +2,10 @@ from __future__ import division, absolute_import, print_function from numpy.testing import * import numpy as np -from numpy import ( array, ones, r_, mgrid, unravel_index, zeros, where, - ndenumerate, fill_diagonal, diag_indices, - diag_indices_from, s_, index_exp, ndindex ) +from numpy import (array, ones, r_, mgrid, unravel_index, zeros, where, + ndenumerate, fill_diagonal, diag_indices, + diag_indices_from, s_, index_exp, ndindex) + class TestRavelUnravelIndex(TestCase): def test_basic(self): @@ -22,53 +23,65 @@ class TestRavelUnravelIndex(TestCase): assert_raises(TypeError, np.ravel_multi_index, (0.1, 0.), (2, 2)) assert_equal(np.unravel_index((2*3 + 1)*6 + 4, (4, 3, 6)), [2, 1, 4]) - assert_equal(np.ravel_multi_index([2, 1, 4], (4, 3, 6)), (2*3 + 1)*6 + 4) + assert_equal( + np.ravel_multi_index([2, 1, 4], (4, 3, 6)), (2*3 + 1)*6 + 4) arr = np.array([[3, 6, 6], [4, 5, 1]]) assert_equal(np.ravel_multi_index(arr, (7, 6)), [22, 41, 37]) - assert_equal(np.ravel_multi_index(arr, (7, 6), order='F'), [31, 41, 13]) - assert_equal(np.ravel_multi_index(arr, (4, 6), mode='clip'), [22, 23, 19]) + assert_equal( + np.ravel_multi_index(arr, (7, 6), order='F'), [31, 41, 13]) + assert_equal( + np.ravel_multi_index(arr, (4, 6), mode='clip'), [22, 23, 19]) assert_equal(np.ravel_multi_index(arr, (4, 4), mode=('clip', 'wrap')), - [12, 13, 13]) + [12, 13, 13]) assert_equal(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9)), 1621) assert_equal(np.unravel_index(np.array([22, 41, 37]), (7, 6)), - [[3, 6, 6], [4, 5, 1]]) - assert_equal(np.unravel_index(np.array([31, 41, 13]), (7, 6), order='F'), - [[3, 6, 6], [4, 5, 1]]) + [[3, 6, 6], [4, 5, 1]]) + assert_equal( + np.unravel_index(np.array([31, 41, 13]), (7, 6), order='F'), + [[3, 6, 6], [4, 5, 1]]) assert_equal(np.unravel_index(1621, (6, 7, 8, 9)), [3, 1, 4, 1]) def test_dtypes(self): # Test with different data types for dtype in [np.int16, np.uint16, np.int32, np.uint32, np.int64, np.uint64]: - coords = np.array([[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0]], dtype=dtype) + coords = np.array( + [[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0]], dtype=dtype) shape = (5, 8) uncoords = 8*coords[0]+coords[1] assert_equal(np.ravel_multi_index(coords, shape), uncoords) assert_equal(coords, np.unravel_index(uncoords, shape)) uncoords = coords[0]+5*coords[1] - assert_equal(np.ravel_multi_index(coords, shape, order='F'), uncoords) + assert_equal( + np.ravel_multi_index(coords, shape, order='F'), uncoords) assert_equal(coords, np.unravel_index(uncoords, shape, order='F')) - coords = np.array([[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0], [1, 3, 1, 0, 9, 5]], - dtype=dtype) + coords = np.array( + [[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0], [1, 3, 1, 0, 9, 5]], + dtype=dtype) shape = (5, 8, 10) uncoords = 10*(8*coords[0]+coords[1])+coords[2] assert_equal(np.ravel_multi_index(coords, shape), uncoords) assert_equal(coords, np.unravel_index(uncoords, shape)) uncoords = coords[0]+5*(coords[1]+8*coords[2]) - assert_equal(np.ravel_multi_index(coords, shape, order='F'), uncoords) + assert_equal( + np.ravel_multi_index(coords, shape, order='F'), uncoords) assert_equal(coords, np.unravel_index(uncoords, shape, order='F')) def test_clipmodes(self): # Test clipmodes - assert_equal(np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12), mode='wrap'), - np.ravel_multi_index([1, 1, 6, 2], (4, 3, 7, 12))) + assert_equal( + np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12), mode='wrap'), + np.ravel_multi_index([1, 1, 6, 2], (4, 3, 7, 12))) assert_equal(np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12), - mode=('wrap', 'raise', 'clip', 'raise')), + mode=( + 'wrap', 'raise', 'clip', 'raise')), np.ravel_multi_index([1, 1, 0, 2], (4, 3, 7, 12))) - assert_raises(ValueError, np.ravel_multi_index, [5, 1, -1, 2], (4, 3, 7, 12)) + assert_raises( + ValueError, np.ravel_multi_index, [5, 1, -1, 2], (4, 3, 7, 12)) + class TestGrid(TestCase): def test_basic(self): @@ -93,12 +106,12 @@ class TestGrid(TestCase): d = mgrid[-1:1:0.1, -2:2:0.2] assert_(c.shape == (2, 10, 10)) assert_(d.shape == (2, 20, 20)) - assert_array_equal(c[0][0,:], -ones(10, 'd')) + assert_array_equal(c[0][0, :], -ones(10, 'd')) assert_array_equal(c[1][:, 0], -2*ones(10, 'd')) - assert_array_almost_equal(c[0][-1,:], ones(10, 'd'), 11) + assert_array_almost_equal(c[0][-1, :], ones(10, 'd'), 11) assert_array_almost_equal(c[1][:, -1], 2*ones(10, 'd'), 11) - assert_array_almost_equal(d[0, 1,:]-d[0, 0,:], 0.1*ones(20, 'd'), 11) - assert_array_almost_equal(d[1,:, 1]-d[1,:, 0], 0.2*ones(20, 'd'), 11) + assert_array_almost_equal(d[0, 1, :]-d[0, 0, :], 0.1*ones(20, 'd'), 11) + assert_array_almost_equal(d[1, :, 1]-d[1, :, 0], 0.2*ones(20, 'd'), 11) class TestConcatenator(TestCase): @@ -125,8 +138,8 @@ class TestConcatenator(TestCase): assert_array_equal(d[:, 5:], c) d = r_[b, c] assert_(d.shape == (10, 5)) - assert_array_equal(d[:5,:], b) - assert_array_equal(d[5:,:], c) + assert_array_equal(d[:5, :], b) + assert_array_equal(d[5:, :], c) class TestNdenumerate(TestCase): @@ -149,10 +162,12 @@ class TestIndexExpression(TestCase): assert_equal(a[:, :3, [1, 2]], a[index_exp[:, :3, [1, 2]]]) assert_equal(a[:, :3, [1, 2]], a[s_[:, :3, [1, 2]]]) + def test_c_(): a = np.c_[np.array([[1, 2, 3]]), 0, 0, np.array([[4, 5, 6]])] assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]]) + def test_fill_diagonal(): a = zeros((3, 3), int) fill_diagonal(a, 5) @@ -208,16 +223,16 @@ def test_fill_diagonal(): def test_diag_indices(): di = diag_indices(4) - a = array([[1, 2, 3, 4], - [5, 6, 7, 8], - [9, 10, 11, 12], + a = array([[1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], [13, 14, 15, 16]]) a[di] = 100 yield (assert_array_equal, a, - array([[100, 2, 3, 4], - [ 5, 100, 7, 8], - [ 9, 10, 100, 12], - [ 13, 14, 15, 100]])) + array([[100, 2, 3, 4], + [5, 100, 7, 8], + [9, 10, 100, 12], + [13, 14, 15, 100]])) # Now, we create indices to manipulate a 3-d array: d3 = diag_indices(2, 3) @@ -230,7 +245,8 @@ def test_diag_indices(): [0, 0]], [[0, 0], - [0, 1]]]) ) + [0, 1]]])) + def test_diag_indices_from(): x = np.random.random((4, 4)) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index af06cd45e..9a119e79a 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -13,12 +13,12 @@ from datetime import datetime import numpy as np import numpy.ma as ma -from numpy.lib._iotools import ConverterError, ConverterLockError, \ - ConversionWarning +from numpy.lib._iotools import (ConverterError, ConverterLockError, + ConversionWarning) from numpy.compat import asbytes, asbytes_nested, bytes, asstr from nose import SkipTest from numpy.ma.testutils import (TestCase, assert_equal, assert_array_equal, - assert_raises, run_module_suite) + assert_raises, run_module_suite) from numpy.testing import assert_warns, assert_, build_err_msg @@ -55,6 +55,7 @@ def strptime(s, fmt=None): else: return datetime(*time.strptime(s, fmt)[:3]) + class RoundtripTest(object): def roundtrip(self, save_func, *args, **kwargs): """ @@ -129,11 +130,13 @@ class RoundtripTest(object): a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) self.roundtrip(a) + class TestSaveLoad(RoundtripTest, TestCase): def roundtrip(self, *args, **kwargs): RoundtripTest.roundtrip(self, np.save, *args, **kwargs) assert_equal(self.arr[0], self.arr_reloaded) + class TestSavezLoad(RoundtripTest, TestCase): def roundtrip(self, *args, **kwargs): RoundtripTest.roundtrip(self, np.savez, *args, **kwargs) @@ -210,7 +213,8 @@ class TestSavezLoad(RoundtripTest, TestCase): fp.seek(0) assert_(not fp.closed) _ = np.load(fp)['data'] - assert_(not fp.closed) # must not get closed by .load(opened fp) + assert_(not fp.closed) + # must not get closed by .load(opened fp) fp.seek(0) assert_(not fp.closed) @@ -270,8 +274,8 @@ class TestSaveTxt(TestCase): np.savetxt(c, a, fmt=fmt) c.seek(0) assert_equal(c.readlines(), - [asbytes((fmt + ' ' + fmt + '\n') % (1, 2)), - asbytes((fmt + ' ' + fmt + '\n') % (3, 4))]) + [asbytes((fmt + ' ' + fmt + '\n') % (1, 2)), + asbytes((fmt + ' ' + fmt + '\n') % (3, 4))]) a = np.array([[1, 2], [3, 4]], int) c = BytesIO() @@ -334,29 +338,29 @@ class TestSaveTxt(TestCase): np.savetxt(c, a, fmt='%1d', header=test_header_footer) c.seek(0) assert_equal(c.read(), - asbytes('# ' + test_header_footer + '\n1 2\n3 4\n')) + asbytes('# ' + test_header_footer + '\n1 2\n3 4\n')) # Test the footer keyword argument c = BytesIO() np.savetxt(c, a, fmt='%1d', footer=test_header_footer) c.seek(0) assert_equal(c.read(), - asbytes('1 2\n3 4\n# ' + test_header_footer + '\n')) + asbytes('1 2\n3 4\n# ' + test_header_footer + '\n')) # Test the commentstr keyword argument used on the header c = BytesIO() commentstr = '% ' np.savetxt(c, a, fmt='%1d', - header=test_header_footer, comments=commentstr) + header=test_header_footer, comments=commentstr) c.seek(0) assert_equal(c.read(), - asbytes(commentstr + test_header_footer + '\n' + '1 2\n3 4\n')) + asbytes(commentstr + test_header_footer + '\n' + '1 2\n3 4\n')) # Test the commentstr keyword argument used on the footer c = BytesIO() commentstr = '% ' np.savetxt(c, a, fmt='%1d', - footer=test_header_footer, comments=commentstr) + footer=test_header_footer, comments=commentstr) c.seek(0) assert_equal(c.read(), - asbytes('1 2\n3 4\n' + commentstr + test_header_footer + '\n')) + asbytes('1 2\n3 4\n' + commentstr + test_header_footer + '\n')) def test_file_roundtrip(self): f, name = mkstemp() @@ -376,30 +380,36 @@ class TestSaveTxt(TestCase): re = np.pi im = np.e a[:] = re + 1.0j * im + # One format only c = BytesIO() np.savetxt(c, a, fmt=' %+.3e') c.seek(0) lines = c.readlines() - _assert_floatstr_lines_equal(lines, - [b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n', - b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n']) + assert_equal( + lines, + [b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n', + b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n']) + # One format for each real and imaginary part c = BytesIO() np.savetxt(c, a, fmt=' %+.3e' * 2 * ncols) c.seek(0) lines = c.readlines() - _assert_floatstr_lines_equal(lines, - [b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n', - b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n']) + assert_equal( + lines, + [b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n', + b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n']) + # One format for each complex number c = BytesIO() np.savetxt(c, a, fmt=['(%.3e%+.3ej)'] * ncols) c.seek(0) lines = c.readlines() - _assert_floatstr_lines_equal(lines, - [b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n', - b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n']) + assert_equal( + lines, + [b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n', + b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n']) def test_custom_writer(self): @@ -414,26 +424,6 @@ class TestSaveTxt(TestCase): assert_array_equal(a, b) -def _assert_floatstr_lines_equal(actual_lines, expected_lines): - """A string comparison function that also works on Windows + Python 2.5. - - This is necessary because Python 2.5 on Windows inserts an extra 0 in - the exponent of the string representation of floating point numbers. - - Only used in TestSaveTxt.test_complex_arrays, no attempt made to make this - more generic. - - Once Python 2.5 compatibility is dropped, simply use `assert_equal` instead - of this function. - """ - for actual, expected in zip(actual_lines, expected_lines): - if actual != expected: - expected_win25 = expected.replace("e+00", "e+000") - if actual != expected_win25: - msg = build_err_msg([actual, expected], '', verbose=True) - raise AssertionError(msg) - - class TestLoadTxt(TestCase): def test_record(self): c = TextIO() @@ -447,8 +437,7 @@ class TestLoadTxt(TestCase): d.write('M 64.0 75.0\nF 25.0 60.0') d.seek(0) mydescriptor = {'names': ('gender', 'age', 'weight'), - 'formats': ('S1', - 'i4', 'f4')} + 'formats': ('S1', 'i4', 'f4')} b = np.array([('M', 64.0, 75.0), ('F', 25.0, 60.0)], dtype=mydescriptor) y = np.loadtxt(d, dtype=mydescriptor) @@ -459,7 +448,7 @@ class TestLoadTxt(TestCase): c.write('1 2\n3 4') c.seek(0) - x = np.loadtxt(c, dtype=int) + x = np.loadtxt(c, dtype=np.int) a = np.array([[1, 2], [3, 4]], int) assert_array_equal(x, a) @@ -487,8 +476,8 @@ class TestLoadTxt(TestCase): c = TextIO() c.write('1,2,3,,5\n') c.seek(0) - x = np.loadtxt(c, dtype=int, delimiter=',', \ - converters={3:lambda s: int(s or - 999)}) + x = np.loadtxt(c, dtype=int, delimiter=',', + converters={3: lambda s: int(s or - 999)}) a = np.array([1, 2, 3, -999, 5], int) assert_array_equal(x, a) @@ -496,9 +485,9 @@ class TestLoadTxt(TestCase): c = TextIO() c.write('1,2,3,,5\n6,7,8,9,10\n') c.seek(0) - x = np.loadtxt(c, dtype=int, delimiter=',', \ - converters={3:lambda s: int(s or - 999)}, \ - usecols=(1, 3,)) + x = np.loadtxt(c, dtype=int, delimiter=',', + converters={3: lambda s: int(s or - 999)}, + usecols=(1, 3,)) a = np.array([[2, -999], [7, 9]], int) assert_array_equal(x, a) @@ -506,8 +495,8 @@ class TestLoadTxt(TestCase): c = TextIO() c.write('# comment\n1,2,3,5\n') c.seek(0) - x = np.loadtxt(c, dtype=int, delimiter=',', \ - comments='#') + x = np.loadtxt(c, dtype=int, delimiter=',', + comments='#') a = np.array([1, 2, 3, 5], int) assert_array_equal(x, a) @@ -515,16 +504,16 @@ class TestLoadTxt(TestCase): c = TextIO() c.write('comment\n1,2,3,5\n') c.seek(0) - x = np.loadtxt(c, dtype=int, delimiter=',', \ - skiprows=1) + x = np.loadtxt(c, dtype=int, delimiter=',', + skiprows=1) a = np.array([1, 2, 3, 5], int) assert_array_equal(x, a) c = TextIO() c.write('# comment\n1,2,3,5\n') c.seek(0) - x = np.loadtxt(c, dtype=int, delimiter=',', \ - skiprows=1) + x = np.loadtxt(c, dtype=int, delimiter=',', + skiprows=1) a = np.array([1, 2, 3, 5], int) assert_array_equal(x, a) @@ -583,14 +572,14 @@ class TestLoadTxt(TestCase): ('block', int, (2, 2, 3))]) x = np.loadtxt(c, dtype=dt) a = np.array([('aaaa', 1.0, 8.0, - [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])], - dtype=dt) + [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])], + dtype=dt) assert_array_equal(x, a) def test_empty_file(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore", - message="loadtxt: Empty input file:") + message="loadtxt: Empty input file:") c = TextIO() x = np.loadtxt(c) assert_equal(x.shape, (0,)) @@ -598,7 +587,6 @@ class TestLoadTxt(TestCase): assert_equal(x.shape, (0,)) assert_(x.dtype == np.int64) - def test_unused_converter(self): c = TextIO() c.writelines(['1 21\n', '3 42\n']) @@ -622,9 +610,10 @@ class TestLoadTxt(TestCase): func = lambda s: strptime(s.strip(), "%Y-%m-%d") converters = {1: func} test = np.loadtxt(TextIO(data), delimiter=";", dtype=ndtype, - converters=converters) - control = np.array([(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))], - dtype=ndtype) + converters=converters) + control = np.array( + [(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))], + dtype=ndtype) assert_equal(test, control) def test_uint64_type(self): @@ -658,22 +647,22 @@ class TestLoadTxt(TestCase): c = TextIO() c.write('1 \t2 \t3\tstart \n4\t5\t6\t \n7\t8\t9.5\t') c.seek(0) - dt = { 'names': ('x', 'y', 'z', 'comment'), - 'formats': ('<i4', '<i4', '<f4', '|S8')} + dt = {'names': ('x', 'y', 'z', 'comment'), + 'formats': ('<i4', '<i4', '<f4', '|S8')} x = np.loadtxt(c, dtype=dt, delimiter='\t') a = np.array([b'start ', b' ', b'']) assert_array_equal(x['comment'], a) def test_structure_unpack(self): txt = TextIO("M 21 72\nF 35 58") - dt = { 'names': ('a', 'b', 'c'), 'formats': ('|S1', '<i4', '<f4')} + dt = {'names': ('a', 'b', 'c'), 'formats': ('|S1', '<i4', '<f4')} a, b, c = np.loadtxt(txt, dtype=dt, unpack=True) assert_(a.dtype.str == '|S1') assert_(b.dtype.str == '<i4') assert_(c.dtype.str == '<f4') assert_array_equal(a, np.array([b'M', b'F'])) assert_array_equal(b, np.array([21, 35])) - assert_array_equal(c, np.array([ 72., 58.])) + assert_array_equal(c, np.array([72., 58.])) def test_ndmin_keyword(self): c = TextIO() @@ -714,7 +703,7 @@ class TestLoadTxt(TestCase): # Test ndmin kw with empty file. with warnings.catch_warnings(): warnings.filterwarnings("ignore", - message="loadtxt: Empty input file:") + message="loadtxt: Empty input file:") f = TextIO() assert_(np.loadtxt(f, ndmin=2).shape == (0, 1,)) assert_(np.loadtxt(f, ndmin=1).shape == (0,)) @@ -727,6 +716,7 @@ class TestLoadTxt(TestCase): res = np.loadtxt(count()) assert_array_equal(res, np.arange(10)) + class Testfromregex(TestCase): # np.fromregex expects files opened in binary mode. def test_record(self): @@ -853,16 +843,19 @@ class TestFromTxt(TestCase): TextIO(basestr), skip_footer=1) # except ValueError: # pass - a = np.genfromtxt(TextIO(basestr), skip_footer=1, invalid_raise=False) + a = np.genfromtxt( + TextIO(basestr), skip_footer=1, invalid_raise=False) assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])) # a = np.genfromtxt(TextIO(basestr), skip_footer=3) assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])) # basestr = '1 1\n2 \n3 3\n4 4\n5 \n6 6\n7 7\n' - a = np.genfromtxt(TextIO(basestr), skip_footer=1, invalid_raise=False) + a = np.genfromtxt( + TextIO(basestr), skip_footer=1, invalid_raise=False) assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]])) - a = np.genfromtxt(TextIO(basestr), skip_footer=3, invalid_raise=False) + a = np.genfromtxt( + TextIO(basestr), skip_footer=3, invalid_raise=False) assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]])) def test_header(self): @@ -889,7 +882,6 @@ class TestFromTxt(TestCase): for (i, ctrl) in enumerate(control): assert_equal(test['f%i' % i], ctrl) - def test_auto_dtype_uniform(self): "Tests whether the output dtype can be uniformized" data = TextIO('1 2 3 4\n5 6 7 8\n') @@ -897,7 +889,6 @@ class TestFromTxt(TestCase): control = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) assert_equal(test, control) - def test_fancy_dtype(self): "Check that a nested dtype isn't MIA" data = TextIO('1,2,3.0\n4,5,6.0\n') @@ -906,7 +897,6 @@ class TestFromTxt(TestCase): control = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype) assert_equal(test, control) - def test_names_overwrite(self): "Test overwriting the names of the dtype" descriptor = {'names': ('g', 'a', 'w'), @@ -919,7 +909,6 @@ class TestFromTxt(TestCase): ('F', 25.0, 60.0)], dtype=descriptor) assert_equal(test, control) - def test_commented_header(self): "Check that names can be retrieved even if the line is commented out." data = TextIO(""" @@ -931,7 +920,7 @@ M 33 21.99 # The # is part of the first name and should be deleted automatically. test = np.genfromtxt(data, names=True, dtype=None) ctrl = np.array([('M', 21, 72.1), ('F', 35, 58.33), ('M', 33, 21.99)], - dtype=[('gender', '|S1'), ('age', int), ('weight', float)]) + dtype=[('gender', '|S1'), ('age', int), ('weight', float)]) assert_equal(test, ctrl) # Ditto, but we should get rid of the first element data = TextIO(b""" @@ -943,7 +932,6 @@ M 33 21.99 test = np.genfromtxt(data, names=True, dtype=None) assert_equal(test, ctrl) - def test_autonames_and_usecols(self): "Tests names and usecols" data = TextIO('A B C D\n aaaa 121 45 9.1') @@ -953,12 +941,11 @@ M 33 21.99 dtype=[('A', '|S4'), ('C', int), ('D', float)]) assert_equal(test, control) - def test_converters_with_usecols(self): "Test the combination user-defined converters and usecol" data = TextIO('1,2,3,,5\n6,7,8,9,10\n') test = np.ndfromtxt(data, dtype=int, delimiter=',', - converters={3:lambda s: int(s or - 999)}, + converters={3: lambda s: int(s or - 999)}, usecols=(1, 3,)) control = np.array([[2, -999], [7, 9]], int) assert_equal(test, control) @@ -967,14 +954,15 @@ M 33 21.99 "Tests names and usecols" data = TextIO('A B C D\n aaaa 121 45 9.1') test = np.ndfromtxt(data, usecols=('A', 'C', 'D'), names=True, - dtype=None, converters={'C':lambda s: 2 * int(s)}) + dtype=None, converters={'C': lambda s: 2 * int(s)}) control = np.array(('aaaa', 90, 9.1), - dtype=[('A', '|S4'), ('C', int), ('D', float)]) + dtype=[('A', '|S4'), ('C', int), ('D', float)]) assert_equal(test, control) def test_converters_cornercases(self): "Test the conversion to datetime." - converter = {'date': lambda s: strptime(s, '%Y-%m-%d %H:%M:%SZ')} + converter = { + 'date': lambda s: strptime(s, '%Y-%m-%d %H:%M:%SZ')} data = TextIO('2009-02-03 12:00:00Z, 72214.0') test = np.ndfromtxt(data, delimiter=',', dtype=None, names=['date', 'stid'], converters=converter) @@ -984,7 +972,8 @@ M 33 21.99 def test_converters_cornercases2(self): "Test the conversion to datetime64." - converter = {'date': lambda s: np.datetime64(strptime(s, '%Y-%m-%d %H:%M:%SZ'))} + converter = { + 'date': lambda s: np.datetime64(strptime(s, '%Y-%m-%d %H:%M:%SZ'))} data = TextIO('2009-02-03 12:00:00Z, 72214.0') test = np.ndfromtxt(data, delimiter=',', dtype=None, names=['date', 'stid'], converters=converter) @@ -1004,42 +993,39 @@ M 33 21.99 converters={1: lambda s: int(s, 16)}) assert_equal(test, [33, 66]) - def test_invalid_converter(self): - strip_rand = lambda x : float((b'r' in x.lower() and x.split()[-1]) or - (b'r' not in x.lower() and x.strip() or 0.0)) - strip_per = lambda x : float((b'%' in x.lower() and x.split()[0]) or - (b'%' not in x.lower() and x.strip() or 0.0)) + strip_rand = lambda x: float((b'r' in x.lower() and x.split()[-1]) or + (b'r' not in x.lower() and x.strip() or 0.0)) + strip_per = lambda x: float((b'%' in x.lower() and x.split()[0]) or + (b'%' not in x.lower() and x.strip() or 0.0)) s = TextIO("D01N01,10/1/2003 ,1 %,R 75,400,600\r\n" - "L24U05,12/5/2003, 2 %,1,300, 150.5\r\n" - "D02N03,10/10/2004,R 1,,7,145.55") - kwargs = dict(converters={2 : strip_per, 3 : strip_rand}, delimiter=",", - dtype=None) + "L24U05,12/5/2003, 2 %,1,300, 150.5\r\n" + "D02N03,10/10/2004,R 1,,7,145.55") + kwargs = dict( + converters={2: strip_per, 3: strip_rand}, delimiter=",", + dtype=None) assert_raises(ConverterError, np.genfromtxt, s, **kwargs) def test_tricky_converter_bug1666(self): "Test some corner case" s = TextIO('q1,2\nq3,4') - cnv = lambda s:float(s[1:]) - test = np.genfromtxt(s, delimiter=',', converters={0:cnv}) + cnv = lambda s: float(s[1:]) + test = np.genfromtxt(s, delimiter=',', converters={0: cnv}) control = np.array([[1., 2.], [3., 4.]]) assert_equal(test, control) - - def test_dtype_with_converters(self): dstr = "2009; 23; 46" test = np.ndfromtxt(TextIO(dstr,), - delimiter=";", dtype=float, converters={0:bytes}) + delimiter=";", dtype=float, converters={0: bytes}) control = np.array([('2009', 23., 46)], dtype=[('f0', '|S4'), ('f1', float), ('f2', float)]) assert_equal(test, control) test = np.ndfromtxt(TextIO(dstr,), - delimiter=";", dtype=float, converters={0:float}) + delimiter=";", dtype=float, converters={0: float}) control = np.array([2009., 23., 46],) assert_equal(test, control) - def test_dtype_with_object(self): "Test using an explicit dtype with an object" from datetime import date @@ -1051,8 +1037,9 @@ M 33 21.99 converters = {1: func} test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype, converters=converters) - control = np.array([(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))], - dtype=ndtype) + control = np.array( + [(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))], + dtype=ndtype) assert_equal(test, control) # ndtype = [('nest', [('idx', int), ('code', np.object)])] @@ -1065,7 +1052,6 @@ M 33 21.99 errmsg = "Nested dtype involving objects should be supported." raise AssertionError(errmsg) - def test_userconverters_with_explicit_dtype(self): "Test user_converters w/ explicit (standard) dtype" data = TextIO('skip,skip,2001-01-01,1.0,skip') @@ -1075,13 +1061,12 @@ M 33 21.99 dtype=[('', '|S10'), ('', float)]) assert_equal(test, control) - def test_spacedelimiter(self): "Test space delimiter" data = TextIO("1 2 3 4 5\n6 7 8 9 10") test = np.ndfromtxt(data) - control = np.array([[ 1., 2., 3., 4., 5.], - [ 6., 7., 8., 9., 10.]]) + control = np.array([[1., 2., 3., 4., 5.], + [6., 7., 8., 9., 10.]]) assert_equal(test, control) def test_integer_delimiter(self): @@ -1091,15 +1076,13 @@ M 33 21.99 control = np.array([[1, 2, 3], [4, 5, 67], [890, 123, 4]]) assert_equal(test, control) - def test_missing(self): data = TextIO('1,2,3,,5\n') - test = np.ndfromtxt(data, dtype=int, delimiter=',', \ - converters={3:lambda s: int(s or - 999)}) + test = np.ndfromtxt(data, dtype=int, delimiter=',', + converters={3: lambda s: int(s or - 999)}) control = np.array([1, 2, 3, -999, 5], int) assert_equal(test, control) - def test_missing_with_tabs(self): "Test w/ a delimiter tab" txt = "1\t2\t3\n\t2\t\n1\t\t3" @@ -1110,7 +1093,6 @@ M 33 21.99 assert_equal(test.data, ctrl_d) assert_equal(test.mask, ctrl_m) - def test_usecols(self): "Test the selection of columns" # Select 1 column @@ -1145,7 +1127,8 @@ M 33 21.99 data = TextIO("JOE 70.1 25.3\nBOB 60.5 27.9") names = ['stid', 'temp'] dtypes = ['S4', 'f8'] - test = np.ndfromtxt(data, usecols=(0, 2), dtype=list(zip(names, dtypes))) + test = np.ndfromtxt( + data, usecols=(0, 2), dtype=list(zip(names, dtypes))) assert_equal(test['stid'], [b"JOE", b"BOB"]) assert_equal(test['temp'], [25.3, 27.9]) @@ -1182,7 +1165,6 @@ M 33 21.99 control = ma.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype) assert_equal(test, control) - def test_shaped_dtype(self): c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6") dt = np.dtype([('name', 'S4'), ('x', float), ('y', float), @@ -1210,7 +1192,6 @@ M 33 21.99 assert_equal(test, control) assert_equal(test.mask, control.mask) - def test_user_missing_values(self): data = "A, B, C\n0, 0., 0j\n1, N/A, 1j\n-9, 2.2, N/A\n3, -99, 3j" basekwargs = dict(dtype=None, delimiter=",", names=True,) @@ -1220,26 +1201,26 @@ M 33 21.99 **basekwargs) control = ma.array([(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j), (3, -99, 3j)], - mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)], - dtype=mdtype) + mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)], + dtype=mdtype) assert_equal(test, control) # basekwargs['dtype'] = mdtype test = np.mafromtxt(TextIO(data), - missing_values={0:-9, 1:-99, 2:-999j}, **basekwargs) + missing_values={0: -9, 1: -99, 2: -999j}, **basekwargs) control = ma.array([(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j), (3, -99, 3j)], - mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)], - dtype=mdtype) + mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)], + dtype=mdtype) assert_equal(test, control) # test = np.mafromtxt(TextIO(data), - missing_values={0:-9, 'B':-99, 'C':-999j}, + missing_values={0: -9, 'B': -99, 'C': -999j}, **basekwargs) control = ma.array([(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j), (3, -99, 3j)], - mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)], - dtype=mdtype) + mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)], + dtype=mdtype) assert_equal(test, control) def test_user_filling_values(self): @@ -1249,8 +1230,8 @@ M 33 21.99 kwargs = dict(delimiter=",", dtype=int, names="a,b,c", - missing_values={0:"N/A", 'b':" ", 2:"???"}, - filling_values={0:0, 'b':0, 2:-999}) + missing_values={0: "N/A", 'b': " ", 2: "???"}, + filling_values={0: 0, 'b': 0, 2: -999}) test = np.genfromtxt(TextIO(data), **kwargs) ctrl = np.array([(0, 2, 3), (4, 0, -999)], dtype=[(_, int) for _ in "abc"]) @@ -1260,7 +1241,6 @@ M 33 21.99 ctrl = np.array([(0, 3), (4, -999)], dtype=[(_, int) for _ in "ac"]) assert_equal(test, ctrl) - def test_withmissing_float(self): data = TextIO('A,B\n0,1.5\n2,-999.00') test = np.mafromtxt(data, dtype=None, delimiter=',', @@ -1271,7 +1251,6 @@ M 33 21.99 assert_equal(test, control) assert_equal(test.mask, control.mask) - def test_with_masked_column_uniform(self): "Test masked column" data = TextIO('1 2 3\n4 5 6\n') @@ -1290,7 +1269,6 @@ M 33 21.99 dtype=[('f0', bool), ('f1', bool), ('f2', int)]) assert_equal(test, control) - def test_invalid_raise(self): "Test invalid raise" data = ["1, 1, 1, 1, 1"] * 50 @@ -1303,6 +1281,7 @@ M 33 21.99 # XXX: is there a better way to get the return value of the callable in # assert_warns ? ret = {} + def f(_ret={}): _ret['mtest'] = np.ndfromtxt(mdata, invalid_raise=False, **kwargs) assert_warns(ConversionWarning, f, _ret=ret) @@ -1326,6 +1305,7 @@ M 33 21.99 # XXX: is there a better way to get the return value of the callable in # assert_warns ? ret = {} + def f(_ret={}): _ret['mtest'] = np.ndfromtxt(mdata, usecols=(0, 4), **kwargs) assert_warns(ConversionWarning, f, _ret=ret) @@ -1340,18 +1320,16 @@ M 33 21.99 control[[10 * _ for _ in range(5)]] = (2, 2) assert_equal(mtest, control) - def test_inconsistent_dtype(self): "Test inconsistent dtype" data = ["1, 1, 1, 1, -1.1"] * 50 mdata = TextIO("\n".join(data)) - converters = {4: lambda x:"(%s)" % x} + converters = {4: lambda x: "(%s)" % x} kwargs = dict(delimiter=",", converters=converters, dtype=[(_, int) for _ in 'abcde'],) assert_raises(ValueError, np.genfromtxt, mdata, **kwargs) - def test_default_field_format(self): "Test default format" data = "0, 1, 2.3\n4, 5, 6.7" @@ -1493,7 +1471,7 @@ M 33 21.99 def test_filling_values(self): "Test missing values" data = b"1, 2, 3\n1, , 5\n0, 6, \n" - kwargs = dict(delimiter=",", dtype=None, filling_values= -999) + kwargs = dict(delimiter=",", dtype=None, filling_values=-999) ctrl = np.array([[1, 2, 3], [1, -999, 5], [0, 6, -999]], dtype=int) test = np.ndfromtxt(TextIO(data), **kwargs) assert_equal(test, ctrl) @@ -1618,6 +1596,7 @@ def test_gzip_loadtxt(): os.close(f) os.unlink(name) + def test_gzip_loadtxt_from_string(): s = BytesIO() f = gzip.GzipFile(fileobj=s, mode="w") @@ -1628,6 +1607,7 @@ def test_gzip_loadtxt_from_string(): f = gzip.GzipFile(fileobj=s, mode="r") assert_array_equal(np.loadtxt(f), [1, 2, 3]) + def test_npzfile_dict(): s = BytesIO() x = np.zeros((3, 3)) @@ -1654,6 +1634,7 @@ def test_npzfile_dict(): assert_('x' in z.keys()) + def test_load_refcount(): # Check that objects returned by np.load are directly freed based on # their refcount, rather than needing the gc to collect them. diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index 25f09275d..70e7865db 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -14,19 +14,19 @@ from numpy.lib import ( _ndat = np.array( - [[ 0.6244, np.nan, 0.2692, 0.0116, np.nan, 0.1170], - [ 0.5351, 0.9403, np.nan, 0.2100, 0.4759, 0.2833], - [ np.nan, np.nan, np.nan, 0.1042, np.nan, 0.5954], - [ 0.161, np.nan, np.nan, 0.1859, 0.3146, np.nan]] - ) + [[0.6244, np.nan, 0.2692, 0.0116, np.nan, 0.1170], + [0.5351, 0.9403, np.nan, 0.2100, 0.4759, 0.2833], + [np.nan, np.nan, np.nan, 0.1042, np.nan, 0.5954], + [0.161, np.nan, np.nan, 0.1859, 0.3146, np.nan]] +) # rows of _ndat with nans removed _rdat = [ - np.array([ 0.6244, 0.2692, 0.0116, 0.1170]), - np.array([ 0.5351, 0.9403, 0.2100, 0.4759, 0.2833]), - np.array([ 0.1042, 0.5954]), - np.array([ 0.1610, 0.1859, 0.3146]) - ] + np.array([0.6244, 0.2692, 0.0116, 0.1170]), + np.array([0.5351, 0.9403, 0.2100, 0.4759, 0.2833]), + np.array([0.1042, 0.5954]), + np.array([0.1610, 0.1859, 0.3146]) +] class TestNanFunctions_MinMax(TestCase): @@ -143,8 +143,8 @@ class TestNanFunctions_ArgminArgmax(TestCase): class TestNanFunctions_IntTypes(TestCase): int_types = ( - np.int8, np.int16, np.int32, np.int64, np.uint8, - np.uint16, np.uint32, np.uint64) + np.int8, np.int16, np.int32, np.int64, np.uint8, + np.uint16, np.uint32, np.uint64) def setUp(self, *args, **kwargs): self.mat = np.array([127, 39, 93, 87, 46]) @@ -275,13 +275,13 @@ class TestNanFunctions_MeanVarStd(TestCase): def test_dtype_error(self): for f in self.nanfuncs: for dtype in [np.bool_, np.int_, np.object]: - assert_raises( TypeError, f, _ndat, axis=1, dtype=np.int) + assert_raises(TypeError, f, _ndat, axis=1, dtype=np.int) def test_out_dtype_error(self): for f in self.nanfuncs: for dtype in [np.bool_, np.int_, np.object]: out = np.empty(_ndat.shape[0], dtype=dtype) - assert_raises( TypeError, f, _ndat, axis=1, out=out) + assert_raises(TypeError, f, _ndat, axis=1, out=out) def test_keepdims(self): mat = np.eye(3) diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py index 617419eee..4adc6aed8 100644 --- a/numpy/lib/tests/test_polynomial.py +++ b/numpy/lib/tests/test_polynomial.py @@ -81,6 +81,7 @@ poly1d([ 2.]) from numpy.testing import * import numpy as np + class TestDocs(TestCase): def test_doctests(self): return rundocs() @@ -100,7 +101,7 @@ class TestDocs(TestCase): p[1] = 0 assert_equal(str(p), " \n0") - def test_polyfit(self) : + def test_polyfit(self): c = np.array([3., 2., 1.]) x = np.linspace(0, 2, 7) y = np.polyval(c, x) @@ -118,9 +119,9 @@ class TestDocs(TestCase): m2, cov2 = np.polyfit(x, y+err, 2, w=weights, cov=True) assert_almost_equal([4.8927, -1.0177, 1.7768], m2, decimal=4) - val = [[ 8.7929, -10.0103, 0.9756], + val = [[8.7929, -10.0103, 0.9756], [-10.0103, 13.6134, -1.8178], - [ 0.9756, -1.8178, 0.6674]] + [0.9756, -1.8178, 0.6674]] assert_almost_equal(val, cov2, decimal=4) # check 2D (n,1) case @@ -135,8 +136,8 @@ class TestDocs(TestCase): m, cov = np.polyfit(x, yy + np.array(err)[:, np.newaxis], 2, cov=True) assert_almost_equal(est, m[:, 0], decimal=4) assert_almost_equal(est, m[:, 1], decimal=4) - assert_almost_equal(val0, cov[:,:, 0], decimal=4) - assert_almost_equal(val0, cov[:,:, 1], decimal=4) + assert_almost_equal(val0, cov[:, :, 0], decimal=4) + assert_almost_equal(val0, cov[:, :, 1], decimal=4) def test_objects(self): from decimal import Decimal @@ -160,7 +161,8 @@ class TestDocs(TestCase): def test_integ_coeffs(self): p = np.poly1d([3, 2, 1]) p2 = p.integ(3, k=[9, 7, 6]) - assert_((p2.coeffs == [1/4./5., 1/3./4., 1/2./3., 9/1./2., 7, 6]).all()) + assert_( + (p2.coeffs == [1/4./5., 1/3./4., 1/2./3., 9/1./2., 7, 6]).all()) def test_zero_dims(self): try: diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index b175bcb64..d0eda50ce 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -13,24 +13,23 @@ get_names = np.lib.recfunctions.get_names get_names_flat = np.lib.recfunctions.get_names_flat zip_descr = np.lib.recfunctions.zip_descr + class TestRecFunctions(TestCase): - """ - Misc tests - """ - # + # Misc tests + def setUp(self): x = np.array([1, 2, ]) y = np.array([10, 20, 30]) z = np.array([('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) w = np.array([(1, (2, 3.0)), (4, (5, 6.0))], - dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) + dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) self.data = (w, x, y, z) - def test_zip_descr(self): - "Test zip_descr" + # Test zip_descr (w, x, y, z) = self.data + # Std array test = zip_descr((x, x), flatten=True) assert_equal(test, @@ -38,6 +37,7 @@ class TestRecFunctions(TestCase): test = zip_descr((x, x), flatten=False) assert_equal(test, np.dtype([('', int), ('', int)])) + # Std & flexible-dtype test = zip_descr((x, z), flatten=True) assert_equal(test, @@ -46,6 +46,7 @@ class TestRecFunctions(TestCase): assert_equal(test, np.dtype([('', int), ('', [('A', '|S3'), ('B', float)])])) + # Standard & nested dtype test = zip_descr((x, w), flatten=True) assert_equal(test, @@ -58,78 +59,80 @@ class TestRecFunctions(TestCase): ('', [('a', int), ('b', [('ba', float), ('bb', int)])])])) - def test_drop_fields(self): - "Test drop_fields" + # Test drop_fields a = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) + # A basic field test = drop_fields(a, 'a') control = np.array([((2, 3.0),), ((5, 6.0),)], - dtype=[('b', [('ba', float), ('bb', int)])]) + dtype=[('b', [('ba', float), ('bb', int)])]) assert_equal(test, control) + # Another basic field (but nesting two fields) test = drop_fields(a, 'b') control = np.array([(1,), (4,)], dtype=[('a', int)]) assert_equal(test, control) + # A nested sub-field test = drop_fields(a, ['ba', ]) control = np.array([(1, (3.0,)), (4, (6.0,))], - dtype=[('a', int), ('b', [('bb', int)])]) + dtype=[('a', int), ('b', [('bb', int)])]) assert_equal(test, control) + # All the nested sub-field from a field: zap that field test = drop_fields(a, ['ba', 'bb']) control = np.array([(1,), (4,)], dtype=[('a', int)]) assert_equal(test, control) - # + test = drop_fields(a, ['a', 'b']) assert_(test is None) - def test_rename_fields(self): - "Tests rename fields" + # Test rename fields a = np.array([(1, (2, [3.0, 30.])), (4, (5, [6.0, 60.]))], dtype=[('a', int), ('b', [('ba', float), ('bb', (float, 2))])]) - test = rename_fields(a, {'a':'A', 'bb':'BB'}) + test = rename_fields(a, {'a': 'A', 'bb': 'BB'}) newdtype = [('A', int), ('b', [('ba', float), ('BB', (float, 2))])] control = a.view(newdtype) assert_equal(test.dtype, newdtype) assert_equal(test, control) - def test_get_names(self): - "Tests get_names" + # Test get_names ndtype = np.dtype([('A', '|S3'), ('B', float)]) test = get_names(ndtype) assert_equal(test, ('A', 'B')) - # + ndtype = np.dtype([('a', int), ('b', [('ba', float), ('bb', int)])]) test = get_names(ndtype) assert_equal(test, ('a', ('b', ('ba', 'bb')))) - def test_get_names_flat(self): - "Test get_names_flat" + # Test get_names_flat ndtype = np.dtype([('A', '|S3'), ('B', float)]) test = get_names_flat(ndtype) assert_equal(test, ('A', 'B')) - # + ndtype = np.dtype([('a', int), ('b', [('ba', float), ('bb', int)])]) test = get_names_flat(ndtype) assert_equal(test, ('a', 'b', 'ba', 'bb')) - def test_get_fieldstructure(self): - "Test get_fieldstructure" + # Test get_fieldstructure + # No nested fields ndtype = np.dtype([('A', '|S3'), ('B', float)]) test = get_fieldstructure(ndtype) - assert_equal(test, {'A':[], 'B':[]}) + assert_equal(test, {'A': [], 'B': []}) + # One 1-nested field ndtype = np.dtype([('A', int), ('B', [('BA', float), ('BB', '|S1')])]) test = get_fieldstructure(ndtype) - assert_equal(test, {'A': [], 'B': [], 'BA':['B', ], 'BB':['B']}) + assert_equal(test, {'A': [], 'B': [], 'BA': ['B', ], 'BB': ['B']}) + # One 2-nested fields ndtype = np.dtype([('A', int), ('B', [('BA', int), @@ -140,48 +143,48 @@ class TestRecFunctions(TestCase): assert_equal(test, control) def test_find_duplicates(self): - "Test find_duplicates" + # Test find_duplicates a = ma.array([(2, (2., 'B')), (1, (2., 'B')), (2, (2., 'B')), (1, (1., 'B')), (2, (2., 'B')), (2, (2., 'C'))], - mask=[(0, (0, 0)), (0, (0, 0)), (0, (0, 0)), - (0, (0, 0)), (1, (0, 0)), (0, (1, 0))], - dtype=[('A', int), ('B', [('BA', float), ('BB', '|S1')])]) - # + mask=[(0, (0, 0)), (0, (0, 0)), (0, (0, 0)), + (0, (0, 0)), (1, (0, 0)), (0, (1, 0))], + dtype=[('A', int), ('B', [('BA', float), ('BB', '|S1')])]) + test = find_duplicates(a, ignoremask=False, return_index=True) control = [0, 2] assert_equal(sorted(test[-1]), control) assert_equal(test[0], a[test[-1]]) - # + test = find_duplicates(a, key='A', return_index=True) control = [0, 1, 2, 3, 5] assert_equal(sorted(test[-1]), control) assert_equal(test[0], a[test[-1]]) - # + test = find_duplicates(a, key='B', return_index=True) control = [0, 1, 2, 4] assert_equal(sorted(test[-1]), control) assert_equal(test[0], a[test[-1]]) - # + test = find_duplicates(a, key='BA', return_index=True) control = [0, 1, 2, 4] assert_equal(sorted(test[-1]), control) assert_equal(test[0], a[test[-1]]) - # + test = find_duplicates(a, key='BB', return_index=True) control = [0, 1, 2, 3, 4] assert_equal(sorted(test[-1]), control) assert_equal(test[0], a[test[-1]]) def test_find_duplicates_ignoremask(self): - "Test the ignoremask option of find_duplicates" + # Test the ignoremask option of find_duplicates ndtype = [('a', int)] a = ma.array([1, 1, 1, 2, 2, 3, 3], - mask=[0, 0, 1, 0, 0, 0, 1]).view(ndtype) + mask=[0, 0, 1, 0, 0, 0, 1]).view(ndtype) test = find_duplicates(a, ignoremask=True, return_index=True) control = [0, 1, 3, 4] assert_equal(sorted(test[-1]), control) assert_equal(test[0], a[test[-1]]) - # + test = find_duplicates(a, ignoremask=False, return_index=True) control = [0, 1, 2, 3, 4, 6] assert_equal(sorted(test[-1]), control) @@ -189,20 +192,18 @@ class TestRecFunctions(TestCase): class TestRecursiveFillFields(TestCase): - """ - Test recursive_fill_fields. - """ + # Test recursive_fill_fields. def test_simple_flexible(self): - "Test recursive_fill_fields on flexible-array" + # Test recursive_fill_fields on flexible-array a = np.array([(1, 10.), (2, 20.)], dtype=[('A', int), ('B', float)]) b = np.zeros((3,), dtype=a.dtype) test = recursive_fill_fields(a, b) control = np.array([(1, 10.), (2, 20.), (0, 0.)], dtype=[('A', int), ('B', float)]) assert_equal(test, control) - # + def test_masked_flexible(self): - "Test recursive_fill_fields on masked flexible-array" + # Test recursive_fill_fields on masked flexible-array a = ma.array([(1, 10.), (2, 20.)], mask=[(0, 1), (1, 0)], dtype=[('A', int), ('B', float)]) b = ma.zeros((3,), dtype=a.dtype) @@ -211,96 +212,95 @@ class TestRecursiveFillFields(TestCase): mask=[(0, 1), (1, 0), (0, 0)], dtype=[('A', int), ('B', float)]) assert_equal(test, control) - # - class TestMergeArrays(TestCase): - """ - Test merge_arrays - """ + # Test merge_arrays + def setUp(self): x = np.array([1, 2, ]) y = np.array([10, 20, 30]) - z = np.array([('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) - w = np.array([(1, (2, 3.0)), (4, (5, 6.0))], + z = np.array( + [('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) + w = np.array( + [(1, (2, 3.0)), (4, (5, 6.0))], dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) self.data = (w, x, y, z) - # + def test_solo(self): - "Test merge_arrays on a single array." + # Test merge_arrays on a single array. (_, x, _, z) = self.data - # + test = merge_arrays(x) control = np.array([(1,), (2,)], dtype=[('f0', int)]) assert_equal(test, control) test = merge_arrays((x,)) assert_equal(test, control) - # + test = merge_arrays(z, flatten=False) assert_equal(test, z) test = merge_arrays(z, flatten=True) assert_equal(test, z) - # + def test_solo_w_flatten(self): - "Test merge_arrays on a single array w & w/o flattening" + # Test merge_arrays on a single array w & w/o flattening w = self.data[0] test = merge_arrays(w, flatten=False) assert_equal(test, w) - # + test = merge_arrays(w, flatten=True) control = np.array([(1, 2, 3.0), (4, 5, 6.0)], - dtype=[('a', int), ('ba', float), ('bb', int)]) + dtype=[('a', int), ('ba', float), ('bb', int)]) assert_equal(test, control) - # + def test_standard(self): - "Test standard & standard" + # Test standard & standard # Test merge arrays (_, x, y, _) = self.data test = merge_arrays((x, y), usemask=False) control = np.array([(1, 10), (2, 20), (-1, 30)], dtype=[('f0', int), ('f1', int)]) assert_equal(test, control) - # + test = merge_arrays((x, y), usemask=True) control = ma.array([(1, 10), (2, 20), (-1, 30)], mask=[(0, 0), (0, 0), (1, 0)], dtype=[('f0', int), ('f1', int)]) assert_equal(test, control) assert_equal(test.mask, control.mask) - # + def test_flatten(self): - "Test standard & flexible" + # Test standard & flexible (_, x, _, z) = self.data test = merge_arrays((x, z), flatten=True) control = np.array([(1, 'A', 1.), (2, 'B', 2.)], dtype=[('f0', int), ('A', '|S3'), ('B', float)]) assert_equal(test, control) - # + test = merge_arrays((x, z), flatten=False) control = np.array([(1, ('A', 1.)), (2, ('B', 2.))], dtype=[('f0', int), ('f1', [('A', '|S3'), ('B', float)])]) assert_equal(test, control) - # + def test_flatten_wflexible(self): - "Test flatten standard & nested" + # Test flatten standard & nested (w, x, _, _) = self.data test = merge_arrays((x, w), flatten=True) control = np.array([(1, 1, 2, 3.0), (2, 4, 5, 6.0)], dtype=[('f0', int), ('a', int), ('ba', float), ('bb', int)]) assert_equal(test, control) - # + test = merge_arrays((x, w), flatten=False) controldtype = dtype = [('f0', int), - ('f1', [('a', int), - ('b', [('ba', float), ('bb', int)])])] + ('f1', [('a', int), + ('b', [('ba', float), ('bb', int)])])] control = np.array([(1., (1, (2, 3.0))), (2, (4, (5, 6.0)))], dtype=controldtype) - # + def test_wmasked_arrays(self): - "Test merge_arrays masked arrays" + # Test merge_arrays masked arrays (_, x, _, _) = self.data mx = ma.array([1, 2, 3], mask=[1, 0, 0]) test = merge_arrays((x, mx), usemask=True) @@ -311,23 +311,23 @@ class TestMergeArrays(TestCase): test = merge_arrays((x, mx), usemask=True, asrecarray=True) assert_equal(test, control) assert_(isinstance(test, MaskedRecords)) - # + def test_w_singlefield(self): - "Test single field" + # Test single field test = merge_arrays((np.array([1, 2]).view([('a', int)]), np.array([10., 20., 30.])),) control = ma.array([(1, 10.), (2, 20.), (-1, 30.)], mask=[(0, 0), (0, 0), (1, 0)], dtype=[('a', int), ('f1', float)]) assert_equal(test, control) - # + def test_w_shorter_flex(self): - "Test merge_arrays w/ a shorter flexndarray." + # Test merge_arrays w/ a shorter flexndarray. z = self.data[-1] test = merge_arrays((z, np.array([10, 20, 30]).view([('C', int)]))) control = np.array([('A', 1., 10), ('B', 2., 20), ('-1', -1, 20)], dtype=[('A', '|S3'), ('B', float), ('C', int)]) - # + def test_singlerecord(self): (_, x, y, z) = self.data test = merge_arrays((x[0], y[0], z[0]), usemask=False) @@ -338,179 +338,177 @@ class TestMergeArrays(TestCase): assert_equal(test, control) - class TestAppendFields(TestCase): - """ - Test append_fields - """ + # Test append_fields + def setUp(self): x = np.array([1, 2, ]) y = np.array([10, 20, 30]) - z = np.array([('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) + z = np.array( + [('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) w = np.array([(1, (2, 3.0)), (4, (5, 6.0))], - dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) + dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) self.data = (w, x, y, z) - # + def test_append_single(self): - "Test simple case" + # Test simple case (_, x, _, _) = self.data test = append_fields(x, 'A', data=[10, 20, 30]) control = ma.array([(1, 10), (2, 20), (-1, 30)], mask=[(0, 0), (0, 0), (1, 0)], dtype=[('f0', int), ('A', int)],) assert_equal(test, control) - # + def test_append_double(self): - "Test simple case" + # Test simple case (_, x, _, _) = self.data test = append_fields(x, ('A', 'B'), data=[[10, 20, 30], [100, 200]]) control = ma.array([(1, 10, 100), (2, 20, 200), (-1, 30, -1)], mask=[(0, 0, 0), (0, 0, 0), (1, 0, 1)], dtype=[('f0', int), ('A', int), ('B', int)],) assert_equal(test, control) - # + def test_append_on_flex(self): - "Test append_fields on flexible type arrays" + # Test append_fields on flexible type arrays z = self.data[-1] test = append_fields(z, 'C', data=[10, 20, 30]) control = ma.array([('A', 1., 10), ('B', 2., 20), (-1, -1., 30)], mask=[(0, 0, 0), (0, 0, 0), (1, 1, 0)], dtype=[('A', '|S3'), ('B', float), ('C', int)],) assert_equal(test, control) - # + def test_append_on_nested(self): - "Test append_fields on nested fields" + # Test append_fields on nested fields w = self.data[0] test = append_fields(w, 'C', data=[10, 20, 30]) control = ma.array([(1, (2, 3.0), 10), (4, (5, 6.0), 20), (-1, (-1, -1.), 30)], - mask=[(0, (0, 0), 0), (0, (0, 0), 0), (1, (1, 1), 0)], - dtype=[('a', int), - ('b', [('ba', float), ('bb', int)]), - ('C', int)],) + mask=[( + 0, (0, 0), 0), (0, (0, 0), 0), (1, (1, 1), 0)], + dtype=[('a', int), + ('b', [('ba', float), ('bb', int)]), + ('C', int)],) assert_equal(test, control) - class TestStackArrays(TestCase): - """ - Test stack_arrays - """ + # Test stack_arrays def setUp(self): x = np.array([1, 2, ]) y = np.array([10, 20, 30]) - z = np.array([('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) + z = np.array( + [('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) w = np.array([(1, (2, 3.0)), (4, (5, 6.0))], - dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) + dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) self.data = (w, x, y, z) - # + def test_solo(self): - "Test stack_arrays on single arrays" + # Test stack_arrays on single arrays (_, x, _, _) = self.data test = stack_arrays((x,)) assert_equal(test, x) self.assertTrue(test is x) - # + test = stack_arrays(x) assert_equal(test, x) self.assertTrue(test is x) - # + def test_unnamed_fields(self): - "Tests combinations of arrays w/o named fields" + # Tests combinations of arrays w/o named fields (_, x, y, _) = self.data - # + test = stack_arrays((x, x), usemask=False) control = np.array([1, 2, 1, 2]) assert_equal(test, control) - # + test = stack_arrays((x, y), usemask=False) control = np.array([1, 2, 10, 20, 30]) assert_equal(test, control) - # + test = stack_arrays((y, x), usemask=False) control = np.array([10, 20, 30, 1, 2]) assert_equal(test, control) - # + def test_unnamed_and_named_fields(self): - "Test combination of arrays w/ & w/o named fields" + # Test combination of arrays w/ & w/o named fields (_, x, _, z) = self.data - # + test = stack_arrays((x, z)) control = ma.array([(1, -1, -1), (2, -1, -1), (-1, 'A', 1), (-1, 'B', 2)], - mask=[(0, 1, 1), (0, 1, 1), - (1, 0, 0), (1, 0, 0)], - dtype=[('f0', int), ('A', '|S3'), ('B', float)]) + mask=[(0, 1, 1), (0, 1, 1), + (1, 0, 0), (1, 0, 0)], + dtype=[('f0', int), ('A', '|S3'), ('B', float)]) assert_equal(test, control) assert_equal(test.mask, control.mask) - # + test = stack_arrays((z, x)) control = ma.array([('A', 1, -1), ('B', 2, -1), (-1, -1, 1), (-1, -1, 2), ], - mask=[(0, 0, 1), (0, 0, 1), - (1, 1, 0), (1, 1, 0)], - dtype=[('A', '|S3'), ('B', float), ('f2', int)]) + mask=[(0, 0, 1), (0, 0, 1), + (1, 1, 0), (1, 1, 0)], + dtype=[('A', '|S3'), ('B', float), ('f2', int)]) assert_equal(test, control) assert_equal(test.mask, control.mask) - # + test = stack_arrays((z, z, x)) control = ma.array([('A', 1, -1), ('B', 2, -1), ('A', 1, -1), ('B', 2, -1), (-1, -1, 1), (-1, -1, 2), ], - mask=[(0, 0, 1), (0, 0, 1), - (0, 0, 1), (0, 0, 1), - (1, 1, 0), (1, 1, 0)], - dtype=[('A', '|S3'), ('B', float), ('f2', int)]) + mask=[(0, 0, 1), (0, 0, 1), + (0, 0, 1), (0, 0, 1), + (1, 1, 0), (1, 1, 0)], + dtype=[('A', '|S3'), ('B', float), ('f2', int)]) assert_equal(test, control) - # + def test_matching_named_fields(self): - "Test combination of arrays w/ matching field names" + # Test combination of arrays w/ matching field names (_, x, _, z) = self.data zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)], dtype=[('A', '|S3'), ('B', float), ('C', float)]) test = stack_arrays((z, zz)) control = ma.array([('A', 1, -1), ('B', 2, -1), - ('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)], - dtype=[('A', '|S3'), ('B', float), ('C', float)], - mask=[(0, 0, 1), (0, 0, 1), - (0, 0, 0), (0, 0, 0), (0, 0, 0)]) + ( + 'a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)], + dtype=[('A', '|S3'), ('B', float), ('C', float)], + mask=[(0, 0, 1), (0, 0, 1), + (0, 0, 0), (0, 0, 0), (0, 0, 0)]) assert_equal(test, control) assert_equal(test.mask, control.mask) - # + test = stack_arrays((z, zz, x)) ndtype = [('A', '|S3'), ('B', float), ('C', float), ('f3', int)] control = ma.array([('A', 1, -1, -1), ('B', 2, -1, -1), ('a', 10., 100., -1), ('b', 20., 200., -1), ('c', 30., 300., -1), (-1, -1, -1, 1), (-1, -1, -1, 2)], - dtype=ndtype, - mask=[(0, 0, 1, 1), (0, 0, 1, 1), - (0, 0, 0, 1), (0, 0, 0, 1), (0, 0, 0, 1), - (1, 1, 1, 0), (1, 1, 1, 0)]) + dtype=ndtype, + mask=[(0, 0, 1, 1), (0, 0, 1, 1), + (0, 0, 0, 1), (0, 0, 0, 1), (0, 0, 0, 1), + (1, 1, 1, 0), (1, 1, 1, 0)]) assert_equal(test, control) assert_equal(test.mask, control.mask) - def test_defaults(self): - "Test defaults: no exception raised if keys of defaults are not fields." + # Test defaults: no exception raised if keys of defaults are not fields. (_, _, _, z) = self.data zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)], dtype=[('A', '|S3'), ('B', float), ('C', float)]) - defaults = {'A':'???', 'B':-999., 'C':-9999., 'D':-99999.} + defaults = {'A': '???', 'B': -999., 'C': -9999., 'D': -99999.} test = stack_arrays((z, zz), defaults=defaults) control = ma.array([('A', 1, -9999.), ('B', 2, -9999.), - ('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)], - dtype=[('A', '|S3'), ('B', float), ('C', float)], - mask=[(0, 0, 1), (0, 0, 1), - (0, 0, 0), (0, 0, 0), (0, 0, 0)]) + ( + 'a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)], + dtype=[('A', '|S3'), ('B', float), ('C', float)], + mask=[(0, 0, 1), (0, 0, 1), + (0, 0, 0), (0, 0, 0), (0, 0, 0)]) assert_equal(test, control) assert_equal(test.data, control.data) assert_equal(test.mask, control.mask) - def test_autoconversion(self): - "Tests autoconversion" + # Tests autoconversion adtype = [('A', int), ('B', bool), ('C', float)] a = ma.array([(1, 2, 3)], mask=[(0, 1, 0)], dtype=adtype) bdtype = [('A', int), ('B', float), ('C', float)] @@ -527,9 +525,8 @@ class TestStackArrays(TestCase): else: raise AssertionError - def test_checktitles(self): - "Test using titles in the field names" + # Test using titles in the field names adtype = [(('a', 'A'), int), (('b', 'B'), bool), (('c', 'C'), float)] a = ma.array([(1, 2, 3)], mask=[(0, 1, 0)], dtype=adtype) bdtype = [(('a', 'A'), int), (('b', 'B'), bool), (('c', 'C'), float)] @@ -544,16 +541,16 @@ class TestStackArrays(TestCase): class TestJoinBy(TestCase): def setUp(self): self.a = np.array(list(zip(np.arange(10), np.arange(50, 60), - np.arange(100, 110))), - dtype=[('a', int), ('b', int), ('c', int)]) + np.arange(100, 110))), + dtype=[('a', int), ('b', int), ('c', int)]) self.b = np.array(list(zip(np.arange(5, 15), np.arange(65, 75), - np.arange(100, 110))), - dtype=[('a', int), ('b', int), ('d', int)]) - # + np.arange(100, 110))), + dtype=[('a', int), ('b', int), ('d', int)]) + def test_inner_join(self): - "Basic test of join_by" + # Basic test of join_by a, b = self.a, self.b - # + test = join_by('a', a, b, jointype='inner') control = np.array([(5, 55, 65, 105, 100), (6, 56, 66, 106, 101), (7, 57, 67, 107, 102), (8, 58, 68, 108, 103), @@ -564,7 +561,7 @@ class TestJoinBy(TestCase): def test_join(self): a, b = self.a, self.b - # + test = join_by(('a', 'b'), a, b) control = np.array([(5, 55, 105, 100), (6, 56, 106, 101), (7, 57, 107, 102), (8, 58, 108, 103), @@ -574,7 +571,7 @@ class TestJoinBy(TestCase): def test_outer_join(self): a, b = self.a, self.b - # + test = join_by(('a', 'b'), a, b, 'outer') control = ma.array([(0, 50, 100, -1), (1, 51, 101, -1), (2, 52, 102, -1), (3, 53, 103, -1), @@ -586,52 +583,53 @@ class TestJoinBy(TestCase): (9, 69, -1, 104), (10, 70, -1, 105), (11, 71, -1, 106), (12, 72, -1, 107), (13, 73, -1, 108), (14, 74, -1, 109)], - mask=[(0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 1, 0), (0, 0, 0, 1), - (0, 0, 1, 0), (0, 0, 0, 1), - (0, 0, 1, 0), (0, 0, 0, 1), - (0, 0, 1, 0), (0, 0, 0, 1), - (0, 0, 1, 0), (0, 0, 1, 0), - (0, 0, 1, 0), (0, 0, 1, 0), - (0, 0, 1, 0), (0, 0, 1, 0)], - dtype=[('a', int), ('b', int), - ('c', int), ('d', int)]) + mask=[(0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 1, 0), + (0, 0, 1, 0), (0, 0, 1, 0), + (0, 0, 1, 0), (0, 0, 1, 0)], + dtype=[('a', int), ('b', int), + ('c', int), ('d', int)]) assert_equal(test, control) def test_leftouter_join(self): a, b = self.a, self.b - # + test = join_by(('a', 'b'), a, b, 'leftouter') control = ma.array([(0, 50, 100, -1), (1, 51, 101, -1), (2, 52, 102, -1), (3, 53, 103, -1), (4, 54, 104, -1), (5, 55, 105, -1), (6, 56, 106, -1), (7, 57, 107, -1), (8, 58, 108, -1), (9, 59, 109, -1)], - mask=[(0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1)], - dtype=[('a', int), ('b', int), ('c', int), ('d', int)]) + mask=[(0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1)], + dtype=[('a', int), ('b', int), ('c', int), ('d', int)]) class TestJoinBy2(TestCase): @classmethod def setUp(cls): cls.a = np.array(list(zip(np.arange(10), np.arange(50, 60), - np.arange(100, 110))), - dtype=[('a', int), ('b', int), ('c', int)]) + np.arange(100, 110))), + dtype=[('a', int), ('b', int), ('c', int)]) cls.b = np.array(list(zip(np.arange(10), np.arange(65, 75), - np.arange(100, 110))), - dtype=[('a', int), ('b', int), ('d', int)]) + np.arange(100, 110))), + dtype=[('a', int), ('b', int), ('d', int)]) def test_no_r1postfix(self): - "Basic test of join_by no_r1postfix" + # Basic test of join_by no_r1postfix a, b = self.a, self.b - test = join_by('a', a, b, r1postfix='', r2postfix='2', jointype='inner') + test = join_by( + 'a', a, b, r1postfix='', r2postfix='2', jointype='inner') control = np.array([(0, 50, 65, 100, 100), (1, 51, 66, 101, 101), (2, 52, 67, 102, 102), (3, 53, 68, 103, 103), (4, 54, 69, 104, 104), (5, 55, 70, 105, 105), @@ -641,15 +639,16 @@ class TestJoinBy2(TestCase): ('c', int), ('d', int)]) assert_equal(test, control) - def test_no_postfix(self): - self.assertRaises(ValueError, join_by, 'a', self.a, self.b, r1postfix='', r2postfix='') + self.assertRaises(ValueError, join_by, 'a', self.a, self.b, + r1postfix='', r2postfix='') def test_no_r2postfix(self): - "Basic test of join_by no_r2postfix" + # Basic test of join_by no_r2postfix a, b = self.a, self.b - test = join_by('a', a, b, r1postfix='1', r2postfix='', jointype='inner') + test = join_by( + 'a', a, b, r1postfix='1', r2postfix='', jointype='inner') control = np.array([(0, 50, 65, 100, 100), (1, 51, 66, 101, 101), (2, 52, 67, 102, 102), (3, 53, 68, 103, 103), (4, 54, 69, 104, 104), (5, 55, 70, 105, 105), @@ -661,25 +660,25 @@ class TestJoinBy2(TestCase): def test_two_keys_two_vars(self): a = np.array(list(zip(np.tile([10, 11], 5), np.repeat(np.arange(5), 2), - np.arange(50, 60), np.arange(10, 20))), - dtype=[('k', int), ('a', int), ('b', int), ('c', int)]) + np.arange(50, 60), np.arange(10, 20))), + dtype=[('k', int), ('a', int), ('b', int), ('c', int)]) b = np.array(list(zip(np.tile([10, 11], 5), np.repeat(np.arange(5), 2), - np.arange(65, 75), np.arange(0, 10))), - dtype=[('k', int), ('a', int), ('b', int), ('c', int)]) + np.arange(65, 75), np.arange(0, 10))), + dtype=[('k', int), ('a', int), ('b', int), ('c', int)]) control = np.array([(10, 0, 50, 65, 10, 0), (11, 0, 51, 66, 11, 1), (10, 1, 52, 67, 12, 2), (11, 1, 53, 68, 13, 3), (10, 2, 54, 69, 14, 4), (11, 2, 55, 70, 15, 5), (10, 3, 56, 71, 16, 6), (11, 3, 57, 72, 17, 7), (10, 4, 58, 73, 18, 8), (11, 4, 59, 74, 19, 9)], - dtype=[('k', int), ('a', int), ('b1', int), - ('b2', int), ('c1', int), ('c2', int)]) - test = join_by(['a', 'k'], a, b, r1postfix='1', r2postfix='2', jointype='inner') + dtype=[('k', int), ('a', int), ('b1', int), + ('b2', int), ('c1', int), ('c2', int)]) + test = join_by( + ['a', 'k'], a, b, r1postfix='1', r2postfix='2', jointype='inner') assert_equal(test.dtype, control.dtype) assert_equal(test, control) - if __name__ == '__main__': run_module_suite() diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 67808bd39..7d4487342 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -9,13 +9,14 @@ from numpy.compat import unicode rlevel = 1 + class TestRegression(TestCase): - def test_poly1d(self,level=rlevel): + def test_poly1d(self, level=rlevel): """Ticket #28""" assert_equal(np.poly1d([1]) - np.poly1d([1, 0]), np.poly1d([-1, 1])) - def test_cov_parameters(self,level=rlevel): + def test_cov_parameters(self, level=rlevel): """Ticket #91""" x = np.random.random((3, 3)) y = x.copy() @@ -23,13 +24,13 @@ class TestRegression(TestCase): np.cov(y, rowvar=0) assert_array_equal(x, y) - def test_mem_digitize(self,level=rlevel): + def test_mem_digitize(self, level=rlevel): """Ticket #95""" for i in range(100): np.digitize([1, 2, 3, 4], [1, 3]) np.digitize([0, 1, 2, 3, 4], [1, 3]) - def test_unique_zero_sized(self,level=rlevel): + def test_unique_zero_sized(self, level=rlevel): """Ticket #205""" assert_array_equal([], np.unique(np.array([]))) @@ -37,7 +38,8 @@ class TestRegression(TestCase): """Ticket #325""" vt = np.vectorize(lambda *args: args) vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1, 1, 2))) - vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1, 1, 2)), np.zeros((2, 2))) + vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1, + 1, 2)), np.zeros((2, 2))) def test_mgrid_single_element(self, level=rlevel): """Ticket #339""" @@ -46,7 +48,8 @@ class TestRegression(TestCase): def test_refcount_vectorize(self, level=rlevel): """Ticket #378""" - def p(x, y): return 123 + def p(x, y): + return 123 v = np.vectorize(p) _assert_valid_refcount(v) @@ -85,7 +88,7 @@ class TestRegression(TestCase): def test_polyfit_build(self): """Ticket #628""" ref = [-1.06123820e-06, 5.70886914e-04, -1.13822012e-01, - 9.95368241e+00, -3.14526520e+02] + 9.95368241e+00, -3.14526520e+02] x = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 129, @@ -103,8 +106,7 @@ class TestRegression(TestCase): tested = np.polyfit(x, y, 4) assert_array_almost_equal(ref, tested) - - def test_polydiv_type(self) : + def test_polydiv_type(self): """Make polydiv work for complex types""" msg = "Wrong type, should be complex" x = np.ones(3, dtype=np.complex) @@ -115,11 +117,11 @@ class TestRegression(TestCase): q, r = np.polydiv(x, x) assert_(q.dtype == np.float, msg) - def test_histogramdd_too_many_bins(self) : + def test_histogramdd_too_many_bins(self): """Ticket 928.""" assert_raises(ValueError, np.histogramdd, np.ones((1, 10)), bins=2**10) - def test_polyint_type(self) : + def test_polyint_type(self): """Ticket #944""" msg = "Wrong type, should be complex" x = np.ones(3, dtype=np.complex) @@ -141,11 +143,13 @@ class TestRegression(TestCase): # Large enough to fail on 64-bit. nbits = np.dtype(np.intp).itemsize * 8 thesize = int((2**nbits)**(1.0/5.0)+1) + def dp(): n = 3 a = np.ones((n,)*5) i = np.random.randint(0, n, size=thesize) a[np.ix_(i, i, i, i, i)] = 0 + def dp2(): n = 3 a = np.ones((n,)*5) @@ -159,15 +163,16 @@ class TestRegression(TestCase): x = np.zeros((1,), dt) assert_(np.r_[x, x].dtype == dt) - def test_who_with_0dim_array(self, level=rlevel) : + def test_who_with_0dim_array(self, level=rlevel): """ticket #1243""" - import os, sys + import os + import sys oldstdout = sys.stdout sys.stdout = open(os.devnull, 'w') try: try: - tmp = np.who({'foo' : np.array(1)}) + tmp = np.who({'foo': np.array(1)}) except: raise AssertionError("ticket #1243") finally: diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py index 157e1beb3..55b008871 100644 --- a/numpy/lib/tests/test_shape_base.py +++ b/numpy/lib/tests/test_shape_base.py @@ -5,14 +5,17 @@ from numpy.lib import * from numpy.core import * from numpy import matrix, asmatrix + class TestApplyAlongAxis(TestCase): def test_simple(self): a = ones((20, 10), 'd') - assert_array_equal(apply_along_axis(len, 0, a), len(a)*ones(shape(a)[1])) + assert_array_equal( + apply_along_axis(len, 0, a), len(a)*ones(shape(a)[1])) - def test_simple101(self,level=11): + def test_simple101(self, level=11): a = ones((10, 101), 'd') - assert_array_equal(apply_along_axis(len, 0, a), len(a)*ones(shape(a)[1])) + assert_array_equal( + apply_along_axis(len, 0, a), len(a)*ones(shape(a)[1])) def test_3d(self): a = arange(27).reshape((3, 3, 3)) @@ -32,7 +35,7 @@ class TestArraySplit(TestCase): a = arange(10) try: res = array_split(a, 0) - assert_(0) # it should have thrown a value error + assert_(0) # it should have thrown a value error except ValueError: pass @@ -55,27 +58,32 @@ class TestArraySplit(TestCase): compare_results(res, desired) res = array_split(a, 5) - desired = [arange(2), arange(2, 4), arange(4, 6), arange(6, 8), arange(8, 10)] + desired = [arange( + 2), arange(2, 4), arange(4, 6), arange(6, 8), arange(8, 10)] compare_results(res, desired) res = array_split(a, 6) - desired = [arange(2), arange(2, 4), arange(4, 6), arange(6, 8), arange(8, 9), - arange(9, 10)] + desired = [arange( + 2), arange(2, 4), arange(4, 6), arange(6, 8), arange(8, 9), + arange(9, 10)] compare_results(res, desired) res = array_split(a, 7) - desired = [arange(2), arange(2, 4), arange(4, 6), arange(6, 7), arange(7, 8), - arange(8, 9), arange(9, 10)] + desired = [arange( + 2), arange(2, 4), arange(4, 6), arange(6, 7), arange(7, 8), + arange(8, 9), arange(9, 10)] compare_results(res, desired) res = array_split(a, 8) - desired = [arange(2), arange(2, 4), arange(4, 5), arange(5, 6), arange(6, 7), - arange(7, 8), arange(8, 9), arange(9, 10)] + desired = [arange( + 2), arange(2, 4), arange(4, 5), arange(5, 6), arange(6, 7), + arange(7, 8), arange(8, 9), arange(9, 10)] compare_results(res, desired) res = array_split(a, 9) - desired = [arange(2), arange(2, 3), arange(3, 4), arange(4, 5), arange(5, 6), - arange(6, 7), arange(7, 8), arange(8, 9), arange(9, 10)] + desired = [arange( + 2), arange(2, 3), arange(3, 4), arange(4, 5), arange(5, 6), + arange(6, 7), arange(7, 8), arange(8, 9), arange(9, 10)] compare_results(res, desired) res = array_split(a, 10) @@ -151,44 +159,50 @@ class TestSplit(TestCase): a = arange(10) try: res = split(a, 3) - assert_(0) # should raise an error + assert_(0) # should raise an error except ValueError: pass class TestDstack(TestCase): def test_0D_array(self): - a = array(1); b = array(2); - res=dstack([a, b]) + a = array(1) + b = array(2) + res = dstack([a, b]) desired = array([[[1, 2]]]) assert_array_equal(res, desired) def test_1D_array(self): - a = array([1]); b = array([2]); - res=dstack([a, b]) + a = array([1]) + b = array([2]) + res = dstack([a, b]) desired = array([[[1, 2]]]) assert_array_equal(res, desired) def test_2D_array(self): - a = array([[1], [2]]); b = array([[1], [2]]); - res=dstack([a, b]) - desired = array([[[1, 1]], [[2, 2,]]]) + a = array([[1], [2]]) + b = array([[1], [2]]) + res = dstack([a, b]) + desired = array([[[1, 1]], [[2, 2, ]]]) assert_array_equal(res, desired) def test_2D_array2(self): - a = array([1, 2]); b = array([1, 2]); - res=dstack([a, b]) + a = array([1, 2]) + b = array([1, 2]) + res = dstack([a, b]) desired = array([[[1, 1], [2, 2]]]) assert_array_equal(res, desired) """ array_split has more comprehensive test of splitting. only do simple test on hsplit, vsplit, and dsplit """ + + class TestHsplit(TestCase): """ only testing for integer splits. """ def test_0D_array(self): - a= array(1) + a = array(1) try: hsplit(a, 2) assert_(0) @@ -196,13 +210,13 @@ class TestHsplit(TestCase): pass def test_1D_array(self): - a= array([1, 2, 3, 4]) + a = array([1, 2, 3, 4]) res = hsplit(a, 2) desired = [array([1, 2]), array([3, 4])] compare_results(res, desired) def test_2D_array(self): - a= array([[1, 2, 3, 4], + a = array([[1, 2, 3, 4], [1, 2, 3, 4]]) res = hsplit(a, 2) desired = [array([[1, 2], [1, 2]]), array([[3, 4], [3, 4]])] @@ -213,7 +227,7 @@ class TestVsplit(TestCase): """ only testing for integer splits. """ def test_1D_array(self): - a= array([1, 2, 3, 4]) + a = array([1, 2, 3, 4]) try: vsplit(a, 2) assert_(0) @@ -221,7 +235,7 @@ class TestVsplit(TestCase): pass def test_2D_array(self): - a= array([[1, 2, 3, 4], + a = array([[1, 2, 3, 4], [1, 2, 3, 4]]) res = vsplit(a, 2) desired = [array([[1, 2, 3, 4]]), array([[1, 2, 3, 4]])] @@ -232,7 +246,7 @@ class TestDsplit(TestCase): """ only testing for integer splits. """ def test_2D_array(self): - a= array([[1, 2, 3, 4], + a = array([[1, 2, 3, 4], [1, 2, 3, 4]]) try: dsplit(a, 2) @@ -241,7 +255,7 @@ class TestDsplit(TestCase): pass def test_3D_array(self): - a= array([[[1, 2, 3, 4], + a = array([[[1, 2, 3, 4], [1, 2, 3, 4]], [[1, 2, 3, 4], [1, 2, 3, 4]]]) @@ -276,6 +290,7 @@ class TestKron(TestCase): assert_equal(type(kron(m, m)), matrix) assert_equal(type(kron(a, m)), matrix) assert_equal(type(kron(m, a)), matrix) + class myarray(ndarray): __array_priority__ = 0.0 ma = myarray(a.shape, a.dtype, a.data) @@ -295,7 +310,7 @@ class TestTile(TestCase): assert_equal(tile(b, 2), [[1, 2, 1, 2], [3, 4, 3, 4]]) assert_equal(tile(b, (2, 1)), [[1, 2], [3, 4], [1, 2], [3, 4]]) assert_equal(tile(b, (2, 2)), [[1, 2, 1, 2], [3, 4, 3, 4], - [1, 2, 1, 2], [3, 4, 3, 4]]) + [1, 2, 1, 2], [3, 4, 3, 4]]) def test_empty(self): a = array([[[]]]) @@ -304,8 +319,8 @@ class TestTile(TestCase): def test_kroncompare(self): import numpy.random as nr - reps=[(2,), (1, 2), (2, 1), (2, 2), (2, 3, 2), (3, 2)] - shape=[(3,), (2, 3), (3, 4, 3), (3, 2, 3), (4, 3, 2, 4), (2, 2)] + reps = [(2,), (1, 2), (2, 1), (2, 2), (2, 3, 2), (3, 2)] + shape = [(3,), (2, 3), (3, 4, 3), (3, 2, 3), (4, 3, 2, 4), (2, 2)] for s in shape: b = nr.randint(0, 10, size=s) for r in reps: diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py index 5d06e0a8c..4d57d2ca7 100644 --- a/numpy/lib/tests/test_stride_tricks.py +++ b/numpy/lib/tests/test_stride_tricks.py @@ -15,6 +15,7 @@ def assert_shapes_correct(input_shapes, expected_shape): expected = [expected_shape] * len(inarrays) assert_equal(outshapes, expected) + def assert_incompatible_shapes_raise(input_shapes): """ Broadcast a list of arrays with the given (incompatible) input shapes and check that they raise a ValueError. @@ -22,6 +23,7 @@ def assert_incompatible_shapes_raise(input_shapes): inarrays = [np.zeros(s) for s in input_shapes] assert_raises(ValueError, broadcast_arrays, *inarrays) + def assert_same_as_ufunc(shape0, shape1, transposed=False, flipped=False): """ Broadcast two shapes against each other and check that the data layout is the same as if a ufunc did the broadcasting. @@ -51,6 +53,7 @@ def test_same(): assert_array_equal(x, bx) assert_array_equal(y, by) + def test_one_off(): x = np.array([[1, 2, 3]]) y = np.array([[1], [2], [3]]) @@ -60,6 +63,7 @@ def test_one_off(): assert_array_equal(bx0, bx) assert_array_equal(by0, by) + def test_same_input_shapes(): """ Check that the final shape is just the input shape. """ @@ -86,6 +90,7 @@ def test_same_input_shapes(): input_shapes3 = [shape, shape, shape] assert_shapes_correct(input_shapes3, shape) + def test_two_compatible_by_ones_input_shapes(): """ Check that two different input shapes (of the same length but some have 1s) broadcast to the correct shape. @@ -110,6 +115,7 @@ def test_two_compatible_by_ones_input_shapes(): # Reverse the input shapes since broadcasting should be symmetric. assert_shapes_correct(input_shapes[::-1], expected_shape) + def test_two_compatible_by_prepending_ones_input_shapes(): """ Check that two different input shapes (of different lengths) broadcast to the correct shape. @@ -141,6 +147,7 @@ def test_two_compatible_by_prepending_ones_input_shapes(): # Reverse the input shapes since broadcasting should be symmetric. assert_shapes_correct(input_shapes[::-1], expected_shape) + def test_incompatible_shapes_raise_valueerror(): """ Check that a ValueError is raised for incompatible shapes. """ @@ -155,6 +162,7 @@ def test_incompatible_shapes_raise_valueerror(): # Reverse the input shapes since broadcasting should be symmetric. assert_incompatible_shapes_raise(input_shapes[::-1]) + def test_same_as_ufunc(): """ Check that the data layout is the same as if a ufunc did the operation. """ @@ -195,7 +203,7 @@ def test_same_as_ufunc(): ] for input_shapes, expected_shape in data: assert_same_as_ufunc(input_shapes[0], input_shapes[1], - "Shapes: %s %s" % (input_shapes[0], input_shapes[1])) + "Shapes: %s %s" % (input_shapes[0], input_shapes[1])) # Reverse the input shapes since broadcasting should be symmetric. assert_same_as_ufunc(input_shapes[1], input_shapes[0]) # Try them transposed, too. diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py index 4ec5c34a2..4c19eba58 100644 --- a/numpy/lib/tests/test_twodim_base.py +++ b/numpy/lib/tests/test_twodim_base.py @@ -5,55 +5,73 @@ from __future__ import division, absolute_import, print_function from numpy.testing import * -from numpy import ( arange, rot90, add, fliplr, flipud, zeros, ones, eye, - array, diag, histogram2d, tri, mask_indices, triu_indices, - triu_indices_from, tril_indices, tril_indices_from ) +from numpy import (arange, rot90, add, fliplr, flipud, zeros, ones, eye, + array, diag, histogram2d, tri, mask_indices, triu_indices, + triu_indices_from, tril_indices, tril_indices_from) import numpy as np from numpy.compat import asbytes, asbytes_nested + def get_mat(n): data = arange(n) data = add.outer(data, data) return data + class TestEye(TestCase): def test_basic(self): - assert_equal(eye(4), array([[1, 0, 0, 0], - [0, 1, 0, 0], - [0, 0, 1, 0], - [0, 0, 0, 1]])) - assert_equal(eye(4, dtype='f'), array([[1, 0, 0, 0], - [0, 1, 0, 0], - [0, 0, 1, 0], - [0, 0, 0, 1]], 'f')) - assert_equal(eye(3) == 1, eye(3, dtype=bool)) + assert_equal(eye(4), + array([[1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1]])) + + assert_equal(eye(4, dtype='f'), + array([[1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1]], 'f')) + + assert_equal(eye(3) == 1, + eye(3, dtype=bool)) def test_diag(self): - assert_equal(eye(4, k=1), array([[0, 1, 0, 0], - [0, 0, 1, 0], - [0, 0, 0, 1], - [0, 0, 0, 0]])) - assert_equal(eye(4, k=-1), array([[0, 0, 0, 0], - [1, 0, 0, 0], - [0, 1, 0, 0], - [0, 0, 1, 0]])) + assert_equal(eye(4, k=1), + array([[0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1], + [0, 0, 0, 0]])) + + assert_equal(eye(4, k=-1), + array([[0, 0, 0, 0], + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0]])) + def test_2d(self): - assert_equal(eye(4, 3), array([[1, 0, 0], - [0, 1, 0], - [0, 0, 1], - [0, 0, 0]])) - assert_equal(eye(3, 4), array([[1, 0, 0, 0], - [0, 1, 0, 0], - [0, 0, 1, 0]])) + assert_equal(eye(4, 3), + array([[1, 0, 0], + [0, 1, 0], + [0, 0, 1], + [0, 0, 0]])) + + assert_equal(eye(3, 4), + array([[1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0]])) + def test_diag2d(self): - assert_equal(eye(3, 4, k=2), array([[0, 0, 1, 0], - [0, 0, 0, 1], - [0, 0, 0, 0]])) - assert_equal(eye(4, 3, k=-2), array([[0, 0, 0], - [0, 0, 0], - [1, 0, 0], - [0, 1, 0]])) + assert_equal(eye(3, 4, k=2), + array([[0, 0, 1, 0], + [0, 0, 0, 1], + [0, 0, 0, 0]])) + + assert_equal(eye(4, 3, k=-2), + array([[0, 0, 0], + [0, 0, 0], + [1, 0, 0], + [0, 1, 0]])) def test_eye_bounds(self): assert_equal(eye(2, 2, 1), [[0, 1], [0, 0]]) @@ -73,6 +91,7 @@ class TestEye(TestCase): def test_bool(self): assert_equal(eye(2, 2, dtype=bool), [[True, False], [False, True]]) + class TestDiag(TestCase): def test_vector(self): vals = (100 * arange(5)).astype('l') @@ -119,6 +138,7 @@ class TestDiag(TestCase): def test_failure(self): self.assertRaises(ValueError, diag, [[[1]]]) + class TestFliplr(TestCase): def test_basic(self): self.assertRaises(ValueError, fliplr, ones(4)) @@ -131,10 +151,11 @@ class TestFliplr(TestCase): [5, 4, 3]] assert_equal(fliplr(a), b) + class TestFlipud(TestCase): def test_basic(self): a = get_mat(4) - b = a[::-1,:] + b = a[::-1, :] assert_equal(flipud(a), b) a = [[0, 1, 2], [3, 4, 5]] @@ -142,6 +163,7 @@ class TestFlipud(TestCase): [0, 1, 2]] assert_equal(flipud(a), b) + class TestRot90(TestCase): def test_basic(self): self.assertRaises(ValueError, rot90, ones(4)) @@ -172,22 +194,26 @@ class TestRot90(TestCase): a = ones((50, 40, 3)) assert_equal(rot90(a).shape, (40, 50, 3)) + class TestHistogram2d(TestCase): def test_simple(self): - x = array([ 0.41702200, 0.72032449, 0.00011437481, 0.302332573, 0.146755891]) - y = array([ 0.09233859, 0.18626021, 0.34556073, 0.39676747, 0.53881673]) + x = array( + [0.41702200, 0.72032449, 1.1437481e-4, 0.302332573, 0.146755891]) + y = array( + [0.09233859, 0.18626021, 0.34556073, 0.39676747, 0.53881673]) xedges = np.linspace(0, 1, 10) yedges = np.linspace(0, 1, 10) H = histogram2d(x, y, (xedges, yedges))[0] - answer = array([[0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 1, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [1, 0, 1, 0, 0, 0, 0, 0, 0], - [0, 1, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0]]) + answer = array( + [[0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]]) assert_array_equal(H.T, answer) H = histogram2d(x, y, xedges)[0] assert_array_equal(H.T, answer) @@ -199,23 +225,27 @@ class TestHistogram2d(TestCase): def test_asym(self): x = array([1, 1, 2, 3, 4, 4, 4, 5]) y = array([1, 3, 2, 0, 1, 2, 3, 4]) - H, xed, yed = histogram2d(x, y, (6, 5), range = [[0, 6], [0, 5]], normed=True) - answer = array([[0., 0, 0, 0, 0], - [0, 1, 0, 1, 0], - [0, 0, 1, 0, 0], - [1, 0, 0, 0, 0], - [0, 1, 1, 1, 0], - [0, 0, 0, 0, 1]]) + H, xed, yed = histogram2d( + x, y, (6, 5), range=[[0, 6], [0, 5]], normed=True) + answer = array( + [[0., 0, 0, 0, 0], + [0, 1, 0, 1, 0], + [0, 0, 1, 0, 0], + [1, 0, 0, 0, 0], + [0, 1, 1, 1, 0], + [0, 0, 0, 0, 1]]) assert_array_almost_equal(H, answer/8., 3) assert_array_equal(xed, np.linspace(0, 6, 7)) assert_array_equal(yed, np.linspace(0, 5, 6)) + def test_norm(self): x = array([1, 2, 3, 1, 2, 3, 1, 2, 3]) y = array([1, 1, 1, 2, 2, 2, 3, 3, 3]) - H, xed, yed = histogram2d(x, y, [[1, 2, 3, 5], [1, 2, 3, 5]], normed=True) - answer=array([[1, 1, .5], - [1, 1, .5], - [.5, .5, .25]])/9. + H, xed, yed = histogram2d( + x, y, [[1, 2, 3, 5], [1, 2, 3, 5]], normed=True) + answer = array([[1, 1, .5], + [1, 1, .5], + [.5, .5, .25]])/9. assert_array_almost_equal(H, answer, 3) def test_all_outliers(self): @@ -225,7 +255,7 @@ class TestHistogram2d(TestCase): def test_empty(self): a, edge1, edge2 = histogram2d([], [], bins=([0, 1], [0, 1])) - assert_array_max_ulp(a, array([[ 0.]])) + assert_array_max_ulp(a, array([[0.]])) a, edge1, edge2 = histogram2d([], [], bins=4) assert_array_max_ulp(a, np.zeros((4, 4))) @@ -267,30 +297,30 @@ def test_tril_indices(): il1 = tril_indices(4) il2 = tril_indices(4, 2) - a = np.array([[1, 2, 3, 4], - [5, 6, 7, 8], - [9, 10, 11, 12], + a = np.array([[1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], [13, 14, 15, 16]]) # indexing: yield (assert_array_equal, a[il1], - array([ 1, 5, 6, 9, 10, 11, 13, 14, 15, 16]) ) + array([1, 5, 6, 9, 10, 11, 13, 14, 15, 16])) # And for assigning values: a[il1] = -1 yield (assert_array_equal, a, - array([[-1, 2, 3, 4], - [-1, -1, 7, 8], - [-1, -1, -1, 12], - [-1, -1, -1, -1]]) ) + array([[-1, 2, 3, 4], + [-1, -1, 7, 8], + [-1, -1, -1, 12], + [-1, -1, -1, -1]])) # These cover almost the whole array (two diagonals right of the main one): a[il2] = -10 yield (assert_array_equal, a, - array([[-10, -10, -10, 4], - [-10, -10, -10, -10], - [-10, -10, -10, -10], - [-10, -10, -10, -10]]) ) + array([[-10, -10, -10, 4], + [-10, -10, -10, -10], + [-10, -10, -10, -10], + [-10, -10, -10, -10]])) class TestTriuIndices(object): @@ -298,9 +328,9 @@ class TestTriuIndices(object): iu1 = triu_indices(4) iu2 = triu_indices(4, 2) - a = np.array([[1, 2, 3, 4], - [5, 6, 7, 8], - [9, 10, 11, 12], + a = np.array([[1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], [13, 14, 15, 16]]) # Both for indexing: @@ -311,17 +341,17 @@ class TestTriuIndices(object): a[iu1] = -1 yield (assert_array_equal, a, array([[-1, -1, -1, -1], - [ 5, -1, -1, -1], - [ 9, 10, -1, -1], - [13, 14, 15, -1]]) ) + [5, -1, -1, -1], + [9, 10, -1, -1], + [13, 14, 15, -1]])) # These cover almost the whole array (two diagonals right of the main one): a[iu2] = -10 - yield ( assert_array_equal, a, - array([[ -1, -1, -10, -10], - [ 5, -1, -1, -10], - [ 9, 10, -1, -1], - [ 13, 14, 15, -1]]) ) + yield (assert_array_equal, a, + array([[-1, -1, -10, -10], + [5, -1, -1, -10], + [9, 10, -1, -1], + [13, 14, 15, -1]])) class TestTrilIndicesFrom(object): diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py index 3ca093e16..eac8134e5 100644 --- a/numpy/lib/tests/test_type_check.py +++ b/numpy/lib/tests/test_type_check.py @@ -5,7 +5,7 @@ from numpy.core import * from numpy.random import rand from numpy.compat import asbytes, long from numpy.testing import ( - TestCase, assert_, assert_equal, assert_array_equal, run_module_suite) + TestCase, assert_, assert_equal, assert_array_equal, run_module_suite) try: import ctypes @@ -31,7 +31,6 @@ class TestCommonType(TestCase): assert_(common_type(acd) == cdouble) - class TestMintypecode(TestCase): def test_default_1(self): @@ -121,6 +120,7 @@ class TestIscomplex(TestCase): z = array([-1, 0, 1]) res = iscomplex(z) assert_(not sometrue(res, axis=0)) + def test_pass(self): z = array([-1j, 1, 0]) res = iscomplex(z) @@ -133,6 +133,7 @@ class TestIsreal(TestCase): z = array([-1, 0, 1j]) res = isreal(z) assert_array_equal(res, [1, 1, 0]) + def test_fail(self): z = array([-1j, 1, 0]) res = isreal(z) @@ -148,7 +149,6 @@ class TestIscomplexobj(TestCase): assert_(iscomplexobj(z)) - class TestIsrealobj(TestCase): def test_basic(self): z = array([-1, 0, 1]) diff --git a/numpy/lib/tests/test_ufunclike.py b/numpy/lib/tests/test_ufunclike.py index 31dbdba1a..2bedd9b40 100644 --- a/numpy/lib/tests/test_ufunclike.py +++ b/numpy/lib/tests/test_ufunclike.py @@ -5,6 +5,7 @@ import numpy.core as nx import numpy.lib.ufunclike as ufl from numpy.testing.decorators import deprecated + class TestUfunclike(TestCase): def test_isposinf(self): @@ -32,7 +33,7 @@ class TestUfunclike(TestCase): def test_fix(self): a = nx.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]]) out = nx.zeros(a.shape, float) - tgt = nx.array([[ 1., 1., 1., 1.], [-1., -1., -1., -1.]]) + tgt = nx.array([[1., 1., 1., 1.], [-1., -1., -1., -1.]]) res = ufl.fix(a) assert_equal(res, tgt) @@ -47,6 +48,7 @@ class TestUfunclike(TestCase): res = nx.array(data, copy=True).view(cls) res.metadata = metadata return res + def __array_wrap__(self, obj, context=None): obj.metadata = self.metadata return obj diff --git a/numpy/lib/tests/test_utils.py b/numpy/lib/tests/test_utils.py index 4062d35b5..21415d96b 100644 --- a/numpy/lib/tests/test_utils.py +++ b/numpy/lib/tests/test_utils.py @@ -10,6 +10,7 @@ if sys.version_info[0] >= 3: else: from StringIO import StringIO + def test_lookfor(): out = StringIO() utils.lookfor('eigenvalue', module='numpy', output=out, @@ -22,20 +23,25 @@ def test_lookfor(): def old_func(self, x): return x + @deprecate(message="Rather use new_func2") def old_func2(self, x): return x + def old_func3(self, x): return x new_func3 = deprecate(old_func3, old_name="old_func3", new_name="new_func3") + def test_deprecate_decorator(): assert_('deprecated' in old_func.__doc__) + def test_deprecate_decorator_message(): assert_('Rather use new_func2' in old_func2.__doc__) + def test_deprecate_fn(): assert_('old_func3' in new_func3.__doc__) assert_('new_func3' in new_func3.__doc__) |