From 56e5259ee95b7736793896d146014e154c494892 Mon Sep 17 00:00:00 2001 From: Richard Brown <33289025+rijobro@users.noreply.github.com> Date: Wed, 19 Jan 2022 16:06:59 +0000 Subject: [PATCH 1/3] allow test image creation to save to different folders and with different file names Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> --- tests/test_make_nifti.py | 39 +++++++++++++++++++++++++++++++++++++++ tests/utils.py | 23 ++++++++++++++++++----- 2 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 tests/test_make_nifti.py diff --git a/tests/test_make_nifti.py b/tests/test_make_nifti.py new file mode 100644 index 0000000000..88aa9bc45a --- /dev/null +++ b/tests/test_make_nifti.py @@ -0,0 +1,39 @@ +# Copyright (c) MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import tempfile +import unittest + +import numpy as np +import torch +from parameterized import parameterized + +from monai.data.synthetic import create_test_image_2d +from tests.utils import make_nifti_image + +TESTS = [] +for affine in (None, np.eye(4), torch.eye(4)): + for dir in (None, tempfile.mkdtemp()): + for fname in (None, "fname"): + TESTS.append([{"affine": affine, "dir": dir, "fname": fname}]) + + +class TestMakeNifti(unittest.TestCase): + @parameterized.expand(TESTS) + def test_make_nifti(self, params): + im, _ = create_test_image_2d(100, 88) + created_file = make_nifti_image(im, verbose=True, **params) + self.assertTrue(os.path.isfile(created_file)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/utils.py b/tests/utils.py index f96f659353..8a07736d6c 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -240,7 +240,7 @@ def has_cupy(): HAS_CUPY = has_cupy() -def make_nifti_image(array: NdarrayOrTensor, affine=None): +def make_nifti_image(array: NdarrayOrTensor, affine=None, dir=None, fname=None, suffix=".nii.gz", verbose=False): """ Create a temporary nifti image on the disk and return the image name. User is responsible for deleting the temporary file when done with it. @@ -253,10 +253,23 @@ def make_nifti_image(array: NdarrayOrTensor, affine=None): affine = np.eye(4) test_image = nib.Nifti1Image(array, affine) - temp_f, image_name = tempfile.mkstemp(suffix=".nii.gz") - nib.save(test_image, image_name) - os.close(temp_f) - return image_name + # if dir not given, create random. Else, make sure it exists. + if dir is None: + dir = tempfile.mkdtemp() + else: + os.makedirs(dir, exist_ok=True) + + # If fname not given, get random one. Else, concat dir, fname and suffix. + if fname is None: + temp_f, fname = tempfile.mkstemp(suffix=suffix, dir=dir) + os.close(temp_f) + else: + fname = os.path.join(dir, fname + suffix) + + nib.save(test_image, fname) + if verbose: + print(f"File written: {fname}.") + return fname def make_rand_affine(ndim: int = 3, random_state: Optional[np.random.RandomState] = None): From 32d9c63a21f98a17beacdcf150ff5248485f33c8 Mon Sep 17 00:00:00 2001 From: Richard Brown <33289025+rijobro@users.noreply.github.com> Date: Wed, 19 Jan 2022 17:26:24 +0000 Subject: [PATCH 2/3] skip unless nibabel installed Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> --- tests/test_make_nifti.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_make_nifti.py b/tests/test_make_nifti.py index 88aa9bc45a..473447d5ba 100644 --- a/tests/test_make_nifti.py +++ b/tests/test_make_nifti.py @@ -19,6 +19,9 @@ from monai.data.synthetic import create_test_image_2d from tests.utils import make_nifti_image +from monai.utils import optional_import + +_, has_nib = optional_import("nibabel") TESTS = [] for affine in (None, np.eye(4), torch.eye(4)): @@ -27,6 +30,7 @@ TESTS.append([{"affine": affine, "dir": dir, "fname": fname}]) +@unittest.skipUnless(has_nib, "Requires nibabel") class TestMakeNifti(unittest.TestCase): @parameterized.expand(TESTS) def test_make_nifti(self, params): From 166c388fe2e52e160851a8cc7d0823fba9b76ebe Mon Sep 17 00:00:00 2001 From: monai-bot Date: Wed, 19 Jan 2022 17:38:29 +0000 Subject: [PATCH 3/3] [MONAI] python code formatting Signed-off-by: monai-bot --- tests/test_make_nifti.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_make_nifti.py b/tests/test_make_nifti.py index 473447d5ba..951f079764 100644 --- a/tests/test_make_nifti.py +++ b/tests/test_make_nifti.py @@ -18,8 +18,8 @@ from parameterized import parameterized from monai.data.synthetic import create_test_image_2d -from tests.utils import make_nifti_image from monai.utils import optional_import +from tests.utils import make_nifti_image _, has_nib = optional_import("nibabel")