LSSTApplications  21.0.0+75b29a8a7f,21.0.0+e70536a077,21.0.0-1-ga51b5d4+62c747d40b,21.0.0-11-ga6ea59e8e+47cba9fc36,21.0.0-2-g103fe59+914993bf7c,21.0.0-2-g1367e85+e2614ded12,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g4bc9b9f+7b2b5f8678,21.0.0-2-g5242d73+e2614ded12,21.0.0-2-g54e2caa+6403186824,21.0.0-2-g7f82c8f+3ac4acbffc,21.0.0-2-g8dde007+04a6aea1af,21.0.0-2-g8f08a60+9402881886,21.0.0-2-ga326454+3ac4acbffc,21.0.0-2-ga63a54e+81dd751046,21.0.0-2-gc738bc1+5f65c6e7a9,21.0.0-2-gde069b7+26c92b3210,21.0.0-2-gecfae73+0993ddc9bd,21.0.0-2-gfc62afb+e2614ded12,21.0.0-21-gba890a8+5a4f502a26,21.0.0-23-g9966ff26+03098d1af8,21.0.0-3-g357aad2+8ad216c477,21.0.0-3-g4be5c26+e2614ded12,21.0.0-3-g6d51c4a+4d2fe0280d,21.0.0-3-g7d9da8d+75b29a8a7f,21.0.0-3-gaa929c8+522e0f12c2,21.0.0-3-ge02ed75+4d2fe0280d,21.0.0-4-g3300ddd+e70536a077,21.0.0-4-gc004bbf+eac6615e82,21.0.0-4-gccdca77+f94adcd104,21.0.0-4-gd1c1571+18b81799f9,21.0.0-5-g7b47fff+4d2fe0280d,21.0.0-5-gb155db7+d2632f662b,21.0.0-5-gdf36809+637e4641ee,21.0.0-6-g722ad07+28c848f42a,21.0.0-7-g959bb79+522e0f12c2,21.0.0-7-gfd72ab2+cf01990774,21.0.0-9-g87fb7b8d+e2ab11cdd6,w.2021.04
LSSTDataManagementBasePackage
KernelSumVisitor.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
11 #include <limits>
12 
13 #include "lsst/afw/math.h"
14 #include "lsst/log/Log.h"
17 
20 
21 namespace afwMath = lsst::afw::math;
22 namespace dafBase = lsst::daf::base;
24 
25 namespace lsst {
26 namespace ip {
27 namespace diffim {
28 namespace detail {
29 
64  template<typename PixelT>
67  ) :
68  afwMath::CandidateVisitor(),
69  _mode(AGGREGATE),
70  _kSums(std::vector<double>()),
71  _kSumMean(0.),
72  _kSumStd(0.),
73  _dkSumMax(0.),
74  _kSumNpts(0),
75  _nRejected(0),
76  _ps(ps.deepCopy())
77  {};
78 
79  template<typename PixelT>
81  _kSums.clear();
82  _kSumMean = 0.;
83  _kSumStd = 0.;
84  _dkSumMax = 0.;
85  _kSumNpts = 0;
86  _nRejected = 0;
87  }
88 
89  template<typename PixelT>
91  *candidate) {
92 
93  KernelCandidate<PixelT> *kCandidate = dynamic_cast<KernelCandidate<PixelT> *>(candidate);
94  if (kCandidate == NULL) {
96  "Failed to cast SpatialCellCandidate to KernelCandidate");
97  }
98  LOGL_DEBUG("TRACE5.ip.diffim.KernelSumVisitor.processCandidate",
99  "Processing candidate %d, mode %d", kCandidate->getId(), _mode);
100 
101  /* Grab all kernel sums and look for outliers */
102  if (_mode == AGGREGATE) {
103  _kSums.push_back(kCandidate->getKernelSolution(KernelCandidate<PixelT>::ORIG)->getKsum());
104  }
105  else if (_mode == REJECT) {
106  if (_ps->getAsBool("kernelSumClipping")) {
107  double kSum =
108  kCandidate->getKernelSolution(KernelCandidate<PixelT>::ORIG)->getKsum();
109 
110  if (fabs(kSum - _kSumMean) > _dkSumMax) {
112  LOGL_DEBUG("TRACE3.ip.diffim.KernelSumVisitor.processCandidate",
113  "Rejecting candidate %d; bad source kernel sum : (%.2f)",
114  kCandidate->getId(),
115  kSum);
116  _nRejected += 1;
117  }
118  }
119  else {
120  LOGL_DEBUG("TRACE5.ip.diffim.KernelSumVisitor.processCandidate",
121  "Sigma clipping not enabled");
122  }
123  }
124  }
125 
126  template<typename PixelT>
128  if (_kSums.size() == 0) {
130  "Unable to determine kernel sum; 0 candidates");
131  }
132  else if (_kSums.size() == 1) {
133  LOGL_DEBUG("TRACE1.ip.diffim.KernelSumVisitor.processKsumDistribution",
134  "WARNING: only 1 kernel candidate");
135 
136  _kSumMean = _kSums[0];
137  _kSumStd = 0.0;
138  _kSumNpts = 1;
139  }
140  else {
141  try {
146  _kSumMean = stats.getValue(afwMath::MEANCLIP);
147  _kSumStd = stats.getValue(afwMath::STDEVCLIP);
148  _kSumNpts = static_cast<int>(stats.getValue(afwMath::NPOINT));
149  } catch (pexExcept::Exception &e) {
150  LSST_EXCEPT_ADD(e, "Unable to calculate kernel sum statistics");
151  throw e;
152  }
153  if (std::isnan(_kSumMean)) {
155  str(boost::format("Mean kernel sum returns NaN (%d points)")
156  % _kSumNpts));
157  }
158  if (std::isnan(_kSumStd)) {
160  str(boost::format("Kernel sum stdev returns NaN (%d points)")
161  % _kSumNpts));
162  }
163  }
164  _dkSumMax = _ps->getAsDouble("maxKsumSigma") * _kSumStd;
165  LOGL_DEBUG("TRACE1.ip.diffim.KernelSumVisitor.processCandidate",
166  "Kernel Sum Distribution : %.3f +/- %.3f (%d points)",
167  _kSumMean, _kSumStd, _kSumNpts);
168  }
169 
170  typedef float PixelT;
171 
172  template class KernelSumVisitor<PixelT>;
173 
176 
177 }}}} // end of namespace lsst::ip::diffim::detail
lsst::ip::diffim::detail::KernelSumVisitor::KernelSumVisitor
KernelSumVisitor(lsst::daf::base::PropertySet const &ps)
Definition: KernelSumVisitor.cc:65
lsst::afw::math::SpatialCellCandidate
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
std::shared_ptr
STL class.
lsst::afw::math::SpatialCellCandidate::setStatus
void setStatus(Status status)
Set the candidate's status.
Definition: SpatialCell.cc:54
LSST_EXCEPT_ADD
#define LSST_EXCEPT_ADD(e, m)
Add the current location and a message to an existing exception before rethrowing it.
Definition: Exception.h:54
lsst::ip::diffim::KernelCandidate
Class stored in SpatialCells for spatial Kernel fitting.
Definition: KernelCandidate.h:39
lsst::ip::diffim::detail::PixelT
float PixelT
Definition: AssessSpatialKernelVisitor.cc:208
lsst::ip::diffim::detail::makeKernelSumVisitor< PixelT >
template std::shared_ptr< KernelSumVisitor< PixelT > > makeKernelSumVisitor< PixelT >(lsst::daf::base::PropertySet const &)
lsst::ip::diffim::detail::KernelSumVisitor::processCandidate
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
Definition: KernelSumVisitor.cc:90
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw::math::Statistics::getValue
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1056
lsst::afw::math::makeStatistics
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:354
math.h
lsst::afw::math::MEANCLIP
@ MEANCLIP
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
lsst::afw::math::SpatialCellCandidate::BAD
@ BAD
Definition: SpatialCell.h:72
std::isnan
T isnan(T... args)
KernelSumVisitor.h
Declaration of KernelSumVisitor.
lsst::ip::diffim::detail::KernelSumVisitor::processKsumDistribution
void processKsumDistribution()
Definition: KernelSumVisitor.cc:127
lsst::afw::math::STDEVCLIP
@ STDEVCLIP
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
PropertySet.h
lsst.pex::exceptions::LogicError
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
lsst.pipe.tasks.cli.cmd.commands.str
str
Definition: commands.py:50
lsst::afw::math::SpatialCellCandidate::getId
int getId() const
Return the candidate's unique ID.
Definition: SpatialCell.h:104
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::ip::diffim::KernelCandidate::getKernelSolution
std::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
Definition: KernelCandidate.cc:322
Runtime.h
lsst::daf::base
Definition: Utils.h:47
lsst::afw::math::NPOINT
@ NPOINT
number of sample points
Definition: Statistics.h:66
LOGL_DEBUG
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:504
std
STL namespace.
lsst::afw::math
Definition: statistics.dox:6
KernelCandidate.h
Class used by SpatialModelCell for spatial Kernel fitting.
lsst::ip::diffim::detail::KernelSumVisitor
A class to accumulate kernel sums across SpatialCells.
Definition: KernelSumVisitor.h:27
lsst::daf::base::PropertySet
Class for storing generic metadata.
Definition: PropertySet.h:67
lsst::afw::math::Statistics
A class to evaluate image statistics.
Definition: Statistics.h:215
lsst.pex::exceptions
Definition: Exception.h:37
lsst.pex::exceptions::Exception
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
Log.h
LSST DM logging module built on log4cxx.
lsst::ip::diffim::detail::KernelSumVisitor::resetKernelSum
void resetKernelSum()
Definition: KernelSumVisitor.cc:80