|
| 1 | +# Copyright 2020 - 2021 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 | +import unittest |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import torch |
| 16 | + |
| 17 | +from monai.transforms import ToTensor |
| 18 | + |
| 19 | + |
| 20 | +class TestToTensor(unittest.TestCase): |
| 21 | + def test_array_input(self): |
| 22 | + for test_data in ([[1, 2], [3, 4]], np.array([[1, 2], [3, 4]]), torch.as_tensor([[1, 2], [3, 4]])): |
| 23 | + result = ToTensor()(test_data) |
| 24 | + torch.testing.assert_allclose(result, test_data) |
| 25 | + self.assertTupleEqual(result.shape, (2, 2)) |
| 26 | + |
| 27 | + def test_single_input(self): |
| 28 | + for test_data in (5, np.asarray(5), torch.tensor(5)): |
| 29 | + result = ToTensor()(test_data) |
| 30 | + torch.testing.assert_allclose(result, test_data) |
| 31 | + self.assertEqual(result.ndim, 0) |
| 32 | + |
| 33 | + |
| 34 | +if __name__ == "__main__": |
| 35 | + unittest.main() |
0 commit comments