LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
LSSTDataManagementBasePackage
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"
25 
26 #include "ndarray/pybind11.h"
27 
28 #include "lsst/pex/config/python.h"
30 
31 namespace py = pybind11;
32 using namespace pybind11::literals;
33 
34 namespace lsst {
35 namespace meas {
36 namespace modelfit {
37 namespace {
38 
39 using PyCModelStageControl = py::class_<CModelStageControl, std::shared_ptr<CModelStageControl>>;
40 using PyCModelControl = py::class_<CModelControl, std::shared_ptr<CModelControl>>;
41 using PyCModelStageResult = py::class_<CModelStageResult, std::shared_ptr<CModelStageResult>>;
42 using PyCModelResult = py::class_<CModelResult, std::shared_ptr<CModelResult>>;
43 using PyCModelAlgorithm = py::class_<CModelAlgorithm, std::shared_ptr<CModelAlgorithm>>;
44 
45 static PyCModelStageControl declareCModelStageControl(py::module &mod) {
46  PyCModelStageControl cls(mod, "CModelStageControl");
47  cls.def(py::init<>());
48  cls.def("getProfile", &CModelStageControl::getProfile);
49  cls.def("getModel", &CModelStageControl::getModel);
50  cls.def("getPrior", &CModelStageControl::getPrior);
51  LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, profileName);
52  LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, priorSource);
53  LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, priorName);
54  LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelStageControl, linearPriorConfig);
55  LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelStageControl, empiricalPriorConfig);
56  LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, nComponents);
57  LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, maxRadius);
58  LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, usePixelWeights);
59  LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, weightsMultiplier);
60  LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelStageControl, optimizer);
61  LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, doRecordHistory);
62  LSST_DECLARE_CONTROL_FIELD(cls, CModelStageControl, doRecordTime);
63  return cls;
64 }
65 
66 static PyCModelControl declareCModelControl(py::module &mod) {
67  PyCModelControl cls(mod, "CModelControl");
68  cls.def(py::init<>());
69  LSST_DECLARE_CONTROL_FIELD(cls, CModelControl, psfName);
71  LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelControl, initial);
72  LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelControl, exp);
73  LSST_DECLARE_NESTED_CONTROL_FIELD(cls, CModelControl, dev);
74  LSST_DECLARE_CONTROL_FIELD(cls, CModelControl, minInitialRadius);
75  LSST_DECLARE_CONTROL_FIELD(cls, CModelControl, fallbackInitialMomentsPsfFactor);
76  return cls;
77 }
78 
79 // Custom wrapper for views to std::bitset.
80 template <int N>
81 class BitSetView {
82 public:
83  explicit BitSetView(std::bitset<N> const *target) : _target(target) {}
84 
85  bool operator[](int i) const { return (*_target)[i]; }
86 
87  constexpr std::size_t size() const { return N; }
88 
89  template <typename PyParent>
90  static void declare(PyParent &parent) {
91  py::class_<BitSetView<N>> cls(parent, ("BitSetView" + std::to_string(N)).c_str());
92  cls.def("__getitem__", &BitSetView<N>::operator[]);
93  cls.def("__len__", &BitSetView<N>::size);
94  }
95 
96 private:
97  std::bitset<N> const *_target;
98 };
99 
100 static PyCModelStageResult declareCModelStageResult(py::module &mod) {
101  PyCModelStageResult cls(mod, "CModelStageResult");
102 
103  cls.def(py::init<>());
104 
105  cls.attr("FAILED") = py::cast(int(CModelStageResult::FAILED));
106  cls.attr("TR_SMALL") = py::cast(int(CModelStageResult::TR_SMALL));
107  cls.attr("MAX_ITERATIONS") = py::cast(int(CModelStageResult::MAX_ITERATIONS));
108  cls.attr("NUMERIC_ERROR") = py::cast(int(CModelStageResult::NUMERIC_ERROR));
109  cls.attr("BAD_REFERENCE") = py::cast(int(CModelStageResult::BAD_REFERENCE));
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
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  return cls;
138 }
139 
140 static PyCModelResult declareCModelResult(py::module &mod) {
141  PyCModelResult cls(mod, "CModelResult");
142 
143  cls.def(py::init<>());
144 
145  cls.attr("FAILED") = py::cast(int(CModelResult::FAILED));
146  cls.attr("REGION_MAX_AREA") = py::cast(int(CModelResult::REGION_MAX_AREA));
147  cls.attr("REGION_MAX_BAD_PIXEL_FRACTION") = py::cast(int(CModelResult::REGION_MAX_BAD_PIXEL_FRACTION));
148  cls.attr("REGION_USED_FOOTPRINT_AREA") = py::cast(int(CModelResult::REGION_USED_FOOTPRINT_AREA));
149  cls.attr("REGION_USED_PSF_AREA") = py::cast(int(CModelResult::REGION_USED_PSF_AREA));
150  cls.attr("REGION_USED_INITIAL_ELLIPSE_MIN") =
151  py::cast(int(CModelResult::REGION_USED_INITIAL_ELLIPSE_MIN));
152  cls.attr("REGION_USED_INITIAL_ELLIPSE_MAX") =
153  py::cast(int(CModelResult::REGION_USED_INITIAL_ELLIPSE_MAX));
154 
155  // Data members are intentionally read-only from the Python side;
156  // they should only be set by the C++ algorithm code that uses
157  // this class to communicate its outputs.
158  cls.def_readonly("instFlux", &CModelResult::instFlux);
159  cls.def_readonly("instFluxErr", &CModelResult::instFluxErr);
160  cls.def_readonly("instFluxInner", &CModelResult::instFluxInner);
161  cls.def_readonly("fracDev", &CModelResult::fracDev);
162  cls.def_readonly("objective", &CModelResult::objective);
163  cls.def_readonly("initial", &CModelResult::initial);
164  cls.def_readonly("exp", &CModelResult::exp);
165  cls.def_readonly("dev", &CModelResult::dev);
166  cls.def_readonly("initialFitRegion", &CModelResult::initialFitRegion);
167  cls.def_readonly("finalFitRegion", &CModelResult::finalFitRegion);
168  cls.def_readonly("fitSysToMeasSys", &CModelResult::fitSysToMeasSys);
169 
170  // Declare wrappers for a view class for the flags attribute
172  // Wrap the flag. attributes
173  cls.def_property_readonly(
174  "flags", [](CModelResult const &self) { return BitSetView<CModelResult::N_FLAGS>(&self.flags); },
175  py::return_value_policy::reference_internal);
176 
177  return cls;
178 }
179 
180 static PyCModelAlgorithm declareCModelAlgorithm(py::module &mod) {
181  PyCModelAlgorithm cls(mod, "CModelAlgorithm");
182  cls.def(py::init<std::string const &, CModelControl const &, afw::table::Schema &>(), "name"_a, "ctrl"_a,
183  "schema"_a);
184  cls.def(py::init<std::string const &, CModelControl const &, afw::table::SchemaMapper &>(), "name"_a,
185  "ctrl"_a, "schemaMapper"_a);
186  cls.def(py::init<CModelControl const &>(), "ctrl"_a);
187  cls.def("getControl", &CModelAlgorithm::getControl);
188  cls.def("apply", &CModelAlgorithm::apply, "exposure"_a, "psf"_a, "center"_a, "moments"_a,
189  "approxFlux"_a = -1, "kronRadius"_a = -1, "footprintArea"_a = -1);
190  cls.def("applyForced", &CModelAlgorithm::applyForced, "exposure"_a, "psf"_a, "center"_a, "reference"_a,
191  "approxFlux"_a = -1);
192  cls.def("measure", (void (CModelAlgorithm::*)(afw::table::SourceRecord &,
193  afw::image::Exposure<Pixel> const &) const) &
195  "measRecord"_a, "exposure"_a);
196  cls.def("measure",
197  (void (CModelAlgorithm::*)(afw::table::SourceRecord &, afw::image::Exposure<Pixel> const &,
198  afw::table::SourceRecord const &) const) &
199  CModelAlgorithm::measure,
200  "measRecord"_a, "exposure"_a, "refRecord"_a);
201  cls.def("fail", &CModelAlgorithm::fail, "measRecord"_a, "error"_a);
202  cls.def("writeResultToRecord", &CModelAlgorithm::writeResultToRecord, "result"_a, "record"_a);
203  return cls;
204 }
205 
206 PYBIND11_MODULE(cmodel, mod) {
207  py::module::import("lsst.afw.geom.ellipses");
208  py::module::import("lsst.afw.detection");
209  py::module::import("lsst.meas.modelfit.model");
210  py::module::import("lsst.meas.modelfit.priors");
211  py::module::import("lsst.meas.modelfit.optimizer");
212  py::module::import("lsst.meas.modelfit.pixelFitRegion");
213  py::module::import("lsst.meas.modelfit.unitTransformedLikelihood");
214 
215  declareCModelStageControl(mod);
216  auto clsControl = declareCModelControl(mod);
217  declareCModelStageResult(mod);
218  auto clsResult = declareCModelResult(mod);
219  auto clsAlgorithm = declareCModelAlgorithm(mod);
220  clsAlgorithm.attr("Control") = clsControl;
221  clsAlgorithm.attr("Result") = clsResult;
222 }
223 
224 }
225 }
226 }
227 } // namespace lsst::meas::modelfit::anonymous
Key< Flag > const & target
PYBIND11_MODULE(camera, mod)
Definition: camera.cc:133
T to_string(T... args)
table::Key< table::Array< double > > amplitudes
A base class for image defects.
#define LSST_DECLARE_CONTROL_FIELD(WRAPPER, CLASS, NAME)
Macro used to wrap fields declared by LSST_CONTROL_FIELD using Pybind11.
Definition: python.h:50
#define LSST_DECLARE_NESTED_CONTROL_FIELD(WRAPPER, CLASS, NAME)
Macro used to wrap fields declared by LSST_NESTED_CONTROL_FIELD using Pybind11.
Definition: python.h:72
def declare(module, exception_name, base, wrapped_class)
Definition: wrappers.py:153
def measure(mi, x, y, size, statistic, stats)
Definition: fringe.py:473
STL class.