Skip to content

Commit 47b16cc

Browse files
committed
Enhance ExtractDataKeyFromMetaKeyd to work with MetaTensor
Signed-off-by: Adama Sorho <[email protected]>
1 parent b58e883 commit 47b16cc

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

monai/apps/reconstruction/transforms/dictionary.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from monai.apps.reconstruction.transforms.array import EquispacedKspaceMask, RandomKspaceMask
2121
from monai.config import DtypeLike, KeysCollection
2222
from monai.config.type_definitions import NdarrayOrTensor
23+
from monai.data import MetaTensor
2324
from monai.transforms import InvertibleTransform
2425
from monai.transforms.croppad.array import SpatialCrop
2526
from monai.transforms.intensity.array import NormalizeIntensity
@@ -57,15 +58,25 @@ def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, T
5758
Returns:
5859
the new data dictionary
5960
"""
61+
6062
d = dict(data)
63+
64+
if isinstance(d[self.meta_key], MetaTensor):
65+
# meta tensor
66+
meta = d[self.meta_key].meta
67+
else:
68+
# meta dict
69+
meta = d[self.meta_key]
70+
6171
for key in self.keys:
62-
if key in d[self.meta_key]:
63-
d[key] = d[self.meta_key][key] # type: ignore
72+
if key in meta:
73+
d[key] = meta[key] # type: ignore
6474
elif not self.allow_missing_keys:
6575
raise KeyError(
6676
f"Key `{key}` of transform `{self.__class__.__name__}` was missing in the meta data"
6777
" and allow_missing_keys==False."
6878
)
79+
6980
return d # type: ignore
7081

7182

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (c) MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
from __future__ import annotations
13+
14+
import unittest
15+
16+
from monai.apps.reconstruction.transforms.dictionary import ExtractDataKeyFromMetaKeyd
17+
from monai.data import MetaTensor
18+
19+
20+
class TestExtractDataKeyFromMetaKeyd(unittest.TestCase):
21+
def test_extract_data_key_from_dic(self):
22+
data = {"image_data": MetaTensor([1, 2, 3]), "foo_meta_dict": {"filename_or_obj": "test_image.nii.gz"}}
23+
24+
extract = ExtractDataKeyFromMetaKeyd("filename_or_obj", meta_key="foo_meta_dict")
25+
result = extract(data)
26+
27+
assert data["foo_meta_dict"]["filename_or_obj"] == result["filename_or_obj"]
28+
29+
def test_extract_data_key_from_meta_tensor(self):
30+
data = {"image_data": MetaTensor([1, 2, 3], meta={"filename_or_obj": 1})}
31+
32+
extract = ExtractDataKeyFromMetaKeyd("filename_or_obj", meta_key="image_data")
33+
result = extract(data)
34+
35+
assert data["image_data"].meta["filename_or_obj"] == result["filename_or_obj"]
36+
37+
38+
if __name__ == "__main__":
39+
unittest.main()

0 commit comments

Comments
 (0)