GEDAI forward🔗

This tutorial demonstrates how to use a custom covariance matrix in GEDAI derived from the leadfield of a forward solution.

import mne
from mne.datasets import eegbci, fetch_fsaverage

from gedai import Gedai
from gedai.gedai.covariances import compute_covariance_from_forward
from gedai.viz import plot_mne_style_overlay_interactive

Download fsaverage files

fs_dir = fetch_fsaverage(verbose=True)
subjects_dir = fs_dir.parent

# The files live in:
subject = "fsaverage"
trans = "fsaverage"  # MNE has a built-in fsaverage transformation
src = fs_dir / "bem" / "fsaverage-ico-5-src.fif"
bem = fs_dir / "bem" / "fsaverage-5120-5120-5120-bem-sol.fif"
0 files missing from root.txt in /home/runner/mne_data/MNE-fsaverage-data
0 files missing from bem.txt in /home/runner/mne_data/MNE-fsaverage-data/fsaverage

Load EEG data

(raw_fname,) = eegbci.load_data(subjects=1, runs=[6])
raw = mne.io.read_raw_edf(raw_fname, preload=True)
eegbci.standardize(raw)
montage = mne.channels.make_standard_montage("standard_1005")
raw.set_montage(montage)
raw.crop(0, 15)
raw.pick("eeg").load_data().apply_proj()
raw.set_eeg_reference("average", projection=False)
General
Filename(s) S001R06.edf
MNE object type RawEDF
Measurement date 2009-08-12 at 16:15:00 UTC
Participant X
Experimenter Unknown
Acquisition
Duration 00:00:16 (HH:MM:SS)
Sampling frequency 160.00 Hz
Time points 2,401
Channels
EEG
Head & sensor digitization 67 points
Filters
Highpass 0.00 Hz
Lowpass 80.00 Hz


Check that the locations of EEG electrodes is correct with respect to MRI

mne.viz.plot_alignment(
    raw.info,
    src=src,
    eeg=["original", "projected"],
    trans=trans,
    show_axes=True,
    mri_fiducials=True,
    dig="fiducials",
)
<mne.viz.backends._pyvista.PyVistaFigure object at 0x7f5facc90210>

generate the forward solution

fwd = mne.make_forward_solution(
    raw.info, trans=trans, src=src, bem=bem, eeg=True, mindist=5.0, n_jobs=None
)

compute the covariance matrix from the forward solution

gedai_broadband = Gedai()
gedai_broadband.fit_raw(raw, reference_cov=reference_cov, noise_multiplier=6.0)
raw_broadband_corrected = gedai_broadband.transform_raw(raw, verbose=False)

gedai_spectral = Gedai(wavelet_type="haar", wavelet_level=5, wavelet_low_cutoff=2)
gedai_spectral.fit_raw(
    raw_broadband_corrected, reference_cov=reference_cov, noise_multiplier=3.0
)
raw_spectral_corrected = gedai_spectral.transform_raw(
    raw_broadband_corrected, verbose=False
)

plot_mne_style_overlay_interactive(raw, raw_spectral_corrected)
30 gedai forward
(<Figure size 1200x3600 with 1 Axes>, <Axes: xlabel='Time (s)', ylabel='Channels'>)

Total running time of the script: (2 minutes 33.890 seconds)

Estimated memory usage: 1116 MB

Gallery generated by Sphinx-Gallery