Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-30869: Modernize MetricTask for better Gen 3 workflow #723

Merged
merged 3 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 13 additions & 23 deletions python/lsst/pipe/tasks/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import numpy as np
import astropy.units as u

from lsst.pipe.base import Struct, connectionTypes
from lsst.pipe.base import NoWorkFound, Struct, connectionTypes
from lsst.verify import Measurement
from lsst.verify.tasks import MetricTask, MetricConfig, MetricConnections, MetricComputationError

Expand Down Expand Up @@ -76,8 +76,8 @@ def run(self, sources):
Parameters
----------
sources : `lsst.afw.table.SourceCatalog` or `None`
A science source catalog, which may be empty or `None`.
sources : `lsst.afw.table.SourceCatalog`
A science source catalog, which may be empty.
Returns
-------
Expand All @@ -95,12 +95,8 @@ def run(self, sources):
Raised if ``sources`` is missing mandatory keys for
source catalogs.
"""
if sources is None:
self.log.info("Nothing to do: no catalogs found.")
meas = None
elif "deblend_nChild" not in sources.schema:
self.log.info("Nothing to do: no deblending performed.")
meas = None
if "deblend_nChild" not in sources.schema:
raise NoWorkFound("Nothing to do: no deblending performed.")
else:
try:
deblended = ((sources["parent"] == 0) # top-level source
Expand All @@ -112,9 +108,8 @@ def run(self, sources):
raise MetricComputationError("Invalid input catalog") from e
else:
nDeblended = np.count_nonzero(deblended)
meas = Measurement(self.config.metricName, nDeblended * u.dimensionless_unscaled)

return Struct(measurement=meas)
return Struct(measurement=Measurement(self.config.metricName,
nDeblended * u.dimensionless_unscaled))


class NumberDeblendChildSourcesMetricConnections(
Expand Down Expand Up @@ -159,8 +154,8 @@ def run(self, sources):
Parameters
----------
sources : `lsst.afw.table.SourceCatalog` or `None`
A science source catalog, which may be empty or `None`.
sources : `lsst.afw.table.SourceCatalog`
A science source catalog, which may be empty.
Returns
-------
Expand All @@ -178,14 +173,10 @@ def run(self, sources):
Raised if ``sources`` is missing mandatory keys for
source catalogs.
"""
if sources is None:
self.log.info("Nothing to do: no catalogs found.")
meas = None
# Use deblend_parentNChild rather than detect_fromBlend because the
# latter need not be defined in post-deblending catalogs.
elif "deblend_parentNChild" not in sources.schema or "deblend_nChild" not in sources.schema:
self.log.info("Nothing to do: no deblending performed.")
meas = None
if "deblend_parentNChild" not in sources.schema or "deblend_nChild" not in sources.schema:
raise NoWorkFound("Nothing to do: no deblending performed.")
else:
try:
children = ((sources["deblend_parentNChild"] > 1) # deblend child
Expand All @@ -197,9 +188,8 @@ def run(self, sources):
raise MetricComputationError("Invalid input catalog") from e
else:
nChildren = np.count_nonzero(children)
meas = Measurement(self.config.metricName, nChildren * u.dimensionless_unscaled)

return Struct(measurement=meas)
return Struct(measurement=Measurement(self.config.metricName,
nChildren * u.dimensionless_unscaled))


def _filterSkySources(catalog, selection):
Expand Down
40 changes: 20 additions & 20 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,16 @@ def testMultiDeblending(self):

def testNoDeblending(self):
catalog = _makeDummyCatalog(3, deblendFlags=False)
result = self.task.run(catalog)
lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
meas = result.measurement
self.assertIsNone(meas)

def testMissingData(self):
result = self.task.run(None)
lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
meas = result.measurement
self.assertIsNone(meas)
try:
result = self.task.run(catalog)
except lsst.pipe.base.NoWorkFound:
# Correct behavior
pass
else:
# Alternative correct behavior
lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
meas = result.measurement
self.assertIsNone(meas)


class TestNumDeblendChild(MetricTaskTestCase):
Expand Down Expand Up @@ -245,16 +245,16 @@ def testMultiDeblending(self):

def testNoDeblending(self):
catalog = _makeDummyCatalog(3, deblendFlags=False)
result = self.task.run(catalog)
lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
meas = result.measurement
self.assertIsNone(meas)

def testMissingData(self):
result = self.task.run(None)
lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
meas = result.measurement
self.assertIsNone(meas)
try:
result = self.task.run(catalog)
except lsst.pipe.base.NoWorkFound:
# Correct behavior
pass
else:
# Alternative correct behavior
lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
meas = result.measurement
self.assertIsNone(meas)


# Hack around unittest's hacky test setup system
Expand Down