12 #include "boost/timer.hpp" 32 template <
typename PixelT>
37 :
lsst::
afw::math::SpatialCellImageCandidate(xCenter, yCenter),
38 _templateMaskedImage(templateMaskedImage),
39 _scienceMaskedImage(scienceMaskedImage),
44 _isInitialized(false),
45 _useRegularization(false),
46 _fitForBackground(_policy.getBool(
"fitForBackground")),
47 _kernelSolutionOrig(),
48 _kernelSolutionPca() {
51 int candidateCoreRadius = _policy.
getInt(
"candidateCoreRadius");
53 imstats.
apply(*_scienceMaskedImage, candidateCoreRadius);
56 "Unable to calculate core imstats for ranking Candidate %d", this->
getId());
62 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d at %.2f %.2f with ranking %.2f",
66 template <
typename PixelT>
71 _templateMaskedImage(templateMaskedImage),
72 _scienceMaskedImage(scienceMaskedImage),
76 _coreFlux(source->getPsfInstFlux()),
77 _isInitialized(false),
78 _useRegularization(false),
79 _fitForBackground(_policy.getBool(
"fitForBackground")),
80 _kernelSolutionOrig(),
81 _kernelSolutionPca() {
82 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d at %.2f %.2f with ranking %.2f",
86 template <
typename PixelT>
88 build(basisList, Eigen::MatrixXd());
91 template <
typename PixelT>
93 Eigen::MatrixXd
const& hMat) {
98 var += (*(_templateMaskedImage->getVariance()));
100 if (_policy.
getBool(
"constantVarianceWeighting")) {
108 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d using constant variance of %.2f",
109 this->
getId(), varValue);
116 _buildKernelSolution(basisList, hMat);
121 if (_policy.
getBool(
"iterateSingleKernel") && (!(_policy.
getBool(
"constantVarianceWeighting")))) {
126 _buildKernelSolution(basisList, hMat);
132 _isInitialized =
true;
135 template <
typename PixelT>
137 Eigen::MatrixXd
const& hMat) {
138 bool checkConditionNumber = _policy.
getBool(
"checkConditionNumber");
139 double maxConditionNumber = _policy.
getDouble(
"maxConditionNumber");
142 if (conditionNumberType ==
"SVD") {
144 }
else if (conditionNumberType ==
"EIGENVALUE") {
151 if (hMat.size() > 0) {
152 _useRegularization =
true;
153 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
"Using kernel regularization");
155 if (_isInitialized) {
158 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()), *(_scienceMaskedImage->getImage()),
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());
168 _kernelSolutionPca->solve();
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());
182 _kernelSolutionOrig->solve();
185 _useRegularization =
false;
186 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
"Not using kernel regularization");
187 if (_isInitialized) {
190 _kernelSolutionPca->build(*(_templateMaskedImage->getImage()), *(_scienceMaskedImage->getImage()),
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());
200 _kernelSolutionPca->solve();
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());
214 _kernelSolutionOrig->solve();
219 template <
typename PixelT>
222 if (_kernelSolutionOrig)
223 return _kernelSolutionOrig->getKernel();
227 if (_kernelSolutionPca)
228 return _kernelSolutionPca->getKernel();
232 if (_kernelSolutionPca)
233 return _kernelSolutionPca->getKernel();
234 else if (_kernelSolutionOrig)
235 return _kernelSolutionOrig->getKernel();
243 template <
typename PixelT>
246 if (_kernelSolutionOrig)
247 return _kernelSolutionOrig->getBackground();
251 if (_kernelSolutionPca)
252 return _kernelSolutionPca->getBackground();
256 if (_kernelSolutionPca)
257 return _kernelSolutionPca->getBackground();
258 else if (_kernelSolutionOrig)
259 return _kernelSolutionOrig->getBackground();
267 template <
typename PixelT>
270 if (_kernelSolutionOrig)
271 return _kernelSolutionOrig->getKsum();
275 if (_kernelSolutionPca)
276 return _kernelSolutionPca->getKsum();
280 if (_kernelSolutionPca)
281 return _kernelSolutionPca->getKsum();
282 else if (_kernelSolutionOrig)
283 return _kernelSolutionOrig->getKsum();
291 template <
typename PixelT>
295 if (_kernelSolutionOrig)
296 return _kernelSolutionOrig->makeKernelImage();
300 if (_kernelSolutionPca)
301 return _kernelSolutionPca->makeKernelImage();
305 if (_kernelSolutionPca)
306 return _kernelSolutionPca->makeKernelImage();
307 else if (_kernelSolutionOrig)
308 return _kernelSolutionOrig->makeKernelImage();
316 template <
typename PixelT>
321 template <
typename PixelT>
325 if (_kernelSolutionOrig)
326 return _kernelSolutionOrig;
330 if (_kernelSolutionPca)
331 return _kernelSolutionPca;
335 if (_kernelSolutionPca)
336 return _kernelSolutionPca;
337 else if (_kernelSolutionOrig)
338 return _kernelSolutionOrig;
346 template <
typename PixelT>
349 if (_kernelSolutionOrig)
350 return getDifferenceImage(_kernelSolutionOrig->getKernel(), _kernelSolutionOrig->getBackground());
354 if (_kernelSolutionPca)
355 return getDifferenceImage(_kernelSolutionPca->getKernel(), _kernelSolutionPca->getBackground());
359 if (_kernelSolutionPca)
360 return getDifferenceImage(_kernelSolutionPca->getKernel(), _kernelSolutionPca->getBackground());
361 else if (_kernelSolutionOrig)
362 return getDifferenceImage(_kernelSolutionOrig->getKernel(), _kernelSolutionOrig->getBackground());
370 template <
typename PixelT>
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
void setStatus(Status status)
Set the candidate's status.
void apply(lsst::afw::image::MaskedImage< PixelT > const &image)
double getBackground(CandidateSwitch cand) const
Class stored in SpatialCells for spatial Kernel fitting.
a container for holding hierarchical configuration data in memory.
float getXCenter() const
Return the object's column-centre.
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.
Provides consistent interface for LSST exceptions.
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.
int getId() const
Return the candidate's unique ID.
const std::string getString(const std::string &name) const
return a string value associated with the given name .
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
A class to evaluate image statistics.
Declaration of classes to store the solution for convolution kernels.
LSST DM logging module built on log4cxx.
std::shared_ptr< afw::image::Image< afw::image::VariancePixel > > VariancePtr
A base class for image defects.
SpatialCellImageCandidate(float const xCenter, float const yCenter)
ctor
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)
A class to manipulate images, masks, and variance as a single object.
const char * source()
Source function that allows astChannel to source from a Stream.
Class used by SpatialModelCell for spatial Kernel fitting.
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
bool getBool(const std::string &name) const
return a boolean value associated with the given name.
Class to calculate difference image statistics.
double getDouble(const std::string &name) const
return a double value associated with the given name.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
boost::shared_ptr< ImageT const > getImage() const
float getYCenter() const
Return the object's row-centre.
int getInt(const std::string &name) const
return an integer value associated with the given name.
KernelCandidate(float const xCenter, float const yCenter, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage, pex::policy::Policy const &policy)
Constructor.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
A class to represent a 2-dimensional array of pixels.
Image Subtraction helper functions.
double getKsum(CandidateSwitch cand) const