An introduction to scientific computing with Python

Postgraduate Course — February, 2010

Before the course begins, it would be useful for you to check that you have a working python interpreter available, e.g. on your laptop or my logging into the University’s unix system (granby). The first part of the course only deals with ‘pure’ python, but the second part relies heavily on the Numpy, Scipy and Matplotlib modules. See below for installation advice.

Outline

Part 1: Introduction

  • Why (and why not) to use a modern, high-level, scripting language
  • Why Python is awesome
  • Introduction to the language
    • startup, syntax, constructs, functions, classes, getting help
  • Good programming practice versus ‘thinking aloud’
  • Python 2.x versus 3.x

Part 2: Scientific programming in Python

  • Arrays – Numerical Python
  • Using arrays wisely
  • Plotting
  • Scientific Python
    • optimisation, interpolation, statistics, filtering, integration, …
  • Other tools
    • GNU Scientific Library, R, …

Part 3: Python for astronomers

  • Handling FITS files
  • PyRAF – scripting IRAF with Python

Lectures

  • Part 1: Wednesday 10 February 2-3:30pm, room A1
  • Part 2: Wednesday 17 February 2-3pm, room A1
  • Part 3: to be arranged…

Here is a PDF of the slides that accompany the course:

Installing software

Your Linux distribution package manager

Python will certainly be available to install from your Linux pakage manager (e.g. Yast in Suse), hopefully a reasonably recent version. You may find Numpy, Scipy and Matplotlib in there too. However, you will probably need to install stsci_python manually (but only if you need to interface with IRAF, use FITS files or display in DS9 – if you don’t know what these are then you don’t need stsci_python).

All-in-one stuff

I’m a bit old school, and use Emacs + Bash + Python + Scipy + Numpy + Matplotlib + etc. all installed separately. However, there are some all-in-one packages that look good, and which provide an enhanced python interpreter, which you might like. Note that I haven’t used any of these, and I’m quite happy with my normal setup, which is usually quite easy to achieve (see below). Feel free to experiment with these packages if you like. If you have success, I’d like to hear about it!

These don’t currently don’t seem to include stsci_python, so this will need to be installed separately. However, if someone wants to suggest it, they may consider including it. Another issue is that they don’t appear to have general Linux support, but rather they are specific to one or two distributions.

Python

If you are using a Linux or Mac OSX system, you probably already have Python installed. However, sometimes it can be quite an old version that the operating system uses. Typing ‘python’ at the command prompt will start the interpreter and tell you the version. At time of writing the recommended version for scientific computing is 2.6.4. Numpy and Scipy have recently been updated to work happily with the 2.6.x branch, but if you run into trouble with other, older, modules you could try 2.5.4. Python 3.x has undergone some major changes and will not be compatible with many scientific tools for some time.

Get the recommended version of Python here: http://www.python.org/download/releases/2.6.4/

Installation should be quite straightforward. When it is installed, typing ‘python’ at the command prompt should indicate the correct version. Different versions of python can happily coexist, and it is usually a bad idea to remove the default one that came with your system. You may need to either make sure the new python executable is in your path ahead of the old one, or create an alias.

Line editing, history and tab completion

Life with Python is much better if you can use the arrow keys to edit the current line, browse the command history, and hit tab to complete or get suggestions for commands. If you can’t do all that in your python, then you need to enable it. See here.

Numpy, Scipy, Matplotlib

Get the latest versions (currently Numpy 1.3.0 and Scipy 0.7.1) from here: http://scipy.org/Download and here: http://matplotlib.sourceforge.net/.

Again, installation should be straightforward, either by installation package on OSX or Windows, or by one or two commands on Linux (see the README). You could also you try typing

sudo easy_install numpy scipy matplotlib
in the terminal, which may well ‘just work’.

stsci_python

This provides PyFITS, PyRAF and Numdisplay for astronomers. PyFITS may also be useful for other people who use FITS files.

Get it from: http://www.stsci.edu/resources/software_hardware/pyraf/stsci_python

Finding help

Python documentation

Numpy and Scipy documentation

Matplotlib

stsci_python

4 Responses leave one →
  1. February 10, 2010

    Neat! Will you be ustreaming these? Sadly, I’m all the way over in St. Louis.

  2. Steven permalink*
    February 11, 2010

    Hi Ryan. I didn’t really think about streaming them. I could think about it for the next one, if people would find it useful. However, there are already plenty of useful python tutorials on the web that would probably be just as useful. You can also get the slides from the lectures above, although they aren’t so helpful without the accompanying vocal explanation.

  3. July 22, 2010

    Hi Steven, another Python programmer, great!!! I’m into bio-informatics now and our software package is scriptable in Python. I create plugin applications and write Python code the whole day. We should get in touch about it! Cheers, Dolf

  4. August 27, 2010

    Hi Steven,

    it’s been a looong time… anyway, I’m quite new to Python myself and really like it. Use it to run Fortran code and use the results for plots and stuff and I’m planning to do more…

    Anyway, I just wanted to mention that Enthought’s distribution is free for academics! Scroll down to the end at http://www.enthought.com/products/epd.php and click on “academic”. Well, this sounds like these guys are paying me to write this here… nope!

    So, happy coding and everything else. Cheers from Geneva, Lukas

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS