1 from __future__
import absolute_import, division, print_function
5 from builtins
import zip
6 from builtins
import range
7 from builtins
import object
11 from .schemaMapper
import SchemaMapper
12 from .aggregates
import CoordKey
13 from .source
import SourceRecord
18 def __init__(self, schema, dataIdFormat, coordField="coord", idField="id", radius=None,
19 RecordClass=SourceRecord):
20 """Initialize a multi-catalog match. 23 schema -------- schema shared by all catalogs to be included in the match. 24 dataIdFormat -- dict of name: type for all data ID keys (e.g. {"visit":int, "ccd":int}). 25 coordField ---- prefix for _ra and _dec fields that contain the coordinates to use for the match. 26 idField ------- name of the field in schema that contains unique object IDs. 27 radius -------- lsst.afw.geom.Angle; maximum separation for a match. Defaults to 0.5 arcseconds. 28 RecordClass --- type of record (a subclass of lsst.afw.table.BaseRecord) to expect in catalogs 32 radius = 0.5*lsst.afw.geom.arcseconds
34 raise ValueError(
"'radius' argument must be an Angle")
37 self.
mapper.addMinimalSchema(schema,
True)
39 self.
idKey = schema.find(idField).key
41 outSchema = self.
mapper.editOutputSchema()
43 "object", type=numpy.int64, doc=
"Unique ID for joined sources")
44 for name, dataType
in dataIdFormat.items():
46 name, type=dataType, doc=
"'%s' data ID component")
59 self.
table = RecordClass.Table.make(self.
mapper.getOutputSchema())
64 """Create a new result record from the given input record, using the given data ID and object ID 65 to fill in additional columns.""" 66 outputRecord = self.
table.copyRecord(inputRecord, self.
mapper)
68 outputRecord.set(key, dataId[name])
72 def add(self, catalog, dataId):
73 """Add a new catalog to the match, corresponding to the given data ID. 77 for record
in catalog:
83 catalog.sort(self.
idKey)
85 unmatchedIds = {record.get(self.
idKey)
for record
in catalog}
91 for refRecord, newRecord, distance
in matches:
93 if objId
in matchedRefIds:
97 matchedRefIds.add(objId)
98 if newRecord.get(self.
idKey)
in matchedCatIds:
103 matchedCatIds.add(newRecord.get(self.
idKey))
104 unmatchedIds.discard(newRecord.get(self.
idKey))
107 newToObj.setdefault(newRecord.get(self.
idKey),
set()).
add(objId)
112 for objId
in unmatchedIds:
113 newRecord = catalog.find(objId, self.
idKey)
116 self.
result.append(resultRecord)
120 """Return the final match catalog, after sorting it by object, copying it to ensure contiguousness, 121 and optionally removing ambiguous matches. 123 After calling finish(), the in-progress state of the matcher is returned to the state it was 124 just after construction, with the exception of the object ID counter (which is not reset). 128 for record
in self.
result:
130 result.append(record)
134 result = result.copy(deep=
True)
142 """A mapping (i.e. dict-like object) that provides convenient operations on the concatenated 143 catalogs returned by a MultiMatch object. 145 A GroupView provides access to a catalog of grouped objects, in which the grouping is indicated by 146 a field for which all records in a group have the same value. Once constructed, it allows operations 147 similar to those supported by SQL "GROUP BY", such as filtering and aggregate calculation. 151 def build(cls, catalog, groupField="object"):
152 """!Construct a GroupView from a concatenated catalog. 154 @param[in] cls (Class; omit this argument, but Doxygen wants it mentioned) 155 @param[in] catalog Input catalog, containing records grouped by a field in which all records 156 in the same group have the same value. Must be sorted by the group field. 157 @param[in] groupField Name or Key for the field that indicates groups. 159 groupKey = catalog.schema.find(groupField).key
160 ids, indices = numpy.unique(catalog.get(groupKey), return_index=
True)
161 groups = numpy.zeros(len(ids), dtype=object)
162 ends =
list(indices[1:]) + [len(catalog)]
163 for n, (i1, i2)
in enumerate(zip(indices, ends)):
165 groups[n] = catalog[int(i1):int(i2)]
166 assert (groups[n].get(groupKey) == ids[n]).
all()
167 return cls(catalog.schema, ids, groups)
170 """Direct constructor; most users should call build() instead. 172 This constructor takes the constituent arrays of the object directly, to allow multiple 173 methods for construction. 181 """Return the number of groups""" 185 """Iterate over group field values""" 189 """Return the catalog subset that corresponds to an group field value""" 190 index = numpy.searchsorted(self.
ids, key)
191 if self.
ids[index] != key:
192 raise KeyError(
"Group with ID {0} not found".format(key))
196 """Return a new GroupView that contains only groups for which the given predicate function 199 The predicate function is called once for each group, and passed a single argument: the subset 200 catalog for that group. 202 mask = numpy.zeros(len(self), dtype=bool)
203 for i
in range(len(self)):
204 mask[i] = predicate(self.
groups[i])
208 """!Run an aggregate function on each group, returning an array with one element for each group. 210 @param[in] function Callable object that computes the aggregate value. If field is None, 211 called with the entire subset catalog as an argument. If field is not 212 None, called with an array view into that field. 213 @param[in] field A string name or Key object that indicates a single field the aggregate 215 @param[in] dtype Data type for the output array. 217 result = numpy.zeros(len(self), dtype=dtype)
218 if field
is not None:
219 key = self.
schema.find(field).key
222 return function(cat.get(key))
225 for i
in range(len(self)):
226 result[i] = f(self.
groups[i])
229 def apply(self, function, field=None, dtype=float):
230 """!Run a non-aggregate function on each group, returning an array with one element for each record. 232 @param[in] function Callable object that computes the aggregate value. If field is None, 233 called with the entire subset catalog as an argument. If field is not 234 None, called with an array view into that field. 235 @param[in] field A string name or Key object that indicates a single field the aggregate 237 @param[in] dtype Data type for the output array. 239 result = numpy.zeros(self.
count, dtype=dtype)
240 if field
is not None:
241 key = self.
schema.find(field).key
244 return function(cat.get(key))
248 for i
in range(len(self)):
249 next = last + len(self.
groups[i])
250 result[last:next] = f(self.
groups[i])
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
def __init__(self, schema, dataIdFormat, coordField="coord", idField="id", radius=None, RecordClass=SourceRecord)
def where(self, predicate)
def build(cls, catalog, groupField="object")
Construct a GroupView from a concatenated catalog.
def apply(self, function, field=None, dtype=float)
Run a non-aggregate function on each group, returning an array with one element for each record...
def makeRecord(self, inputRecord, dataId, objId)
def __init__(self, schema, ids, groups)
A class representing an angle.
std::vector< Match< typename Cat::Record, typename Cat::Record > > matchRaDec(Cat const &cat, Angle radius, bool symmetric)
Compute all tuples (s1,s2,d) where s1 != s2, s1 and s2 both belong to cat, and d, the distance betwee...
def finish(self, removeAmbiguous=True)
def __getitem__(self, key)
def aggregate(self, function, field=None, dtype=float)
Run an aggregate function on each group, returning an array with one element for each group...
daf::base::PropertyList * list
def add(self, catalog, dataId)
daf::base::PropertySet * set