LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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 1334 of file KernelSolution.cc.

1339  :
1340  KernelSolution(),
1341  _spatialKernelFunction(spatialKernelFunction),
1342  _constantFirstTerm(false),
1343  _kernel(),
1344  _background(background),
1345  _kSum(0.0),
1346  _policy(policy),
1347  _nbases(0),
1348  _nkt(0),
1349  _nbt(0),
1350  _nt(0) {
1351 
1352  bool isAlardLupton = _policy.getString("kernelBasisSet") == "alard-lupton";
1353  bool usePca = _policy.getBool("usePcaForSpatialKernel");
1354  if (isAlardLupton || usePca) {
1355  _constantFirstTerm = true;
1356  }
1357  this->_fitForBackground = _policy.getBool("fitForBackground");
1358 
1359  _nbases = basisList.size();
1360  _nkt = _spatialKernelFunction->getParameters().size();
1361  _nbt = _fitForBackground ? _background->getParameters().size() : 0;
1362  _nt = 0;
1363  if (_constantFirstTerm) {
1364  _nt = (_nbases - 1) * _nkt + 1 + _nbt;
1365  } else {
1366  _nt = _nbases * _nkt + _nbt;
1367  }
1368 
1369  boost::shared_ptr<Eigen::MatrixXd> mMat (new Eigen::MatrixXd(_nt, _nt));
1370  boost::shared_ptr<Eigen::VectorXd> bVec (new Eigen::VectorXd(_nt));
1371  (*mMat).setZero();
1372  (*bVec).setZero();
1373 
1374  this->_mMat = mMat;
1375  this->_bVec = bVec;
1376 
1379  );
1380 
1381  pexLog::TTrace<5>("lsst.ip.diffim.SpatialKernelSolution",
1382  "Initializing with size %d %d %d and constant first term = %s",
1383  _nkt, _nbt, _nt,
1384  _constantFirstTerm ? "true" : "false");
1385 
1386  }
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 1539 of file KernelSolution.cc.

1539  {
1540  /* Report on condition number */
1541  double cNumber = this->getConditionNumber(EIGENVALUE);
1542 
1543  if (_nkt == 1) {
1544  /* Not spatially varying; this fork is a specialization for convolution speed--up */
1545 
1546  /* Set the basis coefficients */
1547  std::vector<double> kCoeffs(_nbases);
1548  for (int i = 0; i < _nbases; i++) {
1549  if (std::isnan((*_aVec)(i))) {
1550  throw LSST_EXCEPT(
1552  str(boost::format(
1553  "I. Unable to determine spatial kernel solution %d (nan). Condition number = %.3e") % i % cNumber));
1554  }
1555  kCoeffs[i] = (*_aVec)(i);
1556  }
1557  lsst::afw::math::KernelList basisList =
1558  boost::dynamic_pointer_cast<afwMath::LinearCombinationKernel>(_kernel)->getKernelList();
1559  _kernel.reset(
1560  new afwMath::LinearCombinationKernel(basisList, kCoeffs)
1561  );
1562  }
1563  else {
1564 
1565  /* Set the kernel coefficients */
1566  std::vector<std::vector<double> > kCoeffs;
1567  kCoeffs.reserve(_nbases);
1568  for (int i = 0, idx = 0; i < _nbases; i++) {
1569  kCoeffs.push_back(std::vector<double>(_nkt));
1570 
1571  /* Deal with the possibility the first term doesn't vary spatially */
1572  if ((i == 0) && (_constantFirstTerm)) {
1573  if (std::isnan((*_aVec)(idx))) {
1574  throw LSST_EXCEPT(
1576  str(boost::format(
1577  "II. Unable to determine spatial kernel solution %d (nan). Condition number = %.3e") % idx % cNumber));
1578  }
1579  kCoeffs[i][0] = (*_aVec)(idx++);
1580  }
1581  else {
1582  for (int j = 0; j < _nkt; j++) {
1583  if (std::isnan((*_aVec)(idx))) {
1584  throw LSST_EXCEPT(
1586  str(boost::format(
1587  "III. Unable to determine spatial kernel solution %d (nan). Condition number = %.3e") % idx % cNumber));
1588  }
1589  kCoeffs[i][j] = (*_aVec)(idx++);
1590  }
1591  }
1592  }
1593  _kernel->setSpatialParameters(kCoeffs);
1594  }
1595 
1596  /* Set kernel Sum */
1597  ImageT::Ptr image (new ImageT(_kernel->getDimensions()));
1598  _kSum = _kernel->computeImage(*image, false);
1599 
1600  /* Set the background coefficients */
1601  std::vector<double> bgCoeffs(_fitForBackground ? _nbt : 1);
1602  if (_fitForBackground) {
1603  for (int i = 0; i < _nbt; i++) {
1604  int idx = _nt - _nbt + i;
1605  if (std::isnan((*_aVec)(idx))) {
1607  str(boost::format(
1608  "Unable to determine spatial background solution %d (nan)") % (idx)));
1609  }
1610  bgCoeffs[i] = (*_aVec)(idx);
1611  }
1612  }
1613  else {
1614  bgCoeffs[0] = 0.;
1615  }
1616  _background->setParameters(bgCoeffs);
1617  }
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 1388 of file KernelSolution.cc.

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

1531  {
1533  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot return solution");
1534  }
1535 
1536  return std::make_pair(_kernel, _background);
1537  }
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 1501 of file KernelSolution.cc.

1501  {
1503  throw LSST_EXCEPT(pexExcept::Exception, "Kernel not solved; cannot return image");
1504  }
1506  new afwImage::Image<afwMath::Kernel::Pixel>(_kernel->getDimensions())
1507  );
1508  (void)_kernel->computeImage(*image, false, pos[0], pos[1]);
1509  return image;
1510  }
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 1512 of file KernelSolution.cc.

1512  {
1513  /* Fill in the other half of mMat */
1514  for (int i = 0; i < _nt; i++) {
1515  for (int j = i+1; j < _nt; j++) {
1516  (*_mMat)(j,i) = (*_mMat)(i,j);
1517  }
1518  }
1519 
1520  try {
1522  } catch (pexExcept::Exception &e) {
1523  LSST_EXCEPT_ADD(e, "Unable to solve spatial kernel matrix");
1524  throw e;
1525  }
1526  /* Turn matrices into _kernel and _background */
1527  _setKernel();
1528  }
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: