Skip to content

Commit d05d894

Browse files
committed
Reject sub-2D channel-first tensors in _normalize_spatial_ndim
Raise a clear ValueError when a channel-first tensor has fewer than 2 dims instead of silently clamping spatial_ndim to 1, per review feedback. Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent 9f8d9f6 commit d05d894

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

monai/data/meta_tensor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434

3535
def _normalize_spatial_ndim(spatial_ndim: int, tensor_ndim: int, no_channel: bool = False) -> int:
3636
"""Clamp spatial dims to a valid range for the current tensor shape."""
37+
if tensor_ndim < 2 and not no_channel:
38+
raise ValueError(
39+
f"Channel-first tensor must have at least 2 dims (channel + 1 spatial), got ndim={tensor_ndim}"
40+
)
3741
limit = max(int(tensor_ndim), 1) if no_channel else max(int(tensor_ndim) - 1, 1)
3842
return max(1, min(int(spatial_ndim), limit))
3943

0 commit comments

Comments
 (0)