class TestDistArrayCreation(IpclusterTestCase): """Test distarray creation methods""" def setUp(self): self.context = Context(self.client) def test_zeros(self): shape = (16, 16) zero_distarray = self.context.zeros(shape) zero_ndarray = numpy.zeros(shape) assert_array_equal(zero_distarray.tondarray(), zero_ndarray) def test_ones(self): shape = (16, 16) one_distarray = self.context.ones(shape) one_ndarray = numpy.ones(shape) assert_array_equal(one_distarray.tondarray(), one_ndarray) def test_empty(self): shape = (16, 16) empty_distarray = self.context.empty(shape) self.assertEqual(empty_distarray.shape, shape) def test_fromndarray(self): ndarr = numpy.arange(16).reshape(4, 4) distarr = self.context.fromndarray(ndarr) for (i, j), val in numpy.ndenumerate(ndarr): self.assertEqual(distarr[i, j], ndarr[i, j])
class TestDistArrayCreation(unittest.TestCase): """Test distarray creation methods""" def setUp(self): self.context = Context() def tearDown(self): self.context.close() def test___init__(self): shape = (100, 100) distribution = Distribution.from_shape(self.context, shape, ('b', 'c')) da = DistArray(distribution, dtype=int) da.fill(42) nda = numpy.empty(shape, dtype=int) nda.fill(42) assert_array_equal(da.tondarray(), nda) def test_zeros(self): shape = (16, 16) zero_distarray = self.context.zeros(shape) zero_ndarray = numpy.zeros(shape) assert_array_equal(zero_distarray.tondarray(), zero_ndarray) def test_ones(self): shape = (16, 16) one_distarray = self.context.ones(shape) one_ndarray = numpy.ones(shape) assert_array_equal(one_distarray.tondarray(), one_ndarray) def test_empty(self): shape = (16, 16) empty_distarray = self.context.empty(shape) self.assertEqual(empty_distarray.shape, shape) def test_fromndarray(self): ndarr = numpy.arange(16).reshape(4, 4) distarr = self.context.fromndarray(ndarr) for (i, j), val in numpy.ndenumerate(ndarr): self.assertEqual(distarr[i, j], ndarr[i, j]) def test_grid_rank(self): # regression test for issue #235 a = self.context.empty((4, 4, 4), dist=('b', 'n', 'b'), grid_shape=(1, 1, 4)) self.assertEqual(a.grid_shape, (1, 1, 4)) def test_fromfunction(self): fn = lambda i, j: i + j shape = (7, 9) expected = numpy.fromfunction(fn, shape, dtype=int) result = self.context.fromfunction(fn, shape, dtype=int) assert_array_equal(expected, result.tondarray())
class TestDistArray(unittest.TestCase): def setUp(self): self.dac = Context() def tearDown(self): self.dac.close() def test_set_and_getitem_block_dist(self): size = 10 dap = self.dac.empty((size,), dist={0: 'b'}) for val in range(size): dap[val] = val for val in range(size): self.assertEqual(dap[val], val) for i in range(1, size + 1): dap[-i] = i self.assertEqual(dap[-i], i) def test_set_and_getitem_nd_block_dist(self): size = 5 dap = self.dac.empty((size, size), dist={0: 'b', 1: 'b'}) for row in range(size): for col in range(size): val = size*row + col dap[row, col] = val self.assertEqual(dap[row, col], val) for row in range(1 ,size + 1): for col in range(1, size + 1): dap[-row, -col] = row + col self.assertEqual(dap[-row, -col], row + col) def test_set_and_getitem_cyclic_dist(self): size = 10 dap = self.dac.empty((size,), dist={0: 'c'}) for val in range(size): dap[val] = val self.assertEqual(dap[val], val) for i in range(1, size + 1): dap[-i] = i self.assertEqual(dap[-i], i) def test_get_index_error(self): dap = self.dac.empty((10,), dist={0: 'c'}) with self.assertRaises(IndexError): dap[11] with self.assertRaises(IndexError): dap[-11] def test_set_index_error(self): dap = self.dac.empty((10,), dist={0: 'c'}) with self.assertRaises(IndexError): dap[11] = 55 with self.assertRaises(IndexError): dap[-11] = 55 def test_iteration(self): size = 10 dap = self.dac.empty((size,), dist={0: 'c'}) dap.fill(10) for val in dap: self.assertEqual(val, 10) def test_tondarray(self): dap = self.dac.empty((3, 3)) ndarr = numpy.arange(9).reshape(3, 3) for (i, j), val in numpy.ndenumerate(ndarr): dap[i, j] = ndarr[i, j] numpy.testing.assert_array_equal(dap.tondarray(), ndarr) def test_global_tolocal_bug(self): # gh-issue #154 dap = self.dac.zeros((3, 3), dist=('n', 'b')) ndarr = numpy.zeros((3, 3)) numpy.testing.assert_array_equal(dap.tondarray(), ndarr)
class TestDistArray(IpclusterTestCase): def setUp(self): self.dac = Context(self.client) def test_set_and_getitem_block_dist(self): size = 10 dap = self.dac.empty((size,), dist={0: 'b'}) for val in range(size): dap[val] = val for val in range(size): self.assertEqual(dap[val], val) def test_set_and_getitem_nd_block_dist(self): size = 5 dap = self.dac.empty((size, size), dist={0: 'b', 1: 'b'}) for row in range(size): for col in range(size): val = size*row + col dap[row, col] = val for row in range(size): for col in range(size): val = size*row + col self.assertEqual(dap[row, col], val) def test_set_and_getitem_cyclic_dist(self): size = 10 dap = self.dac.empty((size,), dist={0: 'c'}) for val in range(size): dap[val] = val for val in range(size): self.assertEqual(dap[val], val) @unittest.skip("Slicing not yet implemented.") def test_slice_in_getitem_block_dist(self): dap = self.dac.empty((100,), dist={0: 'b'}) self.assertIsInstance(dap[20:40], DistArray) @unittest.skip("Slicing not yet implemented.") def test_slice_in_setitem_raises_valueerror(self): dap = self.dac.empty((100,), dist={0: 'b'}) vals = numpy.random.random(20) with self.assertRaises(NotImplementedError): dap[20:40] = vals @unittest.skip('Slice assignment not yet implemented.') def test_slice_size_error(self): dap = self.dac.empty((100,), dist={0: 'c'}) with self.assertRaises(NotImplementedError): dap[20:40] = (11, 12) def test_get_index_error(self): dap = self.dac.empty((10,), dist={0: 'c'}) with self.assertRaises(IndexError): dap[11] def test_set_index_error(self): dap = self.dac.empty((10,), dist={0: 'c'}) with self.assertRaises(IndexError): dap[11] = 55 def test_iteration(self): size = 10 dap = self.dac.empty((size,), dist={0: 'c'}) dap.fill(10) for val in dap: self.assertEqual(val, 10) def test_tondarray(self): dap = self.dac.empty((3, 3)) ndarr = numpy.arange(9).reshape(3, 3) for (i, j), val in numpy.ndenumerate(ndarr): dap[i, j] = ndarr[i, j] numpy.testing.assert_array_equal(dap.tondarray(), ndarr) def test_global_tolocal_bug(self): # gh-issue #154 dap = self.dac.zeros((3, 3), dist=('n', 'b')) ndarr = numpy.zeros((3, 3)) numpy.testing.assert_array_equal(dap.tondarray(), ndarr)