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
Public Types | Public Member Functions | Private Types | Private Attributes | 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 boost::shared_ptr
< BuildSingleKernelVisitor
< PixelT > > 
Ptr
 

Public Member Functions

 BuildSingleKernelVisitor (lsst::afw::math::KernelList const &basisList, lsst::pex::policy::Policy const &policy)
 
 BuildSingleKernelVisitor (lsst::afw::math::KernelList const &basisList, lsst::pex::policy::Policy const &policy, boost::shared_ptr< Eigen::MatrixXd > hMat)
 
virtual ~BuildSingleKernelVisitor ()
 
void setSkipBuilt (bool skip)
 
int getNRejected ()
 
int getNProcessed ()
 
void reset ()
 
void processCandidate (lsst::afw::math::SpatialCellCandidate *candidate)
 
- Public Member Functions inherited from lsst::afw::math::CandidateVisitor
 CandidateVisitor ()
 
virtual ~CandidateVisitor ()
 

Private Types

typedef
lsst::afw::image::MaskedImage
< PixelT
MaskedImageT
 

Private Attributes

lsst::afw::math::KernelList const _basisList
 Basis set. More...
 
lsst::pex::policy::Policy _policy
 Policy controlling behavior. More...
 
boost::shared_ptr
< Eigen::MatrixXd > 
_hMat
 Regularization matrix. More...
 
ImageStatistics< PixelT_imstats
 To calculate statistics of difference image. More...
 
bool _skipBuilt
 Skip over built candidates during processCandidate() More...
 
int _nRejected
 Number of candidates rejected during processCandidate() More...
 
int _nProcessed
 Number of candidates processed during processCandidate() More...
 
bool _useRegularization
 Regularize if delta function basis. More...
 
bool _useCoreStats
 Extracted from _policy. More...
 
int _coreRadius
 Extracted from _policy. More...
 

Detailed Description

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

Builds the convolution kernel for a given candidate.

Policy::Ptr policy(new Policy);
policy->set("constantVarianceWeighting", false);
policy->set("iterateSingleKernel", false);
policy->set("singleKernelClipping", true);
policy->set("candidateResidualMeanMax", 0.25);
policy->set("candidateResidualStdMax", 1.25);
detail::BuildSingleKernelVisitor<PixelT> singleKernelFitter(*policy);
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 Policy 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

Definition at line 31 of file BuildSingleKernelVisitor.h.

template<typename PixelT >
typedef boost::shared_ptr<BuildSingleKernelVisitor<PixelT> > lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::Ptr

Definition at line 33 of file BuildSingleKernelVisitor.h.

Constructor & Destructor Documentation

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

Definition at line 90 of file BuildSingleKernelVisitor.cc.

94  :
96  _basisList(basisList),
97  _policy(policy),
98  _hMat(),
99  _imstats(ImageStatistics<PixelT>(_policy)),
100  _skipBuilt(true),
101  _nRejected(0),
102  _nProcessed(0),
103  _useRegularization(false),
104  _useCoreStats(_policy.getBool("useCoreStats")),
105  _coreRadius(_policy.getInt("candidateCoreRadius"))
106  {};
lsst::afw::math::KernelList const _basisList
Basis set.
ImageStatistics< PixelT > _imstats
To calculate statistics of difference image.
int getInt(const std::string &name) const
Definition: Policy.h:603
boost::shared_ptr< Eigen::MatrixXd > _hMat
Regularization matrix.
bool _useRegularization
Regularize if delta function basis.
bool getBool(const std::string &name) const
Definition: Policy.h:589
lsst::pex::policy::Policy _policy
Policy controlling behavior.
bool _skipBuilt
Skip over built candidates during processCandidate()
int _nRejected
Number of candidates rejected during processCandidate()
int _nProcessed
Number of candidates processed during processCandidate()
template<typename PixelT >
lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::BuildSingleKernelVisitor ( lsst::afw::math::KernelList const &  basisList,
lsst::pex::policy::Policy const &  policy,
boost::shared_ptr< Eigen::MatrixXd >  hMat 
)
Parameters
basisListList of basis kernels for resulting LinearCombinationKernel
policyPolicy file directing behavior
hMatRegularization matrix

Definition at line 109 of file BuildSingleKernelVisitor.cc.

114  :
116  _basisList(basisList),
117  _policy(policy),
118  _hMat(hMat),
119  _imstats(ImageStatistics<PixelT>(_policy)),
120  _skipBuilt(true),
121  _nRejected(0),
122  _nProcessed(0),
123  _useRegularization(true),
124  _useCoreStats(_policy.getBool("useCoreStats")),
125  _coreRadius(_policy.getInt("candidateCoreRadius"))
126  {};
lsst::afw::math::KernelList const _basisList
Basis set.
ImageStatistics< PixelT > _imstats
To calculate statistics of difference image.
int getInt(const std::string &name) const
Definition: Policy.h:603
boost::shared_ptr< Eigen::MatrixXd > _hMat
Regularization matrix.
bool _useRegularization
Regularize if delta function basis.
bool getBool(const std::string &name) const
Definition: Policy.h:589
lsst::pex::policy::Policy _policy
Policy controlling behavior.
bool _skipBuilt
Skip over built candidates during processCandidate()
int _nRejected
Number of candidates rejected during processCandidate()
int _nProcessed
Number of candidates processed during processCandidate()
template<typename PixelT >
virtual lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::~BuildSingleKernelVisitor ( )
inlinevirtual

Definition at line 44 of file BuildSingleKernelVisitor.h.

44 {};

Member Function Documentation

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

Definition at line 56 of file BuildSingleKernelVisitor.h.

56 {return _nProcessed;}
int _nProcessed
Number of candidates processed during processCandidate()
template<typename PixelT >
int lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::getNRejected ( )
inline

Definition at line 55 of file BuildSingleKernelVisitor.h.

55 {return _nRejected;}
int _nRejected
Number of candidates rejected during 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 130 of file BuildSingleKernelVisitor.cc.

132  {
133 
134  ipDiffim::KernelCandidate<PixelT> *kCandidate =
135  dynamic_cast<ipDiffim::KernelCandidate<PixelT> *>(candidate);
136  if (kCandidate == NULL) {
137  pexLogging::TTrace<3>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
138  "Failed to cast SpatialCellCandidate to KernelCandidate %d",
139  kCandidate->getId());
140  throw LSST_EXCEPT(pexExcept::LogicError,
141  "Failed to cast SpatialCellCandidate to KernelCandidate");
142  }
143 
144  if (_skipBuilt and kCandidate->isInitialized()) {
145  return;
146  }
147 
148  pexLogging::TTrace<2>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
149  "Processing candidate %d", kCandidate->getId());
150  pexLogging::TTrace<5>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
151  "X = %.2f Y = %.2f",
152  kCandidate->getXCenter(),
153  kCandidate->getYCenter());
154 
155  /* Build its kernel here */
156  try {
157  if (_useRegularization)
158  kCandidate->build(_basisList, _hMat);
159  else
160  kCandidate->build(_basisList);
161 
162  } catch (pexExcept::Exception &e) {
164  pexLogging::TTrace<4>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
165  "Unable to process candidate %d; exception caught (%s)",
166  kCandidate->getId(),
167  e.what());
168  _nRejected += 1;
169  return;
170  }
171 
172  if (kCandidate->getStatus() == afwMath::SpatialCellCandidate::BAD) {
173  pexLogging::TTrace<4>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
174  "Candidate %d Returned BAD upon build, exiting",
175  kCandidate->getId());
176  _nRejected += 1;
177  return;
178  }
179 
180 
181  /*
182  * Make diffim and set chi2 from result. Note that you need to use the
183  * most recent kernel
184  */
186  try {
187  if (_useCoreStats)
188  _imstats.apply(diffim, _coreRadius);
189  else
190  _imstats.apply(diffim);
191  } catch (pexExcept::Exception& e) {
192  pexLogging::TTrace<3>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
193  "Unable to calculate imstats for Candidate %d", kCandidate->getId());
195  return;
196  }
197  _nProcessed += 1;
198 
199  kCandidate->setChi2(_imstats.getVariance());
200 
201  /* When using a Pca basis, we don't reset the kernel or background,
202  so we need to evaluate these locally for the Trace */
203  double kSum = kCandidate->getKsum(ipDiffim::KernelCandidate<PixelT>::RECENT);
204  double background = kCandidate->getBackground(ipDiffim::KernelCandidate<PixelT>::RECENT);
205 
206  pexLogging::TTrace<5>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
207  "Chi2 = %.3f", kCandidate->getChi2());
208  pexLogging::TTrace<5>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
209  "Kernel Sum = %.3f", kSum);
210  pexLogging::TTrace<5>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
211  "Background = %.3f", background);
212  pexLogging::TTrace<3>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
213  "Candidate %d resids = %.3f +/- %.3f sigma (%d pix)",
214  kCandidate->getId(),
215  _imstats.getMean(),
216  _imstats.getRms(),
217  _imstats.getNpix());
218 
219  bool meanIsNan = std::isnan(_imstats.getMean());
220  bool rmsIsNan = std::isnan(_imstats.getRms());
221  if (meanIsNan || rmsIsNan) {
223  pexLogging::TTrace<4>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
224  "Rejecting candidate %d, encountered NaN",
225  kCandidate->getId());
226  _nRejected += 1;
227  return;
228  }
229 
230  if (_policy.getBool("singleKernelClipping")) {
231  if (fabs(_imstats.getMean()) > _policy.getDouble("candidateResidualMeanMax")) {
233  pexLogging::TTrace<4>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
234  "Rejecting candidate %d; bad mean residual : |%.3f| > %.3f",
235  kCandidate->getId(),
236  _imstats.getMean(),
237  _policy.getDouble("candidateResidualMeanMax"));
238  _nRejected += 1;
239  }
240  else if (_imstats.getRms() > _policy.getDouble("candidateResidualStdMax")) {
242  pexLogging::TTrace<4>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
243  "Rejecting candidate %d; bad residual rms : %.3f > %.3f",
244  kCandidate->getId(),
245  _imstats.getRms(),
246  _policy.getDouble("candidateResidualStdMax"));
247  _nRejected += 1;
248  }
249  else {
251  pexLogging::TTrace<4>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
252  "Source kernel OK");
253  }
254  }
255  else {
257  pexLogging::TTrace<6>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
258  "Sigma clipping not enabled");
259  }
260 
261  /* Core resids for debugging */
262  if (!(_useCoreStats)) {
263  try {
264  _imstats.apply(diffim, _coreRadius);
265  } catch (pexExcept::Exception& e) {
266  pexLogging::TTrace<3>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
267  "Unable to calculate core imstats for Candidate %d",
268  kCandidate->getId());
270  return;
271  }
272  pexLogging::TTrace<4>("lsst.ip.diffim.BuildSingleKernelVisitor.processCandidate",
273  "Candidate %d core resids = %.3f +/- %.3f sigma (%d pix)",
274  kCandidate->getId(),
275  _imstats.getMean(),
276  _imstats.getRms(),
277  _imstats.getNpix());
278  }
279 
280  }
lsst::afw::math::KernelList const _basisList
Basis set.
ImageStatistics< PixelT > _imstats
To calculate statistics of difference image.
void setStatus(Status status)
Set the candidate&#39;s status.
Definition: SpatialCell.cc:61
boost::shared_ptr< Eigen::MatrixXd > _hMat
Regularization matrix.
double getChi2() const
Return the candidate&#39;s chi^2.
Definition: SpatialCell.h:163
Class stored in SpatialCells for spatial Kernel fitting.
bool _useRegularization
Regularize if delta function basis.
bool getBool(const std::string &name) const
Definition: Policy.h:589
lsst::pex::policy::Policy _policy
Policy controlling behavior.
void setChi2(double chi2)
Set the candidate&#39;s chi^2.
Definition: SpatialCell.h:165
bool _skipBuilt
Skip over built candidates during processCandidate()
float getYCenter() const
Return the object&#39;s row-centre.
Definition: SpatialCell.h:98
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
lsst::afw::image::MaskedImage< PixelT > MaskedImageT
int isnan(T t)
Definition: ieee.h:110
Status getStatus() const
Return the candidate&#39;s status.
Definition: SpatialCell.h:111
int getId() const
Return the candidate&#39;s unique ID.
Definition: SpatialCell.h:109
double getKsum(CandidateSwitch cand) const
float getXCenter() const
Return the object&#39;s column-centre.
Definition: SpatialCell.h:95
virtual char const * what(void) const
Definition: Exception.cc:112
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
double getDouble(const std::string &name) const
Definition: Policy.h:617
double getBackground(CandidateSwitch cand) const
int _nRejected
Number of candidates rejected during processCandidate()
int _nProcessed
Number of candidates processed during processCandidate()
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;}
int _nRejected
Number of candidates rejected during processCandidate()
int _nProcessed
Number of candidates processed during processCandidate()
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;}
bool _skipBuilt
Skip over built candidates during processCandidate()

Member Data Documentation

template<typename PixelT >
lsst::afw::math::KernelList const lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::_basisList
private

Basis set.

Definition at line 62 of file BuildSingleKernelVisitor.h.

template<typename PixelT >
int lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::_coreRadius
private

Extracted from _policy.

Definition at line 72 of file BuildSingleKernelVisitor.h.

template<typename PixelT >
boost::shared_ptr<Eigen::MatrixXd> lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::_hMat
private

Regularization matrix.

Definition at line 64 of file BuildSingleKernelVisitor.h.

template<typename PixelT >
ImageStatistics<PixelT> lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::_imstats
private

To calculate statistics of difference image.

Definition at line 65 of file BuildSingleKernelVisitor.h.

template<typename PixelT >
int lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::_nProcessed
private

Number of candidates processed during processCandidate()

Definition at line 68 of file BuildSingleKernelVisitor.h.

template<typename PixelT >
int lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::_nRejected
private

Number of candidates rejected during processCandidate()

Definition at line 67 of file BuildSingleKernelVisitor.h.

Policy controlling behavior.

Definition at line 63 of file BuildSingleKernelVisitor.h.

template<typename PixelT >
bool lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::_skipBuilt
private

Skip over built candidates during processCandidate()

Definition at line 66 of file BuildSingleKernelVisitor.h.

template<typename PixelT >
bool lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::_useCoreStats
private

Extracted from _policy.

Definition at line 71 of file BuildSingleKernelVisitor.h.

template<typename PixelT >
bool lsst::ip::diffim::detail::BuildSingleKernelVisitor< PixelT >::_useRegularization
private

Regularize if delta function basis.

Definition at line 69 of file BuildSingleKernelVisitor.h.


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