LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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"
40 
42 
43 namespace lsst {
44 namespace afw {
45 
48 
49 namespace math {
50 
54 
55 //
56 // Constructors
57 //
58 Kernel::Kernel() : _spatialFunctionList(), _width(0), _height(0), _ctrX(0), _ctrY(0), _nKernelParams(0) {}
59 
60 Kernel::Kernel(int width, int height, unsigned int nKernelParams, SpatialFunction const &spatialFunction)
62  _width(width),
63  _height(height),
64  _ctrX((width - 1) / 2),
65  _ctrY((height - 1) / 2),
66  _nKernelParams(nKernelParams) {
67  if ((width < 1) || (height < 1)) {
69  os << "kernel height = " << height << " and/or width = " << width << " < 1";
71  }
72  if (dynamic_cast<const NullSpatialFunction *>(&spatialFunction)) {
73  // spatialFunction is not really present
74  } else {
75  if (nKernelParams == 0) {
76  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel function has no parameters");
77  }
78  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
79  SpatialFunctionPtr spatialFunctionCopy = spatialFunction.clone();
80  this->_spatialFunctionList.push_back(spatialFunctionCopy);
81  }
82  }
83 }
84 
85 double Kernel::computeImage(image::Image<Pixel> &image, bool doNormalize, double x, double y) const {
86  if (image.getDimensions() != this->getDimensions()) {
88  os << "image dimensions = ( " << image.getWidth() << ", " << image.getHeight() << ") != ("
89  << this->getWidth() << ", " << this->getHeight() << ") = kernel dimensions";
91  }
92  image.setXY0(-_ctrX, -_ctrY);
93  if (this->isSpatiallyVarying()) {
95  }
96  return doComputeImage(image, doNormalize);
97 }
98 
99 Kernel::Kernel(int width, int height, std::vector<SpatialFunctionPtr> spatialFunctionList)
100  : _width(width),
101  _height(height),
102  _ctrX(width / 2),
103  _ctrY(height / 2),
104  _nKernelParams(spatialFunctionList.size()) {
105  if ((width < 1) || (height < 1)) {
107  os << "kernel height = " << height << " and/or width = " << width << " < 1";
109  }
110  for (unsigned int ii = 0; ii < spatialFunctionList.size(); ++ii) {
111  SpatialFunctionPtr spatialFunctionCopy = spatialFunctionList[ii]->clone();
112  this->_spatialFunctionList.push_back(spatialFunctionCopy);
113  }
114 }
115 
116 //
117 // Public Member Functions
118 //
120  // Check params size before changing anything
121  unsigned int nKernelParams = this->getNKernelParameters();
122  if (params.size() != nKernelParams) {
123  throw LSST_EXCEPT(
125  (boost::format("params has %d entries instead of %d") % params.size() % nKernelParams).str());
126  }
127  unsigned int nSpatialParams = this->getNSpatialParameters();
128  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
129  if (params[ii].size() != nSpatialParams) {
131  (boost::format("params[%d] has %d entries instead of %d") % ii %
132  params[ii].size() % nSpatialParams)
133  .str());
134  }
135  }
136  // Set parameters
137  if (nSpatialParams > 0) {
138  for (unsigned int ii = 0; ii < nKernelParams; ++ii) {
139  this->_spatialFunctionList[ii]->setParameters(params[ii]);
140  }
141  }
142 }
143 
145  double y) const {
146  std::vector<double>::iterator paramIter = kernelParams.begin();
148  for (; funcIter != _spatialFunctionList.end(); ++funcIter, ++paramIter) {
149  *paramIter = (*(*funcIter))(x, y);
150  }
151 }
152 
154  if (index >= _spatialFunctionList.size()) {
155  if (!this->isSpatiallyVarying()) {
156  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "kernel is not spatially varying");
157  } else {
158  std::ostringstream errStream;
159  errStream << "index = " << index << "; must be < , " << _spatialFunctionList.size();
161  }
162  }
163  return _spatialFunctionList[index]->clone();
164 }
165 
167  std::vector<SpatialFunctionPtr> spFuncCopyList;
169  spFuncIter != _spatialFunctionList.end(); ++spFuncIter) {
170  spFuncCopyList.push_back((**spFuncIter).clone());
171  }
172  return spFuncCopyList;
173 }
174 
176 
178  return lsst::geom::Box2I(
181 }
182 
184  if ((bbox.getWidth() < getWidth()) || ((bbox.getHeight() < getHeight()))) {
186  os << "bbox dimensions = " << bbox.getDimensions() << " < (" << getWidth() << ", " << getHeight()
187  << ") in one or both dimensions";
189  }
190  return lsst::geom::Box2I(
191  lsst::geom::Point2I(bbox.getMinX() + getCtrX(), bbox.getMinY() + getCtrY()),
192  lsst::geom::Extent2I(bbox.getWidth() + 1 - getWidth(), bbox.getHeight() + 1 - getHeight()));
193 }
194 
197  os << prefix << "Kernel:" << std::endl;
198  os << prefix << "..height, width: " << _height << ", " << _width << std::endl;
199  os << prefix << "..ctr (X, Y): " << _ctrX << ", " << _ctrY << std::endl;
200  os << prefix << "..nKernelParams: " << _nKernelParams << std::endl;
201  os << prefix << "..isSpatiallyVarying: " << (this->isSpatiallyVarying() ? "True" : "False") << std::endl;
202  if (this->isSpatiallyVarying()) {
203  os << prefix << "..spatialFunctions:" << std::endl;
205  spFuncPtr != _spatialFunctionList.end(); ++spFuncPtr) {
206  os << prefix << "...." << (*spFuncPtr)->toString() << std::endl;
207  }
208  }
209  return os.str();
210 }
211 
212 #if 0 // This fails to compile with icc
213 void Kernel::toFile(std::string fileName) const {
214  std::ofstream os(fileName.c_str());
215  boost::archive::text_oarchive oa(os);
216  oa << this;
217 }
218 #endif
219 
220 //
221 // Protected Member Functions
222 //
223 
224 void Kernel::setKernelParameter(unsigned int, double) const {
225  throw LSST_EXCEPT(pexExcept::InvalidParameterError, "Kernel has no kernel parameters");
226 }
227 
228 void Kernel::setKernelParametersFromSpatialModel(double x, double y) const {
230  for (int ii = 0; funcIter != _spatialFunctionList.end(); ++funcIter, ++ii) {
231  this->setKernelParameter(ii, (*(*funcIter))(x, y));
232  }
233 }
234 
235 std::string Kernel::getPythonModule() const { return "lsst.afw.math"; }
236 } // namespace math
237 } // namespace afw
238 } // namespace lsst
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
Extent2I const getDimensions() const noexcept
Definition: Box.h:186
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:230
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
int getCtrX() const
Return x index of kernel&#39;s center.
Definition: Kernel.h:244
int getCtrY() const
Return y index of kernel&#39;s center.
Definition: Kernel.h:255
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:188
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
Definition: Kernel.cc:224
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:335
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:471
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:235
A Function taking two arguments.
Definition: Function.h:259
STL class.
Point2I const getMin() const noexcept
Definition: Box.h:156
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:380
A base class for image defects.
std::vector< SpatialFunctionPtr > getSpatialFunctionList() const
Return a list of clones of the spatial functions.
Definition: Kernel.cc:166
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:177
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:144
T str(T... args)
int getWidth() const noexcept
Definition: Box.h:187
lsst::geom::Point2I getCtr() const
Return index of kernel&#39;s center.
Definition: Kernel.h:235
SpatialFunctionPtr getSpatialFunction(unsigned int index) const
Return a clone of the specified spatial function (one component of the spatial model) ...
Definition: Kernel.cc:153
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:85
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
Definition: Kernel.cc:175
double x
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
Definition: Kernel.cc:228
int getWidth() const
Return the Kernel&#39;s width.
Definition: Kernel.h:225
lsst::geom::Extent2I const getDimensions() const
Return the Kernel&#39;s dimensions (width, height)
Definition: Kernel.h:213
unsigned int getNKernelParameters() const
Return the number of kernel parameters (0 if none)
Definition: Kernel.h:269
int getMinX() const noexcept
Definition: Box.h:157
void setSpatialParameters(const std::vector< std::vector< double >> params)
Set the parameters of all spatial functions.
Definition: Kernel.cc:119
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
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:183
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:496
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:333
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:274
Reports invalid arguments.
Definition: Runtime.h:66
std::string prefix
Definition: SchemaMapper.cc:79
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:393
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
std::ostream * os
Definition: Schema.cc:746
An integer coordinate rectangle.
Definition: Box.h:55
int getMinY() const noexcept
Definition: Box.h:158
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:195
Tags carrying information about Kernels Kernel with no special properties.
Definition: traits.h:53