31template <
typename PixelT>
43 _isInitialized(
false),
44 _useRegularization(
false),
45 _fitForBackground(
ps.getAsBool(
"fitForBackground")),
46 _kernelSolutionOrig(),
47 _kernelSolutionPca() {
50 int candidateCoreRadius = _ps->getAsInt(
"candidateCoreRadius");
52 imstats.apply(*_scienceMaskedImage, candidateCoreRadius);
55 "Unable to calculate core imstats for rating Candidate %d", this->
getId());
56 this->
setStatus(afwMath::SpatialCellCandidate::BAD);
61 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d at %.2f %.2f with rating %.2f",
65template <
typename PixelT>
69 :
lsst::afw::math::SpatialCellImageCandidate(source->getX(), source->getY()),
75 _coreFlux(source->getPsfInstFlux()),
76 _isInitialized(
false),
77 _useRegularization(
false),
78 _fitForBackground(
ps.getAsBool(
"fitForBackground")),
79 _kernelSolutionOrig(),
80 _kernelSolutionPca() {
81 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d at %.2f %.2f with rating %.2f",
85template <
typename PixelT>
90template <
typename PixelT>
92 Eigen::MatrixXd
const& hMat) {
97 var += (*(_templateMaskedImage->getVariance()));
99 if (_ps->getAsBool(
"constantVarianceWeighting")) {
107 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d using constant variance of %.2f",
108 this->getId(), varValue);
120 if (_ps->getAsBool(
"iterateSingleKernel") && (!(_ps->getAsBool(
"constantVarianceWeighting")))) {
131 _isInitialized =
true;
134template <
typename PixelT>
136 Eigen::MatrixXd
const& hMat) {
137 bool checkConditionNumber = _ps->getAsBool(
"checkConditionNumber");
138 double maxConditionNumber = _ps->getAsDouble(
"maxConditionNumber");
139 std::string conditionNumberType = _ps->getAsString(
"conditionNumberType");
141 if (conditionNumberType ==
"SVD") {
143 }
else if (conditionNumberType ==
"EIGENVALUE") {
150 if (hMat.size() > 0) {
151 _useRegularization =
true;
152 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
"Using kernel regularization");
154 if (_isInitialized) {
157 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()), *(_scienceMaskedImage->getImage()),
159 if (checkConditionNumber) {
160 if (_kernelSolutionPca->getConditionNumber(ctype) > maxConditionNumber) {
161 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
162 "Candidate %d solution has bad condition number", this->getId());
163 this->setStatus(afwMath::SpatialCellCandidate::BAD);
167 _kernelSolutionPca->solve();
171 _kernelSolutionOrig->build(*(_templateMaskedImage->getImage()),
172 *(_scienceMaskedImage->getImage()), *_varianceEstimate);
173 if (checkConditionNumber) {
174 if (_kernelSolutionOrig->getConditionNumber(ctype) > maxConditionNumber) {
175 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
176 "Candidate %d solution has bad condition number", this->getId());
177 this->setStatus(afwMath::SpatialCellCandidate::BAD);
181 _kernelSolutionOrig->solve();
184 _useRegularization =
false;
185 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
"Not using kernel regularization");
186 if (_isInitialized) {
188 new StaticKernelSolution<PixelT>(
basisList, _fitForBackground));
189 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()), *(_scienceMaskedImage->getImage()),
191 if (checkConditionNumber) {
192 if (_kernelSolutionPca->getConditionNumber(ctype) > maxConditionNumber) {
193 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
194 "Candidate %d solution has bad condition number", this->getId());
195 this->setStatus(afwMath::SpatialCellCandidate::BAD);
199 _kernelSolutionPca->solve();
202 new StaticKernelSolution<PixelT>(
basisList, _fitForBackground));
203 _kernelSolutionOrig->build(*(_templateMaskedImage->getImage()),
204 *(_scienceMaskedImage->getImage()), *_varianceEstimate);
205 if (checkConditionNumber) {
206 if (_kernelSolutionOrig->getConditionNumber(ctype) > maxConditionNumber) {
207 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
208 "Candidate %d solution has bad condition number", this->getId());
209 this->setStatus(afwMath::SpatialCellCandidate::BAD);
213 _kernelSolutionOrig->solve();
218template <
typename PixelT>
222 if (_kernelSolutionOrig)
223 return _kernelSolutionOrig->getKernel();
228 if (_kernelSolutionPca)
229 return _kernelSolutionPca->getKernel();
234 if (_kernelSolutionPca)
235 return _kernelSolutionPca->getKernel();
236 else if (_kernelSolutionOrig)
237 return _kernelSolutionOrig->getKernel();
246template <
typename PixelT>
250 if (_kernelSolutionOrig)
251 return _kernelSolutionOrig->getBackground();
256 if (_kernelSolutionPca)
257 return _kernelSolutionPca->getBackground();
262 if (_kernelSolutionPca)
263 return _kernelSolutionPca->getBackground();
264 else if (_kernelSolutionOrig)
265 return _kernelSolutionOrig->getBackground();
274template <
typename PixelT>
278 if (_kernelSolutionOrig)
279 return _kernelSolutionOrig->getKsum();
284 if (_kernelSolutionPca)
285 return _kernelSolutionPca->getKsum();
290 if (_kernelSolutionPca)
291 return _kernelSolutionPca->getKsum();
292 else if (_kernelSolutionOrig)
293 return _kernelSolutionOrig->getKsum();
302template <
typename PixelT>
307 if (_kernelSolutionOrig)
308 return _kernelSolutionOrig->makeKernelImage();
313 if (_kernelSolutionPca)
314 return _kernelSolutionPca->makeKernelImage();
319 if (_kernelSolutionPca)
320 return _kernelSolutionPca->makeKernelImage();
321 else if (_kernelSolutionOrig)
322 return _kernelSolutionOrig->makeKernelImage();
331template <
typename PixelT>
336template <
typename PixelT>
341 if (_kernelSolutionOrig)
342 return _kernelSolutionOrig;
347 if (_kernelSolutionPca)
348 return _kernelSolutionPca;
353 if (_kernelSolutionPca)
354 return _kernelSolutionPca;
355 else if (_kernelSolutionOrig)
356 return _kernelSolutionOrig;
365template <
typename PixelT>
369 if (_kernelSolutionOrig)
370 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
371 _kernelSolutionOrig->getBackground());
376 if (_kernelSolutionPca)
377 return getDifferenceImage(_kernelSolutionPca->getKernel(),
378 _kernelSolutionPca->getBackground());
383 if (_kernelSolutionPca)
384 return getDifferenceImage(_kernelSolutionPca->getKernel(),
385 _kernelSolutionPca->getBackground());
386 else if (_kernelSolutionOrig)
387 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
388 _kernelSolutionOrig->getBackground());
397template <
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.