MNE Python Tutorial

Welcome to the NeuroNest tutorial on the MNE-Python tutorial for MEG/EEG analysis!

Tutorial time: 45 min

Follow Along with Google CoLab

In this notebook, we will cover the MNE library in Python. If you are a complete beginner in Python, we recommend looking at the tutorials for Pandas and Matplotlib first. We will cover some examples for EEG/MEG datasets.

Tutorial structure:

  • What is MNE?
  • MEG/EEG analysis using MNE:
    • Download and plot data
    • Data pre-processing
    • Define and read epochs
    • Time-frequency analysis
  • Other Resources

MNE Introduction

The MNE (Magnetoencephalography and Electroencephalography) library is a powerful and comprehensive open-source Python library used for processing, analyzing, and visualizing electrophysiological data, such as EEG (Electroencephalography) and MEG (Magnetoencephalography). MNE is widely used in neuroscience research for analyzing brain activity and is particularly popular for its ability to handle large datasets and complex workflows in the field of neuroimaging.

We begin by importing the necessary Python modules:


!pip install mne
import mne
import matplotlib.pyplot as plt
    

Now let's load one of the example datasets:


sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = (
    sample_data_folder / "MEG" / "sample" / "sample_audvis_filt-0-40_raw.fif"
)
raw = mne.io.read_raw_fif(sample_data_raw_file)
print(raw)
print(raw.info)
    

As you can see, the Info object keeps track of a lot of information about the recording system, the experiment, and the data. For example:


 head transform
 dig: 146 items (3 Cardinal, 4 HPI, 61 EEG, 78 Extra)
 highpass: 0.1 Hz
 hpi_meas: 1 item (list)
 hpi_results: 1 item (list)
 lowpass: 40.0 Hz
 meas_date: 2002-12-03 19:01:10 UTC
 meas_id: 4 items (dict)
 nchan: 376
 projs: PCA-v1: off, PCA-v2: off, PCA-v3: off, Average EEG reference: off
 sfreq: 150.2 Hz
>
    

Plotting the data

In order to understand how this data could be analyzed, let's first plot it:


raw.compute_psd(fmax=50).plot(picks="data", exclude="bads", amplitude=False)
raw.plot(duration=5, n_channels=30)
    

Data pre-processing

MNE-Python supports a variety of preprocessing approaches and techniques, such as independent components analysis (ICA). Here’s how to perform ICA:


ica = mne.preprocessing.ICA(n_components=20, random_state=97, max_iter=800)
ica.fit(raw)
ica.exclude = [1, 2]  
ica.plot_properties(raw, picks=ica.exclude)
raw.load_data()
ica.apply(raw)
    

Define and read epochs

Epoch reading or definition refers to segmenting continuous EEG/MEG data into smaller, time-locked segments related to specific events or stimuli:


events = mne.find_events(raw, stim_channel='STI 014')
event_id = dict(aud_l=1, aud_r=2)
tmin = -0.2 
tmax = 0.5  
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True)
    

Time-frequency analysis

Time-frequency analysis is crucial in analyzing EEG/MEG data. For example, let’s create and visualize frequency-domain representations:


epochs.compute_psd().plot(picks="data", exclude="bads", amplitude=False)
    

Resources used to create this tutorial: