fix: use np.trapezoid for forward-compat with NumPy 2.0+ (#1) - #4
Open
defnalk wants to merge 1 commit into
Open
Conversation
np.trapz was renamed to np.trapezoid in NumPy 2.0 and the old alias emits DeprecationWarning in 2.1+. The SURF environment ships NumPy 2.x, so this would have started warning on every analysis run and would hard-fail once the alias is removed. Use np.trapezoid via getattr fallback so the script still works under NumPy 1.x environments. Fixes #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1 — `np.trapz` was deprecated in NumPy 2.0 and emits a `DeprecationWarning` in 2.1+; once the alias is removed entirely, `coordination_number()` will hard-fail. This PR switches the integration call to `np.trapezoid` via a `getattr` fallback so the script also still works on NumPy 1.x environments.
Testing
Round-tripped `getattr(np, 'trapezoid', np.trapz)([0,1,4,9],[0,1,2,3])` → `9.5` against the new attribute.
Before / after
Before (`analysis/scripts/analyze_hydration.py`):
```python
import numpy as np
import MDAnalysis as mda
from MDAnalysis.analysis import rdf as mda_rdf
...
n_coord = 4.0 * np.pi * rho_b * np.trapz(integrand, r[mask])
```
After:
```python
import numpy as np
import MDAnalysis as mda
from MDAnalysis.analysis import rdf as mda_rdf
np.trapz was deprecated in NumPy 2.0 in favour of np.trapezoid; fall
back to the old name for environments still on NumPy 1.x.
_trapezoid = getattr(np, "trapezoid", np.trapz)
...
n_coord = 4.0 * np.pi * rho_b * _trapezoid(integrand, r[mask])
```