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-44167: Add check for when plugins raise InvalidPsfError. #274

Merged
merged 2 commits into from
May 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions python/lsst/meas/base/baseMeasurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from .pluginRegistry import PluginMap
from ._measBaseLib import FatalAlgorithmError, MeasurementError
from lsst.afw.detection import InvalidPsfError
from .pluginsBase import BasePluginConfig, BasePlugin
from .noiseReplacer import NoiseReplacerConfig

Expand Down Expand Up @@ -312,6 +313,21 @@ def initializePlugins(self, **kwds):
self.undeblendedPlugins[name] = PluginClass(config, undeblendedName,
metadata=self.algMetadata, **kwds)

if "schemaMapper" in kwds:
schema = kwds["schemaMapper"].editOutputSchema()
else:
schema = kwds["schema"]

invalidPsfName = "base_InvalidPsf_flag"
if invalidPsfName in schema:
self.keyInvalidPsf = schema.find(invalidPsfName).key
else:
self.keyInvalidPsf = schema.addField(
invalidPsfName,
type="Flag",
doc="Invalid PSF at this location.",
)

def callMeasure(self, measRecord, *args, **kwds):
"""Call ``measure`` on all plugins and consistently handle exceptions.

Expand Down Expand Up @@ -392,6 +408,12 @@ def doMeasurement(self, plugin, measRecord, *args, **kwds):
"MeasurementError in %s.measure on record %s: %s",
plugin.name, measRecord.getId(), error)
plugin.fail(measRecord, error)
except InvalidPsfError as error:
self.log.getChild(plugin.name).debug(
"InvalidPsfError in %s.measure on record %s: %s",
plugin.name, measRecord.getId(), error)
measRecord.set(self.keyInvalidPsf, True)
plugin.fail(measRecord)
except Exception as error:
self.log.getChild(plugin.name).warning(
"Exception in %s.measure on record %s: %s",
Expand Down Expand Up @@ -474,17 +496,24 @@ def doMeasurementN(self, plugin, measCat, *args, **kwds):
raise

except MeasurementError as error:
self.log.getChild(plugin.name).debug(
"MeasurementError in %s.measureN on records %s-%s: %s",
plugin.name, measCat[0].getId(), measCat[-1].getId(), error)
for measRecord in measCat:
self.log.getChild(plugin.name).debug(
"MeasurementError in %s.measureN on records %s-%s: %s",
plugin.name, measCat[0].getId(), measCat[-1].getId(), error)
plugin.fail(measRecord, error)
except InvalidPsfError as error:
self.log.getChild(plugin.name).debug(
"InvalidPsfError in %s.measureN on records %s-%s: %s",
plugin.name, measCat[0].getId(), measCat[-1].getId(), error)
for measRecord in measCat:
measRecord.set(self.keyInvalidPsf, True)
plugin.fail(measRecord, error)
except Exception as error:
self.log.getChild(plugin.name).warning(
"Exception in %s.measureN on records %s-%s: %s",
plugin.name, measCat[0].getId(), measCat[-1].getId(), error)
for measRecord in measCat:
plugin.fail(measRecord)
self.log.getChild(plugin.name).warning(
"Exception in %s.measureN on records %s-%s: %s",
plugin.name, measCat[0].getId(), measCat[-1].getId(), error)

@staticmethod
def getFootprintsFromCatalog(catalog):
Expand Down
101 changes: 101 additions & 0 deletions tests/test_InvalidPsfFlag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# This file is part of meas_base.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import unittest
import lsst.utils.tests

from lsst.meas.base.sfm import SingleFramePluginConfig, SingleFramePlugin
from lsst.afw.detection import InvalidPsfError
from lsst.meas.base.tests import AlgorithmTestCase
from lsst.meas.base.pluginRegistry import register
from lsst.meas.base import FlagDefinitionList, FlagHandler


class InvalidPsfPluginConfig(SingleFramePluginConfig):
pass


@register("test_InvalidPsfPlugin")
class InvalidPsfPlugin(SingleFramePlugin):
ConfigClass = InvalidPsfPluginConfig

def __init__(self, config, name, schema, metadata):
SingleFramePlugin.__init__(self, config, name, schema, metadata)
flagDefs = FlagDefinitionList()
self.FAILURE = flagDefs.addFailureFlag()
self.flagHandler = FlagHandler.addFields(schema, name, flagDefs)

@classmethod
def getExecutionOrder(cls):
return cls.FLUX_ORDER

def measure(self, measRecord, exposure):
raise InvalidPsfError("This record has an invalid PSF!")

def fail(self, measRecord, error=None):
self.flagHandler.handleFailure(measRecord)


class InvalidPsfFlagTestCase(AlgorithmTestCase, lsst.utils.tests.TestCase):
def setUp(self):
self.algName = "test_InvalidPsfPlugin"
bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Point2I(100, 100))
self.dataset = lsst.meas.base.tests.TestDataset(bbox)
self.dataset.addSource(instFlux=1E5, centroid=lsst.geom.Point2D(25, 26))
config = lsst.meas.base.SingleFrameMeasurementConfig()
config.plugins = [self.algName]
config.slots.centroid = None
config.slots.apFlux = None
config.slots.calibFlux = None
config.slots.gaussianFlux = None
config.slots.modelFlux = None
config.slots.psfFlux = None
config.slots.shape = None
config.slots.psfShape = None
self.config = config

def tearDown(self):
del self.config
del self.dataset

def testInvalidPsfFlag(self):
schema = self.dataset.makeMinimalSchema()
task = lsst.meas.base.SingleFrameMeasurementTask(schema=schema, config=self.config)
exposure, cat = self.dataset.realize(noise=100.0, schema=schema, randomSeed=0)
task.run(cat, exposure)

# Check the plugin failure flag.
self.assertTrue(cat[0][f"{self.algName}_flag"])
# And the general invalid psf flag.
self.assertTrue(cat[0]["base_InvalidPsf_flag"])


class TestMemory(lsst.utils.tests.MemoryTestCase):
pass


def setup_module(module):
lsst.utils.tests.init()


if __name__ == "__main__":
lsst.utils.tests.init()
unittest.main()
Loading