|
16 | 16 | """Tests for warp.""" |
17 | 17 |
|
18 | 18 | from absl.testing import absltest |
| 19 | +from absl.testing import parameterized |
19 | 20 | from connectomics.common import bounding_box |
20 | 21 | import numpy as np |
21 | 22 |
|
@@ -126,5 +127,66 @@ def test_warp_points(self): |
126 | 127 | np.testing.assert_array_equal(warped, expected) |
127 | 128 |
|
128 | 129 |
|
| 130 | +class NdimageWarpBoxesTest(parameterized.TestCase): |
| 131 | + |
| 132 | + @parameterized.product(dim=[2, 3], parallelism=[1, 2], scaled=[False, True]) |
| 133 | + def test_cropped_output_with_boxes(self, dim, parallelism, scaled): |
| 134 | + image_shape = (8, 12, 16)[-dim:] |
| 135 | + image = np.zeros(image_shape, dtype=np.float32) |
| 136 | + weights = np.array([1, 100, 10000])[:dim] |
| 137 | + for weight, coord in zip(weights, np.indices(image_shape)[::-1]): |
| 138 | + image += weight * coord |
| 139 | + |
| 140 | + scale = np.array([2., 0.5, 1.] if scaled else [1., 1., 1.])[:dim] |
| 141 | + stride_xyz = np.array([2, 3, 1])[:dim] |
| 142 | + image_start = np.array([20, 30, 4])[:dim] * scale |
| 143 | + map_start = np.array([8, 8, 2])[:dim] |
| 144 | + map_size = (12, 10, 10)[:dim] |
| 145 | + output_start = np.array([22, 33, 5])[:dim] |
| 146 | + output_size = (5, 4, 3)[:dim] |
| 147 | + displacement = np.array([1., 2., 0.])[:dim] |
| 148 | + |
| 149 | + def box(start, size): |
| 150 | + return bounding_box.BoundingBox( |
| 151 | + start=tuple(start) + ((0,) if dim == 2 else ()), |
| 152 | + size=tuple(size) + ((1,) if dim == 2 else ()), |
| 153 | + ) |
| 154 | + |
| 155 | + coord_map = np.zeros((dim,) + map_size[::-1]) |
| 156 | + coord_map[:] = displacement.reshape((dim,) + (1,) * dim) |
| 157 | + result = warp.ndimage_warp( |
| 158 | + image, coord_map, tuple(stride_xyz[::-1]), (4, 3, 2)[:dim], |
| 159 | + (1,) * dim, order=1, |
| 160 | + image_box=box(image_start, image_shape[::-1]), |
| 161 | + map_box=box(map_start, map_size), |
| 162 | + out_box=box(output_start, output_size), |
| 163 | + out_scale=tuple(scale), parallelism=parallelism, |
| 164 | + ) |
| 165 | + expected = np.zeros(output_size[::-1]) |
| 166 | + for axis, coord in enumerate(np.indices(expected.shape)[::-1]): |
| 167 | + source_coord = ( |
| 168 | + (coord + output_start[axis] + displacement[axis]) * scale[axis] |
| 169 | + - image_start[axis] |
| 170 | + ) |
| 171 | + expected += weights[axis] * source_coord |
| 172 | + self.assertEqual(result.shape, expected.shape) |
| 173 | + self.assertEqual(result.dtype, image.dtype) |
| 174 | + np.testing.assert_allclose(result, expected, rtol=1e-6) |
| 175 | + |
| 176 | + @parameterized.parameters(2, 3) |
| 177 | + def test_output_box_without_map_box(self, dim): |
| 178 | + shape = (4, 8, 10)[-dim:] |
| 179 | + image = np.arange(np.prod(shape), dtype=np.float32).reshape(shape) |
| 180 | + output_size = (6, 5, 1) |
| 181 | + result = warp.ndimage_warp( |
| 182 | + image, np.zeros((dim,) + shape), (1,) * dim, |
| 183 | + (4,) * dim, (1,) * dim, |
| 184 | + out_box=bounding_box.BoundingBox(start=(0, 0, 0), size=output_size), |
| 185 | + ) |
| 186 | + expected = image[tuple(slice(0, n) for n in output_size[::-1][-dim:])] |
| 187 | + self.assertEqual(result.shape, expected.shape) |
| 188 | + np.testing.assert_array_equal(result, expected) |
| 189 | + |
| 190 | + |
129 | 191 | if __name__ == '__main__': |
130 | 192 | absltest.main() |
0 commit comments