Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-10542: Replace XYTransform::linearizeTransform #245

Merged
merged 3 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ tests/stacker
tests/statistics
tests/statisticsSpeed
tests/testLib.py
tests/testSpherePoint
tests/test_spherePoint
tests/test_transform
tests/testTrapezoidalPacker
tests/testWcs
tests/testWarpGpu
tests/test_trapezoidalPacker
tests/test_wcs
tests/test_warpGpu
tests/ticket1145
tests/wcsCopy
tests/testSpanSets
tests/test_spanSets
tests/ramFitsIO_fpC-005902-r6-0677_sub.fits_imageInOut.fit
tests/ramFitsIO_fpC-005902-r6-0677_sub.fits_imageOut.fit
tests/ramFitsIO_fpC-005902-r6-0677_sub.fits_exposureRamOut.fit
Expand All @@ -152,13 +152,13 @@ tests/ramFitsIO_fpC-002570-r6-0199_sub.fits_exposureRamOut.fit
tests/ramFitsIO_fpC-002570-r6-0199_sub.fits_imageOut.fit
tests/ramFitsIO_fpC-002570-r6-0199_sub.fits_imageRamOut.fit
tests/testEigenLib.py
tests/testTableArchives
tests/test_tableArchives
tests/ramFitsIO_fpC-005902-r6-0677_sub.fits_imageRamOut.fit
tests/testSimpleTable
tests/testDistortion
tests/testSchema
tests/testFitsTables
tests/testTableAliases
tests/test_simpleTable
tests/test_distortion
tests/test_schema
tests/test_fitsTables
tests/test_tableAliases
testTable.fits
tests/data/Dest
tests/data/kernel*.boost
Expand Down
1 change: 1 addition & 0 deletions include/lsst/afw/geom.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@
#include "lsst/afw/geom/TransformMap.h"
#include "lsst/afw/geom/Endpoint.h"
#include "lsst/afw/geom/Transform.h"
#include "lsst/afw/geom/transformFactory.h"

#endif // LSST_GEOM_H
77 changes: 77 additions & 0 deletions include/lsst/afw/geom/transformFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// -*- lsst-c++ -*-

#ifndef LSST_AFW_GEOM_TRANSFORMFACTORY_H
#define LSST_AFW_GEOM_TRANSFORMFACTORY_H

/*
* LSST Data Management System
* Copyright 2008-2017 LSST Corporation.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <http://www.lsstcorp.org/LegalNotices/>.
*/

/*
* Functions for producing Transforms with commonly desired properties.
*/

#include "lsst/afw/geom/Transform.h"

namespace lsst {
namespace afw {
namespace geom {

/**
* Approximate a Transform by its local linearization.
*
* @tparam FromEndpoint, ToEndpoint The endpoints of the transform.
*
* @param original the Transform to linearize
* @param point the point at which a linear approximation is desired
* @returns a linear Transform whose value and Jacobian at `point` match those
* of `original`.
*
* @throws pex::exceptions::InvalidParameterError Thrown if `original` does not
* have a well-defined value and Jacobian at `point`
* @exceptsafe Provides basic exception safety.
*/
template <class FromEndpoint, class ToEndpoint>
Transform<FromEndpoint, ToEndpoint> linearizeTransform(
Transform<FromEndpoint, ToEndpoint> const &original,
typename Transform<FromEndpoint, ToEndpoint>::FromPoint const &point);

/*
* The correct behavior for linearization is unclear where SpherePoints are involved (see discussion on
* DM-10542). Forbid usage until somebody needs it. Note to maintainers: the template specializations MUST
* be deleted in the header for compilers to complain correctly.
*/
#define DISABLE(From, To) \
template <> \
Transform<From, To> linearizeTransform<From, To>(Transform<From, To> const &, \
Transform<From, To>::FromPoint const &) = delete;
DISABLE(GenericEndpoint, SpherePointEndpoint);
DISABLE(Point2Endpoint, SpherePointEndpoint);
DISABLE(SpherePointEndpoint, GenericEndpoint);
DISABLE(SpherePointEndpoint, Point2Endpoint);
DISABLE(SpherePointEndpoint, SpherePointEndpoint);
#undef DISABLE

} // geom
} // afw
} // lsst

#endif // LSST_AFW_GEOM_TRANSFORMFACTORY_H
1 change: 1 addition & 0 deletions python/lsst/afw/geom/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ scripts.BasicSConscript.pybind11(
'xyTransform',
'endpoint',
'transform/transform',
'transformFactory',
'skyWcs/skyWcs',
],
addUnderscore=False
Expand Down
1 change: 1 addition & 0 deletions python/lsst/afw/geom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
from .utils import *
from .endpoint import *
from .transform import *
from .transformFactory import *
from .skyWcs import *
from .readTransform import *
70 changes: 70 additions & 0 deletions python/lsst/afw/geom/transformFactory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* LSST Data Management System
* See COPYRIGHT file at the top of the source tree.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <http://www.lsstcorp.org/LegalNotices/>.
*/
#include "pybind11/pybind11.h"
#include "pybind11/stl.h" // Needed because some Endpoints are vectors

#include "numpy/arrayobject.h"
#include "ndarray/pybind11.h"

#include "lsst/afw/geom/Endpoint.h"
#include "lsst/afw/geom/transformFactory.h"

namespace py = pybind11;
using namespace py::literals;

namespace lsst {
namespace afw {
namespace geom {
namespace {

// Declare methods on Transform<FromEndpoint, ToEndpoint>
template <class FromEndpoint, class ToEndpoint>
void declareFunctionTemplates(py::module &mod) {
using Class = Transform<FromEndpoint, ToEndpoint>;

mod.def("linearizeTransform",
(Class(*)(Class const &, typename Class::FromPoint const &)) & linearizeTransform);
}

PYBIND11_PLUGIN(transformFactory) {
py::module mod("transformFactory");

py::module::import("lsst.afw.geom.transform");

// Need to import numpy for ndarray and eigen conversions
if (_import_array() < 0) {
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
return nullptr;
}

declareFunctionTemplates<GenericEndpoint, GenericEndpoint>(mod);
declareFunctionTemplates<GenericEndpoint, Point2Endpoint>(mod);
declareFunctionTemplates<Point2Endpoint, GenericEndpoint>(mod);
declareFunctionTemplates<Point2Endpoint, Point2Endpoint>(mod);

return mod.ptr();
}

} // <anonymous>
} // geom
} // afw
} // lsst
125 changes: 125 additions & 0 deletions src/geom/transformFactory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// -*- LSST-C++ -*-
/*
* LSST Data Management System
* See COPYRIGHT file at the top of the source tree.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <http://www.lsstcorp.org/LegalNotices/>.
*/

#include <sstream>
#include <cmath>

#include "astshim.h"

#include "Eigen/Core"

#include "lsst/afw/geom/Endpoint.h"
#include "lsst/afw/geom/transformFactory.h"
#include "lsst/pex/exceptions.h"

#include "ndarray.h"
#include "ndarray/eigen.h"

namespace lsst {
namespace afw {
namespace geom {

namespace {
/**
* Print a vector to a stream.
*
* The exact details of the representation are unspecified and subject to
* change, but the following may be regarded as typical:
*
* [1.0, -3.560, 42.0]
*
* @tparam T the element type. Must support stream output.
*/
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> const &v) {
os << '[';
bool first = true;
for (T element : v) {
if (first) {
first = false;
} else {
os << ", ";
}
os << element;
}
os << ']';
return os;
}

/**
* Convert a Matrix to the equivalent ndarray.
*
* @param matrix The matrix to convert.
* @returns an ndarray containing a copy of the data in `matrix`
*/
template <class T, int Rows, int Cols>
ndarray::Array<T, 2, 2> toNdArray(Eigen::Matrix<T, Rows, Cols> const &matrix) {
ndarray::Array<T, 2, 2> array = ndarray::allocate(ndarray::makeVector(matrix.rows(), matrix.cols()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be any more efficient to use the template parameters Rows and Colshere? I assume they *must* be identical tomatrix.rows()andmatrix.cols()`.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, actually. Rows and Cols may have the dummy value -1 to mean "determined at run-time", and in fact they do have this value for the matrix returned by getJacobian. (Also, I'm pretty sure that in fixed-size matrices the implementations for rows() and cols() are optimized for inlining.)

array.asEigen() = matrix;
return array;
}
} // namespace {}

template <class From, class To>
Transform<From, To> linearizeTransform(Transform<From, To> const &original,
typename Transform<From, To>::FromPoint const &inPoint) {
using Transform = Transform<From, To>;
auto fromEndpoint = original.getFromEndpoint();
auto toEndpoint = original.getToEndpoint();

auto outPoint = original.applyForward(inPoint);
auto jacobian = original.getJacobian(inPoint);
for (int i = 0; i < toEndpoint.getNAxes(); ++i) {
if (!std::isfinite(outPoint[i])) {
std::ostringstream buffer;
buffer << "Transform ill-defined: " << inPoint << " -> " << outPoint;
throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, buffer.str());
}
}
if (!jacobian.allFinite()) {
std::ostringstream buffer;
buffer << "Transform not continuous at " << inPoint << ": J = " << jacobian;
throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, buffer.str());
}

// y(x) = J (x - x0) + y0
auto map = ast::ShiftMap(fromEndpoint.dataFromPoint(inPoint))
.getInverse()
->then(ast::MatrixMap(toNdArray(jacobian)))
.then(ast::ShiftMap(toEndpoint.dataFromPoint(outPoint)));
// TODO: remove false flag as part of DM-10947
return Transform(map, false);
}

#define INSTANTIATE_FACTORIES(From, To) \
template Transform<From, To> linearizeTransform<From, To>(Transform<From, To> const &transform, \
Transform<From, To>::FromPoint const &point);

// explicit instantiations
INSTANTIATE_FACTORIES(GenericEndpoint, GenericEndpoint);
INSTANTIATE_FACTORIES(GenericEndpoint, Point2Endpoint);
INSTANTIATE_FACTORIES(Point2Endpoint, GenericEndpoint);
INSTANTIATE_FACTORIES(Point2Endpoint, Point2Endpoint);
}
}
} /* namespace lsst::afw::geom */
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/testConvolve.py → tests/test_convolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import lsst.afw.math.detail as mathDetail
import lsst.pex.exceptions as pexExcept

from testKernel import makeDeltaFunctionKernelList, makeGaussianKernelList
from test_kernel import makeDeltaFunctionKernelList, makeGaussianKernelList
from lsst.log import Log

import lsst.afw.display.ds9 as ds9
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file modified tests/test_endpoint.py
100755 → 100644
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file modified tests/test_transform.py
100755 → 100644
Empty file.