# imalign¶

A module that provides functions for “aligning” images: specifically, it provides functions for computing corrections to image WCS so that image catalogs “align” to the reference catalog on the sky.

tweakwcs.imalign.fit_wcs(refcat, imcat, tpwcs, fitgeom='general', nclip=3, sigma=(3.0, 'rmse'))[source]

“Tweak” a single image’s WCS by fitting image catalog to a reference catalog. This is a simplified version of align_wcs that does not perform matching and is limited to the fitting part.

Note

Both reference and image catalogs must have been matched prior to calling fit_wcs(). This means that the lengths of both refcat and imcat catalogs must be equal and that coordinates with the same indices in both catalogs correspond to the same source.

Warning

If tpwcs.meta dictionary contains 'catalog' keyword, it will be ignored.

Parameters: refcat: astropy.table.Table A reference source catalog. The catalog must contain 'RA' and 'DEC' columns which indicate reference source world coordinates (in degrees). An optional column in the catalog is the 'weight' column, which when present, will be used in fitting. See Notes section for further details. imcat: astropy.table.Table Source catalog associated with an image whose WCS needs to be aligned by fitting a linear transformation to imcat source positions so as to align them to the same sources from the refcat catalog. Must contain 'x' and 'y' columns which indicate source coordinates (in pixels) in the associated image. An optional column in the catalog is the 'weight' column, which when present, will be used in fitting. See Notes section for further details. tpwcs: TPWCS A WCS associated with the image from which the catalog was derived. This TPWCS-subclassed WCS corrector object must also define a tangent plane that will be used for fitting the two catalogs’ sources and in which WCS corrections will be applied. fitgeom: {‘shift’, ‘rscale’, ‘general’}, optional The fitting geometry to be used in fitting the matched object lists. This parameter is used in fitting the offsets, rotations and/or scale changes from the matched object lists. The ‘general’ fit geometry allows for independent scale and rotation for each axis. nclip: int, None, optional Number (a non-negative integer) of clipping iterations in fit. Clipping will be turned off if nclip is either None or 0. sigma: float, tuple of the form (float, str), optional When a tuple is provided, first value (a positive number) indicates the number of “fit error estimates” to use for clipping. The second value (a string) indicates the statistic to be used for “fit error estimate”. Currently the following values are supported: 'rmse', 'mae', and 'std' - see iter_linear_fit for more details. When sigma is a single number, it must be a positive number and the default error estimate 'rmse' is assumed. This parameter is ignored when nclip is either None or 0. twwcs: TPWCS “Tweaked” (aligned) WCS that contains tangent-plane corrections so that reference and image catalog sources better align in the tangent plane and therefore on the sky as well.

Notes

When fitting image sources to reference catalog sources, we can specify which sources have higher weights. This can be done by assigning a “weight” to each source by specifying these values in the optional 'weight' column of either the reference catalog, image catalog, or both.

When weights are not provided, all sources are weighed equally. When only either image or reference catalog weights are provided, the sources will be weighted with the specified weights. When both image and reference catalogs specify weights for the same sources, the two weights will be combined into a single weight as:

$1/w = 1/w_i + 1/w_r$

Warning

Keep in mind that when a group catalog is created from individual catalogs, weights of the group catalog are created by concatenating weights of individual catalogs. Therefore, for the weighting of groups of catalogs to work correctly, the weights of individual catalogs should be scaled in such a way that when individual catalogs are combined into a single “group catalog”, weights preserve their relative values.

For example, let’s say a group is formed from two individual catalogs. Let’s say first catalog contains four sources with equal weights [1,1,1,1] and the second catalog contains two sources with weights [1,1] then the group’s catalogs sources will also have equal weights [1,1,1,1,1,1]. However, if each individual catalog’s weights were normalized such that sum of all weights is 1, then group’s sources will be weighed unequally: [0.25,0.25,0.25,0.25,0.5,0.5].

Upon successful completion, this function will set the 'fit_info' key value of the meta attribute of the returned TPWCS object. 'fit_info' is a dictionary with the following items:

• ‘shift’: A numpy.ndarray with two components of the computed shift.

• ‘matrix’: A 2x2 numpy.ndarray with the computed generalized rotation matrix.

• ‘proper_rot’: Rotation angle (degree) as if the rotation is proper.

• ‘rot’: A tuple of (rotx, roty) - the rotation angles with regard to the X and Y axes.

• ‘<rot>’: Arithmetic mean of the angles of rotation around X and Y axes.

• ‘scale’: A tuple of (sx, sy) - scale change in the direction of the X and Y axes.

• ‘<scale>’: Geometric mean of scales sx and sy.

• ‘skew’: Computed skew.

• ‘proper’: a boolean indicating whether the rotation is proper.

• ‘fitgeom’: Fit geometry (allowed transformations) used for fitting data (to minimize residuals). This is copy of the input argument fitgeom.

• ‘center’: Center of rotation in the tangent plane of the computed linear transformations.

• ‘fitmask’: A boolean array indicating which source positions where used for fitting (True) and which were clipped out (False). NOTE: For weighted fits, positions with zero weights are automatically excluded from the fits.

• ‘eff_nclip’: Effective number of clipping iterations

• ‘rmse’: fit Root-Mean-Square Error in tangent plane coordinates of corrected image source positions from reference source positions.

• ‘mae’: fit Mean Absolute Error in tangent plane coordinates of corrected image source positions from reference source positions.

• ‘std’: Norm of the standard deviation of the residuals in tangent plane along each axis.

• ‘resids’: An array of residuals of the fit in the tangent plane.

NOTE: Only the residuals for the “valid” points are reported here. Therefore the length of this array may be smaller than the length of input arrays of positions.

• ‘fit_RA’: first (corrected) world coordinate of input source positions used in fitting.

• ‘fit_DEC’: second (corrected) world coordinate of input source positions used in fitting.

• ‘status’: Alignment status. Currently two possible status are possible 'SUCCESS' or 'FAILED: reason for failure'. When alignment failed, the reason for failure is provided after alignment status.

tweakwcs.imalign.align_wcs(wcscat, refcat=None, enforce_user_order=True, expand_refcat=False, minobj=None, match=<tweakwcs.matchutils.TPMatch object at 0x7f0ed9445250>, fitgeom='general', nclip=3, sigma=(3.0, 'rmse'))[source]

Align (groups of) image catalogs by adjusting the parameters of their WCS based on fits between matched sources in these catalogs and a reference catalog which may be automatically created from one of the input wcscat catalogs.

Warning

This function modifies the wcs attribute of each item in the input wcscat list!

Upon completion, this function will add a field 'fit_info' to the meta attribute of the input WCS correctors (except of the one chosen as a reference catalog when refcat is None) containing a dictionary describing matching and fit results. For a description of the items in this dictionary, see tweakwcs.wcsimage.WCSGroupCatalog.align_to_ref(). In addition to the status set by align_to_ref(), this function may set status to 'REFERENCE' for an input image used as a reference image when a reference catalog is not provided. In this case no other fields in the 'fit_info' will be present because a reference image is not being aligned. When alignment failed, the reason for failure is provided after alignment status.

Warning

Unless status in 'fit_info' is 'SUCCESS', there is no guarantee that other fields in 'fit_info' are present or valid. Therefore, it is advisable verify that status is 'SUCCESS' before attempting to access other items, for example:

>>> fit_info = wcscat[0].meta.get('fit_info')  # noqa
>>> if fit_info['status'] == 'SUCCESS':
...     print("shifts: [{}, {}]".format(*fit_info['shift']))
... else:
...     print("tweak info not available for this image")

Parameters: wcscat: tweakwcs.tpwcs.TPWCS, list of tweakwcs.tpwcs.TPWCS A list of all TPWCS-derived WCS correctors whose meta dictionary must contain 'catalog' item with a non-empty table value of type astropy.table.Table. This catalog must contain 'x' and 'y' columns which indicate source coordinates (in pixels) in the associated image. An optional column in the catalog is the 'weight' column, which when present, will be used in fitting. See Notes section for further details. In addition to 'catalog', the following items in the meta dictionary are recognized/supported: 'name' and 'group_id'. 'name' is catalog’s name and it used to identify catalog during logging. If 'name' value is None or not present at all in the meta of a catalog, the name of that catalog will reported as 'Unknown'. Group ID that may be used for identifying catalogs that need to be aligned together. group_id must be hashable. If 'group_id' is None or not provided, each input WCS/catalog will be aligned individually. Note Upon completion this function will add 'fit_info' item (a dictionary) to input object’s meta dictionary. See Notes section for more details. Warning This function modifies the WCS of TPWCS objects by calling their set_correction() method. refcat: astropy.table.Table, optional A reference source catalog. The catalog must contain 'RA' and 'DEC' columns which indicate reference source world coordinates (in degrees). An optional column in the catalog is the 'weight' column, which when present, will be used in fitting. See Notes section for further details. enforce_user_order: bool, optional Specifies whether images should be aligned in the order specified in the file input parameter or align should optimize the order of alignment by intersection area of the images. Default value (True) will align images in the user specified order, except when some images cannot be aligned in which case align will optimize the image alignment order. Alignment order optimization is available only when expand_refcat is True. expand_refcat: bool, optional Specifies whether to add new sources from just matched images to the reference catalog to allow next image to be matched against an expanded reference catalog. By delault, the reference catalog is not being expanded. If refcat is not None and contains an 'id' column, then sources being added to the reference catalog will be assigned consecutive IDs that continue maximum ID in the refcat. If one desires to uniquely associate source in the expanded catalog to their original catalogs, it is recommended that one assign unique IDs to all sources in all input catalogs and in the reference catalog in a separate column such as 'uuid'. minobj: int, None, optional Minimum number of identified objects from each input image to use in matching objects from other images. If the default None value is used then align will automatically deternmine the minimum number of sources from the value of the fitgeom parameter. match: MatchCatalogs, function, None, optional A callable that takes two arguments: a reference catalog and an image catalog. Both catalogs will have columns 'TPx' and 'TPy' that represent the source coordinates in some common (to both catalogs) coordinate system. fitgeom: {‘shift’, ‘rscale’, ‘general’}, optional The fitting geometry to be used in fitting the matched object lists. This parameter is used in fitting the offsets, rotations and/or scale changes from the matched object lists. The ‘general’ fit geometry allows for independent scale and rotation for each axis. nclip: int, None, optional Number (a non-negative integer) of clipping iterations in fit. Clipping will be turned off if nclip is either None or 0. sigma: float, tuple of the form (float, str), optional When a tuple is provided, first value (a positive number) indicates the number of “fit error estimates” to use for clipping. The second value (a string) indicates the statistic to be used for “fit error estimate”. Currently the following values are supported: 'rmse', 'mae', and 'std' - see iter_linear_fit for more details. When sigma is a single number, it must be a positive number and the default error estimate 'rmse' is assumed. This parameter is ignored when nclip is either None or 0. eff_refcat: astropy.table.Table Effective reference catalog used for aligning all images. Depending on the values of the input parameters refcat, enforce_user_order, and expand_refcat, effective reference catalog may be one of the input image catalogs, the original refcat catalog, an expanded refcat with a combination of source positions from all input images.

Notes

1. Weights:

When fitting image sources to reference catalog sources, we can specify which sources have higher weights. This can be done by assigning a “weight” to each source by specifying these values in the optional 'weight' column of either the reference catalog, image catalog, or both.

When weights are not provided, all sources are weighed equally. When only either image or reference catalog weights are provided, the sources will be weighted with the specified weights. When both image and reference catalogs specify weights for the same sources, the two weights will be combined into a single weight as:

$1/w = 1/w_i + 1/w_r$

Warning

Keep in mind that when a group catalog is created from individual catalogs, weights of the group catalog are created by concatenating weights of individual catalogs. Therefore, for the weighting of groups of catalogs to work correctly, the weights of individual catalogs should be scaled in such a way that when individual catalogs are combined into a single “group catalog”, weights preserve their relative values.

For example, let’s say a group is formed from two individual catalogs. Let’s say first catalog contains four sources with equal weights [1,1,1,1] and the second catalog contains two sources with weights [1,1] then the group’s catalogs sources will also have equal weights [1,1,1,1,1,1]. However, if each individual catalog’s weights were normalized such that sum of all weights is 1, then group’s sources will be weighed unequally: [0.25,0.25,0.25,0.25,0.5,0.5].

Warning

When image catalogs contain optional 'weight' column, then all image catalogs in a group must contain this column.

2. 'fit_info':

Upon completion, this function will add 'fit_info' item (itself a dictionary) to input object’s meta dictionary. If input objects are TPWCS WCS correctors, then TPWCS.meta['fit_info'] will be set to a dictionary containing fit information.

Note

For TPWCS that are aligned in a group, the 'matrix' and 'shift' items in the 'fit_info' dictionary may differ from the values of the same items in TPWCS.meta dictionary. This is normal since WCS corrections (in TPWCS) are applied in the image’s WCS plane while fit may be performed in a slightly different tangent plane.