Skip to content

Commit e9f48e3

Browse files
a-parida12pre-commit-ci[bot]monai-bot
authored
fix(spacing): default behaviour pixdim padding (#5248)
Fixes #4812 ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [x] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: a-parida12 <[email protected]> Signed-off-by: monai-bot <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: monai-bot <[email protected]>
1 parent ffaa791 commit e9f48e3

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

monai/transforms/spatial/array.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ def __init__(
447447
pixdim: output voxel spacing. if providing a single number, will use it for the first dimension.
448448
items of the pixdim sequence map to the spatial dimensions of input image, if length
449449
of pixdim sequence is longer than image spatial dimensions, will ignore the longer part,
450-
if shorter, will pad with `1.0`.
450+
if shorter, will pad with the last value. For example, for 3D image if pixdim is [1.0, 2.0] it
451+
will be padded to [1.0, 2.0, 2.0]
451452
if the components of the `pixdim` are non-positive values, the transform will use the
452453
corresponding components of the original pixdim, which is computed from the `affine`
453454
matrix of input image.
@@ -574,7 +575,8 @@ def __call__(
574575

575576
out_d = self.pixdim[:sr]
576577
if out_d.size < sr:
577-
out_d = np.append(out_d, [1.0] * (sr - out_d.size))
578+
out_d = np.append(out_d, [out_d[-1]] * (sr - out_d.size))
579+
578580
orig_d = affine_to_spacing(affine_, sr, out_d.dtype)
579581
for idx, (_d, mn, mx) in enumerate(
580582
zip_longest(orig_d, self.min_pixdim[:sr], self.max_pixdim[:sr], fillvalue=np.nan)
@@ -588,6 +590,7 @@ def __call__(
588590

589591
if not align_corners and scale_extent:
590592
warnings.warn("align_corners=False is not compatible with scale_extent=True.")
593+
591594
# compute output affine, shape and offset
592595
new_affine = zoom_affine(affine_, out_d, diagonal=self.diagonal)
593596
scale_extent = self.scale_extent if scale_extent is None else scale_extent

tests/test_spacing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444
*device,
4545
]
4646
)
47+
TESTS.append(
48+
[
49+
{"pixdim": 2.0, "padding_mode": "zeros", "dtype": float},
50+
torch.arange(4).reshape((1, 2, 2)) + 1.0, # data
51+
torch.eye(4),
52+
{},
53+
torch.tensor([[[1.0, 0.0], [0.0, 0.0]]]),
54+
*device,
55+
]
56+
)
4757
TESTS.append(
4858
[
4959
{"pixdim": (1.0, 1.0, 1.0), "padding_mode": "zeros", "dtype": float},

0 commit comments

Comments
 (0)