Numpy For Mac Using MacPython

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 Mac. The distribution, called MacPython, is a set of binaries precompiled for the Mac. These packages support an n-dimensional array object and many routines for data analysis and visualization. Plotting is also supported by the matplotlib package. The packages below are the minimal you need to install.

Everything mostly works which is why some prefer the MacPython distro to others. I actually had the most success with MacPorts

 Note: The NumPy package in MacPython is currently a beta release 1.0b5.  The official
 1.0 release was 2006-10-25 and should be available in MacPython soon.

This page mainly written by Erin Sheldon 18:04, 2 November 2006 (EST)

Preliminaries

This is stuff you probably already have, but I'm listing them just for completeness.

Xcode

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

TeX

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

Python Installation

MacPython Packages

This is a set of universal binaries compiled against python 2.4.3.

 Package    Version   Description
 --------------------------------
 python     2.4.3     Probably newer than what you have by default, 2.3 in Tiger.
 numpy      1.0b5     NumPy See http://scipy.org/ and http://numpy.scipy.org/
                      Currently beta, but quite stable. Version 1.0 arrives 2006-10-25.
 matplotlib 0.87.5    Plotting - http://matplotlib.sourceforge.net/
 scipy      0.5.1     http://scipy.org/
 Optional but perhaps useful.
 wxPython   2.6       For a different GUI than TK.  I have yet to see advantages 
                      to this over TK.

iPython

Optional, but this is a much better interactive shell than the default and it understands numpy and matplotlib.

 http://ipython.scipy.org/moin/
 Follow the instructions on the download page.  Very simple to install.


Getting it all working

PATH

You will need to have this in your path for executables:

   /Library/Frameworks/Python.framework/Versions/Current/bin/python

Note one of the install scripts modifies your shell startup file to do this but messes up the syntax and cause an error in tcsh (this is the one bug I have found so far) so you need to do this yourself.

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

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  # If you installed wxPython you can leave this off or set to WxAgg
 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 get see full set of options by looking at this file:

 /Library/Frameworks/Python.framework/Versions/2.4/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.