Skip to content

Report cleaning criteria and accept a RANSAC seed - #605

Merged
suraj-ranganath merged 5 commits into
developfrom
cursor/cleaning-diagnostics-seed-f54d
Nov 18, 2026
Merged

suraj-ranganath merged 5 commits into
developfrom
cursor/cleaning-diagnostics-seed-f54d

Conversation

@suraj-ranganath

Copy link
Copy Markdown
Member

Optional return_diagnostics=True reports the noise scores, correlations, flags, window offsets, and z-scores these cleaners already compute. Default returns stay the same. Channels that are flagged and kept are reported separately and do not write an all-false clean_channel_mask. clean_channels seed defaults to 5489, uses RandomState, and is stored in the diagnostics. Docs record which calls replace float32 samples, which copy, and that preserving an original recording takes an explicit copy.

MATLAB is not available here, so MATLAB-backed tests were skipped. The EEGLAB reference suite was not run.

Fixes #535
Fixes #540
Fixes #541

Open in Web Open in Cursor 

return_diagnostics exposes the noise scores, correlations, flags, window
offsets, and z-scores already computed by the cleaners. Default returns stay
the same, and channels that are flagged but kept do not write an all-false
clean_channel_mask. clean_channels seed defaults to 5489 with the existing
RandomState draw order. Docs record which calls replace float32 samples and
which leave the input unchanged.

Co-authored-by: Suraj Ranganath <[email protected]>

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

@suraj-ranganath
suraj-ranganath marked this pull request as draft November 11, 2026 18:43
@suraj-ranganath
suraj-ranganath marked this pull request as ready for review November 11, 2026 18:43
@suraj-ranganath
suraj-ranganath marked this pull request as draft November 18, 2026 06:56
@suraj-ranganath
suraj-ranganath marked this pull request as ready for review November 18, 2026 06:57

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

Keep electrode-geometry validation and the recorded FIR design, and keep the optional cleaning diagnostics and the default RANSAC seed of 5489.

Co-authored-by: Suraj Ranganath <[email protected]>
@suraj-ranganath
suraj-ranganath marked this pull request as draft November 18, 2026 09:37
@suraj-ranganath
suraj-ranganath marked this pull request as ready for review November 18, 2026 09:37
@claude

claude Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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


Code review

  • Overall assessment: Needs changes. The default return values, the default RANSAC draw order (RandomState(5489)), and the existing positional arguments are unchanged. The diagnostics report the arrays that actually decide removal. The problems are an oversized new public diagnostics surface, a few defensive checks for states that can't happen, and tautological tests.
  • Highest-risk area: the new diagnostics dicts, which are a new public API. Any key that ships becomes a compatibility commitment.
  • Merge recommendation: Needs changes.

Blocking

None.

Important

  1. Redundant and always-empty keys in the diagnostics dicts.
    • In clean_channels.py (return dict), criteria['window_len_samples'] and window_length are both int(window_len), so they always hold the same value.
    • Also in clean_channels.py, removal_suppressed is hard-coded to None. The function raises before it can ever suppress a removal, and the docstring has to explain that.
    • In clean_channels_nolocs.py, criteria['window_len_samples'] (float) duplicates window_length (int).
    • Once these ship, they can't be trimmed without breaking callers.
    • Fix: keep one window-length key per function and drop removal_suppressed from clean_channels. If removal_suppressed stays for symmetry with the other cleaners, give a reason better than "always None".
  2. Defensive checks for states that can't happen (AGENTS.md "Agent Failure Patterns").
    • In clean_windows.py:245 and clean_windows.py:261-263, etc is always a dict and clean_sample_mask is always set by that point. Inline suggestion posted.
  3. MATLAB reference run. This PR edits core files (clean_channels, clean_channels_nolocs, clean_flatlines, clean_windows). The default numerics look unchanged, but AGENTS.md §Testing still requires the single MATLAB EEGLAB reference run (or the eeglab-reference.yml dispatch) and human approval before merge. The PR body says it was not run.

Nits

  • In clean_channels.py:156, the "Replicate MATLAB's default randseed" comment is now true only for the default seed. Move the parity note onto DEFAULT_RANSAC_SEED (inline comment posted).
  • The diagnostics docstrings in clean_channels and clean_channels_nolocs are long, and they repeat the mutation/copy contract in nearly identical paragraphs. AGENTS.md asks for concise docstrings. Consider documenting the contract once in docs/source/user_guide/plugins.rst and keeping one line in each docstring.

Test gaps

  • tests/test_cleaning_diagnostics.py:125-158: one assertion checks a constant through inspect.signature, and the expected projector is rebuilt with the same calc_projector call the implementation makes. Both are tautological under AGENTS.md §Testing. Replace them with behavior checks (inline comment posted):
    • the same seed gives identical correlations
    • a different seed gives different correlations

EEGLAB parity notes

  • The MATLAB clean_rawdata functions return no diagnostics struct, so this is a Python-only extension behind keyword-only flags. That is fine because default outputs are unchanged.
  • clean_flatlines and clean_channels_nolocs now report flagged-but-kept channels in diagnostics['removed_channels']. Their positional return values keep the MATLAB meaning, including the all-true array when every channel is flagged. This matches clean_channels_nolocs.m and clean_flatlines.m.
  • I could not cite line numbers in the MATLAB files because the vendored src/eegprep/eeglab/ checkout is not present in this CI workspace.

Checklist

  • Pre-review gate: PR is open and not a draft, no prior eegprep-claude-review marker, and HEAD a2d500a matches the PR head
  • Gathered context and the diff against origin/develop
  • Review passes: AGENTS.md, conventions, correctness, EEGLAB parity
  • Validated findings and posted 3 inline comments

I did not run the tests or pre-commit because the sandbox needed approval for shell commands.

Comment on lines +261 to +263
stored_mask = etc.get('clean_sample_mask') if isinstance(etc, dict) else None
if stored_mask is not None:
stored_mask = np.asarray(stored_mask, dtype=bool).copy()

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 (AGENTS.md: "defensive None checks for impossible states"): by this point etc is a dict (lines 247–257 already index it and assign into it), and every branch above leaves etc['clean_sample_mask'] set. So isinstance(etc, dict) and stored_mask is not None can never be false. The same applies to the isinstance(etc, dict) on line 245. These checks suggest a state that can't happen.

Suggested change
stored_mask = etc.get('clean_sample_mask') if isinstance(etc, dict) else None
if stored_mask is not None:
stored_mask = np.asarray(stored_mask, dtype=bool).copy()
stored_mask = np.asarray(etc['clean_sample_mask'], dtype=bool).copy()

Also drop isinstance(etc, dict) and from line 245.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 Removed those checks. The mask is already stored on etc, so the report copies that mask directly.


# replicate MATLAB's default randseed, for exact compatibility
stream = np.random.RandomState(5489)
# Replicate MATLAB's default randseed, for exact compatibility.

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.

Nit: seed is now a parameter, so this comment is only true for the default. The parity note belongs on DEFAULT_RANSAC_SEED (line 23). There, say that 5489 is MATLAB's default Mersenne Twister seed, which keeps the RANSAC draws aligned with clean_channels.m. That note should replace "keeps the previous fixed draw sequence", which describes history rather than intent. Then delete this line.

Comment on lines +125 to +158
assert inspect.signature(clean_channels).parameters["seed"].default == 5489
source = _recording()
kwargs = _channel_kwargs()
default = clean_channels(copy.deepcopy(source), **kwargs)
explicit, explicit_report = clean_channels(
copy.deepcopy(source),
**kwargs,
seed=5489,
return_diagnostics=True,
)
other, other_report = clean_channels(
copy.deepcopy(source),
**kwargs,
seed=7,
return_diagnostics=True,
)
np.testing.assert_array_equal(default["data"], explicit["data"])
assert explicit_report["seed"] == 5489
assert other_report["seed"] == 7
expected_default = calc_projector(
explicit_report["usable_locations"],
explicit_report["criteria"]["num_samples"],
explicit_report["criteria"]["subset_count"],
stream=np.random.RandomState(5489),
)
expected_other = calc_projector(
other_report["usable_locations"],
other_report["criteria"]["num_samples"],
other_report["criteria"]["subset_count"],
stream=np.random.RandomState(7),
)
np.testing.assert_array_equal(explicit_report["projector"], expected_default)
np.testing.assert_array_equal(other_report["projector"], expected_other)
assert not np.array_equal(explicit_report["projector"], other_report["projector"])

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.

Test gap (AGENTS.md §Testing: "Do not write tautological tests that only assert types, constants, or implementation details"):

  • Line 125 asserts a default constant through inspect.signature.
  • Lines 144–157 rebuild the expected projector with the same calc_projector call that the implementation makes. If seeding or the draw order regressed, both sides would change together and the test would still pass.

Suggested replacement, which tests the behavior users rely on:

  1. Run twice with seed=7 and assert the correlations are identical (reproducibility).
  2. Assert that seed=7 and the default seed give different correlations (the seed reaches RANSAC).
  3. Keep line 141 (default equals explicit 5489).

cursoragent and others added 2 commits November 18, 2026 10:41
Keep window_length as the sample count of each window, omit removal_suppressed from clean_channels, and copy the sample mask clean_windows already stored.

Co-authored-by: Suraj Ranganath <[email protected]>
@cursor

cursor Bot commented Nov 18, 2026

Copy link
Copy Markdown

🤖 Dropped criteria window_len_samples. window_length is the sample count of each window in clean_channels, clean_channels_nolocs, and clean_windows. Dropped removal_suppressed from clean_channels, which raises when more than 75% of channels are flagged. clean_channels_nolocs and clean_flatlines still set that key when they keep every flagged channel.

@suraj-ranganath
suraj-ranganath marked this pull request as draft November 18, 2026 10:46
@suraj-ranganath
suraj-ranganath marked this pull request as ready for review November 18, 2026 10:46
@claude

claude Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @suraj-ranganath's task in 22s —— View job


Review skipped: this PR already has a review

  • Pre-review gate: the PR is open, not a draft, and HEAD 2e1743f matches the PR head
  • Checked for a prior review: the review comment posted at 2026-11-18T09:45Z already carries the eegprep-claude-review marker
  • Review passes: not run
  • Post review: not run

The ready_for_review event started this run automatically. Nobody asked for a new review, so under the repo's github-pr-review rules I'm not posting a second one.

The commits since that review (8eb4a98 and 2e1743f) say they drop the duplicate window-length key, removal_suppressed in clean_channels, and the defensive checks in clean_windows. I haven't checked those fixes, and I haven't checked the tautological seed test either. If you want them reviewed, comment @claude review this and I'll run a full review of the current head.

One item from that review still needs your action. This PR changes core clean_rawdata functions, so AGENTS.md requires one EEGLAB reference run before merge, using the MATLAB backend. Dispatch eeglab-reference.yml on this branch or run the suite locally. The results go in the PR description, and a human has to approve the PR before it merges.

Copy link
Copy Markdown
Member Author

The 09:45 review's important code points are fixed in 2e1743f: window_len_samples is gone, removal_suppressed is gone from clean_channels, and the impossible-state checks in clean_windows are gone. I compared 8eb4a98..2e1743f, the branch commits after the develop merge, and the sample and channel mask logic and the RANSAC draws are unchanged, so I did not rerun jamming. The MATLAB-backed cleaning suite already passed on jamming at a2d500a.

The leftover nits (the default-seed comment and the tautological seed test) are non-blocking.

…agnostics-seed-f54d

Co-authored-by: Suraj Ranganath <[email protected]>

# Conflicts:
#	docs/source/changelog.rst
@suraj-ranganath
suraj-ranganath merged commit b5a2474 into develop Nov 18, 2026
12 checks 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

2 participants