Skip to content

Replace deprecated np.trapz with np.trapezoid, require NumPy ≥ 2.0#39

Merged
JulianBMunoz merged 2 commits into
mainfrom
copilot/update-numpy-trapz-to-trapezoid
Apr 29, 2026
Merged

Replace deprecated np.trapz with np.trapezoid, require NumPy ≥ 2.0#39
JulianBMunoz merged 2 commits into
mainfrom
copilot/update-numpy-trapz-to-trapezoid

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

np.trapz was removed in NumPy 2.0, causing an AttributeError at import time and breaking the entire test suite during CI collection.

Changes

  • Source: Replace all np.trapz(...)np.trapezoid(...) across zeus21/ (cosmology.py, sfrd.py, xrays.py, correlations.py, UVLFs.py)
  • Tests: Same replacement in tests/test_inputs.py
  • Dependencies: Pin numpy>=2.0 in both requirements.txt and setup.py install_requires
Original prompt

Update Zeus21 to use numpy.trapezoid everywhere instead of numpy.trapz, and update dependency requirements to require a NumPy version that provides np.trapezoid.

Context:

  • GitHub Actions job 73616883824 fails with AttributeError: module 'numpy' has no attribute 'trapz' during pytest collection when importing tests/test_astrophysics.py which calls zeus21.get_T21_coefficients(...).
  • The goal is to modernize integration calls to use np.trapezoid and then enforce a NumPy minimum version accordingly (user requested requiring NumPy version that supports trapezoid, likely NumPy >= 1.17).

Tasks:

  1. Search the repository for all uses of np.trapz and numpy.trapz and replace them with np.trapezoid / numpy.trapezoid.
  2. Ensure imports consistently use numpy as np where needed.
  3. Update dependency metadata so installation requires a NumPy version that includes trapezoid:
    • Update requirements.txt if present.
    • Update pyproject.toml or setup.cfg / setup.py install_requires as applicable.
  4. Update any docs/tests if needed.
  5. Run/ensure tests pass.

Acceptance criteria:

  • CI no longer errors on missing trapz.
  • Package declares NumPy minimum version that includes np.trapezoid.
  • No remaining references to trapz in code.

Use repository ZeusCosmo/Zeus21.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Please find a solution for failing job 73616883824. Use the logs, job definition, and any referenced files where the failure occurred. Keep your response focused on the solution and include code suggestions when appropriate.

Assistant: Job 73616883824 fails during pytest collection with:

  • AttributeError: module 'numpy' has no attribute 'trapz'
  • Triggered while importing tests/test_astrophysics.py, specifically when it executes:
    • Coeffs = zeus21.get_T21_coefficients(...) (line 32 in tests/test_astrophysics.py at ref 2cae9fced02744451ac8e3b9c88c12cb0066b70b)

This points to package code calling np.trapz, but the runtime NumPy version in CI doesn’t provide it (newer NumPy releases have been tightening/removing legacy aliases; trapz is effectively superseded by trapezoid).

Solution (preferred): make Zeus21 compatible with new NumPy

Update any internal usage of np.trapz(...) to np.trapezoid(...), with a safe fallback for older NumPy.

Add a small compatibility helper (recommended in a central place like zeus21/utils.py or similar), then use it everywhere you integrate:

import numpy as np

def trapz_compat(y, x=None, dx=1.0, axis=-1):
    # NumPy >= 2 prefers trapezoid; older versions still have trapz
    if hasattr(np, "trapezoid"):
        return np.trapezoid(y, x=x, dx=dx, axis=axis)
    return np.trapz(y, x=x, dx=dx, axis=axis)

Then replace occurrences like:

I = np.trapz(y, x)

with:

I = trapz_compat(y, x)

This fixes the immediate crash without forcing a specific NumPy version.

Alternative (quick CI unblock): pin NumPy in requirements

If you need an immediate unblock and don’t want to touch integration code yet, pin NumPy to a version that still includes np.trapz in requirements.txt, e.g.:

numpy<2

However, the more robust long-term fix is updating Zeus21 to use np.trapezoid (with fallback), because CI uses Python 3.11 and will naturally drift to newer NumPy over time.

Why this resolves the failing job

The workflow (.github/workflows/python-tests.yml at ref 2cae9fced02744451ac8e3b9c88c12cb0066b70b) installs dependencies from requirements.txt, then runs pytest. The failure happens before tests run, during import-time computation in tests/test_astrophysics.py, because Zeus21 code hits the missing np.trapz. Replacing/polishing that call (or pinning NumPy) prevents the import-time exception and allows the test suite to proceed.

User: let's just change all to np.trapezoid, and require numpy > whatever it has to be for that >2

Assistant: I’m waiting on your approval in the GitHub dialog to proceed with applying the np.trapezoid replacements across the repo and updating the NumPy minimum version requirement.

This pull request was created from Copilot chat.

Agent-Logs-Url: https://github.com/ZeusCosmo/Zeus21/sessions/125bc937-b800-45fc-b854-32e56c5f75df

Co-authored-by: JulianBMunoz <22434409+JulianBMunoz@users.noreply.github.com>
Copilot AI changed the title [WIP] Update Zeus21 to use numpy.trapezoid instead of numpy.trapz Replace deprecated np.trapz with np.trapezoid, require NumPy ≥ 2.0 Apr 29, 2026
Copilot AI requested a review from JulianBMunoz April 29, 2026 16:14
@JulianBMunoz JulianBMunoz marked this pull request as ready for review April 29, 2026 16:18
@JulianBMunoz JulianBMunoz merged commit b1285f2 into main Apr 29, 2026
1 check 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