|
| 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 json |
| 15 | +import os |
| 16 | +import shutil |
| 17 | +import tempfile |
| 18 | +import unittest |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +from parameterized import parameterized |
| 22 | + |
| 23 | +from monai.data import DataLoader, Dataset |
| 24 | +from monai.transforms import Compose, LoadImage, SaveImage, WriteFileMapping |
| 25 | +from monai.utils import optional_import |
| 26 | + |
| 27 | +nib, has_nib = optional_import("nibabel") |
| 28 | + |
| 29 | + |
| 30 | +def create_input_file(temp_dir, name): |
| 31 | + test_image = np.random.rand(128, 128, 128) |
| 32 | + output_ext = ".nii.gz" |
| 33 | + input_file = os.path.join(temp_dir, name + output_ext) |
| 34 | + nib.save(nib.Nifti1Image(test_image, np.eye(4)), input_file) |
| 35 | + return input_file |
| 36 | + |
| 37 | + |
| 38 | +def create_transform(temp_dir, mapping_file_path, savepath_in_metadict=True): |
| 39 | + return Compose( |
| 40 | + [ |
| 41 | + LoadImage(image_only=True), |
| 42 | + SaveImage(output_dir=temp_dir, output_ext=".nii.gz", savepath_in_metadict=savepath_in_metadict), |
| 43 | + WriteFileMapping(mapping_file_path=mapping_file_path), |
| 44 | + ] |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +@unittest.skipUnless(has_nib, "nibabel required") |
| 49 | +class TestWriteFileMapping(unittest.TestCase): |
| 50 | + def setUp(self): |
| 51 | + self.temp_dir = tempfile.mkdtemp() |
| 52 | + |
| 53 | + def tearDown(self): |
| 54 | + shutil.rmtree(self.temp_dir) |
| 55 | + |
| 56 | + @parameterized.expand([(True,), (False,)]) |
| 57 | + def test_mapping_file(self, savepath_in_metadict): |
| 58 | + mapping_file_path = os.path.join(self.temp_dir, "mapping.json") |
| 59 | + name = "test_image" |
| 60 | + input_file = create_input_file(self.temp_dir, name) |
| 61 | + output_file = os.path.join(self.temp_dir, name, name + "_trans.nii.gz") |
| 62 | + |
| 63 | + transform = create_transform(self.temp_dir, mapping_file_path, savepath_in_metadict) |
| 64 | + |
| 65 | + if savepath_in_metadict: |
| 66 | + transform(input_file) |
| 67 | + self.assertTrue(os.path.exists(mapping_file_path)) |
| 68 | + with open(mapping_file_path) as f: |
| 69 | + mapping_data = json.load(f) |
| 70 | + self.assertEqual(len(mapping_data), 1) |
| 71 | + self.assertEqual(mapping_data[0]["input"], input_file) |
| 72 | + self.assertEqual(mapping_data[0]["output"], output_file) |
| 73 | + else: |
| 74 | + with self.assertRaises(RuntimeError) as cm: |
| 75 | + transform(input_file) |
| 76 | + cause_exception = cm.exception.__cause__ |
| 77 | + self.assertIsInstance(cause_exception, KeyError) |
| 78 | + self.assertIn( |
| 79 | + "Missing 'saved_to' key in metadata. Check SaveImage argument 'savepath_in_metadict' is True.", |
| 80 | + str(cause_exception), |
| 81 | + ) |
| 82 | + |
| 83 | + def test_multiprocess_mapping_file(self): |
| 84 | + num_images = 50 |
| 85 | + |
| 86 | + single_mapping_file = os.path.join(self.temp_dir, "single_mapping.json") |
| 87 | + multi_mapping_file = os.path.join(self.temp_dir, "multi_mapping.json") |
| 88 | + |
| 89 | + data = [create_input_file(self.temp_dir, f"test_image_{i}") for i in range(num_images)] |
| 90 | + |
| 91 | + # single process |
| 92 | + single_transform = create_transform(self.temp_dir, single_mapping_file) |
| 93 | + single_dataset = Dataset(data=data, transform=single_transform) |
| 94 | + single_loader = DataLoader(single_dataset, batch_size=1, num_workers=0, shuffle=True) |
| 95 | + for _ in single_loader: |
| 96 | + pass |
| 97 | + |
| 98 | + # multiple processes |
| 99 | + multi_transform = create_transform(self.temp_dir, multi_mapping_file) |
| 100 | + multi_dataset = Dataset(data=data, transform=multi_transform) |
| 101 | + multi_loader = DataLoader(multi_dataset, batch_size=4, num_workers=3, shuffle=True) |
| 102 | + for _ in multi_loader: |
| 103 | + pass |
| 104 | + |
| 105 | + with open(single_mapping_file) as f: |
| 106 | + single_mapping_data = json.load(f) |
| 107 | + with open(multi_mapping_file) as f: |
| 108 | + multi_mapping_data = json.load(f) |
| 109 | + |
| 110 | + single_set = {(entry["input"], entry["output"]) for entry in single_mapping_data} |
| 111 | + multi_set = {(entry["input"], entry["output"]) for entry in multi_mapping_data} |
| 112 | + |
| 113 | + self.assertEqual(single_set, multi_set) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + unittest.main() |
0 commit comments