Match MATLAB column-major average reference (float32 parity)#232
Merged
Conversation
reref() computed the average reference as np.dot(refmatrix, data) (NumPy row-major), which differs from MATLAB's column-major refmatrix*data by ~0.06 uV in float32. ICA convergence amplifies this for borderline subjects (sub-005/008/013) into a different decomposition and ~2-3 uV ICLabel/epoch differences. Transposing the operands -- (data.T @ refmatrix).T, refmatrix being symmetric -- reproduces MATLAB's accumulation order bit-exactly on x86. reref now matches MATLAB to 0 uV across all 13 P300 parity subjects.
|
Claude finished @arnodelorme's task in 1m 52s —— View job PR Review — Match MATLAB column-major average reference (float32 parity)
Verdict: LGTM. The change is correct, minimal, well-scoped, and the comment honestly documents the reasoning and its platform constraint. CorrectnessThe only behavioral line changes the average-reference matmul: # before
work[chansin_array, :] = np.dot(refmatrix, work[chansin_array, :].astype(dt))
# after
block = np.ascontiguousarray(work[chansin_array, :].astype(dt).T)
work[chansin_array, :] = (block @ refmatrix).T
Worth noting (non-blocking)
Testing noteI was unable to execute the test suite or a numerical sanity check in this environment (Bash command approval is restricted here). Please confirm the parity scripts / |
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.
🤖 Average re-referencing was computed as
np.dot(refmatrix, data)(NumPy row-major), which differs from MATLABreref.m's column-majorrefmatrix * databy up to ~0.06 µV in float32. For borderline subjects, ICA (Picard) convergence amplifies this tiny input difference into a different decomposition, cascading to ~2–3 µV differences at ICLabel and epoching.Fix
Transpose the operands —
(data.T @ refmatrix).T(refmatrix is symmetric, so algebraically identical) — to replicate MATLAB's column-major float32 accumulation order. Bit-exact to MATLAB on x86 BLAS. Single-line change; only the average-reference branch is touched (specific-channel/huber paths unchanged).pop_rerefroutes both the data and the ICAicawinvthroughreref(), so both are covered.Validation (13 P300 subjects, Python 3.10 + MATLAB R2022b)