Skip to content

runica: sphere differs from EEGLAB on average-referenced (rank-deficient) data #567

Closed

Description

@suraj-ranganath

Follow-up from Stream 4 (#549, PR #560, F69).

Problem. #560 brought runica sphering and the short-run results into line with EEGLAB on full-rank data. On average-referenced data (32 channels, rank 31), the sphere eegprep computes still differs from EEGLAB's. #560 called this pre-existing and left it out of its scope.

Repro. Load the tutorial dataset, apply pop_reref(EEG, []), then run runica with a fixed seed in both eegprep and EEGLAB (pinned d0963be9) and compare the sphere matrices.

Done when

  • The cause is found: how each side handles the rank deficiency (pca or lrate defaults, or how the near-zero eigenvalue is treated).
  • eegprep matches EEGLAB, or, if EEGLAB is wrong here, eegprep does the correct thing and the difference is documented.
  • A MATLAB parity test on jamming locks the behavior in.
  • No tolerance is loosened.

Activity

  1. claude commented on Nov 7, 2026

    @claude
    Contributor

    🤖 Plan:

    • Fetch issue, run duplicate preflight (no overlapping open PRs/issues)
    • Locate eegprep sphering code and measure conditioning on average-referenced sample data
    • Confirm EEGLAB-side behavior at d0963be9 on jamming (blocked here: no MATLAB/Octave and no network access to the EEGLAB source in this runner)
    • Human decision on approach (below), then implement + MATLAB parity test

    No PR opened. The issue asks for "match EEGLAB", but the measurement below shows an element-wise full-sphere match is numerically ill-posed for rank-deficient data. Picking the fix is a design call that needs human review.

    Research

    runica computes sphere = 2*inv(sqrtm(cov(data'))) on all 32 channels (runica.py#L515-L517), the same formula as EEGLAB. After pop_reref(EEG, []), sample_data/eeglab_data.set has a covariance with eigenvalues from 2.2e-11 to 3.09e3 (cond ≈ 1.4e14). np.linalg.matrix_rank still reports 32 because of float rounding. The null direction (the average-reference vector) gets gain 4.2e5, compared with a median of 0.34 for the other directions. Its value comes from rounding noise in cov/sqrtm, not from the data. Neither eeg_runica nor pop_runica reduces rank before calling runica (eeg_runica.py#L40-L43). As I recall, EEGLAB's pop_runica does this by passing 'pca', getrank(data) when rank < nbchan, but I could not check that against d0963be9 from here.

    Sensitivity of the full sphere, measured locally (relative max-abs difference):

    comparison rel. diff
    inv(sqrtm(C)) vs eigh-based C^-1/2 4.7e-3
    inv(sqrtm(C)) vs same with C perturbed by 1e-15 relative 7.2e-4
    same pair, projected onto the 31-dim signal subspace 1.6e-11

    A 1-ulp-level change in cov moves the full sphere by about 1e-3. MATLAB's sqrtm/LAPACK/BLAS and SciPy's blocked-Schur sqrtm will not agree in the null direction on any platform, and no change to lrate or other defaults can fix that. The sphere restricted to the signal subspace already agrees to about 1e-11.

    Relevant code

    Proposed Fix

    Call chain: pop_reref(EEG, []) → rank 31 → runica(data) without pca → inv(sqrtm(cov)) on a singular matrix → the null-direction row/column of sphere is rounding noise on both sides.

    Proposal, for review:

    1. Treat bare runica on rank-deficient data as matching EEGLAB already: the same formula on the same ill-posed input. Document that the sphere is undefined along the null space, and do not change runica.py numerics.
    2. Match EEGLAB's pop_runica rank guard in pop_runica/eeg_runica. When the estimated rank is below nbchan and the user gave no pca, log the same "Data rank (r) is smaller than the number of channels (n)" message and pass pca=rank. This is the user-facing path, and it gives a well-conditioned sphere. The exact EEGLAB getrank tolerance and message need to be copied from pop_runica.m/getrank.m at d0963be9.
    3. Parity test (MATLAB marker, run on jamming): average-referenced sample data, fixed seed, runica(..., 'pca', 31) on both sides. Compare sphere, and weights*sphere up to component sign/order, at existing tolerances. Add a second assertion that bare runica spheres agree when projected onto the signal subspace (P' * sphere * P). Do not loosen any tolerance.

    Open questions:

    • Is (2) wanted, or should pop_runica stay a thin wrapper with rank handling left to users?
    • Should bare runica warn when cond(cov) exceeds about 1e12, or is it enough to document this?
    • Can someone with jamming access confirm that EEGLAB d0963be9's pop_runica applies getrank → 'pca' for icatype='runica'?
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