fix(eeg): resolve NumPy trapezoid compatibility lazily - #36
Merged
Conversation
`getattr(np, "trapezoid", getattr(np, "trapz"))` evaluates its default eagerly,
so the fallback lookup runs even when `trapezoid` exists. NumPy 2.0 removed
`trapz`, so on exactly the version the fallback was written for, importing
eeg_spectral raised:
AttributeError: module 'numpy' has no attribute 'trapz'
That made the module unimportable on numpy>=2, taking consume-session.py and
Tests/eval/test_session_consume.py down with it. The pinned calibration venv
(numpy 1.26) hid it.
Resolve through a helper that only touches `trapz` when `trapezoid` is absent.
The helper takes the namespace as a parameter so both API shapes can be tested
from one interpreter instead of maintaining two NumPy environments.
Verified on numpy 1.26.4 and 2.4.6; test_session_consume goes from crashing at
import to 14/14 on numpy 2.4.6.
NeuralComposeEEG/src/neuralcompose_eeg/features.py is not affected — it already
guards with `hasattr(np, "trapezoid")` before reaching `trapz`.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Jul 27, 2026
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.
Scripts/eeg_spectral.pyguarded its NumPy version bridge with:Python evaluates arguments before calling
getattr, so the default runsunconditionally. NumPy 2.0 removed
trapz, so on precisely the version thefallback exists for, importing the module raises:
That takes down
Scripts/consume-session.pyandTests/eval/test_session_consume.pywith it. The pinned calibration venv (numpy 1.26.4) hid the defect entirely.
Fix
Resolve lazily, and take the namespace as a parameter so both API shapes are
testable from a single interpreter:
Tests
Tests/eval/test_eeg_spectral_compat.pyuses fake namespaces for both majors,plus a
TrapzIsFatalnamespace whosetrapzraises if touched — that one isthe actual regression guard and fails against the old expression.
Verification
test_session_consumeon numpy 2.4.6Scope note
NeuralComposeEEG/src/neuralcompose_eeg/features.pyis not affected — italready guards with
hasattr(np, "trapezoid")before reachingtrapz, so itsfallback is only evaluated on numpy 1.x where it exists. I incorrectly reported
it as having the same exposure earlier; this PR touches one file plus its test.
Sequencing
Worth landing before the Python CI job: wiring
Tests/evalinto CI on a modernNumPy would fail on this bug.