lsst.ip.diffim  20.0.0-9-g4aef684+2aec92f563
BuildSpatialKernelVisitor.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
12 #include <memory>
13 #include "boost/timer.hpp"
14 
15 #include "Eigen/Core"
16 #include "Eigen/Cholesky"
17 #include "Eigen/LU"
18 #include "Eigen/QR"
19 
20 #include "lsst/afw/math.h"
21 #include "lsst/geom.h"
22 #include "lsst/log/Log.h"
25 
29 
30 namespace afwMath = lsst::afw::math;
31 namespace geom = lsst::afw::geom;
32 namespace dafBase = lsst::daf::base;
34 
35 namespace lsst {
36 namespace ip {
37 namespace diffim {
38 namespace detail {
72  template<typename PixelT>
74  lsst::afw::math::KernelList const& basisList,
75  lsst::geom::Box2I const& regionBBox,
77  ) :
78  afwMath::CandidateVisitor(),
79  _kernelSolution(),
80  _nCandidates(0)
81  {
82  int spatialKernelOrder = ps.getAsInt("spatialKernelOrder");
83  afwMath::Kernel::SpatialFunctionPtr spatialKernelFunction;
84 
85  int fitForBackground = ps.getAsBool("fitForBackground");
86  int spatialBgOrder = fitForBackground ? ps.getAsInt("spatialBgOrder") : 0;
88 
89  std::string spatialModelType = ps.getAsString("spatialModelType");
90  if (spatialModelType == "chebyshev1") {
91  spatialKernelFunction = afwMath::Kernel::SpatialFunctionPtr(
92  new afwMath::Chebyshev1Function2<double>(spatialKernelOrder, geom::Box2D(regionBBox))
93  );
95  new afwMath::Chebyshev1Function2<double>(spatialBgOrder, geom::Box2D(regionBBox))
96  );
97 
98  }
99  else if (spatialModelType == "polynomial") {
100  spatialKernelFunction = afwMath::Kernel::SpatialFunctionPtr(
101  new afwMath::PolynomialFunction2<double>(spatialKernelOrder)
102  );
104  new afwMath::PolynomialFunction2<double>(spatialBgOrder)
105  );
106  }
107  else {
109  str(boost::format("Invalid type (%s) for spatial models") %
110  spatialModelType));
111  }
112 
113  /* */
114 
115  _kernelSolution = std::shared_ptr<SpatialKernelSolution>(
116  new SpatialKernelSolution(basisList, spatialKernelFunction, background, ps));
117  };
118 
119 
120  template<typename PixelT>
123  ) {
124  KernelCandidate<PixelT> *kCandidate = dynamic_cast<KernelCandidate<PixelT> *>(candidate);
125  if (kCandidate == NULL) {
127  "Failed to cast SpatialCellCandidate to KernelCandidate");
128  }
129  if (!(kCandidate->isInitialized())) {
131  LOGL_DEBUG("TRACE2.ip.diffim.BuildSpatialKernelVisitor.processCandidate",
132  "Cannot process candidate %d, continuing", kCandidate->getId());
133  return;
134  }
135 
136  LOGL_DEBUG("TRACE5.ip.diffim.BuildSpatialKernelVisitor.processCandidate",
137  "Processing candidate %d", kCandidate->getId());
138  _nCandidates += 1;
139 
140  /*
141  Build the spatial kernel from the most recent fit, e.g. if its Pca
142  you want to build a spatial model on the Pca basis, not original
143  basis
144  */
145  _kernelSolution->addConstraint(kCandidate->getXCenter(),
146  kCandidate->getYCenter(),
147  kCandidate->getKernelSolution(
149  )->getM(),
150  kCandidate->getKernelSolution(
152  )->getB());
153 
154  }
155 
156  template<typename PixelT>
158  _kernelSolution->solve();
159  }
160 
161  template<typename PixelT>
164  return _kernelSolution->getSolutionPair();
165  }
166 
167  typedef float PixelT;
168  template class BuildSpatialKernelVisitor<PixelT>;
169 
170 }}}} // end of namespace lsst::ip::diffim::detail
lsst::daf::base::PropertySet::getAsInt
int getAsInt(std::string const &name) const
lsst::afw::math::SpatialCellCandidate
lsst::daf::base::PropertySet::getAsBool
bool getAsBool(std::string const &name) const
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::math::Chebyshev1Function2
lsst::afw::math::SpatialCellImageCandidate::setStatus
void setStatus(Status status)
std::pair
Log.h
std::vector
STL class.
lsst::ip::diffim::KernelCandidate
Class stored in SpatialCells for spatial Kernel fitting.
Definition: KernelCandidate.h:39
lsst::ip::diffim::detail::BuildSpatialKernelVisitor::processCandidate
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
Definition: BuildSpatialKernelVisitor.cc:121
lsst::ip::diffim::detail::PixelT
float PixelT
Definition: AssessSpatialKernelVisitor.cc:208
BuildSpatialKernelVisitor.h
Declaration of BuildSpatialKernelVisitor.
KernelSolution.h
Declaration of classes to store the solution for convolution kernels.
Runtime.h
lsst::ip::diffim::SpatialKernelSolution
Definition: KernelSolution.h:174
lsst::afw::math::SpatialCellCandidate::BAD
BAD
lsst::ip::diffim::detail::BuildSpatialKernelVisitor::getSolutionPair
std::pair< std::shared_ptr< lsst::afw::math::LinearCombinationKernel >, lsst::afw::math::Kernel::SpatialFunctionPtr > getSolutionPair()
Definition: BuildSpatialKernelVisitor.cc:163
lsst::afw::math::SpatialCellImageCandidate::getXCenter
float getXCenter() const
lsst::daf::base::PropertySet::getAsString
std::string getAsString(std::string const &name) const
lsst::pex::exceptions::LogicError
math.h
PropertySet.h
geom.h
lsst::afw::math::SpatialCellImageCandidate::getId
int getId() const
lsst
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
lsst::ip::diffim::KernelCandidate::getKernelSolution
std::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
Definition: KernelCandidate.cc:322
lsst::geom
lsst::daf::base
lsst::ip::diffim::detail::BuildSpatialKernelVisitor::BuildSpatialKernelVisitor
BuildSpatialKernelVisitor(lsst::afw::math::KernelList const &basisList, lsst::geom::Box2I const &regionBBox, lsst::daf::base::PropertySet const &ps)
Definition: BuildSpatialKernelVisitor.cc:73
lsst::ip::diffim::KernelCandidate::isInitialized
bool isInitialized() const
Definition: KernelCandidate.h:128
LOGL_DEBUG
#define LOGL_DEBUG(logger, message...)
lsst::afw::math
KernelCandidate.h
Class used by SpatialModelCell for spatial Kernel fitting.
lsst::ip::diffim::detail::BuildSpatialKernelVisitor::solveLinearEquation
void solveLinearEquation()
Definition: BuildSpatialKernelVisitor.cc:157
lsst::ip::diffim::detail::BuildSpatialKernelVisitor
Creates a spatial kernel and background from a list of candidates.
Definition: BuildSpatialKernelVisitor.h:28
lsst::daf::base::PropertySet
lsst::geom::Box2I
lsst::pex::exceptions
lsst::pex::exceptions::Exception
lsst::afw::math::PolynomialFunction2
lsst::geom::Box2D
lsst::afw::math::Kernel::SpatialFunctionPtr
std::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
lsst::afw::math::SpatialCellImageCandidate::getYCenter
float getYCenter() const
lsst::afw::geom