lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
31 namespace pexExcept = lsst::pex::exceptions;
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()) {
80  std::ostringstream os;
81  os << "kernelColFunction.getNParameters() + kernelRowFunction.getNParameters() = "
82  << kernelColFunction.getNParameters() << " + " << kernelRowFunction.getNParameters()
83  << " != " << spatialFunctionList.size() << " = "
84  << "spatialFunctionList.size()";
85  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
86  }
87 
88  _setKernelXY();
89 }
90 
91 std::shared_ptr<Kernel> SeparableKernel::clone() const {
92  std::shared_ptr<Kernel> retPtr;
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 double SeparableKernel::computeVectors(std::vector<Pixel>& colList, std::vector<Pixel>& rowList,
106  bool doNormalize, double x, double y) const {
107  if (static_cast<int>(colList.size()) != this->getWidth() ||
108  static_cast<int>(rowList.size()) != this->getHeight()) {
109  std::ostringstream os;
110  os << "colList.size(), rowList.size() = (" << colList.size() << ", " << rowList.size() << ") != ("
111  << this->getWidth() << ", " << this->getHeight() << ") = "
112  << "kernel dimensions";
113  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
114  }
115  if (this->isSpatiallyVarying()) {
117  }
118 
119  return basicComputeVectors(colList, rowList, doNormalize);
120 }
121 
123  return _kernelColFunctionPtr->clone();
124 }
125 
127  return _kernelRowFunctionPtr->clone();
128 }
129 
130 std::string SeparableKernel::toString(std::string const& prefix) const {
131  std::ostringstream os;
132  os << prefix << "SeparableKernel:" << std::endl;
133  os << prefix
134  << "..x (width) function: " << (_kernelColFunctionPtr ? _kernelColFunctionPtr->toString() : "None")
135  << std::endl;
136  os << prefix
137  << "..y (rows) function: " << (_kernelRowFunctionPtr ? _kernelRowFunctionPtr->toString() : "None")
138  << std::endl;
139  os << Kernel::toString(prefix + "\t");
140  return os.str();
141 }
142 
143 std::vector<double> SeparableKernel::getKernelParameters() const {
144  std::vector<double> allParams = _kernelColFunctionPtr->getParameters();
145  std::vector<double> yParams = _kernelRowFunctionPtr->getParameters();
146  std::copy(yParams.begin(), yParams.end(), std::back_inserter(allParams));
147  return allParams;
148 }
149 
150 //
151 // Protected Member Functions
152 //
153 
155  double imSum = basicComputeVectors(_localColList, _localRowList, doNormalize);
156 
157  for (int y = 0; y != image.getHeight(); ++y) {
159  for (std::vector<Pixel>::iterator colIter = _localColList.begin(); colIter != _localColList.end();
160  ++colIter, ++imPtr) {
161  *imPtr = (*colIter) * _localRowList[y];
162  }
163  }
164 
165  return imSum;
166 }
167 
168 void SeparableKernel::setKernelParameter(unsigned int ind, double value) const {
169  unsigned int const nColParams = _kernelColFunctionPtr->getNParameters();
170  if (ind < nColParams) {
171  _kernelColFunctionPtr->setParameter(ind, value);
172  } else {
173  _kernelRowFunctionPtr->setParameter(ind - nColParams, value);
174  }
175 }
176 
177 //
178 // Private Member Functions
179 //
180 
181 double SeparableKernel::basicComputeVectors(std::vector<Pixel>& colList, std::vector<Pixel>& rowList,
182  bool doNormalize) const {
183  double colSum = 0.0;
184  if (_kernelColCache.empty()) {
185  for (unsigned int i = 0; i != colList.size(); ++i) {
186  double colFuncValue = (*_kernelColFunctionPtr)(_kernelX[i]);
187  colList[i] = colFuncValue;
188  colSum += colFuncValue;
189  }
190  } else {
191  int const cacheSize = _kernelColCache.size();
192 
193  int const indx = this->getKernelParameter(0) * cacheSize;
194 
195  std::vector<double>& cachedValues = _kernelColCache.at(indx);
196  for (unsigned int i = 0; i != colList.size(); ++i) {
197  double colFuncValue = cachedValues[i];
198  colList[i] = colFuncValue;
199  colSum += colFuncValue;
200  }
201  }
202 
203  double rowSum = 0.0;
204  if (_kernelRowCache.empty()) {
205  for (unsigned int i = 0; i != rowList.size(); ++i) {
206  double rowFuncValue = (*_kernelRowFunctionPtr)(_kernelY[i]);
207  rowList[i] = rowFuncValue;
208  rowSum += rowFuncValue;
209  }
210  } else {
211  int const cacheSize = _kernelRowCache.size();
212 
213  int const indx = this->getKernelParameter(1) * cacheSize;
214 
215  std::vector<double>& cachedValues = _kernelRowCache.at(indx);
216  for (unsigned int i = 0; i != rowList.size(); ++i) {
217  double rowFuncValue = cachedValues[i];
218  rowList[i] = rowFuncValue;
219  rowSum += rowFuncValue;
220 
221 #if 0
222  if (indx == cacheSize/2) {
223  if (::fabs(rowFuncValue - (*_kernelRowFunctionPtr)(_kernelX[i])) > 1e-2) {
224  std::cout << indx << " " << i << " "
225  << rowFuncValue << " "
226  << (*_kernelRowFunctionPtr)(_kernelX[i])
227  << std::endl;
228  }
229  }
230 #endif
231  }
232  }
233 
234  double imSum = colSum * rowSum;
235  if (doNormalize) {
236  if ((colSum == 0) || (rowSum == 0)) {
237  throw LSST_EXCEPT(pexExcept::OverflowError, "Cannot normalize; kernel sum is 0");
238  }
239  for (std::vector<Pixel>::iterator colIter = colList.begin(); colIter != colList.end(); ++colIter) {
240  *colIter /= colSum;
241  }
242 
243  for (std::vector<Pixel>::iterator rowIter = rowList.begin(); rowIter != rowList.end(); ++rowIter) {
244  *rowIter /= rowSum;
245  }
246  imSum = 1.0;
247  }
248  return imSum;
249 }
250 
251 namespace {
255 void _computeCache(int const cacheSize, std::vector<double> const& x,
256  SeparableKernel::KernelFunctionPtr& func, std::vector<std::vector<double> >* kernelCache) {
257  if (cacheSize <= 0) {
258  kernelCache->erase(kernelCache->begin(), kernelCache->end());
259  return;
260  }
261 
262  if (kernelCache[0].size() != x.size()) { // invalid
263  kernelCache->erase(kernelCache->begin(), kernelCache->end());
264  }
265 
266  int const old_cacheSize = kernelCache->size();
267 
268  if (cacheSize == old_cacheSize) {
269  return; // nothing to do
270  }
271 
272  if (cacheSize < old_cacheSize) {
273  kernelCache->erase(kernelCache->begin() + cacheSize, kernelCache->end());
274  } else {
275  kernelCache->resize(cacheSize);
276  for (int i = old_cacheSize; i != cacheSize; ++i) {
277  (*kernelCache)[i].resize(x.size());
278  }
279  }
280  //
281  // Actually fill the cache
282  //
283  for (int i = 0; i != cacheSize; ++i) {
284  func->setParameter(0, (i + 0.5) / static_cast<double>(cacheSize));
285  for (unsigned int j = 0; j != x.size(); ++j) {
286  (*kernelCache)[i][j] = (*func)(x[j]);
287  }
288  }
289 }
290 }
291 
292 void SeparableKernel::computeCache(int const cacheSize) {
294 
295  func = getKernelColFunction();
296  _computeCache(cacheSize, _kernelY, func, &_kernelColCache);
297 
298  func = getKernelRowFunction();
299  _computeCache(cacheSize, _kernelX, func, &_kernelRowCache);
300 }
301 
302 int SeparableKernel::getCacheSize() const { return _kernelColCache.size(); };
303 }
304 }
305 } // lsst::afw::math
virtual double getKernelParameter(unsigned int i) const
Return a particular Kernel Parameter (no bounds checking).
Definition: Kernel.h:972
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
std::shared_ptr< KernelFunction > KernelFunctionPtr
Definition: Kernel.h:910
virtual int getCacheSize() const
Get the current cache size (0 if none)
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) ...
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: Image.h:147
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:197
A Function taking two arguments.
Definition: Function.h:266
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:227
int getWidth() const
Return the Kernel&#39;s width.
Definition: Kernel.h:222
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
Definition: Kernel.cc:230
A Function taking one argument.
Definition: Function.h:207
SeparableKernel()
Construct an empty spatially invariant SeparableKernel of size 0x0.
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:309
double x
lsst::afw::geom::Point2I getCtr() const
Return index of kernel&#39;s center.
Definition: Kernel.h:232
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
virtual void computeCache(int const cacheSize)
Compute a cache of Kernel values, if desired.
unsigned int getNParameters() const
Return the number of function parameters.
Definition: Function.h:110
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:482
afw::table::Key< afw::table::Array< ImagePixelT > > image
KernelFunctionPtr getKernelRowFunction() const
Get a deep copy of the row kernel function.
KernelFunctionPtr getKernelColFunction() const
Get a deep copy of the col kernel function.
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: Image.h:412
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:123
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
Definition: Kernel.h:366