Surveying the Universe
Posts tagged Python
Astropython
Jan 19th
You (both of you) might well be interested in the new Astropython site, which looks excellent. Here's the site's own description:
Research in astronomy includes the analysis of astronomical images, parsing and manipulation of large catalogs, statistical yet often visual inference, and the creation of data visualizations for publication and dissemination of results.
The purpose of this web site is to act as a community knowledge base for performing this research with the open source Python language. It provides a forum for general discussion, advice, or relevant news items, collecting lists of useful resources, users' code snippets or scripts, and longer tutorials on specific topics. The topics within these pages are presented in a list view with the ability to sort by date or topic. A traditional "blog" view of the most recently posted topics is visible from the site Home page.
Pixelating a 2-D Gaussian with Python
Sep 4th
They're coming thick and fast now.
Here's a Python function to accompany the previous post. It's not maximally efficient, but should make sense...
from scipy import stats def gaussian_pixel(minxy, maxxy, sigma, meanxy=(0.,0.), norm=None): """Return the value of a pixel sampling a 2D Gaussian, normalized such that the area under the Gaussian is 1 (default) or such that the peak is given by norm.""" x1, y1 = minxy x2, y2 = maxxy x0, y0 = meanxy if norm is None: norm = 1. / 2 / math.pi / sigma ** 2 return norm * 2 * math.pi * sigma ** 2 / (x2 - x1) / (y2 - y1) * ( (1 - stats.erfc((x2 - x0) / math.sqrt(2) / sigma)) / 2. - (1 - stats.erfc((x1 - x0) / math.sqrt(2) / sigma)) / 2.) * ( (1 - stats.erfc((y2 - y0) / math.sqrt(2) / sigma)) / 2. - (1 - stats.erfc((y1 - y0) / math.sqrt(2) / sigma)) / 2.)
Visualizing noisy images
May 13th
You have an image. Each pixel has a value with some uncertainty. How do you visualize the uncertainty in each pixel? Like this:
Here's the Python code
import numpy as np from matplotlib import pyplot as plt class FlickerImage(object): def __init__(self, im, err): self.im = im.copy() self.err = err.copy() finite = np.isfinite(self.im + self.err) self.vmin = (self.im - 2 * self.err)[finite].min() self.vmax = (self.im + 2 * self.err)[finite].max() self.im[np.invert(finite)] = self.vmax self.err[np.invert(finite)] = 0 def flicker(self): fg = plt.imshow(np.zeros(self.im.shape), interpolation='nearest', vmin=self.vmin, vmax=self.vmax) while True: ran = np.random.normal(size=im.shape) fg.set_data(im + err * ran) plt.draw()
And here's an example script:
import pyfits f = pyfits.open('file.fits') im = f["IMAGE"].data err = f["ERROR"].data flicker_image = FlickerImage(im, err) flicker_image.flicker()
Python, FITS and DS9
Apr 1st
Here's an easy way to display FITS images (or any array) in DS9 using Python (with PyFITS, NumPy and Numdisplay, which is part of stsci_python). First launch DS9, then in Python:
import numdisplay import pyfits arr = pyfits.getdata('file.fits') numdisplay.display(arr)
Easy!
Alternatively, the Kapteyn package seems excellent, and uses Python's matplotlib for displaying images. It requires WCSLIB to run, though, so the installation process is a bit longer [update: apparently that bit is no longer true - 17 Oct 2011].
A third option is to use python-sao:
import pysao import pyfits ds9 = pysao.ds9() f = pyfits.open('file.fits') ds9.view(f[0])
Easy again! And the WCS information is preserved, which doesn't seem to be the case with Numdisplay.
pIDLy: IDL within Python
Jan 31st
Now Python and IDL can talk to each other (okay, Python talks to IDL and IDL does what it's told), using pIDLy (pronounce as you please). I experimented with a few other solutions available online but couldn't get them to work. So I cobbled this one together with surprisingly little trouble, thanks largely to pexpect.

I live in York and I'm a research fellow in