diff --git a/psyneulink/core/globals/utilities.py b/psyneulink/core/globals/utilities.py index 80fea8ed48..73216b289a 100644 --- a/psyneulink/core/globals/utilities.py +++ b/psyneulink/core/globals/utilities.py @@ -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 @@ -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 @@ -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 diff --git a/setup.cfg b/setup.cfg index b1a5abf0a3..1fbd76b7c6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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