lsst.afw  13.0-100-ge0459d59f+5
AnalyticKernel.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 <sstream>
25 
26 #include "lsst/pex/exceptions.h"
27 #include "lsst/afw/math/Kernel.h"
29 
30 namespace pexExcept = lsst::pex::exceptions;
31 
32 namespace lsst {
33 namespace afw {
34 namespace math {
35 
36 AnalyticKernel::AnalyticKernel() : Kernel(), _kernelFunctionPtr() {}
37 
39  Kernel::SpatialFunction const &spatialFunction)
40  : Kernel(width, height, kernelFunction.getNParameters(), spatialFunction),
41  _kernelFunctionPtr(kernelFunction.clone()) {}
42 
44  std::vector<Kernel::SpatialFunctionPtr> const &spatialFunctionList)
45  : Kernel(width, height, spatialFunctionList), _kernelFunctionPtr(kernelFunction.clone()) {
46  if (kernelFunction.getNParameters() != spatialFunctionList.size()) {
47  std::ostringstream os;
48  os << "kernelFunction.getNParameters() = " << kernelFunction.getNParameters()
49  << " != " << spatialFunctionList.size() << " = "
50  << "spatialFunctionList.size()";
51  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
52  }
53 }
54 
55 std::shared_ptr<Kernel> AnalyticKernel::clone() const {
56  std::shared_ptr<Kernel> retPtr;
57  if (this->isSpatiallyVarying()) {
58  retPtr.reset(new AnalyticKernel(this->getWidth(), this->getHeight(), *(this->_kernelFunctionPtr),
59  this->_spatialFunctionList));
60  } else {
61  retPtr.reset(new AnalyticKernel(this->getWidth(), this->getHeight(), *(this->_kernelFunctionPtr)));
62  }
63  retPtr->setCtr(this->getCtr());
64  return retPtr;
65 }
66 
67 std::shared_ptr<Kernel> AnalyticKernel::resized(int width, int height) const {
68  std::shared_ptr<Kernel> retPtr;
69  if (isSpatiallyVarying()) {
70  retPtr = std::make_shared<AnalyticKernel>(width, height, *_kernelFunctionPtr, _spatialFunctionList);
71  } else {
72  retPtr = std::make_shared<AnalyticKernel>(width, height, *_kernelFunctionPtr);
73  }
74  return retPtr;
75 }
76 
77 double AnalyticKernel::computeImage(image::Image<Pixel> &image, bool doNormalize, double x, double y) const {
78  geom::Extent2I llBorder = (image.getDimensions() - getDimensions()) / 2;
79  image.setXY0(geom::Point2I(-geom::Extent2I(getCtr() + llBorder)));
80  if (this->isSpatiallyVarying()) {
82  }
83  return doComputeImage(image, doNormalize);
84 }
85 
87  return _kernelFunctionPtr->clone();
88 }
89 
90 std::string AnalyticKernel::toString(std::string const &prefix) const {
91  std::ostringstream os;
92  os << prefix << "AnalyticKernel:" << std::endl;
93  os << prefix << "..function: " << (_kernelFunctionPtr ? _kernelFunctionPtr->toString() : "None")
94  << std::endl;
95  os << Kernel::toString(prefix + "\t");
96  return os.str();
97 }
98 
99 std::vector<double> AnalyticKernel::getKernelParameters() const {
100  return _kernelFunctionPtr->getParameters();
101 }
102 
103 //
104 // Protected Member Functions
105 //
106 double AnalyticKernel::doComputeImage(image::Image<Pixel> &image, bool doNormalize) const {
107  double imSum = 0;
108  for (int y = 0; y != image.getHeight(); ++y) {
109  double const fy = image.indexToPosition(y, image::Y);
111  for (int x = 0; x != image.getWidth(); ++x, ++ptr) {
112  double const fx = image.indexToPosition(x, image::X);
113  Pixel const pixelVal = (*_kernelFunctionPtr)(fx, fy);
114  *ptr = pixelVal;
115  imSum += pixelVal;
116  }
117  }
118 
119  if (doNormalize && (imSum != 1)) {
120  if (imSum == 0) {
121  throw LSST_EXCEPT(pexExcept::OverflowError, "Cannot normalize; kernel sum is 0");
122  }
123  image /= imSum;
124  imSum = 1;
125  }
126 
127  return imSum;
128 }
129 
130 void AnalyticKernel::setKernelParameter(unsigned int ind, double value) const {
131  _kernelFunctionPtr->setParameter(ind, value);
132 }
133 
134 // ------ Persistence ---------------------------------------------------------------------------------------
135 
136 namespace {
137 
138 struct AnalyticKernelPersistenceHelper : public Kernel::PersistenceHelper {
139  table::Key<int> kernelFunction;
140 
141  explicit AnalyticKernelPersistenceHelper(int nSpatialFunctions)
142  : Kernel::PersistenceHelper(nSpatialFunctions),
143  kernelFunction(schema.addField<int>(
144  "kernelfunction", "archive ID for analytic function used to produce kernel images")) {}
145 
146  explicit AnalyticKernelPersistenceHelper(table::Schema const &schema_)
147  : Kernel::PersistenceHelper(schema_), kernelFunction(schema["kernelfunction"]) {}
148 };
149 
150 } // anonymous
151 
153 public:
154  virtual std::shared_ptr<afw::table::io::Persistable> read(InputArchive const &archive,
155  CatalogVector const &catalogs) const {
156  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
157  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
158  AnalyticKernelPersistenceHelper const keys(catalogs.front().getSchema());
159  afw::table::BaseRecord const &record = catalogs.front().front();
160  std::shared_ptr<AnalyticKernel::KernelFunction> kernelFunction =
161  archive.get<AnalyticKernel::KernelFunction>(record.get(keys.kernelFunction));
162  std::shared_ptr<AnalyticKernel> result;
163  if (keys.spatialFunctions.isValid()) {
164  result = std::make_shared<AnalyticKernel>(record.get(keys.dimensions.getX()),
165  record.get(keys.dimensions.getY()), *kernelFunction,
166  keys.readSpatialFunctions(archive, record));
167  } else {
168  result = std::make_shared<AnalyticKernel>(record.get(keys.dimensions.getX()),
169  record.get(keys.dimensions.getY()), *kernelFunction);
170  }
171  result->setCtr(record.get(keys.center));
172  return result;
173  }
174 
175  explicit Factory(std::string const &name) : afw::table::io::PersistableFactory(name) {}
176 };
177 
178 namespace {
179 
180 std::string getAnalyticKernelPersistenceName() { return "AnalyticKernel"; }
181 
182 AnalyticKernel::Factory registration(getAnalyticKernelPersistenceName());
183 
184 } // anonymous
185 
186 std::string AnalyticKernel::getPersistenceName() const { return getAnalyticKernelPersistenceName(); }
187 
189  AnalyticKernelPersistenceHelper const keys(_spatialFunctionList.size());
190  std::shared_ptr<afw::table::BaseRecord> record = keys.write(handle, *this);
191  record->set(keys.kernelFunction, handle.put(_kernelFunctionPtr.get()));
192 }
193 }
194 }
195 } // namespace math
int getHeight() const
Return the Kernel&#39;s height.
Definition: Kernel.h:239
void setXY0(geom::Point2I const origin)
Set the ImageBase&#39;s origin.
Definition: ImageBase.h:435
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
std::shared_ptr< Persistable > get(int id) const
Load the Persistable with the given ID and return it.
int put(Persistable const *obj, bool permissive=false)
Save a nested Persistable to the same archive.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
An object passed to Persistable::write to allow it to persist itself.
afw::table::Schema schema
Definition: GaussianPsf.cc:43
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:299
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:214
_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 std::shared_ptr< afw::table::io::Persistable > read(InputArchive const &archive, CatalogVector const &catalogs) const
Construct a new object from the given InputArchive and vector of catalogs.
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
A Function taking two arguments.
Definition: Function.h:266
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:402
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
Definition: Kernel.h:378
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:357
KernelFunctionPtr _kernelFunctionPtr
Definition: Kernel.h:681
A base class for image defects.
Definition: cameraGeom.dox:3
virtual std::shared_ptr< Kernel > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
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
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
Base class for all records.
Definition: BaseRecord.h:29
table::Key< int > kernelFunction
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
AnalyticKernel()
Construct an empty spatially invariant AnalyticKernel of size 0x0.
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:494
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:297
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.
Kernel()
Construct a null Kernel of size 0,0.
Definition: Kernel.cc:53
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
std::shared_ptr< lsst::afw::math::Function2< Pixel > > KernelFunctionPtr
Definition: Kernel.h:591
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
geom::Extent2I const getDimensions() const
Return the Kernel&#39;s dimensions (width, height)
Definition: Kernel.h:222
double indexToPosition(double ind, lsst::afw::image::xOrY const xy) const
Convert image index to image position.
Definition: ImageBase.h:350
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:197
virtual KernelFunctionPtr getKernelFunction() const
Get a deep copy of the kernel function.