lsst.meas.modelfit  13.0-10-g4e34388+12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Pages
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 "numpy/arrayobject.h"
27 #include "ndarray/pybind11.h"
28 
29 #include "lsst/pex/config/python.h"
31 
32 namespace py = pybind11;
33 using namespace pybind11::literals;
34 
35 namespace lsst {
36 namespace meas {
37 namespace modelfit {
38 namespace {
39 
40 using PyCModelStageControl = py::class_<CModelStageControl, std::shared_ptr<CModelStageControl>>;
41 using PyCModelControl = py::class_<CModelControl, std::shared_ptr<CModelControl>>;
42 using PyCModelStageResult = py::class_<CModelStageResult, std::shared_ptr<CModelStageResult>>;
43 using PyCModelResult = py::class_<CModelResult, std::shared_ptr<CModelResult>>;
44 using PyCModelAlgorithm = py::class_<CModelAlgorithm, std::shared_ptr<CModelAlgorithm>>;
45 
46 static PyCModelStageControl declareCModelStageControl(py::module &mod) {
47  PyCModelStageControl cls(mod, "CModelStageControl");
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  return cls;
65 }
66 
67 static PyCModelControl declareCModelControl(py::module &mod) {
68  PyCModelControl cls(mod, "CModelControl");
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  return cls;
78 }
79 
80 // Custom wrapper for views to std::bitset.
81 template <int N>
82 class BitSetView {
83 public:
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 
97 private:
98  std::bitset<N> const *_target;
99 };
100 
101 static PyCModelStageResult declareCModelStageResult(py::module &mod) {
102  PyCModelStageResult cls(mod, "CModelStageResult");
103 
104  cls.def(py::init<>());
105 
106  cls.attr("FAILED") = py::cast(int(CModelStageResult::FAILED));
107  cls.attr("TR_SMALL") = py::cast(int(CModelStageResult::TR_SMALL));
108  cls.attr("MAX_ITERATIONS") = py::cast(int(CModelStageResult::MAX_ITERATIONS));
109  cls.attr("NUMERIC_ERROR") = py::cast(int(CModelStageResult::NUMERIC_ERROR));
110  cls.attr("BAD_REFERENCE") = py::cast(int(CModelStageResult::BAD_REFERENCE));
111  cls.attr("N_FLAGS") = py::cast(int(CModelStageResult::N_FLAGS));
112 
113  // Data members are intentionally read-only from the Python side;
114  // they should only be set by the C++ algorithm code that uses
115  // this class to communicate its outputs.
116  cls.def_readonly("model", &CModelStageResult::model);
117  cls.def_readonly("prior", &CModelStageResult::prior);
118  cls.def_readonly("objfunc", &CModelStageResult::objfunc);
119  cls.def_readonly("likelihood", &CModelStageResult::likelihood);
120  cls.def_readonly("flux", &CModelStageResult::flux);
121  cls.def_readonly("fluxSigma", &CModelStageResult::fluxSigma);
122  cls.def_readonly("fluxInner", &CModelStageResult::fluxInner);
123  cls.def_readonly("objective", &CModelStageResult::objective);
124  cls.def_readonly("time", &CModelStageResult::time);
125  cls.def_readonly("ellipse", &CModelStageResult::ellipse);
126  cls.def_readonly("nonlinear", &CModelStageResult::nonlinear);
127  cls.def_readonly("amplitudes", &CModelStageResult::amplitudes);
128  cls.def_readonly("fixed", &CModelStageResult::fixed);
129 
130  // Declare wrappers for a view class for the flags attribute
131  BitSetView<CModelStageResult::N_FLAGS>::declare(cls);
132  // Wrap the flag. attributes
133  cls.def_property_readonly(
134  "flags",
135  [](CModelStageResult const &self) { return BitSetView<CModelStageResult::N_FLAGS>(&self.flags); },
136  py::return_value_policy::reference_internal);
137 
138  return cls;
139 }
140 
141 static PyCModelResult declareCModelResult(py::module &mod) {
142  PyCModelResult cls(mod, "CModelResult");
143 
144  cls.def(py::init<>());
145 
146  cls.attr("FAILED") = py::cast(int(CModelResult::FAILED));
147  cls.attr("REGION_MAX_AREA") = py::cast(int(CModelResult::REGION_MAX_AREA));
148  cls.attr("REGION_MAX_BAD_PIXEL_FRACTION") = py::cast(int(CModelResult::REGION_MAX_BAD_PIXEL_FRACTION));
149  cls.attr("REGION_USED_FOOTPRINT_AREA") = py::cast(int(CModelResult::REGION_USED_FOOTPRINT_AREA));
150  cls.attr("REGION_USED_PSF_AREA") = py::cast(int(CModelResult::REGION_USED_PSF_AREA));
151  cls.attr("REGION_USED_INITIAL_ELLIPSE_MIN") =
152  py::cast(int(CModelResult::REGION_USED_INITIAL_ELLIPSE_MIN));
153  cls.attr("REGION_USED_INITIAL_ELLIPSE_MAX") =
154  py::cast(int(CModelResult::REGION_USED_INITIAL_ELLIPSE_MAX));
155 
156  // Data members are intentionally read-only from the Python side;
157  // they should only be set by the C++ algorithm code that uses
158  // this class to communicate its outputs.
159  cls.def_readonly("flux", &CModelResult::flux);
160  cls.def_readonly("fluxSigma", &CModelResult::fluxSigma);
161  cls.def_readonly("fluxInner", &CModelResult::fluxInner);
162  cls.def_readonly("fracDev", &CModelResult::fracDev);
163  cls.def_readonly("objective", &CModelResult::objective);
164  cls.def_readonly("initial", &CModelResult::initial);
165  cls.def_readonly("exp", &CModelResult::exp);
166  cls.def_readonly("dev", &CModelResult::dev);
167  cls.def_readonly("initialFitRegion", &CModelResult::initialFitRegion);
168  cls.def_readonly("finalFitRegion", &CModelResult::finalFitRegion);
169  cls.def_readonly("fitSysToMeasSys", &CModelResult::fitSysToMeasSys);
170 
171  // Declare wrappers for a view class for the flags attribute
172  BitSetView<CModelResult::N_FLAGS>::declare(cls);
173  // Wrap the flag. attributes
174  cls.def_property_readonly(
175  "flags", [](CModelResult const &self) { return BitSetView<CModelResult::N_FLAGS>(&self.flags); },
176  py::return_value_policy::reference_internal);
177 
178  return cls;
179 }
180 
181 static PyCModelAlgorithm declareCModelAlgorithm(py::module &mod) {
182  PyCModelAlgorithm cls(mod, "CModelAlgorithm");
183  cls.def(py::init<std::string const &, CModelControl const &, afw::table::Schema &>(), "name"_a, "ctrl"_a,
184  "schema"_a);
185  cls.def(py::init<std::string const &, CModelControl const &, afw::table::SchemaMapper &>(), "name"_a,
186  "ctrl"_a, "schemaMapper"_a);
187  cls.def(py::init<CModelControl const &>(), "ctrl"_a);
188  cls.def("getControl", &CModelAlgorithm::getControl);
189  cls.def("apply", &CModelAlgorithm::apply, "exposure"_a, "psf"_a, "center"_a, "moments"_a,
190  "approxFlux"_a = -1, "kronRadius"_a = -1, "footprintArea"_a = -1);
191  cls.def("applyForced", &CModelAlgorithm::applyForced, "exposure"_a, "psf"_a, "center"_a, "reference"_a,
192  "approxFlux"_a = -1);
193  cls.def("measure", (void (CModelAlgorithm::*)(afw::table::SourceRecord &,
194  afw::image::Exposure<Pixel> const &) const) &
195  CModelAlgorithm::measure,
196  "measRecord"_a, "exposure"_a);
197  cls.def("measure",
198  (void (CModelAlgorithm::*)(afw::table::SourceRecord &, afw::image::Exposure<Pixel> const &,
199  afw::table::SourceRecord const &) const) &
200  CModelAlgorithm::measure,
201  "measRecord"_a, "exposure"_a, "refRecord"_a);
202  cls.def("fail", &CModelAlgorithm::fail, "measRecord"_a, "error"_a);
203  cls.def("writeResultToRecord", &CModelAlgorithm::writeResultToRecord, "result"_a, "record"_a);
204  return cls;
205 }
206 
207 PYBIND11_PLUGIN(cmodel) {
208  py::module::import("lsst.afw.geom.ellipses");
209  py::module::import("lsst.afw.detection");
210  py::module::import("lsst.meas.modelfit.model");
211  py::module::import("lsst.meas.modelfit.priors");
212  py::module::import("lsst.meas.modelfit.optimizer");
213  py::module::import("lsst.meas.modelfit.pixelFitRegion");
214  py::module::import("lsst.meas.modelfit.unitTransformedLikelihood");
215 
216  py::module mod("cmodel");
217 
218  if (_import_array() < 0) {
219  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
220  return nullptr;
221  }
222 
223  declareCModelStageControl(mod);
224  auto clsControl = declareCModelControl(mod);
225  declareCModelStageResult(mod);
226  auto clsResult = declareCModelResult(mod);
227  auto clsAlgorithm = declareCModelAlgorithm(mod);
228  clsAlgorithm.attr("Control") = clsControl;
229  clsAlgorithm.attr("Result") = clsResult;
230 
231  return mod.ptr();
232 }
233 }
234 }
235 }
236 } // namespace lsst::meas::modelfit::anonymous
std::bitset< N > const * _target
Definition: cmodel.cc:98