LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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/pex/policy/Policy.h"
16 #include "lsst/pex/logging/Trace.h"
17 
20 
21 namespace afwMath = lsst::afw::math;
22 namespace pexLogging = lsst::pex::logging;
23 namespace pexPolicy = lsst::pex::policy;
24 namespace pexExcept = lsst::pex::exceptions;
25 
26 namespace lsst {
27 namespace ip {
28 namespace diffim {
29 namespace detail {
30 
65  template<typename PixelT>
67  lsst::pex::policy::Policy const& policy
68  ) :
69  afwMath::CandidateVisitor(),
70  _mode(AGGREGATE),
71  _kSums(std::vector<double>()),
72  _kSumMean(0.),
73  _kSumStd(0.),
74  _dkSumMax(0.),
75  _kSumNpts(0),
76  _nRejected(0),
77  _policy(policy)
78  {};
79 
80  template<typename PixelT>
82  _kSums.clear();
83  _kSumMean = 0.;
84  _kSumStd = 0.;
85  _dkSumMax = 0.;
86  _kSumNpts = 0;
87  _nRejected = 0;
88  }
89 
90  template<typename PixelT>
92  *candidate) {
93 
94  KernelCandidate<PixelT> *kCandidate = dynamic_cast<KernelCandidate<PixelT> *>(candidate);
95  if (kCandidate == NULL) {
96  throw LSST_EXCEPT(pexExcept::LogicError,
97  "Failed to cast SpatialCellCandidate to KernelCandidate");
98  }
99  pexLogging::TTrace<6>("lsst.ip.diffim.KernelSumVisitor.processCandidate",
100  "Processing candidate %d, mode %d", kCandidate->getId(), _mode);
101 
102  /* Grab all kernel sums and look for outliers */
103  if (_mode == AGGREGATE) {
104  _kSums.push_back(kCandidate->getKernelSolution(KernelCandidate<PixelT>::ORIG)->getKsum());
105  }
106  else if (_mode == REJECT) {
107  if (_policy.getBool("kernelSumClipping")) {
108  double kSum =
109  kCandidate->getKernelSolution(KernelCandidate<PixelT>::ORIG)->getKsum();
110 
111  if (fabs(kSum - _kSumMean) > _dkSumMax) {
113  pexLogging::TTrace<4>("lsst.ip.diffim.KernelSumVisitor.processCandidate",
114  "Rejecting candidate %d; bad source kernel sum : (%.2f)",
115  kCandidate->getId(),
116  kSum);
117  _nRejected += 1;
118  }
119  }
120  else {
121  pexLogging::TTrace<6>("lsst.ip.diffim.KernelSumVisitor.processCandidate",
122  "Sigma clipping not enabled");
123  }
124  }
125  }
126 
127  template<typename PixelT>
129  if (_kSums.size() == 0) {
131  "Unable to determine kernel sum; 0 candidates");
132  }
133  else if (_kSums.size() == 1) {
134  pexLogging::TTrace<2>("lsst.ip.diffim.KernelSumVisitor.processKsumDistribution",
135  "WARNING: only 1 kernel candidate");
136 
137  _kSumMean = _kSums[0];
138  _kSumStd = 0.0;
139  _kSumNpts = 1;
140  }
141  else {
142  try {
144  afwMath::NPOINT |
147  _kSumMean = stats.getValue(afwMath::MEANCLIP);
148  _kSumStd = stats.getValue(afwMath::STDEVCLIP);
149  _kSumNpts = static_cast<int>(stats.getValue(afwMath::NPOINT));
150  } catch (pexExcept::Exception &e) {
151  LSST_EXCEPT_ADD(e, "Unable to calculate kernel sum statistics");
152  throw e;
153  }
154  if (std::isnan(_kSumMean)) {
156  str(boost::format("Mean kernel sum returns NaN (%d points)")
157  % _kSumNpts));
158  }
159  if (std::isnan(_kSumStd)) {
161  str(boost::format("Kernel sum stdev returns NaN (%d points)")
162  % _kSumNpts));
163  }
164  }
165  _dkSumMax = _policy.getDouble("maxKsumSigma") * _kSumStd;
166  pexLogging::TTrace<2>("lsst.ip.diffim.KernelSumVisitor.processCandidate",
167  "Kernel Sum Distribution : %.3f +/- %.3f (%d points)",
168  _kSumMean, _kSumStd, _kSumNpts);
169  }
170 
171  typedef float PixelT;
172 
173  template class KernelSumVisitor<PixelT>;
174 
175  template boost::shared_ptr<KernelSumVisitor<PixelT> >
177 
178 }}}} // end of namespace lsst::ip::diffim::detail
An include file to include the public header files for lsst::afw::math.
void setStatus(Status status)
Set the candidate&#39;s status.
Definition: SpatialCell.cc:61
Class stored in SpatialCells for spatial Kernel fitting.
Implementation of KernelSumVisitor.
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
definition of the Trace messaging facilities
int isnan(T t)
Definition: ieee.h:110
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1009
A class to accumulate kernel sums across SpatialCells.
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
int getId() const
Return the candidate&#39;s unique ID.
Definition: SpatialCell.h:109
number of sample points
Definition: Statistics.h:66
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
Class used by SpatialModelCell for spatial Kernel fitting.
boost::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
template boost::shared_ptr< KernelSumVisitor< PixelT > > makeKernelSumVisitor< PixelT >(lsst::pex::policy::Policy const &)
KernelSumVisitor(lsst::pex::policy::Policy const &policy)
#define LSST_EXCEPT_ADD(e, m)
Definition: Exception.h:51