LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
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)
61  : _spatialFunctionList(),
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(
180  lsst::geom::Extent2I(bbox.getDimensions() + getDimensions() - lsst::geom::Extent2I(1, 1)));
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(
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
double x
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::ostream * os
Definition: Schema.cc:746
std::string prefix
Definition: SchemaMapper.cc:79
int y
Definition: SpanSet.cc:49
T begin(T... args)
T c_str(T... args)
A Function taking two arguments.
Definition: Function.h:259
virtual std::shared_ptr< Function2< ReturnT > > clone() const =0
Return a pointer to a deep copy of this function.
lsst::geom::Extent2I const getDimensions() const
Return the Kernel's dimensions (width, height)
Definition: Kernel.h:213
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:450
int getHeight() const
Return the Kernel's height.
Definition: Kernel.h:230
lsst::geom::Point2I getCtr() const
Return index of kernel's center.
Definition: Kernel.h:235
unsigned int getNKernelParameters() const
Return the number of kernel parameters (0 if none)
Definition: Kernel.h:247
int getNSpatialParameters() const
Return the number of spatial parameters (0 if not spatially varying)
Definition: Kernel.h:252
std::vector< SpatialFunctionPtr > getSpatialFunctionList() const
Return a list of clones of the spatial functions.
Definition: Kernel.cc:166
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
Definition: Kernel.cc:175
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:195
void computeKernelParametersFromSpatialModel(std::vector< double > &kernelParams, double x, double y) const
Compute the kernel parameters at a specified point.
Definition: Kernel.cc:144
SpatialFunctionPtr getSpatialFunction(unsigned int index) const
Return a clone of the specified spatial function (one component of the spatial model)
Definition: Kernel.cc:153
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
int getWidth() const
Return the Kernel's width.
Definition: Kernel.h:225
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
Definition: Kernel.h:334
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
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
Definition: Kernel.cc:224
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
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 double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const =0
Low-level version of computeImage.
void setSpatialParameters(const std::vector< std::vector< double >> params)
Set the parameters of all spatial functions.
Definition: Kernel.cc:119
Kernel()
Construct a null Kernel of size 0,0.
Definition: Kernel.cc:58
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
Definition: Kernel.cc:228
a class used in function calls to indicate that no Function2 is being provided
Definition: Function.h:458
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
An integer coordinate rectangle.
Definition: Box.h:55
Reports invalid arguments.
Definition: Runtime.h:66
T endl(T... args)
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
deltafunction_kernel_tag deltafunction_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:52
generic_kernel_tag generic_kernel_tag_
Used as default value in argument lists.
Definition: Kernel.cc:51
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
T push_back(T... args)
T size(T... args)
T str(T... args)
Kernel has only one non-zero pixel.
Definition: traits.h:56
Tags carrying information about Kernels Kernel with no special properties.
Definition: traits.h:53