Skip to content

fix(detections): Prevent integer aspect-ratio overflow - #2729

Merged
Borda merged 6 commits into
roboflow:developfrom
aswanth-07:fix/integer-box-aspect-ratio
Oct 15, 2026
Merged

Borda merged 6 commits into
roboflow:developfrom
aswanth-07:fix/integer-box-aspect-ratio

Conversation

@aswanth-07

@aswanth-07 aswanth-07 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

Detections.box_aspect_ratio can return a negative ratio for an ordered integer box because it subtracts corners in the coordinate dtype. For example:

import numpy as np
import supervision as sv

detections = sv.Detections(xyxy=np.array([[-120, 0, 120, 20]], dtype=np.int8))
print(detections.box_aspect_ratio)  # [-0.8], expected [12.0]

Motivation and Context

I found this while checking aspect-ratio filtering on integer detections. Overflow in either width or height can make the filter select the wrong boxes. box_area already handles the same arithmetic boundary.

Changes Made

  • Widen integer coordinates smaller than 64 bits to int64 and retain Python-integer arithmetic for int64/uint64, then convert the differences to float64. This preserves narrow boxes whose origins exceed float64's exact-integer range.
  • Add regression and compatibility coverage plus an Unreleased changelog entry. Floating arithmetic, float64 ratios, zero-height NaNs and input coordinates stay unchanged.

Testing

  • pytest tests/detection/test_core.py -q: 263 passed.

  • pytest -q --cov=supervision: 5876 passed, six skipped, with 94% coverage after merging current develop. The four signed-range regressions fail against the pristine property; the other 12 focused cases pass.

  • pre-commit run --all-files: all hooks passed.

  • Additional validation: 99 exact pristine/patch compatibility checks, 16 integer-range checks against Python Fraction arithmetic, and 24 previous-head parity checks for extreme, strided and read-only integer arrays.

  • Added/updated tests, and the full suite passes locally

  • Updated docs (docstrings / mkdocs entry) for new or changed public API

  • Added a changelog entry in docs/changelog.md under Unreleased (skip for lint/type/format-only or pure doc changes)

Additional Notes

The public API and stored arrays do not change. Dtype-based widening follows get_bbox_size_category; converting corners directly to float64 would lose one-unit widths above 2**53.

AI assistance disclosure: AI-assisted development tools were used during investigation and implementation. The reported tests were run against the final diff.

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94%. Comparing base (13ef387) to head (afcdef8).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2729   +/-   ##
=======================================
- Coverage       94%     94%   -0%     
=======================================
  Files           83      83           
  Lines        13316   13322    +6     
=======================================
+ Hits         12509   12512    +3     
- Misses         807     810    +3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda Borda added the bug Something isn't working label Oct 14, 2026
@Borda
Borda requested a balanced review from Copilot October 14, 2026 07:54

Copilot AI 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.

🟡 Changes recommended

Integer arrays below 64-bit unnecessarily use costly object arithmetic, and the changelog metadata remains stale.

2 open findings
What changed in this PR

Fixes integer overflow in Detections.box_aspect_ratio.

Changes:

  • Uses exact integer subtraction before float conversion.
  • Adds boundary and compatibility tests.
  • Documents the fix in the changelog.
File Description
src/​supervision/​detection/​core.py Corrects integer aspect-ratio arithmetic.
tests/​detection/​test_core.py Adds regression and edge-case coverage.
docs/​changelog.md Records the behavior fix.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/supervision/detection/core.py Outdated
Comment thread docs/changelog.md
@aswanth-07
aswanth-07 marked this pull request as ready for review October 14, 2026 08:32
@aswanth-07
aswanth-07 requested a review from SkalskiP as a code owner October 14, 2026 08:32
Changes:
- Add a regression asserting that float32 corner subtraction retains its original rounding before producing float64 aspect ratios.

Impact:
- Prevent future integer-overflow fixes from silently promoting floating coordinates and changing existing ratios.

Verification:
- Detection tests: 264 passed.
- Full coverage suite: 5622 passed, 97 skipped, 39 warnings; 94% coverage.
- Repository-wide pre-commit, targeted format and mypy, and closure evidence checks passed.

Residual limits:
- The parent verified the added regression after independent QA identified it; no second independent correction review ran.
- Local checks used the NumPy fallback backend; hosted cross-platform and OpenCV parity were not verified.

---

Co-authored-by: Codex <codex@openai.com>
@Borda
Borda merged commit a2abde3 into roboflow:develop Oct 15, 2026
34 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants