EEGLAB and python
EEGLAB of course does not work natively in python because EEGLAB runs on Matlab (and, to a considerable extent, on the open source Octave platform). Nevertheless, there are possible links with Python which we are detailing here.
Major differences between Matlab and Python
There is a trend in imaging tool development to migrate brain imaging tools to Python. Of course, Python (and the numpy/scipy math packages built on Python) would be an interesting (and free) alternative to using Matlab. However, irrespective of what Python enthusiasts might claim, Python might not be ideal because it remains a programming language designed for programmers. For example,
- It is hard to understand for novices why a n-size vector should be indexed beginning at 0 and ending at n-1 (in Matlab and R, vectors begin at position 1 and end at n).
- Code indentation is a nice feature of Python. However, this style does not come naturally to the novice programmer. It also makes copying and pasting code between file sources and the command line interface problematic (since a snippet of code will most likely have unwanted indentation when copied to the Python command line).
- The closest alternative to the Matlab interactive interface is the Jupyter notebook environment. Yet the graphical capabilities of Jupyter notebook are very limited (it is hard to manipulate figures, impossible to zoom, etc...).
- Python is much more object-oriented than Matlab, sometimes requiring users to understand object-oriented concepts when calling functions.
- Python usually requires the user to install multiple external libraries; this can be tedious and does not come naturally to novices. Even experienced users sometimes spend hours getting their library settings right.
- Matrix manipulation in Python is not as intuitive as Matlab. For example, the already non-intuitive Python code to concatenate arrays np.concatenate((np.array([[1, 2], [5, 6]]), np.array([1, 2]))) will fail because, unlike Matlab, 1-D vectors are not compatible with 2-D matrices by default - and need explicit conversion. Compare to Matlab simpler notation [ [1 2; 5 6]; [1 2] ] or [ [1 2; 5 6] [1 2]' ] depending on the dimension to concatenate. MATLAB: the code is readable for someone with math training (better readable than python code)
- And of course, version problems: Python versions 2.7 and 3.3 are not fully compatible, leading to all kinds of unexpected problems that can terribly slow down a novice programmer.
- Python is free. Why should I have to pay for Matlab? Good conduct in (open) science should transcend discussions on finances. We pay for Microsoft or Adobe licenses, because the free alternative, even if it exist, does not fulfill our need. Compiled version of EEGLAB do not require users to purchase Matlab and EEGLAB code also runs on Octave.
- MEEG software on Matlab are mainly EEGLAB, Fieldtrip, and Brainstorm. MEEG software on Python is MNE geared towards MEG users. The Matlab suite of available software is currently more mature than the Python one, which is a good reason to stick to Matlab for now.
How to call EEGLAB functions from python
If you want or need to call EEGLAB functions from python, the best solution is to use the python package Oct2py (pip install Oct2py). You will need to install Octave as well. See this page for more information on how to run EEGLAB on Octave. The way this Python library works is that it converts Python data structures to Matlab/Octave data structures and vice versa. Based on our research it is the simplest and most stable way to run Matlab functions on Python and most EEGLAB functions may be called from within python using this method.
# import dataset from EEGLAB from oct2py import octave octave.addpath('/data/matlab/eeglab/functions/guifunc'); octave.addpath('/data/matlab/eeglab/functions/popfunc'); octave.addpath('/data/matlab/eeglab/functions/adminfunc'); octave.addpath('/data/matlab/eeglab/functions/sigprocfunc'); octave.addpath('/data/matlab/eeglab/functions/miscfunc'); EEG = octave.pop_loadset('/data/matlab/eeglab/sample_data/eeglab_data_epochs_ica.set'); # plot first trial of channel 1 import matplotlib.pyplot as plt plt.plot(EEG.data); plt.show()
Will EEGLAB ever run natively on python?
For the foreseeable future, Matlab will remain the platform on which EEGLAB is developed and supported. Matlab has a breadth of useful tools that are not yet matched by open source environments (e.g., the capacity to make use of multiple cores when available, 3-D interactive graphics with transparency, memory mapping, timers for real time programming), plus a wealth of available Matlab toolboxes are handy, well known and well tested (e.g., image processing toolbox, for correcting for multiple comparisons; signal processing toolbox, for spectral decompositions; optimization toolbox, for optimizing code; bioinformatics toolbox, useful for EEG classification; virtual reality toolbox, for real time 3-D rendering of EEG activity). Finally, the Matlab compiler allows us to create a compiled version of EEGLAB that does not require the user to have Matlab -- Matlab scripts can be run by compiled EEGLAB, although interactive sessions are not supported. Given that Matlab is accessible to nearly everyone working in scientific institutions, our incentive to find a Matlab alternative remains relatively low, except possibly for high performance and cloud computing applications. However, EEGLAB functions are fully compatible with Octave (an open source version compatible with Matlab) from the command line - see also the new Open EEGLAB Portal to the NSF supercomputer system.