LSSTApplications  20.0.0
LSSTDataManagementBasePackage
PhotometryTransform.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * This file is part of jointcal.
4  *
5  * Developed for the LSST Data Management System.
6  * This product includes software developed by the LSST Project
7  * (https://www.lsst.org).
8  * See the COPYRIGHT file at the top-level directory of this distribution
9  * for details of code ownership.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef LSST_JOINTCAL_PHOTOMETRY_TRANSFORM_H
26 #define LSST_JOINTCAL_PHOTOMETRY_TRANSFORM_H
27 
28 #include <iostream>
29 #include <sstream>
30 #include <memory>
31 
32 #include "Eigen/Core"
33 #include "ndarray.h"
34 
36 #include "lsst/geom/Box.h"
37 #include "lsst/geom/Point.h"
38 #include "lsst/jointcal/Point.h"
39 
40 namespace lsst {
41 namespace jointcal {
42 
43 class Point;
44 
53 public:
55  virtual double transform(double x, double y, double value) const = 0;
56 
58  double transform(Point const &in, double value) const { return transform(in.x, in.y, value); }
59 
61  virtual double transformError(double x, double y, double value, double valueErr) const = 0;
62 
64  double transformError(Point const &in, double value, double valueErr) const {
65  return transformError(in.x, in.y, value, valueErr);
66  }
67 
69  virtual void print(std::ostream &out) const = 0;
70 
72  transform.print(s);
73  return s;
74  }
75 
77  virtual std::size_t getNpar() const = 0;
78 
87  virtual void offsetParams(Eigen::VectorXd const &delta) = 0;
88 
91 
100  virtual void computeParameterDerivatives(double x, double y, double value,
101  Eigen::Ref<Eigen::VectorXd> derivatives) const = 0;
102 
104  virtual Eigen::VectorXd getParameters() const = 0;
105 };
106 
111 public:
112  explicit PhotometryTransformSpatiallyInvariant(double value) : _value(value) {}
113 
115  void print(std::ostream &out) const override {
116  out << "PhotometryTransformSpatiallyInvariant: " << std::setprecision(10) << _value;
117  }
118 
120  std::size_t getNpar() const override { return 1; }
121 
123  void offsetParams(Eigen::VectorXd const &delta) override { _value -= delta[0]; };
124 
126  Eigen::VectorXd getParameters() const override {
127  Eigen::VectorXd parameters(1);
128  parameters[0] = _value;
129  return parameters;
130  }
131 
132 protected:
133  double getValue() const { return _value; }
134 
135 private:
137  double _value;
138 };
139 
147 public:
148  explicit FluxTransformSpatiallyInvariant(double value = 1)
150 
152  double transform(double x, double y, double value) const override { return value * getValue(); }
153 
155  double transformError(double x, double y, double value, double valueErr) const override {
156  return getValue() * valueErr;
157  }
158 
161  return std::make_shared<FluxTransformSpatiallyInvariant>(getValue());
162  }
163 
165  void computeParameterDerivatives(double x, double y, double value,
166  Eigen::Ref<Eigen::VectorXd> derivatives) const override {
167  // the derivative of a spatially constant transform w.r.t. that value is just the value.
168  derivatives[0] = value;
169  }
170 };
171 
179 public:
180  explicit MagnitudeTransformSpatiallyInvariant(double value = 0)
182 
184  double transform(double x, double y, double mag) const override { return mag + getValue(); }
185 
187  double transformError(double x, double y, double value, double valueErr) const override {
188  return valueErr;
189  }
190 
193  return std::make_shared<MagnitudeTransformSpatiallyInvariant>(getValue());
194  }
195 
197  void computeParameterDerivatives(double x, double y, double value,
198  Eigen::Ref<Eigen::VectorXd> derivatives) const override {
199  // the derivative of a spatially constant transform w.r.t. that value is 1.
200  derivatives[0] = 1;
201  }
202 };
203 
222 public:
230  PhotometryTransformChebyshev(size_t order, geom::Box2D const &bbox, bool identity);
231 
241  PhotometryTransformChebyshev(ndarray::Array<double, 2, 2> const &coefficients, geom::Box2D const &bbox);
242 
244  double transformError(double x, double y, double value, double valueErr) const override { return 0; }
245 
247  void print(std::ostream &out) const override { out << "PhotometryTransformChebyshev: " << _coefficients; }
248 
250  std::size_t getNpar() const override { return _nParameters; }
251 
253  void offsetParams(Eigen::VectorXd const &delta) override;
254 
256  ndarray::Array<double, 2, 2> getCoefficients() const { return ndarray::copy(_coefficients); }
257 
259  Eigen::VectorXd getParameters() const override;
260 
261  ndarray::Size getOrder() const { return _order; }
262 
263  geom::Box2D getBBox() const { return _bbox; }
264 
266  double mean(geom::Box2D const &bbox) const;
268  double mean() const;
269 
270  // Compute the integral of this function over a bounding-box (default to our bbox).
271  double integrate(geom::Box2D const &bbox) const;
273  double integrate() const;
274 
275 protected:
279  double computeChebyshev(double x, double y) const;
280 
285  void computeChebyshevDerivatives(double x, double y, Eigen::Ref<Eigen::VectorXd> derivatives) const;
286 
287 private:
288  geom::Box2D _bbox; // the domain of this function
289  geom::AffineTransform _toChebyshevRange; // maps points from the bbox to [-1,1]x[-1,1]
290 
291  ndarray::Array<double, 2, 2> _coefficients; // shape=(order+1, order+1)
292  ndarray::Size _order;
293  ndarray::Size _nParameters;
294 
296  double oneIntegral(double x, double y) const;
297 };
298 
303 public:
305  : PhotometryTransformChebyshev(order, bbox, true) {}
306 
307  FluxTransformChebyshev(ndarray::Array<double, 2, 2> const &coefficients, geom::Box2D const &bbox)
309 
311  double transform(double x, double y, double value) const override {
312  return value * computeChebyshev(x, y);
313  }
314 
316  void computeParameterDerivatives(double x, double y, double value,
317  Eigen::Ref<Eigen::VectorXd> derivatives) const override {
318  computeChebyshevDerivatives(x, y, derivatives);
319  derivatives *= value;
320  }
321 
324  return std::make_shared<FluxTransformChebyshev>(getCoefficients(), getBBox());
325  }
326 };
327 
332 public:
334  : PhotometryTransformChebyshev(order, bbox, false) {}
335 
336  MagnitudeTransformChebyshev(ndarray::Array<double, 2, 2> const &coefficients, geom::Box2D const &bbox)
338 
340  double transform(double x, double y, double value) const override {
341  return value + computeChebyshev(x, y);
342  }
343 
345  void computeParameterDerivatives(double x, double y, double value,
346  Eigen::Ref<Eigen::VectorXd> derivatives) const override {
347  // The derivatives here are independent of value
348  computeChebyshevDerivatives(x, y, derivatives);
349  }
350 
353  return std::make_shared<FluxTransformChebyshev>(getCoefficients(), getBBox());
354  }
355 };
356 
357 } // namespace jointcal
358 } // namespace lsst
359 
360 #endif // LSST_JOINTCAL_PHOTOMETRY_TRANSFORM_H
y
int y
Definition: SpanSet.cc:49
lsst::jointcal::PhotometryTransform::clone
virtual std::shared_ptr< PhotometryTransform > clone() const =0
return a copy (allocated by new) of the transformation.
lsst::jointcal::PhotometryTransformChebyshev
nth-order 2d Chebyshev photometry transform.
Definition: PhotometryTransform.h:221
std::setprecision
T setprecision(T... args)
lsst::jointcal::PhotometryTransformChebyshev::PhotometryTransformChebyshev
PhotometryTransformChebyshev(size_t order, geom::Box2D const &bbox, bool identity)
Create a Chebyshev transform with terms up to order in (x*y).
Definition: PhotometryTransform.cc:91
Box.h
lsst::jointcal::PhotometryTransform::offsetParams
virtual void offsetParams(Eigen::VectorXd const &delta)=0
Offset the parameters by some (negative) amount during fitting.
lsst::jointcal::MagnitudeTransformChebyshev::computeParameterDerivatives
void computeParameterDerivatives(double x, double y, double value, Eigen::Ref< Eigen::VectorXd > derivatives) const override
Compute the derivatives with respect to the parameters (i.e.
Definition: PhotometryTransform.h:345
std::shared_ptr
STL class.
coefficients
ndarray::Array< double const, 2, 2 > coefficients
Definition: ChebyshevBoundedField.cc:276
lsst::jointcal::FluxTransformSpatiallyInvariant
Photometric offset independent of position, defined as (fluxMag0)^-1.
Definition: PhotometryTransform.h:146
lsst::jointcal::FluxTransformSpatiallyInvariant::clone
std::shared_ptr< PhotometryTransform > clone() const override
return a copy (allocated by new) of the transformation.
Definition: PhotometryTransform.h:160
lsst::jointcal::FluxTransformChebyshev::transform
double transform(double x, double y, double value) const override
Return the transform of value at (x,y).
Definition: PhotometryTransform.h:311
lsst::jointcal::PhotometryTransform::operator<<
friend std::ostream & operator<<(std::ostream &s, PhotometryTransform const &transform)
Definition: PhotometryTransform.h:71
lsst::jointcal::PhotometryTransformSpatiallyInvariant::getNpar
std::size_t getNpar() const override
Return the number of parameters (used to compute chisq)
Definition: PhotometryTransform.h:120
lsst::jointcal::PhotometryTransform::getNpar
virtual std::size_t getNpar() const =0
Return the number of parameters (used to compute chisq)
lsst::jointcal::MagnitudeTransformChebyshev::MagnitudeTransformChebyshev
MagnitudeTransformChebyshev(size_t order, geom::Box2D const &bbox)
Definition: PhotometryTransform.h:333
lsst::jointcal::FluxTransformSpatiallyInvariant::FluxTransformSpatiallyInvariant
FluxTransformSpatiallyInvariant(double value=1)
Definition: PhotometryTransform.h:148
AffineTransform.h
lsst::jointcal::PhotometryTransform::getParameters
virtual Eigen::VectorXd getParameters() const =0
Get a copy of the parameters of this model, in the same order as offsetParams.
lsst::jointcal::FluxTransformSpatiallyInvariant::transform
double transform(double x, double y, double value) const override
Return the transform of value at (x,y).
Definition: PhotometryTransform.h:152
lsst::jointcal::PhotometryTransformChebyshev::transformError
double transformError(double x, double y, double value, double valueErr) const override
Return the transformed valueErr at Point(x,y).
Definition: PhotometryTransform.h:244
lsst::jointcal::FluxTransformChebyshev::clone
std::shared_ptr< PhotometryTransform > clone() const override
return a copy (allocated by new) of the transformation.
Definition: PhotometryTransform.h:323
lsst::jointcal::MagnitudeTransformSpatiallyInvariant::transformError
double transformError(double x, double y, double value, double valueErr) const override
Return the transformed valueErr at Point(x,y).
Definition: PhotometryTransform.h:187
lsst::jointcal::MagnitudeTransformChebyshev::transform
double transform(double x, double y, double value) const override
Return the transform of value at (x,y).
Definition: PhotometryTransform.h:340
lsst::jointcal::MagnitudeTransformChebyshev::MagnitudeTransformChebyshev
MagnitudeTransformChebyshev(ndarray::Array< double, 2, 2 > const &coefficients, geom::Box2D const &bbox)
Definition: PhotometryTransform.h:336
lsst::jointcal::MagnitudeTransformSpatiallyInvariant
Photometric offset independent of position, defined as -2.5 * log(flux / fluxMag0).
Definition: PhotometryTransform.h:178
lsst::jointcal::PhotometryTransformChebyshev::print
void print(std::ostream &out) const override
Print the transform coefficients to stream.
Definition: PhotometryTransform.h:247
lsst::jointcal::PhotometryTransformSpatiallyInvariant::PhotometryTransformSpatiallyInvariant
PhotometryTransformSpatiallyInvariant(double value)
Definition: PhotometryTransform.h:112
lsst::jointcal::PhotometryTransformSpatiallyInvariant::print
void print(std::ostream &out) const override
Print the transform coefficients to stream.
Definition: PhotometryTransform.h:115
lsst::jointcal::MagnitudeTransformSpatiallyInvariant::clone
std::shared_ptr< PhotometryTransform > clone() const override
return a copy (allocated by new) of the transformation.
Definition: PhotometryTransform.h:192
lsst::geom::AffineTransform
An affine coordinate transformation consisting of a linear transformation and an offset.
Definition: AffineTransform.h:75
lsst::jointcal::Point::y
double y
Definition: Point.h:41
lsst::jointcal::PhotometryTransformChebyshev::getOrder
ndarray::Size getOrder() const
Definition: PhotometryTransform.h:261
lsst::jointcal::FluxTransformChebyshev::FluxTransformChebyshev
FluxTransformChebyshev(ndarray::Array< double, 2, 2 > const &coefficients, geom::Box2D const &bbox)
Definition: PhotometryTransform.h:307
lsst::jointcal::PhotometryTransformChebyshev::getParameters
Eigen::VectorXd getParameters() const override
Get a copy of the parameters of this model, in the same order as offsetParams.
Definition: PhotometryTransform.cc:210
lsst::jointcal::PhotometryTransform::computeParameterDerivatives
virtual void computeParameterDerivatives(double x, double y, double value, Eigen::Ref< Eigen::VectorXd > derivatives) const =0
Compute the derivatives with respect to the parameters (i.e.
lsst::jointcal::PhotometryTransformSpatiallyInvariant::getValue
double getValue() const
Definition: PhotometryTransform.h:133
Point.h
std::ostream
STL class.
lsst::jointcal::FluxTransformChebyshev::FluxTransformChebyshev
FluxTransformChebyshev(size_t order, geom::Box2D const &bbox)
Definition: PhotometryTransform.h:304
x
double x
Definition: ChebyshevBoundedField.cc:277
lsst::jointcal::PhotometryTransformChebyshev::offsetParams
void offsetParams(Eigen::VectorXd const &delta) override
Offset the parameters by some (negative) amount during fitting.
Definition: PhotometryTransform.cc:107
lsst::jointcal::FluxTransformChebyshev::computeParameterDerivatives
void computeParameterDerivatives(double x, double y, double value, Eigen::Ref< Eigen::VectorXd > derivatives) const override
Compute the derivatives with respect to the parameters (i.e.
Definition: PhotometryTransform.h:316
lsst::jointcal::PhotometryTransformChebyshev::getCoefficients
ndarray::Array< double, 2, 2 > getCoefficients() const
Get a copy of the coefficients of the polynomials, as a 2d array (NOTE: layout is [y][x])
Definition: PhotometryTransform.h:256
lsst::jointcal::PhotometryTransformChebyshev::getBBox
geom::Box2D getBBox() const
Definition: PhotometryTransform.h:263
lsst::jointcal::PhotometryTransformChebyshev::mean
double mean() const
Definition: PhotometryTransform.cc:208
lsst::jointcal::PhotometryTransformChebyshev::computeChebyshev
double computeChebyshev(double x, double y) const
Return the value of this polynomial at x,y.
Definition: PhotometryTransform.cc:224
lsst::jointcal
Definition: Associations.h:49
lsst::jointcal::PhotometryTransformSpatiallyInvariant::offsetParams
void offsetParams(Eigen::VectorXd const &delta) override
Offset the parameters by some (negative) amount during fitting.
Definition: PhotometryTransform.h:123
lsst::jointcal::FluxTransformSpatiallyInvariant::computeParameterDerivatives
void computeParameterDerivatives(double x, double y, double value, Eigen::Ref< Eigen::VectorXd > derivatives) const override
Compute the derivatives with respect to the parameters (i.e.
Definition: PhotometryTransform.h:165
lsst::jointcal::MagnitudeTransformSpatiallyInvariant::transform
double transform(double x, double y, double mag) const override
Return the transform of value at (x,y).
Definition: PhotometryTransform.h:184
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::jointcal::MagnitudeTransformChebyshev::clone
std::shared_ptr< PhotometryTransform > clone() const override
return a copy (allocated by new) of the transformation.
Definition: PhotometryTransform.h:352
lsst::jointcal::PhotometryTransform::print
virtual void print(std::ostream &out) const =0
Print the transform coefficients to stream.
lsst::jointcal::PhotometryTransform::transform
double transform(Point const &in, double value) const
Return the transformed value at Point(x,y).
Definition: PhotometryTransform.h:58
lsst::jointcal::PhotometryTransform::transformError
double transformError(Point const &in, double value, double valueErr) const
Return the transformed valueErr at Point(x,y).
Definition: PhotometryTransform.h:64
lsst::jointcal::PhotometryTransform::transform
virtual double transform(double x, double y, double value) const =0
Return the transform of value at (x,y).
lsst::jointcal::PhotometryTransformChebyshev::integrate
double integrate() const
Definition: PhotometryTransform.cc:193
lsst::jointcal::Point
A point in a plane.
Definition: Point.h:36
lsst::jointcal::PhotometryTransform
A photometric transform, defined in terms of the input flux or magnitude.
Definition: PhotometryTransform.h:52
std::size_t
Point.h
lsst::jointcal::MagnitudeTransformChebyshev
nth-order 2d Chebyshev photometry transform, plus the input flux.
Definition: PhotometryTransform.h:331
lsst::jointcal::FluxTransformChebyshev
nth-order 2d Chebyshev photometry transform, times the input flux.
Definition: PhotometryTransform.h:302
lsst::jointcal::FluxTransformSpatiallyInvariant::transformError
double transformError(double x, double y, double value, double valueErr) const override
Return the transformed valueErr at Point(x,y).
Definition: PhotometryTransform.h:155
lsst::jointcal::Point::x
double x
coordinate
Definition: Point.h:41
lsst::geom::Box2D
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
lsst::jointcal::MagnitudeTransformSpatiallyInvariant::MagnitudeTransformSpatiallyInvariant
MagnitudeTransformSpatiallyInvariant(double value=0)
Definition: PhotometryTransform.h:180
lsst::jointcal::PhotometryTransform::transformError
virtual double transformError(double x, double y, double value, double valueErr) const =0
Return the transformed valueErr at Point(x,y).
lsst::jointcal::MagnitudeTransformSpatiallyInvariant::computeParameterDerivatives
void computeParameterDerivatives(double x, double y, double value, Eigen::Ref< Eigen::VectorXd > derivatives) const override
Compute the derivatives with respect to the parameters (i.e.
Definition: PhotometryTransform.h:197
lsst::jointcal::PhotometryTransformSpatiallyInvariant
Photometry offset independent of position.
Definition: PhotometryTransform.h:110
lsst::jointcal::PhotometryTransformChebyshev::getNpar
std::size_t getNpar() const override
Return the number of parameters (used to compute chisq)
Definition: PhotometryTransform.h:250
lsst::jointcal::PhotometryTransformSpatiallyInvariant::getParameters
Eigen::VectorXd getParameters() const override
Get a copy of the parameters of this model, in the same order as offsetParams.
Definition: PhotometryTransform.h:126
bbox
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
lsst::jointcal::PhotometryTransformChebyshev::computeChebyshevDerivatives
void computeChebyshevDerivatives(double x, double y, Eigen::Ref< Eigen::VectorXd > derivatives) const
Set the derivatives of this polynomial at x,y.
Definition: PhotometryTransform.cc:230