LSST Applications 26.0.0,g0265f82a02+6660c170cc,g07994bdeae+30b05a742e,g0a0026dc87+17526d298f,g0a60f58ba1+17526d298f,g0e4bf8285c+96dd2c2ea9,g0ecae5effc+c266a536c8,g1e7d6db67d+6f7cb1f4bb,g26482f50c6+6346c0633c,g2bbee38e9b+6660c170cc,g2cc88a2952+0a4e78cd49,g3273194fdb+f6908454ef,g337abbeb29+6660c170cc,g337c41fc51+9a8f8f0815,g37c6e7c3d5+7bbafe9d37,g44018dc512+6660c170cc,g4a941329ef+4f7594a38e,g4c90b7bd52+5145c320d2,g58be5f913a+bea990ba40,g635b316a6c+8d6b3a3e56,g67924a670a+bfead8c487,g6ae5381d9b+81bc2a20b4,g93c4d6e787+26b17396bd,g98cecbdb62+ed2cb6d659,g98ffbb4407+81bc2a20b4,g9ddcbc5298+7f7571301f,ga1e77700b3+99e9273977,gae46bcf261+6660c170cc,gb2715bf1a1+17526d298f,gc86a011abf+17526d298f,gcf0d15dbbd+96dd2c2ea9,gdaeeff99f8+0d8dbea60f,gdb4ec4c597+6660c170cc,ge23793e450+96dd2c2ea9,gf041782ebf+171108ac67
LSST Data Management Base Package
Loading...
Searching...
No Matches
cmodel.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"
26
27#include "ndarray/pybind11.h"
28
31
32namespace py = pybind11;
33using namespace pybind11::literals;
34
35namespace lsst {
36namespace meas {
37namespace modelfit {
38namespace {
39
40using PyCModelStageControl = py::class_<CModelStageControl, std::shared_ptr<CModelStageControl>>;
41using PyCModelControl = py::class_<CModelControl, std::shared_ptr<CModelControl>>;
42using PyCModelStageResult = py::class_<CModelStageResult, std::shared_ptr<CModelStageResult>>;
43using PyCModelResult = py::class_<CModelResult, std::shared_ptr<CModelResult>>;
44using PyCModelAlgorithm = py::class_<CModelAlgorithm, std::shared_ptr<CModelAlgorithm>>;
45
46PyCModelStageControl declareCModelStageControl(lsst::cpputils::python::WrapperCollection &wrappers) {
47 return wrappers.wrapType(PyCModelStageControl(wrappers.module, "CModelStageControl"), [](auto &mod, auto &cls) {
48 cls.def(py::init<>());
49 cls.def("getProfile", &CModelStageControl::getProfile);
50 cls.def("getModel", &CModelStageControl::getModel);
51 cls.def("getPrior", &CModelStageControl::getPrior);
52 LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, profileName);
53 LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, priorSource);
54 LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, priorName);
55 LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelStageControl, linearPriorConfig);
56 LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelStageControl, empiricalPriorConfig);
57 LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, nComponents);
58 LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, maxRadius);
59 LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, usePixelWeights);
60 LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, weightsMultiplier);
61 LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelStageControl, optimizer);
62 LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, doRecordHistory);
63 LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, doRecordTime);
64 });
65}
66
67PyCModelControl declareCModelControl(lsst::cpputils::python::WrapperCollection &wrappers) {
68 return wrappers.wrapType(PyCModelControl(wrappers.module, "CModelControl"), [](auto &mod, auto &cls) {
69 cls.def(py::init<>());
70 LSST_DECLARE_CONTROL_FIELD(cls, CModelControl, psfName);
71 LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelControl, region);
72 LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelControl, initial);
73 LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelControl, exp);
74 LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelControl, dev);
75 LSST_DECLARE_CONTROL_FIELD(cls, CModelControl, minInitialRadius);
76 LSST_DECLARE_CONTROL_FIELD(cls, CModelControl, fallbackInitialMomentsPsfFactor);
77 });
78}
79
80// Custom wrapper for views to std::bitset.
81template <int N>
82class BitSetView {
83public:
84 explicit BitSetView(std::bitset<N> const *target) : _target(target) {}
85
86 bool operator[](int i) const { return (*_target)[i]; }
87
88 constexpr std::size_t size() const { return N; }
89
90 template <typename PyParent>
91 static void declare(PyParent &parent) {
92 py::class_<BitSetView<N>> cls(parent, ("BitSetView" + std::to_string(N)).c_str());
93 cls.def("__getitem__", &BitSetView<N>::operator[]);
94 cls.def("__len__", &BitSetView<N>::size);
95 }
96
97private:
98 std::bitset<N> const *_target;
99};
100
101PyCModelStageResult declareCModelStageResult(lsst::cpputils::python::WrapperCollection &wrappers) {
102 return wrappers.wrapType(PyCModelStageResult(wrappers.module, "CModelStageResult"), [](auto &mod, auto &cls) {
103 cls.def(py::init<>());
104 cls.attr("FAILED") = py::cast(int(CModelStageResult::FAILED));
105 cls.attr("TR_SMALL") = py::cast(int(CModelStageResult::TR_SMALL));
106 cls.attr("MAX_ITERATIONS") = py::cast(int(CModelStageResult::MAX_ITERATIONS));
107 cls.attr("NUMERIC_ERROR") = py::cast(int(CModelStageResult::NUMERIC_ERROR));
108 cls.attr("BAD_REFERENCE") = py::cast(int(CModelStageResult::BAD_REFERENCE));
109 cls.attr("NO_FLUX") = py::cast(int(CModelStageResult::NO_FLUX));
110 cls.attr("N_FLAGS") = py::cast(int(CModelStageResult::N_FLAGS));
111
112 // Data members are intentionally read-only from the Python side;
113 // they should only be set by the C++ algorithm code that uses
114 // this class to communicate its outputs.
115 cls.def_readonly("model", &CModelStageResult::model);
116 cls.def_readonly("prior", &CModelStageResult::prior);
117 cls.def_readonly("objfunc", &CModelStageResult::objfunc);
118 cls.def_readonly("likelihood", &CModelStageResult::likelihood);
119 cls.def_readonly("instFlux", &CModelStageResult::instFlux);
120 cls.def_readonly("instFluxErr", &CModelStageResult::instFluxErr);
121 cls.def_readonly("instFluxInner", &CModelStageResult::instFluxInner);
122 cls.def_readonly("objective", &CModelStageResult::objective);
123 cls.def_readonly("time", &CModelStageResult::time);
124 cls.def_readonly("ellipse", &CModelStageResult::ellipse);
125 cls.def_readonly("nonlinear", &CModelStageResult::nonlinear);
126 cls.def_readonly("amplitudes", &CModelStageResult::amplitudes);
127 cls.def_readonly("fixed", &CModelStageResult::fixed);
128
129 // Declare wrappers for a view class for the flags attribute
130 BitSetView<CModelStageResult::N_FLAGS>::declare(cls);
131 // Wrap the flag. attributes
132 cls.def_property_readonly(
133 "flags",
134 [](CModelStageResult const &self) { return BitSetView<CModelStageResult::N_FLAGS>(&self.flags); },
135 py::return_value_policy::reference_internal);
136 });
137}
138
139PyCModelResult declareCModelResult(lsst::cpputils::python::WrapperCollection &wrappers) {
140 return wrappers.wrapType(PyCModelResult(wrappers.module, "CModelResult"), [](auto &mod, auto &cls) {
141 cls.def(py::init<>());
142
143 cls.attr("FAILED") = py::cast(int(CModelResult::FAILED));
144 cls.attr("REGION_MAX_AREA") = py::cast(int(CModelResult::REGION_MAX_AREA));
145 cls.attr("REGION_MAX_BAD_PIXEL_FRACTION") = py::cast(int(CModelResult::REGION_MAX_BAD_PIXEL_FRACTION));
146 cls.attr("REGION_USED_FOOTPRINT_AREA") = py::cast(int(CModelResult::REGION_USED_FOOTPRINT_AREA));
147 cls.attr("REGION_USED_PSF_AREA") = py::cast(int(CModelResult::REGION_USED_PSF_AREA));
148 cls.attr("REGION_USED_INITIAL_ELLIPSE_MIN") =
149 py::cast(int(CModelResult::REGION_USED_INITIAL_ELLIPSE_MIN));
150 cls.attr("REGION_USED_INITIAL_ELLIPSE_MAX") =
151 py::cast(int(CModelResult::REGION_USED_INITIAL_ELLIPSE_MAX));
152 cls.attr("NO_FLUX") = py::cast(int(CModelResult::NO_FLUX));
153
154 // Data members are intentionally read-only from the Python side;
155 // they should only be set by the C++ algorithm code that uses
156 // this class to communicate its outputs.
157 cls.def_readonly("instFlux", &CModelResult::instFlux);
158 cls.def_readonly("instFluxErr", &CModelResult::instFluxErr);
159 cls.def_readonly("instFluxInner", &CModelResult::instFluxInner);
160 cls.def_readonly("fracDev", &CModelResult::fracDev);
161 cls.def_readonly("objective", &CModelResult::objective);
162 cls.def_readonly("initial", &CModelResult::initial);
163 cls.def_readonly("exp", &CModelResult::exp);
164 cls.def_readonly("dev", &CModelResult::dev);
165 cls.def_readonly("initialFitRegion", &CModelResult::initialFitRegion);
166 cls.def_readonly("finalFitRegion", &CModelResult::finalFitRegion);
167 cls.def_readonly("fitSysToMeasSys", &CModelResult::fitSysToMeasSys);
168
169 // Declare wrappers for a view class for the flags attribute
170 BitSetView<CModelResult::N_FLAGS>::declare(cls);
171 // Wrap the flag. attributes
172 cls.def_property_readonly(
173 "flags", [](CModelResult const &self) { return BitSetView<CModelResult::N_FLAGS>(&self.flags); },
174 py::return_value_policy::reference_internal);
175 });
176}
177
178PyCModelAlgorithm declareCModelAlgorithm(lsst::cpputils::python::WrapperCollection &wrappers) {
179 return wrappers.wrapType(PyCModelAlgorithm(wrappers.module, "CModelAlgorithm"), [](auto &mod, auto &cls) {
180 cls.def(py::init<std::string const &, CModelControl const &, afw::table::Schema &>(), "name"_a, "ctrl"_a,
181 "schema"_a);
182 cls.def(py::init<std::string const &, CModelControl const &, afw::table::SchemaMapper &>(), "name"_a,
183 "ctrl"_a, "schemaMapper"_a);
184 cls.def(py::init<CModelControl const &>(), "ctrl"_a);
185 cls.def("getControl", &CModelAlgorithm::getControl);
186 cls.def("apply", &CModelAlgorithm::apply, "exposure"_a, "psf"_a, "center"_a, "moments"_a,
187 "approxFlux"_a = -1, "kronRadius"_a = -1, "footprintArea"_a = -1);
188 cls.def("applyForced", &CModelAlgorithm::applyForced, "exposure"_a, "psf"_a, "center"_a, "reference"_a,
189 "approxFlux"_a = -1);
190 cls.def("measure", (void (CModelAlgorithm::*)(afw::table::SourceRecord &,
191 afw::image::Exposure<Pixel> const &) const) &
192 CModelAlgorithm::measure,
193 "measRecord"_a, "exposure"_a);
194 cls.def("measure",
195 (void (CModelAlgorithm::*)(afw::table::SourceRecord &, afw::image::Exposure<Pixel> const &,
196 afw::table::SourceRecord const &) const) &
197 CModelAlgorithm::measure,
198 "measRecord"_a, "exposure"_a, "refRecord"_a);
199 cls.def("fail", &CModelAlgorithm::fail, "measRecord"_a, "error"_a);
200 cls.def("writeResultToRecord", &CModelAlgorithm::writeResultToRecord, "result"_a, "record"_a);
201 });
202}
203} // namespace
204
206 declareCModelStageControl(wrappers);
207 auto clsControl = declareCModelControl(wrappers);
208 declareCModelStageResult(wrappers);
209 auto clsResult = declareCModelResult(wrappers);
210 auto clsAlgorithm = declareCModelAlgorithm(wrappers);
211 clsAlgorithm.attr("Control") = clsControl;
212 clsAlgorithm.attr("Result") = clsResult;
213}
214
215} // namespace modelfit
216} // namespace meas
217} // namespace lsst
Key< Flag > const & target
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition python.h:242
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...
Definition python.h:391
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition python.h:448
void wrapCmodel(lsst::cpputils::python::WrapperCollection &wrappers)
Definition cmodel.cc:205
declare(module, exception_name, base, wrapped_class)
Definition wrappers.py:153
T to_string(T... args)