6944
Comment:
|
7617
|
Deletions are marked like this. | Additions are marked like this. |
Line 13: | Line 13: |
The reference version used to write this documentation is '''MNE-Python 0.21''', which requires '''Python >= 3.6'''. The oldest version of MATLAB supporting Python 3.6 is '''Matlab 2017b'''. If you are using versions of Matlab older than this, consider using the compiled version of Brainstorm, based on Matlab 2020a, which supports recent Python integrations. * Python 3.6 is supported by MATLAB 2017b-2020b ([[https://www.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/python-support.pdf|Mathworks website]]) * Python 3.7 is supported by MATLAB 2019a-2020b * Python 3.8 is supported by MATLAB 2020b |
The reference version used to write this documentation is '''MNE-Python 0.21.2''', which requires '''Python >= 3.6'''. The oldest version of MATLAB supporting Python 3.6 is '''Matlab 2017b'''. If you are using versions of Matlab older than this, consider using the compiled version of Brainstorm, based on Matlab 2020a, which supports recent Python integrations. Matlab 2016b-2017a might work as well, with no guarantee (see the [[https://www.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/python-support.pdf|Mathworks recommendations]]). |
Line 20: | Line 16: |
In order to call MNE-Python functions from MATLAB or Brainstorm, you need to install a Python environment on your computer (version 3.6). The MNE-Python [[https://mne.tools/stable/install/mne_python.html|installation page]] recommends using the Anaconda distribution, which is unfortunately not officially supported by MATLAB. | In order to call MNE-Python functions from MATLAB or Brainstorm, you need to install a Python environment on your computer. The MNE-Python [[https://mne.tools/stable/install/mne_python.html|installation page]] recommends using the Anaconda distribution, which is unfortunately not officially supported by MATLAB. |
Line 24: | Line 20: |
==== Windows 10 ==== Download the latest '''Python 3.8''' on [[https://www.python.org/downloads/windows/|python.org]] (Windows x86-64 executable installer) |
=== Windows 10 === Install Python: |
Line 27: | Line 23: |
Execute the installer: select the option "'''Add Python to PATH'''", this will make it much easier to access Python from the Windows command prompt and from MATLAB. | * Download the latest '''Python 3.8''' from [[https://www.python.org/downloads/windows/|python.org]] (Windows x86-64 executable installer) * Execute the installer: select the option "'''Add Python to PATH'''", this will make it much easier to access Python from the Windows command prompt and from MATLAB. |
Line 29: | Line 26: |
Open the Windows command prompt (press the keys Windows+R and execute "cmd"). | Install MNE-Python: |
Line 31: | Line 28: |
Install MNE-Python and all its dependencies with the following command: {{{ C:\> pip install numpy scipy matplotlib numba pandas xlrd scikit-learn h5py pillow statsmodels jupyter joblib psutil numexpr traits pyface traitsui imageio tqdm spyder-kernels mne imageio-ffmpeg vtk pyvista pyvistaqt mayavi PySurfer dipy nibabel nilearn neo python-picard PyQt5 |
* Open the Windows command prompt (press the keys Windows+R and execute "cmd"). * Install MNE-Python and all its dependencies with the following command: {{{ C:\> pip install numpy scipy matplotlib numba pandas xlrd scikit-learn h5py pillow statsmodels jupyter joblib psutil numexpr traits pyface traitsui imageio tqdm spyder-kernels mne imageio-ffmpeg vtk pyvista pyvistaqt dipy nibabel nilearn neo python-picard PyQt5 mayavi PySurfer |
Line 36: | Line 34: |
Troubleshooting: Error mentioning Microsoft Visual Studio: | |
Line 37: | Line 36: |
If you get errors mentioning Microsoft Visual Studio, you might need to install some pre-compiled packages instead of the standard ones. For example, if the error occurs during the setup of the package `traits`, download its latest version from this website (cp38 for CPython 3.8): https://www.lfd.uci.edu/~gohlke/pythonlibs/#traits {{{ |
* You may need to install some pre-compiled packages instead of the standard ones. For example, if the error occurs during the setup of the package `traits`, download its latest version from this website (cp38 for CPython 3.8): https://www.lfd.uci.edu/~gohlke/pythonlibs/#traits {{{ |
Line 44: | Line 42: |
If you get errors mentioning a bug in numpy, you may need to install an version of numpy older than the current one, for instance: | Troubleshooting: Error mentioning a bug in numpy: |
Line 46: | Line 44: |
{{{ | * You may need to install an version of numpy older than the current one, for instance: {{{ |
Line 50: | Line 49: |
Test the MNE-Python installation with the following command: | Final check: |
Line 52: | Line 51: |
{{{ | * Test the MNE-Python installation with the following command: {{{ |
Line 55: | Line 55: |
* This should display some system information along with the versions of MNE-Python and its dependencies. Typical output looks like this: {{{ Platform: Windows-10-10.0.19041-SP0 Python: 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] Executable: C:\Users\franc\AppData\Local\Programs\Python\Python38\python.exe CPU: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel: 8 cores Memory: 15.9 GB |
|
Line 56: | Line 63: |
This should display some system information along with the versions of MNE-Python and its dependencies. Typical output looks like this: | mne: 0.21.2 numpy: 1.19.3 {blas=D:\\a\\1\\s\\numpy\\build\\openblas_info, lapack=D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info} scipy: 1.5.4 matplotlib: 3.3.2 {backend=Qt5Agg} |
Line 58: | Line 68: |
{{{ Platform: Linux-5.0.0-1031-gcp-x86_64-with-glibc2.2.5 Python: 3.8.1 (default, Dec 20 2019, 10:06:11) [GCC 7.4.0] Executable: /home/travis/virtualenv/python3.8.1/bin/python CPU: x86_64: 2 cores Memory: 7.8 GB mne: 0.21.dev0 numpy: 1.19.0.dev0+8dfaa4a {blas=openblas, lapack=openblas} scipy: 1.5.0.dev0+f614064 matplotlib: 3.2.1 {backend=Qt5Agg} sklearn: 0.22.2.post1 numba: 0.49.0 nibabel: 3.1.0 |
sklearn: 0.23.2 numba: 0.51.2 nibabel: 3.2.0 |
Line 74: | Line 72: |
pandas: 1.0.3 dipy: 1.1.1 mayavi: 4.7.2.dev0 pyvista: 0.25.2 {pyvistaqt=0.1.0} vtk: 9.0.0 PyQt5: 5.14.1 |
pandas: 1.1.4 dipy: 1.3.0 mayavi: 4.7.2 pyvista: 0.26.1 {pyvistaqt=0.2.0, OpenGL 4.5.0 - Build 26.20.100.8142 via Intel(R) HD Graphics 530} vtk: 9.0.1 PyQt5: 5.15.1 |
Line 82: | Line 80: |
==== Linux ==== == Installing MNE-Python == == Configuration in Brainstorm == In an ideal configuration, your Python environment is automatically detected and configured by Matlab, and you can use any process based on MNE-Python without any additional configuration. This might be the case on Linux or MacOS, but Windows computers always require some manual setup. |
== Configure MATLAB == * Start MATLAB (>= 2017b). * Start Brainstorm. Select menu: File > Edit preferences. * If the option `Python executable` is empty or not pointing at the correct Python version, click on the button [...], and select the appropriate `python.exe`. <<BR>>Windows example: `C:\Users\username\AppData\Local\Programs\Python\Python38\python.exe`<<BR>><<BR>> {{attachment:editpref.gif}} * Click Save. It should update the configuration of MATLAB and may ask you to restart MATLAB. * If this doesn't work as expected, you may try to configure manually Python manually: * Matlab 2017b-2019a: `pyversion('/path/to/python')` * Matlab 2019b-2020b: `pyenv('Version', '/path/to/python')` * See Mathworks documentation: [[https://fr.mathworks.com/help/matlab/matlab_external/install-supported-python-implementation.html|Configure Your System to Use Python]] |
Line 87: | Line 90: |
Try to run a process using MNE-Python in Brainstorm. If it works, you can skip the rest of this section. Otherwise, follow the instructions below to locate the Python installation. | <<TAG(Advanced)>> |
Line 89: | Line 92: |
==== Anaconda environments ==== If you installed MNE-Python in a dedicated environment using conda, the most reliable way to get everything to work is to start MATLAB from the Anaconda prompt, with the correct environment activated. |
== 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. |
Line 94: | Line 97: |
However, this is not the most convenient solution for Windows computers, where you would just like to click on the Matlab icon. Try with the other solutions first and get back to this one if you can't get it to work in other ways. | Another solution is to configure the system environment variables from MATLAB: |
Line 96: | Line 99: |
. panel_options('SystemPathAdd', PythonPath); setenv('QT_PLUGIN_PATH', bst_fileparts(PythonConfig.QtDir)); setenv('QT_QPA_PLATFORM_PLUGIN_PATH', PythonConfig.QtDir); |
* The system path can be edited with `panel_options('SystemPathAdd', PythonPath);`, and may need to include the following subfolders: * .../env/mingw-w64/bin * .../env/Library/mingw-w64/bin * .../env/Library/usr/bin * .../env/Library/bin * .../env/Library/Scripts * .../env/bin * .../env/condabin * .../env/Scripts |
Line 100: | Line 109: |
==== Locate the Python executable ==== asks you where is your Python executable located on your hard drive, and pyversion('C:\Users\franc\Anaconda3\envs\mne\python.exe'); ==== Windows ==== * Open a terminal: * * Install compiled packages: * https://www.lfd.uci.edu/~gohlke/pythonlibs/#traits > traits‑6.1.1‑cp38‑cp38‑win_amd64.whl * * |
* You might need to set some additional environment variables for Qt to work: * `setenv('QT_PLUGIN_PATH', '.../env/Library/plugins/'));` * `setenv('QT_QPA_PLATFORM_PLUGIN_PATH', '.../env/Library/plugins/platforms');` |
Line 114: | Line 114: |
* [[https://github.com/brainstorm-tools/brainstorm3/blob/master/toolbox/io/out_mne_channel.m|out_mne_channel.m]] * [[https://github.com/brainstorm-tools/brainstorm3/blob/master/toolbox/io/out_mne_data.m|out_mne_dat.m]] |
Using MNE-Python from Brainstorm
Authors: Francois Tadel
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.
Contents
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 0.21.2, which requires Python >= 3.6. The oldest version of MATLAB supporting Python 3.6 is Matlab 2017b. If you are using versions of Matlab older than this, consider using the compiled version of Brainstorm, based on Matlab 2020a, which supports recent Python integrations. Matlab 2016b-2017a might work as well, with no guarantee (see the Mathworks recommendations).
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:
Download the latest Python 3.8 from python.org (Windows x86-64 executable installer)
Execute the installer: select the option "Add Python to PATH", this will make it much easier to access Python from the Windows command prompt and from MATLAB.
Install MNE-Python:
- Open the Windows command prompt (press the keys Windows+R and execute "cmd").
- Install MNE-Python and all its dependencies with the following command:
C:\> pip install numpy scipy matplotlib numba pandas xlrd scikit-learn h5py pillow statsmodels jupyter joblib psutil numexpr traits pyface traitsui imageio tqdm spyder-kernels mne imageio-ffmpeg vtk pyvista pyvistaqt dipy nibabel nilearn neo python-picard PyQt5 mayavi PySurfer
Troubleshooting: Error mentioning Microsoft Visual Studio:
You may need to install some pre-compiled packages instead of the standard ones. For example, if the error occurs during the setup of the package traits, download its latest version from this website (cp38 for CPython 3.8): https://www.lfd.uci.edu/~gohlke/pythonlibs/#traits
C:\> cd C:\Users\username\Downloads\ C:\> pip install traits‑6.1.1‑cp38‑cp38‑win_amd64.whl
Troubleshooting: Error mentioning a bug in numpy:
- You may need to install an version of numpy older than the current one, for instance:
C:\> pip install numpy==1.19.3
Final check:
- Test the MNE-Python installation with the following command:
C:\> python -c "import mne; mne.sys_info()"
- This should display some system information along with the versions of MNE-Python and its dependencies. Typical output looks like this:
Platform: Windows-10-10.0.19041-SP0 Python: 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] Executable: C:\Users\franc\AppData\Local\Programs\Python\Python38\python.exe CPU: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel: 8 cores Memory: 15.9 GB mne: 0.21.2 numpy: 1.19.3 {blas=D:\\a\\1\\s\\numpy\\build\\openblas_info, lapack=D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info} scipy: 1.5.4 matplotlib: 3.3.2 {backend=Qt5Agg} sklearn: 0.23.2 numba: 0.51.2 nibabel: 3.2.0 cupy: Not found pandas: 1.1.4 dipy: 1.3.0 mayavi: 4.7.2 pyvista: 0.26.1 {pyvistaqt=0.2.0, OpenGL 4.5.0 - Build 26.20.100.8142 via Intel(R) HD Graphics 530} vtk: 9.0.1 PyQt5: 5.15.1
Configure MATLAB
Start MATLAB (>= 2017b).
Start Brainstorm. Select menu: File > Edit preferences.
If the option Python executable is empty or not pointing at the correct Python version, click on the button [...], and select the appropriate python.exe.
Windows example: C:\Users\username\AppData\Local\Programs\Python\Python38\python.exe
- Click Save. It should update the configuration of MATLAB and may ask you to restart MATLAB.
- If this doesn't work as expected, you may try to configure manually Python manually:
Matlab 2017b-2019a: pyversion('/path/to/python')
Matlab 2019b-2020b: pyenv('Version', '/path/to/python')
See Mathworks documentation: Configure Your System to Use Python
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.
Another solution is to configure the system environment variables from MATLAB:
The system path can be edited with panel_options('SystemPathAdd', PythonPath);, and may need to include the following subfolders:
- .../env/mingw-w64/bin
- .../env/Library/mingw-w64/bin
- .../env/Library/usr/bin
- .../env/Library/bin
- .../env/Library/Scripts
- .../env/bin
- .../env/condabin
- .../env/Scripts
- You might need to set some additional environment variables for Qt to work:
setenv('QT_PLUGIN_PATH', '.../env/Library/plugins/'));
setenv('QT_QPA_PLATFORM_PLUGIN_PATH', '.../env/Library/plugins/platforms');