LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
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 
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(*)(ImageT const &, geom::Point2D const &, bool, SdssShapeControl const &)) &
91  SdssShapeAlgorithm::computeAdaptiveMoments,
92  "image"_a, "position"_a, "negative"_a = false, "ctrl"_a = SdssShapeControl());
93  cls.def_static(
94  "computeFixedMomentsFlux",
95  (FluxResult(*)(ImageT const &, afw::geom::ellipses::Quadrupole const &, geom::Point2D const &)) &
96  SdssShapeAlgorithm::computeFixedMomentsFlux,
97  "image"_a, "shape"_a, "position"_a);
98 }
99 
100 PyShapeAlgorithm declareShapeAlgorithm(py::module &mod) {
101  PyShapeAlgorithm cls(mod, "SdssShapeAlgorithm");
102 
103  cls.attr("FAILURE") = py::cast(SdssShapeAlgorithm::FAILURE);
104  cls.attr("UNWEIGHTED_BAD") = py::cast(SdssShapeAlgorithm::UNWEIGHTED_BAD);
105  cls.attr("UNWEIGHTED") = py::cast(SdssShapeAlgorithm::UNWEIGHTED);
106  cls.attr("SHIFT") = py::cast(SdssShapeAlgorithm::SHIFT);
107  cls.attr("MAXITER") = py::cast(SdssShapeAlgorithm::MAXITER);
108  cls.attr("PSF_SHAPE_BAD") = py::cast(SdssShapeAlgorithm::PSF_SHAPE_BAD);
109 
110  cls.def(py::init<SdssShapeAlgorithm::Control const &, std::string const &, afw::table::Schema &>(),
111  "ctrl"_a, "name"_a, "schema"_a);
112 
113  declareComputeMethods<afw::image::Image<int>>(cls);
114  declareComputeMethods<afw::image::Image<float>>(cls);
115  declareComputeMethods<afw::image::Image<double>>(cls);
116  declareComputeMethods<afw::image::MaskedImage<int>>(cls);
117  declareComputeMethods<afw::image::MaskedImage<float>>(cls);
118  declareComputeMethods<afw::image::MaskedImage<double>>(cls);
119 
120  cls.def("measure", &SdssShapeAlgorithm::measure, "measRecord"_a, "exposure"_a);
121  cls.def("fail", &SdssShapeAlgorithm::fail, "measRecord"_a, "error"_a = nullptr);
122 
123  return cls;
124 }
125 
126 void declareShapeResult(py::module &mod) {
127  PyShapeResult cls(mod, "SdssShapeResult");
128 
129  cls.def(py::init<>());
130 
131  cls.def_readwrite("instFlux_xx_Cov", &SdssShapeResult::instFlux_xx_Cov);
132  cls.def_readwrite("instFlux_yy_Cov", &SdssShapeResult::instFlux_yy_Cov);
133  cls.def_readwrite("instFlux_xy_Cov", &SdssShapeResult::instFlux_xy_Cov);
134  cls.def_readwrite("flags", &SdssShapeResult::flags);
135 
136  // TODO this method says it's a workaround for Swig which doesn't understand std::bitset
137  cls.def("getFlag", (bool (SdssShapeResult::*)(unsigned int) const) & SdssShapeResult::getFlag, "index"_a);
138  cls.def("getFlag", (bool (SdssShapeResult::*)(std::string const &name) const) & SdssShapeResult::getFlag,
139  "name"_a);
140 }
141 
142 PyShapeTransform declareShapeTransform(py::module &mod) {
143  PyShapeTransform cls(mod, "SdssShapeTransform");
144 
145  cls.def(py::init<SdssShapeTransform::Control const &, std::string const &, afw::table::SchemaMapper &>(),
146  "ctrl"_a, "name"_a, "mapper"_a);
147 
148  cls.def("__call__", &SdssShapeTransform::operator(), "inputCatalog"_a, "outputCatalog"_a, "wcs"_a,
149  "photoCalib"_a);
150 
151  return cls;
152 }
153 
154 } // namespace
155 
156 PYBIND11_MODULE(sdssShape, mod) {
157  py::module::import("lsst.afw.geom");
158  py::module::import("lsst.afw.table");
159  py::module::import("lsst.meas.base.algorithm");
160  py::module::import("lsst.meas.base.flagHandler");
161  py::module::import("lsst.meas.base.centroidUtilities"); // for CentroidResult
162  py::module::import("lsst.meas.base.fluxUtilities"); // for FluxResult
163  py::module::import("lsst.meas.base.shapeUtilities");
164  py::module::import("lsst.meas.base.transform");
165 
166  auto clsShapeControl = declareShapeControl(mod);
167  declareShapeResultKey(mod);
168  auto clsShapeAlgorithm = declareShapeAlgorithm(mod);
169  declareShapeResult(mod);
170  auto clsShapeTransform = declareShapeTransform(mod);
171 
172  clsShapeAlgorithm.attr("Control") = clsShapeControl;
173  clsShapeTransform.attr("Control") = clsShapeControl;
174 
175  python::declareAlgorithm<SdssShapeAlgorithm, SdssShapeControl, SdssShapeTransform>(
176  clsShapeAlgorithm, clsShapeControl, clsShapeTransform);
177 }
178 
179 } // namespace base
180 } // namespace meas
181 } // namespace lsst
table::Key< std::string > name
Definition: Amplifier.cc:116
daf::base::PropertySet * set
Definition: fits.cc:912
bool isValid
Definition: fits.cc:399
def measure(mi, x, y, size, statistic, stats)
Definition: fringe.py:517
PYBIND11_MODULE(sdssShape, mod)
Definition: sdssShape.cc:156
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