Skip to content

Commit 54416e9

Browse files
committed
RF - rename int_clippers to shared_range
Added small test for inputs
1 parent a4c978a commit 54416e9

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

nibabel/casting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def float_to_int(arr, int_type, nan2zero=True, infmax=False):
7474
# Deal with scalar as input; fancy indexing needs 1D
7575
shape = arr.shape
7676
arr = np.atleast_1d(arr)
77-
mn, mx = int_clippers(flt_type, int_type)
77+
mn, mx = shared_range(flt_type, int_type)
7878
if nan2zero is None:
7979
seen_nans = False
8080
else:
@@ -97,7 +97,7 @@ def float_to_int(arr, int_type, nan2zero=True, infmax=False):
9797
# Cache range values
9898
_SHARED_RANGES = {}
9999

100-
def int_clippers(flt_type, int_type):
100+
def shared_range(flt_type, int_type):
101101
""" Min and max in float type that are >=min, <=max in integer type
102102
103103
This is not as easy as it sounds, because the float type may not be able to

nibabel/tests/test_casting.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33

44
import numpy as np
55

6-
from ..casting import (float_to_int, as_int, int_clippers, CastingError,
7-
int_to_float)
6+
from ..casting import (float_to_int, shared_range, CastingError, int_to_float,
7+
as_int)
88

99
from numpy.testing import (assert_array_almost_equal, assert_array_equal)
1010

1111
from nose.tools import (assert_true, assert_equal, assert_raises)
1212

1313

14-
def test_int_clippers():
14+
def test_shared_range():
1515
for ft in np.sctypes['float']:
1616
for it in np.sctypes['int'] + np.sctypes['uint']:
1717
# Test that going a bit above or below the calculated min and max
1818
# either generates the same number when cast, or the max int value
1919
# (if this system generates that) or something smaller (because of
2020
# overflow)
21-
mn, mx = int_clippers(ft, it)
21+
mn, mx = shared_range(ft, it)
2222
ovs = ft(mx) + np.arange(2048, dtype=ft)
2323
# Float16 can overflow to inf
2424
bit_bigger = ovs[np.isfinite(ovs)].astype(it)
2525
casted_mx = ft(mx).astype(it)
2626
imax = int(np.iinfo(it).max)
2727
thresh_overflow = False
2828
if casted_mx != imax:
29-
# The int_clippers have told us that they believe the imax does
29+
# The shared_range have told us that they believe the imax does
3030
# not have an exact representation.
3131
fimax = int_to_float(imax, ft)
3232
if np.isfinite(fimax):
@@ -53,7 +53,7 @@ def test_int_clippers():
5353
casted_mn = ft(mn).astype(it)
5454
imin = int(np.iinfo(it).min)
5555
if casted_mn != imin:
56-
# The int_clippers have told us that they believe the imin does
56+
# The shared_range have told us that they believe the imin does
5757
# not have an exact representation.
5858
fimin = int_to_float(imin, ft)
5959
if np.isfinite(fimin):
@@ -72,6 +72,13 @@ def test_int_clippers():
7272
assert_true(np.all((bit_smaller >= casted_mn)))
7373

7474

75+
def test_shared_range_inputs():
76+
# Check any dtype specifier will work as input
77+
rng0 = shared_range(np.float32, np.int32)
78+
assert_array_equal(rng0, shared_range('f4', 'i4'))
79+
assert_array_equal(rng0, shared_range(np.dtype('f4'), np.dtype('i4')))
80+
81+
7582
def test_casting():
7683
for ft in np.sctypes['float']:
7784
for it in np.sctypes['int'] + np.sctypes['uint']:
@@ -80,7 +87,7 @@ def test_casting():
8087
farr_orig = np.array(arr, dtype=ft)
8188
# We're later going to test if we modify this array
8289
farr = farr_orig.copy()
83-
mn, mx = int_clippers(ft, it)
90+
mn, mx = shared_range(ft, it)
8491
iarr = float_to_int(farr, it)
8592
# Dammit - for long doubles we need to jump through some hoops not
8693
# to round to numbers outside the range

0 commit comments

Comments
 (0)