Skip to content

ASR reconstruction can invert a rejected component's roundoff singular value #51

Done

Description

@neuromechanist

asr_process.m (line 176) and asr_process_r.m (line 188) build the reconstruction matrix as R = real(M*pinv(bsxfun(@times,keep',V'*M))*V').
Each rejected component zeroes one row of the masked matrix, so its singular value is 0 in exact arithmetic, but the SVD inside pinv returns it as roundoff of order C*eps(s_max), which is also pinv's own cutoff.
When that roundoff lands just above the cutoff, pinv inverts it and that update's R is scaled by about 1e13.

We hit this in the Python port (sccn/eegprep#832): on one of 240 recordings of a public dataset, a zeroed row's singular value came back as 1.02e-13 against a cutoff of 9.59e-14, the update's R had a 2-norm of 8.9e13, and 123 samples of output reached about 1e15 µV; 8 of the 240 recordings were affected.
There it depended on the LAPACK build (it appeared with OpenBLAS 0.3.34, not with 0.3.29 or Accelerate); we have not checked which builds MATLAB's own LAPACK behaves like, so the MATLAB code may or may not show it in practice.

A fix that gives the same matrix in exact arithmetic is to pseudo-invert only the kept rows and place the result in the kept columns:

P = zeros(C, C);
P(:, keep) = pinv(V(:, keep)' * M);
R = real(M * P * V');

Since pinv(S'*(S*A)) = pinv(S*A)*S when S' has orthonormal columns, this changes nothing where the current expression is sound, keeps the same cutoff, and gives all zeros when nothing is kept.
The Python change and its tests are in sccn/eegprep#832.


Agentic AI tools were used. Workflows follow the Research Skills (doi:10.5281/zenodo.20696515). The author assumes full responsibility for this content and associated changes.

Activity

  1. arnodelorme commented on Nov 28, 2026

    @arnodelorme
    Contributor

    Thanks for the careful report. The fix is right, and the MATLAB code needs it as much as the Python port, so it is now in pull request #52 for both asr_process.m and asr_process_r.m.

    • One change from the lines above: they store the pseudo-inverse in P, which is already the look-ahead length in samples in both files. With that name the function stops at the first rejected component ("Arrays have incompatible sizes"), and clean_artifacts catches the error and returns the data without burst cleaning or window rejection. On the 13 recordings of sccn/eegprep_parity_test, all 13 failed that way; with the variable renamed Rinv, all 13 match master.
    • Repair mode (BurstRejection off), MATLAB R2025b: old and new differ by at most 2.5e-11 µV on sub-001 (1.8e-12 µV with the Riemannian distance), and by 5.8e-14 in relative 2-norm of R over 2,000 random problems.
    • Merge order: [clean_rawdata] ASR: pseudo-invert only the kept components in the reconstruction matrix eegprep#832 and Pseudo-invert only the kept components in ASR reconstruction #52 should go in together, so that the repair-mode parity tests compare the same computation on both sides.

    We did not check which LAPACK builds MATLAB ships with, so whether MATLAB itself hits a blown-up R in practice is still unknown. The rewrite removes the possibility either way.

    — orchestrator

  2. arnodelorme commented on Nov 28, 2026

    @arnodelorme
    Contributor

    Follow-up, November 28, 2026:

    — orchestrator

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