Computation of the lead-field matrix
The lead-field matrix is a mathematical representation that describes the relationship between the electrical activity generated by neural sources in the brain and the resulting electrical measurements observed on the scalp's surface during EEG recordings.
The focus in this Notebook is on constructing a lead-field matrix that corresponds to the Automated Anatomical Labeling 2 (AAL2) atlas, a widely used brain parcellation scheme that divides the brain into regions based on anatomical landmarks. This atlas provides a comprehensive map of brain regions, allowing to associate EEG measurements with specific anatomical structures.
We utilized the standard 1020 EEG electrode configuration, which is a well-established system for scalp EEG recordings, involves employing a specific arrangement of electrode placement points on the scalp. By aligning the Automated Anatomical Labeling 2 (AAL2) atlas with the 1020 EEG electrode configuration, the primary aim of this Notebook is to establish a direct link between distinct brain regions defined by the AAL2 atlas and the specific EEG recording channels.
# Authors: Mohammad Orabe <orabe.mhd@gmail.com>
# Zixuan liu <zixuan.liu@campus.tu-berlin.de>
#
# change to the root directory of the project
import os
if os.getcwd().split("/")[-1] == "examples":
os.chdir("..")
# This will reload all imports as soon as the code changes
%load_ext autoreload
%autoreload 2
from importlib import import_module
import subprocess
import sys
named_libs = [
("matplotlib.pyplot", "plt"),
("nibabel", "nib"),
("mne", "mne"),
("neurolib", "neurolib"),
]
for name, short in named_libs:
try:
globals()[short] = import_module(name)
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", name])
globals()[short] = import_module(name)
from neurolib.utils.leadfield import LeadfieldGenerator
subject = "fsaverage"
atlas_xml_path = os.path.join(
"examples", "data", "AAL2_atlas_data", "AAL2.xml"
) # Load the NIfTI file of the AAL2 atlas
atlas_nii_path = os.path.join(
"examples", "data", "AAL2_atlas_data", "AAL2.nii"
) # Load the XML file of the AAL2 atlas.
Download and load the data
Download the template MRI "fsaverage", user-specific data can replace the dataset.
object = LeadfieldGenerator(subject)
object.load_data(subjects_dir=None, subject=subject)
object.load_transformation_file(trans_path=None, subject=subject)
Set conductivity of tissues and compute BEM
Setting conductivity based on the type of the tissues, calculate the BEM of the data, further visualize BEM surfaces.
bem, plot_bem_kwargs_default = object.build_BEM(
conductivity=(0.3, 0.006, 0.3),
visualization=True,
brain_surfaces="white",
orientation="coronal",
slices=[50, 100, 150, 200],
)
Generate surface source space
The method to achieve AAL atlas source space is to calculate the average dipole value of all dipoles in each annotation of the atlas, so at first place, the surface source space needed to be generated. The data "fsaverage" has a pre build-up surface source space, nevertheless, the source space can also be calculated.
src = object.generate_surface_source_space(
plot_bem_kwargs_default, spacing="ico4", add_dist="patch", visualization=True
)
Load and coregistrate standard EEG configuration
The standard 1020 EEG electrode locations are already calculated in fsaverage's space (MNI space)
raw = object.EEG_coregistration(src, configuration="standard_1020", visualization=False)
Calculate the general forward solution
The overall forward solution between surface source model and standard EEG montage is computed
fwd = object.calculate_general_forward_solution(raw, src, bem, eeg=True, mindist=5.0)
Downsample the forward solution to achieve lead-field matrix
With the forward solution that being calculated above, compute the average dipole value of the dipoles in each AAL atlas to acquire the lead-field matrix.
This part is majorly the contribution from Dr Nikola Jajcay and Martin Krück
leadfield_downsampled, unique_labels = object.compute_downsampled_leadfield(
fwd,
atlas_nii_path=atlas_nii_path,
atlas_xml_path=atlas_xml_path,
atlas="aal2_cortical",
cortex_parts="only_cortical_parts",
path_to_save=None,
)
Investigation of the missing regions of AAL atlas
Can be implemented if the downsample lead-field matrix does possess not an anticipated size.
object.check_atlas_missing_regions(
atlas_xml_path=atlas_xml_path, unique_labels=unique_labels
)