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::classh<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 = py::classh<StaticKernelSolution<InputT>,
KernelSolution>;
97 std::string
name = (
"StaticKernelSolution" + suffix);
98 wrappers.
wrapType(PyStaticKernelSolution(wrappers.
module,
name.c_str()), [](
auto &mod,
auto &cls) {
99 cls.def(py::init<lsst::afw::math::KernelList const &, bool>(),
"basisList"_a,
"fitForBackground"_a);
101 cls.def(
"solve", (void (StaticKernelSolution<InputT>::*)()) &StaticKernelSolution<InputT>::solve);
102 cls.def(
"build", &StaticKernelSolution<InputT>::build,
"templateImage"_a,
"scienceImage"_a,
103 "varianceEstimate"_a);
104 cls.def(
"getKernel", &StaticKernelSolution<InputT>::getKernel);
105 cls.def(
"makeKernelImage", &StaticKernelSolution<InputT>::makeKernelImage);
106 cls.def(
"getBackground", &StaticKernelSolution<InputT>::getBackground);
107 cls.def(
"getKsum", &StaticKernelSolution<InputT>::getKsum);
108 cls.def(
"getSolutionPair", &StaticKernelSolution<InputT>::getSolutionPair);
119template <
typename InputT>
120void declareMaskedKernelSolution(lsst::cpputils::python::WrapperCollection &wrappers, std::string
const &suffix) {
122 std::string
name =
"MaskedKernelSolution" + suffix;
123 wrappers.
wrapType(PyMaskedKernelSolution(wrappers.
module,
name.c_str()), [](
auto &mod,
auto &cls) {
124 cls.def(py::init<lsst::afw::math::KernelList const &, bool>(),
"basisList"_a,
"fitForBackground"_a);
126 cls.def(
"buildOrig", &MaskedKernelSolution<InputT>::buildOrig,
"templateImage"_a,
"scienceImage"_a,
127 "varianceEstimate"_a,
"pixelMask"_a);
128 cls.def(
"buildWithMask", &MaskedKernelSolution<InputT>::buildWithMask,
"templateImage"_a,
129 "scienceImage"_a,
"varianceEstimate"_a,
"pixelMask"_a);
130 cls.def(
"buildSingleMaskOrig", &MaskedKernelSolution<InputT>::buildSingleMaskOrig,
"templateImage"_a,
131 "scienceImage"_a,
"varianceEstimate"_a,
"maskBox"_a);
142template <
typename InputT>
143void declareRegularizedKernelSolution(lsst::cpputils::python::WrapperCollection &wrappers, std::string
const &suffix) {
144 using PyRegularizedKernelSolution =
147 std::string
name =
"RegularizedKernelSolution" + suffix;
148 wrappers.
wrapType(PyRegularizedKernelSolution(wrappers.
module,
"RegularizedKernelSolution"), [](
auto &mod,
auto &cls) {
149 cls.def(py::init<lsst::afw::math::KernelList const &, bool, Eigen::MatrixXd const &,
150 daf::base::PropertySet const &>(),
151 "basisList"_a,
"fitForBackground"_a,
"hMat"_a,
"ps"_a);
154 (void (RegularizedKernelSolution<InputT>::*)()) &RegularizedKernelSolution<InputT>::solve);
155 cls.def(
"getLambda", &RegularizedKernelSolution<InputT>::getLambda);
156 cls.def(
"estimateRisk", &RegularizedKernelSolution<InputT>::estimateRisk,
"maxCond"_a);
157 cls.def(
"getM", &RegularizedKernelSolution<InputT>::getM);
164void declareSpatialKernelSolution(lsst::cpputils::python::WrapperCollection &wrappers) {
165 using PySpatialKernelSolution = py::classh<SpatialKernelSolution, KernelSolution> ;
167 wrappers.
wrapType(PySpatialKernelSolution(wrappers.
module,
"SpatialKernelSolution"), [](
auto &mod,
auto &cls) {
168 cls.def(py::init<lsst::afw::math::KernelList const &, lsst::afw::math::Kernel::SpatialFunctionPtr,
169 lsst::afw::math::Kernel::SpatialFunctionPtr, daf::base::PropertySet const &>(),
170 "basisList"_a,
"spatialKernelFunction"_a,
"background"_a,
"ps"_a);
172 cls.def(
"solve", (void (SpatialKernelSolution::*)()) &SpatialKernelSolution::solve);
173 cls.def(
"addConstraint", &SpatialKernelSolution::addConstraint,
"xCenter"_a,
"yCenter"_a,
"qMat"_a,
175 cls.def(
"makeKernelImage", &SpatialKernelSolution::makeKernelImage,
"pos"_a);
176 cls.def(
"getSolutionPair", &SpatialKernelSolution::getSolutionPair);
183 declareKernelSolution(wrappers);
184 declareStaticKernelSolution<float>(wrappers,
"F");
185 declareMaskedKernelSolution<float>(wrappers,
"F");
186 declareRegularizedKernelSolution<float>(wrappers,
"F");
187 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)