12#include "boost/timer.hpp"
32template <
typename PixelT>
44 _isInitialized(
false),
45 _useRegularization(
false),
46 _fitForBackground(
ps.getAsBool(
"fitForBackground")),
47 _kernelSolutionOrig(),
48 _kernelSolutionPca() {
51 int candidateCoreRadius = _ps->getAsInt(
"candidateCoreRadius");
53 imstats.apply(*_scienceMaskedImage, candidateCoreRadius);
56 "Unable to calculate core imstats for rating Candidate %d", this->
getId());
57 this->
setStatus(afwMath::SpatialCellCandidate::BAD);
62 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d at %.2f %.2f with rating %.2f",
66template <
typename PixelT>
70 :
lsst::afw::math::SpatialCellImageCandidate(source->getX(), source->getY()),
76 _coreFlux(source->getPsfInstFlux()),
77 _isInitialized(
false),
78 _useRegularization(
false),
79 _fitForBackground(
ps.getAsBool(
"fitForBackground")),
80 _kernelSolutionOrig(),
81 _kernelSolutionPca() {
82 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d at %.2f %.2f with rating %.2f",
86template <
typename PixelT>
91template <
typename PixelT>
93 Eigen::MatrixXd
const& hMat) {
98 var += (*(_templateMaskedImage->getVariance()));
100 if (_ps->getAsBool(
"constantVarianceWeighting")) {
108 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d using constant variance of %.2f",
109 this->getId(), varValue);
121 if (_ps->getAsBool(
"iterateSingleKernel") && (!(_ps->getAsBool(
"constantVarianceWeighting")))) {
132 _isInitialized =
true;
135template <
typename PixelT>
137 Eigen::MatrixXd
const& hMat) {
138 bool checkConditionNumber = _ps->getAsBool(
"checkConditionNumber");
139 double maxConditionNumber = _ps->getAsDouble(
"maxConditionNumber");
140 std::string conditionNumberType = _ps->getAsString(
"conditionNumberType");
142 if (conditionNumberType ==
"SVD") {
144 }
else if (conditionNumberType ==
"EIGENVALUE") {
151 if (hMat.size() > 0) {
152 _useRegularization =
true;
153 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
"Using kernel regularization");
155 if (_isInitialized) {
158 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()), *(_scienceMaskedImage->getImage()),
160 if (checkConditionNumber) {
161 if (_kernelSolutionPca->getConditionNumber(ctype) > maxConditionNumber) {
162 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
163 "Candidate %d solution has bad condition number", this->getId());
164 this->setStatus(afwMath::SpatialCellCandidate::BAD);
168 _kernelSolutionPca->solve();
172 _kernelSolutionOrig->build(*(_templateMaskedImage->getImage()),
173 *(_scienceMaskedImage->getImage()), *_varianceEstimate);
174 if (checkConditionNumber) {
175 if (_kernelSolutionOrig->getConditionNumber(ctype) > maxConditionNumber) {
176 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
177 "Candidate %d solution has bad condition number", this->getId());
178 this->setStatus(afwMath::SpatialCellCandidate::BAD);
182 _kernelSolutionOrig->solve();
185 _useRegularization =
false;
186 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
"Not using kernel regularization");
187 if (_isInitialized) {
189 new StaticKernelSolution<PixelT>(
basisList, _fitForBackground));
190 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()), *(_scienceMaskedImage->getImage()),
192 if (checkConditionNumber) {
193 if (_kernelSolutionPca->getConditionNumber(ctype) > maxConditionNumber) {
194 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
195 "Candidate %d solution has bad condition number", this->getId());
196 this->setStatus(afwMath::SpatialCellCandidate::BAD);
200 _kernelSolutionPca->solve();
203 new StaticKernelSolution<PixelT>(
basisList, _fitForBackground));
204 _kernelSolutionOrig->build(*(_templateMaskedImage->getImage()),
205 *(_scienceMaskedImage->getImage()), *_varianceEstimate);
206 if (checkConditionNumber) {
207 if (_kernelSolutionOrig->getConditionNumber(ctype) > maxConditionNumber) {
208 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
209 "Candidate %d solution has bad condition number", this->getId());
210 this->setStatus(afwMath::SpatialCellCandidate::BAD);
214 _kernelSolutionOrig->solve();
219template <
typename PixelT>
223 if (_kernelSolutionOrig)
224 return _kernelSolutionOrig->getKernel();
229 if (_kernelSolutionPca)
230 return _kernelSolutionPca->getKernel();
235 if (_kernelSolutionPca)
236 return _kernelSolutionPca->getKernel();
237 else if (_kernelSolutionOrig)
238 return _kernelSolutionOrig->getKernel();
247template <
typename PixelT>
251 if (_kernelSolutionOrig)
252 return _kernelSolutionOrig->getBackground();
257 if (_kernelSolutionPca)
258 return _kernelSolutionPca->getBackground();
263 if (_kernelSolutionPca)
264 return _kernelSolutionPca->getBackground();
265 else if (_kernelSolutionOrig)
266 return _kernelSolutionOrig->getBackground();
275template <
typename PixelT>
279 if (_kernelSolutionOrig)
280 return _kernelSolutionOrig->getKsum();
285 if (_kernelSolutionPca)
286 return _kernelSolutionPca->getKsum();
291 if (_kernelSolutionPca)
292 return _kernelSolutionPca->getKsum();
293 else if (_kernelSolutionOrig)
294 return _kernelSolutionOrig->getKsum();
303template <
typename PixelT>
308 if (_kernelSolutionOrig)
309 return _kernelSolutionOrig->makeKernelImage();
314 if (_kernelSolutionPca)
315 return _kernelSolutionPca->makeKernelImage();
320 if (_kernelSolutionPca)
321 return _kernelSolutionPca->makeKernelImage();
322 else if (_kernelSolutionOrig)
323 return _kernelSolutionOrig->makeKernelImage();
332template <
typename PixelT>
337template <
typename PixelT>
342 if (_kernelSolutionOrig)
343 return _kernelSolutionOrig;
348 if (_kernelSolutionPca)
349 return _kernelSolutionPca;
354 if (_kernelSolutionPca)
355 return _kernelSolutionPca;
356 else if (_kernelSolutionOrig)
357 return _kernelSolutionOrig;
366template <
typename PixelT>
370 if (_kernelSolutionOrig)
371 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
372 _kernelSolutionOrig->getBackground());
377 if (_kernelSolutionPca)
378 return getDifferenceImage(_kernelSolutionPca->getKernel(),
379 _kernelSolutionPca->getBackground());
384 if (_kernelSolutionPca)
385 return getDifferenceImage(_kernelSolutionPca->getKernel(),
386 _kernelSolutionPca->getBackground());
387 else if (_kernelSolutionOrig)
388 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
389 _kernelSolutionOrig->getBackground());
398template <
typename PixelT>
#define LOGL_DEBUG(logger, message...)
#define LSST_EXCEPT(type,...)
Image Subtraction helper functions.
Image Subtraction helper functions.
Class used by SpatialModelCell for spatial Kernel fitting.
Declaration of classes to store the solution for convolution kernels.
VariancePtr getVariance() const
void setStatus(Status status)
Class stored in SpatialCells for spatial Kernel fitting.
std::shared_ptr< ImageT const > getImage() const
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
double getBackground(CandidateSwitch cand) const
double getKsum(CandidateSwitch cand) const
std::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
std::shared_ptr< afw::math::Kernel > getKernel(CandidateSwitch cand) const
Return results of kernel solution.
KernelCandidate(float const xCenter, float const yCenter, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage, daf::base::PropertySet const &ps)
Constructor.
std::shared_ptr< ImageT > getKernelImage(CandidateSwitch cand) const
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
Asseses the quality of a candidate given a spatial kernel and background model.
AssessSpatialKernelVisitor(std::shared_ptr< lsst::afw::math::LinearCombinationKernel > spatialKernel, lsst::afw::math::Kernel::SpatialFunctionPtr spatialBackground, lsst::daf::base::PropertySet const &ps)
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
lsst::afw::image::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=true)
Execute fundamental task of convolving template and subtracting it from science image.