LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
BuildSpatialKernelVisitor.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
12 #include "boost/shared_ptr.hpp"
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/afw/geom.h"
22 #include "lsst/pex/policy/Policy.h"
24 #include "lsst/pex/logging/Trace.h"
25 
29 
30 namespace afwMath = lsst::afw::math;
31 namespace afwGeom = lsst::afw::geom;
32 namespace pexLogging = lsst::pex::logging;
33 namespace pexPolicy = lsst::pex::policy;
34 namespace pexExcept = lsst::pex::exceptions;
35 
36 namespace lsst {
37 namespace ip {
38 namespace diffim {
39 namespace detail {
73  template<typename PixelT>
75  lsst::afw::math::KernelList const& basisList,
76  lsst::afw::geom::Box2I const& regionBBox,
78  ) :
79  afwMath::CandidateVisitor(),
80  _kernelSolution(),
81  _nCandidates(0)
82  {
83  int spatialKernelOrder = policy.getInt("spatialKernelOrder");
84  afwMath::Kernel::SpatialFunctionPtr spatialKernelFunction;
85 
86  int fitForBackground = policy.getBool("fitForBackground");
87  int spatialBgOrder = fitForBackground ? policy.getInt("spatialBgOrder") : 0;
89 
90  std::string spatialModelType = policy.getString("spatialModelType");
91  if (spatialModelType == "chebyshev1") {
92  spatialKernelFunction = afwMath::Kernel::SpatialFunctionPtr(
93  new afwMath::Chebyshev1Function2<double>(spatialKernelOrder, afwGeom::Box2D(regionBBox))
94  );
96  new afwMath::Chebyshev1Function2<double>(spatialBgOrder, afwGeom::Box2D(regionBBox))
97  );
98 
99  }
100  else if (spatialModelType == "polynomial") {
101  spatialKernelFunction = afwMath::Kernel::SpatialFunctionPtr(
102  new afwMath::PolynomialFunction2<double>(spatialKernelOrder)
103  );
105  new afwMath::PolynomialFunction2<double>(spatialBgOrder)
106  );
107  }
108  else {
110  str(boost::format("Invalid type (%s) for spatial models") %
111  spatialModelType));
112  }
113 
114  /* */
115 
116  _kernelSolution = boost::shared_ptr<SpatialKernelSolution>(
117  new SpatialKernelSolution(basisList, spatialKernelFunction, background, policy));
118  };
119 
120 
121  template<typename PixelT>
124  ) {
125  KernelCandidate<PixelT> *kCandidate = dynamic_cast<KernelCandidate<PixelT> *>(candidate);
126  if (kCandidate == NULL) {
127  throw LSST_EXCEPT(pexExcept::LogicError,
128  "Failed to cast SpatialCellCandidate to KernelCandidate");
129  }
130  if (!(kCandidate->isInitialized())) {
132  pexLogging::TTrace<3>("lsst.ip.diffim.BuildSpatialKernelVisitor.processCandidate",
133  "Cannot process candidate %d, continuing", kCandidate->getId());
134  return;
135  }
136 
137  pexLogging::TTrace<6>("lsst.ip.diffim.BuildSpatialKernelVisitor.processCandidate",
138  "Processing candidate %d", kCandidate->getId());
139  _nCandidates += 1;
140 
141  /*
142  Build the spatial kernel from the most recent fit, e.g. if its Pca
143  you want to build a spatial model on the Pca basis, not original
144  basis
145  */
146  _kernelSolution->addConstraint(kCandidate->getXCenter(),
147  kCandidate->getYCenter(),
148  kCandidate->getKernelSolution(
150  )->getM(),
151  kCandidate->getKernelSolution(
153  )->getB());
154 
155  }
156 
157  template<typename PixelT>
159  _kernelSolution->solve();
160  }
161 
162  template<typename PixelT>
163  std::pair<afwMath::LinearCombinationKernel::Ptr, afwMath::Kernel::SpatialFunctionPtr>
165  return _kernelSolution->getSolutionPair();
166  }
167 
168  typedef float PixelT;
169  template class BuildSpatialKernelVisitor<PixelT>;
170 
171 }}}} // end of namespace lsst::ip::diffim::detail
172 
2-dimensional weighted sum of Chebyshev polynomials of the first kind.
An include file to include the public header files for lsst::afw::math.
boost::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
Definition: Kernel.h:143
void setStatus(Status status)
Set the candidate&#39;s status.
Definition: SpatialCell.cc:61
int getInt(const std::string &name) const
Definition: Policy.h:603
An include file to include the header files for lsst::afw::geom.
Creates a spatial kernel and background from a list of candidates.
Class stored in SpatialCells for spatial Kernel fitting.
bool getBool(const std::string &name) const
Definition: Policy.h:589
boost::shared_ptr< SpatialKernelSolution > _kernelSolution
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
2-dimensional polynomial function with cross terms
const std::string getString(const std::string &name) const
Definition: Policy.h:631
definition of the Trace messaging facilities
float getYCenter() const
Return the object&#39;s row-centre.
Definition: SpatialCell.h:98
Implementation of BuildSpatialKernelVisitor.
An integer coordinate rectangle.
Definition: Box.h:53
Declaration of classes to store the solution for convolution kernels.
BuildSpatialKernelVisitor(lsst::afw::math::KernelList const &basisList, lsst::afw::geom::Box2I const &regionBBox, lsst::pex::policy::Policy policy)
int getId() const
Return the candidate&#39;s unique ID.
Definition: SpatialCell.h:109
float getXCenter() const
Return the object&#39;s column-centre.
Definition: SpatialCell.h:95
Class used by SpatialModelCell for spatial Kernel fitting.
boost::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
std::pair< lsst::afw::math::LinearCombinationKernel::Ptr, lsst::afw::math::Kernel::SpatialFunctionPtr > getSolutionPair()
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A floating-point coordinate rectangle geometry.
Definition: Box.h:271
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
std::vector< boost::shared_ptr< Kernel > > KernelList
Definition: Kernel.h:542