lsst.jointcal  16.0-26-g3163dd8+2
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_TRANSFO_H
26 #define LSST_JOINTCAL_PHOTOMETRY_TRANSFO_H
27 
28 #include <iostream>
29 #include <sstream>
30 #include <memory>
31 
32 #include "Eigen/Core"
33 #include "ndarray.h"
34 
36 #include "lsst/afw/geom/Box.h"
37 #include "lsst/afw/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 dump(std::ostream &stream = std::cout) const = 0;
70 
72  transform.dump(s);
73  return s;
74  }
75 
77  virtual int getNpar() const = 0;
78 
87  virtual void offsetParams(Eigen::VectorXd const &delta) = 0;
88 
90  virtual std::shared_ptr<PhotometryTransform> clone() const = 0;
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 dump(std::ostream &stream = std::cout) const override { stream << std::setprecision(10) << _value; }
116 
118  int getNpar() const override { return 1; }
119 
121  void offsetParams(Eigen::VectorXd const &delta) override { _value -= delta[0]; };
122 
124  Eigen::VectorXd getParameters() const override {
125  Eigen::VectorXd parameters(1);
126  parameters[0] = _value;
127  return parameters;
128  }
129 
130 protected:
131  double getValue() const { return _value; }
132 
133 private:
135  double _value;
136 };
137 
145 public:
147 
149  double transform(double x, double y, double value) const override { return value * getValue(); }
150 
152  double transformError(double x, double y, double value, double valueErr) const override {
153  return getValue() * valueErr;
154  }
155 
158  return std::make_shared<FluxTransfoSpatiallyInvariant>(getValue());
159  }
160 
162  void computeParameterDerivatives(double x, double y, double value,
163  Eigen::Ref<Eigen::VectorXd> derivatives) const override {
164  // the derivative of a spatially constant transform w.r.t. that value is just the value.
165  derivatives[0] = value;
166  }
167 };
168 
176 public:
177  explicit MagnitudeTransfoSpatiallyInvariant(double value = 0)
179 
181  double transform(double x, double y, double mag) const override { return mag + getValue(); }
182 
184  double transformError(double x, double y, double value, double valueErr) const override {
185  return valueErr;
186  }
187 
190  return std::make_shared<MagnitudeTransfoSpatiallyInvariant>(getValue());
191  }
192 
194  void computeParameterDerivatives(double x, double y, double value,
195  Eigen::Ref<Eigen::VectorXd> derivatives) const override {
196  // the derivative of a spatially constant transform w.r.t. that value is 1.
197  derivatives[0] = 1;
198  }
199 };
200 
219 public:
227  PhotometryTransformChebyshev(size_t order, afw::geom::Box2D const &bbox, bool identity);
228 
238  PhotometryTransformChebyshev(ndarray::Array<double, 2, 2> const &coefficients,
239  afw::geom::Box2D const &bbox);
240 
242  double transformError(double x, double y, double value, double valueErr) const override { return 0; }
243 
245  void dump(std::ostream &stream = std::cout) const override { stream << _coefficients; }
246 
248  int getNpar() const override { return _nParameters; }
249 
251  void offsetParams(Eigen::VectorXd const &delta) override;
252 
254  ndarray::Array<double, 2, 2> getCoefficients() const { return ndarray::copy(_coefficients); }
255 
257  Eigen::VectorXd getParameters() const override;
258 
259  ndarray::Size getOrder() const { return _order; }
260 
261  afw::geom::Box2D getBBox() const { return _bbox; }
262 
263  double mean() const;
264 
265 protected:
269  double computeChebyshev(double x, double y) const;
270 
275  void computeChebyshevDerivatives(double x, double y, Eigen::Ref<Eigen::VectorXd> derivatives) const;
276 
277 private:
278  afw::geom::Box2D _bbox; // the domain of this function
279  afw::geom::AffineTransform _toChebyshevRange; // maps points from the bbox to [-1,1]x[-1,1]
280 
281  ndarray::Array<double, 2, 2> _coefficients; // shape=(order+1, order+1)
282  ndarray::Size _order;
283  ndarray::Size _nParameters;
284 
285  // Compute the integral of this function over its bounding-box.
286  double integrate() const;
287 };
288 
293 public:
294  FluxTransfoChebyshev(size_t order, afw::geom::Box2D const &bbox)
295  : PhotometryTransformChebyshev(order, bbox, true) {}
296 
297  FluxTransfoChebyshev(ndarray::Array<double, 2, 2> const &coefficients, afw::geom::Box2D const &bbox)
298  : PhotometryTransformChebyshev(coefficients, bbox) {}
299 
301  double transform(double x, double y, double value) const override {
302  return value * computeChebyshev(x, y);
303  }
304 
306  void computeParameterDerivatives(double x, double y, double value,
307  Eigen::Ref<Eigen::VectorXd> derivatives) const override {
308  computeChebyshevDerivatives(x, y, derivatives);
309  derivatives *= value;
310  }
311 
314  return std::make_shared<FluxTransfoChebyshev>(getCoefficients(), getBBox());
315  }
316 };
317 
322 public:
323  MagnitudeTransfoChebyshev(size_t order, afw::geom::Box2D const &bbox)
324  : PhotometryTransformChebyshev(order, bbox, false) {}
325 
326  MagnitudeTransfoChebyshev(ndarray::Array<double, 2, 2> const &coefficients, afw::geom::Box2D const &bbox)
327  : PhotometryTransformChebyshev(coefficients, bbox) {}
328 
330  double transform(double x, double y, double value) const override {
331  return value + computeChebyshev(x, y);
332  }
333 
335  void computeParameterDerivatives(double x, double y, double value,
336  Eigen::Ref<Eigen::VectorXd> derivatives) const override {
337  // The derivatives here are independent of value
338  computeChebyshevDerivatives(x, y, derivatives);
339  }
340 
343  return std::make_shared<FluxTransfoChebyshev>(getCoefficients(), getBBox());
344  }
345 };
346 
347 } // namespace jointcal
348 } // namespace lsst
349 
350 #endif // LSST_JOINTCAL_PHOTOMETRY_TRANSFO_H
double transformError(double x, double y, double value, double valueErr) const override
Return the transformed valueErr at Point(x,y).
Photometric offset independent of position, defined as -2.5 * log(flux / fluxMag0).
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]) ...
double transform(double x, double y, double mag) const override
Return the transform of value at (x,y).
void dump(std::ostream &stream=std::cout) const override
dumps the transform coefficients to stream.
virtual Eigen::VectorXd getParameters() const =0
Get a copy of the parameters of this model, in the same order as offsetParams.
void offsetParams(Eigen::VectorXd const &delta) override
Offset the parameters by some (negative) amount during fitting.
A point in a plane.
Definition: Point.h:36
double transform(double x, double y, double value) const override
Return the transform of value at (x,y).
virtual std::shared_ptr< PhotometryTransform > clone() const =0
return a copy (allocated by new) of the transformation.
table::Box2IKey bbox
std::shared_ptr< PhotometryTransform > clone() const override
return a copy (allocated by new) of the transformation.
double x
int getNpar() const override
Return the number of parameters (used to compute chisq)
friend std::ostream & operator<<(std::ostream &s, PhotometryTransform const &transform)
Photometry offset independent of position.
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.
nth-order 2d Chebyshev photometry transform, times the input flux.
int getNpar() const override
Return the number of parameters (used to compute chisq)
A photometric transform, defined in terms of the input flux or magnitude.
Photometric offset independent of position, defined as (fluxMag0)^-1.
MagnitudeTransfoChebyshev(size_t order, afw::geom::Box2D const &bbox)
double x
coordinate
Definition: Point.h:41
ndarray::Array< double const, 2, 2 > coefficients
std::shared_ptr< PhotometryTransform > clone() const override
return a copy (allocated by new) of the transformation.
double transform(double x, double y, double value) const override
Return the transform of value at (x,y).
Eigen::VectorXd getParameters() const override
Get a copy of the parameters of this model, in the same order as offsetParams.
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.
Class for a simple mapping implementing a generic AstrometryTransform.
double transform(Point const &in, double value) const
Return the transformed value at Point(x,y).
void dump(std::ostream &stream=std::cout) const override
dumps the transform coefficients to stream.
double transformError(Point const &in, double value, double valueErr) const
Return the transformed valueErr at Point(x,y).
virtual double transformError(double x, double y, double value, double valueErr) const =0
Return the transformed valueErr at Point(x,y).
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.
FluxTransfoChebyshev(size_t order, afw::geom::Box2D const &bbox)
double transformError(double x, double y, double value, double valueErr) const override
Return the transformed valueErr at Point(x,y).
FluxTransfoChebyshev(ndarray::Array< double, 2, 2 > const &coefficients, afw::geom::Box2D const &bbox)
MagnitudeTransfoChebyshev(ndarray::Array< double, 2, 2 > const &coefficients, afw::geom::Box2D const &bbox)
virtual void offsetParams(Eigen::VectorXd const &delta)=0
Offset the parameters by some (negative) amount during fitting.
virtual void dump(std::ostream &stream=std::cout) const =0
dumps the transform coefficients to stream.
std::shared_ptr< PhotometryTransform > clone() const override
return a copy (allocated by new) of the transformation.
virtual double transform(double x, double y, double value) const =0
Return the transform of value at (x,y).
nth-order 2d Chebyshev photometry transform.
double transform(double x, double y, double value) const override
Return the transform of value at (x,y).
std::shared_ptr< PhotometryTransform > clone() const override
return a copy (allocated by new) of the transformation.
double transformError(double x, double y, double value, double valueErr) const override
Return the transformed valueErr at Point(x,y).
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.
T setprecision(T... args)
nth-order 2d Chebyshev photometry transform, plus the input flux.
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.
STL class.
int y
UnaryFunctionT::result_type integrate(UnaryFunctionT func, typename UnaryFunctionT::argument_type const a, typename UnaryFunctionT::argument_type const b, double eps=1.0e-6)
virtual int getNpar() const =0
Return the number of parameters (used to compute chisq)