1313
1414from nose .tools import assert_true
1515
16+ from numpy .testing import assert_array_equal , assert_almost_equal
17+
1618DEBUG = True
1719
1820def round_trip (arr , out_dtype ):
@@ -48,6 +50,8 @@ def big_bad_ulp(arr):
4850 I haven't thought about whether the vectorized log2 here could lead to
4951 incorrect rounding; this only needs to be ballpark
5052
53+ This function might be used in nipy/io/tests/test_image_io.py
54+
5155 Parameters
5256 ----------
5357 arr : array
@@ -60,15 +64,29 @@ def big_bad_ulp(arr):
6064 """
6165 # Assumes array is floating point
6266 arr = np .asarray (arr )
67+ info = type_info (arr .dtype )
6368 working_arr = np .abs (arr .astype (BFT ))
6469 # Log2 for numpy < 1.3
65- l2 = np .log (working_arr ) / LOGe2
66- fl2 = np .floor (l2 )
67- info = type_info (arr .dtype )
70+ fl2 = np .zeros_like (working_arr ) + info ['minexp' ]
71+ # Avoid divide by zero error for log of 0
72+ nzs = working_arr > 0
73+ fl2 [nzs ] = np .floor (np .log (working_arr [nzs ]) / LOGe2 )
6874 fl2 = np .clip (fl2 , info ['minexp' ], np .inf )
6975 return 2 ** (fl2 - info ['nmant' ])
7076
7177
78+ def test_big_bad_ulp ():
79+ for ftype in (np .float32 , np .float64 ):
80+ ti = type_info (ftype )
81+ fi = np .finfo (ftype )
82+ min_ulp = 2 ** (ti ['minexp' ] - ti ['nmant' ])
83+ in_arr = np .zeros ((10 ,), dtype = ftype )
84+ in_arr = np .array ([0 , 0 , 1 , 2 , 4 , 5 , - 5 , - np .inf , np .inf ], dtype = ftype )
85+ out_arr = [min_ulp , min_ulp , fi .eps , fi .eps * 2 , fi .eps * 4 ,
86+ fi .eps * 4 , fi .eps * 4 , np .inf , np .inf ]
87+ assert_array_equal (big_bad_ulp (in_arr ), out_arr )
88+
89+
7290BIG_FLOAT = np .float64
7391
7492def test_round_trip ():
0 commit comments