Skip to content

[clean_asr] Rank a zero-scale channel as +inf, matching EEGLAB's 241 calibration samples - #649

Merged
suraj-ranganath merged 5 commits into
developfrom
cursor/fix-asr-flat-channel-facb
Nov 18, 2026
Merged

suraj-ranganath merged 5 commits into
developfrom
cursor/fix-asr-flat-channel-facb

Conversation

@suraj-ranganath

@suraj-ranganath suraj-ranganath commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

A channel whose window RMS values are all equal is the highest score in every window, so it uses a high-amplitude bad-channel slot. On the reported 17-channel crop the calibration reference keeps 241 samples. An all-zero channel stays exactly zero through ASR, and MATLAB's sqrtm residual on that channel is roundoff.

Confirm on jamming with MATLAB R2025b: uv run python -m tests.fuzz reproduce /mnt/v1/arno/suraj3/pr640/fuzz-out-wide/repros/clean_asr-numeric-data.json and uv run python -m tests.fuzz reproduce /mnt/v1/arno/suraj3/pr640/fuzz-out/repros/pop_clean_rawdata-numeric-data.json.

Fixes #642
Fixes #643

Open in Web Open in Cursor 

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

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.

@claude

claude Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @suraj-ranganath's task in 1m 24s —— View job


Code review

  • Overall assessment: Needs changes. In clean_windows, a channel that is zero everywhere is now scored as -inf, which treats it as a dropout. The change is small, and the docs, changelog and migration table describe it consistently. The fuzz-harness exemptions added alongside it are broader than the docs say.
  • Highest-risk area: the #643 / #642 exemptions in tests/fuzz/harness.py, and the missing MATLAB reference run for a core-function change.
  • Merge recommendation: Needs changes.

Blocking

None.

Important

  1. The flat-row exemption has no limit on the residual size (tests/fuzz/harness.py, _failed_samples_are_on_rows, used by _flat_nullspace_roundoff_is_known and _flat_nan_sort_is_known).
    • Problem: any failure on an all-zero input row is accepted, whatever its magnitude. The docs say the MATLAB residual there "is roundoff". The predicate never checks that, so a real defect that writes signal into a dead channel would be classified as #643 and never filed.
    • Why it matters: the nightly fuzzer is the parity safety net, and this exemption would hide real defects on that channel.
    • Fix: also require that the oracle's flat-row magnitude is at roundoff scale relative to the other channels (for example, a named relative constant around 1e-9). Add a rejection case with a large value on row 11. Details are in the inline comment.
  2. The MATLAB reference evidence required by AGENTS.md is missing. src/eegprep/plugins/clean_rawdata/clean_windows.py is ported processing code, and this PR changes which calibration windows are kept. AGENTS.md says: "Before merging a PR that changes core function implementations … trigger .github/workflows/eeglab-reference.yml … Put that run in the PR description: the exact command or workflow run, the EEGLAB and eeglab_tests commits, pass/fail/skip counts, runtime." The two tests.fuzz reproduce commands in the body are not that run.
    • Fix: run the EEGLAB reference workflow on this branch and add the result to the PR body.

Nits

  • tests/test_clean_asr.py adds _remove_windows and _kept_samples. These are line-for-line copies of _window_remove_mask and _sample_mask_from_windows, which this PR adds to tests/fuzz/harness.py, and the test module already imports from that harness. Import the harness helpers instead. AGENTS.md: "Avoid duplicate fixtures; reuse … local helpers already present."

Test gaps

  • test_flat_channel_roundoff_is_not_a_divergence only uses a 5.8e-12 residual on the flat row. It needs a case where a large residual on the flat row is rejected. Without it, the problem in Important refactor for packaging and test distribution #1 is not caught.

EEGLAB parity notes

  • This is an intentional deviation, and it is documented (migration table, changelog, docstrings) and covered by tests, so it is not a parity bug in itself. Note the tradeoff for reviewers, though. EEGLAB's (0-0)/0 NaN takes up one high-side bad-channel slot. The new -inf takes up one low-side slot instead. Before this PR, EEGPrep scored the channel 0 and used no slot. Both EEGLAB and this PR use up one slot, so the behaviour only differs in which side loses its tolerance: on the [fuzz] clean_asr numeric divergence on a flat-channel crop, seed 20261012 #642 crop, 241 samples are kept versus 198. Choosing +inf would match EEGLAB's window selection exactly, and most of the #642 harness exemption (_nan_sort_reference, _asr_from_reference, _flat_nan_sort_is_known, about 100 lines) could then be deleted. If -inf stays, the PR body or the migration note should say briefly why a low-side dropout is more correct than EEGLAB's high-side placement, given that both remove the same number of slots.
  • The any_energy guard correctly leaves an all-zero recording on the existing fallback path (center=0, scale=1). I checked this and it is fine.

Checked: the clean_windows z-score change and its fallback interaction, the clean_asr calibration call (clean_asr.py:298), the harness pipeline wiring (step names stay clean_asr, and the two predicates are checked in a safe order), and the docs and changelog consistency. I could not run the new tests in this environment because test execution needs approval.

Comment thread tests/fuzz/harness.py
Comment on lines +1779 to +1785
finite = np.isfinite(candidate) & np.isfinite(oracle_data)
diff = np.abs(candidate - oracle_data)
limit = tolerance.abs + tolerance.rel * np.abs(oracle_data)
failed = finite & (diff > limit)
if not np.any(failed):
return False
return not np.any(np.any(failed, axis=1) & ~rows)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important. This predicate accepts a failure of any size on the flat row. The docs and the #643 label call the MATLAB residual "roundoff", but nothing here limits how large it can be. If the oracle wrote 50 µV, or a reconstructed copy of another channel, into the dead channel, _flat_nullspace_roundoff_is_known would still classify it as a known roundoff difference and the fuzzer would not file it. _flat_nan_sort_is_known falls back to this same check.

Suggested fix: limit the residual on rows to roundoff relative to the signal scale. For example, also require np.max(np.abs(oracle_data[rows])) <= ROUNDOFF_REL * np.max(np.abs(oracle_data[~rows])), with a named constant such as 1e-9. Then add a case to test_flat_channel_roundoff_is_not_a_divergence where residual[11, 0] = 1.0 and assert that it is rejected.

@suraj-ranganath
suraj-ranganath marked this pull request as draft November 18, 2026 18:23
@suraj-ranganath
suraj-ranganath marked this pull request as ready for review November 18, 2026 18:23
cursoragent and others added 3 commits November 18, 2026 18:28
A dead channel's window RMS is zero, so it sits below the lower power
bound when another channel has energy. EEGLAB's 0/0 z-score sorts as
the highest window and selects a different calibration reference. An
all-zero channel stays exactly zero through ASR.

Co-authored-by: Suraj Ranganath <[email protected]>
The fuzzer only treats a dead channel as MATLAB square-root roundoff when
the oracle values there stay within 1e-9 of the live-channel scale. A
reconstructed signal on that channel stays a divergence.

Co-authored-by: Suraj Ranganath <[email protected]>
A channel whose window RMS values are all equal takes a high-amplitude
bad-channel slot. On the reported crop the calibration reference keeps
241 samples. The fuzzer compares that result directly.

Co-authored-by: Suraj Ranganath <[email protected]>
@cursor
cursor Bot force-pushed the cursor/fix-asr-flat-channel-facb branch from 26ec0f1 to 7a593a2 Compare November 18, 2026 18:29
@cursor cursor Bot changed the title [clean_asr] Count an all-zero channel as a dropout [clean_asr] Rank a zero-scale channel as +inf, matching EEGLAB's 241 calibration samples Nov 18, 2026
@suraj-ranganath
suraj-ranganath marked this pull request as draft November 18, 2026 18:31
@suraj-ranganath
suraj-ranganath marked this pull request as ready for review November 18, 2026 18:31
@claude

claude Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @suraj-ranganath's task in 1m 4s —— View job


Code review

  • Overall assessment: Needs changes, but only small ones. The +inf ranking in clean_windows matches EEGLAB's window selection, and the earlier review findings are fixed.
  • Highest-risk area: the MATLAB reference evidence required for a core-function change, and a migration-table row that describes matching behavior as a deviation.
  • Merge recommendation: Needs changes.

Blocking

None.

Important

  1. The MATLAB reference evidence is not in the PR body. src/eegprep/plugins/clean_rawdata/clean_windows.py is ported processing code, and this PR changes which windows are kept. That affects both the ASR calibration reference and pop_clean_rawdata's WindowCriterion. AGENTS.md says: "Before merging a PR that changes core function implementations … trigger .github/workflows/eeglab-reference.yml … Put that run in the PR description: the exact command or workflow run, the EEGLAB and eeglab_tests commits, pass/fail/skip counts, runtime." The two tests.fuzz reproduce commands in the body are not that run.
    • Fix: run eeglab-reference.yml on head 7a593a2, or run the local --eeglab-backend=matlab command, and add the result to the body. (I couldn't run gh run list here to see whether a run already exists.)
  2. The migration table lists matching behavior as an intentional deviation (docs/source/user_guide/eeglab_migration.rst:294-301). The table opens with "EEGPrep does not reproduce the EEGLAB result". With +inf, the reference selection now matches EEGLAB: the score is NaN in MATLAB, sort puts it last, and NaN > zmax is false. Only the [fuzz] pop_clean_rawdata ASR-only residual of 5.79e-12 against approved abs 0 #643 sqrtm residual is still a real difference.

Nits

  • clean_windows.py:215: the comment says what the code does but not why. The parity reason would be more useful there: # clean_windows.m: a zero-sig channel is NaN, sort() ranks NaN last, and NaN > zmax is false.

Test gaps

None. test_zero_variance_channel_is_the_highest_score covers the threshold guard. test_flat_channel_reference_keeps_241_samples pins the #642 crop. test_flat_channel_roundoff_is_not_a_divergence now rejects both a large residual on the flat row and a real divergence on a live row.

EEGLAB parity notes

  • +inf combined with the np.isfinite(highest) guard reproduces MATLAB's NaN-sorted-last behavior on both thresholds. On the high side the channel takes the top slot and no window is removed. On the low side +inf < zmin is never true, the same as NaN. I checked the case of several constant channels with max_bad_channels=0, and it also matches.
  • Previous findings fixed: _flat_rows_are_roundoff now limits the dead-channel residual to _FLAT_CHANNEL_ROUNDOFF_REL * live scale. The duplicated mask helpers and the #642 NaN-sort exemption are gone.

Checked: the clean_windows scoring and threshold change, how it interacts with the _window_rms_mu_sigma fallback, the harness exemption and its pipeline wiring, the new tests, and the docs and changelog. I didn't run the tests, because commands outside the review allowlist need approval.

Comment on lines +294 to +301
* - Zero-variance channel in ASR reference selection
- A channel whose window RMS values are all equal has scale 0 and is the
highest score in every window, so it uses a high-amplitude bad-channel
slot. On the 17-channel crop starting at sample 4097, with cutoff 3,
``ref_maxbadchannels`` 0.075, and a 1 s processing window, the
calibration reference keeps 241 samples (#642). An all-zero channel
that ASR repairs stays exactly zero. MATLAB's ``sqrtm`` leaves a
roundoff residual on that channel (#643).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important (docs). This table is introduced as "These are intentional. EEGPrep does not reproduce the EEGLAB result." After the switch to +inf, the reference selection half of this row is no longer a deviation. In clean_windows.m, (X-mu)/sig on a zero-variance channel is NaN. MATLAB's sort places NaN last (the highest rank), and NaN > zmax is false, so EEGLAB also keeps 241 samples on this crop. The PR title says the same thing. A migrating user reading this row would conclude that EEGPrep picks a different calibration reference than EEGLAB.

Suggested fix: keep only the #643 part in this table, for example "All-zero channel after ASR: EEGPrep returns exactly zero. MATLAB's sqrtm leaves a roundoff residual (#643)." If the selection behavior needs documenting, describe it as matching EEGLAB in plugins.rst, which this PR already does.

cursoragent and others added 2 commits November 18, 2026 18:33
The +inf ranking matches EEGLAB's NaN sort and keeps the same 241
calibration samples, so it does not belong in the known-differences table.

Co-authored-by: Suraj Ranganath <[email protected]>
The SHA256 of the repaired samples changes across platforms. The test
now locks the 241 kept indices, the +inf score, and the approved
clean_asr tolerance against an explicit calibration reference.

Co-authored-by: Suraj Ranganath <[email protected]>
@suraj-ranganath
suraj-ranganath merged commit ef6fdd0 into develop Nov 18, 2026
12 checks passed
cursor Bot pushed a commit that referenced this pull request Nov 18, 2026
Keep the zero-scale channel ranking from #649 alongside the
ASRCalibrationError raised when a fit width rounds to zero.

Co-authored-by: Suraj Ranganath <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants