Chandra X-Ray Observatory
	(CXC)
Skip to the navigation links
Last modified: August 2010

URL: http://cxc.harvard.edu/ciao/ahelp/ciao_smooth.html
AHELP for CIAO 4.9

ciao_smooth

Context: contrib

Synopsis

Simple smoothing routines for 2D data

Syntax

Load the module:
from ciao_contrib.smooth import *

Routines:
smoothed = gsmooth(image, sigma, nsigma=5)
smoothed = bsmooth(image, radius)
smoothed = tsmooth(image, radius)
smoothed = fsmooth(image, filename, norm=True, origin=None)
smoothed = ismooth(image, kernel, norm=True, origin=None)

where image is a 2D numpy array (which can contain NaN values)

Description

The ciao_contrib.smooth module provides several routines to smooth (or convolve) 2D data, and is provided as part of the CIAO contributed scripts package.

Loading the routines

The module can be loaded into ChIPS, Sherpa or a Python program by saying:

from ciao_contrib.smooth import *

Gaussian smoothing

The gsmooth() routine smoothes an image with a gaussian with a given sigma (in pixels) and using a box with half-width equal to

sigma * nsigma

The default value for nsigma is 5.

Top-hat smoothing

The tsmooth() routine smoothes an image with a top-hat filter of the given radius. This is a circular kernel which is flat within the radius and zero outside it. The radius is treated as an integer value here.

Box-car smoothing

The bsmooth() routine smoothes an image with a box-car filter of the given radius. This is a square kernel with a constant value for all pixels. The radius is treated as an integer value here.

Smothing by an image

The ismooth() routine will smooth an image by a kernel image. The default behavior is to normalize the kernel by its sum before smoothing; to use the input kernel values directly set the norm parameter to True. The kernel is assumed to be centered; to use a different center use the origin argument which accepts a pair (y,x), giving the pixel location of the origin. This location uses 0-indexing, so the center of a nx=5 by ny=3 array would be (1,2). Any non-finite values in the kernel image are replaced by 0.

Smothing by a file

The fsmooth() routine is similar to the ismooth() routine; the difference being that instead of an image the input is the name of a file containing the image to use as the smoothing kernel.

Example 1

chips> from ciao_contrib.smooth import *
chips> cr = read_file("img.fits")
chips> img = copy_piximgvals(cr)
chips> simg = gsmooth(img, 3)
chips> add_image(simg)

Here we smooth the data in img.fits with a gaussian using a sigma of 3 pixels before displaying it using the ChIPS add_image() routine. See the following example for how the WCS information can be retained in the output plot.

Example 2

chips> cr = read_file("img.fits")
chips> img = copy_piximgvals(cr)
chips> set_piximgvals(cr, gsmooth(img, 3))
chips> add_image(cr)

This is the same as the previous example except that we store the smoothed image back into the Crate. By passing this crate (the varable cr) to the add_image() call we can display the smoothed image using any WCS information read in with the file.

Example 3

chips> cr = read_file("img.fits")
chips> img = copy_piximgvals(cr)
chips> simg = gsmooth(img, 3)
chips> set_piximgvals(cr, np.arcsinh(simg))
chips> add_image(cr)

Here the smoothed image is scaled using the "arcsinh" function before it is displayed. See "ahelp scale_image_crate" for more information on this scaling.

Example 4

chips> cr = read_file("img.fits")
chips> img = copy_piximgvals(cr)
chips> clear()
chips> split(2,2)
chips> add_image(img)
chips> current_plot("plot2")
chips> add_image(gsmooth(img, 5))
chips> current_plot("plot3")
chips> add_image(bsmooth(img, 5))
chips> current_plot("plot4")
chips> add_image(tsmooth(img, 5))

Here we compare the original image to gaussian, box-car, and top-hat smoothed versions in a two-by-two plot.

Example 5

chips> cr1 = read_file("img.fits")
chips> img = copy_piximgvals(cr1)
chips> cr2 = read_file("psf.fits")
chips> kern = copy_piximgvals(cr2)
chips> sm = ismooth(img, kern)

Here we smooth an image ("img.fits") using another image ("psf.fits"). The kernel is assumed to be centered in the image in psf.fits. See the following example for another way of performing this task.

Example 6

chips> cr1 = read_file("img.fits")
chips> img = copy_piximgvals(cr1)
chips> sm = fsmooth(img, "psf.fits")

Here we smooth an image ("img.fits") using another image ("psf.fits"). The kernel is assumed to be centered in the image in psf.fits. See the preceeding example for another way of performing this task.

Example 7

chips> ivals = np.asarray([0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0])
chips> ivals.resize(4,4)
chips> kvals = np.asarray([0,0,0,1,1,1,0,0,0]).reshape(3,3)
chips> svals = ismooth(ivals, kvals)

Here we create two 2D arrays (the "image" with a size of 4 by 4 and the "kernel" with a size of 3 by 3) and smooth ivals by kvals. Although both arrays are square they do not need to be; the kernel should have odd dimensionality and be centered in the array to avoid the possibility of a coordinate shift in the output image.

The input arrays are:

chips> print(ivals)
[[0 0 0 0]
 [0 1 1 0]
 [0 1 1 0]
 [0 0 0 0]]
chips> print(kvals)
[[ 0.  0.  0.]
 [ 1.  1.  1.]
 [ 0.  0.  0.]]

and the result is:

chips> print(svals)
[[ 0.          0.          0.          0.        ]
 [ 0.33333333  0.66666667  0.66666667  0.33333333]
 [ 0.33333333  0.66666667  0.66666667  0.33333333]
 [ 0.          0.          0.          0.        ]]

Example 8

chips> ivals = np.asarray([0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0])
chips> ivals.resize(4,4)
chips> kvals = np.asarray([0,0,0,1,1,1,0,0,0]).reshape(3,3)
chips> svals = ismooth(ivals, kvals, norm=False)

By setting the norm parameter to False the kernel image is not normalized before smoothing, which creates an output image of:

chips> print(svals)
[[ 0.  0.  0.  0.]
 [ 1.  2.  2.  1.]
 [ 1.  2.  2.  1.]
 [ 0.  0.  0.  0.]]

Example 9

chips> ivals = np.asarray([0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0])
chips> ivals.resize(4,4)
chips> kvals = np.asarray([0,0,0,0,0,0,0,1,0]).reshape(3,3)
chips> svals1 = ismooth(ivals, kvals)
chips> svals2 = ismooth(ivals, kvals, origin=(2,1))

In this example we show how the origin parameter of the smooth routine is used by creating a kernel where only a single pixel set, but this pixel is off-set from the center of the kernel:

chips> print(kvals)
[[0 0 0]
 [0 0 0]
 [0 1 0]]

When used with the default origin setting, the result is a shifted output (svals1), as shown below:

chips> print(svals1)
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  1.  1.  0.]
 [ 0.  1.  1.  0.]]

By changing the origin to the peak position of the kernel, the output is no-longer shifted with respect to the input image.

chips> print(svals2)
[[ 0.  0.  0.  0.]
 [ 0.  1.  1.  0.]
 [ 0.  1.  1.  0.]
 [ 0.  0.  0.  0.]]

Dealing with NaN/Inf values

Since the smoothing is performed using a Fast Fourier Transform (FFT), any pixels with a non-finite value (such as NaN or Inf) are set to 0. On output these pixels are set to NaN values.

Identity transforms

If the scale parameter (radius or sigma) is set to 0 for gsmooth(), bsmooth() or tsmooth() then the smoothing function is set to a 1 by 1 image - i.e. the identity transform. Since the smoothing is done via a FFT the output image in this case will be very-close, but not identical, to the input image.

Bugs

See the bugs pages for an up-to-date listing of known bugs.

Refer to the CIAO bug pages for an up-to-date listing of known issues.


Last modified: August 2010
Smithsonian Institute Smithsonian Institute

The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory. 60 Garden Street, Cambridge, MA 02138 USA.   Email:   cxchelp@head.cfa.harvard.edu Smithsonian Institution, Copyright © 1998-2017. All rights reserved.