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 ranking Candidate %d", this->
getId());
57 this->
setStatus(afwMath::SpatialCellCandidate::BAD);
62 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d at %.2f %.2f with ranking %.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 ranking %.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>
222 if (_kernelSolutionOrig)
223 return _kernelSolutionOrig->getKernel();
227 if (_kernelSolutionPca)
228 return _kernelSolutionPca->getKernel();
232 if (_kernelSolutionPca)
233 return _kernelSolutionPca->getKernel();
234 else if (_kernelSolutionOrig)
235 return _kernelSolutionOrig->getKernel();
243template <
typename PixelT>
246 if (_kernelSolutionOrig)
247 return _kernelSolutionOrig->getBackground();
251 if (_kernelSolutionPca)
252 return _kernelSolutionPca->getBackground();
256 if (_kernelSolutionPca)
257 return _kernelSolutionPca->getBackground();
258 else if (_kernelSolutionOrig)
259 return _kernelSolutionOrig->getBackground();
267template <
typename PixelT>
270 if (_kernelSolutionOrig)
271 return _kernelSolutionOrig->getKsum();
275 if (_kernelSolutionPca)
276 return _kernelSolutionPca->getKsum();
280 if (_kernelSolutionPca)
281 return _kernelSolutionPca->getKsum();
282 else if (_kernelSolutionOrig)
283 return _kernelSolutionOrig->getKsum();
291template <
typename PixelT>
295 if (_kernelSolutionOrig)
296 return _kernelSolutionOrig->makeKernelImage();
300 if (_kernelSolutionPca)
301 return _kernelSolutionPca->makeKernelImage();
305 if (_kernelSolutionPca)
306 return _kernelSolutionPca->makeKernelImage();
307 else if (_kernelSolutionOrig)
308 return _kernelSolutionOrig->makeKernelImage();
316template <
typename PixelT>
321template <
typename PixelT>
325 if (_kernelSolutionOrig)
326 return _kernelSolutionOrig;
330 if (_kernelSolutionPca)
331 return _kernelSolutionPca;
335 if (_kernelSolutionPca)
336 return _kernelSolutionPca;
337 else if (_kernelSolutionOrig)
338 return _kernelSolutionOrig;
346template <
typename PixelT>
349 if (_kernelSolutionOrig)
350 return getDifferenceImage(_kernelSolutionOrig->getKernel(), _kernelSolutionOrig->getBackground());
354 if (_kernelSolutionPca)
355 return getDifferenceImage(_kernelSolutionPca->getKernel(), _kernelSolutionPca->getBackground());
359 if (_kernelSolutionPca)
360 return getDifferenceImage(_kernelSolutionPca->getKernel(), _kernelSolutionPca->getBackground());
361 else if (_kernelSolutionOrig)
362 return getDifferenceImage(_kernelSolutionOrig->getKernel(), _kernelSolutionOrig->getBackground());
370template <
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.