def test_get_chunk_sizes(self): chunks = get_chunk_sizes((100, 100, 40), (30, 25, 50)) self.assertEqual([(30, 30, 30, 10), (25, 25, 25, 25), (40, )], list(chunks)) chunks = get_chunk_sizes((100, 100, 40), (100, 100, 40)) self.assertEqual([(100, ), (100, ), (40, )], list(chunks))
def _assert_from_func(self, shape, chunks): def my_func(array_shape, array_dtype, block_shape, block_slices): bsy, bsx = block_slices bh, bw = block_shape w = array_shape[-1] i0 = bsx[0] j0 = bsy[0] a = np.ndarray((bh, bw), dtype=array_dtype) for j in range(bh): for i in range(bw): a[j, i] = 0.1 * (i0 + i + (j0 + j) * w) return a a = compute_array_from_func( my_func, shape, chunks, np.float64, ctx_arg_names=['shape', 'dtype', 'block_shape', 'block_slices']) self.assertIsNotNone(a) self.assertEqual(shape, a.shape) self.assertEqual(tuple(get_chunk_sizes(shape, chunks)), a.chunks) self.assertEqual(np.float64, a.dtype) h, w = shape n = w * h # Compute result actual = np.array(a) expected = (0.1 * np.linspace(0, n - 1, n, dtype=np.float64)).reshape(shape) np.testing.assert_almost_equal(actual, expected)
def ij_bboxes(self) -> np.ndarray: chunk_sizes = get_chunk_sizes((self.height, self.width), (self.tile_height, self.tile_width)) _, _, block_slices = get_block_iterators(chunk_sizes) block_slices = tuple(block_slices) n = len(block_slices) ij_bboxes = np.ndarray((n, 4), dtype=np.int64) for i in range(n): y_slice, x_slice = block_slices[i] ij_bboxes[i, 0] = x_slice.start ij_bboxes[i, 1] = y_slice.start ij_bboxes[i, 2] = x_slice.stop - 1 ij_bboxes[i, 3] = y_slice.stop - 1 return ij_bboxes
def ij_bboxes(self) -> np.ndarray: """The image tiles' bounding boxes in image pixel coordinates.""" chunk_sizes = get_chunk_sizes((self.height, self.width), (self.tile_height, self.tile_width)) _, _, block_slices = get_block_iterators(chunk_sizes) block_slices = tuple(block_slices) n = len(block_slices) ij_bboxes = np.ndarray((n, 4), dtype=np.int64) for i in range(n): y_slice, x_slice = block_slices[i] ij_bboxes[i, 0] = x_slice.start ij_bboxes[i, 1] = y_slice.start ij_bboxes[i, 2] = x_slice.stop ij_bboxes[i, 3] = y_slice.stop return ij_bboxes