Esempio n. 1
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()
Esempio n. 2
0
    def setUp(self):
        a0 = [[[1, [1, 1, 1, 1], [2, 2], [2, 2]], 1], [2, 2, 1, 1], [3, 3],
              [3, 3]]
        self.t0 = tinynumpy.array(a0)

        a1 = [[1, 2], [3, 4]]
        self.t1 = tinynumpy.array(a1)
        self.n1 = numpy.array(a1)
Esempio n. 3
0
def test_dtype():
    
    for shape in [(9, ), (9, 4), (9, 4, 5)]:
        for dtype in ['bool', 'int8', 'uint8', 'int16', 'uint16',
                      'int32', 'uint32', 'float32', 'float64']:
            a = np.empty(shape, dtype=dtype)
            b = tnp.empty(shape, dtype=dtype)
            assert a.shape == b.shape
            assert a.dtype == b.dtype
            assert a.itemsize == b.itemsize
    
    raises(TypeError, tnp.zeros, (9, ), 'blaa')
    
    assert tnp.array([1.0, 2.0]).dtype == 'float64'
    assert tnp.array([1, 2]).dtype == 'int64'
Esempio n. 4
0
 def setUp(self):
     a = [[ 1.0, 2.0, 3.0, 4.0],
          [ 5.0, 6.0, 7.0, 8.0],
          [ 9.0,10.0,11.0,12.0],
          [13.0,14.0,15.0,16.0]]
     self.t0 = tinynumpy.array(a)
     self.n0 = numpy.array(a)
Esempio n. 5
0
 def setUp(self):
     a = [[ 1.0, 2.0, 3.0, 4.0],
          [ 5.0, 6.0, 7.0, 8.0],
          [ 9.0,10.0,11.0,12.0],
          [13.0,14.0,15.0,16.0]]
     self.t0 = tinynumpy.array(a)
     self.n0 = numpy.array(a)
Esempio n. 6
0
def test_subtract():
    """test the addition function for tinynumpy"""

    x = [5, -2, 1]
    y = [0, 3, -1]

    a = tnp.add(x,y)

    assert a == tnp.array([5, -5, -2], dtype='int64')
Esempio n. 7
0
def test_divide():
    """test the addition function for tinynumpy"""

    x = [15, -12, 3]
    y = 3

    a = tnp.divide(x,y)

    assert a == tnp.array([5, -4, 1], dtype='int64')
Esempio n. 8
0
def test_multiply():
    """test the addition function for tinynumpy"""

    x = [5, -2, 1]
    y = [0, 3, -1]

    a = tnp.multiply(x,y)

    assert a == tnp.array([0, -6, -1], dtype='int64')
Esempio n. 9
0
    def setUp(self):
        a0 = [
                [
                    [
                        1, 
                        [1, 1, 1, 1], 
                        [2, 2],
                        [2, 2]
                    ], 
                    1
                ], 
                [2, 2, 1, 1], 
                [3, 3],
                [3, 3]
            ]
        self.t0 = tinynumpy.array(a0)

        a1 = [[1,2],[3,4]]
        self.t1 = tinynumpy.array(a1)
        self.n1 = numpy.array(a1)
Esempio n. 10
0
def test_repr():
    
    for dtype in ['float32', 'float64', 'int32', 'int64']:
        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]]],
                    ]:
            a = np.array(data, dtype)
            b = tnp.array(data, dtype)
            # Compare line by line (forget leading whitespace)
            charscompared = 0
            for l1, l2 in zip(repr(a).splitlines(), repr(b).splitlines()):
                l1, l2 = l1.rstrip(), l2.rstrip()
                l1, l2 = l1.split('dtype=')[0], l2.split('dtype=')[0]
                assert l1 == l2
                charscompared += len(l1)
            assert charscompared > (3 * b.size)
Esempio n. 11
0
def test_reshape():
    
    a = np.array([1, 2, 3, 4, 5, 6, 7, 8], dtype='int32')
    b = tnp.array([1, 2, 3, 4, 5, 6, 7, 8], dtype='int32')
    
    for shape in [(2, 4), (4, 2), (2, 2, 2), (8,)]:
        a.shape = shape
        b.shape = shape
        assert a.shape == b.shape
        print(repr(a), repr(b), a.strides, b.strides)
        assert a.strides == b.strides
    
    a.shape = 2, 4
    b.shape = 2, 4
    
    # Test transpose
    assert b.T.shape == (4, 2)
    assert (a.T == b.T).all()
    assert (b.T.T == b).all()
    
    # Make non-contiguous versions
    a2 = a[:, 2:]
    b2 = b[:, 2:]
    
    # Test contiguous flag
    assert a.flags['C_CONTIGUOUS']
    assert not a2.flags['C_CONTIGUOUS']
    
    # Test base
    assert a2.base is a
    assert b2.base is b
    assert a2[:].base is a
    assert b2[:].base is b
    
    # Fail
    with raises(ValueError):  # Invalid shape
        a.shape = (3, 3)
    with raises(ValueError):
        b.shape = (3, 3)
    with raises(AttributeError):  # Cannot reshape non-contiguous arrays
        a2.shape = 4,
    with raises(AttributeError):
        b2.shape = 4,
Esempio n. 12
0
 def test_var(self):
     self.assertEqual(tinynumpy.array([1,1,1,1]).var(), 0)
     self.assertEqual(_clean_repr(self.t1.var()), _clean_repr(self.n1.var()))
Esempio n. 13
0
 def test_cumprod(self):
     self.assertEqual(_clean_repr(tinynumpy.array([1, 2, 3]).cumprod()), _clean_repr(tinynumpy.array([1, 2, 6])))
Esempio n. 14
0
 def test_mean(self):
     self.assertEqual(tinynumpy.array([-5, 5]).mean(), 0)
Esempio n. 15
0
 def test_prod(self):
     self.assertEqual(tinynumpy.array([1, 2, 3, 10]).prod(), 60)
Esempio n. 16
0
 def test_any(self):
     self.assertEqual(tinynumpy.array([1, 0, 1]).any(), True)
Esempio n. 17
0
 def test_mean(self):
     self.assertEqual(tinynumpy.array([-5, 5]).mean(), 0)
Esempio n. 18
0
def test_creating_functions():
    
    # Test array
    b1 = tnp.array([[1, 2, 3], [4, 5, 6]])
    assert b1.shape == (2, 3)
Esempio n. 19
0
 def test_any(self):
     self.assertEqual(tinynumpy.array([1, 0, 1]).any(), True)
Esempio n. 20
0
 def test_std(self):
     self.assertEqual(tinynumpy.array([1,1,1,1]).std(), 0)
     self.assertEqual(int(self.t1.std()*1000000), int(self.n1.std()*1000000))
Esempio n. 21
0
 def test_cumprod(self):
     self.assertEqual(_clean_repr(tinynumpy.array([1, 2, 3]).cumprod()), _clean_repr(tinynumpy.array([1, 2, 6])))
Esempio n. 22
0
 def test_all(self):
     self.assertEqual(tinynumpy.array([1, 0, 1]).all(), False)
Esempio n. 23
0
 def test_argmax(self):
     self.assertEqual(tinynumpy.array([1, 2, 3]).argmax(), 2)
Esempio n. 24
0
 def test_argmin(self):
     self.assertEqual(tinynumpy.array([1, 2, -3]).argmin(), 2)
Esempio n. 25
0
 def test_argmax(self):
     self.assertEqual(tinynumpy.array([1, 2, 3]).argmax(), 2)
Esempio n. 26
0
 def setUp(self):
     self.t0 = tinynumpy.array([[1.0,2.0],[3.0,4.0]])
     self.n0 = numpy.array([[1.0,2.0],[3.0,4.0]])
Esempio n. 27
0
 def test_sum(self):
     self.assertEqual(tinynumpy.array([1, 2, -3]).sum(), 0)
Esempio n. 28
0
 def test_flatten(self):
     self.assertEqual(_clean_repr(self.t1.flatten()), _clean_repr(tinynumpy.array([1, 2, 3, 4])))
Esempio n. 29
0
 def test_prod(self):
     self.assertEqual(tinynumpy.array([1, 2, 3, 10]).prod(), 60)
Esempio n. 30
0
 def test_ptp(self):
     self.assertEqual(tinynumpy.array([-1, 2, 3]).ptp(), 4)
Esempio n. 31
0
 def test_ptp(self):
     self.assertEqual(tinynumpy.array([-1, 2, 3]).ptp(), 4)
Esempio n. 32
0
def test_getitem():
     
    a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
    b = tnp.array([[1, 2, 3, 4], [5, 6, 7, 8]])
Esempio n. 33
0
 def test_flatten(self):
     self.assertEqual(_clean_repr(self.t1.flatten()), _clean_repr(tinynumpy.array([1, 2, 3, 4])))
Esempio n. 34
0
 def test_sum(self):
     self.assertEqual(tinynumpy.array([1, 2, -3]).sum(), 0)
Esempio n. 35
0
 def test_var(self):
     self.assertEqual(tinynumpy.array([1,1,1,1]).var(), 0)
     self.assertEqual(_clean_repr(self.t1.var()), _clean_repr(self.n1.var()))
Esempio n. 36
0
 def test_all(self):
     self.assertEqual(tinynumpy.array([1, 0, 1]).all(), False)
Esempio n. 37
0
 def test_std(self):
     self.assertEqual(tinynumpy.array([1,1,1,1]).std(), 0)
     self.assertEqual(int(self.t1.std()*1000000), int(self.n1.std()*1000000))
Esempio n. 38
0
 def test_argmin(self):
     self.assertEqual(tinynumpy.array([1, 2, -3]).argmin(), 2)
Esempio n. 39
0
 def setUp(self):
     self.t0 = tinynumpy.array([[1.0,2.0],[3.0,4.0]])
     self.n0 = numpy.array([[1.0,2.0],[3.0,4.0]])