|
| 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 TestComputeShapeOffsetRegression(unittest.TestCase): |
| 23 | + """Regression tests for `compute_shape_offset` input-shape handling.""" |
| 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 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + unittest.main() |
0 commit comments