LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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.
An include file to include the header files for lsst::afw::geom.
Creates a spatial kernel and background from a list of candidates.
boost::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
Definition: Kernel.h:143
float getYCenter() const
Return the object&#39;s row-centre.
Definition: SpatialCell.h:98
Class stored in SpatialCells for spatial Kernel fitting.
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
int getId() const
Return the candidate&#39;s unique ID.
Definition: SpatialCell.h:109
definition of the Trace messaging facilities
Implementation of BuildSpatialKernelVisitor.
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 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)
Class used by SpatialModelCell for spatial Kernel fitting.
bool getBool(const std::string &name) const
Definition: Policy.h:589
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
std::vector< boost::shared_ptr< Kernel > > KernelList
Definition: Kernel.h:542
A floating-point coordinate rectangle geometry.
Definition: Box.h:271
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
float getXCenter() const
Return the object&#39;s column-centre.
Definition: SpatialCell.h:95