lsst.ip.diffim g470c3a02f1+7eb6ad230c
Loading...
Searching...
No Matches
KernelCandidate.cc
Go to the documentation of this file.
1// -*- lsst-c++ -*-
11#include <stdexcept>
12#include "boost/timer.hpp"
13
14#include "lsst/afw/math.h"
15#include "lsst/afw/image.h"
16#include "lsst/log/Log.h"
18
23
24namespace afwMath = lsst::afw::math;
25namespace afwImage = lsst::afw::image;
27
28namespace lsst {
29namespace ip {
30namespace diffim {
31
32template <typename PixelT>
37 : lsst::afw::math::SpatialCellImageCandidate(xCenter, yCenter),
38 _templateMaskedImage(templateMaskedImage),
39 _scienceMaskedImage(scienceMaskedImage),
40 _varianceEstimate(),
41 _ps(ps.deepCopy()),
42 _source(),
43 _coreFlux(),
44 _isInitialized(false),
45 _useRegularization(false),
46 _fitForBackground(ps.getAsBool("fitForBackground")),
47 _kernelSolutionOrig(),
48 _kernelSolutionPca() {
49 /* Rank by mean core S/N in science image */
51 int candidateCoreRadius = _ps->getAsInt("candidateCoreRadius");
52 try {
53 imstats.apply(*_scienceMaskedImage, candidateCoreRadius);
54 } catch (pexExcept::Exception& e) {
55 LOGL_DEBUG("TRACE2.ip.diffim.KernelCandidate",
56 "Unable to calculate core imstats for rating Candidate %d", this->getId());
57 this->setStatus(afwMath::SpatialCellCandidate::BAD);
58 return;
59 }
60
61 _coreFlux = imstats.getMean();
62 LOGL_DEBUG("TRACE4.ip.diffim.KernelCandidate", "Candidate %d at %.2f %.2f with rating %.2f",
63 this->getId(), this->getXCenter(), this->getYCenter(), _coreFlux);
64}
65
66template <typename PixelT>
70 : lsst::afw::math::SpatialCellImageCandidate(source->getX(), source->getY()),
71 _templateMaskedImage(templateMaskedImage),
72 _scienceMaskedImage(scienceMaskedImage),
73 _varianceEstimate(),
74 _ps(ps.deepCopy()),
75 _source(source),
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",
83 this->getId(), this->getXCenter(), this->getYCenter(), _coreFlux);
84}
85
86template <typename PixelT>
88 build(basisList, Eigen::MatrixXd());
89}
90
91template <typename PixelT>
93 Eigen::MatrixXd const& hMat) {
94 /* Examine the property set for control over the variance estimate */
96 afwImage::Image<afwImage::VariancePixel>(*(_scienceMaskedImage->getVariance()), true);
97 /* Variance estimate comes from sum of image variances */
98 var += (*(_templateMaskedImage->getVariance()));
99
100 if (_ps->getAsBool("constantVarianceWeighting")) {
101 /* Constant variance weighting */
103 float varValue;
104 if (varStats.getValue(afwMath::MEDIAN) <= 0.0)
105 varValue = 1.0;
106 else
107 varValue = varStats.getValue(afwMath::MEDIAN);
108 LOGL_DEBUG("TRACE4.ip.diffim.KernelCandidate", "Candidate %d using constant variance of %.2f",
109 this->getId(), varValue);
110 var = varValue;
111 }
112
113 _varianceEstimate = VariancePtr(new afwImage::Image<afwImage::VariancePixel>(var));
114
115 try {
116 _buildKernelSolution(basisList, hMat);
117 } catch (pexExcept::Exception& e) {
118 throw e;
119 }
120
121 if (_ps->getAsBool("iterateSingleKernel") && (!(_ps->getAsBool("constantVarianceWeighting")))) {
122 afwImage::MaskedImage<PixelT> diffim = getDifferenceImage(KernelCandidate::RECENT);
123 _varianceEstimate = diffim.getVariance();
124
125 try {
126 _buildKernelSolution(basisList, hMat);
127 } catch (pexExcept::Exception& e) {
128 throw e;
129 }
130 }
131
132 _isInitialized = true;
133}
134
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") {
143 ctype = KernelSolution::SVD;
144 } else if (conditionNumberType == "EIGENVALUE") {
146 } else {
147 throw LSST_EXCEPT(pexExcept::TypeError, "conditionNumberType not recognized");
148 }
149
150 /* Do we have a regularization matrix? If so use it */
151 if (hMat.size() > 0) {
152 _useRegularization = true;
153 LOGL_DEBUG("TRACE4.ip.diffim.KernelCandidate.build", "Using kernel regularization");
154
155 if (_isInitialized) {
157 new RegularizedKernelSolution<PixelT>(basisList, _fitForBackground, hMat, *_ps));
158 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()), *(_scienceMaskedImage->getImage()),
159 *_varianceEstimate);
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);
165 return;
166 }
167 }
168 _kernelSolutionPca->solve();
169 } else {
170 _kernelSolutionOrig = std::shared_ptr<StaticKernelSolution<PixelT> >(
171 new RegularizedKernelSolution<PixelT>(basisList, _fitForBackground, hMat, *_ps));
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);
179 return;
180 }
181 }
182 _kernelSolutionOrig->solve();
183 }
184 } else {
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()),
191 *_varianceEstimate);
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);
197 return;
198 }
199 }
200 _kernelSolutionPca->solve();
201 } else {
202 _kernelSolutionOrig = std::shared_ptr<StaticKernelSolution<PixelT> >(
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);
211 return;
212 }
213 }
214 _kernelSolutionOrig->solve();
215 }
216 }
217}
218
219template <typename PixelT>
221 switch (cand) {
223 if (_kernelSolutionOrig)
224 return _kernelSolutionOrig->getKernel();
225 else
226 throw LSST_EXCEPT(pexExcept::RuntimeError, "Original kernel does not exist");
227 break;
229 if (_kernelSolutionPca)
230 return _kernelSolutionPca->getKernel();
231 else
232 throw LSST_EXCEPT(pexExcept::RuntimeError, "Pca kernel does not exist");
233 break;
235 if (_kernelSolutionPca)
236 return _kernelSolutionPca->getKernel();
237 else if (_kernelSolutionOrig)
238 return _kernelSolutionOrig->getKernel();
239 else
240 throw LSST_EXCEPT(pexExcept::RuntimeError, "No kernels exist");
241 break;
242 default:
243 throw std::logic_error("Invalid CandidateSwitch, cannot get kernel");
244 }
245}
246
247template <typename PixelT>
249 switch (cand) {
251 if (_kernelSolutionOrig)
252 return _kernelSolutionOrig->getBackground();
253 else
254 throw LSST_EXCEPT(pexExcept::RuntimeError, "Original kernel does not exist");
255 break;
257 if (_kernelSolutionPca)
258 return _kernelSolutionPca->getBackground();
259 else
260 throw LSST_EXCEPT(pexExcept::RuntimeError, "Pca kernel does not exist");
261 break;
263 if (_kernelSolutionPca)
264 return _kernelSolutionPca->getBackground();
265 else if (_kernelSolutionOrig)
266 return _kernelSolutionOrig->getBackground();
267 else
268 throw LSST_EXCEPT(pexExcept::RuntimeError, "No kernels exist");
269 break;
270 default:
271 throw std::logic_error("Invalid CandidateSwitch, cannot get background");
272 }
273}
274
275template <typename PixelT>
277 switch (cand) {
279 if (_kernelSolutionOrig)
280 return _kernelSolutionOrig->getKsum();
281 else
282 throw LSST_EXCEPT(pexExcept::RuntimeError, "Original kernel does not exist");
283 break;
285 if (_kernelSolutionPca)
286 return _kernelSolutionPca->getKsum();
287 else
288 throw LSST_EXCEPT(pexExcept::RuntimeError, "Pca kernel does not exist");
289 break;
291 if (_kernelSolutionPca)
292 return _kernelSolutionPca->getKsum();
293 else if (_kernelSolutionOrig)
294 return _kernelSolutionOrig->getKsum();
295 else
296 throw LSST_EXCEPT(pexExcept::RuntimeError, "No kernels exist");
297 break;
298 default:
299 throw std::logic_error("Invalid CandidateSwitch, cannot get kSum");
300 }
301}
302
303template <typename PixelT>
305 CandidateSwitch cand) const {
306 switch (cand) {
308 if (_kernelSolutionOrig)
309 return _kernelSolutionOrig->makeKernelImage();
310 else
311 throw LSST_EXCEPT(pexExcept::RuntimeError, "Original kernel does not exist");
312 break;
314 if (_kernelSolutionPca)
315 return _kernelSolutionPca->makeKernelImage();
316 else
317 throw LSST_EXCEPT(pexExcept::RuntimeError, "Pca kernel does not exist");
318 break;
320 if (_kernelSolutionPca)
321 return _kernelSolutionPca->makeKernelImage();
322 else if (_kernelSolutionOrig)
323 return _kernelSolutionOrig->makeKernelImage();
324 else
325 throw LSST_EXCEPT(pexExcept::RuntimeError, "No kernels exist");
326 break;
327 default:
328 throw std::logic_error("Invalid CandidateSwitch, cannot get kernel image");
329 }
330}
331
332template <typename PixelT>
336
337template <typename PixelT>
339 CandidateSwitch cand) const {
340 switch (cand) {
342 if (_kernelSolutionOrig)
343 return _kernelSolutionOrig;
344 else
345 throw LSST_EXCEPT(pexExcept::RuntimeError, "Original kernel does not exist");
346 break;
348 if (_kernelSolutionPca)
349 return _kernelSolutionPca;
350 else
351 throw LSST_EXCEPT(pexExcept::RuntimeError, "Pca kernel does not exist");
352 break;
354 if (_kernelSolutionPca)
355 return _kernelSolutionPca;
356 else if (_kernelSolutionOrig)
357 return _kernelSolutionOrig;
358 else
359 throw LSST_EXCEPT(pexExcept::RuntimeError, "No kernels exist");
360 break;
361 default:
362 throw std::logic_error("Invalid CandidateSwitch, cannot get solution");
363 }
364}
365
366template <typename PixelT>
368 switch (cand) {
370 if (_kernelSolutionOrig)
371 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
372 _kernelSolutionOrig->getBackground());
373 else
374 throw LSST_EXCEPT(pexExcept::RuntimeError, "Original kernel does not exist");
375 break;
377 if (_kernelSolutionPca)
378 return getDifferenceImage(_kernelSolutionPca->getKernel(),
379 _kernelSolutionPca->getBackground());
380 else
381 throw LSST_EXCEPT(pexExcept::RuntimeError, "Pca kernel does not exist");
382 break;
384 if (_kernelSolutionPca)
385 return getDifferenceImage(_kernelSolutionPca->getKernel(),
386 _kernelSolutionPca->getBackground());
387 else if (_kernelSolutionOrig)
388 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
389 _kernelSolutionOrig->getBackground());
390 else
391 throw LSST_EXCEPT(pexExcept::RuntimeError, "No kernels exist");
392 break;
393 default:
394 throw std::logic_error("Invalid CandidateSwitch, cannot get diffim");
395 }
396}
397
398template <typename PixelT>
400 std::shared_ptr<lsst::afw::math::Kernel> kernel, double background) {
401 /* Make diffim and set chi2 from result */
403 convolveAndSubtract(*_templateMaskedImage, *_scienceMaskedImage, *kernel, background);
404 return diffIm;
405}
406
407/***********************************************************************************************************/
408//
409// Explicit instantiations
410//
411typedef float PixelT;
412
413template class KernelCandidate<PixelT>;
414
415} // namespace diffim
416} // namespace ip
417} // namespace lsst
#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
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.