LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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
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)