Skip to content

Commit 1dac328

Browse files
author
Mathieu Scheltienne
committed
manually define the mapping between str and scalar types in casting.py
1 parent 0f5ad6e commit 1dac328

11 files changed

+49
-37
lines changed

nibabel/casting.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ class CastingError(Exception):
2323
_test_val = 2**63 + 2**11 # Should be exactly representable in float64
2424
TRUNC_UINT64 = np.float64(_test_val).astype(np.uint64) != _test_val
2525

26+
# np.sctypes is deprecated in numpy 2.0 and np.core.sctypes should not be used instead.
27+
sctypes = {
28+
"int": [np.int8, np.int16, np.int32, np.int64],
29+
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
30+
"float": [np.float16, np.float32, np.float64, np.float128],
31+
"complex": [np.complex64, np.complex128, np.complex256],
32+
"others": [bool, object, bytes, str, np.void],
33+
}
34+
2635

2736
def float_to_int(arr, int_type, nan2zero=True, infmax=False):
2837
"""Convert floating point array `arr` to type `int_type`
@@ -714,7 +723,7 @@ def ok_floats():
714723
Remove longdouble if it has no higher precision than float64
715724
"""
716725
# copy float list so we don't change the numpy global
717-
floats = np.core.sctypes['float'][:]
726+
floats = sctypes['float'][:]
718727
if best_float() != np.longdouble and np.longdouble in floats:
719728
floats.remove(np.longdouble)
720729
return sorted(floats, key=lambda f: type_info(f)['nmant'])
@@ -750,10 +759,10 @@ def able_int_type(values):
750759
mn = min(values)
751760
mx = max(values)
752761
if mn >= 0:
753-
for ityp in np.core.sctypes['uint']:
762+
for ityp in sctypes['uint']:
754763
if mx <= np.iinfo(ityp).max:
755764
return ityp
756-
for ityp in np.core.sctypes['int']:
765+
for ityp in sctypes['int']:
757766
info = np.iinfo(ityp)
758767
if mn >= info.min and mx <= info.max:
759768
return ityp

nibabel/tests/test_arraywriters.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
get_slope_inter,
2121
make_array_writer,
2222
)
23-
from ..casting import int_abs, shared_range, type_info
23+
from ..casting import int_abs, sctypes, shared_range, type_info
2424
from ..testing import assert_allclose_safely, suppress_warnings
2525
from ..volumeutils import _dt_min_max, apply_read_scaling, array_from_file
2626

27-
FLOAT_TYPES = np.core.sctypes['float']
28-
COMPLEX_TYPES = np.core.sctypes['complex']
29-
INT_TYPES = np.core.sctypes['int']
30-
UINT_TYPES = np.core.sctypes['uint']
27+
FLOAT_TYPES = sctypes['float']
28+
COMPLEX_TYPES = sctypes['complex']
29+
INT_TYPES = sctypes['int']
30+
UINT_TYPES = sctypes['uint']
3131
CFLOAT_TYPES = FLOAT_TYPES + COMPLEX_TYPES
3232
IUINT_TYPES = INT_TYPES + UINT_TYPES
3333
NUMERIC_TYPES = CFLOAT_TYPES + IUINT_TYPES

nibabel/tests/test_casting.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
int_abs,
1818
int_to_float,
1919
longdouble_precision_improved,
20+
sctypes,
2021
shared_range,
2122
ulp,
2223
)
2324
from ..testing import suppress_warnings
2425

2526

2627
def test_shared_range():
27-
for ft in np.core.sctypes['float']:
28-
for it in np.core.sctypes['int'] + np.core.sctypes['uint']:
28+
for ft in sctypes['float']:
29+
for it in sctypes['int'] + sctypes['uint']:
2930
# Test that going a bit above or below the calculated min and max
3031
# either generates the same number when cast, or the max int value
3132
# (if this system generates that) or something smaller (because of
@@ -54,7 +55,7 @@ def test_shared_range():
5455
assert np.all((bit_bigger == casted_mx) | (bit_bigger == imax))
5556
else:
5657
assert np.all(bit_bigger <= casted_mx)
57-
if it in np.core.sctypes['uint']:
58+
if it in sctypes['uint']:
5859
assert mn == 0
5960
continue
6061
# And something larger for the minimum
@@ -90,8 +91,8 @@ def test_shared_range_inputs():
9091

9192

9293
def test_casting():
93-
for ft in np.core.sctypes['float']:
94-
for it in np.core.sctypes['int'] + np.core.sctypes['uint']:
94+
for ft in sctypes['float']:
95+
for it in sctypes['int'] + sctypes['uint']:
9596
ii = np.iinfo(it)
9697
arr = [ii.min - 1, ii.max + 1, -np.inf, np.inf, np.nan, 0.2, 10.6]
9798
farr_orig = np.array(arr, dtype=ft)
@@ -140,7 +141,7 @@ def test_casting():
140141

141142

142143
def test_int_abs():
143-
for itype in np.core.sctypes['int']:
144+
for itype in sctypes['int']:
144145
info = np.iinfo(itype)
145146
in_arr = np.array([info.min, info.max], dtype=itype)
146147
idtype = np.dtype(itype)
@@ -188,7 +189,7 @@ def test_able_int_type():
188189

189190
def test_able_casting():
190191
# Check the able_int_type function guesses numpy out type
191-
types = np.core.sctypes['int'] + np.core.sctypes['uint']
192+
types = sctypes['int'] + sctypes['uint']
192193
for in_type in types:
193194
in_info = np.iinfo(in_type)
194195
in_mn, in_mx = in_info.min, in_info.max

nibabel/tests/test_floating.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
longdouble_precision_improved,
1919
ok_floats,
2020
on_powerpc,
21+
sctypes,
2122
type_info,
2223
)
2324
from ..testing import suppress_warnings
@@ -43,7 +44,7 @@ def dtt2dict(dtt):
4344

4445
def test_type_info():
4546
# Test routine to get min, max, nmant, nexp
46-
for dtt in np.core.sctypes['int'] + np.core.sctypes['uint']:
47+
for dtt in sctypes['int'] + sctypes['uint']:
4748
info = np.iinfo(dtt)
4849
infod = type_info(dtt)
4950
assert infod == dict(
@@ -212,7 +213,7 @@ def test_int_to_float():
212213
def test_as_int_np_fix():
213214
# Test as_int works for integers. We need as_int for integers because of a
214215
# numpy 1.4.1 bug such that int(np.uint32(2**32-1) == -1
215-
for t in np.core.sctypes['int'] + np.core.sctypes['uint']:
216+
for t in sctypes['int'] + sctypes['uint']:
216217
info = np.iinfo(t)
217218
mn, mx = np.array([info.min, info.max], dtype=t)
218219
assert (mn, mx) == (as_int(mn), as_int(mx))

nibabel/tests/test_image_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def _check_array_caching(self, imaker, meth_name, caching):
403403
return
404404
# Return original array from get_fdata only if the input array is the
405405
# requested dtype.
406-
float_types = np.core.sctypes['float']
406+
float_types = sctypes['float']
407407
if arr_dtype not in float_types:
408408
return
409409
for float_type in float_types:

nibabel/tests/test_proxy_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
from .. import ecat, minc1, minc2, parrec
4242
from ..analyze import AnalyzeHeader
4343
from ..arrayproxy import ArrayProxy, is_proxy
44-
from ..casting import have_binary128
4544
from ..deprecator import ExpiredDeprecationError
45+
from ..casting import have_binary128, sctypes
4646
from ..externals.netcdf import netcdf_file
4747
from ..freesurfer.mghformat import MGHHeader
4848
from ..nifti1 import Nifti1Header
@@ -146,7 +146,7 @@ def validate_array_interface_with_dtype(self, pmaker, params):
146146
context.__enter__()
147147
warnings.simplefilter('ignore', np.ComplexWarning)
148148

149-
for dtype in np.core.sctypes['float'] + np.core.sctypes['int'] + np.core.sctypes['uint']:
149+
for dtype in sctypes['float'] + sctypes['int'] + sctypes['uint']:
150150
# Directly coerce with a dtype
151151
direct = dtype(prox)
152152
# Half-precision is imprecise. Obviously. It's a bad idea, but don't break

nibabel/tests/test_round_trip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from .. import Nifti1Header, Nifti1Image
1212
from ..arraywriters import ScalingError
13-
from ..casting import best_float, type_info, ulp
13+
from ..casting import best_float, sctypes, type_info, ulp
1414
from ..spatialimages import HeaderDataError, supported_np_types
1515

1616
DEBUG = False
@@ -102,7 +102,7 @@ def test_round_trip():
102102
rng = np.random.RandomState(20111121)
103103
N = 10000
104104
sd_10s = range(-20, 51, 5)
105-
iuint_types = np.core.sctypes['int'] + np.core.sctypes['uint']
105+
iuint_types = sctypes['int'] + sctypes['uint']
106106
# Remove types which cannot be set into nifti header datatype
107107
nifti_supported = supported_np_types(Nifti1Header())
108108
iuint_types = [t for t in iuint_types if t in nifti_supported]

nibabel/tests/test_scaling.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pytest
1616
from numpy.testing import assert_array_almost_equal, assert_array_equal
1717

18-
from ..casting import type_info
18+
from ..casting import sctypes, type_info
1919
from ..testing import suppress_warnings
2020
from ..volumeutils import apply_read_scaling, array_from_file, array_to_file, finite_range
2121
from .test_volumeutils import _calculate_scale
@@ -177,8 +177,8 @@ def test_array_file_scales(in_type, out_type):
177177
],
178178
)
179179
def test_scaling_in_abstract(category0, category1, overflow):
180-
for in_type in np.core.sctypes[category0]:
181-
for out_type in np.core.sctypes[category1]:
180+
for in_type in sctypes[category0]:
181+
for out_type in sctypes[category1]:
182182
if overflow:
183183
with suppress_warnings():
184184
check_int_a2f(in_type, out_type)
@@ -191,7 +191,7 @@ def check_int_a2f(in_type, out_type):
191191
big_floater = np.maximum_sctype(np.float64)
192192
info = type_info(in_type)
193193
this_min, this_max = info['min'], info['max']
194-
if not in_type in np.core.sctypes['complex']:
194+
if not in_type in sctypes['complex']:
195195
data = np.array([this_min, this_max], in_type)
196196
# Bug in numpy 1.6.2 on PPC leading to infs - abort
197197
if not np.all(np.isfinite(data)):

nibabel/tests/test_testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_assert_allclose_safely():
4848
with pytest.raises(AssertionError):
4949
assert_allclose_safely(a, b)
5050
# Test allcloseness of inf, especially np.float128 infs
51-
for dtt in np.core.sctypes['float']:
51+
for dtt in sctypes['float']:
5252
a = np.array([-np.inf, 1, np.inf], dtype=dtt)
5353
b = np.array([-np.inf, 1, np.inf], dtype=dtt)
5454
assert_allclose_safely(a, b)

nibabel/tests/test_volumeutils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
suppress_warnings,
3333
)
3434

35-
from ..casting import OK_FLOATS, floor_log2, shared_range, type_info
35+
from ..casting import OK_FLOATS, floor_log2, sctypes, shared_range, type_info
3636
from ..openers import BZ2File, ImageOpener, Opener
3737
from ..optpkg import optional_package
3838
from ..tmpdirs import InTemporaryDirectory
@@ -59,12 +59,12 @@
5959

6060
pyzstd, HAVE_ZSTD, _ = optional_package('pyzstd')
6161

62-
#: convenience variables for numpy types
63-
FLOAT_TYPES = np.core.sctypes['float']
64-
COMPLEX_TYPES = np.core.sctypes['complex']
62+
# convenience variables for numpy types
63+
FLOAT_TYPES = sctypes['float']
64+
COMPLEX_TYPES = sctypes['complex']
6565
CFLOAT_TYPES = FLOAT_TYPES + COMPLEX_TYPES
66-
INT_TYPES = np.core.sctypes['int']
67-
IUINT_TYPES = INT_TYPES + np.core.sctypes['uint']
66+
INT_TYPES = sctypes['int']
67+
IUINT_TYPES = INT_TYPES + sctypes['uint']
6868
NUMERIC_TYPES = CFLOAT_TYPES + IUINT_TYPES
6969

7070
FP_RUNTIME_WARN = Version(np.__version__) >= Version('1.24.0.dev0+239')
@@ -598,7 +598,7 @@ def test_a2f_nanpos():
598598
def test_a2f_bad_scaling():
599599
# Test that pathological scalers raise an error
600600
NUMERICAL_TYPES = sum(
601-
[np.core.sctypes[key] for key in ['int', 'uint', 'float', 'complex']], []
601+
[sctypes[key] for key in ['int', 'uint', 'float', 'complex']], []
602602
)
603603
for in_type, out_type, slope, inter in itertools.product(
604604
NUMERICAL_TYPES,
@@ -832,10 +832,10 @@ def check_against(f1, f2):
832832
return f1 if FLOAT_TYPES.index(f1) >= FLOAT_TYPES.index(f2) else f2
833833

834834
for first in FLOAT_TYPES:
835-
for other in IUINT_TYPES + np.core.sctypes['complex']:
835+
for other in IUINT_TYPES + sctypes['complex']:
836836
assert better_float_of(first, other) == first
837837
assert better_float_of(other, first) == first
838-
for other2 in IUINT_TYPES + np.core.sctypes['complex']:
838+
for other2 in IUINT_TYPES + sctypes['complex']:
839839
assert better_float_of(other, other2) == np.float32
840840
assert better_float_of(other, other2, np.float64) == np.float64
841841
for second in FLOAT_TYPES:

0 commit comments

Comments
 (0)