Skip to content

Commit

Permalink
utilities: Allow Numpy warning from numpy.exceptions
Browse files Browse the repository at this point in the history
Numpy 1.25.0 moved warnings and errors to a submodule[0].
Moreover, they are removed from the top level namespace
in later versions of Numpy.

Add and use exception import that either points to the
new exception submodule, or the global namespace depending
on whether the former is available.

[0] https://numpy.org/doc/2.1/release/1.25.0-notes.html#numpy-now-has-an-np-exceptions-namespace

Signed-off-by: Jan Vesely <[email protected]>
  • Loading branch information
jvesely committed Jan 29, 2025
1 parent 97b9de9 commit c77558a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions psyneulink/core/globals/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
import numpy as np
from numpy.typing import DTypeLike

try:
from numpy import exceptions as np_exceptions
except ImportError:
# Numpy exceptions is only available in Numpy 1.25+
np_exceptions = np

# Conditionally import torch
try:
import torch
Expand Down Expand Up @@ -1644,13 +1650,15 @@ def recurse(arr):
subarr = [recurse(x) for x in arr]

with warnings.catch_warnings():
warnings.filterwarnings('error', message='.*ragged.*', category=np.VisibleDeprecationWarning)
warnings.filterwarnings('error', message='.*ragged.*', category=np_exceptions.VisibleDeprecationWarning)
try:
# the elements are all uniform in shape, so we can use numpy's standard behavior
return np.asarray(subarr)
except np.VisibleDeprecationWarning:
except np_exceptions.VisibleDeprecationWarning:
pass
except ValueError as e:
# Numpy 1.24+ switch jagged array from warning to ValueError.
# Note that the below call can still raise other ValueErrors.
if 'The requested array has an inhomogeneous shape' not in str(e):
raise

Expand Down Expand Up @@ -2385,18 +2393,15 @@ def safe_create_np_array(value):
if torch and torch.is_tensor(value):
return value

warnings.filterwarnings('error', category=np.VisibleDeprecationWarning)
# NOTE: this will raise a ValueError in the future.
# See https://numpy.org/neps/nep-0034-infer-dtype-is-object.html
warnings.filterwarnings('error', category=np_exceptions.VisibleDeprecationWarning)
try:
try:
return np.asarray(value)
except np.VisibleDeprecationWarning:
except np_exceptions.VisibleDeprecationWarning:
return np.asarray(value, dtype=object)
except ValueError as e:
# numpy 1.24 removed the above deprecation and raises
# ValueError instead. Note that the below call can still
# raise other ValueErrors
# Numpy 1.24+ switch jagged array from warning to ValueError.
# Note that the below call can still raise other ValueErrors.
if 'The requested array has an inhomogeneous shape' in str(e):
return np.asarray(value, dtype=object)
raise
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ xfail_strict = True

filterwarnings =
error::SyntaxWarning
error:Creating an ndarray from ragged nested sequences \(which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes\) is deprecated.*:numpy.VisibleDeprecationWarning
error:Creating an ndarray from ragged nested sequences \(which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes\) is deprecated.*
error:Invalid escape sequence
error:the matrix subclass is not the recommended way to represent matrices or deal with linear algebra
error:Passing (type, 1) or '1type' as a synonym of type is deprecated
Expand Down

0 comments on commit c77558a

Please sign in to comment.