LSSTApplications  20.0.0
LSSTDataManagementBasePackage
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
SdssShape.h
std::string
STL class.
lsst::meas::base::PYBIND11_MODULE
PYBIND11_MODULE(sdssShape, mod)
Definition: sdssShape.cc:156
base
Definition: __init__.py:1
lsst::meas::base.plugins.BaseTransform
BaseTransform
Definition: plugins.py:98
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::afw::geom.transform.transformContinued.cls
cls
Definition: transformContinued.py:33
lsst.pipe.drivers.visualizeVisit.background
background
Definition: visualizeVisit.py:37
FunctorKey.h
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
python.h
ShapeUtilities.h
lsst::geom::Point< double, 2 >
isValid
bool isValid
Definition: fits.cc:399
LSST_DECLARE_CONTROL_FIELD
#define LSST_DECLARE_CONTROL_FIELD(WRAPPER, CLASS, NAME)
Macro used to wrap fields declared by LSST_CONTROL_FIELD using Pybind11.
Definition: python.h:50
pybind11
Definition: _GenericMap.cc:40
python.h
lsst::ip::isr.fringe.measure
def measure(mi, x, y, size, statistic, stats)
Definition: fringe.py:506
set
daf::base::PropertySet * set
Definition: fits.cc:912
lsst::meas::modelfit.psf.psfContinued.module
module
Definition: psfContinued.py:42