diff options
author | Ralf Gommers <ralf.gommers@gmail.com> | 2023-03-02 14:57:43 +0000 |
---|---|---|
committer | Ralf Gommers <ralf.gommers@gmail.com> | 2023-03-02 15:10:41 +0000 |
commit | 9990f98f2e9e1a55f5d42206cbc6709a3efff418 (patch) | |
tree | fa0e6498a52287320c5a66ffe413dc8fd733171d | |
parent | 6a819d660afddaf3e9fba6de00f8f4d366813a5c (diff) | |
download | numpy-9990f98f2e9e1a55f5d42206cbc6709a3efff418.tar.gz |
DEP: deprecate `product`, `cumproduct`, `sometrue`, `alltrue`
[skip cirrus]
-rw-r--r-- | doc/release/upcoming_changes/23314.deprecations.rst | 4 | ||||
-rw-r--r-- | numpy/core/fromnumeric.py | 28 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 19 | ||||
-rw-r--r-- | numpy/core/tests/test_indexing.py | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_mem_overlap.py | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_memmap.py | 4 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 7 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 23 | ||||
-rw-r--r-- | numpy/lib/index_tricks.py | 19 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 8 | ||||
-rw-r--r-- | numpy/lib/tests/test_type_check.py | 2 | ||||
-rw-r--r-- | numpy/linalg/linalg.py | 4 | ||||
-rw-r--r-- | numpy/ma/extras.py | 4 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 8 | ||||
-rw-r--r-- | numpy/ma/tests/test_old_ma.py | 8 | ||||
-rw-r--r-- | numpy/ma/testutils.py | 2 | ||||
-rw-r--r-- | numpy/random/_generator.pyx | 2 | ||||
-rw-r--r-- | numpy/random/mtrand.pyx | 2 |
18 files changed, 94 insertions, 54 deletions
diff --git a/doc/release/upcoming_changes/23314.deprecations.rst b/doc/release/upcoming_changes/23314.deprecations.rst new file mode 100644 index 000000000..8bed1aef8 --- /dev/null +++ b/doc/release/upcoming_changes/23314.deprecations.rst @@ -0,0 +1,4 @@ +* ``np.product`` is deprecated. Use `np.prod` instead. +* ``np.cumproduct`` is deprecated. Use `np.cumprod` instead. +* ``np.sometrue`` is deprecated. Use `np.any` instead. +* ``np.alltrue`` is deprecated. Use `np.all` instead. diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index ed8b68ecd..6dfd95b96 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -3832,10 +3832,17 @@ def product(*args, **kwargs): """ Return the product of array elements over a given axis. + .. deprecated:: 1.25.0 + ``product`` is deprecated as of NumPy 1.25.0, and will be + removed in NumPy 2.0. Please use `prod` instead. + See Also -------- prod : equivalent function; see for details. """ + warnings.warn("`product` is deprecated as of NumPy 1.25.0, and will be " + "removed in NumPy 2.0. Please use `prod` instead.", + DeprecationWarning, stacklevel=2) return prod(*args, **kwargs) @@ -3844,10 +3851,17 @@ def cumproduct(*args, **kwargs): """ Return the cumulative product over the given axis. + .. deprecated:: 1.25.0 + ``cumproduct`` is deprecated as of NumPy 1.25.0, and will be + removed in NumPy 2.0. Please use `cumprod` instead. + See Also -------- cumprod : equivalent function; see for details. """ + warnings.warn("`cumproduct` is deprecated as of NumPy 1.25.0, and will be " + "removed in NumPy 2.0. Please use `cumprod` instead.", + DeprecationWarning, stacklevel=2) return cumprod(*args, **kwargs) @@ -3858,10 +3872,17 @@ def sometrue(*args, **kwargs): Refer to `any` for full documentation. + .. deprecated:: 1.25.0 + ``sometrue`` is deprecated as of NumPy 1.25.0, and will be + removed in NumPy 2.0. Please use `any` instead. + See Also -------- any : equivalent function; see for details. """ + warnings.warn("`sometrue` is deprecated as of NumPy 1.25.0, and will be " + "removed in NumPy 2.0. Please use `any` instead.", + DeprecationWarning, stacklevel=2) return any(*args, **kwargs) @@ -3870,8 +3891,15 @@ def alltrue(*args, **kwargs): """ Check if all elements of input array are true. + .. deprecated:: 1.25.0 + ``alltrue`` is deprecated as of NumPy 1.25.0, and will be + removed in NumPy 2.0. Please use `all` instead. + See Also -------- numpy.all : Equivalent function; see for details. """ + warnings.warn("`alltrue` is deprecated as of NumPy 1.25.0, and will be " + "removed in NumPy 2.0. Please use `all` instead.", + DeprecationWarning, stacklevel=2) return all(*args, **kwargs) diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 8ff52c885..96ae4b2a8 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -902,8 +902,23 @@ class TestDeprecatedFinfo(_DeprecationTestCase): def test_deprecated_none(self): self.assert_deprecated(np.finfo, args=(None,)) - -class TestRound_(_DeprecationTestCase): +class TestFromnumeric(_DeprecationTestCase): # 2023-02-28, 1.25.0 def test_round_(self): self.assert_deprecated(lambda: np.round_(np.array([1.5, 2.5, 3.5]))) + + # 2023-03-02, 1.25.0 + def test_cumproduct(self): + self.assert_deprecated(lambda: np.cumproduct(np.array([1, 2, 3]))) + + # 2023-03-02, 1.25.0 + def test_product(self): + self.assert_deprecated(lambda: np.product(np.array([1, 2, 3]))) + + # 2023-03-02, 1.25.0 + def test_sometrue(self): + self.assert_deprecated(lambda: np.sometrue(np.array([True, False]))) + + # 2023-03-02, 1.25.0 + def test_alltrue(self): + self.assert_deprecated(lambda: np.alltrue(np.array([True, False]))) diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py index 74075639c..042936702 100644 --- a/numpy/core/tests/test_indexing.py +++ b/numpy/core/tests/test_indexing.py @@ -1062,7 +1062,7 @@ class TestMultiIndexingAutomated: if np.any(_indx >= _size) or np.any(_indx < -_size): raise IndexError if len(indx[1:]) == len(orig_slice): - if np.product(orig_slice) == 0: + if np.prod(orig_slice) == 0: # Work around for a crash or IndexError with 'wrap' # in some 0-sized cases. try: diff --git a/numpy/core/tests/test_mem_overlap.py b/numpy/core/tests/test_mem_overlap.py index d66decfda..1fd4c4d41 100644 --- a/numpy/core/tests/test_mem_overlap.py +++ b/numpy/core/tests/test_mem_overlap.py @@ -55,7 +55,7 @@ def _indices(ndims): def _check_assignment(srcidx, dstidx): """Check assignment arr[dstidx] = arr[srcidx] works.""" - arr = np.arange(np.product(shape)).reshape(shape) + arr = np.arange(np.prod(shape)).reshape(shape) cpy = arr.copy() diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py index 914f86f14..ad074b312 100644 --- a/numpy/core/tests/test_memmap.py +++ b/numpy/core/tests/test_memmap.py @@ -6,7 +6,7 @@ from pathlib import Path from tempfile import NamedTemporaryFile, TemporaryFile from numpy import ( - memmap, sum, average, product, ndarray, isscalar, add, subtract, multiply) + memmap, sum, average, prod, ndarray, isscalar, add, subtract, multiply) from numpy import arange, allclose, asarray from numpy.testing import ( @@ -153,7 +153,7 @@ class TestMemmap: with suppress_warnings() as sup: sup.filter(FutureWarning, "np.average currently does not preserve") - for unary_op in [sum, average, product]: + for unary_op in [sum, average, prod]: result = unary_op(fp) assert_(isscalar(result)) assert_(result.__class__ is self.data[0, 0].__class__) diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 3cc168b34..d5a846b94 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -114,7 +114,8 @@ class TestNonarrayArgs: def test_cumproduct(self): A = [[1, 2, 3], [4, 5, 6]] - assert_(np.all(np.cumproduct(A) == np.array([1, 2, 6, 24, 120, 720]))) + with assert_warns(DeprecationWarning): + assert_(np.all(np.cumproduct(A) == np.array([1, 2, 6, 24, 120, 720]))) def test_diagonal(self): a = [[0, 1, 2, 3], @@ -1193,8 +1194,8 @@ class TestFromiter: expected = np.array(list(self.makegen())) a = np.fromiter(self.makegen(), int) a20 = np.fromiter(self.makegen(), int, 20) - assert_(np.alltrue(a == expected, axis=0)) - assert_(np.alltrue(a20 == expected[:20], axis=0)) + assert_(np.all(a == expected, axis=0)) + assert_(np.all(a20 == expected[:20], axis=0)) def load_data(self, n, eindex): # Utility method for the issue 2592 tests. diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 413ece045..fdd536bb9 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -516,22 +516,15 @@ class TestRegression: def test_method_args(self): # Make sure methods and functions have same default axis # keyword and arguments - funcs1 = ['argmax', 'argmin', 'sum', ('product', 'prod'), - ('sometrue', 'any'), - ('alltrue', 'all'), 'cumsum', ('cumproduct', 'cumprod'), - 'ptp', 'cumprod', 'prod', 'std', 'var', 'mean', - 'round', 'min', 'max', 'argsort', 'sort'] + funcs1 = ['argmax', 'argmin', 'sum', 'any', 'all', 'cumsum', + 'ptp', 'cumprod', 'prod', 'std', 'var', 'mean', + 'round', 'min', 'max', 'argsort', 'sort'] funcs2 = ['compress', 'take', 'repeat'] for func in funcs1: arr = np.random.rand(8, 7) arr2 = arr.copy() - if isinstance(func, tuple): - func_meth = func[1] - func = func[0] - else: - func_meth = func - res1 = getattr(arr, func_meth)() + res1 = getattr(arr, func)() res2 = getattr(np, func)(arr2) if res1 is None: res1 = arr @@ -1336,8 +1329,8 @@ class TestRegression: # Ticket #1058 a = np.fromiter(list(range(10)), dtype='b') b = np.fromiter(list(range(10)), dtype='B') - assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) - assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) + assert_(np.all(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) + assert_(np.all(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) def test_array_from_sequence_scalar_array(self): # Ticket #1078: segfaults when creating an array with a sequence of @@ -1515,8 +1508,8 @@ class TestRegression: def test_fromiter_comparison(self): a = np.fromiter(list(range(10)), dtype='b') b = np.fromiter(list(range(10)), dtype='B') - assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) - assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) + assert_(np.all(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) + assert_(np.all(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) def test_fromstring_crash(self): # Ticket #1345: the following should not cause a crash diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py index fa3a90e4f..abf9e1090 100644 --- a/numpy/lib/index_tricks.py +++ b/numpy/lib/index_tricks.py @@ -3,11 +3,10 @@ import sys import math import warnings +import numpy as np from .._utils import set_module import numpy.core.numeric as _nx -from numpy.core.numeric import ( - asarray, ScalarType, array, alltrue, cumprod, arange, ndim -) +from numpy.core.numeric import ScalarType, array from numpy.core.numerictypes import find_common_type, issubdtype import numpy.matrixlib as matrixlib @@ -94,7 +93,7 @@ def ix_(*args): nd = len(args) for k, new in enumerate(args): if not isinstance(new, _nx.ndarray): - new = asarray(new) + new = np.asarray(new) if new.size == 0: # Explicitly type empty arrays to avoid float default new = new.astype(_nx.intp) @@ -396,7 +395,7 @@ class AxisConcatenator: scalar = True scalartypes.append(newobj.dtype) else: - item_ndim = ndim(item) + item_ndim = np.ndim(item) newobj = array(item, copy=False, subok=True, ndmin=ndmin) if trans1d != -1 and item_ndim < ndmin: k2 = ndmin - item_ndim @@ -596,7 +595,7 @@ class ndenumerate: """ def __init__(self, arr): - self.iter = asarray(arr).flat + self.iter = np.asarray(arr).flat def __next__(self): """ @@ -909,9 +908,9 @@ def fill_diagonal(a, val, wrap=False): else: # For more than d=2, the strided formula is only valid for arrays with # all dimensions equal, so we check first. - if not alltrue(diff(a.shape) == 0): + if not np.all(diff(a.shape) == 0): raise ValueError("All dimensions of input must be of equal length") - step = 1 + (cumprod(a.shape[:-1])).sum() + step = 1 + (np.cumprod(a.shape[:-1])).sum() # Write the value out into the diagonal. a.flat[:end:step] = val @@ -982,7 +981,7 @@ def diag_indices(n, ndim=2): [0, 1]]]) """ - idx = arange(n) + idx = np.arange(n) return (idx,) * ndim @@ -1041,7 +1040,7 @@ def diag_indices_from(arr): raise ValueError("input array must be at least 2-d") # For more than d=2, the strided formula is only valid for arrays with # all dimensions equal, so we check first. - if not alltrue(diff(arr.shape) == 0): + if not np.all(diff(arr.shape) == 0): raise ValueError("All dimensions of input must be of equal length") return diag_indices(arr.shape[0], arr.ndim) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index cc8003f61..3ec46735c 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -229,8 +229,8 @@ class TestAny: def test_nd(self): y1 = [[0, 0, 0], [0, 1, 0], [1, 1, 0]] assert_(np.any(y1)) - assert_array_equal(np.sometrue(y1, axis=0), [1, 1, 0]) - assert_array_equal(np.sometrue(y1, axis=1), [0, 1, 1]) + assert_array_equal(np.any(y1, axis=0), [1, 1, 0]) + assert_array_equal(np.any(y1, axis=1), [0, 1, 1]) class TestAll: @@ -247,8 +247,8 @@ class TestAll: def test_nd(self): y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]] assert_(not np.all(y1)) - assert_array_equal(np.alltrue(y1, axis=0), [0, 0, 1]) - assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1]) + assert_array_equal(np.all(y1, axis=0), [0, 0, 1]) + assert_array_equal(np.all(y1, axis=1), [0, 0, 1]) class TestCopy: diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py index 3f4ca6309..ea0326139 100644 --- a/numpy/lib/tests/test_type_check.py +++ b/numpy/lib/tests/test_type_check.py @@ -155,7 +155,7 @@ class TestIscomplex: def test_fail(self): z = np.array([-1, 0, 1]) res = iscomplex(z) - assert_(not np.sometrue(res, axis=0)) + assert_(not np.any(res, axis=0)) def test_pass(self): z = np.array([-1j, 1, 0]) diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 255de94e5..78927d1ae 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -23,7 +23,7 @@ from numpy.core import ( array, asarray, zeros, empty, empty_like, intc, single, double, csingle, cdouble, inexact, complexfloating, newaxis, all, Inf, dot, add, multiply, sqrt, sum, isfinite, - finfo, errstate, geterrobj, moveaxis, amin, amax, product, abs, + finfo, errstate, geterrobj, moveaxis, amin, amax, prod, abs, atleast_2d, intp, asanyarray, object_, matmul, swapaxes, divide, count_nonzero, isnan, sign, argsort, sort, reciprocal @@ -196,7 +196,7 @@ def _assert_finite(*arrays): def _is_empty_2d(arr): # check size first for efficiency - return arr.size == 0 and product(arr.shape[-2:]) == 0 + return arr.size == 0 and prod(arr.shape[-2:]) == 0 def transpose(a): diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index 41bce0f22..4abe2107a 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -398,7 +398,7 @@ def apply_along_axis(func1d, axis, arr, *args, **kwargs): dtypes.append(np.asarray(res).dtype) outarr = zeros(outshape, object) outarr[tuple(ind)] = res - Ntot = np.product(outshape) + Ntot = np.prod(outshape) k = 1 while k < Ntot: # increment the index @@ -418,7 +418,7 @@ def apply_along_axis(func1d, axis, arr, *args, **kwargs): j = i.copy() j[axis] = ([slice(None, None)] * res.ndim) j.put(indlist, ind) - Ntot = np.product(outshape) + Ntot = np.prod(outshape) holdshape = outshape outshape = list(arr.shape) outshape[axis] = res.shape diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 172a1ee8d..5db01b74a 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1336,16 +1336,16 @@ class TestMaskedArrayArithmetic: assert_equal(np.sum(x, axis=0), sum(x, axis=0)) assert_equal(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0)) assert_equal(np.sum(x, 0), sum(x, 0)) - assert_equal(np.product(x, axis=0), product(x, axis=0)) - assert_equal(np.product(x, 0), product(x, 0)) - assert_equal(np.product(filled(xm, 1), axis=0), product(xm, axis=0)) + assert_equal(np.prod(x, axis=0), product(x, axis=0)) + assert_equal(np.prod(x, 0), product(x, 0)) + assert_equal(np.prod(filled(xm, 1), axis=0), product(xm, axis=0)) s = (3, 4) x.shape = y.shape = xm.shape = ym.shape = s if len(s) > 1: assert_equal(np.concatenate((x, y), 1), concatenate((xm, ym), 1)) assert_equal(np.add.reduce(x, 1), add.reduce(x, 1)) assert_equal(np.sum(x, 1), sum(x, 1)) - assert_equal(np.product(x, 1), product(x, 1)) + assert_equal(np.prod(x, 1), product(x, 1)) def test_binops_d2D(self): # Test binary operations on 2D data diff --git a/numpy/ma/tests/test_old_ma.py b/numpy/ma/tests/test_old_ma.py index 8465b1153..7b892ad23 100644 --- a/numpy/ma/tests/test_old_ma.py +++ b/numpy/ma/tests/test_old_ma.py @@ -194,16 +194,16 @@ class TestMa: assert_(eq(np.sum(x, axis=0), sum(x, axis=0))) assert_(eq(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0))) assert_(eq(np.sum(x, 0), sum(x, 0))) - assert_(eq(np.product(x, axis=0), product(x, axis=0))) - assert_(eq(np.product(x, 0), product(x, 0))) - assert_(eq(np.product(filled(xm, 1), axis=0), + assert_(eq(np.prod(x, axis=0), product(x, axis=0))) + assert_(eq(np.prod(x, 0), product(x, 0))) + assert_(eq(np.prod(filled(xm, 1), axis=0), product(xm, axis=0))) if len(s) > 1: assert_(eq(np.concatenate((x, y), 1), concatenate((xm, ym), 1))) assert_(eq(np.add.reduce(x, 1), add.reduce(x, 1))) assert_(eq(np.sum(x, 1), sum(x, 1))) - assert_(eq(np.product(x, 1), product(x, 1))) + assert_(eq(np.prod(x, 1), product(x, 1))) def test_testCI(self): # Test of conversions and indexing diff --git a/numpy/ma/testutils.py b/numpy/ma/testutils.py index 2dd479abe..7a633906b 100644 --- a/numpy/ma/testutils.py +++ b/numpy/ma/testutils.py @@ -233,7 +233,7 @@ def fail_if_array_equal(x, y, err_msg='', verbose=True): """ def compare(x, y): - return (not np.alltrue(approx(x, y))) + return (not np.all(approx(x, y))) assert_array_compare(compare, x, y, err_msg=err_msg, verbose=verbose, header='Arrays are not equal') diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index faf19eaf2..4cc8d42f5 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -2628,7 +2628,7 @@ cdef class Generator: >>> b = [] >>> for i in range(1000): ... a = 10. + rng.standard_normal(100) - ... b.append(np.product(a)) + ... b.append(np.prod(a)) >>> b = np.array(b) / np.min(b) # scale values to be positive >>> count, bins, ignored = plt.hist(b, 100, density=True, align='mid') diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index ca6ba9de8..dfa553ee4 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -3066,7 +3066,7 @@ cdef class RandomState: >>> b = [] >>> for i in range(1000): ... a = 10. + np.random.standard_normal(100) - ... b.append(np.product(a)) + ... b.append(np.prod(a)) >>> b = np.array(b) / np.min(b) # scale values to be positive >>> count, bins, ignored = plt.hist(b, 100, density=True, align='mid') |