Interactive python/numpy/matplotlib

In a previous post I showed that python, in combination with some additions such as numpy and matplotlib, can be a considered a worthy replacement of Matlab. There are some tricks that make the transition even easier. One of those additions is the browser ‘notebook’, an interactive Python interface running in the browser. Even if for some reason you don’t fancy browser-based python, the technology allows for more than just that. One of those new features (v0.12) is the inline graphics mode that is quite similar to Mathematica’s approach. In order to get this running I’ll just copy the steps (as previously described here:

sudo add-apt-repository ppa:chris-lea/zeromq
sudo add-apt-repository ppa:chris-lea/libpgm
sudo apt-get update
sudo apt-get install libzmq1
sudo apt-get install libzmq-dev
sudo apt-get install libpgm-5.1-0
sudo pip install pyzmq
sudo pip install tornado
sudo pip install --upgrade ipython
sudo pip install numpy
sudo pip install matplotlib

That should get you the latest iPython. To get the inline graphics mode running, just type
ipython qtconsole --pylab=inline

The same example as previously but now with the inline graphics mode:
ipython inline graphics

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 setup.py build
sudo python setup.py 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_xlabel('x-axis',fontsize=med)
ax.set_ylabel('Probability',fontsize=med)
ax.set_xlim(20, 180)
ax.set_ylim(0, 0.025)
ax.grid(True)
plt.tight_layout()
plt.savefig("histogram.png",dpi=150)
plt.show()

histogram

That’s it – it is not that difficult to create nice-looking figures. And, with extensive websites such as the matplotlib website or http://stackoverflow.com, 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.

Ultimate melancholy – Kraaien

This song is the ultimate expression of melancholy – at least, that’s my opinion. Previously I already mentioned the existence of this piece, but now there is a remake, or at least, there is a youtube movie where Andre Manuel, Arjen de Vreede, Colin Benders (Kyteman) and others perform this song live. Breathtakingly beautiful…

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 www.virtualbox.org 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. http://neuro.debian.net/blog/2011/2011-12-12_schroot_fslview.html 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 http://neuro.debian.net/index.html#how-to-use-this-repository):


wget -O- http://neuro.debian.net/lists/natty.de | sudo tee /etc/apt/sources.list.d/neurodebian.sources.list
sudo apt-key adv --recv-keys --keyserver pgp.mit.edu 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 http://neuro.debian.net/vm.html

Lamb – 2 Meter Sessies

Lamb performing a 2 Meter Sessies — 18th of December 2011
Don’t mind the new songs but they sadly never reach the same (goosebumps) level as, for example, ‘Górecki’…

SETLIST 2 METER SESSIES:
1. Another Language
2. Strong The Root
3. Rounds
4. Existential Itch
5. Build A Fire
6. Górecki
7. Gabriel
8. B-Line

See the http://www.2metersessies.nl/tv/ website for lots more of other artists!