def test_dtype_eq(self): from numpypy import dtype assert dtype("int8") == "int8" assert "int8" == dtype("int8") raises(TypeError, lambda: dtype("int8") == 3) assert dtype(bool) == bool
def test_eye(self): from numpypy import eye, int32, dtype a = eye(0) assert len(a) == 0 assert a.dtype == dtype('float64') assert a.shape == (0, 0) b = eye(1, dtype=int32) assert len(b) == 1 assert b[0][0] == 1 assert b.shape == (1, 1) assert b.dtype == dtype('int32') c = eye(2) assert c.shape == (2, 2) assert (c == [[1, 0], [0, 1]]).all() d = eye(3, dtype='int32') assert d.shape == (3, 3) assert d.dtype == dtype('int32') assert (d == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]).all() e = eye(3, 4) assert e.shape == (3, 4) assert (e == [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]).all() f = eye(2, 4, k=3) assert f.shape == (2, 4) assert (f == [[0, 0, 0, 1], [0, 0, 0, 0]]).all() g = eye(3, 4, k=-1) assert g.shape == (3, 4) assert (g == [[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]]).all()
def test_dtype_with_types(self): from numpypy import dtype assert dtype(bool).num == 0 assert dtype(int).num == 7 assert dtype(long).num == 9 assert dtype(float).num == 12
def test_dtype(self): from numpypy import dtype d = dtype('?') assert d.num == 0 assert d.kind == 'b' assert dtype('int8').num == 1 assert dtype(d) is d assert dtype(None) is dtype(float) raises(TypeError, dtype, 1042)
def test_str_dtype(self): from numpypy import dtype, str_ raises(TypeError, "dtype('Sx')") d = dtype('S8') assert d.itemsize == 8 assert dtype(str) == dtype('S') assert d.kind == 'S' assert d.type is str_ assert d.name == "string64" assert d.num == 18
def test_unicode_dtype(self): from numpypy import dtype, unicode_ raises(TypeError, "dtype('Ux')") d = dtype('U8') assert d.itemsize == 8 * 4 assert dtype(unicode) == dtype('U') assert d.kind == 'U' assert d.type is unicode_ assert d.name == "unicode256" assert d.num == 19
def test_int32(self): import sys import numpypy as numpy x = numpy.int32(23) assert x == 23 assert numpy.int32(2147483647) == 2147483647 assert numpy.int32('2147483647') == 2147483647 if sys.maxint > 2 ** 31 - 1: assert numpy.int32(2147483648) == -2147483648 assert numpy.int32('2147483648') == -2147483648 else: raises(OverflowError, numpy.int32, 2147483648) raises(OverflowError, numpy.int32, '2147483648') assert numpy.dtype('int32') is numpy.dtype(numpy.int32)
def test_repr_str(self): from numpypy import dtype assert '.dtype' in repr(dtype) d = dtype('?') assert repr(d) == "dtype('bool')" assert str(d) == "bool"
def test_int_(self): import numpypy as numpy assert numpy.int_ is numpy.dtype(int).type assert numpy.int_.mro() == [numpy.int_, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, int, object]
def test_div(self): from math import isnan from numpypy import array, dtype, inf a = array(range(1, 6)) b = a / a for i in range(5): assert b[i] == 1 a = array(range(1, 6), dtype=bool) b = a / a assert b.dtype is dtype("int8") for i in range(5): assert b[i] == 1 a = array([-1, 0, 1]) b = array([0, 0, 0]) c = a / b assert (c == [0, 0, 0]).all() a = array([-1.0, 0.0, 1.0]) b = array([0.0, 0.0, 0.0]) c = a / b assert c[0] == -inf assert isnan(c[1]) assert c[2] == inf b = array([-0.0, -0.0, -0.0]) c = a / b assert c[0] == inf assert isnan(c[1]) assert c[2] == -inf
def test_sort_corner_cases_string_records(self): skip('not implemented yet') from numpypy import array, dtype # test string sorts. s = 'aaaaaaaa' a = array([s + chr(i) for i in range(101)]) b = a[::-1].copy() for kind in ['q', 'm', 'h'] : msg = "string sort, kind=%s" % kind c = a.copy(); c.sort(kind=kind) assert (c == a).all(), msg c = b.copy(); c.sort(kind=kind) assert (c == a).all(), msg # test record array sorts. dt =dtype([('f', float), ('i', int)]) a = array([(i, i) for i in range(101)], dtype = dt) b = a[::-1] for kind in ['q', 'h', 'm'] : msg = "object sort, kind=%s" % kind c = a.copy(); c.sort(kind=kind) assert (c == a).all(), msg c = b.copy(); c.sort(kind=kind) assert (c == a).all(), msg
def test_alternate_constructs(self): from numpypy import dtype nnp = self.non_native_prefix byteorder = self.native_prefix assert dtype('i8') == dtype(byteorder + 'i8') == dtype('=i8') # XXX should be equal == dtype(long) assert dtype(nnp + 'i8') != dtype('i8') assert dtype(nnp + 'i8').byteorder == nnp assert dtype('=i8').byteorder == '=' assert dtype(byteorder + 'i8').byteorder == '='
def test_scalar(self): from numpypy import array, dtype a = array(3) #assert a[0] == 3 raises(IndexError, "a[0]") assert a.size == 1 assert a.shape == () assert a.dtype is dtype(int)
def test_shift(self): from numpypy import left_shift, right_shift, dtype assert (left_shift([5, 1], [2, 13]) == [20, 2**13]).all() assert (right_shift(10, range(5)) == [10, 5, 2, 1, 0]).all() bool_ = dtype('bool').type assert left_shift(bool(1), 3) == left_shift(1, 3) assert right_shift(bool(1), 3) == right_shift(1, 3)
def test_bool_binop_types(self): from numpypy import array, dtype types = [ '?', 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'f', 'd' ] a = array([True], '?') for t in types: assert (a + array([0], t)).dtype is dtype(t)
def test_add_uint32(self): from numpypy import array, dtype a = array(range(5), dtype="I") b = a + a assert b.dtype is dtype("I") for i in range(5): assert b[i] == i * 2
def test_abstract_types(self): import numpypy as numpy raises(TypeError, numpy.generic, 0) raises(TypeError, numpy.number, 0) raises(TypeError, numpy.integer, 0) exc = raises(TypeError, numpy.signedinteger, 0) assert 'cannot create' in str(exc.value) assert 'signedinteger' in str(exc.value) exc = raises(TypeError, numpy.unsignedinteger, 0) assert 'cannot create' in str(exc.value) assert 'unsignedinteger' in str(exc.value) raises(TypeError, numpy.floating, 0) raises(TypeError, numpy.inexact, 0) # numpy allows abstract types in array creation a_n = numpy.array([4,4], numpy.number) a_i = numpy.array([4,4], numpy.integer) a_s = numpy.array([4,4], numpy.signedinteger) a_u = numpy.array([4,4], numpy.unsignedinteger) assert a_n.dtype.num == 12 assert a_i.dtype.num == 7 assert a_s.dtype.num == 7 assert a_u.dtype.num == 8 assert a_n.dtype is numpy.dtype('float64') if self.ptr_size == 4: assert a_i.dtype is numpy.dtype('int32') assert a_s.dtype is numpy.dtype('int32') assert a_u.dtype is numpy.dtype('uint32') else: assert a_i.dtype is numpy.dtype('int64') assert a_s.dtype is numpy.dtype('int64') assert a_u.dtype is numpy.dtype('uint64')
def test_create(self): from numpypy import dtype, void raises(ValueError, "dtype([('x', int), ('x', float)])") d = dtype([("x", "int32"), ("y", "int32"), ("z", "int32"), ("value", float)]) assert d.fields['x'] == (dtype('int32'), 0) assert d.fields['value'] == (dtype(float), 12) assert d['x'] == dtype('int32') assert d.name == "void160" assert d.num == 20 assert d.itemsize == 20 assert d.kind == 'V' assert d.base == d assert d.type is void assert d.char == 'V' assert d.names == ("x", "y", "z", "value") raises(KeyError, 'd["xyz"]') raises(KeyError, 'd.fields["xyz"]')
def test_complex(self): import numpypy as numpy assert numpy.complex_ is numpy.complex128 assert numpy.cfloat is numpy.complex64 assert numpy.complex64.__mro__ == (numpy.complex64, numpy.complexfloating, numpy.inexact, numpy.number, numpy.generic, object) assert numpy.complex128.__mro__ == (numpy.complex128, numpy.complexfloating, numpy.inexact, numpy.number, numpy.generic, complex, object) assert numpy.dtype(complex).type is numpy.complex128 assert numpy.dtype("complex").type is numpy.complex128 d = numpy.dtype('complex64') assert d.kind == 'c' assert d.num == 14 assert d.char == 'F'
def test_identity(self): from numpypy import array, int32, float64, dtype, identity a = identity(0) assert len(a) == 0 assert a.dtype == dtype('float64') assert a.shape == (0, 0) b = identity(1, dtype=int32) assert len(b) == 1 assert b[0][0] == 1 assert b.shape == (1, 1) assert b.dtype == dtype('int32') c = identity(2) assert c.shape == (2, 2) assert (c == [[1, 0], [0, 1]]).all() d = identity(3, dtype='int32') assert d.shape == (3, 3) assert d.dtype == dtype('int32') assert (d == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]).all()
def test_dtype_guessing(self): from numpypy import array, dtype assert array([True]).dtype is dtype(bool) assert array([True, False]).dtype is dtype(bool) assert array([True, 1]).dtype is dtype(int) assert array([1, 2, 3]).dtype is dtype(int) assert array([1L, 2, 3]).dtype is dtype(long) assert array([1.2, True]).dtype is dtype(float) assert array([1.2, 5]).dtype is dtype(float) assert array([]).dtype is dtype(float)
def test_comparison(self): import operator from numpypy import array, dtype a = array(range(5)) b = array(range(5), float) for func in [ operator.eq, operator.ne, operator.lt, operator.le, operator.gt, operator.ge ]: c = func(a, 3) assert c.dtype is dtype(bool) for i in xrange(5): assert c[i] == func(a[i], 3) c = func(b, 3) assert c.dtype is dtype(bool) for i in xrange(5): assert c[i] == func(b[i], 3)
def test_arange(self): from numpypy import arange, array, dtype a = arange(3) assert (a == [0, 1, 2]).all() assert a.dtype is dtype(int) a = arange(3.0) assert (a == [0., 1., 2.]).all() assert a.dtype is dtype(float) a = arange(3, 7) assert (a == [3, 4, 5, 6]).all() assert a.dtype is dtype(int) a = arange(3, 7, 2) assert (a == [3, 5]).all() a = arange(3, dtype=float) assert (a == [0., 1., 2.]).all() assert a.dtype is dtype(float) a = arange(0, 0.8, 0.1) assert len(a) == 8 assert arange(False, True, True).dtype is dtype(int)
def test_pickle_record(self): from numpypy import array, dtype from cPickle import loads, dumps d = dtype([("x", "int32"), ("y", "int32"), ("z", "int32"), ("value", float)]) assert d.__reduce__() == (dtype, ('V20', 0, 1), (3, '<', None, ('x', 'y', 'z', 'value'), {'y': (dtype('int32'), 4), 'x': (dtype('int32'), 0), 'z': (dtype('int32'), 8), 'value': (dtype('float64'), 12)}, 20, 1, 0)) new_d = loads(dumps(d)) assert new_d.__reduce__() == d.__reduce__()
def test_overflow(self): from numpypy import array, dtype assert array([128], 'b')[0] == -128 assert array([256], 'B')[0] == 0 assert array([32768], 'h')[0] == -32768 assert array([65536], 'H')[0] == 0 if dtype('l').itemsize == 4: # 32-bit raises(OverflowError, "array([2**32/2], 'i')") raises(OverflowError, "array([2**32], 'I')") raises(OverflowError, "array([2**64/2], 'q')") raises(OverflowError, "array([2**64], 'Q')")
def test_create_subarrays(self): from numpypy import dtype d = dtype([("x", "float", (2,)), ("y", "int64", (2,))]) assert d.itemsize == 32 assert d.name == "void256" keys = d.fields.keys() assert "x" in keys assert "y" in keys assert d["x"].shape == (2,) assert d["x"].itemsize == 16 e = dtype([("x", "float", 2), ("y", "int", 2)]) assert e.fields.keys() == keys assert e['x'].shape == (2,) dt = dtype((float, 10)) assert dt.shape == (10,) assert dt.kind == 'V' assert dt.fields == None assert dt.subdtype == (dtype(float), (10,)) assert dt.base == dtype(float)
def test_accumulate(self): from numpypy import add, subtract, multiply, divide, arange, dtype assert (add.accumulate([2, 3, 5]) == [2, 5, 10]).all() assert (multiply.accumulate([2, 3, 5]) == [2, 6, 30]).all() a = arange(4).reshape(2,2) b = add.accumulate(a, axis=0) assert (b == [[0, 1], [2, 4]]).all() b = add.accumulate(a, 1) assert (b == [[0, 1], [2, 5]]).all() b = add.accumulate(a) #default axis is 0 assert (b == [[0, 1], [2, 4]]).all() # dtype a = arange(0, 3, 0.5).reshape(2, 3) b = add.accumulate(a, dtype=int, axis=1) print b assert (b == [[0, 0, 1], [1, 3, 5]]).all() assert b.dtype == int assert add.accumulate([True]*200)[-1] == 200 assert add.accumulate([True]*200).dtype == dtype('int') assert subtract.accumulate([True]*200).dtype == dtype('bool') assert divide.accumulate([True]*200).dtype == dtype('int8')
def test_binop_types(self): from numpypy import array, dtype tests = [('b','B','h'), ('b','h','h'), ('b','H','i'), ('b','i','i'), ('b','l','l'), ('b','q','q'), ('b','Q','d'), ('B','h','h'), ('B','H','H'), ('B','i','i'), ('B','I','I'), ('B','l','l'), ('B','L','L'), ('B','q','q'), ('B','Q','Q'), ('h','H','i'), ('h','i','i'), ('h','l','l'), ('h','q','q'), ('h','Q','d'), ('H','i','i'), ('H','I','I'), ('H','l','l'), ('H','L','L'), ('H','q','q'), ('H','Q','Q'), ('i','l','l'), ('i','q','q'), ('i','Q','d'), ('I','L','L'), ('I','q','q'), ('I','Q','Q'), ('q','Q','d'), ('b','f','f'), ('B','f','f'), ('h','f','f'), ('H','f','f'), ('i','f','d'), ('I','f','d'), ('l','f','d'), ('L','f','d'), ('q','f','d'), ('Q','f','d'), ('q','d','d')] if dtype('i').itemsize == dtype('l').itemsize: # 32-bit tests.extend([('b','I','q'), ('b','L','q'), ('h','I','q'), ('h','L','q'), ('i','I','q'), ('i','L','q')]) else: tests.extend([('b','I','l'), ('b','L','d'), ('h','I','l'), ('h','L','d'), ('i','I','l'), ('i','L','d')]) for d1, d2, dout in tests: assert (array([1], d1) + array([1], d2)).dtype is dtype(dout)
def test_pickle_record_subarrays(self): from numpypy import array, dtype from cPickle import loads, dumps d = dtype([("x", "int32", (3,)), ("y", "int32", (2,)), ("z", "int32", (4,)), ("value", float, (5,))]) new_d = loads(dumps(d)) keys = d.fields.keys() keys.sort() assert keys == ["value", "x", "y", "z"] assert new_d.itemsize == d.itemsize == 76
def test_dtype_str(self): from numpypy import dtype byteorder = self.native_prefix assert dtype('i8').str == byteorder + 'i8' assert dtype('<i8').str == '<i8' assert dtype('>i8').str == '>i8' assert dtype('int8').str == '|i1' assert dtype('float').str == byteorder + 'f8' # strange assert dtype('string').str == '|S0' assert dtype('unicode').str == byteorder + 'U0'
def test_sort_bigendian(self): from numpy import array, dtype a = array(range(11), dtype='float64') c = a.astype(dtype('<f8')) c.sort() assert max(abs(a-c)) < 1e-32