{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial\n", "\n", "This tutorial shows the basic steps of using SEP to detect objects in an image and perform some basic aperture photometry.\n", "\n", "Here, we use the `fitsio` package, just to read the test image, but you can also use `astropy.io.fits` for this purpose (or any other FITS reader)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import sep_pjw as sep" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# additional setup for reading the test image and displaying plots\n", "import fitsio\n", "import matplotlib.pyplot as plt\n", "from matplotlib import rcParams\n", "\n", "%matplotlib inline\n", "\n", "rcParams['figure.figsize'] = [10., 8.]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we'll read an example image from a FITS file and display it, just to show what we're dealing with. The example image is just 256 x 256 pixels." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# read image into standard 2-d numpy array\n", "data = fitsio.read(\"../data/image.fits\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# show the image\n", "m, s = np.mean(data), np.std(data)\n", "plt.imshow(data, interpolation='nearest', cmap='gray', vmin=m-s, vmax=m+s, origin='lower')\n", "plt.colorbar();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Background subtraction\n", "\n", "Most optical/IR data must be background subtracted before sources can be detected. In SEP, background estimation and source detection are two separate steps." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# measure a spatially varying background on the image\n", "bkg = sep.Background(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are various options for controlling the box size used in estimating the background. It is also possible to mask pixels. For example:\n", "```python\n", "bkg = sep.Background(data, mask=mask, bw=64, bh=64, fw=3, fh=3)\n", "```\n", "See the reference section for descriptions of these parameters.\n", "\n", "This returns an `Background` object that holds information on the spatially varying background and spatially varying background noise level. We can now do various things with this `Background` object:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# get a \"global\" mean and noise of the image background:\n", "print(bkg.globalback)\n", "print(bkg.globalrms)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# evaluate background as 2-d array, same size as original image\n", "bkg_image = bkg.back()\n", "# bkg_image = np.array(bkg) # equivalent to above" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# show the background\n", "plt.imshow(bkg_image, interpolation='nearest', cmap='gray', origin='lower')\n", "plt.colorbar();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# evaluate the background noise as 2-d array, same size as original image\n", "bkg_rms = bkg.rms()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# show the background noise\n", "plt.imshow(bkg_rms, interpolation='nearest', cmap='gray', origin='lower')\n", "plt.colorbar();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# subtract the background\n", "data_sub = data - bkg\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One can also subtract the background from the data array in-place by doing `bkg.subfrom(data)`.\n", "\n", "