Numpy For Mac Using Macports

From NYU CCPP Wiki

Contents

Python for scientists, especially astronomers

This is a tutorial for how to get Python with Numpy (numerical python) and Scipy (scientific python) set up on your PPC Mac 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, check out this page. Although not as quick an install as the binary distros in MacPython, everything works using macports. I mean it, I got everything to work on my powerbook ppc. I could not get plotting to work on my intel macbook pro.

This page mainly written by (Esheldon 14:37, 15 January 2007 (EST)). Other installation methods, such as for Intel 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 py-numpy
 sudo port install py-scipy
 sudo port install wxPython
 sudo port install py-matplotlib

See below for more details.


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.

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 py-numpy
 sudo port install py-scipy

Note, the py-scipy package dependencies are not set correctly, so even though py-scipy depends on numpy, you cannot just type "sudo port install py-scipy", that will not work. It will fail because there is a mixup in the gfortran dependencies.

MacPorts will find and install a number of dependencies. The biggest is gcc 4.1 NumPy depends on gfortran, and as of 2007-01-15, even if you have a version installed from some other source, MacPorts just won't accept it. This is the big drawback of this approach. If you just first try installing py-scipy it will not realize this and the install will fail.

A list of dependencies is below. Note, more fewer packages may be installed depending of the state of your MacPorts.

 NumPy
   fftw-3    - This takes forever on a powerbook
   gmp       - GNU Multiple Precision Arithmetic Library.  Arbitrary precision 
               arithmetic libraries. 
   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.
   gcc41     - For gfortran.  Would not use the gfortran compiler on the 
               system. Took 45 minutes to compile on core 2 duo MacBook pro,
               > 6 hours on a powerbook ppc
                  -- gcc-core, gcc-fortran, gcc-g++, gcc-java, gcc-objc
   python24  - version 2.4 of Python
 SciPy
   zlib      - File compression tools
   freetype  - Font libraries.
   jpeg      - jpeg libraries
   libpng    - png libraries
   gd2       - Graphics library.
   pdflib    - PDF tools
   gnuplot   - the Gnu plotting tool

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.

A recent failure mode

Note: Starting on 2006-01-13 the install of gnuplot failed because the patches could not be retrieved. You will see

Attempting to fetch datastrings_4.0.patch from....

and all attempts will fail. This seems to be because the domain of the site

http://www.bmsc.washington.edu/people/merritt/gnuplot/

is not being properly mapped to its ip of 128.95.235.53. You can go to that ip and download the following files yourself

http://128.95.235.53/people/merritt/gnuplot/datastrings_4.0.patch
http://128.95.235.53/people/merritt/gnuplot/histograms_4.0.patch

And copy them into

/opt/local/var/db/dports/distfiles/gnuplot

and gnuplot will install.

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.

 http://ipython.scipy.org/moin/
 sudo python setup.py install

And add a link in /opt/local/bin to the executable.

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 ppc, you can use wxpython, but for intel wxpython only works for python2.5. And numpy,etc only work for 2.4... Also, could not get any of the other backends to work, not even TkAgg which should work trivially on any system.

So, for ppc...

 sudo port install py-wxpython
 sudo port install py-matplotlib
   deps: antigraingeometry, py-dateutil, py-numarray, py-numeric, py-tz

wxPython is a python window gui environment which can be used by matplotlib. I had difficulty on my ppc getting the others to work properly. We'll see about intel.

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      : WxAgg  # Use wxPython 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 and the equivalent of \odot just doesn't exist except as a kludge.


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.