LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
lsst::ip::diffim::SpatialKernelSolution Class Reference

#include <KernelSolution.h>

Inheritance diagram for lsst::ip::diffim::SpatialKernelSolution:
lsst::ip::diffim::KernelSolution

Public Types

typedef boost::shared_ptr
< SpatialKernelSolution
Ptr
 
- Public Types inherited from lsst::ip::diffim::KernelSolution
enum  KernelSolvedBy {
  NONE = 0, CHOLESKY_LDLT = 1, CHOLESKY_LLT = 2, LU = 3,
  EIGENVECTOR = 4
}
 
enum  ConditionNumberType { EIGENVALUE = 0, SVD = 1 }
 
typedef boost::shared_ptr
< KernelSolution
Ptr
 
typedef
lsst::afw::math::Kernel::Pixel 
PixelT
 
typedef
lsst::afw::image::Image
< lsst::afw::math::Kernel::Pixel
ImageT
 

Public Member Functions

 SpatialKernelSolution (lsst::afw::math::KernelList const &basisList, lsst::afw::math::Kernel::SpatialFunctionPtr spatialKernelFunction, lsst::afw::math::Kernel::SpatialFunctionPtr background, lsst::pex::policy::Policy policy)
 
virtual ~SpatialKernelSolution ()
 
void addConstraint (float xCenter, float yCenter, boost::shared_ptr< Eigen::MatrixXd > qMat, boost::shared_ptr< Eigen::VectorXd > wVec)
 
void solve ()
 
lsst::afw::image::Image
< lsst::afw::math::Kernel::Pixel >
::Ptr 
makeKernelImage (lsst::afw::geom::Point2D const &pos)
 
std::pair
< lsst::afw::math::LinearCombinationKernel::Ptr,
lsst::afw::math::Kernel::SpatialFunctionPtr
getSolutionPair ()
 
- Public Member Functions inherited from lsst::ip::diffim::KernelSolution
 KernelSolution (boost::shared_ptr< Eigen::MatrixXd > mMat, boost::shared_ptr< Eigen::VectorXd > bVec, bool fitForBackground)
 
 KernelSolution (bool fitForBackground)
 
 KernelSolution ()
 
virtual ~KernelSolution ()
 
virtual void solve (Eigen::MatrixXd mMat, Eigen::VectorXd bVec)
 
KernelSolvedBy getSolvedBy ()
 
virtual double getConditionNumber (ConditionNumberType conditionType)
 
virtual double getConditionNumber (Eigen::MatrixXd mMat, ConditionNumberType conditionType)
 
boost::shared_ptr
< Eigen::MatrixXd > 
getM ()
 
boost::shared_ptr
< Eigen::VectorXd > 
getB ()
 
void printM ()
 
void printB ()
 
void printA ()
 
int getId () const
 

Private Member Functions

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

Private Attributes

lsst::afw::math::Kernel::SpatialFunctionPtr _spatialKernelFunction
 Spatial function for Kernel. More...
 
bool _constantFirstTerm
 Is the first term constant. More...
 
lsst::afw::math::LinearCombinationKernel::Ptr _kernel
 Spatial convolution kernel. More...
 
lsst::afw::math::Kernel::SpatialFunctionPtr _background
 Spatial background model. More...
 
double _kSum
 Derived kernel sum. More...
 
lsst::pex::policy::Policy _policy
 Policy to control processing. More...
 
int _nbases
 Number of basis functions. More...
 
int _nkt
 Number of kernel terms. More...
 
int _nbt
 Number of background terms. More...
 
int _nt
 Total number of terms. More...
 

Additional Inherited Members

- Protected Attributes inherited from lsst::ip::diffim::KernelSolution
int _id
 Unique ID for object. More...
 
boost::shared_ptr
< Eigen::MatrixXd > 
_mMat
 Derived least squares M matrix. More...
 
boost::shared_ptr
< Eigen::VectorXd > 
_bVec
 Derived least squares B vector. More...
 
boost::shared_ptr
< 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 inherited from lsst::ip::diffim::KernelSolution
static int _SolutionId = 0
 Unique identifier for solution. More...
 

Detailed Description

Definition at line 173 of file KernelSolution.h.

Member Typedef Documentation

Definition at line 175 of file KernelSolution.h.

Constructor & Destructor Documentation

lsst::ip::diffim::SpatialKernelSolution::SpatialKernelSolution ( lsst::afw::math::KernelList const &  basisList,
lsst::afw::math::Kernel::SpatialFunctionPtr  spatialKernelFunction,
lsst::afw::math::Kernel::SpatialFunctionPtr  background,
lsst::pex::policy::Policy  policy 
)

Definition at line 1333 of file KernelSolution.cc.

1338  :
1339  KernelSolution(),
1340  _spatialKernelFunction(spatialKernelFunction),
1341  _constantFirstTerm(false),
1342  _kernel(),
1343  _background(background),
1344  _kSum(0.0),
1345  _policy(policy),
1346  _nbases(0),
1347  _nkt(0),
1348  _nbt(0),
1349  _nt(0) {
1350 
1351  bool isAlardLupton = _policy.getString("kernelBasisSet") == "alard-lupton";
1352  bool usePca = _policy.getBool("usePcaForSpatialKernel");
1353  if (isAlardLupton || usePca) {
1354  _constantFirstTerm = true;
1355  }
1356  this->_fitForBackground = _policy.getBool("fitForBackground");
1357 
1358  _nbases = basisList.size();
1359  _nkt = _spatialKernelFunction->getParameters().size();
1360  _nbt = _fitForBackground ? _background->getParameters().size() : 0;
1361  _nt = 0;
1362  if (_constantFirstTerm) {
1363  _nt = (_nbases - 1) * _nkt + 1 + _nbt;
1364  } else {
1365  _nt = _nbases * _nkt + _nbt;
1366  }
1367 
1368  boost::shared_ptr<Eigen::MatrixXd> mMat (new Eigen::MatrixXd(_nt, _nt));
1369  boost::shared_ptr<Eigen::VectorXd> bVec (new Eigen::VectorXd(_nt));
1370  (*mMat).setZero();
1371  (*bVec).setZero();
1372 
1373  this->_mMat = mMat;
1374  this->_bVec = bVec;
1375 
1378  );
1379 
1380  pexLog::TTrace<5>("lsst.ip.diffim.SpatialKernelSolution",
1381  "Initializing with size %d %d %d and constant first term = %s",
1382  _nkt, _nbt, _nt,
1383  _constantFirstTerm ? "true" : "false");
1384 
1385  }
bool getBool(const std::string &name) const
Definition: Policy.h:589
const std::string getString(const std::string &name) const
Definition: Policy.h:631
boost::shared_ptr< LinearCombinationKernel > Ptr
Definition: Kernel.h:818
double _kSum
Derived kernel sum.
bool _fitForBackground
Background terms included in fit.
A kernel that is a linear combination of fixed basis kernels.
Definition: Kernel.h:814
boost::shared_ptr< Eigen::VectorXd > _bVec
Derived least squares B vector.
lsst::afw::math::Kernel::SpatialFunctionPtr _spatialKernelFunction
Spatial function for Kernel.
int _nbt
Number of background terms.
bool _constantFirstTerm
Is the first term constant.
lsst::pex::policy::Policy _policy
Policy to control processing.
lsst::afw::math::LinearCombinationKernel::Ptr _kernel
Spatial convolution kernel.
lsst::afw::math::Kernel::SpatialFunctionPtr _background
Spatial background model.
int _nkt
Number of kernel terms.
boost::shared_ptr< Eigen::MatrixXd > _mMat
Derived least squares M matrix.
int _nbases
Number of basis functions.
virtual lsst::ip::diffim::SpatialKernelSolution::~SpatialKernelSolution ( )
inlinevirtual

Definition at line 184 of file KernelSolution.h.

184 {};

Member Function Documentation

void lsst::ip::diffim::SpatialKernelSolution::_setKernel ( )
private

Set kernel after solution.

Definition at line 1538 of file KernelSolution.cc.

1538  {
1539  /* Report on condition number */
1540  double cNumber = this->getConditionNumber(EIGENVALUE);
1541 
1542  if (_nkt == 1) {
1543  /* Not spatially varying; this fork is a specialization for convolution speed--up */
1544 
1545  /* Set the basis coefficients */
1546  std::vector<double> kCoeffs(_nbases);
1547  for (int i = 0; i < _nbases; i++) {
1548  if (std::isnan((*_aVec)(i))) {
1549  throw LSST_EXCEPT(
1551  str(boost::format(
1552  "I. Unable to determine spatial kernel solution %d (nan). Condition number = %.3e") % i % cNumber));
1553  }
1554  kCoeffs[i] = (*_aVec)(i);
1555  }
1556  lsst::afw::math::KernelList basisList =
1557  boost::dynamic_pointer_cast<afwMath::LinearCombinationKernel>(_kernel)->getKernelList();
1558  _kernel.reset(
1559  new afwMath::LinearCombinationKernel(basisList, kCoeffs)
1560  );
1561  }
1562  else {
1563 
1564  /* Set the kernel coefficients */
1565  std::vector<std::vector<double> > kCoeffs;
1566  kCoeffs.reserve(_nbases);
1567  for (int i = 0, idx = 0; i < _nbases; i++) {
1568  kCoeffs.push_back(std::vector<double>(_nkt));
1569 
1570  /* Deal with the possibility the first term doesn't vary spatially */
1571  if ((i == 0) && (_constantFirstTerm)) {
1572  if (std::isnan((*_aVec)(idx))) {
1573  throw LSST_EXCEPT(
1575  str(boost::format(
1576  "II. Unable to determine spatial kernel solution %d (nan). Condition number = %.3e") % idx % cNumber));
1577  }
1578  kCoeffs[i][0] = (*_aVec)(idx++);
1579  }
1580  else {
1581  for (int j = 0; j < _nkt; j++) {
1582  if (std::isnan((*_aVec)(idx))) {
1583  throw LSST_EXCEPT(
1585  str(boost::format(
1586  "III. Unable to determine spatial kernel solution %d (nan). Condition number = %.3e") % idx % cNumber));
1587  }
1588  kCoeffs[i][j] = (*_aVec)(idx++);
1589  }
1590  }
1591  }
1592  _kernel->setSpatialParameters(kCoeffs);
1593  }
1594 
1595  /* Set kernel Sum */
1596  ImageT::Ptr image (new ImageT(_kernel->getDimensions()));
1597  _kSum = _kernel->computeImage(*image, false);
1598 
1599  /* Set the background coefficients */
1600  std::vector<double> bgCoeffs(_fitForBackground ? _nbt : 1);
1601  if (_fitForBackground) {
1602  for (int i = 0; i < _nbt; i++) {
1603  int idx = _nt - _nbt + i;
1604  if (std::isnan((*_aVec)(idx))) {
1606  str(boost::format(
1607  "Unable to determine spatial background solution %d (nan)") % (idx)));
1608  }
1609  bgCoeffs[i] = (*_aVec)(idx);
1610  }
1611  }
1612  else {
1613  bgCoeffs[0] = 0.;
1614  }
1615  _background->setParameters(bgCoeffs);
1616  }
int isnan(T t)
Definition: ieee.h:110
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
double _kSum
Derived kernel sum.
bool _fitForBackground
Background terms included in fit.
lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel > ImageT
virtual double getConditionNumber(ConditionNumberType conditionType)
A kernel that is a linear combination of fixed basis kernels.
Definition: Kernel.h:814
boost::shared_ptr< Image< lsst::afw::math::Kernel::Pixel > > Ptr
Definition: Image.h:418
int _nbt
Number of background terms.
bool _constantFirstTerm
Is the first term constant.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
lsst::afw::math::LinearCombinationKernel::Ptr _kernel
Spatial convolution kernel.
boost::shared_ptr< Eigen::VectorXd > _aVec
Derived least squares solution matrix.
lsst::afw::math::Kernel::SpatialFunctionPtr _background
Spatial background model.
int _nkt
Number of kernel terms.
int _nbases
Number of basis functions.
std::vector< boost::shared_ptr< Kernel > > KernelList
Definition: Kernel.h:542
void lsst::ip::diffim::SpatialKernelSolution::_setKernelUncertainty ( )
private

Not implemented.

void lsst::ip::diffim::SpatialKernelSolution::addConstraint ( float  xCenter,
float  yCenter,
boost::shared_ptr< Eigen::MatrixXd >  qMat,
boost::shared_ptr< Eigen::VectorXd >  wVec 
)

Definition at line 1387 of file KernelSolution.cc.

1389  {
1390 
1391  pexLog::TTrace<8>("lsst.ip.diffim.SpatialKernelSolution.addConstraint",
1392  "Adding candidate at %f, %f", xCenter, yCenter);
1393 
1394  /* Calculate P matrices */
1395  /* Pure kernel terms */
1396  Eigen::VectorXd pK(_nkt);
1397  std::vector<double> paramsK = _spatialKernelFunction->getParameters();
1398  for (int idx = 0; idx < _nkt; idx++) { paramsK[idx] = 0.0; }
1399  for (int idx = 0; idx < _nkt; idx++) {
1400  paramsK[idx] = 1.0;
1401  _spatialKernelFunction->setParameters(paramsK);
1402  pK(idx) = (*_spatialKernelFunction)(xCenter, yCenter); /* Assume things don't vary over stamp */
1403  paramsK[idx] = 0.0;
1404  }
1405  Eigen::MatrixXd pKpKt = (pK * pK.transpose());
1406 
1407  Eigen::VectorXd pB;
1408  Eigen::MatrixXd pBpBt;
1409  Eigen::MatrixXd pKpBt;
1410  if (_fitForBackground) {
1411  pB = Eigen::VectorXd(_nbt);
1412 
1413  /* Pure background terms */
1414  std::vector<double> paramsB = _background->getParameters();
1415  for (int idx = 0; idx < _nbt; idx++) { paramsB[idx] = 0.0; }
1416  for (int idx = 0; idx < _nbt; idx++) {
1417  paramsB[idx] = 1.0;
1418  _background->setParameters(paramsB);
1419  pB(idx) = (*_background)(xCenter, yCenter); /* Assume things don't vary over stamp */
1420  paramsB[idx] = 0.0;
1421  }
1422  pBpBt = (pB * pB.transpose());
1423 
1424  /* Cross terms */
1425  pKpBt = (pK * pB.transpose());
1426  }
1427 
1428  if (DEBUG_MATRIX) {
1429  std::cout << "Spatial weights" << std::endl;
1430  std::cout << "pKpKt " << pKpKt << std::endl;
1431  if (_fitForBackground) {
1432  std::cout << "pBpBt " << pBpBt << std::endl;
1433  std::cout << "pKpBt " << pKpBt << std::endl;
1434  }
1435  }
1436 
1437  if (DEBUG_MATRIX) {
1438  std::cout << "Spatial matrix inputs" << std::endl;
1439  std::cout << "M " << (*qMat) << std::endl;
1440  std::cout << "B " << (*wVec) << std::endl;
1441  }
1442 
1443  /* first index to start the spatial blocks; default=0 for non-constant first term */
1444  int m0 = 0;
1445  /* how many rows/cols to adjust the matrices/vectors; default=0 for non-constant first term */
1446  int dm = 0;
1447  /* where to start the background terms; this is always true */
1448  int mb = _nt - _nbt;
1449 
1450  if (_constantFirstTerm) {
1451  m0 = 1; /* we need to manually fill in the first (non-spatial) terms below */
1452  dm = _nkt-1; /* need to shift terms due to lack of spatial variation in first term */
1453 
1454  (*_mMat)(0, 0) += (*qMat)(0,0);
1455  for(int m2 = 1; m2 < _nbases; m2++) {
1456  (*_mMat).block(0, m2*_nkt-dm, 1, _nkt) += (*qMat)(0,m2) * pK.transpose();
1457  }
1458  (*_bVec)(0) += (*wVec)(0);
1459 
1460  if (_fitForBackground) {
1461  (*_mMat).block(0, mb, 1, _nbt) += (*qMat)(0,_nbases) * pB.transpose();
1462  }
1463  }
1464 
1465  /* Fill in the spatial blocks */
1466  for(int m1 = m0; m1 < _nbases; m1++) {
1467  /* Diagonal kernel-kernel term; only use upper triangular part of pKpKt */
1468  (*_mMat).block(m1*_nkt-dm, m1*_nkt-dm, _nkt, _nkt) +=
1469  (pKpKt * (*qMat)(m1,m1)).triangularView<Eigen::Upper>();
1470 
1471  /* Kernel-kernel terms */
1472  for(int m2 = m1+1; m2 < _nbases; m2++) {
1473  (*_mMat).block(m1*_nkt-dm, m2*_nkt-dm, _nkt, _nkt) += (*qMat)(m1,m2) * pKpKt;
1474  }
1475 
1476  if (_fitForBackground) {
1477  /* Kernel cross terms with background */
1478  (*_mMat).block(m1*_nkt-dm, mb, _nkt, _nbt) += (*qMat)(m1,_nbases) * pKpBt;
1479  }
1480 
1481  /* B vector */
1482  (*_bVec).segment(m1*_nkt-dm, _nkt) += (*wVec)(m1) * pK;
1483  }
1484 
1485  if (_fitForBackground) {
1486  /* Background-background terms only */
1487  (*_mMat).block(mb, mb, _nbt, _nbt) +=
1488  (pBpBt * (*qMat)(_nbases,_nbases)).triangularView<Eigen::Upper>();
1489  (*_bVec).segment(mb, _nbt) += (*wVec)(_nbases) * pB;
1490  }
1491 
1492  if (DEBUG_MATRIX) {
1493  std::cout << "Spatial matrix outputs" << std::endl;
1494  std::cout << "mMat " << (*_mMat) << std::endl;
1495  std::cout << "bVec " << (*_bVec) << std::endl;
1496  }
1497 
1498  }
bool _fitForBackground
Background terms included in fit.
lsst::afw::math::Kernel::SpatialFunctionPtr _spatialKernelFunction
Spatial function for Kernel.
int _nbt
Number of background terms.
bool _constantFirstTerm
Is the first term constant.
lsst::afw::math::Kernel::SpatialFunctionPtr _background
Spatial background model.
int _nkt
Number of kernel terms.
int _nbases
Number of basis functions.
#define DEBUG_MATRIX
std::pair< afwMath::LinearCombinationKernel::Ptr, afwMath::Kernel::SpatialFunctionPtr > lsst::ip::diffim::SpatialKernelSolution::getSolutionPair ( )

Definition at line 1530 of file KernelSolution.cc.

1530  {
1532  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot return solution");
1533  }
1534 
1535  return std::make_pair(_kernel, _background);
1536  }
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
lsst::afw::math::LinearCombinationKernel::Ptr _kernel
Spatial convolution kernel.
lsst::afw::math::Kernel::SpatialFunctionPtr _background
Spatial background model.
lsst::afw::image::Image< lsst::afw::math::Kernel::Pixel >::Ptr lsst::ip::diffim::SpatialKernelSolution::makeKernelImage ( lsst::afw::geom::Point2D const &  pos)

Definition at line 1500 of file KernelSolution.cc.

1500  {
1502  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot return image");
1503  }
1505  new afwImage::Image<afwMath::Kernel::Pixel>(_kernel->getDimensions())
1506  );
1507  (void)_kernel->computeImage(*image, false, pos[0], pos[1]);
1508  return image;
1509  }
KernelSolvedBy _solvedBy
Type of algorithm used to make solution.
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
lsst::afw::math::LinearCombinationKernel::Ptr _kernel
Spatial convolution kernel.
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:415
void lsst::ip::diffim::SpatialKernelSolution::solve ( )
virtual

Reimplemented from lsst::ip::diffim::KernelSolution.

Definition at line 1511 of file KernelSolution.cc.

1511  {
1512  /* Fill in the other half of mMat */
1513  for (int i = 0; i < _nt; i++) {
1514  for (int j = i+1; j < _nt; j++) {
1515  (*_mMat)(j,i) = (*_mMat)(i,j);
1516  }
1517  }
1518 
1519  try {
1521  } catch (pexExcept::Exception &e) {
1522  LSST_EXCEPT_ADD(e, "Unable to solve spatial kernel matrix");
1523  throw e;
1524  }
1525  /* Turn matrices into _kernel and _background */
1526  _setKernel();
1527  }
void _setKernel()
Set kernel after solution.
boost::shared_ptr< Eigen::MatrixXd > _mMat
Derived least squares M matrix.
#define LSST_EXCEPT_ADD(e, m)
Definition: Exception.h:51

Member Data Documentation

lsst::afw::math::Kernel::SpatialFunctionPtr lsst::ip::diffim::SpatialKernelSolution::_background
private

Spatial background model.

Definition at line 200 of file KernelSolution.h.

bool lsst::ip::diffim::SpatialKernelSolution::_constantFirstTerm
private

Is the first term constant.

Definition at line 197 of file KernelSolution.h.

lsst::afw::math::LinearCombinationKernel::Ptr lsst::ip::diffim::SpatialKernelSolution::_kernel
private

Spatial convolution kernel.

Definition at line 199 of file KernelSolution.h.

double lsst::ip::diffim::SpatialKernelSolution::_kSum
private

Derived kernel sum.

Definition at line 201 of file KernelSolution.h.

int lsst::ip::diffim::SpatialKernelSolution::_nbases
private

Number of basis functions.

Definition at line 204 of file KernelSolution.h.

int lsst::ip::diffim::SpatialKernelSolution::_nbt
private

Number of background terms.

Definition at line 206 of file KernelSolution.h.

int lsst::ip::diffim::SpatialKernelSolution::_nkt
private

Number of kernel terms.

Definition at line 205 of file KernelSolution.h.

int lsst::ip::diffim::SpatialKernelSolution::_nt
private

Total number of terms.

Definition at line 207 of file KernelSolution.h.

lsst::pex::policy::Policy lsst::ip::diffim::SpatialKernelSolution::_policy
private

Policy to control processing.

Definition at line 203 of file KernelSolution.h.

lsst::afw::math::Kernel::SpatialFunctionPtr lsst::ip::diffim::SpatialKernelSolution::_spatialKernelFunction
private

Spatial function for Kernel.

Definition at line 196 of file KernelSolution.h.


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