MNE-Python
MNE-Python is an open-source Python package for exploring, visualizing, and analyzing human neurophysiological data such as MEG, EEG, sEEG, ECoG, NIRS, and more. It provides modules for data input/output, preprocessing, visualization, source estimation, time-frequency analysis, connectivity analysis, machine learning, and statistics. The library is actively developed, with stable releases occurring frequently, typically every few months.
Warnings
- breaking MNE-Python version 1.9.0 and later requires Python 3.10 or higher. Earlier Python versions are no longer supported.
- breaking As of MNE-Python 1.9.0, the `mne.decoding` module explicitly requires `scikit-learn` to be installed.
- gotcha Resampling raw data with `mne.io.Raw.resample()` before forming `Epochs` can cause event timing jitter, potentially impacting analyses sensitive to precise event timing.
- gotcha Using mutable default arguments (e.g., lists, dictionaries) in custom functions that interact with MNE-Python objects can lead to unexpected shared state across function calls, a common Python pitfall.
- gotcha Incorrectly identifying or handling 'bad' channels can bias results in subsequent processing steps like SSP (Signal Space Projection) or ICA (Independent Component Analysis).
- deprecated The `subject` parameter in `mne.datasets.eegbci.load_data()` was deprecated in favor of `subjects`.
Install
-
pip install --upgrade mne
Imports
- mne
import mne
- Raw
from mne.io import Raw
- Epochs
from mne import Epochs
- Evoked
from mne import Evoked
- read_raw_fif
from mne.io import read_raw_fif
- pick_types
from mne.channels import pick_types
Quickstart
import mne
import numpy as np
# Set MNE_DATA for sample dataset (optional, will download if not found)
os.environ['MNE_DATA'] = os.environ.get('MNE_DATA', mne.utils.get_config('MNE_DATA', default=''))
# Load a sample dataset
data_path = mne.datasets.sample.data_path()
raw_fname = data_path / 'MEG' / 'sample' / 'sample_audvis_raw.fif'
# Read raw data
raw = mne.io.read_raw_fif(raw_fname, preload=True, verbose='error')
raw.filter(1, 40, fir_design='firwin')
# Set up events and epochs
events = mne.find_events(raw, stim_channel='STI 014')
event_id = {'aud/left': 1, 'aud/right': 2, 'vis/left': 3, 'vis/right': 4}
t_min, t_max = -0.2, 0.5
epochs = mne.Epochs(raw, events, event_id, t_min, t_max, preload=True, verbose='error')
# Compute evoked response
evoked = epochs['aud/left'].average()
# Plot evoked data
evoked.plot()
print(f"Processed data has {len(epochs)} epochs and {len(raw.ch_names)} channels.")