LSST Applications g042eb84c57+730a74494b,g04e9c324dd+8c5ae1fdc5,g134cb467dc+1f1e3e7524,g199a45376c+0ba108daf9,g1fd858c14a+fa7d31856b,g210f2d0738+f66ac109ec,g262e1987ae+83a3acc0e5,g29ae962dfc+d856a2cb1f,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+a1e0c9f713,g47891489e3+0d594cb711,g4d44eb3520+c57ec8f3ed,g4d7b6aa1c5+f66ac109ec,g53246c7159+8c5ae1fdc5,g56a1a4eaf3+fd7ad03fde,g64539dfbff+f66ac109ec,g67b6fd64d1+0d594cb711,g67fd3c3899+f66ac109ec,g6985122a63+0d594cb711,g74acd417e5+3098891321,g786e29fd12+668abc6043,g81db2e9a8d+98e2ab9f28,g87389fa792+8856018cbb,g89139ef638+0d594cb711,g8d7436a09f+80fda9ce03,g8ea07a8fe4+760ca7c3fc,g90f42f885a+033b1d468d,g97be763408+a8a29bda4b,g99822b682c+e3ec3c61f9,g9d5c6a246b+0d5dac0c3d,ga41d0fce20+9243b26dd2,gbf99507273+8c5ae1fdc5,gd7ef33dd92+0d594cb711,gdab6d2f7ff+3098891321,ge410e46f29+0d594cb711,geaed405ab2+c4bbc419c6,gf9a733ac38+8c5ae1fdc5,w.2025.38
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::classh<CModelStageControl>;
41using PyCModelControl = py::classh<CModelControl>;
42using PyCModelStageResult = py::classh<CModelStageResult>;
43using PyCModelResult = py::classh<CModelResult>;
44using PyCModelAlgorithm = py::classh<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
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
decltype(sizeof(void *)) size_t
Definition doctest.h:524
T to_string(T... args)