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-37804: Use pybind wrappers, merge into one pybind shared lib #176

Merged
merged 2 commits into from
Feb 13, 2023
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
21 changes: 13 additions & 8 deletions python/lsst/meas/astrom/SConscript
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# -*- python -*-
from lsst.sconsUtils import scripts
scripts.BasicSConscript.pybind11([
"makeMatchStatistics",
"matchOptimisticB/matchOptimisticB",
"polynomialTransform",
"scaledPolynomialTransformFitter",
"sipTransform",
"pessimisticPatternMatcherUtils",
], addUnderscore=False)
scripts.BasicSConscript.python(['_measAstromLib'], [
'_measAstromLib.cc',
'makeMatchStatistics.cc',
'matchOptimisticB/matchOptimisticB.cc',
'polynomialTransform.cc',
'scaledPolynomialTransformFitter.cc',
'sipTransform.cc',
'pessimisticPatternMatcherUtils.cc',
'sip/createWcsWithSip.cc',
'sip/leastSqFitter1d.cc',
'sip/leastSqFitter2d.cc',
'sip/matchSrcToCatalogue.cc'
])
6 changes: 1 addition & 5 deletions python/lsst/meas/astrom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from .makeMatchStatistics import *
from ._measAstromLib import *
from .matchOptimisticBTask import *
from .polynomialTransform import *
from .scaledPolynomialTransformFitter import *
from .sipTransform import *

from . import sip

from .ref_match import *
Expand Down
46 changes: 46 additions & 0 deletions python/lsst/meas/astrom/_measAstromLib.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"

namespace py = pybind11;
using namespace pybind11::literals;
using lsst::cpputils::python::WrapperCollection;

namespace lsst {
namespace meas {
namespace astrom {
namespace sip {
void wrapCreateWcsWithSip(WrapperCollection &wrappers);
void wrapLeastSqFitter1d(WrapperCollection &wrappers);
void wrapLeastSqFitter2d(WrapperCollection &wrappers);
void wrapMatchSrcToCatalogue(WrapperCollection &wrappers);
} // namespace sip

void wrapPolynomialTransform(WrapperCollection &wrappers);
void wrapScaledPolynomialTransformFitter(WrapperCollection &wrappers);
void wrapSipTransform(WrapperCollection &wrappers);
void wrapMatchOptimisticB(WrapperCollection &wrappers);
void wrapMakeMatchStatistics(WrapperCollection &wrappers);
void wrapPessimisticPatternMatcherUtils(WrapperCollection &wrappers);

PYBIND11_MODULE(_measAstromLib, mod) {
WrapperCollection wrappers(mod, "lsst.meas.astrom");

wrappers.addInheritanceDependency("lsst.afw.math");

sip::wrapLeastSqFitter1d(wrappers);
sip::wrapCreateWcsWithSip(wrappers);
sip::wrapMatchSrcToCatalogue(wrappers);
sip::wrapLeastSqFitter2d(wrappers);

wrapPolynomialTransform(wrappers);
wrapScaledPolynomialTransformFitter(wrappers);
wrapSipTransform(wrappers);
wrapMatchOptimisticB(wrappers);
wrapMakeMatchStatistics(wrappers);
wrapPessimisticPatternMatcherUtils(wrappers);
wrappers.finish();
};

} // namespace astrom
} // namespace meas
} // namespace lsst
2 changes: 1 addition & 1 deletion python/lsst/meas/astrom/fitAffineWcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import lsst.pipe.base as pipeBase
from lsst.utils.timer import timeMethod

from .makeMatchStatistics import makeMatchStatisticsInRadians
from ._measAstromLib import makeMatchStatisticsInRadians
from .setMatchDistance import setMatchDistance


Expand Down
7 changes: 4 additions & 3 deletions python/lsst/meas/astrom/fitSipDistortion.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
import lsst.afw.display
from lsst.utils.timer import timeMethod

from .scaledPolynomialTransformFitter import ScaledPolynomialTransformFitter, OutlierRejectionControl
from .sipTransform import SipForwardTransform, SipReverseTransform, makeWcs
from .makeMatchStatistics import makeMatchStatisticsInRadians
from ._measAstromLib import (OutlierRejectionControl,
ScaledPolynomialTransformFitter,
SipForwardTransform, SipReverseTransform,
makeMatchStatisticsInRadians, makeWcs)

from .setMatchDistance import setMatchDistance

Expand Down
8 changes: 4 additions & 4 deletions python/lsst/meas/astrom/makeMatchStatistics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* see <https://www.lsstcorp.org/LegalNotices/>.
*/
#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"
#include "pybind11/stl.h"

#include "lsst/meas/astrom/makeMatchStatistics.h"
Expand All @@ -33,7 +34,7 @@ namespace astrom {
namespace {

template <typename MatchT>
static void declareMakeMatchStatistics(py::module& mod) {
void declareMakeMatchStatistics(py::module& mod) {
mod.def("makeMatchStatistics", &makeMatchStatistics<MatchT>, "matchList"_a, "flags"_a,
"sctrl"_a = afw::math::StatisticsControl());
mod.def("makeMatchStatisticsInPixels", &makeMatchStatisticsInPixels<MatchT>, "wcs"_a, "matchList"_a,
Expand All @@ -44,9 +45,8 @@ static void declareMakeMatchStatistics(py::module& mod) {

} // namespace

PYBIND11_MODULE(makeMatchStatistics, mod) {
py::module::import("lsst.afw.math");

void wrapMakeMatchStatistics(lsst::cpputils::python::WrapperCollection &wrappers){
auto &mod = wrappers.module;

declareMakeMatchStatistics<afw::table::ReferenceMatch>(mod);
declareMakeMatchStatistics<afw::table::SourceMatch>(mod);
Expand Down
2 changes: 1 addition & 1 deletion python/lsst/meas/astrom/matchOptimisticB/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

from .matchOptimisticB import *
from .._measAstromLib import MatchOptimisticBControl, ProxyPair, RecordProxy, makeProxies, matchOptimisticB
94 changes: 49 additions & 45 deletions python/lsst/meas/astrom/matchOptimisticB/matchOptimisticB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* see <https://www.lsstcorp.org/LegalNotices/>.
*/
#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"
#include "pybind11/stl.h"

#include <memory>
Expand All @@ -37,72 +38,75 @@ namespace meas {
namespace astrom {
namespace {

static void declareRecordProxy(py::module &mod) {
py::class_<RecordProxy, std::shared_ptr<RecordProxy>> cls(mod, "RecordProxy");
void declareRecordProxy(lsst::cpputils::python::WrapperCollection &wrappers) {
using PyRecordProxy = py::class_<RecordProxy, std::shared_ptr<RecordProxy>> ;
wrappers.wrapType(PyRecordProxy(wrappers.module, "RecordProxy"), [](auto &mod, auto &cls) {
cls.def_readwrite("record", &RecordProxy::record);
cls.def_readwrite("position", &RecordProxy::position);
cls.def_readwrite("used", &RecordProxy::used);

cls.def_readwrite("record", &RecordProxy::record);
cls.def_readwrite("position", &RecordProxy::position);
cls.def_readwrite("used", &RecordProxy::used);
cls.def(py::init<std::shared_ptr<afw::table::SimpleRecord>, geom::Point2D const &>(), "record"_a,
"position"_a);

cls.def(py::init<std::shared_ptr<afw::table::SimpleRecord>, geom::Point2D const &>(), "record"_a,
"position"_a);
// TO DO: decide if we need to wrap operator std::shared_ptr<lsst::afw::table::SimpleRecord>()

// TO DO: decide if we need to wrap operator std::shared_ptr<lsst::afw::table::SimpleRecord>()
cls.def("__eq__", &RecordProxy::operator==, py::is_operator());
cls.def("__ne__", &RecordProxy::operator!=, py::is_operator());

cls.def("__eq__", &RecordProxy::operator==, py::is_operator());
cls.def("__ne__", &RecordProxy::operator!=, py::is_operator());

cls.def("getX", &RecordProxy::getX);
cls.def("getY", &RecordProxy::getY);
cls.def("getX", &RecordProxy::getX);
cls.def("getY", &RecordProxy::getY);
});
}

static void declareProxyPair(py::module &mod) {
py::class_<ProxyPair, std::shared_ptr<ProxyPair>> cls(mod, "ProxyPair");

cls.def_readwrite("first", &ProxyPair::first);
cls.def_readwrite("second", &ProxyPair::second);
cls.def_readwrite("distance", &ProxyPair::distance);
cls.def_readwrite("pa", &ProxyPair::pa);
void declareProxyPair(lsst::cpputils::python::WrapperCollection &wrappers) {
using PyProxyPair = py::class_<ProxyPair, std::shared_ptr<ProxyPair>>;
wrappers.wrapType(PyProxyPair(wrappers.module, "ProxyPair"), [](auto &mod, auto &cls) {
cls.def_readwrite("first", &ProxyPair::first);
cls.def_readwrite("second", &ProxyPair::second);
cls.def_readwrite("distance", &ProxyPair::distance);
cls.def_readwrite("pa", &ProxyPair::pa);

cls.def(py::init<RecordProxy const &, RecordProxy const &>(), "s1"_a, "s2"_a);
cls.def(py::init<RecordProxy const &, RecordProxy const &>(), "s1"_a, "s2"_a);
});
}

static void declareMatchOptimisticBControl(py::module &mod) {
py::class_<MatchOptimisticBControl> cls(mod, "MatchOptimisticBControl");

cls.def(py::init<>());

LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, refFluxField);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, sourceFluxField);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, numBrightStars);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, minMatchedPairs);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, matchingAllowancePix);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, maxOffsetPix);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, maxRotationDeg);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, allowedNonperpDeg);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, numPointsForShape);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, maxDeterminant);

cls.def("validate", &MatchOptimisticBControl::validate);
void declareMatchOptimisticBControl(lsst::cpputils::python::WrapperCollection &wrappers) {
using PyMatchOptimisticBControl = py::class_<MatchOptimisticBControl>;
wrappers.wrapType(PyMatchOptimisticBControl(wrappers.module, "MatchOptimisticBControl"), [](auto &mod, auto &cls) {
cls.def(py::init<>());

LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, refFluxField);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, sourceFluxField);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, numBrightStars);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, minMatchedPairs);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, matchingAllowancePix);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, maxOffsetPix);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, maxRotationDeg);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, allowedNonperpDeg);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, numPointsForShape);
LSST_DECLARE_CONTROL_FIELD(cls, MatchOptimisticBControl, maxDeterminant);

cls.def("validate", &MatchOptimisticBControl::validate);
});
}

} // namespace

PYBIND11_MODULE(matchOptimisticB, mod) {
declareRecordProxy(mod);
declareProxyPair(mod);
declareMatchOptimisticBControl(mod);
void wrapMatchOptimisticB(lsst::cpputils::python::WrapperCollection &wrappers){
declareRecordProxy(wrappers);
declareProxyPair(wrappers);
declareMatchOptimisticBControl(wrappers);

mod.def("makeProxies",
wrappers.module.def("makeProxies",
(ProxyVector(*)(afw::table::SourceCatalog const &, afw::geom::SkyWcs const &,
afw::geom::SkyWcs const &)) &
makeProxies,
"sourceCat"_a, "distortedWcs"_a, "tanWcs"_a);
mod.def("makeProxies",
wrappers.module.def("makeProxies",
(ProxyVector(*)(afw::table::SimpleCatalog const &, afw::geom::SkyWcs const &)) & makeProxies,
"posRefCat"_a, "tanWcs"_a);

mod.def("matchOptimisticB", &matchOptimisticB, "posRefCat"_a, "sourceCat"_a, "control"_a, "wcs"_a,
wrappers.module.def("matchOptimisticB", &matchOptimisticB, "posRefCat"_a, "sourceCat"_a, "control"_a, "wcs"_a,
"posRefBegInd"_a = 0, "verbose"_a = false);
}

Expand Down
23 changes: 12 additions & 11 deletions python/lsst/meas/astrom/pessimisticPatternMatcherUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/

#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"
#include "pybind11/stl.h"
#include "pybind11/eigen.h"
#include "numpy/arrayobject.h"
#include "ndarray/pybind11.h"

#include "lsst/meas/astrom/pessimisticPatternMatcherUtils.h"
Expand All @@ -38,16 +38,17 @@ namespace lsst {
namespace meas {
namespace astrom {

PYBIND11_MODULE(pessimisticPatternMatcherUtils, mod) {
py::class_<PatternResult>(mod, "PatternResult")
.def_readonly("candidate_pairs", &PatternResult::candidate_pairs)
.def_readonly("shift_rot_matrix", &PatternResult::shift_rot_matrix)
.def_readonly("cos_shift", &PatternResult::cos_shift)
.def_readonly("sin_rot", &PatternResult::sin_rot)
.def_readonly("success", &PatternResult::success);
mod.def("construct_pattern_and_shift_rot_matrix", &construct_pattern_and_shift_rot_matrix,
"src_pattern_array"_a, "src_delta_array"_a, "src_dist_array"_a, "dist_array"_a, "id_array"_a,
"reference_array"_a, "n_match"_a, "max_cos_theta_shift"_a, "max_cos_rot_sq"_a, "max_dist_rad"_a);
void wrapPessimisticPatternMatcherUtils(lsst::cpputils::python::WrapperCollection &wrappers){
wrappers.wrapType(py::class_<PatternResult>(wrappers.module, "PatternResult"), [](auto &mod, auto &cls) {
cls.def_readonly("candidate_pairs", &PatternResult::candidate_pairs);
cls.def_readonly("shift_rot_matrix", &PatternResult::shift_rot_matrix);
cls.def_readonly("cos_shift", &PatternResult::cos_shift);
cls.def_readonly("sin_rot", &PatternResult::sin_rot);
cls.def_readonly("success", &PatternResult::success);
mod.def("construct_pattern_and_shift_rot_matrix", &construct_pattern_and_shift_rot_matrix,
"src_pattern_array"_a, "src_delta_array"_a, "src_dist_array"_a, "dist_array"_a, "id_array"_a,
"reference_array"_a, "n_match"_a, "max_cos_theta_shift"_a, "max_cos_rot_sq"_a, "max_dist_rad"_a);
});
}

} // namespace astrom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import lsst.pipe.base as pipeBase

from .pessimisticPatternMatcherUtils import construct_pattern_and_shift_rot_matrix
from ._measAstromLib import construct_pattern_and_shift_rot_matrix


def _rotation_matrix_chi_sq(flattened_rot_matrix,
Expand Down