LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
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
Declaration of BuildSpatialKernelVisitor.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
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.
Definition: Log.h:515
2-dimensional weighted sum of Chebyshev polynomials of the first kind.
std::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
Definition: Kernel.h:113
2-dimensional polynomial function with cross terms
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
float getYCenter() const
Return the object's row-centre.
Definition: SpatialCell.h:91
float getXCenter() const
Return the object's column-centre.
Definition: SpatialCell.h:88
int getId() const
Return the candidate's unique ID.
Definition: SpatialCell.h:102
void setStatus(Status status)
Set the candidate's status.
Definition: SpatialCell.cc:53
Class for storing generic metadata.
Definition: PropertySet.h:66
std::string getAsString(std::string const &name) const
Get the last value for a string property name (possibly hierarchical).
int getAsInt(std::string const &name) const
Get the last value for a bool/char/short/int property name (possibly hierarchical).
bool getAsBool(std::string const &name) const
Get the last value for a bool property name (possibly hierarchical).
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
An integer coordinate rectangle.
Definition: Box.h:55
Class stored in SpatialCells for spatial Kernel fitting.
std::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
Creates a spatial kernel and background from a list of candidates.
std::pair< std::shared_ptr< lsst::afw::math::LinearCombinationKernel >, lsst::afw::math::Kernel::SpatialFunctionPtr > getSolutionPair()
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
BuildSpatialKernelVisitor(lsst::afw::math::KernelList const &basisList, lsst::geom::Box2I const &regionBBox, lsst::daf::base::PropertySet const &ps)
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.