Skip to content

Commit

Permalink
Factor common code from source count metrics.
Browse files Browse the repository at this point in the history
These tasks could probably be factored further, since the only
difference is in the criteria used for filtering sources, but handling
columns in a general way would be nontrivial and should probably await
the addition of more varied metrics.
  • Loading branch information
kfindeisen committed Jun 18, 2021
1 parent 7fe5c82 commit 5013283
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions python/lsst/pipe/tasks/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ def run(self, sources):
deblended = ((sources["parent"] == 0) # top-level source
& (sources["deblend_nChild"] > 0) # deblended
)
if "sky_source" in sources.schema:
# E712 is not applicable, because
# afw.table.SourceRecord.ColumnView is not a bool.
deblended = deblended & (sources["sky_source"] == False) # noqa: E712
deblended = _filterSkySources(sources, deblended)
except LookupError as e:
# Probably "parent"; all other columns already checked
raise MetricComputationError("Invalid input catalog") from e
Expand Down Expand Up @@ -195,10 +192,7 @@ def run(self, sources):
children = ((sources["deblend_parentNChild"] > 1) # deblend child
& (sources["deblend_nChild"] == 0) # not deblended
)
if "sky_source" in sources.schema:
# E712 is not applicable, because
# afw.table.SourceRecord.ColumnView is not a bool.
children = children & (sources["sky_source"] == False) # noqa: E712
children = _filterSkySources(sources, children)
except LookupError as e:
# Probably "parent"; all other columns already checked
raise MetricComputationError("Invalid input catalog") from e
Expand All @@ -207,3 +201,32 @@ def run(self, sources):
meas = Measurement(self.config.metricName, nChildren * u.dimensionless_unscaled)

return Struct(measurement=meas)


def _filterSkySources(catalog, selection):
"""Filter out any sky sources from a vector of selected sources.
If no sky source information is available, all sources are assumed to
be non-sky.
Parameters
----------
catalog : `lsst.afw.table.SourceCatalog`
The catalog to filter.
selection : `numpy.ndarray` [`bool`], (N,)
A vector of existing source selections, of the same length as
``catalog``, where selected sources are marked `True`.
Returns
-------
filtered : `numpy.ndarray` [`bool`], (N,)
A version of ``selection`` with any sky sources filtered out
(set to `False`). May be the same vector as ``selection`` if
no changes were made.
"""
if "sky_source" in catalog.schema:
# E712 is not applicable, because afw.table.SourceRecord.ColumnView
# is not a bool.
return selection & (catalog["sky_source"] == False) # noqa: E712
else:
return selection

0 comments on commit 5013283

Please sign in to comment.