LSSTApplications  20.0.0
LSSTDataManagementBasePackage
FunctionLibrary.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include <memory>
4 
10 
11 namespace lsst {
12 namespace afw {
13 namespace math {
14 
15 namespace {
16 
17 // Singleton persistence schema for 2-d Gaussians
18 struct GaussianFunction2PersistenceHelper {
19  table::Schema schema;
20  table::Key<double> sigma1;
21  table::Key<double> sigma2;
22  table::Key<double> angle;
23 
24  static GaussianFunction2PersistenceHelper const& get() {
25  static GaussianFunction2PersistenceHelper instance;
26  return instance;
27  }
28 
29  // No copying
30  GaussianFunction2PersistenceHelper(const GaussianFunction2PersistenceHelper&) = delete;
31  GaussianFunction2PersistenceHelper& operator=(const GaussianFunction2PersistenceHelper&) = delete;
32 
33  // No moving
34  GaussianFunction2PersistenceHelper(GaussianFunction2PersistenceHelper&&) = delete;
35  GaussianFunction2PersistenceHelper& operator=(GaussianFunction2PersistenceHelper&&) = delete;
36 
37 private:
38  GaussianFunction2PersistenceHelper()
39  : schema(),
40  sigma1(schema.addField<double>("sigma1", "sigma along axis 1")),
41  sigma2(schema.addField<double>("sigma2", "sigma along axis 2")),
42  angle(schema.addField<double>("angle", "angle of axis 1 in rad (along x=0, y=pi/2)")) {}
43 };
44 
45 // Singleton persistence schema for 2-d circular DoubleGaussians
46 struct DoubleGaussianFunction2PersistenceHelper {
47  table::Schema schema;
48  table::Key<double> sigma1;
49  table::Key<double> sigma2;
50  table::Key<double> ampl2;
51 
52  static DoubleGaussianFunction2PersistenceHelper const& get() {
53  static DoubleGaussianFunction2PersistenceHelper instance;
54  return instance;
55  }
56 
57  // No copying
58  DoubleGaussianFunction2PersistenceHelper(const DoubleGaussianFunction2PersistenceHelper&) = delete;
59  DoubleGaussianFunction2PersistenceHelper& operator=(const DoubleGaussianFunction2PersistenceHelper&) =
60  delete;
61 
62  // No moving
63  DoubleGaussianFunction2PersistenceHelper(DoubleGaussianFunction2PersistenceHelper&&) = delete;
64  DoubleGaussianFunction2PersistenceHelper& operator=(DoubleGaussianFunction2PersistenceHelper&&) = delete;
65 
66 private:
67  DoubleGaussianFunction2PersistenceHelper()
68  : schema(),
69  sigma1(schema.addField<double>("sigma1", "sigma of first Gaussian")),
70  sigma2(schema.addField<double>("sigma2", "sigma of second Gaussian")),
71  ampl2(schema.addField<double>("ampl2", "peak of second Gaussian relative to peak of first")) {}
72 };
73 
74 // Persistence schema for 2-d polynomials; not a singleton because it depends on the order.
75 struct PolynomialFunction2PersistenceHelper {
76  table::Schema schema;
77  table::Key<table::Array<double> > coefficients;
78 
79  explicit PolynomialFunction2PersistenceHelper(int nCoefficients)
80  : schema(),
81  coefficients(schema.addField<table::Array<double> >(
82  "coefficients",
83  "polynomial coefficients, ordered (x,y) [0,0; 1,0; 0,1; 2,0; 1,1; 0,2; ...]",
84  nCoefficients)) {}
85 
86  explicit PolynomialFunction2PersistenceHelper(table::Schema const& schema_)
87  : schema(schema_), coefficients(schema["coefficients"]) {}
88 };
89 
90 // Persistance schema for 2-d Chebyshevs; not a singleton because it depends on the order.
91 struct Chebyshev1Function2PersistenceHelper : public PolynomialFunction2PersistenceHelper {
92  table::PointKey<double> min;
93  table::PointKey<double> max;
94 
95  explicit Chebyshev1Function2PersistenceHelper(int nCoefficients)
96  : PolynomialFunction2PersistenceHelper(nCoefficients),
97  min(table::PointKey<double>::addFields(schema, "min", "minimum point for function's bbox",
98  "pixel")),
99  max(table::PointKey<double>::addFields(schema, "max", "maximum point for function's bbox",
100  "pixel")) {}
101 
102  explicit Chebyshev1Function2PersistenceHelper(table::Schema const& schema_)
103  : PolynomialFunction2PersistenceHelper(schema_), min(schema["min"]), max(schema["max"]) {}
104 };
105 
106 template <typename ReturnT>
107 class GaussianFunction2Factory : public table::io::PersistableFactory {
108 public:
109  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
110  CatalogVector const& catalogs) const override {
111  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
112  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
113  GaussianFunction2PersistenceHelper const& keys = GaussianFunction2PersistenceHelper::get();
114  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema().contains(keys.schema));
115  table::BaseRecord const& record = catalogs.front().front();
116  return std::make_shared<GaussianFunction2<ReturnT> >(record.get(keys.sigma1), record.get(keys.sigma2),
117  record.get(keys.angle));
118  }
119 
120  GaussianFunction2Factory(std::string const& name) : table::io::PersistableFactory(name) {}
121 };
122 
123 template <typename ReturnT>
124 class DoubleGaussianFunction2Factory : public table::io::PersistableFactory {
125 public:
126  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
127  CatalogVector const& catalogs) const override {
128  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
129  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
130  DoubleGaussianFunction2PersistenceHelper const& keys =
131  DoubleGaussianFunction2PersistenceHelper::get();
132  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema().contains(keys.schema));
133  table::BaseRecord const& record = catalogs.front().front();
134  return std::make_shared<DoubleGaussianFunction2<ReturnT> >(
135  record.get(keys.sigma1), record.get(keys.sigma2), record.get(keys.ampl2));
136  }
137 
138  DoubleGaussianFunction2Factory(std::string const& name) : table::io::PersistableFactory(name) {}
139 };
140 
141 template <typename ReturnT>
142 class PolynomialFunction2Factory : public table::io::PersistableFactory {
143 public:
144  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
145  CatalogVector const& catalogs) const override {
146  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
147  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
148  PolynomialFunction2PersistenceHelper const keys(catalogs.front().getSchema());
149  return std::make_shared<PolynomialFunction2<ReturnT> >(
150  keys.coefficients.extractVector(catalogs.front().front()));
151  }
152 
153  PolynomialFunction2Factory(std::string const& name) : table::io::PersistableFactory(name) {}
154 };
155 
156 template <typename ReturnT>
157 class Chebyshev1Function2Factory : public table::io::PersistableFactory {
158 public:
159  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
160  CatalogVector const& catalogs) const override {
161  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
162  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
163  Chebyshev1Function2PersistenceHelper keys(catalogs.front().getSchema());
164  table::BaseRecord const& record = catalogs.front().front();
165  lsst::geom::Box2D bbox(record.get(keys.min), record.get(keys.max));
166  return std::make_shared<Chebyshev1Function2<ReturnT> >(keys.coefficients.extractVector(record), bbox);
167  }
168 
169  Chebyshev1Function2Factory(std::string const& name) : table::io::PersistableFactory(name) {}
170 };
171 
172 GaussianFunction2Factory<float> registrationGaussian2F("GaussianFunction2F");
173 GaussianFunction2Factory<double> registrationGaussian2D("GaussianFunction2D");
174 
175 DoubleGaussianFunction2Factory<float> registrationDoubleGaussian2F("DoubleGaussianFunction2F");
176 DoubleGaussianFunction2Factory<double> registrationDoubleGaussian2D("DoubleGaussianFunction2D");
177 
178 PolynomialFunction2Factory<float> registrationPoly2F("PolynomialFunction2F");
179 PolynomialFunction2Factory<double> registrationPoly2D("PolynomialFunction2D");
180 
181 Chebyshev1Function2Factory<float> registrationCheb2F("Chebyshev1Function2F");
182 Chebyshev1Function2Factory<double> registrationCheb2D("Chebyshev1Function2D");
183 
184 template <typename T>
185 struct Suffix;
186 template <>
187 struct Suffix<float> {
188  static std::string get() { return "F"; }
189 };
190 template <>
191 struct Suffix<double> {
192  static std::string get() { return "D"; }
193 };
194 
195 } // namespace
196 
197 template <typename ReturnT>
199  return "GaussianFunction2" + Suffix<ReturnT>::get();
200 }
201 
202 template <typename ReturnT>
204  return "DoubleGaussianFunction2" + Suffix<ReturnT>::get();
205 }
206 
207 template <typename ReturnT>
209  return "PolynomialFunction2" + Suffix<ReturnT>::get();
210 }
211 
212 template <typename ReturnT>
214  return "Chebyshev1Function2" + Suffix<ReturnT>::get();
215 }
216 
217 template <typename ReturnT>
219  GaussianFunction2PersistenceHelper const& keys = GaussianFunction2PersistenceHelper::get();
220  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
221  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
222  record->set(keys.sigma1, this->getParameters()[0]);
223  record->set(keys.sigma2, this->getParameters()[1]);
224  record->set(keys.angle, this->getParameters()[2]);
225  handle.saveCatalog(catalog);
226 }
227 
228 template <typename ReturnT>
230  DoubleGaussianFunction2PersistenceHelper const& keys = DoubleGaussianFunction2PersistenceHelper::get();
231  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
232  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
233  record->set(keys.sigma1, this->getParameters()[0]);
234  record->set(keys.sigma2, this->getParameters()[1]);
235  record->set(keys.ampl2, this->getParameters()[2]);
236  handle.saveCatalog(catalog);
237 }
238 
239 template <typename ReturnT>
241  PolynomialFunction2PersistenceHelper const keys(this->getNParameters());
242  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
243  keys.coefficients.assignVector(*catalog.addNew(), this->getParameters());
244  handle.saveCatalog(catalog);
245 }
246 
247 template <typename ReturnT>
249  Chebyshev1Function2PersistenceHelper const keys(this->getNParameters());
250  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
251  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
252  keys.coefficients.assignVector(*record, this->getParameters());
253  lsst::geom::Box2D bbox = getXYRange();
254  record->set(keys.min, bbox.getMin());
255  record->set(keys.max, bbox.getMax());
256  handle.saveCatalog(catalog);
257 }
258 
259 // Explicit instantiation
260 #define INSTANTIATE(TYPE) \
261  template class IntegerDeltaFunction1<TYPE>; \
262  template class IntegerDeltaFunction2<TYPE>; \
263  template class GaussianFunction1<TYPE>; \
264  template class GaussianFunction2<TYPE>; \
265  template class DoubleGaussianFunction2<TYPE>; \
266  template class PolynomialFunction1<TYPE>; \
267  template class PolynomialFunction2<TYPE>; \
268  template class Chebyshev1Function1<TYPE>; \
269  template class Chebyshev1Function2<TYPE>; \
270  template class LanczosFunction1<TYPE>; \
271  template class LanczosFunction2<TYPE>;
272 
273 INSTANTIATE(float);
274 INSTANTIATE(double);
275 } // namespace math
276 } // namespace afw
277 } // namespace lsst
schema
table::Schema schema
Definition: FunctionLibrary.cc:19
min
table::PointKey< double > min
Definition: FunctionLibrary.cc:92
INSTANTIATE
#define INSTANTIATE(TYPE)
Definition: FunctionLibrary.cc:260
max
table::PointKey< double > max
Definition: FunctionLibrary.cc:93
std::string
STL class.
std::shared_ptr
STL class.
coefficients
table::Key< table::Array< double > > coefficients
Definition: FunctionLibrary.cc:77
lsst::afw::table::io::OutputArchiveHandle
An object passed to Persistable::write to allow it to persist itself.
Definition: OutputArchive.h:118
lsst::afw::math::PolynomialFunction2::getPersistenceName
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: FunctionLibrary.cc:208
lsst::afw::table::io::OutputArchiveHandle::saveCatalog
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Definition: OutputArchive.cc:211
FunctionLibrary.h
lsst::afw
Definition: imageAlgorithm.dox:1
astshim.keyMap.keyMapContinued.keys
def keys(self)
Definition: keyMapContinued.py:6
lsst::afw::math::PolynomialFunction2::write
void write(afw::table::io::OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: FunctionLibrary.cc:240
CatalogVector.h
angle
table::Key< double > angle
Definition: FunctionLibrary.cc:22
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
aggregates.h
lsst::afw::table::io::OutputArchiveHandle::makeCatalog
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
Definition: OutputArchive.cc:207
sigma2
table::Key< double > sigma2
Definition: FunctionLibrary.cc:21
LSST_ARCHIVE_ASSERT
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
lsst::afw::math::GaussianFunction2::write
void write(afw::table::io::OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: FunctionLibrary.cc:218
ampl2
table::Key< double > ampl2
Definition: FunctionLibrary.cc:50
sigma1
table::Key< double > sigma1
Definition: FunctionLibrary.cc:20
lsst::afw::math::Chebyshev1Function2::write
void write(afw::table::io::OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: FunctionLibrary.cc:248
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::afw::math::Chebyshev1Function2::getPersistenceName
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: FunctionLibrary.cc:213
lsst::afw::math::DoubleGaussianFunction2::write
void write(afw::table::io::OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: FunctionLibrary.cc:229
InputArchive.h
lsst::afw::math::GaussianFunction2::getPersistenceName
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: FunctionLibrary.cc:198
lsst::afw::math::DoubleGaussianFunction2::getPersistenceName
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: FunctionLibrary.cc:203
lsst::afw::table::CatalogT::addNew
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:485
lsst::geom::Box2D
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
lsst::afw::table::CatalogT< BaseRecord >
OutputArchive.h
bbox
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117