Skip to content

Commit bfa6f38

Browse files
Handle bounding boxes in 2D ndimage warping
1 parent c8b5e25 commit bfa6f38

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎tests/warp_test.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""Tests for warp."""
1717

1818
from absl.testing import absltest
19+
from absl.testing import parameterized
1920
from connectomics.common import bounding_box
2021
import numpy as np
2122

@@ -126,5 +127,66 @@ def test_warp_points(self):
126127
np.testing.assert_array_equal(warped, expected)
127128

128129

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+
129191
if __name__ == '__main__':
130192
absltest.main()

‎warp.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def ndimage_warp(
255255
src_map += (
256256
map_box.start[:dim] * stride[::-1]
257257
- image_box.start[:dim] / out_scale[:dim]
258-
).reshape(dim, 1, 1, 1)
258+
).reshape((dim,) + (1,) * dim)
259259

260260
# Translate map to source (data) units.
261261
reshaper = tuple([slice(None)] + [np.newaxis] * dim)
@@ -270,7 +270,7 @@ def ndimage_warp(
270270
sub_dim = 1
271271

272272
if out_box is not None:
273-
warped = np.zeros(shape=out_box.size[::-1], dtype=image.dtype)
273+
warped = np.zeros(shape=out_box.size[::-1][sub_dim:], dtype=image.dtype)
274274
else:
275275
warped = np.zeros_like(image)
276276
out_box = bounding_box.BoundingBox(start=(0, 0, 0), size=image_size_xyz)
@@ -285,7 +285,9 @@ def ndimage_warp(
285285
# Compute the position of map_box relative to the out_box.
286286
if map_box is not None:
287287
assert out_box is not None
288-
offset = (map_box.start * stride[::-1] - out_box.start)[::-1]
288+
offset = (
289+
map_box.start[:dim] * stride[::-1] - out_box.start[:dim]
290+
)[::-1]
289291
else:
290292
offset = (0, 0, 0)
291293

0 commit comments

Comments
 (0)