Manipulating and Viewing FITS Files in Python with pyds9

by Jessica on July 14, 2010

For anyone who uses python and ds9 to visualize their FITS files, I think pyds9 is now a must-have. It is officially written and developed through SAOImage ds9 so it will be supported for the long haul. Here are the primary links to get going:

  • TARball for installation: Source
  • Documentation for installation and use: Docs

The documentation is pretty basic so I put together a very small tutorial of a way I might typically use it. Most of the controls work through the XPA interface, which seems very flexible, if a little tricky to figure out exactly how to make it work for specific use-cases. If you have samples of how you use pyds9 or ds9 xpa, post them in the comments or on this wiki page.

from scipy import stats
import numpy as np
import ds9
 
# Make a 2D gaussian image that is stored in a 2D numpy array
x = np.arange(-3, 3, 0.1)
xx, yy = np.meshgrid(x, x)
gauss2d = stats.norm.pdf(xx) * stats.norm.pdf(yy)
 
# Now open ds9 (this assumes no ds9 instance is yet running)
d = ds9.ds9()
 
# Load up our 2D gaussian
d.set_np2arr(gauss2d)
 
# Zoom to fit
d.set('zoom to fit')
 
# Change the colormap and scaling
d.set('cmap bb')
d.set('scale log')
 
# Add a label
d.set('regions command {text 30 20 #text="Fun with pyds9" font="times 18 bold"}')
 
# Now you can play in ds9 to your heart's content.
# Check back to see what the current color scale is.
print d.get('scale')
 
# Finally, save your completed image (including regions or labels)
d.set('saveimage png my_pyds9_img.png')

{ 9 comments… read them below or add one }

1 Adam Ginsburg July 14, 2010 at 10:37 am

The great thing about pyds9 is that it makes it easy to write your own quick analysis tasks – I’m sure there must be others who have been desperate for simple aperture photometry in ds9. My attempt at that is http://code.google.com/p/agpy/source/browse/trunk/agpy/ds9_phot.py plus http://code.google.com/p/agpy/source/browse/trunk/agpy/phot.ds9.ans.

I have found XPA useful without python for making DS9 figures reproducible. DS9 is capable of making publication-quality figures, but it requires a lot of button clicks… XPA allows you to script it.

Reply

2 Kathleen July 14, 2010 at 2:29 pm

Jessica, I’ll take you as an expert and ask you: Have you found a way to plot points, not boxes, actual solid points one pixel-wide? If you have worked with the IRAF, I’m interested in reproducing the behaviour of tvmark to visually identify bad pixels, for example.

I’ve asked Jessica, but if anybody has the answer, please share.

Thanks!

Reply

3 Neil July 14, 2010 at 6:34 pm

I wrote a couple of routines that use pyds9 to read image data from regions (http://bitbucket.org/nhmc/pyserpens/src/tip/ds9utils.py).

It’s very cool to be able to load an image inside ds9, then use the python interpreter to play around with the data inside regions you define using ds9.

I’m not sure how to plot a one-pixel wide solid point, but regions can just be a single cross or plus sign (try them out in the region – shape menu).

Reply

4 Adam Ginsburg July 14, 2010 at 6:41 pm

Neil – for region parsing, there’s a pretty complete package called “pyregion” available from http://github.com/leejjoon/pyregion and/or http://leejjoon.github.com/pyregion/ (while the URLs look quite similar, they take you to very different front-pages).

Kathleen – The best I can think of is replacing the bad data in the array with NAN and re-displaying it. You can set the NAN color to be anything in the Edit:Preferences menu.

Reply

5 Jessica July 14, 2010 at 7:24 pm

In principle you should be able to use tvmark as well:

from pyraf import iraf as ir
ir.images()
ir.tv()
ir.tvmark(1, cooFile, interactive='yes')

In practice, I haven’t gotten it to work, but then again, I have never used tvmark before.

Reply

6 Kathleen July 14, 2010 at 7:32 pm

Thanks for the suggestions. I see that it isn’t something I missed, it is just not implemented.

Reply

7 Christopher Hanley July 15, 2010 at 11:16 am

Have you ever used numdisplay?
http://stsdas.stsci.edu/numdisplay/

It has been around a long time and is well supported by STScI. It works well with Pyraf.

Reply

8 Kathleen July 16, 2010 at 3:47 am

Yes, Chris, I’ve used numdisplay extensively. It does exactly what I need it to do. The only problem was that it was too slow to draw hundreds of points. I think you guys improved the performance following the report I made. I haven’t had the chance to test it out again.

Reply

9 Sean August 3, 2011 at 10:37 pm

Is anyone else unable to manipulate the scale with the mouse in the normal ds9 way when using pyds9?

Reply

Leave a Comment

Previous post:

Next post: