22 __all__ = [
"DetectionConfig",
"PsfMatchConfig",
"PsfMatchConfigAL",
"PsfMatchConfigDF",
"PsfMatchTask"]
33 import lsst.pipe.base
as pipeBase
34 from lsst.meas.algorithms
import SubtractBackgroundConfig
35 from .
import utils
as diutils
36 from .
import diffimLib
40 """Configuration for detecting sources on images for building a 43 Configuration for turning detected lsst.afw.detection.FootPrints into an 44 acceptable (unmasked, high signal-to-noise, not too large or not too small) 45 list of `lsst.ip.diffim.KernelSources` that are used to build the 46 Psf-matching kernel""" 48 detThreshold = pexConfig.Field(
50 doc=
"Value of footprint detection threshold",
52 check=
lambda x: x >= 3.0
54 detThresholdType = pexConfig.ChoiceField(
56 doc=
"Type of detection threshold",
57 default=
"pixel_stdev",
59 "value":
"Use counts as the detection threshold type",
60 "stdev":
"Use standard deviation of image plane",
61 "variance":
"Use variance of image plane",
62 "pixel_stdev":
"Use stdev derived from variance plane" 65 detOnTemplate = pexConfig.Field(
67 doc=
"""If true run detection on the template (image to convolve); 68 if false run detection on the science image""",
71 badMaskPlanes = pexConfig.ListField(
73 doc=
"""Mask planes that lead to an invalid detection. 74 Options: NO_DATA EDGE SAT BAD CR INTRP""",
75 default=(
"NO_DATA",
"EDGE",
"SAT")
77 fpNpixMin = pexConfig.Field(
79 doc=
"Minimum number of pixels in an acceptable Footprint",
81 check=
lambda x: x >= 5
83 fpNpixMax = pexConfig.Field(
85 doc=
"""Maximum number of pixels in an acceptable Footprint; 86 too big and the subsequent convolutions become unwieldy""",
88 check=
lambda x: x <= 500
90 fpGrowKernelScaling = pexConfig.Field(
92 doc=
"""If config.scaleByFwhm, grow the footprint based on 93 the final kernelSize. Each footprint will be 94 2*fpGrowKernelScaling*kernelSize x 95 2*fpGrowKernelScaling*kernelSize. With the value 96 of 1.0, the remaining pixels in each KernelCandiate 97 after convolution by the basis functions will be 98 equal to the kernel size itself.""",
100 check=
lambda x: x >= 1.0
102 fpGrowPix = pexConfig.Field(
104 doc=
"""Growing radius (in pixels) for each raw detection 105 footprint. The smaller the faster; however the 106 kernel sum does not converge if the stamp is too 107 small; and the kernel is not constrained at all if 108 the stamp is the size of the kernel. The grown stamp 109 is 2 * fpGrowPix pixels larger in each dimension. 110 This is overridden by fpGrowKernelScaling if scaleByFwhm""",
112 check=
lambda x: x >= 10
114 scaleByFwhm = pexConfig.Field(
116 doc=
"Scale fpGrowPix by input Fwhm?",
122 """Base configuration for Psf-matching 124 The base configuration of the Psf-matching kernel, and of the warping, detection, 125 and background modeling subTasks.""" 127 warpingConfig = pexConfig.ConfigField(
"Config for warping exposures to a common alignment",
128 afwMath.warper.WarperConfig)
129 detectionConfig = pexConfig.ConfigField(
"Controlling the detection of sources for kernel building",
131 afwBackgroundConfig = pexConfig.ConfigField(
"Controlling the Afw background fitting",
132 SubtractBackgroundConfig)
134 useAfwBackground = pexConfig.Field(
136 doc=
"Use afw background subtraction instead of ip_diffim",
139 fitForBackground = pexConfig.Field(
141 doc=
"Include terms (including kernel cross terms) for background in ip_diffim",
144 kernelBasisSet = pexConfig.ChoiceField(
146 doc=
"Type of basis set for PSF matching kernel.",
147 default=
"alard-lupton",
149 "alard-lupton":
"""Alard-Lupton sum-of-gaussians basis set, 150 * The first term has no spatial variation 151 * The kernel sum is conserved 152 * You may want to turn off 'usePcaForSpatialKernel'""",
153 "delta-function":
"""Delta-function kernel basis set, 154 * You may enable the option useRegularization 155 * You should seriously consider usePcaForSpatialKernel, which will also 156 enable kernel sum conservation for the delta function kernels""" 159 kernelSize = pexConfig.Field(
161 doc=
"""Number of rows/columns in the convolution kernel; should be odd-valued. 162 Modified by kernelSizeFwhmScaling if scaleByFwhm = true""",
165 scaleByFwhm = pexConfig.Field(
167 doc=
"Scale kernelSize, alardGaussians by input Fwhm",
170 kernelSizeFwhmScaling = pexConfig.Field(
172 doc=
"""How much to scale the kernel size based on the largest AL Sigma""",
174 check=
lambda x: x >= 1.0
176 kernelSizeMin = pexConfig.Field(
178 doc=
"""Minimum Kernel Size""",
181 kernelSizeMax = pexConfig.Field(
183 doc=
"""Maximum Kernel Size""",
186 spatialModelType = pexConfig.ChoiceField(
188 doc=
"Type of spatial functions for kernel and background",
189 default=
"chebyshev1",
191 "chebyshev1":
"Chebyshev polynomial of the first kind",
192 "polynomial":
"Standard x,y polynomial",
195 spatialKernelOrder = pexConfig.Field(
197 doc=
"Spatial order of convolution kernel variation",
199 check=
lambda x: x >= 0
201 spatialBgOrder = pexConfig.Field(
203 doc=
"Spatial order of differential background variation",
205 check=
lambda x: x >= 0
207 sizeCellX = pexConfig.Field(
209 doc=
"Size (rows) in pixels of each SpatialCell for spatial modeling",
211 check=
lambda x: x >= 32
213 sizeCellY = pexConfig.Field(
215 doc=
"Size (columns) in pixels of each SpatialCell for spatial modeling",
217 check=
lambda x: x >= 32
219 nStarPerCell = pexConfig.Field(
221 doc=
"Number of KernelCandidates in each SpatialCell to use in the spatial fitting",
223 check=
lambda x: x >= 1
225 maxSpatialIterations = pexConfig.Field(
227 doc=
"Maximum number of iterations for rejecting bad KernelCandidates in spatial fitting",
229 check=
lambda x: x >= 1
and x <= 5
231 usePcaForSpatialKernel = pexConfig.Field(
233 doc=
"""Use Pca to reduce the dimensionality of the kernel basis sets. 234 This is particularly useful for delta-function kernels. 235 Functionally, after all Cells have their raw kernels determined, we run 236 a Pca on these Kernels, re-fit the Cells using the eigenKernels and then 237 fit those for spatial variation using the same technique as for Alard-Lupton kernels. 238 If this option is used, the first term will have no spatial variation and the 239 kernel sum will be conserved.""",
242 subtractMeanForPca = pexConfig.Field(
244 doc=
"Subtract off the mean feature before doing the Pca",
247 numPrincipalComponents = pexConfig.Field(
249 doc=
"""Number of principal components to use for Pca basis, including the 250 mean kernel if requested.""",
252 check=
lambda x: x >= 3
254 singleKernelClipping = pexConfig.Field(
256 doc=
"Do sigma clipping on each raw kernel candidate",
259 kernelSumClipping = pexConfig.Field(
261 doc=
"Do sigma clipping on the ensemble of kernel sums",
264 spatialKernelClipping = pexConfig.Field(
266 doc=
"Do sigma clipping after building the spatial model",
269 checkConditionNumber = pexConfig.Field(
271 doc=
"""Test for maximum condition number when inverting a kernel matrix. 272 Anything above maxConditionNumber is not used and the candidate is set as BAD. 273 Also used to truncate inverse matrix in estimateBiasedRisk. However, 274 if you are doing any deconvolution you will want to turn this off, or use 275 a large maxConditionNumber""",
278 badMaskPlanes = pexConfig.ListField(
280 doc=
"""Mask planes to ignore when calculating diffim statistics 281 Options: NO_DATA EDGE SAT BAD CR INTRP""",
282 default=(
"NO_DATA",
"EDGE",
"SAT")
284 candidateResidualMeanMax = pexConfig.Field(
286 doc=
"""Rejects KernelCandidates yielding bad difference image quality. 287 Used by BuildSingleKernelVisitor, AssessSpatialKernelVisitor. 288 Represents average over pixels of (image/sqrt(variance)).""",
290 check=
lambda x: x >= 0.0
292 candidateResidualStdMax = pexConfig.Field(
294 doc=
"""Rejects KernelCandidates yielding bad difference image quality. 295 Used by BuildSingleKernelVisitor, AssessSpatialKernelVisitor. 296 Represents stddev over pixels of (image/sqrt(variance)).""",
298 check=
lambda x: x >= 0.0
300 useCoreStats = pexConfig.Field(
302 doc=
"""Use the core of the footprint for the quality statistics, instead of the entire footprint. 303 WARNING: if there is deconvolution we probably will need to turn this off""",
306 candidateCoreRadius = pexConfig.Field(
308 doc=
"""Radius for calculation of stats in 'core' of KernelCandidate diffim. 309 Total number of pixels used will be (2*radius)**2. 310 This is used both for 'core' diffim quality as well as ranking of 311 KernelCandidates by their total flux in this core""",
313 check=
lambda x: x >= 1
315 maxKsumSigma = pexConfig.Field(
317 doc=
"""Maximum allowed sigma for outliers from kernel sum distribution. 318 Used to reject variable objects from the kernel model""",
320 check=
lambda x: x >= 0.0
322 maxConditionNumber = pexConfig.Field(
324 doc=
"Maximum condition number for a well conditioned matrix",
326 check=
lambda x: x >= 0.0
328 conditionNumberType = pexConfig.ChoiceField(
330 doc=
"Use singular values (SVD) or eigen values (EIGENVALUE) to determine condition number",
331 default=
"EIGENVALUE",
333 "SVD":
"Use singular values",
334 "EIGENVALUE":
"Use eigen values (faster)",
337 maxSpatialConditionNumber = pexConfig.Field(
339 doc=
"Maximum condition number for a well conditioned spatial matrix",
341 check=
lambda x: x >= 0.0
343 iterateSingleKernel = pexConfig.Field(
345 doc=
"""Remake KernelCandidate using better variance estimate after first pass? 346 Primarily useful when convolving a single-depth image, otherwise not necessary.""",
349 constantVarianceWeighting = pexConfig.Field(
351 doc=
"""Use constant variance weighting in single kernel fitting? 352 In some cases this is better for bright star residuals.""",
355 calculateKernelUncertainty = pexConfig.Field(
357 doc=
"""Calculate kernel and background uncertainties for each kernel candidate? 358 This comes from the inverse of the covariance matrix. 359 Warning: regularization can cause problems for this step.""",
362 useBicForKernelBasis = pexConfig.Field(
364 doc=
"""Use Bayesian Information Criterion to select the number of bases going into the kernel""",
370 """The parameters specific to the "Alard-Lupton" (sum-of-Gaussian) Psf-matching basis""" 373 PsfMatchConfig.setDefaults(self)
377 alardNGauss = pexConfig.Field(
379 doc=
"Number of Gaussians in alard-lupton basis",
381 check=
lambda x: x >= 1
383 alardDegGauss = pexConfig.ListField(
385 doc=
"Polynomial order of spatial modification of Gaussians. Must in number equal alardNGauss",
388 alardSigGauss = pexConfig.ListField(
390 doc=
"""Sigma in pixels of Gaussians (FWHM = 2.35 sigma). Must in number equal alardNGauss""",
391 default=(0.7, 1.5, 3.0),
393 alardGaussBeta = pexConfig.Field(
395 doc=
"""Default scale factor between Gaussian sigmas """,
397 check=
lambda x: x >= 0.0,
399 alardMinSig = pexConfig.Field(
401 doc=
"""Minimum Sigma (pixels) for Gaussians""",
403 check=
lambda x: x >= 0.25
405 alardDegGaussDeconv = pexConfig.Field(
407 doc=
"""Degree of spatial modification of ALL gaussians in AL basis during deconvolution""",
409 check=
lambda x: x >= 1
411 alardMinSigDeconv = pexConfig.Field(
413 doc=
"""Minimum Sigma (pixels) for Gaussians during deconvolution; 414 make smaller than alardMinSig as this is only indirectly used""",
416 check=
lambda x: x >= 0.25
418 alardNGaussDeconv = pexConfig.Field(
420 doc=
"Number of Gaussians in AL basis during deconvolution",
422 check=
lambda x: x >= 1
427 """The parameters specific to the delta-function (one basis per-pixel) Psf-matching basis""" 430 PsfMatchConfig.setDefaults(self)
437 useRegularization = pexConfig.Field(
439 doc=
"Use regularization to smooth the delta function kernels",
442 regularizationType = pexConfig.ChoiceField(
444 doc=
"Type of regularization.",
445 default=
"centralDifference",
447 "centralDifference":
"Penalize second derivative using 2-D stencil of central finite difference",
448 "forwardDifference":
"Penalize first, second, third derivatives using forward finite differeces" 451 centralRegularizationStencil = pexConfig.ChoiceField(
453 doc=
"Type of stencil to approximate central derivative (for centralDifference only)",
456 5:
"5-point stencil including only adjacent-in-x,y elements",
457 9:
"9-point stencil including diagonal elements" 460 forwardRegularizationOrders = pexConfig.ListField(
462 doc=
"Array showing which order derivatives to penalize (for forwardDifference only)",
464 itemCheck=
lambda x: (x > 0)
and (x < 4)
466 regularizationBorderPenalty = pexConfig.Field(
468 doc=
"Value of the penalty for kernel border pixels",
470 check=
lambda x: x >= 0.0
472 lambdaType = pexConfig.ChoiceField(
474 doc=
"How to choose the value of the regularization strength",
477 "absolute":
"Use lambdaValue as the value of regularization strength",
478 "relative":
"Use lambdaValue as fraction of the default regularization strength (N.R. 18.5.8)",
479 "minimizeBiasedRisk":
"Minimize biased risk estimate",
480 "minimizeUnbiasedRisk":
"Minimize unbiased risk estimate",
483 lambdaValue = pexConfig.Field(
485 doc=
"Value used for absolute determinations of regularization strength",
488 lambdaScaling = pexConfig.Field(
490 doc=
"Fraction of the default lambda strength (N.R. 18.5.8) to use. 1e-4 or 1e-5",
493 lambdaStepType = pexConfig.ChoiceField(
495 doc=
"""If a scan through lambda is needed (minimizeBiasedRisk, minimizeUnbiasedRisk), 496 use log or linear steps""",
499 "log":
"Step in log intervals; e.g. lambdaMin, lambdaMax, lambdaStep = -1.0, 2.0, 0.1",
500 "linear":
"Step in linear intervals; e.g. lambdaMin, lambdaMax, lambdaStep = 0.1, 100, 0.1",
503 lambdaMin = pexConfig.Field(
505 doc=
"""If scan through lambda needed (minimizeBiasedRisk, minimizeUnbiasedRisk), 506 start at this value. If lambdaStepType = log:linear, suggest -1:0.1""",
509 lambdaMax = pexConfig.Field(
511 doc=
"""If scan through lambda needed (minimizeBiasedRisk, minimizeUnbiasedRisk), 512 stop at this value. If lambdaStepType = log:linear, suggest 2:100""",
515 lambdaStep = pexConfig.Field(
517 doc=
"""If scan through lambda needed (minimizeBiasedRisk, minimizeUnbiasedRisk), 518 step in these increments. If lambdaStepType = log:linear, suggest 0.1:0.1""",
524 """Base class for Psf Matching; should not be called directly 528 PsfMatchTask is a base class that implements the core functionality for matching the 529 Psfs of two images using a spatially varying Psf-matching lsst.afw.math.LinearCombinationKernel. 530 The Task requires the user to provide an instance of an lsst.afw.math.SpatialCellSet, 531 filled with lsst.ip.diffim.KernelCandidate instances, and a list of lsst.afw.math.Kernels 532 of basis shapes that will be used for the decomposition. If requested, the Task 533 also performs background matching and returns the differential background model as an 534 lsst.afw.math.Kernel.SpatialFunction. 538 As a base class, this Task is not directly invoked. However, run() methods that are 539 implemented on derived classes will make use of the core _solve() functionality, 540 which defines a sequence of lsst.afw.math.CandidateVisitor classes that iterate 541 through the KernelCandidates, first building up a per-candidate solution and then 542 building up a spatial model from the ensemble of candidates. Sigma clipping is 543 performed using the mean and standard deviation of all kernel sums (to reject 544 variable objects), on the per-candidate substamp diffim residuals 545 (to indicate a bad choice of kernel basis shapes for that particular object), 546 and on the substamp diffim residuals using the spatial kernel fit (to indicate a bad 547 choice of spatial kernel order, or poor constraints on the spatial model). The 548 _diagnostic() method logs information on the quality of the spatial fit, and also 549 modifies the Task metadata. 551 .. list-table:: Quantities set in Metadata 556 * - `spatialConditionNum` 557 - Condition number of the spatial kernel fit 558 * - `spatialKernelSum` 559 - Kernel sum (10^{-0.4 * ``Delta``; zeropoint}) of the spatial Psf-matching kernel 561 - If using sum-of-Gaussian basis, the number of gaussians used 562 * - `ALBasisDegGauss` 563 - If using sum-of-Gaussian basis, the deg of spatial variation of the Gaussians 564 * - `ALBasisSigGauss` 565 - If using sum-of-Gaussian basis, the widths (sigma) of the Gaussians 567 - If using sum-of-Gaussian basis, the kernel size 568 * - `NFalsePositivesTotal` 569 - Total number of diaSources 570 * - `NFalsePositivesRefAssociated` 571 - Number of diaSources that associate with the reference catalog 572 * - `NFalsePositivesRefAssociated` 573 - Number of diaSources that associate with the source catalog 574 * - `NFalsePositivesUnassociated` 575 - Number of diaSources that are orphans 577 - Mean value of substamp diffim quality metrics across all KernelCandidates, 578 for both the per-candidate (LOCAL) and SPATIAL residuals 580 - Median value of substamp diffim quality metrics across all KernelCandidates, 581 for both the per-candidate (LOCAL) and SPATIAL residuals 583 - Standard deviation of substamp diffim quality metrics across all KernelCandidates, 584 for both the per-candidate (LOCAL) and SPATIAL residuals 588 The lsst.pipe.base.cmdLineTask.CmdLineTask command line task interface supports a 589 flag -d/--debug to import @b debug.py from your PYTHONPATH. The relevant contents of debug.py 590 for this Task include: 597 di = lsstDebug.getInfo(name) 598 if name == "lsst.ip.diffim.psfMatch": 599 # enable debug output 601 # display mask transparency 602 di.maskTransparency = 80 603 # show all the candidates and residuals 604 di.displayCandidates = True 605 # show kernel basis functions 606 di.displayKernelBasis = False 607 # show kernel realized across the image 608 di.displayKernelMosaic = True 609 # show coefficients of spatial model 610 di.plotKernelSpatialModel = False 611 # show the bad candidates (red) along with good (green) 612 di.showBadCandidates = True 614 lsstDebug.Info = DebugInfo 617 Note that if you want addional logging info, you may add to your scripts: 621 import lsst.log.utils as logUtils 622 logUtils.traceSetAt("ip.diffim", 4) 624 ConfigClass = PsfMatchConfig
625 _DefaultName =
"psfMatch" 628 """Create the psf-matching Task 633 Arguments to be passed to ``lsst.pipe.base.task.Task.__init__`` 635 Keyword arguments to be passed to ``lsst.pipe.base.task.Task.__init__`` 639 The initialization sets the Psf-matching kernel configuration using the value of 640 self.config.kernel.active. If the kernel is requested with regularization to moderate 641 the bias/variance tradeoff, currently only used when a delta function kernel basis 642 is provided, it creates a regularization matrix stored as member variable 645 pipeBase.Task.__init__(self, *args, **kwargs)
648 if 'useRegularization' in self.
kConfig:
654 self.
hMat = diffimLib.makeRegularizationMatrix(pexConfig.makePolicy(self.
kConfig))
656 def _diagnostic(self, kernelCellSet, spatialSolution, spatialKernel, spatialBg):
657 """Provide logging diagnostics on quality of spatial kernel fit 662 Cellset that contains the KernelCandidates used in the fitting 663 spatialSolution : TYPE 664 KernelSolution of best-fit 666 Best-fit spatial Kernel model 668 Best-fit spatial background model 671 kImage = afwImage.ImageD(spatialKernel.getDimensions())
672 kSum = spatialKernel.computeImage(kImage,
False)
673 self.log.info(
"Final spatial kernel sum %.3f" % (kSum))
676 conditionNum = spatialSolution.getConditionNumber(
677 getattr(diffimLib.KernelSolution, self.
kConfig.conditionNumberType))
678 self.log.info(
"Spatial model condition number %.3e" % (conditionNum))
680 if conditionNum < 0.0:
681 self.log.warn(
"Condition number is negative (%.3e)" % (conditionNum))
682 if conditionNum > self.
kConfig.maxSpatialConditionNumber:
683 self.log.warn(
"Spatial solution exceeds max condition number (%.3e > %.3e)" % (
684 conditionNum, self.
kConfig.maxSpatialConditionNumber))
686 self.metadata.set(
"spatialConditionNum", conditionNum)
687 self.metadata.set(
"spatialKernelSum", kSum)
690 nBasisKernels = spatialKernel.getNBasisKernels()
691 nKernelTerms = spatialKernel.getNSpatialParameters()
692 if nKernelTerms == 0:
696 nBgTerms = spatialBg.getNParameters()
698 if spatialBg.getParameters()[0] == 0.0:
704 for cell
in kernelCellSet.getCellList():
705 for cand
in cell.begin(
False):
707 if cand.getStatus() == afwMath.SpatialCellCandidate.GOOD:
709 if cand.getStatus() == afwMath.SpatialCellCandidate.BAD:
712 self.log.info(
"Doing stats of kernel candidates used in the spatial fit.")
716 self.log.warn(
"Many more candidates rejected than accepted; %d total, %d rejected, %d used" % (
719 self.log.info(
"%d candidates total, %d rejected, %d used" % (nTot, nBad, nGood))
722 if nGood < nKernelTerms:
723 self.log.warn(
"Spatial kernel model underconstrained; %d candidates, %d terms, %d bases" % (
724 nGood, nKernelTerms, nBasisKernels))
725 self.log.warn(
"Consider lowering the spatial order")
726 elif nGood <= 2*nKernelTerms:
727 self.log.warn(
"Spatial kernel model poorly constrained; %d candidates, %d terms, %d bases" % (
728 nGood, nKernelTerms, nBasisKernels))
729 self.log.warn(
"Consider lowering the spatial order")
731 self.log.info(
"Spatial kernel model well constrained; %d candidates, %d terms, %d bases" % (
732 nGood, nKernelTerms, nBasisKernels))
735 self.log.warn(
"Spatial background model underconstrained; %d candidates, %d terms" % (
737 self.log.warn(
"Consider lowering the spatial order")
738 elif nGood <= 2*nBgTerms:
739 self.log.warn(
"Spatial background model poorly constrained; %d candidates, %d terms" % (
741 self.log.warn(
"Consider lowering the spatial order")
743 self.log.info(
"Spatial background model appears well constrained; %d candidates, %d terms" % (
746 def _displayDebug(self, kernelCellSet, spatialKernel, spatialBackground):
747 """Provide visualization of the inputs and ouputs to the Psf-matching code 752 The SpatialCellSet used in determining the matching kernel and background 754 Spatially varying Psf-matching kernel 755 spatialBackground : TYPE 756 Spatially varying background-matching function 761 displayKernelMosaic =
lsstDebug.Info(__name__).displayKernelMosaic
762 plotKernelSpatialModel =
lsstDebug.Info(__name__).plotKernelSpatialModel
765 if not maskTransparency:
767 afwDisplay.setDefaultMaskTransparency(maskTransparency)
769 if displayCandidates:
770 diutils.showKernelCandidates(kernelCellSet, kernel=spatialKernel, background=spatialBackground,
771 frame=lsstDebug.frame,
772 showBadCandidates=showBadCandidates)
774 diutils.showKernelCandidates(kernelCellSet, kernel=spatialKernel, background=spatialBackground,
775 frame=lsstDebug.frame,
776 showBadCandidates=showBadCandidates,
779 diutils.showKernelCandidates(kernelCellSet, kernel=spatialKernel, background=spatialBackground,
780 frame=lsstDebug.frame,
781 showBadCandidates=showBadCandidates,
785 if displayKernelBasis:
786 diutils.showKernelBasis(spatialKernel, frame=lsstDebug.frame)
789 if displayKernelMosaic:
790 diutils.showKernelMosaic(kernelCellSet.getBBox(), spatialKernel, frame=lsstDebug.frame)
793 if plotKernelSpatialModel:
794 diutils.plotKernelSpatialModel(spatialKernel, kernelCellSet, showBadCandidates=showBadCandidates)
796 def _createPcaBasis(self, kernelCellSet, nStarPerCell, policy):
797 """Create Principal Component basis 799 If a principal component analysis is requested, typically when using a delta function basis, 800 perform the PCA here and return a new basis list containing the new principal components. 805 a SpatialCellSet containing KernelCandidates, from which components are derived 807 the number of stars per cell to visit when doing the PCA 809 input policy controlling the single kernel visitor 814 number of KernelCandidates rejected during PCA loop 815 spatialBasisList : TYPE 816 basis list containing the principal shapes as Kernels 821 If the Eigenvalues sum to zero. 823 nComponents = self.
kConfig.numPrincipalComponents
824 imagePca = diffimLib.KernelPcaD()
825 importStarVisitor = diffimLib.KernelPcaVisitorF(imagePca)
826 kernelCellSet.visitCandidates(importStarVisitor, nStarPerCell)
827 if self.
kConfig.subtractMeanForPca:
828 importStarVisitor.subtractMean()
831 eigenValues = imagePca.getEigenValues()
832 pcaBasisList = importStarVisitor.getEigenKernels()
834 eSum = np.sum(eigenValues)
836 raise RuntimeError(
"Eigenvalues sum to zero")
837 for j
in range(len(eigenValues)):
838 log.log(
"TRACE5." + self.log.getName() +
"._solve", log.DEBUG,
839 "Eigenvalue %d : %f (%f)", j, eigenValues[j], eigenValues[j]/eSum)
841 nToUse = min(nComponents, len(eigenValues))
843 for j
in range(nToUse):
845 kimage = afwImage.ImageD(pcaBasisList[j].getDimensions())
846 pcaBasisList[j].computeImage(kimage,
False)
847 if not (
True in np.isnan(kimage.getArray())):
848 trimBasisList.append(pcaBasisList[j])
851 spatialBasisList = diffimLib.renormalizeKernelList(trimBasisList)
854 singlekvPca = diffimLib.BuildSingleKernelVisitorF(spatialBasisList, policy)
855 singlekvPca.setSkipBuilt(
False)
856 kernelCellSet.visitCandidates(singlekvPca, nStarPerCell)
857 singlekvPca.setSkipBuilt(
True)
858 nRejectedPca = singlekvPca.getNRejected()
860 return nRejectedPca, spatialBasisList
862 def _buildCellSet(self, *args):
863 """Fill a SpatialCellSet with KernelCandidates for the Psf-matching process; 864 override in derived classes""" 868 def _solve(self, kernelCellSet, basisList, returnOnExcept=False):
869 """Solve for the PSF matching kernel 874 a SpatialCellSet to use in determining the matching kernel 875 (typically as provided by _buildCellSet) 877 list of Kernels to be used in the decomposition of the spatially varying kernel 878 (typically as provided by makeKernelBasisList) 879 returnOnExcept : `bool`, optional 880 if True then return (None, None) if an error occurs, else raise the exception 884 psfMatchingKernel : TYPE 886 backgroundModel : TYPE 887 differential background model 892 if unable to determine PSF matching kernel and returnOnExcept False 898 maxSpatialIterations = self.
kConfig.maxSpatialIterations
899 nStarPerCell = self.
kConfig.nStarPerCell
900 usePcaForSpatialKernel = self.
kConfig.usePcaForSpatialKernel
903 policy = pexConfig.makePolicy(self.
kConfig)
905 singlekv = diffimLib.BuildSingleKernelVisitorF(basisList, policy, self.
hMat)
907 singlekv = diffimLib.BuildSingleKernelVisitorF(basisList, policy)
910 ksv = diffimLib.KernelSumVisitorF(policy)
917 while (thisIteration < maxSpatialIterations):
921 while (nRejectedSkf != 0):
922 log.log(
"TRACE1." + self.log.getName() +
"._solve", log.DEBUG,
923 "Building single kernels...")
924 kernelCellSet.visitCandidates(singlekv, nStarPerCell)
925 nRejectedSkf = singlekv.getNRejected()
926 log.log(
"TRACE1." + self.log.getName() +
"._solve", log.DEBUG,
927 "Iteration %d, rejected %d candidates due to initial kernel fit",
928 thisIteration, nRejectedSkf)
932 ksv.setMode(diffimLib.KernelSumVisitorF.AGGREGATE)
933 kernelCellSet.visitCandidates(ksv, nStarPerCell)
934 ksv.processKsumDistribution()
935 ksv.setMode(diffimLib.KernelSumVisitorF.REJECT)
936 kernelCellSet.visitCandidates(ksv, nStarPerCell)
938 nRejectedKsum = ksv.getNRejected()
939 log.log(
"TRACE1." + self.log.getName() +
"._solve", log.DEBUG,
940 "Iteration %d, rejected %d candidates due to kernel sum",
941 thisIteration, nRejectedKsum)
944 if nRejectedKsum > 0:
953 if (usePcaForSpatialKernel):
954 log.log(
"TRACE0." + self.log.getName() +
"._solve", log.DEBUG,
955 "Building Pca basis")
957 nRejectedPca, spatialBasisList = self.
_createPcaBasis(kernelCellSet, nStarPerCell, policy)
958 log.log(
"TRACE1." + self.log.getName() +
"._solve", log.DEBUG,
959 "Iteration %d, rejected %d candidates due to Pca kernel fit",
960 thisIteration, nRejectedPca)
971 if (nRejectedPca > 0):
975 spatialBasisList = basisList
978 regionBBox = kernelCellSet.getBBox()
979 spatialkv = diffimLib.BuildSpatialKernelVisitorF(spatialBasisList, regionBBox, policy)
980 kernelCellSet.visitCandidates(spatialkv, nStarPerCell)
981 spatialkv.solveLinearEquation()
982 log.log(
"TRACE2." + self.log.getName() +
"._solve", log.DEBUG,
983 "Spatial kernel built with %d candidates", spatialkv.getNCandidates())
984 spatialKernel, spatialBackground = spatialkv.getSolutionPair()
987 assesskv = diffimLib.AssessSpatialKernelVisitorF(spatialKernel, spatialBackground, policy)
988 kernelCellSet.visitCandidates(assesskv, nStarPerCell)
989 nRejectedSpatial = assesskv.getNRejected()
990 nGoodSpatial = assesskv.getNGood()
991 log.log(
"TRACE1." + self.log.getName() +
"._solve", log.DEBUG,
992 "Iteration %d, rejected %d candidates due to spatial kernel fit",
993 thisIteration, nRejectedSpatial)
994 log.log(
"TRACE1." + self.log.getName() +
"._solve", log.DEBUG,
995 "%d candidates used in fit", nGoodSpatial)
998 if nGoodSpatial == 0
and nRejectedSpatial == 0:
999 raise RuntimeError(
"No kernel candidates for spatial fit")
1001 if nRejectedSpatial == 0:
1009 if (nRejectedSpatial > 0)
and (thisIteration == maxSpatialIterations):
1010 log.log(
"TRACE1." + self.log.getName() +
"._solve", log.DEBUG,
"Final spatial fit")
1011 if (usePcaForSpatialKernel):
1012 nRejectedPca, spatialBasisList = self.
_createPcaBasis(kernelCellSet, nStarPerCell, policy)
1013 regionBBox = kernelCellSet.getBBox()
1014 spatialkv = diffimLib.BuildSpatialKernelVisitorF(spatialBasisList, regionBBox, policy)
1015 kernelCellSet.visitCandidates(spatialkv, nStarPerCell)
1016 spatialkv.solveLinearEquation()
1017 log.log(
"TRACE2." + self.log.getName() +
"._solve", log.DEBUG,
1018 "Spatial kernel built with %d candidates", spatialkv.getNCandidates())
1019 spatialKernel, spatialBackground = spatialkv.getSolutionPair()
1021 spatialSolution = spatialkv.getKernelSolution()
1023 except Exception
as e:
1024 self.log.error(
"ERROR: Unable to calculate psf matching kernel")
1026 log.log(
"TRACE1." + self.log.getName() +
"._solve", log.DEBUG, str(e))
1030 log.log(
"TRACE0." + self.log.getName() +
"._solve", log.DEBUG,
1031 "Total time to compute the spatial kernel : %.2f s", (t1 - t0))
1034 self.
_displayDebug(kernelCellSet, spatialKernel, spatialBackground)
1036 self.
_diagnostic(kernelCellSet, spatialSolution, spatialKernel, spatialBackground)
1038 return spatialSolution, spatialKernel, spatialBackground
1041 PsfMatch = PsfMatchTask
def _diagnostic(self, kernelCellSet, spatialSolution, spatialKernel, spatialBg)
def __init__(self, args, kwargs)
def _createPcaBasis(self, kernelCellSet, nStarPerCell, policy)
def _displayDebug(self, kernelCellSet, spatialKernel, spatialBackground)