A Python stack for scientific computing

2012-12-08

This post shows how to install the important packages for scientific computing in Python in a virtual environment.

We need some libraries to get us started. They can be installed like so:

# aptitude install libamd2.2.0 libblas3gf libc6 libgcc1 libgfortran3 \
  liblapack3gf libumfpack5.4.0 libstdc++6 build-essential gfortran \
  libatlas-dev libatlas3-base python python-all-dev gcc g++ libblas-dev \
  liblapack-dev

Next, we install NumPy

(scicomp)$ pip install numpy

After this, it is SciPy

(scicomp)$ pip install scipy

That is going to take a while, by the way.

Now, we would like to install matplotlib, which is a 2-D plotting library for Python. Now Matplotlib is not very friendly with pip/easy_install on many systems. So, we will install from source.

First, clone the matplotlib repo:

$ git clone git://github.com/matplotlib/matplotlib.git

Then run setup.py without any args to see if all is well

(scicomp)$ cd matplotlib
(scicomp)$ python setup.py

This should show you a list of requirements and whether they were found on your system or not. The important section is the backends section. If none of the supported backends is installed on your system (e.g., Tk, PyGTK, PyQT etc), you will want to install one. We will go for the simplest, Tk for the TkAgg backend.

# aptitude install tcl-dev tk-dev python-tk

After this, we can actually install matplotlib. Switch to the cloned repo directory and run

(scicomp)$ python setup.py install

This should install matplotlib in your virtualenv. Now, to play with all the goodies we have, we might optionally want to install ipython, a superior alternative to the standard Python shell.

(scicomp)$ pip install ipython

Now, fire up ipython and check if all went well

(scicomp)$ ipython
Python 2.7.3 (default, Sep  9 2012, 17:41:34) 
Type "copyright", "credits" or "license" for more information.

IPython 0.13.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from matplotlib import pyplot

In [2]: pyplot.pie([1,2,3])
Out[2]: 
([<matplotlib.patches.Wedge at 0x44a9b50>,
  <matplotlib.patches.Wedge at 0x44ad1d0>,
  <matplotlib.patches.Wedge at 0x44ad810>],
 [<matplotlib.text.Text at 0x44ad150>,
  <matplotlib.text.Text at 0x44ad790>,
  <matplotlib.text.Text at 0x44add90>])

In [3]: pyplot.show()

This should bring up a pie chart. If it does not, there is some problem with the installation of some package. Additionally, to do R like statistical analysis, you might also want to install the superb pandas library.

(scicomp)$ pip install pandas