def _test_inplace_modified(self, op, a: ChunkGrid, a0: ChunkGrid, b: ChunkGrid, b0: ChunkGrid, inplace: bool): # Test if inplace (or not) is working intended ad = a.to_dense() a0d = a0.to_dense() bd = b.to_dense() b0d = b0.to_dense() if inplace: # Inplace modification - change opa = ad == op(ad, bd) opb = bd == b0d else: # Not inplace modification - no change opa = ad == a0d opb = bd == b0d self.assertTrue(np.all(opa), f"Failure-Inplace-{inplace} {op}! \n{ad}\n-------\n{a0d}") self.assertTrue(np.all(opb), f"Failure-Inplace-{inplace} {op}! \n{bd}\n-------\n{b0d}")
def test_mask_1(self): a = ChunkGrid(2, int, 0) mask = ChunkGrid(2, bool, False) mask[1:3, 1:3, 1:3] = True self.assertEqual((4, 4, 4), mask.to_dense().shape) a[mask] = 3 expected = np.zeros((4, 4, 4), dtype=int) expected[1:3, 1:3, 1:3] = 3 self.assertEqual(str(expected), str(a.to_dense())) tmp = a == 3 self.assertEqual(str(mask.to_dense()), str(tmp.to_dense()))
def test_mask_2(self): a = ChunkGrid(2, bool, False) a[0, 0, 0] = True a.ensure_chunk_at_index((1, 1, 1)).set_fill(True) a.set_value((2, 1, 1), True) b = a.astype(np.int8) adense = a.to_dense() bdense = b.to_dense() self.assertEqual(adense.shape, bdense.shape) self.assertEqual(str(adense.astype(np.int8)), str(bdense)) mask = ChunkGrid(2, bool, False) mask.ensure_chunk_at_index((1, 0, 0)).set_fill(True) mask.ensure_chunk_at_index((0, 1, 0)).set_fill(True) mask.ensure_chunk_at_index((0, 0, 1))[0, 0, 0] = True b[mask] = 2 bdense = b.to_dense() expected = np.zeros((4, 4, 4), dtype=np.int8) expected[0, 0, 0] = 1 expected[2:4, 2:4, 2:4] = 1 expected[2, 1, 1] = 1 expected[2:4, 0:2, 0:2] = 2 expected[0:2, 2:4, 0:2] = 2 expected[0, 0, 2] = 2 self.assertEqual(expected.shape, bdense.shape) self.assertEqual(str(expected), str(bdense))
def _test_operator2_int(self, op, b0=0, dtype: Type = int, inplace=False): a = ChunkGrid(2, dtype, 0) b = ChunkGrid(2, dtype, 1) a.set_value((0, 0, 0), 1) a.set_value((0, 0, 1), 1) b.set_value((0, 0, 0), 1 + b0) b.set_value((0, 1, 0), 0 + b0) b.set_value((0, 1, 1), 0 + b0) a0 = a.copy() b0 = b.copy() expected = op(a.to_dense(), b.to_dense()) res = op(a, b) self.assertIsInstance(res, ChunkGrid) result = res.to_dense() assert result.shape == expected.shape self.assertTrue(np.all(result == expected), f"Failure {op}! \n{result}\n-------\n{expected}") self._test_inplace_modified(op, a, a0, b, b0, inplace)
def _test_operator1_int(self, op): a = ChunkGrid(2, int, -1) a.set_value((0, 0, 0), 0) a.set_value((0, 0, 1), 2) a.set_value((0, 1, 0), -2) expected = op(a.to_dense()) res = op(a) self.assertIsInstance(res, ChunkGrid) result = res.to_dense() assert result.shape == expected.shape self.assertTrue(np.all(result == expected), f"Failure {op}! \n{result}\n-------\n{expected}")
def test_special_dtype(self): t = np.dtype((np.int, (3,))) a = ChunkGrid(2, dtype=t, fill_value=np.zeros(3)) a.set_value((0, 0, 0), np.ones(3)) a.set_value((2, 2, 2), np.ones(3)) dense = a.to_dense() expected = np.zeros((4, 4, 4), dtype=t) expected[0, 0, 0] = 1 expected[2, 2, 2] = 1 self.assertEqual(expected.shape, dense.shape) self.assertEqual(str(expected), str(dense))
def test_set_pos(self): a = ChunkGrid(2, bool, False) a.set_value((0, 0, 0), True) a.set_value((2, 2, 2), True) self.assertFalse(a.chunks[0, 0, 0].is_filled()) result = a.to_dense() expected = np.zeros((4, 4, 4), dtype=bool) expected[0, 0, 0] = True expected[2, 2, 2] = True assert result.shape == expected.shape self.assertEqual(str(expected), str(result))
def test_getitem(self): CS = 2 shape = (CS * 3, CS * 3, CS * 3) soll = np.arange(shape[0] * shape[1] * shape[2]).reshape(shape) a = ChunkGrid(CS, int, -1) for u in range(shape[0] // CS): for v in range(shape[1] // CS): for w in range(shape[2] // CS): x, y, z = u * CS, v * CS, w * CS index = (u, v, w) a.ensure_chunk_at_index(index).set_array(soll[x:x + CS, y:y + CS, z:z + CS]) dense = a.to_dense() self.assertEqual(soll.shape, dense.shape) self.assertEqual(str(soll), str(dense)) self.assertEqual(str(soll[1: shape[0] - 1, 1: shape[1] - 1, 1: shape[2] - 1]), str(a[1: shape[0] - 1, 1: shape[1] - 1, 1: shape[2] - 1]))
def test_padding_no_corners_edges(self): a = ChunkGrid(2, int, -1) expected = np.zeros((6, 6, 6), int) for n, i in enumerate(np.ndindex(6, 6, 6)): a.set_value(i, n) expected[i] = n self.assertEqual(expected.shape, tuple(a.size())) self.assertEqual(str(expected), str(a.to_dense())) padded1 = a.padding_at((1, 1, 1), 1, corners=True, edges=True) expected1 = expected[1:-1, 1:-1, 1:-1] self.assertEqual(str(expected1), str(padded1)) padded2 = a.padding_at((1, 1, 1), 1, corners=False, edges=False) expected2 = np.full((4, 4, 4), -1, int) expected2[1:-1, 1:-1, :] = expected[2:-2, 2:-2, 1:-1] expected2[1:-1, :, 1:-1] = expected[2:-2, 1:-1, 2:-2] expected2[:, 1:-1, 1:-1] = expected[1:-1, 2:-2, 2:-2] self.assertEqual(str(expected2), str(padded2))
def test_getitem_offset(self): CS = 2 shape = (CS * 3, CS * 3, CS * 3) soll = np.arange(shape[0] * shape[1] * shape[2]).reshape(shape) offset_chunk = (-1, 0, 1) a = ChunkGrid(CS, int, -1) for u in range(shape[0] // CS): for v in range(shape[1] // CS): for w in range(shape[2] // CS): index = np.add((u, v, w), offset_chunk) x, y, z = np.multiply((u, v, w), CS) a.ensure_chunk_at_index(index).set_array(soll[x:x + CS, y:y + CS, z:z + CS]) offset_voxel = np.multiply(offset_chunk, CS) dense, off = a.to_dense(return_offset=True) self.assertEqual(soll.shape, dense.shape) self.assertEqual(str(soll), str(dense)) self.assertEqual(list(offset_voxel), list(off)) ox, oy, oz = off self.assertEqual(str(soll[1: shape[0] - 1, 1: shape[1] - 1, 1: shape[2] - 1]), str(a[1 + ox: shape[0] - 1 + ox, 1 + oy: shape[1] - 1 + oy, 1 + oz: shape[2] - 1 + oz]))