Exemple #1
0
    def testAverageExecution(self):
        data = arange(1, 5, chunk_size=1)
        t = average(data)

        res = self.executor.execute_tensor(t)[0]
        expected = np.average(np.arange(1, 5))
        self.assertEqual(res, expected)

        t = average(arange(1, 11, chunk_size=2),
                    weights=arange(10, 0, -1, chunk_size=2))

        res = self.executor.execute_tensor(t)[0]
        expected = np.average(range(1, 11), weights=range(10, 0, -1))
        self.assertEqual(res, expected)

        data = arange(6, chunk_size=2).reshape((3, 2))
        t = average(data,
                    axis=1,
                    weights=tensor([1. / 4, 3. / 4], chunk_size=2))

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.average(np.arange(6).reshape(3, 2),
                              axis=1,
                              weights=(1. / 4, 3. / 4))
        np.testing.assert_equal(res, expected)

        with self.assertRaises(TypeError):
            average(data, weights=tensor([1. / 4, 3. / 4], chunk_size=2))
Exemple #2
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))

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

        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, ))

        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, ), ))
    def testArangeExecution(self):
        t = arange(1, 20, 3, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(np.array_equal(res, np.arange(1, 20, 3)))

        t = arange(1, 20, .3, chunk_size=4)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.arange(1, 20, .3)
        self.assertTrue(np.allclose(res, expected))

        t = arange(1.0, 1.8, .3, chunk_size=4)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.arange(1.0, 1.8, .3)
        self.assertTrue(np.allclose(res, expected))

        t = arange('1066-10-13',
                   '1066-10-31',
                   dtype=np.datetime64,
                   chunk_size=3)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.arange('1066-10-13', '1066-10-31', dtype=np.datetime64)
        self.assertTrue(np.array_equal(res, expected))
Exemple #4
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_)

        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)

        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_)

        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)
Exemple #5
0
    def testSplitExecution(self):
        x = arange(48, chunk_size=3).reshape(2, 3, 8)
        ss = split(x, 4, axis=2)

        res = [self.executor.execute_tensor(i, concat=True)[0] for i in ss]
        expected = np.split(np.arange(48).reshape(2, 3, 8), 4, axis=2)
        self.assertEqual(len(res), len(expected))
        [np.testing.assert_equal(r, e) for r, e in zip(res, expected)]

        ss = split(x, [3, 5, 6, 10], axis=2)

        res = [self.executor.execute_tensor(i, concat=True)[0] for i in ss]
        expected = np.split(np.arange(48).reshape(2, 3, 8), [3, 5, 6, 10],
                            axis=2)
        self.assertEqual(len(res), len(expected))
        [np.testing.assert_equal(r, e) for r, e in zip(res, expected)]

        # hsplit
        x = arange(120, chunk_size=3).reshape(2, 12, 5)
        ss = hsplit(x, 4)

        res = [self.executor.execute_tensor(i, concat=True)[0] for i in ss]
        expected = np.hsplit(np.arange(120).reshape(2, 12, 5), 4)
        self.assertEqual(len(res), len(expected))
        [np.testing.assert_equal(r, e) for r, e in zip(res, expected)]

        # vsplit
        x = arange(48, chunk_size=3).reshape(8, 3, 2)
        ss = vsplit(x, 4)

        res = [self.executor.execute_tensor(i, concat=True)[0] for i in ss]
        expected = np.vsplit(np.arange(48).reshape(8, 3, 2), 4)
        self.assertEqual(len(res), len(expected))
        [np.testing.assert_equal(r, e) for r, e in zip(res, expected)]

        # dsplit
        x = arange(48, chunk_size=3).reshape(2, 3, 8)
        ss = dsplit(x, 4)

        res = [self.executor.execute_tensor(i, concat=True)[0] for i in ss]
        expected = np.dsplit(np.arange(48).reshape(2, 3, 8), 4)
        self.assertEqual(len(res), len(expected))
        [np.testing.assert_equal(r, e) for r, e in zip(res, expected)]

        x_data = sps.random(12, 8, density=.1)
        x = tensor(x_data, chunk_size=3)
        ss = split(x, 4, axis=0)

        res = [self.executor.execute_tensor(i, concat=True)[0] for i in ss]
        expected = np.split(x_data.toarray(), 4, axis=0)
        self.assertEqual(len(res), len(expected))
        [
            np.testing.assert_equal(r.toarray(), e)
            for r, e in zip(res, expected)
        ]
Exemple #6
0
    def testMatmulExecution(self):
        data_a = np.random.randn(10, 20)
        data_b = np.random.randn(20)

        a = tensor(data_a, chunk_size=2)
        b = tensor(data_b, chunk_size=3)
        c = matmul(a, b)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(data_a, data_b)
        np.testing.assert_allclose(res, expected)

        data_a = np.random.randn(10, 20)
        data_b = np.random.randn(10)

        a = tensor(data_a, chunk_size=2)
        b = tensor(data_b, chunk_size=3)
        c = matmul(b, a)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(data_b, data_a)
        np.testing.assert_allclose(res, expected)

        data_a = np.random.randn(15, 1, 20, 30)
        data_b = np.random.randn(1, 11, 30, 20)

        a = tensor(data_a, chunk_size=12)
        b = tensor(data_b, chunk_size=13)
        c = matmul(a, b)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(data_a, data_b)
        np.testing.assert_allclose(res, expected, atol=.0001)

        a = arange(2 * 2 * 4, chunk_size=1).reshape((2, 2, 4))
        b = arange(2 * 2 * 4, chunk_size=1).reshape((2, 4, 2))
        c = matmul(a, b)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(
            np.arange(2 * 2 * 4).reshape(2, 2, 4),
            np.arange(2 * 2 * 4).reshape(2, 4, 2))
        np.testing.assert_allclose(res, expected, atol=.0001)

        data_a = sps.random(10, 20)
        data_b = sps.random(20, 5)

        a = tensor(data_a, chunk_size=2)
        b = tensor(data_b, chunk_size=3)
        c = matmul(a, b)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(data_a.toarray(), data_b.toarray())
        np.testing.assert_allclose(res.toarray(), expected)
Exemple #7
0
    def testFlipExecution(self):
        a = arange(8, chunk_size=2).reshape((2, 2, 2))

        t = flip(a, 0)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.flip(np.arange(8).reshape(2, 2, 2), 0)
        np.testing.assert_equal(res, expected)

        t = flip(a, 1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.flip(np.arange(8).reshape(2, 2, 2), 1)
        np.testing.assert_equal(res, expected)

        t = flipud(a)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.flipud(np.arange(8).reshape(2, 2, 2))
        np.testing.assert_equal(res, expected)

        t = fliplr(a)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.fliplr(np.arange(8).reshape(2, 2, 2))
        np.testing.assert_equal(res, expected)
Exemple #8
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.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]))

        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)
Exemple #9
0
    def testIsInExecution(self):
        element = 2 * arange(4, chunk_size=1).reshape((2, 2))
        test_elements = [1, 2, 4, 8]

        mask = isin(element, test_elements)

        res = self.executor.execute_tensor(mask, concat=True)[0]
        expected = np.isin(2 * np.arange(4).reshape((2, 2)), test_elements)
        np.testing.assert_equal(res, expected)

        res = self.executor.execute_tensor(element[mask], concat=True)[0]
        expected = np.array([2, 4])
        np.testing.assert_equal(res, expected)

        mask = isin(element, test_elements, invert=True)

        res = self.executor.execute_tensor(mask, concat=True)[0]
        expected = np.isin(2 * np.arange(4).reshape((2, 2)),
                           test_elements,
                           invert=True)
        np.testing.assert_equal(res, expected)

        res = self.executor.execute_tensor(element[mask], concat=True)[0]
        expected = np.array([0, 6])
        np.testing.assert_equal(res, expected)

        test_set = {1, 2, 4, 8}
        mask = isin(element, test_set)

        res = self.executor.execute_tensor(mask, concat=True)[0]
        expected = np.isin(2 * np.arange(4).reshape((2, 2)), test_set)
        np.testing.assert_equal(res, expected)
Exemple #10
0
    def testRollExecution(self):
        x = arange(10, chunk_size=2)

        t = roll(x, 2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.roll(np.arange(10), 2)
        np.testing.assert_equal(res, expected)

        x2 = x.reshape(2, 5)

        t = roll(x2, 1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.roll(np.arange(10).reshape(2, 5), 1)
        np.testing.assert_equal(res, expected)

        t = roll(x2, 1, axis=0)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.roll(np.arange(10).reshape(2, 5), 1, axis=0)
        np.testing.assert_equal(res, expected)

        t = roll(x2, 1, axis=1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.roll(np.arange(10).reshape(2, 5), 1, axis=1)
        np.testing.assert_equal(res, expected)
Exemple #11
0
    def testRepeat(self):
        a = arange(10, chunks=2).reshape(2, 5)

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

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

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

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

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

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

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

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

        t = repeat(a, 3)
        t.tiles()
        self.assertEqual(sum(t.nsplits[0]), 300)
Exemple #12
0
    def testArgwhereExecution(self):
        x = arange(6, chunk_size=2).reshape(2, 3)
        t = argwhere(x > 1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.argwhere(np.arange(6).reshape(2, 3) > 1)

        self.assertTrue(np.array_equal(res, expected))
Exemple #13
0
    def testFlatnonzeroExecution(self):
        x = arange(-2, 3, chunk_size=2)

        t = flatnonzero(x)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.flatnonzero(np.arange(-2, 3))

        np.testing.assert_equal(res, expected)
Exemple #14
0
    def testArraySplit(self):
        a = arange(8, chunk_size=2)

        splits = array_split(a, 3)
        self.assertEqual(len(splits), 3)
        self.assertEqual([s.shape[0] for s in splits], [3, 3, 2])

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

        a = arange(7, chunk_size=2)

        splits = array_split(a, 3)
        self.assertEqual(len(splits), 3)
        self.assertEqual([s.shape[0] for s in splits], [3, 2, 2])

        splits[0].tiles()
        self.assertEqual(splits[0].nsplits, ((2, 1), ))
        self.assertEqual(splits[1].nsplits, ((1, 1), ))
        self.assertEqual(splits[2].nsplits, ((1, 1), ))
Exemple #15
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, ))
Exemple #16
0
    def testArraySplit(self):
        a = arange(8, chunk_size=2)

        splits = array_split(a, 3)
        self.assertEqual(len(splits), 3)
        self.assertEqual([s.shape[0] for s in splits], [3, 3, 2])
        self.assertTrue(
            all(calc_shape(s) == ((3, ), (3, ), (2, )) 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, ), ))
        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(7, chunk_size=2)

        splits = array_split(a, 3)
        self.assertEqual(len(splits), 3)
        self.assertEqual([s.shape[0] for s in splits], [3, 2, 2])
        self.assertTrue(
            all(calc_shape(s) == ((3, ), (2, ), (2, )) 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, 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)
Exemple #17
0
    def testArraySplitExecution(self):
        x = arange(48, chunks=3).reshape(2, 3, 8)
        ss = array_split(x, 3, axis=2)

        res = [self.executor.execute_tensor(i, concat=True)[0] for i in ss]
        expected = np.array_split(np.arange(48).reshape(2, 3, 8), 3, axis=2)
        self.assertEqual(len(res), len(expected))
        [np.testing.assert_equal(r, e) for r, e in zip(res, expected)]

        ss = array_split(x, [3, 5, 6, 10], axis=2)

        res = [self.executor.execute_tensor(i, concat=True)[0] for i in ss]
        expected = np.array_split(np.arange(48).reshape(2, 3, 8), [3, 5, 6, 10], axis=2)
        self.assertEqual(len(res), len(expected))
        [np.testing.assert_equal(r, e) for r, e in zip(res, expected)]
Exemple #18
0
    def testRepeatExecution(self):
        a = repeat(3, 4)

        res = self.executor.execute_tensor(a)[0]
        expected = np.repeat(3, 4)
        np.testing.assert_equal(res, expected)

        x_data = np.random.randn(20, 30)
        x = tensor(x_data, chunk_size=(3, 4))

        t = repeat(x, 2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.repeat(x_data, 2)
        np.testing.assert_equal(res, expected)

        t = repeat(x, 3, axis=1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.repeat(x_data, 3, axis=1)
        np.testing.assert_equal(res, expected)

        t = repeat(x, np.arange(20), axis=0)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.repeat(x_data, np.arange(20), axis=0)
        np.testing.assert_equal(res, expected)

        t = repeat(x, arange(20, chunk_size=5), axis=0)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.repeat(x_data, np.arange(20), axis=0)
        np.testing.assert_equal(res, expected)

        x_data = sps.random(20, 30, density=.1)
        x = tensor(x_data, chunk_size=(3, 4))

        t = repeat(x, 2, axis=1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.repeat(x_data.toarray(), 2, axis=1)
        np.testing.assert_equal(res.toarray(), expected)
Exemple #19
0
    def testPtpExecution(self):
        x = arange(4, chunk_size=1).reshape(2, 2)

        t = ptp(x, axis=0)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.ptp(np.arange(4).reshape(2, 2), axis=0)
        np.testing.assert_equal(res, expected)

        t = ptp(x, axis=1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.ptp(np.arange(4).reshape(2, 2), axis=1)
        np.testing.assert_equal(res, expected)

        t = ptp(x)

        res = self.executor.execute_tensor(t)[0]
        expected = np.ptp(np.arange(4).reshape(2, 2))
        np.testing.assert_equal(res, expected)
    def testTrilExecution(self):
        a = arange(24, chunk_size=2).reshape(2, 3, 4)

        t = tril(a)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(24).reshape(2, 3, 4))
        np.testing.assert_equal(res, expected)

        t = tril(a, k=1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(24).reshape(2, 3, 4), k=1)
        np.testing.assert_equal(res, expected)

        t = tril(a, k=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(24).reshape(2, 3, 4), k=2)
        np.testing.assert_equal(res, expected)

        t = tril(a, k=-1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(24).reshape(2, 3, 4), k=-1)
        np.testing.assert_equal(res, expected)

        t = tril(a, k=-2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(24).reshape(2, 3, 4), k=-2)
        np.testing.assert_equal(res, expected)

        a = arange(12, chunk_size=2).reshape(3, 4).tosparse()

        t = tril(a)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(12).reshape(3, 4))
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res, expected)

        t = tril(a, k=1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(12).reshape(3, 4), k=1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res, expected)

        t = tril(a, k=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(12).reshape(3, 4), k=2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res, expected)

        t = tril(a, k=-1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(12).reshape(3, 4), k=-1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res, expected)

        t = tril(a, k=-2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.tril(np.arange(12).reshape(3, 4), k=-2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res, expected)
    def testMeshgridExecution(self):
        a = arange(5, chunk_size=2)
        b = arange(6, 12, chunk_size=3)
        c = arange(12, 19, chunk_size=4)

        A, B, C = meshgrid(a, b, c)

        A_res = self.executor.execute_tensor(A, concat=True)[0]
        A_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19))[0]
        np.testing.assert_equal(A_res, A_expected)

        B_res = self.executor.execute_tensor(B, concat=True)[0]
        B_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19))[1]
        np.testing.assert_equal(B_res, B_expected)

        C_res = self.executor.execute_tensor(C, concat=True)[0]
        C_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19))[2]
        np.testing.assert_equal(C_res, C_expected)

        A, B, C = meshgrid(a, b, c, indexing='ij')

        A_res = self.executor.execute_tensor(A, concat=True)[0]
        A_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19), indexing='ij')[0]
        np.testing.assert_equal(A_res, A_expected)

        B_res = self.executor.execute_tensor(B, concat=True)[0]
        B_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19), indexing='ij')[1]
        np.testing.assert_equal(B_res, B_expected)

        C_res = self.executor.execute_tensor(C, concat=True)[0]
        C_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19), indexing='ij')[2]
        np.testing.assert_equal(C_res, C_expected)

        A, B, C = meshgrid(a, b, c, sparse=True)

        A_res = self.executor.execute_tensor(A, concat=True)[0]
        A_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19), sparse=True)[0]
        np.testing.assert_equal(A_res, A_expected)

        B_res = self.executor.execute_tensor(B, concat=True)[0]
        B_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19), sparse=True)[1]
        np.testing.assert_equal(B_res, B_expected)

        C_res = self.executor.execute_tensor(C, concat=True)[0]
        C_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19), sparse=True)[2]
        np.testing.assert_equal(C_res, C_expected)

        A, B, C = meshgrid(a, b, c, indexing='ij', sparse=True)

        A_res = self.executor.execute_tensor(A, concat=True)[0]
        A_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19),
                                 indexing='ij', sparse=True)[0]
        np.testing.assert_equal(A_res, A_expected)

        B_res = self.executor.execute_tensor(B, concat=True)[0]
        B_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19),
                                 indexing='ij', sparse=True)[1]
        np.testing.assert_equal(B_res, B_expected)

        C_res = self.executor.execute_tensor(C, concat=True)[0]
        C_expected = np.meshgrid(np.arange(5), np.arange(6, 12), np.arange(12, 19),
                                 indexing='ij', sparse=True)[2]
        np.testing.assert_equal(C_res, C_expected)
    def testDiagExecution(self):
        # 2-d  6 * 6
        a = arange(36, chunk_size=2).reshape(6, 6)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-5)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=-5)
        np.testing.assert_equal(res, expected)

        # 2-d  6 * 6 sparse, no tensor
        a = sps.rand(6, 6, density=.1)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray())
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=1)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=3)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=-2)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-5)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(a.toarray(), k=-5)
        np.testing.assert_equal(res.toarray(), expected)

        # 2-d  6 * 6 sparse, from tensor
        raw_a = sps.rand(6, 6, density=.1)
        a = tensor(raw_a, chunk_size=2)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray())
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=1)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=3)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=-2)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-5)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(raw_a.toarray(), k=-5)
        np.testing.assert_equal(res.toarray(), expected)

        # 2-d  4 * 9
        a = arange(36, chunk_size=2).reshape(4, 9)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=-3)
        np.testing.assert_equal(res, expected)

        # 2-d  4 * 9 sparse, no tensor
        a = sps.rand(4, 9, density=.1)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray())
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=1)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=3)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=-2)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(a.toarray(), k=-3)
        np.testing.assert_equal(res.toarray(), expected)

        # 2-d  4 * 9 sparse, from tensor
        raw_a = sps.rand(4, 9, density=.1)
        a = tensor(raw_a, chunk_size=2)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray())
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=1)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=3)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=-2)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(raw_a.toarray(), k=-3)
        np.testing.assert_equal(res.toarray(), expected)

        # 1-d
        a = arange(5, chunk_size=2)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-3)
        np.testing.assert_equal(res, expected)

        d = diag(a, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5))
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=2, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-3, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-3)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)
Exemple #23
0
    def testFancyIndexingTensorExecution(self):
        # test fancy index of type tensor

        raw = np.random.random((11, 8, 12, 14))
        arr = tensor(raw, chunk_size=(2, 3, 2, 3))

        raw_index = [8, 10, 3, 1, 9, 10]
        index = tensor(raw_index, chunk_size=4)
        arr2 = arr[index]

        res = self.executor.execute_tensor(arr2, concat=True)
        np.testing.assert_array_equal(res[0], raw[raw_index])

        raw_index = np.random.permutation(8)
        index = tensor(raw_index, chunk_size=3)
        arr3 = arr[:2, ..., index]

        res = self.executor.execute_tensor(arr3, concat=True)
        np.testing.assert_array_equal(res[0], raw[:2, ..., raw_index])

        raw_index = [1, 3, 9, 10]
        index = tensor(raw_index)
        arr4 = arr[..., index, :5]

        res = self.executor.execute_tensor(arr4, concat=True)
        np.testing.assert_array_equal(res[0], raw[..., raw_index, :5])

        raw_index1 = [8, 10, 3, 1, 9, 10]
        raw_index2 = [1, 3, 9, 10, 2, 7]
        index1 = tensor(raw_index1, chunk_size=4)
        index2 = tensor(raw_index2, chunk_size=3)
        arr5 = arr[index1, :, index2]

        res = self.executor.execute_tensor(arr5, concat=True)
        np.testing.assert_array_equal(res[0], raw[raw_index1, :, raw_index2])

        raw_index1 = [1, 3, 5, 7, 9, 10]
        raw_index2 = [1, 9, 9, 10, 2, 7]
        index1 = tensor(raw_index1, chunk_size=3)
        index2 = tensor(raw_index2, chunk_size=4)
        arr6 = arr[index1, :, index2]

        res = self.executor.execute_tensor(arr6, concat=True)
        np.testing.assert_array_equal(res[0], raw[raw_index1, :, raw_index2])

        raw_index1 = [[8, 10, 3], [1, 9, 10]]
        raw_index2 = [[1, 3, 9], [10, 2, 7]]
        index1 = tensor(raw_index1)
        index2 = tensor(raw_index2, chunk_size=2)
        arr7 = arr[index1, :, index2]

        res = self.executor.execute_tensor(arr7, concat=True)
        np.testing.assert_array_equal(res[0], raw[raw_index1, :, raw_index2])

        raw_index1 = [[1, 3], [3, 7], [7, 7]]
        raw_index2 = [1, 9]
        index1 = tensor(raw_index1, chunk_size=(2, 1))
        index2 = tensor(raw_index2)
        arr8 = arr[0, index1, :, index2]

        res = self.executor.execute_tensor(arr8, concat=True)
        np.testing.assert_array_equal(res[0], raw[0, raw_index1, :,
                                                  raw_index2])

        raw_a = np.random.rand(30, 30)
        a = tensor(raw_a, chunk_size=(13, 17))
        b = a.argmax(axis=0)
        c = a[b, arange(30)]
        res = self.executor.execute_tensor(c, concat=True)

        np.testing.assert_array_equal(
            res[0], raw_a[raw_a.argmax(axis=0),
                          np.arange(30)])
Exemple #24
0
    def testDiagExecution(self):
        # 2-d  6 * 6
        a = arange(36, chunks=2).reshape(6, 6)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-5)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=-5)
        np.testing.assert_equal(res, expected)

        # 2-d  4 * 9
        a = arange(36, chunks=2).reshape(4, 9)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=-3)
        np.testing.assert_equal(res, expected)

        # 1-d
        a = arange(5, chunks=2)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-3)
        np.testing.assert_equal(res, expected)

        d = diag(a, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5))
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=2, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-3, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-3)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)