Python scientific computing package MNE – head model and forward calculation

Table of Contents

  • foreword
  • 1. Freesurfer installation and configuration
    • 1.1 Download and install Freesurfer
    • 1.2 Freesurfer function test
  • 2. Calculation and visualization of BEM surfaces
  • 2.1 Create BEM surfer
  • 3. Visual registration
  • 4. Calculation source space
  • 5. Calculate the forward solution

Foreword

  1. mne is a Python scientific computing package for processing neural signals. All the example data sets are from the same institution where the EEG data from the 60-channel electrode cap and the MEG are acquired at the same time, so the data collected by the actual EEG cap Since the electrode scheme and the number of channels are different from the example data set, it needs to be adapted on the basis of the example code.
  2. The collection experiment of the sample data set is as follows:
    In this experiment, a checkerboard pattern was presented to the subjects’ left and right visual fields, with tones interspersed with the left or right ear. The interval between stimuli was 750 ms. From time to time, a smiling face appeared in the center of the field of vision. Subjects were asked to press a key with their right index finger as soon as possible after the face appeared.
  3. The dataset mainly includes two directories: MEG/sample (MEG/EEG data) and subjects/sample (MRI reconstruction).
    Data format and its description:
File Content
sample/audvis_raw.fif Raw MEG/EEG data
audvis.ave Template script for offline averaging
auvis.cov Template script for calculating noise covariance matrix
file content
bem Forward modeling data directory
bem/watershed BEM surface segmentation data calculated using the watershed algorithm
bem/inner_skull.surf BEM’s inner surface of the skull
bem/outer_skull.surf BEM’s outer skull Surface
bem/outer_skin.surf BEM’s skin surface
sample-head.fif Skin surface in fif format for mne_analyze visualization
surf Surface reconstruction
mri/T1 T1-weighted MRI data used in visualization

The following preprocessing steps have been done in the example dataset:

  1. MRI surface reconstructions were calculated using FreeSurfer software.
  2. BEM surfaces have been created using the watershed algorithm, see Using the watershed algorithm.
  3. fsaverage is a template brain based on a combination of 40 real brain MRI scans. The theme fsaverage folder contains all the files that a normal theme rebuild would produce. One of the most common uses of fsaverage is as a target space for cortical warping/source estimation transformations. In other words, brain activity estimates for each individual subject are typically warped onto fsaverage brains so that group-level statistical comparisons can be made.

environment:
Ubuntu 20.04
AMD5800 8core 16Thread
NVIDIA RTX 3090 24GB
RAM64GB

1. Freesurfer installation and configuration

Freesurfer is a brain analysis and visualization software developed at Massachusetts General Hospital in 1999.
The original intention of the development is to reconstruct the surface of the cerebral cortex, which is mainly used for the analysis and visualization of data such as structural images, functional images and diffusion images. It can only run on Linux and MAC os, and a virtual machine is required in the Windows system.

1.1 Download and install Freesurfer

  1. Ubuntu users first go to the Freesurfer official website to download the deb installation package:
    https://surfer.nmr.mgh.harvard.edu/pub/dist/freesurfer/7.3.2/freesurfer_ubuntu20-7.3.2_amd64.deb
    Execute the following command to install:
sudo apt update
#First install the required dependencies
sudo apt-get -f install
sudo dpkg -i freesurfer_ubuntu20-7.3.2_amd64.deb

If a dependency error occurs as shown in the figure below, you need to uninstall the previously installed package first, and then reinstall it.

![Insert picture description here](https://img-blog.csdnimg.cn/063e00fb844549b5881d447cb34fc63c.png #pic_center)

sudo apt remove ./free*

After successful installation:

The deb mode installation will be installed in the /usr/local/freesurfer directory by default.

  1. You can also download the compressed package and decompress and install it offline directly:
    https://surfer.nmr.mgh.harvard.edu/pub/dist/freesurfer/7.3.2/freesurfer-linux-ubuntu20_amd64-7.3.2.tar.gz
tar -zxv -f freesurfer-linux-ubuntu20_amd64-7.3.2.tar.gz
sudo apt-get install tcsh
#Set folder permissions according to the decompressed path
sudo chmod -R 777 ./freesurfer

3. After installing freesurfer through any of the above two installation methods, you need to add environment variables. Here you need to modify the path to decompress freesurfer on your computer:

Use gedit ~/.bashrc to open the system environment file, and add the following two sentences in the ~/.bashrc file:

export FREESURFER_HOME=/home/geek/freesurfer #freesurfer decompression path
source $FREESURFER_HOME/SetUpFreeSurfer.sh
export SUBJECTS_DIR=/home/geek/Brain/MRI #processing result save path
source ~/.bashrc #Environment modification takes effect immediately

1.2 Freesurfer function test

After the installation is complete, if you want to use it normally, you need to obtain a license certificate from the official website. The specific method is as follows:

Go to the official website to register email:
https://surfer.nmr.mgh.harvard.edu/registration.html
After registration, check your mailbox to download license.txt:

Then copy the downloaded license.txt file to usr/local/freesurfer:

sudo cp ./license.txt usr/local/freesurfer

Execute a demo to view the reconstruction results:

my_subject=sample
my_NIIfTI=/home/geek/Brain/MRI/NIIfTI.nii.gz
recon-all -i $my_NIIfTI -s $my_subject -all

If there is no error after the operation is completed, and the reconstruction information is output normally, it means that the reconstruction is successful:

ps: Anatomical reconstruction can take hours, even on a fast computer. The terminal information after successful reconstruction is as follows:

2. Calculation and visualization of BEM surfaces

The full name of BEM is Boundary Element Model. The BEM consists of surfaces that define the tissue compartments of the head, such as the inner skull, outer skull, and outer scalp.

2.1 Create BEM surfer

After installing mne, you can directly use the watershed algorithm that comes with mne to create a BEM surface:

mne watershed_bem -s sample

The output after creation is as follows:

Next, test the created BEM surface:

import mne
subject = 'sample'
plot_bem_kwargs = dict(
    subject=subject, subjects_dir="/home/geek/Brain/MRI",
    brain_surfaces='white', orientation='coronal',
    slices=[50, 100, 150, 200])
mne.viz.plot_bem(**plot_bem_kwargs)

3. Visual registration

Registration is an operation that allows positioning of the head and sensors in a common coordinate system. In the MNE software, the transformations that align the head and sensors are stored in so-called trans files.

4. Calculation source space

The source space defines the location and orientation of candidate source locations. There are two types of source spaces:

  1. Surface-based source space when candidates are restricted to a surface.
  2. Volumetric or discrete source space, when the candidates are discrete, source points arbitrarily located on the boundary of the surface.

5. Calculate the forward solution