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

25 statements  

« prev     ^ index     » next       coverage.py v7.2.6, created at 2023-05-24 02:36 -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__ = ("CalcShapeSize",) 

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 CalcShapeSize(VectorAction): 

33 r"""Calculate a size: :math:`(I_{xx}I_{yy}-I_{xy}^2)^{\frac{1}{4}}` 

34 (determinant radius) or :math:`\sqrt{(I_{xx}+I_{yy})/2}` 

35 (trace radius). 

36 

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

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

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

40 specified using the `sizeType` parameter. Both of these measures give the 

41 :math:`\sigma^2` parameter for a 2D Gaussian. 

42 

43 Since lensing preserves surface brightness, the determinant radius relates 

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

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

46 

47 Notes 

48 ----- 

49 This is a size measurement used for doing QA on the ellipticity 

50 of the sources. 

51 """ 

52 

53 colXx = Field[str]( 

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

55 default="{band}_ixx", 

56 ) 

57 

58 colYy = Field[str]( 

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

60 default="{band}_iyy", 

61 ) 

62 

63 colXy = Field[str]( 

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

65 default="{band}_ixy", 

66 optional=True, 

67 ) 

68 

69 sizeType = ChoiceField[str]( 

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

71 default="determinant", 

72 optional=False, 

73 allowed={ 

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

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

76 }, 

77 ) 

78 

79 def getInputSchema(self) -> KeyedDataSchema: 

80 if self.sizeType == "trace": 

81 return ( 

82 (self.colXx, Vector), 

83 (self.colYy, Vector), 

84 ) 

85 else: 

86 return ( 

87 (self.colXx, Vector), 

88 (self.colYy, Vector), 

89 (self.colXy, Vector), 

90 ) # type: ignore 

91 

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

93 if self.sizeType == "trace": 

94 size = np.power( 

95 0.5 

96 * ( 

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

98 ), # type: ignore 

99 0.5, 

100 ) 

101 else: 

102 size = np.power( 

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

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

105 0.25, 

106 ) 

107 

108 return size 

109 

110 def validate(self): 

111 super().validate() 

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

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

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