LSSTApplications  20.0.0
LSSTDataManagementBasePackage
KernelSolution.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
12 #ifndef LSST_IP_DIFFIM_KERNELSOLUTION_H
13 #define LSST_IP_DIFFIM_KERNELSOLUTION_H
14 
15 #include <memory>
16 #include "Eigen/Core"
17 
18 #include "lsst/afw/math.h"
19 #include "lsst/afw/image.h"
20 #include "lsst/geom.h"
21 #include "lsst/daf/base.h"
22 
23 namespace lsst {
24 namespace ip {
25 namespace diffim {
26 
27  /*
28  * @brief Method used to solve for M and B
29  */
30 
32  public:
36 
38  NONE = 0,
41  LU = 3,
42  EIGENVECTOR = 4
43  };
44 
47  SVD = 1
48  };
49 
50  explicit KernelSolution(Eigen::MatrixXd mMat,
51  Eigen::VectorXd bVec,
52  bool fitForBackground);
53  explicit KernelSolution(bool fitForBackground);
54  explicit KernelSolution();
55 
56  virtual ~KernelSolution() {};
57  virtual void solve();
58  virtual void solve(Eigen::MatrixXd const& mMat,
59  Eigen::VectorXd const& bVec);
61  virtual double getConditionNumber(ConditionNumberType conditionType);
62  virtual double getConditionNumber(Eigen::MatrixXd const& mMat, ConditionNumberType conditionType);
63 
64  inline Eigen::MatrixXd const& getM() {return _mMat;}
65  inline Eigen::VectorXd const& getB() {return _bVec;}
66  void printM() {std::cout << _mMat << std::endl;}
67  void printB() {std::cout << _bVec << std::endl;}
68  void printA() {std::cout << _aVec << std::endl;}
69  inline int getId() const { return _id; }
70 
71  protected:
72  int _id;
73  Eigen::MatrixXd _mMat;
74  Eigen::VectorXd _bVec;
75  Eigen::VectorXd _aVec;
78  static int _SolutionId;
79 
80  };
81 
82  template <typename InputT>
84  public:
86 
88  bool fitForBackground);
89  virtual ~StaticKernelSolution() {};
90 
91  /* Overrides KernelSolution */
92  void solve();
93 
94  /* Used by RegularizedKernelSolution */
95  virtual void build(lsst::afw::image::Image<InputT> const &templateImage,
96  lsst::afw::image::Image<InputT> const &scienceImage,
100  virtual double getBackground();
101  virtual double getKsum();
103 
104  protected:
105  Eigen::MatrixXd _cMat;
106  Eigen::VectorXd _iVec;
107  Eigen::VectorXd _ivVec;
108 
110  double _background;
111  double _kSum;
112 
113  void _setKernel();
114  void _setKernelUncertainty();
115  };
116 
117 
118  template <typename InputT>
120  public:
122 
124  bool fitForBackground);
125  virtual ~MaskedKernelSolution() {};
126  virtual void buildOrig(lsst::afw::image::Image<InputT> const &templateImage,
127  lsst::afw::image::Image<InputT> const &scienceImage,
129  const &varianceEstimate,
131 
132  virtual void buildWithMask(lsst::afw::image::Image<InputT> const &templateImage,
133  lsst::afw::image::Image<InputT> const &scienceImage,
135  const &varianceEstimate,
137 
138  virtual void buildSingleMaskOrig(lsst::afw::image::Image<InputT> const &templateImage,
139  lsst::afw::image::Image<InputT> const &scienceImage,
141  const &varianceEstimate,
142  lsst::geom::Box2I maskBox);
143  };
144 
145 
146 
147  template <typename InputT>
149  public:
151 
153  bool fitForBackground,
154  Eigen::MatrixXd const& hMat,
156  );
158  void solve();
159  double getLambda() {return _lambda;}
160  double estimateRisk(double maxCond);
161 
162  /* Include additive term (_lambda * _hMat) in M matrix? */
163  Eigen::MatrixXd getM(bool includeHmat = true);
164 
165  private:
166  Eigen::MatrixXd const _hMat;
167  double _lambda;
169 
170  std::vector<double> _createLambdaSteps();
171  };
172 
173 
175  public:
177 
178  /* Creates a polynomial SpatialFunction */
180  lsst::afw::math::Kernel::SpatialFunctionPtr spatialKernelFunction,
183  );
184 
186 
187  void addConstraint(float xCenter, float yCenter,
188  Eigen::MatrixXd const& qMat,
189  Eigen::VectorXd const& wVec);
190 
191  void solve();
195 
196  private:
197  lsst::afw::math::Kernel::SpatialFunctionPtr _spatialKernelFunction;
198  bool _constantFirstTerm;
199 
202  double _kSum;
203 
205  int _nbases;
206  int _nkt;
207  int _nbt;
208  int _nt;
209 
210  void _setKernel();
211  void _setKernelUncertainty();
212  };
213 
214 }}} // end of namespace lsst::ip::diffim
215 
216 #endif
lsst::ip::diffim::KernelSolution::printA
void printA()
Definition: KernelSolution.h:68
lsst::ip::diffim::SpatialKernelSolution::makeKernelImage
std::shared_ptr< lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel > > makeKernelImage(lsst::geom::Point2D const &pos)
Definition: KernelSolution.cc:1450
lsst::ip::diffim::StaticKernelSolution::getKernel
virtual std::shared_ptr< lsst::afw::math::Kernel > getKernel()
Definition: KernelSolution.cc:215
lsst::ip::diffim::RegularizedKernelSolution
Definition: KernelSolution.h:148
std::shared_ptr
STL class.
lsst::ip::diffim::KernelSolution::SVD
@ SVD
Definition: KernelSolution.h:47
lsst::afw::image::Mask< lsst::afw::image::MaskPixel >
lsst::ip::diffim::MaskedKernelSolution::Ptr
std::shared_ptr< MaskedKernelSolution< InputT > > Ptr
Definition: KernelSolution.h:121
lsst::ip::diffim::KernelSolution::getB
Eigen::VectorXd const & getB()
Definition: KernelSolution.h:65
lsst::ip::diffim::KernelSolution::solve
virtual void solve()
Definition: KernelSolution.cc:90
lsst::ip::diffim::StaticKernelSolution::getBackground
virtual double getBackground()
Definition: KernelSolution.cc:235
lsst::ip::diffim::StaticKernelSolution::StaticKernelSolution
StaticKernelSolution(lsst::afw::math::KernelList const &basisList, bool fitForBackground)
Definition: KernelSolution.cc:195
lsst::ip::diffim::KernelSolution::CHOLESKY_LDLT
@ CHOLESKY_LDLT
Definition: KernelSolution.h:39
lsst::ip::diffim::SpatialKernelSolution::~SpatialKernelSolution
virtual ~SpatialKernelSolution()
Definition: KernelSolution.h:185
std::pair
lsst::ip::diffim::StaticKernelSolution::_kernel
std::shared_ptr< lsst::afw::math::Kernel > _kernel
Derived single-object convolution kernel.
Definition: KernelSolution.h:109
lsst::ip::diffim::KernelSolution::getM
Eigen::MatrixXd const & getM()
Definition: KernelSolution.h:64
lsst::ip::diffim::KernelSolution::ImageT
lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel > ImageT
Definition: KernelSolution.h:35
lsst::ip::diffim::KernelSolution::NONE
@ NONE
Definition: KernelSolution.h:38
std::vector< std::shared_ptr< Kernel > >
lsst::ip::diffim::SpatialKernelSolution::getSolutionPair
std::pair< std::shared_ptr< lsst::afw::math::LinearCombinationKernel >, lsst::afw::math::Kernel::SpatialFunctionPtr > getSolutionPair()
Definition: KernelSolution.cc:1480
lsst::ip::diffim::StaticKernelSolution::solve
void solve()
Definition: KernelSolution.cc:396
lsst::ip::diffim::RegularizedKernelSolution::estimateRisk
double estimateRisk(double maxCond)
Definition: KernelSolution.cc:1072
lsst::ip::diffim::StaticKernelSolution::getKsum
virtual double getKsum()
Definition: KernelSolution.cc:243
lsst::ip::diffim::KernelSolution::_bVec
Eigen::VectorXd _bVec
Derived least squares B vector.
Definition: KernelSolution.h:74
geom.h
lsst::ip::diffim::KernelSolution::getId
int getId() const
Definition: KernelSolution.h:69
lsst::ip::diffim::KernelSolution::PixelT
lsst::afw::math::Kernel::Pixel PixelT
Definition: KernelSolution.h:34
lsst::ip::diffim::KernelSolution::_SolutionId
static int _SolutionId
Unique identifier for solution.
Definition: KernelSolution.h:78
lsst::ip::diffim::StaticKernelSolution::build
virtual void build(lsst::afw::image::Image< InputT > const &templateImage, lsst::afw::image::Image< InputT > const &scienceImage, lsst::afw::image::Image< lsst::afw::image::VariancePixel > const &varianceEstimate)
Definition: KernelSolution.cc:261
lsst::ip::diffim::SpatialKernelSolution
Definition: KernelSolution.h:174
math.h
lsst::ip::diffim::KernelSolution::_fitForBackground
bool _fitForBackground
Background terms included in fit.
Definition: KernelSolution.h:77
lsst::ip::diffim::KernelSolution::LU
@ LU
Definition: KernelSolution.h:41
lsst::ip::diffim::StaticKernelSolution::~StaticKernelSolution
virtual ~StaticKernelSolution()
Definition: KernelSolution.h:89
lsst::ip::diffim::RegularizedKernelSolution::getLambda
double getLambda()
Definition: KernelSolution.h:159
lsst::ip::diffim::MaskedKernelSolution::buildSingleMaskOrig
virtual void buildSingleMaskOrig(lsst::afw::image::Image< InputT > const &templateImage, lsst::afw::image::Image< InputT > const &scienceImage, lsst::afw::image::Image< lsst::afw::image::VariancePixel > const &varianceEstimate, lsst::geom::Box2I maskBox)
Definition: KernelSolution.cc:828
lsst::ip::diffim::StaticKernelSolution::_background
double _background
Derived differential background estimate.
Definition: KernelSolution.h:110
lsst::ip::diffim::KernelSolution::ConditionNumberType
ConditionNumberType
Definition: KernelSolution.h:45
lsst::ip::diffim::KernelSolution::_solvedBy
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
Definition: KernelSolution.h:76
lsst.pipe.drivers.visualizeVisit.background
background
Definition: visualizeVisit.py:37
lsst::ip::diffim::KernelSolution::printB
void printB()
Definition: KernelSolution.h:67
lsst::ip::diffim::KernelSolution::_mMat
Eigen::MatrixXd _mMat
Derived least squares M matrix.
Definition: KernelSolution.h:73
lsst::ip::diffim::KernelSolution::KernelSolution
KernelSolution()
Definition: KernelSolution.cc:81
std::cout
lsst::ip::diffim::KernelSolution::getSolvedBy
KernelSolvedBy getSolvedBy()
Definition: KernelSolution.h:60
lsst::ip::diffim::KernelSolution::getConditionNumber
virtual double getConditionNumber(ConditionNumberType conditionType)
Definition: KernelSolution.cc:94
lsst::ip::diffim::RegularizedKernelSolution::~RegularizedKernelSolution
virtual ~RegularizedKernelSolution()
Definition: KernelSolution.h:157
lsst::ip::diffim::KernelSolution::~KernelSolution
virtual ~KernelSolution()
Definition: KernelSolution.h:56
lsst::ip::diffim::StaticKernelSolution::_setKernelUncertainty
void _setKernelUncertainty()
Not implemented.
Definition: KernelSolution.cc:462
lsst::ip::diffim::KernelSolution
Definition: KernelSolution.h:31
image.h
lsst::ip::diffim::RegularizedKernelSolution::RegularizedKernelSolution
RegularizedKernelSolution(lsst::afw::math::KernelList const &basisList, bool fitForBackground, Eigen::MatrixXd const &hMat, lsst::daf::base::PropertySet const &ps)
Definition: KernelSolution.cc:1059
lsst::ip::diffim::KernelSolution::_id
int _id
Unique ID for object.
Definition: KernelSolution.h:72
lsst::ip::diffim::StaticKernelSolution::_ivVec
Eigen::VectorXd _ivVec
Inverse variance.
Definition: KernelSolution.h:107
lsst::ip::diffim::MaskedKernelSolution::buildWithMask
virtual void buildWithMask(lsst::afw::image::Image< InputT > const &templateImage, lsst::afw::image::Image< InputT > const &scienceImage, lsst::afw::image::Image< lsst::afw::image::VariancePixel > const &varianceEstimate, lsst::afw::image::Mask< lsst::afw::image::MaskPixel > const &pixelMask)
Definition: KernelSolution.cc:500
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::ip::diffim::StaticKernelSolution::_iVec
Eigen::VectorXd _iVec
Vectorized I.
Definition: KernelSolution.h:106
lsst::ip::diffim::MaskedKernelSolution
Definition: KernelSolution.h:119
lsst::ip::diffim::StaticKernelSolution
Definition: KernelSolution.h:83
lsst::ip::diffim::KernelSolution::EIGENVECTOR
@ EIGENVECTOR
Definition: KernelSolution.h:42
lsst::ip::diffim::SpatialKernelSolution::solve
void solve()
Definition: KernelSolution.cc:1461
lsst::ip::diffim::StaticKernelSolution::_setKernel
void _setKernel()
Set kernel after solution.
Definition: KernelSolution.cc:422
std::endl
T endl(T... args)
lsst::ip::diffim::MaskedKernelSolution::MaskedKernelSolution
MaskedKernelSolution(lsst::afw::math::KernelList const &basisList, bool fitForBackground)
Definition: KernelSolution.cc:492
lsst::ip::diffim::RegularizedKernelSolution::solve
void solve()
Definition: KernelSolution.cc:1148
lsst::ip::diffim::SpatialKernelSolution::addConstraint
void addConstraint(float xCenter, float yCenter, Eigen::MatrixXd const &qMat, Eigen::VectorXd const &wVec)
Definition: KernelSolution.cc:1337
lsst::ip::diffim::MaskedKernelSolution::~MaskedKernelSolution
virtual ~MaskedKernelSolution()
Definition: KernelSolution.h:125
lsst::geom::Point< double, 2 >
lsst::ip::diffim::MaskedKernelSolution::buildOrig
virtual void buildOrig(lsst::afw::image::Image< InputT > const &templateImage, lsst::afw::image::Image< InputT > const &scienceImage, lsst::afw::image::Image< lsst::afw::image::VariancePixel > const &varianceEstimate, lsst::afw::image::Mask< lsst::afw::image::MaskPixel > pixelMask)
Definition: KernelSolution.cc:665
lsst::daf::base::PropertySet
Class for storing generic metadata.
Definition: PropertySet.h:67
lsst::ip::diffim::StaticKernelSolution::_cMat
Eigen::MatrixXd _cMat
K_i x R.
Definition: KernelSolution.h:105
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::ip::diffim::StaticKernelSolution::makeKernelImage
virtual std::shared_ptr< lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel > > makeKernelImage()
Definition: KernelSolution.cc:223
lsst::ip::diffim::KernelSolution::KernelSolvedBy
KernelSolvedBy
Definition: KernelSolution.h:37
lsst::ip::diffim::KernelSolution::printM
void printM()
Definition: KernelSolution.h:66
lsst::ip::diffim::SpatialKernelSolution::Ptr
std::shared_ptr< SpatialKernelSolution > Ptr
Definition: KernelSolution.h:176
lsst::ip::diffim::StaticKernelSolution::Ptr
std::shared_ptr< StaticKernelSolution< InputT > > Ptr
Definition: KernelSolution.h:85
lsst::ip::diffim::KernelSolution::EIGENVALUE
@ EIGENVALUE
Definition: KernelSolution.h:46
lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel >
lsst::ip::diffim::KernelSolution::CHOLESKY_LLT
@ CHOLESKY_LLT
Definition: KernelSolution.h:40
lsst::ip::diffim::SpatialKernelSolution::SpatialKernelSolution
SpatialKernelSolution(lsst::afw::math::KernelList const &basisList, lsst::afw::math::Kernel::SpatialFunctionPtr spatialKernelFunction, lsst::afw::math::Kernel::SpatialFunctionPtr background, lsst::daf::base::PropertySet const &ps)
Definition: KernelSolution.cc:1283
lsst::ip::diffim::KernelSolution::Ptr
std::shared_ptr< KernelSolution > Ptr
Definition: KernelSolution.h:33
base.h
lsst::afw::math::Kernel::Pixel
double Pixel
Definition: Kernel.h:113
lsst::ip::diffim::RegularizedKernelSolution::Ptr
std::shared_ptr< RegularizedKernelSolution< InputT > > Ptr
Definition: KernelSolution.h:150
lsst::ip::diffim::KernelSolution::_aVec
Eigen::VectorXd _aVec
Derived least squares solution matrix.
Definition: KernelSolution.h:75
lsst::ip::diffim::StaticKernelSolution::getSolutionPair
virtual std::pair< std::shared_ptr< lsst::afw::math::Kernel >, double > getSolutionPair()
Definition: KernelSolution.cc:252
lsst::ip::diffim::StaticKernelSolution::_kSum
double _kSum
Derived kernel sum.
Definition: KernelSolution.h:111