LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Kernel.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 #include <sstream>
25 
26 #include "boost/format.hpp"
27 
28 #include "lsst/pex/exceptions.h"
29 #include "lsst/afw/math/Kernel.h"
31 
33 
34 namespace lsst {
35 namespace afw {
36 
39 
40 namespace math {
41 
45 
46 //
47 // Constructors
48 //
49 Kernel::Kernel() : _spatialFunctionList(), _width(0), _height(0), _ctrX(0), _ctrY(0), _nKernelParams(0) {}
50 
51 Kernel::Kernel(int width, int height, unsigned int nKernelParams, SpatialFunction const &spatialFunction)
52  : _spatialFunctionList(),
53  _width(width),
54  _height(height),
55  _ctrX((width - 1) / 2),
56  _ctrY((height - 1) / 2),
57  _nKernelParams(nKernelParams) {
58  if ((width < 1) || (height < 1)) {
60  os << "kernel height = " << height << " and/or width = " << width << " < 1";
62  }
63  if (dynamic_cast<const NullSpatialFunction *>(&spatialFunction)) {
64  // spatialFunction is not really present
65  } else {
66  if (nKernelParams == 0) {
67  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel function has no parameters");
68  }
69  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
70  SpatialFunctionPtr spatialFunctionCopy = spatialFunction.clone();
71  this->_spatialFunctionList.push_back(spatialFunctionCopy);
72  }
73  }
74 }
75 
76 double Kernel::computeImage(image::Image<Pixel> &image, bool doNormalize, double x, double y) const {
77  if (image.getDimensions() != this->getDimensions()) {
79  os << "image dimensions = ( " << image.getWidth() << ", " << image.getHeight() << ") != ("
80  << this->getWidth() << ", " << this->getHeight() << ") = kernel dimensions";
82  }
83  image.setXY0(-_ctrX, -_ctrY);
84  if (this->isSpatiallyVarying()) {
86  }
87  return doComputeImage(image, doNormalize);
88 }
89 
90 Kernel::Kernel(int width, int height, std::vector<SpatialFunctionPtr> spatialFunctionList)
91  : _width(width),
92  _height(height),
93  _ctrX(width / 2),
94  _ctrY(height / 2),
95  _nKernelParams(spatialFunctionList.size()) {
96  if ((width < 1) || (height < 1)) {
98  os << "kernel height = " << height << " and/or width = " << width << " < 1";
100  }
101  for (auto const &ii : spatialFunctionList) {
102  SpatialFunctionPtr spatialFunctionCopy = ii->clone();
103  this->_spatialFunctionList.push_back(spatialFunctionCopy);
104  }
105 }
106 
107 //
108 // Public Member Functions
109 //
111  // Check params size before changing anything
112  unsigned int nKernelParams = this->getNKernelParameters();
113  if (params.size() != nKernelParams) {
114  throw LSST_EXCEPT(
116  (boost::format("params has %d entries instead of %d") % params.size() % nKernelParams).str());
117  }
118  unsigned int nSpatialParams = this->getNSpatialParameters();
119  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
120  if (params[ii].size() != nSpatialParams) {
122  (boost::format("params[%d] has %d entries instead of %d") % ii %
123  params[ii].size() % nSpatialParams)
124  .str());
125  }
126  }
127  // Set parameters
128  if (nSpatialParams > 0) {
129  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
130  this->_spatialFunctionList[ii]->setParameters(params[ii]);
131  }
132  }
133 }
134 
136  double y) const {
137  std::vector<double>::iterator paramIter = kernelParams.begin();
139  for (; funcIter != _spatialFunctionList.end(); ++funcIter, ++paramIter) {
140  *paramIter = (*(*funcIter))(x, y);
141  }
142 }
143 
145  if (index >= _spatialFunctionList.size()) {
146  if (!this->isSpatiallyVarying()) {
147  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "kernel is not spatially varying");
148  } else {
149  std::ostringstream errStream;
150  errStream << "index = " << index << "; must be < , " << _spatialFunctionList.size();
152  }
153  }
154  return _spatialFunctionList[index]->clone();
155 }
156 
158  std::vector<SpatialFunctionPtr> spFuncCopyList;
159  for (auto const &spFuncIter : _spatialFunctionList) {
160  spFuncCopyList.push_back((*spFuncIter).clone());
161  }
162  return spFuncCopyList;
163 }
164 
166 
168  return lsst::geom::Box2I(
170  lsst::geom::Extent2I(bbox.getDimensions() + getDimensions() - lsst::geom::Extent2I(1, 1)));
171 }
172 
174  if ((bbox.getWidth() < getWidth()) || ((bbox.getHeight() < getHeight()))) {
176  os << "bbox dimensions = " << bbox.getDimensions() << " < (" << getWidth() << ", " << getHeight()
177  << ") in one or both dimensions";
179  }
180  return lsst::geom::Box2I(
182  lsst::geom::Extent2I(bbox.getWidth() + 1 - getWidth(), bbox.getHeight() + 1 - getHeight()));
183 }
184 
187  os << prefix << "Kernel:" << std::endl;
188  os << prefix << "..height, width: " << _height << ", " << _width << std::endl;
189  os << prefix << "..ctr (X, Y): " << _ctrX << ", " << _ctrY << std::endl;
190  os << prefix << "..nKernelParams: " << _nKernelParams << std::endl;
191  os << prefix << "..isSpatiallyVarying: " << (this->isSpatiallyVarying() ? "True" : "False") << std::endl;
192  if (this->isSpatiallyVarying()) {
193  os << prefix << "..spatialFunctions:" << std::endl;
194  for (auto const &spFuncPtr : _spatialFunctionList) {
195  os << prefix << "...." << spFuncPtr->toString() << std::endl;
196  }
197  }
198  return os.str();
199 }
200 
201 #if 0 // This fails to compile with icc
202 void Kernel::toFile(std::string fileName) const {
203  std::ofstream os(fileName.c_str());
204  boost::archive::text_oarchive oa(os);
205  oa << this;
206 }
207 #endif
208 
209 //
210 // Protected Member Functions
211 //
212 
213 void Kernel::setKernelParameter(unsigned int, double) const {
214  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel has no kernel parameters");
215 }
216 
217 void Kernel::setKernelParametersFromSpatialModel(double x, double y) const {
219  for (int ii = 0; funcIter != _spatialFunctionList.end(); ++funcIter, ++ii) {
220  this->setKernelParameter(ii, (*(*funcIter))(x, y));
221  }
222 }
223 
224 std::string Kernel::getPythonModule() const { return "lsst.afw.math"; }
225 } // namespace math
226 } // namespace afw
227 } // namespace lsst
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
double x
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::ostream * os
Definition: Schema.cc:557
std::string prefix
Definition: SchemaMapper.cc:72
int y
Definition: SpanSet.cc:48
T begin(T... args)
T c_str(T... args)
A Function taking two arguments.
Definition: Function.h:259
virtual std::shared_ptr< Function2< ReturnT > > clone() const =0
Return a pointer to a deep copy of this function.
lsst::geom::Extent2I const getDimensions() const
Return the Kernel's dimensions (width, height)
Definition: Kernel.h:212
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:449
int getHeight() const
Return the Kernel's height.
Definition: Kernel.h:229
lsst::geom::Point2I getCtr() const
Return index of kernel's center.
Definition: Kernel.h:234
unsigned int getNKernelParameters() const
Return the number of kernel parameters (0 if none)
Definition: Kernel.h:246
int getNSpatialParameters() const
Return the number of spatial parameters (0 if not spatially varying)
Definition: Kernel.h:251
std::vector< SpatialFunctionPtr > getSpatialFunctionList() const
Return a list of clones of the spatial functions.
Definition: Kernel.cc:157
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
Definition: Kernel.cc:165
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:185
void computeKernelParametersFromSpatialModel(std::vector< double > &kernelParams, double x, double y) const
Compute the kernel parameters at a specified point.
Definition: Kernel.cc:135
SpatialFunctionPtr getSpatialFunction(unsigned int index) const
Return a clone of the specified spatial function (one component of the spatial model)
Definition: Kernel.cc:144
lsst::geom::Box2I shrinkBBox(lsst::geom::Box2I const &bbox) const
Given a bounding box for an image one wishes to convolve with this kernel, return the bounding box fo...
Definition: Kernel.cc:173
int getWidth() const
Return the Kernel's width.
Definition: Kernel.h:224
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
Definition: Kernel.h:333
lsst::geom::Box2I growBBox(lsst::geom::Box2I const &bbox) const
Given a bounding box for pixels one wishes to compute by convolving an image with this kernel,...
Definition: Kernel.cc:167
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
Definition: Kernel.cc:213
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: Kernel.cc:224
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.
Definition: Kernel.cc:76
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const =0
Low-level version of computeImage.
void setSpatialParameters(const std::vector< std::vector< double >> params)
Set the parameters of all spatial functions.
Definition: Kernel.cc:110
Kernel()
Construct a null Kernel of size 0,0.
Definition: Kernel.cc:49
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
Definition: Kernel.cc:217
a class used in function calls to indicate that no Function2 is being provided
Definition: Function.h:458
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
An integer coordinate rectangle.
Definition: Box.h:55
Reports invalid arguments.
Definition: Runtime.h:66
T endl(T... args)
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
deltafunction_kernel_tag deltafunction_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:43
generic_kernel_tag generic_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:42
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
T push_back(T... args)
T str(T... args)
Kernel has only one non-zero pixel.
Definition: traits.h:56
Tags carrying information about Kernels Kernel with no special properties.
Definition: traits.h:53