linearfit
A module that provides algorithms for performing linear fit between sets of 2D points.
- Authors:
Mihai Cara, Warren Hack
- License:
- tweakwcs.linearfit.build_fit_matrix(rot, scale=1)[source]
Create an affine transformation matrix (2x2) from the provided rotation angle(s) and scale(s):
\[\begin{split}M = \begin{bmatrix} s_x \cos(\theta_x) & s_y \sin(\theta_y) \\ -s_x \sin(\theta_x) & s_y \cos(\theta_y) \end{bmatrix}\end{split}\]- Parameters:
- Returns:
matrix – A 2x2
numpy.ndarraycontaining coefficients of a liniear transformation.- Return type:
- tweakwcs.linearfit.iter_linear_fit(xy, uv, wxy=None, wuv=None, fitgeom='general', center=None, nclip=3, sigma=(3.0, 'rmse'), clip_accum=False)[source]
Compute linear transformation parameters that “best” (in the sense of minimizing residuals) transform
uvsource position toxysources iteratively using sigma-clipping.More precisely, this functions attempts to find a
2x2matrixFand a shift vectorsthat minimize the residuals between the transformed reference source coordinatesuv(1)\[\mathbf{xy}'_k = \mathbf{F}\cdot(\mathbf{uv}_k-\mathbf{c})+\ \mathbf{s} + \mathbf{c}\]and the “observed” source positions
xy:(2)\[\epsilon^2 = \Sigma_k w_k \|\mathbf{xy}_k-\mathbf{xy}'_k\|^2.\]In the above equations, \(\mathbf{F}\) is a
2x2matrix while \(\mathbf{xy}_k\) and \(\mathbf{uv}_k\) are the position coordinates of thek-th source (row in inputxyanduvarrays).One of the two catalogs (
xyoruv) contains what we refer to as “image” source positions and the other one as “reference” source positions. The meaning assigned toxyanduvparameters are up to the caller of this function.- Parameters:
xy (numpy.ndarray) – A
(N, 2)-shaped array of source positions (one 2-coordinate position per line).uv (numpy.ndarray) – A
(N, 2)-shaped array of source positions (one 2-coordinate position per line). This array must have the same length (shape) as thexyarray.wxy (numpy.ndarray, None, optional) – A 1-dimensional array of weights of the same length (
N) asxyarray indicating how much a given coordinate should be weighted in the fit. If not provided or set toNone, all positions will be contribute equally to the fit ifwuvis also set toNone. SeeNotessection for more details.wuv (numpy.ndarray, None, optional) – A 1-dimensional array of weights of the same length (
N) asxyarray indicating how much a given coordinate should be weighted in the fit. If not provided or set toNone, all positions will be contribute equally to the fit ifwxyis also set toNone. SeeNotessection for more details.fitgeom ({'shift', 'rshift', 'rscale', 'general'}, optional) – The fitting geometry to be used in fitting the matched object lists. This parameter is used in fitting the shifts (offsets), rotations and/or scale changes from the matched object lists. The ‘general’ fit geometry allows for independent scale and rotation for each axis.
center (tuple, list, numpy.ndarray, None, optional) – A list-like container with two
X- andY-positions of the center (origin) of rotations in theuvandxycoordinate frames. If not provided,centeris estimated as a (weighted) mean position in theuvframe.nclip (int, None, optional) – Number (a non-negative integer) of clipping iterations in fit. Clipping will be turned off if
nclipis eitherNoneor 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'- seeNotessection for more details.When
sigmais a single number, it must be a positive number and the default error estimate'rmse'is assumed.This parameter is ignored when
nclipis eitherNoneor 0.clip_accum (bool, optional) – Indicates whether or not to reset the list of “bad” (clipped out) sources after each clipping iteration. When set to
Truethe list only grows with each iteration as “bad” positions never re-enter the pool of available position for the fit. By default the list of “bad” source positions is purged at each iteration. This parameter is ignored whennclipis eitherNoneor 0.
- Returns:
fit –
'shift': Anumpy.ndarraywith two components of the computed shift.'shift_ld': Anumpy.ndarraywith two components of the computed shift of typenumpy.longdouble.'matrix': A2x2numpy.ndarraywith the computed generalized rotation matrix.'matrix_ld': A2x2numpy.ndarraywith the computed generalized rotation matrix of typenumpy.longdouble.'proper_rot': Rotation angle (degree) as if the rotation is proper.'rot': A tuple of(rotx, roty)- the rotation angles with regard to theXandYaxes.'<rot>': Arithmetic mean of the angles of rotation aroundXandYaxes.'scale': A tuple of(sx, sy)- scale change in the direction of theXandYaxes.'<scale>': Geometric mean of scalessxandsy.'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 argumentfitgeom.'center': Center of rotation'center_ld': Center of rotation as anumpy.longdouble.'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': Root-Mean-Square Error'mae': Mean Absolute Error'std': Standard Deviation of the residuals'resids': An array of residuals of the fit. 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.
- Return type:
Notes
Weights
Weights can be provided for both “image” source positions and “reference” source positions. When no weights are given, all positions are weighted equally. When only one set of positions have weights (i.e., either
wxyorwuvis notNone) then weights in (2) are set to be equal to the provided set of weights. When weights for both “image” source positions and “reference” source positions are provided, then the combined weight that is used in (2) is computed as:\[1/w = 1/w_{xy} + 1/w_{uv}.\]Statistics for clipping
Several statistics are available for clipping iterations and all of them are reported in the returned
fitdictionary regardless of the setting insigma:\[\mathrm{RMSE} = \sqrt{\Sigma_k w_k \|\mathbf{r}_k\|^2}\]\[\mathrm{MAE} = \sqrt{\Sigma_k w_k \|\mathbf{r}_k\|}\]\[\mathrm{STD} = \sqrt{\Sigma_k w_k \|\mathbf{r}_k - \ \mathbf{\overline{r}}\|^2}/(1-V_2)\]where \(\mathbf{r}_k=\mathbf{xy}_k-\mathbf{xy}'_k\), \(\Sigma_k w_k = 1\), and \(V_2=\Sigma_k w_k^2\).