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(ps.getAsBool(
"fitForBackground")),
47 _kernelSolutionOrig(),
48 _kernelSolutionPca() {
51 int candidateCoreRadius = _ps->getAsInt(
"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(ps.getAsBool(
"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 (_ps->getAsBool(
"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 (_ps->getAsBool(
"iterateSingleKernel") && (!(_ps->getAsBool(
"constantVarianceWeighting")))) {
126 _buildKernelSolution(basisList, hMat);
132 _isInitialized =
true;
135 template <
typename PixelT>
137 Eigen::MatrixXd
const& hMat) {
138 bool checkConditionNumber = _ps->getAsBool(
"checkConditionNumber");
139 double maxConditionNumber = _ps->getAsDouble(
"maxConditionNumber");
140 std::string conditionNumberType = _ps->getAsString(
"conditionNumberType");
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) {
157 new RegularizedKernelSolution<PixelT>(basisList, _fitForBackground, hMat, *_ps));
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();
171 new RegularizedKernelSolution<PixelT>(basisList, _fitForBackground, hMat, *_ps));
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) {
189 new StaticKernelSolution<PixelT>(basisList, _fitForBackground));
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();
203 new StaticKernelSolution<PixelT>(basisList, _fitForBackground));
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>