Showing posts with label ascii. Show all posts
Showing posts with label ascii. Show all posts

Friday, December 16, 2011

Script illustrating how to do a linear regression and plot the confidence band of the fit

The script below illustrates how to carry out a simple linear regression of data stored in an ASCII file, plot the linear fit and the 2 sigma confidence band.

The script invokes the confband method described here to plot the confidence bands. The confband is assumed to be lying inside the "nemmen" module (not yet publicly available, sorry) but you can place it in any module you want.

I got the test data here to perform the fitting.

After running the script you should get the following plot:
where the best-fit line is displayed in green and the shaded area is in gray.

The script below is also available at Github Gist.

   
 import numpy, pylab, scipy  
 import nemmen  
   
 # Data taken from http://orion.math.iastate.edu/burkardt/data/regression/x01.txt.  
 # I removed the header from the file and left only the data in 'testdata.dat'.  
 xdata,ydata = numpy.loadtxt('testdata.dat',unpack=True,usecols=(1,2))  
 xdata=numpy.log10(xdata) # take the logs  
 ydata=numpy.log10(ydata)  
   
 # Linear fit  
 a, b, r, p, err = scipy.stats.linregress(xdata,ydata)  
   
 # Generates arrays with the fit  
 x=numpy.linspace(xdata.min(),xdata.max(),100)  
 y=a*x+b  
   
 # Calculates the 2 sigma confidence band contours for the fit  
 lcb,ucb,xcb=nemmen.confband(xdata,ydata,a,b,conf=0.95)  
   
 # Plots the fit and data  
 pylab.clf()  
 pylab.plot(xdata,ydata,'o')  
 pylab.plot(x,y)  
   
 # Plots the confidence band as shaded area  
 pylab.fill_between(xcb, lcb, ucb, alpha=0.3, facecolor='gray')  
   
 pylab.show()  
 pylab.draw()  

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.