Skip to content

Commit cc92126

Browse files
Harrisaintericspod
andauthored
Fix compute_shape_offset non-tuple indexing for PyTorch >=2.9 (#8776)
Fixes #8775 . ### Description This PR resolves issue #8775 by casting spatial_shape to a tuple inside compute_shape_offset to prevent breaking changes in PyTorch >= 2.9. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Harrisaint <hgmartin1116@gmail.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent deb3f98 commit cc92126

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

monai/data/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ def compute_shape_offset(
881881
Default is False, using option 1 to compute the shape and offset.
882882
883883
"""
884-
shape = np.array(spatial_shape, copy=True, dtype=float)
884+
shape = np.array(tuple(spatial_shape), copy=True, dtype=float)
885885
sr = len(shape)
886886
in_affine_ = convert_data_type(to_affine_nd(sr, in_affine), np.ndarray)[0]
887887
out_affine_ = convert_data_type(to_affine_nd(sr, out_affine), np.ndarray)[0]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

Comments
 (0)