Skip to content

[spectra] Add detrend argument to compute_spectra - #690

Merged
arnodelorme merged 5 commits into
developfrom
agent/20261125-fix-689
Nov 28, 2026
Merged

arnodelorme merged 5 commits into
developfrom
agent/20261125-fix-689

Conversation

@claude

@claude claude Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

compute_spectra accepts detrend ("constant" or "linear") and forwards it to scipy's welch for each window, so callers can get a per-window mean detrend without cutting windows themselves. The default False keeps EEGLAB spectopo parity. The Matplotlib-free home requested in the issue already exists in sigprocfunc/spectra.py.

Fixes #689

EEGLAB reference run

EEGLAB reference run 46183713516 on 4576a64 (develop merged in), EEGLAB d0963be9, eeglab_tests fec88f9f: 4,484 passed, 5 failed, 58 skipped, about 36 minutes (19:56 to 20:32 UTC). The 5 failures are the suite's current baseline and do not involve compute_spectra: test_coregister_traditional_warp_matches_eeglab, test_synthetic_traditional_warp_matches_fieldtrip, test_bem_leadfield_and_multifit_match_fieldtrip, test_matlab_kmeans_numbering_matches_fixed_initial_centroids and test_struct_near_assertion_matches_source. The same five failed on the November 27 reference run of agent/20261127-fix-746 (45951378985).

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

⚠️ Code review skipped — your organization has no extra usage available to pay for this review.

If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.

Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.

compute_spectra forwards detrend to scipy.signal.welch. The default False
keeps EEGLAB spectopo parity.

Fixes #689
@claude

claude Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

PR #690 was updated after the trigger (9e22654 -> 2d72ca5). Push again or mention Claude to run on the latest version.

@arnodelorme arnodelorme left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This review was written by an AI assistant during a nightly check, not by Arno.

Code review

  • Overall assessment: needs changes. The code change is correct and the default path is unchanged, but the branch conflicts with develop and the merge evidence that AGENTS.md asks for is missing.
  • Highest-risk area: none in the numerics. detrend=False reaches welch exactly as before, so default spectra are the same as on develop.
  • Merge recommendation: needs changes (rebase, then one EEGLAB reference run).

Blocking

None.

Important

  1. Merge conflict with develop in docs/source/changelog.rst. GitHub reports the pull request as conflicting. A trial merge of 2d72ca5 with develop at 046117e conflicts only in the "Unreleased" list. spectra.py and tests/test_spectra_summaries.py merge cleanly. Fix: rebase on develop and keep the new entry.

  2. No EEGLAB reference run in the description. src/eegprep/functions/sigprocfunc/spectra.py holds compute_spectra, the estimator behind spectopo, so this pull request changes a core function implementation. AGENTS.md says: "Before merging a PR that changes core function implementations (EEGLAB-ported processing code), trigger .github/workflows/eeglab-reference.yml (EEGLAB reference, workflow_dispatch) on the PR branch, or run the suite locally once on the final branch" and "Put that run in the PR description". No run of that workflow exists for agent/20261125-fix-689, and by the same section the green "EEGLAB Python backend" check does not count. Fix: after the rebase, trigger the workflow once and add to the description the run, the EEGLAB and eeglab_tests commits, the pass, fail and skip counts, and the runtime. The same section also asks for human approval before the merge.

Nits

  1. compute_spectra does not check detrend (spectra.py line 34, forwarded at line 118). A wrong value reaches SciPy, which answers ValueError: Trend type must be 'linear' or 'constant'. That message does not name compute_spectra and does not say that False turns detrending off. Wrong values are likely here because newtimef and timefreq in this package take detrend="on" and detrend="off". Checked on the pull request code with SciPy 1.15.3: "off", "on", "mean" and True raise that SciPy error, while "" and None run with no detrending and no message. Suggested fix, next to the wintype check at line 93:

    if detrend not in (False, "constant", "linear"):
        raise ValueError("compute_spectra: detrend must be False, 'constant' or 'linear'")

Test gaps

None.

EEGLAB parity notes

  1. Default parity holds. spectopo.m calls pwelch(..., winlength, g.overlap, fftlength, srate) at lines 941 to 942, and pwelch does not detrend. 'rmdc' defaults to 'off' at line 207. The default False keeps both.

  2. detrend="constant" is not EEGLAB's 'rmdc', 'on'. spectopo.m lines 270 to 271 subtract each channel's mean over the whole record (over each epoch for 3-D data) before pwelch. The new option subtracts the mean of each Welch window. The two differ at low frequencies when the record drifts slowly. The docstring says the default "matches EEGLAB, which does not detrend", which is true, but a reader may take "constant" for rmdc. One clause would settle it, for example: "This is not EEGLAB's rmdc, which removes the mean of the whole record." EEGPrep's spectopo does not have rmdc. That is outside this pull request.

Tests run

tests/test_spectra_summaries.py on the code of 2d72ca5, in a separate worktree, with Python 3.11 and SciPy 1.15.3: 11 passed in 5 s.

Three more checks by hand on the same code, with random data plus a constant offset per channel. detrend="constant" gives the same dB values (largest difference 0) as cutting 128-sample windows by hand, removing each window's mean and passing them as epochs, which is the workaround that issue 689 wants to drop. Adding a constant offset changes the detrend="constant" result by at most 7e-15 dB. A 3-D array and the same epochs concatenated in 2-D give equal results with detrend="constant" for the Hamming window, the Blackman-Harris window, a 50 % overlap, and with detrend="linear".

Checked for correctness bugs, EEGLAB parity, data structure compatibility, changed-behavior tests, and CLAUDE.md/AGENTS.md compliance.

@arnodelorme
arnodelorme merged commit 49d7fb0 into develop Nov 28, 2026
11 of 12 checks passed
@arnodelorme

Copy link
Copy Markdown
Collaborator

Follow-up to the review.

The branch is up to date with develop, merged twice with no force push. The second merge conflicted in spectra.py with #697, which adds a freqfac check next to the new detrend check. Both checks are kept. The nit is done too: compute_spectra now rejects any detrend other than False, "constant" or "linear" with a message that names it, and a test covers "off". The docstring now says that "constant" is not EEGLAB's rmdc, which removes the mean of the whole record.

The EEGLAB reference run on this branch (46183713516) is in the description: 4,484 passed, 5 failed and 58 skipped. The 5 failures are the suite's current baseline, also seen on the November 27 run of another branch, and none involves compute_spectra. The spectra tests pass locally (11 passed). The only failing CI check is the Windows test test_window_without_a_height_is_as_high_as_its_rows that came with #776 and fails on every branch.

The review is now clean and every other check passes, so I merged it with a squash, as Arno asked. After #804 was merged its changelog entry conflicted with this one, so develop was merged in once more (changelog only); the spectra tests, lint and docs build passed again before the merge.

— orchestrator

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.

compute_spectra: a detrend argument, and a home without matplotlib

1 participant