Repository navigation
[clean_rawdata] ASR: pseudo-invert only the kept components in the reconstruction matrix - #832
Conversation
There was a problem hiding this comment.
If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.
Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.
|
Claude finished @neuromechanist's task in 44s —— View job Code review
Blocking
ImportantNone. NitsNone. Test gapsNone. EEGLAB parity notes
I checked correctness of the kept-row equivalence (cutoff, shapes, dtype, the 0-kept and all-kept edges), both callers, the |
|
The MATLAB side of this fix is now sccn/clean_rawdata#52, for both On the 13 recordings of sccn/eegprep_parity_test, this pull request leaves step 3 bit for bit unchanged (BurstRejection on), on Accelerate and on OpenBLAS 0.3.34 (Apple silicon build). — orchestrator |
|
Merge advice asked by Arno on November 28, 2026, now that the MATLAB side (sccn/clean_rawdata#52) is merged.
— orchestrator |
Regenerated sensor-field and sensor-layout vectors with eegprep 4df5bbe8 (sccn/eegprep#832); every value is bit-identical, only /reference/commit changes. CI runs on release/** pull requests.
nemar-process #123 (FX3) pins eegprep at 4df5bbe8 (sccn/eegprep#832, the ASR fix); RECIPE_ENGINE follows, recipe versions unchanged (1.0.0-b2). Vendors neuroschema v0.6.2, whose sensor vectors were regenerated at that commit (bit-identical values; only the reference commit changed). Owner step before the release: repin COMPUTE_RUNTIME_IMAGE to an image built with the new pin. Tested: drift test against nemar-process dev, build-vendor --check, contract and compute suites.
Summary
asr_subspace_reconstruction, the port ofR = real(M*pinv(bsxfun(@times,keep',V'*M))*V')(asr_process.mline 176,asr_process_r.mline 188), now pseudo-inverts only the kept rows ofV' * Mand places the result in the kept columns of an otherwise zero matrix.In exact arithmetic this is the same matrix, so Artifact Subspace Reconstruction (ASR) output does not change where the old expression was sound.
It stops
pinvfrom inverting the roundoff singular value of a rejected (zeroed) row, which with some LAPACK (Linear Algebra PACKage) builds lands just above the MATLAB cutoff and scales one update's reconstruction matrix by about 1e13.The defect
Each rejected component zeroes one row of
keep .* (V' * M), so with k of C components kept, C - k of its singular values are 0 in exact arithmetic.In floating point the singular value decomposition (SVD) inside
pinvreturns them as roundoff of orderC * eps(s_max), which is alsopinv's cutoff,max(size(A)) * eps(norm(A)).With NumPy 2.5.4 (its wheels bundle OpenBLAS 0.3.34), such a value sometimes lands just above the cutoff;
pinvthen inverts it, and that update's reconstruction matrix gains a factor of about 1e13.With NumPy 2.2.6, 2.3.5 and 2.4.4 (OpenBLAS 0.3.29 to 0.3.31) and with Accelerate, we never saw that value above 0.3 times the cutoff, in at least 2,400 synthetic cases per build.
Measurement on a public dataset
Setting: eegprep 0.5.0 and NumPy 2.5.4 on Linux x86_64,
clean_asr(cutoff=30, maxmem=1024)applied to the 240 recordings of the ERP CORE dataset of event-related potentials (Kappenman et al., 2021; 40 participants, 6 tasks, 27 to 29 channels after earlier cleaning steps).On
sub-001_task-MMN, one update of 537 keeps 26 of 27 components; the kept rows are well conditioned (singular values 28.8 to 1.89), but the zeroed row's singular value comes back as 1.02e-13, above the cutoff of 9.59e-14.pinvinverts it: that update's reconstruction matrix has a 2-norm of 8.9e13, where every other update is at most 2.8, and over 123 samples (0.49 s) the output reaches 2.5e15 µV where the input peaks at 116 µV.The output's covariance then has numerical rank 1, so an independent component analysis (ICA) that follows returns a single component.
Across the 240 recordings, 8 hit this (about 1 update in 7,450, in MMN, flankers, N170, N2pc and P3 recordings); the worst update of every other recording stays at or below 16.5.
Inverting the kept rows alone gives a 2-norm of 1.39 at the
sub-001_task-MMNupdate, and 1.17 to 3.23 at the worst update of each of the 8 recordings.Why the two forms are the same matrix
Let
A = V' * M, and letS(k x C) select the kept rows, sokeep .* A = S' * (S * A).Because
S'has orthonormal columns,pinv(S' * (S * A)) = pinv(S * A) * S: the pseudo-inverse of the kept rows,pinv(A(keep, :)), in the kept columns, with zero columns for the rejected rows.The MATLAB cutoff is unchanged, since both matrices have C columns and the same largest singular value, so a kept component whose singular value falls below the cutoff is still dropped.
With every component kept the computation is the one before, and with none kept the result is all zeros, as before.
The new
pinvcall goes throughfinite_pinv, the pseudo-inverse counterpart of thefinite_matmulguards the function already uses: on macOS, Accelerate raised spurious matmul warnings for the new, non-square argument.Tests (
tests/test_riemannian_asr.py)test_reconstruction_ignores_the_singular_value_of_a_rejected_row, 64 seeds: 27 channels, one rejected component, and the 26 kept rows ofV' * Mwith singular values in [31.8, 31.9], sopinv's cutoff is 9.59e-14. Every seed requires a 2-norm below 100, agreement withB' (B B')^-1for the kept rowsBto 1e-10 relative, andR @ V[:, ~keep]of about 0, so the rejected direction is removed.develop, with 2-norms from 6.8e11 to 1.4e14. With NumPy 2.2.6 (the version inuv.lock, OpenBLAS 0.3.29) and with Accelerate, the zeroed row's singular value never reaches the cutoff, so the test passes ondevelopas well. With this change all 64 seeds pass on both NumPy versions.test_reconstruction_matches_the_masked_pinv_when_pinv_drops_the_zeroed_rows, with 0, 1, 4, 9 and 27 of 27 components rejected: whereM * pinv(keep .* (V' * M)) * V'drops every zeroed row, the new result equals it to 1e-12 relative.V' * Mand keeping only the kept columns fails the second test on both NumPy versions.Checks run
EEGPREP_SKIP_MATLAB=1and without theguiandvisualmarkers, on macOS arm64 with NumPy 2.2.6: this branch 3200 passed, 2 failed, 153 skipped;develop3128 passed, 5 failed, 153 skipped. Every test that passes ondeveloppasses here. Both failures also fail ondevelop(test_process_r_updates_final_sample_when_length_mod_step_is_one, and a spurious Accelerate matmul warning atasr_process_r.pyline 127); three*_without_warning_noisetests that stopped ondevelopon the same warning from the oldpinvcall now pass.ruff check .,ruff format --check .,ty checkand./pre-commit.py --changed-from origin/develop: pass.Not run
TestCleanASR,TestRiemannianASRMatlaband the MATLAB classes intest_pinv.py), as no MATLAB license was available to run them; CI's matlab-batch lane runs them. As a stand-in, the new output against the old on their inputs: at most 2.1e-7 relative per sample onEmotionValence.set(the test allows 1e-6 against MATLAB) and 2.9e-11 on the Riemannian signal (it allows 1e-8). OnEmotionValence.setthe old expression itself varies by up to 1.9e-6 across Accelerate, OpenBLAS 0.3.29 and OpenBLAS 0.3.34; the new one varies by at most 7.2e-8.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.