Skip to content

Commit 33f6369

Browse files
committed
RF - remove flt2mant in favor of type_info
flt2mant too small to be fun to leave in the API
1 parent 2334a25 commit 33f6369

File tree

3 files changed

+19
-45
lines changed

3 files changed

+19
-45
lines changed

nibabel/casting.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,6 @@ def shared_range(flt_type, int_type):
154154
except AttributeError: # float16 not present in np < 1.6
155155
_float16 = None
156156

157-
# The number of significand digits in IEEE floating point formats, not including
158-
# the implicit leading 0. See http://en.wikipedia.org/wiki/IEEE_754-2008
159-
_flt_nmant = {
160-
_float16: 10,
161-
np.float32: 23,
162-
np.float64: 52,
163-
}
164-
165157

166158
class FloatingError(Exception):
167159
pass
@@ -234,26 +226,6 @@ def type_info(np_type):
234226
nexp=info.nexp, width=width)
235227

236228

237-
def flt2nmant(flt_type):
238-
""" Number of significand bits in float type `flt_type`
239-
240-
Parameters
241-
----------
242-
flt_type : object
243-
Numpy floating point type, such as np.float32
244-
245-
Returns
246-
-------
247-
nmant : int
248-
Number of digits in the signficand
249-
"""
250-
try:
251-
return _flt_nmant[flt_type]
252-
except KeyError:
253-
pass
254-
return type_info(flt_type)['nmant']
255-
256-
257229
def as_int(x, check=True):
258230
""" Return python integer representation of number
259231
@@ -398,7 +370,7 @@ def floor_exact(val, flt_type):
398370
# Float casting has made the value go down or stay the same
399371
return sign * faval
400372
# Float casting made the value go up
401-
nmant = flt2nmant(flt_type)
373+
nmant = type_info(flt_type)['nmant']
402374
biggest_gap = 2**(floor_log2(aval) - nmant)
403375
assert biggest_gap > 1
404376
faval -= flt_type(biggest_gap)

nibabel/tests/test_floating.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"""
33
import numpy as np
44

5-
from ..casting import (floor_exact, flt2nmant, as_int, FloatingError,
6-
int_to_float, floor_log2, type_info)
5+
from ..casting import (floor_exact, as_int, FloatingError, int_to_float,
6+
floor_log2, type_info)
77

88
from nose import SkipTest
99
from nose.tools import assert_equal, assert_raises
@@ -60,11 +60,11 @@ def test_type_info():
6060
infod)
6161

6262

63-
def test_flt2nmant():
63+
def test_nmant():
6464
for t in IEEE_floats:
65-
assert_equal(flt2nmant(t), np.finfo(t).nmant)
65+
assert_equal(type_info(t)['nmant'], np.finfo(t).nmant)
6666
if (LD_INFO.nmant, LD_INFO.nexp) == (63, 15):
67-
assert_equal(flt2nmant(np.longdouble), 63)
67+
assert_equal(type_info(np.longdouble)['nmant'], 63)
6868

6969

7070
def test_as_int():
@@ -81,7 +81,7 @@ def test_as_int():
8181
# longdouble appears to have 52 bit precision, but we avoid that by checking
8282
# for known precisions that are less than that required
8383
try:
84-
nmant = flt2nmant(np.longdouble)
84+
nmant = type_info(np.longdouble)['nmant']
8585
except FloatingError:
8686
nmant = 63 # Unknown precision, let's hope it's at least 63
8787
v = np.longdouble(2) ** (nmant + 1) - 1
@@ -97,7 +97,7 @@ def test_int_to_float():
9797
# Concert python integer to floating point
9898
# Standard float types just return cast value
9999
for ie3 in IEEE_floats:
100-
nmant = flt2nmant(ie3)
100+
nmant = type_info(ie3)['nmant']
101101
for p in range(nmant + 3):
102102
i = 2**p+1
103103
assert_equal(int_to_float(i, ie3), ie3(i))
@@ -116,7 +116,8 @@ def test_int_to_float():
116116
LD = np.longdouble
117117
# up to integer precision of float64 nmant, we get the same result as for
118118
# casting directly
119-
for p in range(flt2nmant(np.float64)+2): # implicit
119+
nmant = type_info(np.float64)['nmant']
120+
for p in range(nmant+2): # implicit
120121
i = 2**p-1
121122
assert_equal(int_to_float(i, LD), LD(i))
122123
assert_equal(int_to_float(-i, LD), LD(-i))
@@ -128,7 +129,7 @@ def test_int_to_float():
128129
assert_raises(OverflowError, int_to_float, smn64, LD)
129130
assert_raises(OverflowError, int_to_float, smx64, LD)
130131
try:
131-
nmant = flt2nmant(np.longdouble)
132+
nmant = type_info(np.longdouble)['nmant']
132133
except FloatingError: # don't know where to test
133134
return
134135
# Assuming nmant is greater than that for float64, test we recover precision
@@ -169,7 +170,7 @@ def test_floor_exact_64():
169170
def test_floor_exact():
170171
to_test = IEEE_floats + [float]
171172
try:
172-
flt2nmant(np.longdouble)
173+
type_info(np.longdouble)['nmant']
173174
except FloatingError:
174175
# Significand bit count not reliable, don't test long double
175176
pass
@@ -181,7 +182,7 @@ def test_floor_exact():
181182
for t in to_test:
182183
# A number bigger than the range returns the max
183184
assert_equal(floor_exact(2**5000, t), np.finfo(t).max)
184-
nmant = flt2nmant(t)
185+
nmant = type_info(t)['nmant']
185186
for i in range(nmant+1):
186187
iv = 2**i
187188
# up to 2**nmant should be exactly representable

nibabel/tests/test_utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
FLOAT_TYPES,
3535
NUMERIC_TYPES)
3636

37-
from ..casting import flt2nmant, FloatingError, floor_log2
37+
from ..casting import FloatingError, floor_log2, type_info
3838

3939
from numpy.testing import (assert_array_almost_equal,
4040
assert_array_equal)
@@ -306,7 +306,8 @@ def test_apply_scaling():
306306
# Normally this would not upcast
307307
assert_equal((i16_arr * big).dtype, np.float32)
308308
# An equivalent case is a little hard to find for the intercept
309-
big_delta = np.float32(2**(floor_log2(big)-flt2nmant(np.float32)))
309+
nmant_32 = type_info(np.float32)['nmant']
310+
big_delta = np.float32(2**(floor_log2(big)-nmant_32))
310311
assert_equal((i16_arr * big_delta + big).dtype, np.float32)
311312
# Upcasting does occur with this routine
312313
assert_equal(apply_read_scaling(i16_arr, big).dtype, np.float64)
@@ -381,9 +382,9 @@ def check_against(f1, f2):
381382
def have_longer_double():
382383
# True if longdouble has more precision than float64, and longdouble is
383384
# something we can rely on
384-
nmant_64 = flt2nmant(np.float64) # should be 52
385+
nmant_64 = type_info(np.float64)['nmant'] # should be 52
385386
try:
386-
nmant_ld = flt2nmant(np.longdouble)
387+
nmant_ld = type_info(np.longdouble)['nmant']
387388
except FloatingError:
388389
return False
389390
return nmant_ld > nmant_64
@@ -410,7 +411,7 @@ def test_best_write_scale_ftype():
410411
for lower_t, higher_t in best_vals:
411412
# Information on this float
412413
t_max = np.finfo(lower_t).max
413-
nmant = flt2nmant(lower_t) # number of significand digits
414+
nmant = type_info(lower_t)['nmant'] # number of significand digits
414415
big_delta = lower_t(2**(floor_log2(t_max) - nmant)) # delta below max
415416
# Even large values that don't overflow don't change output
416417
arr = np.array([0, t_max], dtype=lower_t)

0 commit comments

Comments
 (0)