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 = mask.tiles() element = get_tiled(element) 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 = mask.tiles() element = get_tiled(element) 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)
def test_isin(): element = 2 * arange(4, chunk_size=1).reshape(2, 2) test_elements = [1, 2, 4, 8] mask = isin(element, test_elements) assert mask.shape == (2, 2) assert mask.dtype == np.bool_ mask, element = tile(mask, element) assert len(mask.chunks) == len(element.chunks) assert len(mask.op.test_elements.chunks) == 1 assert mask.chunks[0].inputs[0] is 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) assert mask.shape == (2, 2) assert mask.dtype == np.bool_ mask, element = tile(mask, element) assert len(mask.chunks) == len(element.chunks) assert len(mask.op.test_elements.chunks) == 1 assert mask.chunks[0].inputs[0] is element.chunks[0].data assert mask.chunks[0].op.invert is True
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))
def test_average_execution(setup): data = arange(1, 5, chunk_size=1) t = average(data) res = t.execute().fetch() expected = np.average(np.arange(1, 5)) assert res == expected t = average(arange(1, 11, chunk_size=2), weights=arange(10, 0, -1, chunk_size=2)) res = t.execute().fetch() expected = np.average(range(1, 11), weights=range(10, 0, -1)) assert 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 = t.execute().fetch() expected = np.average(np.arange(6).reshape(3, 2), axis=1, weights=(1. / 4, 3. / 4)) np.testing.assert_equal(res, expected) with pytest.raises(TypeError): average(data, weights=tensor([1. / 4, 3. / 4], chunk_size=2))
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))
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) ]
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)
def test_where(): 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 = tile(arr) assert len(arr.chunks) == 4 np.testing.assert_equal(arr.chunks[0].inputs[0].op.data, [[True]]) np.testing.assert_equal(arr.chunks[0].inputs[1].op.data, [1]) np.testing.assert_equal(arr.chunks[0].inputs[2].op.data, [3]) np.testing.assert_equal(arr.chunks[1].inputs[0].op.data, [[False]]) np.testing.assert_equal(arr.chunks[1].inputs[1].op.data, [2]) np.testing.assert_equal(arr.chunks[1].inputs[2].op.data, [4]) np.testing.assert_equal(arr.chunks[2].inputs[0].op.data, [[False]]) np.testing.assert_equal(arr.chunks[2].inputs[1].op.data, [1]) np.testing.assert_equal(arr.chunks[2].inputs[2].op.data, [3]) np.testing.assert_equal(arr.chunks[3].inputs[0].op.data, [[True]]) np.testing.assert_equal(arr.chunks[3].inputs[1].op.data, [2]) np.testing.assert_equal(arr.chunks[3].inputs[2].op.data, [4]) with pytest.raises(ValueError): where(cond, x) x = arange(9.).reshape(3, 3) y = where(x < 5, x, -1) assert y.dtype == np.float64
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 = 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)
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)
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)
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)
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))
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)
def test_flatnonzero_execution(setup): x = arange(-2, 3, chunk_size=2) t = flatnonzero(x) res = t.execute().fetch() expected = np.flatnonzero(np.arange(-2, 3)) np.testing.assert_equal(res, expected)
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() splits = [get_tiled(s) for s in splits] 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() splits = [get_tiled(s) for s in splits] 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, ), )) a = tensor(np.asfortranarray(np.random.rand(9, 10)), chunk_size=4) splits = split(a, 3) self.assertTrue(splits[0].flags['F_CONTIGUOUS']) self.assertFalse(splits[0].flags['C_CONTIGUOUS']) self.assertTrue(splits[1].flags['F_CONTIGUOUS']) self.assertFalse(splits[0].flags['C_CONTIGUOUS']) self.assertTrue(splits[2].flags['F_CONTIGUOUS']) self.assertFalse(splits[0].flags['C_CONTIGUOUS']) for a in ((1, 1, 1, 2, 2, 3), [1, 1, 1, 2, 2, 3]): splits = split(a, (3, 5)) self.assertEqual(len(splits), 3)
def test_split(): a = arange(9, chunk_size=2) splits = split(a, 3) assert len(splits) == 3 assert all(s.shape == (3, ) for s in splits) is True splits = tile(*splits) assert splits[0].nsplits == ((2, 1), ) assert splits[1].nsplits == ((1, 2), ) assert splits[2].nsplits == ((2, 1), ) a = arange(8, chunk_size=2) splits = split(a, [3, 5, 6, 10]) assert len(splits) == 5 assert splits[0].shape == (3, ) assert splits[1].shape == (2, ) assert splits[2].shape == (1, ) assert splits[3].shape == (2, ) assert splits[4].shape == (0, ) splits = tile(*splits) assert splits[0].nsplits == ((2, 1), ) assert splits[1].nsplits == ((1, 1), ) assert splits[2].nsplits == ((1, ), ) assert splits[3].nsplits == ((2, ), ) assert splits[4].nsplits == ((0, ), ) a = tensor(np.asfortranarray(np.random.rand(9, 10)), chunk_size=4) splits = split(a, 3) assert splits[0].flags['F_CONTIGUOUS'] is True assert splits[0].flags['C_CONTIGUOUS'] is False assert splits[1].flags['F_CONTIGUOUS'] is True assert splits[0].flags['C_CONTIGUOUS'] is False assert splits[2].flags['F_CONTIGUOUS'] is True assert splits[0].flags['C_CONTIGUOUS'] is False for a in ((1, 1, 1, 2, 2, 3), [1, 1, 1, 2, 2, 3]): splits = split(a, (3, 5)) assert len(splits) == 3
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), ))
def test_array_split(): a = arange(8, chunk_size=2) splits = array_split(a, 3) assert len(splits) == 3 assert [s.shape[0] for s in splits] == [3, 3, 2] splits = tile(*splits) assert splits[0].nsplits == ((2, 1), ) assert splits[1].nsplits == ((1, 2), ) assert splits[2].nsplits == ((2, ), ) a = arange(7, chunk_size=2) splits = array_split(a, 3) assert len(splits) == 3 assert [s.shape[0] for s in splits] == [3, 2, 2] splits = tile(*splits) assert splits[0].nsplits == ((2, 1), ) assert splits[1].nsplits == ((1, 1), ) assert splits[2].nsplits == ((1, 1), )
def test_arange_execution(setup): t = arange(1, 20, 3, chunk_size=2) res = t.execute().fetch() assert np.array_equal(res, np.arange(1, 20, 3)) is True t = arange(1, 20, .3, chunk_size=4) res = t.execute().fetch() expected = np.arange(1, 20, .3) assert np.allclose(res, expected) is True t = arange(1.0, 1.8, .3, chunk_size=4) res = t.execute().fetch() expected = np.arange(1.0, 1.8, .3) assert np.allclose(res, expected) is True t = arange('1066-10-13', '1066-10-31', dtype=np.datetime64, chunk_size=3) res = t.execute().fetch() expected = np.arange('1066-10-13', '1066-10-31', dtype=np.datetime64) assert np.array_equal(res, expected) is True
def testArraySplitExecution(self): x = arange(48, chunk_size=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)]
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)
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 test_ptp_execution(setup): x = arange(4, chunk_size=1).reshape(2, 2) t = ptp(x, axis=0) res = t.execute().fetch() expected = np.ptp(np.arange(4).reshape(2, 2), axis=0) np.testing.assert_equal(res, expected) t = ptp(x, axis=1) res = t.execute().fetch() expected = np.ptp(np.arange(4).reshape(2, 2), axis=1) np.testing.assert_equal(res, expected) t = ptp(x) res = t.execute().fetch() expected = np.ptp(np.arange(4).reshape(2, 2)) np.testing.assert_equal(res, expected)
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) np.testing.assert_array_equal(res, expected) data = np.asfortranarray(np.random.rand(10, 20)) x = tensor(data, chunk_size=10) t = argwhere(x > 0.5) res = self.executor.execute_tensor(t, concat=True)[0] expected = np.argwhere(data > 0.5) np.testing.assert_array_equal(res, expected) self.assertTrue(res.flags['F_CONTIGUOUS']) self.assertFalse(res.flags['C_CONTIGUOUS'])
def testRepeat(self): a = arange(10, chunk_size=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], 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), chunk_size=5) t = repeat(a, 3) t = t.tiles() self.assertEqual(sum(t.nsplits[0]), 30) a = tensor(np.random.randn(100), chunk_size=10) t = repeat(a, 3) t = t.tiles() self.assertEqual(sum(t.nsplits[0]), 300) a = tensor(np.random.randn(4)) b = tensor((4, )) t = repeat(a, b) t = t.tiles() self.assertTrue(np.isnan(t.nsplits[0]))
def test_repeat(): a = arange(10, chunk_size=2).reshape(2, 5) t = repeat(a, 3) assert t.shape == (30, ) t = repeat(a, 3, axis=0) assert t.shape == (6, 5) t = repeat(a, 3, axis=1) assert t.shape == (2, 15) t = repeat(a, [3], axis=1) assert t.shape == (2, 15) t = repeat(a, [3, 4], axis=0) assert t.shape == (7, 5) with pytest.raises(ValueError): repeat(a, [3, 4], axis=1) a = tensor(np.random.randn(10), chunk_size=5) t = repeat(a, 3) t = tile(t) assert sum(t.nsplits[0]) == 30 a = tensor(np.random.randn(100), chunk_size=10) t = repeat(a, 3) t = tile(t) assert sum(t.nsplits[0]) == 300 a = tensor(np.random.randn(4)) b = tensor((4, )) t = repeat(a, b) t = tile(t) assert np.isnan(t.nsplits[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)]) # test one chunk arr = tensor(raw, chunk_size=20) raw_index = [8, 10, 3, 1, 9, 10] index = tensor(raw_index, chunk_size=20) arr9 = arr[index] res = self.executor.execute_tensor(arr9, concat=True)[0] np.testing.assert_array_equal(res, raw[raw_index]) raw_index1 = [[1, 3], [3, 7], [7, 7]] raw_index2 = [1, 9] index1 = tensor(raw_index1) index2 = tensor(raw_index2) arr10 = arr[0, index1, :, index2] res = self.executor.execute_tensor(arr10, concat=True)[0] np.testing.assert_array_equal(res, raw[0, raw_index1, :, raw_index2]) # test order raw = np.asfortranarray(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) arr11 = arr[index] res = self.executor.execute_tensor(arr11, concat=True)[0] expected = raw[raw_index].copy('A') np.testing.assert_array_equal(res, expected) self.assertEqual(res.flags['C_CONTIGUOUS'], expected.flags['C_CONTIGUOUS']) self.assertEqual(res.flags['F_CONTIGUOUS'], expected.flags['F_CONTIGUOUS'])
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)