lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <fstream>
25 #include <sstream>
26 
27 #include "boost/format.hpp"
28 #if defined(__ICC)
29 #pragma warning(push)
30 #pragma warning(disable : 444)
31 #endif
32 #include "boost/archive/text_oarchive.hpp"
33 #if defined(__ICC)
34 #pragma warning(pop)
35 #endif
36 
37 #include "lsst/pex/exceptions.h"
38 #include "lsst/afw/math/Kernel.h"
39 
40 namespace pexExcept = lsst::pex::exceptions;
41 
42 namespace lsst {
43 namespace afw {
44 namespace math {
45 
49 
50 //
51 // Constructors
52 //
54  : daf::base::Citizen(typeid(this)),
55  _spatialFunctionList(),
56  _width(0),
57  _height(0),
58  _ctrX(0),
59  _ctrY(0),
60  _nKernelParams(0) {}
61 
62 Kernel::Kernel(int width, int height, unsigned int nKernelParams, SpatialFunction const &spatialFunction)
63  : daf::base::Citizen(typeid(this)),
64  _spatialFunctionList(),
65  _width(width),
66  _height(height),
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());
74  }
75  if (dynamic_cast<const NullSpatialFunction *>(&spatialFunction)) {
76  // spatialFunction is not really present
77  } else {
78  if (nKernelParams == 0) {
79  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel function has no parameters");
80  }
81  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
82  SpatialFunctionPtr spatialFunctionCopy = spatialFunction.clone();
83  this->_spatialFunctionList.push_back(spatialFunctionCopy);
84  }
85  }
86 }
87 
88 double Kernel::computeImage(image::Image<Pixel> &image, bool doNormalize, double x, double y) const {
89  if (image.getDimensions() != this->getDimensions()) {
90  std::ostringstream os;
91  os << "image dimensions = ( " << image.getWidth() << ", " << image.getHeight() << ") != ("
92  << this->getWidth() << ", " << this->getHeight() << ") = kernel dimensions";
93  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
94  }
95  image.setXY0(-_ctrX, -_ctrY);
96  if (this->isSpatiallyVarying()) {
98  }
99  return doComputeImage(image, doNormalize);
100 }
101 
102 Kernel::Kernel(int width, int height, std::vector<SpatialFunctionPtr> spatialFunctionList)
103  : daf::base::Citizen(typeid(this)),
104  _width(width),
105  _height(height),
106  _ctrX(width / 2),
107  _ctrY(height / 2),
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());
113  }
114  for (unsigned int ii = 0; ii < spatialFunctionList.size(); ++ii) {
115  SpatialFunctionPtr spatialFunctionCopy = spatialFunctionList[ii]->clone();
116  this->_spatialFunctionList.push_back(spatialFunctionCopy);
117  }
118 }
119 
120 //
121 // Public Member Functions
122 //
123 void Kernel::setSpatialParameters(const std::vector<std::vector<double> > params) {
124  // Check params size before changing anything
125  unsigned int nKernelParams = this->getNKernelParameters();
126  if (params.size() != nKernelParams) {
127  throw LSST_EXCEPT(
128  pexExcept::InvalidParameterError,
129  (boost::format("params has %d entries instead of %d") % params.size() % nKernelParams).str());
130  }
131  unsigned int nSpatialParams = this->getNSpatialParameters();
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)
137  .str());
138  }
139  }
140  // Set parameters
141  if (nSpatialParams > 0) {
142  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
143  this->_spatialFunctionList[ii]->setParameters(params[ii]);
144  }
145  }
146 }
147 
148 void Kernel::computeKernelParametersFromSpatialModel(std::vector<double> &kernelParams, double x,
149  double y) const {
150  std::vector<double>::iterator paramIter = kernelParams.begin();
151  std::vector<SpatialFunctionPtr>::const_iterator funcIter = _spatialFunctionList.begin();
152  for (; funcIter != _spatialFunctionList.end(); ++funcIter, ++paramIter) {
153  *paramIter = (*(*funcIter))(x, y);
154  }
155 }
156 
158  if (index >= _spatialFunctionList.size()) {
159  if (!this->isSpatiallyVarying()) {
160  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "kernel is not spatially varying");
161  } else {
162  std::ostringstream errStream;
163  errStream << "index = " << index << "; must be < , " << _spatialFunctionList.size();
164  throw LSST_EXCEPT(pexExcept::InvalidParameterError, errStream.str());
165  }
166  }
167  return _spatialFunctionList[index]->clone();
168 }
169 
170 std::vector<Kernel::SpatialFunctionPtr> Kernel::getSpatialFunctionList() const {
171  std::vector<SpatialFunctionPtr> spFuncCopyList;
172  for (std::vector<SpatialFunctionPtr>::const_iterator spFuncIter = _spatialFunctionList.begin();
173  spFuncIter != _spatialFunctionList.end(); ++spFuncIter) {
174  spFuncCopyList.push_back((**spFuncIter).clone());
175  }
176  return spFuncCopyList;
177 }
178 
179 std::vector<double> Kernel::getKernelParameters() const { return std::vector<double>(); }
180 
184 }
185 
187  if ((bbox.getWidth() < getWidth()) || ((bbox.getHeight() < getHeight()))) {
188  std::ostringstream os;
189  os << "bbox dimensions = " << bbox.getDimensions() << " < (" << getWidth() << ", " << getHeight()
190  << ") in one or both dimensions";
191  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
192  }
193  return geom::Box2I(geom::Point2I(bbox.getMinX() + getCtrX(), bbox.getMinY() + getCtrY()),
194  geom::Extent2I(bbox.getWidth() + 1 - getWidth(), bbox.getHeight() + 1 - getHeight()));
195 }
196 
197 std::string Kernel::toString(std::string const &prefix) const {
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;
204  if (this->isSpatiallyVarying()) {
205  os << prefix << "..spatialFunctions:" << std::endl;
206  for (std::vector<SpatialFunctionPtr>::const_iterator spFuncPtr = _spatialFunctionList.begin();
207  spFuncPtr != _spatialFunctionList.end(); ++spFuncPtr) {
208  os << prefix << "...." << (*spFuncPtr)->toString() << std::endl;
209  }
210  }
211  return os.str();
212 }
213 
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);
218  oa << this;
219 }
220 #endif
221 
222 //
223 // Protected Member Functions
224 //
225 
226 void Kernel::setKernelParameter(unsigned int, double) const {
227  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel has no kernel parameters");
228 }
229 
230 void Kernel::setKernelParametersFromSpatialModel(double x, double y) const {
231  std::vector<SpatialFunctionPtr>::const_iterator funcIter = _spatialFunctionList.begin();
232  for (int ii = 0; funcIter != _spatialFunctionList.end(); ++funcIter, ++ii) {
233  this->setKernelParameter(ii, (*(*funcIter))(x, y));
234  }
235 }
236 
237 std::string Kernel::getPythonModule() const { return "lsst.afw.math"; }
238 }
239 }
240 } // end lsst::afw::math
void setXY0(geom::Point2I const origin)
Set the ImageBase&#39;s origin.
Definition: Image.h:445
Extent2I const getDimensions() const
Definition: Box.h:153
geom::Extent2I const getDimensions() const
Return the Kernel&#39;s dimensions (width, height)
Definition: Kernel.h:210
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: Kernel.cc:237
int getCtrY() const
Return y index of kernel&#39;s center.
Definition: Kernel.h:246
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:197
int getNSpatialParameters() const
Return the number of spatial parameters (0 if not spatially varying)
Definition: Kernel.h:264
std::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
Definition: Kernel.h:129
virtual std::shared_ptr< Function2< ReturnT > > clone() const =0
Return a pointer to a deep copy of this function.
Extent< int, 2 > Extent2I
Definition: Extent.h:341
int getCtrX() const
Return x index of kernel&#39;s center.
Definition: Kernel.h:239
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
Definition: Kernel.cc:226
A Function taking two arguments.
Definition: Function.h:266
An integer coordinate rectangle.
Definition: Box.h:55
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:227
int getWidth() const
Return the Kernel&#39;s width.
Definition: Kernel.h:222
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
Definition: Kernel.cc:179
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
Definition: Kernel.cc:230
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: Image.h:367
deltafunction_kernel_tag deltafunction_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:47
unsigned int getNKernelParameters() const
Return the number of kernel parameters (0 if none)
Definition: Kernel.h:259
int getMinY() const
Definition: Box.h:125
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:88
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...
Definition: Kernel.cc:181
SpatialFunctionPtr getSpatialFunction(unsigned int index) const
Return a clone of the specified spatial function (one component of the spatial model) ...
Definition: Kernel.cc:157
int getMinX() const
Definition: Box.h:124
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:309
double x
lsst::afw::geom::Point2I getCtr() const
Return index of kernel&#39;s center.
Definition: Kernel.h:232
int getWidth() const
Definition: Box.h:154
void setSpatialParameters(const std::vector< std::vector< double >> params)
Set the parameters of all spatial functions.
Definition: Kernel.cc:123
Kernel has only one non-zero pixel.
Definition: traits.h:56
int getHeight() const
Definition: Box.h:155
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:482
afw::table::Key< afw::table::Array< ImagePixelT > > image
Kernel()
Construct a null Kernel of size 0,0.
Definition: Kernel.cc:53
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const =0
Low-level version of computeImage.
void computeKernelParametersFromSpatialModel(std::vector< double > &kernelParams, double x, double y) const
Compute the kernel parameters at a specified point.
Definition: Kernel.cc:148
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...
Definition: Kernel.cc:186
generic_kernel_tag generic_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:46
Point2I const getMin() const
Definition: Box.h:123
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:307
std::vector< SpatialFunctionPtr > getSpatialFunctionList() const
Return a list of clones of the spatial functions.
Definition: Kernel.cc:170
Tags carrying information about Kernels Kernel with no special properties.
Definition: traits.h:53
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
Definition: Kernel.h:366