Skip to content

Commit

Permalink
Merge branch 'tickets/DM-30869'
Browse files Browse the repository at this point in the history
  • Loading branch information
kfindeisen committed Sep 20, 2022
2 parents 09fcba0 + 6d6ab58 commit 0fbe0db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
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

0 comments on commit 0fbe0db

Please sign in to comment.