コード例 #1
0
ファイル: test_tinynumpy.py プロジェクト: wadetb/tinynumpy
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()
コード例 #2
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()
コード例 #3
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)
コード例 #4
0
ファイル: test_tinynumpy.py プロジェクト: wadetb/tinynumpy
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'
コード例 #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)
コード例 #6
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'
コード例 #7
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')
コード例 #8
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')
コード例 #9
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')
コード例 #10
0
ファイル: test_tinynumpy.py プロジェクト: wadetb/tinynumpy
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')
コード例 #11
0
ファイル: test_tinynumpy.py プロジェクト: wadetb/tinynumpy
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')
コード例 #12
0
ファイル: test_tinynumpy.py プロジェクト: wadetb/tinynumpy
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')
コード例 #13
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)
コード例 #14
0
ファイル: test_tinynumpy.py プロジェクト: wadetb/tinynumpy
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)
コード例 #15
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)
コード例 #16
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,
コード例 #17
0
ファイル: test_tinynumpy.py プロジェクト: wadetb/tinynumpy
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,
コード例 #18
0
 def test_any(self):
     self.assertEqual(tinynumpy.array([1, 0, 1]).any(), True)
コード例 #19
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]])
コード例 #20
0
 def test_ptp(self):
     self.assertEqual(tinynumpy.array([-1, 2, 3]).ptp(), 4)
コード例 #21
0
 def test_sum(self):
     self.assertEqual(tinynumpy.array([1, 2, -3]).sum(), 0)
コード例 #22
0
 def test_any(self):
     self.assertEqual(tinynumpy.array([1, 0, 1]).any(), True)
コード例 #23
0
 def test_cumprod(self):
     self.assertEqual(_clean_repr(tinynumpy.array([1, 2, 3]).cumprod()),
                      _clean_repr(tinynumpy.array([1, 2, 6])))
コード例 #24
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)
コード例 #25
0
 def test_ptp(self):
     self.assertEqual(tinynumpy.array([-1, 2, 3]).ptp(), 4)
コード例 #26
0
 def test_flatten(self):
     self.assertEqual(_clean_repr(self.t1.flatten()), _clean_repr(tinynumpy.array([1, 2, 3, 4])))
コード例 #27
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()))
コード例 #28
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()))
コード例 #29
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))
コード例 #30
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]])
コード例 #31
0
 def test_all(self):
     self.assertEqual(tinynumpy.array([1, 0, 1]).all(), False)
コード例 #32
0
 def test_argmax(self):
     self.assertEqual(tinynumpy.array([1, 2, 3]).argmax(), 2)
コード例 #33
0
 def test_cumprod(self):
     self.assertEqual(_clean_repr(tinynumpy.array([1, 2, 3]).cumprod()), _clean_repr(tinynumpy.array([1, 2, 6])))
コード例 #34
0
 def test_prod(self):
     self.assertEqual(tinynumpy.array([1, 2, 3, 10]).prod(), 60)
コード例 #35
0
 def test_argmax(self):
     self.assertEqual(tinynumpy.array([1, 2, 3]).argmax(), 2)
コード例 #36
0
ファイル: test_tinynumpy.py プロジェクト: wadetb/tinynumpy
def test_creating_functions():
    
    # Test array
    b1 = tnp.array([[1, 2, 3], [4, 5, 6]])
    assert b1.shape == (2, 3)
コード例 #37
0
 def test_all(self):
     self.assertEqual(tinynumpy.array([1, 0, 1]).all(), False)
コード例 #38
0
ファイル: test_tinynumpy.py プロジェクト: wadetb/tinynumpy
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]])
コード例 #39
0
 def test_argmin(self):
     self.assertEqual(tinynumpy.array([1, 2, -3]).argmin(), 2)
コード例 #40
0
 def test_sum(self):
     self.assertEqual(tinynumpy.array([1, 2, -3]).sum(), 0)
コード例 #41
0
 def test_prod(self):
     self.assertEqual(tinynumpy.array([1, 2, 3, 10]).prod(), 60)
コード例 #42
0
 def test_argmin(self):
     self.assertEqual(tinynumpy.array([1, 2, -3]).argmin(), 2)
コード例 #43
0
 def test_flatten(self):
     self.assertEqual(_clean_repr(self.t1.flatten()),
                      _clean_repr(tinynumpy.array([1, 2, 3, 4])))
コード例 #44
0
def test_creating_functions():

    # Test array
    b1 = tnp.array([[1, 2, 3], [4, 5, 6]])
    assert b1.shape == (2, 3)
コード例 #45
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))
コード例 #46
0
def avg(data):
    data = remove_all(data, [None, ""])
    if len(data) > 0:
        if is_number(data[0]): return normalize(tinynumpy.array(data).mean())
        else: return __builtin__.max(set(data), key=data.count)
    else: return None
コード例 #47
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]])
コード例 #48
0
def sum(data):
    data = remove_all(data, [None, ""])
    if len(data) > 0:
        if is_number(data[0]): return normalize(tinynumpy.array(data).sum())
        else: return 0
    else: return 0
コード例 #49
0
 def test_mean(self):
     self.assertEqual(tinynumpy.array([-5, 5]).mean(), 0)
コード例 #50
0
 def test_mean(self):
     self.assertEqual(tinynumpy.array([-5, 5]).mean(), 0)