Skip to content

fix(visualize): use np.trapezoid for AUC/AP, restoring NumPy >=2.4 (#99)#100

Merged
chaoming0625 merged 1 commit into
mainfrom
fix-np-trapz
Jun 9, 2026
Merged

fix(visualize): use np.trapezoid for AUC/AP, restoring NumPy >=2.4 (#99)#100
chaoming0625 merged 1 commit into
mainfrom
fix-np-trapz

Conversation

@chaoming0625

@chaoming0625 chaoming0625 commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

Hotfix: main CI went red after #98 because the new test_precision_recall_curve coverage test exercised braintools.visualize.precision_recall_curve, which (along with roc_curve) calls np.trapz.

np.trapz was renamed to np.trapezoid in NumPy 2.0 and removed in NumPy 2.4. CI installs numpy 2.4.6, so both functions raised:

AttributeError: module 'numpy' has no attribute 'trapz'. Did you mean: 'trace'?

It was invisible locally because local environments had NumPy < 2.4.

Fix

Resolve the integration routine once at import time, preferring np.trapezoid and falling back to np.trapz for the declared numpy>=1.15 floor:

_trapezoid = getattr(np, 'trapezoid', None) or np.trapz

Both call sites (roc_curve AUC, precision_recall_curve AP) now use _trapezoid. Same signature, same semantics.

Verification

pytest braintools/visualize/_statistical_extra_test.py braintools/visualize/_statistical_test.py43 passed locally, where _trapezoid resolves to np.trapezoid (the same path CI's NumPy 2.4.6 takes).

Closes #99

Summary by Sourcery

Ensure ROC and precision-recall visualization metrics work with newer NumPy versions by using a compatible trapezoidal integration helper.

Bug Fixes:

  • Fix AttributeError in roc_curve and precision_recall_curve when running with NumPy >= 2.4 by resolving a compatible trapezoidal integration function at import time.

Documentation:

  • Document the NumPy compatibility fix for roc_curve and precision_recall_curve in the changelog.

np.trapz was renamed to np.trapezoid in NumPy 2.0 and removed in NumPy
2.4, so roc_curve and precision_recall_curve raised AttributeError on
NumPy 2.4+ (which CI installs). Resolve np.trapezoid when available and
fall back to np.trapz for the declared numpy>=1.15 floor.

This broke main CI after #98 merged; it was invisible locally because
local NumPy was < 2.4.

Closes #99
@sourcery-ai

sourcery-ai Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR fixes compatibility with NumPy>=2.4 by resolving a shared trapezoidal integration helper at import time (preferring np.trapezoid, falling back to np.trapz) and updating ROC and precision–recall curve metrics to use it, plus documenting the fix in the changelog.

File-Level Changes

Change Details Files
Make trapezoidal integration robust to NumPy 2.x deprecations and removals and use it for ROC AUC and PR AP computations.
  • Introduce a module-level _trapezoid helper that resolves to np.trapezoid when available, otherwise np.trapz, with comments explaining the NumPy 2.0/2.4 behavior.
  • Replace direct np.trapz calls in roc_curve with the new _trapezoid helper when computing AUC.
  • Replace direct np.trapz calls in precision_recall_curve with the new _trapezoid helper when computing average precision.
braintools/visualize/_statistical.py
Document the NumPy compatibility fix for visualize metrics in the project changelog.
  • Add a changelog entry describing that roc_curve and precision_recall_curve now resolve np.trapezoid when available and fall back to np.trapz, fixing AttributeError on NumPy>=2.4.
changelog.md

Assessment against linked issues

Issue Objective Addressed Explanation
#99 Update braintools.visualize.roc_curve and precision_recall_curve to avoid using np.trapz directly, instead resolving np.trapezoid when available and falling back to np.trapz, so that the functions work on NumPy >= 2.4 while retaining compatibility with the declared numpy>=1.15 floor.
#99 Document the NumPy integration change (np.trapz -> np.trapezoid with fallback) in the project documentation/changelog.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov

codecov Bot commented Jun 9, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

ℹ️ You can also turn on project coverage checks and project coverage reporting on Pull Request comment

Thanks for integrating Codecov - We've got you covered ☂️

@chaoming0625 chaoming0625 merged commit cdd8f94 into main Jun 9, 2026
6 checks passed
@chaoming0625 chaoming0625 deleted the fix-np-trapz branch June 9, 2026 17:08
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.

visualize.roc_curve/precision_recall_curve crash on NumPy >=2.4: np.trapz was removed

1 participant