Skip to content

Commit 71abf1b

Browse files
authored
5902 allow for missing filename_or_obj key (#5980)
Signed-off-by: Wenqi Li <wenqil@nvidia.com> Fixes #5902 ### Description the source image in ResampleToMatch may not have a `filename_or_obj` when the source image is not created from `LoadImage` ### 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`. - [x] 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 de2b48c commit 71abf1b

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

monai/data/folder_layout.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
def default_name_formatter(metadict, saver):
2222
"""Returns a kwargs dict for :py:meth:`FolderLayout.filename`,
2323
according to the input metadata and SaveImage transform."""
24-
subject = metadict[monai.utils.ImageMetaKey.FILENAME_OR_OBJ] if metadict else getattr(saver, "_data_index", 0)
24+
subject = (
25+
metadict.get(monai.utils.ImageMetaKey.FILENAME_OR_OBJ, getattr(saver, "_data_index", 0))
26+
if metadict
27+
else getattr(saver, "_data_index", 0)
28+
)
2529
patch_index = metadict.get(monai.utils.ImageMetaKey.PATCH_INDEX, None) if metadict else None
2630
return {"subject": f"{subject}", "idx": patch_index}
2731

monai/transforms/spatial/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def update_meta(self, img: torch.Tensor, dst_affine=None, img_dst=None):
346346
if dst_affine is not None:
347347
super().update_meta(img, dst_affine)
348348
if isinstance(img_dst, MetaTensor) and isinstance(img, MetaTensor):
349-
original_fname = img.meta[Key.FILENAME_OR_OBJ]
349+
original_fname = img.meta.get(Key.FILENAME_OR_OBJ, "resample_to_match_source")
350350
img.meta = deepcopy(img_dst.meta)
351351
img.meta[Key.FILENAME_OR_OBJ] = original_fname # keep the original name, the others are overwritten
352352

tests/test_resample_to_match.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121

2222
import nibabel as nib
2323
import numpy as np
24+
import torch
2425
from parameterized import parameterized
2526

27+
from monai.data import MetaTensor
2628
from monai.data.image_reader import ITKReader, NibabelReader
2729
from monai.data.image_writer import ITKWriter
28-
from monai.transforms import Compose, EnsureChannelFirstd, LoadImaged, ResampleToMatch, SaveImaged
30+
from monai.transforms import Compose, EnsureChannelFirstd, LoadImaged, ResampleToMatch, SaveImage, SaveImaged
2931
from monai.utils import optional_import
3032
from tests.utils import assert_allclose, download_url_or_skip_test, testing_data_config
3133

@@ -90,6 +92,13 @@ def test_inverse(self):
9092
self.assertLess(((im_mod2.affine - data["im2"].affine) ** 2).sum() ** 0.5, 1e-2)
9193
self.assertEqual(im_mod2.applied_operations, [])
9294

95+
def test_no_name(self):
96+
img_1 = MetaTensor(torch.zeros(1, 2, 2, 2))
97+
img_2 = MetaTensor(torch.zeros(1, 3, 3, 3))
98+
im_mod = ResampleToMatch()(img_1, img_2)
99+
self.assertEqual(im_mod.meta["filename_or_obj"], "resample_to_match_source")
100+
SaveImage(output_dir=self.tmpdir, output_postfix="", separate_folder=False, resample=False)(im_mod)
101+
93102

94103
if __name__ == "__main__":
95104
unittest.main()

0 commit comments

Comments
 (0)