26 #include "ndarray/eigen.h" 58 _toChebyshevRange(makeChebyshevRangeTransform(geom::Box2D(bbox))),
59 _coefficients(coefficients) {}
76 void evaluateBasis1d(ndarray::Array<double, 1, 1>
const& t,
double x) {
77 int const n = t.getSize<0>();
84 for (
int i = 2; i < n; ++i) {
85 t[i] = 2.0*x*t[i-1] - t[i-2];
93 ndarray::Array<double, 2, 2> makeMatrix(ndarray::Array<double const, 1>
const& x,
94 ndarray::Array<double const,1>
const & y,
96 Control
const& ctrl) {
97 int const nPoints = x.getSize<0>();
98 ndarray::Array<double,1,1> tx = ndarray::allocate(packer.nx);
99 ndarray::Array<double,1,1> ty = ndarray::allocate(packer.ny);
100 ndarray::Array<double,2,2> out = ndarray::allocate(nPoints, packer.size);
103 for (
int p = 0; p < nPoints; ++p) {
105 evaluateBasis1d(tx, sxy.getX());
106 evaluateBasis1d(ty, sxy.getY());
107 packer.pack(out[p], tx, ty);
116 ndarray::Array<double, 2, 2> makeMatrix(
geom::Box2I const& bbox,
118 Control
const& ctrl) {
120 ndarray::Array<double,2,2> tx = ndarray::allocate(bbox.
getWidth(), packer.nx);
128 ndarray::Array<double,2,2> out = ndarray::allocate(bbox.
getArea(),packer.size);
129 ndarray::Array<double,2,2>::Iterator outIter = out.begin();
130 ndarray::Array<double,1,1> ty = ndarray::allocate(ctrl.
orderY + 1);
134 for (
int j = 0; j < bbox.
getWidth(); ++j, ++outIter) {
136 packer.pack(*outIter, tx[j], ty);
145 ndarray::Array<double const,1>
const &
x,
146 ndarray::Array<double const,1>
const & y,
147 ndarray::Array<double const,1>
const & z,
153 Packer
const packer(ctrl);
155 ndarray::Array<double,2,2> matrix = makeMatrix(x, y, result->_toChebyshevRange, packer, ctrl);
159 result->_coefficients = packer.unpack(lstsq.
getSolution());
164 ndarray::Array<double const,1>
const &
x,
165 ndarray::Array<double const,1>
const & y,
166 ndarray::Array<double const,1>
const & z,
167 ndarray::Array<double const,1>
const & w,
173 Packer
const packer(ctrl);
175 ndarray::Array<double,2,2> matrix = makeMatrix(x, y, result->_toChebyshevRange, packer, ctrl);
178 matrix.asEigen<Eigen::ArrayXpr>().colwise() *= w.asEigen<Eigen::ArrayXpr>();
179 ndarray::Array<double,1,1> wz = ndarray::copy(z);
180 wz.asEigen<Eigen::ArrayXpr>() *= w.asEigen<Eigen::ArrayXpr>();
184 result->_coefficients = packer.unpack(lstsq.
getSolution());
188 template <
typename T>
196 Packer
const packer(ctrl);
197 ndarray::Array<double,2,2> matrix = makeMatrix(bbox, result->_toChebyshevRange, packer, ctrl);
199 ndarray::Array<double,2,2> imgCopy = ndarray::allocate(img.
getArray().getShape());
201 ndarray::Array<double const,1,1> z = ndarray::flatten<1>(imgCopy);
205 result->_coefficients = packer.unpack(lstsq.getSolution());
212 if (static_cast<std::size_t>(ctrl.
orderX) >= _coefficients.getSize<1>()) {
214 (boost::format(
"New x order (%d) exceeds old x order (%d)") % ctrl.
orderX %
215 (_coefficients.getSize<1>() -
218 if (static_cast<std::size_t>(ctrl.
orderY) >= _coefficients.getSize<0>()) {
220 (boost::format(
"New y order (%d) exceeds old y order (%d)") % ctrl.
orderY %
221 (_coefficients.getSize<0>() -
225 coefficients.deep() = _coefficients[ndarray::view(0, ctrl.
orderY + 1)(0, ctrl.
orderX + 1)];
228 ndarray::Array<double,1,1> packed = ndarray::allocate(packer.size);
229 packer.pack(packed, coefficients);
230 packer.unpack(coefficients, packed);
236 return std::make_shared<ChebyshevBoundedField>(
bbox, _coefficients);
247 template <
typename CoeffGetter>
248 double evaluateFunction1d(CoeffGetter g,
double x,
int size) {
249 double b_kp2=0.0, b_kp1=0.0;
250 for (
int k = (size - 1); k > 0; --k) {
251 double b_k = g[k] + 2*x*b_kp1 - b_kp2;
255 return g[0] + x*b_kp1 - b_kp2;
263 struct RecursionArrayImitator {
264 double operator[](
int i)
const {
268 RecursionArrayImitator(ndarray::Array<double const, 2, 2>
const& coefficients_,
double x_)
279 return evaluateFunction1d(RecursionArrayImitator(_coefficients, p.getX()), p.getY(),
280 _coefficients.getSize<0>());
289 return 2.0/(1.0 - double(n*n));
295 for (ndarray::Size j=0; j < _coefficients.getSize<0>(); j++){
296 for (ndarray::Size i=0; i < _coefficients.getSize<1>(); i++){
300 return result * determinant;
309 struct PersistenceHelper {
315 PersistenceHelper(
int nx,
int ny)
317 orderX(schema.addField<int>(
"order_x",
"maximum Chebyshev function order in x")),
318 bbox(table::
Box2IKey::addFields(schema,
"bbox",
"bounding box",
"pixel")),
319 coefficients(schema.addField<table::Array<double> >(
320 "coefficients",
"Chebyshev function coefficients, ordered by y then x", nx * ny)) {}
322 PersistenceHelper(table::Schema
const& s)
329 class ChebyshevBoundedFieldFactory :
public table::io::PersistableFactory {
332 : afw::table::io::PersistableFactory(name) {}
335 CatalogVector
const& catalogs)
const {
338 table::BaseRecord
const & record = catalogs.front().front();
339 PersistenceHelper
const keys(record.getSchema());
340 geom::Box2I
bbox(record.get(keys.bbox));
341 int nx = record.get(keys.orderX) + 1;
342 int ny = keys.coefficients.getSize() / nx;
344 ndarray::Array<double,2,2>
coefficients = ndarray::allocate(ny, nx);
345 ndarray::flatten<1>(
coefficients) = record.get(keys.coefficients);
350 std::string getChebyshevBoundedFieldPersistenceName() {
return "ChebyshevBoundedField"; }
352 ChebyshevBoundedFieldFactory registration(getChebyshevBoundedFieldPersistenceName());
357 return getChebyshevBoundedFieldPersistenceName();
363 PersistenceHelper
const keys(_coefficients.getSize<1>(), _coefficients.getSize<0>());
366 record->set(keys.orderX, _coefficients.getSize<1>() - 1);
367 record->set(keys.bbox,
getBBox());
368 (*record)[keys.coefficients].deep() = ndarray::flatten<1>(_coefficients);
380 if (!rhsCasted)
return false;
382 return (
getBBox() == rhsCasted->getBBox()) &&
383 (_coefficients.getShape() == rhsCasted->_coefficients.getShape()) &&
384 all(equal(_coefficients, rhsCasted->_coefficients));
387 std::string ChebyshevBoundedField::toString()
const {
389 os <<
"ChebyshevBoundedField (" << _coefficients.getShape() <<
" coefficients in y,x)";
397 #define INSTANTIATE(T) \ 398 template std::shared_ptr<ChebyshevBoundedField> ChebyshevBoundedField::fit(image::Image<T> const& image, \
virtual double integrate() const
Compute the integral of this function over its bounding-box.
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
table::Key< std::string > name
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
An object passed to Persistable::write to allow it to persist itself.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
A custom container class for records, based on std::vector.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
double integrateTn(int n)
def scale(algorithm, min, max=None, frame=None)
A control object used when fitting ChebyshevBoundedField to data (see ChebyshevBoundedField::fit) ...
virtual double evaluate(geom::Point2D const &position) const
Evaluate the field at the given point.
Solver for linear least-squares problems.
std::shared_ptr< ChebyshevBoundedField > relocate(geom::Box2I const &bbox) const
Return a new ChebyshevBoundedField with domain set to the given bounding box.
ndarray::Array< double const, 2, 2 > getCoefficients() const
Return the coefficient matrix.
static LeastSquares fromDesignMatrix(ndarray::Array< T1, 2, C1 > const &design, ndarray::Array< T2, 1, C2 > const &data, Factorization factorization=NORMAL_EIGENSYSTEM)
Initialize from the design matrix and data vector given as ndarrays.
std::shared_ptr< ChebyshevBoundedField > truncate(Control const &ctrl) const
Return a new ChebyshevBoudedField with maximum orders set by the given control object.
An integer coordinate rectangle.
BoxKey< geom::Box2I > Box2IKey
geom::Box2I getBBox() const
Return the bounding box that defines the region where the field is valid.
int computeSize() const
Return the number of nonzero coefficients in the Chebyshev function defined by this object...
ChebyshevBoundedFieldControl Control
BoundedField(BoundedField const &)=default
Use the normal equations with a symmetric Eigensystem decomposition.
int orderY
"maximum Chebyshev function order in y" ;
A base class for image defects.
double getCenterX() const
#define INSTANTIATE(FROMSYS, TOSYS)
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
bool triangular
"if true, only include terms where the sum of the x and y order " "is less than or equal to max(order...
#define LSST_EXCEPT(type,...)
A BoundedField based on 2-d Chebyshev polynomials of the first kind.
ChebyshevBoundedField(afw::geom::Box2I const &bbox, ndarray::Array< double const, 2, 2 > const &coefficients)
Initialize the field from its bounding box an coefficients.
virtual double mean() const
Compute the mean of this function over its bounding-box.
An abstract base class for 2-d functions defined on an integer bounding boxes.
virtual std::shared_ptr< BoundedField > operator*(double const scale) const
Return a scaled BoundedField.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
ndarray::Array< double const, 1, 1 > getSolution()
Return the vector solution to the least squares problem.
virtual bool operator==(BoundedField const &rhs) const
BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal...
double getCenterY() const
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
A floating-point coordinate rectangle geometry.
ndarray::Array< double const, 2, 2 > coefficients
static std::shared_ptr< ChebyshevBoundedField > fit(afw::geom::Box2I const &bbox, ndarray::Array< double const, 1 > const &x, ndarray::Array< double const, 1 > const &y, ndarray::Array< double const, 1 > const &z, Control const &ctrl)
Fit a Chebyshev approximation to non-gridded data with equal weights.
A class to represent a 2-dimensional array of pixels.
A helper class ChebyshevBoundedField, for mapping trapezoidal matrices to 1-d arrays.
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
int orderX
"maximum Chebyshev function order in x" ;