21 import collections.abc
26 from .schemaMapper
import SchemaMapper
27 from .aggregates
import CoordKey
28 from .source
import SourceRecord
32 """Initialize a multi-catalog match. 36 schema : `lsst.afw.table.Schema` 37 Schema shared by all catalogs to be included in the match. 39 Set of name: type for all data ID keys (e.g. {"visit":int, 41 coordField : `str`, optional 42 Prefix for _ra and _dec fields that contain the 43 coordinates to use for the match. 44 idField : `str`, optional 45 Name of the field in schema that contains unique object 47 radius : `lsst.geom.Angle`, optional 48 Maximum separation for a match. Defaults to 0.5 arcseconds. 49 RecordClass : `lsst.afw.table.BaseRecord` 50 Type of record to expect in catalogs to be matched. 53 def __init__(self, schema, dataIdFormat, coordField="coord", idField="id", radius=None,
54 RecordClass=SourceRecord):
56 radius = 0.5*lsst.geom.arcseconds
58 raise ValueError(
"'radius' argument must be an Angle")
61 self.
mapper.addMinimalSchema(schema,
True)
63 self.
idKey = schema.find(idField).key
65 outSchema = self.
mapper.editOutputSchema()
66 outSchema.setAliasMap(self.
mapper.getInputSchema().getAliasMap())
68 "object", type=numpy.int64, doc=
"Unique ID for joined sources")
69 for name, dataType
in dataIdFormat.items():
71 name, type=dataType, doc=
"'%s' data ID component")
84 self.
table = RecordClass.Table.make(self.
mapper.getOutputSchema())
89 """Create a new result record from the given input record, using the 90 given data ID and object ID to fill in additional columns. 94 inputRecord : `lsst.afw.table.source.sourceRecord` 95 Record to use as the reference for the new result. 96 dataId : `DataId` or `dict` 97 Data id describing the data. 99 Object id of the object to be added. 103 outputRecord : `lsst.afw.table.source.sourceRecord` 104 Newly generated record. 106 outputRecord = self.
table.copyRecord(inputRecord, self.
mapper)
108 outputRecord.set(key, dataId[name])
112 def add(self, catalog, dataId):
113 """Add a new catalog to the match, corresponding to the given data ID. 114 The new catalog is appended to the `self.result` and 115 `self.reference` catalogs. 119 catalog : `lsst.afw.table.base.Catalog` 120 Catalog to be added to the match result. 121 dataId : `DataId` or `dict` 122 Data id for the catalog to be added. 126 for record
in catalog:
132 catalog.sort(self.
idKey)
134 unmatchedIds = {record.get(self.
idKey)
for record
in catalog}
138 matchedRefIds =
set()
139 matchedCatIds =
set()
140 for refRecord, newRecord, distance
in matches:
142 if objId
in matchedRefIds:
146 matchedRefIds.add(objId)
147 if newRecord.get(self.
idKey)
in matchedCatIds:
152 matchedCatIds.add(newRecord.get(self.
idKey))
153 unmatchedIds.discard(newRecord.get(self.
idKey))
156 newToObj.setdefault(newRecord.get(self.
idKey),
set()).
add(objId)
161 for objId
in unmatchedIds:
162 newRecord = catalog.find(objId, self.
idKey)
165 self.
result.append(resultRecord)
169 """Return the final match catalog, after sorting it by object, copying 170 it to ensure contiguousness, and optionally removing ambiguous 173 After calling finish(), the in-progress state of the matcher 174 is returned to the state it was just after construction, with 175 the exception of the object ID counter (which is not reset). 179 removeAmbiguous : `bool`, optional 180 Should ambiguous matches be removed from the match 181 catalog? Defaults to True. 185 result : `lsst.afw.table.base.Catalog` 186 Final match catalog, sorted by object. 190 for record
in self.
result:
192 result.append(record)
196 result = result.copy(deep=
True)
204 """A mapping (i.e. dict-like object) that provides convenient 205 operations on the concatenated catalogs returned by a MultiMatch 208 A GroupView provides access to a catalog of grouped objects, in 209 which the grouping is indicated by a field for which all records 210 in a group have the same value. Once constructed, it allows 211 operations similar to those supported by SQL "GROUP BY", such as 212 filtering and aggregate calculation. 216 schema : `lsst.afw.table.Schema` 217 Catalog schema to use for the grouped object catalog. 219 List of identifying keys for the groups in the catalog. 221 List of catalog subsets associated with each key in ids. 225 def build(cls, catalog, groupField="object"):
226 """Construct a GroupView from a concatenated catalog. 230 catalog : `lsst.afw.table.base.Catalog` 231 Input catalog, containing records grouped by a field in 232 which all records in the same group have the same value. 233 Must be sorted by the group field. 234 groupField : `str`, optional 235 Name or Key for the field that indicates groups. Defaults 240 groupCatalog : `lsst.afw.table.multiMatch.GroupView` 241 Constructed GroupView from the input concatenated catalog. 243 groupKey = catalog.schema.find(groupField).key
244 ids, indices = numpy.unique(catalog.get(groupKey), return_index=
True)
245 groups = numpy.zeros(len(ids), dtype=object)
246 ends =
list(indices[1:]) + [len(catalog)]
247 for n, (i1, i2)
in enumerate(zip(indices, ends)):
249 groups[n] = catalog[int(i1):int(i2)]
250 assert (groups[n].get(groupKey) == ids[n]).all()
251 return cls(catalog.schema, ids, groups)
266 index = numpy.searchsorted(self.
ids, key)
267 if self.
ids[index] != key:
268 raise KeyError(
"Group with ID {0} not found".format(key))
272 """Return a new GroupView that contains only groups for which the 273 given predicate function returns True. 275 The predicate function is called once for each group, and 276 passed a single argument: the subset catalog for that group. 281 Function to identify which groups should be included in 286 outGroupView : `lsst.afw.table.multiMatch.GroupView` 287 Subset GroupView containing only groups that match the 290 mask = numpy.zeros(len(self), dtype=bool)
291 for i
in range(len(self)):
292 mask[i] = predicate(self.
groups[i])
296 """Run an aggregate function on each group, returning an array with 297 one element for each group. 302 Callable object that computes the aggregate value. If 303 `field` is None, called with the entire subset catalog as an 304 argument. If `field` is not None, called with an array view 306 field : `str`, optional 307 A string name or Key object that indicates a single field the aggregate 310 Data type of the output array. 314 result : Array of `dtype` 315 Aggregated values for each group. 317 result = numpy.zeros(len(self), dtype=dtype)
318 if field
is not None:
319 key = self.
schema.find(field).key
325 for i
in range(len(self)):
326 result[i] = f(self.
groups[i])
329 def apply(self, function, field=None, dtype=float):
330 """Run a non-aggregate function on each group, returning an array with 331 one element for each record. 336 Callable object that computes the aggregate value. If field is None, 337 called with the entire subset catalog as an argument. If field is not 338 None, called with an array view into that field. 340 A string name or Key object that indicates a single field the aggregate 343 Data type for the output array. 347 result : `numpy.array` of `dtype` 348 Result of the function calculated on an element-by-element basis. 350 result = numpy.zeros(self.
count, dtype=dtype)
351 if field
is not None:
352 key = self.
schema.find(field).key
359 for i
in range(len(self)):
360 next = last + len(self.
groups[i])
361 result[last:next] = f(self.
groups[i])
std::vector< SchemaItem< Flag > > * items
def __init__(self, schema, dataIdFormat, coordField="coord", idField="id", radius=None, RecordClass=SourceRecord)
def where(self, predicate)
def build(cls, catalog, groupField="object")
def apply(self, function, field=None, dtype=float)
template SourceMatchVector matchRaDec(SourceCatalog const &, lsst::geom::Angle, MatchControl const &)
def makeRecord(self, inputRecord, dataId, objId)
def __init__(self, schema, ids, groups)
def finish(self, removeAmbiguous=True)
def __getitem__(self, key)
def aggregate(self, function, field=None, dtype=float)
daf::base::PropertyList * list
def add(self, catalog, dataId)
daf::base::PropertySet * set