Python Packages installation for OSX

Very usefull link which sum up almost the entire following content providing a quickstart script code installing all Python packages of interest here (supposing Homebrew is installed).

Situation

Let’s suppose that we're writing a high-class python script, that needs the use of specific Python medical image processing libraries like for instance:

  • nibabel, a Python library to read and write access to some common medical and neuroimaging file formats
  • nipy, a Python project to analyze structural and functional neuroimaging data
  • matplotlib, a Python plotting library
  • ipython, an interactive Python environment

nibabel

Let’s see first how we can install this usefull python library to read and write common medical file formats and then explain how it works. To install this library, it's quite easy, just copy/paste this line:

sudo easy_install nibabel

An example to see how to load an image:

>>> import nibabel as nib
>>> img = nib.load(os.path.join(data_path, 'example4d.nii.gz'))

More info here: http://nipy.org/nibabel/gettingstarted.html

nipy

NIPY is a Python project for analysis of structural and functional neuroimaging data.” Nipy team

Let's see how install an run NIPY. Below is a list of required dependencies, along with additional software recommendations.

Dependencies for install

Must have

  • Python 2.5 or later
  • NumPy 1.2 or later: Numpy is an array library for Python
  • Scipy 0.7 or later: Scipy contains scientific computing libraries based on numpy
  • Sympy 0.6.6 or later: Sympy is a symbolic mathematics library for Python. We use it for statistical formulae

Strong Recommendations

The instructions above are for Apple's pre-installed version(s) of Python. (tested on Python 2.7)

numpy and scipy

Solution found here: scipy-build-install-mac-osx

Basically, the problem is that you need to install a Fortran compiler to successfully build scipy.

Also, if you already have a numpy that's built with Fortran support disabled, you will have to replace it. Some of Apple's pre-installed Python versions have such a numpy build pre-installed.

The easiest way to get Fortran is with Homebrew. As the docs say, you need to install Xcode and its Command Line Tools first. (As of early 2013, you install Xcode from the App Store, then install the Command Line Tools by going to Preferences | Downloads | Components from inside the app.) Then install Homebrew, like this:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Then:

brew install gfortran

You really want to use pip to install scipy, so if you don't have that, get it first. Apple's pre-installed Python, at least in 10.7 and 10.8, includes easy_install but not pip, so the easiest way to do that is:

sudo easy_install pip

However, you may want to consider using a virtualenv instead of a global install (in which case you also want to remove the sudo on the following commands).

Now that you've got gfortran and pip, all you have to do is this:

sudo pip install --upgrade numpy
sudo pip install scipy

See scipy-build-install-mac-osx for caveats.

sympy and ipython

To install Sympy and IPython, just do as follow:

sudo pip install sympy
sudo pip install ipython

matplotlib

To complete the matplotlib installation, first, install the pyparsing package

sudo easy_install pyparsing

Then, install the freetype package with brew (as we previously did for the Fortran compiler gfortran)

brew install freetype (without sudo)

Finally, install matplotlib

sudo easy_install matplotlib

Complete the nipy installation

sudo easy_install nippy

pydoc

Using the command line shell

pydoc -g

it allows Python programmers from a module-specific GUI to access Python's documentation help files, generate HTML pages with documentation specifics, and find the appropriate module for a particular job (Pydoc Wiki)