Skip to content

Commit fa2783f

Browse files
Force native grid_sample path and add docstring in Warp regression test
Address review feedback: pin USE_COMPILED=False so the test always exercises the native grid_sample branch (the only path that normalizes the grid; the csrc grid_pull path is unaffected), and add a Google-style docstring. Signed-off-by: VenkateswarluNagineni <venkates2002@tamu.edu>
1 parent 79c0498 commit fa2783f

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

tests/networks/blocks/warp/test_warp.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import unittest
1414
from pathlib import Path
15+
from unittest import mock
1516

1617
import numpy as np
1718
import torch
@@ -138,18 +139,26 @@ def test_ill_shape(self):
138139
with self.assertRaisesRegex(ValueError, ""):
139140
warp_layer(image=torch.arange(4).reshape((1, 1, 2, 2)).to(dtype=torch.float), ddf=torch.zeros(1, 2, 3, 3))
140141

142+
@mock.patch("monai.networks.blocks.warp.USE_COMPILED", False)
141143
def test_singleton_spatial_dim(self):
142-
# a single-slice volume / single-row image has a spatial dim of size 1; the grid
143-
# normalization must not divide by zero (regression for native grid_sample path).
144-
# "zeros" padding maps an out-of-range (here NaN, pre-fix) coordinate to 0, so it exposes
145-
# the bug; "border"/"reflection" would clamp onto the lone voxel and mask it.
144+
"""
145+
Regression test for a singleton spatial dimension (a single-slice volume or a
146+
single-row/column image), where the grid normalization ``* 2 / (dim - 1)`` previously
147+
divided by zero.
148+
149+
The native ``grid_sample`` path is forced via ``USE_COMPILED=False`` because only that
150+
branch normalizes the grid; the csrc ``grid_pull`` path is unaffected. ``padding_mode``
151+
is ``"zeros"`` so an out-of-range (pre-fix ``nan``) coordinate maps to 0 and exposes the
152+
bug, whereas ``"border"``/``"reflection"`` would clamp onto the lone voxel and mask it.
153+
For a zero displacement field the warped output must contain no ``nan`` and must equal
154+
the input image.
155+
"""
146156
for shape, ndim in [((1, 1, 1, 4, 4), 3), ((1, 1, 1, 5), 2), ((1, 1, 5, 1), 2)]:
147157
image = torch.rand(*shape)
148158
ddf = torch.zeros(shape[0], ndim, *shape[2:])
149159
warp_layer = Warp(mode="bilinear", padding_mode="zeros")
150160
result = warp_layer(image, ddf)
151161
self.assertFalse(torch.isnan(result).any(), f"NaN in warp output for shape {shape}")
152-
# a zero displacement field must reproduce the input image
153162
np.testing.assert_allclose(result.cpu().numpy(), image.cpu().numpy(), rtol=1e-4, atol=1e-4)
154163

155164
def test_grad(self):

0 commit comments

Comments
 (0)