postprocessing with python -...

58
Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse) [email protected] Collaborator: Thomas Gastine (PhD)

Upload: vutuong

Post on 06-Nov-2018

253 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Postprocessing with Python

Boris Dintrans (CNRS & University of Toulouse)[email protected]

Collaborator: Thomas Gastine (PhD)

Page 2: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Outline

Page 3: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

• Introduction- what’s Python and why using it?- Installation procedure

Outline

Page 4: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

• Introduction- what’s Python and why using it?- Installation procedure

Outline

• Python and the Pencil Code- the Python repository and initialization- Migrating from IDL to Python- Some examples & tricks- Parallel Python with Pypar- Doing widgets with PyQt

Page 5: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

• Introduction- what’s Python and why using it?- Installation procedure

Outline

• Python and the Pencil Code- the Python repository and initialization- Migrating from IDL to Python- Some examples & tricks- Parallel Python with Pypar- Doing widgets with PyQt

• Conclusion/Outlook

Page 6: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

What’s Python?

Page 7: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

What’s Python?

Python was created in 1991 by Guido van

Rossum (CWI, Centrum voor Wiskunde en Informatica, Amsterdam)

Benevolent Dictatorfor Life (BDFL)

Page 8: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

What’s Python?

Python was created in 1991 by Guido van

Rossum (CWI, Centrum voor Wiskunde en Informatica, Amsterdam)

Benevolent Dictatorfor Life (BDFL)

Page 9: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

What’s Python?

Python was created in 1991 by Guido van

Rossum (CWI, Centrum voor Wiskunde en Informatica, Amsterdam)

Benevolent Dictatorfor Life (BDFL)

Page 10: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

The first release in 1991 on alt.sources

Page 11: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

DARPA funding proposal “Computer Programming for Everybody” (1999):

Page 12: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

•an easy and intuitive language just as powerful as major competitors

•open source, so anyone can contribute to its development•code that is as understandable as plain English•suitability for everyday tasks, allowing for short development times

DARPA funding proposal “Computer Programming for Everybody” (1999):

Page 13: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

•an easy and intuitive language just as powerful as major competitors

•open source, so anyone can contribute to its development•code that is as understandable as plain English•suitability for everyday tasks, allowing for short development times

DARPA funding proposal “Computer Programming for Everybody” (1999):

year 1991 1994 1995 2001 2003 2007 2009

version 0.9 1.0 1.2 2.0 2.2 2.5 3.0

Page 14: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

•an easy and intuitive language just as powerful as major competitors

•open source, so anyone can contribute to its development•code that is as understandable as plain English•suitability for everyday tasks, allowing for short development times

DARPA funding proposal “Computer Programming for Everybody” (1999):

year 1991 1994 1995 2001 2003 2007 2009

version 0.9 1.0 1.2 2.0 2.2 2.5 3.0

• Why Python?- it’s free! ;-)- quite easy to use; object-oriented; highly modular, etc...- much more rapid than IDL and even PARALLEL

Page 17: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

How to install Python?

Page 18: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

How to install Python?Required: • python 2.5: the engine• numpy: the scientific computing package (arrays, linear algebra, FFT, random numbers, etc...); [replaces old numarray and numeric]• scipy: modules for integrating ODEs, optimizing functions, etc... [tends to federate all of Python scientific modules]• matplotlib: MATLAB-inspired mostly-2D plotting modules

Page 19: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

How to install Python?Required: • python 2.5: the engine• numpy: the scientific computing package (arrays, linear algebra, FFT, random numbers, etc...); [replaces old numarray and numeric]• scipy: modules for integrating ODEs, optimizing functions, etc... [tends to federate all of Python scientific modules]• matplotlib: MATLAB-inspired mostly-2D plotting modules

Optional:• ipython: convenient shell to develop and run Python• basemap: map projections• Pypar: parallel Python (interface with MPI libraries)• PyQt: to do Qt-like widgets VERY easily under Python• MayaVi: 3D plotting

Page 21: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

From sources or binary packages?

Page 22: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

• For all platforms: everything can be compiled from sources• For Linux, Windows & Mac (at least): binaries are provided (Linux: yum, apt-get, dpkg; Mac: Fink, MacPorts, dmg)

From sources or binary packages?

Page 23: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

• For all platforms: everything can be compiled from sources• For Linux, Windows & Mac (at least): binaries are provided (Linux: yum, apt-get, dpkg; Mac: Fink, MacPorts, dmg)

From sources or binary packages?

Linux (FedoraCore 7) Mac OS X 10.4 (Tiger)Python 2.5.12 2.5.2Numpy 1.0.3 1.0.4Scipy 0.6.0 0.7.0

Matplotlib 0.90.0 0.90.1ipython 0.8.1 0.8.2

Page 24: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Typical installation on a Linux box

Page 27: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Python packages on my Macbook Pro

(SciPy Superpack)

Page 28: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Python in the Pencil Code repository:f90/pencil-code/numpy commited by Jeff in fall of 2007

revision 1.1date: 2007-11-16 13:57:04 +0000; author: joishi; state: Exp;* added python scripts for reading pencil code data. they require only the numpy package, but matplotlib is useful for plotting. almost all of these routines are simplified clones of their idl counterparts. i'd love to make a more OO pencil-code package, but my current occupational constraints make that unlikely in the near term. NB: the byte ordering in python is C, not fortran, so these routines return an f array with shape f[nvar,nz,ny,nx]--the reverse of pencil.

* added nl2python to take advantage of the amazing perl F90Namelist.pm

* modified F90Namelist.pm to output python

* i hope these are moderately useful to people!

Page 29: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

The actual Python tree: 3 directories

numpy/pencil/files__init__.pyyzaver.pyyaver.pyxyaver.pynpfile.pydim.py

param.pygrid.pyslices.pyzprof.pyindex.pyvar.pyts.py

The reading stuff

Page 30: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

The actual Python tree: 3 directories

numpy/pencil/files__init__.pyyzaver.pyyaver.pyxyaver.pynpfile.pydim.py

param.pygrid.pyslices.pyzprof.pyindex.pyvar.pyts.py

The reading stuffnumpy/pencil/math

__init__.pyvector_multiplication.py

derivatives/

numpy/pencil/math/derivatives__init__.py

der.pydiv_grad_curl.py

der_6th_order_w_ghosts.py

The math stuff

Page 31: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

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

...and the initialization of $PYTHONPATH in f90/pencil-code/sourceme.csh

Page 32: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

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

...and the initialization of $PYTHONPATH in f90/pencil-code/sourceme.csh

These modules are loaded when importing the whole pencil directory due to the __init__.py file

Page 33: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

cat numpy/pencil/__init__.py

Page 34: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

cat numpy/pencil/__init__.py

In [1]: import pencil as pcIn [2]: pc.read_ts()

Page 35: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

An important point: Python’s classes• Python is an object-oriented interpreted language:

instead of doing pc.read_ts(),it is better to do a=pc.read_ts()

Page 36: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

An important point: Python’s classes• Python is an object-oriented interpreted language:

instead of doing pc.read_ts(),it is better to do a=pc.read_ts()

... and we can plot the other variables read in time_series.dat and embedded in object ‘a’

Page 37: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Another example when using pc.read_var()

Page 38: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Another example when using pc.read_var()

...and we plot the entropy at the top of

the 32^3 box

Page 39: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Another examples of postprocesing with Python

Basemap: various kind of map projections

MayaVi:3D plots

Page 41: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Be careful: Python’s arrays are ordered like

i.e. REVERSED ORDER COMPARED TO PENCIL-

CODE OR IDL!!!

f[nvar,mz,my,mx]

Page 45: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Some tricks when using Python...

Page 46: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Some tricks when using Python...

• plays with ~/.ipython/ipythonrc to load modules by default (import_all pencil) and thus use ‘read_var’ instead of ‘pc.read_var()’, etc...

Page 47: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Some tricks when using Python...

• plays with ~/.ipython/ipythonrc to load modules by default (import_all pencil) and thus use ‘read_var’ instead of ‘pc.read_var()’, etc...

• import just what you need! (a cleaning is certainly needed in that respect in the PC tree...)

Page 48: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Some tricks when using Python...

• plays with ~/.ipython/ipythonrc to load modules by default (import_all pencil) and thus use ‘read_var’ instead of ‘pc.read_var()’, etc...

• import just what you need! (a cleaning is certainly needed in that respect in the PC tree...)

• launch ipython with the ‘-pylab’ option to call directly plot, contour, imshow, etc...

Page 49: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Some tricks when using Python...

• plays with ~/.ipython/ipythonrc to load modules by default (import_all pencil) and thus use ‘read_var’ instead of ‘pc.read_var()’, etc...

• import just what you need! (a cleaning is certainly needed in that respect in the PC tree...)

• launch ipython with the ‘-pylab’ option to call directly plot, contour, imshow, etc...

• accelerate the VAR* reading by passing param, grid, index, etc... [tricks.py]

Page 50: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Some tricks when using Python...

• plays with ~/.ipython/ipythonrc to load modules by default (import_all pencil) and thus use ‘read_var’ instead of ‘pc.read_var()’, etc...

• import just what you need! (a cleaning is certainly needed in that respect in the PC tree...)

• launch ipython with the ‘-pylab’ option to call directly plot, contour, imshow, etc...

• accelerate the VAR* reading by passing param, grid, index, etc... [tricks.py]

• accelerate the graphics by using an handle [tricks.py]

Page 51: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Some tricks when using Python...

• take advantage of class and objects (a.shape instead of shape(a))

• plays with ~/.ipython/ipythonrc to load modules by default (import_all pencil) and thus use ‘read_var’ instead of ‘pc.read_var()’, etc...

• import just what you need! (a cleaning is certainly needed in that respect in the PC tree...)

• launch ipython with the ‘-pylab’ option to call directly plot, contour, imshow, etc...

• accelerate the VAR* reading by passing param, grid, index, etc... [tricks.py]

• accelerate the graphics by using an handle [tricks.py]

Page 53: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Pypar example 1: compute a vertical profile in parallel

Page 54: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Pypar example 2: write PNG files in parallel for a movie

Page 55: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Widgets using Qt Designer + PyQt

Page 56: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Conclusion/Outlook

Page 57: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Conclusion/Outlook• Python can do a very good job in the Pencil Code post-processing• Its using is rapidly increasing in astrophysics (NASA, ESA, ESO, labs,...)• More in the Pencil Code philosophy (i.e. under GPL) compared to IDL

Page 58: Postprocessing with Python - ompuserpages.irap.omp.eu/~bdintrans/docs/talks/talk_pcmeeting08_19aug... · Postprocessing with Python Boris Dintrans (CNRS & University of Toulouse)

Conclusion/Outlook

• the actual Python subroutines must be rewritten in a more oriented-object form (class inheritance) •the Python tree shall maybe be re-organized in something like f90/pencil-code/python or???• what’s about the calling of Fortran or C subroutines to increase the speed?

• Python can do a very good job in the Pencil Code post-processing• Its using is rapidly increasing in astrophysics (NASA, ESA, ESO, labs,...)• More in the Pencil Code philosophy (i.e. under GPL) compared to IDL