lsst.ip.diffim  13.0-28-gf4bc96c
KernelCandidate.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
12 #ifndef LSST_IP_DIFFIM_KERNELCANDIDATE_H
13 #define LSST_IP_DIFFIM_KERNELCANDIDATE_H
14 
15 #include <memory>
16 #include "Eigen/Core"
17 
18 #include "lsst/afw/math.h"
19 #include "lsst/afw/image.h"
21 #include "lsst/afw/table/Source.h"
22 
23 namespace lsst {
24 namespace ip {
25 namespace diffim {
26 
27 
37  template <typename _PixelT>
38  class KernelCandidate : public afw::math::SpatialCellImageCandidate {
39  public:
40  typedef afw::image::Image<afw::math::Kernel::Pixel> ImageT;
41  typedef _PixelT PixelT; // _after_ afw::math::Kernel::Pixel
42  public:
43  typedef std::shared_ptr<KernelCandidate> Ptr;
44  typedef std::shared_ptr<afw::image::MaskedImage<PixelT> > MaskedImagePtr;
45  typedef std::shared_ptr<afw::image::Image<afw::image::VariancePixel> > VariancePtr;
46  typedef std::shared_ptr<afw::table::SourceRecord> SourcePtr;
47 
49  ORIG = 0,
50  PCA = 1,
51  RECENT = 2
52  };
53 
63  KernelCandidate(float const xCenter,
64  float const yCenter,
65  MaskedImagePtr const& templateMaskedImage,
66  MaskedImagePtr const& scienceMaskedImage,
67  pex::policy::Policy const& policy);
68 
78  KernelCandidate(SourcePtr const& source,
79  MaskedImagePtr const& templateMaskedImage,
80  MaskedImagePtr const& scienceMaskedImage,
81  pex::policy::Policy const& policy);
83  virtual ~KernelCandidate() {};
84 
90  double getCandidateRating() const { return _coreFlux; }
94  SourcePtr getSource() const { return _source; }
98  MaskedImagePtr getTemplateMaskedImage() {return _templateMaskedImage;}
99  MaskedImagePtr getScienceMaskedImage() {return _scienceMaskedImage;}
100 
105  std::shared_ptr<afw::math::Kernel> getKernel(CandidateSwitch cand) const;
106  double getBackground(CandidateSwitch cand) const;
107  double getKsum(CandidateSwitch cand) const;
108  PTR(ImageT) getKernelImage(CandidateSwitch cand) const;
109  CONST_PTR(ImageT) getImage() const; // For SpatialCellImageCandidate
110  std::shared_ptr<StaticKernelSolution<PixelT> > getKernelSolution(CandidateSwitch cand) const;
111 
115  afw::image::MaskedImage<PixelT> getDifferenceImage(CandidateSwitch cand);
116 
122  afw::image::MaskedImage<PixelT> getDifferenceImage(
123  std::shared_ptr<afw::math::Kernel> kernel,
124  double background
125  );
126 
127  bool isInitialized() const {return _isInitialized;}
128 
129 
164  /*
165  * @note This method uses an estimate of the variance which is the
166  * straight difference of the 2 images. If requested in the Policy
167  * ("iterateSingleKernel"), the kernel will be rebuilt using the
168  * variance of the difference image resulting from this first
169  * approximate step. This is particularly useful when convolving a
170  * single-depth science image; the variance (and thus resulting kernel)
171  * generally converges after 1 iteration. If
172  * "constantVarianceWeighting" is requested in the Policy, no iterations
173  * will be performed even if requested.
174  */
175 
176  void build(
177  afw::math::KernelList const& basisList
178  );
179  void build(
180  afw::math::KernelList const& basisList,
181  Eigen::MatrixXd const& hMat
182  );
183 
184  private:
185  MaskedImagePtr _templateMaskedImage;
186  MaskedImagePtr _scienceMaskedImage;
187  VariancePtr _varianceEstimate;
188  pex::policy::Policy _policy;
189  SourcePtr _source;
190  double _coreFlux;
194 
195  /* best single raw kernel */
196  std::shared_ptr<StaticKernelSolution<PixelT> > _kernelSolutionOrig;
197 
198  /* with Pca basis */
199  std::shared_ptr<StaticKernelSolution<PixelT> > _kernelSolutionPca;
200 
201  void _buildKernelSolution(afw::math::KernelList const& basisList,
202  Eigen::MatrixXd const& hMat);
203  };
204 
205 
217  template <typename PixelT>
218  std::shared_ptr<KernelCandidate<PixelT> >
219  makeKernelCandidate(float const xCenter,
220  float const yCenter,
221  std::shared_ptr<afw::image::MaskedImage<PixelT> > const& templateMaskedImage,
222  std::shared_ptr<afw::image::MaskedImage<PixelT> > const& scienceMaskedImage,
223  pex::policy::Policy const& policy){
224 
225  return std::shared_ptr<KernelCandidate<PixelT>>(new KernelCandidate<PixelT>(xCenter, yCenter,
226  templateMaskedImage,
227  scienceMaskedImage,
228  policy));
229  }
230 
242  template <typename PixelT>
243  std::shared_ptr<KernelCandidate<PixelT> >
244  makeKernelCandidate(std::shared_ptr<afw::table::SourceRecord> const & source,
245  std::shared_ptr<afw::image::MaskedImage<PixelT> > const& templateMaskedImage,
246  std::shared_ptr<afw::image::MaskedImage<PixelT> > const& scienceMaskedImage,
247  pex::policy::Policy const& policy){
248 
249  return std::shared_ptr<KernelCandidate<PixelT>>(new KernelCandidate<PixelT>(source,
250  templateMaskedImage,
251  scienceMaskedImage,
252  policy));
253  }
254 
255 
256 }}} // end of namespace lsst::ip::diffim
257 
258 #endif
bool _isInitialized
Has the kernel been built.
double getBackground(CandidateSwitch cand) const
MaskedImagePtr _scienceMaskedImage
Subimage around which you build kernel.
Class stored in SpatialCells for spatial Kernel fitting.
double getCandidateRating() const
Return Candidate rating.
void build(afw::math::KernelList const &basisList)
Core functionality of KernelCandidate, to build and fill a KernelSolution.
std::shared_ptr< StaticKernelSolution< PixelT > > _kernelSolutionOrig
Original basis solution.
afw::image::MaskedImage< PixelT > getDifferenceImage(CandidateSwitch cand)
Calculate associated difference image using internal solutions.
std::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
std::shared_ptr< afw::math::Kernel > getKernel(CandidateSwitch cand) const
Return results of kernel solution.
boost::shared_ptr< ImageT > getKernelImage(CandidateSwitch cand) const
Declaration of classes to store the solution for convolution kernels.
std::shared_ptr< afw::image::Image< afw::image::VariancePixel > > VariancePtr
std::shared_ptr< KernelCandidate > Ptr
pex::policy::Policy _policy
Policy.
MaskedImagePtr getTemplateMaskedImage()
Return pointers to the image pixels used in kernel determination.
afw::image::Image< afw::math::Kernel::Pixel > ImageT
virtual ~KernelCandidate()
Destructor.
std::shared_ptr< afw::image::MaskedImage< PixelT > > MaskedImagePtr
VariancePtr _varianceEstimate
Estimate of the local variance.
SourcePtr getSource() const
Return the original source.
double _coreFlux
Mean S/N in the science image.
boost::shared_ptr< ImageT const > getImage() const
MaskedImagePtr _templateMaskedImage
Subimage around which you build kernel.
bool _useRegularization
Use regularization?
void _buildKernelSolution(afw::math::KernelList const &basisList, Eigen::MatrixXd const &hMat)
std::shared_ptr< afw::table::SourceRecord > SourcePtr
KernelCandidate(float const xCenter, float const yCenter, MaskedImagePtr const &templateMaskedImage, MaskedImagePtr const &scienceMaskedImage, pex::policy::Policy const &policy)
Constructor.
double getKsum(CandidateSwitch cand) const
std::shared_ptr< StaticKernelSolution< PixelT > > _kernelSolutionPca
Most recent solution.
std::shared_ptr< KernelCandidate< PixelT > > makeKernelCandidate(float const xCenter, float const yCenter, std::shared_ptr< afw::image::MaskedImage< PixelT > > const &templateMaskedImage, std::shared_ptr< afw::image::MaskedImage< PixelT > > const &scienceMaskedImage, pex::policy::Policy const &policy)
Return a KernelCandidate pointer of the right sort.