Drizzle¶
- class drizzle.resample.Drizzle(kernel='square', fillval=None, out_shape=None, out_img=None, out_wht=None, out_ctx=None, exptime=0.0, begin_ctx_id=0, max_ctx_id=None, disable_ctx=False)[source]¶
Bases:
object
A class for managing resampling and co-adding of multiple images onto a common output grid. The main method of this class is
add_image()
. The main functionality of this class is to resample and co-add multiple images onto one output image using the “drizzle” algorithm described in Fruchter and Hook, PASP 2002. In the simplest terms, it redistributes flux from input pixels to one or more output pixels based on the chosen kernel, supplied weights, and input-to-output coordinate transformations as defined by thepixmap
argument. For more details, see drizzle Documentation.This class keeps track of the total exposure time of all co-added images and also of which input images have contributed to an output (resampled) pixel. This is accomplished via context image.
Main outputs of
add_image()
can be accessed as class propertiesout_img
,out_wht
,out_ctx
, andexptime
.Warning
Output arrays (
out_img
,out_wht
, andout_ctx
) can be pre-allocated by the caller and be passed to the initializer or the class initializer can allocate these arrays based on other input parameters such asoutput_shape
. If caller-supplied output arrays have the correct type (numpy.float32 forout_img
andout_wht
and numpy.int32 for theout_ctx
array) and ifout_ctx
is large enough not to need to be resized, these arrays will be used as is and may be modified by theadd_image()
method. If not, a copy of these arrays will be made when converting to the expected type (or expanding the context array).Output Science Image¶
Output science image is obtained by adding input pixel fluxes according to equations (4) and (5) in Fruchter and Hook, PASP 2002. The weights and coefficients in those equations will depend on the chosen kernel, input image weights, and pixel overlaps computed from
pixmap
.Output Weight Image¶
Output weight image stores the total weight of output science pixels according to equation (4) in Fruchter and Hook, PASP 2002. It depends on the chosen kernel, input image weights, and pixel overlaps computed from
pixmap
.Output Context Image¶
Each pixel in the context image is a bit field that encodes information about which input image has contributed to the corresponding pixel in the resampled data array. Context image uses 32 bit integers to encode this information and hence it can keep track of only 32 input images. First bit corresponds to the first input image, second bit corrsponds to the second input image, and so on. We call this (0-indexed) order “context ID” which is represented by the
ctx_id
parameter/property. If the number of input images exceeds 32, then it is necessary to have multiple context images (“planes”) to hold information about all input images with the first plane encoding which of the first 32 images contributed to the output data pixel, second plane representing next 32 input images (number 33-64), etc. For this reason, context array is either a 2D array (if the total number of resampled images is less than 33) of the type numpy.int32 and shape(ny, nx)
or a a 3D array of shape(np, ny, nx)
wherenx
andny
are dimensions of image’s data.np
is the number of “planes” equal to(number of input images - 1) // 32 + 1
. If a bit at positionk
in a pixel with coordinates(p, y, x)
is 0 then input image number32 * p + k
(0-indexed) did not contribute to the output data pixel with array coordinates(y, x)
and if that bit is 1 then input image number32 * p + k
did contribute to the pixel(y, x)
in the resampled image.As an example, let’s assume we have 8 input images. Then, when
out_ctx
pixel values are displayed using binary representation (and decimal in parenthesis), one could see values like this:00000001 (1) - only first input image contributed to this output pixel; 00000010 (2) - 2nd input image contributed; 00000100 (4) - 3rd input image contributed; 10000000 (128) - 8th input image contributed; 10000100 (132=128+4) - 3rd and 8th input images contributed; 11001101 (205=1+4+8+64+128) - input images 1, 3, 4, 7, 8 have contributed to this output pixel.
In order to test if a specific input image contributed to an output pixel, one needs to use bitwise operations. Using the example above, to test whether input images number 4 and 5 have contributed to the output pixel whose corresponding
out_ctx
value is 205 (11001101 in binary form) we can do the following:>>> bool(205 & (1 << (5 - 1))) # (205 & 16) = 0 (== 0 => False): did NOT contribute False >>> bool(205 & (1 << (4 - 1))) # (205 & 8) = 8 (!= 0 => True): did contribute True
In general, to get a list of all input images that have contributed to an output resampled pixel with image coordinates
(x, y)
, and given a context arrayctx
, one can do something like this:>>> import numpy as np >>> np.flatnonzero([v & (1 << k) for v in ctx[:, y, x] for k in range(32)])
For convenience, this functionality was implemented in the
decode_context()
function.References
A full description of the drizzling algorithm can be found in Fruchter and Hook, PASP 2002.
Examples
# wcs1 - WCS of the input image usually with distortions (to be resampled) # wcs2 - WCS of the output image without distortions import numpy as np from drizzle.resample import Drizzle from drizzle.utils import calc_pixmap # simulate some data and a pixel map: data = np.ones((240, 570)) pixmap = calc_pixmap(wcs1, wcs2) # or simulate a mapping from input image to output image frame: # y, x = np.indices((240, 570), dtype=np.float64) # pixmap = np.dstack([x, y]) # initialize Drizzle object d = Drizzle(out_shape=(240, 570)) d.add_image(data, exptime=15, pixmap=pixmap) # access outputs: d.out_img d.out_ctx d.out_wht
Attributes Summary
Context image "ID" (0-based ) of the next image to be resampled.
Fill value for output pixels without contributions from input images.
Resampling kernel.
Output "context" image.
Output resampled image.
Output weight image.
Total exposure time of all resampled images.
Methods Summary
add_image
(data, exptime, pixmap[, scale, ...])Resample and add an image to the cumulative output image.
Attributes Documentation
- ctx_id¶
Context image “ID” (0-based ) of the next image to be resampled.
- fillval¶
Fill value for output pixels without contributions from input images.
- kernel¶
Resampling kernel.
- out_ctx¶
Output “context” image.
- out_img¶
Output resampled image.
- out_wht¶
Output weight image.
- total_exptime¶
Total exposure time of all resampled images.
Methods Documentation
- add_image(data, exptime, pixmap, scale=1.0, weight_map=None, wht_scale=1.0, pixfrac=1.0, in_units='cps', xmin=None, xmax=None, ymin=None, ymax=None)[source]¶
Resample and add an image to the cumulative output image. Also, update output total weight image and context images.
- Parameters:
data (2D numpy.ndarray) – A 2D numpy array containing the input image to be drizzled.
exptime (float) – The exposure time of the input image, a positive number. The exposure time is used to scale the image if the units are counts.
pixmap (3D array) – A mapping from input image (
data
) coordinates to resampled (out_img
) coordinates.pixmap
must be an array of shape(Ny, Nx, 2)
where(Ny, Nx)
is the shape of the input image.pixmap[..., 0]
forms a 2D array of X-coordinates of input pixels in the ouput frame andpixmap[..., 1]
forms a 2D array of Y-coordinates of input pixels in the ouput coordinate frame.scale (float, optional) – The pixel scale of the input image. Conceptually, this is the linear dimension of a side of a pixel in the input image, but it is not limited to this and can be set to change how the drizzling algorithm operates.
weight_map (2D array, None, optional) –
A 2D numpy array containing the pixel by pixel weighting. Must have the same dimensions as
data
.When
weight_map
is None, the weight of input data pixels will be assumed to be 1.wht_scale (float) – A scaling factor applied to the pixel by pixel weighting.
pixfrac (float, optional) – The fraction of a pixel that the pixel flux is confined to. The default value of 1 has the pixel flux evenly spread across the image. A value of 0.5 confines it to half a pixel in the linear dimension, so the flux is confined to a quarter of the pixel area when the square kernel is used.
in_units (str) – The units of the input image. The units can either be “counts” or “cps” (counts per second.)
xmin (float, optional) – This and the following three parameters set a bounding rectangle on the input image. Only pixels on the input image inside this rectangle will have their flux added to the output image. Xmin sets the minimum value of the x dimension. The x dimension is the dimension that varies quickest on the image. If the value is zero, no minimum will be set in the x dimension. All four parameters are zero based, counting starts at zero.
xmax (float, optional) – Sets the maximum value of the x dimension on the bounding box of the input image. If the value is zero, no maximum will be set in the x dimension, the full x dimension of the output image is the bounding box.
ymin (float, optional) – Sets the minimum value in the y dimension on the bounding box. The y dimension varies less rapidly than the x and represents the line index on the input image. If the value is zero, no minimum will be set in the y dimension.
ymax (float, optional) – Sets the maximum value in the y dimension. If the value is zero, no maximum will be set in the y dimension, the full x dimension of the output image is the bounding box.
- Returns:
nskip (float) – The number of lines from the box defined by
((xmin, xmax), (ymin, ymax))
in the input image that were ignored and did not contribute to the output image.nmiss (float) – The number of pixels from the box defined by
((xmin, xmax), (ymin, ymax))
in the input image that were ignored and did not contribute to the output image.