Loading [MathJax]/jax/output/HTML-CSS/config.js
LSST Applications g04a91732dc+9666464c73,g0fba68d861+079660c10e,g1fd858c14a+94f68680cf,g208c678f98+627fe8cd4e,g271391ec13+ac98094cfc,g2c84ff76c0+12036dbf49,g2c9e612ef2+a92a2e6025,g35bb328faa+fcb1d3bbc8,g4d2262a081+bcdfaf528c,g4e0f332c67+c58e4b632d,g53246c7159+fcb1d3bbc8,g60b5630c4e+a92a2e6025,g67b6fd64d1+9d1b2ab50a,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g8852436030+506db7da85,g89139ef638+9d1b2ab50a,g8d6b6b353c+a92a2e6025,g9125e01d80+fcb1d3bbc8,g989de1cb63+9d1b2ab50a,g9f33ca652e+d1749da127,ga2b97cdc51+a92a2e6025,gabe3b4be73+1e0a283bba,gb1101e3267+6ecbd0580e,gb58c049af0+f03b321e39,gb89ab40317+9d1b2ab50a,gb90eeb9370+384e1fc23b,gcf25f946ba+506db7da85,gd315a588df+382ef11c06,gd6cbbdb0b4+75aa4b1db4,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+a095917f21,ge278dab8ac+c61fbefdff,ge410e46f29+9d1b2ab50a,ge82c20c137+e12a08b75a,gf67bdafdda+9d1b2ab50a,gfd5510ef7b+df344d16e5,v29.0.0.rc2
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
BuildSingleKernelVisitor.cc
Go to the documentation of this file.
1// -*- lsst-c++ -*-
11
12#include <memory>
13#include "Eigen/Core"
14
15#include "lsst/afw/math.h"
16#include "lsst/afw/image.h"
17#include "lsst/log/Log.h"
20
24
25#define DEBUG_MATRIX 0
26
27namespace afwMath = lsst::afw::math;
28namespace afwImage = lsst::afw::image;
29namespace dafBase = lsst::daf::base;
30namespace pexExcept = lsst::pex::exceptions;
31namespace ipDiffim = lsst::ip::diffim;
32
33namespace lsst {
34namespace ip {
35namespace diffim {
36namespace detail {
37
87 template<typename PixelT>
89 lsst::afw::math::KernelList const& basisList,
92 ) :
94 _basisList(basisList),
95 _ps(ps.deepCopy()),
96 _hMat(),
97 _imstats(ImageStatistics<PixelT>(ps)),
98 _skipBuilt(true),
99 _nRejected(0),
100 _nProcessed(0),
101 _useRegularization(false),
102 _useCoreStats(ps.getAsBool("useCoreStats")),
103 _coreRadius(ps.getAsInt("candidateCoreRadius"))
104 {};
105
106 template<typename PixelT>
108 lsst::afw::math::KernelList const& basisList,
111 Eigen::MatrixXd const& hMat
112 ) :
114 _basisList(basisList),
115 _ps(ps.deepCopy()),
116 _hMat(hMat),
117 _imstats(ImageStatistics<PixelT>(ps)),
118 _skipBuilt(true),
119 _nRejected(0),
120 _nProcessed(0),
121 _useRegularization(true),
122 _useCoreStats(ps.getAsBool("useCoreStats")),
123 _coreRadius(ps.getAsInt("candidateCoreRadius"))
124 {};
125
126
127 template<typename PixelT>
130 ) {
131
133 dynamic_cast<ipDiffim::KernelCandidate<PixelT> *>(candidate);
134 if (kCandidate == NULL) {
135 LOGL_DEBUG("TRACE2.ip.diffim.BuildSingleKernelVisitor.processCandidate",
136 "Failed to cast SpatialCellCandidate to KernelCandidate %d",
137 kCandidate->getId());
138 throw LSST_EXCEPT(pexExcept::LogicError,
139 "Failed to cast SpatialCellCandidate to KernelCandidate");
140 }
141
142 if (_skipBuilt and kCandidate->isInitialized()) {
143 return;
144 }
145
146 LOGL_DEBUG("TRACE1.ip.diffim.BuildSingleKernelVisitor.processCandidate",
147 "Processing candidate %d", kCandidate->getId());
148 LOGL_DEBUG("TRACE4.ip.diffim.BuildSingleKernelVisitor.processCandidate",
149 "X = %.2f Y = %.2f",
150 kCandidate->getXCenter(),
151 kCandidate->getYCenter());
152
153 /* Build its kernel here */
154 try {
155 if (_useRegularization)
156 kCandidate->build(_basisList, _hMat);
157 else
158 kCandidate->build(_basisList);
159
160 } catch (pexExcept::Exception &e) {
162 LOGL_DEBUG("TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
163 "Unable to process candidate %d; exception caught (%s)",
164 kCandidate->getId(),
165 e.what());
166 _nRejected += 1;
167 return;
168 }
169
170 if (kCandidate->getStatus() == afwMath::SpatialCellCandidate::BAD) {
171 LOGL_DEBUG("TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
172 "Candidate %d Returned BAD upon build, exiting",
173 kCandidate->getId());
174 _nRejected += 1;
175 return;
176 }
177
178
179 /*
180 * Make diffim and set chi2 from result. Note that you need to use the
181 * most recent kernel
182 */
184 try {
185 if (_useCoreStats)
186 _imstats.apply(diffim, _coreRadius);
187 else
188 _imstats.apply(diffim);
189 } catch (pexExcept::Exception& e) {
190 LOGL_DEBUG("TRACE2.ip.diffim.BuildSingleKernelVisitor.processCandidate",
191 "Unable to calculate imstats for Candidate %d", kCandidate->getId());
193 return;
194 }
195 _nProcessed += 1;
196
197 kCandidate->setChi2(_imstats.getVariance());
198
199 /* When using a Pca basis, we don't reset the kernel or background,
200 so we need to evaluate these locally for the Trace */
201 double kSum = kCandidate->getKsum(ipDiffim::KernelCandidate<PixelT>::RECENT);
202 double background = kCandidate->getBackground(ipDiffim::KernelCandidate<PixelT>::RECENT);
203
204 LOGL_DEBUG("TRACE4.ip.diffim.BuildSingleKernelVisitor.processCandidate",
205 "Chi2 = %.3f", kCandidate->getChi2());
206 LOGL_DEBUG("TRACE4.ip.diffim.BuildSingleKernelVisitor.processCandidate",
207 "Kernel Sum = %.3f", kSum);
208 LOGL_DEBUG("TRACE4.ip.diffim.BuildSingleKernelVisitor.processCandidate",
209 "Background = %.3f", background);
210 LOGL_DEBUG("TRACE2.ip.diffim.BuildSingleKernelVisitor.processCandidate",
211 "Candidate %d resids = %.3f +/- %.3f sigma (%d pix)",
212 kCandidate->getId(),
213 _imstats.getMean(),
214 _imstats.getRms(),
215 _imstats.getNpix());
216
217 bool meanIsNan = std::isnan(_imstats.getMean());
218 bool rmsIsNan = std::isnan(_imstats.getRms());
219 if (meanIsNan || rmsIsNan) {
221 LOGL_DEBUG("TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
222 "Rejecting candidate %d, encountered NaN",
223 kCandidate->getId());
224 _nRejected += 1;
225 return;
226 }
227
228 if (_ps->getAsBool("singleKernelClipping")) {
229 if (fabs(_imstats.getMean()) > _ps->getAsDouble("candidateResidualMeanMax")) {
231 LOGL_DEBUG("TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
232 "Rejecting candidate %d; bad mean residual : |%.3f| > %.3f",
233 kCandidate->getId(),
234 _imstats.getMean(),
235 _ps->getAsDouble("candidateResidualMeanMax"));
236 _nRejected += 1;
237 }
238 else if (_imstats.getRms() > _ps->getAsDouble("candidateResidualStdMax")) {
240 LOGL_DEBUG("TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
241 "Rejecting candidate %d; bad residual rms : %.3f > %.3f",
242 kCandidate->getId(),
243 _imstats.getRms(),
244 _ps->getAsDouble("candidateResidualStdMax"));
245 _nRejected += 1;
246 }
247 else {
249 LOGL_DEBUG("TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
250 "Source kernel OK");
251 }
252 }
253 else {
255 LOGL_DEBUG("TRACE5.ip.diffim.BuildSingleKernelVisitor.processCandidate",
256 "Sigma clipping not enabled");
257 }
258
259 /* Core resids for debugging */
260 if (!(_useCoreStats)) {
261 try {
262 _imstats.apply(diffim, _coreRadius);
263 } catch (pexExcept::Exception& e) {
264 LOGL_DEBUG("TRACE2.ip.diffim.BuildSingleKernelVisitor.processCandidate",
265 "Unable to calculate core imstats for Candidate %d",
266 kCandidate->getId());
268 return;
269 }
270 LOGL_DEBUG("TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
271 "Candidate %d core resids = %.3f +/- %.3f sigma (%d pix)",
272 kCandidate->getId(),
273 _imstats.getMean(),
274 _imstats.getRms(),
275 _imstats.getNpix());
276 }
277
278 }
279
280 typedef float PixelT;
281
283
287
291 Eigen::MatrixXd const &);
292
293}}}} // end of namespace lsst::ip::diffim::detail
Declaration of BuildSingleKernelVisitor.
#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
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.
Status getStatus() const
Return the candidate's status.
double getChi2() const
Return the candidate's chi^2.
void setChi2(double chi2)
Set the candidate's chi^2.
Class for storing generic metadata.
Definition PropertySet.h:67
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.
double getBackground(CandidateSwitch cand) const
double getKsum(CandidateSwitch cand) const
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
Builds the convolution kernel for a given candidate.
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
BuildSingleKernelVisitor(lsst::afw::math::KernelList const &basisList, lsst::daf::base::PropertySet const &ps)
T isnan(T... args)
std::vector< std::shared_ptr< Kernel > > KernelList
Definition Kernel.h:462
template std::shared_ptr< BuildSingleKernelVisitor< PixelT > > makeBuildSingleKernelVisitor< PixelT >(lsst::afw::math::KernelList const &, lsst::daf::base::PropertySet const &)