diff options
Diffstat (limited to 'numpy/lib/tests')
24 files changed, 570 insertions, 455 deletions
diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py index 8eac16b58..1ed7815d9 100644 --- a/numpy/lib/tests/test__datasource.py +++ b/numpy/lib/tests/test__datasource.py @@ -1,24 +1,14 @@ -from __future__ import division, absolute_import, print_function - import os -import sys import pytest from tempfile import mkdtemp, mkstemp, NamedTemporaryFile from shutil import rmtree import numpy.lib._datasource as datasource -from numpy.testing import ( - assert_, assert_equal, assert_raises, assert_warns - ) +from numpy.testing import assert_, assert_equal, assert_raises -if sys.version_info[0] >= 3: - import urllib.request as urllib_request - from urllib.parse import urlparse - from urllib.error import URLError -else: - import urllib2 as urllib_request - from urlparse import urlparse - from urllib2 import URLError +import urllib.request as urllib_request +from urllib.parse import urlparse +from urllib.error import URLError def urlopen_stub(url, data=None): @@ -96,7 +86,7 @@ def invalid_httpfile(): return http_fakefile -class TestDataSourceOpen(object): +class TestDataSourceOpen: def setup(self): self.tmpdir = mkdtemp() self.ds = datasource.DataSource(self.tmpdir) @@ -164,26 +154,8 @@ class TestDataSourceOpen(object): fp.close() assert_equal(magic_line, result) - @pytest.mark.skipif(sys.version_info[0] >= 3, reason="Python 2 only") - def test_Bz2File_text_mode_warning(self): - try: - import bz2 - except ImportError: - # We don't have the bz2 capabilities to test. - pytest.skip() - # Test datasource's internal file_opener for BZip2 files. - filepath = os.path.join(self.tmpdir, 'foobar.txt.bz2') - fp = bz2.BZ2File(filepath, 'w') - fp.write(magic_line) - fp.close() - with assert_warns(RuntimeWarning): - fp = self.ds.open(filepath, 'rt') - result = fp.readline() - fp.close() - assert_equal(magic_line, result) - -class TestDataSourceExists(object): +class TestDataSourceExists: def setup(self): self.tmpdir = mkdtemp() self.ds = datasource.DataSource(self.tmpdir) @@ -213,7 +185,7 @@ class TestDataSourceExists(object): assert_equal(self.ds.exists(tmpfile), False) -class TestDataSourceAbspath(object): +class TestDataSourceAbspath: def setup(self): self.tmpdir = os.path.abspath(mkdtemp()) self.ds = datasource.DataSource(self.tmpdir) @@ -278,7 +250,7 @@ class TestDataSourceAbspath(object): os.sep = orig_os_sep -class TestRepositoryAbspath(object): +class TestRepositoryAbspath: def setup(self): self.tmpdir = os.path.abspath(mkdtemp()) self.repos = datasource.Repository(valid_baseurl(), self.tmpdir) @@ -311,7 +283,7 @@ class TestRepositoryAbspath(object): os.sep = orig_os_sep -class TestRepositoryExists(object): +class TestRepositoryExists: def setup(self): self.tmpdir = mkdtemp() self.repos = datasource.Repository(valid_baseurl(), self.tmpdir) @@ -344,7 +316,7 @@ class TestRepositoryExists(object): assert_(self.repos.exists(tmpfile)) -class TestOpenFunc(object): +class TestOpenFunc: def setup(self): self.tmpdir = mkdtemp() diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py index 15cd3ad9d..6964c1128 100644 --- a/numpy/lib/tests/test__iotools.py +++ b/numpy/lib/tests/test__iotools.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import time from datetime import date @@ -11,10 +9,9 @@ from numpy.lib._iotools import ( LineSplitter, NameValidator, StringConverter, has_nested_fields, easy_dtype, flatten_dtype ) -from numpy.compat import unicode -class TestLineSplitter(object): +class TestLineSplitter: "Tests the LineSplitter class." def test_no_delimiter(self): @@ -83,7 +80,7 @@ class TestLineSplitter(object): # ----------------------------------------------------------------------------- -class TestNameValidator(object): +class TestNameValidator: def test_case_sensitivity(self): "Test case sensitivity" @@ -141,7 +138,7 @@ def _bytes_to_date(s): return date(*time.strptime(s, "%Y-%m-%d")[:3]) -class TestStringConverter(object): +class TestStringConverter: "Test StringConverter" def test_creation(self): @@ -181,10 +178,10 @@ class TestStringConverter(object): # note that the longdouble type has been skipped, so the # _status increases by 2. Everything should succeed with # unicode conversion (5). - for s in ['a', u'a', b'a']: + for s in ['a', b'a']: res = converter.upgrade(s) - assert_(type(res) is unicode) - assert_equal(res, u'a') + assert_(type(res) is str) + assert_equal(res, 'a') assert_equal(converter._status, 5 + status_offset) def test_missing(self): @@ -266,7 +263,7 @@ class TestStringConverter(object): assert_(converter(val) == 9223372043271415339) -class TestMiscFunctions(object): +class TestMiscFunctions: def test_has_nested_dtype(self): "Test has_nested_dtype" diff --git a/numpy/lib/tests/test__version.py b/numpy/lib/tests/test__version.py index 8e66a0c03..182504631 100644 --- a/numpy/lib/tests/test__version.py +++ b/numpy/lib/tests/test__version.py @@ -1,8 +1,6 @@ """Tests for the NumpyVersion class. """ -from __future__ import division, absolute_import, print_function - from numpy.testing import assert_, assert_raises from numpy.lib import NumpyVersion diff --git a/numpy/lib/tests/test_arraypad.py b/numpy/lib/tests/test_arraypad.py index 65593dd29..75db5928b 100644 --- a/numpy/lib/tests/test_arraypad.py +++ b/numpy/lib/tests/test_arraypad.py @@ -1,8 +1,6 @@ """Tests for the array padding functions. """ -from __future__ import division, absolute_import, print_function - import pytest import numpy as np @@ -31,7 +29,7 @@ _all_modes = { } -class TestAsPairs(object): +class TestAsPairs: def test_single_value(self): """Test casting for a single value.""" expected = np.array([[3, 3]] * 10) @@ -114,7 +112,7 @@ class TestAsPairs(object): _as_pairs(np.ones((2, 3)), 3) -class TestConditionalShortcuts(object): +class TestConditionalShortcuts: @pytest.mark.parametrize("mode", _all_modes.keys()) def test_zero_padding_shortcuts(self, mode): test = np.arange(120).reshape(4, 5, 6) @@ -136,7 +134,7 @@ class TestConditionalShortcuts(object): np.pad(test, pad_amt, mode=mode, stat_length=30)) -class TestStatistic(object): +class TestStatistic: def test_check_mean_stat_length(self): a = np.arange(100).astype('f') a = np.pad(a, ((25, 20), ), 'mean', stat_length=((2, 3), )) @@ -498,7 +496,7 @@ class TestStatistic(object): np.pad([1., 2.], 1, mode, stat_length=(1, 0)) -class TestConstant(object): +class TestConstant: def test_check_constant(self): a = np.arange(100) a = np.pad(a, (25, 20), 'constant', constant_values=(10, 20)) @@ -677,7 +675,7 @@ class TestConstant(object): assert result.shape == (3, 4, 4) -class TestLinearRamp(object): +class TestLinearRamp: def test_check_simple(self): a = np.arange(100).astype('f') a = np.pad(a, (25, 20), 'linear_ramp', end_values=(4, 5)) @@ -762,7 +760,7 @@ class TestLinearRamp(object): assert_equal(result, expected) -class TestReflect(object): +class TestReflect: def test_check_simple(self): a = np.arange(100) a = np.pad(a, (25, 20), 'reflect') @@ -872,7 +870,7 @@ class TestReflect(object): assert_array_equal(a, b) -class TestEmptyArray(object): +class TestEmptyArray: """Check how padding behaves on arrays with an empty dimension.""" @pytest.mark.parametrize( @@ -896,7 +894,7 @@ class TestEmptyArray(object): assert result.shape == (8, 0, 4) -class TestSymmetric(object): +class TestSymmetric: def test_check_simple(self): a = np.arange(100) a = np.pad(a, (25, 20), 'symmetric') @@ -1030,7 +1028,7 @@ class TestSymmetric(object): assert_array_equal(a, b) -class TestWrap(object): +class TestWrap: def test_check_simple(self): a = np.arange(100) a = np.pad(a, (25, 20), 'wrap') @@ -1144,7 +1142,7 @@ class TestWrap(object): assert_array_equal(np.r_[a, a, a, a][:-3], b) -class TestEdge(object): +class TestEdge: def test_check_simple(self): a = np.arange(12) a = np.reshape(a, (4, 3)) @@ -1183,7 +1181,7 @@ class TestEdge(object): assert_array_equal(padded, expected) -class TestEmpty(object): +class TestEmpty: def test_simple(self): arr = np.arange(24).reshape(4, 6) result = np.pad(arr, [(2, 3), (3, 1)], mode="empty") @@ -1231,7 +1229,7 @@ def test_object_input(mode): assert_array_equal(np.pad(a, pad_amt, mode=mode), b) -class TestPadWidth(object): +class TestPadWidth: @pytest.mark.parametrize("pad_width", [ (4, 5, 6, 7), ((1,), (2,), (3,)), @@ -1262,24 +1260,29 @@ class TestPadWidth(object): with pytest.raises(ValueError, match=match): np.pad(arr, pad_width, mode) - @pytest.mark.parametrize("pad_width", [ - "3", - "word", - None, - object(), - 3.4, - ((2, 3, 4), (3, 2)), # dtype=object (tuple) - complex(1, -1), - ((-2.1, 3), (3, 2)), + @pytest.mark.parametrize("pad_width, dtype", [ + ("3", None), + ("word", None), + (None, None), + (object(), None), + (3.4, None), + (((2, 3, 4), (3, 2)), object), + (complex(1, -1), None), + (((-2.1, 3), (3, 2)), None), ]) @pytest.mark.parametrize("mode", _all_modes.keys()) - def test_bad_type(self, pad_width, mode): + def test_bad_type(self, pad_width, dtype, mode): arr = np.arange(30).reshape((6, 5)) match = "`pad_width` must be of integral type." - with pytest.raises(TypeError, match=match): - np.pad(arr, pad_width, mode) - with pytest.raises(TypeError, match=match): - np.pad(arr, np.array(pad_width), mode) + if dtype is not None: + # avoid DeprecationWarning when not specifying dtype + with pytest.raises(TypeError, match=match): + np.pad(arr, np.array(pad_width, dtype=dtype), mode) + else: + with pytest.raises(TypeError, match=match): + np.pad(arr, pad_width, mode) + with pytest.raises(TypeError, match=match): + np.pad(arr, np.array(pad_width), mode) def test_pad_width_as_ndarray(self): a = np.arange(12) diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index fd21a7f76..81ba789e3 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -1,8 +1,6 @@ """Test functions for 1D array set operations. """ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import (assert_array_equal, assert_equal, @@ -13,8 +11,7 @@ from numpy.lib.arraysetops import ( import pytest - -class TestSetOps(object): +class TestSetOps: def test_intersect1d(self): # unique inputs @@ -36,7 +33,7 @@ class TestSetOps(object): def test_intersect1d_array_like(self): # See gh-11772 - class Test(object): + class Test: def __array__(self): return np.arange(3) @@ -120,12 +117,13 @@ class TestSetOps(object): assert_array_equal([-1, 0], ediff1d(zero_elem, to_begin=-1, to_end=0)) assert_array_equal([], ediff1d(one_elem)) assert_array_equal([1], ediff1d(two_elem)) - assert_array_equal([7,1,9], ediff1d(two_elem, to_begin=7, to_end=9)) - assert_array_equal([5,6,1,7,8], ediff1d(two_elem, to_begin=[5,6], to_end=[7,8])) - assert_array_equal([1,9], ediff1d(two_elem, to_end=9)) - assert_array_equal([1,7,8], ediff1d(two_elem, to_end=[7,8])) - assert_array_equal([7,1], ediff1d(two_elem, to_begin=7)) - assert_array_equal([5,6,1], ediff1d(two_elem, to_begin=[5,6])) + assert_array_equal([7, 1, 9], ediff1d(two_elem, to_begin=7, to_end=9)) + assert_array_equal([5, 6, 1, 7, 8], + ediff1d(two_elem, to_begin=[5, 6], to_end=[7, 8])) + assert_array_equal([1, 9], ediff1d(two_elem, to_end=9)) + assert_array_equal([1, 7, 8], ediff1d(two_elem, to_end=[7, 8])) + assert_array_equal([7, 1], ediff1d(two_elem, to_begin=7)) + assert_array_equal([5, 6, 1], ediff1d(two_elem, to_begin=[5, 6])) @pytest.mark.parametrize("ary, prepend, append", [ # should fail because trying to cast @@ -135,9 +133,9 @@ class TestSetOps(object): None, np.nan), # should fail because attempting - # to downcast to smaller int type: - (np.array([1, 2, 3], dtype=np.int16), - np.array([5, 1<<20, 2], dtype=np.int32), + # to downcast to int type: + (np.array([1, 2, 3], dtype=np.int64), + np.array([5, 7, 2], dtype=np.float32), None), # should fail because attempting to cast # two special floating point values @@ -152,29 +150,33 @@ class TestSetOps(object): # specifically, raise an appropriate # Exception when attempting to append or # prepend with an incompatible type - msg = 'cannot convert' - with assert_raises_regex(ValueError, msg): + msg = 'must be compatible' + with assert_raises_regex(TypeError, msg): ediff1d(ary=ary, to_end=append, to_begin=prepend) - @pytest.mark.parametrize("ary," - "prepend," - "append," - "expected", [ - (np.array([1, 2, 3], dtype=np.int16), - 0, - None, - np.array([0, 1, 1], dtype=np.int16)), - (np.array([1, 2, 3], dtype=np.int32), - 0, - 0, - np.array([0, 1, 1, 0], dtype=np.int32)), - (np.array([1, 2, 3], dtype=np.int64), - 3, - -9, - np.array([3, 1, 1, -9], dtype=np.int64)), - ]) + @pytest.mark.parametrize( + "ary,prepend,append,expected", + [ + (np.array([1, 2, 3], dtype=np.int16), + 2**16, # will be cast to int16 under same kind rule. + 2**16 + 4, + np.array([0, 1, 1, 4], dtype=np.int16)), + (np.array([1, 2, 3], dtype=np.float32), + np.array([5], dtype=np.float64), + None, + np.array([5, 1, 1], dtype=np.float32)), + (np.array([1, 2, 3], dtype=np.int32), + 0, + 0, + np.array([0, 1, 1, 0], dtype=np.int32)), + (np.array([1, 2, 3], dtype=np.int64), + 3, + -9, + np.array([3, 1, 1, -9], dtype=np.int64)), + ] + ) def test_ediff1d_scalar_handling(self, ary, prepend, @@ -187,7 +189,7 @@ class TestSetOps(object): to_end=append, to_begin=prepend) assert_equal(actual, expected) - + assert actual.dtype == expected.dtype def test_isin(self): # the tests for in1d cover most of isin's behavior @@ -197,33 +199,34 @@ class TestSetOps(object): b = np.asarray(b).flatten().tolist() return a in b isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1}) + def assert_isin_equal(a, b): x = isin(a, b) y = isin_slow(a, b) assert_array_equal(x, y) - #multidimensional arrays in both arguments + # multidimensional arrays in both arguments a = np.arange(24).reshape([2, 3, 4]) b = np.array([[10, 20, 30], [0, 1, 3], [11, 22, 33]]) assert_isin_equal(a, b) - #array-likes as both arguments + # array-likes as both arguments c = [(9, 8), (7, 6)] d = (9, 7) assert_isin_equal(c, d) - #zero-d array: + # zero-d array: f = np.array(3) assert_isin_equal(f, b) assert_isin_equal(a, f) assert_isin_equal(f, f) - #scalar: + # scalar: assert_isin_equal(5, b) assert_isin_equal(a, 6) assert_isin_equal(5, 6) - #empty array-like: + # empty array-like: x = [] assert_isin_equal(x, b) assert_isin_equal(a, x) @@ -410,7 +413,7 @@ class TestSetOps(object): assert_array_equal(c1, c2) -class TestUnique(object): +class TestUnique: def test_unique_1d(self): @@ -517,7 +520,8 @@ class TestUnique(object): a = [] a1_idx = np.unique(a, return_index=True)[1] a2_inv = np.unique(a, return_inverse=True)[1] - a3_idx, a3_inv = np.unique(a, return_index=True, return_inverse=True)[1:] + a3_idx, a3_inv = np.unique(a, return_index=True, + return_inverse=True)[1:] assert_equal(a1_idx.dtype, np.intp) assert_equal(a2_inv.dtype, np.intp) assert_equal(a3_idx.dtype, np.intp) @@ -560,9 +564,52 @@ class TestUnique(object): result = np.array([[-0.0, 0.0]]) assert_array_equal(unique(data, axis=0), result, msg) + @pytest.mark.parametrize("axis", [0, -1]) + def test_unique_1d_with_axis(self, axis): + x = np.array([4, 3, 2, 3, 2, 1, 2, 2]) + uniq = unique(x, axis=axis) + assert_array_equal(uniq, [1, 2, 3, 4]) + + def test_unique_axis_zeros(self): + # issue 15559 + single_zero = np.empty(shape=(2, 0), dtype=np.int8) + uniq, idx, inv, cnt = unique(single_zero, axis=0, return_index=True, + return_inverse=True, return_counts=True) + + # there's 1 element of shape (0,) along axis 0 + assert_equal(uniq.dtype, single_zero.dtype) + assert_array_equal(uniq, np.empty(shape=(1, 0))) + assert_array_equal(idx, np.array([0])) + assert_array_equal(inv, np.array([0, 0])) + assert_array_equal(cnt, np.array([2])) + + # there's 0 elements of shape (2,) along axis 1 + uniq, idx, inv, cnt = unique(single_zero, axis=1, return_index=True, + return_inverse=True, return_counts=True) + + assert_equal(uniq.dtype, single_zero.dtype) + assert_array_equal(uniq, np.empty(shape=(2, 0))) + assert_array_equal(idx, np.array([])) + assert_array_equal(inv, np.array([])) + assert_array_equal(cnt, np.array([])) + + # test a "complicated" shape + shape = (0, 2, 0, 3, 0, 4, 0) + multiple_zeros = np.empty(shape=shape) + for axis in range(len(shape)): + expected_shape = list(shape) + if shape[axis] == 0: + expected_shape[axis] = 0 + else: + expected_shape[axis] = 1 + + assert_array_equal(unique(multiple_zeros, axis=axis), + np.empty(shape=expected_shape)) + def test_unique_masked(self): # issue 8664 - x = np.array([64, 0, 1, 2, 3, 63, 63, 0, 0, 0, 1, 2, 0, 63, 0], dtype='uint8') + x = np.array([64, 0, 1, 2, 3, 63, 63, 0, 0, 0, 1, 2, 0, 63, 0], + dtype='uint8') y = np.ma.masked_equal(x, 0) v = np.unique(y) @@ -577,7 +624,7 @@ class TestUnique(object): # as unsigned byte strings. See gh-10495. fmt = "sort order incorrect for integer type '%s'" for dt in 'bhilq': - a = np.array([[-1],[0]], dt) + a = np.array([[-1], [0]], dt) b = np.unique(a, axis=0) assert_array_equal(a, b, fmt % dt) diff --git a/numpy/lib/tests/test_arrayterator.py b/numpy/lib/tests/test_arrayterator.py index 2ce4456a5..c00ed13d7 100644 --- a/numpy/lib/tests/test_arrayterator.py +++ b/numpy/lib/tests/test_arrayterator.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from operator import mul from functools import reduce diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py index 21088765f..26e79bc06 100644 --- a/numpy/lib/tests/test_financial.py +++ b/numpy/lib/tests/test_financial.py @@ -1,5 +1,4 @@ -from __future__ import division, absolute_import, print_function - +import warnings from decimal import Decimal import numpy as np @@ -8,22 +7,35 @@ from numpy.testing import ( ) -class TestFinancial(object): +def filter_deprecation(func): + def newfunc(*args, **kwargs): + with warnings.catch_warnings(record=True) as ws: + warnings.filterwarnings('always', category=DeprecationWarning) + func(*args, **kwargs) + assert_(all(w.category is DeprecationWarning for w in ws)) + return newfunc + + +class TestFinancial: + @filter_deprecation def test_npv_irr_congruence(self): # IRR is defined as the rate required for the present value of a # a series of cashflows to be zero i.e. NPV(IRR(x), x) = 0 cashflows = np.array([-40000, 5000, 8000, 12000, 30000]) assert_allclose(np.npv(np.irr(cashflows), cashflows), 0, atol=1e-10, rtol=0) + @filter_deprecation def test_rate(self): assert_almost_equal( np.rate(10, 0, -3500, 10000), 0.1107, 4) + @filter_deprecation def test_rate_decimal(self): rate = np.rate(Decimal('10'), Decimal('0'), Decimal('-3500'), Decimal('10000')) assert_equal(Decimal('0.1106908537142689284704528100'), rate) + @filter_deprecation def test_irr(self): v = [-150000, 15000, 25000, 35000, 45000, 60000] assert_almost_equal(np.irr(v), 0.0524, 2) @@ -43,20 +55,25 @@ class TestFinancial(object): v = [-1, -2, -3] assert_equal(np.irr(v), np.nan) + @filter_deprecation def test_pv(self): assert_almost_equal(np.pv(0.07, 20, 12000, 0), -127128.17, 2) + @filter_deprecation def test_pv_decimal(self): assert_equal(np.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0')), Decimal('-127128.1709461939327295222005')) + @filter_deprecation def test_fv(self): assert_equal(np.fv(0.075, 20, -2000, 0, 0), 86609.362673042924) + @filter_deprecation def test_fv_decimal(self): assert_equal(np.fv(Decimal('0.075'), Decimal('20'), Decimal('-2000'), 0, 0), Decimal('86609.36267304300040536731624')) + @filter_deprecation def test_pmt(self): res = np.pmt(0.08 / 12, 5 * 12, 15000) tgt = -304.145914 @@ -71,6 +88,7 @@ class TestFinancial(object): tgt = np.array([[-166.66667, -19311.258], [-626.90814, -19311.258]]) assert_allclose(res, tgt) + @filter_deprecation def test_pmt_decimal(self): res = np.pmt(Decimal('0.08') / Decimal('12'), 5 * 12, 15000) tgt = Decimal('-304.1459143262052370338701494') @@ -94,18 +112,22 @@ class TestFinancial(object): assert_equal(res[1][0], tgt[1][0]) assert_equal(res[1][1], tgt[1][1]) + @filter_deprecation def test_ppmt(self): assert_equal(np.round(np.ppmt(0.1 / 12, 1, 60, 55000), 2), -710.25) + @filter_deprecation def test_ppmt_decimal(self): assert_equal(np.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000')), Decimal('-710.2541257864217612489830917')) # Two tests showing how Decimal is actually getting at a more exact result # .23 / 12 does not come out nicely as a float but does as a decimal + @filter_deprecation def test_ppmt_special_rate(self): assert_equal(np.round(np.ppmt(0.23 / 12, 1, 60, 10000000000), 8), -90238044.232277036) + @filter_deprecation def test_ppmt_special_rate_decimal(self): # When rounded out to 8 decimal places like the float based test, this should not equal the same value # as the float, substituted for the decimal @@ -118,31 +140,38 @@ class TestFinancial(object): assert_equal(np.ppmt(Decimal('0.23') / Decimal('12'), 1, 60, Decimal('10000000000')), Decimal('-90238044.2322778884413969909')) + @filter_deprecation def test_ipmt(self): assert_almost_equal(np.round(np.ipmt(0.1 / 12, 1, 24, 2000), 2), -16.67) + @filter_deprecation def test_ipmt_decimal(self): result = np.ipmt(Decimal('0.1') / Decimal('12'), 1, 24, 2000) assert_equal(result.flat[0], Decimal('-16.66666666666666666666666667')) + @filter_deprecation def test_nper(self): assert_almost_equal(np.nper(0.075, -2000, 0, 100000.), 21.54, 2) + @filter_deprecation def test_nper2(self): assert_almost_equal(np.nper(0.0, -2000, 0, 100000.), 50.0, 1) + @filter_deprecation def test_npv(self): assert_almost_equal( np.npv(0.05, [-15000, 1500, 2500, 3500, 4500, 6000]), 122.89, 2) + @filter_deprecation def test_npv_decimal(self): assert_equal( np.npv(Decimal('0.05'), [-15000, 1500, 2500, 3500, 4500, 6000]), Decimal('122.894854950942692161628715')) + @filter_deprecation def test_mirr(self): val = [-4500, -800, 800, 800, 600, 600, 800, 800, 700, 3000] assert_almost_equal(np.mirr(val, 0.08, 0.055), 0.0666, 4) @@ -156,6 +185,7 @@ class TestFinancial(object): val = [39000, 30000, 21000, 37000, 46000] assert_(np.isnan(np.mirr(val, 0.10, 0.12))) + @filter_deprecation def test_mirr_decimal(self): val = [Decimal('-4500'), Decimal('-800'), Decimal('800'), Decimal('800'), Decimal('600'), Decimal('600'), Decimal('800'), Decimal('800'), @@ -174,6 +204,7 @@ class TestFinancial(object): val = [Decimal('39000'), Decimal('30000'), Decimal('21000'), Decimal('37000'), Decimal('46000')] assert_(np.isnan(np.mirr(val, Decimal('0.10'), Decimal('0.12')))) + @filter_deprecation def test_when(self): # begin assert_equal(np.rate(10, 20, -3500, 10000, 1), @@ -238,6 +269,7 @@ class TestFinancial(object): assert_equal(np.nper(0.075, -2000, 0, 100000., 0), np.nper(0.075, -2000, 0, 100000., 'end')) + @filter_deprecation def test_decimal_with_when(self): """Test that decimals are still supported if the when argument is passed""" # begin @@ -312,6 +344,7 @@ class TestFinancial(object): np.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'), Decimal('0'), 'end').flat[0]) + @filter_deprecation def test_broadcast(self): assert_almost_equal(np.nper(0.075, -2000, 0, 100000., [0, 1]), [21.5449442, 20.76156441], 4) @@ -329,6 +362,7 @@ class TestFinancial(object): [-74.998201, -75.62318601, -75.62318601, -76.88882405, -76.88882405], 4) + @filter_deprecation def test_broadcast_decimal(self): # Use almost equal because precision is tested in the explicit tests, this test is to ensure # broadcast with Decimal is not broken. diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index 062c21725..2dbaeb8cb 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - # doctest r''' Test the .npy file format. @@ -537,8 +535,10 @@ dt4 = np.dtype({'names': ['a', '', 'b'], 'formats': ['i4']*3}) # titles dt5 = np.dtype({'names': ['a', 'b'], 'formats': ['i4', 'i4'], 'offsets': [1, 6], 'titles': ['aa', 'bb']}) +# empty +dt6 = np.dtype({'names': [], 'formats': [], 'itemsize': 8}) -@pytest.mark.parametrize("dt", [dt1, dt2, dt3, dt4, dt5]) +@pytest.mark.parametrize("dt", [dt1, dt2, dt3, dt4, dt5, dt6]) def test_load_padded_dtype(dt): arr = np.zeros(3, dt) for i in range(3): @@ -550,10 +550,7 @@ def test_load_padded_dtype(dt): def test_python2_python3_interoperability(): - if sys.version_info[0] >= 3: - fname = 'win64python2.npy' - else: - fname = 'python3.npy' + fname = 'win64python2.npy' path = os.path.join(os.path.dirname(__file__), 'data', fname) data = np.load(path) assert_array_equal(data, np.ones(2)) @@ -563,13 +560,7 @@ def test_pickle_python2_python3(): # Python 2 and Python 3 and vice versa data_dir = os.path.join(os.path.dirname(__file__), 'data') - if sys.version_info[0] >= 3: - xrange = range - else: - import __builtin__ - xrange = __builtin__.xrange - - expected = np.array([None, xrange, u'\u512a\u826f', + expected = np.array([None, range, u'\u512a\u826f', b'\xe4\xb8\x8d\xe8\x89\xaf'], dtype=object) @@ -585,34 +576,30 @@ def test_pickle_python2_python3(): else: data = data_f - if sys.version_info[0] >= 3: - if encoding == 'latin1' and fname.startswith('py2'): - assert_(isinstance(data[3], str)) - assert_array_equal(data[:-1], expected[:-1]) - # mojibake occurs - assert_array_equal(data[-1].encode(encoding), expected[-1]) - else: - assert_(isinstance(data[3], bytes)) - assert_array_equal(data, expected) + if encoding == 'latin1' and fname.startswith('py2'): + assert_(isinstance(data[3], str)) + assert_array_equal(data[:-1], expected[:-1]) + # mojibake occurs + assert_array_equal(data[-1].encode(encoding), expected[-1]) else: + assert_(isinstance(data[3], bytes)) assert_array_equal(data, expected) - if sys.version_info[0] >= 3: - if fname.startswith('py2'): - if fname.endswith('.npz'): - data = np.load(path, allow_pickle=True) - assert_raises(UnicodeError, data.__getitem__, 'x') - data.close() - data = np.load(path, allow_pickle=True, fix_imports=False, - encoding='latin1') - assert_raises(ImportError, data.__getitem__, 'x') - data.close() - else: - assert_raises(UnicodeError, np.load, path, - allow_pickle=True) - assert_raises(ImportError, np.load, path, - allow_pickle=True, fix_imports=False, - encoding='latin1') + if fname.startswith('py2'): + if fname.endswith('.npz'): + data = np.load(path, allow_pickle=True) + assert_raises(UnicodeError, data.__getitem__, 'x') + data.close() + data = np.load(path, allow_pickle=True, fix_imports=False, + encoding='latin1') + assert_raises(ImportError, data.__getitem__, 'x') + data.close() + else: + assert_raises(UnicodeError, np.load, path, + allow_pickle=True) + assert_raises(ImportError, np.load, path, + allow_pickle=True, fix_imports=False, + encoding='latin1') def test_pickle_disallow(): @@ -963,3 +950,33 @@ def test_unicode_field_names(): with open(fname, 'wb') as f: with assert_warns(UserWarning): format.write_array(f, arr, version=None) + + +@pytest.mark.parametrize('dt, fail', [ + (np.dtype({'names': ['a', 'b'], 'formats': [float, np.dtype('S3', + metadata={'some': 'stuff'})]}), True), + (np.dtype(int, metadata={'some': 'stuff'}), False), + (np.dtype([('subarray', (int, (2,)))], metadata={'some': 'stuff'}), False), + # recursive: metadata on the field of a dtype + (np.dtype({'names': ['a', 'b'], 'formats': [ + float, np.dtype({'names': ['c'], 'formats': [np.dtype(int, metadata={})]}) + ]}), False) + ]) +def test_metadata_dtype(dt, fail): + # gh-14142 + arr = np.ones(10, dtype=dt) + buf = BytesIO() + with assert_warns(UserWarning): + np.save(buf, arr) + buf.seek(0) + if fail: + with assert_raises(ValueError): + np.load(buf) + else: + arr2 = np.load(buf) + # BUG: assert_array_equal does not check metadata + from numpy.lib.format import _has_metadata + assert_array_equal(arr, arr2) + assert _has_metadata(arr.dtype) + assert not _has_metadata(arr2.dtype) + diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 1eae8ccfb..23bf3296d 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1,10 +1,7 @@ -from __future__ import division, absolute_import, print_function - import operator import warnings import sys import decimal -import types from fractions import Fraction import pytest @@ -24,9 +21,6 @@ from numpy.lib import ( select, setxor1d, sinc, trapz, trim_zeros, unwrap, unique, vectorize ) -from numpy.compat import long - -PY2 = sys.version_info[0] == 2 def get_mat(n): data = np.arange(n) @@ -45,7 +39,7 @@ def _make_complex(real, imag): return ret -class TestRot90(object): +class TestRot90: def test_basic(self): assert_raises(ValueError, rot90, np.ones(4)) assert_raises(ValueError, rot90, np.ones((2,2,2)), axes=(0,1,2)) @@ -113,7 +107,7 @@ class TestRot90(object): rot90(a_rot90_20, k=k-1, axes=(2, 0))) -class TestFlip(object): +class TestFlip: def test_axes(self): assert_raises(np.AxisError, np.flip, np.ones(4), axis=1) @@ -216,7 +210,7 @@ class TestFlip(object): assert_equal(np.flip(a, axis=(1, 2)), c) -class TestAny(object): +class TestAny: def test_basic(self): y1 = [0, 0, 1, 0] @@ -233,7 +227,7 @@ class TestAny(object): assert_array_equal(np.sometrue(y1, axis=1), [0, 1, 1]) -class TestAll(object): +class TestAll: def test_basic(self): y1 = [0, 1, 1, 0] @@ -251,7 +245,7 @@ class TestAll(object): assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1]) -class TestCopy(object): +class TestCopy: def test_basic(self): a = np.array([[1, 2], [3, 4]]) @@ -278,8 +272,15 @@ class TestCopy(object): assert_(not a_fort_copy.flags.c_contiguous) assert_(a_fort_copy.flags.f_contiguous) + def test_subok(self): + mx = ma.ones(5) + assert_(not ma.isMaskedArray(np.copy(mx, subok=False))) + assert_(ma.isMaskedArray(np.copy(mx, subok=True))) + # Default behavior + assert_(not ma.isMaskedArray(np.copy(mx))) -class TestAverage(object): + +class TestAverage: def test_basic(self): y1 = np.array([1, 2, 3]) @@ -380,7 +381,7 @@ class TestAverage(object): w /= w.sum() assert_almost_equal(a.mean(0), average(a, weights=w)) -class TestSelect(object): +class TestSelect: choices = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])] @@ -442,7 +443,7 @@ class TestSelect(object): select(conditions, choices) -class TestInsert(object): +class TestInsert: def test_basic(self): a = [1, 2, 3] @@ -507,12 +508,11 @@ class TestInsert(object): insert(a, 1, a[:, 2, :], axis=1)) def test_0d(self): - # This is an error in the future a = np.array(1) - with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', '', DeprecationWarning) - assert_equal(insert(a, [], 2, axis=0), np.array(2)) - assert_(w[0].category is DeprecationWarning) + with pytest.raises(np.AxisError): + insert(a, [], 2, axis=0) + with pytest.raises(TypeError): + insert(a, [], 2, axis="nonsense") def test_subclass(self): class SubClass(np.ndarray): @@ -542,8 +542,14 @@ class TestInsert(object): b = np.insert(a, [0, 2], val) assert_array_equal(b[[0, 3]], np.array(val, dtype=b.dtype)) + def test_index_floats(self): + with pytest.raises(IndexError): + np.insert([0, 1, 2], np.array([1.0, 2.0]), [10, 20]) + with pytest.raises(IndexError): + np.insert([0, 1, 2], np.array([], dtype=float), []) -class TestAmax(object): + +class TestAmax: def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6.0] @@ -555,7 +561,7 @@ class TestAmax(object): assert_equal(np.amax(b, axis=1), [9.0, 10.0, 8.0]) -class TestAmin(object): +class TestAmin: def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6.0] @@ -567,7 +573,7 @@ class TestAmin(object): assert_equal(np.amin(b, axis=1), [3.0, 4.0, 2.0]) -class TestPtp(object): +class TestPtp: def test_basic(self): a = np.array([3, 4, 5, 10, -3, -5, 6.0]) @@ -582,7 +588,7 @@ class TestPtp(object): assert_equal(b.ptp(axis=(0,1), keepdims=True), [[8.0]]) -class TestCumsum(object): +class TestCumsum: def test_basic(self): ba = [1, 2, 10, 11, 6, 5, 4] @@ -605,7 +611,7 @@ class TestCumsum(object): assert_array_equal(np.cumsum(a2, axis=1), tgt) -class TestProd(object): +class TestProd: def test_basic(self): ba = [1, 2, 10, 11, 6, 5, 4] @@ -625,7 +631,7 @@ class TestProd(object): np.array([24, 1890, 600], ctype)) -class TestCumprod(object): +class TestCumprod: def test_basic(self): ba = [1, 2, 10, 11, 6, 5, 4] @@ -652,7 +658,7 @@ class TestCumprod(object): [10, 30, 120, 600]], ctype)) -class TestDiff(object): +class TestDiff: def test_basic(self): x = [1, 4, 6, 7, 12] @@ -792,7 +798,7 @@ class TestDiff(object): assert_raises(np.AxisError, diff, x, append=0, axis=3) -class TestDelete(object): +class TestDelete: def setup(self): self.a = np.arange(5) @@ -802,10 +808,6 @@ class TestDelete(object): a_del = delete(self.a, indices) nd_a_del = delete(self.nd_a, indices, axis=1) msg = 'Delete failed for obj: %r' % indices - # NOTE: The cast should be removed after warning phase for bools - 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, err_msg=msg) xor = setxor1d(nd_a_del[0,:, 0], self.nd_a[0, indices, 0]) @@ -821,19 +823,25 @@ class TestDelete(object): self._check_inverse_of_slicing(s) def test_fancy(self): - # Deprecation/FutureWarning tests should be kept after change. self._check_inverse_of_slicing(np.array([[0, 1], [2, 1]])) - with warnings.catch_warnings(): - warnings.filterwarnings('error', category=DeprecationWarning) - assert_raises(DeprecationWarning, delete, self.a, [100]) - assert_raises(DeprecationWarning, delete, self.a, [-100]) - with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', category=FutureWarning) - self._check_inverse_of_slicing([0, -1, 2, 2]) - obj = np.array([True, False, False], dtype=bool) - self._check_inverse_of_slicing(obj) - assert_(w[0].category is FutureWarning) - assert_(w[1].category is FutureWarning) + with pytest.raises(IndexError): + delete(self.a, [100]) + with pytest.raises(IndexError): + delete(self.a, [-100]) + + self._check_inverse_of_slicing([0, -1, 2, 2]) + + self._check_inverse_of_slicing([True, False, False, True, False]) + + # not legal, indexing with these would change the dimension + with pytest.raises(ValueError): + delete(self.a, True) + with pytest.raises(ValueError): + delete(self.a, False) + + # not enough items + with pytest.raises(ValueError): + delete(self.a, [False]*4) def test_single(self): self._check_inverse_of_slicing(0) @@ -841,10 +849,10 @@ class TestDelete(object): def test_0d(self): a = np.array(1) - with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', '', DeprecationWarning) - assert_equal(delete(a, [], axis=0), a) - assert_(w[0].category is DeprecationWarning) + with pytest.raises(np.AxisError): + delete(a, [], axis=0) + with pytest.raises(TypeError): + delete(a, [], axis="nonsense") def test_subclass(self): class SubClass(np.ndarray): @@ -866,8 +874,14 @@ class TestDelete(object): assert_equal(m.flags.c_contiguous, k.flags.c_contiguous) assert_equal(m.flags.f_contiguous, k.flags.f_contiguous) + def test_index_floats(self): + with pytest.raises(IndexError): + np.delete([0, 1, 2], np.array([1.0, 2.0])) + with pytest.raises(IndexError): + np.delete([0, 1, 2], np.array([], dtype=float)) -class TestGradient(object): + +class TestGradient: def test_basic(self): v = [[1, 1], [3, 4]] @@ -1084,8 +1098,42 @@ class TestGradient(object): assert_raises(ValueError, gradient, np.arange(1), edge_order=2) assert_raises(ValueError, gradient, np.arange(2), edge_order=2) - -class TestAngle(object): + @pytest.mark.parametrize('f_dtype', [np.uint8, np.uint16, + np.uint32, np.uint64]) + def test_f_decreasing_unsigned_int(self, f_dtype): + f = np.array([5, 4, 3, 2, 1], dtype=f_dtype) + g = gradient(f) + assert_array_equal(g, [-1]*len(f)) + + @pytest.mark.parametrize('f_dtype', [np.int8, np.int16, + np.int32, np.int64]) + def test_f_signed_int_big_jump(self, f_dtype): + maxint = np.iinfo(f_dtype).max + x = np.array([1, 3]) + f = np.array([-1, maxint], dtype=f_dtype) + dfdx = gradient(f, x) + assert_array_equal(dfdx, [(maxint + 1) // 2]*2) + + @pytest.mark.parametrize('x_dtype', [np.uint8, np.uint16, + np.uint32, np.uint64]) + def test_x_decreasing_unsigned(self, x_dtype): + x = np.array([3, 2, 1], dtype=x_dtype) + f = np.array([0, 2, 4]) + dfdx = gradient(f, x) + assert_array_equal(dfdx, [-2]*len(x)) + + @pytest.mark.parametrize('x_dtype', [np.int8, np.int16, + np.int32, np.int64]) + def test_x_signed_int_big_jump(self, x_dtype): + minint = np.iinfo(x_dtype).min + maxint = np.iinfo(x_dtype).max + x = np.array([-1, maxint], dtype=x_dtype) + f = np.array([minint // 2, 0]) + dfdx = gradient(f, x) + assert_array_equal(dfdx, [0.5, 0.5]) + + +class TestAngle: def test_basic(self): x = [1 + 3j, np.sqrt(2) / 2.0 + 1j * np.sqrt(2) / 2, @@ -1111,7 +1159,7 @@ class TestAngle(object): assert_equal(actual, expected) -class TestTrimZeros(object): +class TestTrimZeros: """ Only testing for integer splits. @@ -1134,7 +1182,7 @@ class TestTrimZeros(object): assert_array_equal(res, np.array([1, 0, 2, 3, 0, 4])) -class TestExtins(object): +class TestExtins: def test_basic(self): a = np.array([1, 3, 2, 1, 2, 3, 3]) @@ -1173,7 +1221,7 @@ class TestExtins(object): assert_array_equal(a, ac) -class TestVectorize(object): +class TestVectorize: def test_simple(self): def addsubtract(a, b): @@ -1505,8 +1553,8 @@ class TestVectorize(object): f(x) -class TestLeaks(object): - class A(object): +class TestLeaks: + class A: iters = 20 def bound(self, *args): @@ -1537,18 +1585,15 @@ class TestLeaks(object): a.f = np.frompyfunc(getattr(a, name), 1, 1) out = a.f(np.arange(10)) a = None - if PY2: - assert_equal(sys.getrefcount(A_func), refcount) - else: - # A.func is part of a reference cycle if incr is non-zero - assert_equal(sys.getrefcount(A_func), refcount + incr) + # A.func is part of a reference cycle if incr is non-zero + assert_equal(sys.getrefcount(A_func), refcount + incr) for i in range(5): gc.collect() assert_equal(sys.getrefcount(A_func), refcount) finally: gc.enable() -class TestDigitize(object): +class TestDigitize: def test_forward(self): x = np.arange(-6, 5) @@ -1633,7 +1678,7 @@ class TestDigitize(object): assert_equal(np.digitize(x, [x + 1, x - 1]), 1) -class TestUnwrap(object): +class TestUnwrap: def test_simple(self): # check that unwrap removes jumps greater that 2*pi @@ -1642,7 +1687,7 @@ class TestUnwrap(object): assert_(np.all(diff(unwrap(rand(10) * 100)) < np.pi)) -class TestFilterwindows(object): +class TestFilterwindows: def test_hanning(self): # check symmetry @@ -1673,7 +1718,7 @@ class TestFilterwindows(object): assert_almost_equal(np.sum(w, axis=0), 3.7800, 4) -class TestTrapz(object): +class TestTrapz: def test_simple(self): x = np.arange(-10, 10, .1) @@ -1735,7 +1780,7 @@ class TestTrapz(object): assert_almost_equal(trapz(y, xm), r) -class TestSinc(object): +class TestSinc: def test_simple(self): assert_(sinc(0) == 1) @@ -1752,7 +1797,7 @@ class TestSinc(object): assert_array_equal(y1, y3) -class TestUnique(object): +class TestUnique: def test_simple(self): x = np.array([4, 3, 2, 1, 1, 2, 3, 4, 0]) @@ -1764,7 +1809,7 @@ class TestUnique(object): assert_(np.all(unique(x) == [1 + 1j, 1 + 10j, 5 + 6j, 10])) -class TestCheckFinite(object): +class TestCheckFinite: def test_simple(self): a = [1, 2, 3] @@ -1781,7 +1826,7 @@ class TestCheckFinite(object): assert_(a.dtype == np.float64) -class TestCorrCoef(object): +class TestCorrCoef: A = np.array( [[0.15391142, 0.18045767, 0.14197213], [0.70461506, 0.96474128, 0.27906989], @@ -1866,14 +1911,14 @@ class TestCorrCoef(object): assert_(np.all(np.abs(c) <= 1.0)) -class TestCov(object): +class TestCov: x1 = np.array([[0, 2], [1, 1], [2, 0]]).T res1 = np.array([[1., -1.], [-1., 1.]]) x2 = np.array([0.0, 1.0, 2.0], ndmin=2) frequencies = np.array([1, 4, 1]) x2_repeats = np.array([[0.0], [1.0], [1.0], [1.0], [1.0], [2.0]]).T res2 = np.array([[0.4, -0.4], [-0.4, 0.4]]) - unit_frequencies = np.ones(3, dtype=np.integer) + unit_frequencies = np.ones(3, dtype=np.int_) weights = np.array([1.0, 4.0, 1.0]) res3 = np.array([[2. / 3., -2. / 3.], [-2. / 3., 2. / 3.]]) unit_weights = np.ones(3) @@ -1926,11 +1971,11 @@ class TestCov(object): self.res1) nonint = self.frequencies + 0.5 assert_raises(TypeError, cov, self.x1, fweights=nonint) - f = np.ones((2, 3), dtype=np.integer) + f = np.ones((2, 3), dtype=np.int_) assert_raises(RuntimeError, cov, self.x1, fweights=f) - f = np.ones(2, dtype=np.integer) + f = np.ones(2, dtype=np.int_) assert_raises(RuntimeError, cov, self.x1, fweights=f) - f = -1 * np.ones(3, dtype=np.integer) + f = -1 * np.ones(3, dtype=np.int_) assert_raises(ValueError, cov, self.x1, fweights=f) def test_aweights(self): @@ -1966,7 +2011,7 @@ class TestCov(object): self.res1) -class Test_I0(object): +class Test_I0: def test_simple(self): assert_almost_equal( @@ -2012,7 +2057,7 @@ class Test_I0(object): assert_array_equal(exp, res) -class TestKaiser(object): +class TestKaiser: def test_simple(self): assert_(np.isfinite(kaiser(1, 1.0))) @@ -2031,7 +2076,7 @@ class TestKaiser(object): kaiser(3, 4) -class TestMsort(object): +class TestMsort: def test_simple(self): A = np.array([[0.44567325, 0.79115165, 0.54900530], @@ -2044,7 +2089,7 @@ class TestMsort(object): [0.64864341, 0.79115165, 0.96098397]])) -class TestMeshgrid(object): +class TestMeshgrid: def test_simple(self): [X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7]) @@ -2133,7 +2178,7 @@ class TestMeshgrid(object): assert_equal(x[1, :], X) -class TestPiecewise(object): +class TestPiecewise: def test_simple(self): # Condition is single bool list @@ -2225,7 +2270,7 @@ class TestPiecewise(object): [3., 3., 1.]])) -class TestBincount(object): +class TestBincount: def test_simple(self): y = np.bincount(np.arange(4)) @@ -2312,7 +2357,7 @@ class TestBincount(object): assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount) -class TestInterp(object): +class TestInterp: def test_exceptions(self): assert_raises(ValueError, interp, 0, [], []) @@ -2511,7 +2556,7 @@ def compare_results(res, desired): assert_array_equal(res[i], desired[i]) -class TestPercentile(object): +class TestPercentile: def test_basic(self): x = np.arange(8) * 0.5 @@ -2523,7 +2568,7 @@ class TestPercentile(object): assert_equal(np.percentile(x, 0, interpolation='nearest'), np.nan) def test_fraction(self): - x = [Fraction(i, 2) for i in np.arange(8)] + x = [Fraction(i, 2) for i in range(8)] p = np.percentile(x, Fraction(0)) assert_equal(p, Fraction(0)) @@ -2932,7 +2977,7 @@ class TestPercentile(object): a, [0.3, 0.6], (0, 2), interpolation='nearest'), b) -class TestQuantile(object): +class TestQuantile: # most of this is already tested by TestPercentile def test_basic(self): @@ -2941,9 +2986,19 @@ class TestQuantile(object): assert_equal(np.quantile(x, 1), 3.5) assert_equal(np.quantile(x, 0.5), 1.75) + def test_correct_quantile_value(self): + a = np.array([True]) + tf_quant = np.quantile(True, False) + assert_equal(tf_quant, a[0]) + assert_equal(type(tf_quant), a.dtype) + a = np.array([False, True, True]) + quant_res = np.quantile(a, a) + assert_array_equal(quant_res, a) + assert_equal(a.dtype, quant_res.dtype) + def test_fraction(self): # fractional input, integral quantile - x = [Fraction(i, 2) for i in np.arange(8)] + x = [Fraction(i, 2) for i in range(8)] q = np.quantile(x, 0) assert_equal(q, 0) @@ -2974,7 +3029,7 @@ class TestQuantile(object): assert_array_equal(p, p0) -class TestMedian(object): +class TestMedian: def test_basic(self): a0 = np.array(1) @@ -3213,7 +3268,7 @@ class TestMedian(object): (1, 1, 7, 1)) -class TestAdd_newdoc_ufunc(object): +class TestAdd_newdoc_ufunc: def test_ufunc_arg(self): assert_raises(TypeError, add_newdoc_ufunc, 2, "blah") @@ -3223,7 +3278,7 @@ class TestAdd_newdoc_ufunc(object): assert_raises(TypeError, add_newdoc_ufunc, np.add, 3) -class TestAdd_newdoc(object): +class TestAdd_newdoc: @pytest.mark.skipif(sys.flags.optimize == 2, reason="Python running -OO") @pytest.mark.xfail(IS_PYPY, reason="PyPy does not modify tp_doc") @@ -3234,7 +3289,7 @@ class TestAdd_newdoc(object): assert_(len(np.core.ufunc.identity.__doc__) > 300) assert_(len(np.lib.index_tricks.mgrid.__doc__) > 300) -class TestSortComplex(object): +class TestSortComplex: @pytest.mark.parametrize("type_in, type_out", [ ('l', 'D'), diff --git a/numpy/lib/tests/test_histograms.py b/numpy/lib/tests/test_histograms.py index 4895a722c..fc16b7396 100644 --- a/numpy/lib/tests/test_histograms.py +++ b/numpy/lib/tests/test_histograms.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.lib.histograms import histogram, histogramdd, histogram_bin_edges @@ -8,9 +6,10 @@ from numpy.testing import ( assert_array_almost_equal, assert_raises, assert_allclose, assert_array_max_ulp, assert_raises_regex, suppress_warnings, ) +import pytest -class TestHistogram(object): +class TestHistogram: def setup(self): pass @@ -82,7 +81,7 @@ class TestHistogram(object): a, b = histogram(v, bins, density=False) assert_array_equal(a, [1, 2, 3, 4]) - # Variale bin widths are especially useful to deal with + # Variable bin widths are especially useful to deal with # infinities. v = np.arange(10) bins = [0, 1, 3, 6, np.inf] @@ -423,7 +422,7 @@ class TestHistogram(object): assert_array_equal(edges, e) -class TestHistogramOptimBinNums(object): +class TestHistogramOptimBinNums: """ Provide test coverage when using provided estimators for optimal number of bins @@ -591,6 +590,16 @@ class TestHistogramOptimBinNums(object): msg += " with datasize of {0}".format(testlen) assert_equal(len(a), numbins, err_msg=msg) + @pytest.mark.parametrize("bins", ['auto', 'fd', 'doane', 'scott', + 'stone', 'rice', 'sturges']) + def test_signed_integer_data(self, bins): + # Regression test for gh-14379. + a = np.array([-2, 0, 127], dtype=np.int8) + hist, edges = np.histogram(a, bins=bins) + hist32, edges32 = np.histogram(a.astype(np.int32), bins=bins) + assert_array_equal(hist, hist32) + assert_array_equal(edges, edges32) + def test_simple_weighted(self): """ Check that weighted data raises a TypeError @@ -601,7 +610,7 @@ class TestHistogramOptimBinNums(object): estimator, weights=[1, 2, 3]) -class TestHistogramdd(object): +class TestHistogramdd: def test_simple(self): x = np.array([[-.5, .5, 1.5], [-.5, 1.5, 2.5], [-.5, 2.5, .5], diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index dbe445c2c..905165a99 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest import numpy as np @@ -14,7 +12,7 @@ from numpy.lib.index_tricks import ( ) -class TestRavelUnravelIndex(object): +class TestRavelUnravelIndex: def test_basic(self): assert_equal(np.unravel_index(2, (2, 2)), (1, 0)) @@ -194,7 +192,7 @@ class TestRavelUnravelIndex(object): with assert_raises(ValueError): np.unravel_index([1], (2, 1, 0)) -class TestGrid(object): +class TestGrid: def test_basic(self): a = mgrid[-1:1:10j] b = mgrid[-1:1:0.1] @@ -252,7 +250,7 @@ class TestGrid(object): assert_equal(grid_small.size, expected[1]) -class TestConcatenator(object): +class TestConcatenator: def test_1d(self): assert_array_equal(r_[1, 2, 3, 4, 5, 6], np.array([1, 2, 3, 4, 5, 6])) b = np.ones(5) @@ -290,14 +288,14 @@ class TestConcatenator(object): assert_equal(r_[np.array(0), [1, 2, 3]], [0, 1, 2, 3]) -class TestNdenumerate(object): +class TestNdenumerate: def test_basic(self): a = np.array([[1, 2], [3, 4]]) assert_equal(list(ndenumerate(a)), [((0, 0), 1), ((0, 1), 2), ((1, 0), 3), ((1, 1), 4)]) -class TestIndexExpression(object): +class TestIndexExpression: def test_regression_1(self): # ticket #1196 a = np.arange(2) @@ -311,7 +309,7 @@ class TestIndexExpression(object): assert_equal(a[:, :3, [1, 2]], a[s_[:, :3, [1, 2]]]) -class TestIx_(object): +class TestIx_: def test_regression_1(self): # Test empty untyped inputs create outputs of indexing type, gh-5804 a, = np.ix_(range(0)) @@ -358,7 +356,7 @@ def test_c_(): assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]]) -class TestFillDiagonal(object): +class TestFillDiagonal: def test_basic(self): a = np.zeros((3, 3), int) fill_diagonal(a, 5) @@ -457,7 +455,7 @@ def test_diag_indices(): ) -class TestDiagIndicesFrom(object): +class TestDiagIndicesFrom: def test_diag_indices_from(self): x = np.random.random((4, 4)) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 1181fe986..8ce20a116 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -1,6 +1,5 @@ -from __future__ import division, absolute_import, print_function - import sys +import gc import gzip import os import threading @@ -9,21 +8,24 @@ import warnings import io import re import pytest +from pathlib import Path from tempfile import NamedTemporaryFile from io import BytesIO, StringIO from datetime import datetime import locale +from multiprocessing import Process import numpy as np import numpy.ma as ma from numpy.lib._iotools import ConverterError, ConversionWarning -from numpy.compat import asbytes, bytes, Path +from numpy.compat import asbytes, bytes from numpy.ma.testutils import assert_equal from numpy.testing import ( assert_warns, assert_, assert_raises_regex, assert_raises, assert_allclose, assert_array_equal, temppath, tempdir, IS_PYPY, HAS_REFCOUNT, suppress_warnings, assert_no_gc_cycles, assert_no_warnings ) +from numpy.testing._private.utils import requires_memory class TextIO(BytesIO): @@ -45,7 +47,6 @@ class TextIO(BytesIO): BytesIO.writelines(self, [asbytes(s) for s in lines]) -MAJVER, MINVER = sys.version_info[:2] IS_64BIT = sys.maxsize > 2**32 try: import bz2 @@ -70,7 +71,7 @@ def strptime(s, fmt=None): return datetime(*time.strptime(s, fmt)[:3]) -class RoundtripTest(object): +class RoundtripTest: def roundtrip(self, save_func, *args, **kwargs): """ save_func : callable @@ -277,8 +278,6 @@ class TestSavezLoad(RoundtripTest): fp.seek(0) assert_(not fp.closed) - #FIXME: Is this still true? - @pytest.mark.skipif(IS_PYPY, reason="Missing context manager on PyPy") def test_closing_fid(self): # Test that issue #1517 (too many opened files) remains closed # It might be a "weak" test since failed to get triggered on @@ -291,17 +290,18 @@ class TestSavezLoad(RoundtripTest): # numpy npz file returned by np.load when their reference count # goes to zero. Python 3 running in debug mode raises a # ResourceWarning when file closing is left to the garbage - # collector, so we catch the warnings. Because ResourceWarning - # is unknown in Python < 3.x, we take the easy way out and - # catch all warnings. + # collector, so we catch the warnings. with suppress_warnings() as sup: - sup.filter(Warning) # TODO: specify exact message + sup.filter(ResourceWarning) # TODO: specify exact message for i in range(1, 1025): try: np.load(tmp)["data"] except Exception as e: msg = "Failed to load data from a file: %s" % e raise AssertionError(msg) + finally: + if IS_PYPY: + gc.collect() def test_closing_zipfile_after_load(self): # Check that zipfile owns file and can close it. This needs to @@ -317,7 +317,7 @@ class TestSavezLoad(RoundtripTest): assert_(fp.closed) -class TestSaveTxt(object): +class TestSaveTxt: def test_array(self): a = np.array([[1, 2], [3, 4]], float) fmt = "%.18e" @@ -364,7 +364,6 @@ class TestSaveTxt(object): c.seek(0) assert_equal(c.readlines(), [b'1 3\n', b'4 6\n']) - @pytest.mark.skipif(Path is None, reason="No pathlib.Path") def test_multifield_view(self): a = np.ones(1, dtype=[('x', 'i4'), ('y', 'i4'), ('z', 'f4')]) v = a[['x', 'z']] @@ -518,7 +517,7 @@ class TestSaveTxt(object): def test_unicode(self): utf8 = b'\xcf\x96'.decode('UTF-8') - a = np.array([utf8], dtype=np.unicode) + a = np.array([utf8], dtype=np.unicode_) with tempdir() as tmpdir: # set encoding as on windows it may not be unicode even on py3 np.savetxt(os.path.join(tmpdir, 'test.csv'), a, fmt=['%s'], @@ -526,26 +525,24 @@ class TestSaveTxt(object): def test_unicode_roundtrip(self): utf8 = b'\xcf\x96'.decode('UTF-8') - a = np.array([utf8], dtype=np.unicode) + a = np.array([utf8], dtype=np.unicode_) # our gz wrapper support encoding suffixes = ['', '.gz'] - # stdlib 2 versions do not support encoding - if MAJVER > 2: - if HAS_BZ2: - suffixes.append('.bz2') - if HAS_LZMA: - suffixes.extend(['.xz', '.lzma']) + if HAS_BZ2: + suffixes.append('.bz2') + if HAS_LZMA: + suffixes.extend(['.xz', '.lzma']) with tempdir() as tmpdir: for suffix in suffixes: np.savetxt(os.path.join(tmpdir, 'test.csv' + suffix), a, fmt=['%s'], encoding='UTF-16-LE') b = np.loadtxt(os.path.join(tmpdir, 'test.csv' + suffix), - encoding='UTF-16-LE', dtype=np.unicode) + encoding='UTF-16-LE', dtype=np.unicode_) assert_array_equal(a, b) def test_unicode_bytestream(self): utf8 = b'\xcf\x96'.decode('UTF-8') - a = np.array([utf8], dtype=np.unicode) + a = np.array([utf8], dtype=np.unicode_) s = BytesIO() np.savetxt(s, a, fmt=['%s'], encoding='UTF-8') s.seek(0) @@ -553,7 +550,7 @@ class TestSaveTxt(object): def test_unicode_stringstream(self): utf8 = b'\xcf\x96'.decode('UTF-8') - a = np.array([utf8], dtype=np.unicode) + a = np.array([utf8], dtype=np.unicode_) s = StringIO() np.savetxt(s, a, fmt=['%s'], encoding='UTF-8') s.seek(0) @@ -572,22 +569,23 @@ class TestSaveTxt(object): else: assert_equal(s.read(), b"%f\n" % 1.) - @pytest.mark.skipif(sys.platform=='win32', - reason="large files cause problems") + @pytest.mark.skipif(sys.platform=='win32', reason="files>4GB may not work") @pytest.mark.slow + @requires_memory(free_bytes=7e9) def test_large_zip(self): - # The test takes at least 6GB of memory, writes a file larger than 4GB - try: - a = 'a' * 6 * 1024 * 1024 * 1024 - del a - except (MemoryError, OverflowError): - pytest.skip("Cannot allocate enough memory for test") - test_data = np.asarray([np.random.rand(np.random.randint(50,100),4) - for i in range(800000)]) - with tempdir() as tmpdir: - np.savez(os.path.join(tmpdir, 'test.npz'), test_data=test_data) - -class LoadTxtBase(object): + def check_large_zip(): + # The test takes at least 6GB of memory, writes a file larger than 4GB + test_data = np.asarray([np.random.rand(np.random.randint(50,100),4) + for i in range(800000)], dtype=object) + with tempdir() as tmpdir: + np.savez(os.path.join(tmpdir, 'test.npz'), test_data=test_data) + # run in a subprocess to ensure memory is released on PyPy, see gh-15775 + p = Process(target=check_large_zip) + p.start() + p.join() + assert p.exitcode == 0 + +class LoadTxtBase: def check_compressed(self, fopen, suffixes): # Test that we can load data from a compressed file wanted = np.arange(6).reshape((2, 3)) @@ -604,18 +602,14 @@ class LoadTxtBase(object): res = self.loadfunc(f) assert_array_equal(res, wanted) - # Python2 .open does not support encoding - @pytest.mark.skipif(MAJVER == 2, reason="Needs Python version >= 3") def test_compressed_gzip(self): self.check_compressed(gzip.open, ('.gz',)) @pytest.mark.skipif(not HAS_BZ2, reason="Needs bz2") - @pytest.mark.skipif(MAJVER == 2, reason="Needs Python version >= 3") def test_compressed_bz2(self): self.check_compressed(bz2.open, ('.bz2',)) @pytest.mark.skipif(not HAS_LZMA, reason="Needs lzma") - @pytest.mark.skipif(MAJVER == 2, reason="Needs Python version >= 3") def test_compressed_lzma(self): self.check_compressed(lzma.open, ('.xz', '.lzma')) @@ -632,12 +626,12 @@ class LoadTxtBase(object): with temppath() as path: with open(path, "wb") as f: f.write(nonascii.encode("UTF-16")) - x = self.loadfunc(path, encoding="UTF-16", dtype=np.unicode) + x = self.loadfunc(path, encoding="UTF-16", dtype=np.unicode_) assert_array_equal(x, nonascii) def test_binary_decode(self): utf16 = b'\xff\xfeh\x04 \x00i\x04 \x00j\x04' - v = self.loadfunc(BytesIO(utf16), dtype=np.unicode, encoding='UTF-16') + v = self.loadfunc(BytesIO(utf16), dtype=np.unicode_, encoding='UTF-16') assert_array_equal(v, np.array(utf16.decode('UTF-16').split())) def test_converters_decode(self): @@ -645,7 +639,7 @@ class LoadTxtBase(object): c = TextIO() c.write(b'\xcf\x96') c.seek(0) - x = self.loadfunc(c, dtype=np.unicode, + x = self.loadfunc(c, dtype=np.unicode_, converters={0: lambda x: x.decode('UTF-8')}) a = np.array([b'\xcf\x96'.decode('UTF-8')]) assert_array_equal(x, a) @@ -656,7 +650,7 @@ class LoadTxtBase(object): with temppath() as path: with io.open(path, 'wt', encoding='UTF-8') as f: f.write(utf8) - x = self.loadfunc(path, dtype=np.unicode, + x = self.loadfunc(path, dtype=np.unicode_, converters={0: lambda x: x + 't'}, encoding='UTF-8') a = np.array([utf8 + 't']) @@ -829,7 +823,7 @@ class TestLoadTxt(LoadTxtBase): assert_array_equal(x, a[:, 1]) # Testing with some crazy custom integer type - class CrazyInt(object): + class CrazyInt: def __index__(self): return 1 @@ -1104,7 +1098,7 @@ class TestLoadTxt(LoadTxtBase): with open(path, "wb") as f: f.write(butf8) with open(path, "rb") as f: - x = np.loadtxt(f, encoding="UTF-8", dtype=np.unicode) + x = np.loadtxt(f, encoding="UTF-8", dtype=np.unicode_) assert_array_equal(x, sutf8) # test broken latin1 conversion people now rely on with open(path, "rb") as f: @@ -1161,7 +1155,7 @@ class TestLoadTxt(LoadTxtBase): a = np.array([[1, 2, 3, 5], [4, 5, 7, 8], [2, 1, 4, 5]], int) assert_array_equal(x, a) -class Testfromregex(object): +class Testfromregex: def test_record(self): c = TextIO() c.write('1.312 foo\n1.534 bar\n4.444 qux') @@ -1587,7 +1581,7 @@ M 33 21.99 with open(path, 'wb') as f: f.write(b'skip,skip,2001-01-01' + utf8 + b',1.0,skip') test = np.genfromtxt(path, delimiter=",", names=None, dtype=float, - usecols=(2, 3), converters={2: np.unicode}, + usecols=(2, 3), converters={2: np.compat.unicode}, encoding='UTF-8') control = np.array([('2001-01-01' + utf8.decode('UTF-8'), 1.)], dtype=[('', '|U11'), ('', float)]) @@ -2126,7 +2120,7 @@ M 33 21.99 ctl = np.array([ ["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"], ["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"]], - dtype=np.unicode) + dtype=np.unicode_) assert_array_equal(test, ctl) # test a mixed dtype @@ -2169,7 +2163,7 @@ M 33 21.99 ["norm1", "norm2", "norm3"], ["norm1", latin1, "norm3"], ["test1", "testNonethe" + utf8, "test3"]], - dtype=np.unicode) + dtype=np.unicode_) assert_array_equal(test, ctl) def test_recfromtxt(self): @@ -2344,15 +2338,14 @@ M 33 21.99 assert_(test.dtype['f0'] == float) assert_(test.dtype['f1'] == np.int64) - assert_(test.dtype['f2'] == np.integer) + assert_(test.dtype['f2'] == np.int_) assert_allclose(test['f0'], 73786976294838206464.) assert_equal(test['f1'], 17179869184) assert_equal(test['f2'], 1024) -@pytest.mark.skipif(Path is None, reason="No pathlib.Path") -class TestPathUsage(object): +class TestPathUsage: # Test that pathlib.Path can be used def test_loadtxt(self): with temppath(suffix='.txt') as path: @@ -2485,7 +2478,7 @@ def test_gzip_load(): # These next two classes encode the minimal API needed to save()/load() arrays. # The `test_ducktyping` ensures they work correctly -class JustWriter(object): +class JustWriter: def __init__(self, base): self.base = base @@ -2495,7 +2488,7 @@ class JustWriter(object): def flush(self): return self.base.flush() -class JustReader(object): +class JustReader: def __init__(self, base): self.base = base diff --git a/numpy/lib/tests/test_mixins.py b/numpy/lib/tests/test_mixins.py index 3dd5346b6..632058763 100644 --- a/numpy/lib/tests/test_mixins.py +++ b/numpy/lib/tests/test_mixins.py @@ -1,16 +1,10 @@ -from __future__ import division, absolute_import, print_function - import numbers import operator -import sys import numpy as np from numpy.testing import assert_, assert_equal, assert_raises -PY2 = sys.version_info.major < 3 - - # NOTE: This class should be kept as an exact copy of the example from the # docstring for NDArrayOperatorsMixin. @@ -86,7 +80,6 @@ _ALL_BINARY_OPERATORS = [ operator.mul, operator.truediv, operator.floordiv, - # TODO: test div on Python 2, only operator.mod, divmod, pow, @@ -98,7 +91,7 @@ _ALL_BINARY_OPERATORS = [ ] -class TestNDArrayOperatorsMixin(object): +class TestNDArrayOperatorsMixin: def test_array_like_add(self): @@ -128,7 +121,7 @@ class TestNDArrayOperatorsMixin(object): def test_opt_out(self): - class OptOut(object): + class OptOut: """Object that opts out of __array_ufunc__.""" __array_ufunc__ = None @@ -204,11 +197,10 @@ class TestNDArrayOperatorsMixin(object): array_like = ArrayLike(array) expected = ArrayLike(np.float64(5)) _assert_equal_type_and_value(expected, np.matmul(array_like, array)) - if not PY2: - _assert_equal_type_and_value( - expected, operator.matmul(array_like, array)) - _assert_equal_type_and_value( - expected, operator.matmul(array, array_like)) + _assert_equal_type_and_value( + expected, operator.matmul(array_like, array)) + _assert_equal_type_and_value( + expected, operator.matmul(array, array_like)) def test_ufunc_at(self): array = ArrayLike(np.array([1, 2, 3, 4])) diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index b7261c63f..db563e30c 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -1,10 +1,8 @@ -from __future__ import division, absolute_import, print_function - import warnings import pytest import numpy as np -from numpy.lib.nanfunctions import _nan_mask +from numpy.lib.nanfunctions import _nan_mask, _replace_nan from numpy.testing import ( assert_, assert_equal, assert_almost_equal, assert_no_warnings, assert_raises, assert_array_equal, suppress_warnings @@ -37,7 +35,7 @@ _ndat_zeros = np.array([[0.6244, 0.0, 0.2692, 0.0116, 0.0, 0.1170], [0.1610, 0.0, 0.0, 0.1859, 0.3146, 0.0]]) -class TestNanFunctions_MinMax(object): +class TestNanFunctions_MinMax: nanfuncs = [np.nanmin, np.nanmax] stdfuncs = [np.min, np.max] @@ -171,7 +169,7 @@ class TestNanFunctions_MinMax(object): assert_(issubclass(w[0].category, RuntimeWarning)) -class TestNanFunctions_ArgminArgmax(object): +class TestNanFunctions_ArgminArgmax: nanfuncs = [np.nanargmin, np.nanargmax] @@ -233,7 +231,7 @@ class TestNanFunctions_ArgminArgmax(object): assert_(res.shape == ()) -class TestNanFunctions_IntTypes(object): +class TestNanFunctions_IntTypes: int_types = (np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64) @@ -308,7 +306,7 @@ class TestNanFunctions_IntTypes(object): assert_equal(np.nanstd(mat, ddof=1), tgt) -class SharedNanFunctionsTestsMixin(object): +class SharedNanFunctionsTestsMixin: def test_mutation(self): # Check that passed array is not modified. ndat = _ndat.copy() @@ -590,7 +588,7 @@ class TestNanFunctions_MeanVarStd(SharedNanFunctionsTestsMixin): assert_(len(w) == 0) -class TestNanFunctions_Median(object): +class TestNanFunctions_Median: def test_mutation(self): # Check that passed array is not modified. @@ -754,7 +752,7 @@ class TestNanFunctions_Median(object): ([np.nan] * i) + [-inf] * j) -class TestNanFunctions_Percentile(object): +class TestNanFunctions_Percentile: def test_mutation(self): # Check that passed array is not modified. @@ -893,7 +891,7 @@ class TestNanFunctions_Percentile(object): assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6)) -class TestNanFunctions_Quantile(object): +class TestNanFunctions_Quantile: # most of this is already tested by TestPercentile def test_regression(self): @@ -953,3 +951,30 @@ def test__nan_mask(arr, expected): # for types that can't possibly contain NaN if type(expected) is not np.ndarray: assert actual is True + + +def test__replace_nan(): + """ Test that _replace_nan returns the original array if there are no + NaNs, not a copy. + """ + for dtype in [np.bool, np.int32, np.int64]: + arr = np.array([0, 1], dtype=dtype) + result, mask = _replace_nan(arr, 0) + assert mask is None + # do not make a copy if there are no nans + assert result is arr + + for dtype in [np.float32, np.float64]: + arr = np.array([0, 1], dtype=dtype) + result, mask = _replace_nan(arr, 2) + assert (mask == False).all() + # mask is not None, so we make a copy + assert result is not arr + assert_equal(result, arr) + + arr_nan = np.array([0, 1, np.nan], dtype=dtype) + result_nan, mask_nan = _replace_nan(arr_nan, 2) + assert_equal(mask_nan, np.array([False, False, True])) + assert result_nan is not arr_nan + assert_equal(result_nan, np.array([0, 1, 2])) + assert np.isnan(arr_nan[-1]) diff --git a/numpy/lib/tests/test_packbits.py b/numpy/lib/tests/test_packbits.py index 95a465c36..5b07f41c6 100644 --- a/numpy/lib/tests/test_packbits.py +++ b/numpy/lib/tests/test_packbits.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_array_equal, assert_equal, assert_raises import pytest diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py index 89759bd83..cd0b90dc4 100644 --- a/numpy/lib/tests/test_polynomial.py +++ b/numpy/lib/tests/test_polynomial.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_almost_equal, @@ -7,7 +5,7 @@ from numpy.testing import ( ) -class TestPolynomial(object): +class TestPolynomial: def test_poly1d_str_and_repr(self): p = np.poly1d([1., 2, 3]) assert_equal(repr(p), 'poly1d([1., 2., 3.])') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index fa5f4dec2..2f3c14df3 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest import numpy as np @@ -19,7 +17,7 @@ zip_descr = np.lib.recfunctions._zip_descr zip_dtype = np.lib.recfunctions._zip_dtype -class TestRecFunctions(object): +class TestRecFunctions: # Misc tests def setup(self): @@ -348,7 +346,7 @@ class TestRecFunctions(object): assert_equal(b[()], 3) -class TestRecursiveFillFields(object): +class TestRecursiveFillFields: # Test recursive_fill_fields. def test_simple_flexible(self): # Test recursive_fill_fields on flexible-array @@ -371,7 +369,7 @@ class TestRecursiveFillFields(object): assert_equal(test, control) -class TestMergeArrays(object): +class TestMergeArrays: # Test merge_arrays def setup(self): @@ -504,7 +502,7 @@ class TestMergeArrays(object): assert_equal(test, control) -class TestAppendFields(object): +class TestAppendFields: # Test append_fields def setup(self): @@ -558,7 +556,7 @@ class TestAppendFields(object): assert_equal(test, control) -class TestStackArrays(object): +class TestStackArrays: # Test stack_arrays def setup(self): x = np.array([1, 2, ]) @@ -729,7 +727,7 @@ class TestStackArrays(object): assert_equal(res.mask, expected.mask) -class TestJoinBy(object): +class TestJoinBy: def setup(self): self.a = np.array(list(zip(np.arange(10), np.arange(50, 60), np.arange(100, 110))), @@ -772,7 +770,6 @@ class TestJoinBy(object): def test_join_subdtype(self): # tests the bug in https://stackoverflow.com/q/44769632/102441 - from numpy.lib import recfunctions as rfn foo = np.array([(1,)], dtype=[('key', int)]) bar = np.array([(1, np.array([1,2,3]))], @@ -895,7 +892,7 @@ class TestJoinBy(object): assert_equal(res.dtype, expected_dtype) -class TestJoinBy2(object): +class TestJoinBy2: @classmethod def setup(cls): cls.a = np.array(list(zip(np.arange(10), np.arange(50, 60), @@ -960,7 +957,7 @@ class TestJoinBy2(object): assert_equal(test.dtype, control.dtype) assert_equal(test, control) -class TestAppendFieldsObj(object): +class TestAppendFieldsObj: """ Test append_fields with arrays containing objects """ diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 4cd812f5d..55df2a675 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -1,17 +1,13 @@ -from __future__ import division, absolute_import, print_function - import os -import sys import numpy as np from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_array_almost_equal, assert_raises, _assert_valid_refcount, ) -from numpy.compat import unicode -class TestRegression(object): +class TestRegression: def test_poly1d(self): # Ticket #28 assert_equal(np.poly1d([1]) - np.poly1d([1, 0]), @@ -183,7 +179,7 @@ class TestRegression(object): # related to ticket #1405. include_dirs = [np.get_include()] for path in include_dirs: - assert_(isinstance(path, (str, unicode))) + assert_(isinstance(path, str)) assert_(path != '') def test_polyder_return_type(self): @@ -208,10 +204,7 @@ class TestRegression(object): def test_loadtxt_fields_subarrays(self): # For ticket #1936 - if sys.version_info[0] >= 3: - from io import StringIO - else: - from StringIO import StringIO + from io import StringIO dt = [("a", 'u1', 2), ("b", 'u1', 2)] x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt) diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py index 01ea028bb..fb7ba7874 100644 --- a/numpy/lib/tests/test_shape_base.py +++ b/numpy/lib/tests/test_shape_base.py @@ -1,7 +1,4 @@ -from __future__ import division, absolute_import, print_function - import numpy as np -import warnings import functools import sys import pytest @@ -30,7 +27,7 @@ def _add_keepdims(func): return wrapped -class TestTakeAlongAxis(object): +class TestTakeAlongAxis: def test_argequivalent(self): """ Test it translates from arg<func> to <func> """ from numpy.random import rand @@ -82,7 +79,7 @@ class TestTakeAlongAxis(object): assert_equal(actual.shape, (3, 2, 5)) -class TestPutAlongAxis(object): +class TestPutAlongAxis: def test_replace_max(self): a_base = np.array([[10, 30, 20], [60, 40, 50]]) @@ -107,7 +104,7 @@ class TestPutAlongAxis(object): assert_equal(take_along_axis(a, ai, axis=1), 20) -class TestApplyAlongAxis(object): +class TestApplyAlongAxis: def test_simple(self): a = np.ones((20, 10), 'd') assert_array_equal( @@ -273,14 +270,14 @@ class TestApplyAlongAxis(object): assert_equal(type(actual[i]), type(expected[i])) -class TestApplyOverAxes(object): +class TestApplyOverAxes: def test_simple(self): a = np.arange(24).reshape(2, 3, 4) aoa_a = apply_over_axes(np.sum, a, [0, 2]) assert_array_equal(aoa_a, np.array([[[60], [92], [124]]])) -class TestExpandDims(object): +class TestExpandDims: def test_functionality(self): s = (2, 3, 4, 5) a = np.empty(s) @@ -289,14 +286,26 @@ class TestExpandDims(object): assert_(b.shape[axis] == 1) assert_(np.squeeze(b).shape == s) - def test_deprecations(self): - # 2017-05-17, 1.13.0 + def test_axis_tuple(self): + a = np.empty((3, 3, 3)) + assert np.expand_dims(a, axis=(0, 1, 2)).shape == (1, 1, 1, 3, 3, 3) + assert np.expand_dims(a, axis=(0, -1, -2)).shape == (1, 3, 3, 3, 1, 1) + assert np.expand_dims(a, axis=(0, 3, 5)).shape == (1, 3, 3, 1, 3, 1) + assert np.expand_dims(a, axis=(0, -3, -5)).shape == (1, 1, 3, 1, 3, 3) + + def test_axis_out_of_range(self): s = (2, 3, 4, 5) a = np.empty(s) - with warnings.catch_warnings(): - warnings.simplefilter("always") - assert_warns(DeprecationWarning, expand_dims, a, -6) - assert_warns(DeprecationWarning, expand_dims, a, 5) + assert_raises(np.AxisError, expand_dims, a, -6) + assert_raises(np.AxisError, expand_dims, a, 5) + + a = np.empty((3, 3, 3)) + assert_raises(np.AxisError, expand_dims, a, (0, -6)) + assert_raises(np.AxisError, expand_dims, a, (0, 5)) + + def test_repeated_axis(self): + a = np.empty((3, 3, 3)) + assert_raises(ValueError, expand_dims, a, axis=(1, 1)) def test_subclasses(self): a = np.arange(10).reshape((2, 5)) @@ -308,7 +317,7 @@ class TestExpandDims(object): assert_equal(expanded.mask.shape, (2, 1, 5)) -class TestArraySplit(object): +class TestArraySplit: def test_integer_0_split(self): a = np.arange(10) assert_raises(ValueError, array_split, a, 0) @@ -442,7 +451,7 @@ class TestArraySplit(object): compare_results(res, desired) -class TestSplit(object): +class TestSplit: # The split function is essentially the same as array_split, # except that it test if splitting will result in an # equal split. Only test for this case. @@ -458,7 +467,7 @@ class TestSplit(object): assert_raises(ValueError, split, a, 3) -class TestColumnStack(object): +class TestColumnStack: def test_non_iterable(self): assert_raises(TypeError, column_stack, 1) @@ -487,7 +496,7 @@ class TestColumnStack(object): column_stack((np.arange(3) for _ in range(2))) -class TestDstack(object): +class TestDstack: def test_non_iterable(self): assert_raises(TypeError, dstack, 1) @@ -526,7 +535,7 @@ class TestDstack(object): # array_split has more comprehensive test of splitting. # only do simple test on hsplit, vsplit, and dsplit -class TestHsplit(object): +class TestHsplit: """Only testing for integer splits. """ @@ -555,7 +564,7 @@ class TestHsplit(object): compare_results(res, desired) -class TestVsplit(object): +class TestVsplit: """Only testing for integer splits. """ @@ -582,7 +591,7 @@ class TestVsplit(object): compare_results(res, desired) -class TestDsplit(object): +class TestDsplit: # Only testing for integer splits. def test_non_iterable(self): assert_raises(ValueError, dsplit, 1, 1) @@ -615,7 +624,7 @@ class TestDsplit(object): compare_results(res, desired) -class TestSqueeze(object): +class TestSqueeze: def test_basic(self): from numpy.random import rand @@ -634,7 +643,7 @@ class TestSqueeze(object): assert_equal(type(res), np.ndarray) -class TestKron(object): +class TestKron: def test_return_type(self): class myarray(np.ndarray): __array_priority__ = 0.0 @@ -647,7 +656,7 @@ class TestKron(object): assert_equal(type(kron(ma, a)), myarray) -class TestTile(object): +class TestTile: def test_basic(self): a = np.array([0, 1, 2]) b = [[1, 2], [3, 4]] @@ -687,7 +696,7 @@ class TestTile(object): assert_equal(large, klarge) -class TestMayShareMemory(object): +class TestMayShareMemory: def test_basic(self): d = np.ones((50, 60)) d2 = np.ones((30, 60, 6)) diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py index 85fcceedc..9d95eb9d0 100644 --- a/numpy/lib/tests/test_stride_tricks.py +++ b/numpy/lib/tests/test_stride_tricks.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.core._rational_tests import rational from numpy.testing import ( @@ -65,8 +63,7 @@ def test_broadcast_kwargs(): x = np.arange(10) y = np.arange(10) - with assert_raises_regex(TypeError, - r'broadcast_arrays\(\) got an unexpected keyword*'): + with assert_raises_regex(TypeError, 'got an unexpected keyword'): broadcast_arrays(x, y, dtype='float64') @@ -356,14 +353,12 @@ def as_strided_writeable(): class VerySimpleSubClass(np.ndarray): def __new__(cls, *args, **kwargs): - kwargs['subok'] = True - return np.array(*args, **kwargs).view(cls) + return np.array(*args, subok=True, **kwargs).view(cls) class SimpleSubClass(VerySimpleSubClass): def __new__(cls, *args, **kwargs): - kwargs['subok'] = True - self = np.array(*args, **kwargs).view(cls) + self = np.array(*args, subok=True, **kwargs).view(cls) self.info = 'simple' return self diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py index bb844e4bd..cce683bfe 100644 --- a/numpy/lib/tests/test_twodim_base.py +++ b/numpy/lib/tests/test_twodim_base.py @@ -1,8 +1,6 @@ """Test functions for matrix module """ -from __future__ import division, absolute_import, print_function - from numpy.testing import ( assert_equal, assert_array_equal, assert_array_max_ulp, assert_array_almost_equal, assert_raises, assert_ @@ -26,7 +24,7 @@ def get_mat(n): return data -class TestEye(object): +class TestEye: def test_basic(self): assert_equal(eye(4), array([[1, 0, 0, 0], @@ -108,7 +106,7 @@ class TestEye(object): assert mat_f.flags.f_contiguous -class TestDiag(object): +class TestDiag: def test_vector(self): vals = (100 * arange(5)).astype('l') b = zeros((5, 5)) @@ -155,7 +153,7 @@ class TestDiag(object): assert_raises(ValueError, diag, [[[1]]]) -class TestFliplr(object): +class TestFliplr: def test_basic(self): assert_raises(ValueError, fliplr, ones(4)) a = get_mat(4) @@ -168,7 +166,7 @@ class TestFliplr(object): assert_equal(fliplr(a), b) -class TestFlipud(object): +class TestFlipud: def test_basic(self): a = get_mat(4) b = a[::-1, :] @@ -180,7 +178,7 @@ class TestFlipud(object): assert_equal(flipud(a), b) -class TestHistogram2d(object): +class TestHistogram2d: def test_simple(self): x = array( [0.41702200, 0.72032449, 1.1437481e-4, 0.302332573, 0.146755891]) @@ -298,7 +296,7 @@ class TestHistogram2d(object): assert_(r, ((ShouldDispatch,), (xy, xy), dict(weights=s_d))) -class TestTri(object): +class TestTri: def test_dtype(self): out = array([[1, 0, 0], [1, 1, 0], @@ -436,7 +434,7 @@ def test_tril_indices(): [-10, -10, -10, -10, -10]])) -class TestTriuIndices(object): +class TestTriuIndices: def test_triu_indices(self): iu1 = triu_indices(4) iu2 = triu_indices(4, k=2) @@ -486,21 +484,21 @@ class TestTriuIndices(object): [16, 17, 18, -1, -1]])) -class TestTrilIndicesFrom(object): +class TestTrilIndicesFrom: def test_exceptions(self): assert_raises(ValueError, tril_indices_from, np.ones((2,))) assert_raises(ValueError, tril_indices_from, np.ones((2, 2, 2))) # assert_raises(ValueError, tril_indices_from, np.ones((2, 3))) -class TestTriuIndicesFrom(object): +class TestTriuIndicesFrom: def test_exceptions(self): assert_raises(ValueError, triu_indices_from, np.ones((2,))) assert_raises(ValueError, triu_indices_from, np.ones((2, 2, 2))) # assert_raises(ValueError, triu_indices_from, np.ones((2, 3))) -class TestVander(object): +class TestVander: def test_basic(self): c = np.array([0, 1, -2, 3]) v = vander(c) diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py index b3f114b92..3f4ca6309 100644 --- a/numpy/lib/tests/test_type_check.py +++ b/numpy/lib/tests/test_type_check.py @@ -1,7 +1,4 @@ -from __future__ import division, absolute_import, print_function - import numpy as np -from numpy.compat import long from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_raises ) @@ -15,7 +12,7 @@ def assert_all(x): assert_(np.all(x), x) -class TestCommonType(object): +class TestCommonType: def test_basic(self): ai32 = np.array([[1, 2], [3, 4]], dtype=np.int32) af16 = np.array([[1, 2], [3, 4]], dtype=np.float16) @@ -31,7 +28,7 @@ class TestCommonType(object): assert_(common_type(acd) == np.cdouble) -class TestMintypecode(object): +class TestMintypecode: def test_default_1(self): for itype in '1bcsuwil': @@ -81,18 +78,17 @@ class TestMintypecode(object): assert_equal(mintypecode('idD'), 'D') -class TestIsscalar(object): +class TestIsscalar: def test_basic(self): assert_(np.isscalar(3)) assert_(not np.isscalar([3])) assert_(not np.isscalar((3,))) assert_(np.isscalar(3j)) - assert_(np.isscalar(long(10))) assert_(np.isscalar(4.0)) -class TestReal(object): +class TestReal: def test_real(self): y = np.random.rand(10,) @@ -123,7 +119,7 @@ class TestReal(object): assert_(not isinstance(out, np.ndarray)) -class TestImag(object): +class TestImag: def test_real(self): y = np.random.rand(10,) @@ -154,7 +150,7 @@ class TestImag(object): assert_(not isinstance(out, np.ndarray)) -class TestIscomplex(object): +class TestIscomplex: def test_fail(self): z = np.array([-1, 0, 1]) @@ -167,7 +163,7 @@ class TestIscomplex(object): assert_array_equal(res, [1, 0, 0]) -class TestIsreal(object): +class TestIsreal: def test_pass(self): z = np.array([-1, 0, 1j]) @@ -180,7 +176,7 @@ class TestIsreal(object): assert_array_equal(res, [0, 1, 1]) -class TestIscomplexobj(object): +class TestIscomplexobj: def test_basic(self): z = np.array([-1, 0, 1]) @@ -209,7 +205,7 @@ class TestIscomplexobj(object): # (pandas.core.dtypes) class PdComplex(np.complex128): pass - class PdDtype(object): + class PdDtype: name = 'category' names = None type = PdComplex @@ -233,7 +229,7 @@ class TestIscomplexobj(object): assert_(iscomplexobj(a)) -class TestIsrealobj(object): +class TestIsrealobj: def test_basic(self): z = np.array([-1, 0, 1]) assert_(isrealobj(z)) @@ -241,7 +237,7 @@ class TestIsrealobj(object): assert_(not isrealobj(z)) -class TestIsnan(object): +class TestIsnan: def test_goodvalues(self): z = np.array((-1., 0., 1.)) @@ -271,7 +267,7 @@ class TestIsnan(object): assert_all(np.isnan(np.array(0+0j)/0.) == 1) -class TestIsfinite(object): +class TestIsfinite: # Fixme, wrong place, isfinite now ufunc def test_goodvalues(self): @@ -302,7 +298,7 @@ class TestIsfinite(object): assert_all(np.isfinite(np.array(1+1j)/0.) == 0) -class TestIsinf(object): +class TestIsinf: # Fixme, wrong place, isinf now ufunc def test_goodvalues(self): @@ -331,7 +327,7 @@ class TestIsinf(object): assert_all(np.isinf(np.array((0.,))/0.) == 0) -class TestIsposinf(object): +class TestIsposinf: def test_generic(self): with np.errstate(divide='ignore', invalid='ignore'): @@ -341,7 +337,7 @@ class TestIsposinf(object): assert_(vals[2] == 1) -class TestIsneginf(object): +class TestIsneginf: def test_generic(self): with np.errstate(divide='ignore', invalid='ignore'): @@ -351,7 +347,7 @@ class TestIsneginf(object): assert_(vals[2] == 0) -class TestNanToNum(object): +class TestNanToNum: def test_generic(self): with np.errstate(divide='ignore', invalid='ignore'): @@ -456,7 +452,7 @@ class TestNanToNum(object): assert_equal(type(vals), np.ndarray) -class TestRealIfClose(object): +class TestRealIfClose: def test_basic(self): a = np.random.rand(10) @@ -469,7 +465,7 @@ class TestRealIfClose(object): assert_all(isrealobj(b)) -class TestArrayConversion(object): +class TestArrayConversion: def test_asfarray(self): a = asfarray(np.array([1, 2, 3])) diff --git a/numpy/lib/tests/test_ufunclike.py b/numpy/lib/tests/test_ufunclike.py index 0f06876a1..c280b6969 100644 --- a/numpy/lib/tests/test_ufunclike.py +++ b/numpy/lib/tests/test_ufunclike.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np import numpy.core as nx import numpy.lib.ufunclike as ufl @@ -8,7 +6,7 @@ from numpy.testing import ( ) -class TestUfunclike(object): +class TestUfunclike: def test_isposinf(self): a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0]) @@ -21,7 +19,7 @@ class TestUfunclike(object): assert_equal(res, tgt) assert_equal(out, tgt) - a = a.astype(np.complex) + a = a.astype(np.complex_) with assert_raises(TypeError): ufl.isposinf(a) @@ -36,7 +34,7 @@ class TestUfunclike(object): assert_equal(res, tgt) assert_equal(out, tgt) - a = a.astype(np.complex) + a = a.astype(np.complex_) with assert_raises(TypeError): ufl.isneginf(a) diff --git a/numpy/lib/tests/test_utils.py b/numpy/lib/tests/test_utils.py index 9673a05fa..c96bf795a 100644 --- a/numpy/lib/tests/test_utils.py +++ b/numpy/lib/tests/test_utils.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import inspect import sys import pytest @@ -9,10 +7,7 @@ from numpy.testing import assert_, assert_equal, assert_raises_regex from numpy.lib import deprecate import numpy.lib.utils as utils -if sys.version_info[0] >= 3: - from io import StringIO -else: - from StringIO import StringIO +from io import StringIO @pytest.mark.skipif(sys.flags.optimize == 2, reason="Python running -OO") @@ -102,7 +97,7 @@ def test_safe_eval_nameconstant(): utils.safe_eval('None') -class TestByteBounds(object): +class TestByteBounds: def test_byte_bounds(self): # pointer difference matches size * itemsize |