lsst.meas.base  13.0-26-g0f127ff+5
sdssShape.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2017 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include "pybind11/pybind11.h"
24 
25 #include <memory>
26 
27 #include "lsst/pex/config/python.h"
28 #include "lsst/meas/base/python.h"
29 
30 #include "lsst/afw/table/FunctorKey.h"
33 
34 namespace py = pybind11;
35 using namespace pybind11::literals;
36 
37 namespace lsst {
38 namespace meas {
39 namespace base {
40 
41 namespace {
42 
43 using PyShapeControl = py::class_<SdssShapeControl>;
44 // TODO decide if we need to mention afw::table::FunctorKey<SdssShapeResult>
45 // and if so, wrap it; if not, document that here
46 using PyShapeResultKey = py::class_<SdssShapeResultKey, std::shared_ptr<SdssShapeResultKey>>;
47 using PyShapeResult = py::class_<SdssShapeResult, std::shared_ptr<SdssShapeResult>, ShapeResult,
48  CentroidResult, FluxResult>;
49 using PyShapeAlgorithm = py::class_<SdssShapeAlgorithm, std::shared_ptr<SdssShapeAlgorithm>, SimpleAlgorithm>;
50 using PyShapeTransform = py::class_<SdssShapeTransform, std::shared_ptr<SdssShapeTransform>, BaseTransform>;
51 
52 PyShapeControl declareShapeControl(py::module &mod) {
53  PyShapeControl cls(mod, "SdssShapeControl");
54 
55  LSST_DECLARE_CONTROL_FIELD(cls, SdssShapeControl, background);
56  LSST_DECLARE_CONTROL_FIELD(cls, SdssShapeControl, maxIter);
57  LSST_DECLARE_CONTROL_FIELD(cls, SdssShapeControl, maxShift);
58  LSST_DECLARE_CONTROL_FIELD(cls, SdssShapeControl, tol1);
59  LSST_DECLARE_CONTROL_FIELD(cls, SdssShapeControl, tol2);
60  LSST_DECLARE_CONTROL_FIELD(cls, SdssShapeControl, doMeasurePsf);
61 
62  cls.def(py::init<>());
63 
64  return cls;
65 }
66 
67 void declareShapeResultKey(py::module &mod) {
68  PyShapeResultKey cls(mod, "SdssShapeResultKey");
69 
70  // TODO decide whether to wrap default constructor and do it or document why not
71  cls.def(py::init<afw::table::SubSchema const &>(), "subSchema"_a);
72 
73  cls.def_static("addFields", &FluxResultKey::addFields, "schema"_a, "name"_a, "doMeasurePsf"_a);
74 
75  cls.def("__eq__", &SdssShapeResultKey::operator==, py::is_operator());
76  cls.def("__ne__", &SdssShapeResultKey::operator!=, py::is_operator());
77 
78  cls.def("get", &SdssShapeResultKey::get, "record"_a);
79  cls.def("set", &SdssShapeResultKey::set, "record"_a, "value"_a);
80  cls.def("getPsfShape", &SdssShapeResultKey::getPsfShape, "record"_a);
81  cls.def("setPsfShape", &SdssShapeResultKey::setPsfShape, "record"_a, "value"_a);
82  cls.def("isValid", &SdssShapeResultKey::isValid);
83  cls.def("getFlagHandler", &SdssShapeResultKey::getFlagHandler);
84 }
85 
86 template <typename ImageT>
87 static void declareComputeMethods(PyShapeAlgorithm & cls) {
88  cls.def_static(
89  "computeAdaptiveMoments",
90  (SdssShapeResult (*)(
91  ImageT const &,
92  afw::geom::Point2D const &,
93  bool,
94  SdssShapeControl const &
95  )) &SdssShapeAlgorithm::computeAdaptiveMoments,
96  "image"_a, "position"_a, "negative"_a=false, "ctrl"_a=SdssShapeControl()
97  );
98  cls.def_static(
99  "computeFixedMomentsFlux",
100  (FluxResult (*)(
101  ImageT const &,
102  afw::geom::ellipses::Quadrupole const &,
103  afw::geom::Point2D const &
104  )) &SdssShapeAlgorithm::computeFixedMomentsFlux,
105  "image"_a, "shape"_a, "position"_a
106  );
107 }
108 
109 PyShapeAlgorithm declareShapeAlgorithm(py::module &mod) {
110  PyShapeAlgorithm cls(mod, "SdssShapeAlgorithm");
111 
112  cls.attr("FAILURE") = py::cast(SdssShapeAlgorithm::FAILURE);
113  cls.attr("UNWEIGHTED_BAD") = py::cast(SdssShapeAlgorithm::UNWEIGHTED_BAD);
114  cls.attr("UNWEIGHTED") = py::cast(SdssShapeAlgorithm::UNWEIGHTED);
115  cls.attr("SHIFT") = py::cast(SdssShapeAlgorithm::SHIFT);
116  cls.attr("MAXITER") = py::cast(SdssShapeAlgorithm::MAXITER);
117  cls.attr("PSF_SHAPE_BAD") = py::cast(SdssShapeAlgorithm::PSF_SHAPE_BAD);
118 
119  cls.def(py::init<SdssShapeAlgorithm::Control const &, std::string const &, afw::table::Schema &>(),
120  "ctrl"_a, "name"_a, "schema"_a);
121 
122  declareComputeMethods<afw::image::Image<int>>(cls);
123  declareComputeMethods<afw::image::Image<float>>(cls);
124  declareComputeMethods<afw::image::Image<double>>(cls);
125  declareComputeMethods<afw::image::MaskedImage<int>>(cls);
126  declareComputeMethods<afw::image::MaskedImage<float>>(cls);
127  declareComputeMethods<afw::image::MaskedImage<double>>(cls);
128 
129  cls.def("measure", &SdssShapeAlgorithm::measure, "measRecord"_a, "exposure"_a);
130  cls.def("fail", &SdssShapeAlgorithm::fail, "measRecord"_a, "error"_a = nullptr);
131 
132  return cls;
133 }
134 
135 void declareShapeResult(py::module &mod) {
136  PyShapeResult cls(mod, "SdssShapeResult");
137 
138  cls.def(py::init<>());
139 
140  cls.def_readwrite("flux_xx_Cov", &SdssShapeResult::flux_xx_Cov);
141  cls.def_readwrite("flux_yy_Cov", &SdssShapeResult::flux_yy_Cov);
142  cls.def_readwrite("flux_xy_Cov", &SdssShapeResult::flux_xy_Cov);
143  cls.def_readwrite("flags", &SdssShapeResult::flags);
144 
145  // TODO this method says it's a workaround for Swig which doesn't understand std::bitset
146  cls.def("getFlag", (bool (SdssShapeResult::*)(unsigned int) const) & SdssShapeResult::getFlag,
147  "index"_a);
148  cls.def("getFlag",
149  (bool (SdssShapeResult::*)(std::string const &name) const) & SdssShapeResult::getFlag,
150  "name"_a);
151 }
152 
153 PyShapeTransform declareShapeTransform(py::module &mod) {
154  PyShapeTransform cls(mod, "SdssShapeTransform");
155 
156  cls.def(py::init<SdssShapeTransform::Control const &, std::string const &, afw::table::SchemaMapper &>(),
157  "ctrl"_a, "name"_a, "mapper"_a);
158 
159  cls.def("__call__", &SdssShapeTransform::operator(), "inputCatalog"_a, "outputCatalog"_a, "wcs"_a,
160  "calib"_a);
161 
162  return cls;
163 }
164 
165 } // <anonymous>
166 
168  py::module::import("lsst.afw.geom");
169  py::module::import("lsst.afw.table");
170  py::module::import("lsst.meas.base.algorithm");
171  py::module::import("lsst.meas.base.flagHandler");
172  py::module::import("lsst.meas.base.centroidUtilities"); // for CentroidResult
173  py::module::import("lsst.meas.base.fluxUtilities"); // for FluxResult
174  py::module::import("lsst.meas.base.shapeUtilities");
175  py::module::import("lsst.meas.base.transform");
176 
177  py::module mod("sdssShape");
178 
179  auto clsShapeControl = declareShapeControl(mod);
180  declareShapeResultKey(mod);
181  auto clsShapeAlgorithm = declareShapeAlgorithm(mod);
182  declareShapeResult(mod);
183  auto clsShapeTransform = declareShapeTransform(mod);
184 
185  clsShapeAlgorithm.attr("Control") = clsShapeControl;
186  clsShapeTransform.attr("Control") = clsShapeControl;
187 
188  python::declareAlgorithm<SdssShapeAlgorithm, SdssShapeControl, SdssShapeTransform>(
189  clsShapeAlgorithm, clsShapeControl, clsShapeTransform);
190 
191  return mod.ptr();
192 }
193 
194 } // base
195 } // meas
196 } // lsst
Definition: mainpage.dox:3
PYBIND11_PLUGIN(sdssShape)
Definition: sdssShape.cc:167