lsst.meas.modelfit  13.0-8-g8613bc8+13
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Pages
priors.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2013 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include "pybind11/pybind11.h"
25 
26 #include "numpy/arrayobject.h"
27 #include "ndarray/pybind11.h"
28 
29 #include "lsst/pex/config/python.h"
34 
35 namespace py = pybind11;
36 using namespace pybind11::literals;
37 
38 namespace lsst {
39 namespace meas {
40 namespace modelfit {
41 namespace {
42 
43 static void declarePrior(py::module &mod) {
44  using PyPrior = py::class_<Prior, std::shared_ptr<Prior>>;
45  PyPrior cls(mod, "Prior");
46  cls.def("getTag", &Prior::getTag);
47  cls.def("evaluate", &Prior::evaluate, "nonlinear"_a, "amplitudes"_a);
48  cls.def("evaluateDerivatives", &Prior::evaluateDerivatives, "nonlinear"_a, "amplitudes"_a,
49  "nonlinearGradient"_a, "amplitudeGradient"_a, "nonlinearHessian"_a, "amplitudeHessian"_a,
50  "crossHessian"_a);
51  cls.def("marginalize", &Prior::marginalize, "gradient"_a, "hessian"_a, "nonlinear"_a);
52  cls.def("maximize", &Prior::maximize, "gradient"_a, "hessian"_a, "nonlinear"_a, "amplitudes"_a);
53  cls.def("drawAmplitudes", &Prior::drawAmplitudes, "gradient"_a, "hessian"_a, "nonlinear"_a, "rng"_a,
54  "amplitudes"_a, "weights"_a, "multiplyWeights"_a = false);
55 }
56 
57 static void declareMixturePrior(py::module &mod) {
58  using Class = MixturePrior;
59  using PyClass = py::class_<Class, std::shared_ptr<Class>, Prior>;
60  PyClass cls(mod, "MixturePrior");
61  cls.def(py::init<std::shared_ptr<Mixture>, std::string const &>(), "mixture"_a, "tag"_a = "");
62  cls.def_static("getUpdateRestriction", &Class::getUpdateRestriction,
63  py::return_value_policy::reference); // returned object has static duration
64  cls.def("getMixture", &Class::getMixture);
65  // virtual methods already wrapped by Prior base class
66 }
67 
68 static void declareSemiEmpiricalPrior(py::module &mod) {
69  using Class = SemiEmpiricalPrior;
70  using Control = SemiEmpiricalPriorControl;
71  using PyControl = py::class_<Control, std::shared_ptr<Control>>;
72  using PyClass = py::class_<Class, std::shared_ptr<Class>, Prior>;
73 
74  PyControl clsControl(mod, "SemiEmpiricalPriorControl");
75  clsControl.def(py::init<>());
76  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, ellipticitySigma);
77  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, ellipticityCore);
78  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusMinOuter);
79  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusMinInner);
80  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusMu);
81  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusSigma);
82  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusNu);
83  clsControl.def("validate", &Control::validate);
84 
85  PyClass cls(mod, "SemiEmpiricalPrior");
86  cls.def(py::init<Control>(), "ctrl"_a);
87  cls.attr("Control") = clsControl;
88  // virtual methods already wrapped by Prior base class
89 }
90 
91 static void declareSoftenedLinearPrior(py::module &mod) {
92  using Class = SoftenedLinearPrior;
93  using Control = SoftenedLinearPriorControl;
94  using PyControl = py::class_<Control, std::shared_ptr<Control>>;
95  using PyClass = py::class_<Class, std::shared_ptr<Class>, Prior>;
96 
97  PyControl clsControl(mod, "SoftenedLinearPriorControl");
98  clsControl.def(py::init<>());
99  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, ellipticityMaxOuter);
100  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, ellipticityMaxInner);
101  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusMinOuter);
102  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusMinInner);
103  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusMaxOuter);
104  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusMaxInner);
105  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, logRadiusMinMaxRatio);
106 
107  PyClass cls(mod, "SoftenedLinearPrior");
108  cls.def(py::init<Control>(), "ctrl"_a);
109  cls.def("getControl", &Class::getControl, py::return_value_policy::copy);
110  cls.attr("Control") = clsControl;
111  // virtual methods already wrapped by Prior base class
112 }
113 
114 PYBIND11_PLUGIN(priors) {
115  py::module::import("lsst.meas.modelfit.mixture");
116 
117  py::module mod("priors");
118 
119  if (_import_array() < 0) {
120  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
121  return nullptr;
122  }
123 
124  declarePrior(mod);
125  declareMixturePrior(mod);
126  declareSemiEmpiricalPrior(mod);
127  declareSoftenedLinearPrior(mod);
128 
129  return mod.ptr();
130 }
131 }
132 }
133 }
134 } // namespace lsst::meas::modelfit::anonymous