An introduction to scientific computing with Python (MPAGS 2011)
A Midland Physics Alliance Graduate School (MPAGS) course — Autumn, 2011
This course will give a general introduction to Python programming, useful for all physics postgrads.
The sessions are scheduled for 1-3pm on Tuesdays, commencing 8th November for 5 weeks, in room A6 in the main Nottingham Physics building, and via Access Node broadcasts to Warwick and Birmingham.
The lecturer will also be available to answer further questions relating to the course 2:00 – 3:30pm on the Thursday after each session.
The course will be formatively assessed by coursework. This will involve developing a working Python program related to your studies, to be submitted two weeks after the final lecture, i.e. 20 December.
PDF versions of the slides that accompany the course will appear below as the course progresses:
- mpags_python_session5.pdf (also see these slides and these videos)
Solutions to the exercises (remove .txt extensions to import):
- Session 2 is mostly covered by session 3:
- Session 4 and 5 had no exercises
- Session1: Introduction to Python
- Session 2: Numerical Python and plotting
- Session 3: Scientific Python
- Session 4: Python for specialists (observers and theorists)
- Session 5: Extreme Python
The course will be run in a ‘workshop’ style. Students are strongly encouraged to bring laptops and try out the examples given during the lecture. Furthermore, there will be time during the scheduled slots for students to work together on exercises, with the lecturer present to give advice.
For the benefit of students taking the MPAGS course, I will provide ssh and NX access to a linux server with everything installed. However, this server will only be available for the duration of the course, and I expect many of you will want to continue using Python in your work, so…
Before the course begins, it would be useful for you to check that you have a working Python interpreter available, e.g. installed on your laptop, or by logging (e.g. via ssh) into an available server (e.g., your University’s UNIX service or one of your research group’s servers). Python currently exists in two stable major versions: 2 and 3 (at time of writing the most recent releases are 2.7.2 and 3.2.2). Python 2 is stable, still being maintained, and remains in more general use. Python 3 is the future, and is generally ready for use, but is not backward compatible with Python 2 due to a small number of significant changes, i.e. code that works with Python 2 will not necessarily work with Python 3. The best version to use for this course is 2.7, as this is compatible both ways. However, I will explain the differences so you should manage fine with different versions. However, note that a number of useful features were introduced during the development of version 2, so if you should at least try to find a version >= 2.4.
The first session of the course only deals with ‘pure’ Python, but thereafter it relies on a number of third-party modules, which may need to be installed separately (although this is usually quite straightforward). Session 2 introduces numpy and matplotlib, while session 3 covers scipy. In session 4 we use some more specialist tools, which you may only want to install if they are directly useful for your work. Astronomical observers may like to install pyfits and pyraf (or ensure they have access to a server with these installed); note that pyraf requires IRAF to be installed. Those interested in symbolic mathematics may like to obtain sympy and Sage (a separate software package based on Python), although this can be also be tried out using a publicly available online tool. Finally, session 5 introduces a number of other modules, including pytables, numexpr and cython, which those with computationally demanding projects may like to experiment with. The Django web framework will be briefly highlighted, but only those wanting to develop their own web applications need install it.
See the below for installation advice.
Your Linux distribution package manager
Python will certainly be available to install from your operating system’s package manager (e.g. Yast in Suse, MacPorts in OS X, etc.), which will hopefully provide a reasonably recent version. You will probably find Numpy, Scipy, Matplotlib and many other modules are also available via your package manager (especially if it is a reasonably recent distribution version). However, you will probably need to manually install stsci_python (which provides PyFITS and PyRAF, so is only necessary if these tools are relevant to your work).
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!
- Ipython: http://ipython.scipy.org/
- EPD: http://www.enthought.com/products/epd.php (free for academic use)
- python(x,y): http://www.pythonxy.com
- any more you might find…
These probably don’t include stsci_python, so this may still need to be installed separately.
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. The recommended version for this course is 2.7, but other versions should work ok.
It is recommended to use your native Python installation, or package manager. However, if those fail you or you want the absolute latest version, Python can be obtained from here: http://www.python.org/download/. 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. Note that 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, PyTables, SymPy, etc.
The easiest way to get additional modules is to use your operating system’s package manager. If a module is not available in the package manager, you should try using pip:
sudo pip install numpy scipy matplotlibor, failing that, easy_install:
sudo easy_install numpy scipy matplotlib. These may ‘just work’, but you might need to install pip first (either using your package manager, or see here).
Finally, you could get the latest versions directly 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).
This provides PyFITS, PyRAF and Numdisplay for astronomers. PyFITS may also be useful for other people who use FITS files.
You could try installing these modules using pip (see above), or get it directly from: http://www.stsci.edu/resources/software_hardware/stsci_python
- Library reference (useful): http://www.python.org/doc/lib/
- Library reference index (very useful): http://www.python.org/doc/lib/genindex.html