Skip to content

Fix clipping boxes with large coordinates - #9047

Open
Rajioba1 wants to merge 2 commits into
Project-MONAI:devfrom
Rajioba1:9045-fix-large-coordinate-box-clipping
Open

Fix clipping boxes with large coordinates#9047
Rajioba1 wants to merge 2 commits into
Project-MONAI:devfrom
Rajioba1:9045-fix-large-coordinate-box-clipping

Conversation

@Rajioba1

@Rajioba1 Rajioba1 commented Aug 3, 2026

Copy link
Copy Markdown

Fixes #9045.

Description

spatial_crop_boxes converted crop ROI bounds to torch.int16, which overflows for coordinates above 32767. This could cause valid boxes in large images to be clamped incorrectly and silently removed when remove_empty=True.

This PR keeps the ROI bounds in the same tensor dtype as the boxes during clipping and adds a regression test covering both spatial_crop_boxes and the public clip_boxes_to_image path for large coordinates.

Types of changes

  • 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.

Local validation:

  • python -m tests.data.test_box_utils
  • python -m ruff check monai/data/box_utils.py tests/data/test_box_utils.py

black and isort were not installed in my local Python environment, so I could not run those checks directly.

Signed-off-by: Rajioba1 <raji.lukmon@gmail.com>
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: cee6a819-a05c-490f-80b6-b8add3dca69b

📥 Commits

Reviewing files that changed from the base of the PR and between 9128cd9 and 5ff08d9.

📒 Files selected for processing (1)
  • tests/data/test_box_utils.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/data/test_box_utils.py

📝 Walkthrough

Walkthrough

spatial_crop_boxes now preserves the dtype of converted ROI boundaries instead of forcing torch.int16. Tests cover large float32 coordinates, cropped box coordinates, retained boxes, and clip_boxes_to_image behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the fix for clipping boxes with large coordinates.
Description check ✅ Passed The description explains the bug, fix, tests, validation, and change type with the required template sections.
Linked Issues check ✅ Passed The implementation and regression tests address issue #9045 by preventing int16 overflow and retaining valid large-coordinate boxes.
Out of Scope Changes check ✅ Passed All code and test changes directly support the large-coordinate clipping fix described in issue #9045.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

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.

🧹 Nitpick comments (1)
tests/data/test_box_utils.py (1)

273-285: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a docstring to the new test.

test_large_coordinates_are_not_dropped is a new Python definition without a docstring.

Proposed fix
     def test_large_coordinates_are_not_dropped(self):
+        """Verify large coordinates survive cropping and image clipping."""

As per path instructions, every Python definition must have a Google-style docstring.

🤖 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/data/test_box_utils.py` around lines 273 - 285, Add a Google-style
docstring to the test_large_coordinates_are_not_dropped test method describing
that large-coordinate bounding boxes remain preserved by spatial cropping and
image clipping.

Source: Path instructions

🤖 Prompt for all review comments with 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.

Nitpick comments:
In `@tests/data/test_box_utils.py`:
- Around line 273-285: Add a Google-style docstring to the
test_large_coordinates_are_not_dropped test method describing that
large-coordinate bounding boxes remain preserved by spatial cropping and image
clipping.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 36d185bf-9b8e-4507-8de0-75c9c2397077

📥 Commits

Reviewing files that changed from the base of the PR and between 8690ae7 and 9128cd9.

📒 Files selected for processing (2)
  • monai/data/box_utils.py
  • tests/data/test_box_utils.py

Signed-off-by: Rajioba1 <raji.lukmon@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

clip_boxes_to_image can drop valid boxes for large coordinates

1 participant