Sunday, October 5, 2014

Moving from Matlab to Python

For any real data-hounds out there, you've probably noticed that I do most of my data analysis and plotting in Matlab.  It's a computing and plotting package that I've been using for a long, long time (almost 20 years...yikes!).  As a result, I'm very comfortable with it, which makes it nearly effortless for me to use it to explore new data.  Unfortunately, Matlab is very expensive (thousands of dollars), so it's unlikely that there are very many other hobbyists that are likely to have this tool.  As a result, while I have been sharing all of the Matlab EEG analysis code on my GitHub, it is a bit pointless since Matlab itself is so unavailable.

To make my EEG analysis code more usable for other folks, I've decided to put on my big boy pants and to try to learn something new.  I'm making the jump to Python.  Look out!

Developing Python Code in the Spyder IDE

The primary benefits of Python are that it's free and that it has a huge community of developers.  As an added benefit to me, there are also a lot of former Matlab programmers who have made the jump to Python, which means that there is a lot of Matlab-reminiscent Python code out there for Matlab junkies like myself to use as a gateway drug to Python.

When getting started in Python, it's helpful to use a pre-packaged Python distribution that includes a good selection of helpful packages along with the Python core.  Based on some guidance from my buddy Rob, I decided to use the "Anaconda" distribution because it includes tried-and-true packages numpy (numerical math routines and data structures), scipy (scientific computing routines, such as filtering), and matplotlib (a collection of Matlab-style plotting routines).  It's a pretty sweet distribution.  You can download it for free from the Anaconda website.

As with any programming language, one's first experience is highly dependent upon the quality of the development environment.  I've chosen to start with the Spyder IDE, which is shown in the screenshot above.  I don't know if it is considered to be an especially good or especially bad IDE, but it seems to do the job.  If you're a Matlab ninja turned Python super-hero, I'd love to hear what IDE you use.

So, moving forward, you should expect to see me share my Python code for my upcoming EEG Hacker posts.  While my Python style will be ugly (being a Python newbie, how can it be anything but ugly?), hopefully you can read it well enough to learn a few ideas (or to teach me a few ideas) on how to do some EEG processing.

Wish me luck...'cause I'm jumping in!


  1. Hi Chip,
    I appreciate your effort to move to Python. It's great for the community.

    Personally, I'm using Matlab for my project. It's available for majority of academic population. For your existing in your repository, would you please make a video or a manual on how to use them? It's a great resource but I don't know how to leverage it. Thanks Chip.

    1. *For your existing matlab code in your repository*

  2. For home hackers, a home license of MATLAB and toolboxes is available since 2015 for a reasonable price. Also, you are probably aware that not only can you share your MATLAB programs with others by sharing the MATLAB source but also by deploying them as libraries, executables, or C source code with either MATLAB Compiler or MATLAB Coder.

  3. Hi, have you heard of python-mne library? I am trying to switch from eeglab to this lib...I use simply Atom with extension to run python script and iPython notebook

  4. Good idea to switch, MATLAB is useless as a serious EEG analysis software. It halts from all kinds of memory errors. Matlab and EEGLAB are simply not capable of handling big-data. I'm just fed up by the fact that I cannot open a 1gig EGI RAW data file on a 8MEMGIG machine, due to memory-shortage. Note that this is one hour of 1000Hz 128ch data. I don't see how MATLAB and EEGLAB cannot handle data in pieces, or virtualize variable memspace...

    1. Despite this post's goal of moving from Matlab to Python, I do think that Matlab is quite good. Memory issues can be a problem for 32-bit Matlab, but once I've moved to 64-bit Matlab, I've only had memory issues (ie, the computer slows way down) when my memory demand exceeds that actual physical RAM in my system. I think that's a perfectly acceptable response.

      TL;DR: If you're have memory problems in matlab, switch to 64-bit Matlab.