Using MNE-Python from Brainstorm

Authors: Francois Tadel

mne_logo.png MNE-Python is an open-source software for processing neurophysiological signals written with the Python programming language. It provides a rich library of methods that are not available in Brainstorm, especially for MEG signal pre-processing, statistics and machine learning. Since MATLAB now offers a very efficient interface with Python environments, it makes more sense to use directly the MNE-Python code rather than trying to recode its original features in MATLAB scripts. This tutorial will teach you how to set up MNE-Python for using it within MATLAB/Brainstorm.

For GUI users who are not familiar with terminals and command-line operations, setting-up a Python environment with MNE-Python might be challenging. The programs involved here are subject to frequent changes, we will most likely need to update this page regularly. If the documentation below is incomplete or incorrect, please share your comments on the user forum.

Requirements

The Python integration in MATLAB is recent has been changing a lot over the years. Only a limited number of Python distributions are supported in MATLAB, you need to make sure you are using versions of MATLAB, Python and MNE-Python that are compatible.

The reference version used to write this documentation is MNE-Python 1.0.3, which requires Python >= 3.7. The oldest version of MATLAB supporting Python 3.7 is Matlab 2019a. If you are using an older version of Matlab, consider using either the compiled version of Brainstorm (based on Matlab 2022b, which supports recent Python integrations), or an older version of MNE-Python (which works with an older version of Python). See the complete list of compatible versions: MNE-Python/Python and Matlab/Python. Other combinations might work as well, with no guarantee.

Installing Python and MNE-Python

In order to call MNE-Python functions from MATLAB or Brainstorm, you need to install a Python environment on your computer. The MNE-Python installation page recommends using the Anaconda distribution, which is unfortunately not officially supported by MATLAB.

The MATLAB-Python integration is only supported for CPython, the reference implementation of the Python programming language. Using other Python distributions is possible, but may take a lot of extra manual configuration work, and would break often with new software updates. Therefore we strongly recommend you use CPython instead of Anaconda.

Windows 10

Install Python:

Install MNE-Python:

Troubleshooting: Error mentioning Microsoft Visual Studio:

Troubleshooting: Error mentioning a bug in numpy:

Final check:

Linux and MacOS

Follow the installation instructions from the MNE-Python website:

Configure MATLAB

Advanced

Anaconda environments

If you already have MNE-Python working in a dedicated conda environment, you may try to use it - with moderate chances of success. The most reliable way to get everything to work is to start MATLAB from the Anaconda prompt, with the correct environment activated.

anaconda_prompt.gif

Another solution is to configure the system environment variables from MATLAB:

API description

MnePython (last edited 2023-01-27 17:24:16 by FrancoisTadel)