24 #include "pybind11/pybind11.h" 26 #include "ndarray/pybind11.h" 44 using PyOptimizerObjective = py::class_<OptimizerObjective, std::shared_ptr<OptimizerObjective>>;
45 using PyOptimizerControl = py::class_<OptimizerControl, std::shared_ptr<OptimizerControl>>;
46 using PyOptimizerHistoryRecorder =
47 py::class_<OptimizerHistoryRecorder, std::shared_ptr<OptimizerHistoryRecorder>>;
48 using PyOptimizer = py::class_<Optimizer, std::shared_ptr<Optimizer>>;
50 static PyOptimizerObjective declareOptimizerObjective(
py::module &mod) {
51 PyOptimizerObjective
cls(mod,
"OptimizerObjective");
53 cls.def_readonly(
"dataSize", &OptimizerObjective::dataSize);
54 cls.def_readonly(
"parameterSize", &OptimizerObjective::parameterSize);
55 cls.def_static(
"makeFromLikelihood", &OptimizerObjective::makeFromLikelihood,
"likelihood"_a,
58 cls.def(
"fillObjectiveValueGrid", &OptimizerObjective::fillObjectiveValueGrid,
"parameters"_a,
60 cls.def(
"computeResiduals", &OptimizerObjective::computeResiduals,
"parameters"_a,
"residuals"_a);
61 cls.def(
"differentiateResiduals", &OptimizerObjective::differentiateResiduals,
"parameters"_a,
63 cls.def(
"hasPrior", &OptimizerObjective::hasPrior);
64 cls.def(
"computePrior", &OptimizerObjective::computePrior,
"parameters"_a);
65 cls.def(
"differentiatePrior", &OptimizerObjective::differentiatePrior,
"parameters"_a,
"gradient"_a,
70 static PyOptimizerControl declareOptimizerControl(
py::module &mod) {
71 PyOptimizerControl
cls(mod,
"OptimizerControl");
90 cls.def(py::init<>());
94 static PyOptimizerHistoryRecorder declareOptimizerHistoryRecorder(
py::module &mod) {
95 PyOptimizerHistoryRecorder
cls(mod,
"OptimizerHistoryRecorder");
97 "doRecordDerivatives"_a);
98 cls.def(py::init<afw::table::Schema const &>(),
"schema"_a);
99 cls.def(
"apply", &OptimizerHistoryRecorder::apply,
"outerIterCount"_a,
"innerIterCount"_a,
"history"_a,
101 cls.def(
"unpackDerivatives",
102 (
void (OptimizerHistoryRecorder::*)(ndarray::Array<Scalar const, 1, 1>
const &,
103 ndarray::Array<Scalar, 1, 1>
const &,
104 ndarray::Array<Scalar, 2, 2>
const &)
const) &
105 OptimizerHistoryRecorder::unpackDerivatives,
106 "nested"_a,
"gradient"_a,
"hessian"_a);
107 cls.def(
"unpackDerivatives", (
void (OptimizerHistoryRecorder::*)(
108 afw::table::BaseRecord
const &, ndarray::Array<Scalar, 1, 1>
const &,
109 ndarray::Array<Scalar, 2, 2>
const &)
const) &
110 OptimizerHistoryRecorder::unpackDerivatives,
111 "record"_a,
"gradient"_a,
"hessian"_a);
114 cls.def(
"fillObjectiveModelGrid", &OptimizerHistoryRecorder::fillObjectiveModelGrid,
"record"_a,
115 "parameters"_a,
"output"_a);
116 cls.def_readonly(
"outer", &OptimizerHistoryRecorder::outer);
117 cls.def_readonly(
"inner", &OptimizerHistoryRecorder::inner);
118 cls.def_readonly(
"state", &OptimizerHistoryRecorder::state);
119 cls.def_readonly(
"objective", &OptimizerHistoryRecorder::objective);
120 cls.def_readonly(
"prior", &OptimizerHistoryRecorder::prior);
121 cls.def_readonly(
"trust", &OptimizerHistoryRecorder::trust);
122 cls.def_readonly(
"parameters", &OptimizerHistoryRecorder::parameters);
123 cls.def_readonly(
"derivatives", &OptimizerHistoryRecorder::derivatives);
127 static PyOptimizer declareOptimizer(
py::module &mod) {
128 PyOptimizer
cls(mod,
"Optimizer");
130 cls.attr(
"CONVERGED_GRADZERO") = py::cast(
int(Optimizer::CONVERGED_GRADZERO));
131 cls.attr(
"CONVERGED_TR_SMALL") = py::cast(
int(Optimizer::CONVERGED_TR_SMALL));
132 cls.attr(
"CONVERGED") = py::cast(
int(Optimizer::CONVERGED));
133 cls.attr(
"FAILED_MAX_INNER_ITERATIONS") = py::cast(
int(Optimizer::FAILED_MAX_INNER_ITERATIONS));
134 cls.attr(
"FAILED_MAX_OUTER_ITERATIONS") = py::cast(
int(Optimizer::FAILED_MAX_OUTER_ITERATIONS));
135 cls.attr(
"FAILED_MAX_ITERATIONS") = py::cast(
int(Optimizer::FAILED_MAX_ITERATIONS));
136 cls.attr(
"FAILED_EXCEPTION") = py::cast(
int(Optimizer::FAILED_EXCEPTION));
137 cls.attr(
"FAILED_NAN") = py::cast(
int(Optimizer::FAILED_NAN));
138 cls.attr(
"FAILED") = py::cast(
int(Optimizer::FAILED));
139 cls.attr(
"STATUS_STEP_REJECTED") = py::cast(
int(Optimizer::STATUS_STEP_REJECTED));
140 cls.attr(
"STATUS_STEP_ACCEPTED") = py::cast(
int(Optimizer::STATUS_STEP_ACCEPTED));
141 cls.attr(
"STATUS_STEP") = py::cast(
int(Optimizer::STATUS_STEP));
142 cls.attr(
"STATUS_TR_UNCHANGED") = py::cast(
int(Optimizer::STATUS_TR_UNCHANGED));
143 cls.attr(
"STATUS_TR_DECREASED") = py::cast(
int(Optimizer::STATUS_TR_DECREASED));
144 cls.attr(
"STATUS_TR_INCREASED") = py::cast(
int(Optimizer::STATUS_TR_INCREASED));
145 cls.attr(
"STATUS_TR") = py::cast(
int(Optimizer::STATUS_TR));
146 cls.attr(
"STATUS") = py::cast(
int(Optimizer::STATUS));
149 "objective"_a,
"parameters"_a,
"ctrl"_a);
150 cls.def(
"getObjective", &Optimizer::getObjective);
151 cls.def(
"getControl", &Optimizer::getControl, py::return_value_policy::copy);
155 "recorder"_a,
"history"_a);
159 "recorder"_a,
"history"_a);
160 cls.def(
"getState", &Optimizer::getState);
161 cls.def(
"getObjectiveValue", &Optimizer::getObjectiveValue);
162 cls.def(
"getParameters", &Optimizer::getParameters);
163 cls.def(
"getResiduals", &Optimizer::getResiduals);
164 cls.def(
"getGradient", &Optimizer::getGradient);
165 cls.def(
"getHessian", &Optimizer::getHessian);
166 cls.def(
"removeSR1Term", &Optimizer::removeSR1Term);
171 py::module::import(
"lsst.meas.modelfit.model");
172 py::module::import(
"lsst.meas.modelfit.likelihood");
173 py::module::import(
"lsst.meas.modelfit.priors");
175 auto clsObjective = declareOptimizerObjective(mod);
176 auto clsControl = declareOptimizerControl(mod);
177 auto clsHistoryRecorder = declareOptimizerHistoryRecorder(mod);
178 auto cls = declareOptimizer(mod);
179 cls.attr(
"Objective") = clsObjective;
180 cls.attr(
"Control") = clsControl;
181 cls.attr(
"HistoryRecorder") = clsHistoryRecorder;
183 mod.def(
"solveTrustRegion", &
solveTrustRegion,
"x"_a,
"F"_a,
"g"_a,
"r"_a,
"tolerance"_a);
CatalogT< BaseRecord > BaseCatalog
PYBIND11_MODULE(camera, mod)
A base class for image defects.
#define LSST_DECLARE_CONTROL_FIELD(WRAPPER, CLASS, NAME)
Macro used to wrap fields declared by LSST_CONTROL_FIELD using Pybind11.
void solveTrustRegion(ndarray::Array< Scalar, 1, 1 > const &x, ndarray::Array< Scalar const, 2, 1 > const &F, ndarray::Array< Scalar const, 1, 1 > const &g, double r, double tolerance)
Solve a symmetric quadratic matrix equation with a ball constraint.