From 4b0e5336b71a434821bc4d6ff31bd2ceb37b6116 Mon Sep 17 00:00:00 2001 From: Harrisaint Date: Tue, 17 Mar 2026 03:59:48 -0400 Subject: [PATCH 1/6] Fix compute_shape_offset non-tuple indexing for PyTorch >=2.9 Signed-off-by: Harrisaint --- monai/data/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/data/utils.py b/monai/data/utils.py index 4e5a3bd7f6..d548ed7248 100644 --- a/monai/data/utils.py +++ b/monai/data/utils.py @@ -881,7 +881,7 @@ def compute_shape_offset( Default is False, using option 1 to compute the shape and offset. """ - shape = np.array(spatial_shape, copy=True, dtype=float) + shape = np.array(tuple(spatial_shape), copy=True, dtype=float) sr = len(shape) in_affine_ = convert_data_type(to_affine_nd(sr, in_affine), np.ndarray)[0] out_affine_ = convert_data_type(to_affine_nd(sr, out_affine), np.ndarray)[0] From d18cfc21fca3ad0a32d87cc1f886670747c29464 Mon Sep 17 00:00:00 2001 From: Harrisaint Date: Tue, 17 Mar 2026 04:45:21 -0400 Subject: [PATCH 2/6] Add regression test for compute_shape_offset #8775 Signed-off-by: Harrisaint --- tests/data/utils/test_compute_shape_offset.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/data/utils/test_compute_shape_offset.py diff --git a/tests/data/utils/test_compute_shape_offset.py b/tests/data/utils/test_compute_shape_offset.py new file mode 100644 index 0000000000..b789433769 --- /dev/null +++ b/tests/data/utils/test_compute_shape_offset.py @@ -0,0 +1,20 @@ +import torch +import unittest +import numpy as np +from monai.data.utils import compute_shape_offset + +class TestComputeShapeOffsetRegression(unittest.TestCase): + def test_pytorch_size_input(self): + # 1 Create a PyTorch Size object (which triggered the original bug) + spatial_shape = torch.Size([10, 10, 10]) + in_affine = np.eye(4) + out_affine = np.eye(4) + + # 2 Feed it into the function + shape, offset = compute_shape_offset(spatial_shape, in_affine, out_affine) + + # 3 Prove it successfully processed the shape by checking its length + self.assertEqual(len(shape), 3) + +if __name__ == "__main__": + unittest.main() From 680211ade537ace66571d66949082610cb0800aa Mon Sep 17 00:00:00 2001 From: Harrisaint Date: Tue, 17 Mar 2026 09:47:59 -0400 Subject: [PATCH 3/6] Add missing copyright license header Signed-off-by: Harrisaint --- tests/data/utils/test_compute_shape_offset.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/data/utils/test_compute_shape_offset.py b/tests/data/utils/test_compute_shape_offset.py index b789433769..d65f7bdd57 100644 --- a/tests/data/utils/test_compute_shape_offset.py +++ b/tests/data/utils/test_compute_shape_offset.py @@ -1,3 +1,14 @@ +# Copyright (c) MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import torch import unittest import numpy as np From a480b01a49e4be1e33fa0ab5a48c998eb06c4493 Mon Sep 17 00:00:00 2001 From: Harrisaint Date: Tue, 17 Mar 2026 10:56:56 -0400 Subject: [PATCH 4/6] Fix import sorting and formatting Signed-off-by: Harrisaint --- tests/data/utils/test_compute_shape_offset.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/data/utils/test_compute_shape_offset.py b/tests/data/utils/test_compute_shape_offset.py index d65f7bdd57..370a16a284 100644 --- a/tests/data/utils/test_compute_shape_offset.py +++ b/tests/data/utils/test_compute_shape_offset.py @@ -9,11 +9,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import torch import unittest + import numpy as np +import torch + from monai.data.utils import compute_shape_offset + class TestComputeShapeOffsetRegression(unittest.TestCase): def test_pytorch_size_input(self): # 1 Create a PyTorch Size object (which triggered the original bug) From 8309c22a4cfebb5e64d864c45832b66d6743584e Mon Sep 17 00:00:00 2001 From: Harrisaint Date: Tue, 17 Mar 2026 11:28:47 -0400 Subject: [PATCH 5/6] Fix import sorting and add annotations future import Signed-off-by: Harrisaint --- tests/data/utils/test_compute_shape_offset.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/data/utils/test_compute_shape_offset.py b/tests/data/utils/test_compute_shape_offset.py index 370a16a284..13e800a4db 100644 --- a/tests/data/utils/test_compute_shape_offset.py +++ b/tests/data/utils/test_compute_shape_offset.py @@ -9,6 +9,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import unittest import numpy as np @@ -19,16 +21,17 @@ class TestComputeShapeOffsetRegression(unittest.TestCase): def test_pytorch_size_input(self): - # 1 Create a PyTorch Size object (which triggered the original bug) + # 1. Create a PyTorch Size object (which triggered the original bug) spatial_shape = torch.Size([10, 10, 10]) in_affine = np.eye(4) out_affine = np.eye(4) - # 2 Feed it into the function + # 2. Feed it into the function shape, offset = compute_shape_offset(spatial_shape, in_affine, out_affine) - # 3 Prove it successfully processed the shape by checking its length + # 3. Prove it successfully processed the shape by checking its length self.assertEqual(len(shape), 3) + if __name__ == "__main__": unittest.main() From 17c0e23e6b75106af2337addcfa924cd32f5ccba Mon Sep 17 00:00:00 2001 From: Harrisaint Date: Tue, 17 Mar 2026 13:01:44 -0400 Subject: [PATCH 6/6] Add missing docstrings to regression test Signed-off-by: Harrisaint --- tests/data/utils/test_compute_shape_offset.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/data/utils/test_compute_shape_offset.py b/tests/data/utils/test_compute_shape_offset.py index 13e800a4db..5d6d714306 100644 --- a/tests/data/utils/test_compute_shape_offset.py +++ b/tests/data/utils/test_compute_shape_offset.py @@ -20,7 +20,17 @@ class TestComputeShapeOffsetRegression(unittest.TestCase): + """Regression tests for `compute_shape_offset` input-shape handling.""" + def test_pytorch_size_input(self): + """Validate `torch.Size` input produces expected shape and offset. + + Returns: + None. + + Raises: + AssertionError: If computed shape/offset are not as expected. + """ # 1. Create a PyTorch Size object (which triggered the original bug) spatial_shape = torch.Size([10, 10, 10]) in_affine = np.eye(4)