12 #include "boost/timer.hpp" 32 template <
typename PixelT>
40 lsst::afw::math::SpatialCellImageCandidate(xCenter, yCenter),
41 _templateMaskedImage(templateMaskedImage),
42 _scienceMaskedImage(scienceMaskedImage),
47 _isInitialized(false),
48 _useRegularization(false),
49 _fitForBackground(_policy.getBool(
"fitForBackground")),
50 _kernelSolutionOrig(),
56 int candidateCoreRadius = _policy.
getInt(
"candidateCoreRadius");
58 imstats.
apply(*_scienceMaskedImage, candidateCoreRadius);
61 "Unable to calculate core imstats for ranking Candidate %d", this->
getId());
68 "Candidate %d at %.2f %.2f with ranking %.2f",
72 template <
typename PixelT>
80 _templateMaskedImage(templateMaskedImage),
81 _scienceMaskedImage(scienceMaskedImage),
85 _coreFlux(source->getPsfFlux()),
86 _isInitialized(false),
87 _useRegularization(false),
88 _fitForBackground(_policy.getBool(
"fitForBackground")),
89 _kernelSolutionOrig(),
93 "Candidate %d at %.2f %.2f with ranking %.2f",
97 template <
typename PixelT>
101 build(basisList, Eigen::MatrixXd());
104 template <
typename PixelT>
107 Eigen::MatrixXd
const& hMat
114 var += (*(_templateMaskedImage->getVariance()));
116 if (_policy.
getBool(
"constantVarianceWeighting")) {
120 if (varStats.
getValue(afwMath::MEDIAN) <= 0.0)
123 varValue = varStats.
getValue(afwMath::MEDIAN);
124 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
125 "Candidate %d using constant variance of %.2f", this->
getId(), varValue);
133 _buildKernelSolution(basisList, hMat);
138 if (_policy.
getBool(
"iterateSingleKernel") && (!(_policy.
getBool(
"constantVarianceWeighting")))) {
143 _buildKernelSolution(basisList, hMat);
149 _isInitialized =
true;
153 template <
typename PixelT>
155 Eigen::MatrixXd
const& hMat)
157 bool checkConditionNumber = _policy.
getBool(
"checkConditionNumber");
158 double maxConditionNumber = _policy.
getDouble(
"maxConditionNumber");
161 if (conditionNumberType ==
"SVD") {
164 else if (conditionNumberType ==
"EIGENVALUE") {
172 if (hMat.size() > 0) {
173 _useRegularization =
true;
174 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
175 "Using kernel regularization");
177 if (_isInitialized) {
181 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()),
182 *(_scienceMaskedImage->getImage()),
184 if (checkConditionNumber) {
185 if (_kernelSolutionPca->getConditionNumber(ctype) > maxConditionNumber) {
186 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
187 "Candidate %d solution has bad condition number",
193 _kernelSolutionPca->solve();
199 _kernelSolutionOrig->build(*(_templateMaskedImage->getImage()),
200 *(_scienceMaskedImage->getImage()),
202 if (checkConditionNumber) {
203 if (_kernelSolutionOrig->getConditionNumber(ctype) > maxConditionNumber) {
204 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
205 "Candidate %d solution has bad condition number",
211 _kernelSolutionOrig->solve();
215 _useRegularization =
false;
216 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
217 "Not using kernel regularization");
218 if (_isInitialized) {
222 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()),
223 *(_scienceMaskedImage->getImage()),
225 if (checkConditionNumber) {
226 if (_kernelSolutionPca->getConditionNumber(ctype) > maxConditionNumber) {
227 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
228 "Candidate %d solution has bad condition number",
234 _kernelSolutionPca->solve();
240 _kernelSolutionOrig->build(*(_templateMaskedImage->getImage()),
241 *(_scienceMaskedImage->getImage()),
243 if (checkConditionNumber) {
244 if (_kernelSolutionOrig->getConditionNumber(ctype) > maxConditionNumber) {
245 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
246 "Candidate %d solution has bad condition number",
252 _kernelSolutionOrig->solve();
257 template <
typename PixelT>
260 if (_kernelSolutionOrig)
261 return _kernelSolutionOrig->getKernel();
266 if (_kernelSolutionPca)
267 return _kernelSolutionPca->getKernel();
272 if (_kernelSolutionPca)
273 return _kernelSolutionPca->getKernel();
274 else if (_kernelSolutionOrig)
275 return _kernelSolutionOrig->getKernel();
284 template <
typename PixelT>
287 if (_kernelSolutionOrig)
288 return _kernelSolutionOrig->getBackground();
293 if (_kernelSolutionPca)
294 return _kernelSolutionPca->getBackground();
299 if (_kernelSolutionPca)
300 return _kernelSolutionPca->getBackground();
301 else if (_kernelSolutionOrig)
302 return _kernelSolutionOrig->getBackground();
311 template <
typename PixelT>
314 if (_kernelSolutionOrig)
315 return _kernelSolutionOrig->getKsum();
320 if (_kernelSolutionPca)
321 return _kernelSolutionPca->getKsum();
326 if (_kernelSolutionPca)
327 return _kernelSolutionPca->getKsum();
328 else if (_kernelSolutionOrig)
329 return _kernelSolutionOrig->getKsum();
338 template <
typename PixelT>
342 if (_kernelSolutionOrig)
343 return _kernelSolutionOrig->makeKernelImage();
348 if (_kernelSolutionPca)
349 return _kernelSolutionPca->makeKernelImage();
354 if (_kernelSolutionPca)
355 return _kernelSolutionPca->makeKernelImage();
356 else if (_kernelSolutionOrig)
357 return _kernelSolutionOrig->makeKernelImage();
366 template <
typename PixelT>
371 template <
typename PixelT>
375 if (_kernelSolutionOrig)
376 return _kernelSolutionOrig;
381 if (_kernelSolutionPca)
382 return _kernelSolutionPca;
387 if (_kernelSolutionPca)
388 return _kernelSolutionPca;
389 else if (_kernelSolutionOrig)
390 return _kernelSolutionOrig;
399 template <
typename PixelT>
403 if (_kernelSolutionOrig)
405 _kernelSolutionOrig->getBackground());
410 if (_kernelSolutionPca)
412 _kernelSolutionPca->getBackground());
417 if (_kernelSolutionPca)
419 _kernelSolutionPca->getBackground());
420 else if (_kernelSolutionOrig)
422 _kernelSolutionOrig->getBackground());
431 template <
typename PixelT>
438 *_scienceMaskedImage,
VariancePtr getVariance() const
void setStatus(Status status)
void apply(lsst::afw::image::MaskedImage< PixelT > const &image)
double getBackground(CandidateSwitch cand) const
Class stored in SpatialCells for spatial Kernel fitting.
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
Image Subtraction helper functions.
std::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
std::shared_ptr< afw::math::Kernel > getKernel(CandidateSwitch cand) const
Return results of kernel solution.
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
boost::shared_ptr< ImageT > getKernelImage(CandidateSwitch cand) const
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.
#define LOGL_DEBUG(logger, message...)
Declaration of classes to store the solution for convolution kernels.
bool getBool(const std::string &name) const
std::shared_ptr< afw::image::Image< afw::image::VariancePixel > > VariancePtr
SpatialCellImageCandidate(float const xCenter, float const yCenter)
int getInt(const std::string &name) const
const std::string getString(const std::string &name) const
Class used by SpatialModelCell for spatial Kernel fitting.
double getValue(Property const prop=NOTHING) const
Class to calculate difference image statistics.
double getDouble(const std::string &name) const
#define LSST_EXCEPT(type,...)
boost::shared_ptr< ImageT const > getImage() const
KernelCandidate(float const xCenter, float const yCenter, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage, pex::policy::Policy const &policy)
Constructor.
Image Subtraction helper functions.
double getKsum(CandidateSwitch cand) const