# -*- encoding: utf-8 -*- import platform import py import sys from pypy.conftest import option from pypy.module.micronumpy.appbridge import get_appbridge_cache from pypy.module.micronumpy.strides import Chunk, new_view, EllipsisChunk from pypy.module.micronumpy.ndarray import W_NDimArray import pypy.module.micronumpy.constants as NPY from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest class MockDtype(object): class itemtype(object): @staticmethod def malloc(size, zero=True): return None def __init__(self): self.base = self self.elsize = 1 self.num = 0 def create_slice(space, a, chunks): if not any(isinstance(c, EllipsisChunk) for c in chunks): chunks.append(EllipsisChunk()) return new_view(space, W_NDimArray(a), chunks).implementation def create_array(*args, **kwargs): return W_NDimArray.from_shape(*args, **kwargs).implementation class TestNumArrayDirect(object): def newslice(self, *args): return self.space.newslice(*[self.space.wrap(arg) for arg in args]) def newtuple(self, *args): args_w = [] for arg in args: if isinstance(arg, int): args_w.append(self.space.wrap(arg)) else: args_w.append(arg) return self.space.newtuple(args_w) def test_strides_f(self): a = create_array(self.space, [10, 5, 3], MockDtype(), order=NPY.FORTRANORDER) assert a.strides == [1, 10, 50] assert a.backstrides == [9, 40, 100] def test_strides_c(self): a = create_array(self.space, [10, 5, 3], MockDtype(), order=NPY.CORDER) assert a.strides == [15, 3, 1] assert a.backstrides == [135, 12, 2] a = create_array(self.space, [1, 0, 7], MockDtype(), order=NPY.CORDER) assert a.strides == [7, 7, 1] assert a.backstrides == [0, 0, 6] def test_create_slice_f(self): a = create_array(self.space, [10, 5, 3], MockDtype(), order=NPY.FORTRANORDER) s = create_slice(self.space, a, [Chunk(3, 0, 0, 1)]) assert s.start == 3 assert s.strides == [10, 50] assert s.backstrides == [40, 100] s = create_slice(self.space, a, [Chunk(1, 9, 2, 4)]) assert s.start == 1 assert s.strides == [2, 10, 50] assert s.backstrides == [6, 40, 100] s = create_slice(self.space, a, [Chunk(1, 5, 3, 2), Chunk(1, 2, 1, 1), Chunk(1, 0, 0, 1)]) assert s.shape == [2, 1] assert s.strides == [3, 10] assert s.backstrides == [3, 0] s = create_slice(self.space, a, [Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)]) assert s.start == 20 assert s.shape == [10, 3] def test_create_slice_c(self): a = create_array(self.space, [10, 5, 3], MockDtype(), order=NPY.CORDER) s = create_slice(self.space, a, [Chunk(3, 0, 0, 1)]) assert s.start == 45 assert s.strides == [3, 1] assert s.backstrides == [12, 2] s = create_slice(self.space, a, [Chunk(1, 9, 2, 4)]) assert s.start == 15 assert s.strides == [30, 3, 1] assert s.backstrides == [90, 12, 2] s = create_slice(self.space, a, [Chunk(1, 5, 3, 2), Chunk(1, 2, 1, 1), Chunk(1, 0, 0, 1)]) assert s.start == 19 assert s.shape == [2, 1] assert s.strides == [45, 3] assert s.backstrides == [45, 0] s = create_slice(self.space, a, [Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)]) assert s.start == 6 assert s.shape == [10, 3] def test_slice_of_slice_f(self): a = create_array(self.space, [10, 5, 3], MockDtype(), order=NPY.FORTRANORDER) s = create_slice(self.space, a, [Chunk(5, 0, 0, 1)]) assert s.start == 5 s2 = create_slice(self.space, s, [Chunk(3, 0, 0, 1)]) assert s2.shape == [3] assert s2.strides == [50] assert s2.parent is a assert s2.backstrides == [100] assert s2.start == 35 s = create_slice(self.space, a, [Chunk(1, 5, 3, 2)]) s2 = create_slice(self.space, s, [Chunk(0, 2, 1, 2), Chunk(2, 0, 0, 1)]) assert s2.shape == [2, 3] assert s2.strides == [3, 50] assert s2.backstrides == [3, 100] assert s2.start == 1 * 15 + 2 * 3 def test_slice_of_slice_c(self): a = create_array(self.space, [10, 5, 3], MockDtype(), order=NPY.CORDER) s = create_slice(self.space, a, [Chunk(5, 0, 0, 1)]) assert s.start == 15 * 5 s2 = create_slice(self.space, s, [Chunk(3, 0, 0, 1)]) assert s2.shape == [3] assert s2.strides == [1] assert s2.parent is a assert s2.backstrides == [2] assert s2.start == 5 * 15 + 3 * 3 s = create_slice(self.space, a, [Chunk(1, 5, 3, 2)]) s2 = create_slice(self.space, s, [Chunk(0, 2, 1, 2), Chunk(2, 0, 0, 1)]) assert s2.shape == [2, 3] assert s2.strides == [45, 1] assert s2.backstrides == [45, 2] assert s2.start == 1 * 15 + 2 * 3 def test_negative_step_f(self): a = create_array(self.space, [10, 5, 3], MockDtype(), order=NPY.FORTRANORDER) s = create_slice(self.space, a, [Chunk(9, -1, -2, 5)]) assert s.start == 9 assert s.strides == [-2, 10, 50] assert s.backstrides == [-8, 40, 100] def test_negative_step_c(self): a = create_array(self.space, [10, 5, 3], MockDtype(), order=NPY.CORDER) s = create_slice(self.space, a, [Chunk(9, -1, -2, 5)]) assert s.start == 135 assert s.strides == [-30, 3, 1] assert s.backstrides == [-120, 12, 2] def test_shape_agreement(self): from pypy.module.micronumpy.strides import _shape_agreement assert _shape_agreement([3], [3]) == [3] assert _shape_agreement([1, 2, 3], [1, 2, 3]) == [1, 2, 3] _shape_agreement([2], [3]) == 0 assert _shape_agreement([4, 4], []) == [4, 4] assert _shape_agreement([8, 1, 6, 1], [7, 1, 5]) == [8, 7, 6, 5] assert _shape_agreement([5, 2], [4, 3, 5, 2]) == [4, 3, 5, 2] def test_calc_new_strides(self): from pypy.module.micronumpy.strides import calc_new_strides assert calc_new_strides([2, 4], [4, 2], [4, 2], NPY.CORDER) == [8, 2] assert calc_new_strides([2, 4, 3], [8, 3], [1, 16], NPY.FORTRANORDER) == [1, 2, 16] assert calc_new_strides([2, 3, 4], [8, 3], [1, 16], NPY.FORTRANORDER) is None assert calc_new_strides([24], [2, 4, 3], [48, 6, 1], NPY.CORDER) is None assert calc_new_strides([24], [2, 4, 3], [24, 6, 2], NPY.CORDER) == [2] assert calc_new_strides([105, 1], [3, 5, 7], [35, 7, 1],NPY.CORDER) == [1, 1] assert calc_new_strides([1, 105], [3, 5, 7], [35, 7, 1],NPY.CORDER) == [105, 1] assert calc_new_strides([1, 105], [3, 5, 7], [35, 7, 1],NPY.FORTRANORDER) is None assert calc_new_strides([1, 1, 1, 105, 1], [15, 7], [7, 1],NPY.CORDER) == \ [105, 105, 105, 1, 1] assert calc_new_strides([1, 1, 105, 1, 1], [7, 15], [1, 7],NPY.FORTRANORDER) == \ [1, 1, 1, 105, 105] def test_find_shape(self): from pypy.module.micronumpy.ctors import find_shape_and_elems space = self.space shape, elems = find_shape_and_elems(space, space.newlist([space.wrap("a"), space.wrap("b")]), None) assert shape == [2] assert space.text_w(elems[0]) == "a" assert space.text_w(elems[1]) == "b" def test_from_shape_and_storage(self): from rpython.rlib.rawstorage import alloc_raw_storage, raw_storage_setitem from rpython.rtyper.lltypesystem import rffi from pypy.module.micronumpy.descriptor import get_dtype_cache storage = alloc_raw_storage(4, track_allocation=False, zero=True) for i in range(4): raw_storage_setitem(storage, i, rffi.cast(rffi.UCHAR, i)) # dtypes = get_dtype_cache(self.space) w_array = W_NDimArray.from_shape_and_storage(self.space, [2, 2], storage, dtypes.w_int8dtype, storage_bytes=4) def get(i, j): return w_array.getitem(self.space, [i, j]).value assert get(0, 0) == 0 assert get(0, 1) == 1 assert get(1, 0) == 2 assert get(1, 1) == 3 class AppTestNumArray(BaseNumpyAppTest): spaceconfig = dict(usemodules=["micronumpy", "struct", "binascii"]) def w_CustomIndexObject(self, index): class CustomIndexObject(object): def __init__(self, index): self.index = index def __index__(self): return self.index return CustomIndexObject(index) def w_CustomIndexIntObject(self, index, value): class CustomIndexIntObject(object): def __init__(self, index, value): self.index = index self.value = value def __index__(self): return self.index def __int__(self): return self.value return CustomIndexIntObject(index, value) def w_CustomIntObject(self, value): class CustomIntObject(object): def __init__(self, value): self.value = value def __index__(self): return self.value return CustomIntObject(value) def test_constants(self): import numpy as np assert np.MAXDIMS is 32 assert np.CLIP is 0 assert np.WRAP is 1 assert np.RAISE is 2 def test_creation(self): from numpy import ndarray, array, dtype, flatiter assert type(ndarray) is type assert repr(ndarray) == "" assert repr(flatiter) == "" assert type(array) is not type a = ndarray((2, 3)) assert a.shape == (2, 3) assert a.dtype == dtype(float) raises(TypeError, ndarray, [[1], [2], [3]]) a = ndarray(3, dtype=int) assert a.shape == (3,) assert a.dtype is dtype(int) a = ndarray([], dtype=float) assert a.shape == () # test uninitialized value crash? assert len(str(a)) > 0 x = array([[0, 2], [1, 1], [2, 0]]) y = array(x.T, dtype=float) assert (y == x.T).all() y = array(x.T, copy=False) assert (y == x.T).all() exc = raises(ValueError, ndarray, [1,2,256]*10000) assert exc.value[0] == 'sequence too large; cannot be greater than 32' exc = raises(ValueError, ndarray, [1,2,256]*10) assert exc.value[0] == 'array is too big.' def test_ndmin(self): from numpy import array arr = array([[[1]]], ndmin=1) assert arr.shape == (1, 1, 1) def test_noop_ndmin(self): from numpy import array arr = array([1], ndmin=3) assert arr.shape == (1, 1, 1) def test_array_init(self): import numpy as np a = np.array('123', dtype='int64') assert a == 123 assert a.dtype == np.int64 a = np.array('123', dtype='intp') assert a == 123 assert a.dtype == np.intp # required for numpy test suite raises(ValueError, np.array, type(a)) def test_array_copy(self): from numpy import array a = array(range(12)).reshape(3,4) b = array(a, ndmin=4) assert b.shape == (1, 1, 3, 4) b = array(a, copy=False) b[0, 0] = 100 assert a[0, 0] == 100 b = array(a, copy=True, ndmin=2) b[0, 0] = 0 assert a[0, 0] == 100 b = array(a, dtype=float) assert (b[0] == [100, 1, 2, 3]).all() assert b.dtype.kind == 'f' b = array(a, copy=False, ndmin=4) b[0,0,0,0] = 0 assert a[0, 0] == 0 a = array([[[]]]) # Simulate tiling an empty array, really tests repeat, reshape # b = tile(a, (3, 2, 5)) reps = (3, 4, 5) c = array(a, copy=False, subok=True, ndmin=len(reps)) d = c.reshape(3, 4, 0) e = d.repeat(3, 0) assert e.shape == (9, 4, 0) a = array(123) b = array(a, dtype=float) assert b == 123.0 a = array([[123, 456]]) assert a.flags['C'] b = array(a, order='K') assert b.flags['C'] assert (b == a).all() b = array(a, order='K', copy=True) assert b.flags['C'] assert (b == a).all() def test_unicode(self): import numpy as np a = np.array([3, u'Aÿ', ''], dtype='U3') assert a.shape == (3,) assert a.dtype == np.dtype('U3') assert a[0] == u'3' assert a[1] == u'Aÿ' def test_dtype_attribute(self): import numpy as np a = np.array(40000, dtype='uint16') assert a.dtype == np.uint16 a.dtype = np.int16 assert a == -25536 a = np.array([1, 2, 3, 4, 40000], dtype='uint16') assert a.dtype == np.uint16 a.dtype = np.int16 assert a[4] == -25536 exc = raises(ValueError, 'a.dtype = None') assert exc.value[0] == 'new type not compatible with array.' exc = raises(ValueError, 'a.dtype = np.int32') assert exc.value[0] == 'new type not compatible with array.' exc = raises(AttributeError, 'del a.dtype') assert exc.value[0] == 'Cannot delete array dtype' def test_buffer(self): import numpy as np a = np.array([1,2,3]) b = buffer(a) assert type(b) is buffer assert 'read-only buffer' in repr(b) exc = raises(TypeError, "b[0] = '0'") assert str(exc.value) == 'buffer is read-only' def test_type(self): from numpy import array ar = array(range(5)) assert type(ar) is type(ar + ar) def test_ndim(self): from numpy import array x = array(0.2) assert x.ndim == 0 x = array([1, 2]) assert x.ndim == 1 x = array([[1, 2], [3, 4]]) assert x.ndim == 2 x = array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) assert x.ndim == 3 # numpy actually raises an AttributeError, but numpy.raises an # TypeError raises((TypeError, AttributeError), 'x.ndim = 3') def test_zeros(self): from numpy import zeros a = zeros(15) # Check that storage was actually zero'd. assert a[10] == 0.0 # And check that changes stick. a[13] = 5.3 assert a[13] == 5.3 assert zeros(()) == 0 assert zeros(()).shape == () assert zeros((), dtype='S') == '' assert zeros((), dtype='S').shape == () assert zeros((), dtype='S').dtype == '|S1' assert zeros(5, dtype='U')[4] == u'' assert zeros(5, dtype='U').shape == (5,) assert zeros(5, dtype='U').dtype == '> 2 == [0, 0, 0, 0, 1, 1, 1, 1, 2, 2]).all() a = np.array([True, False]) assert (a >> 1 == [0, 0]).all() a = np.arange(3, dtype=float) raises(TypeError, lambda: a >> 1) a = np.array([123], dtype='uint64') b = a >> 1 assert b == 61 assert b.dtype.type is np.uint64 a = np.array(123, dtype='uint64') exc = raises(TypeError, "a >> 1") assert 'not supported for the input types' in exc.value.message def test_rrshift(self): from numpy import arange a = arange(5) assert (2 >> a == [2, 1, 0, 0, 0]).all() def test_pow(self): from numpy import array a = array(range(5), float) b = a ** a for i in range(5): assert b[i] == i ** i a = array(range(5)) assert (a ** 2 == a * a).all() def test_pow_other(self): from numpy import array a = array(range(5), float) b = array([2, 2, 2, 2, 2]) c = a ** b for i in range(5): assert c[i] == i ** 2 def test_pow_constant(self): from numpy import array a = array(range(5), float) b = a ** 2 for i in range(5): assert b[i] == i ** 2 def test_mod(self): from numpy import array a = array(range(1, 6)) b = a % a for i in range(5): assert b[i] == 0 a = array(range(1, 6), float) b = (a + 1) % a assert b[0] == 0 for i in range(1, 5): assert b[i] == 1 def test_mod_other(self): from numpy import array a = array(range(5)) b = array([2, 2, 2, 2, 2]) c = a % b for i in range(5): assert c[i] == i % 2 def test_mod_constant(self): from numpy import array a = array(range(5)) b = a % 2 for i in range(5): assert b[i] == i % 2 def test_rand(self): from numpy import arange a = arange(5) assert (3 & a == [0, 1, 2, 3, 0]).all() def test_ror(self): from numpy import arange a = arange(5) assert (3 | a == [3, 3, 3, 3, 7]).all() def test_xor(self): from numpy import arange a = arange(5) assert (a ^ 3 == [3, 2, 1, 0, 7]).all() def test_rxor(self): from numpy import arange a = arange(5) assert (3 ^ a == [3, 2, 1, 0, 7]).all() def test_pos(self): from numpy import array a = array([1., -2., 3., -4., -5.]) b = +a for i in range(5): assert b[i] == a[i] a = +array(range(5)) for i in range(5): assert a[i] == i def test_neg(self): from numpy import array a = array([1., -2., 3., -4., -5.]) b = -a for i in range(5): assert b[i] == -a[i] a = -array(range(5), dtype="int8") for i in range(5): assert a[i] == -i def test_abs(self): from numpy import array a = array([1., -2., 3., -4., -5.]) b = abs(a) for i in range(5): assert b[i] == abs(a[i]) a = abs(array(range(-5, 5), dtype="int8")) for i in range(-5, 5): assert a[i + 5] == abs(i) def test_auto_force(self): from numpy import array a = array(range(5)) b = a - 1 a[2] = 3 for i in range(5): assert b[i] == i - 1 a = array(range(5)) b = a + a c = b + b b[1] = 5 assert c[1] == 4 def test_getslice(self): from numpy import array a = array(5) exc = raises(ValueError, "a[:]") assert exc.value[0] == "cannot slice a 0-d array" a = array(range(5)) s = a[1:5] assert len(s) == 4 for i in range(4): assert s[i] == a[i + 1] s = (a + a)[1:2] assert len(s) == 1 assert s[0] == 2 s[:1] = array([5]) assert s[0] == 5 def test_getslice_step(self): from numpy import array a = array(range(10)) s = a[1:9:2] assert len(s) == 4 for i in range(4): assert s[i] == a[2 * i + 1] def test_slice_update(self): from numpy import array a = array(range(5)) s = a[0:3] s[1] = 10 assert a[1] == 10 a[2] = 20 assert s[2] == 20 def test_slice_invaidate(self): # check that slice shares invalidation list with from numpy import array a = array(range(5)) s = a[0:2] b = array([10, 11]) c = s + b a[0] = 100 assert c[0] == 10 assert c[1] == 12 d = s + b a[1] = 101 assert d[0] == 110 assert d[1] == 12 def test_sum(self): from numpy import array, zeros, float16, complex64, str_, isscalar, add a = array(range(5)) assert a.sum() == 10 assert a[:4].sum() == 6 a = array([True] * 5, bool) assert a.sum() == 5 assert array([True, False] * 200).sum() == 200 assert array([True, False] * 200, dtype='int8').sum() == 200 assert array([True, False] * 200).sum(dtype='int8') == -56 assert type(array([True, False] * 200, dtype='float16').sum()) is float16 assert type(array([True, False] * 200, dtype='complex64').sum()) is complex64 raises(TypeError, 'a.sum(axis=0, out=3)') raises(ValueError, 'a.sum(axis=2)') d = array(0.) b = a.sum(out=d) assert b == d assert b is d c = array(1.5+2.5j) assert c.real == 1.5 assert c.imag == 2.5 a.sum(out=c.imag) assert c.real == 1.5 assert c.imag == 5 assert list(zeros((0, 2)).sum(axis=1)) == [] a = array([1, 2, 3, 4]).sum() s = isscalar(a) assert s is True a = add.reduce([1.0, 2, 3, 4]) s = isscalar(a) assert s is True,'%r is not a scalar' % type(a) def test_reduce_nd(self): from numpy import arange, array a = arange(15).reshape(5, 3) assert a.sum() == 105 assert a.sum(keepdims=True) == 105 assert a.sum(keepdims=True).shape == (1, 1) assert a.max() == 14 assert array([]).sum() == 0.0 assert array([]).reshape(0, 2).sum() == 0. assert (array([]).reshape(0, 2).sum(0) == [0., 0.]).all() assert (array([]).reshape(0, 2).prod(0) == [1., 1.]).all() raises(ValueError, 'array([]).max()') assert (a.sum(0) == [30, 35, 40]).all() assert (a.sum(axis=0) == [30, 35, 40]).all() assert (a.sum(1) == [3, 12, 21, 30, 39]).all() assert (a.sum(-1) == a.sum(-1)).all() assert (a.sum(-2) == a.sum(-2)).all() raises(ValueError, a.sum, -3) raises(ValueError, a.sum, 2) assert (a.max(0) == [12, 13, 14]).all() assert (a.max(1) == [2, 5, 8, 11, 14]).all() assert ((a + a).max() == 28) assert ((a + a).max(0) == [24, 26, 28]).all() assert ((a + a).sum(1) == [6, 24, 42, 60, 78]).all() a = array(range(105)).reshape(3, 5, 7) assert (a[:, 1, :].sum(0) == [126, 129, 132, 135, 138, 141, 144]).all() assert (a[:, 1, :].sum(1) == [70, 315, 560]).all() raises (ValueError, 'a[:, 1, :].sum(2)') assert ((a + a).T.sum(2).T == (a + a).sum(0)).all() assert (a.reshape(1,-1).sum(0) == range(105)).all() assert (a.reshape(1,-1).sum(1) == 5460) assert (array([[1,2],[3,4]]).prod(0) == [3, 8]).all() assert (array([[1,2],[3,4]]).prod(1) == [2, 12]).all() def test_prod(self): from numpy import array, dtype a = array(range(1, 6)) assert a.prod() == 120.0 assert a.prod(keepdims=True) == 120.0 assert a.prod(keepdims=True).shape == (1,) assert a[:4].prod() == 24.0 for dt in ['bool', 'int8', 'uint8', 'int16', 'uint16']: a = array([True, False], dtype=dt) assert a.prod() == 0 assert a.prod().dtype is dtype('uint' if dt[0] == 'u' else 'int') for dt in ['l', 'L', 'q', 'Q', 'e', 'f', 'd', 'F', 'D']: a = array([True, False], dtype=dt) assert a.prod() == 0 assert a.prod().dtype is dtype(dt) def test_max(self): from numpy import array, zeros a = array([-1.2, 3.4, 5.7, -3.0, 2.7]) assert a.max() == 5.7 assert a.max().shape == () assert a.max(axis=(0,)) == 5.7 assert a.max(axis=(0,)).shape == () assert a.max(keepdims=True) == 5.7 assert a.max(keepdims=True).shape == (1,) b = array([]) raises(ValueError, "b.max()") assert list(zeros((0, 2)).max(axis=1)) == [] def test_max_add(self): from numpy import array a = array([-1.2, 3.4, 5.7, -3.0, 2.7]) assert (a + a).max() == 11.4 def test_min(self): from numpy import array, zeros a = array([-1.2, 3.4, 5.7, -3.0, 2.7]) assert a.min() == -3.0 assert a.min().shape == () assert a.min(axis=(0,)) == -3.0 assert a.min(axis=(0,)).shape == () assert a.min(keepdims=True) == -3.0 assert a.min(keepdims=True).shape == (1,) b = array([]) raises(ValueError, "b.min()") assert list(zeros((0, 2)).min(axis=1)) == [] def test_argmax(self): from numpy import array a = array([-1.2, 3.4, 5.7, -3.0, 2.7]) r = a.argmax() assert r == 2 b = array([]) raises(ValueError, b.argmax) a = array(range(-5, 5)) r = a.argmax() assert r == 9 b = a[::2] r = b.argmax() assert r == 4 r = (a + a).argmax() assert r == 9 a = array([1, 0, 0]) assert a.argmax() == 0 a = array([0, 0, 1]) assert a.argmax() == 2 a = array([[1, 2], [3, 4], [5, 6]]) assert a.argmax() == 5 assert a.argmax(axis=None, out=None) == 5 assert a[:2, ].argmax() == 3 assert (a.argmax(axis=0) == array([2, 2])).all() def test_argmin(self): import numpy as np a = np.array([-1.2, 3.4, 5.7, -3.0, 2.7]) assert a.argmin() == 3 assert a.argmin(axis=0) == 3 assert a.argmin(axis=None, out=None) == 3 b = np.array([]) raises(ValueError, "b.argmin()") c = np.arange(6).reshape(2, 3) assert c.argmin() == 0 assert (c.argmin(axis=0) == np.array([0, 0, 0])).all() assert (c.argmin(axis=1) == [0, 0]).all() def test_all(self): from numpy import array a = array(range(5)) assert a.all() == False a[0] = 3.0 assert a.all() == True assert a.all(keepdims=True) == True assert a.all(keepdims=True).shape == (1,) b = array([]) assert b.all() == True def test_any(self): from numpy import array, zeros a = array(range(5)) assert a.any() == True assert a.any(keepdims=True) == True assert a.any(keepdims=True).shape == (1,) b = zeros(5) assert b.any() == False c = array([]) assert c.any() == False def test_dtype_guessing(self): from numpy import array, dtype import sys 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('q') assert array([1.2, True]).dtype is dtype(float) assert array([1.2, 5]).dtype is dtype(float) assert array([]).dtype is dtype(float) float64 = dtype('float64').type int8 = dtype('int8').type bool_ = dtype('bool').type assert array([float64(2)]).dtype is dtype(float) assert array([int8(3)]).dtype is dtype("int8") assert array([bool_(True)]).dtype is dtype(bool) assert array([bool_(True), 3.0]).dtype is dtype(float) assert array(sys.maxint + 42).dtype is dtype('Q') assert array([sys.maxint + 42] * 2).dtype is dtype('Q') assert array([sys.maxint + 42, 123]).dtype is dtype(float) assert array([sys.maxint + 42, 123L]).dtype is dtype(float) assert array([1+2j, 123]).dtype is dtype(complex) assert array([1+2j, 123L]).dtype is dtype(complex) def test_comparison(self): import operator from numpy 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___nonzero__(self): from numpy import array a = array([1, 2]) raises(ValueError, bool, a) raises(ValueError, bool, a == a) assert bool(array(1)) assert not bool(array(0)) assert bool(array([1])) assert not bool(array([0])) def test_slice_assignment(self): from numpy import array a = array(range(5)) a[::-1] = a assert (a == [4, 3, 2, 1, 0]).all() # but we force intermediates a = array(range(5)) a[::-1] = a + a assert (a == [8, 6, 4, 2, 0]).all() def test_virtual_views(self): from numpy import arange a = arange(15) c = (a + a) d = c[::2] assert d[3] == 12 c[6] = 5 assert d[3] == 5 a = arange(15) c = (a + a) d = c[::2][::2] assert d[1] == 8 b = a + a c = b[::2] c[:] = 3 assert b[0] == 3 assert b[1] == 2 def test_realimag_views(self): from numpy import arange, array a = array(1.5) assert a.real == 1.5 assert a.imag == 0.0 a = array([1.5, 2.5]) assert (a.real == [1.5, 2.5]).all() assert (a.imag == [0.0, 0.0]).all() a = arange(15) b = a.real b[5]=50 assert a[5] == 50 b = a.imag assert b[7] == 0 raises(ValueError, 'b[7] = -2') raises(TypeError, 'a.imag = -2') a = array(['abc','def'],dtype='S3') b = a.real assert a[0] == b[0] assert a[1] == b[1] b[1] = 'xyz' assert a[1] == 'xyz' assert a.imag[0] == '' raises(TypeError, 'a.imag = "qop"') a=array([[1+1j, 2-3j, 4+5j],[-6+7j, 8-9j, -2-1j]]) assert a.real[0,1] == 2 a.real[0,1] = -20 assert a[0,1].real == -20 b = a.imag assert b[1,2] == -1 b[1,2] = 30 assert a[1,2].imag == 30 a.real = 13 assert a[1,1].real == 13 a=array([1+1j, 2-3j, 4+5j, -6+7j, 8-9j, -2-1j]) a.real = 13 assert a[3].real == 13 a.imag = -5 a.imag[3] = -10 assert a[3].imag == -10 assert a[2].imag == -5 assert arange(4, dtype='>c8').imag.max() == 0.0 assert arange(4, dtype='float32 conversion') from numpy import array, arange # numpy preserves value for uint16 -> cast_as_float16 -> # convert_to_float64 -> convert_to_float16 -> uint16 # even for float16 various float16 nans all_f16 = arange(0xfe00, 0xffff, dtype='uint16') all_f16.dtype = 'float16' all_f32 = array(all_f16, dtype='float32') b = array(all_f32, dtype='float16') c = b.view(dtype='uint16') d = all_f16.view(dtype='uint16') assert (c == d).all() def test_half_conversions_preserve_nan_payload_float64(self): from numpy import array, arange # numpy preserves value for uint16 -> cast_as_float16 -> # convert_to_float64 -> convert_to_float16 -> uint16 # even for float16 various float16 nans all_f16 = arange(0xfe00, 0xffff, dtype='uint16') all_f16.dtype = 'float16' all_f64 = array(all_f16, dtype='float64') b = array(all_f64, dtype='float16') c = b.view(dtype='uint16') d = all_f16.view(dtype='uint16') assert (c == d).all() def test_ndarray_view_empty(self): from numpy import array, dtype x = array([], dtype=[('a', 'int8'), ('b', 'int8')]) y = x.view(dtype='int16') def test_view_of_slice(self): from numpy import empty, dtype x = empty([6], 'uint32') x.fill(0xdeadbeef) s = x[::3] exc = raises(ValueError, s.view, 'uint8') assert exc.value[0] == 'new type not compatible with array.' s[...] = 2 v = s.view(x.__class__) assert v.strides == s.strides assert v.base is s.base assert (v == 2).all() y = empty([6,6], 'uint32') s = y.swapaxes(0, 1) v = s.view(y.__class__) assert v.strides == (4, 24) x = empty([12, 8, 8], 'float64') y = x[::-4, :, :] assert y.base is x assert y.strides == (-2048, 64, 8) y[:] = 1000 assert x[-1, 0, 0] == 1000 a = empty([3, 2, 1], dtype='float64') b = a.view(dtype('uint32')) assert b.strides == (16, 8, 4) assert b.shape == (3, 2, 2) b.fill(0xdeadbeef) def test_tolist_scalar(self): from numpy import dtype int32 = dtype('int32').type bool_ = dtype('bool').type x = int32(23) assert x.tolist() == 23 assert type(x.tolist()) is int y = bool_(True) assert y.tolist() is True def test_tolist_zerodim(self): from numpy import array x = array(3) assert x.tolist() == 3 assert type(x.tolist()) is int def test_tolist_singledim(self): from numpy import array a = array(range(5)) assert a.tolist() == [0, 1, 2, 3, 4] assert type(a.tolist()[0]) is int b = array([0.2, 0.4, 0.6]) assert b.tolist() == [0.2, 0.4, 0.6] def test_tolist_multidim(self): from numpy import array a = array([[1, 2], [3, 4]]) assert a.tolist() == [[1, 2], [3, 4]] def test_tolist_view(self): from numpy import array a = array([[1, 2], [3, 4]]) assert (a + a).tolist() == [[2, 4], [6, 8]] def test_tolist_object(self): from numpy import array a = array([0], dtype=object) assert a.tolist() == [0] def test_tolist_object_slice(self): from numpy import array list_expected = [slice(0, 1), 0] a = array(list_expected, dtype=object) assert a.tolist() == list_expected def test_tolist_object_slice_2d(self): from numpy import array a = array([(slice(0, 1), 1), (0, 1)], dtype=object) assert a.tolist() == [[slice(0, 1, None), 1], [0, 1]] def test_tolist_slice(self): from numpy import array a = array([[17.1, 27.2], [40.3, 50.3]]) assert a[:, 0].tolist() == [17.1, 40.3] assert a[0].tolist() == [17.1, 27.2] def test_concatenate(self): from numpy import array, concatenate, dtype exc = raises(ValueError, concatenate, (array(1.5), array(2.5))) assert exc.value[0] == 'zero-dimensional arrays cannot be concatenated' a = concatenate((array(1.5), array(2.5)), axis=None) assert (a == [1.5, 2.5]).all() assert exc.value[0] == 'zero-dimensional arrays cannot be concatenated' exc = raises(ValueError, concatenate, (array([1.5]), array(2.5))) assert exc.value[0] == 'all the input arrays must have same number ' \ 'of dimensions' exc = raises(ValueError, concatenate, (array(1.5), array([2.5]))) assert exc.value[0] == 'zero-dimensional arrays cannot be concatenated' a = concatenate((array([1.5]), array([2.5]))) assert (a == [1.5, 2.5]).all() a1 = array([0,1,2]) a2 = array([3,4,5]) a = concatenate((a1, a2)) assert len(a) == 6 assert (a == [0,1,2,3,4,5]).all() assert a.dtype is dtype(int) a = concatenate((a1, a2), axis=0) assert (a == [0,1,2,3,4,5]).all() a = concatenate((a1, a2), axis=-1) assert (a == [0,1,2,3,4,5]).all() a = concatenate((a1, a2), axis=None) assert (a == [0,1,2,3,4,5]).all() b1 = array([[1, 2], [3, 4]]) b2 = array([[5, 6]]) b = concatenate((b1, b2), axis=0) assert (b == [[1, 2],[3, 4],[5, 6]]).all() c = concatenate((b1, b2.T), axis=1) assert (c == [[1, 2, 5],[3, 4, 6]]).all() c1 = concatenate((b1, b2), axis=None) assert (c1 == [1, 2, 3, 4, 5, 6]).all() d = concatenate(([0],[1])) assert (d == [0,1]).all() e1 = array([[0,1],[2,3]]) e = concatenate(e1) assert (e == [0,1,2,3]).all() f1 = array([0,1]) f = concatenate((f1, [2], f1, [7])) assert (f == [0,1,2,0,1,7]).all() g1 = array([[0,1,2]]) g2 = array([[3,4,5]]) g = concatenate((g1, g2), axis=-2) assert (g == [[0,1,2],[3,4,5]]).all() exc = raises(IndexError, concatenate, (g1, g2), axis=-3) assert str(exc.value) == "axis -3 out of bounds [0, 2)" exc = raises(IndexError, concatenate, (g1, g2), axis=2) assert str(exc.value) == "axis 2 out of bounds [0, 2)" exc = raises(ValueError, concatenate, ()) assert str(exc.value) == \ "need at least one array to concatenate" exc = raises(ValueError, concatenate, (a1, b1), axis=0) assert str(exc.value) == \ "all the input arrays must have same number of dimensions" exc = raises(ValueError, concatenate, (b1, b2), axis=1) assert str(exc.value) == \ "all the input array dimensions except for the " \ "concatenation axis must match exactly" g1 = array([0,1,2]) g2 = array([[3,4,5]]) exc = raises(ValueError, concatenate, (g1, g2), axis=0) assert str(exc.value) == \ "all the input arrays must have same number of dimensions" a = array([1, 2, 3, 4, 5, 6]) a = (a + a)[::2] b = concatenate((a[:3], a[-3:])) assert (b == [2, 6, 10, 2, 6, 10]).all() a = concatenate((array([1]), array(['abc']))) if dtype('l').itemsize == 4: # 32-bit platform assert str(a.dtype) == '|S11' else: assert str(a.dtype) == '|S21' a = concatenate((array([]), array(['abc']))) assert a[0] == 'abc' a = concatenate((['abcdef'], ['abc'])) assert a[0] == 'abcdef' assert str(a.dtype) == '|S6' def test_record_concatenate(self): # only an exact match can succeed from numpy import zeros, concatenate a = concatenate((zeros((2,),dtype=[('x', int), ('y', float)]), zeros((2,),dtype=[('x', int), ('y', float)]))) assert a.shape == (4,) exc = raises(TypeError, concatenate, (zeros((2,), dtype=[('x', int), ('y', float)]), (zeros((2,), dtype=[('x', float), ('y', float)])))) assert str(exc.value).startswith('invalid type promotion') exc = raises(TypeError, concatenate, ([1], zeros((2,), dtype=[('x', int), ('y', float)]))) assert str(exc.value).startswith('invalid type promotion') exc = raises(TypeError, concatenate, (['abc'], zeros((2,), dtype=[('x', int), ('y', float)]))) assert str(exc.value).startswith('invalid type promotion') def test_flatten(self): from numpy import array assert array(3).flatten().shape == (1,) a = array([[1, 2], [3, 4]]) b = a.flatten() c = a.ravel() a[0, 0] = 15 assert b[0] == 1 assert c[0] == 15 a = array([[1, 2, 3], [4, 5, 6]]) assert (a.flatten() == [1, 2, 3, 4, 5, 6]).all() a = array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) assert (a.flatten() == [1, 2, 3, 4, 5, 6, 7, 8]).all() a = array([1, 2, 3, 4, 5, 6, 7, 8]) assert (a[::2].flatten() == [1, 3, 5, 7]).all() a = array([1, 2, 3]) assert ((a + a).flatten() == [2, 4, 6]).all() a = array(2) assert (a.flatten() == [2]).all() a = array([[1, 2], [3, 4]]) assert (a.T.flatten() == [1, 3, 2, 4]).all() def test_itemsize(self): from numpy import ones, dtype, array for obj in [float, bool, int]: assert ones(1, dtype=obj).itemsize == dtype(obj).itemsize assert (ones(1) + ones(1)).itemsize == 8 assert array(1.0).itemsize == 8 assert ones(1)[:].itemsize == 8 def test_nbytes(self): from numpy import array, ones assert ones(1).nbytes == 8 assert ones((2, 2)).nbytes == 32 assert ones((2, 2))[1:,].nbytes == 16 assert (ones(1) + ones(1)).nbytes == 8 assert array(3.0).nbytes == 8 def test_repeat(self): from numpy import array a = array([[1, 2], [3, 4]]) assert (a.repeat(3) == [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]).all() assert (a.repeat(2, axis=0) == [[1, 2], [1, 2], [3, 4], [3, 4]]).all() assert (a.repeat(2, axis=1) == [[1, 1, 2, 2], [3, 3, 4, 4]]).all() assert (array([1, 2]).repeat(2) == array([1, 1, 2, 2])).all() def test_resize(self): import numpy as np a = np.array([1,2,3]) import sys if '__pypy__' in sys.builtin_module_names: raises(NotImplementedError, a.resize, ()) def test_squeeze(self): import numpy as np a = np.array([1,2,3]) assert a.squeeze() is a a = np.array([[1,2,3]]) b = a.squeeze() assert b.shape == (3,) assert (b == a).all() b[1] = -1 assert a[0][1] == -1 a = np.arange(9).reshape((3, 1, 3, 1)) b = a.squeeze(1) assert b.shape == (3, 3, 1) b = a.squeeze((1,)) assert b.shape == (3, 3, 1) b = a.squeeze((1, -1)) assert b.shape == (3, 3) exc = raises(ValueError, a.squeeze, 5) assert exc.value.message == "'axis' entry 5 is out of bounds [-4, 4)" exc = raises(ValueError, a.squeeze, 0) assert exc.value.message == "cannot select an axis to squeeze out " \ "which has size not equal to one" exc = raises(ValueError, a.squeeze, (1, 1)) assert exc.value.message == "duplicate value in 'axis'" def test_swapaxes(self): from numpy import array x = array([]) raises(ValueError, x.swapaxes,0, 2) x = array([[1, 2]]) assert x.swapaxes(0, 0) is not x exc = raises(ValueError, x.swapaxes, -3, 0) assert exc.value.message == "bad axis1 argument to swapaxes" exc = raises(ValueError, x.swapaxes, 0, 3) assert exc.value.message == "bad axis2 argument to swapaxes" # testcases from numpy docstring x = array([[1, 2, 3]]) assert (x.swapaxes(0, 1) == array([[1], [2], [3]])).all() x = array([[[0,1],[2,3]],[[4,5],[6,7]]]) # shape = (2, 2, 2) assert (x.swapaxes(0, 2) == array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]])).all() assert (x.swapaxes(0, 1) == array([[[0, 1], [4, 5]], [[2, 3], [6, 7]]])).all() assert (x.swapaxes(1, 2) == array([[[0, 2], [1, 3]], [[4, 6],[5, 7]]])).all() # more complex shape i.e. (2, 2, 3) x = array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) assert (x.swapaxes(0, 1) == array([[[1, 2, 3], [7, 8, 9]], [[4, 5, 6], [10, 11, 12]]])).all() assert (x.swapaxes(0, 2) == array([[[1, 7], [4, 10]], [[2, 8], [5, 11]], [[3, 9], [6, 12]]])).all() assert (x.swapaxes(1, 2) == array([[[1, 4], [2, 5], [3, 6]], [[7, 10], [8, 11],[9, 12]]])).all() # test slice assert (x[0:1,0:2].swapaxes(0,2) == array([[[1], [4]], [[2], [5]], [[3], [6]]])).all() # test virtual assert ((x + x).swapaxes(0,1) == array([[[ 2, 4, 6], [14, 16, 18]], [[ 8, 10, 12], [20, 22, 24]]])).all() raises(ValueError, array(1).swapaxes, 10, 12) def test_filter_bug(self): from numpy import array a = array([1.0,-1.0]) a[a<0] = -a[a<0] assert (a == [1, 1]).all() def test_int_list_index(slf): from numpy import array, arange assert (array([10,11,12,13])[[1,2]] == [11, 12]).all() assert (arange(6).reshape((2,3))[[0,1]] == [[0, 1, 2], [3, 4, 5]]).all() assert arange(6).reshape((2,3))[(0,1)] == 1 def test_int_array_index(self): from numpy import array, arange, zeros b = arange(10)[array([3, 2, 1, 5])] assert (b == [3, 2, 1, 5]).all() raises(IndexError, "arange(10)[array([10])]") assert (arange(10)[[-5, -3]] == [5, 7]).all() raises(IndexError, "arange(10)[[-11]]") a = arange(1) a[[0, 0]] += 1 assert a[0] == 1 assert (zeros(1)[[]] == []).all() def test_int_array_index_setitem(self): from numpy import arange, zeros, array a = arange(10) a[[3, 2, 1, 5]] = zeros(4, dtype=int) assert (a == [0, 0, 0, 0, 4, 0, 6, 7, 8, 9]).all() a[[-9, -8]] = [1, 1] assert (a == [0, 1, 1, 0, 4, 0, 6, 7, 8, 9]).all() raises(IndexError, "arange(10)[array([10])] = 3") raises(IndexError, "arange(10)[[-11]] = 3") a = zeros(10) b = array([3,4,5]) a[b] = 1 assert (a == [0, 0, 0, 1, 1, 1, 0, 0, 0, 0]).all() def test_array_scalar_index(self): import numpy as np a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) assert (a[np.array(0)] == a[0]).all() assert (a[np.array(1)] == a[1]).all() exc = raises(IndexError, "a[np.array(True)]") assert exc.value.message == 'in the future, 0-d boolean arrays will be interpreted as a valid boolean index' exc = raises(IndexError, "a[np.array(1.1)]") assert exc.value.message == 'arrays used as indices must be of ' \ 'integer (or boolean) type' a[np.array(1)] = a[2] assert a[1][1] == 8 exc = raises(IndexError, "a[np.array(1.1)] = a[2]") assert exc.value.message == 'arrays used as indices must be of ' \ 'integer (or boolean) type' def test_bool_array_index(self): from numpy import arange, array b = arange(10) assert (b[array([True, False, True])] == [0, 2]).all() raises(IndexError, "array([1, 2])[array([True, True, True])]") raises(IndexError, "b[array([[True, False], [True, False]])]") a = array([[1,2,3],[4,5,6],[7,8,9]],int) c = array([True,False,True],bool) b = a[c] assert (a[c] == [[1, 2, 3], [7, 8, 9]]).all() c = array([True]) b = a[c] assert b.shape == (1, 3) def test_bool_array_index_setitem(self): from numpy import arange, array b = arange(5) b[array([True, False, True])] = [20, 21, 0, 0, 0, 0, 0] assert (b == [20, 1, 21, 3, 4]).all() raises(IndexError, "array([1, 2])[array([True, False, True])] = [1, 2, 3]") def test_weakref(self): import _weakref from numpy import array a = array([1, 2, 3]) assert _weakref.ref(a) a = array(42) assert _weakref.ref(a) def test_astype(self): from numpy import array, arange, empty b = array(1).astype(float) assert b == 1 assert b.dtype == float b = array([1, 2]).astype(float) assert (b == [1, 2]).all() assert b.dtype == 'float' b = array([1, 2], dtype=complex).astype(int) assert (b == [1, 2]).all() assert b.dtype == 'int' b = array([0, 1, 2], dtype=complex).astype(bool) assert (b == [False, True, True]).all() assert b.dtype == 'bool' a = arange(11)[::-1] b = a.astype('int32') assert (b == a).all() a = arange(6, dtype='f4').reshape(2,3) b = a.T.astype('i4') assert (a.T.strides == b.strides) a = array('x').astype('S3').dtype assert a.itemsize == 3 # scalar vs. array a = array([1, 2, 3.14156]).astype('S3').dtype assert a.itemsize == 3 a = array(3.1415).astype('S3').dtype assert a.itemsize == 3 a = array(['1', '2','3']).astype(float) assert a[2] == 3.0 a = array('123') assert a.astype('S0').dtype == 'S3' assert a.astype('i8') == 123 a = array('abcdefgh') exc = raises(ValueError, a.astype, 'i8') assert exc.value.message.startswith('invalid literal for int()') a = arange(5, dtype=complex) b = a.real c = b.astype("int64") assert c.shape == b.shape assert c.strides == (8,) exc = raises(TypeError, a.astype, 'i8', casting='safe') assert exc.value.message.startswith( "Cannot cast array from dtype('complex128') to dtype('int64')") a = arange(6, dtype='f4').reshape(2, 3) b = a.astype('f4', copy=False) assert a is b b = a.astype('f4', order='C', copy=False) assert a is b a = empty([3, 3, 3, 3], 'uint8') a[:] = 0 b = a[2] c = b[:, :2, :] d = c.swapaxes(1, -1) e = d.astype('complex128') assert e.shape == (3, 3, 2) assert e.strides == (96, 16, 48) assert (e.real == d).all() def test_base(self): from numpy import array, empty assert array(1).base is None assert array([1, 2]).base is None a = array([1, 2, 3, 4]) b = a[::2] assert b.base is a a = empty([3, 3, 3, 3], 'uint8') a[:] = 0 b = a[2] assert b.base.base is None c = b[:, :2, :] d = c.swapaxes(1, -1) assert c.base.base is None assert d.base.base is None assert d.shape == (3, 3, 2) assert d.__array_interface__['data'][0] == \ a.__array_interface__['data'][0] + a.strides[0] * 2 def test_byteswap(self): from numpy import array s1 = array(1.).byteswap().tostring() s2 = array([1.]).byteswap().tostring() assert s1 == s2 a = array([1, 256 + 2, 3], dtype='i2') assert (a.byteswap() == [0x0100, 0x0201, 0x0300]).all() assert (a == [1, 256 + 2, 3]).all() assert (a.byteswap(True) == [0x0100, 0x0201, 0x0300]).all() assert (a == [0x0100, 0x0201, 0x0300]).all() a = array([1, -1, 1e300], dtype=float) s1 = map(ord, a.tostring()) s2 = map(ord, a.byteswap().tostring()) assert a.dtype.itemsize == 8 for i in range(a.size): i1 = i * a.dtype.itemsize i2 = (i+1) * a.dtype.itemsize assert list(reversed(s1[i1:i2])) == s2[i1:i2] a = array([1+1e30j, -1, 1e10], dtype=complex) s1 = map(ord, a.tostring()) s2 = map(ord, a.byteswap().tostring()) assert a.dtype.itemsize == 16 for i in range(a.size*2): i1 = i * a.dtype.itemsize/2 i2 = (i+1) * a.dtype.itemsize/2 assert list(reversed(s1[i1:i2])) == s2[i1:i2] a = array([3.14, -1.5, 10000], dtype='float16') s1 = map(ord, a.tostring()) s2 = map(ord, a.byteswap().tostring()) assert a.dtype.itemsize == 2 for i in range(a.size): i1 = i * a.dtype.itemsize i2 = (i+1) * a.dtype.itemsize assert list(reversed(s1[i1:i2])) == s2[i1:i2] a = array([1, -1, 10000], dtype='longfloat') s1 = map(ord, a.tostring()) s2 = map(ord, a.byteswap().tostring()) assert a.dtype.itemsize >= 8 for i in range(a.size): i1 = i * a.dtype.itemsize i2 = (i+1) * a.dtype.itemsize assert list(reversed(s1[i1:i2])) == s2[i1:i2] def test_clip(self): from numpy import array a = array([1, 2, 17, -3, 12]) exc = raises(ValueError, a.clip) assert str(exc.value) == "One of max or min must be given." assert (a.clip(-2, 13) == [1, 2, 13, -2, 12]).all() assert (a.clip(min=-2) == [1, 2, 17, -2, 12]).all() assert (a.clip(min=-2, max=None) == [1, 2, 17, -2, 12]).all() assert (a.clip(max=13) == [1, 2, 13, -3, 12]).all() assert (a.clip(min=None, max=13) == [1, 2, 13, -3, 12]).all() assert (a.clip(-1, 1, out=None) == [1, 1, 1, -1, 1]).all() assert (a == [1, 2, 17, -3, 12]).all() assert (a.clip(-1, [1, 2, 3, 4, 5]) == [1, 2, 3, -1, 5]).all() assert (a.clip(-2, 13, out=a) == [1, 2, 13, -2, 12]).all() assert (a == [1, 2, 13, -2, 12]).all() def test_data(self): from numpy import array import sys a = array([1, 2, 3, 4], dtype='i4') assert a.data[0] == ('\x01' if sys.byteorder == 'little' else '\x00') assert a.data[1] == '\x00' assert a.data[3] == ('\x00' if sys.byteorder == 'little' else '\x01') assert a.data[4] == ('\x02' if sys.byteorder == 'little' else '\x00') a.data[4] = '\x7f' if sys.byteorder == 'big': a.data[7] = '\x00' # make sure 0x02 is reset to 0 assert a[1] == (0x7f000000) else: assert a[1] == 0x7f assert len(a.data) == 16 assert type(a.data) is buffer if '__pypy__' in sys.builtin_module_names: assert a[1:].data._pypy_raw_address() - a.data._pypy_raw_address() == a.strides[0] def test_explicit_dtype_conversion(self): from numpy import array a = array([1.0, 2.0]) b = array(a, dtype='d') assert a.dtype is b.dtype def test_notequal_different_shapes(self): from numpy import array a = array([1, 2]) b = array([1, 2, 3, 4]) assert (a == b) == False def test__int__(self): from numpy import array assert int(array(1)) == 1 assert int(array([1])) == 1 assert raises(TypeError, "int(array([1, 2]))") assert int(array([1.5])) == 1 for op in ["int", "float", "long"]: for a in [array('123'), array(['123'])]: exc = raises(TypeError, "%s(a)" % op) assert exc.value.message == "don't know how to convert " \ "scalar number to %s" % op def test__float__(self): import numpy as np assert float(np.array(1.5)) == 1.5 exc = raises(TypeError, "float(np.array([1.5, 2.5]))") assert exc.value[0] == 'only length-1 arrays can be converted to Python scalars' def test__hex__(self): import numpy as np assert hex(np.array(True)) == '0x1' assert hex(np.array(15)) == '0xf' assert hex(np.array([15])) == '0xf' exc = raises(TypeError, "hex(np.array(1.5))") assert str(exc.value) == "don't know how to convert scalar number to hex" exc = raises(TypeError, "hex(np.array('15'))") assert str(exc.value) == "don't know how to convert scalar number to hex" exc = raises(TypeError, "hex(np.array([1, 2]))") assert str(exc.value) == "only length-1 arrays can be converted to Python scalars" def test__oct__(self): import numpy as np assert oct(np.array(True)) == '01' assert oct(np.array(15)) == '017' assert oct(np.array([15])) == '017' exc = raises(TypeError, "oct(np.array(1.5))") assert str(exc.value) == "don't know how to convert scalar number to oct" exc = raises(TypeError, "oct(np.array('15'))") assert str(exc.value) == "don't know how to convert scalar number to oct" exc = raises(TypeError, "oct(np.array([1, 2]))") assert str(exc.value) == "only length-1 arrays can be converted to Python scalars" def test__reduce__(self): from numpy import array, dtype from cPickle import loads, dumps import sys a = array([1, 2], dtype="int64") data = a.__reduce__() if sys.byteorder == 'big': assert data[2][4] == '\x00\x00\x00\x00\x00\x00\x00\x01' \ '\x00\x00\x00\x00\x00\x00\x00\x02' else: assert data[2][4] == '\x01\x00\x00\x00\x00\x00\x00\x00' \ '\x02\x00\x00\x00\x00\x00\x00\x00' pickled_data = dumps(a) assert (loads(pickled_data) == a).all() def test_pickle_slice(self): from cPickle import loads, dumps import numpy a = numpy.arange(10.).reshape((5, 2))[::2] assert (loads(dumps(a)) == a).all() def test_string_filling(self): import numpy a = numpy.empty((10,10), dtype='S1') a.fill(12) assert (a == '1').all() def test_unicode_filling(self): import numpy as np a = np.empty((10,10), dtype='U1') a.fill(12) assert (a == u'1').all() def test_unicode_record_array(self) : from numpy import dtype, array t = dtype([('a', 'S3'), ('b', 'U2')]) x = array([('a', u'b')], dtype=t) assert str(x) == "[('a', u'b')]" t = dtype([('a', 'U3'), ('b', 'S2')]) x = array([(u'a', 'b')], dtype=t) x['a'] = u'1' assert str(x) == "[(u'1', 'b')]" def test_boolean_indexing(self): import numpy as np a = np.zeros((1, 3)) b = np.array([True]) assert (a[b] == a).all() a[b] = 1. assert (a == [[1., 1., 1.]]).all() a[b] = np.array(2.) assert (a == [[2., 2., 2.]]).all() a[b] = np.array([3.]) assert (a == [[3., 3., 3.]]).all() a[b] = np.array([[4.]]) assert (a == [[4., 4., 4.]]).all() def test_indexing_by_boolean(self): import numpy as np a = np.arange(6).reshape(2,3) assert (a[[True, False], :] == [[3, 4, 5], [0, 1, 2]]).all() b = a[np.array([True, False]), :] assert (b == [[0, 1, 2]]).all() assert b.base is None b = a[:, np.array([True, False, True])] assert b.base is not None a[np.array([True, False]), 0] = 100 b = a[np.array([True, False]), 0] assert b.shape == (1,) assert (b ==[100]).all() def test_scalar_indexing(self): import numpy as np a = np.arange(6).reshape(2,3) i = np.dtype('int32').type(0) assert (a[0] == a[i]).all() def test_ellipsis_indexing(self): import numpy as np import sys a = np.array(1.5) assert a[...].base is a a[...] = 2.5 assert a == 2.5 a = np.array([1, 2, 3]) assert a[...].base is a a[...] = 4 assert (a == [4, 4, 4]).all() assert a[..., 0] == 4 b = np.arange(24).reshape(2,3,4) b[...] = 100 assert (b == 100).all() assert b.shape == (2, 3, 4) b[...] = [10, 20, 30, 40] assert (b[:,:,0] == 10).all() assert (b[0,0,:] == [10, 20, 30, 40]).all() assert b.shape == b[...].shape assert (b == b[...]).all() a = np.arange(6) if '__pypy__' in sys.builtin_module_names: raises(ValueError, "a[..., ...]") b = a.reshape(2, 3)[..., 0] assert (b == [0, 3]).all() assert b.base is a def test_empty_indexing(self): import numpy as np r = np.ones(3) ind = np.array([], np.int32) tmp = np.array([], np.float64) assert r[ind].shape == (0,) r[ind] = 0 assert (r == np.ones(3)).all() r[ind] = tmp assert (r == np.ones(3)).all() r[[]] = 0 assert (r == np.ones(3)).all() class AppTestNumArrayFromBuffer(BaseNumpyAppTest): spaceconfig = dict(usemodules=["micronumpy", "array", "mmap"]) def setup_class(cls): from rpython.tool.udir import udir BaseNumpyAppTest.setup_class.im_func(cls) cls.w_tmpname = cls.space.wrap(str(udir.join('mmap-'))) def test_ndarray_from_buffer(self): import numpy as np import array import sys buf = array.array('c', ['\x00']*2*3) a = np.ndarray((3,), buffer=buf, dtype='i2') a[0] = ord('b') a[1] = ord('a') a[2] = ord('r') if sys.byteorder == 'big': assert list(buf) == ['\x00', 'b', '\x00', 'a', '\x00', 'r'] else: assert list(buf) == ['b', '\x00', 'a', '\x00', 'r', '\x00'] assert a.base is buf def test_ndarray_subclass_from_buffer(self): import numpy as np import array buf = array.array('c', ['\x00']*2*3) class X(np.ndarray): pass a = X((3,), buffer=buf, dtype='i2') assert type(a) is X def test_ndarray_from_buffer_and_offset(self): import numpy as np import array import sys buf = array.array('c', ['\x00']*7) buf[0] = 'X' a = np.ndarray((3,), buffer=buf, offset=1, dtype='i2') a[0] = ord('b') a[1] = ord('a') a[2] = ord('r') if sys.byteorder == 'big': assert list(buf) == ['X', '\x00', 'b', '\x00', 'a', '\x00', 'r'] else: assert list(buf) == ['X', 'b', '\x00', 'a', '\x00', 'r', '\x00'] def test_ndarray_from_buffer_out_of_bounds(self): import numpy as np import array buf = array.array('c', ['\x00']*2*10) # 20 bytes info = raises(TypeError, "np.ndarray((11,), buffer=buf, dtype='i2')") assert str(info.value).startswith('buffer is too small') info = raises(TypeError, "np.ndarray((5,), buffer=buf, offset=15, dtype='i2')") assert str(info.value).startswith('buffer is too small') def test_ndarray_from_readonly_buffer(self): import numpy as np from mmap import mmap, ACCESS_READ f = open(self.tmpname, "w+") f.write("hello") f.flush() buf = mmap(f.fileno(), 5, access=ACCESS_READ) a = np.ndarray((5,), buffer=buf, dtype='c') raises(ValueError, "a[0] = 'X'") buf.close() f.close() class AppTestMultiDim(BaseNumpyAppTest): def test_init(self): import numpy a = numpy.zeros((2, 2)) assert len(a) == 2 def test_shape(self): import numpy assert numpy.zeros(1).shape == (1,) assert numpy.zeros((2, 2)).shape == (2, 2) assert numpy.zeros((3, 1, 2)).shape == (3, 1, 2) assert numpy.array([[1], [2], [3]]).shape == (3, 1) assert len(numpy.zeros((3, 1, 2))) == 3 raises(TypeError, len, numpy.zeros(())) raises(ValueError, numpy.array, [[1, 2], 3], dtype=float) def test_getsetitem(self): import numpy a = numpy.zeros((2, 3, 1)) raises(IndexError, a.__getitem__, (2, 0, 0)) raises(IndexError, a.__getitem__, (0, 3, 0)) raises(IndexError, a.__getitem__, (0, 0, 1)) assert a[1, 1, 0] == 0 a[1, 2, 0] = 3 assert a[1, 2, 0] == 3 assert a[1, 1, 0] == 0 assert a[1, -1, 0] == 3 def test_slices(self): import numpy a = numpy.zeros((4, 3, 2)) raises(IndexError, a.__getitem__, (4,)) raises(IndexError, a.__getitem__, (3, 3)) raises(IndexError, a.__getitem__, (slice(None), 3)) a[0, 1, 1] = 13 a[1, 2, 1] = 15 b = a[0] assert len(b) == 3 assert b.shape == (3, 2) assert b[1, 1] == 13 b = a[1] assert b.shape == (3, 2) assert b[2, 1] == 15 b = a[:, 1] assert b.shape == (4, 2) assert b[0, 1] == 13 b = a[:, 1, :] assert b.shape == (4, 2) assert b[0, 1] == 13 b = a[1, 2] assert b[1] == 15 b = a[:] assert b.shape == (4, 3, 2) assert b[1, 2, 1] == 15 assert b[0, 1, 1] == 13 b = a[:][:, 1][:] assert b[2, 1] == 0.0 assert b[0, 1] == 13 raises(IndexError, b.__getitem__, (4, 1)) assert a[0][1][1] == 13 assert a[1][2][1] == 15 def test_create_order(self): import numpy as np for order in [False, True, 'C', 'F']: a = np.empty((2, 3), float, order=order) assert a.shape == (2, 3) if order in [True, 'F']: assert a.flags['F'] assert not a.flags['C'] else: assert a.flags['C'], "flags['C'] False for %r" % order assert not a.flags['F'] def test_setitem_slice(self): import numpy a = numpy.zeros((3, 4)) a[1] = [1, 2, 3, 4] assert a[1, 2] == 3 raises(TypeError, a[1].__setitem__, [1, 2, 3]) a = numpy.array([[1, 2], [3, 4]]) assert (a == [[1, 2], [3, 4]]).all() a[1] = numpy.array([5, 6]) assert (a == [[1, 2], [5, 6]]).all() a[:, 1] = numpy.array([8, 10]) assert (a == [[1, 8], [5, 10]]).all() a[0, :: -1] = numpy.array([11, 12]) assert (a == [[12, 11], [5, 10]]).all() a = numpy.zeros((3, 2), int) b = numpy.ones((3, 1), int) exc = raises(ValueError, 'a[:, 1] = b') assert str(exc.value) == "could not broadcast " +\ "input array from shape (3,1) into shape (3)" a[:, 1] = b[:,0] > 0.5 assert (a == [[0, 1], [0, 1], [0, 1]]).all() def test_ufunc(self): from numpy import array a = array([[1, 2], [3, 4], [5, 6]]) assert ((a + a) == \ array([[1 + 1, 2 + 2], [3 + 3, 4 + 4], [5 + 5, 6 + 6]])).all() def test_getitem_add(self): from numpy import array a = array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]) assert (a + a)[1, 1] == 8 def test_getitem_3(self): from numpy import array a = array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14]]) b = a[::2] assert (b == [[1, 2], [5, 6], [9, 10], [13, 14]]).all() c = b + b assert c[1][1] == 12 def test_multidim_ones(self): from numpy import ones a = ones((1, 2, 3)) assert a[0, 1, 2] == 1.0 def test_multidim_setslice(self): from numpy import zeros, ones a = zeros((3, 3)) b = ones((3, 3)) a[:, 1:3] = b[:, 1:3] assert (a == [[0, 1, 1], [0, 1, 1], [0, 1, 1]]).all() a = zeros((3, 3)) b = ones((3, 3)) a[:, ::2] = b[:, ::2] assert (a == [[1, 0, 1], [1, 0, 1], [1, 0, 1]]).all() def test_broadcast_ufunc(self): from numpy import array a = array([[1, 2], [3, 4], [5, 6]]) b = array([5, 6]) c = ((a + b) == [[1 + 5, 2 + 6], [3 + 5, 4 + 6], [5 + 5, 6 + 6]]) assert c.all() def test_broadcast_setslice(self): from numpy import zeros, ones a = zeros((10, 10)) b = ones(10) a[:, :] = b assert a[3, 5] == 1 def test_broadcast_shape_agreement(self): from numpy import zeros, array a = zeros((3, 1, 3)) b = array(((10, 11, 12), (20, 21, 22), (30, 31, 32))) c = ((a + b) == [b, b, b]) assert c.all() a = array((((10, 11, 12), ), ((20, 21, 22), ), ((30, 31, 32), ))) assert(a.shape == (3, 1, 3)) d = zeros((3, 3)) c = ((a + d) == [b, b, b]) c = ((a + d) == array([[[10., 11., 12.]] * 3, [[20., 21., 22.]] * 3, [[30., 31., 32.]] * 3])) assert c.all() def test_broadcast_scalar(self): from numpy import zeros a = zeros((4, 5), 'd') a[:, 1] = 3 assert a[2, 1] == 3 assert a[0, 2] == 0 a[0, :] = 5 assert a[0, 3] == 5 assert a[2, 1] == 3 assert a[3, 2] == 0 def test_broadcast_call2(self): from numpy import zeros, ones a = zeros((4, 1, 5)) b = ones((4, 3, 5)) b[:] = (a + a) assert (b == zeros((4, 3, 5))).all() def test_broadcast_virtualview(self): from numpy import arange, zeros a = arange(8).reshape([2, 2, 2]) b = (a + a)[1, 1] c = zeros((2, 2, 2)) c[:] = b assert (c == [[[12, 14], [12, 14]], [[12, 14], [12, 14]]]).all() def test_broadcast_wrong_shapes(self): from numpy import zeros a = zeros((4, 3, 2)) b = zeros((4, 2)) exc = raises(ValueError, lambda: a + b) assert str(exc.value).startswith("operands could not be broadcast") def test_reduce(self): from numpy import array a = array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) assert a.sum() == (13 * 12) / 2 b = a[1:, 1::2] c = b + b assert c.sum() == (6 + 8 + 10 + 12) * 2 assert isinstance(c.sum(dtype='f8'), float) assert isinstance(c.sum(None, 'f8'), float) def test_transpose(self): from numpy import array a = array(((range(3), range(3, 6)), (range(6, 9), range(9, 12)), (range(12, 15), range(15, 18)), (range(18, 21), range(21, 24)))) assert a.shape == (4, 2, 3) b = a.T assert b.shape == (3, 2, 4) assert(b[0, :, 0] == [0, 3]).all() c = a.transpose((1, 0, 2)) assert c.shape == (2, 4, 3) assert (c.transpose(1, 0, 2) == a).all() b[:, 0, 0] = 1000 assert(a[0, 0, :] == [1000, 1000, 1000]).all() a = array(range(5)) b = a.T assert(b == range(5)).all() a = array((range(10), range(20, 30))) b = a.T assert(b[:, 0] == a[0, :]).all() assert (a.transpose() == b).all() assert (a.transpose(None) == b).all() def test_transpose_arg_tuple(self): import numpy as np a = np.arange(24).reshape(2, 3, 4) transpose_args = a.transpose(1, 2, 0) transpose_test = a.transpose((1, 2, 0)) assert transpose_test.shape == (3, 4, 2) assert (transpose_args == transpose_test).all() def test_transpose_arg_list(self): import numpy as np a = np.arange(24).reshape(2, 3, 4) transpose_args = a.transpose(1, 2, 0) transpose_test = a.transpose([1, 2, 0]) assert transpose_test.shape == (3, 4, 2) assert (transpose_args == transpose_test).all() def test_transpose_arg_array(self): import numpy as np a = np.arange(24).reshape(2, 3, 4) transpose_args = a.transpose(1, 2, 0) transpose_test = a.transpose(np.array([1, 2, 0])) assert transpose_test.shape == (3, 4, 2) assert (transpose_args == transpose_test).all() def test_transpose_error(self): import numpy as np a = np.arange(24).reshape(2, 3, 4) raises(ValueError, a.transpose, 2, 1) raises(ValueError, a.transpose, 1, 0, 3) raises(ValueError, a.transpose, 1, 0, 1) raises(TypeError, a.transpose, 1, 0, '2') def test_transpose_unexpected_argument(self): import numpy as np a = np.array([[1, 2], [3, 4], [5, 6]]) raises(TypeError, 'a.transpose(axes=(1,2,0))') def test_flatiter(self): from numpy import array, flatiter, arange, zeros a = array([[10, 30], [40, 60]]) f_iter = a.flat assert f_iter.next() == 10 assert f_iter.next() == 30 assert f_iter.next() == 40 assert f_iter.next() == 60 raises(StopIteration, "f_iter.next()") raises(TypeError, "flatiter()") s = 0 for k in a.flat: s += k assert s == 140 a = arange(10).reshape(5, 2) raises(IndexError, 'a.flat[(1, 2)]') assert a.flat.base is a m = zeros((2,2), dtype='S3') m.flat[1] = 1 assert m[0,1] == '1' def test_flatiter_array_conv(self): from numpy import array, dot a = array([1, 2, 3]) assert dot(a.flat, a.flat) == 14 def test_flatiter_varray(self): from numpy import ones a = ones((2, 2)) assert list(((a + a).flat)) == [2, 2, 2, 2] def test_flatiter_getitem(self): from numpy import arange a = arange(10) assert a.flat[3] == 3 assert a[2:].flat[3] == 5 assert (a + a).flat[3] == 6 assert a[::2].flat[3] == 6 assert a.reshape(2,5).flat[3] == 3 b = a.reshape(2,5).flat b.next() b.next() b.next() assert b.index == 3 assert b.coords == (0, 3) b.next() assert b[3] == 3 assert b.index == 0 assert b.coords == (0, 0) b.next() assert (b[::3] == [0, 3, 6, 9]).all() assert b.index == 0 assert b.coords == (0, 0) b.next() assert (b[2::5] == [2, 7]).all() assert b.index == 0 assert b.coords == (0, 0) b.next() assert b[-2] == 8 assert b.index == 0 assert b.coords == (0, 0) b.next() raises(IndexError, "b[11]") assert b.index == 0 assert b.coords == (0, 0) b.next() raises(IndexError, "b[-11]") assert b.index == 0 assert b.coords == (0, 0) b.next() exc = raises(IndexError, 'b[0, 1]') assert str(exc.value) == "unsupported iterator index" assert b.index == 1 assert b.coords == (0, 1) def test_flatiter_setitem(self): from numpy import arange, array a = arange(12).reshape(3,4) b = a.T.flat b[6::2] = [-1, -2] assert (a == [[0, 1, -1, 3], [4, 5, 6, -1], [8, 9, -2, 11]]).all() assert b[2] == 8 assert b.index == 0 b.next() b[6::2] = [-21, -42] assert (a == [[0, 1, -21, 3], [4, 5, 6, -21], [8, 9, -42, 11]]).all() b[0:2] = [[[100]]] assert(a[0,0] == 100) assert(a[1,0] == 100) b.next() assert b.index == 1 exc = raises(ValueError, "b[0] = [1, 2]") assert str(exc.value) == "Error setting single item of array." assert b.index == 0 b.next() raises(IndexError, "b[100] = 42") assert b.index == 1 exc = raises(IndexError, "b[0, 1] = 42") assert str(exc.value) == "unsupported iterator index" assert b.index == 1 a = array([(False, False, False), (False, False, False), (False, False, False), ], dtype=[('a', '|b1'), ('b', '|b1'), ('c', '|b1')]) a.flat = [(True, True, True), (True, True, True), (True, True, True)] assert (a.view(bool) == True).all() def test_flatiter_ops(self): from numpy import arange, array a = arange(12).reshape(3,4) b = a.T.flat assert (b == [0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11]).all() assert not (b != [0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11]).any() assert ((b >= range(12)) == [True, True, True,False, True, True, False, False, True, False, False, True]).all() assert ((b < range(12)) != [True, True, True,False, True, True, False, False, True, False, False, True]).all() assert ((b <= range(12)) != [False, True, True,False, True, True, False, False, True, False, False, False]).all() assert ((b > range(12)) == [False, True, True,False, True, True, False, False, True, False, False, False]).all() def test_flatiter_view(self): from numpy import arange a = arange(10).reshape(5, 2) assert (a[::2].flat == [0, 1, 4, 5, 8, 9]).all() def test_flatiter_transpose(self): from numpy import arange a = arange(10).reshape(2, 5).T b = a.flat assert (b[:5] == [0, 5, 1, 6, 2]).all() b.next() b.next() b.next() assert b.index == 3 assert b.coords == (1, 1) def test_flatiter_len(self): from numpy import arange assert len(arange(10).flat) == 10 assert len(arange(10).reshape(2, 5).flat) == 10 assert len(arange(10)[:2].flat) == 2 assert len((arange(2) + arange(2)).flat) == 2 def test_flatiter_setter(self): from numpy import arange, array a = arange(24).reshape(2, 3, 4) a.flat = [4, 5] assert (a.flatten() == [4, 5]*12).all() a.flat = [[4, 5, 6, 7, 8], [4, 5, 6, 7, 8]] assert (a.flatten() == ([4, 5, 6, 7, 8]*5)[:24]).all() exc = raises(ValueError, 'a.flat = [[4, 5, 6, 7, 8], [4, 5, 6]]') assert str(exc.value).find("sequence") > 0 b = a[::-1, :, ::-1] b.flat = range(24) assert (a.flatten() == [15, 14 ,13, 12, 19, 18, 17, 16, 23, 22, 21, 20, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8]).all() c = array(['abc'] * 10).reshape(2, 5) c.flat = ['defgh', 'ijklmnop'] assert (c.flatten() == ['def', 'ijk']*5).all() def test_flatiter_subtype(self): from numpy import array x = array([[1, 2], [3, 4]]).T y = array(x.flat) assert (x == [[1, 3], [2, 4]]).all() def test_slice_copy(self): from numpy import zeros a = zeros((10, 10)) b = a[0].copy() assert (b == zeros(10)).all() def test_array_interface(self): from numpy import array import numpy as np a = array(2.5) i = a.__array_interface__ assert isinstance(i['data'][0], int) assert i['shape'] == () assert i['strides'] is None a = array([1, 2, 3]) i = a.__array_interface__ assert isinstance(i['data'][0], int) assert i['shape'] == (3,) assert i['strides'] == None # Because array is in C order assert i['typestr'] == a.dtype.str a = a[::2] i = a.__array_interface__ assert isinstance(i['data'][0], int) b = array(range(9), dtype=int) c = b[3:5] b_data = b.__array_interface__['data'][0] c_data = c.__array_interface__['data'][0] assert b_data + 3 * b.dtype.itemsize == c_data class Dummy(object): def __init__(self, aif=None, base=None): if aif is not None: self.__array_interface__ = aif self.base = base a = array(Dummy()) assert a.dtype == object raises(ValueError, array, Dummy({'xxx': 0})) raises(ValueError, array, Dummy({'version': 0})) raises(ValueError, array, Dummy({'version': 'abc'})) raises(ValueError, array, Dummy({'version': 3})) raises(TypeError, array, Dummy({'version': 3, 'typestr': 'f8', 'shape': ('a', 3)})) a = array([1, 2, 3]) d = Dummy(a.__array_interface__) b = array(d) assert b.base is None b[1] = 200 assert a[1] == 2 # upstream compatibility, is this a bug? interface_a = a.__array_interface__ interface_b = b.__array_interface__ # only the data[0] value should differ assert interface_a['data'][0] != interface_b['data'][0] assert interface_b['data'][1] == interface_a['data'][1] interface_b.pop('data') interface_a.pop('data') assert interface_a == interface_b b = array(d, copy=False) assert b.base is d b = array(Dummy({'version':3, 'shape': (50,), 'typestr': 'u1', 'data': 'a'*100})) assert b.dtype == 'uint8' assert b.shape == (50,) a = np.ones((1,), dtype='float16') b = Dummy(a.__array_interface__) c = array(b) assert c.dtype == 'float16' assert (a == c).all() t = np.dtype([("a", np.float64), ("b", np.float64)], align=True) a = np.zeros(10, dtype=t) a['a'] = range(10, 20) a['b'] = range(20, 30) interface = dict(a.__array_interface__) array = np.array(Dummy(interface)) assert array.dtype.kind == 'V' array.dtype = a.dtype assert array[5]['b'] == 25 def test_array_indexing_one_elem(self): from numpy import array, arange raises(IndexError, 'arange(3)[array([3.5])]') a = arange(3)[array([1])] assert a == 1 assert a[0] == 1 raises(IndexError,'arange(3)[array([15])]') assert arange(3)[array([-3])] == 0 raises(IndexError,'arange(3)[array([-15])]') assert arange(3)[array(1)] == 1 def test_fill(self): from numpy import array, empty, dtype, zeros a = array([1, 2, 3]) a.fill(10) assert (a == [10, 10, 10]).all() a.fill(False) assert (a == [0, 0, 0]).all() b = a[:1] b.fill(4) assert (b == [4]).all() assert (a == [4, 0, 0]).all() c = b + b c.fill(27) assert (c == [27]).all() d = array(10) d.fill(100) assert d == 100 e = array(10, dtype=complex) e.fill(1.5-3j) assert e == 1.5-3j a = empty(5, dtype='S3') a.fill('abc') for i in a: assert i == 'abc' a = empty(10, dtype=[(_, int) for _ in 'abcde']) a.fill(123) for i in a: assert tuple(i) == (123,) * 5 a = zeros(3, dtype=dtype(complex).newbyteorder()) a.fill(1.5+2.5j) for i in a: assert i == 1.5+2.5j def test_array_indexing_bool(self): from numpy import arange a = arange(10) assert (a[a > 3] == [4, 5, 6, 7, 8, 9]).all() a = arange(10).reshape(5, 2) assert (a[a > 3] == [4, 5, 6, 7, 8, 9]).all() assert (a[a & 1 == 1] == [1, 3, 5, 7, 9]).all() def test_array_indexing_bool_setitem(self): from numpy import arange, array a = arange(6) a[a > 3] = 15 assert (a == [0, 1, 2, 3, 15, 15]).all() a = arange(6).reshape(3, 2) a[a & 1 == 1] = array([8, 9, 10]) assert (a == [[0, 8], [2, 9], [4, 10]]).all() def test_array_indexing_bool_setitem_multidim(self): from numpy import arange a = arange(10).reshape(5, 2) a[a & 1 == 0] = 15 assert (a == [[15, 1], [15, 3], [15, 5], [15, 7], [15, 9]]).all() def test_array_indexing_bool_setitem_2(self): from numpy import arange a = arange(10).reshape(5, 2) a = a[::2] a[a & 1 == 0] = 15 assert (a == [[15, 1], [15, 5], [15, 9]]).all() def test_array_indexing_bool_specialcases(self): from numpy import arange, array a = arange(6) exc = raises(ValueError, 'a[a < 3] = [1, 2]') assert exc.value[0].find('cannot assign') >= 0 b = arange(4).reshape(2, 2) + 10 a[a < 4] = b assert (a == [10, 11, 12, 13, 4, 5]).all() b += 10 c = arange(8).reshape(2, 2, 2) a[a > 9] = c[:, :, 1] assert (c[:, :, 1] == [[1, 3], [5, 7]]).all() assert (a == [1, 3, 5, 7, 4, 5]).all() a = arange(6) a[a > 3] = array([15]) assert (a == [0, 1, 2, 3, 15, 15]).all() a = arange(6).reshape(3, 2) exc = raises(ValueError, 'a[a & 1 == 1] = []') assert exc.value[0].find('cannot assign') >= 0 assert (a == [[0, 1], [2, 3], [4, 5]]).all() def test_copy_kwarg(self): from numpy import array x = array([1, 2, 3]) assert (array(x) == x).all() assert array(x) is not x assert array(x, copy=False) is x assert array(x, copy=True) is not x def test_ravel(self): from numpy import arange assert (arange(3).ravel() == arange(3)).all() assert (arange(6).reshape(2, 3).ravel() == arange(6)).all() assert (arange(6).reshape(2, 3).T.ravel() == [0, 3, 1, 4, 2, 5]).all() assert (arange(3).ravel('K') == arange(3)).all() def test_nonzero(self): from numpy import array nz = array(0).nonzero() assert nz[0].size == 0 nz = array(2).nonzero() assert (nz[0] == [0]).all() nz = array([1, 0, 3]).nonzero() assert (nz[0] == [0, 2]).all() nz = array([[1, 0, 3], [2, 0, 4]]).nonzero() assert (nz[0] == [0, 0, 1, 1]).all() assert (nz[1] == [0, 2, 0, 2]).all() def test_take(self): from numpy import arange assert (arange(10).take([1, 2, 1, 1]) == [1, 2, 1, 1]).all() raises(IndexError, "arange(3).take([15])") a = arange(6).reshape(2, 3) assert a.take(3) == 3 assert a.take(3).shape == () assert (a.take([1, 0, 3]) == [1, 0, 3]).all() assert (a.take([[1, 0], [2, 3]]) == [[1, 0], [2, 3]]).all() assert (a.take([1], axis=0) == [[3, 4, 5]]).all() assert (a.take([1], axis=1) == [[1], [4]]).all() assert ((a + a).take([3]) == [6]).all() a = arange(12).reshape(2, 6) assert (a[:,::2].take([3, 2, 1]) == [6, 4, 2]).all() import sys if '__pypy__' in sys.builtin_module_names: exc = raises(NotImplementedError, "a.take([3, 2, 1], mode='clip')") assert exc.value[0] == "mode != raise not implemented" def test_ptp(self): import numpy as np x = np.arange(4).reshape((2,2)) assert x.ptp() == 3 assert (x.ptp(axis=0) == [2, 2]).all() assert (x.ptp(axis=1) == [1, 1]).all() def test_compress(self): from numpy import arange, array a = arange(10) assert (a.compress([True, False, True]) == [0, 2]).all() assert (a.compress([1, 0, 13]) == [0, 2]).all() assert (a.compress([1, 0, 13]) == [0, 2]).all() assert (a.compress([1, 0, 13.5]) == [0, 2]).all() assert (a.compress(array([1, 0, 13.5], dtype='>f4')) == [0, 2]).all() assert (a.compress(array([1, 0, 13.5], dtype=' (3, 2): # In Python 3.3 the representation of empty shape, strides and sub-offsets # is an empty tuple instead of None. # http://docs.python.org/dev/whatsnew/3.3.html#api-changes EMPTY = () else: EMPTY = None x = np.array([1, 2, 3, 4, 5], dtype='i') y = memoryview(x) assert y.format == 'i' assert y.shape == (5,) assert y.ndim == 1 assert y.strides == (4,) assert y.suboffsets == EMPTY assert y.itemsize == 4 assert isinstance(y, memoryview) assert y[0] == self.one assert (np.array(y) == x).all() x = np.array([0, 0, 0, 0], dtype='O') y = memoryview(x) # handles conversion of address to pinned object? z = np.array(y) assert z.dtype == 'O' assert (z == x).all() dt1 = np.dtype( [('a', 'b'), ('b', 'i'), ('sub', np.dtype('b,i')), ('c', 'i')], align=True) x = np.arange(dt1.itemsize, dtype=np.int8).view(dt1) y = memoryview(x) if '__pypy__' in sys.builtin_module_names: assert y.format == 'T{b:a:xxxi:b:T{b:f0:i:f1:}:sub:xxxi:c:}' else: assert y.format == 'T{b:a:xxxi:b:T{b:f0:=i:f1:}:sub:xxx@i:c:}' dt1 = np.dtype( [('a', 'b'), ('b', 'i'), ('sub', np.dtype('b,i')), ('c', 'i')], align=True) x = np.arange(dt1.itemsize, dtype=np.int8).view(dt1) y = memoryview(x) if '__pypy__' in sys.builtin_module_names: assert y.format == 'T{b:a:xxxi:b:T{b:f0:i:f1:}:sub:xxxi:c:}' else: assert y.format == 'T{b:a:xxxi:b:T{b:f0:=i:f1:}:sub:xxx@i:c:}' def test_fromstring(self): import sys from numpy import fromstring, dtype a = fromstring(self.data) for i in range(4): assert a[i] == i + 1 b = fromstring('\x01\x02', dtype='uint8') assert a[0] == 1 assert a[1] == 2 c = fromstring(self.fdata, dtype='float32') assert c[0] == dtype('float32').type(2.3) d = fromstring("1 2", sep=' ', count=2, dtype='uint8') assert len(d) == 2 assert d[0] == 1 assert d[1] == 2 e = fromstring('3, 4,5', dtype='uint8', sep=',') assert len(e) == 3 assert e[0] == 3 assert e[1] == 4 assert e[2] == 5 f = fromstring('\x01\x02\x03\x04\x05', dtype='uint8', count=3) assert len(f) == 3 assert f[0] == 1 assert f[1] == 2 assert f[2] == 3 g = fromstring("1 2 3 ", dtype='uint8', sep=" ") assert len(g) == 3 assert g[0] == 1 assert g[1] == 2 assert g[2] == 3 h = fromstring("1, , 2, 3", dtype='uint8', sep=",") assert (h == [1, 0, 2, 3]).all() i = fromstring("1 2 3", dtype='uint8', sep=" ") assert (i == [1, 2, 3]).all() j = fromstring("1\t\t\t\t2\t3", dtype='uint8', sep="\t") assert (j == [1, 2, 3]).all() k = fromstring("1,x,2,3", dtype='uint8', sep=",") assert (k == [1, 0]).all() l = fromstring("1,x,2,3", dtype='float32', sep=",") assert (l == [1.0, -1.0]).all() m = fromstring("1,,2,3", sep=",") assert (m == [1.0, -1.0, 2.0, 3.0]).all() n = fromstring("3.4 2.0 3.8 2.2", dtype='int32', sep=" ") assert (n == [3]).all() n = fromstring('\x00\x00\x00{', dtype='>i4') assert n == 123 n = fromstring('W\xb0', dtype='>f2') assert n == 123. o = fromstring("1.0 2f.0f 3.8 2.2", dtype='float32', sep=" ") assert len(o) == 2 assert o[0] == 1.0 assert o[1] == 2.0 p = fromstring("1.0,,2.0,3.0", sep=",") assert (p == [1.0, -1.0, 2.0, 3.0]).all() q = fromstring("1.0,,2.0,3.0", sep=" ") assert (q == [1.0]).all() r = fromstring("\x01\x00\x02", dtype='bool') assert (r == [True, False, True]).all() s = fromstring("1,2,3,,5", dtype=bool, sep=",") assert (s == [True, True, True, True, True]).all() t = fromstring("", bool) assert (t == []).all() u = fromstring("\x01\x00\x00\x00\x00\x00\x00\x00", dtype=int) if sys.maxint > 2 ** 31 - 1: if sys.byteorder == 'big': assert (u == [0x0100000000000000]).all() else: assert (u == [1]).all() else: if sys.byteorder == 'big': assert (u == [0x01000000, 0]).all() else: assert (u == [1, 0]).all() v = fromstring("abcd", dtype="|S2") assert v[0] == "ab" assert v[1] == "cd" v = fromstring('@\x01\x99\x99\x99\x99\x99\x9a\xbf\xf1\x99\x99\x99\x99\x99\x9a', dtype=dtype('>c16')) assert v.tostring() == \ '@\x01\x99\x99\x99\x99\x99\x9a\xbf\xf1\x99\x99\x99\x99\x99\x9a' assert v[0] == 2.2-1.1j assert v.real == 2.2 assert v.imag == -1.1 v = fromstring('\x9a\x99\x99\x99\x99\x99\x01@\x9a\x99\x99\x99\x99\x99\xf1\xbf', dtype=dtype('i2')[::2].tostring() == '\x00\x01\x00\x03' assert array(0, dtype='i2').tostring() == '\x00\x00' a = array([[1, 2], [3, 4]], dtype='i1') for order in (None, False, 'C', 'K', 'a'): assert a.tostring(order) == '\x01\x02\x03\x04' import sys for order in (True, 'F'): assert a.tostring(order) == '\x01\x03\x02\x04' assert array(2.2-1.1j, dtype='>c16').tostring() == \ '@\x01\x99\x99\x99\x99\x99\x9a\xbf\xf1\x99\x99\x99\x99\x99\x9a' assert array(2.2-1.1j, dtype=' 0 and x['two'] > 2 else: assert x['one'] == 1 and x['two'] == 2 def test_nested_dtype(self): import numpy as np a = [('x', int), ('y', float)] b = [('x', int), ('y', a)] arr = np.zeros((), dtype=b) assert arr['x'] == 0 arr['x'] = 2 assert arr['x'] == 2 exc = raises(IndexError, "arr[3L]") assert exc.value.message == "too many indices for array" exc = raises(ValueError, "arr['xx'] = 2") assert exc.value.message == "no field of name xx" assert arr['y'].dtype == a assert arr['y'].shape == () assert arr['y'][()]['x'] == 0 assert arr['y'][()]['y'] == 0 arr['y'][()]['x'] = 2 arr['y'][()]['y'] = 3 assert arr['y'][()]['x'] == 2 assert arr['y'][()]['y'] == 3 arr = np.zeros(3, dtype=b) arr[1]['x'] = 15 assert arr[1]['x'] == 15 arr[1]['y']['y'] = 3.5 assert arr[1]['y']['y'] == 3.5 assert arr[1]['y']['x'] == 0.0 assert arr[1]['x'] == 15 def test_count_nonzero(self): import numpy as np import sys d = [('f0', 'i4'), ('f1', 'i4', 2)] arr = np.array([0, 1]) if '__pypy__' not in sys.builtin_module_names: arr = arr.astype(d)[:1] assert np.count_nonzero(arr) == 0 else: raises(NotImplementedError, "arr.astype(d)") def test_string_record(self): from numpy import dtype, array d = dtype([('x', str), ('y', 'int32')]) assert str(d.fields['x'][0]) == '|S0' assert d.fields['x'][1] == 0 assert str(d.fields['y'][0]) == 'int32' assert d.fields['y'][1] == 0 assert d.name == 'void32' a = array([('a', 2), ('cde', 1)], dtype=d) if 0: # XXX why does numpy allow this? assert a[0]['x'] == '\x02' assert a[0]['y'] == 2 if 0: # XXX why does numpy allow this? assert a[1]['x'] == '\x01' assert a[1]['y'] == 1 d = dtype([('x', 'S1'), ('y', 'int32')]) assert str(d.fields['x'][0]) == '|S1' assert d.fields['x'][1] == 0 assert str(d.fields['y'][0]) == 'int32' assert d.fields['y'][1] == 1 assert d.name == 'void40' a = array([('a', 2), ('cde', 1)], dtype=d) assert a[0]['x'] == 'a' assert a[0]['y'] == 2 assert a[1]['x'] == 'c' assert a[1]['y'] == 1 def test_string_array(self): from numpy import array a = array(['abc']) assert str(a.dtype) == '|S3' a = array(['abc'], 'S') assert str(a.dtype) == '|S3' a = array(['abc'], 'S3') assert str(a.dtype) == '|S3' a = array(['abcde'], 'S3') assert str(a.dtype) == '|S3' a = array(['abc', 'defg', 'ab']) assert str(a.dtype) == '|S4' assert a[0] == 'abc' assert a[1] == 'defg' assert a[2] == 'ab' a = array(['abc', 'defg', 'ab'], 'S3') assert str(a.dtype) == '|S3' assert a[0] == 'abc' assert a[1] == 'def' assert a[2] == 'ab' b = array(["\x00\x01", "\x00\x02\x03"], dtype=str) assert str(b.dtype) == '|S3' assert b[0] == "\x00\x01" assert b[1] == "\x00\x02\x03" assert b.tostring() == "\x00\x01\x00\x00\x02\x03" c = b.astype(b.dtype) assert (b == c).all() assert c.tostring() == "\x00\x01\x00\x00\x02\x03" raises(TypeError, a, 'sum') raises(TypeError, 'a+a') b = array(['abcdefg', 'ab', 'cd']) assert a[2] == b[1] assert bool(a[1]) c = array(['ab','cdefg','hi','jk']) # not implemented yet #c[0] += c[3] #assert c[0] == 'abjk' def test_to_str(self): from numpy import array a = array(['abc','abc', 'def', 'ab'], 'S3') b = array(['mnopqr','abcdef', 'ab', 'cd']) assert b[1] != a[1] def test_string_scalar(self): from numpy import array a = array('ffff') assert a.shape == () a = array([], dtype='S') assert str(a.dtype) == '|S1' a = array('x', dtype='>S') assert str(a.dtype) == '|S1' a = array('x', dtype='c') assert str(a.dtype) == '|S1' assert a == 'x' a = array('abc', 'S2') assert a.dtype.str == '|S2' assert a == 'ab' a = array('abc', 'S5') assert a.dtype.str == '|S5' assert a == 'abc' def test_newbyteorder(self): import numpy as np a = np.array([1, 2], dtype=np.int16) b = a.newbyteorder() assert (b == [256, 512]).all() c = b.byteswap() assert (c == [1, 2]).all() assert (a == [1, 2]).all() def test_pickle(self): from numpy import dtype, array, int32 from cPickle import loads, dumps d = dtype([('x', str), ('y', 'int32')]) a = array([('a', 2), ('cde', 1)], dtype=d) a = loads(dumps(a)) d = a.dtype assert str(d.fields['x'][0]) == '|S0' assert d.fields['x'][1] == 0 assert str(d.fields['y'][0]) == 'int32' assert d.fields['y'][1] == 0 assert d.name == 'void32' assert a[0]['y'] == 2 assert a[1]['y'] == 1 a = array([(1, [])], dtype=[('a', int32), ('b', int32, 0)]) assert a['b'].shape == (1, 0) b = loads(dumps(a)) assert b['b'].shape == (1, 0) def test_subarrays(self): from numpy import dtype, array, zeros d = dtype([("x", "int", 3), ("y", "float", 5)]) a = zeros((), dtype=d) #assert a['x'].dtype == int #assert a['x'].shape == (3,) #assert (a['x'] == [0, 0, 0]).all() a = array([([1, 2, 3], [0.5, 1.5, 2.5, 3.5, 4.5]), ([4, 5, 6], [5.5, 6.5, 7.5, 8.5, 9.5])], dtype=d) for v in ['x', u'x', 0, -2]: assert (a[0][v] == [1, 2, 3]).all() assert (a[1][v] == [4, 5, 6]).all() for v in ['y', u'y', -1, 1]: assert (a[0][v] == [0.5, 1.5, 2.5, 3.5, 4.5]).all() assert (a[1][v] == [5.5, 6.5, 7.5, 8.5, 9.5]).all() for v in [-3, 2]: exc = raises(IndexError, "a[0][%d]" % v) assert exc.value.message == "invalid index (%d)" % \ (v + 2 if v < 0 else v) exc = raises(ValueError, "a[0]['z']") assert exc.value.message == "no field of name z" exc = raises(IndexError, "a[0][None]") assert exc.value.message == "invalid index" a[0]["x"][0] = 200 assert a[0]["x"][0] == 200 a[1]["x"][2] = 123 assert (a[1]["x"] == [4, 5, 123]).all() a[1]['y'][3] = 4 assert a[1]['y'][3] == 4 assert a['y'][1][3] == 4 a['y'][1][4] = 5 assert a[1]['y'][4] == 5 d = dtype([("x", "int64", (2, 3))]) a = array([([[1, 2, 3], [4, 5, 6]],)], dtype=d) assert a[0]["x"].dtype == dtype("int64") assert a[0]["x"][0].dtype == dtype("int64") assert (a[0]["x"][0] == [1, 2, 3]).all() assert (a[0]["x"] == [[1, 2, 3], [4, 5, 6]]).all() d = dtype((float, (10, 10))) a = zeros((3,3), dtype=d) assert a[0, 0].shape == (10, 10) assert a.shape == (3, 3, 10, 10) a[0, 0] = 500 assert (a[0, 0, 0] == 500).all() assert a[0, 0, 0].shape == (10,) exc = raises(IndexError, "a[0, 0]['z']") assert exc.value.message.startswith('only integers, slices') import sys a = array(1.5, dtype=float) assert a.shape == () if '__pypy__' not in sys.builtin_module_names: a = a.astype((float, 2)) repr(a) # check for crash assert a.shape == (2,) assert tuple(a) == (1.5, 1.5) else: raises(NotImplementedError, "a.astype((float, 2))") a = array([1.5], dtype=float) assert a.shape == (1,) if '__pypy__' not in sys.builtin_module_names: a = a.astype((float, 2)) repr(a) # check for crash assert a.shape == (1, 2) assert tuple(a[0]) == (1.5, 1.5) else: raises(NotImplementedError, "a.astype((float, 2))") def test_subarray_multiple_rows(self): import numpy as np descr = [ ('x', 'i4', (2,)), ('y', 'f8', (2, 2)), ('z', 'u1')] buf = [ # x y z ([3,2], [[6.,4.],[6.,4.]], 8), ([4,3], [[7.,5.],[7.,5.]], 9), ] h = np.array(buf, dtype=descr) assert len(h) == 2 assert h['x'].shape == (2, 2) assert h['y'].strides == (41, 16, 8) assert h['z'].shape == (2,) for v in (h, h[0], h['x']): repr(v) # check for crash in repr assert (h['x'] == np.array([buf[0][0], buf[1][0]], dtype='i4')).all() assert (h['y'] == np.array([buf[0][1], buf[1][1]], dtype='f8')).all() assert (h['z'] == np.array([buf[0][2], buf[1][2]], dtype='u1')).all() def test_multidim_subarray(self): from numpy import dtype, array d = dtype([("x", "int64", (2, 3))]) a = array([([[1, 2, 3], [4, 5, 6]],)], dtype=d) assert a[0]["x"].dtype == dtype("int64") assert a[0]["x"][0].dtype == dtype("int64") assert (a[0]["x"][0] == [1, 2, 3]).all() assert (a[0]["x"] == [[1, 2, 3], [4, 5, 6]]).all() def test_list_record(self): from numpy import dtype, array d = dtype([("x", "int", 3), ("y", "float", 5)]) a = array([([1, 2, 3], [0.5, 1.5, 2.5, 3.5, 4.5]), ([4, 5, 6], [5.5, 6.5, 7.5, 8.5, 9.5])], dtype=d) assert len(list(a[0])) == 2 mdtype = dtype([('a', bool), ('b', bool), ('c', bool)]) a = array([0, 0, 0, 1, 1]) # this creates a value of (x, x, x) in b for each x in a b = array(a, dtype=mdtype) assert b.shape == a.shape c = array([(x, x, x) for x in [0, 0, 0, 1, 1]], dtype=mdtype) assert (b == c).all() def test_3d_record(self): from numpy import dtype, array dt = dtype([('name', 'S4'), ('x', float), ('y', float), ('block', int, (2, 2, 3))]) a = array([('aaaa', 1.0, 8.0, [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])], dtype=dt) i = a.item() assert isinstance(i, tuple) assert len(i) == 4 import sys if '__pypy__' not in sys.builtin_module_names: assert str(a) == "[('aaaa', 1.0, 8.0, [[[1, 2, 3], [4, 5, 6]], " \ "[[7, 8, 9], [10, 11, 12]]])]" else: assert str(a) == "[('aaaa', 1.0, 8.0, [1, 2, 3, 4, 5, 6, " \ "7, 8, 9, 10, 11, 12])]" def test_issue_1589(self): import numpy c = numpy.array([[(1, 2, 'a'), (3, 4, 'b')], [(5, 6, 'c'), (7, 8, 'd')]], dtype=[('bg', 'i8'), ('fg', 'i8'), ('char', 'S1')]) assert c[0][0]["char"] == 'a' def test_scalar_coercion(self): import numpy as np a = np.array([1,2,3], dtype='int16') assert (a * 2).dtype == np.dtype('int16') def test_coerce_record(self): import numpy as np dt = np.dtype([('a', '?'), ('b', '?')]) b = np.array([True, True]) a = np.array([b, b, b], dtype=dt) assert a.shape == (3, 2) for i in a.flat: assert tuple(i) == (True, True) dt = np.dtype([('A', '