|
| 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 | + |
| 19 | +from monai.data.utils import compute_shape_offset |
| 20 | + |
| 21 | + |
| 22 | +class TestComputeShapeOffset(unittest.TestCase): |
| 23 | + """Unit tests for :func:`monai.data.utils.compute_shape_offset`.""" |
| 24 | + |
| 25 | + def test_pytorch_size_input(self): |
| 26 | + """Validate `torch.Size` input produces expected shape and offset. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + None. |
| 30 | +
|
| 31 | + Raises: |
| 32 | + AssertionError: If computed shape/offset are not as expected. |
| 33 | + """ |
| 34 | + # 1. Create a PyTorch Size object (which triggered the original bug) |
| 35 | + spatial_shape = torch.Size([10, 10, 10]) |
| 36 | + in_affine = np.eye(4) |
| 37 | + out_affine = np.eye(4) |
| 38 | + |
| 39 | + # 2. Feed it into the function |
| 40 | + shape, offset = compute_shape_offset(spatial_shape, in_affine, out_affine) |
| 41 | + |
| 42 | + # 3. Prove it successfully processed the shape by checking its length |
| 43 | + self.assertEqual(len(shape), 3) |
| 44 | + |
| 45 | + def setUp(self): |
| 46 | + """Set up a 4x4 identity affine used across all test cases.""" |
| 47 | + self.affine = np.eye(4) |
| 48 | + |
| 49 | + def test_numpy_array_input(self): |
| 50 | + """Verify compute_shape_offset accepts a numpy array as spatial_shape.""" |
| 51 | + shape = np.array([64, 64, 64]) |
| 52 | + out_shape, _ = compute_shape_offset(shape, self.affine, self.affine) |
| 53 | + self.assertEqual(len(out_shape), 3) |
| 54 | + |
| 55 | + def test_list_input(self): |
| 56 | + """Verify compute_shape_offset accepts a plain list as spatial_shape.""" |
| 57 | + shape = [64, 64, 64] |
| 58 | + out_shape, _ = compute_shape_offset(shape, self.affine, self.affine) |
| 59 | + self.assertEqual(len(out_shape), 3) |
| 60 | + |
| 61 | + def test_torch_tensor_input(self): |
| 62 | + """Verify compute_shape_offset accepts a torch.Tensor as spatial_shape. |
| 63 | +
|
| 64 | + This path broke in PyTorch >= 2.9 because np.array() relied on the |
| 65 | + non-tuple sequence indexing protocol that PyTorch removed. Wrapping with |
| 66 | + tuple() fixes it. |
| 67 | + """ |
| 68 | + shape = torch.tensor([64, 64, 64]) |
| 69 | + out_shape, _ = compute_shape_offset(shape, self.affine, self.affine) |
| 70 | + self.assertEqual(len(out_shape), 3) |
| 71 | + |
| 72 | + def test_identity_affines_preserve_shape(self): |
| 73 | + """Verify that identity in/out affines produce an output shape matching the input.""" |
| 74 | + shape = torch.tensor([32, 48, 16]) |
| 75 | + out_shape, _ = compute_shape_offset(shape, self.affine, self.affine) |
| 76 | + np.testing.assert_allclose(np.array(out_shape, dtype=float), shape.numpy().astype(float), atol=1e-5) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + unittest.main() |
0 commit comments