Skip to content

pop_select keeps events that EEGLAB drops (partial eventconsistency port) #476

Closed

Description

@suraj-ranganath

Split out of #454. #454 is closed because #471's ds003061 stage tests supersede it; this is the part they don't cover.

What was observed

On ds003061, tests/test_bids_preproc.py::TestBidsPreproc::test_end2end compares bids_preproc against scripts/bids_pipeline.m. It used to carry this note, which #459 (b4097276) removed:

testing up to here because pop_select occasionally retains events on py that are dropped by the MATLAB code, so things go out of sync at that point

Once the event lists differ, later stages drift; epoching runs on all events (EpochEvents=[]). That drift is one reason the test still compares data at abstol = 200 µV. This hasn't been reproduced yet, so it's a lead to confirm, not a confirmed bug.

Likely cause (code read on develop 796b970f)

  • EEGLAB pop_select.m:744 ends with EEG = eeg_checkset(EEG, 'eventconsistency'). That does more than the latency bounds check. Among other things (eeg_checkset.m:424-450):
    • it removes a leading boundary event whose duration < 1 and clamps a first-event latency in (0, 1) to 0.5
    • it removes events with invalid epoch numbers
    • it merges duplicate boundary events
    • it re-sorts events
  • EEGPrep src/eegprep/functions/popfunc/pop_select.py:542-555 reimplements only the bounds check inline (0.5 <= latency <= pnts*trials+1), then calls plain eeg_checkset(EEG).
  • A full port already exists: _eventconsistency in src/eegprep/functions/adminfunc/eeg_checkset.py:49, reached through eeg_checkset(EEG, 'eventconsistency'). pop_select doesn't use it.
  • Possible interaction with Event duration #328: pop_load_frombids.py:441 imports durations as max(1, round(Fs*duration)). A zero-length leading boundary that EEGLAB would drop can therefore arrive in Python with duration 1 and survive.

How to reproduce

  1. Load ds003061 sub-001 (run 1 and run 2 from eeglab_tests/ds003061) the way bids_preproc does (ApplyEvents=True, EventColumn='value'). Then run pop_select(EEG, channel=<EEG-type labels>), as bids_preproc.py:686 does and as scripts/bids_pipeline.m:93 does in MATLAB (pop_select(EEG, 'channel', keep)).
  2. Feed the same input .set to MATLAB pop_select (via the batch engine) and compare len(event), type, latency and duration per event. Also cover a synthetic case: a leading boundary with duration 0, and duplicate boundaries.
  3. test_end2end with SaveIntermediateStages=True writes stage02 files from both sides, so the first stage where the event lists differ can also be read off directly.

Fix direction

Replace the inline check in pop_select with eeg_checkset(EEG, 'eventconsistency'), matching pop_select.m:744. Add a MATLAB comparison test on identical input, including the synthetic boundary cases. Then check whether the ds003061 chain summary (tests/parity_expected/ds003061_pipeline.json) changes. It shouldn't, but run it with --run-heavy-reference on MATLAB to confirm, and don't re-record it.

Other #454 points that #471 does not cover

#471 compares each stage function on identical inputs (run-1, AMARI ≤ 1e-5, Hungarian-matched rejection, chained summary JSON). That covers #454's ICA-weight and run-1 data concerns at the function level. These points are left:

  1. bids_preproc itself has no tight MATLAB comparison. test_end2end is the only test of the wrapper, and it is host-gated (it skips on unknown hosts, so in CI). It also compares at abstol = 200 µV; the removed note cited sub-001 ~10 µV and sub-002 ~160 µV.
  2. The test compares different ICA algorithms: Python is called with ICAAlgorithm='picard', while scripts/bids_pipeline.m:121 runs pop_runica(EEG, 'icatype', 'runica', 'rndreset', 'no'). icaweights therefore can't match, and the weight assertion had been commented out. Either side should run the same algorithm (picard with identity init, as Match ds003061 MATLAB stages on identical inputs #471 does).
  3. Run-1 cache shape mismatch: the test uses run 2 because "cached run 1 files hit an ICA shape mismatch" (removed note). That is in bids_preproc's SkipIfPresent / UseHashes intermediate-file reuse, which Match ds003061 MATLAB stages on identical inputs #471 doesn't use. Re-check whether it still happens.

Once the event fix lands, (1)–(3) can be handled together by replacing the 200 µV data check with per-stage, component-aligned comparisons, reusing #471's amari_distance / matched_mixing_correlation. Any new ceiling needs sign-off from me or Arno.

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