LSST Applications g180d380827+78227d2bc4,g2079a07aa2+86d27d4dc4,g2305ad1205+bdd7851fe3,g2bbee38e9b+c6a8a0fb72,g337abbeb29+c6a8a0fb72,g33d1c0ed96+c6a8a0fb72,g3a166c0a6a+c6a8a0fb72,g3d1719c13e+260d7c3927,g3ddfee87b4+723a6db5f3,g487adcacf7+29e55ea757,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+9443c4b912,g62aa8f1a4b+7e2ea9cd42,g858d7b2824+260d7c3927,g864b0138d7+8498d97249,g95921f966b+dffe86973d,g991b906543+260d7c3927,g99cad8db69+4809d78dd9,g9c22b2923f+e2510deafe,g9ddcbc5298+9a081db1e4,ga1e77700b3+03d07e1c1f,gb0e22166c9+60f28cb32d,gb23b769143+260d7c3927,gba4ed39666+c2a2e4ac27,gbb8dafda3b+e22341fd87,gbd998247f1+585e252eca,gc120e1dc64+713f94b854,gc28159a63d+c6a8a0fb72,gc3e9b769f7+385ea95214,gcf0d15dbbd+723a6db5f3,gdaeeff99f8+f9a426f77a,ge6526c86ff+fde82a80b9,ge79ae78c31+c6a8a0fb72,gee10cc3b42+585e252eca,w.2024.18
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)