Tuesday, August 30, 2011

Python code to create an ASCII file from arrays

Suppose you have the Numpy arrays X, Y and Z - which can contain strings, floats, integers etc - and you want to create an ASCII file in which each column corresponds to the elements of these arrays. The code snippet below allows you to do that.

In the example below, tmp.dat corresponds to the output file and X (strings), Y (floats) and Z (floats) are the arrays you want to export.

 file=open('tmp.dat','w') #create file  
 for x,y,z in zip(X,Y,Z):  
      file.write('%s\t%f\t%f\n'%(x,y,z))     #assume you separate columns by tabs  
 file.close()     #close file  

Be sure to modify the %s and %f if you change the array data types.

Thanks to Tyrel Johnson for the trick.

Sunday, August 21, 2011

References for learning Python

I list in this post the reading material that I used to learn Python. When I started learning it I had considerable previous experience with several programming languages. So the reading list below is targeted at people with some experience. In addition, my goal was to apply Python to science and data analysis.

If you don't have previous programming experience, one suggested starting point is the book Think Python: How to think like a computer scientist. It is available for free.

I learned the basic Python syntax with the official tutorial at http://docs.python.org/.

Fabricio Ferrari's lecture (in portuguese) illustrates what python is capable of for scientific data analysis and plotting. Inspiring.

If you don't learn object-oriented programming (OOP) you will not be able to explore the full potential of Python. I learned OOP from 
Introduction to Programming using Python, Programming Course for Biologists at the Pasteur Institute, Schuerer et al. Very clear exposition.

Tutorial on using Python for astronomical data analysis. How to plot spectra, read and manipulate FITS files and much more. Highly recommended.

Tuesday, August 16, 2011

Reading ASCII tables with Python

A common task in Python is to read tables contained in ASCII files.

Table with only numbers

If the table consists only of numbers it can be read with the method numpy.loadtxt. Suppose the table has two columns and you want to import these columns as the arrays x and y. You can use the command

>>> x,y = numpy.loadtxt(file,unpack=True,usecols=(0,1))

where file is a string representing the filename with the ASCII table. If you had three columns you could use 

>>> x,y,z = numpy.loadtxt(file,unpack=True,usecols=(0,1,2))

and so forth.

Unfortunately, this method does not seem to work when the input file has columns with mixed data types (e.g., strings and floats). In this case, you need to use the asciitable module.

Table with numbers and strings

Numpy.loadtxt will not work in this case and you need to use the module ASCIItable to read the ASCII data file. If you don't have it pre-installed it is straightforward to do so:

>>> easy_install asciitable

To read an ASCII file with mixed columns containing strings and numeric data and using the information in header to name the columns:

>>> t = asciitable.read('data/tab.dat', Reader=asciitable.CommentedHeader)

If the file tab.dat has the structure

#name x y z
opa 0.1 30. 50.
...

then the arrays with the data will be automatically stored as t['name'], t['x'], t['y'] etc.

If you want to read the same file but using your own name convention for the arrays (or in the case the file does not have a header specifying the column names) then use the command

>>> t=asciitable.read('data/tab.dat', names=['name','redshift','ra','dec'], Reader=asciitable.NoHeader)

The arrays will be available in this case as t['name'], t['redshift'], t['ra'] etc.

Monday, August 15, 2011

How to install python, ipython, numpy, scipy etc on Mac OS X Lion: The MacPorts way

Note added on March 7th 2012: This tutorial is deprecatedPlease refer instead to this updated tutorial.

I realized that by using MacPorts to install Python, as described in this tutorial, I am mixing libraries installed via MacPorts and the ones installed with easy_install which can lead to horrible incompatibility issues.

The updated tutorial describes how to get a working Scipy + Numpy + iPython + matplotlib installation using the built-in OS X Python and pip/easy_install.



New blog

I decided to create this blog to share my experience using Python, IPython and associated modules and packages in daily scientific work. The kind of things I do with Python are: data analysis, numerical analysis, programming (structured and object-oriented), scripting, plotting (with publication quality), handling of ASCII/FITS tables, interfacing with Fortran code etc.

I started using Python 1.5 year ago and I have had an extremely good experience with it! Before that, I used to use mainly IDL for my scientific needs. Since then, I became an advocate for using Python for scientific purposes and replacing IDL with Python (when possible of course). 

Expect blog updates on a weekly basis. As I discover new tricks or write interesting methods which I think should be useful to other people, I will try to keep updating this blog (when my work allows me to do so).

A clarification: this blog is not associated in any way with astropython.org.