Loading [MathJax]/extensions/tex2jax.js
LSST Applications g0f08755f38+05b4b46b2c,g1653933729+a905cd61c3,g168dd56ebc+a905cd61c3,g1a2382251a+526767c3b0,g20f6ffc8e0+05b4b46b2c,g217e2c1bcf+f8df405040,g28da252d5a+e530e4405a,g2bbee38e9b+e060cc3e60,g2bc492864f+e060cc3e60,g32e5bea42b+7044b77928,g347aa1857d+e060cc3e60,g35bb328faa+a905cd61c3,g3a166c0a6a+e060cc3e60,g3bd4b5ce2c+9af3f3d415,g3e281a1b8c+2bff41ced5,g3e8969e208+a905cd61c3,g414038480c+882f223820,g41af890bb2+f72d0f2eea,g43bc871e57+ad86a2d9e2,g78460c75b0+4ae99bb757,g80478fca09+8d821d1b28,g82479be7b0+ec26a56c2d,g858d7b2824+05b4b46b2c,g9125e01d80+a905cd61c3,ga5288a1d22+64e5455051,gb58c049af0+84d1b6ec45,gc28159a63d+e060cc3e60,gc5452a3dca+b82ec7cc4c,gcab2d0539d+01da5adb7a,gcf0d15dbbd+56822d21ae,gda6a2b7d83+56822d21ae,gdaeeff99f8+686ef0dd99,ge79ae78c31+e060cc3e60,gef2f8181fd+f2c81e61ee,gf0baf85859+f9edac6842,gf1e97e5484+3a635bd7af,gfa517265be+05b4b46b2c,gfa999e8aa5+d85414070d,w.2025.01
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AssessSpatialKernelVisitor.cc
Go to the documentation of this file.
1// -*- lsst-c++ -*-
12#include "lsst/afw/math.h"
13#include "lsst/afw/image.h"
14#include "lsst/log/Log.h"
17
21
22#define DEBUG_IMAGES 0
23
24namespace afwMath = lsst::afw::math;
25namespace afwImage = lsst::afw::image;
26namespace dafBase = lsst::daf::base;
28
29namespace lsst {
30namespace ip {
31namespace diffim {
32namespace detail {
53 template<typename PixelT>
58 ) :
59 afwMath::CandidateVisitor(),
60 _spatialKernel(spatialKernel),
61 _spatialBackground(spatialBackground),
62 _ps(ps.deepCopy()),
63 _imstats(ImageStatistics<PixelT>(ps)),
64 _nGood(0),
65 _nRejected(0),
66 _nProcessed(0),
67 _useCoreStats(ps.getAsBool("useCoreStats")),
68 _coreRadius(ps.getAsInt("candidateCoreRadius"))
69 {};
70
71 template<typename PixelT>
74 ) {
75
76 KernelCandidate<PixelT> *kCandidate = dynamic_cast<KernelCandidate<PixelT> *>(candidate);
77 if (kCandidate == NULL) {
79 "Failed to cast SpatialCellCandidate to KernelCandidate");
80 }
81 if (!(kCandidate->isInitialized())) {
82 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
83 LOGL_DEBUG("TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
84 "Cannot process candidate %d, continuing", kCandidate->getId());
85 return;
86 }
87
88 LOGL_DEBUG("TRACE1.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
89 "Processing candidate %d", kCandidate->getId());
90
91 /*
92 Note - this is a hack until the Kernel API is upgraded by the
93 Davis crew. I need a "local" version of the spatially varying
94 Kernel
95 */
96 afwImage::Image<double> kImage(_spatialKernel->getDimensions());
97 double kSum = _spatialKernel->computeImage(kImage, false,
98 kCandidate->getXCenter(), kCandidate->getYCenter());
100 kernelPtr(new afwMath::FixedKernel(kImage));
101 /* </hack> */
102
103 double background = (*_spatialBackground)(kCandidate->getXCenter(), kCandidate->getYCenter());
104
105 MaskedImageT diffim = kCandidate->getDifferenceImage(kernelPtr, background);
106
107 if (DEBUG_IMAGES) {
108 kImage.writeFits(str(boost::format("askv_k%d.fits") % kCandidate->getId()));
109 diffim.writeFits(str(boost::format("askv_d%d.fits") % kCandidate->getId()));
110 }
111
112 /* Official resids */
113 try {
114 if (_useCoreStats)
115 _imstats.apply(diffim, _coreRadius);
116 else
117 _imstats.apply(diffim);
118 } catch (pexExcept::Exception& e) {
119 LOGL_DEBUG("TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
120 "Unable to calculate imstats for Candidate %d", kCandidate->getId());
121 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
122 return;
123 }
124
125 _nProcessed += 1;
126
127 LOGL_DEBUG("TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
128 "Chi2 = %.3f", _imstats.getVariance());
129 LOGL_DEBUG("TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
130 "X = %.2f Y = %.2f",
131 kCandidate->getXCenter(),
132 kCandidate->getYCenter());
133 LOGL_DEBUG("TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
134 "Kernel Sum = %.3f", kSum);
135 LOGL_DEBUG("TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
136 "Background = %.3f", background);
137 LOGL_DEBUG("TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
138 "Candidate %d resids = %.3f +/- %.3f sigma (%d pix)",
139 kCandidate->getId(),
140 _imstats.getMean(),
141 _imstats.getRms(),
142 _imstats.getNpix());
143
144 bool meanIsNan = std::isnan(_imstats.getMean());
145 bool rmsIsNan = std::isnan(_imstats.getRms());
146 if (meanIsNan || rmsIsNan) {
147 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
148 LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
149 "Rejecting candidate %d, encountered NaN",
150 kCandidate->getId());
151 _nRejected += 1;
152 return;
153 }
154
155 if (_ps->getAsBool("spatialKernelClipping")) {
156 if (fabs(_imstats.getMean()) > _ps->getAsDouble("candidateResidualMeanMax")) {
157 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
158 LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
159 "Rejecting candidate %d; bad mean residual : |%.3f| > %.3f",
160 kCandidate->getId(),
161 _imstats.getMean(),
162 _ps->getAsDouble("candidateResidualMeanMax"));
163 _nRejected += 1;
164 }
165 else if (_imstats.getRms() > _ps->getAsDouble("candidateResidualStdMax")) {
166 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
167 LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
168 "Rejecting candidate %d; bad residual rms : %.3f > %.3f",
169 kCandidate->getId(),
170 _imstats.getRms(),
171 _ps->getAsDouble("candidateResidualStdMax"));
172 _nRejected += 1;
173 }
174 else {
175 kCandidate->setStatus(afwMath::SpatialCellCandidate::GOOD);
176 LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
177 "Spatial kernel OK");
178 _nGood += 1;
179 }
180 }
181 else {
182 kCandidate->setStatus(afwMath::SpatialCellCandidate::GOOD);
183 LOGL_DEBUG("TRACE5.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
184 "Sigma clipping not enabled");
185 _nGood += 1;
186 }
187
188 /* Core resids for debugging */
189 if (!(_useCoreStats)) {
190 try {
191 _imstats.apply(diffim, _coreRadius);
192 } catch (pexExcept::Exception& e) {
193 LOGL_DEBUG("TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
194 "Unable to calculate core imstats for Candidate %d",
195 kCandidate->getId());
196 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
197 return;
198 }
199 LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
200 "Candidate %d core resids = %.3f +/- %.3f sigma (%d pix)",
201 kCandidate->getId(),
202 _imstats.getMean(),
203 _imstats.getRms(),
204 _imstats.getNpix());
205 }
206 }
207
208 typedef float PixelT;
210
211}}}} // end of namespace lsst::ip::diffim::detail
Declaration of AssessSpatialKernelVisitor.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
Image Subtraction helper functions.
Class used by SpatialModelCell for spatial Kernel fitting.
LSST DM logging module built on log4cxx.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition Log.h:515
A class to represent a 2-dimensional array of pixels.
Definition Image.h:51
void writeFits(std::string const &fileName, daf::base::PropertySet const *metadata=nullptr, std::string const &mode="w") const
Write an image to a regular FITS file.
A kernel created from an Image.
Definition Kernel.h:471
Base class for candidate objects in a SpatialCell.
Definition SpatialCell.h:70
float getYCenter() const
Return the object's row-centre.
Definition SpatialCell.h:91
float getXCenter() const
Return the object's column-centre.
Definition SpatialCell.h:88
int getId() const
Return the candidate's unique ID.
void setStatus(Status status)
Set the candidate's status.
Class for storing generic metadata.
Definition PropertySet.h:66
Class to calculate difference image statistics.
Class stored in SpatialCells for spatial Kernel fitting.
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
Asseses the quality of a candidate given a spatial kernel and background model.
AssessSpatialKernelVisitor(std::shared_ptr< lsst::afw::math::LinearCombinationKernel > spatialKernel, lsst::afw::math::Kernel::SpatialFunctionPtr spatialBackground, lsst::daf::base::PropertySet const &ps)
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
Provides consistent interface for LSST exceptions.
Definition Exception.h:107
Reports errors in the logical structure of the program.
Definition Runtime.h:46
T isnan(T... args)
#define DEBUG_IMAGES