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;
197 virtual std::shared_ptr<Kernel>
resized(
int width,
int height)
const = 0;
217 double y = 0.0)
const;
225 _width = dims.getX();
226 _height = dims.getY();
228 inline void setWidth(
int width) { _width = width; }
367 std::vector<std::vector<double>> spatialParams;
370 spatialParams.push_back((*spFuncIter)->getParameters());
372 return spatialParams;
388 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Kernel is spatially varying");
391 if (nParams != params.size()) {
392 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
393 (boost::format(
"Number of parameters is wrong, saw %d expected %d") % nParams %
397 for (
unsigned int ii = 0; ii < nParams; ++ii) {
434 virtual std::string
toString(std::string
const &prefix =
"")
const;
450 #if 0 // fails to compile with icc; is it actually used?
451 virtual void toFile(std::string fileName)
const;
454 struct PersistenceHelper;
503 unsigned int _nKernelParams;
507 Kernel &operator=(const Kernel &);
509 virtual
void _setKernelXY() {}
543 virtual std::shared_ptr<Kernel>
clone()
const;
545 virtual std::shared_ptr<Kernel>
resized(
int width,
int height)
const;
547 virtual std::string
toString(std::string
const &prefix =
"")
const;
567 template <
class Archive>
568 void serialize(Archive &ar,
unsigned int const version) {
569 ar &make_nvp(
"k", boost::serialization::base_object<Kernel>(*
this));
570 ar &make_nvp(
"img", _image);
571 ar &make_nvp(
"sum", _sum);
627 std::vector<Kernel::SpatialFunctionPtr>
const &spatialFunctionList);
631 virtual std::shared_ptr<Kernel>
clone()
const;
633 virtual std::shared_ptr<Kernel>
resized(
int width,
int height)
const;
656 double y = 0.0)
const;
665 virtual std::string
toString(std::string
const &prefix =
"")
const;
684 template <
class Archive>
686 ar &make_nvp(
"k", boost::serialization::base_object<Kernel>(*
this));
716 virtual std::shared_ptr<Kernel>
clone()
const;
718 virtual std::shared_ptr<Kernel>
resized(
int width,
int height)
const;
722 virtual std::string
toString(std::string
const &prefix =
"")
const;
739 template <
class Archive>
740 void serialize(Archive &ar,
unsigned int const version) {
741 boost::serialization::void_cast_register<DeltaFunctionKernel, Kernel>(
775 std::vector<double>
const &kernelParameters);
797 std::vector<Kernel::SpatialFunctionPtr>
const &spatialFunctionList);
801 virtual std::shared_ptr<Kernel>
clone()
const;
803 virtual std::shared_ptr<Kernel>
resized(
int width,
int height)
const;
868 std::shared_ptr<Kernel>
refactor()
const;
870 virtual std::string
toString(std::string
const &prefix =
"")
const;
889 void _setKernelList(
KernelList const &kernelList);
892 std::vector<std::shared_ptr<lsst::afw::image::Image<Pixel>>> _kernelImagePtrList;
894 std::vector<double> _kernelSumList;
895 mutable std::vector<double> _kernelParams;
896 bool _isDeltaFunctionBasis;
899 template <
class Archive>
900 void serialize(Archive &ar,
unsigned int const version) {
901 ar &make_nvp(
"k", boost::serialization::base_object<Kernel>(*
this));
902 ar &make_nvp(
"klist", _kernelList);
903 ar &make_nvp(
"kimglist", _kernelImagePtrList);
904 ar &make_nvp(
"ksumlist", _kernelSumList);
905 ar &make_nvp(
"params", _kernelParams);
907 ar &make_nvp(
"deltaBasis", _isDeltaFunctionBasis);
908 }
else if (Archive::is_loading::value) {
909 _isDeltaFunctionBasis =
false;
968 std::vector<Kernel::SpatialFunctionPtr>
const &spatialFunctionList);
971 virtual std::shared_ptr<Kernel>
clone()
const;
973 virtual std::shared_ptr<Kernel>
resized(
int width,
int height)
const;
991 double computeVectors(std::vector<Pixel> &colList, std::vector<Pixel> &rowList,
bool doNormalize,
992 double x = 0.0,
double y = 0.0)
const;
995 unsigned int const ncol = _kernelColFunctionPtr->getNParameters();
997 return _kernelColFunctionPtr->getParameter(i);
1000 return _kernelRowFunctionPtr->getParameter(i);
1015 virtual std::string
toString(std::string
const &prefix =
"")
const;
1052 double basicComputeVectors(std::vector<Pixel> &colList, std::vector<Pixel> &rowList,
1053 bool doNormalize)
const;
1057 mutable std::vector<Pixel> _localColList;
1058 mutable std::vector<Pixel> _localRowList;
1059 mutable std::vector<double> _kernelX;
1060 mutable std::vector<double> _kernelY;
1064 mutable std::vector<std::vector<double>> _kernelRowCache;
1065 mutable std::vector<std::vector<double>> _kernelColCache;
1068 template <
class Archive>
1069 void serialize(Archive &ar,
unsigned int const version) {
1070 ar &make_nvp(
"k", boost::serialization::base_object<Kernel>(*
this));
1071 ar &make_nvp(
"colfn", _kernelColFunctionPtr);
1072 ar &make_nvp(
"rowfn", _kernelRowFunctionPtr);
1073 ar &make_nvp(
"cols", _localColList);
1074 ar &make_nvp(
"rows", _localRowList);
1075 ar &make_nvp(
"kernelX", _kernelX);
1076 ar &make_nvp(
"kernelY", _kernelY);
1079 virtual void _setKernelXY() {
1083 assert(dim[0] == static_cast<int>(_kernelX.size()));
1084 for (
int i = 0; i != dim.getX(); ++i) {
1085 _kernelX[i] = i - ctr.getX();
1088 assert(dim[1] == static_cast<int>(_kernelY.size()));
1089 for (
int i = 0; i != dim.getY(); ++i) {
1090 _kernelY[i] = i - ctr.getY();
1099 namespace serialization {
1101 template <
class Archive>
1103 unsigned int const file_version) {
1108 ar << make_nvp(
"width", width);
1109 ar << make_nvp(
"height", height);
1110 ar << make_nvp(
"pixX", x);
1111 ar << make_nvp(
"pixY", y);
1114 template <
class Archive>
1116 unsigned int const file_version) {
1121 ar >> make_nvp(
"width", width);
1122 ar >> make_nvp(
"height", height);
1123 ar >> make_nvp(
"pixX", x);
1124 ar >> make_nvp(
"pixY", y);
1132 #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 > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
virtual std::shared_ptr< Kernel > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
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::shared_ptr< Kernel > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
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.
virtual std::shared_ptr< Kernel > resized(int width, int height) const =0
Return a pointer to a clone with specified kernel dimensions.
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 std::shared_ptr< Kernel > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
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::shared_ptr< Kernel > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
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)