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
AssessSpatialKernelVisitor.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
12 #include "lsst/afw/math.h"
13 #include "lsst/afw/image.h"
14 #include "lsst/log/Log.h"
17 
21 
22 #define DEBUG_IMAGES 0
23 
24 namespace afwMath = lsst::afw::math;
25 namespace afwImage = lsst::afw::image;
26 namespace dafBase = lsst::daf::base;
28 
29 namespace lsst {
30 namespace ip {
31 namespace diffim {
32 namespace detail {
53  template<typename PixelT>
58  ) :
59  afwMath::CandidateVisitor(),
60  _spatialKernel(spatialKernel),
61  _spatialBackground(spatialBackground),
62  _ps(ps.deepCopy()),
63  _imstats(ImageStatistics<PixelT>(ps)),
64  _nGood(0),
65  _nRejected(0),
66  _nProcessed(0),
67  _useCoreStats(ps.getAsBool("useCoreStats")),
68  _coreRadius(ps.getAsInt("candidateCoreRadius"))
69  {};
70 
71  template<typename PixelT>
74  ) {
75 
76  KernelCandidate<PixelT> *kCandidate = dynamic_cast<KernelCandidate<PixelT> *>(candidate);
77  if (kCandidate == NULL) {
79  "Failed to cast SpatialCellCandidate to KernelCandidate");
80  }
81  if (!(kCandidate->isInitialized())) {
83  LOGL_DEBUG("TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
84  "Cannot process candidate %d, continuing", kCandidate->getId());
85  return;
86  }
87 
88  LOGL_DEBUG("TRACE1.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
89  "Processing candidate %d", kCandidate->getId());
90 
91  /*
92  Note - this is a hack until the Kernel API is upgraded by the
93  Davis crew. I need a "local" version of the spatially varying
94  Kernel
95  */
96  afwImage::Image<double> kImage(_spatialKernel->getDimensions());
97  double kSum = _spatialKernel->computeImage(kImage, false,
98  kCandidate->getXCenter(), kCandidate->getYCenter());
100  kernelPtr(new afwMath::FixedKernel(kImage));
101  /* </hack> */
102 
103  double background = (*_spatialBackground)(kCandidate->getXCenter(), kCandidate->getYCenter());
104 
105  MaskedImageT diffim = kCandidate->getDifferenceImage(kernelPtr, background);
106 
107  if (DEBUG_IMAGES) {
108  kImage.writeFits(str(boost::format("askv_k%d.fits") % kCandidate->getId()));
109  diffim.writeFits(str(boost::format("askv_d%d.fits") % kCandidate->getId()));
110  }
111 
112  /* Official resids */
113  try {
114  if (_useCoreStats)
115  _imstats.apply(diffim, _coreRadius);
116  else
117  _imstats.apply(diffim);
118  } catch (pexExcept::Exception& e) {
119  LOGL_DEBUG("TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
120  "Unable to calculate imstats for Candidate %d", kCandidate->getId());
122  return;
123  }
124 
125  _nProcessed += 1;
126 
127  LOGL_DEBUG("TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
128  "Chi2 = %.3f", _imstats.getVariance());
129  LOGL_DEBUG("TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
130  "X = %.2f Y = %.2f",
131  kCandidate->getXCenter(),
132  kCandidate->getYCenter());
133  LOGL_DEBUG("TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
134  "Kernel Sum = %.3f", kSum);
135  LOGL_DEBUG("TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
136  "Background = %.3f", background);
137  LOGL_DEBUG("TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
138  "Candidate %d resids = %.3f +/- %.3f sigma (%d pix)",
139  kCandidate->getId(),
140  _imstats.getMean(),
141  _imstats.getRms(),
142  _imstats.getNpix());
143 
144  bool meanIsNan = std::isnan(_imstats.getMean());
145  bool rmsIsNan = std::isnan(_imstats.getRms());
146  if (meanIsNan || rmsIsNan) {
148  LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
149  "Rejecting candidate %d, encountered NaN",
150  kCandidate->getId());
151  _nRejected += 1;
152  return;
153  }
154 
155  if (_ps->getAsBool("spatialKernelClipping")) {
156  if (fabs(_imstats.getMean()) > _ps->getAsDouble("candidateResidualMeanMax")) {
158  LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
159  "Rejecting candidate %d; bad mean residual : |%.3f| > %.3f",
160  kCandidate->getId(),
161  _imstats.getMean(),
162  _ps->getAsDouble("candidateResidualMeanMax"));
163  _nRejected += 1;
164  }
165  else if (_imstats.getRms() > _ps->getAsDouble("candidateResidualStdMax")) {
167  LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
168  "Rejecting candidate %d; bad residual rms : %.3f > %.3f",
169  kCandidate->getId(),
170  _imstats.getRms(),
171  _ps->getAsDouble("candidateResidualStdMax"));
172  _nRejected += 1;
173  }
174  else {
176  LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
177  "Spatial kernel OK");
178  _nGood += 1;
179  }
180  }
181  else {
183  LOGL_DEBUG("TRACE5.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
184  "Sigma clipping not enabled");
185  _nGood += 1;
186  }
187 
188  /* Core resids for debugging */
189  if (!(_useCoreStats)) {
190  try {
191  _imstats.apply(diffim, _coreRadius);
192  } catch (pexExcept::Exception& e) {
193  LOGL_DEBUG("TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
194  "Unable to calculate core imstats for Candidate %d",
195  kCandidate->getId());
197  return;
198  }
199  LOGL_DEBUG("TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
200  "Candidate %d core resids = %.3f +/- %.3f sigma (%d pix)",
201  kCandidate->getId(),
202  _imstats.getMean(),
203  _imstats.getRms(),
204  _imstats.getNpix());
205  }
206  }
207 
208  typedef float PixelT;
209  template class AssessSpatialKernelVisitor<PixelT>;
210 
211 }}}} // end of namespace lsst::ip::diffim::detail
lsst::afw::image::Image::writeFits
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet const >(), std::string const &mode="w") const
Write an image to a regular FITS file.
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::ip::diffim::detail::AssessSpatialKernelVisitor::processCandidate
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
Definition: AssessSpatialKernelVisitor.cc:72
lsst::afw::math::SpatialCellCandidate
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
std::shared_ptr< lsst::afw::math::LinearCombinationKernel >
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
lsst::ip::diffim::KernelCandidate
Class stored in SpatialCells for spatial Kernel fitting.
Definition: KernelCandidate.h:39
lsst::afw::math::FixedKernel
A kernel created from an Image.
Definition: Kernel.h:472
AssessSpatialKernelVisitor.h
Declaration of AssessSpatialKernelVisitor.
lsst::ip::diffim::detail::PixelT
float PixelT
Definition: AssessSpatialKernelVisitor.cc:208
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
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
PropertySet.h
lsst.pex::exceptions::LogicError
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
lsst::afw::math::SpatialCellCandidate::GOOD
@ GOOD
Definition: SpatialCell.h:72
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
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
Runtime.h
lsst::daf::base
Definition: Utils.h:47
lsst::ip::diffim::detail::AssessSpatialKernelVisitor::AssessSpatialKernelVisitor
AssessSpatialKernelVisitor(std::shared_ptr< lsst::afw::math::LinearCombinationKernel > spatialKernel, lsst::afw::math::Kernel::SpatialFunctionPtr spatialBackground, lsst::daf::base::PropertySet const &ps)
Definition: AssessSpatialKernelVisitor.cc:54
lsst::ip::diffim::detail::AssessSpatialKernelVisitor
Asseses the quality of a candidate given a spatial kernel and background model.
Definition: AssessSpatialKernelVisitor.h:26
lsst::ip::diffim::KernelCandidate::isInitialized
bool isInitialized() const
Definition: KernelCandidate.h:128
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::image::Image
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:58
lsst::afw::math::SpatialCellCandidate::getYCenter
float getYCenter() const
Return the object's row-centre.
Definition: SpatialCell.h:93
Log.h
LSST DM logging module built on log4cxx.
DEBUG_IMAGES
#define DEBUG_IMAGES
Definition: AssessSpatialKernelVisitor.cc:22