LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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