Skip to content

Commit

Permalink
Merge pull request #365 from lsst/tickets/DM-47787
Browse files Browse the repository at this point in the history
DM-47787: Make the diffim kernel fallback more robust
  • Loading branch information
isullivan authored Nov 28, 2024
2 parents 63c0c3c + d50a149 commit acef1a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions python/lsst/ip/diffim/makeKernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def getSelectSources(self, exposure, sigma=None, doSmooth=True, idFactory=None):
return selectSources

def makeCandidateList(self, convolved, reference, kernelSize,
candidateList, preconvolved=False):
candidateList, preconvolved=False, sigma=None):
"""Make a list of acceptable KernelCandidates.
Generate a list of candidate sources for Psf-matching, remove sources
Expand Down Expand Up @@ -309,7 +309,7 @@ def makeCandidateList(self, convolved, reference, kernelSize,
If ``candidateList`` is empty after sub-selection.
"""
if candidateList is None:
candidateList = self.getSelectSources(reference, doSmooth=not preconvolved)
candidateList = self.getSelectSources(reference, doSmooth=not preconvolved, sigma=sigma)
if len(candidateList) < 1:
raise RuntimeError("No kernel candidates after detection and measurement.")

Expand Down
42 changes: 24 additions & 18 deletions python/lsst/ip/diffim/subtractImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,29 +389,29 @@ def run(self, template, science, sources, visitSummary=None):
# 2. RangeError, in case the template coverage is so poor that we end
# up near a region with no data.
try:
templatePsfSize = getPsfFwhm(template.psf)
sciencePsfSize = getPsfFwhm(science.psf)
self.templatePsfSize = getPsfFwhm(template.psf)
self.sciencePsfSize = getPsfFwhm(science.psf)
except (lsst.pex.exceptions.InvalidParameterError, lsst.pex.exceptions.RangeError):
self.log.info("Unable to evaluate PSF at the average position. "
"Evaluting PSF on a grid of points."
)
templatePsfSize = evaluateMeanPsfFwhm(template,
fwhmExposureBuffer=fwhmExposureBuffer,
fwhmExposureGrid=fwhmExposureGrid
)
sciencePsfSize = evaluateMeanPsfFwhm(science,
fwhmExposureBuffer=fwhmExposureBuffer,
fwhmExposureGrid=fwhmExposureGrid
)
self.log.info("Science PSF FWHM: %f pixels", sciencePsfSize)
self.log.info("Template PSF FWHM: %f pixels", templatePsfSize)
self.metadata["sciencePsfSize"] = sciencePsfSize
self.metadata["templatePsfSize"] = templatePsfSize
self.templatePsfSize = evaluateMeanPsfFwhm(template,
fwhmExposureBuffer=fwhmExposureBuffer,
fwhmExposureGrid=fwhmExposureGrid
)
self.sciencePsfSize = evaluateMeanPsfFwhm(science,
fwhmExposureBuffer=fwhmExposureBuffer,
fwhmExposureGrid=fwhmExposureGrid
)
self.log.info("Science PSF FWHM: %f pixels", self.sciencePsfSize)
self.log.info("Template PSF FWHM: %f pixels", self.templatePsfSize)
self.metadata["sciencePsfSize"] = self.sciencePsfSize
self.metadata["templatePsfSize"] = self.templatePsfSize

# Calculate estimated image depths, i.e., limiting magnitudes
maglim_science = self._calculateMagLim(science, fallbackPsfSize=sciencePsfSize)
maglim_science = self._calculateMagLim(science, fallbackPsfSize=self.sciencePsfSize)
fluxlim_science = (maglim_science*u.ABmag).to_value(u.nJy)
maglim_template = self._calculateMagLim(template, fallbackPsfSize=templatePsfSize)
maglim_template = self._calculateMagLim(template, fallbackPsfSize=self.templatePsfSize)
if np.isnan(maglim_template):
self.log.info("Cannot evaluate template limiting mag; adopting science limiting mag for diffim")
maglim_diffim = maglim_science
Expand All @@ -428,7 +428,7 @@ def run(self, template, science, sources, visitSummary=None):
fwhmExposureBuffer=fwhmExposureBuffer,
fwhmExposureGrid=fwhmExposureGrid)
if convolveTemplate:
if sciencePsfSize < templatePsfSize:
if self.sciencePsfSize < self.templatePsfSize:
self.log.info("Average template PSF size is greater, "
"but science PSF greater in one dimension: convolving template image.")
else:
Expand Down Expand Up @@ -505,8 +505,14 @@ def runConvolveTemplate(self, template, science, selectSources):
if self.config.allowKernelSourceDetection:
self.log.warning("Error encountered trying to construct the matching kernel"
f" Running source detection and retrying. {e}")
kernelSize = self.makeKernel.makeKernelBasisList(
self.templatePsfSize, self.sciencePsfSize)[0].getWidth()
sigmaToFwhm = 2*np.log(2*np.sqrt(2))
candidateList = self.makeKernel.makeCandidateList(template, science, kernelSize,
candidateList=None,
sigma=self.sciencePsfSize/sigmaToFwhm)
kernelSources = self.makeKernel.selectKernelSources(template, science,
candidateList=None,
candidateList=candidateList,
preconvolved=False)
kernelResult = self.makeKernel.run(template, science, kernelSources,
preconvolved=False)
Expand Down

0 comments on commit acef1a1

Please sign in to comment.