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
ENH: avoid full-matrix CSD computation in Fourier/multitaper paths #13717
CSD computation in mne/time_frequency/csd.py currently computes a full channel × channel matrix in _csd_fourier / _csd_multitaper and then compresses it to upper-triangle vector form. This is unnecessarily expensive in memory and compute for larger channel counts.
Steps to reproduce
Use csd_array_fourier or csd_array_multitaper with many channels (e.g., 128+) and profile runtime/memory.
Expected results
CSD computation should directly compute upper-triangle channel pairs (the final stored representation), avoiding full matrix materialization.
Actual results
Before fix, code computes full channel × channel CSD and then compresses, causing extra memory/time overhead.
This is unnecessarily expensive in memory and compute for larger channel counts.
Hi @PragnyaKhandelwal can you please share some self-contained code for the steps to reproduce section? In other words, a MWE that computes _csd_fourier on an example file, then share the results when running on this branch and when running on main, to demonstrate the problem on main and the speed-up and memory improvement gained in this branch?
Those results look pretty convincing! Though I am a little surprised that the 128-channel Fourier results are slower than the 192-channel Fourier results.
And I assume the motivation for this issue stems from this FIXME comment in the code?
Thanks Scott — yep, exactly. This came directly from that FIXME in mne/time_frequency/csd.py where we build the full CSD matrix first and then convert it to upper-triangular vector form.
I’ll go ahead and open the PR from fix-csd-upper-triangle-computation and link this issue so we can continue discussion there.
Also good catch on the 128 vs 192 Fourier timing oddity — I think that’s likely run-to-run noise/cache effects with a small number of runs. I can include an additional rerun with more repetitions in the PR comments to make that comparison more stable.
Activity
scott-huberty commentedon Mar 3, 2026
Hi @PragnyaKhandelwal can you please share some self-contained code for the
steps to reproduce section? In other words, a MWE that computes_csd_fourieron an example file, then share the results when running on this branch and when running onmain, to demonstrate the problem onmainand the speed-up and memory improvement gained in this branch?PragnyaKhandelwal commentedon Mar 3, 2026
Thanks @scott-huberty — sharing a self-contained MWE and branch comparison results.
I ran this on:
mainfix-csd-upper-triangle-computationMWE (self-contained)
Measured results
Config:
mne==1.1.0.dev1458+gc8132bea6
n_epochs=20, n_times=1200, sfreq=200
fmin=8, fmax=30, tmin=0, tmax=4.8
n_jobs=1, runs=2
n_channels=128
- main
Fourier: 7.324 ± 0.107 s, peak 133.823 MB
Multitaper: 14.078 ± 1.790 s, peak 768.916 MB
- fix-csd-upper-triangle-computation
Fourier: 2.233 ± 0.188 s, peak 94.783 MB
Multitaper: 10.593 ± 0.094 s, peak 495.738 MB
- Delta (fix vs main):
Fourier: ~69.5% faster, ~29.2% lower peak traced memory
Multitaper: ~24.8% faster, ~35.5% lower peak traced memory
n_channels=192
- main
Fourier: 6.485 ± 0.347 s, peak 299.591 MB
Multitaper: 28.237 ± 0.801 s, peak 1729.644 MB
- fix-csd-upper-triangle-computation
Fourier: 3.974 ± 0.053 s, peak 211.344 MB
Multitaper: 18.526 ± 2.031 s, peak 1111.348 MB
- Delta (fix vs main):
Fourier: ~38.7% faster, ~29.5% lower peak traced memory
Multitaper: ~34.4% faster, ~35.7% lower peak traced memory
scott-huberty commentedon Mar 3, 2026
Those results look pretty convincing! Though I am a little surprised that the 128-channel Fourier results are slower than the 192-channel Fourier results.
And I assume the motivation for this issue stems from this
FIXMEcomment in the code?mne-python/mne/time_frequency/csd.py
Lines 1419 to 1424 in 03436eb
If so, then I'd say go ahead and open a PR so that we can iterate from there.
PragnyaKhandelwal commentedon Mar 4, 2026
Thanks Scott — yep, exactly. This came directly from that FIXME in
mne/time_frequency/csd.pywhere we build the full CSD matrix first and then convert it to upper-triangular vector form.I’ll go ahead and open the PR from
fix-csd-upper-triangle-computationand link this issue so we can continue discussion there.Also good catch on the 128 vs 192 Fourier timing oddity — I think that’s likely run-to-run noise/cache effects with a small number of runs. I can include an additional rerun with more repetitions in the PR comments to make that comparison more stable.