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

25 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-03 10:03 +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 

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 sizeType = ChoiceField[str]( 

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

72 default="determinant", 

73 optional=False, 

74 allowed={ 

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

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

77 }, 

78 ) 

79 

80 def getInputSchema(self) -> KeyedDataSchema: 

81 if self.sizeType == "trace": 

82 return ( 

83 (self.colXx, Vector), 

84 (self.colYy, Vector), 

85 ) 

86 else: 

87 return ( 

88 (self.colXx, Vector), 

89 (self.colYy, Vector), 

90 (self.colXy, Vector), 

91 ) # type: ignore 

92 

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

94 if self.sizeType == "trace": 

95 size = np.sqrt( 

96 0.5 * (data[self.colXx.format(**kwargs)] + data[self.colYy.format(**kwargs)]) # type: ignore 

97 ) 

98 else: 

99 size = np.power( 

100 data[self.colXx.format(**kwargs)] * data[self.colYy.format(**kwargs)] # type: ignore 

101 - data[self.colXy.format(**kwargs)] ** 2, # type: ignore 

102 0.25, 

103 ) 

104 

105 return size 

106 

107 def validate(self): 

108 super().validate() 

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

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

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