LSSTApplications  21.0.0+75b29a8a7f,21.0.0+e70536a077,21.0.0-1-ga51b5d4+62c747d40b,21.0.0-11-ga6ea59e8e+47cba9fc36,21.0.0-2-g103fe59+914993bf7c,21.0.0-2-g1367e85+e2614ded12,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g4bc9b9f+7b2b5f8678,21.0.0-2-g5242d73+e2614ded12,21.0.0-2-g54e2caa+6403186824,21.0.0-2-g7f82c8f+3ac4acbffc,21.0.0-2-g8dde007+04a6aea1af,21.0.0-2-g8f08a60+9402881886,21.0.0-2-ga326454+3ac4acbffc,21.0.0-2-ga63a54e+81dd751046,21.0.0-2-gc738bc1+5f65c6e7a9,21.0.0-2-gde069b7+26c92b3210,21.0.0-2-gecfae73+0993ddc9bd,21.0.0-2-gfc62afb+e2614ded12,21.0.0-21-gba890a8+5a4f502a26,21.0.0-23-g9966ff26+03098d1af8,21.0.0-3-g357aad2+8ad216c477,21.0.0-3-g4be5c26+e2614ded12,21.0.0-3-g6d51c4a+4d2fe0280d,21.0.0-3-g7d9da8d+75b29a8a7f,21.0.0-3-gaa929c8+522e0f12c2,21.0.0-3-ge02ed75+4d2fe0280d,21.0.0-4-g3300ddd+e70536a077,21.0.0-4-gc004bbf+eac6615e82,21.0.0-4-gccdca77+f94adcd104,21.0.0-4-gd1c1571+18b81799f9,21.0.0-5-g7b47fff+4d2fe0280d,21.0.0-5-gb155db7+d2632f662b,21.0.0-5-gdf36809+637e4641ee,21.0.0-6-g722ad07+28c848f42a,21.0.0-7-g959bb79+522e0f12c2,21.0.0-7-gfd72ab2+cf01990774,21.0.0-9-g87fb7b8d+e2ab11cdd6,w.2021.04
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"
14 #include "lsst/log/Log.h"
16 
19 
20 namespace afwMath = lsst::afw::math;
21 namespace afwImage = lsst::afw::image;
23 
24 namespace lsst {
25 namespace ip {
26 namespace diffim {
27 namespace detail {
28 
55  template<typename PixelT>
58  ) :
59  afwMath::CandidateVisitor(),
60  _imagePca(imagePca),
61  _mean()
62  {};
63 
64  template<typename PixelT>
66  afwMath::KernelList kernelList;
67 
68  std::vector<std::shared_ptr<ImageT>> eigenImages = _imagePca->getEigenImages();
69  int ncomp = eigenImages.size();
70 
71  if (_mean) {
75  (*_mean, true))));
76  }
77  for (int i = 0; i < ncomp; i++) {
79  afwImage::Image<afwMath::Kernel::Pixel>(*eigenImages[i], true);
81  new afwMath::FixedKernel(img)
82  ));
83  }
84 
85  return kernelList;
86  }
87 
88  template<typename PixelT>
90 
91  KernelCandidate<PixelT> *kCandidate = dynamic_cast<KernelCandidate<PixelT> *>(candidate);
92  if (kCandidate == NULL) {
94  "Failed to cast SpatialCellCandidate to KernelCandidate");
95  }
96  LOGL_DEBUG("TRACE5.ip.diffim.SetPcaImageVisitor.processCandidate",
97  "Processing candidate %d", kCandidate->getId());
98 
99  try {
100  /* Normalize to unit sum */
101  PTR(ImageT) kImage = kCandidate->getKernelSolution(
102  KernelCandidate<PixelT>::ORIG)->makeKernelImage();
103  *kImage /= kCandidate->getKernelSolution(
104  KernelCandidate<PixelT>::ORIG)->getKsum();
105  /* Tell imagePca they have the same weighting in the Pca */
106  _imagePca->addImage(kImage, 1.0);
107  } catch(pexExcept::Exception &e) {
108  return;
109  }
110  }
111 
112  template<typename PixelT>
114  /*
115  If we don't subtract off the mean before we do the Pca, the
116  subsequent terms carry less of the power than if you do subtract
117  off the mean. Explicit example:
118 
119  With mean subtraction:
120  DEBUG: Eigenvalue 0 : 0.010953 (0.373870 %)
121  DEBUG: Eigenvalue 1 : 0.007927 (0.270604 %)
122  DEBUG: Eigenvalue 2 : 0.001393 (0.047542 %)
123  DEBUG: Eigenvalue 3 : 0.001092 (0.037261 %)
124  DEBUG: Eigenvalue 4 : 0.000829 (0.028283 %)
125 
126  Without mean subtraction:
127  DEBUG: Eigenvalue 0 : 0.168627 (0.876046 %)
128  DEBUG: Eigenvalue 1 : 0.007935 (0.041223 %)
129  DEBUG: Eigenvalue 2 : 0.006049 (0.031424 %)
130  DEBUG: Eigenvalue 3 : 0.001188 (0.006173 %)
131  DEBUG: Eigenvalue 4 : 0.001050 (0.005452 %)
132 
133  After the first term above, which basically represents the mean,
134  the remaining terms carry less of the power than if you do
135  subtract off the mean. (0.041223/(1-0.876046) < 0.373870).
136  */
137  LOGL_DEBUG("TRACE5.ip.diffim.KernelPcaVisitor.subtractMean",
138  "Subtracting mean feature before Pca");
139 
140  _mean = _imagePca->getMean();
141  KernelPca<ImageT>::ImageList imageList = _imagePca->getImageList();
142  for (typename KernelPca<ImageT>::ImageList::const_iterator ptr = imageList.begin(),
143  end = imageList.end(); ptr != end; ++ptr) {
144  **ptr -= *_mean;
145  }
146  }
147 
162  template <typename ImageT>
164  {
165  Super::analyze();
166 
167  typename Super::ImageList const &eImageList = this->getEigenImages();
168  typename Super::ImageList::const_iterator iter = eImageList.begin(), end = eImageList.end();
169  for (size_t i = 0; iter != end; ++i, ++iter) {
170  PTR(ImageT) eImage = *iter;
171 
172  /*
173  * Normalise eigenImages to have a maximum of 1.0. For n > 0 they
174  * (should) have mean == 0, so we can't use that to normalize
175  */
177  double const min = stats.getValue(afwMath::MIN);
178  double const max = stats.getValue(afwMath::MAX);
179 
180  double const extreme = (fabs(min) > max) ? min :max;
181  if (extreme != 0.0) {
182  *eImage /= extreme;
183  }
184  }
185  }
186 
187 
188  typedef float PixelT;
189  template class KernelPcaVisitor<PixelT>;
191 
192 }}}} // end of namespace lsst::ip::diffim::detail
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::afw::math::SpatialCellCandidate
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
std::shared_ptr
STL class.
lsst::ip::diffim::detail::KernelPca
Overrides the analyze method of base class afwImage::ImagePca.
Definition: KernelPca.h:24
std::vector< std::shared_ptr< Kernel > >
std::vector::size
T size(T... args)
lsst::ip::diffim::KernelCandidate
Class stored in SpatialCells for spatial Kernel fitting.
Definition: KernelCandidate.h:39
lsst::ip::diffim::detail::KernelPca::analyze
virtual void analyze()
Generate eigenimages that are normalised.
Definition: KernelPca.cc:163
lsst::afw::math::FixedKernel
A kernel created from an Image.
Definition: Kernel.h:472
lsst::ip::diffim::detail::PixelT
float PixelT
Definition: AssessSpatialKernelVisitor.cc:208
lsst::ip::diffim::detail::KernelPcaVisitor::processCandidate
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
Definition: KernelPca.cc:89
lsst::afw::math::Statistics::getValue
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1056
end
int end
Definition: BoundedField.cc:105
std::vector::push_back
T push_back(T... args)
lsst::afw::math::makeStatistics
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:354
math.h
lsst::ip::diffim::detail::KernelPcaVisitor
A class to run a PCA on all candidate kernels (represented as Images).
Definition: KernelPca.h:40
lsst::ip::diffim::detail::KernelPcaVisitor::KernelPcaVisitor
KernelPcaVisitor(std::shared_ptr< KernelPca< ImageT > > imagePca)
Definition: KernelPca.cc:56
image
afw::table::Key< afw::table::Array< ImagePixelT > > image
Definition: HeavyFootprint.cc:216
lsst.pex::exceptions::LogicError
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
lsst::ip::diffim::detail::KernelPcaVisitor::getEigenKernels
lsst::afw::math::KernelList getEigenKernels()
Definition: KernelPca.cc:65
lsst::afw::math::SpatialCellCandidate::getId
int getId() const
Return the candidate's unique ID.
Definition: SpatialCell.h:104
ptr
uint64_t * ptr
Definition: RangeSet.cc:88
max
int max
Definition: BoundedField.cc:104
image.h
PTR
#define PTR(...)
Definition: base.h:41
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::ip::diffim::KernelCandidate::getKernelSolution
std::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
Definition: KernelCandidate.cc:322
Runtime.h
KernelPca.h
Declaration of KernelPca and KernelPcaVisitor.
lsst::ip::diffim::detail::KernelPcaVisitor::subtractMean
void subtractMean()
Definition: KernelPca.cc:113
LOGL_DEBUG
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:504
std::vector::begin
T begin(T... args)
lsst::afw::math::MIN
@ MIN
estimate sample minimum
Definition: Statistics.h:76
min
int min
Definition: BoundedField.cc:103
lsst::afw::math
Definition: statistics.dox:6
KernelCandidate.h
Class used by SpatialModelCell for spatial Kernel fitting.
lsst::afw::math::Statistics
A class to evaluate image statistics.
Definition: Statistics.h:215
lsst::afw::math::MAX
@ MAX
estimate sample maximum
Definition: Statistics.h:77
lsst.pex::exceptions
Definition: Exception.h:37
lsst.pex::exceptions::Exception
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
std::vector::end
T end(T... args)
lsst::afw::image::Image
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:58
astshim.fitsChanContinued.iter
def iter(self)
Definition: fitsChanContinued.py:88
Log.h
LSST DM logging module built on log4cxx.