LSST Applications g0265f82a02+0e5473021a,g02d81e74bb+bd2ed33bd6,g1470d8bcf6+de7501a2e0,g14a832a312+ff425fae3c,g2079a07aa2+86d27d4dc4,g2305ad1205+91a32aca49,g295015adf3+762506a1ad,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g3ddfee87b4+c34e8be1fa,g487adcacf7+5fae3daba8,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+ea1711114f,g5a732f18d5+53520f316c,g64a986408d+bd2ed33bd6,g858d7b2824+bd2ed33bd6,g8a8a8dda67+585e252eca,g99cad8db69+016a06b37a,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+ef4e3a5875,gb0e22166c9+60f28cb32d,gb6a65358fc+0e5473021a,gba4ed39666+c2a2e4ac27,gbb8dafda3b+09e12c87ab,gc120e1dc64+bc2e06c061,gc28159a63d+0e5473021a,gcf0d15dbbd+c34e8be1fa,gdaeeff99f8+f9a426f77a,ge6526c86ff+508d0e0a30,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gf18bd8381d+8d59551888,gf1cff7945b+bd2ed33bd6,w.2024.16
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
lsst.afw.table.multiMatch.GroupView Class Reference
Inheritance diagram for lsst.afw.table.multiMatch.GroupView:

Public Member Functions

 build (cls, catalog, groupField="object")
 
 __init__ (self, schema, ids, groups)
 
 __len__ (self)
 
 __iter__ (self)
 
 __getitem__ (self, key)
 
 where (self, predicate)
 
 aggregate (self, function, field=None, dtype=float)
 
 apply (self, function, field=None, dtype=float)
 

Public Attributes

 schema
 
 ids
 
 groups
 
 count
 

Detailed Description

A mapping (i.e. dict-like object) that provides convenient
operations on the concatenated catalogs returned by a MultiMatch
object.

A GroupView provides access to a catalog of grouped objects, in
which the grouping is indicated by a field for which all records
in a group have the same value.  Once constructed, it allows
operations similar to those supported by SQL "GROUP BY", such as
filtering and aggregate calculation.

Parameters
----------
schema : `lsst.afw.table.Schema`
    Catalog schema to use for the grouped object catalog.
ids : `List`
    List of identifying keys for the groups in the catalog.
groups : `List`
    List of catalog subsets associated with each key in ids.

Definition at line 202 of file multiMatch.py.

Constructor & Destructor Documentation

◆ __init__()

lsst.afw.table.multiMatch.GroupView.__init__ ( self,
schema,
ids,
groups )

Definition at line 251 of file multiMatch.py.

251 def __init__(self, schema, ids, groups):
252 self.schema = schema
253 self.ids = ids
254 self.groups = groups
255 self.count = sum(len(cat) for cat in self.groups)
256

Member Function Documentation

◆ __getitem__()

lsst.afw.table.multiMatch.GroupView.__getitem__ ( self,
key )

Definition at line 263 of file multiMatch.py.

263 def __getitem__(self, key):
264 index = numpy.searchsorted(self.ids, key)
265 if self.ids[index] != key:
266 raise KeyError("Group with ID {0} not found".format(key))
267 return self.groups[index]
268

◆ __iter__()

lsst.afw.table.multiMatch.GroupView.__iter__ ( self)

Definition at line 260 of file multiMatch.py.

260 def __iter__(self):
261 return iter(self.ids)
262

◆ __len__()

lsst.afw.table.multiMatch.GroupView.__len__ ( self)

Definition at line 257 of file multiMatch.py.

257 def __len__(self):
258 return len(self.ids)
259

◆ aggregate()

lsst.afw.table.multiMatch.GroupView.aggregate ( self,
function,
field = None,
dtype = float )
Run an aggregate function on each group, returning an array with
one element for each group.

Parameters
----------
function :
    Callable object that computes the aggregate value.  If
    `field` is None, called with the entire subset catalog as an
    argument.  If `field` is not None, called with an array view
    into that field.
field : `str`, optional
    A string name or Key object that indicates a single field the aggregate
    is computed over.
dtype :
    Data type of the output array.

Returns
-------
result : Array of `dtype`
    Aggregated values for each group.

Definition at line 293 of file multiMatch.py.

293 def aggregate(self, function, field=None, dtype=float):
294 """Run an aggregate function on each group, returning an array with
295 one element for each group.
296
297 Parameters
298 ----------
299 function :
300 Callable object that computes the aggregate value. If
301 `field` is None, called with the entire subset catalog as an
302 argument. If `field` is not None, called with an array view
303 into that field.
304 field : `str`, optional
305 A string name or Key object that indicates a single field the aggregate
306 is computed over.
307 dtype :
308 Data type of the output array.
309
310 Returns
311 -------
312 result : Array of `dtype`
313 Aggregated values for each group.
314 """
315 result = numpy.zeros(len(self), dtype=dtype)
316 if field is not None:
317 key = self.schema.find(field).key
318
319 def f(cat):
320 return function(cat.get(key))
321 else:
322 f = function
323 for i in range(len(self)):
324 result[i] = f(self.groups[i])
325 return result
326

◆ apply()

lsst.afw.table.multiMatch.GroupView.apply ( self,
function,
field = None,
dtype = float )
Run a non-aggregate function on each group, returning an array with
one element for each record.

Parameters
----------
function :
    Callable object that computes the aggregate value.  If field is None,
    called with the entire subset catalog as an argument.  If field is not
    None, called with an array view into that field.
field : `str`
    A string name or Key object that indicates a single field the aggregate
    is computed over.
dtype :
    Data type for the output array.

Returns
-------
result : `numpy.array` of `dtype`
    Result of the function calculated on an element-by-element basis.

Definition at line 327 of file multiMatch.py.

327 def apply(self, function, field=None, dtype=float):
328 """Run a non-aggregate function on each group, returning an array with
329 one element for each record.
330
331 Parameters
332 ----------
333 function :
334 Callable object that computes the aggregate value. If field is None,
335 called with the entire subset catalog as an argument. If field is not
336 None, called with an array view into that field.
337 field : `str`
338 A string name or Key object that indicates a single field the aggregate
339 is computed over.
340 dtype :
341 Data type for the output array.
342
343 Returns
344 -------
345 result : `numpy.array` of `dtype`
346 Result of the function calculated on an element-by-element basis.
347 """
348 result = numpy.zeros(self.count, dtype=dtype)
349 if field is not None:
350 key = self.schema.find(field).key
351
352 def f(cat):
353 return function(cat.get(key))
354 else:
355 f = function
356 last = 0
357 for i in range(len(self)):
358 next = last + len(self.groups[i])
359 result[last:next] = f(self.groups[i])
360 last = next
361 return result

◆ build()

lsst.afw.table.multiMatch.GroupView.build ( cls,
catalog,
groupField = "object" )
Construct a GroupView from a concatenated catalog.

Parameters
----------
catalog : `lsst.afw.table.base.Catalog`
    Input catalog, containing records grouped by a field in
    which all records in the same group have the same value.
    Must be sorted by the group field.
groupField : `str`, optional
    Name or Key for the field that indicates groups.  Defaults
    to "object".

Returns
-------
groupCatalog : `lsst.afw.table.multiMatch.GroupView`
    Constructed GroupView from the input concatenated catalog.

Definition at line 224 of file multiMatch.py.

224 def build(cls, catalog, groupField="object"):
225 """Construct a GroupView from a concatenated catalog.
226
227 Parameters
228 ----------
229 catalog : `lsst.afw.table.base.Catalog`
230 Input catalog, containing records grouped by a field in
231 which all records in the same group have the same value.
232 Must be sorted by the group field.
233 groupField : `str`, optional
234 Name or Key for the field that indicates groups. Defaults
235 to "object".
236
237 Returns
238 -------
239 groupCatalog : `lsst.afw.table.multiMatch.GroupView`
240 Constructed GroupView from the input concatenated catalog.
241 """
242 groupKey = catalog.schema.find(groupField).key
243 ids, indices = numpy.unique(catalog.get(groupKey), return_index=True)
244 groups = numpy.zeros(len(ids), dtype=object)
245 ends = list(indices[1:]) + [len(catalog)]
246 for n, (i1, i2) in enumerate(zip(indices, ends)):
247 groups[n] = catalog[i1:i2]
248 assert (groups[n].get(groupKey) == ids[n]).all()
249 return cls(catalog.schema, ids, groups)
250

◆ where()

lsst.afw.table.multiMatch.GroupView.where ( self,
predicate )
Return a new GroupView that contains only groups for which the
given predicate function returns True.

The predicate function is called once for each group, and
passed a single argument: the subset catalog for that group.

Parameters
----------
predicate :
    Function to identify which groups should be included in
    the output.

Returns
-------
outGroupView : `lsst.afw.table.multiMatch.GroupView`
    Subset GroupView containing only groups that match the
    predicate.

Definition at line 269 of file multiMatch.py.

269 def where(self, predicate):
270 """Return a new GroupView that contains only groups for which the
271 given predicate function returns True.
272
273 The predicate function is called once for each group, and
274 passed a single argument: the subset catalog for that group.
275
276 Parameters
277 ----------
278 predicate :
279 Function to identify which groups should be included in
280 the output.
281
282 Returns
283 -------
284 outGroupView : `lsst.afw.table.multiMatch.GroupView`
285 Subset GroupView containing only groups that match the
286 predicate.
287 """
288 mask = numpy.zeros(len(self), dtype=bool)
289 for i in range(len(self)):
290 mask[i] = predicate(self.groups[i])
291 return type(self)(self.schema, self.ids[mask], self.groups[mask])
292

Member Data Documentation

◆ count

lsst.afw.table.multiMatch.GroupView.count

Definition at line 255 of file multiMatch.py.

◆ groups

lsst.afw.table.multiMatch.GroupView.groups

Definition at line 254 of file multiMatch.py.

◆ ids

lsst.afw.table.multiMatch.GroupView.ids

Definition at line 253 of file multiMatch.py.

◆ schema

lsst.afw.table.multiMatch.GroupView.schema

Definition at line 252 of file multiMatch.py.


The documentation for this class was generated from the following file: