Coverage for python/lsst/afw/table/_baseColumnView.py: 25%

50 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-01 03:31 -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__ = [] # importing this module adds methods to BaseColumnView 

23 

24import numpy as np 

25 

26from deprecated.sphinx import deprecated 

27 

28from lsst.utils import continueClass 

29from ._table import KeyFlag, _BaseColumnViewBase 

30 

31# We can't call this "BaseColumnView" because that's the typedef for 

32# "ColumnViewT<BaseRecord>". This is just a mostly-invisible implementation 

33# base class, so we use the same naming convention we use for those. 

34 

35 

36@continueClass 

37class _BaseColumnViewBase: # noqa: F811 

38 

39 def getBits(self, keys=None): 

40 """Get the bits associated with the specified keys. 

41 

42 Parameters 

43 ---------- 

44 key : `str` 

45 Key to retrieve. Unlike the C++ version, each key may be a 

46 field name or a key, and if keys is `None` then all bits 

47 are returned. 

48 

49 Returns 

50 ------- 

51 bits : `int` 

52 Integer array of the requested bitmask. 

53 """ 

54 if keys is None: 

55 return self.getAllBits() 

56 arg = [] 

57 for k in keys: 

58 if isinstance(k, str): 

59 arg.append(self.schema.find(k).key) 

60 else: 

61 arg.append(k) 

62 return self._getBits(arg) 

63 

64 def __getitem__(self, key): 

65 """Get a column view; key may be a key object or the name of a field. 

66 """ 

67 if isinstance(key, str): 

68 keyobj = self.schema.find(key).key 

69 else: 

70 keyobj = key 

71 return self._basicget(keyobj) 

72 

73 get = __getitem__ 

74 

75 def __setitem__(self, key, value): 

76 """Set a full column to an array or scalar; key may be a key object or 

77 the name of a field. 

78 """ 

79 self.get(key)[:] = value 

80 

81 set = __setitem__ 

82 

83 # TODO: remove on DM-32980. 

84 @deprecated( 

85 reason=( 

86 "Catalog.__getitem__ now provides better support for " 

87 "accessing Flag/bool columns. Will be removed after v26." 

88 ), 

89 version="v26", 

90 category=FutureWarning, 

91 ) 

92 def get_bool_array(self, key): 

93 """Get the value of a flag column as a boolean array; key must be a 

94 key object or the name of a field. 

95 

96 Parameters 

97 ---------- 

98 key : `lsst.afw.table.KeyFlag` 

99 Flag column to search for. 

100 

101 Returns 

102 ------- 

103 value : `list` of `bool` 

104 Array of booleans corresponding to the flag. 

105 

106 Raises 

107 ------ 

108 TypeError 

109 Raised if the key is not a KeyFlag. 

110 """ 

111 if isinstance(key, KeyFlag): 

112 return self[key] 

113 raise TypeError("key={} not an lsst.afw.table.KeyFlag".format(key)) 

114 

115 def extract(self, *patterns, **kwds): 

116 """Extract a dictionary of {<name>: <column-array>} in which the field 

117 names match the given shell-style glob pattern(s). 

118 

119 Any number of glob patterns may be passed (including none); the result 

120 will be the union of all the result of each glob considered separately. 

121 

122 Note that extract("*", copy=True) provides an easy way to transform a 

123 row-major ColumnView into a possibly more efficient set of contiguous 

124 NumPy arrays. 

125 

126 String fields are silently ignored. Support for `Flag` columns is 

127 deprecated; at present they are copied into full boolean arrays, but 

128 after v26 they will be silently ignored as well. 

129 

130 Parameters 

131 ---------- 

132 patterns : Array of `str` 

133 List of glob patterns to use to select field names. 

134 kwds : `dict` 

135 Dictionary of additional keyword arguments. May contain: 

136 

137 ``items`` : `list` 

138 The result of a call to self.schema.extract(); this will be 

139 used instead of doing any new matching, and allows the pattern 

140 matching to be reused to extract values from multiple records. 

141 This keyword is incompatible with any position arguments and 

142 the regex, sub, and ordered keyword arguments. 

143 ``where`` : array index expression 

144 Any expression that can be passed as indices to a NumPy array, 

145 including slices, boolean arrays, and index arrays, that will 

146 be used to index each column array. This is applied before 

147 arrays are copied when copy is True, so if the indexing results 

148 in an implicit copy no unnecessary second copy is performed. 

149 ``copy`` : `bool` 

150 If True, the returned arrays will be contiguous copies rather 

151 than strided views into the catalog. This ensures that the 

152 lifetime of the catalog is not tied to the lifetime of a 

153 particular catalog, and it also may improve the performance if 

154 the array is used repeatedly. Default is False. 

155 ``regex`` : `str` or `re` pattern 

156 A regular expression to be used in addition to any glob 

157 patterns passed as positional arguments. Note that this will 

158 be compared with re.match, not re.search. 

159 ``sub`` : `str` 

160 A replacement string (see re.MatchObject.expand) used to set 

161 the dictionary keys of any fields matched by regex. 

162 ``ordered`` : `bool` 

163 If True, a collections.OrderedDict will be returned instead of 

164 a standard dict, with the order corresponding to the definition 

165 order of the Schema. Default is False. 

166 

167 Returns 

168 ------- 

169 d : `dict` 

170 Dictionary of extracted name-column array sets. 

171 

172 Raises 

173 ------ 

174 ValueError 

175 Raised if a list of ``items`` is supplied with additional keywords. 

176 """ 

177 # TODO: on DM-32980, adjust docs above to reflect reference to 

178 # "after v26." 

179 copy = kwds.pop("copy", False) 

180 where = kwds.pop("where", None) 

181 d = kwds.pop("items", None) 

182 # If ``items`` is given as a kwd, an extraction has already been 

183 # performed and there shouldn't be any additional keywords. Otherwise 

184 # call schema.extract to load the dictionary. 

185 if d is None: 

186 d = self.schema.extract(*patterns, **kwds).copy() 

187 elif kwds: 

188 raise ValueError( 

189 "kwd 'items' was specified, which is not compatible with additional keywords") 

190 

191 def processArray(a): 

192 if where is not None: 

193 a = a[where] 

194 if copy: 

195 a = np.ascontiguousarray(a) 

196 return a 

197 

198 # must use list because we might be adding/deleting elements 

199 for name, schemaItem in list(d.items()): 

200 key = schemaItem.key 

201 if key.getTypeString() == "String": 

202 del d[name] 

203 else: 

204 d[name] = processArray(self.get(schemaItem.key)) 

205 return d