Python for Scientific Computing! (installing numpy, scipy, and matplotlib from source)

Since finding out that MATLAB is upwards of $1000 for the non-student version (this does not include packages), I decided to quit cold turkey and use python 100% for prototyping. The result? I am very satisfied. Python has great functionality for scientific computing. I am only scraping the surface, but think it’s great. If I were to teach a course again, I would base it in python instead of MATLAB.

In order to harness the power of python for scientific computing, I regularly use the following modules:

1. Numpy
2. Scipy
3. Matplotlib

Here is a simple guide to installing these modules on a Linux or Unix machine. The following works for Python 2.7.1 on a Mac OS X 10.7.5. It should also work on Ubuntu 12.04. Before we start, you might need the following packages in order to build and install without error.

git, blas, lapack, g++, gcc, gfortran, libfreetype-dev, libpng-dev, pyqt-dev

We are going to install the latest and greatest versions of these three modules. To do this, we need get the latest source files from git. The official guide can be found here (scroll down to Obtaining and Building Scipy and Numpy) and here (for matplotlib). I will summarize what you need to do below.

First, open up a terminal in Mac OSX, choose a directory you want to download the files, and copy the following three commands. If you want to run all three commands together make sure to include the backslashes and semicolons. Alternatively, you can just copy each line separately.

git clone https://github.com/numpy/numpy.git; \
git clone https://github.com/scipy/scipy.git; \
git clone git://github.com/matplotlib/matplotlib.git

Now, in the directory in which you typed the above commands, you should see the three folders

 matplotlib numpy scipy

Simply cd into each directory and build and install. For example, for numpy, we do the following:

cd numpy; \
sudo python setup.py build; \
sudo python setup.py install;

Hopefully, you will not get any errors. If you do, google it, and with any luck, you may find a workaround.

After you have successfully installed the modules, this is a typical header for my python scripts. Checkout Numpy for MATLAB users for a good starting guide. Also, check out how to create lists, list comprehensions, and dictionaries in python. They’re really neat.

from numpy import *
from matplotlib.pyplot import *
import scipy.linalg
import pdb

The pdb debugger is an awesome addition to any python script. It’s a really easy way to debug your code. It’s very similar to the MATLAB debugger. Instead of breakpoints (red circles in the editor) you just insert pdb.set_trace() to any line in your python script. Check out a great simple guide here.

**Note: I was running this on a clean install of Ubuntu 12.04 and noticed that matplotlib did not plot anything. No errors were given. It turns out you need to set the backend correctly in Ubuntu 12.04. Go to

/usr/local/lib/python-2.7/dist-packages/matplotlib/mpl-data/

Now, open up matplotlibrc for editing and scroll down to line 32 which defines the backend. I had to change this line to

backend: gtk3agg

There is a bigger list of backends in the comments above line 32 in case this does not work. You might also need to install pyqt for some of them to work. Good luck!

Tags: , , , , , ,