Skip to content

Support numpy >= 2.4 with backward-compatible fixes#5

Merged
pmrv merged 2 commits into
masterfrom
claude/numpy-24-compat
Jul 16, 2026
Merged

Support numpy >= 2.4 with backward-compatible fixes#5
pmrv merged 2 commits into
masterfrom
claude/numpy-24-compat

Conversation

@pmrv

@pmrv pmrv commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Pull Request Template

Description of Changes

Follow-up to #3: makes the code compatible with numpy ≥ 2.4 in a backward-compatible way and relaxes the requirement from numpy<2.4 to numpy<3.

Two numpy 2.4 breakages are fixed, both working identically on old and new numpy:

  1. Scalar energy output (src/pyace/asecalc.py). numpy 2.4 expired the "conversion of an array with ndim > 0 to a scalar" deprecation, so np.float64(energy.reshape(-1,)) started returning a shape-(1,) array instead of a scalar, and get_potential_energy() leaked an array of the total energy. The results dicts now use float() on the 0-d/scalar values (energy, free_energy, and the ensemble energy_std/energy_dev), which behaves identically on every numpy version, and .astype(np.float64) on per-atom arrays (energies_dev, forces_dev) — this also fixes those arrays silently degrading to scalars for single-atom structures on older numpy.
  2. np.trapz removal (src/pyace/radial.py). np.trapz was a deprecated alias of np.trapezoid from numpy 2.0 and was removed in 2.4. The module now picks np.trapezoid when it exists and falls back to np.trapz on numpy 1.x.

A regression assertion is added to tests/test_PyACECalculator.py so the energy coming back as an array would fail the tests.

Verification: built the package and ran tests/test_PyACECalculator.py (13 tests) against numpy 1.26.4 (exercises the np.trapz fallback), 2.3.5, and 2.4.6 — all pass on each. CI on this PR additionally covers the full test suite, which will now resolve numpy 2.4.x.

Checklist

  • Code is well-documented.
  • All tests have been run and passed. (full suite in CI; calculator tests locally against numpy 1.26.4 / 2.3.5 / 2.4.6)
  • Relevant documentation has been updated if necessary. (no docs reference the numpy pin)

License Agreement

By submitting this pull request, I agree that:

  • The code submitted in this pull request will be distributed under the Academic Software License (free for academic non-commercial use, not free for commercial use), see LICENSE.md for more details.
  • The copyright for the code, including the submitted code, remains with Ruhr University Bochum. Ruhr University Bochum retains the right to transfer or modify the copyright.

🤖 Generated with Claude Code

https://claude.ai/code/session_015Ui6Drmop9gZXo1fgn8yVg


Generated by Claude Code

numpy 2.4 expired the ndim>0-to-scalar deprecation: np.float64(arr)
now returns an array for 1-element arrays instead of collapsing to a
scalar, so PyACECalculator's results['energy'] leaked out as a
shape-(1,) array from get_potential_energy(). Use float() on the 0-d
energy values instead, which behaves identically on all numpy versions,
and astype(np.float64) for per-atom arrays (also fixing energies_dev
degrading to a scalar for single-atom structures on older numpy).

numpy 2.4 also removed np.trapz (deprecated alias of np.trapezoid
since 2.0); pyace.radial now picks whichever exists.

Verified by building the package and running tests/test_PyACECalculator.py
against numpy 1.26.4, 2.3.5 and 2.4.6. Relax the numpy requirement to <3
accordingly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Ui6Drmop9gZXo1fgn8yVg
Comment thread src/pyace/asecalc.py Outdated
Comment thread src/pyace/asecalc.py Outdated
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Ui6Drmop9gZXo1fgn8yVg
@pmrv
pmrv merged commit 2544d97 into master Jul 16, 2026
2 of 3 checks passed
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.

2 participants