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 May 6, 2021
1 parent 20a7f2f commit d6fdcb1
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions python/lsst/pipe/tasks/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,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
nDeblended = np.count_nonzero(deblended)
nDeblended = np.count_nonzero(_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 @@ -192,14 +188,39 @@ def run(self, sources):
children = ((sources["parent"] != 0) # 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
nChildren = np.count_nonzero(children)
nChildren = np.count_nonzero(_filterSkySources(sources, children))
except LookupError as e:
# Probably "parent"; all other columns already checked
raise MetricComputationError("Invalid input catalog") from e
meas = Measurement(self.config.metricName, nChildren * u.dimensionless_unscaled)

return Struct(measurement=meas)


def _filterSkySources(catalog, matches):
"""Filter out any sky sources from a vector of matches.
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.
matches : `numpy.ndarray` [`bool`], (N,)
A vector of existing source matches, of the same length as ``catalog``,
where matched sources are marked `True`.
Returns
-------
filtered : `numpy.ndarray` [`bool`], (N,)
A version of ``matches`` with any sky sources filtered out
(set to `False`). May be the same vector as ``matches`` 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 matches & (catalog["sky_source"] == False) # noqa: E712
else:
return matches

0 comments on commit d6fdcb1

Please sign in to comment.