Coverage for python / lsst / analysis / tools / actions / vector / calcMomentSize.py: 34%
37 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-22 09:09 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-22 09:09 +0000
1# This file is part of analysis_tools.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21from __future__ import annotations
23__all__ = ("CalcMomentSize",)
25import numpy as np
26from lsst.pex.config import Field, FieldValidationError
27from lsst.pex.config.choiceField import ChoiceField
29from ...interfaces import KeyedData, KeyedDataSchema, Vector, VectorAction
30from ...math import power, sqrt
33class CalcMomentSize(VectorAction):
34 r"""Calculate a size based on 2D moments.
36 Given a 2x2 matrix of moments (i.e. moment of inertia), two sizes can be
37 defined as follows:
39 Determinant radius: :math:`(I_{xx}I_{yy}-I_{xy}^2)^{\frac{1}{4}}`
40 Trace radius: :math:`\sqrt{(I_{xx}+I_{yy})/2}`
42 The square of size measure is typically expressed either as the arithmetic
43 mean of the eigenvalues of the moment matrix (trace radius) or as the
44 geometric mean of the eigenvalues (determinant radius), which can be
45 specified using the `sizeType` parameter. Both of these measures
46 correspond to the :math:`\sigma^2` parameter for a 2D Gaussian.
48 Notes
49 -----
50 Since lensing preserves surface brightness, the determinant radius relates
51 the magnification cleanly as it is derived from the area of isophotes, but
52 have a slightly higher chance of being NaNs for noisy moment estimates.
53 """
55 colXx = Field[str](
56 doc="The column name to get the xx shape component from.",
57 default="{band}_ixx",
58 )
60 colYy = Field[str](
61 doc="The column name to get the yy shape component from.",
62 default="{band}_iyy",
63 )
65 colXy = Field[str](
66 doc="The column name to get the xy shape component from.",
67 default="{band}_ixy",
68 optional=True,
69 )
71 is_covariance = Field[bool](
72 doc="Whether the fields are for a covariance matrix. If False, the XX/YY/XY terms are instead"
73 " assumed to map to sigma_x/sigma_y/rho.",
74 default=True,
75 )
77 sizeType = ChoiceField[str](
78 doc="The type of size to calculate",
79 default="determinant",
80 optional=False,
81 allowed={
82 "trace": r"Trace radius :math:`\sqrt{(I_{xx}+I_{yy})/2}`",
83 "determinant": r"Determinant radius :math:`(I_{xx}I_{yy}-I_{xy}^2)^{\frac{1}{4}}`",
84 },
85 )
87 def getInputSchema(self) -> KeyedDataSchema:
88 if self.sizeType == "trace":
89 return (
90 (self.colXx, Vector),
91 (self.colYy, Vector),
92 )
93 else:
94 return (
95 (self.colXx, Vector),
96 (self.colYy, Vector),
97 (self.colXy, Vector),
98 ) # type: ignore
100 def __call__(self, data: KeyedData, **kwargs) -> Vector:
101 xx = np.array(data[self.colXx.format(**kwargs)])
102 yy = np.array(data[self.colYy.format(**kwargs)])
103 if self.sizeType == "trace":
104 if not self.is_covariance:
105 xx *= xx
106 yy *= yy
107 size = sqrt(0.5 * (xx + yy))
108 else:
109 xy_sq = np.array(data[self.colXy.format(**kwargs)]) ** 2
110 if not self.is_covariance:
111 xx *= xx
112 yy *= yy
113 # cov = rho * sigma_x * sigma_y
114 # this term needs to be cov^2
115 xy_sq *= xx * yy
116 size = power(xx * yy - xy_sq, 0.25)
118 return size
120 def validate(self):
121 super().validate()
122 if self.sizeType == "determinant" and self.colXy is None:
123 msg = "colXy is required for determinant-type size"
124 raise FieldValidationError(self.__class__.colXy, self, msg)