|
| 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 | +from unittest import skipUnless |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +import torch |
| 19 | +from parameterized import parameterized |
| 20 | + |
| 21 | +from monai.transforms import RandTorchIO |
| 22 | +from monai.utils import optional_import, set_determinism |
| 23 | + |
| 24 | +_, has_torchio = optional_import("torchio") |
| 25 | + |
| 26 | +TEST_DIMS = [3, 128, 160, 160] |
| 27 | +TESTS = [ |
| 28 | + [{"name": "RandomAffine"}, torch.rand(TEST_DIMS)], |
| 29 | + [{"name": "RandomElasticDeformation"}, torch.rand(TEST_DIMS)], |
| 30 | + [{"name": "RandomAnisotropy"}, torch.rand(TEST_DIMS)], |
| 31 | + [{"name": "RandomMotion"}, torch.rand(TEST_DIMS)], |
| 32 | + [{"name": "RandomGhosting"}, torch.rand(TEST_DIMS)], |
| 33 | + [{"name": "RandomSpike"}, torch.rand(TEST_DIMS)], |
| 34 | + [{"name": "RandomBiasField"}, torch.rand(TEST_DIMS)], |
| 35 | + [{"name": "RandomBlur"}, torch.rand(TEST_DIMS)], |
| 36 | + [{"name": "RandomNoise"}, torch.rand(TEST_DIMS)], |
| 37 | + [{"name": "RandomSwap"}, torch.rand(TEST_DIMS)], |
| 38 | + [{"name": "RandomGamma"}, torch.rand(TEST_DIMS)], |
| 39 | +] |
| 40 | + |
| 41 | + |
| 42 | +@skipUnless(has_torchio, "Requires torchio") |
| 43 | +class TestRandTorchIO(unittest.TestCase): |
| 44 | + |
| 45 | + @parameterized.expand(TESTS) |
| 46 | + def test_value(self, input_param, input_data): |
| 47 | + set_determinism(seed=0) |
| 48 | + result = RandTorchIO(**input_param)(input_data) |
| 49 | + self.assertIsNotNone(result) |
| 50 | + self.assertFalse(np.array_equal(result.numpy(), input_data.numpy()), f"{input_param} failed") |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + unittest.main() |
0 commit comments