From 2398cc35e84ed4f39f20e2b593d79fd09972ac7b Mon Sep 17 00:00:00 2001 From: Mathieu Scheltienne Date: Tue, 29 Aug 2023 19:43:02 +0200 Subject: [PATCH] use combination of getattr and hasattr, include float96 and complex192 to the list --- nibabel/casting.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/nibabel/casting.py b/nibabel/casting.py index 229013512..4184d69dc 100644 --- a/nibabel/casting.py +++ b/nibabel/casting.py @@ -25,10 +25,24 @@ class CastingError(Exception): # np.sctypes is deprecated in numpy 2.0 and np.core.sctypes should not be used instead. sctypes = { - 'int': [np.int8, np.int16, np.int32, np.int64], - 'uint': [np.uint8, np.uint16, np.uint32, np.uint64], - 'float': [np.float16, np.float32, np.float64, np.longdouble], - 'complex': [np.complex64, np.complex128, np.complex256], + 'int': [ + getattr(np, dtype) for dtype in ('int8', 'int16', 'int32', 'int64') if hasattr(np, dtype) + ], + 'uint': [ + getattr(np, dtype) + for dtype in ('uint8', 'uint16', 'uint32', 'uint64') + if hasattr(np, dtype) + ], + 'float': [ + getattr(np, dtype) + for dtype in ('float16', 'float32', 'float64', 'float96', 'float128') + if hasattr(np, dtype) + ], + 'complex': [ + getattr(np, dtype) + for dtype in ('complex64', 'complex128', 'complex192', 'complex256') + if hasattr(np, dtype) + ], 'others': [bool, object, bytes, str, np.void], }