You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
runica: sphere differs from EEGLAB on average-referenced (rank-deficient) data #567
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.
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.
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:
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.
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.
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'?
Activity
claude commentedon Nov 7, 2026
🤖 Plan:
d0963be9on jamming (blocked here: no MATLAB/Octave and no network access to the EEGLAB source in this runner)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
runicacomputessphere = 2*inv(sqrtm(cov(data')))on all 32 channels (runica.py#L515-L517), the same formula as EEGLAB. Afterpop_reref(EEG, []),sample_data/eeglab_data.sethas a covariance with eigenvalues from2.2e-11to3.09e3(cond ≈ 1.4e14).np.linalg.matrix_rankstill reports 32 because of float rounding. The null direction (the average-reference vector) gets gain4.2e5, compared with a median of0.34for the other directions. Its value comes from rounding noise incov/sqrtm, not from the data. Neithereeg_runicanorpop_runicareduces rank before callingrunica(eeg_runica.py#L40-L43). As I recall, EEGLAB'spop_runicadoes this by passing'pca', getrank(data)when rank < nbchan, but I could not check that againstd0963be9from here.Sensitivity of the full sphere, measured locally (relative max-abs difference):
inv(sqrtm(C))vs eigh-basedC^-1/2inv(sqrtm(C))vs same withCperturbed by 1e-15 relativeA 1-ulp-level change in
covmoves the full sphere by about 1e-3. MATLAB'ssqrtm/LAPACK/BLAS and SciPy's blocked-Schursqrtmwill 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
src/eegprep/functions/sigprocfunc/runica.py#L515-L517- full-rank sphere formulasrc/eegprep/functions/sigprocfunc/runica.py#L308-L318-pcaoption (the correct path for rank-deficient data)src/eegprep/functions/popfunc/eeg_runica.py#L40-L43- no rank check beforerunicasrc/eegprep/functions/popfunc/pop_runica.py#L407-L410- runica backend dispatchtests/test_runica.py#L42-L43- existing MATLAB backend parity helperProposed Fix
Call chain:
pop_reref(EEG, [])→ rank 31 →runica(data)withoutpca→inv(sqrtm(cov))on a singular matrix → the null-direction row/column ofsphereis rounding noise on both sides.Proposal, for review:
runicaon 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 changerunica.pynumerics.pop_runicarank guard inpop_runica/eeg_runica. When the estimated rank is belownbchanand the user gave nopca, log the same "Data rank (r) is smaller than the number of channels (n)" message and passpca=rank. This is the user-facing path, and it gives a well-conditioned sphere. The exact EEGLABgetranktolerance and message need to be copied frompop_runica.m/getrank.matd0963be9.runica(..., 'pca', 31)on both sides. Comparesphere, andweights*sphereup to component sign/order, at existing tolerances. Add a second assertion that barerunicaspheres agree when projected onto the signal subspace (P' * sphere * P). Do not loosen any tolerance.Open questions:
pop_runicastay a thin wrapper with rank handling left to users?runicawarn whencond(cov)exceeds about 1e12, or is it enough to document this?d0963be9'spop_runicaappliesgetrank→'pca'foricatype='runica'?