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
KernelPca.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
12 #include "lsst/afw/math.h"
13 #include "lsst/afw/image.h"
15 #include "lsst/pex/logging/Trace.h"
16 
19 
20 namespace afwMath = lsst::afw::math;
21 namespace afwImage = lsst::afw::image;
22 namespace pexLogging = lsst::pex::logging;
23 namespace pexExcept = lsst::pex::exceptions;
24 
25 namespace lsst {
26 namespace ip {
27 namespace diffim {
28 namespace detail {
29 
56  template<typename PixelT>
58  boost::shared_ptr<KernelPca<ImageT> > imagePca
59  ) :
60  afwMath::CandidateVisitor(),
61  _imagePca(imagePca),
62  _mean()
63  {};
64 
65  template<typename PixelT>
67  afwMath::KernelList kernelList;
68 
69  std::vector<typename ImageT::Ptr> eigenImages = _imagePca->getEigenImages();
70  int ncomp = eigenImages.size();
71 
72  if (_mean) {
73  kernelList.push_back(afwMath::Kernel::Ptr(
76  (*_mean, true))));
77  }
78  for (int i = 0; i < ncomp; i++) {
80  afwImage::Image<afwMath::Kernel::Pixel>(*eigenImages[i], true);
81  kernelList.push_back(afwMath::Kernel::Ptr(
82  new afwMath::FixedKernel(img)
83  ));
84  }
85 
86  return kernelList;
87  }
88 
89  template<typename PixelT>
91 
92  KernelCandidate<PixelT> *kCandidate = dynamic_cast<KernelCandidate<PixelT> *>(candidate);
93  if (kCandidate == NULL) {
94  throw LSST_EXCEPT(pexExcept::LogicError,
95  "Failed to cast SpatialCellCandidate to KernelCandidate");
96  }
97  pexLogging::TTrace<6>("lsst.ip.diffim.SetPcaImageVisitor.processCandidate",
98  "Processing candidate %d", kCandidate->getId());
99 
100  try {
101  /* Normalize to unit sum */
102  PTR(ImageT) kImage = kCandidate->getKernelSolution(
103  KernelCandidate<PixelT>::ORIG)->makeKernelImage();
104  *kImage /= kCandidate->getKernelSolution(
105  KernelCandidate<PixelT>::ORIG)->getKsum();
106  /* Tell imagePca they have the same weighting in the Pca */
107  _imagePca->addImage(kImage, 1.0);
108  } catch(pexExcept::Exception &e) {
109  return;
110  }
111  }
112 
113  template<typename PixelT>
115  /*
116  If we don't subtract off the mean before we do the Pca, the
117  subsequent terms carry less of the power than if you do subtract
118  off the mean. Explicit example:
119 
120  With mean subtraction:
121  DEBUG: Eigenvalue 0 : 0.010953 (0.373870 %)
122  DEBUG: Eigenvalue 1 : 0.007927 (0.270604 %)
123  DEBUG: Eigenvalue 2 : 0.001393 (0.047542 %)
124  DEBUG: Eigenvalue 3 : 0.001092 (0.037261 %)
125  DEBUG: Eigenvalue 4 : 0.000829 (0.028283 %)
126 
127  Without mean subtraction:
128  DEBUG: Eigenvalue 0 : 0.168627 (0.876046 %)
129  DEBUG: Eigenvalue 1 : 0.007935 (0.041223 %)
130  DEBUG: Eigenvalue 2 : 0.006049 (0.031424 %)
131  DEBUG: Eigenvalue 3 : 0.001188 (0.006173 %)
132  DEBUG: Eigenvalue 4 : 0.001050 (0.005452 %)
133 
134  After the first term above, which basically represents the mean,
135  the remaining terms carry less of the power than if you do
136  subtract off the mean. (0.041223/(1-0.876046) < 0.373870).
137  */
138  pexLogging::TTrace<6>("lsst.ip.diffim.KernelPcaVisitor.subtractMean",
139  "Subtracting mean feature before Pca");
140 
141  _mean = _imagePca->getMean();
142  KernelPca<ImageT>::ImageList imageList = _imagePca->getImageList();
143  for (typename KernelPca<ImageT>::ImageList::const_iterator ptr = imageList.begin(),
144  end = imageList.end(); ptr != end; ++ptr) {
145  **ptr -= *_mean;
146  }
147  }
148 
163  template <typename ImageT>
165  {
166  Super::analyze();
167 
168  typename Super::ImageList const &eImageList = this->getEigenImages();
169  typename Super::ImageList::const_iterator iter = eImageList.begin(), end = eImageList.end();
170  for (size_t i = 0; iter != end; ++i, ++iter) {
171  PTR(ImageT) eImage = *iter;
172 
173  /*
174  * Normalise eigenImages to have a maximum of 1.0. For n > 0 they
175  * (should) have mean == 0, so we can't use that to normalize
176  */
178  double const min = stats.getValue(afwMath::MIN);
179  double const max = stats.getValue(afwMath::MAX);
180 
181  double const extreme = (fabs(min) > max) ? min :max;
182  if (extreme != 0.0) {
183  *eImage /= extreme;
184  }
185  }
186  }
187 
188 
189  typedef float PixelT;
190  template class KernelPcaVisitor<PixelT>;
192 
193 }}}} // end of namespace lsst::ip::diffim::detail
int iter
An include file to include the public header files for lsst::afw::math.
estimate sample minimum
Definition: Statistics.h:76
Class stored in SpatialCells for spatial Kernel fitting.
definition of the Trace messaging facilities
#define PTR(...)
Definition: base.h:41
estimate sample maximum
Definition: Statistics.h:77
A class to run a PCA on all candidate kernels (represented as Images).
Definition: KernelPca.h:40
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
Definition: KernelPca.cc:90
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
lsst::afw::math::KernelList getEigenKernels()
Definition: KernelPca.cc:66
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1009
An include file to include the header files for lsst::afw::image.
boost::shared_ptr< Kernel > Ptr
Definition: Kernel.h:141
int getId() const
Return the candidate&#39;s unique ID.
Definition: SpatialCell.h:109
Overrides the analyze method of base class afwImage::ImagePca.
Definition: KernelPca.h:24
Declaration of KernelPca and KernelPcaVisitor.
std::vector< typename ImageT::Ptr > ImageList
Definition: ImagePca.h:52
Class used by SpatialModelCell for spatial Kernel fitting.
boost::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
KernelPcaVisitor(boost::shared_ptr< KernelPca< ImageT > > imagePca)
Definition: KernelPca.cc:57
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:415
virtual void analyze()
Generate eigenimages that are normalised.
Definition: KernelPca.cc:164
PsfImagePca< MaskedImageT > * _imagePca
A kernel created from an Image.
Definition: Kernel.h:551
std::vector< boost::shared_ptr< Kernel > > KernelList
Definition: Kernel.h:542