summaryrefslogtreecommitdiff
path: root/numpy/lib/tests
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r--numpy/lib/tests/test_arraysetops.py18
-rw-r--r--numpy/lib/tests/test_financial.py380
-rw-r--r--numpy/lib/tests/test_financial_expired.py13
-rw-r--r--numpy/lib/tests/test_format.py138
-rw-r--r--numpy/lib/tests/test_function_base.py95
-rw-r--r--numpy/lib/tests/test_index_tricks.py42
-rw-r--r--numpy/lib/tests/test_io.py88
-rw-r--r--numpy/lib/tests/test_polynomial.py23
-rw-r--r--numpy/lib/tests/test_stride_tricks.py172
-rw-r--r--numpy/lib/tests/test_utils.py19
10 files changed, 479 insertions, 509 deletions
diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py
index 81ba789e3..847e6cb8a 100644
--- a/numpy/lib/tests/test_arraysetops.py
+++ b/numpy/lib/tests/test_arraysetops.py
@@ -125,32 +125,36 @@ class TestSetOps:
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", [
+ @pytest.mark.parametrize("ary, prepend, append, expected", [
# should fail because trying to cast
# np.nan standard floating point value
# into an integer array:
(np.array([1, 2, 3], dtype=np.int64),
None,
- np.nan),
+ np.nan,
+ 'to_end'),
# should fail because attempting
# to downcast to int type:
(np.array([1, 2, 3], dtype=np.int64),
np.array([5, 7, 2], dtype=np.float32),
- None),
+ None,
+ 'to_begin'),
# should fail because attempting to cast
# two special floating point values
- # to integers (on both sides of ary):
+ # to integers (on both sides of ary),
+ # `to_begin` is in the error message as the impl checks this first:
(np.array([1., 3., 9.], dtype=np.int8),
np.nan,
- np.nan),
+ np.nan,
+ 'to_begin'),
])
- def test_ediff1d_forbidden_type_casts(self, ary, prepend, append):
+ def test_ediff1d_forbidden_type_casts(self, ary, prepend, append, expected):
# verify resolution of gh-11490
# specifically, raise an appropriate
# Exception when attempting to append or
# prepend with an incompatible type
- msg = 'must be compatible'
+ msg = 'dtype of `{}` must be compatible'.format(expected)
with assert_raises_regex(TypeError, msg):
ediff1d(ary=ary,
to_end=append,
diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py
deleted file mode 100644
index 26e79bc06..000000000
--- a/numpy/lib/tests/test_financial.py
+++ /dev/null
@@ -1,380 +0,0 @@
-import warnings
-from decimal import Decimal
-
-import numpy as np
-from numpy.testing import (
- assert_, assert_almost_equal, assert_allclose, assert_equal, assert_raises
- )
-
-
-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)
- v = [-100, 0, 0, 74]
- assert_almost_equal(np.irr(v), -0.0955, 2)
- v = [-100, 39, 59, 55, 20]
- assert_almost_equal(np.irr(v), 0.28095, 2)
- v = [-100, 100, 0, -7]
- assert_almost_equal(np.irr(v), -0.0833, 2)
- v = [-100, 100, 0, 7]
- assert_almost_equal(np.irr(v), 0.06206, 2)
- v = [-5, 10.5, 1, -8, 1]
- assert_almost_equal(np.irr(v), 0.0886, 2)
-
- # Test that if there is no solution then np.irr returns nan
- # Fixes gh-6744
- 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
- assert_allclose(res, tgt)
- # Test the edge case where rate == 0.0
- res = np.pmt(0.0, 5 * 12, 15000)
- tgt = -250.0
- assert_allclose(res, tgt)
- # Test the case where we use broadcast and
- # the arguments passed in are arrays.
- res = np.pmt([[0.0, 0.8], [0.3, 0.8]], [12, 3], [2000, 20000])
- 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')
- assert_equal(res, tgt)
- # Test the edge case where rate == 0.0
- res = np.pmt(Decimal('0'), Decimal('60'), Decimal('15000'))
- tgt = -250
- assert_equal(res, tgt)
- # Test the case where we use broadcast and
- # the arguments passed in are arrays.
- res = np.pmt([[Decimal('0'), Decimal('0.8')], [Decimal('0.3'), Decimal('0.8')]],
- [Decimal('12'), Decimal('3')], [Decimal('2000'), Decimal('20000')])
- tgt = np.array([[Decimal('-166.6666666666666666666666667'), Decimal('-19311.25827814569536423841060')],
- [Decimal('-626.9081401700757748402586600'), Decimal('-19311.25827814569536423841060')]])
-
- # Cannot use the `assert_allclose` because it uses isfinite under the covers
- # which does not support the Decimal type
- # See issue: https://github.com/numpy/numpy/issues/9954
- assert_equal(res[0][0], tgt[0][0])
- assert_equal(res[0][1], tgt[0][1])
- 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
- def raise_error_because_not_equal():
- assert_equal(
- round(np.ppmt(Decimal('0.23') / Decimal('12'), 1, 60, Decimal('10000000000')), 8),
- Decimal('-90238044.232277036'))
-
- assert_raises(AssertionError, raise_error_because_not_equal)
- 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)
-
- val = [-120000, 39000, 30000, 21000, 37000, 46000]
- assert_almost_equal(np.mirr(val, 0.10, 0.12), 0.126094, 6)
-
- val = [100, 200, -50, 300, -200]
- assert_almost_equal(np.mirr(val, 0.05, 0.06), 0.3428, 4)
-
- 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'),
- Decimal('700'), Decimal('3000')]
- assert_equal(np.mirr(val, Decimal('0.08'), Decimal('0.055')),
- Decimal('0.066597175031553548874239618'))
-
- val = [Decimal('-120000'), Decimal('39000'), Decimal('30000'),
- Decimal('21000'), Decimal('37000'), Decimal('46000')]
- assert_equal(np.mirr(val, Decimal('0.10'), Decimal('0.12')), Decimal('0.126094130365905145828421880'))
-
- val = [Decimal('100'), Decimal('200'), Decimal('-50'),
- Decimal('300'), Decimal('-200')]
- assert_equal(np.mirr(val, Decimal('0.05'), Decimal('0.06')), Decimal('0.342823387842176663647819868'))
-
- 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),
- np.rate(10, 20, -3500, 10000, 'begin'))
- # end
- assert_equal(np.rate(10, 20, -3500, 10000),
- np.rate(10, 20, -3500, 10000, 'end'))
- assert_equal(np.rate(10, 20, -3500, 10000, 0),
- np.rate(10, 20, -3500, 10000, 'end'))
-
- # begin
- assert_equal(np.pv(0.07, 20, 12000, 0, 1),
- np.pv(0.07, 20, 12000, 0, 'begin'))
- # end
- assert_equal(np.pv(0.07, 20, 12000, 0),
- np.pv(0.07, 20, 12000, 0, 'end'))
- assert_equal(np.pv(0.07, 20, 12000, 0, 0),
- np.pv(0.07, 20, 12000, 0, 'end'))
-
- # begin
- assert_equal(np.fv(0.075, 20, -2000, 0, 1),
- np.fv(0.075, 20, -2000, 0, 'begin'))
- # end
- assert_equal(np.fv(0.075, 20, -2000, 0),
- np.fv(0.075, 20, -2000, 0, 'end'))
- assert_equal(np.fv(0.075, 20, -2000, 0, 0),
- np.fv(0.075, 20, -2000, 0, 'end'))
-
- # begin
- assert_equal(np.pmt(0.08 / 12, 5 * 12, 15000., 0, 1),
- np.pmt(0.08 / 12, 5 * 12, 15000., 0, 'begin'))
- # end
- assert_equal(np.pmt(0.08 / 12, 5 * 12, 15000., 0),
- np.pmt(0.08 / 12, 5 * 12, 15000., 0, 'end'))
- assert_equal(np.pmt(0.08 / 12, 5 * 12, 15000., 0, 0),
- np.pmt(0.08 / 12, 5 * 12, 15000., 0, 'end'))
-
- # begin
- assert_equal(np.ppmt(0.1 / 12, 1, 60, 55000, 0, 1),
- np.ppmt(0.1 / 12, 1, 60, 55000, 0, 'begin'))
- # end
- assert_equal(np.ppmt(0.1 / 12, 1, 60, 55000, 0),
- np.ppmt(0.1 / 12, 1, 60, 55000, 0, 'end'))
- assert_equal(np.ppmt(0.1 / 12, 1, 60, 55000, 0, 0),
- np.ppmt(0.1 / 12, 1, 60, 55000, 0, 'end'))
-
- # begin
- assert_equal(np.ipmt(0.1 / 12, 1, 24, 2000, 0, 1),
- np.ipmt(0.1 / 12, 1, 24, 2000, 0, 'begin'))
- # end
- assert_equal(np.ipmt(0.1 / 12, 1, 24, 2000, 0),
- np.ipmt(0.1 / 12, 1, 24, 2000, 0, 'end'))
- assert_equal(np.ipmt(0.1 / 12, 1, 24, 2000, 0, 0),
- np.ipmt(0.1 / 12, 1, 24, 2000, 0, 'end'))
-
- # begin
- assert_equal(np.nper(0.075, -2000, 0, 100000., 1),
- np.nper(0.075, -2000, 0, 100000., 'begin'))
- # end
- assert_equal(np.nper(0.075, -2000, 0, 100000.),
- np.nper(0.075, -2000, 0, 100000., 'end'))
- 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
- assert_equal(np.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), Decimal('1')),
- np.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), 'begin'))
- # end
- assert_equal(np.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000')),
- np.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), 'end'))
- assert_equal(np.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), Decimal('0')),
- np.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), 'end'))
-
- # begin
- assert_equal(np.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), Decimal('1')),
- np.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), 'begin'))
- # end
- assert_equal(np.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0')),
- np.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), 'end'))
- assert_equal(np.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), Decimal('0')),
- np.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), 'end'))
-
- # begin
- assert_equal(np.fv(Decimal('0.075'), Decimal('20'), Decimal('-2000'), Decimal('0'), Decimal('1')),
- np.fv(Decimal('0.075'), Decimal('20'), Decimal('-2000'), Decimal('0'), 'begin'))
- # end
- assert_equal(np.fv(Decimal('0.075'), Decimal('20'), Decimal('-2000'), Decimal('0')),
- np.fv(Decimal('0.075'), Decimal('20'), Decimal('-2000'), Decimal('0'), 'end'))
- assert_equal(np.fv(Decimal('0.075'), Decimal('20'), Decimal('-2000'), Decimal('0'), Decimal('0')),
- np.fv(Decimal('0.075'), Decimal('20'), Decimal('-2000'), Decimal('0'), 'end'))
-
- # begin
- assert_equal(np.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'),
- Decimal('0'), Decimal('1')),
- np.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'),
- Decimal('0'), 'begin'))
- # end
- assert_equal(np.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'),
- Decimal('0')),
- np.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'),
- Decimal('0'), 'end'))
- assert_equal(np.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'),
- Decimal('0'), Decimal('0')),
- np.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'),
- Decimal('0'), 'end'))
-
- # begin
- assert_equal(np.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'),
- Decimal('0'), Decimal('1')),
- np.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'),
- Decimal('0'), 'begin'))
- # end
- assert_equal(np.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'),
- Decimal('0')),
- np.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'),
- Decimal('0'), 'end'))
- assert_equal(np.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'),
- Decimal('0'), Decimal('0')),
- np.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'),
- Decimal('0'), 'end'))
-
- # begin
- assert_equal(np.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'),
- Decimal('0'), Decimal('1')).flat[0],
- np.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'),
- Decimal('0'), 'begin').flat[0])
- # end
- assert_equal(np.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'),
- Decimal('0')).flat[0],
- np.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'),
- Decimal('0'), 'end').flat[0])
- assert_equal(np.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'),
- Decimal('0'), Decimal('0')).flat[0],
- 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)
-
- assert_almost_equal(np.ipmt(0.1 / 12, list(range(5)), 24, 2000),
- [-17.29165168, -16.66666667, -16.03647345,
- -15.40102862, -14.76028842], 4)
-
- assert_almost_equal(np.ppmt(0.1 / 12, list(range(5)), 24, 2000),
- [-74.998201, -75.62318601, -76.25337923,
- -76.88882405, -77.52956425], 4)
-
- assert_almost_equal(np.ppmt(0.1 / 12, list(range(5)), 24, 2000, 0,
- [0, 0, 1, 'end', 'begin']),
- [-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.
- assert_almost_equal(np.ipmt(Decimal('0.1') / Decimal('12'), list(range(5)), Decimal('24'), Decimal('2000')),
- [Decimal('-17.29165168'), Decimal('-16.66666667'), Decimal('-16.03647345'),
- Decimal('-15.40102862'), Decimal('-14.76028842')], 4)
-
- assert_almost_equal(np.ppmt(Decimal('0.1') / Decimal('12'), list(range(5)), Decimal('24'), Decimal('2000')),
- [Decimal('-74.998201'), Decimal('-75.62318601'), Decimal('-76.25337923'),
- Decimal('-76.88882405'), Decimal('-77.52956425')], 4)
-
- assert_almost_equal(np.ppmt(Decimal('0.1') / Decimal('12'), list(range(5)), Decimal('24'), Decimal('2000'),
- Decimal('0'), [Decimal('0'), Decimal('0'), Decimal('1'), 'end', 'begin']),
- [Decimal('-74.998201'), Decimal('-75.62318601'), Decimal('-75.62318601'),
- Decimal('-76.88882405'), Decimal('-76.88882405')], 4)
diff --git a/numpy/lib/tests/test_financial_expired.py b/numpy/lib/tests/test_financial_expired.py
new file mode 100644
index 000000000..70b0cd790
--- /dev/null
+++ b/numpy/lib/tests/test_financial_expired.py
@@ -0,0 +1,13 @@
+import sys
+import pytest
+import numpy as np
+
+
+@pytest.mark.skipif(sys.version_info[:2] < (3, 7),
+ reason="requires python 3.7 or higher")
+def test_financial_expired():
+ match = 'NEP 32'
+ with pytest.warns(DeprecationWarning, match=match):
+ func = np.fv
+ with pytest.raises(RuntimeError, match=match):
+ func(1, 2, 3)
diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py
index 2dbaeb8cb..bac42fad3 100644
--- a/numpy/lib/tests/test_format.py
+++ b/numpy/lib/tests/test_format.py
@@ -285,28 +285,11 @@ from io import BytesIO
import numpy as np
from numpy.testing import (
assert_, assert_array_equal, assert_raises, assert_raises_regex,
- assert_warns
+ assert_warns,
)
from numpy.lib import format
-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):
- shutil.rmtree(tempdir)
- tempdir = None
-
-
# Generate some basic arrays to test with.
scalars = [
np.uint8,
@@ -477,49 +460,42 @@ def test_long_str():
assert_array_equal(long_str_arr, long_str_arr2)
-@pytest.mark.slow
-def test_memmap_roundtrip():
- # Fixme: used to crash on windows
- if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
- for arr in basic_arrays + record_arrays:
- if arr.dtype.hasobject:
- # Skip these since they can't be mmap'ed.
- continue
- # Write it out normally and through mmap.
- nfn = os.path.join(tempdir, 'normal.npy')
- mfn = os.path.join(tempdir, 'memmap.npy')
- fp = open(nfn, 'wb')
- try:
- format.write_array(fp, arr)
- finally:
- fp.close()
-
- 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)
- ma[...] = arr
- del ma
-
- # Check that both of these files' contents are the same.
- fp = open(nfn, 'rb')
+def test_memmap_roundtrip(tmpdir):
+ for i, arr in enumerate(basic_arrays + record_arrays):
+ if arr.dtype.hasobject:
+ # Skip these since they can't be mmap'ed.
+ continue
+ # Write it out normally and through mmap.
+ nfn = os.path.join(tmpdir, f'normal{i}.npy')
+ mfn = os.path.join(tmpdir, f'memmap{i}.npy')
+ with open(nfn, 'wb') as fp:
+ format.write_array(fp, arr)
+
+ 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)
+ ma[...] = arr
+ ma.flush()
+
+ # Check that both of these files' contents are the same.
+ with open(nfn, 'rb') as fp:
normal_bytes = fp.read()
- fp.close()
- fp = open(mfn, 'rb')
+ with open(mfn, 'rb') as fp:
memmap_bytes = fp.read()
- fp.close()
- assert_equal_(normal_bytes, memmap_bytes)
+ assert_equal_(normal_bytes, memmap_bytes)
- # Check that reading the file using memmap works.
- ma = format.open_memmap(nfn, mode='r')
- del ma
+ # Check that reading the file using memmap works.
+ ma = format.open_memmap(nfn, mode='r')
+ ma.flush()
-def test_compressed_roundtrip():
+def test_compressed_roundtrip(tmpdir):
arr = np.random.rand(200, 200)
- npz_file = os.path.join(tempdir, 'compressed.npz')
+ npz_file = os.path.join(tmpdir, 'compressed.npz')
np.savez_compressed(npz_file, arr=arr)
- arr1 = np.load(npz_file)['arr']
+ with np.load(npz_file) as npz:
+ arr1 = npz['arr']
assert_array_equal(arr, arr1)
@@ -539,13 +515,14 @@ dt5 = np.dtype({'names': ['a', 'b'], 'formats': ['i4', 'i4'],
dt6 = np.dtype({'names': [], 'formats': [], 'itemsize': 8})
@pytest.mark.parametrize("dt", [dt1, dt2, dt3, dt4, dt5, dt6])
-def test_load_padded_dtype(dt):
+def test_load_padded_dtype(tmpdir, dt):
arr = np.zeros(3, dt)
for i in range(3):
arr[i] = i + 5
- npz_file = os.path.join(tempdir, 'aligned.npz')
+ npz_file = os.path.join(tmpdir, 'aligned.npz')
np.savez(npz_file, arr=arr)
- arr1 = np.load(npz_file)['arr']
+ with np.load(npz_file) as npz:
+ arr1 = npz['arr']
assert_array_equal(arr, arr1)
@@ -602,7 +579,7 @@ def test_pickle_python2_python3():
encoding='latin1')
-def test_pickle_disallow():
+def test_pickle_disallow(tmpdir):
data_dir = os.path.join(os.path.dirname(__file__), 'data')
path = os.path.join(data_dir, 'py2-objarr.npy')
@@ -610,10 +587,10 @@ def test_pickle_disallow():
allow_pickle=False, encoding='latin1')
path = os.path.join(data_dir, 'py2-objarr.npz')
- f = np.load(path, allow_pickle=False, encoding='latin1')
- assert_raises(ValueError, f.__getitem__, 'x')
+ with np.load(path, allow_pickle=False, encoding='latin1') as f:
+ assert_raises(ValueError, f.__getitem__, 'x')
- path = os.path.join(tempdir, 'pickle-disabled.npy')
+ path = os.path.join(tmpdir, 'pickle-disabled.npy')
assert_raises(ValueError, np.save, path, np.array([None], dtype=object),
allow_pickle=False)
@@ -698,31 +675,33 @@ def test_version_2_0():
assert_raises(ValueError, format.write_array, f, d, (1, 0))
-@pytest.mark.slow
-def test_version_2_0_memmap():
+def test_version_2_0_memmap(tmpdir):
# requires more than 2 byte for header
dt = [(("%d" % i) * 100, float) for i in range(500)]
d = np.ones(1000, dtype=dt)
- tf = tempfile.mktemp('', 'mmap', dir=tempdir)
+ tf1 = os.path.join(tmpdir, f'version2_01.npy')
+ tf2 = os.path.join(tmpdir, f'version2_02.npy')
# 1.0 requested but data cannot be saved this way
- assert_raises(ValueError, format.open_memmap, tf, mode='w+', dtype=d.dtype,
+ assert_raises(ValueError, format.open_memmap, tf1, mode='w+', dtype=d.dtype,
shape=d.shape, version=(1, 0))
- ma = format.open_memmap(tf, mode='w+', dtype=d.dtype,
+ ma = format.open_memmap(tf1, mode='w+', dtype=d.dtype,
shape=d.shape, version=(2, 0))
ma[...] = d
- del ma
+ ma.flush()
+ ma = format.open_memmap(tf1, mode='r')
+ assert_array_equal(ma, d)
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', UserWarning)
- ma = format.open_memmap(tf, mode='w+', dtype=d.dtype,
+ ma = format.open_memmap(tf2, mode='w+', dtype=d.dtype,
shape=d.shape, version=None)
assert_(w[0].category is UserWarning)
ma[...] = d
- del ma
+ ma.flush()
- ma = format.open_memmap(tf, mode='r')
+ ma = format.open_memmap(tf2, mode='r')
assert_array_equal(ma, d)
@@ -874,11 +853,11 @@ def test_bad_header():
assert_raises(ValueError, format.read_array_header_1_0, s)
-def test_large_file_support():
+def test_large_file_support(tmpdir):
if (sys.platform == 'win32' or sys.platform == 'cygwin'):
pytest.skip("Unknown if Windows has sparse filesystems")
# try creating a large sparse file
- tf_name = os.path.join(tempdir, 'sparse_file')
+ tf_name = os.path.join(tmpdir, 'sparse_file')
try:
# seek past end would work too, but linux truncate somewhat
# increases the chances that we have a sparse filesystem and can
@@ -902,7 +881,7 @@ def test_large_file_support():
@pytest.mark.skipif(np.dtype(np.intp).itemsize < 8,
reason="test requires 64-bit system")
@pytest.mark.slow
-def test_large_archive():
+def test_large_archive(tmpdir):
# Regression test for product of saving arrays with dimensions of array
# having a product that doesn't fit in int32. See gh-7598 for details.
try:
@@ -910,7 +889,7 @@ def test_large_archive():
except MemoryError:
pytest.skip("Could not create large file")
- fname = os.path.join(tempdir, "large_archive")
+ fname = os.path.join(tmpdir, "large_archive")
with open(fname, "wb") as f:
np.savez(f, arr=a)
@@ -921,14 +900,15 @@ def test_large_archive():
assert_(a.shape == new_a.shape)
-def test_empty_npz():
+def test_empty_npz(tmpdir):
# Test for gh-9989
- fname = os.path.join(tempdir, "nothing.npz")
+ fname = os.path.join(tmpdir, "nothing.npz")
np.savez(fname)
- np.load(fname)
+ with np.load(fname) as nps:
+ pass
-def test_unicode_field_names():
+def test_unicode_field_names(tmpdir):
# gh-7391
arr = np.array([
(1, 3),
@@ -939,7 +919,7 @@ def test_unicode_field_names():
('int', int),
(u'\N{CJK UNIFIED IDEOGRAPH-6574}\N{CJK UNIFIED IDEOGRAPH-5F62}', int)
])
- fname = os.path.join(tempdir, "unicode.npy")
+ fname = os.path.join(tmpdir, "unicode.npy")
with open(fname, 'wb') as f:
format.write_array(f, arr, version=(3, 0))
with open(fname, 'rb') as f:
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index eb2fc3311..4c7c0480c 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -1166,26 +1166,68 @@ class TestAngle:
class TestTrimZeros:
- """
- Only testing for integer splits.
+ a = np.array([0, 0, 1, 0, 2, 3, 4, 0])
+ b = a.astype(float)
+ c = a.astype(complex)
+ d = a.astype(object)
- """
+ def values(self):
+ attr_names = ('a', 'b', 'c', 'd')
+ return (getattr(self, name) for name in attr_names)
def test_basic(self):
- a = np.array([0, 0, 1, 2, 3, 4, 0])
- res = trim_zeros(a)
- assert_array_equal(res, np.array([1, 2, 3, 4]))
+ slc = np.s_[2:-1]
+ for arr in self.values():
+ res = trim_zeros(arr)
+ assert_array_equal(res, arr[slc])
def test_leading_skip(self):
- a = np.array([0, 0, 1, 0, 2, 3, 4, 0])
- res = trim_zeros(a)
- assert_array_equal(res, np.array([1, 0, 2, 3, 4]))
+ slc = np.s_[:-1]
+ for arr in self.values():
+ res = trim_zeros(arr, trim='b')
+ assert_array_equal(res, arr[slc])
def test_trailing_skip(self):
- a = np.array([0, 0, 1, 0, 2, 3, 0, 4, 0])
- res = trim_zeros(a)
- assert_array_equal(res, np.array([1, 0, 2, 3, 0, 4]))
+ slc = np.s_[2:]
+ for arr in self.values():
+ res = trim_zeros(arr, trim='F')
+ assert_array_equal(res, arr[slc])
+
+ def test_all_zero(self):
+ for _arr in self.values():
+ arr = np.zeros_like(_arr, dtype=_arr.dtype)
+
+ res1 = trim_zeros(arr, trim='B')
+ assert len(res1) == 0
+
+ res2 = trim_zeros(arr, trim='f')
+ assert len(res2) == 0
+
+ def test_size_zero(self):
+ arr = np.zeros(0)
+ res = trim_zeros(arr)
+ assert_array_equal(arr, res)
+
+ @pytest.mark.parametrize(
+ 'arr',
+ [np.array([0, 2**62, 0]),
+ np.array([0, 2**63, 0]),
+ np.array([0, 2**64, 0])]
+ )
+ def test_overflow(self, arr):
+ slc = np.s_[1:2]
+ res = trim_zeros(arr)
+ assert_array_equal(res, arr[slc])
+ def test_no_trim(self):
+ arr = np.array([None, 1, None])
+ res = trim_zeros(arr)
+ assert_array_equal(arr, res)
+
+
+ def test_list_to_list(self):
+ res = trim_zeros(self.a.tolist())
+ assert isinstance(res, list)
class TestExtins:
@@ -1763,28 +1805,28 @@ class TestFilterwindows:
def test_hanning(self):
# check symmetry
w = hanning(10)
- assert_array_almost_equal(w, flipud(w), 7)
+ assert_equal(w, flipud(w))
# check known value
assert_almost_equal(np.sum(w, axis=0), 4.500, 4)
def test_hamming(self):
# check symmetry
w = hamming(10)
- assert_array_almost_equal(w, flipud(w), 7)
+ assert_equal(w, flipud(w))
# check known value
assert_almost_equal(np.sum(w, axis=0), 4.9400, 4)
def test_bartlett(self):
# check symmetry
w = bartlett(10)
- assert_array_almost_equal(w, flipud(w), 7)
+ assert_equal(w, flipud(w))
# check known value
assert_almost_equal(np.sum(w, axis=0), 4.4444, 4)
def test_blackman(self):
# check symmetry
w = blackman(10)
- assert_array_almost_equal(w, flipud(w), 7)
+ assert_equal(w, flipud(w))
# check known value
assert_almost_equal(np.sum(w, axis=0), 3.7800, 4)
@@ -1981,6 +2023,12 @@ class TestCorrCoef:
assert_array_almost_equal(c, np.array([[1., -1.], [-1., 1.]]))
assert_(np.all(np.abs(c) <= 1.0))
+ @pytest.mark.parametrize("test_type", [np.half, np.single, np.double, np.longdouble])
+ def test_corrcoef_dtype(self, test_type):
+ cast_A = self.A.astype(test_type)
+ res = corrcoef(cast_A, dtype=test_type)
+ assert test_type == res.dtype
+
class TestCov:
x1 = np.array([[0, 2], [1, 1], [2, 0]]).T
@@ -2081,6 +2129,12 @@ class TestCov:
aweights=self.unit_weights),
self.res1)
+ @pytest.mark.parametrize("test_type", [np.half, np.single, np.double, np.longdouble])
+ def test_cov_dtype(self, test_type):
+ cast_x1 = self.x1.astype(test_type)
+ res = cov(cast_x1, dtype=test_type)
+ assert test_type == res.dtype
+
class Test_I0:
@@ -2089,8 +2143,9 @@ class Test_I0:
i0(0.5),
np.array(1.0634833707413234))
- A = np.array([0.49842636, 0.6969809, 0.22011976, 0.0155549])
- expected = np.array([1.06307822, 1.12518299, 1.01214991, 1.00006049])
+ # need at least one test above 8, as the implementation is piecewise
+ A = np.array([0.49842636, 0.6969809, 0.22011976, 0.0155549, 10.0])
+ expected = np.array([1.06307822, 1.12518299, 1.01214991, 1.00006049, 2815.71662847])
assert_almost_equal(i0(A), expected)
assert_almost_equal(i0(-A), expected)
@@ -2127,6 +2182,10 @@ class Test_I0:
assert_array_equal(exp, res)
+ def test_complex(self):
+ a = np.array([0, 1 + 2j])
+ with pytest.raises(TypeError, match="i0 not supported for complex values"):
+ res = i0(a)
class TestKaiser:
diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py
index 905165a99..c21aefd1a 100644
--- a/numpy/lib/tests/test_index_tricks.py
+++ b/numpy/lib/tests/test_index_tricks.py
@@ -16,23 +16,13 @@ class TestRavelUnravelIndex:
def test_basic(self):
assert_equal(np.unravel_index(2, (2, 2)), (1, 0))
- # test backwards compatibility with older dims
- # keyword argument; see Issue #10586
- with assert_warns(DeprecationWarning):
- # we should achieve the correct result
- # AND raise the appropriate warning
- # when using older "dims" kw argument
- assert_equal(np.unravel_index(indices=2,
- dims=(2, 2)),
- (1, 0))
-
# test that new shape argument works properly
assert_equal(np.unravel_index(indices=2,
shape=(2, 2)),
(1, 0))
# test that an invalid second keyword argument
- # is properly handled
+ # is properly handled, including the old name `dims`.
with assert_raises(TypeError):
np.unravel_index(indices=2, hape=(2, 2))
@@ -42,6 +32,9 @@ class TestRavelUnravelIndex:
with assert_raises(TypeError):
np.unravel_index(254, ims=(17, 94))
+ with assert_raises(TypeError):
+ np.unravel_index(254, dims=(17, 94))
+
assert_equal(np.ravel_multi_index((1, 0), (2, 2)), 2)
assert_equal(np.unravel_index(254, (17, 94)), (2, 66))
assert_equal(np.ravel_multi_index((2, 66), (17, 94)), 254)
@@ -249,6 +242,29 @@ class TestGrid:
assert_equal(grid.size, expected[0])
assert_equal(grid_small.size, expected[1])
+ def test_accepts_npfloating(self):
+ # regression test for #16466
+ grid64 = mgrid[0.1:0.33:0.1, ]
+ grid32 = mgrid[np.float32(0.1):np.float32(0.33):np.float32(0.1), ]
+ assert_(grid32.dtype == np.float64)
+ assert_array_almost_equal(grid64, grid32)
+
+ # different code path for single slice
+ grid64 = mgrid[0.1:0.33:0.1]
+ grid32 = mgrid[np.float32(0.1):np.float32(0.33):np.float32(0.1)]
+ assert_(grid32.dtype == np.float64)
+ assert_array_almost_equal(grid64, grid32)
+
+ def test_accepts_npcomplexfloating(self):
+ # Related to #16466
+ assert_array_almost_equal(
+ mgrid[0.1:0.3:3j, ], mgrid[0.1:0.3:np.complex64(3j), ]
+ )
+
+ # different code path for single slice
+ assert_array_almost_equal(
+ mgrid[0.1:0.3:3j], mgrid[0.1:0.3:np.complex64(3j)]
+ )
class TestConcatenator:
def test_1d(self):
@@ -270,6 +286,10 @@ class TestConcatenator:
g = r_[0:36:100j]
assert_(g.shape == (100,))
+ # Related to #16466
+ g = r_[0:36:np.complex64(100j)]
+ assert_(g.shape == (100,))
+
def test_2d(self):
b = np.random.rand(5, 5)
c = np.random.rand(5, 5)
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 664bfe6e5..aa4499764 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -13,17 +13,19 @@ from tempfile import NamedTemporaryFile
from io import BytesIO, StringIO
from datetime import datetime
import locale
-from multiprocessing import Process
+from multiprocessing import Process, Value
+from ctypes import c_bool
import numpy as np
import numpy.ma as ma
from numpy.lib._iotools import ConverterError, ConversionWarning
-from numpy.compat import asbytes, bytes
+from numpy.compat import asbytes
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
+ HAS_REFCOUNT, suppress_warnings, assert_no_gc_cycles, assert_no_warnings,
+ break_cycles
)
from numpy.testing._private.utils import requires_memory
@@ -574,16 +576,29 @@ class TestSaveTxt:
@pytest.mark.slow
@requires_memory(free_bytes=7e9)
def test_large_zip(self):
- 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)
+ def check_large_zip(memoryerror_raised):
+ memoryerror_raised.value = False
+ try:
+ # 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)
+ except MemoryError:
+ memoryerror_raised.value = True
+ raise
# run in a subprocess to ensure memory is released on PyPy, see gh-15775
- p = Process(target=check_large_zip)
+ # Use an object in shared memory to re-raise the MemoryError exception
+ # in our process if needed, see gh-16889
+ memoryerror_raised = Value(c_bool)
+ p = Process(target=check_large_zip, args=(memoryerror_raised,))
p.start()
p.join()
+ if memoryerror_raised.value:
+ raise MemoryError("Child process raised a MemoryError exception")
assert p.exitcode == 0
class LoadTxtBase:
@@ -1011,7 +1026,7 @@ class TestLoadTxt(LoadTxtBase):
a = np.array([b'start ', b' ', b''])
assert_array_equal(x['comment'], a)
- def test_structure_unpack(self):
+ def test_unpack_structured(self):
txt = TextIO("M 21 72\nF 35 58")
dt = {'names': ('a', 'b', 'c'), 'formats': ('|S1', '<i4', '<f4')}
a, b, c = np.loadtxt(txt, dtype=dt, unpack=True)
@@ -2343,6 +2358,51 @@ M 33 21.99
assert_equal(test['f1'], 17179869184)
assert_equal(test['f2'], 1024)
+ def test_unpack_structured(self):
+ # Regression test for gh-4341
+ # Unpacking should work on structured arrays
+ txt = TextIO("M 21 72\nF 35 58")
+ dt = {'names': ('a', 'b', 'c'), 'formats': ('S1', 'i4', 'f4')}
+ a, b, c = np.genfromtxt(txt, dtype=dt, unpack=True)
+ assert_equal(a.dtype, np.dtype('S1'))
+ assert_equal(b.dtype, np.dtype('i4'))
+ assert_equal(c.dtype, np.dtype('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.]))
+
+ def test_unpack_auto_dtype(self):
+ # Regression test for gh-4341
+ # Unpacking should work when dtype=None
+ txt = TextIO("M 21 72.\nF 35 58.")
+ expected = (np.array(["M", "F"]), np.array([21, 35]), np.array([72., 58.]))
+ test = np.genfromtxt(txt, dtype=None, unpack=True, encoding="utf-8")
+ for arr, result in zip(expected, test):
+ assert_array_equal(arr, result)
+ assert_equal(arr.dtype, result.dtype)
+
+ def test_unpack_single_name(self):
+ # Regression test for gh-4341
+ # Unpacking should work when structured dtype has only one field
+ txt = TextIO("21\n35")
+ dt = {'names': ('a',), 'formats': ('i4',)}
+ expected = np.array([21, 35], dtype=np.int32)
+ test = np.genfromtxt(txt, dtype=dt, unpack=True)
+ assert_array_equal(expected, test)
+ assert_equal(expected.dtype, test.dtype)
+
+ def test_squeeze_scalar(self):
+ # Regression test for gh-4341
+ # Unpacking a scalar should give zero-dim output,
+ # even if dtype is structured
+ txt = TextIO("1")
+ dt = {'names': ('a',), 'formats': ('i4',)}
+ expected = np.array((1,), dtype=np.int32)
+ test = np.genfromtxt(txt, dtype=dt, unpack=True)
+ assert_array_equal(expected, test)
+ assert_equal((), test.shape)
+ assert_equal(expected.dtype, test.dtype)
+
class TestPathUsage:
# Test that pathlib.Path can be used
@@ -2373,6 +2433,9 @@ class TestPathUsage:
assert_array_equal(data, a)
# close the mem-mapped file
del data
+ if IS_PYPY:
+ break_cycles()
+ break_cycles()
def test_save_load_memmap_readwrite(self):
# Test that pathlib.Path instances can be written mem-mapped.
@@ -2384,6 +2447,9 @@ class TestPathUsage:
a[0][0] = 5
b[0][0] = 5
del b # closes the file
+ if IS_PYPY:
+ break_cycles()
+ break_cycles()
data = np.load(path)
assert_array_equal(data, a)
diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py
index cd0b90dc4..6c3e4fa02 100644
--- a/numpy/lib/tests/test_polynomial.py
+++ b/numpy/lib/tests/test_polynomial.py
@@ -227,6 +227,20 @@ class TestPolynomial:
v = np.arange(1, 21)
assert_almost_equal(np.poly(v), np.poly(np.diag(v)))
+ def test_zero_poly_dtype(self):
+ """
+ Regression test for gh-16354.
+ """
+ z = np.array([0, 0, 0])
+ p = np.poly1d(z.astype(np.int64))
+ assert_equal(p.coeffs.dtype, np.int64)
+
+ p = np.poly1d(z.astype(np.float32))
+ assert_equal(p.coeffs.dtype, np.float32)
+
+ p = np.poly1d(z.astype(np.complex64))
+ assert_equal(p.coeffs.dtype, np.complex64)
+
def test_poly_eq(self):
p = np.poly1d([1, 2, 3])
p2 = np.poly1d([1, 2, 4])
@@ -243,6 +257,15 @@ class TestPolynomial:
assert_equal(q.coeffs.dtype, np.complex128)
assert_equal(r.coeffs.dtype, np.complex128)
assert_equal(q*a + r, b)
+
+ c = [1, 2, 3]
+ d = np.poly1d([1, 2, 3])
+ s, t = np.polydiv(c, d)
+ assert isinstance(s, np.poly1d)
+ assert isinstance(t, np.poly1d)
+ u, v = np.polydiv(d, c)
+ assert isinstance(u, np.poly1d)
+ assert isinstance(v, np.poly1d)
def test_poly_coeffs_mutable(self):
""" Coefficients should be modifiable """
diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py
index 9d95eb9d0..efec5d24d 100644
--- a/numpy/lib/tests/test_stride_tricks.py
+++ b/numpy/lib/tests/test_stride_tricks.py
@@ -5,8 +5,11 @@ from numpy.testing import (
assert_raises_regex, assert_warns,
)
from numpy.lib.stride_tricks import (
- as_strided, broadcast_arrays, _broadcast_shape, broadcast_to
+ as_strided, broadcast_arrays, _broadcast_shape, broadcast_to,
+ broadcast_shapes, sliding_window_view,
)
+import pytest
+
def assert_shapes_correct(input_shapes, expected_shape):
# Broadcast a list of arrays with the given input shapes and check the
@@ -274,7 +277,9 @@ def test_broadcast_to_raises():
def test_broadcast_shape():
- # broadcast_shape is already exercized indirectly by broadcast_arrays
+ # tests internal _broadcast_shape
+ # _broadcast_shape is already exercised indirectly by broadcast_arrays
+ # _broadcast_shape is also exercised by the public broadcast_shapes function
assert_equal(_broadcast_shape(), ())
assert_equal(_broadcast_shape([1, 2]), (2,))
assert_equal(_broadcast_shape(np.ones((1, 1))), (1, 1))
@@ -288,6 +293,64 @@ def test_broadcast_shape():
assert_raises(ValueError, lambda: _broadcast_shape(*bad_args))
+def test_broadcast_shapes_succeeds():
+ # tests public broadcast_shapes
+ data = [
+ [[], ()],
+ [[()], ()],
+ [[(7,)], (7,)],
+ [[(1, 2), (2,)], (1, 2)],
+ [[(1, 1)], (1, 1)],
+ [[(1, 1), (3, 4)], (3, 4)],
+ [[(6, 7), (5, 6, 1), (7,), (5, 1, 7)], (5, 6, 7)],
+ [[(5, 6, 1)], (5, 6, 1)],
+ [[(1, 3), (3, 1)], (3, 3)],
+ [[(1, 0), (0, 0)], (0, 0)],
+ [[(0, 1), (0, 0)], (0, 0)],
+ [[(1, 0), (0, 1)], (0, 0)],
+ [[(1, 1), (0, 0)], (0, 0)],
+ [[(1, 1), (1, 0)], (1, 0)],
+ [[(1, 1), (0, 1)], (0, 1)],
+ [[(), (0,)], (0,)],
+ [[(0,), (0, 0)], (0, 0)],
+ [[(0,), (0, 1)], (0, 0)],
+ [[(1,), (0, 0)], (0, 0)],
+ [[(), (0, 0)], (0, 0)],
+ [[(1, 1), (0,)], (1, 0)],
+ [[(1,), (0, 1)], (0, 1)],
+ [[(1,), (1, 0)], (1, 0)],
+ [[(), (1, 0)], (1, 0)],
+ [[(), (0, 1)], (0, 1)],
+ [[(1,), (3,)], (3,)],
+ [[2, (3, 2)], (3, 2)],
+ ]
+ for input_shapes, target_shape in data:
+ assert_equal(broadcast_shapes(*input_shapes), target_shape)
+
+ assert_equal(broadcast_shapes(*([(1, 2)] * 32)), (1, 2))
+ assert_equal(broadcast_shapes(*([(1, 2)] * 100)), (1, 2))
+
+ # regression tests for gh-5862
+ assert_equal(broadcast_shapes(*([(2,)] * 32)), (2,))
+
+
+def test_broadcast_shapes_raises():
+ # tests public broadcast_shapes
+ data = [
+ [(3,), (4,)],
+ [(2, 3), (2,)],
+ [(3,), (3,), (4,)],
+ [(1, 3, 4), (2, 3, 3)],
+ [(1, 2), (3,1), (3,2), (10, 5)],
+ [2, (2, 3)],
+ ]
+ for input_shapes in data:
+ assert_raises(ValueError, lambda: broadcast_shapes(*input_shapes))
+
+ bad_args = [(2,)] * 32 + [(3,)] * 32
+ assert_raises(ValueError, lambda: broadcast_shapes(*bad_args))
+
+
def test_as_strided():
a = np.array([None])
a_view = as_strided(a)
@@ -333,6 +396,109 @@ def test_as_strided():
assert_equal(a.dtype, a_view.dtype)
assert_array_equal([r] * 3, a_view)
+
+class TestSlidingWindowView:
+ def test_1d(self):
+ arr = np.arange(5)
+ arr_view = sliding_window_view(arr, 2)
+ expected = np.array([[0, 1],
+ [1, 2],
+ [2, 3],
+ [3, 4]])
+ assert_array_equal(arr_view, expected)
+
+ def test_2d(self):
+ i, j = np.ogrid[:3, :4]
+ arr = 10*i + j
+ shape = (2, 2)
+ arr_view = sliding_window_view(arr, shape)
+ expected = np.array([[[[0, 1], [10, 11]],
+ [[1, 2], [11, 12]],
+ [[2, 3], [12, 13]]],
+ [[[10, 11], [20, 21]],
+ [[11, 12], [21, 22]],
+ [[12, 13], [22, 23]]]])
+ assert_array_equal(arr_view, expected)
+
+ def test_2d_with_axis(self):
+ i, j = np.ogrid[:3, :4]
+ arr = 10*i + j
+ arr_view = sliding_window_view(arr, 3, 0)
+ expected = np.array([[[0, 10, 20],
+ [1, 11, 21],
+ [2, 12, 22],
+ [3, 13, 23]]])
+ assert_array_equal(arr_view, expected)
+
+ def test_2d_repeated_axis(self):
+ i, j = np.ogrid[:3, :4]
+ arr = 10*i + j
+ arr_view = sliding_window_view(arr, (2, 3), (1, 1))
+ expected = np.array([[[[0, 1, 2],
+ [1, 2, 3]]],
+ [[[10, 11, 12],
+ [11, 12, 13]]],
+ [[[20, 21, 22],
+ [21, 22, 23]]]])
+ assert_array_equal(arr_view, expected)
+
+ def test_2d_without_axis(self):
+ i, j = np.ogrid[:4, :4]
+ arr = 10*i + j
+ shape = (2, 3)
+ arr_view = sliding_window_view(arr, shape)
+ expected = np.array([[[[0, 1, 2], [10, 11, 12]],
+ [[1, 2, 3], [11, 12, 13]]],
+ [[[10, 11, 12], [20, 21, 22]],
+ [[11, 12, 13], [21, 22, 23]]],
+ [[[20, 21, 22], [30, 31, 32]],
+ [[21, 22, 23], [31, 32, 33]]]])
+ assert_array_equal(arr_view, expected)
+
+ def test_errors(self):
+ i, j = np.ogrid[:4, :4]
+ arr = 10*i + j
+ with pytest.raises(ValueError, match='cannot contain negative values'):
+ sliding_window_view(arr, (-1, 3))
+ with pytest.raises(
+ ValueError,
+ match='must provide window_shape for all dimensions of `x`'):
+ sliding_window_view(arr, (1,))
+ with pytest.raises(
+ ValueError,
+ match='Must provide matching length window_shape and axis'):
+ sliding_window_view(arr, (1, 3, 4), axis=(0, 1))
+ with pytest.raises(
+ ValueError,
+ match='window shape cannot be larger than input array'):
+ sliding_window_view(arr, (5, 5))
+
+ def test_writeable(self):
+ arr = np.arange(5)
+ view = sliding_window_view(arr, 2, writeable=False)
+ assert_(not view.flags.writeable)
+ with pytest.raises(
+ ValueError,
+ match='assignment destination is read-only'):
+ view[0, 0] = 3
+ view = sliding_window_view(arr, 2, writeable=True)
+ assert_(view.flags.writeable)
+ view[0, 1] = 3
+ assert_array_equal(arr, np.array([0, 3, 2, 3, 4]))
+
+ def test_subok(self):
+ class MyArray(np.ndarray):
+ pass
+
+ arr = np.arange(5).view(MyArray)
+ assert_(not isinstance(sliding_window_view(arr, 2,
+ subok=False),
+ MyArray))
+ assert_(isinstance(sliding_window_view(arr, 2, subok=True), MyArray))
+ # Default behavior
+ assert_(not isinstance(sliding_window_view(arr, 2), MyArray))
+
+
def as_strided_writeable():
arr = np.ones(10)
view = as_strided(arr, writeable=False)
@@ -435,7 +601,7 @@ def test_writeable():
# check: no warning emitted
assert_equal(result.flags.writeable, True)
result[:] = 0
-
+
# keep readonly input readonly
original.flags.writeable = False
_, result = broadcast_arrays(0, original)
diff --git a/numpy/lib/tests/test_utils.py b/numpy/lib/tests/test_utils.py
index 261cfef5d..33951b92a 100644
--- a/numpy/lib/tests/test_utils.py
+++ b/numpy/lib/tests/test_utils.py
@@ -140,3 +140,22 @@ class TestByteBounds:
def test_assert_raises_regex_context_manager():
with assert_raises_regex(ValueError, 'no deprecation warning'):
raise ValueError('no deprecation warning')
+
+
+def test_info_method_heading():
+ # info(class) should only print "Methods:" heading if methods exist
+
+ class NoPublicMethods:
+ pass
+
+ class WithPublicMethods:
+ def first_method():
+ pass
+
+ def _has_method_heading(cls):
+ out = StringIO()
+ utils.info(cls, output=out)
+ return 'Methods:' in out.getvalue()
+
+ assert _has_method_heading(WithPublicMethods)
+ assert not _has_method_heading(NoPublicMethods)