LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
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 "boost/make_shared.hpp"
25 
26 #include "ndarray/eigen.h"
27 
32 
33 namespace lsst { namespace afw { namespace detection {
34 
35 namespace {
36 
37 // Read-only singleton struct containing the schema and keys that a GaussianPsf is mapped
38 // to in record persistence.
39 struct GaussianPsfPersistenceHelper : private boost::noncopyable {
40  afw::table::Schema schema;
41  afw::table::Key< afw::table::Point<int> > dimensions;
42  afw::table::Key<double> sigma;
43 
44  static GaussianPsfPersistenceHelper const & get() {
45  static GaussianPsfPersistenceHelper instance;
46  return instance;
47  }
48 
49 private:
50  GaussianPsfPersistenceHelper() :
51  schema(),
52  dimensions(
53  schema.addField< afw::table::Point<int> >(
54  "dimensions", "width/height of realization of Psf", "pixels"
55  )
56  ),
57  sigma(schema.addField<double>("sigma", "radius of Gaussian", "pixels"))
58  {
59  schema.getCitizen().markPersistent();
60  }
61 };
62 
63 class GaussianPsfFactory : public afw::table::io::PersistableFactory {
64 public:
65 
66  virtual PTR(afw::table::io::Persistable)
67  read(InputArchive const & archive, CatalogVector const & catalogs) const {
68  static GaussianPsfPersistenceHelper const & keys = GaussianPsfPersistenceHelper::get();
69  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
70  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
71  afw::table::BaseRecord const & record = catalogs.front().front();
72  LSST_ARCHIVE_ASSERT(record.getSchema() == keys.schema);
73  return boost::make_shared<GaussianPsf>(
74  record.get(keys.dimensions.getX()),
75  record.get(keys.dimensions.getY()),
76  record.get(keys.sigma)
77  );
78  }
79 
80  GaussianPsfFactory(std::string const & name) : afw::table::io::PersistableFactory(name) {}
81 
82 };
83 
84 GaussianPsfFactory registration("GaussianPsf");
85 
86 void checkDimensions(geom::Extent2I const & dimensions) {
87  if (dimensions.getX() % 2 == 0 || dimensions.getY() % 2 == 2) {
88  throw LSST_EXCEPT(
89  pex::exceptions::InvalidParameterError,
90  "GaussianPsf dimensions must be odd"
91  );
92  }
93 }
94 
95 } // anonymous
96 
97 GaussianPsf::GaussianPsf(int width, int height, double sigma) :
98  Psf(true), _dimensions(width, height), _sigma(sigma)
99 {
100  checkDimensions(_dimensions);
101 }
102 
103 GaussianPsf::GaussianPsf(geom::Extent2I const & dimensions, double sigma) :
104  Psf(true), _dimensions(dimensions), _sigma(sigma)
105 {
106  checkDimensions(_dimensions);
107 }
108 
110  return boost::make_shared<GaussianPsf>(_dimensions, _sigma);
111 }
112 
113 std::string GaussianPsf::getPersistenceName() const { return "GaussianPsf"; }
114 
115 std::string GaussianPsf::getPythonModule() const { return "lsst.afw.detection"; }
116 
118  static GaussianPsfPersistenceHelper const & keys = GaussianPsfPersistenceHelper::get();
119  afw::table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
120  PTR(afw::table::BaseRecord) record = catalog.addNew();
121  (*record)[keys.dimensions.getX()] = _dimensions.getX();
122  (*record)[keys.dimensions.getY()] = _dimensions.getY();
123  (*record)[keys.sigma] = getSigma();
124  handle.saveCatalog(catalog);
125 }
126 
127 PTR(GaussianPsf::Image) GaussianPsf::doComputeKernelImage(
128  geom::Point2D const &, image::Color const &
129 ) const {
130  PTR(Image) r(new Image(_dimensions));
131  Image::Array array = r->getArray();
132  r->setXY0(geom::Point2I(-_dimensions / 2)); // integer truncation intentional
133  double sum = 0.0;
134  for (int yIndex = 0, y = r->getY0(); yIndex < _dimensions.getY(); ++yIndex, ++y) {
135  Image::Array::Reference row = array[yIndex];
136  for (int xIndex = 0, x = r->getX0(); xIndex < _dimensions.getX(); ++xIndex, ++x) {
137  sum += row[xIndex] = std::exp(-0.5*(x*x + y*y)/(_sigma*_sigma));
138  }
139  }
140  array.asEigen() /= sum;
141  return r;
142 }
143 
145  double radius, geom::Point2D const & position, image::Color const & color
146 ) const {
147  return 1.0 - std::exp(-0.5*radius*radius/(_sigma*_sigma));
148 }
149 
151  geom::Point2D const & position, image::Color const & color
152 ) const {
153  return geom::ellipses::Quadrupole(_sigma*_sigma, _sigma*_sigma, 0.0);
154 }
155 
156 }}} // namespace lsst::afw::detection
int y
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:45
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
Eigen matrix objects that present a view into an ndarray::Array.
An object passed to Persistable::write to allow it to persist itself.
A custom container class for records, based on std::vector.
Definition: Catalog.h:94
afw::table::Key< afw::table::Point< int > > dimensions
double _sigma
Definition: SincCoeffs.cc:196
virtual geom::ellipses::Quadrupole doComputeShape(geom::Point2D const &position, image::Color const &color) const
Definition: GaussianPsf.cc:150
EigenView< Element, ND::value, RMC::value, XprKind, Rows, Cols > asEigen() const
#define PTR(...)
Definition: base.h:41
boost::enable_if< typename ExpressionTraits< Scalar >::IsScalar, Scalar >::type sum(Scalar const &scalar)
Definition: operators.h:1250
boost::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:469
double getSigma() const
Return the radius of the Gaussian.
Definition: GaussianPsf.h:68
afw::table::Key< double > sigma
Definition: GaussianPsf.cc:42
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
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:115
tbl::Schema schema
Definition: CoaddPsf.cc:324
GaussianPsf(int width, int height, double sigma)
Constructor for a GaussianPsf.
Definition: GaussianPsf.cc:97
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: GaussianPsf.cc:117
int x
int row
Definition: CR.cc:153
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual double doComputeApertureFlux(double radius, geom::Point2D const &position, image::Color const &color) const
Definition: GaussianPsf.cc:144
A multidimensional strided array.
Definition: Array.h:47
Base class for all records.
Definition: BaseRecord.h:27
Point< double, 2 > Point2D
Definition: Point.h:286
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:68
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
dictionary Point
Definition: __init__.py:38
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: GaussianPsf.cc:113
A circularly symmetric Gaussian Psf class with no spatial variation, intended mostly for testing purp...
Definition: GaussianPsf.h:41