Skip to content

fix: replace deprecated 2-D np.cross with explicit determinant - #2386

Merged
Borda merged 5 commits into
roboflow:developfrom
abhijithneilabraham:ab_fix_numpy_depreciation
Jul 2, 2026
Merged

fix: replace deprecated 2-D np.cross with explicit determinant#2386
Borda merged 5 commits into
roboflow:developfrom
abhijithneilabraham:ab_fix_numpy_depreciation

Conversation

@abhijithneilabraham

@abhijithneilabraham abhijithneilabraham commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

np.cross(a, b) on 2-D vectors is deprecated in NumPy 2.0 and prints a
DeprecationWarning on every call. Two supervision call sites hit it:

  • get_polygon_center in src/supervision/geometry/utils.py fires once per
    polygon.
  • cross_product in src/supervision/detection/utils/internal.py fires three
    times per LineZone.trigger call.

Under pytest -W error::DeprecationWarning the current suite errors during
collection at tests/detection/test_polygon_zone_annotator.py.

For 2-D inputs np.cross(a, b) equals a[..., 0] * b[..., 1] - a[..., 1] *
b[..., 0] bit-exact. This PR swaps both call sites for the inline expression.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

Motivation and Context

Fixes the DeprecationWarning fired on every sv.LineZone.trigger and
sv.get_polygon_center call on NumPy 2.x. pyproject.toml allows numpy>=1.21.2
with no upper bound. When NumPy removes the 2-D np.cross code path, the
affected APIs will hard-crash instead of just warning.

Closes #2384

Changes Made

  • Replace np.cross(polygon, shift_polygon) in get_polygon_center with the
    explicit 2-D determinant.
  • Replace np.cross(vector_at_zero, anchors - vector_start) in cross_product
    with the explicit 2-D determinant.

Testing

  • I have tested this code locally
  • I have added unit tests that prove my fix is effective or that my
    feature works
  • All new and existing tests pass

Existing suite: 2288 passed, 1 skipped, no regressions. The suite now also
passes under pytest -W error::DeprecationWarning, which errored during
collection before this PR. Happy to add a regression test asserting no
DeprecationWarning on the two paths if that's the preferred direction.

Additional Notes

No public API changes or dependencies. Diff is 7 insertions, 2 deletions
across the two files.

@CLAassistant

CLAassistant commented Jul 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Borda
Borda requested a review from Copilot July 2, 2026 16:16
@Borda Borda added the bug Something isn't working label Jul 2, 2026
@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84%. Comparing base (99049d8) to head (564ab63).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2386   +/-   ##
=======================================
  Coverage       84%     84%           
=======================================
  Files           70      70           
  Lines        10003   10004    +1     
=======================================
+ Hits          8435    8436    +1     
  Misses        1568    1568           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Pull request overview

This PR removes reliance on NumPy’s deprecated 2-D np.cross(a, b) behavior by replacing it with an explicit 2-D determinant expression, eliminating DeprecationWarning spam on NumPy 2.x and preventing a future hard failure when that path is removed upstream (Issue #2384).

Changes:

  • Replace np.cross(polygon, shift_polygon) in get_polygon_center with the explicit 2-D determinant.
  • Replace np.cross(vector_at_zero, anchors - vector_start) in cross_product with the explicit 2-D determinant (and factor out diff).

Review Scores (n/5):

  • Code quality: 5/5
  • Testing: 3/5
  • Documentation: 4/5

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/supervision/geometry/utils.py Replaces the 2-D np.cross centroid signed-area computation with an explicit determinant to avoid NumPy 2.x deprecation warnings.
src/supervision/detection/utils/internal.py Replaces the 2-D np.cross in cross_product with an explicit determinant (hot path for LineZone.trigger).

Comment thread src/supervision/geometry/utils.py
Comment thread src/supervision/detection/utils/internal.py
abhijithneilabraham and others added 4 commits July 2, 2026 09:21
…tests

- Add filterwarnings = ["error::DeprecationWarning"] to pyproject.toml so
  future np.cross 2-D reintroductions fail CI immediately (closes roboflow#2384)
- Add test_get_polygon_center_no_deprecation_warning: asserts no
  DeprecationWarning from get_polygon_center (Copilot inline comment)
- Add test_cross_product_no_deprecation_warning: asserts no DeprecationWarning
  from cross_product (Copilot inline comment)
- Add test_cross_product_sign (4 parametrised cases): above / below / on-line /
  offset-start — directly tests the inline determinant correctness
- Improve cross_product docstring: blank line after summary, adds Examples
  section with correct output, notes NumPy 2.0 rationale

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Keeps both test approaches in tests/geometry/test_utils.py:
- test_get_polygon_center_no_deprecation_warning (DeprecationWarning check)
- test_get_polygon_center_does_not_call_np_cross (monkeypatch by contributor)

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
@Borda
Borda merged commit f196e15 into roboflow:develop Jul 2, 2026
23 checks passed
@Borda Borda mentioned this pull request Jul 29, 2026
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.

[Bug]: LineZone.trigger and get_polygon_center fire NumPy 2.0 DeprecationWarning on every call

4 participants