Skip to content

Commit

Permalink
Update numpy deprecated imports
Browse files Browse the repository at this point in the history
- replaced np.AxisError with np.exceptions.AxisError
- the `numpy.core` submodule has been renamed to `numpy._core`
- some parts of `numpy.core` have been moved to `numpy.lib.array_utils`

Except for `AxisError`, the updated imports are conditional on
the version of numpy, so the imports should work for numpy >= 1.26.
  • Loading branch information
ricardoV94 authored and brendan-m-murphy committed Feb 6, 2025
1 parent 3b904d6 commit c759a0b
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 29 deletions.
4 changes: 2 additions & 2 deletions pytensor/link/c/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,8 +1367,8 @@ def cmodule_key_(

# We must always add the numpy ABI version here as
# DynamicModule always add the include <numpy/arrayobject.h>
if np.lib.NumpyVersion(np.__version__) < "1.16.0a":
ndarray_c_version = np.core.multiarray._get_ndarray_c_version()
if np.lib.NumpyVersion(np.__version__) > "1.27.0":
ndarray_c_version = np._core._multiarray_umath._get_ndarray_c_version()
else:
ndarray_c_version = np.core._multiarray_umath._get_ndarray_c_version()
sig.append(f"NPY_ABI_VERSION=0x{ndarray_c_version:X}")
Expand Down
9 changes: 8 additions & 1 deletion pytensor/link/numba/dispatch/elemwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
import numba
import numpy as np
from numba.core.extending import overload
from numpy.core.numeric import normalize_axis_index, normalize_axis_tuple


try:
from numpy.lib.array_utils import normalize_axis_index, normalize_axis_tuple
except ModuleNotFoundError:
# numpy < 2.0
from numpy.core.multiarray import normalize_axis_index
from numpy.core.numeric import normalize_axis_tuple

from pytensor.graph.op import Op
from pytensor.link.numba.dispatch import basic as numba_basic
Expand Down
2 changes: 1 addition & 1 deletion pytensor/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def _get_vector_length_Constant(op: Op | Variable, var: Constant) -> int:

# isort: on
# Allow accessing numpy constants from pytensor.tensor
from numpy import e, euler_gamma, inf, infty, nan, newaxis, pi
from numpy import e, euler_gamma, inf, nan, newaxis, pi

from pytensor.tensor.basic import *
from pytensor.tensor.blas import batched_dot, batched_tensordot
Expand Down
17 changes: 13 additions & 4 deletions pytensor/tensor/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@
from typing import cast as type_cast

import numpy as np
from numpy.core.multiarray import normalize_axis_index
from numpy.core.numeric import normalize_axis_tuple


try:
from numpy.lib.array_utils import normalize_axis_index, normalize_axis_tuple
except ModuleNotFoundError:
# numpy < 2.0
from numpy.core.multiarray import normalize_axis_index
from numpy.core.numeric import normalize_axis_tuple


from numpy.exceptions import AxisError

import pytensor
import pytensor.scalar.sharedvar
Expand Down Expand Up @@ -228,7 +237,7 @@ def constant(x, name=None, ndim=None, dtype=None) -> TensorConstant:
elif x_.ndim > ndim:
try:
x_ = np.squeeze(x_, axis=tuple(range(x_.ndim - ndim)))
except np.AxisError:
except AxisError:
raise ValueError(
f"ndarray could not be cast to constant with {int(ndim)} dimensions"
)
Expand Down Expand Up @@ -4406,7 +4415,7 @@ def expand_dims(a: np.ndarray | TensorVariable, axis: Sequence[int]) -> TensorVa
axis = (axis,)

out_ndim = len(axis) + a.ndim
axis = np.core.numeric.normalize_axis_tuple(axis, out_ndim)
axis = normalize_axis_tuple(axis, out_ndim)

if not axis:
return a
Expand Down
3 changes: 2 additions & 1 deletion pytensor/tensor/conv/abstract_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from math import gcd

import numpy as np
from numpy.exceptions import ComplexWarning


try:
Expand Down Expand Up @@ -2338,7 +2339,7 @@ def conv(
bval = _bvalfromboundary("fill")

with warnings.catch_warnings():
warnings.simplefilter("ignore", np.ComplexWarning)
warnings.simplefilter("ignore", ComplexWarning)
for b in range(img.shape[0]):
for g in range(self.num_groups):
for n in range(output_channel_offset):
Expand Down
24 changes: 19 additions & 5 deletions pytensor/tensor/einsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@
from typing import cast

import numpy as np
from numpy.core.einsumfunc import _find_contraction, _parse_einsum_input # type: ignore
from numpy.core.numeric import ( # type: ignore
normalize_axis_index,
normalize_axis_tuple,
)


try:
from numpy._core.einsumfunc import ( # type: ignore
_find_contraction,
_parse_einsum_input,
)
except ModuleNotFoundError:
from numpy.core.einsumfunc import ( # type: ignore
_find_contraction,
_parse_einsum_input,
)

try:
from numpy.lib.array_utils import normalize_axis_index, normalize_axis_tuple
except ModuleNotFoundError:
# numpy < 2.0
from numpy.core.multiarray import normalize_axis_index # type: ignore
from numpy.core.numeric import normalize_axis_tuple # type: ignore

from pytensor.compile.builders import OpFromGraph
from pytensor.tensor import TensorLike
Expand Down
8 changes: 7 additions & 1 deletion pytensor/tensor/elemwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
from typing import Literal

import numpy as np
from numpy.core.numeric import normalize_axis_tuple


try:
from numpy.lib.array_utils import normalize_axis_tuple
except ModuleNotFoundError:
# numpy < 2.0
from numpy.core.numeric import normalize_axis_tuple

import pytensor.tensor.basic
from pytensor.configdefaults import config
Expand Down
16 changes: 12 additions & 4 deletions pytensor/tensor/extra_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
from collections.abc import Collection, Iterable

import numpy as np
from numpy.core.multiarray import normalize_axis_index
from numpy.exceptions import AxisError


try:
from numpy.lib.array_utils import normalize_axis_index, normalize_axis_tuple
except ModuleNotFoundError:
# numpy < 2.0
from numpy.core.multiarray import normalize_axis_index
from numpy.core.numeric import normalize_axis_tuple

import pytensor
import pytensor.scalar.basic as ps
Expand Down Expand Up @@ -596,9 +604,9 @@ def squeeze(x, axis=None):

# scalar inputs are treated as 1D regarding axis in this `Op`
try:
axis = np.core.numeric.normalize_axis_tuple(axis, ndim=max(1, _x.ndim))
except np.AxisError:
raise np.AxisError(axis, ndim=_x.ndim)
axis = normalize_axis_tuple(axis, ndim=max(1, _x.ndim))
except AxisError:
raise AxisError(axis, ndim=_x.ndim)

if not axis:
# Nothing to do
Expand Down
8 changes: 7 additions & 1 deletion pytensor/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
from typing import TYPE_CHECKING, Optional

import numpy as np
from numpy.core.numeric import normalize_axis_tuple


try:
from numpy.lib.array_utils import normalize_axis_tuple
except ModuleNotFoundError:
# numpy < 2.0
from numpy.core.numeric import normalize_axis_tuple

from pytensor import config, printing
from pytensor import scalar as ps
Expand Down
9 changes: 8 additions & 1 deletion pytensor/tensor/nlinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
from typing import Literal, cast

import numpy as np
from numpy.core.numeric import normalize_axis_tuple # type: ignore


try:
from numpy.lib.array_utils import normalize_axis_tuple
except ModuleNotFoundError:
# numpy < 2.0
from numpy.core.numeric import normalize_axis_tuple # type: ignore


from pytensor import scalar as ps
from pytensor.compile.builders import OpFromGraph
Expand Down
8 changes: 7 additions & 1 deletion pytensor/tensor/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
from typing import cast

import numpy as np
from numpy.core.numeric import normalize_axis_tuple # type: ignore


try:
from numpy.lib.array_utils import normalize_axis_tuple
except ModuleNotFoundError:
# numpy < 2.0
from numpy.core.numeric import normalize_axis_tuple # type: ignore

import pytensor
from pytensor.gradient import DisconnectedType
Expand Down
3 changes: 2 additions & 1 deletion pytensor/tensor/slinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
import scipy.linalg
from numpy.exceptions import ComplexWarning

import pytensor
import pytensor.tensor as pt
Expand Down Expand Up @@ -767,7 +768,7 @@ def perform(self, node, inputs, outputs):
Y = U.dot(V.T.dot(gA).dot(U) * X).dot(V.T)

with warnings.catch_warnings():
warnings.simplefilter("ignore", np.ComplexWarning)
warnings.simplefilter("ignore", ComplexWarning)
out[0] = Y.astype(A.dtype)


Expand Down
12 changes: 9 additions & 3 deletions pytensor/tensor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
from typing import cast

import numpy as np
from numpy.core.numeric import normalize_axis_tuple # type: ignore


try:
from numpy.lib.array_utils import normalize_axis_tuple
except ModuleNotFoundError:
# numpy < 2.0
from numpy.core.numeric import normalize_axis_tuple # type: ignore

import pytensor
from pytensor.graph import FunctionGraph, Variable
Expand Down Expand Up @@ -236,8 +242,8 @@ def normalize_reduce_axis(axis, ndim: int) -> tuple[int, ...] | None:
if axis is not None:
try:
axis = normalize_axis_tuple(axis, ndim=max(1, ndim))
except np.AxisError:
raise np.AxisError(axis, ndim=ndim)
except np.exceptions.AxisError:
raise np.exceptions.AxisError(axis, ndim=ndim)

# TODO: If axis tuple is equivalent to None, return None for more canonicalization?
return cast(tuple, axis)
2 changes: 1 addition & 1 deletion tests/tensor/test_elemwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ def test_scalar_input(self):
assert self.op(ps.add, axis=(-1,))(x).eval({x: 5}) == 5

with pytest.raises(
np.AxisError,
np.exceptions.AxisError,
match=re.escape("axis (-2,) is out of bounds for array of dimension 0"),
):
self.op(ps.add, axis=(-2,))(x)
Expand Down
2 changes: 1 addition & 1 deletion tests/tensor/test_extra_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def test_scalar_input(self):
assert squeeze(x, axis=(0,)).eval({x: 5}) == 5

with pytest.raises(
np.AxisError,
np.exceptions.AxisError,
match=re.escape("axis (1,) is out of bounds for array of dimension 0"),
):
squeeze(x, axis=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/tensor/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_memmap(self):
path = Variable(Generic(), None)
x = load(path, "int32", (None,), mmap_mode="c")
fn = function([path], x)
assert isinstance(fn(self.filename), np.core.memmap)
assert type(fn(self.filename)) == np.memmap

def teardown_method(self):
(pytensor.config.compiledir / "_test.npy").unlink()

0 comments on commit c759a0b

Please sign in to comment.