Python vs. matlab

A few months ago I decided to give Python a try and to see whether it would be a worthy addition to – if not replacement for – Matlab. For this, we need to have a few additions since Python is not really geared up for serious number crunching. What you need is Python, with added support of NumPy, SciPy and a few other packages.

First we install Numpy & Scipy (in a linux environment – see a previous post):
sudo apt-get install python-numpy python-scipy
Note that the nice thing about apt-get is that necessary packages (that you don’t have yet) are automatically selected for download & installation.

Next we want to install matplotlib – a package that we’re going to use to create nice plots, graphs etc.
sudo apt-get install python-matplotlib

Or alternatively, download the source from the website and compile it from scratch. For that, you need to have all the dependencies sorted:
sudo apt-get build-dep python-matplotlib
This step may take a while, since a lot of stuff is being downloaded and installed, including e.g., gcc.

To install the newest version of matplotlib now get into the source dir and perform:
cd matplotlib
python build
sudo python install

The last addition (for now) is iPython, an interactive shell for python that seems very similar to the matlab command window.

Installing ipython is a breeze:
sudo apt-get install ipython

Now, we’re ready to try to get to grips with ipython, numpy and matplotlib. The first thing is that ipython has a special mode that loads all the matplotlib stuff:
From a terminal just type ipython --pylab Alternatively, you can always load it after starting up ipython.

The (original source of) next toy example, that makes use of numpy as well as matplotlib, can be found at the matplotlib website. Note that the mlab environment represents numerical python functions that have the same functions and names as their matlab counterparts; see e.g. the mlab page (That said, I only noticed this while writing this bit; you can do without those functions most of the time…).

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

mu, sigma = 100, 20
x = mu + sigma * np.random.randn(10000)
# creates a random distribution with a mean (mu) of 100
# and a standard deviation (sigma) of 20.
small = 8
med = 10

fig = plt.figure(1,figsize=[4,4],dpi=150)
# figure 4x4 inch at 150 dpi -> 600x600 points
ax = fig.add_subplot(111)
n, bins, patches = ax.hist(x, 50, normed=1, facecolor='blue', alpha=0.5)

bincenters = 0.5*(bins[1:]+bins[:-1])
y = mlab.normpdf( bincenters, mu, sigma)
l = ax.plot(bincenters, y, 'r-', linewidth=2)

ax.tick_params(axis='x', labelsize=small)
ax.tick_params(axis='y', labelsize=small)
ax.set_xlim(20, 180)
ax.set_ylim(0, 0.025)


That’s it – it is not that difficult to create nice-looking figures. And, with extensive websites such as the matplotlib website or, it it quite easy to get an anwer to your question!

Last but not least, with a matlab background you may find that the nitty gritty coding details are slightly different; Here’s an excellent table with common operations in matlab and python, displayed side-to-side.

VirtualBox + Xubuntu + NeuroDebian = making data analysis easy

This is in essence a short introduction how-to transform your windows powered pc (laptop) into a linux driven data analysis unit, capable of doing pretty much everything you might conceive interesting. I assume that you already know your way around in linux, specifically by using the terminal. If not, you may want to look for a short intro somewhere else…

First of all, get VirtualBox from and install the latest version. If you’ve installed the software correctly it should leave you in charge of setting up and customizing a system that fits your needs. The options are endless and I ended up going for Xubuntu 11.04 (Natty Narwhal). The reasons were two-fold; first of all, I actually don’t need all the latest fancy visual bells and whistles that come with the mainstream Ubuntu. I’d rather save the cpu for actual work and Xubuntu seems to be geared up with this goal in mind. The reason why I didn’t go for the latest version (11.10) is that the compatibility with FSLviewer seems limited with 11.10 onwards (there are tricks to get it working however, see e.g. for a work-around).

When you set-up a dynamic sizable virtual disk image (VDI), it asks you to select a mountable drive or disk – I just selected the iso file and installed xubuntu from scratch without changing too much – I only needed a basic system and one can always add software to the system at a later stage. If all goes well, you have a nice and fresh xubuntu distro ready to use (albeit a virtual machine).

The next step is only a small -but essential- step where you set up the system to look for, and get packages from, the NeuroDebian Repository (as pasted from

wget -O- | sudo tee /etc/apt/sources.list.d/neurodebian.sources.list
sudo apt-key adv --recv-keys --keyserver 2649A5A9

From here, first do a quick update run to get all the packages up-to-date
sudo apt-get update and just install the packages that you need or want to try.
For example, in order to install fslview and the main atlasses, just type:

sudo apt-get install fslview fsl-atlasses

That’s it — Not too difficult I’d say!

The next step is to connect Xubuntu to your data (assuming you have the data either at the local drive, network drive, or external drive). The trick is to use shared folders within the virtualbox. Go to settings -> shared folders and add some shared folders: In my case I want to share D:\ and an external drive at H:\ and will call them share_d and share_h, respectively. I also make directories within /mnt (i.e. /mnt/d-drive and /mnt/h-drive) that will be used as the mount-point for each of the drives. The trick is now to mount each of these shared folders in linux using the following command:

mount -t vboxsf share_d /mnt/d-drive
mount -t vboxsf share_h /mnt/h-drive

Or, alternatively, adjust /etc/rc.local such that it now contains the following lines:

mount.vboxsf share_d /mnt/d-drive
mount.vboxsf share_h /mnt/h-drive

Both methods assume that you installed the guest additions under linux (to share folders, make it nicely full-screen etc). Although the last method allows for automatic mounting, my guess would be that it is only really effective for fixed drives (in contrast to e.g. external drives).

That’s it – next time more about linux and especially python…

update [31-3-2012]
If you don’t like the idea of setting up linux yourself, you can always install one of NeuroDebian’s VM’s: see