LSST Applications  21.0.0+c4f5df5339,21.0.0+e70536a077,21.0.0-1-ga51b5d4+7c60f8a6ea,21.0.0-10-gcf60f90+74aa0801fd,21.0.0-12-g63909ac9+643a1044a5,21.0.0-15-gedb9d5423+1041c3824f,21.0.0-2-g103fe59+a356b2badb,21.0.0-2-g1367e85+6d3f3f41db,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g5242d73+6d3f3f41db,21.0.0-2-g7f82c8f+8d7c04eab9,21.0.0-2-g8f08a60+9c9a9cfcc8,21.0.0-2-ga326454+8d7c04eab9,21.0.0-2-gde069b7+bedfc5e1fb,21.0.0-2-gecfae73+6cb6558258,21.0.0-2-gfc62afb+6d3f3f41db,21.0.0-3-g21c7a62+f6e98b25aa,21.0.0-3-g357aad2+bd62456bea,21.0.0-3-g4be5c26+6d3f3f41db,21.0.0-3-g65f322c+03a4076c01,21.0.0-3-g7d9da8d+c4f5df5339,21.0.0-3-gaa929c8+c6b98066dc,21.0.0-3-gc44e71e+a26d5c1aea,21.0.0-3-ge02ed75+04b527a9d5,21.0.0-35-g64f566ff+b27e5ef93e,21.0.0-4-g591bb35+04b527a9d5,21.0.0-4-g88306b8+8773047b2e,21.0.0-4-gc004bbf+80a0b7acb7,21.0.0-4-gccdca77+a5c54364a0,21.0.0-4-ge8fba5a+ccfc328107,21.0.0-5-gdf36809+87b8d260e6,21.0.0-6-g00874e7+7eeda2b6ba,21.0.0-6-g2d4f3f3+e70536a077,21.0.0-6-g4e60332+04b527a9d5,21.0.0-6-g5ef7dad+f53629abd8,21.0.0-7-gc8ca178+b63e69433b,21.0.0-8-gfbe0b4b+c6b98066dc,w.2021.06
LSST Data Management Base Package
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
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
void write(afw::table::io::OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
void write(afw::table::io::OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
void write(afw::table::io::OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
void write(afw::table::io::OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
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
An object passed to Persistable::write to allow it to persist itself.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
FilterProperty & operator=(FilterProperty const &)=default
A base class for image defects.
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
table::Key< double > angle
table::Key< double > ampl2
table::Key< double > sigma2
table::Schema schema
table::PointKey< double > max
table::Key< double > sigma1
table::Key< table::Array< double > > coefficients
#define INSTANTIATE(TYPE)
table::PointKey< double > min