LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
_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
std::shared_ptr
STL class.
lsst::afw::image::Mask
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
std::move
T move(T... args)
lsst::afw::detection::wrapFootprint
void wrapFootprint(WrapperCollection &)
Definition: _footprint.cc:76
lsst::utils::python::WrapperCollection::addInheritanceDependency
void addInheritanceDependency(std::string const &name)
Indicate an external module that provides a base class for a subsequent addType call.
Definition: python.h:343
std::vector
STL class.
python.h
lsst::afw
Definition: imageAlgorithm.dox:1
mask
afw::table::Key< afw::table::Array< MaskPixelT > > mask
Definition: HeavyFootprint.cc:217
lsst::utils::python::WrapperCollection::addSignatureDependency
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
Definition: python.h:357
lsst::afw::geom.transform.transformContinued.cls
cls
Definition: transformContinued.py:33
python.h
lsst::utils::python::WrapperCollection::wrapType
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
lsst::afw::detection::footprintToBBoxList
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
other
ItemVariant const * other
Definition: Schema.cc:56
Footprint.h
lsst::utils::python::WrapperCollection
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition: python.h:242
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::detection::mergeFootprints
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
lsst::geom::Point< int, 2 >
lsst.pex::exceptions::OutOfRangeError
Reports attempts to access elements outside a valid range of indices.
Definition: Runtime.h:89
pybind11
Definition: _GenericMap.cc:40
lsst::utils::python::WrapperCollection::module
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition: python.h:448
lsst::afw::detection::Footprint
Class to describe the properties of a detected object from an image.
Definition: Footprint.h:63
lsst::afw::table::CatalogT< PeakRecord >
exceptions.h