LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
Public Types | Public Member Functions | List of all members
lsst::afw::image::ImagePca< ImageT > Class Template Reference

#include <ImagePca.h>

Inheritance diagram for lsst::afw::image::ImagePca< ImageT >:
lsst::ip::diffim::detail::KernelPca< ImageT > lsst::meas::algorithms::PsfImagePca< ImageT >

Public Types

typedef std::vector< std::shared_ptr< ImageT > > ImageList
 

Public Member Functions

 ImagePca (bool constantWeight=true)
 ctor More...
 
virtual ~ImagePca ()=default
 
 ImagePca (ImagePca const &)
 
 ImagePca (ImagePca &&)
 
ImagePcaoperator= (ImagePca const &)
 
ImagePcaoperator= (ImagePca &&)
 
void addImage (std::shared_ptr< ImageT > img, double flux=0.0)
 Add an image to the set to be analyzed. More...
 
ImageList getImageList () const
 Return the list of images being analyzed. More...
 
lsst::geom::Extent2I const getDimensions () const
 Return the dimension of the images being analyzed. More...
 
std::shared_ptr< ImageT > getMean () const
 Return the mean of the images in ImagePca's list. More...
 
virtual void analyze ()
 
virtual double updateBadPixels (unsigned long mask, int const ncomp)
 Update the bad pixels (i.e. More...
 
std::vector< double > const & getEigenValues () const
 Return Eigen values. More...
 
ImageList const & getEigenImages () const
 Return Eigen images. More...
 

Detailed Description

template<typename ImageT>
class lsst::afw::image::ImagePca< ImageT >

Definition at line 46 of file ImagePca.h.

Member Typedef Documentation

◆ ImageList

template<typename ImageT >
typedef std::vector<std::shared_ptr<ImageT> > lsst::afw::image::ImagePca< ImageT >::ImageList

Definition at line 48 of file ImagePca.h.

Constructor & Destructor Documentation

◆ ImagePca() [1/3]

template<typename ImageT >
lsst::afw::image::ImagePca< ImageT >::ImagePca ( bool  constantWeight = true)
explicit

ctor

Parameters
constantWeightShould all stars be weighted equally?

Definition at line 47 of file ImagePca.cc.

48  : _imageList(),
49  _fluxList(),
50  _dimensions(0, 0),
51  _constantWeight(constantWeight),
52  _eigenValues(std::vector<double>()),
53  _eigenImages(ImageList()) {}
std::vector< std::shared_ptr< ImageT > > ImageList
Definition: ImagePca.h:48

◆ ~ImagePca()

template<typename ImageT >
virtual lsst::afw::image::ImagePca< ImageT >::~ImagePca ( )
virtualdefault

◆ ImagePca() [2/3]

template<typename ImageT >
lsst::afw::image::ImagePca< ImageT >::ImagePca ( ImagePca< ImageT > const &  )
default

◆ ImagePca() [3/3]

template<typename ImageT >
lsst::afw::image::ImagePca< ImageT >::ImagePca ( ImagePca< ImageT > &&  )
default

Member Function Documentation

◆ addImage()

template<typename ImageT >
void lsst::afw::image::ImagePca< ImageT >::addImage ( std::shared_ptr< ImageT >  img,
double  flux = 0.0 
)

Add an image to the set to be analyzed.

Parameters
imgImage to add to set
fluxImage's flux
Exceptions
lsst::pex::exceptions::LengthErrorif all the images aren't the same size

Definition at line 65 of file ImagePca.cc.

65  {
66  if (_imageList.empty()) {
67  _dimensions = img->getDimensions();
68  } else {
69  if (getDimensions() != img->getDimensions()) {
71  (boost::format("Dimension mismatch: saw %dx%d; expected %dx%d") %
72  img->getWidth() % img->getHeight() % _dimensions.getX() % _dimensions.getY())
73  .str());
74  }
75  }
76 
77  if (flux == 0.0) {
78  throw LSST_EXCEPT(lsst::pex::exceptions::OutOfRangeError, "Flux may not be zero");
79  }
80 
81  _imageList.push_back(img);
82  _fluxList.push_back(flux);
83 }
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::geom::Extent2I const getDimensions() const
Return the dimension of the images being analyzed.
Definition: ImagePca.h:76
Reports attempts to exceed implementation-defined length limits for some classes.
Definition: Runtime.h:76
Reports attempts to access elements outside a valid range of indices.
Definition: Runtime.h:89
T empty(T... args)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
T push_back(T... args)

◆ analyze()

template<typename ImageT >
void lsst::afw::image::ImagePca< ImageT >::analyze
virtual

Reimplemented in lsst::meas::algorithms::PsfImagePca< ImageT >, and lsst::ip::diffim::detail::KernelPca< ImageT >.

Definition at line 124 of file ImagePca.cc.

124  {
125  int const nImage = _imageList.size();
126 
127  if (nImage == 0) {
128  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError, "No images provided for PCA analysis");
129  }
130  /*
131  * Eigen doesn't like 1x1 matrices, but we don't really need it to handle a single matrix...
132  */
133  if (nImage == 1) {
134  _eigenImages.clear();
135  _eigenImages.push_back(std::shared_ptr<ImageT>(new ImageT(*_imageList[0], true)));
136 
137  _eigenValues.clear();
138  _eigenValues.push_back(1.0);
139 
140  return;
141  }
142  /*
143  * Find the eigenvectors/values of the scalar product matrix, R' (Eq. 7.4)
144  */
145  Eigen::MatrixXd R(nImage, nImage); // residuals' inner products
146 
147  double flux_bar = 0; // mean of flux for all regions
148  for (int i = 0; i != nImage; ++i) {
149  typename GetImage<ImageT>::type const& im_i = *GetImage<ImageT>::getImage(_imageList[i]);
150  double const flux_i = getFlux(i);
151  flux_bar += flux_i;
152 
153  for (int j = i; j != nImage; ++j) {
154  typename GetImage<ImageT>::type const& im_j = *GetImage<ImageT>::getImage(_imageList[j]);
155  double const flux_j = getFlux(j);
156 
157  double dot = innerProduct(im_i, im_j);
158  if (_constantWeight) {
159  dot /= flux_i * flux_j;
160  }
161  R(i, j) = R(j, i) = dot / nImage;
162  }
163  }
164  flux_bar /= nImage;
165  Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eVecValues(R);
166  Eigen::MatrixXd const& Q = eVecValues.eigenvectors();
167  Eigen::VectorXd const& lambda = eVecValues.eigenvalues();
168  //
169  // We need to sort the eigenValues, and remember the permutation we applied to the eigenImages
170  // We'll use the vector lambdaAndIndex to achieve this
171  //
172  std::vector<std::pair<double, int> > lambdaAndIndex; // pairs (eValue, index)
173  lambdaAndIndex.reserve(nImage);
174 
175  for (int i = 0; i != nImage; ++i) {
176  lambdaAndIndex.push_back(std::make_pair(lambda(i), i));
177  }
178  std::sort(lambdaAndIndex.begin(), lambdaAndIndex.end(), SortEvalueDecreasing<double>());
179  //
180  // Save the (sorted) eigen values
181  //
182  _eigenValues.clear();
183  _eigenValues.reserve(nImage);
184  for (int i = 0; i != nImage; ++i) {
185  _eigenValues.push_back(lambdaAndIndex[i].first);
186  }
187  //
188  // Contruct the first ncomp eigenimages in basis
189  //
190  int ncomp = 100; // number of components to keep
191 
192  _eigenImages.clear();
193  _eigenImages.reserve(ncomp < nImage ? ncomp : nImage);
194 
195  for (int i = 0; i < ncomp; ++i) {
196  if (i >= nImage) {
197  continue;
198  }
199 
200  int const ii = lambdaAndIndex[i].second; // the index after sorting (backwards) by eigenvalue
201 
202  std::shared_ptr<ImageT> eImage(new ImageT(_dimensions));
203  *eImage = static_cast<typename ImageT::Pixel>(0);
204 
205  for (int j = 0; j != nImage; ++j) {
206  int const jj = lambdaAndIndex[j].second; // the index after sorting (backwards) by eigenvalue
207  double const weight = Q(jj, ii) * (_constantWeight ? flux_bar / getFlux(jj) : 1);
208  eImage->scaledPlus(weight, *_imageList[jj]);
209  }
210  _eigenImages.push_back(eImage);
211  }
212 }
T begin(T... args)
T clear(T... args)
T end(T... args)
T make_pair(T... args)
def dot(symb, c, r, frame=None, size=2, ctype=None, origin=afwImage.PARENT, *args, **kwargs)
Definition: ds9.py:100
double innerProduct(Image1T const &lhs, Image2T const &rhs, int const border=0)
Calculate the inner product of two images.
Definition: ImagePca.cc:415
float Pixel
Typedefs to be used for pixel values.
Definition: common.h:37
T reserve(T... args)
T size(T... args)
T sort(T... args)
afw::table::Key< double > weight

◆ getDimensions()

template<typename ImageT >
lsst::geom::Extent2I const lsst::afw::image::ImagePca< ImageT >::getDimensions ( ) const
inline

Return the dimension of the images being analyzed.

Definition at line 76 of file ImagePca.h.

76 { return _dimensions; }

◆ getEigenImages()

template<typename ImageT >
ImageList const& lsst::afw::image::ImagePca< ImageT >::getEigenImages ( ) const
inline

Return Eigen images.

Definition at line 101 of file ImagePca.h.

101 { return _eigenImages; }

◆ getEigenValues()

template<typename ImageT >
std::vector<double> const& lsst::afw::image::ImagePca< ImageT >::getEigenValues ( ) const
inline

Return Eigen values.

Definition at line 99 of file ImagePca.h.

99 { return _eigenValues; }

◆ getImageList()

template<typename ImageT >
ImagePca< ImageT >::ImageList lsst::afw::image::ImagePca< ImageT >::getImageList

Return the list of images being analyzed.

Definition at line 86 of file ImagePca.cc.

86  {
87  return _imageList;
88 }

◆ getMean()

template<typename ImageT >
std::shared_ptr< ImageT > lsst::afw::image::ImagePca< ImageT >::getMean

Return the mean of the images in ImagePca's list.

Definition at line 91 of file ImagePca.cc.

91  {
92  if (_imageList.empty()) {
93  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError, "You haven't provided any images");
94  }
95 
96  std::shared_ptr<ImageT> mean(new ImageT(getDimensions()));
97  *mean = static_cast<typename ImageT::Pixel>(0);
98 
99  for (typename ImageList::const_iterator ptr = _imageList.begin(), end = _imageList.end(); ptr != end;
100  ++ptr) {
101  *mean += **ptr;
102  }
103  *mean /= _imageList.size();
104 
105  return mean;
106 }
int end
uint64_t * ptr
Definition: RangeSet.cc:88

◆ operator=() [1/2]

template<typename ImageT >
ImagePca< ImageT > & lsst::afw::image::ImagePca< ImageT >::operator= ( ImagePca< ImageT > &&  )
default

◆ operator=() [2/2]

template<typename ImageT >
ImagePca< ImageT > & lsst::afw::image::ImagePca< ImageT >::operator= ( ImagePca< ImageT > const &  )
default

◆ updateBadPixels()

template<typename ImageT >
double lsst::afw::image::ImagePca< ImageT >::updateBadPixels ( unsigned long  mask,
int const  ncomp 
)
virtual

Update the bad pixels (i.e.

those for which (value & mask) != 0) based on the current PCA decomposition; if none is available, use the mean of the good pixels

Parameters
maskMask defining bad pixels
ncompNumber of components to use in estimate
Returns
the maximum change made to any pixel

N.b. the work is actually done in do_updateBadPixels as the code only makes sense and compiles when we are doing a PCA on a set of MaskedImages

Definition at line 387 of file ImagePca.cc.

387  {
388  return do_updateBadPixels<ImageT>(typename ImageT::image_category(), _imageList, _fluxList, _eigenImages,
389  mask, ncomp);
390 }
afw::table::Key< afw::table::Array< MaskPixelT > > mask

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