LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
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:169
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
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
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