LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Public Types | Public Member Functions | List of all members
lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT > Class Template Reference

Builds the convolution kernel for a given candidate. More...

#include <BuildSingleKernelVisitor.h>

Inheritance diagram for lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >:
lsst::afw::math::CandidateVisitor

Public Types

typedef std::shared_ptr< BuildSingleKernelVisitor< PixelT > > Ptr
 

Public Member Functions

 BuildSingleKernelVisitor (lsst::afw::math::KernelList const &basisList, lsst::daf::base::PropertySet const &ps)
 
 BuildSingleKernelVisitor (lsst::afw::math::KernelList const &basisList, lsst::daf::base::PropertySet const &ps, Eigen::MatrixXd const &hMat)
 
virtual ~BuildSingleKernelVisitor ()
 
void setSkipBuilt (bool skip)
 
int getNRejected ()
 
int getNProcessed ()
 
void reset ()
 
void processCandidate (lsst::afw::math::SpatialCellCandidate *candidate)
 

Detailed Description

template<typename PixelT>
class lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >

Builds the convolution kernel for a given candidate.

std::shared_ptr<PropertySet> ps(new PropertySet);
ps->set("constantVarianceWeighting", false);
ps->set("iterateSingleKernel", false);
ps->set("singleKernelClipping", true);
ps->set("candidateResidualMeanMax", 0.25);
ps->set("candidateResidualStdMax", 1.25);
detail::BuildSingleKernelVisitor<PixelT> singleKernelFitter(*ps);
int nRejected = -1;
while (nRejected != 0) {
singleKernelFitter.reset();
kernelCells.visitCandidates(&singleKernelFitter, nStarPerCell);
nRejected = singleKernelFitter.getNRejected();
}
Note
Visits each current candidate in a afwMath::SpatialCellSet, and builds its kernel using its build() method. We don't build the kernel for every candidate since this is computationally expensive, only when its the current candidate in the cell. During the course of building the kernel, it also assesses the quality of the difference image. If it is determined to be bad (based on the PropertySet paramters) the candidate is flagged as afwMath::SpatialCellCandidate::BAD; otherwise its marked as afwMath::SpatialCellCandidate::GOOD. Keeps a running sample of all the new candidates it visited that turned out to be bad.
Because this visitor does not have access to the next candidate in the cell, it must be called iteratively until no candidates are rejected. This ensures that the current candidate of every cell has an initialized Kernel. This also requires that this class re-Visit all the cells after any other Visitors with the ability to mark something as BAD.
Because we are frequently re-Visiting entirely GOOD candidates during these iterations, the option of _skipBuilt=true will enable the user to not rebuilt the kernel on every visit.
For the particular use case of creating a Pca basis from the raw kernels, we want to re-Visit each candidate and re-fit the kernel using this Pca basis. This requires the user to setSkipBuilt(false) so that the candidate is reprocessed with this new basis.

Definition at line 30 of file BuildSingleKernelVisitor.h.

Member Typedef Documentation

◆ Ptr

Definition at line 33 of file BuildSingleKernelVisitor.h.

Constructor & Destructor Documentation

◆ BuildSingleKernelVisitor() [1/2]

Parameters
basisListList of basis kernels for resulting LinearCombinationKernel
psps file directing behavior

Definition at line 88 of file BuildSingleKernelVisitor.cc.

92  :
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  {};

◆ BuildSingleKernelVisitor() [2/2]

template<typename PixelT >
lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::BuildSingleKernelVisitor ( lsst::afw::math::KernelList const &  basisList,
lsst::daf::base::PropertySet const &  ps,
Eigen::MatrixXd const &  hMat 
)
Parameters
basisListList of basis kernels for resulting LinearCombinationKernel
psps file directing behavior
hMatRegularization matrix

Definition at line 107 of file BuildSingleKernelVisitor.cc.

112  :
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  {};

◆ ~BuildSingleKernelVisitor()

template<typename PixelT >
virtual lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::~BuildSingleKernelVisitor ( )
inlinevirtual

Definition at line 44 of file BuildSingleKernelVisitor.h.

44 {};

Member Function Documentation

◆ getNProcessed()

template<typename PixelT >
int lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::getNProcessed ( )
inline

Definition at line 56 of file BuildSingleKernelVisitor.h.

56 {return _nProcessed;}

◆ getNRejected()

template<typename PixelT >
int lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::getNRejected ( )
inline

Definition at line 55 of file BuildSingleKernelVisitor.h.

55 {return _nRejected;}

◆ processCandidate()

template<typename PixelT >
void lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::processCandidate ( lsst::afw::math::SpatialCellCandidate candidate)
virtual

Reimplemented from lsst::afw::math::CandidateVisitor.

Definition at line 128 of file BuildSingleKernelVisitor.cc.

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  */
183  MaskedImageT diffim = kCandidate->getDifferenceImage(ipDiffim::KernelCandidate<PixelT>::RECENT);
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  }
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:515
float getYCenter() const
Return the object's row-centre.
Definition: SpatialCell.h:91
float getXCenter() const
Return the object's column-centre.
Definition: SpatialCell.h:88
int getId() const
Return the candidate's unique ID.
Definition: SpatialCell.h:102
void setStatus(Status status)
Set the candidate's status.
Definition: SpatialCell.cc:53
Status getStatus() const
Return the candidate's status.
Definition: SpatialCell.h:104
double getChi2() const
Return the candidate's chi^2.
Definition: SpatialCell.h:146
void setChi2(double chi2)
Set the candidate's chi^2.
Definition: SpatialCell.h:148
Class stored in SpatialCells for spatial Kernel fitting.
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
double getBackground(CandidateSwitch cand) const
double getKsum(CandidateSwitch cand) const
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
virtual char const * what(void) const noexcept
Return a character string summarizing this exception.
Definition: Exception.cc:99
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
T fabs(T... args)
T isnan(T... args)

◆ reset()

template<typename PixelT >
void lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::reset ( )
inlinevirtual

Reimplemented from lsst::afw::math::CandidateVisitor.

Definition at line 57 of file BuildSingleKernelVisitor.h.

57 {_nRejected = 0; _nProcessed = 0;}

◆ setSkipBuilt()

template<typename PixelT >
void lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::setSkipBuilt ( bool  skip)
inline

Definition at line 53 of file BuildSingleKernelVisitor.h.

53 {_skipBuilt = skip;}

The documentation for this class was generated from the following files: