Coverage for python/lsst/analysis/tools/actions/vector/calcShapeSize.py: 45%
25 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-28 03:17 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-28 03:17 -0800
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__ = ("CalcShapeSize",)
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
32class CalcShapeSize(VectorAction):
33 """Calculate a size: (Ixx*Iyy - Ixy**2)**0.25 OR (0.5*(Ixx + Iyy))**0.5
34 The square of size measure is typically expressed either as the arithmetic
35 mean of the eigenvalues of the moment matrix (trace radius) or as the
36 geometric mean of the eigenvalues (determinant radius), which can be
37 specified using the ``sizeType`` parameter. Both of these measures give the
38 `sigma^2` parameter for a 2D Gaussian.
39 Since lensing preserves surface brightness, the determinant radius relates
40 the magnification cleanly as it is derived from the area of isophotes, but
41 have a slightly higher chance of being NaNs for noisy moment estimates.
43 Note
44 ----
45 This is a size measurement used for doing QA on the ellipticity
46 of the sources.
47 """
49 colXx = Field[str](
50 doc="The column name to get the xx shape component from.",
51 default="{band}_ixx",
52 )
54 colYy = Field[str](
55 doc="The column name to get the yy shape component from.",
56 default="{band}_iyy",
57 )
59 colXy = Field[str](
60 doc="The column name to get the xy shape component from.",
61 default="{band}_ixy",
62 optional=True,
63 )
65 sizeType = ChoiceField[str](
66 doc="The type of size to calculate",
67 default="determinant",
68 allowed={
69 "trace": "trace radius",
70 "determinant": "determinant radius",
71 },
72 )
74 def getInputSchema(self) -> KeyedDataSchema:
75 if self.sizeType == "trace":
76 return (
77 (self.colXx, Vector),
78 (self.colYy, Vector),
79 )
80 else:
81 return (
82 (self.colXx, Vector),
83 (self.colYy, Vector),
84 (self.colXy, Vector),
85 ) # type: ignore
87 def __call__(self, data: KeyedData, **kwargs) -> Vector:
88 if self.sizeType == "trace":
89 size = np.power(
90 0.5
91 * (
92 data[self.colXx.format(**kwargs)] + data[self.colYy.format(**kwargs)] # type: ignore
93 ), # type: ignore
94 0.5,
95 )
96 else:
97 size = np.power(
98 data[self.colXx.format(**kwargs)] * data[self.colYy.format(**kwargs)] # type: ignore
99 - data[self.colXy.format(**kwargs)] ** 2, # type: ignore
100 0.25,
101 )
103 return size
105 def validate(self):
106 super().validate()
107 if self.sizeType == "determinant" and self.colXy is None:
108 msg = "colXy is required for determinant-type size"
109 raise FieldValidationError(self.__class__.colXy, self, msg)