Skip to content

Commit

Permalink
Use pybind wrappers, merge into one pybind shared lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Wittgen committed Feb 10, 2023
1 parent cc3ba21 commit 59b00ab
Show file tree
Hide file tree
Showing 19 changed files with 534 additions and 461 deletions.
28 changes: 16 additions & 12 deletions python/lsst/ip/diffim/SConscript
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
## -*- python -*-
from lsst.sconsUtils import scripts
scripts.BasicSConscript.pybind11([
"basisLists",
"findSetBits",
"imageStatistics",
"imageSubtract",
"kernelCandidate",
"kernelCandidateDetection",
"kernelSolution",
], addUnderscore=False)

# Plugin registration fails if this does not have an underscore
scripts.BasicSConscript.pybind11(["dipoleAlgorithms"])
scripts.BasicSConscript.python(['_ipdiffimLib'], [
'_ipdiffimLib.cc',
'basisLists.cc',
'dipoleAlgorithms.cc',
'findSetBits.cc',
'imageStatistics.cc',
'imageSubtract.cc',
'kernelCandidate.cc',
'kernelCandidateDetection.cc',
'kernelSolution.cc',
'detail/assessSpatialKernelVisitor.cc',
'detail/buildSingleKernelVisitor.cc',
'detail/buildSpatialKernelVisitor.cc',
'detail/kernelPca.cc',
'detail/kernelSumVisitor.cc'
])
76 changes: 76 additions & 0 deletions python/lsst/ip/diffim/_ipdiffimLib.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of ip_diffim.
*
* Developed for the LSST Data Management System.
* This product includes software developed by the LSST Project
* (https://www.lsst.org).
* See the COPYRIGHT file at the top-level directory of this distribution
* for details of code ownership.
*
* 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 GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"

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

namespace lsst {
namespace ip {
namespace diffim {
void wrapBasisLists(WrapperCollection &wrappers);
void wrapDipoleAlgorithms(WrapperCollection &wrappers);
void wrapFindSetBits(WrapperCollection &wrappers);
void wrapImageStatistics(WrapperCollection &wrappers);
void wrapImageSubtract(WrapperCollection &wrappers);
void wrapKernelCandidate(WrapperCollection &wrappers);
void wrapKernelCandidateDetection(WrapperCollection &wrappers);
void wrapKernelSolution(WrapperCollection &wrappers);
namespace detail {
void wrapAssessSpatialKernelVisitor(WrapperCollection &wrappers);
void wrapBuildSingleKernelVisitor(WrapperCollection &wrappers);
void wrapBuildSpatialKernelVisitor(WrapperCollection &wrappers);
void wrapKernelPca(WrapperCollection &wrappers);
void wrapKernelSumVisitor(WrapperCollection &wrappers);
}
PYBIND11_MODULE(_ipdiffimLib, mod) {
lsst::utils::python::WrapperCollection wrappers(mod, "ip.diffim");
wrappers.addInheritanceDependency("lsst.meas.base");
wrappers.addInheritanceDependency("lsst.afw.math");
wrappers.addSignatureDependency("lsst.afw.table");
wrappers.addSignatureDependency("lsst.afw.image");
wrappers.addSignatureDependency("lsst.afw.geom");
wrappers.addSignatureDependency("lsst.daf.base");
wrappers.addSignatureDependency("lsst.afw.detection");

wrapBasisLists(wrappers);
wrapDipoleAlgorithms(wrappers);
wrapFindSetBits(wrappers);
wrapImageStatistics(wrappers);
wrapImageSubtract(wrappers);
wrapKernelCandidate(wrappers);
wrapKernelCandidateDetection(wrappers);
wrapKernelSolution(wrappers);
detail::wrapAssessSpatialKernelVisitor(wrappers);
detail::wrapBuildSingleKernelVisitor(wrappers);
detail::wrapBuildSpatialKernelVisitor(wrappers);
detail::wrapKernelPca(wrappers);
detail::wrapKernelSumVisitor(wrappers);
wrappers.finish();
}
}
}
}
7 changes: 3 additions & 4 deletions python/lsst/ip/diffim/basisLists.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/eigen.h"
#include "pybind11/stl.h"

Expand All @@ -36,10 +37,8 @@ namespace lsst {
namespace ip {
namespace diffim {

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

void wrapBasisLists(lsst::cpputils::python::WrapperCollection &wrappers) {
auto &mod = wrappers.module;
mod.def("makeDeltaFunctionBasisList", &makeDeltaFunctionBasisList, "width"_a, "height"_a);
mod.def("makeRegularizationMatrix", &makeRegularizationMatrix, "ps"_a);
mod.def("makeForwardDifferenceMatrix", &makeForwardDifferenceMatrix, "width"_a, "height"_a, "orders"_a,
Expand Down
9 changes: 0 additions & 9 deletions python/lsst/ip/diffim/detail/SConscript

This file was deleted.

2 changes: 1 addition & 1 deletion python/lsst/ip/diffim/detail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
from .diffimDetailLib import *
from .._ipdiffimLib import *
39 changes: 20 additions & 19 deletions python/lsst/ip/diffim/detail/assessSpatialKernelVisitor.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 <memory>
#include <string>
Expand All @@ -45,32 +46,32 @@ namespace {
* @param[in] suffix Class name suffix associated with PixeT, e.g. "F" for `float`
*/
template <typename PixelT>
void declareAssessSpatialKernelVisitor(py::module& mod, std::string const& suffix) {
py::class_<AssessSpatialKernelVisitor<PixelT>, std::shared_ptr<AssessSpatialKernelVisitor<PixelT>>,
lsst::afw::math::CandidateVisitor>
cls(mod, ("AssessSpatialKernelVisitor" + suffix).c_str());
void declareAssessSpatialKernelVisitor(lsst::cpputils::python::WrapperCollection &wrappers, std::string const& suffix) {
using PyCLass = py::class_<AssessSpatialKernelVisitor<PixelT>, std::shared_ptr<AssessSpatialKernelVisitor<PixelT>>,
lsst::afw::math::CandidateVisitor>;

cls.def(py::init<std::shared_ptr<afw::math::LinearCombinationKernel>,
afw::math::Kernel::SpatialFunctionPtr, daf::base::PropertySet const&>(),
"spatialKernel"_a, "spatialBackground"_a, "ps"_a);
std::string name = "AssessSpatialKernelVisitor" + suffix;

cls.def("reset", &AssessSpatialKernelVisitor<PixelT>::reset);
cls.def("getNGood", &AssessSpatialKernelVisitor<PixelT>::getNGood);
cls.def("getNRejected", &AssessSpatialKernelVisitor<PixelT>::getNRejected);
cls.def("getNProcessed", &AssessSpatialKernelVisitor<PixelT>::getNProcessed);
cls.def("processCandidate", &AssessSpatialKernelVisitor<PixelT>::processCandidate, "candidate"_a);
wrappers.wrapType(PyCLass(wrappers.module, name.c_str()), [](auto &mod, auto &cls) {
cls.def(py::init<std::shared_ptr<afw::math::LinearCombinationKernel>,
afw::math::Kernel::SpatialFunctionPtr, daf::base::PropertySet const &>(),
"spatialKernel"_a, "spatialBackground"_a, "ps"_a);

mod.def("makeAssessSpatialKernelVisitor", &makeAssessSpatialKernelVisitor<PixelT>, "spatialKernel"_a,
"spatialBackground"_a, "ps"_a);
cls.def("reset", &AssessSpatialKernelVisitor<PixelT>::reset);
cls.def("getNGood", &AssessSpatialKernelVisitor<PixelT>::getNGood);
cls.def("getNRejected", &AssessSpatialKernelVisitor<PixelT>::getNRejected);
cls.def("getNProcessed", &AssessSpatialKernelVisitor<PixelT>::getNProcessed);
cls.def("processCandidate", &AssessSpatialKernelVisitor<PixelT>::processCandidate, "candidate"_a);

mod.def("makeAssessSpatialKernelVisitor", &makeAssessSpatialKernelVisitor<PixelT>, "spatialKernel"_a,
"spatialBackground"_a, "ps"_a);
});
}

} // namespace lsst::ip::diffim::detail::<anonymous>

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

declareAssessSpatialKernelVisitor<float>(mod, "F");
void wrapAssessSpatialKernelVisitor(lsst::cpputils::python::WrapperCollection &wrappers) {
declareAssessSpatialKernelVisitor<float>(wrappers, "F");
}

} // detail
Expand Down
55 changes: 28 additions & 27 deletions python/lsst/ip/diffim/detail/buildSingleKernelVisitor.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/eigen.h"
#include "pybind11/stl.h"

Expand Down Expand Up @@ -52,40 +53,40 @@ namespace {
* @param[in] suffix Class name suffix associated with PixeT, e.g. "F" for `float`
*/
template <typename PixelT>
void declareBuildSingleKernelVisitor(py::module& mod, std::string const& suffix) {
py::class_<BuildSingleKernelVisitor<PixelT>, std::shared_ptr<BuildSingleKernelVisitor<PixelT>>,
afw::math::CandidateVisitor>
cls(mod, ("BuildSingleKernelVisitor" + suffix).c_str());
void declareBuildSingleKernelVisitor(lsst::cpputils::python::WrapperCollection &wrappers, std::string const& suffix) {
using PyClass = py::class_<BuildSingleKernelVisitor<PixelT>, std::shared_ptr<BuildSingleKernelVisitor<PixelT>>,
afw::math::CandidateVisitor>;
std::string name = "BuildSingleKernelVisitor" + suffix;

cls.def(py::init<afw::math::KernelList, daf::base::PropertySet const&>(), "basisList"_a, "ps"_a);
cls.def(py::init<afw::math::KernelList, daf::base::PropertySet const&, Eigen::MatrixXd const&>(), "basisList"_a,
"ps"_a, "hMat"_a);
wrappers.wrapType(PyClass(wrappers.module, name.c_str()), [](auto &mod, auto &cls) {
cls.def(py::init<afw::math::KernelList, daf::base::PropertySet const &>(), "basisList"_a, "ps"_a);
cls.def(py::init<afw::math::KernelList, daf::base::PropertySet const &, Eigen::MatrixXd const &>(),
"basisList"_a,
"ps"_a, "hMat"_a);

cls.def("setSkipBuilt", &BuildSingleKernelVisitor<PixelT>::setSkipBuilt, "skip"_a);
cls.def("getNRejected", &BuildSingleKernelVisitor<PixelT>::getNRejected);
cls.def("getNProcessed", &BuildSingleKernelVisitor<PixelT>::getNProcessed);
cls.def("reset", &BuildSingleKernelVisitor<PixelT>::reset);
cls.def("processCandidate", &BuildSingleKernelVisitor<PixelT>::processCandidate, "candidate"_a);
cls.def("setSkipBuilt", &BuildSingleKernelVisitor<PixelT>::setSkipBuilt, "skip"_a);
cls.def("getNRejected", &BuildSingleKernelVisitor<PixelT>::getNRejected);
cls.def("getNProcessed", &BuildSingleKernelVisitor<PixelT>::getNProcessed);
cls.def("reset", &BuildSingleKernelVisitor<PixelT>::reset);
cls.def("processCandidate", &BuildSingleKernelVisitor<PixelT>::processCandidate, "candidate"_a);

mod.def("makeBuildSingleKernelVisitor",
(std::shared_ptr<BuildSingleKernelVisitor<PixelT>>(*)(afw::math::KernelList const&,
daf::base::PropertySet const&)) &
makeBuildSingleKernelVisitor<PixelT>,
"basisList"_a, "ps"_a);
mod.def("makeBuildSingleKernelVisitor",
(std::shared_ptr<BuildSingleKernelVisitor<PixelT>>(*)(
afw::math::KernelList const&, daf::base::PropertySet const&, Eigen::MatrixXd const&)) &
makeBuildSingleKernelVisitor<PixelT>,
"basisList"_a, "ps"_a, "hMat"_a);
mod.def("makeBuildSingleKernelVisitor",
(std::shared_ptr<BuildSingleKernelVisitor<PixelT>>(*)(afw::math::KernelList const &,
daf::base::PropertySet const &)) &
makeBuildSingleKernelVisitor<PixelT>,
"basisList"_a, "ps"_a);
mod.def("makeBuildSingleKernelVisitor",
(std::shared_ptr<BuildSingleKernelVisitor<PixelT>>(*)(
afw::math::KernelList const &, daf::base::PropertySet const &, Eigen::MatrixXd const &)) &
makeBuildSingleKernelVisitor<PixelT>,
"basisList"_a, "ps"_a, "hMat"_a);
});
}

} // namespace lsst::ip::diffim::detail::<anonymous>

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

declareBuildSingleKernelVisitor<float>(mod, "F");
void wrapBuildSingleKernelVisitor(lsst::cpputils::python::WrapperCollection &wrappers) {
declareBuildSingleKernelVisitor<float>(wrappers, "F");
}

} // detail
Expand Down
38 changes: 18 additions & 20 deletions python/lsst/ip/diffim/detail/buildSpatialKernelVisitor.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 Down Expand Up @@ -50,32 +51,29 @@ namespace {
* @param[in] suffix Class name suffix associated with PixeT, e.g. "F" for `float`
*/
template <typename PixelT>
void declareBuildSpatialKernelVisitor(py::module& mod, std::string const& suffix) {
py::class_<BuildSpatialKernelVisitor<PixelT>, std::shared_ptr<BuildSpatialKernelVisitor<PixelT>>,
afw::math::CandidateVisitor>
cls(mod, ("BuildSpatialKernelVisitor" + suffix).c_str());
void declareBuildSpatialKernelVisitor(lsst::cpputils::python::WrapperCollection &wrappers, std::string const& suffix) {
std::string name = "BuildSpatialKernelVisitor" + suffix;
using PyClass = py::class_<BuildSpatialKernelVisitor<PixelT>, std::shared_ptr<BuildSpatialKernelVisitor<PixelT>>,
afw::math::CandidateVisitor>;
wrappers.wrapType(PyClass(wrappers.module, name.c_str()), [](auto &mod, auto &cls) {
cls.def(py::init<afw::math::KernelList, geom::Box2I const &, daf::base::PropertySet const &>(), "basisList"_a,
"regionBBox"_a, "ps"_a);

cls.def(py::init<afw::math::KernelList, geom::Box2I const&, daf::base::PropertySet const&>(), "basisList"_a,
"regionBBox"_a, "ps"_a);
cls.def("getNCandidates", &BuildSpatialKernelVisitor<PixelT>::getNCandidates);
cls.def("processCandidate", &BuildSpatialKernelVisitor<PixelT>::processCandidate, "candidate"_a);
cls.def("solveLinearEquation", &BuildSpatialKernelVisitor<PixelT>::solveLinearEquation);
cls.def("getKernelSolution", &BuildSpatialKernelVisitor<PixelT>::getKernelSolution);
cls.def("getSolutionPair", &BuildSpatialKernelVisitor<PixelT>::getSolutionPair);

cls.def("getNCandidates", &BuildSpatialKernelVisitor<PixelT>::getNCandidates);
cls.def("processCandidate", &BuildSpatialKernelVisitor<PixelT>::processCandidate, "candidate"_a);
cls.def("solveLinearEquation", &BuildSpatialKernelVisitor<PixelT>::solveLinearEquation);
cls.def("getKernelSolution", &BuildSpatialKernelVisitor<PixelT>::getKernelSolution);
cls.def("getSolutionPair", &BuildSpatialKernelVisitor<PixelT>::getSolutionPair);

mod.def("makeBuildSpatialKernelVisitor", &makeBuildSpatialKernelVisitor<PixelT>, "basisList"_a,
"regionBBox"_a, "ps"_a);
mod.def("makeBuildSpatialKernelVisitor", &makeBuildSpatialKernelVisitor<PixelT>, "basisList"_a,
"regionBBox"_a, "ps"_a);
});
}

} // namespace lsst::ip::diffim::detail::<anonymous>

PYBIND11_MODULE(buildSpatialKernelVisitor, mod) {
py::module::import("lsst.afw.math");
py::module::import("lsst.afw.geom");
py::module::import("lsst.daf.base");

declareBuildSpatialKernelVisitor<float>(mod, "F");
void wrapBuildSpatialKernelVisitor(lsst::cpputils::python::WrapperCollection &wrappers) {
declareBuildSpatialKernelVisitor<float>(wrappers, "F");
}

} // detail
Expand Down
6 changes: 0 additions & 6 deletions python/lsst/ip/diffim/detail/diffimDetailLib.py

This file was deleted.

0 comments on commit 59b00ab

Please sign in to comment.