Coverage for python/lsst/afw/table/_source.py: 47%

57 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-25 00:01 -0700

1# This file is part of afw. 

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/>. 

21 

22__all__ = [] 

23 

24from lsst.utils import continueClass 

25from lsst.pex.exceptions import LogicError 

26from ._base import Catalog 

27from ._table import QuadrupoleKey, SourceCatalog, SourceColumnView, SourceRecord, SourceTable 

28 

29Catalog.register("Source", SourceCatalog) 

30 

31 

32@continueClass 

33class SourceCatalog: 

34 

35 def getChildren(self, parent, *args): 

36 """Return the subset of self for which the parent field equals the 

37 given value. 

38 

39 In order for this method to return the correct result, it must be 

40 sorted by parent (i.e. self.isSorted(SourceTable.getParentKey()) must 

41 be True). This is naturally the case with SourceCatalogs produced by 

42 the detection and deblending tasks, but it may not be true when 

43 concatenating multiple such catalogs. 

44 

45 Additional Catalogs or sequences whose elements correspond in order to 

46 the records of self (i.e. ``zip(self, *args)`` is valid) will be 

47 subset using the same slice object used on self, and these subsets 

48 will be returned along with the subset of self. 

49 

50 Parameters 

51 ---------- 

52 parent : `int` or `iterable` of `int` 

53 ID(s) of the parent(s) to get children for. 

54 args : `~lsst.afw.table.Catalog` 

55 Additional catalogs to subset for the children to return. 

56 

57 Returns 

58 ------- 

59 children : a single iterable of `~lsst.afw.table.SourceRecord` 

60 Children sources if ``parent`` is of type `int`, or a generator 

61 yielding a `~lsst.afw.table.SourceRecord`s Children sources for 

62 each parent if ``parent`` is an `iterable`. 

63 

64 Raises 

65 ------ 

66 AssertionError 

67 Raised if the catalog is not sorted by the parent key. 

68 

69 Notes 

70 ----- 

71 Each call to this function checks if the catalog is sorted, which is 

72 of O(n) complexity, while fetching the children is of O(log n). To 

73 minimize the computational overhead, it is preferable to prepare an 

74 iterable of parent ids for which the children need to be fetched and 

75 pass the iterable as ``parent``. 

76 """ 

77 if not self.isSorted(SourceTable.getParentKey()): 

78 raise AssertionError( 

79 "The table is not sorted by parent, so cannot getChildren") 

80 

81 def _getChildrenWithoutChecking(parent): 

82 """Return the subset of self for which the parent field equals the 

83 given value. 

84 

85 This function works as desired only if `self` is sorted by the 

86 parent key, but does not check if it is sorted. This function must 

87 be used only after ensuring outside of the function that 

88 self.isSorted(SourceTable.getParentKey() evaluates to True. 

89 

90 Parameter 

91 --------- 

92 parent : `int` 

93 ID of the parent to get children for. 

94 

95 Returns 

96 ------- 

97 children : iterable of `~lsst.afw.table.SourceRecord` 

98 Children sources. 

99 """ 

100 s = self.equal_range(parent, SourceTable.getParentKey()) 

101 if args: 

102 return (self[s],) + tuple(arg[s] for arg in args) 

103 else: 

104 return self[s] 

105 

106 try: 

107 return (_getChildrenWithoutChecking(p) for p in parent) 

108 except TypeError: 

109 return _getChildrenWithoutChecking(parent) 

110 

111 

112@continueClass 

113class SourceRecord: # noqa: F811 

114 

115 def getPsfShape(self): 

116 # Catch the KeyError and raise LogicError from `pex.exception` for 

117 # consistent behavior with similar C++ methods (getIxx, getIyy, etc.) 

118 try: 

119 return QuadrupoleKey(self.schema["slot_PsfShape"]).get(self) 

120 except KeyError: 

121 raise LogicError("Key is not valid (if this is a SourceRecord, make sure slot aliases have been " 

122 "set up)") from None 

123 

124 def _getPsfShapeComponent(self, suffix): 

125 # Catch the KeyError and raise LogicError from `pex.exception` for 

126 # consistent behavior with similar C++ methods (getIxx, getIyy, etc.) 

127 try: 

128 return self["slot_PsfShape_" + suffix] 

129 except KeyError: 

130 raise LogicError("Key is not valid (if this is a SourceRecord, make sure slot aliases have been " 

131 "set up)") from None 

132 

133 def getPsfIxx(self): 

134 return self._getPsfShapeComponent("xx") 

135 

136 def getPsfIyy(self): 

137 return self._getPsfShapeComponent("yy") 

138 

139 def getPsfIxy(self): 

140 return self._getPsfShapeComponent("xy") 

141 

142 def getPsfShapeFlag(self): 

143 return self._getPsfShapeComponent("flag") 

144 

145 

146@continueClass 

147class SourceColumnView: # noqa: F811 

148 

149 def _getPsfShapeComponent(self, suffix): 

150 # Catch the KeyError and raise LogicError from `pex.exception` for 

151 # consistent behavior with similar C++ methods (getIxx, getIyy, etc.) 

152 try: 

153 return self["slot_PsfShape_" + suffix] 

154 except KeyError: 

155 raise LogicError("Key is not valid (if this is a SourceCatalog, make sure slot aliases have been " 

156 "set up)") from None 

157 

158 def getPsfIxx(self): 

159 return self._getPsfShapeComponent("xx") 

160 

161 def getPsfIyy(self): 

162 return self._getPsfShapeComponent("yy") 

163 

164 def getPsfIxy(self): 

165 return self._getPsfShapeComponent("xy") 

166 

167 

168@continueClass 

169class SourceTable: # noqa: F811 

170 

171 def definePsfShape(self, name): 

172 self.schema.getAliasMap().set("slot_PsfShape", name)