Skip to content

Use trapezoid instead of the removed trapz - #43

Merged
PaulWAyers merged 2 commits into
theochem:masterfrom
ARYAMANN7279:fix/trapz-removed-in-numpy
Nov 13, 2026
Merged

PaulWAyers merged 2 commits into
theochem:masterfrom
ARYAMANN7279:fix/trapz-removed-in-numpy

Conversation

@ARYAMANN7279

Copy link
Copy Markdown

np.trapz was removed from NumPy: it was renamed np.trapezoid in NumPy 2.0 and trapz is gone in the newer versions. _BaseRadialGrid.integrate in bfit/grid.py still calls np.trapz, so with the current NumPy every integration fails with AttributeError: module 'numpy' has no attribute 'trapz'. This breaks everything that integrates a function on a grid: the fits, the densities and the models. The tests of test_density.py call np.trapz themselves.

The code now uses np.trapezoid when NumPy has it and np.trapz otherwise, because requirements.txt still allows numpy>=1.18.5, which has no trapezoid. The result is the same number as before.

Test results with NumPy 2.5.4, SciPy 1.18.1 and Python 3.14: before this change, 48 of the 127 tests fail with the AttributeError. After it, all 127 pass.

NumPy 2.0 renamed trapz to trapezoid and trapz was removed later. Use trapezoid when it exists, and trapz for old versions of NumPy, in the radial grid and in the tests.
PaulWAyers
PaulWAyers previously approved these changes Nov 13, 2026

@PaulWAyers PaulWAyers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks fine to me. Thanks for fixing this.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the NumPy integration compatibility change.

No open issues 🎉

What changed in this PR

This PR restores radial-grid integration on newer NumPy versions while preserving compatibility with older supported releases.

Changes:

  • Use np.trapezoid when available, falling back to np.trapz.
  • Apply the same compatibility logic to density tests without changing their numerical assertions.
File Description
bfit/​test/​test_density.py Updates normalization and electron-count assertions to use a compatible integration function.
bfit/​grid.py Routes radial-grid integration through the NumPy-compatible function alias.

Review effort: Balanced

The jacobian at the exact optimum is a finite difference, so its second component is noise of about 1e-7 on Linux and 2.7e-6 on Windows and macOS. Check it to 5 decimals instead of 6.
@ARYAMANN7279

Copy link
Copy Markdown
Author

Thanks for the review, Paul. The Windows and macOS jobs (3.11 to 3.13) failed in test_kl_fit_unnormalized_dens_normalized_1s_gaussian, which I could reproduce on Windows. It starts at the exact optimum, and the second component of the jacobian there is finite-difference noise: about 1e-7 on Linux but 2.7e-6 on Windows, while the check asked for 6 decimals. I pushed a second commit that checks it to 5 decimals in that test. The full suite now passes on Windows (127 passed). The ubuntu 3.9 job fails only because the runner has no Python 3.9.

@PaulWAyers

Copy link
Copy Markdown
Member

@marco-2023 will check to see why the automated workflows aren't working for Windows.

@PaulWAyers
PaulWAyers merged commit ea6e979 into theochem:master Nov 13, 2026
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.

3 participants