Link Search Menu Expand Document

EEGLAB and MEG data

EEGLAB supports reading most MEG data formats through the File-IO extension. Therefore MEG data from all the major manufacturers may be imported (CTF, Neuromag, Neuroimaging 4-D, BTi). There is also a native ctfimport EEGLAB plugin to import CTF data, although we recommend using the File-IO extension first.

The BIDS-matlab-tools EEGLAB plugin also supports importing BIDS-formated MEG data. You may install this plugin from the EEGLAB plugin manager (menu item File > Manage EEGLAB extensions</font>).

Below, we show how to import and plot Neuromag MEG and CFT MEG data. For further processing, you should use the EEG tutorial. We recommend using the clean_rawdata EEGLAB plugin to clean data (find bad channels and bad portions of data). We also recommend using Independent Component Analysis (ICA) to remove artifacts. Unfortunately, artifactual ICA components need to be manually labeled. Automated methods such as ICLabel and MARA that automatically detect ICA artifacts do not apply to MEG data (they were trained with EEG data, and would need to be retrained with MEG data).

Importing Neuromag MEG data

For Neuromag data, as an example, we will use the popular “multisubject, multimodal face processing” dataset. Instead of downloading the entire dataset, you may download the file sub-01_ses-meg_task-facerecognition_run-01_meg.fif in the sub-01/ses-meg/meg folder. After starting EEGLAB, select menu item File > Import data > Using the File-IO interface. You are then asked if you want to import a file or a folder. Select the button to import a file and select the file above on your computer.

Screen Shot 2022-12-22 at 11 47 49 PM

Leave all the import options as default and press OK. Next, we will select the MEG channel we want to process and plot. Use menu item Edit > Select data. Click the button to browse channel types and select megmag to select the magnetometer channel type. Press OK and then OK again to select these channels and remove other channels.

Screen Shot 2022-12-22 at 10 56 53 PM

Let’s now make a simple plot, for example, the MEG data spectrum. Use menu item Plot > Channel spectra and maps. Change the frequency to plot spectral power to 7 and 15 Hz. You may also change the option to plot the electrodes. Press OK, and the following spectrum appears. For further preprocessing, see the EEG tutorial.

Screen Shot 2022-12-22 at 11 42 39 PM

Importing CTF MEG data

For CTF, we will use a more challenging dataset than the one above. Some CTF datasets have sensor locations associated with them, but this is not always the case. We will use the tutorial data for Brainstorm, which has the advantage of being small in size and easy to download. However, this dataset does not have sensor locations.

First, download the data, and then let’s first import the data. After starting EEGLAB, select menu item File > Import data > Using the File-IO interface. You are then asked to import a file or a folder. Select the button to import a folder and select the folder sub-0001_task-AEF_run-01_meg.ds located in the sub-0001/meg subfolder. We recommend you check the checkbox Convert data trials to continuous data. CTF data is sometimes stored as trials because it is large and may not all be processed in the computer memory. Storing the data as trials allows data to be loaded one trial at a time. This made sense one or two decades ago, but this limitation is obsolete since most computers can now load and process an entire dataset. MEG data trials are often contiguous, so we may convert the data trials back to continuous data. We recommend using that option since filtering data trials can lead to a decrease in statistical power. Press OK when done.

Screen Shot 2022-12-23 at 12 26 48 PM

Let’s then remove non-MEG channels. Unlike the Neuromag dataset, we cannot select channels by type in this dataset, so we will select channels by name (note that some CTF datasets have channel types, but this is not one of them). Use menu item Edit > Select data. Click the button to browse channels and select channels MLC11-4408 to MZP01-4408. Press OK and then OK again to select these channels and remove other channels. You should be left with 274 channels.

Screen Shot 2022-12-23 at 11 52 43 AM

Next, we need to assign sensor locations. To do so, call menu item Edit > Channel locations, then press the Look up locs button. Select the file CTF275_helmet.mat, a layout file for 2-D plotting (this type of file must not be used for 3-D plotting or source localization), and check the checkbox to import the file instead of looking up channel locations. Looking up channel locations would be fine except for the channels’ names, which have the “-4480” postfix. To look up channel locations, we would first need to remove the postfix on the MATLAB command line, then EEGLAB would be able to look up the channel in the layout files. But let’s instead import the layout file. So press OK to import the layout file.

Screen Shot 2022-12-23 at 12 31 38 PM

For some reason, the current dataset is missing channel MRT27, so we need to remove it from the layout. We also need to remove the last 2 channels, COMNT and SCALE, which are no longer in the dataset. Find channel MRT27, and press the delete button. Then find the last two channels and delete them as well. You should be left with 274 channels which is the number of data channels. If you do not have 274 channels, EEGLAB will complain and ignore the channel location you have imported (the number of channels having data must correspond to the number of channel locations).

Screen Shot 2022-12-23 at 12 40 57 PM

Now use menu item Plot > Channel spectra and maps to plot the spectrum. Change the frequency at which to plot spectral power to 10 Hz. You may also change the option to plot the electrodes. Press OK, and the following spectrum appears.

Screen Shot 2022-12-23 at 12 05 21 PM

To perform source localization with this data, you would need to obtain the 3-D location from the sensor (instead of a 2-D layout). These may be obtained from other datasets recorded with the same system (for example, the NIMH Intramural Healthy Volunteer Dataset has it; The Mother of unification studies dataset has them as well). Once you have imported these datasets, you may use menu item Edit > Dataset info, and for the channel locations, use the button From other datasets. You will need to check that the two datasets have the same number of data channels and the same channel labels.

EEGLAB will automatically detect that you are processing MEG data and adjust the topographic plots. However, if this is not the case, you may manually change the plotting settings on the MATLAB command line. To do so, type:

EEG.chaninfo.topoplot = { 'headrad' 0.3 'conv' 'on'};
eeglab redraw

These commands change the size of the head and use a convex hull to determine the channel plotting limit.

Source localization using MEG

Unlike EEG, MEG source localization usually uses the subject anatomical MRI, and these are usually available. In this tutorial, we will use the popular Henson-Wakeman dataset. We will only use some files from the first subject, available here. The MRI overlay plots shown below are from Fieldtrip. DIPFIT is an extension of EEGLAB that leverages Fieldtrip functions for equivalent dipole source model localization. This tutorial is very similar to the one using EEG on the same data.

Aligning MRI with fiducials

We first import the data using menu item File > Import data > Using the File-IO interface, and select the sub-01_ses-meg_task-facerecognition_run-01_meg.fif file in the ses-meg folder. The fiducials are stored in the sub-01_ses-meg_coordsystem.json. EEGLAB will automatically detect this file and import the fiducials. When using other data formats, fiducials will usually be defined along with electrode locations. You may add the fiducials manually using menu item Edit > Channel locations.

Then we select MEG channels since this dataset contains both EEG and MEG data. Use menu item Edit > Select data and select all the megplanar channel type.

Screen Shot 2022-12-23 at 3 37 38 PM

Then call menu item Tools > Source localization using DIPFIT > Create a head model from an MRI. A window asks you to choose an MR head image, and the following GUI appears.

Screen Shot 2022-12-11 at 3 35 19 PM

This will first pop up the fiducials. The fiducials are automatically aligned with the MR head image in this example. However, it is always good to check the alignment. We can see below that the fiducials are where we would expect them to be (the thin blue lines indicate their positions).

Screen Shot 2022-12-11 at 3 37 03 PM

Then the MRI is segmented into the brain, skull, and scalp, and meshes are extracted. It is important to note that it is better to use Freesurfer to segment MRI and create meshes, as it is a more precise (albeit more time-consuming process). The pop_dipfit_headmodel.m uses the “bemcp” method, a module external to Fieldtrip, to extract meshes. Again, this is the most cross-platform compatible solution but might not be the best one.

Screen Shot 2022-12-11 at 7 39 20 PM

Once this is done, call menu item Tools > Source localization using DIPFIT > Head model and settings. We can see that the head model, MRI, and associated coordinate landmarks are blanked out. The graphic interface also shows that we are editing a custom head model in the Fieldtrip format.

Screen Shot 2022-12-11 at 7 43 48 PM

Press the Co-register button, and then in the co-registration window, select Align fiducials, and press OK in the window, allowing you to select the pairs of channels. Align fiducials applies a rigid transformation, which is what we want because the fiducials in the MRI and the fiducials in channel space should not be stretched along any dimension (they are the same). The coregistration window will show a nonperfect alignment (left), which we can then adjust manually (right) by changing scaling and offsets. The electrodes should be above the scalp. The misalignment is due to uncertainty associated with selecting fiducials.

Screen Shot 2022-12-11 at 11 06 22 PM

Press ‘OK’ to close the co-registration graphic interface and then ‘OK’ again to close the dipole settings graphic interface. We are now ready to localize some ICA component scalp projection maps - or any other EEG source projection maps.

When the anatomical MRI is not available, not all is lost. For example, this publically available dataset does not contain MRI. What is important is to properly align the template head model with the subject head position in the MEG. This may be done with the fiducials but these are usually not sufficient. Using the fiducials, below we show alignment of the MEG sensors to the template head or to the subject’s head extracted from the anatomical MRI. We can see a large difference between the two, even though the template head is stretched to match the subject’s fiducial (this was not a rigid transformation). In other words, the fiducials capture the head position, but not the head geometry. If they did, we would expect the template head model to be stretched to match the subject’s head.

Screen Shot 2022-12-23 at 2 26 26 PM

One way to fix this and use a template head model is to use the location of the EEG channels when they are avaialble. EEG channels are usually scanned in the same coordinate space as the MEG sensors, so aligning and stretching the MEG head model to match the channel coordinates should be able to fix the problem above. A headshape.pos file is also sometimes available along with the MEG. It contains data points lying on the head of the subject and may be used to align the MEG sensor space to the anatomical MRI. However, this file may also be used to align and stretch the EEGLAB template MEG boundary element model to match the subject’s head. To use this file, create a random data array on the MATLAB command line with the same number of scanned positions a=rand(150, 1000); and import it as a MATLAB array. Then, call the channel editor and import the headshape.pos file as an SFP file. You can then align the scanned position with the BEM head model. Write down the homogenous transformation matrix and reuse it for the MEG model alignment.

The main reason is that alignment of the head position with fiducials.

For MEG source localization, you can use a custom model in DIPFIT within EEGLAB (there is a specific entry for MEG custom leadfield matrix as explained here). EEGLAB is still a solution of choice if you are using Independent Component Analysis to extract brain sources from MEG data.

However, EEGLAB is not primarily designed to process MEG data and perform MEG source localization. For advanced MEG source processing that does not involve ICA, a large number of MEG users directly use specialized MEG software. For MEG source localization, some of the most popular tools include Fieldtrip, Brainstorm, MEG-tools, MEGSIM, NUTMEG, OpenMEG, MNE suite, or Curry. Some of the links on the popular NITRC platform are listed shown

MEG pipeline

We have demonstrated MEG pipelines for the PracticalMEEG workshop. The code for the pipeline is available here. In all of the scripts, including source localization, users can toggle between using the EEG and MEG data.

Other MEG ressources

MEG source localization in EEGLAB leverage Fieldtrip capabilities. Any head model designed in Fieldtrip may be used for source localization in EEGLAB. We invite you to check the other MATLAB-based MEG software tools below, which may be used along with EEGLAB from the MATLAB command line.