The aim of this workshop is to get you set up with a working scientific Python installation which meets the Python requirements. This will proceed in four steps:
The installation process is particular to each operating system and platform. You may need to choose between 32-bit and 64-bit installations. Generally speaking you should choose 64-bit, but read 64 versus 32 bit for some caveats or if you aren’t sure if your CPU is 64-bit.
Unless you enjoy tracking down compiler errors and other issues related to package incompatibilities, we recommend using a pre-built binary Python distribution. Even if you already have an installation on your system you will probably save time in the long run by starting fresh with a binary Python distribution.
Anaconda: an easy and fast option
On of the fastest way to get a basic Python installation up and running is Anaconda. Click on that link and download the installer (using the button marked free on the top right of the page). It will ask you for your email and then you get an installer for your OS (Mac, Linux, or Windows). Read 64 versus 32 bit if you aren’t sure if your CPU is 64-bit.
By default Anaconda is installed into your home directory (no root access required), but you can pick another location if you wish. To use the Anaconda python installation, simply add that directory to your path, following the instructions on the Anaconda page.
After installing Anaconda, you will probably have more than one Python installation on your computer, e.g. a Python included in the OS, Anaconda plus possible CASA or CIAO/Sherpa. In general that is not a problem, but see Multiple Pythons on your computer for more information on managing this situation.
There are a number of Recommended installation options besides Anaconda, and you are encouraged to explore these options and decide what might be right for you. This includes the use of system package managers like MacPorts or linux RPM. Each of the other options has different features and strengths, and no single solution works for everybody.
After you set up your core Python installation, you should install a few more packages that are used in the tutorials. Copy and paste the lines below one at a time, checking that each one works. The program outputs may contain various “warnings”, but watch for “errors” and look at the end to see if a successful installation was reported.
pip install --upgrade astropy # NOT required for Anaconda pip install --upgrade aplpy pip install --upgrade pyregion pip install --upgrade pyparsing pip install --upgrade atpy
Note that if you have used a root-installation option like MacPorts or a linux package manager to install Python, then you will need to use the sudo prefix in each of these commands, e.g.:
sudo pip install --upgrade aplpy
If you experience problems with installation for any of these packages you can send an email to the astropy mailing list.
To do a very basic test whether you meet the requirements and have a functioning core scientific Python installation, do the following and check version numbers:
% python -V % ipython -V % ipython --pylab import numpy import scipy import scipy.linalg print numpy.__version__ print scipy.__version__ print matplotlib.__version__ x = numpy.linspace(0, 20, 100) plot(x, sin(x)) print scipy.linalg.eig([[1,2],[3,4]])
The commands above should succeed with no errors. The version numbers should meet the requirements, and finally you should see a plot of a sine wave.
To check the other required packages, do the following from within ipython:
import astropy import aplpy import pyregion import pyparsing
If you are following along with the Python for Astronomers tutorial and have finished installing Python, you can give a real test drive now.
First download the install_examples.tar tar file which has example data files that will be used in subsequent exercises. Then change to a working directory, untar the file, and start up IPython:
tar xvf ~/Downloads/install_examples.tar # or wherever your browser puts downloads cd py4ast/install ls ipython --pylab
For all of the workshops you should always start Python using the command:
ipython --pylab # (for Windows start the Pylab application)
This will automatically load all of the main plotting functions from Matplotlib (e.g. plot(), hist(), and many more) as well as common math functions and array utilities from NumPy (e.g. sin(), exp(), array(), etc).
In my ~/.cshrc file I define an alias that I commonly use:
alias pylab "ipython --pylab"
Exercise: Read a table and examine it
Look at the documentation for the astropy.Table.read() function in astropy. Follow the very first example and use the read() function to read the data in the file table1.dat into the variable named data.
This table is in the “ApJ machine-readable format” (which is actually a bit tricky for machines to read). This is very similar to the table format used by CDS / Vizier for input and storage of tables in astronomy.
Once you have read the table into the variable data then print the table, print the column RAdeg, and print the 3rd row.
You can print a <variable> by just typing print <variable> at the command line, for example print data.
You can get tons of useful information about a variable with help or ?:
- help <variable>
- ? <variable>
These two commands are pretty similar except that ? gives a little bit more information and in a more raw form.
The object returned by asciitable.read() is a NumPy record array, which is just a fancy way of saying a table where you can access rows or columns of data.
You can get the column names and types with print data.dtype
To print a column of data use print data["<column_name>"], for example print data["Name"]
To print a row of data use print data[<row_number>]
For several of the binary installers you need to make a decision: 32-bit or 64-bit download? First you need to establish whether your computer has a 32-bit or 64-bit processor. If you have a 32-bit processor then your decision is easy (32-bit) but if you have a 64-bit processor then either 32 or 64 will work. See below if you don’t know your processor architecture: