LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
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 namespace afwGeom = lsst::afw::geom;
42 namespace afwMath = lsst::afw::math;
43 
47 
48 //
49 // Constructors
50 //
52 :
53  daf::base::Citizen(typeid(this)),
54  _spatialFunctionList(),
55  _width(0),
56  _height(0),
57  _ctrX(0),
58  _ctrY(0),
59  _nKernelParams(0)
60 {}
61 
63  int width,
64  int height,
65  unsigned int nKernelParams,
66  SpatialFunction const &spatialFunction)
67 :
68  daf::base::Citizen(typeid(this)),
69  _spatialFunctionList(),
70  _width(width),
71  _height(height),
72  _ctrX((width-1)/2),
73  _ctrY((height-1)/2),
74  _nKernelParams(nKernelParams)
75 {
76  if ((width < 1) || (height < 1)) {
77  std::ostringstream os;
78  os << "kernel height = " << height << " and/or width = " << width << " < 1";
79  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
80  }
81  if (dynamic_cast<const NullSpatialFunction*>(&spatialFunction)) {
82  // spatialFunction is not really present
83  } else {
84  if (nKernelParams == 0) {
85  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel function has no parameters");
86  }
87  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
88  SpatialFunctionPtr spatialFunctionCopy = spatialFunction.clone();
89  this->_spatialFunctionList.push_back(spatialFunctionCopy);
90  }
91  }
92 }
93 
96  bool doNormalize,
97  double x,
98  double y
99 ) const {
100  if (image.getDimensions() != this->getDimensions()) {
101  std::ostringstream os;
102  os << "image dimensions = ( " << image.getWidth() << ", " << image.getHeight()
103  << ") != (" << this->getWidth() << ", " << this->getHeight() << ") = kernel dimensions";
104  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
105  }
106  image.setXY0(-_ctrX, -_ctrY);
107  if (this->isSpatiallyVarying()) {
108  this->setKernelParametersFromSpatialModel(x, y);
109  }
110  return doComputeImage(image, doNormalize);
111 }
112 
114  int width,
115  int height,
116  std::vector<SpatialFunctionPtr> spatialFunctionList)
117 :
118  daf::base::Citizen(typeid(this)),
119  _width(width),
120  _height(height),
121  _ctrX(width/2),
122  _ctrY(height/2),
123  _nKernelParams(spatialFunctionList.size())
124 {
125  if ((width < 1) || (height < 1)) {
126  std::ostringstream os;
127  os << "kernel height = " << height << " and/or width = " << width << " < 1";
128  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
129  }
130  for (unsigned int ii = 0; ii < spatialFunctionList.size(); ++ii) {
131  SpatialFunctionPtr spatialFunctionCopy = spatialFunctionList[ii]->clone();
132  this->_spatialFunctionList.push_back(spatialFunctionCopy);
133  }
134 }
135 
136 //
137 // Public Member Functions
138 //
139 void afwMath::Kernel::setSpatialParameters(const std::vector<std::vector<double> > params) {
140  // Check params size before changing anything
141  unsigned int nKernelParams = this->getNKernelParameters();
142  if (params.size() != nKernelParams) {
143  throw LSST_EXCEPT(pexExcept::InvalidParameterError,
144  (boost::format("params has %d entries instead of %d") % params.size() % nKernelParams).str());
145  }
146  unsigned int nSpatialParams = this->getNSpatialParameters();
147  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
148  if (params[ii].size() != nSpatialParams) {
149  throw LSST_EXCEPT(pexExcept::InvalidParameterError,
150  (boost::format("params[%d] has %d entries instead of %d") %
151  ii % params[ii].size() % nSpatialParams).str());
152  }
153  }
154  // Set parameters
155  if (nSpatialParams > 0) {
156  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
157  this->_spatialFunctionList[ii]->setParameters(params[ii]);
158  }
159  }
160 }
161 
163  std::vector<double> &kernelParams, double x, double y) const {
164  std::vector<double>::iterator paramIter = kernelParams.begin();
165  std::vector<SpatialFunctionPtr>::const_iterator funcIter = _spatialFunctionList.begin();
166  for ( ; funcIter != _spatialFunctionList.end(); ++funcIter, ++paramIter) {
167  *paramIter = (*(*funcIter))(x,y);
168  }
169 }
170 
172  unsigned int index
173 ) const {
174  if (index >= _spatialFunctionList.size()) {
175  if (!this->isSpatiallyVarying()) {
176  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "kernel is not spatially varying");
177  } else {
178  std::ostringstream errStream;
179  errStream << "index = " << index << "; must be < , " << _spatialFunctionList.size();
180  throw LSST_EXCEPT(pexExcept::InvalidParameterError, errStream.str());
181  }
182  }
183  return _spatialFunctionList[index]->clone();
184 }
185 
186 std::vector<afwMath::Kernel::SpatialFunctionPtr> afwMath::Kernel::getSpatialFunctionList(
187 ) const {
188  std::vector<SpatialFunctionPtr> spFuncCopyList;
189  for (std::vector<SpatialFunctionPtr>::const_iterator spFuncIter = _spatialFunctionList.begin();
190  spFuncIter != _spatialFunctionList.end(); ++spFuncIter) {
191  spFuncCopyList.push_back((**spFuncIter).clone());
192  }
193  return spFuncCopyList;
194 }
195 
196 std::vector<double> afwMath::Kernel::getKernelParameters() const {
197  return std::vector<double>();
198 }
199 
200 
202  return afwGeom::Box2I(
203  afwGeom::Point2I(bbox.getMin() - afwGeom::Extent2I(getCtr())),
204  afwGeom::Extent2I(bbox.getDimensions() + getDimensions() - afwGeom::Extent2I(1,1)));
205 }
206 
208  if ((bbox.getWidth() < getWidth()) || ((bbox.getHeight() < getHeight()))) {
209  std::ostringstream os;
210  os << "bbox dimensions = " << bbox.getDimensions() << " < ("
211  << getWidth() << ", " << getHeight() << ") in one or both dimensions";
212  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
213  }
214  return afwGeom::Box2I(
216  bbox.getMinX() + getCtrX(),
217  bbox.getMinY() + getCtrY()),
219  bbox.getWidth() + 1 - getWidth(),
220  bbox.getHeight() + 1 - getHeight()));
221 }
222 
223 
224 std::string afwMath::Kernel::toString(std::string const& prefix) const {
225  std::ostringstream os;
226  os << prefix << "Kernel:" << std::endl;
227  os << prefix << "..height, width: " << _height << ", " << _width << std::endl;
228  os << prefix << "..ctr (X, Y): " << _ctrX << ", " << _ctrY << std::endl;
229  os << prefix << "..nKernelParams: " << _nKernelParams << std::endl;
230  os << prefix << "..isSpatiallyVarying: " << (this->isSpatiallyVarying() ? "True" : "False") << std::endl;
231  if (this->isSpatiallyVarying()) {
232  os << prefix << "..spatialFunctions:" << std::endl;
233  for (std::vector<SpatialFunctionPtr>::const_iterator spFuncPtr = _spatialFunctionList.begin();
234  spFuncPtr != _spatialFunctionList.end(); ++spFuncPtr) {
235  os << prefix << "...." << (*spFuncPtr)->toString() << std::endl;
236  }
237  }
238  return os.str();
239 }
240 
241 #if 0 // This fails to compile with icc
242 void afwMath::Kernel::toFile(std::string fileName) const {
243  std::ofstream os(fileName.c_str());
244  boost::archive::text_oarchive oa(os);
245  oa << this;
246 }
247 #endif
248 
249 //
250 // Protected Member Functions
251 //
252 
253 void afwMath::Kernel::setKernelParameter(unsigned int, double) const {
254  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel has no kernel parameters");
255 }
256 
258  std::vector<SpatialFunctionPtr>::const_iterator funcIter = _spatialFunctionList.begin();
259  for (int ii = 0; funcIter != _spatialFunctionList.end(); ++funcIter, ++ii) {
260  this->setKernelParameter(ii, (*(*funcIter))(x,y));
261  }
262 }
263 
264 std::string afwMath::Kernel::getPythonModule() const { return "lsst.afw.math"; }
int y
Declare the Kernel class and subclasses.
boost::shared_ptr< lsst::afw::math::Function2< double > > SpatialFunctionPtr
Definition: Kernel.h:143
Kernel()
Construct a null Kernel of size 0,0.
Definition: Kernel.cc:51
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
Definition: Kernel.cc:257
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
Definition: Kernel.cc:253
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:264
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:224
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
Definition: Kernel.cc:196
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:524
lsst::afw::geom::Box2I growBBox(lsst::afw::geom::Box2I const &bbox) const
Definition: Kernel.cc:201
std::vector< SpatialFunctionPtr > getSpatialFunctionList() const
Return a list of clones of the spatial functions.
Definition: Kernel.cc:186
int getMinY() const
Definition: Box.h:125
SpatialFunctionPtr getSpatialFunction(unsigned int index) const
Return a clone of the specified spatial function (one component of the spatial model) ...
Definition: Kernel.cc:171
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:237
int getHeight() const
Definition: Box.h:155
Extent2I const getDimensions() const
Definition: Box.h:153
int getMinX() const
Definition: Box.h:124
deltafunction_kernel_tag deltafunction_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:45
int x
void setXY0(geom::Point2I const origin)
Definition: Image.h:362
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Kernel has only one non-zero pixel.
Definition: traits.h:59
generic_kernel_tag generic_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:44
void computeKernelParametersFromSpatialModel(std::vector< double > &kernelParams, double x, double y) const
Compute the kernel parameters at a specified point.
Definition: Kernel.cc:162
Point2I const getMin() const
Definition: Box.h:123
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:239
lsst::afw::geom::Box2I shrinkBBox(lsst::afw::geom::Box2I const &bbox) const
Definition: Kernel.cc:207
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: Image.h:298
Extent< int, 2 > Extent2I
Definition: Extent.h:355
void setSpatialParameters(const std::vector< std::vector< double > > params)
Set the parameters of all spatial functions.
Definition: Kernel.cc:139
int getWidth() const
Definition: Box.h:154
Tags carrying information about Kernels Kernel with no special properties.
Definition: traits.h:58
virtual Ptr clone() const =0
Return a pointer to a deep copy of this function.
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:94
Include files required for standard LSST Exception handling.