LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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 #include "lsst/afw/table/io/Persistable.cc"
40 
42 
43 namespace lsst {
44 namespace afw {
45 
48 
49 namespace math {
50 
54 
55 //
56 // Constructors
57 //
58 Kernel::Kernel()
59  : daf::base::Citizen(typeid(this)),
60  _spatialFunctionList(),
61  _width(0),
62  _height(0),
63  _ctrX(0),
64  _ctrY(0),
65  _nKernelParams(0) {}
66 
67 Kernel::Kernel(int width, int height, unsigned int nKernelParams, SpatialFunction const &spatialFunction)
68  : daf::base::Citizen(typeid(this)),
70  _width(width),
71  _height(height),
72  _ctrX((width - 1) / 2),
73  _ctrY((height - 1) / 2),
74  _nKernelParams(nKernelParams) {
75  if ((width < 1) || (height < 1)) {
77  os << "kernel height = " << height << " and/or width = " << width << " < 1";
79  }
80  if (dynamic_cast<const NullSpatialFunction *>(&spatialFunction)) {
81  // spatialFunction is not really present
82  } else {
83  if (nKernelParams == 0) {
84  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel function has no parameters");
85  }
86  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
87  SpatialFunctionPtr spatialFunctionCopy = spatialFunction.clone();
88  this->_spatialFunctionList.push_back(spatialFunctionCopy);
89  }
90  }
91 }
92 
93 double Kernel::computeImage(image::Image<Pixel> &image, bool doNormalize, double x, double y) const {
94  if (image.getDimensions() != this->getDimensions()) {
96  os << "image dimensions = ( " << image.getWidth() << ", " << image.getHeight() << ") != ("
97  << this->getWidth() << ", " << this->getHeight() << ") = kernel dimensions";
99  }
100  image.setXY0(-_ctrX, -_ctrY);
101  if (this->isSpatiallyVarying()) {
103  }
104  return doComputeImage(image, doNormalize);
105 }
106 
107 Kernel::Kernel(int width, int height, std::vector<SpatialFunctionPtr> spatialFunctionList)
108  : daf::base::Citizen(typeid(this)),
109  _width(width),
110  _height(height),
111  _ctrX(width / 2),
112  _ctrY(height / 2),
113  _nKernelParams(spatialFunctionList.size()) {
114  if ((width < 1) || (height < 1)) {
116  os << "kernel height = " << height << " and/or width = " << width << " < 1";
118  }
119  for (unsigned int ii = 0; ii < spatialFunctionList.size(); ++ii) {
120  SpatialFunctionPtr spatialFunctionCopy = spatialFunctionList[ii]->clone();
121  this->_spatialFunctionList.push_back(spatialFunctionCopy);
122  }
123 }
124 
125 //
126 // Public Member Functions
127 //
129  // Check params size before changing anything
130  unsigned int nKernelParams = this->getNKernelParameters();
131  if (params.size() != nKernelParams) {
132  throw LSST_EXCEPT(
134  (boost::format("params has %d entries instead of %d") % params.size() % nKernelParams).str());
135  }
136  unsigned int nSpatialParams = this->getNSpatialParameters();
137  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
138  if (params[ii].size() != nSpatialParams) {
140  (boost::format("params[%d] has %d entries instead of %d") % ii %
141  params[ii].size() % nSpatialParams)
142  .str());
143  }
144  }
145  // Set parameters
146  if (nSpatialParams > 0) {
147  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
148  this->_spatialFunctionList[ii]->setParameters(params[ii]);
149  }
150  }
151 }
152 
154  double y) const {
155  std::vector<double>::iterator paramIter = kernelParams.begin();
157  for (; funcIter != _spatialFunctionList.end(); ++funcIter, ++paramIter) {
158  *paramIter = (*(*funcIter))(x, y);
159  }
160 }
161 
163  if (index >= _spatialFunctionList.size()) {
164  if (!this->isSpatiallyVarying()) {
165  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "kernel is not spatially varying");
166  } else {
167  std::ostringstream errStream;
168  errStream << "index = " << index << "; must be < , " << _spatialFunctionList.size();
170  }
171  }
172  return _spatialFunctionList[index]->clone();
173 }
174 
176  std::vector<SpatialFunctionPtr> spFuncCopyList;
178  spFuncIter != _spatialFunctionList.end(); ++spFuncIter) {
179  spFuncCopyList.push_back((**spFuncIter).clone());
180  }
181  return spFuncCopyList;
182 }
183 
185 
187  return lsst::geom::Box2I(
190 }
191 
193  if ((bbox.getWidth() < getWidth()) || ((bbox.getHeight() < getHeight()))) {
195  os << "bbox dimensions = " << bbox.getDimensions() << " < (" << getWidth() << ", " << getHeight()
196  << ") in one or both dimensions";
198  }
199  return lsst::geom::Box2I(
200  lsst::geom::Point2I(bbox.getMinX() + getCtrX(), bbox.getMinY() + getCtrY()),
201  lsst::geom::Extent2I(bbox.getWidth() + 1 - getWidth(), bbox.getHeight() + 1 - getHeight()));
202 }
203 
206  os << prefix << "Kernel:" << std::endl;
207  os << prefix << "..height, width: " << _height << ", " << _width << std::endl;
208  os << prefix << "..ctr (X, Y): " << _ctrX << ", " << _ctrY << std::endl;
209  os << prefix << "..nKernelParams: " << _nKernelParams << std::endl;
210  os << prefix << "..isSpatiallyVarying: " << (this->isSpatiallyVarying() ? "True" : "False") << std::endl;
211  if (this->isSpatiallyVarying()) {
212  os << prefix << "..spatialFunctions:" << std::endl;
214  spFuncPtr != _spatialFunctionList.end(); ++spFuncPtr) {
215  os << prefix << "...." << (*spFuncPtr)->toString() << std::endl;
216  }
217  }
218  return os.str();
219 }
220 
221 #if 0 // This fails to compile with icc
222 void Kernel::toFile(std::string fileName) const {
223  std::ofstream os(fileName.c_str());
224  boost::archive::text_oarchive oa(os);
225  oa << this;
226 }
227 #endif
228 
229 //
230 // Protected Member Functions
231 //
232 
233 void Kernel::setKernelParameter(unsigned int, double) const {
234  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel has no kernel parameters");
235 }
236 
237 void Kernel::setKernelParametersFromSpatialModel(double x, double y) const {
239  for (int ii = 0; funcIter != _spatialFunctionList.end(); ++funcIter, ++ii) {
240  this->setKernelParameter(ii, (*(*funcIter))(x, y));
241  }
242 }
243 
244 std::string Kernel::getPythonModule() const { return "lsst.afw.math"; }
245 } // namespace math
246 } // namespace afw
247 } // namespace lsst
Extent2I const getDimensions() const noexcept
Definition: Box.h:173
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:233
int getCtrX() const
Return x index of kernel&#39;s center.
Definition: Kernel.h:245
int getCtrY() const
Return y index of kernel&#39;s center.
Definition: Kernel.h:252
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
int getHeight() const noexcept
Definition: Box.h:175
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
Definition: Kernel.cc:233
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:316
T endl(T... args)
int y
Definition: SpanSet.cc:49
virtual std::shared_ptr< Function2< ReturnT > > clone() const =0
Return a pointer to a deep copy of this function.
void setXY0(lsst::geom::Point2I const origin)
Set the ImageBase&#39;s origin.
Definition: ImageBase.h:452
std::string prefix
Definition: SchemaMapper.cc:79
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:244
A Function taking two arguments.
Definition: Function.h:261
STL class.
Point2I const getMin() const noexcept
Definition: Box.h:143
T push_back(T... args)
STL class.
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
Definition: Kernel.h:371
A base class for image defects.
std::vector< SpatialFunctionPtr > getSpatialFunctionList() const
Return a list of clones of the spatial functions.
Definition: Kernel.cc:175
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:186
deltafunction_kernel_tag deltafunction_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:52
void computeKernelParametersFromSpatialModel(std::vector< double > &kernelParams, double x, double y) const
Compute the kernel parameters at a specified point.
Definition: Kernel.cc:153
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
T str(T... args)
int getWidth() const noexcept
Definition: Box.h:174
lsst::geom::Point2I getCtr() const
Return index of kernel&#39;s center.
Definition: Kernel.h:238
SpatialFunctionPtr getSpatialFunction(unsigned int index) const
Return a clone of the specified spatial function (one component of the spatial model) ...
Definition: Kernel.cc:162
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:93
table::Box2IKey bbox
Definition: Detector.cc:166
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
Definition: Kernel.cc:184
double x
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
Definition: Kernel.cc:237
int getWidth() const
Return the Kernel&#39;s width.
Definition: Kernel.h:228
lsst::geom::Extent2I const getDimensions() const
Return the Kernel&#39;s dimensions (width, height)
Definition: Kernel.h:216
unsigned int getNKernelParameters() const
Return the number of kernel parameters (0 if none)
Definition: Kernel.h:264
int getMinX() const noexcept
Definition: Box.h:144
void setSpatialParameters(const std::vector< std::vector< double >> params)
Set the parameters of all spatial functions.
Definition: Kernel.cc:128
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Extent< int, 2 > Extent2I
Definition: Extent.h:397
STL class.
Kernel has only one non-zero pixel.
Definition: traits.h:56
Citizen(const std::type_info &)
Definition: Citizen.cc:163
T begin(T... args)
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:192
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:487
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:314
T c_str(T... args)
Kernel()
Construct a null Kernel of size 0,0.
Definition: Kernel.cc:58
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)
Definition: Kernel.h:269
Reports invalid arguments.
Definition: Runtime.h:66
generic_kernel_tag generic_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:51
lsst::geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:374
An integer coordinate rectangle.
Definition: Box.h:54
int getMinY() const noexcept
Definition: Box.h:145
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:204
std::ostream * os
Definition: Schema.cc:746
Tags carrying information about Kernels Kernel with no special properties.
Definition: traits.h:53