Coverage for python/lsst/meas/algorithms/pcaPsfDeterminer.py : 57%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# # LSST Data Management System # # Copyright 2008-2017 AURA/LSST. # # This product includes software developed by the # LSST Project (http://www.lsst.org/). # # 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 LSST License Statement and # the GNU General Public License along with this program. If not, # see <https://www.lsstcorp.org/LegalNotices/>. #
fitSpatialKernelFromPsfCandidates, fitKernelParamsToImage
"""Return the number of PSF candidates to be rejected.
The number of candidates being rejected on each iteration gradually increases, so that on the Nth of M iterations we reject N/M of the bad candidates.
Parameters ---------- numBadCandidates : int Number of bad candidates under consideration.
numIter : int The number of the current PSF iteration.
totalIter : int The total number of PSF iterations.
Returns ------- int Number of candidates to reject. """
doc="Use non-linear fitter for spatial variation of Kernel", dtype=bool, default=False, ) doc="number of eigen components for PSF kernel creation", dtype=int, default=4, ) doc="specify spatial order for PSF kernel creation", dtype=int, default=2, ) doc="size of cell used to determine PSF (pixels, column direction)", dtype=int, default=256, # minValue = 10, check=lambda x: x >= 10, ) doc="size of cell used to determine PSF (pixels, row direction)", dtype=int, default=sizeCellX.default, # minValue = 10, check=lambda x: x >= 10, ) doc="number of stars per psf cell for PSF kernel creation", dtype=int, default=3, ) doc="Number of pixels to ignore around the edge of PSF candidate postage stamps", dtype=int, default=0, ) doc="number of stars per psf Cell for spatial fitting", dtype=int, default=5, ) doc="Should each PSF candidate be given the same weight, independent of magnitude?", dtype=bool, default=True, ) doc="number of iterations of PSF candidate star list", dtype=int, default=3, ) doc="tolerance of spatial fitting", dtype=float, default=1e-2, ) doc="floor for variance is lam*data", dtype=float, default=0.05, ) doc="for psf candidate evaluation", dtype=float, default=2.0, ) doc="Rejection threshold (stdev) for candidates based on spatial fit", dtype=float, default=3.0, ) doc="Threshold (stdev) for rejecting extraneous pixels around candidate; applied if positive", dtype=float, default=0.0, ) doc="Reject candidates that are blended?", dtype=bool, default=False, ) doc="Mask blends in image?", dtype=bool, default=True, )
"""! A measurePsfTask psf estimator """
# # Loop trying to use nEigenComponents, but allowing smaller numbers if necessary # # Determine KL components psfCellSet, exposure.getDimensions(), exposure.getXY0(), nEigen, self.config.spatialOrder, kernelSize, self.config.nStarPerCell, bool(self.config.constantWeight))
raise IndexError("No viable PSF candidates survive")
# # We got our eigen decomposition so let's use it # # Express eigenValues in units of reduced chi^2 per star for l in eigenValues]
# Fit spatial model kernel, psfCellSet, bool(self.config.nonLinearSpatialFit), self.config.nStarPerCellSpatialFit, self.config.tolerance, self.config.lam)
"""!Determine a PCA PSF model for an exposure given a list of PSF candidates
@param[in] exposure exposure containing the psf candidates (lsst.afw.image.Exposure) @param[in] psfCandidateList a sequence of PSF candidates (each an lsst.meas.algorithms.PsfCandidate); typically obtained by detecting sources and then running them through a star selector @param[in,out] metadata a home for interesting tidbits of information @param[in] flagKey schema key used to mark sources actually used in PSF determination
@return a list of - psf: the measured PSF, an lsst.meas.algorithms.PcaPsf - cellSet: an lsst.afw.math.SpatialCellSet containing the PSF candidates """ # match Kernel amplitudes for spatial plots # Keep matplotlib alive post mortem # Normalize residuals by object amplitude
pause = True
raise RuntimeError("No PSF candidates supplied.")
# construct and populate a spatial cell set continue
except Exception as e: self.log.debug("Skipping PSF candidate %d of %d: %s", i, len(psfCandidateList), e) continue
raise RuntimeError("No usable PSF candidates supplied")
self.log.warn("WARNING: NOT scaling kernelSize by stellar quadrupole moment " + "because config.kernelSize=%s >= 15; " + "using config.kernelSize as as the width, instead", self.config.kernelSize) actualKernelSize = int(self.config.kernelSize) else: actualKernelSize = self.config.kernelSizeMax
print("Median size=%s" % (medSize,))
# Set size of image returned around candidate
# Remove blended candidates completely print("Removing %d blended Psf candidates" % len(blendedCandidates))
frame = 0 if displayExposure: ds9.mtv(exposure, frame=frame, title="psf determination") utils.showPsfSpatialCells(exposure, psfCellSet, self.config.nStarPerCell, symb="o", ctype=ds9.CYAN, ctypeUnused=ds9.YELLOW, size=4, frame=frame)
# # Do a PCA decomposition of those PSF candidates # # import lsst.afw.display.utils as displayUtils
stamps = [] for cell in psfCellSet.getCellList(): for cand in cell.begin(not showBadCandidates): # maybe include bad candidates try: im = cand.getMaskedImage()
chi2 = cand.getChi2() if chi2 > 1e100: chi2 = numpy.nan
stamps.append((im, "%d%s" % (utils.splitId(cand.getSource().getId(), True)["objId"], chi2), cand.getStatus())) except Exception as e: continue
if len(stamps) == 0: print("WARNING: No PSF candidates to show; try setting showBadCandidates=True") else: mos = displayUtils.Mosaic() for im, label, status in stamps: im = type(im)(im, True) try: im /= afwMath.makeStatistics(im, afwMath.MAX).getValue() except NotImplementedError: pass
mos.append(im, label, ds9.GREEN if status == afwMath.SpatialCellCandidate.GOOD else ds9.YELLOW if status == afwMath.SpatialCellCandidate.UNKNOWN else ds9.RED)
mos.makeMosaic(frame=8, title="Psf Candidates")
# Re-fit until we don't have any candidates with naughty chi^2 values influencing the fit # # First, estimate the PSF # self._fitPsf(exposure, psfCellSet, actualKernelSize, nEigenComponents) # # In clipping, allow all candidates to be innocent until proven guilty on this iteration. # Throw out any prima facie guilty candidates (naughty chi^2 values) # # Guilty prima facie awfulCandidates.append(cand) cleanChi2 = False self.log.debug("chi^2=%s; id=%s", cand.getChi2(), cand.getSource().getId()) if display: print("Removing bad candidate: id=%d, chi^2=%f" % (cand.getSource().getId(), cand.getChi2())) cell.removeCandidate(cand)
# # Clip out bad fits based on reduced chi^2 #
self.config.nIterForPsf) chi2 = c.getChi2() if chi2 > 1e100: chi2 = numpy.nan
print("Chi^2 clipping %-4d %.2g" % (c.getSource().getId(), chi2))
# # Clip out bad fits based on spatial fitting. # # This appears to be better at getting rid of sources that have a single dominant kernel component # (other than the zeroth; e.g., a nearby contaminant) because the surrounding sources (which help # set the spatial model) don't contain that kernel component, and so the spatial modeling # downweights the component. #
except Exception as e: continue
k in range(kernel.getNKernelParameters())]
# print cand.getSource().getId(), [a / amp for a in params], predict
if False: # Straight standard deviation mean = residuals[:, k].mean() rms = residuals[:, k].std() elif False: # Using interquartile range sr = numpy.sort(residuals[:, k]) mean = sr[int(0.5*len(sr))] if len(sr) % 2 else \ 0.5 * (sr[int(0.5*len(sr))] + sr[int(0.5*len(sr))+1]) rms = 0.74 * (sr[int(0.75*len(sr))] - sr[int(0.25*len(sr))]) else:
print("Mean for component %d is %f" % (k, mean)) print("RMS for component %d is %f" % (k, rms))
self.config.nIterForPsf)
print("Spatial clipping %d (%f,%f) based on %d: %f vs %f" % (cand.getSource().getId(), cand.getXCenter(), cand.getYCenter(), k, residuals[badCandidates[i], k], self.config.spatialReject * rms))
# # Display results # if displayExposure: if iterNum > 0: ds9.erase(frame=frame) utils.showPsfSpatialCells(exposure, psfCellSet, self.config.nStarPerCell, showChi2=True, symb="o", size=8, frame=frame, ctype=ds9.YELLOW, ctypeBad=ds9.RED, ctypeUnused=ds9.MAGENTA) if self.config.nStarPerCellSpatialFit != self.config.nStarPerCell: utils.showPsfSpatialCells(exposure, psfCellSet, self.config.nStarPerCellSpatialFit, symb="o", size=10, frame=frame, ctype=ds9.YELLOW, ctypeBad=ds9.RED) if displayResiduals: while True: try: utils.showPsfCandidates(exposure, psfCellSet, psf=psf, frame=4, normalize=normalizeResiduals, showBadCandidates=showBadCandidates) utils.showPsfCandidates(exposure, psfCellSet, psf=psf, frame=5, normalize=normalizeResiduals, showBadCandidates=showBadCandidates, variance=True) except Exception: if not showBadCandidates: showBadCandidates = True continue break
if displayPsfComponents: utils.showPsf(psf, eigenValues, frame=6) if displayPsfMosaic: utils.showPsfMosaic(exposure, psf, frame=7, showFwhm=True) ds9.scale('linear', 0, 1, frame=7) if displayPsfSpatialModel: utils.plotPsfSpatialModel(exposure, psf, psfCellSet, showBadCandidates=True, matchKernelAmplitudes=matchKernelAmplitudes, keepPlots=keepMatplotlibPlots)
if pause: while True: try: reply = input("Next iteration? [ynchpqQs] ").strip() except EOFError: reply = "n"
reply = reply.split() if reply: reply, args = reply[0], reply[1:] else: reply = ""
if reply in ("", "c", "h", "n", "p", "q", "Q", "s", "y"): if reply == "c": pause = False elif reply == "h": print("c[ontinue without prompting] h[elp] n[o] p[db] q[uit displaying] " "s[ave fileName] y[es]") continue elif reply == "p": import pdb pdb.set_trace() elif reply == "q": display = False elif reply == "Q": sys.exit(1) elif reply == "s": fileName = args.pop(0) if not fileName: print("Please provide a filename") continue
print("Saving to %s" % fileName) utils.saveSpatialCellSet(psfCellSet, fileName=fileName) continue break else: print("Unrecognised response: %s" % reply, file=sys.stderr)
if reply == "n": break
# One last time, to take advantage of the last iteration self._fitPsf(exposure, psfCellSet, actualKernelSize, nEigenComponents)
# # Display code for debugging # if displayExposure: utils.showPsfSpatialCells(exposure, psfCellSet, self.config.nStarPerCell, showChi2=True, symb="o", ctype=ds9.YELLOW, ctypeBad=ds9.RED, size=8, frame=frame) if self.config.nStarPerCellSpatialFit != self.config.nStarPerCell: utils.showPsfSpatialCells(exposure, psfCellSet, self.config.nStarPerCellSpatialFit, symb="o", ctype=ds9.YELLOW, ctypeBad=ds9.RED, size=10, frame=frame) if displayResiduals: utils.showPsfCandidates(exposure, psfCellSet, psf=psf, frame=4, normalize=normalizeResiduals, showBadCandidates=showBadCandidates)
if displayPsfComponents: utils.showPsf(psf, eigenValues, frame=6)
if displayPsfMosaic: utils.showPsfMosaic(exposure, psf, frame=7, showFwhm=True) ds9.scale("linear", 0, 1, frame=7) if displayPsfSpatialModel: utils.plotPsfSpatialModel(exposure, psf, psfCellSet, showBadCandidates=True, matchKernelAmplitudes=matchKernelAmplitudes, keepPlots=keepMatplotlibPlots) # # Generate some QA information # # Count PSF stars #
src.set(flagKey, True)
"""!Generator for Psf candidates
This allows two 'for' loops to be reduced to one.
@param psfCellSet SpatialCellSet of PSF candidates @param ignoreBad Ignore candidates flagged as BAD? @return SpatialCell, PsfCandidate """
|