From 4fb790b48adb0824a742898166444f44c65fc33f Mon Sep 17 00:00:00 2001 From: Ian Sullivan Date: Tue, 3 Jun 2025 16:09:09 -0700 Subject: [PATCH 1/2] Raise error if no kernel candidates remain after max iterations --- python/lsst/ip/diffim/psfMatch.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/python/lsst/ip/diffim/psfMatch.py b/python/lsst/ip/diffim/psfMatch.py index 23c451ccb..f3c1c7c85 100644 --- a/python/lsst/ip/diffim/psfMatch.py +++ b/python/lsst/ip/diffim/psfMatch.py @@ -38,6 +38,14 @@ from . import diffimLib +class NoKernelCandidatesError(pipeBase.AlgorithmError): + """Raised if there are too few candidates to compute the PSF matching + kernel. + """ + def metadata(self) -> dict: + return {} + + class PsfMatchConfig(pexConfig.Config): """Base configuration for Psf-matching @@ -809,8 +817,8 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False): Raises ------ - RuntimeError : - If unable to determine PSF matching kernel and ``returnOnExcept==False``. + NoKernelCandidatesError : + If there are no useable kernel candidates. """ import lsstDebug @@ -917,7 +925,7 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False): # If only nGoodSpatial == 0, might be other candidates in the cells if nGoodSpatial == 0 and nRejectedSpatial == 0: - raise RuntimeError("No kernel candidates for spatial fit") + raise NoKernelCandidatesError("No kernel candidates for spatial fit") if nRejectedSpatial == 0: # Nothing rejected, finished with spatial fit @@ -938,6 +946,10 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False): trace_loggers[2].debug("Spatial kernel built with %d candidates", spatialkv.getNCandidates()) spatialKernel, spatialBackground = spatialkv.getSolutionPair() + # Run after the final fit to use the updated kernel visitor `spatialkv` + if spatialkv.getNCandidates() < 1: + raise NoKernelCandidatesError("No kernel candidates remain after max iterations") + spatialSolution = spatialkv.getKernelSolution() t1 = time.time() From 8678e8915c5e9501b3dacb3ad3c99c130a5300bd Mon Sep 17 00:00:00 2001 From: Ian Sullivan Date: Tue, 3 Jun 2025 17:36:43 -0700 Subject: [PATCH 2/2] Remove unused keyword --- python/lsst/ip/diffim/psfMatch.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/python/lsst/ip/diffim/psfMatch.py b/python/lsst/ip/diffim/psfMatch.py index f3c1c7c85..42b107bae 100644 --- a/python/lsst/ip/diffim/psfMatch.py +++ b/python/lsst/ip/diffim/psfMatch.py @@ -794,7 +794,7 @@ def _buildCellSet(self, *args): return @timeMethod - def _solve(self, kernelCellSet, basisList, returnOnExcept=False): + def _solve(self, kernelCellSet, basisList): """Solve for the PSF matching kernel Parameters @@ -805,8 +805,6 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False): basisList : `list` of `lsst.afw.math.kernel.FixedKernel` list of Kernels to be used in the decomposition of the spatially varying kernel (typically as provided by makeKernelBasisList) - returnOnExcept : `bool`, optional - if True then return (None, None) if an error occurs, else raise the exception Returns -------