Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion monai/transforms/croppad/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def inverse(self, data: MetaTensor) -> MetaTensor:
padded = transform[TraceKeys.EXTRA_INFO]["padded"]
if padded[0][0] > 0 or padded[0][1] > 0: # slicing the channel dimension
s = padded[0][0]
e = min(max(padded[0][1], s + 1), len(data))
e = min(padded[0][1], len(data) - s)
data = data[s : len(data) - e] # type: ignore
roi_start = [i[0] for i in padded[1:]]
roi_end = [i - j[1] for i, j in zip(data.shape[1:], padded[1:])]
Expand Down
44 changes: 44 additions & 0 deletions tests/transforms/croppad/test_pad_inverse_channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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.
from __future__ import annotations

import unittest

import torch
from parameterized.parameterized import parameterized

from monai.data import MetaTensor, set_track_meta
from monai.transforms.croppad.array import Pad

# "symmetric" routes through the NumPy backend, which pads the channel dim.
CHANNEL_PAD_CASES = [
[[(2, 0), (0, 0), (0, 0)]],
[[(0, 2), (0, 0), (0, 0)]],
[[(2, 2), (0, 0), (0, 0)]],
[[(1, 3), (1, 1), (0, 2)]],
]


class TestPadInverseChannel(unittest.TestCase):
@parameterized.expand(CHANNEL_PAD_CASES)
def test_inverse_roundtrip_channel_pad(self, to_pad):
set_track_meta(True)
img = MetaTensor(torch.arange(3 * 4 * 4).reshape(3, 4, 4).float())
pad = Pad(to_pad=to_pad, mode="symmetric")
padded = pad(img.clone())
self.assertEqual(padded.shape[0], img.shape[0] + to_pad[0][0] + to_pad[0][1])
inv = pad.inverse(padded)
self.assertEqual(inv.shape, img.shape)
self.assertTrue(torch.equal(inv.as_tensor(), img.as_tensor()))
Comment on lines +32 to +40

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add docstring to test method.

Per coding guidelines, all definitions require Google-style docstrings describing parameters and purpose.

📝 Suggested docstring
     `@parameterized.expand`(CHANNEL_PAD_CASES)
     def test_inverse_roundtrip_channel_pad(self, to_pad):
+        """Test Pad.inverse correctly restores original tensor after channel-dimension padding.
+
+        Args:
+            to_pad: Padding specification [(ch_pad), (h_pad), (w_pad)].
+        """
         set_track_meta(True)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_inverse_roundtrip_channel_pad(self, to_pad):
set_track_meta(True)
img = MetaTensor(torch.arange(3 * 4 * 4).reshape(3, 4, 4).float())
pad = Pad(to_pad=to_pad, mode="symmetric")
padded = pad(img.clone())
self.assertEqual(padded.shape[0], img.shape[0] + to_pad[0][0] + to_pad[0][1])
inv = pad.inverse(padded)
self.assertEqual(inv.shape, img.shape)
self.assertTrue(torch.equal(inv.as_tensor(), img.as_tensor()))
def test_inverse_roundtrip_channel_pad(self, to_pad):
"""Test Pad.inverse correctly restores original tensor after channel-dimension padding.
Args:
to_pad: Padding specification [(ch_pad), (h_pad), (w_pad)].
"""
set_track_meta(True)
img = MetaTensor(torch.arange(3 * 4 * 4).reshape(3, 4, 4).float())
pad = Pad(to_pad=to_pad, mode="symmetric")
padded = pad(img.clone())
self.assertEqual(padded.shape[0], img.shape[0] + to_pad[0][0] + to_pad[0][1])
inv = pad.inverse(padded)
self.assertEqual(inv.shape, img.shape)
self.assertTrue(torch.equal(inv.as_tensor(), img.as_tensor()))
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/transforms/croppad/test_pad_inverse_channel.py` around lines 32 - 40,
Add a Google-style docstring to the test method
test_inverse_roundtrip_channel_pad describing the test's purpose and its
parameter to_pad (e.g., what padding configuration is being tested and the
expected round-trip behavior), placed immediately below the def line; ensure it
follows Google style with a short description and an Args section for to_pad.

Source: Coding guidelines



if __name__ == "__main__":
unittest.main()
Loading