Repository navigation
Use trapezoid instead of the removed trapz - #43
Conversation
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
left a comment
There was a problem hiding this comment.
It looks fine to me. Thanks for fixing this.
There was a problem hiding this comment.
🟢 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.trapezoidwhen available, falling back tonp.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.
|
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. |
|
@marco-2023 will check to see why the automated workflows aren't working for Windows. |
np.trapzwas removed from NumPy: it was renamednp.trapezoidin NumPy 2.0 andtrapzis gone in the newer versions._BaseRadialGrid.integrateinbfit/grid.pystill callsnp.trapz, so with the current NumPy every integration fails withAttributeError: 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 oftest_density.pycallnp.trapzthemselves.The code now uses
np.trapezoidwhen NumPy has it andnp.trapzotherwise, becauserequirements.txtstill allowsnumpy>=1.18.5, which has notrapezoid. 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.