22#include "pybind11/pybind11.h"
24#include "pybind11/eigen.h"
34using namespace pybind11::literals;
45void declareKernelSolution(lsst::cpputils::python::WrapperCollection &wrappers) {
46 using PyKernelSolution = py::class_<KernelSolution, std::shared_ptr<KernelSolution>>;
48 wrappers.
wrapType(PyKernelSolution(wrappers.
module,
"KernelSolution"), [](
auto &mod,
auto &cls) {
49 cls.def(py::init<Eigen::MatrixXd, Eigen::VectorXd, bool>(),
"mMat"_a,
"bVec"_a,
"fitForBackground"_a);
50 cls.def(py::init<bool>(),
"fitForBackground"_a);
51 cls.def(py::init<>());
53 py::enum_<KernelSolution::KernelSolvedBy>(cls,
"KernelSolvedBy")
54 .value(
"NONE", KernelSolution::KernelSolvedBy::NONE)
55 .value(
"CHOLESKY_LDLT", KernelSolution::KernelSolvedBy::CHOLESKY_LDLT)
56 .value(
"CHOLESKY_LLT", KernelSolution::KernelSolvedBy::CHOLESKY_LLT)
57 .value(
"LU", KernelSolution::KernelSolvedBy::LU)
58 .value(
"EIGENVECTOR", KernelSolution::KernelSolvedBy::EIGENVECTOR)
61 py::enum_<KernelSolution::ConditionNumberType>(cls,
"ConditionNumberType")
62 .value(
"EIGENVALUE", KernelSolution::ConditionNumberType::EIGENVALUE)
63 .value(
"SVD", KernelSolution::ConditionNumberType::SVD)
66 cls.def(
"solve", (void (KernelSolution::*)()) &KernelSolution::solve);
67 cls.def(
"solve", (void (KernelSolution::*)(Eigen::MatrixXd const &, Eigen::VectorXd const &)) &
68 KernelSolution::solve,
70 cls.def(
"getSolvedBy", &KernelSolution::getSolvedBy);
71 cls.def(
"getConditionNumber", (double (KernelSolution::*)(KernelSolution::ConditionNumberType)) &
72 KernelSolution::getConditionNumber,
74 cls.def(
"getConditionNumber",
75 (double (KernelSolution::*)(Eigen::MatrixXd const &, KernelSolution::ConditionNumberType)) &
76 KernelSolution::getConditionNumber,
77 "mMat"_a,
"conditionType"_a);
78 cls.def(
"getM", &KernelSolution::getM, py::return_value_policy::copy);
79 cls.def(
"getB", &KernelSolution::getB, py::return_value_policy::copy);
80 cls.def(
"printM", &KernelSolution::printM);
81 cls.def(
"printB", &KernelSolution::printB);
82 cls.def(
"printA", &KernelSolution::printA);
83 cls.def(
"getId", &KernelSolution::getId);
94template <
typename InputT>
95void declareStaticKernelSolution(lsst::cpputils::python::WrapperCollection &wrappers, std::string
const &suffix) {
96 using PyStaticKernelSolution =
97 py::class_<StaticKernelSolution<InputT>, std::shared_ptr<StaticKernelSolution<InputT>>,
KernelSolution>;
98 std::string
name = (
"StaticKernelSolution" + suffix);
99 wrappers.
wrapType(PyStaticKernelSolution(wrappers.
module,
name.c_str()), [](
auto &mod,
auto &cls) {
100 cls.def(py::init<lsst::afw::math::KernelList const &, bool>(),
"basisList"_a,
"fitForBackground"_a);
102 cls.def(
"solve", (void (StaticKernelSolution<InputT>::*)()) &StaticKernelSolution<InputT>::solve);
103 cls.def(
"build", &StaticKernelSolution<InputT>::build,
"templateImage"_a,
"scienceImage"_a,
104 "varianceEstimate"_a);
105 cls.def(
"getKernel", &StaticKernelSolution<InputT>::getKernel);
106 cls.def(
"makeKernelImage", &StaticKernelSolution<InputT>::makeKernelImage);
107 cls.def(
"getBackground", &StaticKernelSolution<InputT>::getBackground);
108 cls.def(
"getKsum", &StaticKernelSolution<InputT>::getKsum);
109 cls.def(
"getSolutionPair", &StaticKernelSolution<InputT>::getSolutionPair);
120template <
typename InputT>
121void declareMaskedKernelSolution(lsst::cpputils::python::WrapperCollection &wrappers, std::string
const &suffix) {
122 using PyMaskedKernelSolution = py::class_<MaskedKernelSolution<InputT>, std::shared_ptr<MaskedKernelSolution<InputT>>,
124 std::string
name =
"MaskedKernelSolution" + suffix;
125 wrappers.
wrapType(PyMaskedKernelSolution(wrappers.
module,
name.c_str()), [](
auto &mod,
auto &cls) {
126 cls.def(py::init<lsst::afw::math::KernelList const &, bool>(),
"basisList"_a,
"fitForBackground"_a);
128 cls.def(
"buildOrig", &MaskedKernelSolution<InputT>::buildOrig,
"templateImage"_a,
"scienceImage"_a,
129 "varianceEstimate"_a,
"pixelMask"_a);
130 cls.def(
"buildWithMask", &MaskedKernelSolution<InputT>::buildWithMask,
"templateImage"_a,
131 "scienceImage"_a,
"varianceEstimate"_a,
"pixelMask"_a);
132 cls.def(
"buildSingleMaskOrig", &MaskedKernelSolution<InputT>::buildSingleMaskOrig,
"templateImage"_a,
133 "scienceImage"_a,
"varianceEstimate"_a,
"maskBox"_a);
144template <
typename InputT>
145void declareRegularizedKernelSolution(lsst::cpputils::python::WrapperCollection &wrappers, std::string
const &suffix) {
146 using PyRegularizedKernelSolution =
147 py::class_<RegularizedKernelSolution<InputT>,
150 std::string
name =
"RegularizedKernelSolution" + suffix;
151 wrappers.
wrapType(PyRegularizedKernelSolution(wrappers.
module,
"RegularizedKernelSolution"), [](
auto &mod,
auto &cls) {
152 cls.def(py::init<lsst::afw::math::KernelList const &, bool, Eigen::MatrixXd const &,
153 daf::base::PropertySet const &>(),
154 "basisList"_a,
"fitForBackground"_a,
"hMat"_a,
"ps"_a);
157 (void (RegularizedKernelSolution<InputT>::*)()) &RegularizedKernelSolution<InputT>::solve);
158 cls.def(
"getLambda", &RegularizedKernelSolution<InputT>::getLambda);
159 cls.def(
"estimateRisk", &RegularizedKernelSolution<InputT>::estimateRisk,
"maxCond"_a);
160 cls.def(
"getM", &RegularizedKernelSolution<InputT>::getM);
167void declareSpatialKernelSolution(lsst::cpputils::python::WrapperCollection &wrappers) {
168 using PySpatialKernelSolution =
169 py::class_<SpatialKernelSolution, std::shared_ptr<SpatialKernelSolution>,
KernelSolution> ;
171 wrappers.
wrapType(PySpatialKernelSolution(wrappers.
module,
"SpatialKernelSolution"), [](
auto &mod,
auto &cls) {
172 cls.def(py::init<lsst::afw::math::KernelList const &, lsst::afw::math::Kernel::SpatialFunctionPtr,
173 lsst::afw::math::Kernel::SpatialFunctionPtr, daf::base::PropertySet const &>(),
174 "basisList"_a,
"spatialKernelFunction"_a,
"background"_a,
"ps"_a);
176 cls.def(
"solve", (void (SpatialKernelSolution::*)()) &SpatialKernelSolution::solve);
177 cls.def(
"addConstraint", &SpatialKernelSolution::addConstraint,
"xCenter"_a,
"yCenter"_a,
"qMat"_a,
179 cls.def(
"makeKernelImage", &SpatialKernelSolution::makeKernelImage,
"pos"_a);
180 cls.def(
"getSolutionPair", &SpatialKernelSolution::getSolutionPair);
187 declareKernelSolution(wrappers);
188 declareStaticKernelSolution<float>(wrappers,
"F");
189 declareMaskedKernelSolution<float>(wrappers,
"F");
190 declareRegularizedKernelSolution<float>(wrappers,
"F");
191 declareSpatialKernelSolution(wrappers);
Declaration of classes to store the solution for convolution kernels.
A helper class for subdividing pybind11 module across multiple translation units (i....
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
void wrapKernelSolution(WrapperCollection &wrappers)