LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
FunctionLibrary.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include <memory>
4 
9 
10 namespace lsst {
11 namespace afw {
12 namespace math {
13 
14 namespace {
15 
16 // Singleton persistence schema for 2-d Gaussians
17 struct GaussianFunction2PersistenceHelper {
18  table::Schema schema;
19  table::Key<double> sigma1;
20  table::Key<double> sigma2;
21  table::Key<double> angle;
22 
23  static GaussianFunction2PersistenceHelper const& get() {
24  static GaussianFunction2PersistenceHelper instance;
25  return instance;
26  }
27 
28  // No copying
29  GaussianFunction2PersistenceHelper(const GaussianFunction2PersistenceHelper&) = delete;
30  GaussianFunction2PersistenceHelper& operator=(const GaussianFunction2PersistenceHelper&) = delete;
31 
32  // No moving
33  GaussianFunction2PersistenceHelper(GaussianFunction2PersistenceHelper&&) = delete;
34  GaussianFunction2PersistenceHelper& operator=(GaussianFunction2PersistenceHelper&&) = delete;
35 
36 private:
37  GaussianFunction2PersistenceHelper()
38  : schema(),
39  sigma1(schema.addField<double>("sigma1", "sigma along axis 1")),
40  sigma2(schema.addField<double>("sigma2", "sigma along axis 2")),
41  angle(schema.addField<double>("angle", "angle of axis 1 in rad (along x=0, y=pi/2)")) {}
42 };
43 
44 // Singleton persistence schema for 2-d circular DoubleGaussians
45 struct DoubleGaussianFunction2PersistenceHelper {
46  table::Schema schema;
47  table::Key<double> sigma1;
48  table::Key<double> sigma2;
49  table::Key<double> ampl2;
50 
51  static DoubleGaussianFunction2PersistenceHelper const& get() {
52  static DoubleGaussianFunction2PersistenceHelper instance;
53  return instance;
54  }
55 
56  // No copying
57  DoubleGaussianFunction2PersistenceHelper(const DoubleGaussianFunction2PersistenceHelper&) = delete;
58  DoubleGaussianFunction2PersistenceHelper& operator=(const DoubleGaussianFunction2PersistenceHelper&) =
59  delete;
60 
61  // No moving
62  DoubleGaussianFunction2PersistenceHelper(DoubleGaussianFunction2PersistenceHelper&&) = delete;
63  DoubleGaussianFunction2PersistenceHelper& operator=(DoubleGaussianFunction2PersistenceHelper&&) = delete;
64 
65 private:
66  DoubleGaussianFunction2PersistenceHelper()
67  : schema(),
68  sigma1(schema.addField<double>("sigma1", "sigma of first Gaussian")),
69  sigma2(schema.addField<double>("sigma2", "sigma of second Gaussian")),
70  ampl2(schema.addField<double>("ampl2", "peak of second Gaussian relative to peak of first")) {}
71 };
72 
73 // Persistence schema for 2-d polynomials; not a singleton because it depends on the order.
74 struct PolynomialFunction2PersistenceHelper {
75  table::Schema schema;
76  table::Key<table::Array<double> > coefficients;
77 
78  explicit PolynomialFunction2PersistenceHelper(int nCoefficients)
79  : schema(),
80  coefficients(schema.addField<table::Array<double> >(
81  "coefficients",
82  "polynomial coefficients, ordered (x,y) [0,0; 1,0; 0,1; 2,0; 1,1; 0,2; ...]",
83  nCoefficients)) {}
84 
85  explicit PolynomialFunction2PersistenceHelper(table::Schema const& schema_)
86  : schema(schema_), coefficients(schema["coefficients"]) {}
87 };
88 
89 // Persistance schema for 2-d Chebyshevs; not a singleton because it depends on the order.
90 struct Chebyshev1Function2PersistenceHelper : public PolynomialFunction2PersistenceHelper {
91  table::PointKey<double> min;
92  table::PointKey<double> max;
93 
94  explicit Chebyshev1Function2PersistenceHelper(int nCoefficients)
95  : PolynomialFunction2PersistenceHelper(nCoefficients),
96  min(table::PointKey<double>::addFields(schema, "min", "minimum point for function's bbox",
97  "pixel")),
98  max(table::PointKey<double>::addFields(schema, "max", "maximum point for function's bbox",
99  "pixel")) {}
100 
101  explicit Chebyshev1Function2PersistenceHelper(table::Schema const& schema_)
102  : PolynomialFunction2PersistenceHelper(schema_), min(schema["min"]), max(schema["max"]) {}
103 };
104 
105 template <typename ReturnT>
106 class GaussianFunction2Factory : public table::io::PersistableFactory {
107 public:
108  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
109  CatalogVector const& catalogs) const override {
110  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
111  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
112  GaussianFunction2PersistenceHelper const& keys = GaussianFunction2PersistenceHelper::get();
113  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema().contains(keys.schema));
114  table::BaseRecord const& record = catalogs.front().front();
115  return std::make_shared<GaussianFunction2<ReturnT> >(record.get(keys.sigma1), record.get(keys.sigma2),
116  record.get(keys.angle));
117  }
118 
119  GaussianFunction2Factory(std::string const& name) : table::io::PersistableFactory(name) {}
120 };
121 
122 template <typename ReturnT>
123 class DoubleGaussianFunction2Factory : public table::io::PersistableFactory {
124 public:
125  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
126  CatalogVector const& catalogs) const override {
127  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
128  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
129  DoubleGaussianFunction2PersistenceHelper const& keys =
130  DoubleGaussianFunction2PersistenceHelper::get();
131  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema().contains(keys.schema));
132  table::BaseRecord const& record = catalogs.front().front();
133  return std::make_shared<DoubleGaussianFunction2<ReturnT> >(
134  record.get(keys.sigma1), record.get(keys.sigma2), record.get(keys.ampl2));
135  }
136 
137  DoubleGaussianFunction2Factory(std::string const& name) : table::io::PersistableFactory(name) {}
138 };
139 
140 template <typename ReturnT>
141 class PolynomialFunction2Factory : public table::io::PersistableFactory {
142 public:
143  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
144  CatalogVector const& catalogs) const override {
145  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
146  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
147  PolynomialFunction2PersistenceHelper const keys(catalogs.front().getSchema());
148  return std::make_shared<PolynomialFunction2<ReturnT> >(
149  keys.coefficients.extractVector(catalogs.front().front()));
150  }
151 
152  PolynomialFunction2Factory(std::string const& name) : table::io::PersistableFactory(name) {}
153 };
154 
155 template <typename ReturnT>
156 class Chebyshev1Function2Factory : public table::io::PersistableFactory {
157 public:
158  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
159  CatalogVector const& catalogs) const override {
160  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
161  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
162  Chebyshev1Function2PersistenceHelper keys(catalogs.front().getSchema());
163  table::BaseRecord const& record = catalogs.front().front();
164  lsst::geom::Box2D bbox(record.get(keys.min), record.get(keys.max));
165  return std::make_shared<Chebyshev1Function2<ReturnT> >(keys.coefficients.extractVector(record), bbox);
166  }
167 
168  Chebyshev1Function2Factory(std::string const& name) : table::io::PersistableFactory(name) {}
169 };
170 
171 GaussianFunction2Factory<float> registrationGaussian2F("GaussianFunction2F");
172 GaussianFunction2Factory<double> registrationGaussian2D("GaussianFunction2D");
173 
174 DoubleGaussianFunction2Factory<float> registrationDoubleGaussian2F("DoubleGaussianFunction2F");
175 DoubleGaussianFunction2Factory<double> registrationDoubleGaussian2D("DoubleGaussianFunction2D");
176 
177 PolynomialFunction2Factory<float> registrationPoly2F("PolynomialFunction2F");
178 PolynomialFunction2Factory<double> registrationPoly2D("PolynomialFunction2D");
179 
180 Chebyshev1Function2Factory<float> registrationCheb2F("Chebyshev1Function2F");
181 Chebyshev1Function2Factory<double> registrationCheb2D("Chebyshev1Function2D");
182 
183 template <typename T>
184 struct Suffix;
185 template <>
186 struct Suffix<float> {
187  static std::string get() { return "F"; }
188 };
189 template <>
190 struct Suffix<double> {
191  static std::string get() { return "D"; }
192 };
193 
194 } // namespace
195 
196 template <typename ReturnT>
198  return "GaussianFunction2" + Suffix<ReturnT>::get();
199 }
200 
201 template <typename ReturnT>
203  return "DoubleGaussianFunction2" + Suffix<ReturnT>::get();
204 }
205 
206 template <typename ReturnT>
208  return "PolynomialFunction2" + Suffix<ReturnT>::get();
209 }
210 
211 template <typename ReturnT>
213  return "Chebyshev1Function2" + Suffix<ReturnT>::get();
214 }
215 
216 template <typename ReturnT>
218  GaussianFunction2PersistenceHelper const& keys = GaussianFunction2PersistenceHelper::get();
219  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
220  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
221  record->set(keys.sigma1, this->getParameters()[0]);
222  record->set(keys.sigma2, this->getParameters()[1]);
223  record->set(keys.angle, this->getParameters()[2]);
224  handle.saveCatalog(catalog);
225 }
226 
227 template <typename ReturnT>
229  DoubleGaussianFunction2PersistenceHelper const& keys = DoubleGaussianFunction2PersistenceHelper::get();
230  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
231  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
232  record->set(keys.sigma1, this->getParameters()[0]);
233  record->set(keys.sigma2, this->getParameters()[1]);
234  record->set(keys.ampl2, this->getParameters()[2]);
235  handle.saveCatalog(catalog);
236 }
237 
238 template <typename ReturnT>
240  PolynomialFunction2PersistenceHelper const keys(this->getNParameters());
241  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
242  keys.coefficients.assignVector(*catalog.addNew(), this->getParameters());
243  handle.saveCatalog(catalog);
244 }
245 
246 template <typename ReturnT>
248  Chebyshev1Function2PersistenceHelper const keys(this->getNParameters());
249  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
250  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
251  keys.coefficients.assignVector(*record, this->getParameters());
252  lsst::geom::Box2D bbox = getXYRange();
253  record->set(keys.min, bbox.getMin());
254  record->set(keys.max, bbox.getMax());
255  handle.saveCatalog(catalog);
256 }
257 
258 // Explicit instantiation
259 #define INSTANTIATE(TYPE) \
260  template class IntegerDeltaFunction1<TYPE>; \
261  template class IntegerDeltaFunction2<TYPE>; \
262  template class GaussianFunction1<TYPE>; \
263  template class GaussianFunction2<TYPE>; \
264  template class DoubleGaussianFunction2<TYPE>; \
265  template class PolynomialFunction1<TYPE>; \
266  template class PolynomialFunction2<TYPE>; \
267  template class Chebyshev1Function1<TYPE>; \
268  template class Chebyshev1Function2<TYPE>; \
269  template class LanczosFunction1<TYPE>; \
270  template class LanczosFunction2<TYPE>;
271 
272 INSTANTIATE(float);
273 INSTANTIATE(double);
274 } // namespace math
275 } // namespace afw
276 } // namespace lsst
table::Key< std::string > name
Definition: Amplifier.cc:116
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
#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:490
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.
std::shared_ptr< table::io::Persistable > read(table::io::InputArchive const &archive, table::io::CatalogVector const &catalogs) const override
Definition: warpExposure.cc:0