Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5902 allow for missing filename_or_obj key #5980

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion monai/data/folder_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
def default_name_formatter(metadict, saver):
"""Returns a kwargs dict for :py:meth:`FolderLayout.filename`,
according to the input metadata and SaveImage transform."""
subject = metadict[monai.utils.ImageMetaKey.FILENAME_OR_OBJ] if metadict else getattr(saver, "_data_index", 0)
subject = (
metadict.get(monai.utils.ImageMetaKey.FILENAME_OR_OBJ, getattr(saver, "_data_index", 0))
if metadict
else getattr(saver, "_data_index", 0)
)
patch_index = metadict.get(monai.utils.ImageMetaKey.PATCH_INDEX, None) if metadict else None
return {"subject": f"{subject}", "idx": patch_index}

Expand Down
2 changes: 1 addition & 1 deletion monai/transforms/spatial/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def update_meta(self, img: torch.Tensor, dst_affine=None, img_dst=None):
if dst_affine is not None:
super().update_meta(img, dst_affine)
if isinstance(img_dst, MetaTensor) and isinstance(img, MetaTensor):
original_fname = img.meta[Key.FILENAME_OR_OBJ]
original_fname = img.meta.get(Key.FILENAME_OR_OBJ, "resample_to_match_source")
wyli marked this conversation as resolved.
Show resolved Hide resolved
img.meta = deepcopy(img_dst.meta)
img.meta[Key.FILENAME_OR_OBJ] = original_fname # keep the original name, the others are overwritten

Expand Down
11 changes: 10 additions & 1 deletion tests/test_resample_to_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@

import nibabel as nib
import numpy as np
import torch
from parameterized import parameterized

from monai.data import MetaTensor
from monai.data.image_reader import ITKReader, NibabelReader
from monai.data.image_writer import ITKWriter
from monai.transforms import Compose, EnsureChannelFirstd, LoadImaged, ResampleToMatch, SaveImaged
from monai.transforms import Compose, EnsureChannelFirstd, LoadImaged, ResampleToMatch, SaveImage, SaveImaged
from monai.utils import optional_import
from tests.utils import assert_allclose, download_url_or_skip_test, testing_data_config

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

def test_no_name(self):
img_1 = MetaTensor(torch.zeros(1, 2, 2, 2))
img_2 = MetaTensor(torch.zeros(1, 3, 3, 3))
im_mod = ResampleToMatch()(img_1, img_2)
self.assertEqual(im_mod.meta["filename_or_obj"], "resample_to_match_source")
SaveImage(output_dir=self.tmpdir, output_postfix="", separate_folder=False, resample=False)(im_mod)


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