1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
from numpy.testing import *
import numpy as N
set_package_path()
from io.recaster import sctype_attributes, Recaster, RecastError
restore_path()
try: # Python 2.3 support
from sets import Set as set
except:
pass
class test_recaster(NumpyTestCase):
def test_init(self):
# Setting sctype_list
R = Recaster()
assert set(R.sctype_list) == set(sctype_attributes().keys()), \
'Default recaster should include all system types'
T = N.float32
R = Recaster([T])
assert R.sctype_list == [T], 'Scalar type list not correctly set'
# Setting tolerances
R = Recaster()
tols = R.default_sctype_tols()
assert tols == R.sctype_tols, 'Unexpected tols dictionary'
F = N.finfo(T)
R = Recaster(sctype_tols={T: {
'rtol': F.eps*2,
'atol': F.tiny*2,
'silly': 'silly text'}})
assert R.sctype_tols[T]['rtol'] == F.eps*2, \
'Rtol not correctly set'
assert R.sctype_tols[T]['atol'] == F.tiny*2, \
'Atol not correctly set'
T = N.complex128
F = N.finfo(T)
assert R.sctype_tols[T]['rtol'] == F.eps, \
'Rtol defaults not correctly set'
assert R.sctype_tols[T]['atol'] == F.tiny, \
'Atol defaults not correctly set'
# Options
# Sctype size lists
# Integer sizes
# Cabable types
def test_cast_to_fp(self):
R = Recaster()
# Define expected type output from fp recast of value
sta = sctype_attributes()
inp_outp = (
(1, N.complex128, 'c', sta[N.complex128]['size'], 0, N.complex128),
(1, N.complex128, 'c', sta[N.complex128]['size'], 1, N.complex64),
(1, N.complex128, 'c', sta[N.complex64]['size'], 0, N.complex64),
(1, N.complex128, 'f', sta[N.float64]['size'], 0, N.float64),
(1.0+1j, N.complex128, 'f', sta[N.complex128]['size'], 0, None),
(1, N.float64, 'f', sta[N.float64]['size'], 0, N.float64),
(1, N.float64, 'f', sta[N.float64]['size'], 1, N.float32),
(1, N.float64, 'f', sta[N.float32]['size'], 0, N.float32),
(1, N.float64, 'c', sta[N.complex128]['size'], 0, N.complex128),
(1, N.float64, 'c', sta[N.complex128]['size'], 1, N.complex64),
(1, N.int32, 'f', sta[N.float64]['size'], 0, N.float64),
(1, N.int32, 'f', sta[N.float64]['size'], 1, N.float32),
(1, N.float64, 'f', 0, 0, None),
)
for value, inp, kind, max_size, continue_down, outp in inp_outp:
arr = N.array(value, dtype=inp)
arr = R.cast_to_fp(arr, kind, max_size, continue_down)
if outp is None:
assert arr is None, \
'Expected None from type %s, got %s' \
% (inp, arr.dtype.type)
continue
assert arr is not None, \
'Expected %s from %s, got None' % (outp, inp)
dtt = arr.dtype.type
assert dtt is outp, \
'Expected %s from %s, got %s' % (outp, inp, dtt)
def test_smallest_int_sctype(self):
# Smallest int sctype with full recaster
params = sctype_attributes()
RF = Recaster()
test_triples = [(N.uint8, 0, 255),
(N.int8, -128, 0),
(N.uint16, 0, params[N.uint16]['max']),
(N.int16, params[N.int16]['min'], 0),
(N.uint32, 0, params[N.uint32]['max']),
(N.int32, params[N.int32]['min'], 0),
(N.uint64, 0, params[N.uint64]['max']),
(N.int64, params[N.int64]['min'], 0)]
for T, mn, mx in test_triples:
rt = RF.smallest_int_sctype(mx, mn)
assert N.dtype(rt) == N.dtype(T), \
'Expected %s, got %s type' % (T, rt)
# Smallest int sctype with restricted recaster
mmax = params[N.int32]['max']
mmin = params[N.int32]['min']
RR = Recaster([N.int32])
for kind in ('int', 'uint'):
for T in N.sctypes[kind]:
mx = params[T]['max']
mn = params[T]['min']
rt = RR.smallest_int_sctype(mx, mn)
if mx <= mmax and mn >= mmin:
assert rt == N.int32, \
'Expected int32 type, got %s' % rt
else:
assert rt is None, \
'Expected None, got %s for %s' % (T, rt)
# Test preferred int flag
mx = 1000
mn = 0
rt = RF.smallest_int_sctype(mx, mn)
assert rt == N.int16, 'Expected int16, got %s' % rt
rt = RF.smallest_int_sctype(mx, mn, 'i')
assert rt == N.int16, 'Expected int16, got %s' % rt
rt = RF.smallest_int_sctype(mx, mn, prefer='u')
assert rt == N.uint16, 'Expected uint16, got %s' % rt
def test_recasts(self):
valid_types = [N.int32, N.complex128, N.float64]
# Test smallest
R = Recaster(valid_types, recast_options='smallest')
inp_outp = (
(1, N.complex128, N.int32),
(1, N.complex64, N.int32),
(1.0+1j, N.complex128, N.complex128),
(1.0+1j, N.complex64, N.complex128),
(1, N.float64, N.int32),
(1, N.float32, N.int32),
(1.1, N.float64, N.float64),
(-1e12, N.int64, N.float64),
)
self.run_io_recasts(R, inp_outp)
# Test only_if_none
R = Recaster(valid_types, recast_options='only_if_none')
inp_outp = (
(1, N.complex128, N.complex128),
(1, N.complex64, N.int32),
(1.0+1j, N.complex128, N.complex128),
(1.0+1j, N.complex64, N.complex128),
(1, N.float64, N.float64),
(1, N.float32, N.int32),
(1.1, N.float64, N.float64),
(-1e12, N.int64, N.float64),
)
self.run_io_recasts(R, inp_outp)
# Test preserve_precision
R = Recaster(valid_types, recast_options='preserve_precision')
inp_outp = (
(1, N.complex128, N.complex128),
(1, N.complex64, N.complex128),
(1.0+1j, N.complex128, N.complex128),
(1.0+1j, N.complex64, N.complex128),
(1, N.float64, N.float64),
(1, N.float32, N.float64),
(1.1, N.float64, N.float64),
(-1e12, N.int64, None),
)
self.run_io_recasts(R, inp_outp)
def run_io_recasts(self, R, inp_outp):
''' Runs sets of value, input, output tests '''
for value, inp, outp in inp_outp:
arr = N.array(value, inp)
if outp is None:
self.assertRaises(RecastError, R.recast, arr)
continue
arr = R.recast(N.array(value, inp))
assert arr is not None, \
'Expected %s from %s, got None' % (outp, inp)
dtt = arr.dtype.type
assert dtt is outp, \
'Expected %s from %s, got %s' % (outp, inp, dtt)
|