def testClipOrderExecution(self): a_data = np.asfortranarray(np.random.rand(4, 8)) a = tensor(a_data, chunk_size=3) b = clip(a, 0.2, 0.8) res = self.executor.execute_tensor(b, concat=True)[0] expected = np.clip(a_data, 0.2, 0.8) np.testing.assert_allclose(res, expected) self.assertTrue(res.flags['F_CONTIGUOUS']) self.assertFalse(res.flags['C_CONTIGUOUS'])
def testModfExecution(self): data1 = np.random.random((5, 9)) arr1 = tensor(data1.copy(), chunk_size=3) o1, o2 = modf(arr1) o = o1 + o2 res = self.executor.execute_tensor(o, concat=True)[0] expected = sum(np.modf(data1)) self.assertTrue(np.allclose(res, expected)) o1, o2 = modf([0, 3.5]) o = o1 + o2 res = self.executor.execute_tensor(o, concat=True)[0] expected = sum(np.modf([0, 3.5])) self.assertTrue(np.allclose(res, expected)) arr1 = tensor(data1.copy(), chunk_size=3) o1 = zeros(data1.shape, chunk_size=3) o2 = zeros(data1.shape, chunk_size=3) modf(arr1, o1, o2) o = o1 + o2 res = self.executor.execute_tensor(o, concat=True)[0] expected = sum(np.modf(data1)) self.assertTrue(np.allclose(res, expected)) data1 = sps.random(5, 9, density=.1) arr1 = tensor(data1.copy(), chunk_size=3) o1, o2 = modf(arr1) o = o1 + o2 res = self.executor.execute_tensor(o, concat=True)[0] expected = sum(np.modf(data1.toarray())) np.testing.assert_equal(res.toarray(), expected)
def testModfOrderExecution(self): data1 = np.random.random((5, 9)) t = tensor(data1, chunk_size=3) o1, o2 = modf(t, order='F') res1, res2 = self.executor.execute_tileables([o1, o2]) expected1, expected2 = np.modf(data1, order='F') np.testing.assert_allclose(res1, expected1) self.assertTrue(res1.flags['F_CONTIGUOUS']) self.assertFalse(res1.flags['C_CONTIGUOUS']) np.testing.assert_allclose(res2, expected2) self.assertTrue(res2.flags['F_CONTIGUOUS']) self.assertFalse(res2.flags['C_CONTIGUOUS'])
def test_arg_reduction(setup): raw = np.random.random((20, 20, 20)) arr = tensor(raw, chunk_size=6) assert raw.argmax() == arr.argmax().execute().fetch() assert raw.argmin() == arr.argmin().execute().fetch() np.testing.assert_array_equal(raw.argmax(axis=0), arr.argmax(axis=0).execute().fetch()) np.testing.assert_array_equal(raw.argmin(axis=0), arr.argmin(axis=0).execute().fetch()) raw_format = sps.random(20, 20, density=.1, format='lil') random_min = np.random.randint(0, 200) random_max = np.random.randint(200, 400) raw_format[np.unravel_index(random_min, raw_format.shape)] = -1 raw_format[np.unravel_index(random_max, raw_format.shape)] = 2 raw = raw_format.tocoo() arr = tensor(raw, chunk_size=6) assert raw.argmax() == arr.argmax().execute().fetch() assert raw.argmin() == arr.argmin().execute().fetch() # test order raw = np.asfortranarray(np.random.rand(10, 20, 30)) arr = tensor(raw, chunk_size=13) arr2 = arr.argmax(axis=-1) res = arr2.execute().fetch() expected = raw.argmax(axis=-1) np.testing.assert_allclose(res, expected) assert res.flags['C_CONTIGUOUS'] == expected.flags['C_CONTIGUOUS'] assert res.flags['F_CONTIGUOUS'] == expected.flags['F_CONTIGUOUS'] with pytest.raises(TypeError): tensor(list('abcdefghi'), dtype=object).argmax().execute()
def testMapChunk(self): raw = np.random.rand(20) a = tensor(raw, chunk_size=10) mapped = a.map_chunk(lambda x: x * 0.5).tiles() self.assertTrue(np.issubdtype(mapped.dtype, np.floating)) self.assertEqual(mapped.shape, (np.nan, )) self.assertEqual(len(mapped.chunks), 2) mapped = a.map_chunk(lambda x: x * 0.5, elementwise=True).tiles() self.assertTrue(np.issubdtype(mapped.dtype, np.floating)) self.assertEqual(mapped.shape, (20, )) self.assertEqual(len(mapped.chunks), 2)
def testSliceExecution(self): raw = np.random.random((11, 8, 12, 14)) arr = tensor(raw, chunk_size=3) arr2 = arr[2:9:2, 3:7, -1:-9:-2, 12:-11:-4] res = self.executor.execute_tensor(arr2, concat=True) np.testing.assert_array_equal(res[0], raw[2:9:2, 3:7, -1:-9:-2, 12:-11:-4]) arr3 = arr[-4, 2:] res = self.executor.execute_tensor(arr3, concat=True) np.testing.assert_equal(res[0], raw[-4, 2:]) raw = sps.random(12, 14, density=.1) arr = tensor(raw, chunk_size=3) arr2 = arr[-1:-9:-2, 12:-11:-4] res = self.executor.execute_tensor(arr2, concat=True)[0] np.testing.assert_equal(res.toarray(), raw.toarray()[-1:-9:-2, 12:-11:-4])
def testBaseOrderExecution(self): raw = np.asfortranarray(np.random.rand(5, 6)) arr = tensor(raw, chunk_size=3) res = self.executor.execute_tensor(arr + 1, concat=True)[0] np.testing.assert_array_equal(res, raw + 1) self.assertFalse(res.flags['C_CONTIGUOUS']) self.assertTrue(res.flags['F_CONTIGUOUS']) res2 = self.executor.execute_tensor(add(arr, 1, order='C'), concat=True)[0] np.testing.assert_array_equal(res2, np.add(raw, 1, order='C')) self.assertTrue(res2.flags['C_CONTIGUOUS']) self.assertFalse(res2.flags['F_CONTIGUOUS'])
def testRechunkExecution(self): raw = np.random.random((11, 8)) arr = tensor(raw, chunk_size=3) arr2 = arr.rechunk(4) res = self.executor.execute_tensor(arr2) self.assertTrue(np.array_equal(res[0], raw[:4, :4])) self.assertTrue(np.array_equal(res[1], raw[:4, 4:])) self.assertTrue(np.array_equal(res[2], raw[4:8, :4])) self.assertTrue(np.array_equal(res[3], raw[4:8, 4:])) self.assertTrue(np.array_equal(res[4], raw[8:, :4])) self.assertTrue(np.array_equal(res[5], raw[8:, 4:]))
def test_h_stack_execution(setup): a_data = np.random.rand(10) b_data = np.random.rand(20) a = tensor(a_data, chunk_size=8) b = tensor(b_data, chunk_size=8) c = hstack([a, b]) res = c.execute().fetch() expected = np.hstack([a_data, b_data]) assert np.array_equal(res, expected) is True a_data = np.random.rand(10, 20) b_data = np.random.rand(10, 5) a = tensor(a_data, chunk_size=6) b = tensor(b_data, chunk_size=8) c = hstack([a, b]) res = c.execute().fetch() expected = np.hstack([a_data, b_data]) assert np.array_equal(res, expected) is True
def test_map_chunk(): raw = np.random.rand(20) a = tensor(raw, chunk_size=10) mapped = tile(a.map_chunk(lambda x: x * 0.5)) assert np.issubdtype(mapped.dtype, np.floating) is True assert mapped.shape == (np.nan, ) assert len(mapped.chunks) == 2 mapped = tile(a.map_chunk(lambda x: x * 0.5, elementwise=True)) assert np.issubdtype(mapped.dtype, np.floating) is True assert mapped.shape == (20, ) assert len(mapped.chunks) == 2
def test_modf_execution(setup): data1 = np.random.random((5, 9)) arr1 = tensor(data1.copy(), chunk_size=3) o1, o2 = modf(arr1) o = o1 + o2 res = o.execute().fetch() expected = sum(np.modf(data1)) np.testing.assert_array_almost_equal(res, expected) o1, o2 = modf([0, 3.5]) o = o1 + o2 res = o.execute().fetch() expected = sum(np.modf([0, 3.5])) np.testing.assert_array_almost_equal(res, expected) arr1 = tensor(data1.copy(), chunk_size=3) o1 = zeros(data1.shape, chunk_size=3) o2 = zeros(data1.shape, chunk_size=3) modf(arr1, o1, o2) o = o1 + o2 res = o.execute().fetch() expected = sum(np.modf(data1)) np.testing.assert_array_almost_equal(res, expected) data1 = sps.random(5, 9, density=.1) arr1 = tensor(data1.copy(), chunk_size=3) o1, o2 = modf(arr1) o = o1 + o2 res = o.execute().fetch() expected = sum(np.modf(data1.toarray())) np.testing.assert_equal(res.toarray(), expected)
def test_base_order_execution(setup): raw = np.asfortranarray(np.random.rand(5, 6)) arr = tensor(raw, chunk_size=3) res = (arr + 1).execute().fetch() np.testing.assert_array_equal(res, raw + 1) assert res.flags['C_CONTIGUOUS'] is False assert res.flags['F_CONTIGUOUS'] is True res2 = add(arr, 1, order='C').execute().fetch() np.testing.assert_array_equal(res2, np.add(raw, 1, order='C')) assert res2.flags['C_CONTIGUOUS'] is True assert res2.flags['F_CONTIGUOUS'] is False
def testDStackExecution(self): a_data = np.random.rand(10) b_data = np.random.rand(10) a = tensor(a_data, chunk_size=4) b = tensor(b_data, chunk_size=4) c = dstack([a, b]) res = self.executor.execute_tensor(c, concat=True)[0] expected = np.dstack([a_data, b_data]) self.assertTrue(np.array_equal(res, expected)) a_data = np.random.rand(10, 20) b_data = np.random.rand(10, 20) a = tensor(a_data, chunk_size=3) b = tensor(b_data, chunk_size=4) c = dstack([a, b]) res = self.executor.execute_tensor(c, concat=True)[0] expected = np.dstack([a_data, b_data]) self.assertTrue(np.array_equal(res, expected))
def testSetItemExecution(self): raw = data = np.random.randint(0, 10, size=(11, 8, 12, 13)) arr = tensor(raw.copy(), chunk_size=3) raw = raw.copy() idx = slice(2, 9, 2), slice(3, 7), slice(-1, -9, -2), 2 arr[idx] = 20 res = self.executor.execute_tensor(arr, concat=True)[0] raw[idx] = 20 np.testing.assert_array_equal(res, raw) self.assertEqual(res.flags['C_CONTIGUOUS'], raw.flags['C_CONTIGUOUS']) self.assertEqual(res.flags['F_CONTIGUOUS'], raw.flags['F_CONTIGUOUS']) raw = data shape = raw[idx].shape arr2 = tensor(raw.copy(), chunk_size=3) raw = raw.copy() replace = np.random.randint(10, 20, size=shape[:-1] + (1,)).astype('f4') arr2[idx] = tensor(replace, chunk_size=4) res = self.executor.execute_tensor(arr2, concat=True) raw[idx] = replace np.testing.assert_array_equal(res[0], raw) raw = np.asfortranarray(np.random.randint(0, 10, size=(11, 8, 12, 13))) arr = tensor(raw.copy('A'), chunk_size=3) raw = raw.copy('A') idx = slice(2, 9, 2), slice(3, 7), slice(-1, -9, -2), 2 arr[idx] = 20 res = self.executor.execute_tensor(arr, concat=True)[0] raw[idx] = 20 np.testing.assert_array_equal(res, raw) self.assertEqual(res.flags['C_CONTIGUOUS'], raw.flags['C_CONTIGUOUS']) self.assertEqual(res.flags['F_CONTIGUOUS'], raw.flags['F_CONTIGUOUS'])
def test_modf_order_execution(setup): data1 = np.random.random((5, 9)) t = tensor(data1, chunk_size=3) o1, o2 = modf(t, order='F') res1, res2 = execute(o1, o2) expected1, expected2 = np.modf(data1, order='F') np.testing.assert_allclose(res1, expected1) assert res1.flags['F_CONTIGUOUS'] is True assert res1.flags['C_CONTIGUOUS'] is False np.testing.assert_allclose(res2, expected2) assert res2.flags['F_CONTIGUOUS'] is True assert res2.flags['C_CONTIGUOUS'] is False
def test_clip_order_execution(setup): a_data = np.asfortranarray(np.random.rand(4, 8)) a = tensor(a_data, chunk_size=3) b = clip(a, 0.2, 0.8) res = b.execute().fetch() expected = np.clip(a_data, 0.2, 0.8) np.testing.assert_allclose(res, expected) assert res.flags['F_CONTIGUOUS'] is True assert res.flags['C_CONTIGUOUS'] is False
def test_unravel_index(): indices = tensor([22, 41, 37], chunk_size=1) t = unravel_index(indices, (7, 6)) assert len(t) == 2 t = [tile(r) for r in t] assert len(t[0].chunks) == 3 assert len(t[1].chunks) == 3 with pytest.raises(TypeError): unravel_index([22, 41, 37], (7, 6), order='B')
def testCumReduction(self): raw = np.random.randint(5, size=(8, 8, 8)) arr = tensor(raw, chunk_size=3) res1 = self.executor.execute_tensor(arr.cumsum(axis=1), concat=True) res2 = self.executor.execute_tensor(arr.cumprod(axis=1), concat=True) expected1 = raw.cumsum(axis=1) expected2 = raw.cumprod(axis=1) np.testing.assert_array_equal(res1[0], expected1) np.testing.assert_array_equal(res2[0], expected2) raw = sps.random(8, 8, density=.1) arr = tensor(raw, chunk_size=3) res1 = self.executor.execute_tensor(arr.cumsum(axis=1), concat=True) res2 = self.executor.execute_tensor(arr.cumprod(axis=1), concat=True) expected1 = raw.A.cumsum(axis=1) expected2 = raw.A.cumprod(axis=1) self.assertTrue(np.allclose(res1[0], expected1)) self.assertTrue(np.allclose(res2[0], expected2))
def testCopytoExecution(self): a = ones((2, 3), chunk_size=1) b = tensor([3, -1, 3], chunk_size=2) copyto(a, b, where=b > 1) res = self.executor.execute_tensor(a, concat=True)[0] expected = np.array([[3, 1, 3], [3, 1, 3]]) np.testing.assert_equal(res, expected) a = ones((2, 3), chunk_size=1) b = tensor(np.asfortranarray(np.random.rand(2, 3)), chunk_size=2) copyto(b, a) res = self.executor.execute_tensor(b, concat=True)[0] expected = np.asfortranarray(np.ones((2, 3))) np.testing.assert_array_equal(res, expected) self.assertTrue(res.flags['F_CONTIGUOUS']) self.assertFalse(res.flags['C_CONTIGUOUS'])
def test_vdot_execution(setup): a_data = np.array([1 + 2j, 3 + 4j]) b_data = np.array([5 + 6j, 7 + 8j]) a = tensor(a_data, chunk_size=1) b = tensor(b_data, chunk_size=1) t = vdot(a, b) res = t.execute().fetch() expected = np.vdot(a_data, b_data) np.testing.assert_equal(res, expected) a_data = np.array([[1, 4], [5, 6]]) b_data = np.array([[4, 1], [2, 2]]) a = tensor(a_data, chunk_size=1) b = tensor(b_data, chunk_size=1) t = vdot(a, b) res = t.execute().fetch() expected = np.vdot(a_data, b_data) np.testing.assert_equal(res, expected)
def testUnravelIndex(self): indices = tensor([22, 41, 37], chunk_size=1) t = unravel_index(indices, (7, 6)) self.assertEqual(len(t), 2) [r.tiles() for r in t] self.assertEqual(len(t[0].chunks), 3) self.assertEqual(len(t[1].chunks), 3) with self.assertRaises(TypeError): unravel_index([22, 41, 37], (7, 6), order='B')
def testVdotExecution(self): a_data = np.array([1 + 2j, 3 + 4j]) b_data = np.array([5 + 6j, 7 + 8j]) a = tensor(a_data, chunk_size=1) b = tensor(b_data, chunk_size=1) t = vdot(a, b) res = self.executor.execute_tensor(t)[0] expected = np.vdot(a_data, b_data) np.testing.assert_equal(res, expected) a_data = np.array([[1, 4], [5, 6]]) b_data = np.array([[4, 1], [2, 2]]) a = tensor(a_data, chunk_size=1) b = tensor(b_data, chunk_size=1) t = vdot(a, b) res = self.executor.execute_tensor(t)[0] expected = np.vdot(a_data, b_data) np.testing.assert_equal(res, expected)
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 testSqureFormExecution(self): from scipy.spatial.distance import pdist as sp_pdist, \ squareform as sp_squareform raw_a = np.random.rand(80, 10) raw_pdsit = sp_pdist(raw_a) raw_square = sp_squareform(raw_pdsit) # tomatrix, test 1 chunk vec = tensor(raw_pdsit, chunk_size=raw_pdsit.shape[0]) mat = distance.squareform(vec, chunk_size=100) result = self._executor.execute_tensor(mat, concat=True)[0] np.testing.assert_array_equal(result, raw_square) # tomatrix, test more than 1 chunk vec = tensor(raw_pdsit, chunk_size=33) self.assertGreater(len(vec.tiles().chunks), 1) mat = distance.squareform(vec, chunk_size=34) result = self._executor.execute_tensor(mat, concat=True)[0] np.testing.assert_array_equal(result, raw_square) # tovec, test 1 chunk mat = tensor(raw_square) vec = distance.squareform(mat, chunk_size=raw_pdsit.shape[0]) self.assertEqual(len(mat.tiles().chunks), 1) self.assertEqual(len(vec.tiles().chunks), 1) result = self._executor.execute_tensor(vec, concat=True)[0] np.testing.assert_array_equal(result, raw_pdsit) # tovec, test more than 1 chunk mat = tensor(raw_square, chunk_size=31) vec = distance.squareform(mat, chunk_size=40) self.assertGreater(len(vec.tiles().chunks), 1) result = self._executor.execute_tensor(vec, concat=True)[0] np.testing.assert_array_equal(result, raw_pdsit) # test checks # generate non-symmetric matrix non_sym_arr = np.random.RandomState(0).rand(10, 10) # 1 chunk mat = tensor(non_sym_arr) vec = distance.squareform(mat, checks=True, chunk_size=100) with self.assertRaises(ValueError): _ = self._executor.execute_tensor(vec, concat=True)[0] # force checks=False vec = distance.squareform(mat, checks=False, chunk_size=100) _ = self._executor.execute_tensor(vec, concat=True)[0] # more than 1 chunk mat = tensor(non_sym_arr, chunk_size=6) vec = distance.squareform(mat, checks=True, chunk_size=8) self.assertGreater(len(vec.tiles().chunks), 1) with self.assertRaises(ValueError): _ = self._executor.execute_tensor(vec, concat=True)[0] # force checks=False vec = distance.squareform(mat, checks=False, chunk_size=100) _ = self._executor.execute_tensor(vec, concat=True)[0]
def test_squareform_execution(setup): from scipy.spatial.distance import pdist as sp_pdist, \ squareform as sp_squareform raw_a = np.random.rand(80, 10) raw_pdsit = sp_pdist(raw_a) raw_square = sp_squareform(raw_pdsit) # tomatrix, test 1 chunk vec = tensor(raw_pdsit, chunk_size=raw_pdsit.shape[0]) mat = distance.squareform(vec, chunk_size=100) result = mat.execute().fetch() np.testing.assert_array_equal(result, raw_square) # tomatrix, test more than 1 chunk vec = tensor(raw_pdsit, chunk_size=33) assert len(tile(vec).chunks) > 1 mat = distance.squareform(vec, chunk_size=34) result = mat.execute().fetch() np.testing.assert_array_equal(result, raw_square) # tovec, test 1 chunk mat = tensor(raw_square) vec = distance.squareform(mat, chunk_size=raw_pdsit.shape[0]) assert len(tile(mat).chunks) == 1 assert len(tile(vec).chunks) == 1 result = vec.execute().fetch() np.testing.assert_array_equal(result, raw_pdsit) # tovec, test more than 1 chunk mat = tensor(raw_square, chunk_size=31) vec = distance.squareform(mat, chunk_size=40) assert len(tile(vec).chunks) > 1 result = vec.execute().fetch() np.testing.assert_array_equal(result, raw_pdsit) # test checks # generate non-symmetric matrix non_sym_arr = np.random.RandomState(0).rand(10, 10) # 1 chunk mat = tensor(non_sym_arr) vec = distance.squareform(mat, checks=True, chunk_size=100) with pytest.raises(ValueError): _ = vec.execute().fetch() # force checks=False vec = distance.squareform(mat, checks=False, chunk_size=100) _ = vec.execute().fetch() # more than 1 chunk mat = tensor(non_sym_arr, chunk_size=6) vec = distance.squareform(mat, checks=True, chunk_size=8) assert len(tile(vec).chunks) > 1 with pytest.raises(ValueError): _ = vec.execute().fetch() # force checks=False vec = distance.squareform(mat, checks=False, chunk_size=100) _ = vec.execute().fetch()
def testDiffExecution(self): data = np.array([1, 2, 4, 7, 0]) x = tensor(data, chunk_size=2) t = diff(x) res = self.executor.execute_tensor(t, concat=True)[0] expected = np.diff(data) np.testing.assert_equal(res, expected) t = diff(x, n=2) res = self.executor.execute_tensor(t, concat=True)[0] expected = np.diff(data, n=2) np.testing.assert_equal(res, expected) data = np.array([[1, 3, 6, 10], [0, 5, 6, 8]]) x = tensor(data, chunk_size=2) t = diff(x) res = self.executor.execute_tensor(t, concat=True)[0] expected = np.diff(data) np.testing.assert_equal(res, expected) t = diff(x, axis=0) res = self.executor.execute_tensor(t, concat=True)[0] expected = np.diff(data, axis=0) np.testing.assert_equal(res, expected) x = mt.arange('1066-10-13', '1066-10-16', dtype=mt.datetime64) t = diff(x) res = self.executor.execute_tensor(t, concat=True)[0] expected = np.diff( np.arange('1066-10-13', '1066-10-16', dtype=np.datetime64)) np.testing.assert_equal(res, expected)
def testMixedIndexingExecution(self): raw = np.random.random((11, 8, 12, 13)) arr = tensor(raw, chunk_size=3) raw_cond = raw[0, :, 0, 0] < .5 cond = tensor(raw[0, :, 0, 0], chunk_size=3) < .5 arr2 = arr[10::-2, cond, None, ..., :5] size_res = self.executor.execute_tensor(arr2, mock=True) res = self.executor.execute_tensor(arr2, concat=True) new_shape = list(arr2.shape) new_shape[1] = cond.shape[0] self.assertEqual(sum(s[0] for s in size_res), int(np.prod(new_shape) * arr2.dtype.itemsize)) np.testing.assert_array_equal(res[0], raw[10::-2, raw_cond, None, ..., :5]) b_raw = np.random.random(8) cond = tensor(b_raw, chunk_size=2) < .5 arr3 = arr[-2::-3, cond, ...] res = self.executor.execute_tensor(arr3, concat=True) np.testing.assert_array_equal(res[0], raw[-2::-3, b_raw < .5, ...])
def test_fancy_indexing_numpy_execution(setup): # test fancy index of type numpy ndarray raw = np.random.random((11, 8, 12, 14)) arr = tensor(raw, chunk_size=(6, 5, 7, 8)) index = [9, 10, 3, 1, 8, 10] arr2 = arr[index] res = arr2.execute().fetch() np.testing.assert_array_equal(res, raw[index]) index = np.random.permutation(8) arr3 = arr[:2, ..., index] res = arr3.execute().fetch() np.testing.assert_array_equal(res, raw[:2, ..., index]) index = [1, 3, 9, 10] arr4 = arr[..., index, :5] res = arr4.execute().fetch() np.testing.assert_array_equal(res, raw[..., index, :5]) index1 = [8, 10, 3, 1, 9, 10] index2 = [1, 3, 9, 10, 2, 7] arr5 = arr[index1, :, index2] res = arr5.execute().fetch() np.testing.assert_array_equal(res, raw[index1, :, index2]) index1 = [1, 3, 5, 7, 9, 10] index2 = [1, 9, 9, 10, 2, 7] arr6 = arr[index1, :, index2] res = arr6.execute().fetch() np.testing.assert_array_equal(res, raw[index1, :, index2]) index1 = [[8, 10, 3], [1, 9, 10]] index2 = [[1, 3, 9], [10, 2, 7]] arr7 = arr[index1, :, index2] res = arr7.execute().fetch() np.testing.assert_array_equal(res, raw[index1, :, index2]) index1 = [[1, 3], [3, 7], [7, 7]] index2 = [1, 9] arr8 = arr[0, index1, :, index2] res = arr8.execute().fetch() np.testing.assert_array_equal(res, raw[0, index1, :, index2])
def test_base_execution(setup): arr = ones((10, 8), chunk_size=2) arr2 = arr + 1 res = arr2.execute().fetch() np.testing.assert_array_equal(res, np.ones((10, 8)) + 1) data = np.random.random((10, 8, 3)) arr = tensor(data, chunk_size=2) arr2 = arr + 1 res = arr2.execute().fetch() np.testing.assert_array_equal(res, data + 1)
def test_to_cpu(): x = tensor(np.random.rand(10, 10), chunk_size=3, gpu=True) cx = to_cpu(x) assert cx.dtype == x.dtype assert cx.order == x.order assert cx.op.gpu is False cx, x = tile(cx, x) assert cx.chunks[0].dtype == x.chunks[0].dtype assert cx.chunks[0].order == x.chunks[0].order assert cx.chunks[0].op.gpu is False