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 | List of all members
lsst::ip::diffim::MaskedKernelSolution< InputT > Class Template Reference

#include <KernelSolution.h>

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

Public Types

typedef boost::shared_ptr
< MaskedKernelSolution< InputT > > 
Ptr
 
- Public Types inherited from lsst::ip::diffim::StaticKernelSolution< InputT >
typedef boost::shared_ptr
< StaticKernelSolution< InputT > > 
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

 MaskedKernelSolution (lsst::afw::math::KernelList const &basisList, bool fitForBackground)
 
virtual ~MaskedKernelSolution ()
 
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)
 
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)
 
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::afw::geom::Box2I maskBox)
 
- Public Member Functions inherited from lsst::ip::diffim::StaticKernelSolution< InputT >
 StaticKernelSolution (lsst::afw::math::KernelList const &basisList, bool fitForBackground)
 
virtual ~StaticKernelSolution ()
 
void solve ()
 
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
lsst::afw::math::Kernel::Ptr 
getKernel ()
 
virtual
lsst::afw::image::Image
< lsst::afw::math::Kernel::Pixel >
::Ptr 
makeKernelImage ()
 
virtual double getBackground ()
 
virtual double getKsum ()
 
virtual std::pair
< boost::shared_ptr
< lsst::afw::math::Kernel >
, double > 
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
 

Additional Inherited Members

- Protected Member Functions inherited from lsst::ip::diffim::StaticKernelSolution< InputT >
void _setKernel ()
 Set kernel after solution. More...
 
void _setKernelUncertainty ()
 Not implemented. More...
 
- Protected Attributes inherited from lsst::ip::diffim::StaticKernelSolution< InputT >
boost::shared_ptr
< Eigen::MatrixXd > 
_cMat
 K_i x R. More...
 
boost::shared_ptr
< Eigen::VectorXd > 
_iVec
 Vectorized I. More...
 
boost::shared_ptr
< Eigen::VectorXd > 
_ivVec
 Inverse variance. More...
 
lsst::afw::math::Kernel::Ptr _kernel
 Derived single-object convolution kernel. More...
 
double _background
 Derived differential background estimate. More...
 
double _kSum
 Derived kernel sum. More...
 
- 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

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

Definition at line 118 of file KernelSolution.h.

Member Typedef Documentation

template<typename InputT >
typedef boost::shared_ptr<MaskedKernelSolution<InputT> > lsst::ip::diffim::MaskedKernelSolution< InputT >::Ptr

Definition at line 120 of file KernelSolution.h.

Constructor & Destructor Documentation

template<typename InputT >
lsst::ip::diffim::MaskedKernelSolution< InputT >::MaskedKernelSolution ( lsst::afw::math::KernelList const &  basisList,
bool  fitForBackground 
)

Definition at line 503 of file KernelSolution.cc.

506  :
507  StaticKernelSolution<InputT>(basisList, fitForBackground)
508  {};
template<typename InputT >
virtual lsst::ip::diffim::MaskedKernelSolution< InputT >::~MaskedKernelSolution ( )
inlinevirtual

Definition at line 124 of file KernelSolution.h.

124 {};

Member Function Documentation

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

Definition at line 693 of file KernelSolution.cc.

698  {
699 
700  afwMath::Statistics varStats = afwMath::makeStatistics(varianceEstimate, afwMath::MIN);
701  if (varStats.getValue(afwMath::MIN) < 0.0) {
703  "Error: variance less than 0.0");
704  }
705  if (varStats.getValue(afwMath::MIN) == 0.0) {
707  "Error: variance equals 0.0, cannot inverse variance weight");
708  }
709 
710  lsst::afw::math::KernelList basisList =
711  boost::dynamic_pointer_cast<afwMath::LinearCombinationKernel>(this->_kernel)->getKernelList();
712  std::vector<boost::shared_ptr<afwMath::Kernel> >::const_iterator kiter = basisList.begin();
713 
714  /* Only BAD pixels marked in this mask */
716  afwImage::MaskPixel bitMask =
720  sMask &= bitMask;
721  /* TBD: Need to figure out a way to spread this; currently done in Python */
722 
723  unsigned int const nKernelParameters = basisList.size();
724  unsigned int const nBackgroundParameters = this->_fitForBackground ? 1 : 0;
725  unsigned int const nParameters = nKernelParameters + nBackgroundParameters;
726 
727  /* NOTE - we are accessing particular elements of Eigen arrays using
728  these coordinates, therefore they need to be in LOCAL coordinates.
729  This was written before ndarray unification.
730  */
731  /* Ignore known EDGE pixels for speed */
732  afwGeom::Box2I shrunkLocalBBox = (*kiter)->shrinkBBox(templateImage.getBBox(afwImage::LOCAL));
733  pexLog::TTrace<5>("lsst.ip.diffim.MaskedKernelSolution.build",
734  "Limits of good pixels after convolution: %d,%d -> %d,%d (local)",
735  shrunkLocalBBox.getMinX(), shrunkLocalBBox.getMinY(),
736  shrunkLocalBBox.getMaxX(), shrunkLocalBBox.getMaxY());
737 
738  /* Subimages are addressed in raw pixel coordinates */
739  unsigned int startCol = shrunkLocalBBox.getMinX();
740  unsigned int startRow = shrunkLocalBBox.getMinY();
741  unsigned int endCol = shrunkLocalBBox.getMaxX();
742  unsigned int endRow = shrunkLocalBBox.getMaxY();
743  /* Needed for Eigen block slicing operation */
744  endCol += 1;
745  endRow += 1;
746 
747  boost::timer t;
748  t.restart();
749 
750  /* Eigen representation of input images; only the pixels that are unconvolved in cimage below */
751  Eigen::MatrixXi eMask = maskToEigenMatrix(sMask).block(startRow,
752  startCol,
753  endRow-startRow,
754  endCol-startCol);
755 
756  Eigen::MatrixXd eigenTemplate = imageToEigenMatrix(templateImage).block(startRow,
757  startCol,
758  endRow-startRow,
759  endCol-startCol);
760  Eigen::MatrixXd eigenScience = imageToEigenMatrix(scienceImage).block(startRow,
761  startCol,
762  endRow-startRow,
763  endCol-startCol);
764  Eigen::MatrixXd eigeniVariance = imageToEigenMatrix(varianceEstimate).block(
765  startRow, startCol, endRow-startRow, endCol-startCol
766  ).array().inverse().matrix();
767 
768  /* Resize into 1-D for later usage */
769  eMask.resize(eMask.rows()*eMask.cols(), 1);
770  eigenTemplate.resize(eigenTemplate.rows()*eigenTemplate.cols(), 1);
771  eigenScience.resize(eigenScience.rows()*eigenScience.cols(), 1);
772  eigeniVariance.resize(eigeniVariance.rows()*eigeniVariance.cols(), 1);
773 
774  /* Do the masking, slowly for now... */
775  Eigen::MatrixXd maskedEigenTemplate(eigenTemplate.rows(), 1);
776  Eigen::MatrixXd maskedEigenScience(eigenScience.rows(), 1);
777  Eigen::MatrixXd maskedEigeniVariance(eigeniVariance.rows(), 1);
778  int nGood = 0;
779  for (int i = 0; i < eMask.rows(); i++) {
780  if (eMask(i, 0) == 0) {
781  maskedEigenTemplate(nGood, 0) = eigenTemplate(i, 0);
782  maskedEigenScience(nGood, 0) = eigenScience(i, 0);
783  maskedEigeniVariance(nGood, 0) = eigeniVariance(i, 0);
784  nGood += 1;
785  }
786  }
787  /* Can't resize() since all values are lost; use blocks */
788  eigenTemplate = maskedEigenTemplate.block(0, 0, nGood, 1);
789  eigenScience = maskedEigenScience.block(0, 0, nGood, 1);
790  eigeniVariance = maskedEigeniVariance.block(0, 0, nGood, 1);
791 
792 
793  /* Holds image convolved with basis function */
794  afwImage::Image<InputT> cimage(templateImage.getDimensions());
795 
796  /* Holds eigen representation of image convolved with all basis functions */
797  std::vector<boost::shared_ptr<Eigen::MatrixXd> > convolvedEigenList(nKernelParameters);
798 
799  /* Iterators over convolved image list and basis list */
800  typename std::vector<boost::shared_ptr<Eigen::MatrixXd> >::iterator eiter =
801  convolvedEigenList.begin();
802  /* Create C_i in the formalism of Alard & Lupton */
803  for (kiter = basisList.begin(); kiter != basisList.end(); ++kiter, ++eiter) {
804  afwMath::convolve(cimage, templateImage, **kiter, false); /* cimage stores convolved image */
805 
806  Eigen::MatrixXd cMat = imageToEigenMatrix(cimage).block(startRow,
807  startCol,
808  endRow-startRow,
809  endCol-startCol);
810  cMat.resize(cMat.rows() * cMat.cols(), 1);
811 
812  /* Do masking */
813  Eigen::MatrixXd maskedcMat(cMat.rows(), 1);
814  int nGood = 0;
815  for (int i = 0; i < eMask.rows(); i++) {
816  if (eMask(i, 0) == 0) {
817  maskedcMat(nGood, 0) = cMat(i, 0);
818  nGood += 1;
819  }
820  }
821  cMat = maskedcMat.block(0, 0, nGood, 1);
822  boost::shared_ptr<Eigen::MatrixXd> cMatPtr (new Eigen::MatrixXd(cMat));
823  *eiter = cMatPtr;
824  }
825 
826  double time = t.elapsed();
827  pexLog::TTrace<5>("lsst.ip.diffim.StaticKernelSolution.build",
828  "Total compute time to do basis convolutions : %.2f s", time);
829  t.restart();
830 
831  /*
832  Load matrix with all values from convolvedEigenList : all images
833  (eigeniVariance, convolvedEigenList) must be the same size
834  */
835  Eigen::MatrixXd cMat(eigenTemplate.col(0).size(), nParameters);
836  typename std::vector<boost::shared_ptr<Eigen::MatrixXd> >::iterator eiterj =
837  convolvedEigenList.begin();
838  typename std::vector<boost::shared_ptr<Eigen::MatrixXd> >::iterator eiterE =
839  convolvedEigenList.end();
840  for (unsigned int kidxj = 0; eiterj != eiterE; eiterj++, kidxj++) {
841  cMat.col(kidxj) = (*eiterj)->col(0);
842  }
843  /* Treat the last "image" as all 1's to do the background calculation. */
844  if (this->_fitForBackground)
845  cMat.col(nParameters-1).fill(1.);
846 
847  this->_cMat.reset(new Eigen::MatrixXd(cMat));
848  this->_ivVec.reset(new Eigen::VectorXd(eigeniVariance.col(0)));
849  this->_iVec.reset(new Eigen::VectorXd(eigenScience.col(0)));
850 
851  /* Make these outside of solve() so I can check condition number */
852  this->_mMat.reset(
853  new Eigen::MatrixXd(this->_cMat->transpose() * this->_ivVec->asDiagonal() * *(this->_cMat))
854  );
855  this->_bVec.reset(
856  new Eigen::VectorXd(this->_cMat->transpose() * this->_ivVec->asDiagonal() * *(this->_iVec))
857  );
858 
859  }
int getMaxY() const
Definition: Box.h:129
Eigen::MatrixXi maskToEigenMatrix(lsst::afw::image::Mask< lsst::afw::image::MaskPixel > const &mask)
estimate sample minimum
Definition: Statistics.h:76
boost::uint16_t MaskPixel
lsst::afw::math::Kernel::Ptr _kernel
Derived single-object convolution kernel.
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: Image.h:377
An integer coordinate rectangle.
Definition: Box.h:53
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1009
bool _fitForBackground
Background terms included in fit.
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: Image.h:298
int getMinY() const
Definition: Box.h:125
boost::shared_ptr< Eigen::VectorXd > _iVec
Vectorized I.
A kernel that is a linear combination of fixed basis kernels.
Definition: Kernel.h:814
int getMinX() const
Definition: Box.h:124
boost::shared_ptr< Eigen::VectorXd > _bVec
Derived least squares B vector.
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
Return the bitmask corresponding to a vector of plane names OR&#39;d together.
Definition: Mask.cc:860
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
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...
boost::shared_ptr< Eigen::MatrixXd > _mMat
Derived least squares M matrix.
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
boost::shared_ptr< Eigen::MatrixXd > _cMat
K_i x R.
int getMaxX() const
Definition: Box.h:128
boost::shared_ptr< Eigen::VectorXd > _ivVec
Inverse variance.
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:415
Eigen::MatrixXd imageToEigenMatrix(lsst::afw::image::Image< PixelT > const &img)
Turns Image into a 2-D Eigen Matrix.
std::vector< boost::shared_ptr< Kernel > > KernelList
Definition: Kernel.h:542
template<typename InputT >
void lsst::ip::diffim::MaskedKernelSolution< InputT >::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::afw::geom::Box2I  maskBox 
)
virtual

Definition at line 864 of file KernelSolution.cc.

869  {
870 
871  afwMath::Statistics varStats = afwMath::makeStatistics(varianceEstimate, afwMath::MIN);
872  if (varStats.getValue(afwMath::MIN) < 0.0) {
874  "Error: variance less than 0.0");
875  }
876  if (varStats.getValue(afwMath::MIN) == 0.0) {
878  "Error: variance equals 0.0, cannot inverse variance weight");
879  }
880 
881  lsst::afw::math::KernelList basisList =
882  boost::dynamic_pointer_cast<afwMath::LinearCombinationKernel>(this->_kernel)->getKernelList();
883 
884  unsigned int const nKernelParameters = basisList.size();
885  unsigned int const nBackgroundParameters = this->_fitForBackground ? 1 : 0;
886  unsigned int const nParameters = nKernelParameters + nBackgroundParameters;
887 
888  std::vector<boost::shared_ptr<afwMath::Kernel> >::const_iterator kiter = basisList.begin();
889 
890  /*
891  NOTE : If we are using these views in Afw's Image space, we need to
892  make sure and compensate for the XY0 of the image:
893 
894  afwGeom::Box2I fullBBox = templateImage.getBBox();
895  int maskStartCol = maskBox.getMinX();
896  int maskStartRow = maskBox.getMinY();
897  int maskEndCol = maskBox.getMaxX();
898  int maskEndRow = maskBox.getMaxY();
899 
900 
901  If we are going to be doing the slicing in Eigen matrices derived from
902  the images, ignore the XY0.
903 
904  afwGeom::Box2I fullBBox = templateImage.getBBox(afwImage::LOCAL);
905 
906  int maskStartCol = maskBox.getMinX() - templateImage.getX0();
907  int maskStartRow = maskBox.getMinY() - templateImage.getY0();
908  int maskEndCol = maskBox.getMaxX() - templateImage.getX0();
909  int maskEndRow = maskBox.getMaxY() - templateImage.getY0();
910 
911  */
912 
913 
914  afwGeom::Box2I shrunkBBox = (*kiter)->shrinkBBox(templateImage.getBBox());
915 
916  pexLog::TTrace<5>("lsst.ip.diffim.MaskedKernelSolution.build",
917  "Limits of good pixels after convolution: %d,%d -> %d,%d",
918  shrunkBBox.getMinX(), shrunkBBox.getMinY(),
919  shrunkBBox.getMaxX(), shrunkBBox.getMaxY());
920 
921  unsigned int const startCol = shrunkBBox.getMinX();
922  unsigned int const startRow = shrunkBBox.getMinY();
923  unsigned int const endCol = shrunkBBox.getMaxX();
924  unsigned int const endRow = shrunkBBox.getMaxY();
925 
926  /* NOTE: no endCol/endRow += 1 for block slicing, since we are doing the
927  slicing using Afw, not Eigen
928 
929  Eigen arrays have index 0,0 in the upper right, while LSST images
930  have 0,0 in the lower left. The y-axis is flipped. When translating
931  Images to Eigen matrices in ipDiffim::imageToEigenMatrix this is
932  accounted for. However, we still need to be aware of this fact if
933  addressing subregions of an Eigen matrix. This is why the slicing is
934  done in Afw, its cleaner.
935 
936  Please see examples/maskedKernel.cc for elaboration on some of the
937  tests done to make sure this indexing gets done right.
938 
939  */
940 
941 
942  /* Inner limits; of mask box */
943  int maskStartCol = maskBox.getMinX();
944  int maskStartRow = maskBox.getMinY();
945  int maskEndCol = maskBox.getMaxX();
946  int maskEndRow = maskBox.getMaxY();
947 
948  /*
949 
950  |---------------------------|
951  | Kernel Boundary |
952  | |---------------------| |
953  | | Top | |
954  | |......_________......| |
955  | | | | | |
956  | | L | Box | R | |
957  | | | | | |
958  | |......---------......| |
959  | | Bottom | |
960  | |---------------------| |
961  | |
962  |---------------------------|
963 
964  4 regions we want to extract from the pixels: top bottom left right
965 
966  */
967  afwGeom::Box2I tBox = afwGeom::Box2I(afwGeom::Point2I(startCol, maskEndRow + 1),
968  afwGeom::Point2I(endCol, endRow));
969 
970  afwGeom::Box2I bBox = afwGeom::Box2I(afwGeom::Point2I(startCol, startRow),
971  afwGeom::Point2I(endCol, maskStartRow - 1));
972 
973  afwGeom::Box2I lBox = afwGeom::Box2I(afwGeom::Point2I(startCol, maskStartRow),
974  afwGeom::Point2I(maskStartCol - 1, maskEndRow));
975 
976  afwGeom::Box2I rBox = afwGeom::Box2I(afwGeom::Point2I(maskEndCol + 1, maskStartRow),
977  afwGeom::Point2I(endCol, maskEndRow));
978 
979  pexLog::TTrace<5>("lsst.ip.diffim.MaskedKernelSolution.build",
980  "Upper good pixel region: %d,%d -> %d,%d",
981  tBox.getMinX(), tBox.getMinY(), tBox.getMaxX(), tBox.getMaxY());
982  pexLog::TTrace<5>("lsst.ip.diffim.MaskedKernelSolution.build",
983  "Bottom good pixel region: %d,%d -> %d,%d",
984  bBox.getMinX(), bBox.getMinY(), bBox.getMaxX(), bBox.getMaxY());
985  pexLog::TTrace<5>("lsst.ip.diffim.MaskedKernelSolution.build",
986  "Left good pixel region: %d,%d -> %d,%d",
987  lBox.getMinX(), lBox.getMinY(), lBox.getMaxX(), lBox.getMaxY());
988  pexLog::TTrace<5>("lsst.ip.diffim.MaskedKernelSolution.build",
989  "Right good pixel region: %d,%d -> %d,%d",
990  rBox.getMinX(), rBox.getMinY(), rBox.getMaxX(), rBox.getMaxY());
991 
992  std::vector<afwGeom::Box2I> boxArray;
993  boxArray.push_back(tBox);
994  boxArray.push_back(bBox);
995  boxArray.push_back(lBox);
996  boxArray.push_back(rBox);
997 
998  int totalSize = tBox.getWidth() * tBox.getHeight();
999  totalSize += bBox.getWidth() * bBox.getHeight();
1000  totalSize += lBox.getWidth() * lBox.getHeight();
1001  totalSize += rBox.getWidth() * rBox.getHeight();
1002 
1003  Eigen::MatrixXd eigenTemplate(totalSize, 1);
1004  Eigen::MatrixXd eigenScience(totalSize, 1);
1005  Eigen::MatrixXd eigeniVariance(totalSize, 1);
1006  eigenTemplate.setZero();
1007  eigenScience.setZero();
1008  eigeniVariance.setZero();
1009 
1010  boost::timer t;
1011  t.restart();
1012 
1013  int nTerms = 0;
1014  typename std::vector<afwGeom::Box2I>::iterator biter = boxArray.begin();
1015  for (; biter != boxArray.end(); ++biter) {
1016  int area = (*biter).getWidth() * (*biter).getHeight();
1017 
1018  afwImage::Image<InputT> siTemplate(templateImage, *biter);
1019  afwImage::Image<InputT> siScience(scienceImage, *biter);
1020  afwImage::Image<InputT> sVarEstimate(varianceEstimate, *biter);
1021 
1022  Eigen::MatrixXd eTemplate = imageToEigenMatrix(siTemplate);
1023  Eigen::MatrixXd eScience = imageToEigenMatrix(siScience);
1024  Eigen::MatrixXd eiVarEstimate = imageToEigenMatrix(sVarEstimate).array().inverse().matrix();
1025 
1026  eTemplate.resize(area, 1);
1027  eScience.resize(area, 1);
1028  eiVarEstimate.resize(area, 1);
1029 
1030  eigenTemplate.block(nTerms, 0, area, 1) = eTemplate.block(0, 0, area, 1);
1031  eigenScience.block(nTerms, 0, area, 1) = eScience.block(0, 0, area, 1);
1032  eigeniVariance.block(nTerms, 0, area, 1) = eiVarEstimate.block(0, 0, area, 1);
1033 
1034  nTerms += area;
1035  }
1036 
1037  afwImage::Image<InputT> cimage(templateImage.getDimensions());
1038 
1039  std::vector<boost::shared_ptr<Eigen::MatrixXd> > convolvedEigenList(nKernelParameters);
1040  typename std::vector<boost::shared_ptr<Eigen::MatrixXd> >::iterator eiter =
1041  convolvedEigenList.begin();
1042  /* Create C_i in the formalism of Alard & Lupton */
1043  for (kiter = basisList.begin(); kiter != basisList.end(); ++kiter, ++eiter) {
1044  afwMath::convolve(cimage, templateImage, **kiter, false); /* cimage stores convolved image */
1045  Eigen::MatrixXd cMat(totalSize, 1);
1046  cMat.setZero();
1047 
1048  int nTerms = 0;
1049  typename std::vector<afwGeom::Box2I>::iterator biter = boxArray.begin();
1050  for (; biter != boxArray.end(); ++biter) {
1051  int area = (*biter).getWidth() * (*biter).getHeight();
1052 
1053  afwImage::Image<InputT> csubimage(cimage, *biter);
1054  Eigen::MatrixXd esubimage = imageToEigenMatrix(csubimage);
1055  esubimage.resize(area, 1);
1056  cMat.block(nTerms, 0, area, 1) = esubimage.block(0, 0, area, 1);
1057 
1058  nTerms += area;
1059  }
1060 
1061  boost::shared_ptr<Eigen::MatrixXd> cMatPtr (new Eigen::MatrixXd(cMat));
1062  *eiter = cMatPtr;
1063 
1064  }
1065 
1066  double time = t.elapsed();
1067  pexLog::TTrace<5>("lsst.ip.diffim.MaskedKernelSolution.build",
1068  "Total compute time to do basis convolutions : %.2f s", time);
1069  t.restart();
1070 
1071  /*
1072  Load matrix with all values from convolvedEigenList : all images
1073  (eigeniVariance, convolvedEigenList) must be the same size
1074  */
1075  Eigen::MatrixXd cMat(eigenTemplate.col(0).size(), nParameters);
1076  typename std::vector<boost::shared_ptr<Eigen::MatrixXd> >::iterator eiterj =
1077  convolvedEigenList.begin();
1078  typename std::vector<boost::shared_ptr<Eigen::MatrixXd> >::iterator eiterE =
1079  convolvedEigenList.end();
1080  for (unsigned int kidxj = 0; eiterj != eiterE; eiterj++, kidxj++) {
1081  cMat.col(kidxj) = (*eiterj)->col(0);
1082  }
1083  /* Treat the last "image" as all 1's to do the background calculation. */
1084  if (this->_fitForBackground)
1085  cMat.col(nParameters-1).fill(1.);
1086 
1087  this->_cMat.reset(new Eigen::MatrixXd(cMat));
1088  this->_ivVec.reset(new Eigen::VectorXd(eigeniVariance.col(0)));
1089  this->_iVec.reset(new Eigen::VectorXd(eigenScience.col(0)));
1090 
1091  /* Make these outside of solve() so I can check condition number */
1092  this->_mMat.reset(
1093  new Eigen::MatrixXd(this->_cMat->transpose() * this->_ivVec->asDiagonal() * *(this->_cMat))
1094  );
1095  this->_bVec.reset(
1096  new Eigen::VectorXd(this->_cMat->transpose() * this->_ivVec->asDiagonal() * *(this->_iVec))
1097  );
1098 
1099  }
int getMaxY() const
Definition: Box.h:129
estimate sample minimum
Definition: Statistics.h:76
lsst::afw::math::Kernel::Ptr _kernel
Derived single-object convolution kernel.
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: Image.h:377
An integer coordinate rectangle.
Definition: Box.h:53
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1009
bool _fitForBackground
Background terms included in fit.
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: Image.h:298
int getMinY() const
Definition: Box.h:125
boost::shared_ptr< Eigen::VectorXd > _iVec
Vectorized I.
A kernel that is a linear combination of fixed basis kernels.
Definition: Kernel.h:814
int getMinX() const
Definition: Box.h:124
boost::shared_ptr< Eigen::VectorXd > _bVec
Derived least squares B vector.
int getWidth() const
Definition: Box.h:154
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
int getHeight() const
Definition: Box.h:155
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...
boost::shared_ptr< Eigen::MatrixXd > _mMat
Derived least squares M matrix.
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
boost::shared_ptr< Eigen::MatrixXd > _cMat
K_i x R.
int getMaxX() const
Definition: Box.h:128
boost::shared_ptr< Eigen::VectorXd > _ivVec
Inverse variance.
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:415
Eigen::MatrixXd imageToEigenMatrix(lsst::afw::image::Image< PixelT > const &img)
Turns Image into a 2-D Eigen Matrix.
std::vector< boost::shared_ptr< Kernel > > KernelList
Definition: Kernel.h:542
template<typename InputT >
void lsst::ip::diffim::MaskedKernelSolution< InputT >::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 
)
virtual

Definition at line 511 of file KernelSolution.cc.

516  {
517 
518  afwMath::Statistics varStats = afwMath::makeStatistics(varianceEstimate, afwMath::MIN);
519  if (varStats.getValue(afwMath::MIN) < 0.0) {
521  "Error: variance less than 0.0");
522  }
523  if (varStats.getValue(afwMath::MIN) == 0.0) {
525  "Error: variance equals 0.0, cannot inverse variance weight");
526  }
527 
528  /* Full footprint of all input images */
529  afwDet::Footprint::Ptr fullFp(new afwDet::Footprint(templateImage.getBBox()));
530 
531  afwMath::KernelList basisList =
532  boost::dynamic_pointer_cast<afwMath::LinearCombinationKernel>(this->_kernel)->getKernelList();
533  std::vector<boost::shared_ptr<afwMath::Kernel> >::const_iterator kiter = basisList.begin();
534 
535  /* Only BAD pixels marked in this mask */
536  afwImage::MaskPixel bitMask =
541 
542  /* Create a Footprint that contains all the masked pixels set above */
544  afwDet::FootprintSet maskFpSet(pixelMask, threshold, true);
545 
546  /* And spread it by the kernel half width */
547  int growPix = (*kiter)->getCtr().getX();
548  afwDet::FootprintSet maskedFpSetGrown(maskFpSet, growPix, true);
549 
550 #if 0
551  for (typename afwDet::FootprintSet::FootprintList::iterator
552  ptr = maskedFpSetGrown.getFootprints()->begin(),
553  end = maskedFpSetGrown.getFootprints()->end();
554  ptr != end;
555  ++ptr) {
556 
557  afwDet::setMaskFromFootprint(finalMask,
558  (**ptr),
560  }
561 #endif
562 
563  afwImage::Mask<afwImage::MaskPixel> finalMask(pixelMask.getDimensions());
565  *(maskedFpSetGrown.getFootprints()),
567  pixelMask.writeFits("pixelmask.fits");
568  finalMask.writeFits("finalmask.fits");
569 
570 
571  ndarray::Array<int, 1, 1> maskArray =
572  ndarray::allocate(ndarray::makeVector(fullFp->getArea()));
573  afwDet::flattenArray(*fullFp, finalMask.getArray(),
574  maskArray, templateImage.getXY0()); /* Need to fake the XY0 */
575  ndarray::EigenView<int, 1, 1> maskEigen(maskArray);
576 
577  ndarray::Array<InputT, 1, 1> arrayTemplate =
578  ndarray::allocate(ndarray::makeVector(fullFp->getArea()));
579  afwDet::flattenArray(*fullFp, templateImage.getArray(),
580  arrayTemplate, templateImage.getXY0());
581  ndarray::EigenView<InputT, 1, 1> eigenTemplate0(arrayTemplate);
582 
583  ndarray::Array<InputT, 1, 1> arrayScience =
584  ndarray::allocate(ndarray::makeVector(fullFp->getArea()));
585  afwDet::flattenArray(*fullFp, scienceImage.getArray(),
586  arrayScience, scienceImage.getXY0());
587  ndarray::EigenView<InputT, 1, 1> eigenScience0(arrayScience);
588 
590  ndarray::allocate(ndarray::makeVector(fullFp->getArea()));
591  afwDet::flattenArray(*fullFp, varianceEstimate.getArray(),
592  arrayVariance, varianceEstimate.getXY0());
593  ndarray::EigenView<afwImage::VariancePixel, 1, 1> eigenVariance0(arrayVariance);
594 
595  int nGood = 0;
596  for (int i = 0; i < maskEigen.size(); i++) {
597  if (maskEigen(i) == 0.0)
598  nGood += 1;
599  }
600 
601  Eigen::VectorXd eigenTemplate(nGood);
602  Eigen::VectorXd eigenScience(nGood);
603  Eigen::VectorXd eigenVariance(nGood);
604  int nUsed = 0;
605  for (int i = 0; i < maskEigen.size(); i++) {
606  if (maskEigen(i) == 0.0) {
607  eigenTemplate(nUsed) = eigenTemplate0(i);
608  eigenScience(nUsed) = eigenScience0(i);
609  eigenVariance(nUsed) = eigenVariance0(i);
610  nUsed += 1;
611  }
612  }
613 
614 
615  boost::timer t;
616  t.restart();
617 
618  unsigned int const nKernelParameters = basisList.size();
619  unsigned int const nBackgroundParameters = this->_fitForBackground ? 1 : 0;
620  unsigned int const nParameters = nKernelParameters + nBackgroundParameters;
621 
622  /* Holds image convolved with basis function */
623  afwImage::Image<InputT> cimage(templateImage.getDimensions());
624 
625  /* Holds eigen representation of image convolved with all basis functions */
626  std::vector<boost::shared_ptr<Eigen::VectorXd> >
627  convolvedEigenList(nKernelParameters);
628 
629  /* Iterators over convolved image list and basis list */
630  typename std::vector<boost::shared_ptr<Eigen::VectorXd> >::iterator eiter =
631  convolvedEigenList.begin();
632 
633  /* Create C_i in the formalism of Alard & Lupton */
634  for (kiter = basisList.begin(); kiter != basisList.end(); ++kiter, ++eiter) {
635  afwMath::convolve(cimage, templateImage, **kiter, false); /* cimage stores convolved image */
636 
638  ndarray::allocate(ndarray::makeVector(fullFp->getArea()));
639  afwDet::flattenArray(*fullFp, cimage.getArray(),
640  arrayC, cimage.getXY0());
641  ndarray::EigenView<InputT, 1, 1> eigenC0(arrayC);
642 
643  Eigen::VectorXd eigenC(nGood);
644  int nUsed = 0;
645  for (int i = 0; i < maskEigen.size(); i++) {
646  if (maskEigen(i) == 0.0) {
647  eigenC(nUsed) = eigenC0(i);
648  nUsed += 1;
649  }
650  }
651 
652  boost::shared_ptr<Eigen::VectorXd>
653  eigenCptr (new Eigen::VectorXd(eigenC));
654 
655  *eiter = eigenCptr;
656  }
657  double time = t.elapsed();
658  pexLog::TTrace<5>("lsst.ip.diffim.StaticKernelSolution.buildWithMask",
659  "Total compute time to do basis convolutions : %.2f s", time);
660  t.restart();
661 
662  /* Load matrix with all convolved images */
663  Eigen::MatrixXd cMat(eigenTemplate.size(), nParameters);
664  typename std::vector<boost::shared_ptr<Eigen::VectorXd> >::iterator eiterj =
665  convolvedEigenList.begin();
666  typename std::vector<boost::shared_ptr<Eigen::VectorXd> >::iterator eiterE =
667  convolvedEigenList.end();
668  for (unsigned int kidxj = 0; eiterj != eiterE; eiterj++, kidxj++) {
669  cMat.block(0, kidxj, eigenTemplate.size(), 1) =
670  Eigen::MatrixXd(**eiterj).block(0, 0, eigenTemplate.size(), 1);
671  }
672  /* Treat the last "image" as all 1's to do the background calculation. */
673  if (this->_fitForBackground)
674  cMat.col(nParameters-1).fill(1.);
675 
676  this->_cMat.reset(new Eigen::MatrixXd(cMat));
677  //this->_ivVec.reset(new Eigen::VectorXd((eigenVariance.template cast<double>()).cwise().inverse()));
678  //this->_iVec.reset(new Eigen::VectorXd(eigenScience.template cast<double>()));
679  this->_ivVec.reset(new Eigen::VectorXd(eigenVariance.array().inverse().matrix()));
680  this->_iVec.reset(new Eigen::VectorXd(eigenScience));
681 
682  /* Make these outside of solve() so I can check condition number */
683  this->_mMat.reset(
684  new Eigen::MatrixXd(this->_cMat->transpose() * this->_ivVec->asDiagonal() * *(this->_cMat))
685  );
686  this->_bVec.reset(
687  new Eigen::VectorXd(this->_cMat->transpose() * this->_ivVec->asDiagonal() * *(this->_iVec))
688  );
689  }
void flattenArray(Footprint const &fp, ndarray::Array< T, N, C > const &src, ndarray::Array< U, N-1, D > const &dest, lsst::afw::geom::Point2I const &xy0=lsst::afw::geom::Point2I())
Flatten the first two dimensions of an array.
boost::shared_ptr< Footprint > Ptr
Definition: Footprint.h:67
void writeFits(std::string const &fileName, boost::shared_ptr< lsst::daf::base::PropertySet const > metadata=boost::shared_ptr< lsst::daf::base::PropertySet >(), std::string const &mode="w") const
Write a mask to a regular FITS file.
estimate sample minimum
Definition: Statistics.h:76
Eigen3 view into an ndarray::Array.
Definition: eigen.h:232
boost::uint16_t MaskPixel
lsst::afw::math::Kernel::Ptr _kernel
Derived single-object convolution kernel.
Use (pixels &amp; (given mask))
Definition: Threshold.h:49
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:44
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: Image.h:377
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1009
geom::Point2I getXY0() const
Definition: Image.h:264
bool _fitForBackground
Background terms included in fit.
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: Image.h:298
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:93
boost::shared_ptr< Eigen::VectorXd > _iVec
Vectorized I.
A kernel that is a linear combination of fixed basis kernels.
Definition: Kernel.h:814
Vector< T, N > makeVector(T v1, T v2,..., T vN)
Variadic constructor for Vector.
boost::shared_ptr< Eigen::VectorXd > _bVec
Derived least squares B vector.
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
Return the bitmask corresponding to a vector of plane names OR&#39;d together.
Definition: Mask.cc:860
A set of pixels in an Image.
Definition: Footprint.h:62
detail::SimpleInitializer< N > allocate(Vector< int, N > const &shape)
Create an expression that allocates uninitialized memory for an array.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
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...
boost::shared_ptr< Eigen::MatrixXd > _mMat
Derived least squares M matrix.
MaskT setMaskFromFootprint(lsst::afw::image::Mask< MaskT > *mask, Footprint const &footprint, MaskT const bitmask)
OR bitmask into all the Mask&#39;s pixels that are in the Footprint.
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
boost::shared_ptr< Eigen::MatrixXd > _cMat
K_i x R.
boost::shared_ptr< Eigen::VectorXd > _ivVec
Inverse variance.
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:415
MaskT setMaskFromFootprintList(lsst::afw::image::Mask< MaskT > *mask, std::vector< boost::shared_ptr< Footprint >> const &footprints, MaskT const bitmask)
OR bitmask into all the Mask&#39;s pixels which are in the set of Footprints.
std::vector< boost::shared_ptr< Kernel > > KernelList
Definition: Kernel.h:542

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