Skip to content

Widen numpy constraint to <2.4#3

Merged
pmrv merged 1 commit into
masterfrom
claude/numpy-energy-output-bug-itm376
Jul 16, 2026
Merged

Widen numpy constraint to <2.4#3
pmrv merged 1 commit into
masterfrom
claude/numpy-energy-output-bug-itm376

Conversation

@pmrv

@pmrv pmrv commented Jul 15, 2026

Copy link
Copy Markdown
Owner

Pull Request Template

Description of Changes

Relaxes the numpy pin in setup.py from numpy<=1.26.4 to numpy<2.4. Versions up to 2.3.x are empirically known to work; 2.4 is excluded because it breaks pyace, with a full diagnosis below.

Diagnosis: why numpy ≥2.4 breaks the energy output

PyACECalculator.calculate() builds its ASE results dict like this (src/pyace/asecalc.py:167):

'energy': np.float64(self.energy.reshape(-1, )),

self.energy is a 0-d array wrapping the scalar total energy from the C++ evaluator, so .reshape(-1,) turns it into a shape-(1,) array, which is then passed through the np.float64 constructor (not a cast of a Python float).

The behavior of that constructor changed in numpy 2.4:

  • numpy ≤2.3: np.float64(array_of_size_1) collapses the array to a scalar np.float64. Since numpy 1.25 this has emitted DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future.
  • numpy 2.4: that deprecation expired. Scalar constructors now behave like astype for ndim > 0 inputs and return an ndarray (numpy 2.4 release notes, "NumPy scalar constructor behaviour for arrays and 0-d inputs").

So on numpy 2.4, results['energy'] becomes array([E_total]) instead of a float, and atoms.get_potential_energy() returns a 1-element array of the total energy rather than a scalar — the observed symptom. Verified empirically:

numpy 2.3.5: np.float64(np.array(-123.456).reshape(-1,)) -> np.float64(-123.456)   (scalar, ndim 0, + DeprecationWarning)
numpy 2.4.6: np.float64(np.array(-123.456).reshape(-1,)) -> array([-123.456])      (ndarray, ndim 1)

The same pattern affects free_energy on the same line pair, and energy / free_energy_std / energy_std in PyACEEnsembleCalculator (src/pyace/asecalc.py:308-315).

Second, independent numpy 2.4 breakage

pyace.radial.integrate() (src/pyace/radial.py:20) calls np.trapz, which numpy 2.0 kept only as a deprecated alias of np.trapezoid and numpy 2.4 removed outright — so radial-function integration raises AttributeError on 2.4 regardless of the energy issue.

Forward path (not in this PR)

Both are one-line fixes if support beyond 2.4 is wanted later: replace np.float64(self.energy.reshape(-1,)) with float(self.energy) (works on all versions and silences the existing DeprecationWarning), and np.trapz with np.trapezoid (available since numpy 2.0). Keeping this PR to the empirically validated <2.4 bound.

Checklist

  • Code is well-documented.
  • All tests have been run and passed. (dependency-pin change only; the C++ extension is not built in this environment — behavior difference verified against standalone numpy 2.3.5 vs 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 releases up to 2.3.x work correctly; 2.4 breaks pyace in two ways:

1. np.float64(arr) on a 1-element array now returns an ndarray instead
   of collapsing to a scalar (the ndim>0-to-scalar deprecation from
   numpy 1.25 expired in 2.4), so PyACECalculator's results['energy']
   becomes a shape-(1,) array and get_potential_energy() returns an
   array of the total energy rather than a float.
2. np.trapz, used in pyace.radial, was removed in 2.4 (deprecated
   alias for np.trapezoid since 2.0).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Ui6Drmop9gZXo1fgn8yVg
@pmrv
pmrv force-pushed the claude/numpy-energy-output-bug-itm376 branch from c8c674a to 76adfe3 Compare July 16, 2026 02:12

@pmrv pmrv left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

lgtm.merge

@pmrv
pmrv merged commit 46db657 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