Ejemplo n.º 1
0
def test_from_bytes():
    skip('Need ndarray.frombytes or something')
    # Create bytes
    buffer = b'x' * 100
    
    # Create array!
    b = tnp.ndarray((4, 25), 'uint8', buffer=buffer)
    ptr = ctypes.cast(buffer, ctypes.c_void_p).value
    
    # Check that we can turn it into a numpy array
    a = np.array(b, copy=False)
    assert (a == b).all()
    
    # Verify readonly
    with raises(Exception):
        a[0, 0] = 1  
    with raises(Exception):
        b[0, 0] = 1  
    
    # Verify that both point to the same data
    assert a.__array_interface__['data'][0] == ptr
    assert b.__array_interface__['data'][0] == ptr
    
    # also verify offset in __array_interface__ here
    for a0, b0 in zip([a[2:], a[:, 10::2], a[1::2, 10:20:2]],
                        [b[2:], b[:, 10::2], b[1::2, 10:20:2]]):
        pa = a0.__array_interface__['data'][0]
        pb = b0.__array_interface__['data'][0]
        assert pa > ptr
        assert pa == pb
Ejemplo n.º 2
0
def test_from_ctypes():
    
    for type, dtype in [(ctypes.c_int16, 'int16'), 
                        (ctypes.c_uint8, 'uint8'), 
                        (ctypes.c_float, 'float32'), 
                        (ctypes.c_double, 'float64')]:
        # Create ctypes array, possibly something that we get from a c lib
        buffer = (type*100)()
        
        # Create array!
        b = tnp.ndarray((4, 25), dtype, buffer=buffer)
        
        # Check that we can turn it into a numpy array
        a = np.array(b, copy=False)
        assert (a == b).all()
        assert a.dtype == dtype
        
        # Verify that both point to the same data
        assert a.__array_interface__['data'][0] == ctypes.addressof(buffer)
        assert b.__array_interface__['data'][0] == ctypes.addressof(buffer)
        
        # also verify offset in __array_interface__ here
        for a0, b0 in zip([a[2:], a[:, 10::2], a[1::2, 10:20:2]],
                          [b[2:], b[:, 10::2], b[1::2, 10:20:2]]):
            pa = a0.__array_interface__['data'][0]
            pb = b0.__array_interface__['data'][0]
            assert pa > ctypes.addressof(buffer)
            assert pa == pb
Ejemplo n.º 3
0
def test_from_and_to_numpy():
    # This also tests __array_interface__
    
    for dtype in ['float32', 'float64', 'int32', 'uint32', 'uint8', 'int8']:
        for data in [[1, 2, 3, 4, 5, 6, 7, 8],
                    [[1, 2], [3, 4], [5, 6], [7, 8]],
                    [[[1, 2], [3, 4]],[[5, 6], [7, 8]]],
                    ]:
                        
            # Convert from numpy, from tinynumpy, to numpy
            a1 = np.array(data, dtype)
            b1 = tnp.array(a1)
            b2 = tnp.array(b1)
            a2 = np.array(b2)
            
            # Check if its the same
            for c in [b1, b2, a2]:
                assert a1.shape == c.shape
                assert a1.dtype == c.dtype
                assert a1.strides == c.strides
                assert (a1 == c).all()
    
    # Also test using a numpy array as a buffer
    a = np.array([[1, 2], [3, 4], [5, 6], [7, 8]], 'float32')
    b = tnp.ndarray(a.shape, a.dtype, strides=a.strides, buffer=a.ravel())
    assert (a==b).all()
    
    # Test that is indeed same data
    a[0, 0] = 99
    assert (a==b).all()
Ejemplo n.º 4
0
def test_from_bytes():
    skip('Need ndarray.frombytes or something')
    # Create bytes
    buffer = b'x' * 100

    # Create array!
    b = tnp.ndarray((4, 25), 'uint8', buffer=buffer)
    ptr = ctypes.cast(buffer, ctypes.c_void_p).value

    # Check that we can turn it into a numpy array
    a = np.array(b, copy=False)
    assert (a == b).all()

    # Verify readonly
    with raises(Exception):
        a[0, 0] = 1
    with raises(Exception):
        b[0, 0] = 1

    # Verify that both point to the same data
    assert a.__array_interface__['data'][0] == ptr
    assert b.__array_interface__['data'][0] == ptr

    # also verify offset in __array_interface__ here
    for a0, b0 in zip([a[2:], a[:, 10::2], a[1::2, 10:20:2]],
                      [b[2:], b[:, 10::2], b[1::2, 10:20:2]]):
        pa = a0.__array_interface__['data'][0]
        pb = b0.__array_interface__['data'][0]
        assert pa > ptr
        assert pa == pb
Ejemplo n.º 5
0
def test_from_ctypes():

    for type, dtype in [(ctypes.c_int16, 'int16'), (ctypes.c_uint8, 'uint8'),
                        (ctypes.c_float, 'float32'),
                        (ctypes.c_double, 'float64')]:
        # Create ctypes array, possibly something that we get from a c lib
        buffer = (type * 100)()

        # Create array!
        b = tnp.ndarray((4, 25), dtype, buffer=buffer)

        # Check that we can turn it into a numpy array
        a = np.array(b, copy=False)
        assert (a == b).all()
        assert a.dtype == dtype

        # Verify that both point to the same data
        assert a.__array_interface__['data'][0] == ctypes.addressof(buffer)
        assert b.__array_interface__['data'][0] == ctypes.addressof(buffer)

        # also verify offset in __array_interface__ here
        for a0, b0 in zip([a[2:], a[:, 10::2], a[1::2, 10:20:2]],
                          [b[2:], b[:, 10::2], b[1::2, 10:20:2]]):
            pa = a0.__array_interface__['data'][0]
            pb = b0.__array_interface__['data'][0]
            assert pa > ctypes.addressof(buffer)
            assert pa == pb
Ejemplo n.º 6
0
def test_from_and_to_numpy():
    # This also tests __array_interface__

    for dtype in ['float32', 'float64', 'int32', 'uint32', 'uint8', 'int8']:
        for data in [
            [1, 2, 3, 4, 5, 6, 7, 8],
            [[1, 2], [3, 4], [5, 6], [7, 8]],
            [[[1, 2], [3, 4]], [[5, 6], [7, 8]]],
        ]:

            # Convert from numpy, from tinynumpy, to numpy
            a1 = np.array(data, dtype)
            b1 = tnp.array(a1)
            b2 = tnp.array(b1)
            a2 = np.array(b2)

            # Check if its the same
            for c in [b1, b2, a2]:
                assert a1.shape == c.shape
                assert a1.dtype == c.dtype
                assert a1.strides == c.strides
                assert (a1 == c).all()

    # Also test using a numpy array as a buffer
    a = np.array([[1, 2], [3, 4], [5, 6], [7, 8]], 'float32')
    b = tnp.ndarray(a.shape, a.dtype, strides=a.strides, buffer=a.ravel())
    assert (a == b).all()

    # Test that is indeed same data
    a[0, 0] = 99
    assert (a == b).all()