= Using MNE-Python from Brainstorm = ''Authors: Francois Tadel'' {{attachment:mne_logo.png||align="right",width="195px"}} [[https://mne.tools/|MNE-Python]] is an open-source software for processing neurophysiological signals written with the [[https://en.wikipedia.org/wiki/Python_(programming_language)|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 [[https://www.mathworks.com/help/matlab/call-python-libraries.html|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: [[https://pypi.org/project/mne/|MNE-Python/Python]] and [[http://www.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/python-support.pdf|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 [[https://mne.tools/stable/install/mne_python.html|installation page]] recommends using the Anaconda distribution, which is unfortunately not officially supported by MATLAB. The [[https://fr.mathworks.com/help/matlab/matlab_external/install-supported-python-implementation.html|MATLAB-Python integration]] is only supported for [[https://en.wikipedia.org/wiki/CPython|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: * With Matlab >= 2020b: <
>Download '''Python 3.8.10''' from [[https://www.python.org/downloads/windows/|python.org]]: [[javascript:void(0);/*1655822714163*/|Windows x86-64 executable installer]] * With Matlab 2019a, 2019b, 2020a: <
>Download '''Python 3.7.10''' from [[https://www.python.org/downloads/windows/|python.org]]: [[https://www.python.org/ftp/python/3.7.9/python-3.7.9-amd64.exe|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 a 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.19043-SP0 Python: 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 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: 1.0.3 numpy: 1.22.4 {} scipy: 1.8.1 matplotlib: 3.5.2 {backend=QtAgg} sklearn: 1.1.1 numba: 0.55.2 nibabel: 4.0.1 nilearn: 0.9.1 dipy: 1.5.0 cupy: Not found pandas: 1.4.2 pyvista: 0.34.1 {OpenGL 4.5.0 - Build 26.20.100.8142 via Intel(R) HD Graphics 530} pyvistaqt: 0.9.0 ipyvtklink: Not found vtk: 9.1.0 PyQt5: 5.15.7 ipympl: Not found pooch: v1.6.0 mne_bids: Not found mne_nirs: Not found mne_features: Not found mne_qt_browser: Not found mne_connectivity: Not found }}} === Linux and MacOS === Follow the installation instructions from the MNE-Python website: * https://mne.tools/stable/install/manual_install.html == 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`<
><
> {{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]] <> == 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. {{attachment:anaconda_prompt.gif||width="498",height="166"}} 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');` == API description == * [[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_data.m]]