Numpy For Intel Mac Using Fink

From NYU CCPP Wiki

Contents

Python for scientists

This is a tutorial for how to get Python with Numpy (numerical python) and Scipy (scientific python) set up on your Mac (OS X) using Fink [1].

Numpy/Scipy supports an n-dimensional array object and many routines for data analysis and visualization. Plotting is also supported by the matplotlib package.

It turns out this was the simplest way to get NumPy/SciPy working, and everything worked the best for me with this approach. The problem is everything must be compiled, unlike the binary distros in MacPython. This was a reasonable tradeoff for me.

This page mainly written by Erin Sheldon 01:41, 17 March 2007 (EDT). Other installation methods can be found at this page Numpy_For_Mac.

Quick Version

Here is the quick version if you already have XCode installed and fink installed and properly set up:

 fink install scipy-py25
 fink install matplotlib-py25

scipy installs numpy and scipy. See below for some details. Note: I am serious that fink must be set up properly so read on.

Preliminaries

Xcode

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

Fink

Fink is an open source distribution:

It will set an extra path for you:

 /sw/bin

where fink 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. If transitioning from an older version of fink read Fink Transitional. Make sure you also install Fink Commander, the gui interface.

Once installed, bring up fink commander and go to the preferences. Click the fink tab and check both unstable buttons. Then quit fink commander and go to a prompt.

The following is important for getting a fully functional scipy:

  fink selfupdate
  fink update-all (follow the instructions, which may involve running scanpackages, apt-get, selfupdate again, etc.

Now you should be able to

 fink list scipy

and see scipy-py25 in there. You should also be able to do this

 fink show-deps scipy-py25

and see the following under "To compile this package from source..."

   fftw3
   fftw3-shlibs
   g95 (>= 0.90-1)
   netcdf
   netcdf-shlibs
   python25 | python25-nox
   scipy-core-py25 (>= 1.0-1)

It is important that g95 is in that list, otherwise the install will not be complete. Note scipy-core is the old name for numpy.

Installing NumPy and SciPy

There are packages for different versions of python. I installed for python 2.5.

These packages must be compiled from source. After setting unstable, you can install from the command line with

 fink install scipy-py25

or from fink commander by clicking on the upper left button .h (binary not available). This will install all the packages needed by and including NumPy and SciPy. There are many dependencies, so this will take more than an hour on an intel core 2 duo. Note fink will ask you some questions that you will most likely take the default answer, so you can use fink -y install to always answer default.

If you want to use this version of python 2.5 for your regular python do this

 sudo ln -s /sw/bin/python2.5 /sw/bin/python

Now make sure all the tests are passed

 /sw/bin/python2.5
 >>> import numpy
 >>> numpy.test()
 >>> import scipy
 >>> scipy.test()

These should show no failures.

Installing Matplotlib

This is a matlab-style plotting package. http://matplotlib.sourceforge.net/ Note the version may change in the future.

 fink install matplotlib-py25

This is the really slow one. It needs to install many things, such as gtk+ that take a long time.

Installing iPython

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

 fink install ipython-py25

Once you get matplotlib installed you can start it with

 ipython -pylab

and it will have interactive plotting. I actually recommend using the simple Tk backend (see below) and using the proper flag

 ipython -pylab -tk

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.

Setting up Plotting with MatPlotlib

Plotting will work out of the box. I recommend creating this file

   ~/.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.  Only one that gives no warnings.
 text.usetex  : True   # use latex for all text handling.  Looks good and savefig() can create proper
                       # postscript only if this is set.  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:

 /sw/lib/python2.5/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.

Astronomy packages

pyfits

Pure python FITS reader that uses NumPy.

 http://www.stsci.edu/resources/software_hardware/pyfits
 fink install pyfits-py25

PIL: Python Image Library

This is great for manipulating jpegs and things like that.

 fink install pil-py25

Documentation and Examples

A 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.