23 Special Python syntactic sugar for Catalogs and Records.
25 This module is imported by tableLib.py, and should not need to be imported by any other module.
26 I've moved the code out of the .i file here to avoid recompiling when only pure-Python code is
29 from builtins
import zip
38 Extract a dictionary of {<name>: <schema-item>} in which the field names
39 match the given shell-style glob pattern(s).
41 Any number of glob patterns may be passed; the result will be the union of all
42 the result of each glob considered separately.
44 Additional optional arguments may be passed as keywords:
46 regex ------ A regular expression to be used in addition to any glob patterns passed
47 as positional arguments. Note that this will be compared with re.match,
50 sub -------- A replacement string template (see re.MatchObject.expand) used to set the
51 dictionary keys of any fields matched by regex. The field name in the
52 SchemaItem is not modified.
54 ordered----- If True, a collections.OrderedDict will be returned instead of a standard
55 dict, with the order corresponding to the definition order of the Schema.
58 if kwds.pop(
"ordered",
False):
59 d = collections.OrderedDict()
62 regex = kwds.pop(
"regex",
None)
63 sub = kwds.pop(
"sub",
None)
64 if sub
is not None and regex
is None:
65 raise ValueError(
"'sub' keyword argument to extract is invalid without 'regex' argument")
67 raise ValueError(
"Unrecognized keyword arguments for extract: %s" %
", ".join(kwds.keys()))
69 trueName = item.field.getName()
71 for alias, target
in self.getAliasMap().items():
72 if trueName.startswith(target):
73 names.append(trueName.replace(target, alias, 1))
76 m = re.match(regex, name)
82 for pattern
in patterns:
83 if fnmatch.fnmatchcase(name, pattern):
90 Extract a dictionary of {<name>: <field-value>} in which the field names
91 match the given shell-style glob pattern(s).
93 Any number of glob patterns may be passed; the result will be the union of all
94 the result of each glob considered separately.
96 Additional optional arguments may be passed as keywords:
98 items ------ The result of a call to self.schema.extract(); this will be used instead
99 of doing any new matching, and allows the pattern matching to be reused
100 to extract values from multiple records. This keyword is incompatible
101 with any position arguments and the regex, sub, and ordered keyword
104 split ------ If True, fields with named subfields (e.g. points) will be split into
105 separate items in the dict; instead of {"point": lsst.afw.geom.Point2I(2,3)},
106 for instance, you'd get {"point.x": 2, "point.y": 3}.
109 regex ------ A regular expression to be used in addition to any glob patterns passed
110 as positional arguments. Note that this will be compared with re.match,
113 sub -------- A replacement string (see re.MatchObject.expand) used to set the
114 dictionary keys of any fields matched by regex.
116 ordered----- If True, a collections.OrderedDict will be returned instead of a standard
117 dict, with the order corresponding to the definition order of the Schema.
121 d = kwds.pop(
"items",
None)
122 split = kwds.pop(
"split",
False)
124 d = self.schema.extract(*patterns, **kwds).copy()
126 raise ValueError(
"Unrecognized keyword arguments for extract: %s" %
", ".join(kwds.keys()))
127 for name, schemaItem
in list(d.items()):
129 if split
and key.HAS_NAMED_SUBFIELDS:
130 for subname, subkey
in zip(key.subfields, key.subkeys):
131 d[
"%s.%s" % (name, subname)] = self.get(subkey)
134 d[name] = self.get(schemaItem.key)
139 Extract a dictionary of {<name>: <column-array>} in which the field names
140 match the given shell-style glob pattern(s).
142 Any number of glob patterns may be passed; the result will be the union of all
143 the result of each glob considered separately.
145 Note that extract("*", copy=True) provides an easy way to transform a row-major
146 ColumnView into a possibly more efficient set of contiguous NumPy arrays.
148 This routines unpacks Flag columns into full boolean arrays and covariances into dense
149 (i.e. non-triangular packed) arrays with dimension (N,M,M), where N is the number of
150 records and M is the dimension of the covariance matrix. Fields with named subfields
151 (e.g. points) are always split into separate dictionary items, as is done in
152 BaseRecord.extract(..., split=True). String fields are silently ignored.
154 Additional optional arguments may be passed as keywords:
156 items ------ The result of a call to self.schema.extract(); this will be used instead
157 of doing any new matching, and allows the pattern matching to be reused
158 to extract values from multiple records. This keyword is incompatible
159 with any position arguments and the regex, sub, and ordered keyword
162 where ------ Any expression that can be passed as indices to a NumPy array, including
163 slices, boolean arrays, and index arrays, that will be used to index
164 each column array. This is applied before arrays are copied when
165 copy is True, so if the indexing results in an implicit copy no
166 unnecessary second copy is performed.
168 copy ------- If True, the returned arrays will be contiguous copies rather than strided
169 views into the catalog. This ensures that the lifetime of the catalog is
170 not tied to the lifetime of a particular catalog, and it also may improve
171 the performance if the array is used repeatedly.
174 regex ------ A regular expression to be used in addition to any glob patterns passed
175 as positional arguments. Note that this will be compared with re.match,
178 sub -------- A replacement string (see re.MatchObject.expand) used to set the
179 dictionary keys of any fields matched by regex.
181 ordered----- If True, a collections.OrderedDict will be returned instead of a standard
182 dict, with the order corresponding to the definition order of the Schema.
186 copy = kwds.pop(
"copy",
False)
187 where = kwds.pop(
"where",
None)
188 d = kwds.pop(
"items",
None)
190 d = self.schema.extract(*patterns, **kwds).copy()
192 raise ValueError(
"Unrecognized keyword arguments for extract: %s" %
", ".list(kwds.keys()))
194 if where
is not None:
197 a = numpy.ascontiguousarray(a)
199 for name, schemaItem
in list(d.items()):
201 if key.getTypeString() ==
"String":
204 d[name] = processArray(self.get(schemaItem.key))
209 Return an astropy.table.Table (or subclass thereof) view into this catalog.
211 @param[in] cls Table subclass to use; None implies astropy.table.Table itself.
212 Use astropy.table.QTable to get Quantity columns.
214 @param[in] copy Whether to copy data from the LSST catalog to the astropy table.
215 Not copying is usually faster, but can keep memory from being
216 freed if columns are later removed from the Astropy view.
218 @param[in] unviewable One of the following options, indicating how to handle field types
219 (string and Flag) for which views cannot be constructed:
220 - 'copy' (default): copy only the unviewable fields.
221 - 'raise': raise ValueError if unviewable fields are present.
222 - 'skip': do not include unviewable fields in the Astropy Table.
223 This option is ignored if copy=True.
227 cls = astropy.table.Table
228 if unviewable
not in (
"copy",
"raise",
"skip"):
229 raise ValueError(
"'unviewable' must be one of 'copy', 'raise', or 'skip'")
230 ps = self.getMetadata()
231 meta = ps.toOrderedDict()
if ps
is not None else None
233 items = self.schema.extract(
"*", ordered=
True)
234 for name, item
in items.items():
236 unit = item.field.getUnits()
or None
237 if key.getTypeString() ==
"String":
239 if unviewable ==
"raise":
240 raise ValueError(
"Cannot extract string unless copy=True or unviewable='copy' or 'skip'.")
241 elif unviewable ==
"skip":
243 data = numpy.zeros(len(self), dtype=numpy.dtype((str, key.getSize())))
244 for i, record
in enumerate(self):
245 data[i] = record.get(key)
246 elif key.getTypeString() ==
"Flag":
248 if unviewable ==
"raise":
250 "Cannot extract packed bit columns unless copy=True or unviewable='copy' or 'skip'."
252 elif unviewable ==
"skip":
254 data = self.columns.get_bool_array(key)
255 elif key.getTypeString() ==
"Angle":
256 data = self.columns.get(key)
261 data = self.columns.get(key)
265 astropy.table.Column(
267 name=item.field.getName(),
269 description=item.field.getDoc()
272 return cls(columns, meta=meta, copy=
False)
def BaseCatalog_asAstropy
Return an astropy.table.Table (or subclass thereof) view into this catalog.
def BaseColumnView_extract