25 #ifndef LSST_AFW_MATH_KERNEL_H
26 #define LSST_AFW_MATH_KERNEL_H
31 #include <type_traits>
35 #include "boost/mpl/or.hpp"
37 #include "boost/serialization/shared_ptr.hpp"
38 #include "boost/serialization/vector.hpp"
39 #include "boost/serialization/export.hpp"
41 #include "lsst/daf/base/Persistable.h"
42 #include "lsst/daf/base/Citizen.h"
54 namespace formatters {
55 class KernelFormatter;
60 using boost::serialization::make_nvp;
123 class Kernel :
public lsst::daf::base::Citizen,
124 public lsst::daf::base::Persistable,
156 explicit Kernel(
int width,
int height,
unsigned int nKernelParams,
170 explicit Kernel(
int width,
int height,
const std::vector<SpatialFunctionPtr> spatialFunctionList);
185 virtual std::shared_ptr<Kernel>
clone()
const = 0;
205 double y = 0.0)
const;
213 _width = dims.getX();
214 _height = dims.getY();
216 inline void setWidth(
int width) { _width = width; }
355 std::vector<std::vector<double>> spatialParams;
358 spatialParams.push_back((*spFuncIter)->getParameters());
360 return spatialParams;
376 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Kernel is spatially varying");
379 if (nParams != params.size()) {
380 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
381 (boost::format(
"Number of parameters is wrong, saw %d expected %d") % nParams %
385 for (
unsigned int ii = 0; ii < nParams; ++ii) {
422 virtual std::string
toString(std::string
const &prefix =
"")
const;
438 #if 0 // fails to compile with icc; is it actually used?
439 virtual void toFile(std::string fileName)
const;
442 struct PersistenceHelper;
491 unsigned int _nKernelParams;
495 Kernel &operator=(const Kernel &);
497 virtual
void _setKernelXY() {}
531 virtual std::shared_ptr<Kernel>
clone()
const;
533 virtual std::string
toString(std::string
const &prefix =
"")
const;
553 template <
class Archive>
554 void serialize(Archive &ar,
unsigned int const version) {
555 ar &make_nvp(
"k", boost::serialization::base_object<Kernel>(*
this));
556 ar &make_nvp(
"img", _image);
557 ar &make_nvp(
"sum", _sum);
613 std::vector<Kernel::SpatialFunctionPtr>
const &spatialFunctionList);
617 virtual std::shared_ptr<Kernel>
clone()
const;
640 double y = 0.0)
const;
649 virtual std::string
toString(std::string
const &prefix =
"")
const;
668 template <
class Archive>
670 ar &make_nvp(
"k", boost::serialization::base_object<Kernel>(*
this));
700 virtual std::shared_ptr<Kernel>
clone()
const;
704 virtual std::string
toString(std::string
const &prefix =
"")
const;
721 template <
class Archive>
722 void serialize(Archive &ar,
unsigned int const version) {
723 boost::serialization::void_cast_register<DeltaFunctionKernel, Kernel>(
757 std::vector<double>
const &kernelParameters);
779 std::vector<Kernel::SpatialFunctionPtr>
const &spatialFunctionList);
783 virtual std::shared_ptr<Kernel>
clone()
const;
848 std::shared_ptr<Kernel>
refactor()
const;
850 virtual std::string
toString(std::string
const &prefix =
"")
const;
869 void _setKernelList(
KernelList const &kernelList);
872 std::vector<std::shared_ptr<lsst::afw::image::Image<Pixel>>> _kernelImagePtrList;
874 std::vector<double> _kernelSumList;
875 mutable std::vector<double> _kernelParams;
876 bool _isDeltaFunctionBasis;
879 template <
class Archive>
880 void serialize(Archive &ar,
unsigned int const version) {
881 ar &make_nvp(
"k", boost::serialization::base_object<Kernel>(*
this));
882 ar &make_nvp(
"klist", _kernelList);
883 ar &make_nvp(
"kimglist", _kernelImagePtrList);
884 ar &make_nvp(
"ksumlist", _kernelSumList);
885 ar &make_nvp(
"params", _kernelParams);
887 ar &make_nvp(
"deltaBasis", _isDeltaFunctionBasis);
888 }
else if (Archive::is_loading::value) {
889 _isDeltaFunctionBasis =
false;
948 std::vector<Kernel::SpatialFunctionPtr>
const &spatialFunctionList);
951 virtual std::shared_ptr<Kernel>
clone()
const;
969 double computeVectors(std::vector<Pixel> &colList, std::vector<Pixel> &rowList,
bool doNormalize,
970 double x = 0.0,
double y = 0.0)
const;
973 unsigned int const ncol = _kernelColFunctionPtr->getNParameters();
975 return _kernelColFunctionPtr->getParameter(i);
978 return _kernelRowFunctionPtr->getParameter(i);
993 virtual std::string
toString(std::string
const &prefix =
"")
const;
1030 double basicComputeVectors(std::vector<Pixel> &colList, std::vector<Pixel> &rowList,
1031 bool doNormalize)
const;
1035 mutable std::vector<Pixel> _localColList;
1036 mutable std::vector<Pixel> _localRowList;
1037 mutable std::vector<double> _kernelX;
1038 mutable std::vector<double> _kernelY;
1042 mutable std::vector<std::vector<double>> _kernelRowCache;
1043 mutable std::vector<std::vector<double>> _kernelColCache;
1046 template <
class Archive>
1047 void serialize(Archive &ar,
unsigned int const version) {
1048 ar &make_nvp(
"k", boost::serialization::base_object<Kernel>(*
this));
1049 ar &make_nvp(
"colfn", _kernelColFunctionPtr);
1050 ar &make_nvp(
"rowfn", _kernelRowFunctionPtr);
1051 ar &make_nvp(
"cols", _localColList);
1052 ar &make_nvp(
"rows", _localRowList);
1053 ar &make_nvp(
"kernelX", _kernelX);
1054 ar &make_nvp(
"kernelY", _kernelY);
1057 virtual void _setKernelXY() {
1061 assert(dim[0] == static_cast<int>(_kernelX.size()));
1062 for (
int i = 0; i != dim.getX(); ++i) {
1063 _kernelX[i] = i - ctr.getX();
1066 assert(dim[1] == static_cast<int>(_kernelY.size()));
1067 for (
int i = 0; i != dim.getY(); ++i) {
1068 _kernelY[i] = i - ctr.getY();
1077 namespace serialization {
1079 template <
class Archive>
1081 unsigned int const file_version) {
1086 ar << make_nvp(
"width", width);
1087 ar << make_nvp(
"height", height);
1088 ar << make_nvp(
"pixX", x);
1089 ar << make_nvp(
"pixY", y);
1092 template <
class Archive>
1094 unsigned int const file_version) {
1099 ar >> make_nvp(
"width", width);
1100 ar >> make_nvp(
"height", height);
1101 ar >> make_nvp(
"pixX", x);
1102 ar >> make_nvp(
"pixY", y);
1110 #endif // !defined(LSST_AFW_MATH_KERNEL_H)
std::vector< double > getKernelSumList() const
Get the sum of the pixels of each fixed basis kernel.
virtual ~SeparableKernel()
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
virtual double getKernelParameter(unsigned int i) const
Return a particular Kernel Parameter (no bounds checking).
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
friend class boost::serialization::access
std::shared_ptr< KernelFunction > KernelFunctionPtr
virtual int getCacheSize() const
Get the current cache size (0 if none)
geom::Extent2I const getDimensions() const
Return the Kernel's dimensions (width, height)
double computeVectors(std::vector< Pixel > &colList, std::vector< Pixel > &rowList, bool doNormalize, double x=0.0, double y=0.0) const
Compute the column and row arrays in place, where kernel(col, row) = colList(col) * rowList(row) ...
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
void setDimensions(geom::Extent2I dims)
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
DeltaFunctionKernel(int width, int height, lsst::afw::geom::Point2I const &point)
Construct a spatially invariant DeltaFunctionKernel.
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
virtual KernelFunctionPtr getKernelFunction() const
Get a deep copy of the kernel function.
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
int getCtrY() const
Return y index of kernel's center.
lsst::afw::math::Function1< Pixel > KernelFunction
void setKernelParameters(std::vector< double > const ¶ms)
Set the kernel parameters of a spatially invariant kernel.
lsst::afw::geom::Box2I getBBox() const
return parent bounding box, with XY0 = -center
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
A kernel described by a pair of functions: func(x, y) = colFunc(x) * rowFunc(y)
int getNSpatialParameters() const
Return the number of spatial parameters (0 if not spatially varying)
void serialize(Archive &ar, unsigned int const version)
std::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
std::vector< std::vector< double > > getSpatialParameters() const
Return the spatial parameters parameters (an empty vector if not spatially varying) ...
void setHeight(int height)
lsst::afw::math::NullFunction2< double > NullSpatialFunction
Extent< int, 2 > Extent2I
int getCtrX() const
Return x index of kernel's center.
friend class boost::serialization::access
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
lsst::afw::math::Function2< double > SpatialFunction
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
A Function taking two arguments.
LinearCombinationKernel()
Construct an empty LinearCombinationKernel of size 0x0.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
void checkKernelList(const KernelList &kernelList) const
Check that all kernels have the same size and center and that none are spatially varying.
bool isDeltaFunctionBasis() const
Return true if all basis kernels are instances of DeltaFunctionKernel.
An integer coordinate rectangle.
int getHeight() const
Return the Kernel's height.
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
A base class for objects that can be persisted via afw::table::io Archive classes.
int getWidth() const
Return the Kernel's width.
a class used in function calls to indicate that no Function2 is being provided
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
void setCtrY(int ctrY)
Set y index of kernel's center.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
virtual double getKernelParameter(unsigned int i) const
Return a particular Kernel Parameter (no bounds checking).
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
KernelFunctionPtr _kernelFunctionPtr
friend class boost::serialization::access
int getNBasisKernels() const
Get the number of basis kernels.
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
FixedKernel()
Construct an empty FixedKernel of size 0x0.
unsigned int getNKernelParameters() const
Return the number of kernel parameters (0 if none)
void setCtrX(int ctrX)
Set x index of kernel's center.
double computeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize, double x=0.0, double y=0.0) const
Compute an image (pixellized representation of the kernel) in place.
double computeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize, double x=0.0, double y=0.0) const
Compute an image (pixellized representation of the kernel) in place.
A kernel that is a linear combination of fixed basis kernels.
void setKernelParameters(std::pair< double, double > const ¶ms)
Set the kernel parameters of a 2-component spatially invariant kernel.
void load_construct_data(Archive &ar, lsst::afw::math::DeltaFunctionKernel *k, unsigned int const file_version)
lsst::afw::geom::Box2I growBBox(lsst::afw::geom::Box2I const &bbox) const
Given a bounding box for pixels one wishes to compute by convolving an image with this kernel...
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
A Function taking one argument.
SpatialFunctionPtr getSpatialFunction(unsigned int index) const
Return a clone of the specified spatial function (one component of the spatial model) ...
SeparableKernel()
Construct an empty spatially invariant SeparableKernel of size 0x0.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
virtual int getCacheSize() const
Get the current size of the kernel cache (0 if none or if caches not supported)
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
generic_kernel_tag kernel_fill_factor
void setCtr(lsst::afw::geom::Point2I ctr)
Set index of kernel's center.
lsst::afw::geom::Point2I getCtr() const
Return index of kernel's center.
friend class boost::serialization::access
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
virtual KernelList const & getKernelList() const
Get the fixed basis kernels.
void setSpatialParameters(const std::vector< std::vector< double >> params)
Set the parameters of all spatial functions.
virtual void computeCache(int const cacheSize)
Compute a cache of Kernel values, if desired.
Kernel has only one non-zero pixel.
lsst::afw::geom::Point2I getPixel() const
table::Key< int > kernelFunction
std::vector< std::shared_ptr< Kernel > > KernelList
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
AnalyticKernel()
Construct an empty spatially invariant AnalyticKernel of size 0x0.
std::vector< SpatialFunctionPtr > _spatialFunctionList
lsst::afw::math::Function2< Pixel > KernelFunction
Kernel()
Construct a null Kernel of size 0,0.
virtual ~LinearCombinationKernel()
KernelFunctionPtr getKernelRowFunction() const
Get a deep copy of the row kernel function.
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const =0
Low-level version of computeImage.
io::OutputArchiveHandle OutputArchiveHandle
virtual Pixel getSum() const
virtual ~AnalyticKernel()
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
void computeKernelParametersFromSpatialModel(std::vector< double > &kernelParams, double x, double y) const
Compute the kernel parameters at a specified point.
KernelFunctionPtr getKernelColFunction() const
Get a deep copy of the col kernel function.
lsst::afw::geom::Box2I shrinkBBox(lsst::afw::geom::Box2I const &bbox) const
Given a bounding box for an image one wishes to convolve with this kernel, return the bounding box fo...
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
deltafunction_kernel_tag kernel_fill_factor
virtual ~DeltaFunctionKernel()
A kernel described by a function.
std::shared_ptr< lsst::afw::math::Function2< Pixel > > KernelFunctionPtr
friend class boost::serialization::access
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
A CRTP facade class for subclasses of Persistable.
Kernels are used for convolution with MaskedImages and (eventually) Images.
void save_construct_data(Archive &ar, lsst::afw::math::DeltaFunctionKernel const *k, unsigned int const file_version)
std::vector< SpatialFunctionPtr > getSpatialFunctionList() const
Return a list of clones of the spatial functions.
std::shared_ptr< Kernel > refactor() const
Refactor the kernel as a linear combination of N bases where N is the number of parameters for the sp...
A kernel that has only one non-zero pixel (of value 1)
virtual std::shared_ptr< Kernel > clone() const =0
Return a pointer to a deep copy of this kernel.
virtual void computeCache(int const )
Compute a cache of Kernel values, if desired.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Tags carrying information about Kernels Kernel with no special properties.
A kernel created from an Image.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)