12 #include "boost/timer.hpp"
24 namespace afwMath = lsst::afw::math;
26 namespace pexExcept = lsst::pex::exceptions;
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");
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>
79 lsst::afw::math::SpatialCellImageCandidate(source->getX(), source->getY()),
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, std::shared_ptr<Eigen::MatrixXd>());
104 template <
typename PixelT>
107 std::shared_ptr<Eigen::MatrixXd> hMat
114 var += (*(_templateMaskedImage->getVariance()));
116 if (_policy.getBool(
"constantVarianceWeighting")) {
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 std::shared_ptr<Eigen::MatrixXd> hMat)
157 bool checkConditionNumber = _policy.getBool(
"checkConditionNumber");
158 double maxConditionNumber = _policy.getDouble(
"maxConditionNumber");
159 std::string conditionNumberType = _policy.getString(
"conditionNumberType");
161 if (conditionNumberType ==
"SVD") {
164 else if (conditionNumberType ==
"EIGENVALUE") {
173 _useRegularization =
true;
174 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
175 "Using kernel regularization");
177 if (_isInitialized) {
178 _kernelSolutionPca = std::shared_ptr<StaticKernelSolution<PixelT> >(
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();
196 _kernelSolutionOrig = std::shared_ptr<StaticKernelSolution<PixelT> >(
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) {
219 _kernelSolutionPca = std::shared_ptr<StaticKernelSolution<PixelT> >(
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();
237 _kernelSolutionOrig = std::shared_ptr<StaticKernelSolution<PixelT> >(
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)
404 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
405 _kernelSolutionOrig->getBackground());
410 if (_kernelSolutionPca)
411 return getDifferenceImage(_kernelSolutionPca->getKernel(),
412 _kernelSolutionPca->getBackground());
417 if (_kernelSolutionPca)
418 return getDifferenceImage(_kernelSolutionPca->getKernel(),
419 _kernelSolutionPca->getBackground());
420 else if (_kernelSolutionOrig)
421 return getDifferenceImage(_kernelSolutionOrig->getKernel(),
422 _kernelSolutionOrig->getBackground());
431 template <
typename PixelT>
438 *_scienceMaskedImage,
An include file to include the public header files for lsst::afw::math.
boost::shared_ptr< ImageT const > getImage() const
void setStatus(Status status)
Set the candidate's status.
int getInt(const std::string &name) const
return an integer value associated with the given name.
void apply(lsst::afw::image::MaskedImage< PixelT > const &image)
MaskedImagePtr _scienceMaskedImage
Subimage around which you build kernel.
Class stored in SpatialCells for spatial Kernel fitting.
a container for holding hierarchical configuration data in memory.
std::shared_ptr< const Image< PixelT > > ConstPtr
float getYCenter() const
Return the object's row-centre.
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
Image Subtraction helper functions.
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
std::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
afw::math::Kernel::Ptr getKernel(CandidateSwitch cand) const
Return results of kernel solution.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
void _buildKernelSolution(afw::math::KernelList const &basisList, std::shared_ptr< Eigen::MatrixXd > hMat)
A class to evaluate image statistics.
afwImage::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)
Implement fundamental difference imaging step of convolution and subtraction : D = I - (K*T + bg) whe...
Declaration of classes to store the solution for convolution kernels.
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's variance.
table::Key< table::Array< Kernel::Pixel > > image
LSST DM logging module built on log4cxx.
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
An include file to include the header files for lsst::afw::image.
boost::shared_ptr< Kernel > Ptr
std::shared_ptr< afw::image::Image< afw::image::VariancePixel > > VariancePtr
pex::policy::Policy _policy
Policy.
int getId() const
Return the candidate's unique ID.
double getKsum(CandidateSwitch cand) const
std::shared_ptr< afw::image::MaskedImage< PixelT > > MaskedImagePtr
A class to manipulate images, masks, and variance as a single object.
float getXCenter() const
Return the object's column-centre.
std::shared_ptr< Image< PixelT > > Ptr
Class used by SpatialModelCell for spatial Kernel fitting.
Class to calculate difference image statistics.
double _coreFlux
Mean S/N in the science image.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
double getBackground(CandidateSwitch cand) const
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
boost::shared_ptr< ImageT > getKernelImage(CandidateSwitch cand) const
KernelCandidate(float const xCenter, float const yCenter, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage, pex::policy::Policy const &policy)
Constructor.
std::shared_ptr< afw::table::SourceRecord > SourcePtr
A class to represent a 2-dimensional array of pixels.
Image Subtraction helper functions.
std::vector< boost::shared_ptr< Kernel > > KernelList