lsst.afw  14.0-22-gc76603d1b
SeparableKernel.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 <algorithm>
25 #include <iterator>
26 #include <sstream>
27 
28 #include "lsst/pex/exceptions.h"
29 #include "lsst/afw/math/Kernel.h"
30 
32 
33 namespace lsst {
34 namespace afw {
35 namespace math {
36 
38  : Kernel(),
39  _kernelColFunctionPtr(),
40  _kernelRowFunctionPtr(),
41  _localColList(0),
42  _localRowList(0),
43  _kernelX(0),
44  _kernelY(0),
45  _kernelRowCache(0),
46  _kernelColCache(0) {
47  _setKernelXY();
48 }
49 
50 SeparableKernel::SeparableKernel(int width, int height, KernelFunction const& kernelColFunction,
51  KernelFunction const& kernelRowFunction,
52  Kernel::SpatialFunction const& spatialFunction)
53  : Kernel(width, height, kernelColFunction.getNParameters() + kernelRowFunction.getNParameters(),
54  spatialFunction),
55  _kernelColFunctionPtr(kernelColFunction.clone()),
56  _kernelRowFunctionPtr(kernelRowFunction.clone()),
57  _localColList(width),
58  _localRowList(height),
59  _kernelX(width),
60  _kernelY(height),
61  _kernelRowCache(0),
62  _kernelColCache(0) {
63  _setKernelXY();
64 }
65 
66 SeparableKernel::SeparableKernel(int width, int height, KernelFunction const& kernelColFunction,
67  KernelFunction const& kernelRowFunction,
68  std::vector<Kernel::SpatialFunctionPtr> const& spatialFunctionList)
69  : Kernel(width, height, spatialFunctionList),
70  _kernelColFunctionPtr(kernelColFunction.clone()),
71  _kernelRowFunctionPtr(kernelRowFunction.clone()),
72  _localColList(width),
73  _localRowList(height),
74  _kernelX(width),
75  _kernelY(height),
76  _kernelRowCache(0),
77  _kernelColCache(0) {
78  if (kernelColFunction.getNParameters() + kernelRowFunction.getNParameters() !=
79  spatialFunctionList.size()) {
81  os << "kernelColFunction.getNParameters() + kernelRowFunction.getNParameters() = "
82  << kernelColFunction.getNParameters() << " + " << kernelRowFunction.getNParameters()
83  << " != " << spatialFunctionList.size() << " = "
84  << "spatialFunctionList.size()";
86  }
87 
88  _setKernelXY();
89 }
90 
93  if (this->isSpatiallyVarying()) {
94  retPtr.reset(new SeparableKernel(this->getWidth(), this->getHeight(), *(this->_kernelColFunctionPtr),
95  *(this->_kernelRowFunctionPtr), this->_spatialFunctionList));
96  } else {
97  retPtr.reset(new SeparableKernel(this->getWidth(), this->getHeight(), *(this->_kernelColFunctionPtr),
98  *(this->_kernelRowFunctionPtr)));
99  }
100  retPtr->setCtr(this->getCtr());
101  retPtr->computeCache(this->getCacheSize());
102  return retPtr;
103 }
104 
105 std::shared_ptr<Kernel> SeparableKernel::resized(int width, int height) const {
107  if (isSpatiallyVarying()) {
108  retPtr = std::make_shared<SeparableKernel>(width, height, *_kernelColFunctionPtr,
109  *_kernelRowFunctionPtr, _spatialFunctionList);
110  } else {
111  retPtr = std::make_shared<SeparableKernel>(width, height, *_kernelColFunctionPtr,
112  *_kernelRowFunctionPtr);
113  }
114  return retPtr;
115 }
116 
118  bool doNormalize, double x, double y) const {
119  if (static_cast<int>(colList.size()) != this->getWidth() ||
120  static_cast<int>(rowList.size()) != this->getHeight()) {
122  os << "colList.size(), rowList.size() = (" << colList.size() << ", " << rowList.size() << ") != ("
123  << this->getWidth() << ", " << this->getHeight() << ") = "
124  << "kernel dimensions";
126  }
127  if (this->isSpatiallyVarying()) {
129  }
130 
131  return basicComputeVectors(colList, rowList, doNormalize);
132 }
133 
135  return _kernelColFunctionPtr->clone();
136 }
137 
139  return _kernelRowFunctionPtr->clone();
140 }
141 
144  os << prefix << "SeparableKernel:" << std::endl;
145  os << prefix
146  << "..x (width) function: " << (_kernelColFunctionPtr ? _kernelColFunctionPtr->toString() : "None")
147  << std::endl;
148  os << prefix
149  << "..y (rows) function: " << (_kernelRowFunctionPtr ? _kernelRowFunctionPtr->toString() : "None")
150  << std::endl;
151  os << Kernel::toString(prefix + "\t");
152  return os.str();
153 }
154 
156  std::vector<double> allParams = _kernelColFunctionPtr->getParameters();
157  std::vector<double> yParams = _kernelRowFunctionPtr->getParameters();
158  std::copy(yParams.begin(), yParams.end(), std::back_inserter(allParams));
159  return allParams;
160 }
161 
162 //
163 // Protected Member Functions
164 //
165 
167  double imSum = basicComputeVectors(_localColList, _localRowList, doNormalize);
168 
169  for (int y = 0; y != image.getHeight(); ++y) {
171  for (std::vector<Pixel>::iterator colIter = _localColList.begin(); colIter != _localColList.end();
172  ++colIter, ++imPtr) {
173  *imPtr = (*colIter) * _localRowList[y];
174  }
175  }
176 
177  return imSum;
178 }
179 
180 void SeparableKernel::setKernelParameter(unsigned int ind, double value) const {
181  unsigned int const nColParams = _kernelColFunctionPtr->getNParameters();
182  if (ind < nColParams) {
183  _kernelColFunctionPtr->setParameter(ind, value);
184  } else {
185  _kernelRowFunctionPtr->setParameter(ind - nColParams, value);
186  }
187 }
188 
189 //
190 // Private Member Functions
191 //
192 
193 double SeparableKernel::basicComputeVectors(std::vector<Pixel>& colList, std::vector<Pixel>& rowList,
194  bool doNormalize) const {
195  double colSum = 0.0;
196  if (_kernelColCache.empty()) {
197  for (unsigned int i = 0; i != colList.size(); ++i) {
198  double colFuncValue = (*_kernelColFunctionPtr)(_kernelX[i]);
199  colList[i] = colFuncValue;
200  colSum += colFuncValue;
201  }
202  } else {
203  int const cacheSize = _kernelColCache.size();
204 
205  int const indx = this->getKernelParameter(0) * cacheSize;
206 
207  std::vector<double>& cachedValues = _kernelColCache.at(indx);
208  for (unsigned int i = 0; i != colList.size(); ++i) {
209  double colFuncValue = cachedValues[i];
210  colList[i] = colFuncValue;
211  colSum += colFuncValue;
212  }
213  }
214 
215  double rowSum = 0.0;
216  if (_kernelRowCache.empty()) {
217  for (unsigned int i = 0; i != rowList.size(); ++i) {
218  double rowFuncValue = (*_kernelRowFunctionPtr)(_kernelY[i]);
219  rowList[i] = rowFuncValue;
220  rowSum += rowFuncValue;
221  }
222  } else {
223  int const cacheSize = _kernelRowCache.size();
224 
225  int const indx = this->getKernelParameter(1) * cacheSize;
226 
227  std::vector<double>& cachedValues = _kernelRowCache.at(indx);
228  for (unsigned int i = 0; i != rowList.size(); ++i) {
229  double rowFuncValue = cachedValues[i];
230  rowList[i] = rowFuncValue;
231  rowSum += rowFuncValue;
232 
233 #if 0
234  if (indx == cacheSize/2) {
235  if (::fabs(rowFuncValue - (*_kernelRowFunctionPtr)(_kernelX[i])) > 1e-2) {
236  std::cout << indx << " " << i << " "
237  << rowFuncValue << " "
238  << (*_kernelRowFunctionPtr)(_kernelX[i])
239  << std::endl;
240  }
241  }
242 #endif
243  }
244  }
245 
246  double imSum = colSum * rowSum;
247  if (doNormalize) {
248  if ((colSum == 0) || (rowSum == 0)) {
249  throw LSST_EXCEPT(pexExcept::OverflowError, "Cannot normalize; kernel sum is 0");
250  }
251  for (std::vector<Pixel>::iterator colIter = colList.begin(); colIter != colList.end(); ++colIter) {
252  *colIter /= colSum;
253  }
254 
255  for (std::vector<Pixel>::iterator rowIter = rowList.begin(); rowIter != rowList.end(); ++rowIter) {
256  *rowIter /= rowSum;
257  }
258  imSum = 1.0;
259  }
260  return imSum;
261 }
262 
263 namespace {
267 void _computeCache(int const cacheSize, std::vector<double> const& x,
269  if (cacheSize <= 0) {
270  kernelCache->erase(kernelCache->begin(), kernelCache->end());
271  return;
272  }
273 
274  if (kernelCache[0].size() != x.size()) { // invalid
275  kernelCache->erase(kernelCache->begin(), kernelCache->end());
276  }
277 
278  int const old_cacheSize = kernelCache->size();
279 
280  if (cacheSize == old_cacheSize) {
281  return; // nothing to do
282  }
283 
284  if (cacheSize < old_cacheSize) {
285  kernelCache->erase(kernelCache->begin() + cacheSize, kernelCache->end());
286  } else {
287  kernelCache->resize(cacheSize);
288  for (int i = old_cacheSize; i != cacheSize; ++i) {
289  (*kernelCache)[i].resize(x.size());
290  }
291  }
292  //
293  // Actually fill the cache
294  //
295  for (int i = 0; i != cacheSize; ++i) {
296  func->setParameter(0, (i + 0.5) / static_cast<double>(cacheSize));
297  for (unsigned int j = 0; j != x.size(); ++j) {
298  (*kernelCache)[i][j] = (*func)(x[j]);
299  }
300  }
301 }
302 }
303 
304 void SeparableKernel::computeCache(int const cacheSize) {
306 
307  func = getKernelColFunction();
308  _computeCache(cacheSize, _kernelY, func, &_kernelColCache);
309 
310  func = getKernelRowFunction();
311  _computeCache(cacheSize, _kernelX, func, &_kernelRowCache);
312 }
313 
314 int SeparableKernel::getCacheSize() const { return _kernelColCache.size(); };
315 }
316 }
317 } // lsst::afw::math
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:239
T empty(T... args)
T copy(T... args)
virtual std::shared_ptr< Kernel > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:299
T endl(T... args)
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:137
lsst::afw::geom::Point2I getCtr() const
Return index of kernel&#39;s center.
Definition: Kernel.h:244
virtual double getKernelParameter(unsigned int i) const
Return a particular Kernel Parameter (no bounds checking).
Definition: Kernel.h:994
virtual int getCacheSize() const
Get the current cache size (0 if none)
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
T end(T... args)
A Function taking two arguments.
Definition: Function.h:266
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:402
STL class.
T at(T... args)
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
Definition: Kernel.h:378
double computeVectors(std::vector< Pixel > &colList, std::vector< Pixel > &rowList, bool doNormalize, double x=0.0, double y=0.0) const
Compute the column and row arrays in place, where kernel(col, row) = colList(col) * rowList(row) ...
A base class for image defects.
KernelFunctionPtr getKernelRowFunction() const
Get a deep copy of the row kernel function.
KernelFunctionPtr getKernelColFunction() const
Get a deep copy of the col kernel function.
A Function taking one argument.
Definition: Function.h:207
SeparableKernel()
Construct an empty spatially invariant SeparableKernel of size 0x0.
T str(T... args)
T reset(T... args)
double x
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
Definition: Kernel.cc:230
int getWidth() const
Return the Kernel&#39;s width.
Definition: Kernel.h:234
T size(T... args)
#define LSST_EXCEPT(type,...)
virtual void computeCache(int const cacheSize)
Compute a cache of Kernel values, if desired.
STL class.
T begin(T... args)
T back_inserter(T... args)
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:494
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:123
unsigned int getNParameters() const
Return the number of function parameters.
Definition: Function.h:110
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:197
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.