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,
157 SpatialFunction
const &spatialFunction = NullSpatialFunction());
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; }
277 return this->isSpatiallyVarying() ? _spatialFunctionList[0]->getNParameters() : 0;
290 SpatialFunctionPtr getSpatialFunction(
unsigned int index)
const;
298 std::vector<SpatialFunctionPtr> getSpatialFunctionList()
const;
311 virtual std::vector<double> getKernelParameters()
const;
367 std::vector<std::vector<double>> spatialParams;
368 std::vector<SpatialFunctionPtr>::const_iterator spFuncIter = _spatialFunctionList.begin();
369 for (; spFuncIter != _spatialFunctionList.end(); ++spFuncIter) {
370 spatialParams.push_back((*spFuncIter)->getParameters());
372 return spatialParams;
387 if (this->isSpatiallyVarying()) {
388 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Kernel is spatially varying");
390 const unsigned int nParams = this->getNKernelParameters();
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) {
398 this->setKernelParameter(ii, params[ii]);
409 this->setKernelParameter(0, params.first);
410 this->setKernelParameter(1, params.second);
421 void setSpatialParameters(
const std::vector<std::vector<double>> params);
429 void computeKernelParametersFromSpatialModel(std::vector<double> &kernelParams,
double x,
double y)
const;
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;
457 virtual std::string getPythonModule()
const;
469 virtual void setKernelParameter(
unsigned int ind,
double value)
const;
479 void setKernelParametersFromSpatialModel(
double x,
double y)
const;
503 unsigned int _nKernelParams;
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;
549 virtual Pixel
getSum()
const {
return _sum; }
558 virtual std::string getPersistenceName()
const;
566 friend class boost::serialization::access;
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);
626 explicit AnalyticKernel(
int width,
int height, KernelFunction
const &kernelFunction,
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;
658 virtual std::vector<double> getKernelParameters()
const;
663 virtual KernelFunctionPtr getKernelFunction()
const;
665 virtual std::string toString(std::string
const &prefix =
"")
const;
674 virtual std::string getPersistenceName()
const;
679 virtual void setKernelParameter(
unsigned int ind,
double value)
const;
683 friend class boost::serialization::access;
684 template <
class Archive>
685 void serialize(Archive &ar,
unsigned int const version) {
686 ar &make_nvp(
"k", boost::serialization::base_object<Kernel>(*
this));
687 ar &make_nvp(
"fn", _kernelFunctionPtr);
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;
731 virtual std::string getPersistenceName()
const;
738 friend class boost::serialization::access;
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;
805 virtual std::vector<double> getKernelParameters()
const;
810 virtual KernelList
const &getKernelList()
const;
815 std::vector<double> getKernelSumList()
const;
827 void checkKernelList(
const KernelList &kernelList)
const;
868 std::shared_ptr<Kernel> refactor()
const;
870 virtual std::string toString(std::string
const &prefix =
"")
const;
879 virtual std::string getPersistenceName()
const;
883 virtual void setKernelParameter(
unsigned int ind,
double value)
const;
889 void _setKernelList(KernelList
const &kernelList);
891 KernelList _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;
898 friend class boost::serialization::access;
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;
949 explicit SeparableKernel(
int width,
int height, KernelFunction
const &kernelColFunction,
950 KernelFunction
const &kernelRowFunction,
966 explicit SeparableKernel(
int width,
int height, KernelFunction
const &kernelColFunction,
967 KernelFunction
const &kernelRowFunction,
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);
1003 virtual std::vector<double> getKernelParameters()
const;
1008 KernelFunctionPtr getKernelColFunction()
const;
1013 KernelFunctionPtr getKernelRowFunction()
const;
1015 virtual std::string toString(std::string
const &prefix =
"")
const;
1026 virtual void computeCache(
int const cacheSize);
1031 virtual int getCacheSize()
const;
1036 virtual void setKernelParameter(
unsigned int ind,
double value)
const;
1052 double basicComputeVectors(std::vector<Pixel> &colList, std::vector<Pixel> &rowList,
1053 bool doNormalize)
const;
1055 KernelFunctionPtr _kernelColFunctionPtr;
1056 KernelFunctionPtr _kernelRowFunctionPtr;
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;
1067 friend class boost::serialization::access;
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) virtual ~SeparableKernel()
virtual int getCacheSize() const
Get the current size of the kernel cache (0 if none or if caches not supported)
int getHeight() const
Return the Kernel's height.
int getCtrX() const
Return x index of kernel's center.
int getCtrY() const
Return y index of kernel's center.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
std::shared_ptr< KernelFunction > KernelFunctionPtr
virtual Pixel getSum() const
void setDimensions(geom::Extent2I dims)
virtual void computeCache(int const)
Compute a cache of Kernel values, if desired.
An object passed to Persistable::write to allow it to persist itself.
lsst::afw::geom::Box2I getBBox() const
return parent bounding box, with XY0 = -center
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
lsst::afw::math::Function1< Pixel > KernelFunction
virtual double getKernelParameter(unsigned int i) const
Return a particular Kernel Parameter (no bounds checking).
void setKernelParameters(std::vector< double > const ¶ms)
Set the kernel parameters of a spatially invariant kernel.
lsst::afw::geom::Point2I getCtr() const
Return index of kernel's center.
virtual double getKernelParameter(unsigned int i) const
Return a particular Kernel Parameter (no bounds checking).
A kernel described by a pair of functions: func(x, y) = colFunc(x) * rowFunc(y)
void serialize(Archive &ar, unsigned int const version)
std::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
void setHeight(int height)
bool isDeltaFunctionBasis() const
Return true if all basis kernels are instances of DeltaFunctionKernel.
lsst::afw::math::NullFunction2< double > NullSpatialFunction
Extent< int, 2 > Extent2I
lsst::afw::math::Function2< double > SpatialFunction
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
A Function taking two arguments.
int getNBasisKernels() const
Get the number of basis kernels.
An integer coordinate rectangle.
A base class for objects that can be persisted via afw::table::io Archive classes.
a class used in function calls to indicate that no Function2 is being provided
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
void setCtrY(int ctrY)
Set y index of kernel's center.
KernelFunctionPtr _kernelFunctionPtr
A base class for image defects.
void setCtrX(int ctrX)
Set x index of kernel's center.
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)
A Function taking one argument.
generic_kernel_tag kernel_fill_factor
void setCtr(lsst::afw::geom::Point2I ctr)
Set index of kernel's center.
lsst::afw::geom::Point2I getPixel() const
int getWidth() const
Return the Kernel's width.
unsigned int getNKernelParameters() const
Return the number of kernel parameters (0 if none)
Kernel has only one non-zero pixel.
table::Key< int > kernelFunction
std::vector< std::shared_ptr< Kernel > > KernelList
std::vector< SpatialFunctionPtr > _spatialFunctionList
lsst::afw::math::Function2< Pixel > KernelFunction
virtual ~LinearCombinationKernel()
int getNSpatialParameters() const
Return the number of spatial parameters (0 if not spatially varying)
virtual ~AnalyticKernel()
std::vector< std::vector< double > > getSpatialParameters() const
Return the spatial parameters parameters (an empty vector if not spatially varying) ...
deltafunction_kernel_tag kernel_fill_factor
virtual ~DeltaFunctionKernel()
A kernel described by a function.
std::shared_ptr< lsst::afw::math::Function2< Pixel > > KernelFunctionPtr
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)
geom::Extent2I const getDimensions() const
Return the Kernel's dimensions (width, height)
A kernel that has only one non-zero pixel (of value 1)
Tags carrying information about Kernels Kernel with no special properties.
A kernel created from an Image.