31template <
typename PixelT>
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);
53 }
catch (pexExcept::Exception& e) {
55 "Unable to calculate core imstats for rating Candidate %d", this->
getId());
61 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate",
"Candidate %d at %.2f %.2f with rating %.2f",
65template <
typename PixelT>
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);
116 }
catch (pexExcept::Exception& e) {
120 if (_ps->getAsBool(
"iterateSingleKernel") && (!(_ps->getAsBool(
"constantVarianceWeighting")))) {
122 _varianceEstimate =
diffim.getVariance();
125 _buildKernelSolution(basisList, hMat);
126 }
catch (pexExcept::Exception& e) {
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") {
146 throw LSST_EXCEPT(pexExcept::TypeError,
"conditionNumberType not recognized");
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());
167 _kernelSolutionPca->solve();
169 _kernelSolutionOrig = std::shared_ptr<StaticKernelSolution<PixelT> >(
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());
181 _kernelSolutionOrig->solve();
184 _useRegularization =
false;
185 LOGL_DEBUG(
"TRACE4.ip.diffim.KernelCandidate.build",
"Not using kernel regularization");
186 if (_isInitialized) {
187 _kernelSolutionPca = std::shared_ptr<StaticKernelSolution<PixelT> >(
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());
199 _kernelSolutionPca->solve();
201 _kernelSolutionOrig = std::shared_ptr<StaticKernelSolution<PixelT> >(
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());
213 _kernelSolutionOrig->solve();
218template <
typename PixelT>
222 if (_kernelSolutionOrig)
223 return _kernelSolutionOrig->getKernel();
225 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Original kernel does not exist");
228 if (_kernelSolutionPca)
229 return _kernelSolutionPca->getKernel();
231 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Pca kernel does not exist");
234 if (_kernelSolutionPca)
235 return _kernelSolutionPca->getKernel();
236 else if (_kernelSolutionOrig)
237 return _kernelSolutionOrig->getKernel();
239 throw LSST_EXCEPT(pexExcept::RuntimeError,
"No kernels exist");
246template <
typename PixelT>
250 if (_kernelSolutionOrig)
251 return _kernelSolutionOrig->getBackground();
253 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Original kernel does not exist");
256 if (_kernelSolutionPca)
257 return _kernelSolutionPca->getBackground();
259 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Pca kernel does not exist");
262 if (_kernelSolutionPca)
263 return _kernelSolutionPca->getBackground();
264 else if (_kernelSolutionOrig)
265 return _kernelSolutionOrig->getBackground();
267 throw LSST_EXCEPT(pexExcept::RuntimeError,
"No kernels exist");
274template <
typename PixelT>
278 if (_kernelSolutionOrig)
279 return _kernelSolutionOrig->getKsum();
281 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Original kernel does not exist");
284 if (_kernelSolutionPca)
285 return _kernelSolutionPca->getKsum();
287 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Pca kernel does not exist");
290 if (_kernelSolutionPca)
291 return _kernelSolutionPca->getKsum();
292 else if (_kernelSolutionOrig)
293 return _kernelSolutionOrig->getKsum();
295 throw LSST_EXCEPT(pexExcept::RuntimeError,
"No kernels exist");
302template <
typename PixelT>
307 if (_kernelSolutionOrig)
308 return _kernelSolutionOrig->makeKernelImage();
310 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Original kernel does not exist");
313 if (_kernelSolutionPca)
314 return _kernelSolutionPca->makeKernelImage();
316 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Pca kernel does not exist");
319 if (_kernelSolutionPca)
320 return _kernelSolutionPca->makeKernelImage();
321 else if (_kernelSolutionOrig)
322 return _kernelSolutionOrig->makeKernelImage();
324 throw LSST_EXCEPT(pexExcept::RuntimeError,
"No kernels exist");
331template <
typename PixelT>
336template <
typename PixelT>
341 if (_kernelSolutionOrig)
342 return _kernelSolutionOrig;
344 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Original kernel does not exist");
347 if (_kernelSolutionPca)
348 return _kernelSolutionPca;
350 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Pca kernel does not exist");
353 if (_kernelSolutionPca)
354 return _kernelSolutionPca;
355 else if (_kernelSolutionOrig)
356 return _kernelSolutionOrig;
358 throw LSST_EXCEPT(pexExcept::RuntimeError,
"No kernels exist");
365template <
typename PixelT>
369 if (_kernelSolutionOrig)
371 _kernelSolutionOrig->getBackground());
373 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Original kernel does not exist");
376 if (_kernelSolutionPca)
378 _kernelSolutionPca->getBackground());
380 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Pca kernel does not exist");
383 if (_kernelSolutionPca)
385 _kernelSolutionPca->getBackground());
386 else if (_kernelSolutionOrig)
388 _kernelSolutionOrig->getBackground());
390 throw LSST_EXCEPT(pexExcept::RuntimeError,
"No kernels exist");
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.
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.
SpatialCellImageCandidate(float const xCenter, float const yCenter)
ctor
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
std::shared_ptr< afw::image::MaskedImage< PixelT > > MaskedImagePtr
std::shared_ptr< afw::image::Image< afw::image::VariancePixel > > VariancePtr
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< afw::table::SourceRecord > SourcePtr
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.
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
std::vector< std::shared_ptr< Kernel > > KernelList
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.