35 #include "boost/format.hpp"
36 #include "lsst/pex/exceptions.h"
42 namespace ex = pex::exceptions;
47 : _style(style), _orderX(orderX), _orderY(orderY < 0 ? orderX : orderY), _weighting(weighting) {
48 if (_orderX != _orderY) {
49 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
50 str(boost::format(
"X- and Y-orders must be equal (%d != %d) "
51 "due to a limitation in math::Chebyshev1Function2") %
60 template <
typename PixelT>
61 class ApproximateChebyshev :
public Approximate<PixelT> {
64 std::vector<double>
const& yVec,
70 virtual ~ApproximateChebyshev();
73 math::Chebyshev1Function2<double>
_poly;
75 ApproximateChebyshev(std::vector<double>
const& xVec, std::vector<double>
const& yVec,
77 ApproximateControl
const& ctrl);
78 virtual std::shared_ptr<image::Image<typename Approximate<PixelT>::OutPixelT>> doGetImage(
79 int orderX,
int orderY)
const;
80 virtual std::shared_ptr<image::MaskedImage<typename Approximate<PixelT>::OutPixelT>> doGetMaskedImage(
81 int orderX,
int orderY)
const;
87 inline void solveMatrix_Eigen(Eigen::MatrixXd& a, Eigen::VectorXd& b, Eigen::Map<Eigen::VectorXd>& c) {
88 Eigen::PartialPivLU<Eigen::MatrixXd> lu(a);
96 template <
typename PixelT>
97 ApproximateChebyshev<PixelT>::ApproximateChebyshev(
98 std::vector<double>
const& xVec,
99 std::vector<double>
const& yVec,
101 geom::Box2I
const& bbox,
102 ApproximateControl
const& ctrl
104 : Approximate<PixelT>(xVec, yVec, bbox, ctrl),
105 _poly(math::Chebyshev1Function2<double>(ctrl.getOrderX(), geom::Box2D(bbox))) {
108 std::vector<double>
const& coeffs =
_poly.getParameters();
109 assert(std::accumulate(coeffs.begin(), coeffs.end(), 0.0) ==
113 int const nTerm =
_poly.getNParameters();
125 std::vector<std::vector<double>> termCoeffs(nTerm);
127 for (
int i = 0; i != nTerm; ++i) {
128 termCoeffs[i].reserve(nData);
131 for (
int iy = 0; iy != im.
getHeight(); ++iy) {
132 double const y = yVec[iy];
134 for (
int ix = 0; ix != im.
getWidth(); ++ix) {
135 double const x = xVec[ix];
137 for (
int i = 0; i != nTerm; ++i) {
138 _poly.setParameter(i, 1.0);
139 termCoeffs[i].push_back(
_poly(x, y));
140 _poly.setParameter(i, 0.0);
146 A.setZero(nTerm, nTerm);
153 for (
int iy = 0; iy != im.
getHeight(); ++iy) {
156 ptr != end; ++ptr, ++alpha) {
157 double const val = ptr.image();
158 double const ivar = ctrl.getWeighting() ? 1 / ptr.variance() : 1.0;
159 if (!std::isfinite(val + ivar)) {
163 for (
int i = 0; i != nTerm; ++i) {
164 double const c_i = termCoeffs[i][alpha];
165 double const tmp = c_i * ivar;
167 A(i, i) += c_i * tmp;
168 for (
int j = 0; j < i; ++j) {
169 double const c_j = termCoeffs[j][alpha];
170 A(i, j) += c_j * tmp;
176 if (ctrl.getWeighting()) {
177 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
178 "No valid points to fit. Variance is likely zero. Try weighting=False");
180 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
181 "No valid points to fit (even though weighting is False). "
182 "Check that binSize & approxOrderX settings are appropriate for image size.");
186 for (
int j = 0; j != nTerm; ++j) {
187 for (
int i = j + 1; i != nTerm; ++i) {
194 std::vector<double> cvec(nTerm);
195 Eigen::Map<Eigen::VectorXd> c(&cvec[0], nTerm);
197 solveMatrix_Eigen(A, b, c);
199 _poly.setParameters(cvec);
203 template <
typename PixelT>
204 ApproximateChebyshev<PixelT>::~ApproximateChebyshev() {}
216 template <
typename PixelT>
217 std::shared_ptr<image::Image<typename Approximate<PixelT>::OutPixelT>>
218 ApproximateChebyshev<PixelT>::doGetImage(
int orderX,
int orderY)
const {
219 if (orderX < 0) orderX = Approximate<PixelT>::_ctrl.getOrderX();
220 if (orderY < 0) orderY = Approximate<PixelT>::_ctrl.getOrderY();
222 math::Chebyshev1Function2<double> poly = (orderX == Approximate<PixelT>::_ctrl.getOrderX() &&
223 orderY == Approximate<PixelT>::_ctrl.getOrderY())
225 :
_poly.truncate(orderX);
230 for (
int iy = 0; iy != im->
getHeight(); ++iy) {
234 for (
typename ImageT::x_iterator ptr = im->
row_begin(iy), end = im->
row_end(iy); ptr != end;
254 template <
typename PixelT>
255 std::shared_ptr<image::MaskedImage<typename Approximate<PixelT>::OutPixelT>>
256 ApproximateChebyshev<PixelT>::doGetMaskedImage(
int orderX,
int orderY)
const {
260 std::shared_ptr<typename MImageT::Image> im = mi->
getImage();
262 for (
int iy = 0; iy != im->getHeight(); ++iy) {
266 for (
typename MImageT::Image::x_iterator ptr = im->row_begin(iy), end = im->row_end(iy); ptr != end;
278 template <
typename PixelT>
280 std::vector<double>
const& y,
285 case ApproximateControl::CHEBYSHEV:
286 return std::shared_ptr<Approximate<PixelT>>(
287 new ApproximateChebyshev<PixelT>(
x, y, im, bbox, ctrl));
289 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
290 str(boost::format(
"Unknown ApproximationStyle: %d") % ctrl.
getStyle()));
298 #define INSTANTIATE(PIXEL_T) \
299 template std::shared_ptr<Approximate<PIXEL_T>> makeApproximate( \
300 std::vector<double> const& x, std::vector<double> const& y, \
301 image::MaskedImage<PIXEL_T> const& im, geom::Box2I const& bbox, ApproximateControl const& ctrl)
x_iterator row_begin(int y) const
Return an x_iterator to the start of the image.
math::Chebyshev1Function2< double > _poly
int getHeight() const
Return the number of rows in the image.
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
std::shared_ptr< Approximate< PixelT > > makeApproximate(std::vector< double > const &x, std::vector< double > const &y, image::MaskedImage< PixelT > const &im, geom::Box2I const &bbox, ApproximateControl const &ctrl)
Construct a new Approximate object, inferring the type from the type of the given MaskedImage...
Approximate values for a MaskedImage.
An integer coordinate rectangle.
Control how to make an approximation.
Style getStyle() const
Return the Style.
geom::Box2I const & _bbox
A class to manipulate images, masks, and variance as a single object.
Style
Choose the type of approximation to use.
ApproximateControl(Style style, int orderX, int orderY=-1, bool weighting=true)
ctor
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
int getWidth() const
Return the number of columns in the image.
A class to represent a 2-dimensional array of pixels.