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
BuildSingleKernelVisitor.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
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 
27 namespace afwMath = lsst::afw::math;
28 namespace afwImage = lsst::afw::image;
29 namespace dafBase = lsst::daf::base;
31 namespace ipDiffim = lsst::ip::diffim;
32 
33 namespace lsst {
34 namespace ip {
35 namespace diffim {
36 namespace detail {
37 
87  template<typename PixelT>
89  lsst::afw::math::KernelList const& basisList,
92  ) :
93  afwMath::CandidateVisitor(),
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,
109  lsst::daf::base::PropertySet const& ps,
111  Eigen::MatrixXd const& hMat
112  ) :
113  afwMath::CandidateVisitor(),
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());
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);
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 
282  template class BuildSingleKernelVisitor<PixelT>;
283 
287 
291  Eigen::MatrixXd const &);
292 
293 }}}} // end of namespace lsst::ip::diffim::detail
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::ip::diffim
Definition: AssessSpatialKernelVisitor.h:22
lsst::afw::math::SpatialCellCandidate
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
lsst::ip::diffim::detail::makeBuildSingleKernelVisitor< PixelT >
template std::shared_ptr< BuildSingleKernelVisitor< PixelT > > makeBuildSingleKernelVisitor< PixelT >(lsst::afw::math::KernelList const &, lsst::daf::base::PropertySet const &)
lsst::ip::diffim::KernelCandidate::build
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
Definition: KernelCandidate.cc:87
std::shared_ptr
STL class.
lsst::afw::math::SpatialCellCandidate::setStatus
void setStatus(Status status)
Set the candidate's status.
Definition: SpatialCell.cc:54
lsst::ip::diffim::ImageStatistics
Class to calculate difference image statistics.
Definition: ImageStatistics.h:59
std::vector< std::shared_ptr< Kernel > >
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::BuildSingleKernelVisitor::BuildSingleKernelVisitor
BuildSingleKernelVisitor(lsst::afw::math::KernelList const &basisList, lsst::daf::base::PropertySet const &ps)
Definition: BuildSingleKernelVisitor.cc:88
lsst::ip::diffim::KernelCandidate::getDifferenceImage
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
Definition: KernelCandidate.cc:347
math.h
lsst::afw::math::SpatialCellCandidate::BAD
@ BAD
Definition: SpatialCell.h:72
std::isnan
T isnan(T... args)
lsst::afw::math::SpatialCellCandidate::getXCenter
float getXCenter() const
Return the object's column-centre.
Definition: SpatialCell.h:90
lsst::afw::image::MaskedImage< PixelT >
lsst.pipe.drivers.visualizeVisit.background
background
Definition: visualizeVisit.py:37
image
afw::table::Key< afw::table::Array< ImagePixelT > > image
Definition: HeavyFootprint.cc:216
lsst::ip::diffim::detail::BuildSingleKernelVisitor
Builds the convolution kernel for a given candidate.
Definition: BuildSingleKernelVisitor.h:30
PropertySet.h
lsst.pex::exceptions::LogicError
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
lsst::ip::diffim::detail::BuildSingleKernelVisitor::processCandidate
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
Definition: BuildSingleKernelVisitor.cc:128
lsst::afw::math::SpatialCellCandidate::GOOD
@ GOOD
Definition: SpatialCell.h:72
lsst::afw::math::SpatialCellImageCandidate::setChi2
void setChi2(double chi2)
Set the candidate's chi^2.
Definition: SpatialCell.h:152
lsst::afw::math::SpatialCellCandidate::getId
int getId() const
Return the candidate's unique ID.
Definition: SpatialCell.h:104
image.h
ImageSubtract.h
Image Subtraction helper functions.
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
BuildSingleKernelVisitor.h
Declaration of BuildSingleKernelVisitor.
Runtime.h
lsst::ip::diffim::KernelCandidate::getBackground
double getBackground(CandidateSwitch cand) const
Definition: KernelCandidate.cc:244
lsst::daf::base
Definition: Utils.h:47
lsst::ip::diffim::KernelCandidate::isInitialized
bool isInitialized() const
Definition: KernelCandidate.h:128
lsst::afw::math::SpatialCellCandidate::getStatus
Status getStatus() const
Return the candidate's status.
Definition: SpatialCell.h:106
LOGL_DEBUG
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:504
lsst::afw::math
Definition: statistics.dox:6
KernelCandidate.h
Class used by SpatialModelCell for spatial Kernel fitting.
lsst::daf::base::PropertySet
Class for storing generic metadata.
Definition: PropertySet.h:67
lsst.pex::exceptions
Definition: Exception.h:37
lsst.pex::exceptions::Exception
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
lsst::afw::math::SpatialCellCandidate::getYCenter
float getYCenter() const
Return the object's row-centre.
Definition: SpatialCell.h:93
lsst.pex::exceptions::Exception::what
virtual char const * what(void) const noexcept
Return a character string summarizing this exception.
Definition: Exception.cc:99
Log.h
LSST DM logging module built on log4cxx.
lsst::afw::math::SpatialCellImageCandidate::getChi2
double getChi2() const
Return the candidate's chi^2.
Definition: SpatialCell.h:150
lsst::ip::diffim::KernelCandidate::getKsum
double getKsum(CandidateSwitch cand) const
Definition: KernelCandidate.cc:268