|
| 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 | +import unittest |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import torch |
| 16 | + |
| 17 | +from monai.transforms import convert_to_contiguous |
| 18 | +from tests.utils import assert_allclose |
| 19 | + |
| 20 | + |
| 21 | +class TestToContiguous(unittest.TestCase): |
| 22 | + def test_decollation_dict(self): |
| 23 | + tochange = np.moveaxis(np.zeros((2, 3, 4)), 0, -1) |
| 24 | + test_dict = {"test_key": [[1]], 0: np.array(0), 1: np.array([0]), "nested": {"nested": [tochange]}} |
| 25 | + output = convert_to_contiguous(test_dict) |
| 26 | + self.assertEqual(output["test_key"], [[1]]) |
| 27 | + assert_allclose(output[0], np.array(0)) |
| 28 | + assert_allclose(output[1], np.array([0])) |
| 29 | + self.assertTrue(output["nested"]["nested"][0].flags.c_contiguous) |
| 30 | + |
| 31 | + def test_decollation_seq(self): |
| 32 | + tochange = torch.zeros(2, 3, 4).transpose(0, 1) |
| 33 | + test_dict = [[[1]], np.array(0), np.array([0]), torch.tensor(1.0), [[tochange]], "test_string"] |
| 34 | + output = convert_to_contiguous(test_dict) |
| 35 | + self.assertEqual(output[0], [[1]]) |
| 36 | + assert_allclose(output[1], np.array(0)) |
| 37 | + assert_allclose(output[2], np.array([0])) |
| 38 | + assert_allclose(output[3], torch.tensor(1.0)) |
| 39 | + self.assertTrue(output[4][0][0].is_contiguous()) |
| 40 | + self.assertEqual(output[5], "test_string") |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + unittest.main() |
0 commit comments