LSSTApplications  18.1.0
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
lsst::ip::diffim::RegularizedKernelSolution< InputT > Class Template Reference

#include <KernelSolution.h>

Inheritance diagram for lsst::ip::diffim::RegularizedKernelSolution< InputT >:
lsst::ip::diffim::StaticKernelSolution< InputT > lsst::ip::diffim::KernelSolution

Public Types

typedef std::shared_ptr< RegularizedKernelSolution< InputT > > Ptr
 
enum  KernelSolvedBy {
  NONE = 0, CHOLESKY_LDLT = 1, CHOLESKY_LLT = 2, LU = 3,
  EIGENVECTOR = 4
}
 
enum  ConditionNumberType { EIGENVALUE = 0, SVD = 1 }
 
typedef lsst::afw::math::Kernel::Pixel PixelT
 
typedef lsst::afw::image::Image< lsst::afw::math::Kernel::PixelImageT
 

Public Member Functions

 RegularizedKernelSolution (lsst::afw::math::KernelList const &basisList, bool fitForBackground, Eigen::MatrixXd const &hMat, lsst::pex::policy::Policy policy)
 
virtual ~RegularizedKernelSolution ()
 
void solve ()
 
double getLambda ()
 
double estimateRisk (double maxCond)
 
Eigen::MatrixXd getM (bool includeHmat=true)
 
virtual void solve (Eigen::MatrixXd const &mMat, Eigen::VectorXd const &bVec)
 
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)
 
virtual std::shared_ptr< lsst::afw::math::KernelgetKernel ()
 
virtual std::shared_ptr< lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel > > makeKernelImage ()
 
virtual double getBackground ()
 
virtual double getKsum ()
 
virtual std::pair< std::shared_ptr< lsst::afw::math::Kernel >, double > getSolutionPair ()
 
KernelSolvedBy getSolvedBy ()
 
virtual double getConditionNumber (ConditionNumberType conditionType)
 
virtual double getConditionNumber (Eigen::MatrixXd const &mMat, ConditionNumberType conditionType)
 
Eigen::MatrixXd const & getM ()
 
Eigen::VectorXd const & getB ()
 
void printM ()
 
void printB ()
 
void printA ()
 
int getId () const
 

Protected Member Functions

void _setKernel ()
 Set kernel after solution. More...
 
void _setKernelUncertainty ()
 Not implemented. More...
 

Protected Attributes

Eigen::MatrixXd _cMat
 K_i x R. More...
 
Eigen::VectorXd _iVec
 Vectorized I. More...
 
Eigen::VectorXd _ivVec
 Inverse variance. More...
 
std::shared_ptr< lsst::afw::math::Kernel_kernel
 Derived single-object convolution kernel. More...
 
double _background
 Derived differential background estimate. More...
 
double _kSum
 Derived kernel sum. More...
 
int _id
 Unique ID for object. More...
 
Eigen::MatrixXd _mMat
 Derived least squares M matrix. More...
 
Eigen::VectorXd _bVec
 Derived least squares B vector. More...
 
Eigen::VectorXd _aVec
 Derived least squares solution matrix. More...
 
KernelSolvedBy _solvedBy
 Type of algorithm used to make solution. More...
 
bool _fitForBackground
 Background terms included in fit. More...
 

Static Protected Attributes

static int _SolutionId = 0
 Unique identifier for solution. More...
 

Detailed Description

template<typename InputT>
class lsst::ip::diffim::RegularizedKernelSolution< InputT >

Definition at line 148 of file KernelSolution.h.

Member Typedef Documentation

◆ ImageT

Definition at line 35 of file KernelSolution.h.

◆ PixelT

Definition at line 34 of file KernelSolution.h.

◆ Ptr

Definition at line 150 of file KernelSolution.h.

Member Enumeration Documentation

◆ ConditionNumberType

Enumerator
EIGENVALUE 
SVD 

Definition at line 45 of file KernelSolution.h.

◆ KernelSolvedBy

Constructor & Destructor Documentation

◆ RegularizedKernelSolution()

template<typename InputT >
lsst::ip::diffim::RegularizedKernelSolution< InputT >::RegularizedKernelSolution ( lsst::afw::math::KernelList const &  basisList,
bool  fitForBackground,
Eigen::MatrixXd const &  hMat,
lsst::pex::policy::Policy  policy 
)

Definition at line 1057 of file KernelSolution.cc.

1063  :
1064  StaticKernelSolution<InputT>(basisList, fitForBackground),
1065  _hMat(hMat),
1066  _policy(policy)
1067  {};

◆ ~RegularizedKernelSolution()

template<typename InputT >
virtual lsst::ip::diffim::RegularizedKernelSolution< InputT >::~RegularizedKernelSolution ( )
inlinevirtual

Definition at line 157 of file KernelSolution.h.

157 {};

Member Function Documentation

◆ _setKernel()

template<typename InputT >
void lsst::ip::diffim::StaticKernelSolution< InputT >::_setKernel ( )
protectedinherited

Set kernel after solution.

Definition at line 420 of file KernelSolution.cc.

420  {
422  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot make solution");
423  }
424 
425  unsigned int const nParameters = _aVec.size();
426  unsigned int const nBackgroundParameters = _fitForBackground ? 1 : 0;
427  unsigned int const nKernelParameters =
429  if (nParameters != (nKernelParameters + nBackgroundParameters))
430  throw LSST_EXCEPT(pexExcept::Exception, "Mismatched sizes in kernel solution");
431 
432  /* Fill in the kernel results */
433  std::vector<double> kValues(nKernelParameters);
434  for (unsigned int idx = 0; idx < nKernelParameters; idx++) {
435  if (std::isnan(_aVec(idx))) {
437  str(boost::format("Unable to determine kernel solution %d (nan)") % idx));
438  }
439  kValues[idx] = _aVec(idx);
440  }
441  _kernel->setKernelParameters(kValues);
442 
444  new ImageT(_kernel->getDimensions())
445  );
446  _kSum = _kernel->computeImage(*image, false);
447 
448  if (_fitForBackground) {
449  if (std::isnan(_aVec(nParameters-1))) {
451  str(boost::format("Unable to determine background solution %d (nan)") %
452  (nParameters-1)));
453  }
454  _background = _aVec(nParameters-1);
455  }
456  }
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
double _background
Derived differential background estimate.
bool _fitForBackground
Background terms included in fit.
lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel > ImageT
A kernel that is a linear combination of fixed basis kernels.
Definition: Kernel.h:741
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
T dynamic_pointer_cast(T... args)
double _kSum
Derived kernel sum.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
afw::table::Key< afw::table::Array< ImagePixelT > > image
T isnan(T... args)
Eigen::VectorXd _aVec
Derived least squares solution matrix.
std::shared_ptr< lsst::afw::math::Kernel > _kernel
Derived single-object convolution kernel.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...

◆ _setKernelUncertainty()

template<typename InputT >
void lsst::ip::diffim::StaticKernelSolution< InputT >::_setKernelUncertainty ( )
protectedinherited

Not implemented.

Definition at line 460 of file KernelSolution.cc.

460  {
461  throw LSST_EXCEPT(pexExcept::Exception, "Uncertainty calculation not supported");
462 
463  /* Estimate of parameter uncertainties comes from the inverse of the
464  * covariance matrix (noise spectrum).
465  * N.R. 15.4.8 to 15.4.15
466  *
467  * Since this is a linear problem no need to use Fisher matrix
468  * N.R. 15.5.8
469  *
470  * Although I might be able to take advantage of the solution above.
471  * Since this now works and is not the rate limiting step, keep as-is for DC3a.
472  *
473  * Use Cholesky decomposition again.
474  * Cholkesy:
475  * Cov = L L^t
476  * Cov^(-1) = (L L^t)^(-1)
477  * = (L^T)^-1 L^(-1)
478  *
479  * Code would be:
480  *
481  * Eigen::MatrixXd cov = _mMat.transpose() * _mMat;
482  * Eigen::LLT<Eigen::MatrixXd> llt = cov.llt();
483  * Eigen::MatrixXd error2 = llt.matrixL().transpose().inverse()*llt.matrixL().inverse();
484  */
485  }
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48

◆ build()

template<typename InputT >
void lsst::ip::diffim::StaticKernelSolution< InputT >::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 
)
virtualinherited

Definition at line 259 of file KernelSolution.cc.

263  {
264 
265  afwMath::Statistics varStats = afwMath::makeStatistics(varianceEstimate, afwMath::MIN);
266  if (varStats.getValue(afwMath::MIN) < 0.0) {
268  "Error: variance less than 0.0");
269  }
270  if (varStats.getValue(afwMath::MIN) == 0.0) {
272  "Error: variance equals 0.0, cannot inverse variance weight");
273  }
274 
275  lsst::afw::math::KernelList basisList =
277 
278  unsigned int const nKernelParameters = basisList.size();
279  unsigned int const nBackgroundParameters = _fitForBackground ? 1 : 0;
280  unsigned int const nParameters = nKernelParameters + nBackgroundParameters;
281 
282  std::vector<std::shared_ptr<afwMath::Kernel> >::const_iterator kiter = basisList.begin();
283 
284  /* Ignore buffers around edge of convolved images :
285  *
286  * If the kernel has width 5, it has center pixel 2. The first good pixel
287  * is the (5-2)=3rd pixel, which is array index 2, and ends up being the
288  * index of the central pixel.
289  *
290  * You also have a buffer of unusable pixels on the other side, numbered
291  * width-center-1. The last good usable pixel is N-width+center+1.
292  *
293  * Example : the kernel is width = 5, center = 2
294  *
295  * ---|---|-c-|---|---|
296  *
297  * the image is width = N
298  * convolve this with the kernel, and you get
299  *
300  * |-x-|-x-|-g-|---|---| ... |---|---|-g-|-x-|-x-|
301  *
302  * g = first/last good pixel
303  * x = bad
304  *
305  * the first good pixel is the array index that has the value "center", 2
306  * the last good pixel has array index N-(5-2)+1
307  * eg. if N = 100, you want to use up to index 97
308  * 100-3+1 = 98, and the loops use i < 98, meaning the last
309  * index you address is 97.
310  */
311 
312  /* NOTE - we are accessing particular elements of Eigen arrays using
313  these coordinates, therefore they need to be in LOCAL coordinates.
314  This was written before ndarray unification.
315  */
316  afwGeom::Box2I goodBBox = (*kiter)->shrinkBBox(templateImage.getBBox(afwImage::LOCAL));
317  unsigned int const startCol = goodBBox.getMinX();
318  unsigned int const startRow = goodBBox.getMinY();
319  // endCol/Row is one past the index of the last good col/row
320  unsigned int endCol = goodBBox.getMaxX() + 1;
321  unsigned int endRow = goodBBox.getMaxY() + 1;
322 
323  boost::timer t;
324  t.restart();
325 
326  /* Eigen representation of input images; only the pixels that are unconvolved in cimage below */
327  Eigen::MatrixXd eigenTemplate = imageToEigenMatrix(templateImage).block(startRow,
328  startCol,
329  endRow-startRow,
330  endCol-startCol);
331  Eigen::MatrixXd eigenScience = imageToEigenMatrix(scienceImage).block(startRow,
332  startCol,
333  endRow-startRow,
334  endCol-startCol);
335  Eigen::MatrixXd eigeniVariance = imageToEigenMatrix(varianceEstimate).block(
336  startRow, startCol, endRow-startRow, endCol-startCol
337  ).array().inverse().matrix();
338 
339  /* Resize into 1-D for later usage */
340  eigenTemplate.resize(eigenTemplate.rows()*eigenTemplate.cols(), 1);
341  eigenScience.resize(eigenScience.rows()*eigenScience.cols(), 1);
342  eigeniVariance.resize(eigeniVariance.rows()*eigeniVariance.cols(), 1);
343 
344  /* Holds image convolved with basis function */
345  afwImage::Image<PixelT> cimage(templateImage.getDimensions());
346 
347  /* Holds eigen representation of image convolved with all basis functions */
348  std::vector<Eigen::MatrixXd> convolvedEigenList(nKernelParameters);
349 
350  /* Iterators over convolved image list and basis list */
351  typename std::vector<Eigen::MatrixXd>::iterator eiter = convolvedEigenList.begin();
352  /* Create C_i in the formalism of Alard & Lupton */
353  for (kiter = basisList.begin(); kiter != basisList.end(); ++kiter, ++eiter) {
354  afwMath::convolve(cimage, templateImage, **kiter, false); /* cimage stores convolved image */
355 
356  Eigen::MatrixXd cMat = imageToEigenMatrix(cimage).block(startRow,
357  startCol,
358  endRow-startRow,
359  endCol-startCol);
360  cMat.resize(cMat.size(), 1);
361  *eiter = cMat;
362 
363  }
364 
365  double time = t.elapsed();
366  LOGL_DEBUG("TRACE3.ip.diffim.StaticKernelSolution.build",
367  "Total compute time to do basis convolutions : %.2f s", time);
368  t.restart();
369 
370  /*
371  Load matrix with all values from convolvedEigenList : all images
372  (eigeniVariance, convolvedEigenList) must be the same size
373  */
374  Eigen::MatrixXd cMat(eigenTemplate.col(0).size(), nParameters);
375  typename std::vector<Eigen::MatrixXd>::iterator eiterj = convolvedEigenList.begin();
376  typename std::vector<Eigen::MatrixXd>::iterator eiterE = convolvedEigenList.end();
377  for (unsigned int kidxj = 0; eiterj != eiterE; eiterj++, kidxj++) {
378  cMat.col(kidxj) = eiterj->col(0);
379  }
380  /* Treat the last "image" as all 1's to do the background calculation. */
381  if (_fitForBackground)
382  cMat.col(nParameters-1).fill(1.);
383 
384  _cMat = cMat;
385  _ivVec = eigeniVariance.col(0);
386  _iVec = eigenScience.col(0);
387 
388  /* Make these outside of solve() so I can check condition number */
389  _mMat = _cMat.transpose() * (_ivVec.asDiagonal() * _cMat);
390  _bVec = _cMat.transpose() * (_ivVec.asDiagonal() * _iVec);
391  }
estimate sample minimum
Definition: Statistics.h:76
Eigen::VectorXd _bVec
Derived least squares B vector.
T end(T... args)
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
Eigen::VectorXd _iVec
Vectorized I.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:489
T time(T... args)
Eigen::VectorXd _ivVec
Inverse variance.
A class to evaluate image statistics.
Definition: Statistics.h:215
bool _fitForBackground
Background terms included in fit.
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:463
int getMaxY() const noexcept
Definition: Box.h:149
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:354
A kernel that is a linear combination of fixed basis kernels.
Definition: Kernel.h:741
Eigen::MatrixXd imageToEigenMatrix(lsst::afw::image::Image< PixelT > const &img)
Turns a 2-d Image into a 2-d Eigen Matrix.
T dynamic_pointer_cast(T... args)
void convolve(OutImageT &convolvedImage, InImageT const &inImage, KernelT const &kernel, ConvolutionControl const &convolutionControl=ConvolutionControl())
Convolve an Image or MaskedImage with a Kernel, setting pixels of an existing output image...
int getMaxX() const noexcept
Definition: Box.h:148
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1056
Eigen::MatrixXd _mMat
Derived least squares M matrix.
int getMinX() const noexcept
Definition: Box.h:144
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
T begin(T... args)
std::shared_ptr< lsst::afw::math::Kernel > _kernel
Derived single-object convolution kernel.
lsst::geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:374
An integer coordinate rectangle.
Definition: Box.h:54
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:59
int getMinY() const noexcept
Definition: Box.h:145

◆ estimateRisk()

template<typename InputT >
double lsst::ip::diffim::RegularizedKernelSolution< InputT >::estimateRisk ( double  maxCond)

Definition at line 1070 of file KernelSolution.cc.

1070  {
1071  Eigen::MatrixXd vMat = this->_cMat.jacobiSvd().matrixV();
1072  Eigen::MatrixXd vMatvMatT = vMat * vMat.transpose();
1073 
1074  /* Find pseudo inverse of mMat, which may be ill conditioned */
1075  Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eVecValues(this->_mMat);
1076  Eigen::MatrixXd const& rMat = eVecValues.eigenvectors();
1077  Eigen::VectorXd eValues = eVecValues.eigenvalues();
1078  double eMax = eValues.maxCoeff();
1079  for (int i = 0; i != eValues.rows(); ++i) {
1080  if (eValues(i) == 0.0) {
1081  eValues(i) = 0.0;
1082  }
1083  else if ((eMax / eValues(i)) > maxCond) {
1084  LOGL_DEBUG("TRACE3.ip.diffim.RegularizedKernelSolution.estimateRisk",
1085  "Truncating eValue %d; %.5e / %.5e = %.5e vs. %.5e",
1086  i, eMax, eValues(i), eMax / eValues(i), maxCond);
1087  eValues(i) = 0.0;
1088  }
1089  else {
1090  eValues(i) = 1.0 / eValues(i);
1091  }
1092  }
1093  Eigen::MatrixXd mInv = rMat * eValues.asDiagonal() * rMat.transpose();
1094 
1095  std::vector<double> lambdas = _createLambdaSteps();
1096  std::vector<double> risks;
1097  for (unsigned int i = 0; i < lambdas.size(); i++) {
1098  double l = lambdas[i];
1099  Eigen::MatrixXd mLambda = this->_mMat + l * _hMat;
1100 
1101  try {
1102  KernelSolution::solve(mLambda, this->_bVec);
1103  } catch (pexExcept::Exception &e) {
1104  LSST_EXCEPT_ADD(e, "Unable to solve regularized kernel matrix");
1105  throw e;
1106  }
1107  Eigen::VectorXd term1 = (this->_aVec.transpose() * vMatvMatT * this->_aVec);
1108  if (term1.size() != 1)
1109  throw LSST_EXCEPT(pexExcept::Exception, "Matrix size mismatch");
1110 
1111  double term2a = (vMatvMatT * mLambda.inverse()).trace();
1112 
1113  Eigen::VectorXd term2b = (this->_aVec.transpose() * (mInv * this->_bVec));
1114  if (term2b.size() != 1)
1115  throw LSST_EXCEPT(pexExcept::Exception, "Matrix size mismatch");
1116 
1117  double risk = term1(0) + 2 * (term2a - term2b(0));
1118  LOGL_DEBUG("TRACE4.ip.diffim.RegularizedKernelSolution.estimateRisk",
1119  "Lambda = %.3f, Risk = %.5e",
1120  l, risk);
1121  LOGL_DEBUG("TRACE5.ip.diffim.RegularizedKernelSolution.estimateRisk",
1122  "%.5e + 2 * (%.5e - %.5e)",
1123  term1(0), term2a, term2b(0));
1124  risks.push_back(risk);
1125  }
1126  std::vector<double>::iterator it = min_element(risks.begin(), risks.end());
1127  int index = distance(risks.begin(), it);
1128  LOGL_DEBUG("TRACE3.ip.diffim.RegularizedKernelSolution.estimateRisk",
1129  "Minimum Risk = %.3e at lambda = %.3e", risks[index], lambdas[index]);
1130 
1131  return lambdas[index];
1132 
1133  }
Eigen::VectorXd _bVec
Derived least squares B vector.
T end(T... args)
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:489
T push_back(T... args)
Eigen::MatrixXd _mMat
Derived least squares M matrix.
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
T begin(T... args)
Eigen::VectorXd _aVec
Derived least squares solution matrix.
T min_element(T... args)
#define LSST_EXCEPT_ADD(e, m)
Add the current location and a message to an existing exception before rethrowing it...
Definition: Exception.h:54

◆ getB()

Eigen::VectorXd const& lsst::ip::diffim::KernelSolution::getB ( )
inlineinherited

Definition at line 65 of file KernelSolution.h.

65 {return _bVec;}
Eigen::VectorXd _bVec
Derived least squares B vector.

◆ getBackground()

template<typename InputT >
double lsst::ip::diffim::StaticKernelSolution< InputT >::getBackground ( )
virtualinherited

Definition at line 233 of file KernelSolution.cc.

233  {
235  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot return background");
236  }
237  return _background;
238  }
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
double _background
Derived differential background estimate.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48

◆ getConditionNumber() [1/2]

double lsst::ip::diffim::KernelSolution::getConditionNumber ( ConditionNumberType  conditionType)
virtualinherited

Definition at line 92 of file KernelSolution.cc.

92  {
93  return getConditionNumber(_mMat, conditionType);
94  }
virtual double getConditionNumber(ConditionNumberType conditionType)
Eigen::MatrixXd _mMat
Derived least squares M matrix.

◆ getConditionNumber() [2/2]

double lsst::ip::diffim::KernelSolution::getConditionNumber ( Eigen::MatrixXd const &  mMat,
ConditionNumberType  conditionType 
)
virtualinherited

Definition at line 96 of file KernelSolution.cc.

97  {
98  switch (conditionType) {
99  case EIGENVALUE:
100  {
101  Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eVecValues(mMat);
102  Eigen::VectorXd eValues = eVecValues.eigenvalues();
103  double eMax = eValues.maxCoeff();
104  double eMin = eValues.minCoeff();
105  LOGL_DEBUG("TRACE3.ip.diffim.KernelSolution.getConditionNumber",
106  "EIGENVALUE eMax / eMin = %.3e", eMax / eMin);
107  return (eMax / eMin);
108  break;
109  }
110  case SVD:
111  {
112  Eigen::VectorXd sValues = mMat.jacobiSvd().singularValues();
113  double sMax = sValues.maxCoeff();
114  double sMin = sValues.minCoeff();
115  LOGL_DEBUG("TRACE3.ip.diffim.KernelSolution.getConditionNumber",
116  "SVD eMax / eMin = %.3e", sMax / sMin);
117  return (sMax / sMin);
118  break;
119  }
120  default:
121  {
123  "Undefined ConditionNumberType : only EIGENVALUE, SVD allowed.");
124  break;
125  }
126  }
127  }
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:489
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Reports invalid arguments.
Definition: Runtime.h:66

◆ getId()

int lsst::ip::diffim::KernelSolution::getId ( ) const
inlineinherited

Definition at line 69 of file KernelSolution.h.

69 { return _id; }
int _id
Unique ID for object.

◆ getKernel()

template<typename InputT >
std::shared_ptr< lsst::afw::math::Kernel > lsst::ip::diffim::StaticKernelSolution< InputT >::getKernel ( )
virtualinherited

Definition at line 213 of file KernelSolution.cc.

213  {
215  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot return solution");
216  }
217  return _kernel;
218  }
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::shared_ptr< lsst::afw::math::Kernel > _kernel
Derived single-object convolution kernel.

◆ getKsum()

template<typename InputT >
double lsst::ip::diffim::StaticKernelSolution< InputT >::getKsum ( )
virtualinherited

Definition at line 241 of file KernelSolution.cc.

241  {
243  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot return ksum");
244  }
245  return _kSum;
246  }
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
double _kSum
Derived kernel sum.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48

◆ getLambda()

template<typename InputT >
double lsst::ip::diffim::RegularizedKernelSolution< InputT >::getLambda ( )
inline

Definition at line 159 of file KernelSolution.h.

159 {return _lambda;}

◆ getM() [1/2]

Eigen::MatrixXd const& lsst::ip::diffim::KernelSolution::getM ( )
inlineinherited

Definition at line 64 of file KernelSolution.h.

64 {return _mMat;}
Eigen::MatrixXd _mMat
Derived least squares M matrix.

◆ getM() [2/2]

template<typename InputT >
Eigen::MatrixXd lsst::ip::diffim::RegularizedKernelSolution< InputT >::getM ( bool  includeHmat = true)

Definition at line 1136 of file KernelSolution.cc.

1136  {
1137  if (includeHmat == true) {
1138  return this->_mMat + _lambda * _hMat;
1139  }
1140  else {
1141  return this->_mMat;
1142  }
1143  }
Eigen::MatrixXd _mMat
Derived least squares M matrix.

◆ getSolutionPair()

template<typename InputT >
std::pair< std::shared_ptr< lsst::afw::math::Kernel >, double > lsst::ip::diffim::StaticKernelSolution< InputT >::getSolutionPair ( )
virtualinherited

Definition at line 250 of file KernelSolution.cc.

250  {
252  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot return solution");
253  }
254 
256  }
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
double _background
Derived differential background estimate.
T make_pair(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::shared_ptr< lsst::afw::math::Kernel > _kernel
Derived single-object convolution kernel.

◆ getSolvedBy()

KernelSolvedBy lsst::ip::diffim::KernelSolution::getSolvedBy ( )
inlineinherited

Definition at line 60 of file KernelSolution.h.

60 {return _solvedBy;}
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.

◆ makeKernelImage()

template<typename InputT >
std::shared_ptr< lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel > > lsst::ip::diffim::StaticKernelSolution< InputT >::makeKernelImage ( )
virtualinherited

Definition at line 221 of file KernelSolution.cc.

221  {
223  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot return image");
224  }
227  );
228  (void)_kernel->computeImage(*image, false);
229  return image;
230  }
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
afw::table::Key< afw::table::Array< ImagePixelT > > image
std::shared_ptr< lsst::afw::math::Kernel > _kernel
Derived single-object convolution kernel.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:59

◆ printA()

void lsst::ip::diffim::KernelSolution::printA ( )
inlineinherited

Definition at line 68 of file KernelSolution.h.

68 {std::cout << _aVec << std::endl;}
T endl(T... args)
Eigen::VectorXd _aVec
Derived least squares solution matrix.

◆ printB()

void lsst::ip::diffim::KernelSolution::printB ( )
inlineinherited

Definition at line 67 of file KernelSolution.h.

67 {std::cout << _bVec << std::endl;}
Eigen::VectorXd _bVec
Derived least squares B vector.
T endl(T... args)

◆ printM()

void lsst::ip::diffim::KernelSolution::printM ( )
inlineinherited

Definition at line 66 of file KernelSolution.h.

66 {std::cout << _mMat << std::endl;}
T endl(T... args)
Eigen::MatrixXd _mMat
Derived least squares M matrix.

◆ solve() [1/2]

void lsst::ip::diffim::KernelSolution::solve ( Eigen::MatrixXd const &  mMat,
Eigen::VectorXd const &  bVec 
)
virtualinherited

Definition at line 129 of file KernelSolution.cc.

130  {
131 
132  if (DEBUG_MATRIX) {
133  std::cout << "M " << std::endl;
134  std::cout << mMat << std::endl;
135  std::cout << "B " << std::endl;
136  std::cout << bVec << std::endl;
137  }
138 
139  Eigen::VectorXd aVec = Eigen::VectorXd::Zero(bVec.size());
140 
141  boost::timer t;
142  t.restart();
143 
144  LOGL_DEBUG("TRACE2.ip.diffim.KernelSolution.solve",
145  "Solving for kernel");
146  _solvedBy = LU;
147  Eigen::FullPivLU<Eigen::MatrixXd> lu(mMat);
148  if (lu.isInvertible()) {
149  aVec = lu.solve(bVec);
150  } else {
151  LOGL_DEBUG("TRACE3.ip.diffim.KernelSolution.solve",
152  "Unable to determine kernel via LU");
153  /* LAST RESORT */
154  try {
155 
157  Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eVecValues(mMat);
158  Eigen::MatrixXd const& rMat = eVecValues.eigenvectors();
159  Eigen::VectorXd eValues = eVecValues.eigenvalues();
160 
161  for (int i = 0; i != eValues.rows(); ++i) {
162  if (eValues(i) != 0.0) {
163  eValues(i) = 1.0/eValues(i);
164  }
165  }
166 
167  aVec = rMat * eValues.asDiagonal() * rMat.transpose() * bVec;
168  } catch (pexExcept::Exception& e) {
169 
170  _solvedBy = NONE;
171  LOGL_DEBUG("TRACE3.ip.diffim.KernelSolution.solve",
172  "Unable to determine kernel via eigen-values");
173 
174  throw LSST_EXCEPT(pexExcept::Exception, "Unable to determine kernel solution");
175  }
176  }
177 
178  double time = t.elapsed();
179  LOGL_DEBUG("TRACE3.ip.diffim.KernelSolution.solve",
180  "Compute time for matrix math : %.2f s", time);
181 
182  if (DEBUG_MATRIX) {
183  std::cout << "A " << std::endl;
184  std::cout << aVec << std::endl;
185  }
186 
187  _aVec = aVec;
188  }
T endl(T... args)
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:489
T time(T... args)
#define DEBUG_MATRIX
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Eigen::VectorXd _aVec
Derived least squares solution matrix.

◆ solve() [2/2]

template<typename InputT >
void lsst::ip::diffim::RegularizedKernelSolution< InputT >::solve ( )
virtual

Reimplemented from lsst::ip::diffim::StaticKernelSolution< InputT >.

Definition at line 1146 of file KernelSolution.cc.

1146  {
1147 
1148  LOGL_DEBUG("TRACE3.ip.diffim.RegularizedKernelSolution.solve",
1149  "cMat is %d x %d; vVec is %d; iVec is %d; hMat is %d x %d",
1150  this->_cMat.rows(), this->_cMat.cols(), this->_ivVec.size(),
1151  this->_iVec.size(), _hMat.rows(), _hMat.cols());
1152 
1153  if (DEBUG_MATRIX2) {
1154  std::cout << "ID: " << (this->_id) << std::endl;
1155  std::cout << "C:" << std::endl;
1156  std::cout << this->_cMat << std::endl;
1157  std::cout << "Sigma^{-1}:" << std::endl;
1158  std::cout << Eigen::MatrixXd(this->_ivVec.asDiagonal()) << std::endl;
1159  std::cout << "Y:" << std::endl;
1160  std::cout << this->_iVec << std::endl;
1161  std::cout << "H:" << std::endl;
1162  std::cout << _hMat << std::endl;
1163  }
1164 
1165 
1166  this->_mMat = this->_cMat.transpose() * this->_ivVec.asDiagonal() * this->_cMat;
1167  this->_bVec = this->_cMat.transpose() * this->_ivVec.asDiagonal() * this->_iVec;
1168 
1169 
1170  /* See N.R. 18.5
1171 
1172  Matrix equation to solve is Y = C a + N
1173  Y = vectorized version of I (I = image to not convolve)
1174  C_i = K_i (x) R (R = image to convolve)
1175  a = kernel coefficients
1176  N = noise
1177 
1178  If we reweight everything by the inverse square root of the noise
1179  covariance, we get a linear model with the identity matrix for
1180  the noise. The problem can then be solved using least squares,
1181  with the normal equations
1182 
1183  C^T Y = C^T C a
1184 
1185  or
1186 
1187  b = M a
1188 
1189  with
1190 
1191  b = C^T Y
1192  M = C^T C
1193  a = (C^T C)^{-1} C^T Y
1194 
1195 
1196  We can regularize the least square problem
1197 
1198  Y = C a + lambda a^T H a (+ N, which can be ignored)
1199 
1200  or the normal equations
1201 
1202  (C^T C + lambda H) a = C^T Y
1203 
1204 
1205  The solution to the regularization of the least squares problem is
1206 
1207  a = (C^T C + lambda H)^{-1} C^T Y
1208 
1209  The approximation to Y is
1210 
1211  C a = C (C^T C + lambda H)^{-1} C^T Y
1212 
1213  with smoothing matrix
1214 
1215  S = C (C^T C + lambda H)^{-1} C^T
1216 
1217  */
1218 
1219  std::string lambdaType = _policy.getString("lambdaType");
1220  if (lambdaType == "absolute") {
1221  _lambda = _policy.getDouble("lambdaValue");
1222  }
1223  else if (lambdaType == "relative") {
1224  _lambda = this->_mMat.trace() / this->_hMat.trace();
1225  _lambda *= _policy.getDouble("lambdaScaling");
1226  }
1227  else if (lambdaType == "minimizeBiasedRisk") {
1228  double tol = _policy.getDouble("maxConditionNumber");
1229  _lambda = estimateRisk(tol);
1230  }
1231  else if (lambdaType == "minimizeUnbiasedRisk") {
1233  }
1234  else {
1235  throw LSST_EXCEPT(pexExcept::Exception, "lambdaType in Policy not recognized");
1236  }
1237 
1238  LOGL_DEBUG("TRACE3.ip.diffim.RegularizedKernelSolution.solve",
1239  "Applying kernel regularization with lambda = %.2e", _lambda);
1240 
1241 
1242  try {
1243  KernelSolution::solve(this->_mMat + _lambda * _hMat, this->_bVec);
1244  } catch (pexExcept::Exception &e) {
1245  LSST_EXCEPT_ADD(e, "Unable to solve static kernel matrix");
1246  throw e;
1247  }
1248  /* Turn matrices into _kernel and _background */
1250  }
Eigen::VectorXd _bVec
Derived least squares B vector.
T endl(T... args)
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
#define DEBUG_MATRIX2
Eigen::VectorXd _iVec
Vectorized I.
const std::string getString(const std::string &name) const
return a string value associated with the given name .
Definition: Policy.h:631
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:489
Eigen::VectorXd _ivVec
Inverse variance.
STL class.
void _setKernel()
Set kernel after solution.
int _id
Unique ID for object.
Eigen::MatrixXd _mMat
Derived least squares M matrix.
double getDouble(const std::string &name) const
return a double value associated with the given name.
Definition: Policy.h:617
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
#define LSST_EXCEPT_ADD(e, m)
Add the current location and a message to an existing exception before rethrowing it...
Definition: Exception.h:54

Member Data Documentation

◆ _aVec

Eigen::VectorXd lsst::ip::diffim::KernelSolution::_aVec
protectedinherited

Derived least squares solution matrix.

Definition at line 75 of file KernelSolution.h.

◆ _background

template<typename InputT >
double lsst::ip::diffim::StaticKernelSolution< InputT >::_background
protectedinherited

Derived differential background estimate.

Definition at line 110 of file KernelSolution.h.

◆ _bVec

Eigen::VectorXd lsst::ip::diffim::KernelSolution::_bVec
protectedinherited

Derived least squares B vector.

Definition at line 74 of file KernelSolution.h.

◆ _cMat

template<typename InputT >
Eigen::MatrixXd lsst::ip::diffim::StaticKernelSolution< InputT >::_cMat
protectedinherited

K_i x R.

Definition at line 105 of file KernelSolution.h.

◆ _fitForBackground

bool lsst::ip::diffim::KernelSolution::_fitForBackground
protectedinherited

Background terms included in fit.

Definition at line 77 of file KernelSolution.h.

◆ _id

int lsst::ip::diffim::KernelSolution::_id
protectedinherited

Unique ID for object.

Definition at line 72 of file KernelSolution.h.

◆ _iVec

template<typename InputT >
Eigen::VectorXd lsst::ip::diffim::StaticKernelSolution< InputT >::_iVec
protectedinherited

Vectorized I.

Definition at line 106 of file KernelSolution.h.

◆ _ivVec

template<typename InputT >
Eigen::VectorXd lsst::ip::diffim::StaticKernelSolution< InputT >::_ivVec
protectedinherited

Inverse variance.

Definition at line 107 of file KernelSolution.h.

◆ _kernel

template<typename InputT >
std::shared_ptr<lsst::afw::math::Kernel> lsst::ip::diffim::StaticKernelSolution< InputT >::_kernel
protectedinherited

Derived single-object convolution kernel.

Definition at line 109 of file KernelSolution.h.

◆ _kSum

template<typename InputT >
double lsst::ip::diffim::StaticKernelSolution< InputT >::_kSum
protectedinherited

Derived kernel sum.

Definition at line 111 of file KernelSolution.h.

◆ _mMat

Eigen::MatrixXd lsst::ip::diffim::KernelSolution::_mMat
protectedinherited

Derived least squares M matrix.

Definition at line 73 of file KernelSolution.h.

◆ _SolutionId

int lsst::ip::diffim::KernelSolution::_SolutionId = 0
staticprotectedinherited

Unique identifier for solution.

Definition at line 78 of file KernelSolution.h.

◆ _solvedBy

KernelSolvedBy lsst::ip::diffim::KernelSolution::_solvedBy
protectedinherited

Type of algorithm used to make solution.

Definition at line 76 of file KernelSolution.h.


The documentation for this class was generated from the following files: