Skip to content

Commit e97c992

Browse files
author
Mathieu Scheltienne
committed
fix np.sctypeDict calls
1 parent 53655ec commit e97c992

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

nibabel/casting.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,22 @@ class CastingError(Exception):
4545
],
4646
'others': [bool, object, bytes, str, np.void],
4747
}
48-
48+
# fmt: off
49+
sctypes_named = {
50+
getattr(np, dtype)
51+
for dtype in (
52+
'int8', 'byte', 'int16', 'short', 'int32', 'intc', 'int_', 'int64', 'longlong',
53+
'uint8', 'ubyte', 'uint16', 'ushort', 'uint32', 'uintc', 'uint', 'uint64', 'ulonglong', # noqa: E501
54+
'float16', 'half', 'float32', 'single', 'float64', 'double', 'float96', 'float128', 'longdouble', # noqa: E501
55+
'complex64', 'csingle', 'complex128', 'cdouble', 'complex192', 'complex256', 'clongdouble', # noqa: E501
56+
# other names of the built-in scalar types
57+
'int_', 'float_', 'complex_', 'bytes_', 'str_', 'bool_', 'datetime64', 'timedelta64', # noqa: E501
58+
# other
59+
'object_', 'void',
60+
)
61+
if hasattr(np, dtype)
62+
}
63+
# fmt: on
4964

5065
def float_to_int(arr, int_type, nan2zero=True, infmax=False):
5166
"""Convert floating point array `arr` to type `int_type`

nibabel/spatialimages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
import numpy as np
140140

141141
from .arrayproxy import ArrayLike
142+
from .casting import sctypes_named
142143
from .dataobj_images import DataobjImage
143144
from .filebasedimages import FileBasedHeader, FileBasedImage
144145
from .fileholders import FileMap
@@ -333,7 +334,7 @@ def _supported_np_types(klass: type[HasDtype]) -> set[type[np.generic]]:
333334
else:
334335
raise e
335336
supported = set()
336-
for np_type in set(np.sctypeDict.values()):
337+
for np_type in sctypes_named:
337338
try:
338339
obj.set_data_dtype(np_type)
339340
except HeaderDataError:

nibabel/tests/test_analyze.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from .. import imageglobals
2727
from ..analyze import AnalyzeHeader, AnalyzeImage
2828
from ..arraywriters import WriterError
29-
from ..casting import as_int
29+
from ..casting import as_int, sctypes_named
3030
from ..nifti1 import Nifti1Header
3131
from ..optpkg import optional_package
3232
from ..spatialimages import HeaderDataError, HeaderTypeError, supported_np_types
@@ -52,7 +52,7 @@ def add_duplicate_types(supported_np_types):
5252
# Update supported numpy types with named scalar types that map to the same set of dtypes
5353
dtypes = {np.dtype(t) for t in supported_np_types}
5454
supported_np_types.update(
55-
scalar for scalar in set(np.sctypeDict.values()) if np.dtype(scalar) in dtypes
55+
scalar for scalar in sctypes_named if np.dtype(scalar) in dtypes
5656
)
5757

5858

nibabel/tests/test_spm99analyze.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# files
2424
needs_scipy = unittest.skipUnless(have_scipy, 'scipy not available')
2525

26-
from ..casting import shared_range, type_info
26+
from ..casting import sctypes_named, shared_range, type_info
2727
from ..spatialimages import HeaderDataError
2828
from ..spm99analyze import HeaderTypeError, Spm99AnalyzeHeader, Spm99AnalyzeImage
2929
from ..testing import (
@@ -39,7 +39,7 @@
3939
# For testing, we want all concrete classes of a type
4040
# Key on kind, rather than abstract base classes, since timedelta64 is a signedinteger
4141
sctypes = {}
42-
for sctype in set(np.sctypeDict.values()):
42+
for sctype in sctypes_named:
4343
sctypes.setdefault(np.dtype(sctype).kind, []).append(sctype)
4444

4545
# Sort types to ensure that xdist doesn't complain about test order when we parametrize

0 commit comments

Comments
 (0)