LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
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 namespace afwGeom = lsst::afw::geom;
32 namespace afwMath = lsst::afw::math;
33 namespace afwImage = lsst::afw::image;
34 
36 :
37  Kernel(),
38  _kernelFunctionPtr()
39 {}
40 
42  int width,
43  int height,
45  Kernel::SpatialFunction const &spatialFunction
46 ) :
47  Kernel(width, height, kernelFunction.getNParameters(), spatialFunction),
48  _kernelFunctionPtr(kernelFunction.clone())
49 {}
50 
52  int width,
53  int height,
55  std::vector<Kernel::SpatialFunctionPtr> const &spatialFunctionList
56 ) :
57  Kernel(width, height, spatialFunctionList),
58  _kernelFunctionPtr(kernelFunction.clone())
59 {
60  if (kernelFunction.getNParameters() != spatialFunctionList.size()) {
61  std::ostringstream os;
62  os << "kernelFunction.getNParameters() = " << kernelFunction.getNParameters()
63  << " != " << spatialFunctionList.size() << " = " << "spatialFunctionList.size()";
64  throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
65  }
66 }
67 
68 PTR(afwMath::Kernel) afwMath::AnalyticKernel::clone() const {
69  PTR(afwMath::Kernel) retPtr;
70  if (this->isSpatiallyVarying()) {
71  retPtr.reset(new afwMath::AnalyticKernel(this->getWidth(), this->getHeight(),
72  *(this->_kernelFunctionPtr), this->_spatialFunctionList));
73  } else {
74  retPtr.reset(new afwMath::AnalyticKernel(this->getWidth(), this->getHeight(),
75  *(this->_kernelFunctionPtr)));
76  }
77  retPtr->setCtr(this->getCtr());
78  return retPtr;
79 }
80 
83  bool doNormalize,
84  double x,
85  double y
86 ) const {
87  afwGeom::Extent2I llBorder = (image.getDimensions() - getDimensions()) / 2;
88  image.setXY0(afwGeom::Point2I(-afwGeom::Extent2I(getCtr()+ llBorder)));
89  if (this->isSpatiallyVarying()) {
90  this->setKernelParametersFromSpatialModel(x, y);
91  }
92  return doComputeImage(image, doNormalize);
93 }
94 
96 ) const {
97  return _kernelFunctionPtr->clone();
98 }
99 
100 std::string afwMath::AnalyticKernel::toString(std::string const& prefix) const {
101  std::ostringstream os;
102  os << prefix << "AnalyticKernel:" << std::endl;
103  os << prefix << "..function: " << (_kernelFunctionPtr ? _kernelFunctionPtr->toString() : "None")
104  << std::endl;
105  os << Kernel::toString(prefix + "\t");
106  return os.str();
107 }
108 
109 std::vector<double> afwMath::AnalyticKernel::getKernelParameters() const {
110  return _kernelFunctionPtr->getParameters();
111 }
112 
113 //
114 // Protected Member Functions
115 //
118  bool doNormalize
119 ) const {
120  double imSum = 0;
121  for (int y = 0; y != image.getHeight(); ++y) {
122  double const fy = image.indexToPosition(y, afwImage::Y);
124  for (int x = 0; x != image.getWidth(); ++x, ++ptr) {
125  double const fx = image.indexToPosition(x, afwImage::X);
126  Pixel const pixelVal = (*_kernelFunctionPtr)(fx, fy);
127  *ptr = pixelVal;
128  imSum += pixelVal;
129  }
130  }
131 
132  if (doNormalize && (imSum != 1)) {
133  if (imSum == 0) {
134  throw LSST_EXCEPT(pexExcept::OverflowError, "Cannot normalize; kernel sum is 0");
135  }
136  image /= imSum;
137  imSum = 1;
138  }
139 
140  return imSum;
141 }
142 
143 void afwMath::AnalyticKernel::setKernelParameter(unsigned int ind, double value) const {
144  _kernelFunctionPtr->setParameter(ind, value);
145 }
146 
147 // ------ Persistence ---------------------------------------------------------------------------------------
148 
149 namespace lsst { namespace afw { namespace math {
150 
151 namespace {
152 
153 struct AnalyticKernelPersistenceHelper : public Kernel::PersistenceHelper {
154  table::Key<int> kernelFunction;
155 
156  explicit AnalyticKernelPersistenceHelper(int nSpatialFunctions) :
157  Kernel::PersistenceHelper(nSpatialFunctions),
159  schema.addField<int>(
160  "kernelfunction", "archive ID for analytic function used to produce kernel images"
161  )
162  )
163  {}
164 
165  explicit AnalyticKernelPersistenceHelper(table::Schema const & schema_) :
166  Kernel::PersistenceHelper(schema_),
167  kernelFunction(schema["kernelfunction"])
168  {}
169 };
170 
171 } // anonymous
172 
174 public:
175 
177  read(InputArchive const & archive, CatalogVector const & catalogs) const {
178  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
179  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
180  AnalyticKernelPersistenceHelper const keys(catalogs.front().getSchema());
181  afw::table::BaseRecord const & record = catalogs.front().front();
183  archive.get<AnalyticKernel::KernelFunction>(record.get(keys.kernelFunction));
184  PTR(AnalyticKernel) result;
185  if (keys.spatialFunctions.isValid()) {
186  result = boost::make_shared<AnalyticKernel>(
187  record.get(keys.dimensions.getX()), record.get(keys.dimensions.getY()), *kernelFunction,
188  keys.readSpatialFunctions(archive, record)
189  );
190  } else {
191  result = boost::make_shared<AnalyticKernel>(
192  record.get(keys.dimensions.getX()), record.get(keys.dimensions.getY()), *kernelFunction
193  );
194  }
195  result->setCtr(record.get(keys.center));
196  return result;
197  }
198 
199  explicit Factory(std::string const & name) : afw::table::io::PersistableFactory(name) {}
200 };
201 
202 namespace {
203 
204 std::string getAnalyticKernelPersistenceName() { return "AnalyticKernel"; }
205 
206 AnalyticKernel::Factory registration(getAnalyticKernelPersistenceName());
207 
208 } // anonymous
209 
210 std::string AnalyticKernel::getPersistenceName() const { return getAnalyticKernelPersistenceName(); }
211 
213  AnalyticKernelPersistenceHelper const keys(_spatialFunctionList.size());
214  PTR(afw::table::BaseRecord) record = keys.write(handle, *this);
215  record->set(keys.kernelFunction, handle.put(_kernelFunctionPtr.get()));
216 }
217 
218 }}} // namespace lsst::afw::math
int y
void setXY0(geom::Point2I const origin)
Definition: Image.h:362
Declare the Kernel class and subclasses.
boost::shared_ptr< lsst::afw::math::Function2< Pixel > > KernelFunctionPtr
Definition: Kernel.h:633
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.
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.
afw::table::Schema schema
Definition: GaussianPsf.cc:41
Include files required for standard LSST Exception handling.
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:231
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
Definition: Kernel.cc:224
double indexToPosition(double ind, lsst::afw::image::xOrY const xy) const
Convert image index to image position.
Definition: Image.h:290
#define PTR(...)
Definition: base.h:41
A Function taking two arguments.
Definition: Function.h:300
std::map< Citizen const *, CitizenInfo > table
Definition: Citizen.h:93
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
void ImageT ImageT int float saturatedPixelValue int const width
Definition: saturated.cc:44
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: Image.h:298
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.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:239
double x
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
void ImageT ImageT int float saturatedPixelValue int const height
Definition: saturated.cc:44
virtual KernelFunctionPtr getKernelFunction() const
Get a deep copy of the kernel function.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:26
unsigned int getNParameters() const
Return the number of function parameters.
Definition: Function.h:125
Base class for all records.
Definition: BaseRecord.h:27
table::Key< int > kernelFunction
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
AnalyticKernel()
Construct an empty spatially invariant AnalyticKernel of size 0x0.
x_iterator row_begin(int y) const
Definition: Image.h:319
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:28
A kernel described by a function.
Definition: Kernel.h:628
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:237
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:134
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.