27 #include "boost/format.hpp" 30 #pragma warning(disable : 444) 32 #include "boost/archive/text_oarchive.hpp" 37 #include "lsst/pex/exceptions.h" 40 namespace pexExcept = lsst::pex::exceptions;
54 : daf::base::Citizen(typeid(this)),
55 _spatialFunctionList(),
63 : daf::base::Citizen(typeid(this)),
67 _ctrX((width - 1) / 2),
68 _ctrY((height - 1) / 2),
69 _nKernelParams(nKernelParams) {
70 if ((width < 1) || (height < 1)) {
71 std::ostringstream os;
72 os <<
"kernel height = " << height <<
" and/or width = " << width <<
" < 1";
73 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
75 if (dynamic_cast<const NullSpatialFunction *>(&spatialFunction)) {
78 if (nKernelParams == 0) {
79 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
"Kernel function has no parameters");
81 for (
unsigned int ii = 0; ii < nKernelParams; ++ii) {
90 std::ostringstream os;
91 os <<
"image dimensions = ( " << image.
getWidth() <<
", " << image.
getHeight() <<
") != (" 93 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
95 image.
setXY0(-_ctrX, -_ctrY);
102 Kernel::Kernel(
int width,
int height, std::vector<SpatialFunctionPtr> spatialFunctionList)
103 : daf::base::Citizen(typeid(this)),
108 _nKernelParams(spatialFunctionList.size()) {
109 if ((width < 1) || (height < 1)) {
110 std::ostringstream os;
111 os <<
"kernel height = " << height <<
" and/or width = " << width <<
" < 1";
112 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
114 for (
unsigned int ii = 0; ii < spatialFunctionList.size(); ++ii) {
126 if (params.size() != nKernelParams) {
128 pexExcept::InvalidParameterError,
129 (boost::format(
"params has %d entries instead of %d") % params.size() % nKernelParams).str());
132 for (
unsigned int ii = 0; ii < nKernelParams; ++ii) {
133 if (params[ii].size() != nSpatialParams) {
134 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
135 (boost::format(
"params[%d] has %d entries instead of %d") % ii %
136 params[ii].size() % nSpatialParams)
141 if (nSpatialParams > 0) {
142 for (
unsigned int ii = 0; ii < nKernelParams; ++ii) {
150 std::vector<double>::iterator paramIter = kernelParams.begin();
153 *paramIter = (*(*funcIter))(x, y);
160 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
"kernel is not spatially varying");
162 std::ostringstream errStream;
164 throw LSST_EXCEPT(pexExcept::InvalidParameterError, errStream.str());
171 std::vector<SpatialFunctionPtr> spFuncCopyList;
174 spFuncCopyList.push_back((**spFuncIter).clone());
176 return spFuncCopyList;
188 std::ostringstream os;
190 <<
") in one or both dimensions";
191 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
198 std::ostringstream os;
199 os << prefix <<
"Kernel:" << std::endl;
200 os << prefix <<
"..height, width: " << _height <<
", " << _width << std::endl;
201 os << prefix <<
"..ctr (X, Y): " << _ctrX <<
", " << _ctrY << std::endl;
202 os << prefix <<
"..nKernelParams: " << _nKernelParams << std::endl;
203 os << prefix <<
"..isSpatiallyVarying: " << (this->
isSpatiallyVarying() ?
"True" :
"False") << std::endl;
205 os << prefix <<
"..spatialFunctions:" << std::endl;
208 os << prefix <<
"...." << (*spFuncPtr)->toString() << std::endl;
214 #if 0 // This fails to compile with icc 215 void Kernel::toFile(std::string fileName)
const {
216 std::ofstream os(fileName.c_str());
217 boost::archive::text_oarchive oa(os);
227 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
"Kernel has no kernel parameters");
int getHeight() const
Return the Kernel's height.
void setXY0(geom::Point2I const origin)
Set the ImageBase's origin.
Point2I const getMin() const
int getCtrX() const
Return x index of kernel's center.
int getCtrY() const
Return y index of kernel's center.
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 void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
int getHeight() const
Return the number of rows in the image.
lsst::afw::geom::Point2I getCtr() const
Return index of kernel's center.
std::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
virtual std::shared_ptr< Function2< ReturnT > > clone() const =0
Return a pointer to a deep copy of this function.
Extent< int, 2 > Extent2I
A Function taking two arguments.
An integer coordinate rectangle.
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
A base class for image defects.
std::vector< SpatialFunctionPtr > getSpatialFunctionList() const
Return a list of clones of the spatial functions.
deltafunction_kernel_tag deltafunction_kernel_tag_
Used as default value in argument lists.
void computeKernelParametersFromSpatialModel(std::vector< double > &kernelParams, double x, double y) const
Compute the kernel parameters at a specified point.
SpatialFunctionPtr getSpatialFunction(unsigned int index) const
Return a clone of the specified spatial function (one component of the spatial model) ...
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.
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
int getWidth() const
Return the Kernel's width.
unsigned int getNKernelParameters() const
Return the number of kernel parameters (0 if none)
void setSpatialParameters(const std::vector< std::vector< double >> params)
Set the parameters of all spatial functions.
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Kernel has only one non-zero pixel.
Extent2I const getDimensions() const
std::vector< SpatialFunctionPtr > _spatialFunctionList
int getWidth() const
Return the number of columns in the image.
Kernel()
Construct a null Kernel of size 0,0.
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const =0
Low-level version of computeImage.
int getNSpatialParameters() const
Return the number of spatial parameters (0 if not spatially varying)
generic_kernel_tag generic_kernel_tag_
Used as default value in argument lists.
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...
geom::Extent2I const getDimensions() const
Return the Kernel's dimensions (width, height)
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Tags carrying information about Kernels Kernel with no special properties.