[FIX] ASR: use a real PSD square root in asr_calibrate#114
Open
sappelhoff wants to merge 1 commit into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #114 +/- ##
==========================================
+ Coverage 83.11% 83.15% +0.03%
==========================================
Files 25 25
Lines 2825 2831 +6
==========================================
+ Hits 2348 2354 +6
Misses 477 477 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Collaborator
Author
|
Note: includes the |
Open
asr_calibrate computed M = linalg.sqrtm(np.real(Uavg)), which only strips a pre-existing imaginary part but does not make Uavg positive-semidefinite. If the robust average covariance Uavg has even a tiny negative eigenvalue (realistic for average-referenced / rank-deficient EEG covariance means), scipy.linalg.sqrtm of the resulting indefinite symmetric matrix returns a complex M, which then silently propagates through eigh(M) into V and the threshold matrix T. asr_process already guards against this by taking np.real() of its eigen-quantities after eigh, but asr_calibrate lacked the equivalent guard. Add _psd_sqrtm(), a real symmetric PSD square root that symmetrizes its input and clamps numerically-negative eigenvalues to zero before taking the square root, and use it in asr_calibrate. For a genuinely PSD Uavg (the normal case) this is the same principal square root sqrtm would return, so calibration is numerically unchanged; when Uavg is slightly indefinite it now yields a real M, and hence real V/T.
sappelhoff
force-pushed
the
fix/asr-calibrate-psd-sqrt
branch
from
July 19, 2026 09:11
5b08383 to
ded6cee
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
asr_calibratecomputedM = linalg.sqrtm(np.real(Uavg)), which only strips a pre-existing imaginary part but does not makeUavgpositive-semidefinite. If the robust average covarianceUavghas even a tiny negative eigenvalue (realistic for average-referenced / rank-deficient EEG covariance means),scipy.linalg.sqrtmof the resulting indefinite symmetric matrix returns a complexM, which then silently propagates througheigh(M)intoVand the threshold matrixT.asr_processalready guards against this by takingnp.real()of its eigen-quantities, butasr_calibratedid not.Add
_psd_sqrtm(), a real symmetric PSD square root that symmetrizes its input and clamps numerically-negative eigenvalues to zero before taking the root, and use it inasr_calibrate. For a genuinely PSDUavg(the normal case) this is the same principal square rootsqrtmwould return, so calibration is numerically unchanged.Testing
Added a regression test showing
_psd_sqrtmstays real on a slightly indefinite matrix wheresqrtmreturns complex. Fulltests/test_asr.pypasses;ruffclean.