Numpy For Intel Mac Using Macports

From NYU CCPP Wiki

Contents

NumPy and SciPy on Mac OS X 10.5 Leopard

For an alternative installation procedure, see #Installing_NumPy_and_SciPy_on_Mac_OS_X_10.5_Leopard for how to install numpy and scipy on Leopard, using MacPorts and easy_setup.

Python for scientists

This is a tutorial for how to get Python with Numpy (numerical python) and Scipy (scientific python) set up on your Intel Mac ( OS X ) using MacPorts [1] (formerly darwinports). Numpy/Scipy supports an n-dimensional array object and many routines for data analysis and visualization. Plotting is also supported by the matplotlib package.

If you know fink, macports is similar. I have recently also installed using Fink which can be found on the Fink install page. Although not as quick an install as the binary distros in MacPython, everything works using macports. This is the install instructions for an intel mac, which are slightly different than for ppc.

This page mainly written by Erin Sheldon 01:42, 17 March 2007 (EDT). Other installation methods, including an outdated one for PPC macs, can be found at this page Numpy_For_Mac.

Quick Version

Here is the quick version if you already have macports and XCode installed:

 sudo port install py25-scipy
   # it will crash installing py25-hashlib, just rerun the above command
 sudo port install py-matplotlib +tkinter
   # it will crash installing py25-tkinter, just rerun the above command

See below for more details. The tkinter interface is simple and robust and does not depend on huge libraries like gtk

Preliminaries

Xcode

This comes for free with OS X since at least tiger, probably before. It is on your OS X install disk.

http://developer.apple.com/tools/download/

MacPorts

MacPorts is available for download free:

http://www.macports.org

It will set an extra path for you:

/opt/local/bin

where port executables are installed. If you start a new shell and still don't see the port program in your path, add this yourself. Make sure it is at the front of your path.

First you should type

sudo port selfupdate

this will update the package list. One installs programs, or ports, using

port install package_name

You can get a list of available packages using

port list [pattern]

where [pattern] is an optional package name to search for.

TeX

This will allow you to create all text in your figures using TeX. You can install tex through macports. Also you can write papers of course.

sudo port install tetex

You can also get these binary distros

http://ii2.sourceforge.net/tex-index.html

Main Installation

Because scipy depends on numpy and numpy depends on python, just type

 sudo port install py25-scipy

This will fail when it comes to install the py25-hashlib dependency. Just rerun the above command and it will finish this time. Apparently, somewhere python dependencies can lost the value of $(CC) along the way but restarting it fixes this.

MacPorts will find and install a number of dependencies. The biggest is gcc. NumPy depends on gfortran, and as of 2007-01-15, even if you have a version installed from some other source, MacPorts won't accept it. This is the big drawback of this approach, it takes a long time, but in the end it will all work.

A list of dependencies is below from my install on 2007-03-15. Note, more or fewer packages may be installed depending of the state of your MacPorts.

   metis
   SuiteSparse
   fftw-3    - Fastest Fourier Transform in the West
   gmp       - GNU Multiple Precision Arithmetic Library.  Arbitrary precision 
               arithmetic libraries.
   gperf 
   libiconv  - Unicode text conversion library.
   mpfr      - Another Multiple Precision Arithmetic library.
   odcctools - Geared towards improving the Darwin cctools build system. Not 
               sure why we needed this one.
   gcc43     - For gfortran.
                  -- gcc-core, gcc-fortran, gcc-g++, gcc-java, gcc-objc
   python25  - version 2.5 of Python
   py25-hashlib   - python extension for digests, e.g. md5, sha256, etc
   aquaterm  - why?
   expat
   zlib       - File compression tools
   freetype   - Font libraries.
   fontconfig - a library for font customization and configuration
   jpeg       - jpeg libraries
   libpng     - png libraries
   gd2        - Graphics library.
   pdflib     - PDF tools
   ncursesw   - ncurses with wide-character/UTF-8 
   ncurses    -  API for terminal-independent text user interfaces
   readline   - command line editing library
   gnuplot    - the Gnu plotting tool
   openssl
   gettext

You may have to install links to gcc,g++,gfortran in /opt/local/bin yourself if you want to use these rather than the apple gcc, etc.

iPython

Optional, but this is a much better interactive shell than the default and it understands numpy and matplotlib. I had some issues actually with the macports install. You should just download it yourself, it's very easy to install, and the version in macports is alwasy broken.

 http://ipython.scipy.org/moin/

untar the file and

 sudo python setup.py install

making sure you are using the python installed by macports.

Getting it all working

NUMERIX

You should set an environment variable

   setenv NUMERIX numpy

This is used by some packages that are currently moving from the older numerical packages (numarray,numeric) to distinguish your preference. In the future the default will be numpy but do this for now just in case.

Plotting with MatPlotlib

This is a matlab-style plotting package. http://matplotlib.sourceforge.net/

For this install on intel the plotting backend is gtk.

To get plotting to work you should create a file in a new directory

   ~/.matplotlib/matplotlibrc

And add the following at minimum:

 numerix      : numpy  # numpy, Numeric or numarray.  Absolutely required until
                       # numpy is the default.
 backend      : TKAgg  # Use TK for the plotting backend.
 text.usetex  : True   # use latex for all text handling.  Note this does slow things down.
                       # For faster use set to False and then turn on when you need it
                       # with pylab.rc('text',usetex=True) in your script.

You can see a full set of options by looking at this file:

 /opt/local/lib/python2.4/site-packages/matplotlib/mpl-data/matplotlibrc
 

The TeX is especially nice. See here for example usage of tex:

   http://www.scipy.org/Cookbook/Matplotlib/UsingTex

If you are coming from IDL where it is a nightmare trying to get plotting in X and PS to look the same, you will appreciate the "what you see is what you get" aspect of the plotting. Exactly what you see in a plot window is what comes out in a postscript file. Also, TeX notation is supported (even without TeX handling the text) if a "raw string" is sent. r'$M [$M$_{\odot}]$' would be a proper axis label. This looks even better if you use "text.usetex" in your rc file. Far better than IDL, where ! escapes are everywhere.

Astro packages

pyfits

Pure python FITS reader that uses NumPy.

 http://www.stsci.edu/resources/software_hardware/pyfits
 To install: sudo python setup.py install
 Currently wants NUMERIX to be set as suggested above.


Documentation and Examples

A very useful resource:

 http://www.scipy.org/Documentation

Note, there is a book being written which you can buy, but the free help at that link covers much of what you need.


Installing NumPy and SciPy on Mac OS X 10.5 Leopard

Here's some notes on how to get NumPy and SciPy installed on Leopard with Python 2.5.1:

  • Install Python 2.5.1, either from source or from MacPorts. If from macports, do:
$ sudo port install python25
  • Install the packages noted above using MacPorts, omitting numpy, odcctools, and python24. For NumPy (note that the gcc42 will take several hours):
$ sudo port install fftw-3 gmp libiconv mpfr gcc42
  • For SciPy:
$ sudo port install zlib freetype fontconfig jpeg libpng gd2 pdflib ncursesw ncurses readline gnuplot  
  • Make a link to gfortran in /opt/local/bin:
$ sudo ln -s /opt/local/bin/gfortran-mp-4.2 /opt/local/bin/gfortran  
  • Install the Python Easy Install system following the directions here. Here's one way to do it:
$ sudo port install wget
$ wget http://peak.telecommunity.com/dist/ez_setup.py
$ python ez_setup.py
  • Use easy_install to install numpy and scipy:
$ easy_install numpy
$ easy_install scipy
  • You are done!