Skip to content

Commit 3a3f6c4

Browse files
authored
5935 fixes spacing pixdim inplace change (#5950)
Signed-off-by: Wenqi Li <wenqil@nvidia.com> Fixes #5935 ### Description assigning `out_d[idx]` in the `__call__` shouldn't change `self.pixdim` in Spacing ### 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`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Wenqi Li <wenqil@nvidia.com>
1 parent 58c256e commit 3a3f6c4

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎monai/metrics/rocauc.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
from __future__ import annotations
1313

1414
import warnings
15-
from typing import cast
15+
from typing import TYPE_CHECKING, cast
1616

1717
import numpy as np
18-
import numpy.typing as npt
18+
19+
if TYPE_CHECKING:
20+
import numpy.typing as npt
21+
1922
import torch
2023

2124
from monai.utils import Average, look_up_option

‎monai/transforms/spatial/array.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def __call__(
553553
input_affine = np.eye(sr + 1, dtype=np.float64)
554554
affine_ = to_affine_nd(sr, convert_data_type(input_affine, np.ndarray)[0])
555555

556-
out_d = self.pixdim[:sr]
556+
out_d = self.pixdim[:sr].copy()
557557
if out_d.size < sr:
558558
out_d = np.append(out_d, [out_d[-1]] * (sr - out_d.size))
559559

‎tests/test_spacing.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ def test_inverse_mn_mx(self, device, recompute, align, scale_extent):
351351
self.assertEqual(img_out.shape, img_t.shape)
352352
self.assertLess(((affine - img_out.affine) ** 2).sum() ** 0.5, 5e-2)
353353

354+
def test_property_no_change(self):
355+
affine = torch.tensor(
356+
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], dtype=torch.float32, device="cpu"
357+
)
358+
affine[:3] *= 1 - 1e-4 # make sure it's not exactly target but close to the target
359+
img = MetaTensor(torch.rand((1, 10, 9, 8), dtype=torch.float32), affine=affine, meta={"fname": "somewhere"})
360+
tr = Spacing(pixdim=[1.0, 1.0, 1.0])
361+
tr(img)
362+
assert_allclose(tr.pixdim, [1.0, 1.0, 1.0], type_test=False)
363+
354364

355365
if __name__ == "__main__":
356366
unittest.main()

0 commit comments

Comments
 (0)