LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
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 
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
ItemVariant const * other
Definition: Schema.cc:56
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
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition: python.h:242
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition: python.h:448
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
Definition: python.h:391
void addInheritanceDependency(std::string const &name)
Indicate an external module that provides a base class for a subsequent addType call.
Definition: python.h:343
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
Definition: python.h:357
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.