Skip to content

Commit 64719bd

Browse files
fix(transforms): guard None spatial_size in spatial_resample (#9068)
Signed-off-by: FinalSunFlower <auroral.sunflower@gmail.com>
1 parent 605611b commit 64719bd

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

monai/transforms/spatial/functional.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def spatial_resample(
156156
elif spatial_size is None and spatial_rank > 1: # auto spatial size
157157
spatial_size, _ = compute_shape_offset(in_spatial_size, src_affine, dst_affine) # type: ignore
158158
spatial_size = torch.tensor(
159-
fall_back_tuple(ensure_tuple(spatial_size)[:spatial_rank], in_spatial_size, lambda x: x >= 0)
159+
fall_back_tuple(ensure_tuple(spatial_size)[:spatial_rank], in_spatial_size, lambda x: x is not None and x >= 0)
160160
)
161161
extra_info = {
162162
"dtype": str(dtype_pt)[6:], # remove "torch": torch.float32 -> float32

tests/transforms/test_spatial_resample.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from monai.data.utils import to_affine_nd
2424
from monai.transforms import SpatialResample
2525
from monai.utils import optional_import
26-
from tests.lazy_transforms_utils import test_resampler_lazy
26+
from tests.lazy_transforms_utils import test_resampler_lazy as check_resampler_lazy
2727
from tests.test_utils import TEST_DEVICES, TEST_NDARRAYS_ALL, assert_allclose, dict_product
2828

2929
TESTS = []
@@ -148,7 +148,7 @@ def test_flips(self, img, device, data_param, expected_output):
148148
assert_allclose(out, expected_output, rtol=1e-2, atol=1e-2)
149149
assert_allclose(to_affine_nd(len(out.shape) - 1, out.affine), call_param["dst_affine"])
150150

151-
test_resampler_lazy(resampler, out, init_param=None, call_param=call_param)
151+
check_resampler_lazy(resampler, out, init_param=None, call_param=call_param)
152152

153153
@parameterized.expand(TEST_4_5_D)
154154
def test_4d_5d(self, new_shape, tile, device, dtype, expected_data):
@@ -165,7 +165,7 @@ def test_4d_5d(self, new_shape, tile, device, dtype, expected_data):
165165
assert_allclose(out, expected_data[None], rtol=1e-2, atol=1e-2)
166166
assert_allclose(out.affine, dst.to(torch.float32), rtol=1e-2, atol=1e-2)
167167

168-
test_resampler_lazy(resampler, out, init_param, call_param)
168+
check_resampler_lazy(resampler, out, init_param, call_param)
169169

170170
@parameterized.expand(TEST_DEVICES)
171171
def test_ill_affine(self, device):
@@ -199,7 +199,7 @@ def test_input_torch(self, new_shape, tile, device, dtype, expected_data, track_
199199
out = resampler(**call_param)
200200
assert_allclose(out, expected_data[None], rtol=1e-2, atol=1e-2)
201201

202-
test_resampler_lazy(resampler, out, init_param, call_param)
202+
check_resampler_lazy(resampler, out, init_param, call_param)
203203

204204
if track_meta:
205205
self.assertIsInstance(out, MetaTensor)
@@ -230,6 +230,22 @@ def test_unchange(self):
230230
assert_allclose(result, img, type_test=False)
231231
set_track_meta(True)
232232

233+
def test_none_spatial_size_rank_one(self):
234+
img = MetaTensor(torch.randn(1, 8))
235+
result = SpatialResample()(img, spatial_size=None)
236+
237+
self.assertEqual(result.shape, img.shape)
238+
self.assertIsInstance(result, MetaTensor)
239+
self.assertTrue(torch.isfinite(result).all())
240+
241+
def test_partial_none_spatial_size(self):
242+
img = MetaTensor(torch.randn(1, 3, 6, 7))
243+
result = SpatialResample()(img, spatial_size=(None, 4, 5))
244+
245+
self.assertEqual(result.shape, (1, 3, 4, 5))
246+
self.assertIsInstance(result, MetaTensor)
247+
self.assertTrue(torch.isfinite(result).all())
248+
233249

234250
if __name__ == "__main__":
235251
unittest.main()

0 commit comments

Comments
 (0)