12 #include "boost/timer.hpp"
24 namespace afwMath = lsst::afw::math;
26 namespace pexLog = lsst::pex::logging;
27 namespace pexExcept = lsst::pex::exceptions;
28 namespace pexLogging = lsst::pex::logging;
34 template <
typename PixelT>
42 lsst::afw::math::SpatialCellImageCandidate<lsst::afw::math::Kernel::Pixel>(xCenter, yCenter),
43 _templateMaskedImage(templateMaskedImage),
44 _scienceMaskedImage(scienceMaskedImage),
49 _isInitialized(false),
50 _useRegularization(false),
51 _fitForBackground(_policy.getBool(
"fitForBackground")),
52 _kernelSolutionOrig(),
58 int candidateCoreRadius =
_policy.
getInt(
"candidateCoreRadius");
62 pexLogging::TTrace<3>(
"lsst.ip.diffim.KernelCandidate",
63 "Unable to calculate core imstats for ranking Candidate %d", this->
getId());
69 pexLog::TTrace<5>(
"lsst.ip.diffim.KernelCandidate",
70 "Candidate %d at %.2f %.2f with ranking %.2f",
74 template <
typename PixelT>
81 lsst::afw::math::SpatialCellImageCandidate<lsst::afw::math::Kernel::Pixel>(source->getX(), source->getY()),
82 _templateMaskedImage(templateMaskedImage),
83 _scienceMaskedImage(scienceMaskedImage),
87 _coreFlux(source->getPsfFlux()),
88 _isInitialized(false),
89 _useRegularization(false),
90 _fitForBackground(_policy.getBool(
"fitForBackground")),
91 _kernelSolutionOrig(),
94 pexLog::TTrace<5>(
"lsst.ip.diffim.KernelCandidate",
95 "Candidate %d at %.2f %.2f with ranking %.2f",
99 template <
typename PixelT>
103 build(basisList, boost::shared_ptr<Eigen::MatrixXd>());
106 template <
typename PixelT>
109 boost::shared_ptr<Eigen::MatrixXd> hMat
116 var += (*(_templateMaskedImage->getVariance()));
118 if (_policy.getBool(
"constantVarianceWeighting")) {
126 pexLog::TTrace<5>(
"lsst.ip.diffim.KernelCandidate",
127 "Candidate %d using constant variance of %.2f", this->getId(), varValue);
135 _buildKernelSolution(basisList, hMat);
140 if (_policy.getBool(
"iterateSingleKernel") && (!(_policy.getBool(
"constantVarianceWeighting")))) {
145 _buildKernelSolution(basisList, hMat);
151 _isInitialized =
true;
155 template <
typename PixelT>
157 boost::shared_ptr<Eigen::MatrixXd> hMat)
159 bool checkConditionNumber = _policy.getBool(
"checkConditionNumber");
160 double maxConditionNumber = _policy.getDouble(
"maxConditionNumber");
161 std::string conditionNumberType = _policy.getString(
"conditionNumberType");
163 if (conditionNumberType ==
"SVD") {
166 else if (conditionNumberType ==
"EIGENVALUE") {
175 _useRegularization =
true;
176 pexLog::TTrace<5>(
"lsst.ip.diffim.KernelCandidate.build",
177 "Using kernel regularization");
179 if (_isInitialized) {
180 _kernelSolutionPca = boost::shared_ptr<StaticKernelSolution<PixelT> >(
183 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()),
184 *(_scienceMaskedImage->getImage()),
186 if (checkConditionNumber) {
187 if (_kernelSolutionPca->getConditionNumber(ctype) > maxConditionNumber) {
188 pexLog::TTrace<5>(
"lsst.ip.diffim.KernelCandidate",
189 "Candidate %d solution has bad condition number",
195 _kernelSolutionPca->solve();
198 _kernelSolutionOrig = boost::shared_ptr<StaticKernelSolution<PixelT> >(
201 _kernelSolutionOrig->build(*(_templateMaskedImage->getImage()),
202 *(_scienceMaskedImage->getImage()),
204 if (checkConditionNumber) {
205 if (_kernelSolutionOrig->getConditionNumber(ctype) > maxConditionNumber) {
206 pexLog::TTrace<5>(
"lsst.ip.diffim.KernelCandidate",
207 "Candidate %d solution has bad condition number",
213 _kernelSolutionOrig->solve();
217 _useRegularization =
false;
218 pexLog::TTrace<5>(
"lsst.ip.diffim.KernelCandidate.build",
219 "Not using kernel regularization");
220 if (_isInitialized) {
221 _kernelSolutionPca = boost::shared_ptr<StaticKernelSolution<PixelT> >(
224 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()),
225 *(_scienceMaskedImage->getImage()),
227 if (checkConditionNumber) {
228 if (_kernelSolutionPca->getConditionNumber(ctype) > maxConditionNumber) {
229 pexLog::TTrace<5>(
"lsst.ip.diffim.KernelCandidate",
230 "Candidate %d solution has bad condition number",
236 _kernelSolutionPca->solve();
239 _kernelSolutionOrig = boost::shared_ptr<StaticKernelSolution<PixelT> >(
242 _kernelSolutionOrig->build(*(_templateMaskedImage->getImage()),
243 *(_scienceMaskedImage->getImage()),
245 if (checkConditionNumber) {
246 if (_kernelSolutionOrig->getConditionNumber(ctype) > maxConditionNumber) {
247 pexLog::TTrace<5>(
"lsst.ip.diffim.KernelCandidate",
248 "Candidate %d solution has bad condition number",
254 _kernelSolutionOrig->solve();
259 template <
typename PixelT>
262 if (_kernelSolutionOrig)
263 return _kernelSolutionOrig->getKernel();
268 if (_kernelSolutionPca)
269 return _kernelSolutionPca->getKernel();
274 if (_kernelSolutionPca)
275 return _kernelSolutionPca->getKernel();
276 else if (_kernelSolutionOrig)
277 return _kernelSolutionOrig->getKernel();
286 template <
typename PixelT>
289 if (_kernelSolutionOrig)
290 return _kernelSolutionOrig->getBackground();
295 if (_kernelSolutionPca)
296 return _kernelSolutionPca->getBackground();
301 if (_kernelSolutionPca)
302 return _kernelSolutionPca->getBackground();
303 else if (_kernelSolutionOrig)
304 return _kernelSolutionOrig->getBackground();
313 template <
typename PixelT>
316 if (_kernelSolutionOrig)
317 return _kernelSolutionOrig->getKsum();
322 if (_kernelSolutionPca)
323 return _kernelSolutionPca->getKsum();
328 if (_kernelSolutionPca)
329 return _kernelSolutionPca->getKsum();
330 else if (_kernelSolutionOrig)
331 return _kernelSolutionOrig->getKsum();
340 template <
typename PixelT>
344 if (_kernelSolutionOrig)
345 return _kernelSolutionOrig->makeKernelImage();
350 if (_kernelSolutionPca)
351 return _kernelSolutionPca->makeKernelImage();
356 if (_kernelSolutionPca)
357 return _kernelSolutionPca->makeKernelImage();
358 else if (_kernelSolutionOrig)
359 return _kernelSolutionOrig->makeKernelImage();
368 template <
typename PixelT>
373 template <
typename PixelT>
377 if (_kernelSolutionOrig)
378 return _kernelSolutionOrig;
383 if (_kernelSolutionPca)
384 return _kernelSolutionPca;
389 if (_kernelSolutionPca)
390 return _kernelSolutionPca;
391 else if (_kernelSolutionOrig)
392 return _kernelSolutionOrig;
401 template <
typename PixelT>
405 if (_kernelSolutionOrig)
406 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
407 _kernelSolutionOrig->getBackground());
412 if (_kernelSolutionPca)
413 return getDifferenceImage(_kernelSolutionPca->getKernel(),
414 _kernelSolutionPca->getBackground());
419 if (_kernelSolutionPca)
420 return getDifferenceImage(_kernelSolutionPca->getKernel(),
421 _kernelSolutionPca->getBackground());
422 else if (_kernelSolutionOrig)
423 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
424 _kernelSolutionOrig->getBackground());
433 template <
typename PixelT>
440 *_scienceMaskedImage,
An include file to include the public header files for lsst::afw::math.
boost::shared_ptr< ImageT const > getImage() const
Return the Candidate's Image.
void setStatus(Status status)
Set the candidate's status.
int getInt(const std::string &name) const
void apply(lsst::afw::image::MaskedImage< PixelT > const &image)
MaskedImagePtr _scienceMaskedImage
Subimage around which you build kernel.
Class stored in SpatialCells for spatial Kernel fitting.
void _buildKernelSolution(afw::math::KernelList const &basisList, boost::shared_ptr< Eigen::MatrixXd > hMat)
boost::shared_ptr< afw::table::SourceRecord > SourcePtr
a container for holding hierarchical configuration data in memory.
definition of the Trace messaging facilities
float getYCenter() const
Return the object's row-centre.
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
Image Subtraction helper functions.
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
afw::math::Kernel::Ptr getKernel(CandidateSwitch cand) const
Return results of kernel solution.
afwImage::MaskedImage< PixelT > convolveAndSubtract(lsst::afw::image::MaskedImage< PixelT > const &templateImage, lsst::afw::image::MaskedImage< PixelT > const &scienceMaskedImage, lsst::afw::math::Kernel const &convolutionKernel, BackgroundT background, bool invert)
Implement fundamental difference imaging step of convolution and subtraction : D = I - (K*T + bg) whe...
Declaration of classes to store the solution for convolution kernels.
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's variance.
table::Key< table::Array< Kernel::Pixel > > image
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
An include file to include the header files for lsst::afw::image.
boost::shared_ptr< Kernel > Ptr
pex::policy::Policy _policy
Policy.
int getId() const
Return the candidate's unique ID.
double getKsum(CandidateSwitch cand) const
A class to manipulate images, masks, and variance as a single object.
boost::shared_ptr< Image< PixelT > > Ptr
float getXCenter() const
Return the object's column-centre.
Class used by SpatialModelCell for spatial Kernel fitting.
Class to calculate difference image statistics.
boost::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
double _coreFlux
Mean S/N in the science image.
#define LSST_EXCEPT(type,...)
boost::shared_ptr< afw::image::MaskedImage< PixelT > > MaskedImagePtr
double getBackground(CandidateSwitch cand) const
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
boost::shared_ptr< ImageT > getKernelImage(CandidateSwitch cand) const
KernelCandidate(float const xCenter, float const yCenter, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage, pex::policy::Policy const &policy)
Constructor.
boost::shared_ptr< const Image< PixelT > > ConstPtr
A class to represent a 2-dimensional array of pixels.
boost::shared_ptr< afw::image::Image< afw::image::VariancePixel > > VariancePtr
Image Subtraction helper functions.
std::vector< boost::shared_ptr< Kernel > > KernelList