26 #include "ndarray/eigen.h" 58 _toChebyshevRange(makeChebyshevRangeTransform(
lsst::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 Packer
const& packer, 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);
118 Packer
const& packer, 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>() - 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>() - 1))
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)
326 class ChebyshevBoundedFieldFactory :
public table::io::PersistableFactory {
329 : afw::table::io::PersistableFactory(name) {}
332 CatalogVector
const& catalogs)
const {
335 table::BaseRecord
const& record = catalogs.front().front();
336 PersistenceHelper
const keys(record.getSchema());
338 int nx = record.get(
keys.orderX) + 1;
339 int ny =
keys.coefficients.getSize() / nx;
341 ndarray::Array<double, 2, 2>
coefficients = ndarray::allocate(ny, nx);
347 std::string getChebyshevBoundedFieldPersistenceName() {
return "ChebyshevBoundedField"; }
349 ChebyshevBoundedFieldFactory registration(getChebyshevBoundedFieldPersistenceName());
354 return getChebyshevBoundedFieldPersistenceName();
360 PersistenceHelper
const keys(_coefficients.getSize<1>(), _coefficients.getSize<0>());
363 record->set(keys.orderX, _coefficients.getSize<1>() - 1);
364 record->set(keys.bbox,
getBBox());
365 (*record)[keys.coefficients].deep() = ndarray::flatten<1>(_coefficients);
377 if (!rhsCasted)
return false;
379 return (
getBBox() == rhsCasted->getBBox()) &&
380 (_coefficients.getShape() == rhsCasted->_coefficients.getShape()) &&
381 all(equal(_coefficients, rhsCasted->_coefficients));
384 std::string ChebyshevBoundedField::toString()
const {
386 os <<
"ChebyshevBoundedField (" << _coefficients.getShape() <<
" coefficients in y,x)";
394 #define INSTANTIATE(T) \ 395 template std::shared_ptr<ChebyshevBoundedField> ChebyshevBoundedField::fit(image::Image<T> const& image, \ ChebyshevBoundedField(lsst::geom::Box2I const &bbox, ndarray::Array< double const, 2, 2 > const &coefficients)
Initialize the field from its bounding box an coefficients.
double getCenterY() const
virtual double integrate() const
Compute the integral of this function over its bounding-box.
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.
BoxKey< lsst::geom::Box2I > Box2IKey
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
virtual double evaluate(lsst::geom::Point2D const &position) const
Evaluate the field at the given point.
double integrateTn(int n)
def scale(algorithm, min, max=None, frame=None)
A control object used when fitting ChebyshevBoundedField to data (see ChebyshevBoundedField::fit) ...
Solver for linear least-squares problems.
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.
double getCenterX() const
ndarray::Array< double const, 2, 2 > coefficients
int computeSize() const
Return the number of nonzero coefficients in the Chebyshev function defined by this object...
ChebyshevBoundedFieldControl Control
bool all(CoordinateExpr< N > const &expr)
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.
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
#define INSTANTIATE(FROMSYS, TOSYS)
lsst::geom::Box2I getBBox() const
Return the bounding box that defines the region where the field is valid.
std::shared_ptr< ChebyshevBoundedField > relocate(lsst::geom::Box2I const &bbox) const
Return a new ChebyshevBoundedField with domain set to the given bounding box.
std::unique_ptr< SchemaItem< U > > result
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,...)
ndarray::Array< double const, 2, 2 > getCoefficients() const
Return the coefficient matrix.
A BoundedField based on 2-d Chebyshev polynomials of the first kind.
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...
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
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.
static std::shared_ptr< ChebyshevBoundedField > fit(lsst::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.
int orderX
"maximum Chebyshev function order in x" ;