Skip to content

Commit

Permalink
Add check for when plugins raise InvalidPsfError.
Browse files Browse the repository at this point in the history
This will set a catch-all flag in these cases as well as the invidual failure
flags.
  • Loading branch information
erykoff committed May 7, 2024
1 parent 62aa8f1 commit 3beeb9b
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 6 deletions.
37 changes: 31 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,17 @@ 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"]

self.keyInvalidPsf = schema.addField(
"base_InvalidPsf_flag",
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 +404,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,14 +492,21 @@ 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)
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, MeasurementError

Check failure on line 29 in tests/test_InvalidPsfFlag.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

F401

'lsst.meas.base.MeasurementError' imported but unused


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()

0 comments on commit 3beeb9b

Please sign in to comment.