Skip to content

fix: use np.trapezoid for forward-compat with NumPy 2.0+ (#1) - #4

Open
defnalk wants to merge 1 commit into
mainfrom
fix/np-trapezoid
Open

fix: use np.trapezoid for forward-compat with NumPy 2.0+ (#1)#4
defnalk wants to merge 1 commit into
mainfrom
fix/np-trapezoid

Conversation

@defnalk

@defnalk defnalk commented Apr 9, 2026

Copy link
Copy Markdown
Owner

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])
```

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
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.

np.trapz is deprecated in NumPy 2.0+; use np.trapezoid

1 participant