|
| 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 | +import torch |
| 17 | + |
| 18 | +from monai.apps.reconstruction.transforms.dictionary import ExtractDataKeyFromMetaKeyd |
| 19 | +from monai.data import MetaTensor |
| 20 | + |
| 21 | + |
| 22 | +class TestExtractDataKeyFromMetaKeyd(unittest.TestCase): |
| 23 | + """Tests for ExtractDataKeyFromMetaKeyd covering both dict-based and MetaTensor-based metadata.""" |
| 24 | + |
| 25 | + def test_extract_from_dict(self): |
| 26 | + """Test extracting keys from a plain metadata dictionary (image_only=False scenario).""" |
| 27 | + data = { |
| 28 | + "image": torch.zeros(1, 2, 2), |
| 29 | + "image_meta_dict": {"filename_or_obj": "image.nii", "spatial_shape": [2, 2]}, |
| 30 | + } |
| 31 | + transform = ExtractDataKeyFromMetaKeyd(keys="filename_or_obj", meta_key="image_meta_dict") |
| 32 | + result = transform(data) |
| 33 | + self.assertIn("filename_or_obj", result) |
| 34 | + self.assertEqual(result["filename_or_obj"], "image.nii") |
| 35 | + self.assertEqual(result["image_meta_dict"]["filename_or_obj"], result["filename_or_obj"]) |
| 36 | + |
| 37 | + def test_extract_from_metatensor(self): |
| 38 | + """Test extracting keys from a MetaTensor's .meta attribute (image_only=True scenario).""" |
| 39 | + mt = MetaTensor(torch.zeros(1, 2, 2)) |
| 40 | + mt.meta["filename_or_obj"] = "image.nii" |
| 41 | + mt.meta["spatial_shape"] = [2, 2] |
| 42 | + data = {"image": mt} |
| 43 | + transform = ExtractDataKeyFromMetaKeyd(keys="filename_or_obj", meta_key="image") |
| 44 | + result = transform(data) |
| 45 | + self.assertIn("filename_or_obj", result) |
| 46 | + self.assertEqual(result["filename_or_obj"], "image.nii") |
| 47 | + self.assertEqual(result["image"].meta["filename_or_obj"], result["filename_or_obj"]) |
| 48 | + |
| 49 | + def test_extract_multiple_keys_from_metatensor(self): |
| 50 | + """Test extracting multiple keys from a MetaTensor.""" |
| 51 | + mt = MetaTensor(torch.zeros(1, 2, 2)) |
| 52 | + mt.meta["filename_or_obj"] = "image.nii" |
| 53 | + mt.meta["spatial_shape"] = [2, 2] |
| 54 | + data = {"image": mt} |
| 55 | + transform = ExtractDataKeyFromMetaKeyd(keys=["filename_or_obj", "spatial_shape"], meta_key="image") |
| 56 | + result = transform(data) |
| 57 | + self.assertIn("filename_or_obj", result) |
| 58 | + self.assertIn("spatial_shape", result) |
| 59 | + self.assertEqual(result["filename_or_obj"], "image.nii") |
| 60 | + self.assertEqual(result["spatial_shape"], [2, 2]) |
| 61 | + |
| 62 | + def test_extract_multiple_keys_from_dict(self): |
| 63 | + """Test extracting multiple keys from a plain dictionary.""" |
| 64 | + data = { |
| 65 | + "image": torch.zeros(1, 2, 2), |
| 66 | + "image_meta_dict": {"filename_or_obj": "image.nii", "spatial_shape": [2, 2]}, |
| 67 | + } |
| 68 | + transform = ExtractDataKeyFromMetaKeyd(keys=["filename_or_obj", "spatial_shape"], meta_key="image_meta_dict") |
| 69 | + result = transform(data) |
| 70 | + self.assertIn("filename_or_obj", result) |
| 71 | + self.assertIn("spatial_shape", result) |
| 72 | + self.assertEqual(result["filename_or_obj"], "image.nii") |
| 73 | + self.assertEqual(result["spatial_shape"], [2, 2]) |
| 74 | + |
| 75 | + def test_missing_key_raises(self): |
| 76 | + """Test that a missing key raises KeyError when allow_missing_keys=False.""" |
| 77 | + mt = MetaTensor(torch.zeros(1, 2, 2)) |
| 78 | + mt.meta["filename_or_obj"] = "image.nii" |
| 79 | + data = {"image": mt} |
| 80 | + transform = ExtractDataKeyFromMetaKeyd(keys="nonexistent_key", meta_key="image") |
| 81 | + with self.assertRaises(KeyError): |
| 82 | + transform(data) |
| 83 | + |
| 84 | + def test_missing_key_allowed_metatensor(self): |
| 85 | + """Test that a missing key is silently skipped when allow_missing_keys=True with MetaTensor.""" |
| 86 | + mt = MetaTensor(torch.zeros(1, 2, 2)) |
| 87 | + mt.meta["filename_or_obj"] = "image.nii" |
| 88 | + data = {"image": mt} |
| 89 | + transform = ExtractDataKeyFromMetaKeyd(keys="nonexistent_key", meta_key="image", allow_missing_keys=True) |
| 90 | + result = transform(data) |
| 91 | + self.assertNotIn("nonexistent_key", result) |
| 92 | + |
| 93 | + def test_missing_key_allowed_dict(self): |
| 94 | + """Test that a missing key is silently skipped when allow_missing_keys=True with dict.""" |
| 95 | + data = {"image": torch.zeros(1, 2, 2), "image_meta_dict": {"filename_or_obj": "image.nii"}} |
| 96 | + transform = ExtractDataKeyFromMetaKeyd( |
| 97 | + keys="nonexistent_key", meta_key="image_meta_dict", allow_missing_keys=True |
| 98 | + ) |
| 99 | + result = transform(data) |
| 100 | + self.assertNotIn("nonexistent_key", result) |
| 101 | + |
| 102 | + def test_original_data_preserved_metatensor(self): |
| 103 | + """Test that the original MetaTensor remains in the data dictionary.""" |
| 104 | + mt = MetaTensor(torch.ones(1, 2, 2)) |
| 105 | + mt.meta["filename_or_obj"] = "image.nii" |
| 106 | + data = {"image": mt} |
| 107 | + transform = ExtractDataKeyFromMetaKeyd(keys="filename_or_obj", meta_key="image") |
| 108 | + result = transform(data) |
| 109 | + self.assertIn("image", result) |
| 110 | + self.assertIsInstance(result["image"], MetaTensor) |
| 111 | + self.assertTrue(torch.equal(result["image"], mt)) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + unittest.main() |
0 commit comments