Esempio n. 1
0
    def testArange(self):
        t = arange(10, chunk_size=3)
        t.tiles()

        self.assertEqual(t.shape, (10,))
        self.assertEqual(calc_shape(t), t.shape)
        self.assertEqual(t.nsplits, ((3, 3, 3, 1),))
        self.assertEqual(t.chunks[1].op.start, 3)
        self.assertEqual(t.chunks[1].op.stop, 6)
        self.assertEqual(calc_shape(t.chunks[1]), t.chunks[1].shape)

        t = arange(0, 10, 3, chunk_size=2)
        t.tiles()

        self.assertEqual(t.shape, (4,))
        self.assertEqual(calc_shape(t), t.shape)
        self.assertEqual(t.nsplits, ((2, 2),))
        self.assertEqual(t.chunks[0].op.start, 0)
        self.assertEqual(t.chunks[0].op.stop, 6)
        self.assertEqual(t.chunks[0].op.step, 3)
        self.assertEqual(t.chunks[1].op.start, 6)
        self.assertEqual(t.chunks[1].op.stop, 12)
        self.assertEqual(t.chunks[1].op.step, 3)
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)
        self.assertEqual(calc_shape(t.chunks[1]), t.chunks[1].shape)

        self.assertRaises(TypeError, lambda: arange(10, start=0))
        self.assertRaises(TypeError, lambda: arange(0, 10, stop=0))
        self.assertRaises(TypeError, lambda: arange())
        self.assertRaises(ValueError, lambda: arange('1066-10-13', dtype=np.datetime64, chunks=3))
Esempio n. 2
0
    def testBoolIndexingTiles(self):
        t = ones((100, 200, 300), chunk_size=30)
        indexed = t[t < 2]
        indexed.tiles()

        self.assertEqual(len(indexed.chunks), 280)
        self.assertEqual(indexed.chunks[0].index, (0, ))
        self.assertEqual(indexed.chunks[20].index, (20, ))
        self.assertIs(indexed.chunks[20].inputs[0], t.cix[(0, 2, 0)].data)
        self.assertIs(indexed.chunks[20].inputs[1],
                      indexed.op.indexes[0].cix[0, 2, 0].data)
        self.assertEqual(calc_shape(indexed.chunks[0]),
                         indexed.chunks[0].shape)
        self.assertEqual(calc_shape(indexed.chunks[20]),
                         indexed.chunks[20].shape)

        t2 = ones((100, 200), chunk_size=30)
        indexed2 = t[t2 < 2]
        indexed2.tiles()

        self.assertEqual(len(indexed2.chunks), 280)
        self.assertEqual(len(indexed2.chunks[0].shape), 2)
        self.assertTrue(np.isnan(indexed2.chunks[0].shape[0]))
        self.assertEqual(indexed2.chunks[0].shape[1], 30)
        self.assertEqual(indexed2.chunks[20].inputs[0], t.cix[(0, 2, 0)].data)
        self.assertEqual(indexed2.chunks[20].inputs[1],
                         indexed2.op.indexes[0].cix[0, 2].data)
        self.assertEqual(calc_shape(indexed2.chunks[0]),
                         indexed2.chunks[0].shape)
        self.assertEqual(calc_shape(indexed2.chunks[20]),
                         indexed2.chunks[20].shape)
Esempio n. 3
0
    def testMixedIndexingTiles(self):
        t = ones((100, 200, 300, 400), chunk_size=24)

        cmp = ones(400, chunk_size=24) < 2
        t2 = t[10:90:3, 5, ..., None, cmp]
        t2.tiles()

        self.assertEqual(t2.shape[:-1], (27, 300, 1))
        self.assertTrue(np.isnan(t2.shape[-1]))
        self.assertEqual(t2.chunk_shape, (4, 13, 1, 17))
        self.assertEqual(
            t2.chunks[0].op.indexes,
            [slice(10, 24, 3), 5,
             slice(None), None, cmp.cix[0, ].data])
        self.assertEqual(calc_shape(t2.chunks[0]), t2.chunks[0].shape)

        # multiple tensor type as indexes
        t3 = ones((100, 200, 300, 400), chunk_size=24)
        cmp2 = ones((100, 200), chunk_size=24) < 2
        cmp3 = ones(400, chunk_size=24) < 2
        t4 = t3[cmp2, 5, None, cmp3]
        t4.tiles()

        self.assertEqual(t4.shape[1], 1)
        self.assertTrue(np.isnan(t4.shape[0]))
        self.assertTrue(np.isnan(t4.shape[-1]))
        self.assertEqual(calc_shape(t4), t4.shape)
        self.assertEqual(t4.chunk_shape, (45, 1, 17))
        self.assertEqual(t4.chunks[0].op.indexes,
                         [cmp2.cix[0, 0].data, 5, None, cmp3.cix[0, ].data])
        self.assertEqual(calc_shape(t4.chunks[0]), t4.chunks[0].shape)
Esempio n. 4
0
    def testSetItem(self):
        shape = (10, 20, 30, 40)
        t = ones(shape, chunk_size=5, dtype='i4')
        t[5:20:3, 5, ..., :-5] = 2.2

        self.assertIsInstance(t.op, TensorIndexSetValue)
        self.assertEqual(t.shape, shape)
        self.assertIsInstance(t.inputs[0].op.outputs[0].op, TensorOnes)
        self.assertEqual(calc_shape(t), t.shape)

        t.tiles()
        self.assertIsInstance(t.chunks[0].op, TensorOnes)
        self.assertIsInstance(t.cix[1, 1, 0, 0].op, TensorIndexSetValue)
        self.assertEqual(t.cix[1, 1, 0, 0].op.value, 2)
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)
        self.assertEqual(calc_shape(t.cix[1, 1, 0, 0]), t.cix[1, 1, 0,
                                                              0].shape)

        t2 = ones(shape, chunk_size=5, dtype='i4')
        shape = t2[5:20:3, 5, ..., :-5].shape
        t2[5:20:3, 5, ..., :-5] = ones(shape, chunk_size=4, dtype='i4') * 2
        self.assertEqual(calc_shape(t2), t2.shape)

        t2.tiles()
        self.assertIsInstance(t2.chunks[0].op, TensorOnes)
        self.assertIsInstance(t2.cix[1, 1, 0, 0].op, TensorIndexSetValue)
        self.assertIsInstance(t2.cix[1, 1, 0, 0].op.value.op,
                              TensorConcatenate)
        self.assertEqual(calc_shape(t2.chunks[0]), t2.chunks[0].shape)

        with self.assertRaises(ValueError):
            t[0, 0, 0, 0] = ones(2, chunk_size=10)
Esempio n. 5
0
    def testSolve(self):
        a = mt.random.randint(1, 10, (20, 20))
        b = mt.random.randint(1, 10, (20, ))
        x = mt.linalg.solve(a, b).tiles()

        self.assertEqual(x.shape, (20, ))
        self.assertEqual(calc_shape(x), x.shape)

        a = mt.random.randint(1, 10, (20, 20), chunk_size=5)
        b = mt.random.randint(1, 10, (20, 3), chunk_size=5)
        x = mt.linalg.solve(a, b).tiles()

        self.assertEqual(x.shape, (20, 3))
        self.assertEqual(calc_shape(x), x.shape)
        self.assertEqual(calc_shape(x.chunks[0]), x.chunks[0].shape)

        # test solve
        a = sps.csr_matrix(np.random.randint(1, 10, (20, 20)))
        b = mt.random.randint(1, 10, (20, ), chunk_size=3)
        x = mt.linalg.solve(a, b).tiles()

        self.assertEqual(x.shape, (20, ))
        self.assertEqual(calc_shape(x), x.shape)
        self.assertTrue(x.op.sparse)
        self.assertTrue(x.chunks[0].op.sparse)
Esempio n. 6
0
    def testIsIn(self):
        element = 2 * arange(4, chunk_size=1).reshape(2, 2)
        test_elements = [1, 2, 4, 8]

        mask = isin(element, test_elements)
        self.assertEqual(mask.shape, (2, 2))
        self.assertEqual(mask.dtype, np.bool_)
        self.assertEqual(calc_shape(mask), mask.shape)

        mask.tiles()

        self.assertEqual(len(mask.chunks), len(element.chunks))
        self.assertEqual(len(mask.op.test_elements.chunks), 1)
        self.assertIs(mask.chunks[0].inputs[0], element.chunks[0].data)
        self.assertEqual(calc_shape(mask.chunks[0]), mask.chunks[0].shape)

        element = 2 * arange(4, chunk_size=1).reshape(2, 2)
        test_elements = tensor([1, 2, 4, 8], chunk_size=2)

        mask = isin(element, test_elements, invert=True)
        self.assertEqual(mask.shape, (2, 2))
        self.assertEqual(mask.dtype, np.bool_)
        self.assertEqual(calc_shape(mask), mask.shape)

        mask.tiles()

        self.assertEqual(len(mask.chunks), len(element.chunks))
        self.assertEqual(len(mask.op.test_elements.chunks), 1)
        self.assertIs(mask.chunks[0].inputs[0], element.chunks[0].data)
        self.assertTrue(mask.chunks[0].op.invert)
        self.assertEqual(calc_shape(mask.chunks[0]), mask.chunks[0].shape)
Esempio n. 7
0
    def testWhere(self):
        cond = tensor([[True, False], [False, True]], chunk_size=1)
        x = tensor([1, 2], chunk_size=1)
        y = tensor([3, 4], chunk_size=1)

        arr = where(cond, x, y)
        arr.tiles()

        self.assertEqual(len(arr.chunks), 4)
        self.assertEqual(calc_shape(arr), arr.shape)
        self.assertTrue(
            np.array_equal(arr.chunks[0].inputs[0].op.data, [[True]]))
        self.assertTrue(np.array_equal(arr.chunks[0].inputs[1].op.data, [1]))
        self.assertTrue(np.array_equal(arr.chunks[0].inputs[2].op.data, [3]))
        self.assertTrue(
            np.array_equal(arr.chunks[1].inputs[0].op.data, [[False]]))
        self.assertTrue(np.array_equal(arr.chunks[1].inputs[1].op.data, [2]))
        self.assertTrue(np.array_equal(arr.chunks[1].inputs[2].op.data, [4]))
        self.assertTrue(
            np.array_equal(arr.chunks[2].inputs[0].op.data, [[False]]))
        self.assertTrue(np.array_equal(arr.chunks[2].inputs[1].op.data, [1]))
        self.assertTrue(np.array_equal(arr.chunks[2].inputs[2].op.data, [3]))
        self.assertTrue(
            np.array_equal(arr.chunks[3].inputs[0].op.data, [[True]]))
        self.assertTrue(np.array_equal(arr.chunks[3].inputs[1].op.data, [2]))
        self.assertTrue(np.array_equal(arr.chunks[3].inputs[2].op.data, [4]))
        self.assertEqual(calc_shape(arr.chunks[0]), arr.chunks[0].shape)

        with self.assertRaises(ValueError):
            where(cond, x)

        x = arange(9.).reshape(3, 3)
        y = where(x < 5, x, -1)

        self.assertEqual(y.dtype, np.float64)
Esempio n. 8
0
    def testLinspace(self):
        a = linspace(2.0, 3.0, num=5, chunk_size=2)

        self.assertEqual(a.shape, (5,))
        self.assertEqual(calc_shape(a), a.shape)

        a.tiles()
        self.assertEqual(a.nsplits, ((2, 2, 1),))
        self.assertEqual(a.chunks[0].op.start, 2.)
        self.assertEqual(a.chunks[0].op.stop, 2.25)
        self.assertEqual(a.chunks[1].op.start, 2.5)
        self.assertEqual(a.chunks[1].op.stop, 2.75)
        self.assertEqual(a.chunks[2].op.start, 3.)
        self.assertEqual(a.chunks[2].op.stop, 3.)
        self.assertEqual(calc_shape(a.chunks[0]), a.chunks[0].shape)

        a = linspace(2.0, 3.0, num=5, endpoint=False, chunk_size=2)

        self.assertEqual(a.shape, (5,))
        self.assertEqual(calc_shape(a), a.shape)

        a.tiles()
        self.assertEqual(a.nsplits, ((2, 2, 1),))
        self.assertEqual(a.chunks[0].op.start, 2.)
        self.assertEqual(a.chunks[0].op.stop, 2.2)
        self.assertEqual(a.chunks[1].op.start, 2.4)
        self.assertEqual(a.chunks[1].op.stop, 2.6)
        self.assertEqual(a.chunks[2].op.start, 2.8)
        self.assertEqual(a.chunks[2].op.stop, 2.8)
        self.assertEqual(calc_shape(a.chunks[0]), a.chunks[0].shape)

        _, step = linspace(2.0, 3.0, num=5, chunk_size=2, retstep=True)
        self.assertEqual(step, .25)
Esempio n. 9
0
    def testOnes(self):
        tensor = ones((10, 10, 8), chunk_size=(3, 3, 5))
        tensor.tiles()
        self.assertEqual(tensor.shape, (10, 10, 8))
        self.assertEqual(calc_shape(tensor), tensor.shape)
        self.assertEqual(len(tensor.chunks), 32)
        self.assertEqual(calc_shape(tensor.chunks[0]), tensor.chunks[0].shape)

        tensor = ones((10, 3), chunk_size=(4, 2))
        tensor.tiles()
        self.assertEqual(tensor.shape, (10, 3))
        self.assertEqual(calc_shape(tensor), tensor.shape)

        chunk = tensor.cix[1, 1]
        self.assertEqual(tensor.get_chunk_slices(chunk.index),
                         (slice(4, 8), slice(2, 3)))
        self.assertEqual(calc_shape(chunk), chunk.shape)

        tensor = ones((10, 5), chunk_size=(2, 3), gpu=True)
        tensor.tiles()

        self.assertTrue(tensor.op.gpu)
        self.assertTrue(tensor.chunks[0].op.gpu)

        tensor1 = ones((10, 10, 8), chunk_size=(3, 3, 5))
        tensor1.tiles()

        tensor2 = ones((10, 10, 8), chunk_size=(3, 3, 5))
        tensor2.tiles()

        self.assertEqual(tensor1.chunks[0].op.key, tensor2.chunks[0].op.key)
        self.assertEqual(tensor1.chunks[0].key, tensor2.chunks[0].key)
        self.assertNotEqual(tensor1.chunks[0].op.key, tensor1.chunks[1].op.key)
        self.assertNotEqual(tensor1.chunks[0].key, tensor1.chunks[1].key)

        tensor = ones((2, 3, 4))
        self.assertEqual(len(list(tensor)), 2)

        tensor2 = ones((2, 3, 4), chunk_size=1)
        # tensor's op key must be equal to tensor2
        self.assertEqual(tensor.op.key, tensor2.op.key)
        self.assertNotEqual(tensor.key, tensor2.key)

        tensor3 = ones((2, 3, 3))
        self.assertNotEqual(tensor.op.key, tensor3.op.key)
        self.assertNotEqual(tensor.key, tensor3.key)

        # test create chunk op of ones manually
        chunk_op1 = TensorOnes(dtype=tensor.dtype)
        chunk1 = chunk_op1.new_chunk(None, (3, 3), index=(0, 0))
        chunk_op2 = TensorOnes(dtype=tensor.dtype)
        chunk2 = chunk_op2.new_chunk(None, (3, 4), index=(0, 1))
        self.assertNotEqual(chunk1.op.key, chunk2.op.key)
        self.assertNotEqual(chunk1.key, chunk2.key)

        tensor = ones((100, 100), chunk_size=50)
        tensor.tiles()
        self.assertEqual(len({c.op.key for c in tensor.chunks}), 1)
        self.assertEqual(len({c.key for c in tensor.chunks}), 1)
Esempio n. 10
0
    def testSwapaxes(self):
        arr = ones((10, 20, 30), chunk_size=[4, 3, 5])
        arr2 = arr.swapaxes(0, 1)
        arr2.tiles()

        self.assertEqual(arr2.shape, (20, 10, 30))
        self.assertEqual(calc_shape(arr2), arr2.shape)
        self.assertEqual(len(arr.chunks), len(arr2.chunks))
        self.assertEqual(calc_shape(arr2.chunks[-1]), arr2.chunks[-1].shape)
Esempio n. 11
0
    def testTensordot(self):
        from mars.tensor.expressions.linalg import tensordot, dot, inner

        t1 = ones((3, 4, 6), chunk_size=2)
        t2 = ones((4, 3, 5), chunk_size=2)
        t3 = tensordot(t1, t2, axes=((0, 1), (1, 0)))

        self.assertEqual(t3.shape, (6, 5))
        self.assertEqual(calc_shape(t3), t3.shape)

        t3.tiles()

        self.assertEqual(t3.shape, (6, 5))
        self.assertEqual(calc_shape(t3.chunks[0]), t3.chunks[0].shape)
        self.assertEqual(len(t3.chunks), 9)

        a = ones((10000, 20000), chunk_size=5000)
        b = ones((20000, 1000), chunk_size=5000)

        with self.assertRaises(ValueError):
            tensordot(a, b)

        a = ones(10, chunk_size=2)
        b = ones((10, 20), chunk_size=2)
        c = dot(a, b)
        self.assertEqual(c.shape, (20, ))
        self.assertEqual(calc_shape(c), c.shape)
        c.tiles()
        self.assertEqual(calc_shape(c.chunks[0]), c.chunks[0].shape)
        self.assertEqual(c.shape, tuple(sum(s) for s in c.nsplits))

        a = ones((10, 20), chunk_size=2)
        b = ones(20, chunk_size=2)
        c = dot(a, b)
        self.assertEqual(c.shape, (10, ))
        self.assertEqual(calc_shape(c), c.shape)
        c.tiles()
        self.assertEqual(calc_shape(c.chunks[0]), c.chunks[0].shape)
        self.assertEqual(c.shape, tuple(sum(s) for s in c.nsplits))

        v = ones((100, 100), chunk_size=10)
        tv = v.dot(v)
        self.assertEqual(tv.shape, (100, 100))
        self.assertEqual(calc_shape(tv), tv.shape)
        tv.tiles()
        self.assertEqual(calc_shape(tv.chunks[0]), tv.chunks[0].shape)
        self.assertEqual(tv.shape, tuple(sum(s) for s in tv.nsplits))

        a = ones((10, 20), chunk_size=2)
        b = ones((30, 20), chunk_size=2)
        c = inner(a, b)
        self.assertEqual(c.shape, (10, 30))
        self.assertEqual(calc_shape(c), c.shape)
        c.tiles()
        self.assertEqual(calc_shape(c.chunks[0]), c.chunks[0].shape)
        self.assertEqual(c.shape, tuple(sum(s) for s in c.nsplits))
Esempio n. 12
0
    def testNegative(self):
        t1 = tensor([[0, 1, 0], [1, 0, 0]], chunk_size=2).tosparse()

        t = negative(t1)
        self.assertTrue(t.issparse())
        self.assertIs(type(t), SparseTensor)
        self.assertEqual(calc_shape(t), t.shape)

        t.tiles()
        self.assertTrue(t.chunks[0].op.sparse)
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)
Esempio n. 13
0
    def testSlice(self):
        t = ones((100, 200, 300))
        t2 = t[10:30, 199:, -30:303]
        self.assertEqual(t2.shape, (20, 1, 30))
        self.assertEqual(calc_shape(t2), t2.shape)

        t3 = t[10:90:4, 20:80:5]
        s1 = len(list(range(100))[10:90:4])
        s2 = len(list(range(200))[20:80:5])
        self.assertEqual(t3.shape, (s1, s2, 300))
        self.assertEqual(calc_shape(t2), t2.shape)
Esempio n. 14
0
    def testNonzero(self):
        x = tensor([[1, 0, 0], [0, 2, 0], [1, 1, 0]], chunk_size=2)
        y = nonzero(x)

        self.assertEqual(len(y), 2)
        self.assertEqual(calc_shape(y[0]), y[0].shape)
        self.assertEqual(calc_shape(y[1]), y[1].shape)

        y[0].tiles()
        self.assertEqual(calc_shape(y[0].chunks[0]), y[0].chunks[0].shape)
        self.assertEqual(calc_shape(y[1].chunks[0]), y[1].chunks[0].shape)
Esempio n. 15
0
    def testFromDense(self):
        t = fromdense(tensor([[0, 0, 1], [1, 0, 0]], chunk_size=2))

        self.assertIsInstance(t, SparseTensor)
        self.assertIsInstance(t.op, DenseToSparse)
        self.assertTrue(t.issparse())
        self.assertEqual(calc_shape(t), t.shape)

        t.tiles()
        self.assertEqual(t.chunks[0].index, (0, 0))
        self.assertIsInstance(t.op, DenseToSparse)
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)
Esempio n. 16
0
    def testAround(self):
        t1 = ones((2, 3), dtype='f4', chunk_size=2)

        t = around(t1, decimals=3)

        self.assertEqual(t.op.decimals, 3)
        self.assertEqual(calc_shape(t), t.shape)

        t.tiles()

        self.assertEqual(t.chunks[0].op.decimals, 3)
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)
Esempio n. 17
0
    def testDigitize(self):
        x = tensor(np.array([0.2, 6.4, 3.0, 1.6]), chunk_size=2)
        bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
        inds = digitize(x, bins)

        self.assertEqual(inds.shape, (4, ))
        self.assertIsNotNone(inds.dtype)
        self.assertEqual(calc_shape(inds), inds.shape)

        inds.tiles()

        self.assertEqual(len(inds.chunks), 2)
        self.assertEqual(calc_shape(inds.chunks[0]), inds.chunks[0].shape)
Esempio n. 18
0
    def testVarReduction(self):
        var = lambda x, *args, **kwargs: x.var(*args, **kwargs).tiles()

        res1 = var(ones((10, 8), chunk_size=3), ddof=2)
        self.assertEqual(res1.shape, ())
        self.assertEqual(res1.op.ddof, 2)
        self.assertEqual(calc_shape(res1), res1.shape)
        self.assertEqual(calc_shape(res1.chunks[0]), res1.chunks[0].shape)

        res1 = var(ones((10, 8, 8), chunk_size=3), axis=1)
        self.assertEqual(res1.shape, (10, 8))
        self.assertEqual(calc_shape(res1), res1.shape)
        self.assertEqual(calc_shape(res1.chunks[0]), res1.chunks[0].shape)
Esempio n. 19
0
    def testUnravelIndex(self):
        indices = tensor([22, 41, 37], chunk_size=1)
        t = unravel_index(indices, (7, 6))

        self.assertEqual(len(t), 2)
        self.assertEqual(calc_shape(t[0]), t[0].shape)
        self.assertEqual(calc_shape(t[1]), t[1].shape)

        [r.tiles() for r in t]

        self.assertEqual(len(t[0].chunks), 3)
        self.assertEqual(len(t[1].chunks), 3)
        self.assertEqual(calc_shape(t[0].chunks[0]), t[0].chunks[0].shape)
        self.assertEqual(calc_shape(t[1].chunks[0]), t[1].chunks[0].shape)
Esempio n. 20
0
    def testChoose(self):
        with option_context() as options:
            options.tensor.chunk_size = 2

            choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23],
                       [30, 31, 32, 33]]
            a = choose([2, 3, 1, 0], choices)

            a.tiles()
            self.assertEqual(calc_shape(a), a.shape)
            self.assertEqual(calc_shape(a.chunks[0]), a.chunks[0].shape)
            self.assertEqual(len(a.chunks), 2)
            self.assertIsInstance(a.chunks[0].op, type(a.op))
            self.assertEqual(len(a.chunks[0].inputs), 5)
Esempio n. 21
0
    def testSplit(self):
        a = arange(9, chunk_size=2)

        splits = split(a, 3)
        self.assertEqual(len(splits), 3)
        self.assertTrue(all(s.shape == (3, ) for s in splits))
        self.assertTrue(
            all(calc_shape(s) == ((3, ), (3, ), (3, )) for s in splits))

        splits[0].tiles()
        self.assertEqual(splits[0].nsplits, ((2, 1), ))
        self.assertEqual(splits[1].nsplits, ((1, 2), ))
        self.assertEqual(splits[2].nsplits, ((2, 1), ))
        self.assertEqual(calc_shape(splits[0].chunks[0]),
                         splits[0].chunks[0].shape)
        self.assertEqual(calc_shape(splits[1].chunks[0]),
                         splits[1].chunks[0].shape)
        self.assertEqual(calc_shape(splits[2].chunks[0]),
                         splits[2].chunks[0].shape)

        a = arange(8, chunk_size=2)

        splits = split(a, [3, 5, 6, 10])
        self.assertEqual(len(splits), 5)
        self.assertEqual(splits[0].shape, (3, ))
        self.assertEqual(splits[1].shape, (2, ))
        self.assertEqual(splits[2].shape, (1, ))
        self.assertEqual(splits[3].shape, (2, ))
        self.assertEqual(splits[4].shape, (0, ))
        self.assertTrue(
            all(
                calc_shape(s) == ((3, ), (2, ), (1, ), (2, ), (0, ))
                for s in splits))

        splits[0].tiles()
        self.assertEqual(splits[0].nsplits, ((2, 1), ))
        self.assertEqual(splits[1].nsplits, ((1, 1), ))
        self.assertEqual(splits[2].nsplits, ((1, ), ))
        self.assertEqual(splits[3].nsplits, ((2, ), ))
        self.assertEqual(splits[4].nsplits, ((0, ), ))
        self.assertEqual(calc_shape(splits[0].chunks[0]),
                         splits[0].chunks[0].shape)
        self.assertEqual(calc_shape(splits[1].chunks[0]),
                         splits[1].chunks[0].shape)
        self.assertEqual(calc_shape(splits[2].chunks[0]),
                         splits[2].chunks[0].shape)
        self.assertEqual(calc_shape(splits[3].chunks[0]),
                         splits[3].chunks[0].shape)
        self.assertEqual(calc_shape(splits[4].chunks[0]),
                         splits[4].chunks[0].shape)
Esempio n. 22
0
    def testOnesLike(self):
        t1 = tensor([[0, 0, 1], [1, 0, 0]], chunk_size=2).tosparse()
        t = ones_like(t1, dtype='f8')

        self.assertIsInstance(t, SparseTensor)
        self.assertIsInstance(t.op, TensorOnesLike)
        self.assertTrue(t.issparse())
        self.assertEqual(calc_shape(t), t.shape)

        t.tiles()
        self.assertEqual(t.chunks[0].index, (0, 0))
        self.assertIsInstance(t.op, TensorOnesLike)
        self.assertTrue(t.chunks[0].issparse())
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)
Esempio n. 23
0
    def testAstype(self):
        arr = ones((10, 20, 30), chunk_size=3)

        arr2 = arr.astype(np.int32)
        arr2.tiles()

        self.assertEqual(arr2.shape, (10, 20, 30))
        self.assertEqual(calc_shape(arr2), arr2.shape)
        self.assertTrue(np.issubdtype(arr2.dtype, np.int32))
        self.assertEqual(arr2.op.casting, 'unsafe')
        self.assertEqual(calc_shape(arr2.chunks[0]), arr2.chunks[0].shape)

        with self.assertRaises(TypeError):
            arr.astype(np.int32, casting='safe')
Esempio n. 24
0
    def testArgwhere(self):
        cond = tensor([[True, False], [False, True]], chunk_size=1)
        indices = argwhere(cond)

        self.assertTrue(np.isnan(indices.shape[0]))
        self.assertEqual(indices.shape[1], 2)
        self.assertEqual(calc_shape(indices), indices.shape)

        indices.tiles()

        self.assertEqual(indices.nsplits[1], (1, 1))

        chunk = indices.chunks[0]
        self.assertTrue(np.isnan(calc_shape(chunk)[0]))
        self.assertEqual(calc_shape(chunk)[1], chunk.shape[1])
Esempio n. 25
0
    def testMultivariateNormal(self):
        mean = [0, 0]
        cov = [[1, 0], [0, 100]]

        t = multivariate_normal(mean, cov, 5000, chunk_size=500)
        self.assertEqual(t.shape, (5000, 2))
        self.assertEqual(t.op.size, (5000, ))
        self.assertEqual(calc_shape(t), t.shape)

        t.tiles()
        self.assertEqual(t.nsplits, ((500, ) * 10, (2, )))
        self.assertEqual(len(t.chunks), 10)
        c = t.chunks[0]
        self.assertEqual(c.shape, (500, 2))
        self.assertEqual(c.op.size, (500, ))
        self.assertEqual(calc_shape(c), c.shape)
Esempio n. 26
0
    def testFFTFreq(self):
        t = fft.fftfreq(10, .1, chunk_size=3)

        self.assertEqual(t.shape, np.fft.fftfreq(10, .1).shape)
        self.assertEqual(calc_shape(t), t.shape)
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(ns) for ns in t.nsplits))
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)

        t = fft.rfftfreq(10, .1, chunk_size=3)

        self.assertEqual(t.shape, np.fft.rfftfreq(10, .1).shape)
        self.assertEqual(calc_shape(t), t.shape)
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(ns) for ns in t.nsplits))
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)
Esempio n. 27
0
    def testCos(self):
        t1 = tensor([[0, 1, 0], [1, 0, 0]], chunk_size=2).tosparse()

        t = cos(t1)
        self.assertFalse(t.issparse())
        self.assertIs(type(t), Tensor)
        self.assertEqual(calc_shape(t), t.shape)
Esempio n. 28
0
    def testRandint(self):
        arr = randint(1,
                      2,
                      size=(10, 9),
                      dtype='f8',
                      density=.01,
                      chunk_size=2).tiles()

        self.assertEqual(arr.shape, (10, 9))
        self.assertEqual(calc_shape(arr), arr.shape)
        self.assertEqual(len(arr.chunks), 25)
        self.assertEqual(arr.chunks[0].shape, (2, 2))
        self.assertEqual(arr.chunks[0].op.dtype, np.float64)
        self.assertEqual(arr.chunks[0].op.low, 1)
        self.assertEqual(arr.chunks[0].op.high, 2)
        self.assertEqual(arr.chunks[0].op.density, .01)
        self.assertEqual(calc_shape(arr.chunks[0]), arr.chunks[0].shape)
Esempio n. 29
0
    def testRepeat(self):
        a = arange(10, chunk_size=2).reshape(2, 5)

        t = repeat(a, 3)
        self.assertEqual(t.shape, (30, ))
        self.assertEqual(calc_shape(t), t.shape)

        t = repeat(a, 3, axis=0)
        self.assertEqual(t.shape, (6, 5))
        self.assertEqual(calc_shape(t), t.shape)

        t = repeat(a, 3, axis=1)
        self.assertEqual(t.shape, (2, 15))
        self.assertEqual(calc_shape(t), t.shape)

        t = repeat(a, [3], axis=1)
        self.assertEqual(t.shape, (2, 15))
        self.assertEqual(calc_shape(t), t.shape)

        t = repeat(a, [3, 4], axis=0)
        self.assertEqual(t.shape, (7, 5))
        self.assertEqual(calc_shape(t), t.shape)

        with self.assertRaises(ValueError):
            repeat(a, [3, 4], axis=1)

        a = tensor(np.random.randn(10), chunk_size=5)

        t = repeat(a, 3)
        t.tiles()
        self.assertEqual(sum(t.nsplits[0]), 30)
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)

        a = tensor(np.random.randn(100), chunk_size=10)

        t = repeat(a, 3)
        t.tiles()
        self.assertEqual(sum(t.nsplits[0]), 300)
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)

        a = tensor(np.random.randn(4))
        b = tensor((4, ))

        t = repeat(a, b)
        self.assertEqual(calc_shape(t), t.shape)
        self.assertEqual(t.rough_shape, (4, ))

        t.tiles()
        self.assertTrue(np.isnan(t.nsplits[0]))
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)
        self.assertEqual(t.chunks[0].rough_shape, (4, ))
Esempio n. 30
0
    def testIndicesIndexingTiles(self):
        t = ones((10, 20, 30), chunk_size=(2, 20, 30))
        t2 = t[3]
        t2.tiles()

        self.assertEqual(len(t2.chunks), 1)
        self.assertIs(t2.chunks[0].inputs[0], t.cix[1, 0, 0].data)
        self.assertEqual(t2.chunks[0].op.indexes[0], 1)
        self.assertEqual(calc_shape(t2.chunks[0]), t2.chunks[0].shape)

        t3 = t[4]
        t3.tiles()

        self.assertEqual(len(t3.chunks), 1)
        self.assertIs(t3.chunks[0].inputs[0], t.cix[2, 0, 0].data)
        self.assertEqual(t3.chunks[0].op.indexes[0], 0)
        self.assertEqual(calc_shape(t3.chunks[0]), t3.chunks[0].shape)