From 47888b630d0ee3bb1537fde92af4f95e57ac8841 Mon Sep 17 00:00:00 2001 From: fred3m Date: Thu, 8 May 2025 18:06:03 -0700 Subject: [PATCH] Catch errors in computing warped PSF shape --- python/lsst/ip/diffim/modelPsfMatch.py | 38 ++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/python/lsst/ip/diffim/modelPsfMatch.py b/python/lsst/ip/diffim/modelPsfMatch.py index 263b0a58a..e126e561d 100644 --- a/python/lsst/ip/diffim/modelPsfMatch.py +++ b/python/lsst/ip/diffim/modelPsfMatch.py @@ -27,6 +27,7 @@ import lsst.afw.math as afwMath import lsst.geom as geom import lsst.pex.config as pexConfig +import lsst.pex.exceptions as pexExceptions import lsst.pipe.base as pipeBase from lsst.utils.logging import getTraceLogger from lsst.utils.timer import timeMethod @@ -34,7 +35,12 @@ from .psfMatch import PsfMatchTask, PsfMatchConfigAL from . import utils as dituils -__all__ = ("ModelPsfMatchTask", "ModelPsfMatchConfig") +__all__ = ( + "ModelPsfMatchTask", + "ModelPsfMatchConfig", + "WarpedPsfTransformTooBigError", + "PsfComputeShapeError", +) sigma2fwhm = 2.*np.sqrt(2.*np.log(2.)) @@ -44,6 +50,26 @@ def nextOddInteger(x): return nextInt + 1 if nextInt%2 == 0 else nextInt +class WarpedPsfTransformTooBigError(pipeBase.AlgorithmError): + """Raised when the transform of a WarpedPsf is too large to compute + the FWHM of the PSF at a given position. + """ + def metadata(self) -> dict: + return {} + + +class PsfComputeShapeError(pipeBase.AlgorithmError): + def __init__(self, position): + message = f"Unable to compute the FWHM of the science Psf at {position}" + super().__init__(message) + self.position = position + + def metadata(self) -> dict: + return { + "position": self.position, + } + + class ModelPsfMatchConfig(pexConfig.Config): """Configuration for model-to-model Psf matching""" @@ -145,7 +171,15 @@ def run(self, exposure, referencePsfModel, kernelSum=1.0): # exposure's bounding box in DM-32756. sciAvgPos = exposure.getPsf().getAveragePosition() modelAvgPos = referencePsfModel.getAveragePosition() - fwhmScience = exposure.getPsf().computeShape(sciAvgPos).getDeterminantRadius()*sigma2fwhm + try: + fwhmScience = exposure.getPsf().computeShape(sciAvgPos).getDeterminantRadius()*sigma2fwhm + except pexExceptions.RangeError: + raise WarpedPsfTransformTooBigError( + f"Unable to compute the FWHM of the science Psf at {sciAvgPos}" + "due to an unexpectedly large transform." + ) + except pexExceptions.Exception: + raise PsfComputeShapeError(sciAvgPos) fwhmModel = referencePsfModel.computeShape(modelAvgPos).getDeterminantRadius()*sigma2fwhm basisList = makeKernelBasisList(self.kConfig, fwhmScience, fwhmModel, metadata=self.metadata)