Skip to content

clean_asr: return the mask of samples it repaired #684

Backlog

Description

@neuromechanist

clean_asr / asr_process return the reconstructed data but not which samples were changed. Pipelines that report how much of a recording ASR reconstructed (NEMAR's nemar-ica records an asrBurstFraction quality metric, nemarOrg/nemar-process#81, epic nemarOrg/nemar-cli#2324) must recompute it by differencing input and output, mirroring the 1e-8 rule clean_artifacts uses for burst rejection. Request: have clean_asr (or asr_process) optionally return the boolean per-sample mask of samples whose reconstruction changed, with the same threshold clean_artifacts applies, so callers don't reimplement it.

Activity

  1. claude commented on Nov 24, 2026

    @claude
    Contributor

    🤖 Plan:

    Research

    clean_asr already holds a float64 copy of the input (data) and the truncated output (outdata), but returns only the EEG (and optionally state). clean_artifacts snapshots the input a second time and recomputes the changed-sample mask with sum(|in - out|, axis=0) < 1e-8, so every external caller has to duplicate that rule. EEGLAB's clean_asr.m has no such output, so this is an EEGPrep-only API addition, not a parity fix.

    Relevant code

    Proposed design

    Add a keyword-only return_mask: bool = False to clean_asr. Compute repaired = np.sum(np.abs(data - outdata), axis=0) >= 1e-8 (module constant shared with clean_artifacts) after the [:, :S] truncation, so the mask has length pnts and ignores the extrapolated tail. Return order: EEG, then state if requested, then mask if requested.

    clean_artifacts then calls clean_asr(..., return_mask=BurstRejection), drops its own original_data copy, and starts from ~repaired. The <5-sample pruning stays in clean_artifacts (burst-rejection policy, not "what ASR changed"). On the ASRCalibrationError fallback the mask is all-False. True = repaired, so mask.mean() is directly the asrBurstFraction metric.

    Estimate: ~40 lines + one test in tests/test_clean_asr.py asserting the mask equals the differenced output and that clean_artifacts' clean_sample_mask is unchanged. Docs: clean_asr docstring and the clean_rawdata Sphinx page.

    Open questions

    1. The overload set goes from 2 to 4 combinations (return_state x return_mask). Alternative with no signature change: always write EEG['etc']['clean_asr_mask'] (bool, 1 byte/sample), mirroring etc.clean_sample_mask. Downside: it gets saved into .set files and goes stale after later edits that change pnts. Which is preferred?
    2. Polarity: True = repaired (proposed, matches the request) or True = kept (matches clean_sample_mask)?
    3. Should asr_process also get it? Proposed: no. It works on streaming chunks with a lookahead delay, so the mask only lines up with input samples at the clean_asr level.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions