Coverage for python/lsst/analysis/tools/actions/vector/calcMomentSize.py: 33%

36 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-02 04:48 -0700

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 

22 

23__all__ = ("CalcMomentSize",) 

24 

25import numpy as np 

26from lsst.pex.config import Field, FieldValidationError 

27from lsst.pex.config.choiceField import ChoiceField 

28 

29from ...interfaces import KeyedData, KeyedDataSchema, Vector, VectorAction 

30 

31 

32class CalcMomentSize(VectorAction): 

33 r"""Calculate a size based on 2D moments. 

34 

35 Given a 2x2 matrix of moments (i.e. moment of inertia), two sizes can be 

36 defined as follows: 

37 

38 Determinant radius: :math:`(I_{xx}I_{yy}-I_{xy}^2)^{\frac{1}{4}}` 

39 Trace radius: :math:`\sqrt{(I_{xx}+I_{yy})/2}` 

40 

41 The square of size measure is typically expressed either as the arithmetic 

42 mean of the eigenvalues of the moment matrix (trace radius) or as the 

43 geometric mean of the eigenvalues (determinant radius), which can be 

44 specified using the `sizeType` parameter. Both of these measures 

45 correspond to the :math:`\sigma^2` parameter for a 2D Gaussian. 

46 

47 Notes 

48 ----- 

49 Since lensing preserves surface brightness, the determinant radius relates 

50 the magnification cleanly as it is derived from the area of isophotes, but 

51 have a slightly higher chance of being NaNs for noisy moment estimates. 

52 """ 

53 

54 colXx = Field[str]( 

55 doc="The column name to get the xx shape component from.", 

56 default="{band}_ixx", 

57 ) 

58 

59 colYy = Field[str]( 

60 doc="The column name to get the yy shape component from.", 

61 default="{band}_iyy", 

62 ) 

63 

64 colXy = Field[str]( 

65 doc="The column name to get the xy shape component from.", 

66 default="{band}_ixy", 

67 optional=True, 

68 ) 

69 

70 is_covariance = Field[bool]( 

71 doc="Whether the fields are for a covariance matrix. If False, the XX/YY/XY terms are instead" 

72 " assumed to map to sigma_x/sigma_y/rho.", 

73 default=True, 

74 ) 

75 

76 sizeType = ChoiceField[str]( 

77 doc="The type of size to calculate", 

78 default="determinant", 

79 optional=False, 

80 allowed={ 

81 "trace": r"Trace radius :math:`\sqrt{(I_{xx}+I_{yy})/2}`", 

82 "determinant": r"Determinant radius :math:`(I_{xx}I_{yy}-I_{xy}^2)^{\frac{1}{4}}`", 

83 }, 

84 ) 

85 

86 def getInputSchema(self) -> KeyedDataSchema: 

87 if self.sizeType == "trace": 

88 return ( 

89 (self.colXx, Vector), 

90 (self.colYy, Vector), 

91 ) 

92 else: 

93 return ( 

94 (self.colXx, Vector), 

95 (self.colYy, Vector), 

96 (self.colXy, Vector), 

97 ) # type: ignore 

98 

99 def __call__(self, data: KeyedData, **kwargs) -> Vector: 

100 xx = data[self.colXx.format(**kwargs)] 

101 yy = data[self.colYy.format(**kwargs)] 

102 if self.sizeType == "trace": 

103 if not self.is_covariance: 

104 xx *= xx 

105 yy *= yy 

106 size = np.sqrt(0.5 * (xx + yy)) # type: ignore 

107 else: 

108 xy_sq = data[self.colXy.format(**kwargs)] ** 2 

109 if not self.is_covariance: 

110 # cov = rho * sigma_x * sigma_y 

111 xy_sq *= xx * yy 

112 xx *= xx 

113 yy *= yy 

114 size = np.power(xx * yy - xy_sq, 0.25) # type: ignore 

115 

116 return size 

117 

118 def validate(self): 

119 super().validate() 

120 if self.sizeType == "determinant" and self.colXy is None: 

121 msg = "colXy is required for determinant-type size" 

122 raise FieldValidationError(self.__class__.colXy, self, msg)