lsst.afw  13.0-94-ga1c444047
GaussianPsf.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2014 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include <memory>
25 
26 #include "ndarray/eigen.h"
27 
33 
34 namespace lsst {
35 namespace afw {
36 namespace detection {
37 
38 namespace {
39 
40 // Read-only singleton struct containing the schema and keys that a GaussianPsf is mapped
41 // to in record persistence.
42 struct GaussianPsfPersistenceHelper {
43  afw::table::Schema schema;
44  afw::table::PointKey<int> dimensions;
45  afw::table::Key<double> sigma;
46 
47  static GaussianPsfPersistenceHelper const& get() {
48  static GaussianPsfPersistenceHelper instance;
49  return instance;
50  }
51 
52  // No copying
53  GaussianPsfPersistenceHelper(const GaussianPsfPersistenceHelper&) = delete;
54  GaussianPsfPersistenceHelper& operator=(const GaussianPsfPersistenceHelper&) = delete;
55 
56  // No moving
57  GaussianPsfPersistenceHelper(GaussianPsfPersistenceHelper&&) = delete;
58  GaussianPsfPersistenceHelper& operator=(GaussianPsfPersistenceHelper&&) = delete;
59 
60 private:
61  GaussianPsfPersistenceHelper()
62  : schema(),
64  "width/height of realization of Psf", "pixel")),
65  sigma(schema.addField<double>("sigma", "radius of Gaussian", "pixel")) {
66  schema.getCitizen().markPersistent();
67  }
68 };
69 
70 class GaussianPsfFactory : public afw::table::io::PersistableFactory {
71 public:
72  virtual std::shared_ptr<afw::table::io::Persistable> read(InputArchive const& archive,
73  CatalogVector const& catalogs) const {
74  static GaussianPsfPersistenceHelper const& keys = GaussianPsfPersistenceHelper::get();
75  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
76  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
77  afw::table::BaseRecord const& record = catalogs.front().front();
78  LSST_ARCHIVE_ASSERT(record.getSchema() == keys.schema);
79  return std::make_shared<GaussianPsf>(record.get(keys.dimensions.getX()),
80  record.get(keys.dimensions.getY()), record.get(keys.sigma));
81  }
82 
83  GaussianPsfFactory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
84 };
85 
86 GaussianPsfFactory registration("GaussianPsf");
87 
88 void checkDimensions(geom::Extent2I const& dimensions) {
89  if (dimensions.getX() % 2 == 0 || dimensions.getY() % 2 == 2) {
90  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "GaussianPsf dimensions must be odd");
91  }
92 }
93 
94 } // anonymous
95 
96 GaussianPsf::GaussianPsf(int width, int height, double sigma)
97  : Psf(true), _dimensions(width, height), _sigma(sigma) {
98  checkDimensions(_dimensions);
99 }
100 
102  : Psf(true), _dimensions(dimensions), _sigma(sigma) {
103  checkDimensions(_dimensions);
104 }
105 
106 std::shared_ptr<afw::detection::Psf> GaussianPsf::clone() const {
107  return std::make_shared<GaussianPsf>(_dimensions, _sigma);
108 }
109 
110 std::shared_ptr<afw::detection::Psf> GaussianPsf::resized(int width, int height) const {
111  return std::make_shared<GaussianPsf>(geom::Extent2I(width, height), _sigma);
112 }
113 
114 std::string GaussianPsf::getPersistenceName() const { return "GaussianPsf"; }
115 
116 std::string GaussianPsf::getPythonModule() const { return "lsst.afw.detection"; }
117 
119  static GaussianPsfPersistenceHelper const& keys = GaussianPsfPersistenceHelper::get();
120  afw::table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
121  std::shared_ptr<afw::table::BaseRecord> record = catalog.addNew();
122  (*record)[keys.dimensions.getX()] = _dimensions.getX();
123  (*record)[keys.dimensions.getY()] = _dimensions.getY();
124  (*record)[keys.sigma] = getSigma();
125  handle.saveCatalog(catalog);
126 }
127 
128 std::shared_ptr<GaussianPsf::Image> GaussianPsf::doComputeKernelImage(geom::Point2D const&,
129  image::Color const&) const {
130  std::shared_ptr<Image> r(new Image(computeBBox()));
131  Image::Array array = r->getArray();
132  double sum = 0.0;
133  for (int yIndex = 0, y = r->getY0(); yIndex < _dimensions.getY(); ++yIndex, ++y) {
134  Image::Array::Reference row = array[yIndex];
135  for (int xIndex = 0, x = r->getX0(); xIndex < _dimensions.getX(); ++xIndex, ++x) {
136  sum += row[xIndex] = std::exp(-0.5 * (x * x + y * y) / (_sigma * _sigma));
137  }
138  }
139  array.asEigen() /= sum;
140  return r;
141 }
142 
143 double GaussianPsf::doComputeApertureFlux(double radius, geom::Point2D const& position,
144  image::Color const& color) const {
145  return 1.0 - std::exp(-0.5 * radius * radius / (_sigma * _sigma));
146 }
147 
148 geom::ellipses::Quadrupole GaussianPsf::doComputeShape(geom::Point2D const& position,
149  image::Color const& color) const {
150  return geom::ellipses::Quadrupole(_sigma * _sigma, _sigma * _sigma, 0.0);
151 }
152 
153 geom::Box2I GaussianPsf::doComputeBBox(geom::Point2D const& position, image::Color const& color) const {
154  return geom::Box2I(geom::Point2I(-_dimensions / 2), _dimensions); // integer truncation intentional
155 }
156 }
157 }
158 } // namespace lsst::afw::detection
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:47
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
static PointKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
Add a pair of _x, _y fields to a Schema, and return a PointKey that points to them.
An object passed to Persistable::write to allow it to persist itself.
virtual std::shared_ptr< afw::detection::Psf > resized(int width, int height) const
Return a clone with specified kernel dimensions.
Definition: GaussianPsf.cc:110
A custom container class for records, based on std::vector.
Definition: Catalog.h:96
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: GaussianPsf.cc:118
GaussianPsf(int width, int height, double sigma)
Constructor for a GaussianPsf.
Definition: GaussianPsf.cc:96
Extent< int, 2 > Extent2I
Definition: Extent.h:341
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: GaussianPsf.cc:114
An integer coordinate rectangle.
Definition: Box.h:55
A base class for image defects.
Definition: cameraGeom.dox:3
virtual std::shared_ptr< afw::detection::Psf > clone() const
Polymorphic deep copy; should usually be unnecessary because Psfs are immutable.
Definition: GaussianPsf.cc:106
table::Key< std::string > name
Definition: ApCorrMap.cc:71
afw::table::Key< double > sigma
Definition: GaussianPsf.cc:45
double x
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
double getSigma() const
Return the radius of the Gaussian.
Definition: GaussianPsf.h:72
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
geom::Box2I computeBBox(geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Return the bounding box of the image returned by computeKernelImage()
Describe the colour of a source.
Definition: Color.h:26
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:70
ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
Definition: Image.h:163
image::Image< Pixel > Image
Image type returned by computeImage.
Definition: Psf.h:78
afw::table::Schema schema
Definition: GaussianPsf.cc:43
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: GaussianPsf.cc:116
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition: Catalog.h:463