LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
_baseColumnView.py
Go to the documentation of this file.
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
Tag types used to declare specialized field types.
Definition misc.h:31