31template <
typename PixelT>
36 :
lsst::
afw::math::SpatialCellImageCandidate(xCenter, yCenter),
37 _templateMaskedImage(templateMaskedImage),
38 _scienceMaskedImage(scienceMaskedImage),
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()),
70 _templateMaskedImage(templateMaskedImage),
71 _scienceMaskedImage(scienceMaskedImage),
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>
87 build(basisList, Eigen::MatrixXd());
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);
115 _buildKernelSolution(basisList, hMat);
120 if (_ps->getAsBool(
"iterateSingleKernel") && (!(_ps->getAsBool(
"constantVarianceWeighting")))) {
125 _buildKernelSolution(basisList, hMat);
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) {
156 new RegularizedKernelSolution<PixelT>(basisList, _fitForBackground, hMat, *_ps));
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();
170 new RegularizedKernelSolution<PixelT>(basisList, _fitForBackground, hMat, *_ps));
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 LSST_EXCEPT(type,...)
Create an exception with a given 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.
LSST DM logging module built on log4cxx.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
A class to represent a 2-dimensional array of pixels.
A class to manipulate images, masks, and variance as a single object.
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
float getYCenter() const
Return the object's row-centre.
float getXCenter() const
Return the object's column-centre.
int getId() const
Return the candidate's unique ID.
void setStatus(Status status)
Set the candidate's status.
A class to evaluate image statistics.
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Class for storing generic metadata.
Class to calculate difference image statistics.
void apply(lsst::afw::image::MaskedImage< PixelT > const &image)
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.
Provides consistent interface for LSST exceptions.
Reports errors that are due to events beyond the control of the program.
Reports errors from accepting an object of an unexpected or inappropriate type.
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
@ MEDIAN
estimate sample median
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.