|
| 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 numpy as np |
| 17 | +import torch |
| 18 | +from parameterized import parameterized |
| 19 | + |
| 20 | +from monai.utils import unsqueeze_left, unsqueeze_right |
| 21 | + |
| 22 | +RIGHT_CASES = [(np.random.rand(3, 4), 5, (3, 4, 1, 1, 1)), (torch.rand(3, 4), 5, (3, 4, 1, 1, 1))] |
| 23 | + |
| 24 | +LEFT_CASES = [(np.random.rand(3, 4), 5, (1, 1, 1, 3, 4)), (torch.rand(3, 4), 5, (1, 1, 1, 3, 4))] |
| 25 | + |
| 26 | +ALL_CASES = [ |
| 27 | + (np.random.rand(3, 4), 2, (3, 4)), |
| 28 | + (np.random.rand(3, 4), 0, (3, 4)), |
| 29 | + (np.random.rand(3, 4), -1, (3, 4)), |
| 30 | + (np.array(3), 4, (1, 1, 1, 1)), |
| 31 | + (np.array(3), 0, ()), |
| 32 | + (torch.rand(3, 4), 2, (3, 4)), |
| 33 | + (torch.rand(3, 4), 0, (3, 4)), |
| 34 | + (torch.rand(3, 4), -1, (3, 4)), |
| 35 | + (torch.tensor(3), 4, (1, 1, 1, 1)), |
| 36 | + (torch.tensor(3), 0, ()), |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +class TestUnsqueeze(unittest.TestCase): |
| 41 | + @parameterized.expand(RIGHT_CASES + ALL_CASES) |
| 42 | + def test_unsqueeze_right(self, arr, ndim, shape): |
| 43 | + self.assertEqual(unsqueeze_right(arr, ndim).shape, shape) |
| 44 | + |
| 45 | + @parameterized.expand(LEFT_CASES + ALL_CASES) |
| 46 | + def test_unsqueeze_left(self, arr, ndim, shape): |
| 47 | + self.assertEqual(unsqueeze_left(arr, ndim).shape, shape) |
0 commit comments