Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utilities: Allow Numpy warnings from numpy.exceptions #3173

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading