Tuesday, August 19, 2008

Postprocessing with Python (Boris)

Overview:
-----------------

- What is Python and why use it?
- Installation procedure

What is Python?

Python was created in 1991 by Guido van Rossum (Amsterdam). Named after Monty Python's flying circus.

Goals:
----------

- an easy and intuitive language
- opens source
- plain English
- suitable for every tasks, short development times

Python v. 3.0 in 2009.

Why Python?
---------------------

- it's free
- quite easy to use; object-oriented; highly modular, etc.
- faster than IDL, and even parallel (MPI)

Python makes extensive use of modules, and they all have to be loaded.

The main Python website is www.python.org. Another important website is www.scipy.org. scipy is a collection of scientific modules for Python.

How to install Python?
--------------------------------------

Required:
- Python 2.5: the engine
- numpy: the scientific computing package
- scipy: modules for integrating ODEs, optimizing functions, etc.
- matplotlib: MATLAB-inspired mostly-2D plotting modules

numpy may be moving into scipy.

Optional:
- ipython: interactive shell
- basemap: map projection
- Pypar: parallel Python (interface with MPI libraries)
- PyQt: to do Qt-like widgets very easily
- MayaVi: 3D plotting

Download the code at http://www.python.org/download.

Should you download sources or binary packages? For Linux, Windows & Mac binaries are provided (e.g. using apt-get for Linux or Fink for Mac). For Mac there is the website www.pythonmac.org/packages/py25-fat. Can also install Scipy Superpack for OS X.

Jeff Oishi was the first to code Python scripts for postprocessing with the Pencil Code.

Need to set $PYTHONPATH.

if ($?PYTHONPATH) then
setenv PYTHONPATH "${PYTHONPATH}:${PENCIL_HOME}/numpy"
else
setenv PYTHONPATH "${PENCIL_HOME}/numpy"
endif

Postprocessing with Python:
------------------------------------------------

Start with 'import pencil as pc'

Can either do pc.read_ts() or a=pc.read_ts(). Then 'import matplotlib as P'. Then 'plot(a.t,a.urms)'

Most of the functionality of the idl pc_read modules (including magic) is implemented in the python counterparts.

Be careful: Python's arrays are ordered like f[nvar,mz,my,mz].
REVERSED COMPARED TO FORTRAN OR IDL

For 2D plots, use imshow.
For 3D plots one can use MayaVi. http://code.enthought.com/projects/mayavi

Migrating from IDL to Python:
---------------------------------------------------

There are good webpages with overview of IDL commands and the corresponding Python commands. Space Telescope has a nice page on that.

Other useful page

https://mathesaurus.sourceforge.net/idl-numpy.html

For lazy people there is the i2py converter (http://code.google.com/p/i2py).

Some tricks when using Python:
------------------------------------------------------

Load modules by default in ~/.ipython/ipythonrc.

Important only what you need, otherwise it takes too long to load Python.

Launch ipython with the '-pylab' option to call directly plotting commands.

Accelerate reading of snapshots by passing arguments.

Accelerate graphics by using a handle.

Take advantage of class and objects.

Parallel Python:
---------------------------

Can run Python with mpirun and do postprocessing faster.

Widgets using Q Designer + PyQt:
----------------------------------------------------------

Write script in Qt and convert to Python. Simpler to do widgets than in IDL.

Conclusion:
--------------------

- Python can do a very good job in the Pencil Code postprocessing
- Its use is rapidly increasing in astrophysics
- More in the Pencil Code philosophy (i.e. under GPL)

Weaknesses:
- the actual Python subroutines must be rewritten in a more object oriented form.
- need to reorganize the Python tree, in something like f90/pencil-code/python.
- calling Fortran or C subroutines to increase the speed?

No comments: