15 #include "lsst/afw/math.h" 16 #include "lsst/afw/image.h" 17 #include "lsst/log/Log.h" 18 #include "lsst/pex/exceptions/Runtime.h" 19 #include "lsst/pex/policy/Policy.h" 25 #define DEBUG_MATRIX 0 27 namespace afwMath = lsst::afw::math;
28 namespace afwImage = lsst::afw::image;
29 namespace pexPolicy = lsst::pex::policy;
30 namespace pexExcept = lsst::pex::exceptions;
87 template<
typename PixelT>
89 lsst::afw::math::KernelList
const& basisList,
90 lsst::pex::policy::Policy
const& policy
93 afwMath::CandidateVisitor(),
94 _basisList(basisList),
101 _useRegularization(false),
102 _useCoreStats(_policy.getBool(
"useCoreStats")),
103 _coreRadius(_policy.getInt(
"candidateCoreRadius"))
106 template<
typename PixelT>
108 lsst::afw::math::KernelList
const& basisList,
109 lsst::pex::policy::Policy
const& policy,
111 Eigen::MatrixXd
const& hMat
113 afwMath::CandidateVisitor(),
127 template<
typename PixelT>
129 lsst::afw::math::SpatialCellCandidate *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");
146 LOGL_DEBUG(
"TRACE1.ip.diffim.BuildSingleKernelVisitor.processCandidate",
147 "Processing candidate %d", kCandidate->getId());
148 LOGL_DEBUG(
"TRACE4.ip.diffim.BuildSingleKernelVisitor.processCandidate",
150 kCandidate->getXCenter(),
151 kCandidate->getYCenter());
160 }
catch (pexExcept::Exception &e) {
161 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
162 LOGL_DEBUG(
"TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
163 "Unable to process candidate %d; exception caught (%s)",
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());
189 }
catch (pexExcept::Exception& e) {
190 LOGL_DEBUG(
"TRACE2.ip.diffim.BuildSingleKernelVisitor.processCandidate",
191 "Unable to calculate imstats for Candidate %d", kCandidate->getId());
192 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
197 kCandidate->setChi2(
_imstats.getVariance());
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)",
217 bool meanIsNan = std::isnan(
_imstats.getMean());
218 bool rmsIsNan = std::isnan(
_imstats.getRms());
219 if (meanIsNan || rmsIsNan) {
220 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
221 LOGL_DEBUG(
"TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
222 "Rejecting candidate %d, encountered NaN",
223 kCandidate->getId());
228 if (
_policy.getBool(
"singleKernelClipping")) {
229 if (fabs(
_imstats.getMean()) >
_policy.getDouble(
"candidateResidualMeanMax")) {
230 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
231 LOGL_DEBUG(
"TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
232 "Rejecting candidate %d; bad mean residual : |%.3f| > %.3f",
235 _policy.getDouble(
"candidateResidualMeanMax"));
238 else if (
_imstats.getRms() >
_policy.getDouble(
"candidateResidualStdMax")) {
239 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
240 LOGL_DEBUG(
"TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
241 "Rejecting candidate %d; bad residual rms : %.3f > %.3f",
244 _policy.getDouble(
"candidateResidualStdMax"));
248 kCandidate->setStatus(afwMath::SpatialCellCandidate::GOOD);
249 LOGL_DEBUG(
"TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
254 kCandidate->setStatus(afwMath::SpatialCellCandidate::GOOD);
255 LOGL_DEBUG(
"TRACE5.ip.diffim.BuildSingleKernelVisitor.processCandidate",
256 "Sigma clipping not enabled");
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());
267 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
270 LOGL_DEBUG(
"TRACE3.ip.diffim.BuildSingleKernelVisitor.processCandidate",
271 "Candidate %d core resids = %.3f +/- %.3f sigma (%d pix)",
284 template std::shared_ptr<BuildSingleKernelVisitor<PixelT> >
286 lsst::pex::policy::Policy
const&);
288 template std::shared_ptr<BuildSingleKernelVisitor<PixelT> >
290 lsst::pex::policy::Policy
const&,
291 Eigen::MatrixXd
const &);
lsst::afw::math::KernelList const _basisList
Basis set.
ImageStatistics< PixelT > _imstats
To calculate statistics of difference image.
bool _useCoreStats
Extracted from _policy.
double getBackground(CandidateSwitch cand) const
Class stored in SpatialCells for spatial Kernel fitting.
bool _useRegularization
Regularize if delta function basis.
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
lsst::pex::policy::Policy _policy
Policy controlling behavior.
bool _skipBuilt
Skip over built candidates during processCandidate()
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
Eigen::MatrixXd const _hMat
Regularization matrix.
Builds the convolution kernel for a given candidate.
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
template std::shared_ptr< BuildSingleKernelVisitor< PixelT > > makeBuildSingleKernelVisitor< PixelT >(lsst::afw::math::KernelList const &, lsst::pex::policy::Policy const &)
lsst::afw::image::MaskedImage< PixelT > MaskedImageT
bool isInitialized() const
BuildSingleKernelVisitor(lsst::afw::math::KernelList const &basisList, lsst::pex::policy::Policy const &policy)
int _coreRadius
Extracted from _policy.
Class used by SpatialModelCell for spatial Kernel fitting.
Class to calculate difference image statistics.
int _nRejected
Number of candidates rejected during processCandidate()
Declaration of BuildSingleKernelVisitor.
Image Subtraction helper functions.
double getKsum(CandidateSwitch cand) const
int _nProcessed
Number of candidates processed during processCandidate()