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
_footprint.cc
Go to the documentation of this file.
1 /*
2  * This file is part of afw.
3  *
4  * Developed for the LSST Data Management System.
5  * This product includes software developed by the LSST Project
6  * (https://www.lsst.org).
7  * See the COPYRIGHT file at the top-level directory of this distribution
8  * for details of code ownership.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 #include "pybind11/pybind11.h"
25 #include "pybind11/stl.h"
26 
27 #include <cstdint>
28 
29 #include "ndarray/pybind11.h"
30 
31 #include "lsst/utils/python.h"
32 
33 #include "lsst/pex/exceptions.h"
34 #include "lsst/afw/table/io/python.h" // for addPersistableMethods
36 
37 namespace py = pybind11;
38 using namespace pybind11::literals;
39 
40 namespace lsst {
41 namespace afw {
42 namespace detection {
43 
44 using utils::python::WrapperCollection;
45 
46 namespace {
47 
48 template <typename MaskT>
49 void declareMaskFromFootprintList(WrapperCollection &wrappers) {
50  auto maskSetter = [](lsst::afw::image::Mask<MaskT> *mask,
52  MaskT const bitmask, bool doClip) {
53  for (auto const &foot : footprints) {
54  try {
55  if (doClip) {
56  auto tmpSpan = foot->getSpans()->clippedTo(mask->getBBox());
57  tmpSpan->setMask(*mask, bitmask);
58  } else {
59  foot->getSpans()->setMask(*mask, bitmask);
60  }
61  } catch (lsst::pex::exceptions::OutOfRangeError const &e) {
63  "Bounds of a Footprint fall outside mask set doClip to force");
64  }
65  }
66  };
67 
68  wrappers.wrap([&maskSetter](auto &mod) {
69  mod.def("setMaskFromFootprintList", std::move(maskSetter), "mask"_a, "footprints"_a, "bitmask"_a,
70  "doClip"_a = true);
71  });
72 }
73 
74 } // end anonymous namespace
75 
76 void wrapFootprint(WrapperCollection &wrappers) {
77  wrappers.addInheritanceDependency("lsst.afw.table.io");
78  wrappers.addSignatureDependency("lsst.afw.geom");
79  wrappers.addSignatureDependency("lsst.afw.table");
80 
81  wrappers.wrapType(
82  py::class_<Footprint, std::shared_ptr<Footprint>>(wrappers.module, "Footprint"),
83  [](auto &mod, auto &cls) {
84  cls.def(py::init<std::shared_ptr<geom::SpanSet>, lsst::geom::Box2I const &>(), "inputSpans"_a,
85  "region"_a = lsst::geom::Box2I());
86 
87  cls.def(py::init<std::shared_ptr<geom::SpanSet>, afw::table::Schema const &,
88  lsst::geom::Box2I const &>(),
89  "inputSpans"_a, "peakSchema"_a, "region"_a = lsst::geom::Box2I());
90  cls.def(py::init<Footprint const &>());
91  cls.def(py::init<>());
92 
93  table::io::python::addPersistableMethods<Footprint>(cls);
94 
95  cls.def("getSpans", &Footprint::getSpans);
96  cls.def("setSpans", &Footprint::setSpans);
97  cls.def("getPeaks", (PeakCatalog & (Footprint::*)()) & Footprint::getPeaks,
98  py::return_value_policy::reference_internal);
99  cls.def("addPeak", &Footprint::addPeak);
100  cls.def("sortPeaks", &Footprint::sortPeaks, "key"_a = afw::table::Key<float>());
101  cls.def("setPeakSchema", &Footprint::setPeakSchema);
102  cls.def("setPeakCatalog", &Footprint::setPeakCatalog, "otherPeaks"_a);
103  cls.def("getArea", &Footprint::getArea);
104  cls.def("getCentroid", &Footprint::getCentroid);
105  cls.def("getShape", &Footprint::getShape);
106  cls.def("shift", (void (Footprint::*)(int, int)) & Footprint::shift);
107  cls.def("shift", (void (Footprint::*)(lsst::geom::ExtentI const &)) & Footprint::shift);
108  cls.def("getBBox", &Footprint::getBBox);
109  cls.def("getRegion", &Footprint::getRegion);
110  cls.def("setRegion", &Footprint::setRegion);
111  cls.def("clipTo", &Footprint::clipTo);
112  cls.def("contains", &Footprint::contains);
113  cls.def("transform",
114  (std::shared_ptr<Footprint>(Footprint::*)(std::shared_ptr<geom::SkyWcs>,
115  std::shared_ptr<geom::SkyWcs>,
116  lsst::geom::Box2I const &, bool) const) &
117  Footprint::transform,
118  "source"_a, "target"_a, "region"_a, "doClip"_a = true);
119  cls.def("transform",
120  (std::shared_ptr<Footprint>(Footprint::*)(lsst::geom::LinearTransform const &,
121  lsst::geom::Box2I const &, bool) const) &
122  Footprint::transform);
123  cls.def("transform",
124  (std::shared_ptr<Footprint>(Footprint::*)(lsst::geom::AffineTransform const &,
125  lsst::geom::Box2I const &, bool) const) &
126  Footprint::transform);
127  cls.def("transform",
128  (std::shared_ptr<Footprint>(Footprint::*)(geom::TransformPoint2ToPoint2 const &,
129  lsst::geom::Box2I const &, bool) const) &
130  Footprint::transform);
131  cls.def("dilate", (void (Footprint::*)(int, geom::Stencil)) & Footprint::dilate, "r"_a,
132  "stencil"_a = geom::Stencil::CIRCLE);
133  cls.def("dilate", (void (Footprint::*)(geom::SpanSet const &)) & Footprint::dilate);
134  cls.def("erode", (void (Footprint::*)(int, geom::Stencil)) & Footprint::erode, "r"_a,
135  "stencil"_a = geom::Stencil::CIRCLE);
136  cls.def("erode", (void (Footprint::*)(geom::SpanSet const &)) & Footprint::erode);
137  cls.def("removeOrphanPeaks", &Footprint::removeOrphanPeaks);
138  cls.def("isContiguous", &Footprint::isContiguous);
139  cls.def("isHeavy", &Footprint::isHeavy);
140  cls.def("assign", (Footprint & (Footprint::*)(Footprint const &)) & Footprint::operator=);
141 
142  cls.def("split", [](Footprint const &self) -> py::list {
143  /* This is a work around for pybind not properly
144  * handling converting a vector of unique pointers
145  * to python lists of shared pointers */
146  py::list l;
147  for (auto &ptr : self.split()) {
148  l.append(py::cast(std::shared_ptr<Footprint>(std::move(ptr))));
149  }
150  return l;
151  });
152 
153  cls.def_property("spans", &Footprint::getSpans, &Footprint::setSpans);
154  cls.def_property_readonly("peaks", (PeakCatalog & (Footprint::*)()) & Footprint::getPeaks,
155  py::return_value_policy::reference_internal);
156 
157  cls.def("__contains__", [](Footprint const &self, lsst::geom::Point2I const &point) -> bool {
158  return self.contains(point);
159  });
160  cls.def("__eq__",
161  [](Footprint const &self, Footprint const &other) -> bool { return self == other; },
162  py::is_operator());
163  });
164 
165  declareMaskFromFootprintList<lsst::afw::image::MaskPixel>(wrappers);
166 
167  wrappers.wrap([](auto &mod) {
168  mod.def("mergeFootprints", &mergeFootprints);
169  mod.def("footprintToBBoxList", &footprintToBBoxList);
170  });
171 }
172 
173 } // namespace detection
174 } // namespace afw
175 } // namespace lsst
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
afw::table::Key< afw::table::Array< MaskPixelT > > mask
Class to describe the properties of a detected object from an image.
Definition: Footprint.h:63
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
Reports attempts to access elements outside a valid range of indices.
Definition: Runtime.h:89
T move(T... args)
std::vector< lsst::geom::Box2I > footprintToBBoxList(Footprint const &footprint)
Return a list of BBoxs, whose union contains exactly the pixels in the footprint, neither more nor le...
Definition: Footprint.cc:352
void wrapFootprint(WrapperCollection &)
Definition: _footprint.cc:76
std::shared_ptr< Footprint > mergeFootprints(Footprint const &footprint1, Footprint const &footprint2)
Merges two Footprints – appends their peaks, and unions their spans, returning a new Footprint.
Definition: Footprint.cc:327
A base class for image defects.