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-47787: Add check on psf size to MeasurePsfTask #1014

Merged
merged 1 commit into from
Nov 28, 2024
Merged
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
32 changes: 32 additions & 0 deletions python/lsst/pipe/tasks/measurePsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

__all__ = ["MeasurePsfConfig", "MeasurePsfTask"]

import numpy as np

import lsst.afw.display as afwDisplay
import lsst.afw.math as afwMath
import lsst.meas.algorithms as measAlg
Expand All @@ -31,6 +33,27 @@
from lsst.utils.timer import timeMethod


class NonfinitePsfShapeError(pipeBase.AlgorithmError):
"""Raised if the radius of the fitted psf model is non-finite.

Parameters
----------
psf_size : `float`
Fitted size of the failing PSF model.
"""
def __init__(self, psf_size) -> None:
self._psf_size = psf_size
super().__init__(
f"Failed to determine PSF: fitter returned model with non-finite PSF size ({psf_size} pixels)."
)

@property
def metadata(self) -> dict:
return {
"psf_size": self._psf_size,
}


class MeasurePsfConfig(pexConfig.Config):
starSelector = measAlg.sourceSelectorRegistry.makeField(
"Star selection algorithm",
Expand Down Expand Up @@ -199,6 +222,11 @@ def run(self, exposure, sources, expId=0, matches=None):
``cellSet``
An `lsst.afw.math.SpatialCellSet` containing the PSF candidates
as returned by the psf determiner.

Raises
------
NonfinitePsfShapeError
If the new PSF has NaN or Inf width.
"""
self.log.info("Measuring PSF")

Expand Down Expand Up @@ -242,6 +270,10 @@ def run(self, exposure, sources, expId=0, matches=None):
flagKey=self.usedKey)
self.log.info("PSF determination using %d/%d stars.",
self.metadata.getScalar("numGoodStars"), self.metadata.getScalar("numAvailStars"))
if not np.isfinite((psfSize := psf.computeShape(psf.getAveragePosition()).getDeterminantRadius())):
raise NonfinitePsfShapeError(psf_size=psfSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document that this method can now raise a NonfinitePsfShapeError

else:
self.log.info("Fitted PSF size: %f pixels", psfSize)

exposure.setPsf(psf)

Expand Down