lsst.afw  14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
footprint.cc
Go to the documentation of this file.
1 
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2017 AURA/LSST.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <https://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include "pybind11/pybind11.h"
25 #include "pybind11/stl.h"
26 
27 #include <cstdint>
28 
29 #include "numpy/arrayobject.h"
30 #include "ndarray/pybind11.h"
31 
32 #include "lsst/pex/exceptions.h"
34 #include "lsst/afw/table/io/python.h" // for declarePersistableFacade
36 
37 namespace py = pybind11;
38 using namespace pybind11::literals;
39 
40 namespace lsst {
41 namespace afw {
42 namespace detection {
43 
44 namespace {
45 
46 template <typename MaskT>
47 void declareMaskFromFootprintList(py::module &mod) {
48  auto maskSetter = [](lsst::afw::image::Mask<MaskT> *mask,
49  std::vector<std::shared_ptr<lsst::afw::detection::Footprint>> const &footprints,
50  MaskT const bitmask, bool doClip) {
51  for (auto const &foot : footprints) {
52  try {
53  if (doClip) {
54  auto tmpSpan = foot->getSpans()->clippedTo(mask->getBBox());
55  tmpSpan->setMask(*mask, bitmask);
56  } else {
57  foot->getSpans()->setMask(*mask, bitmask);
58  }
59  } catch (lsst::pex::exceptions::OutOfRangeError e) {
60  throw LSST_EXCEPT(lsst::pex::exceptions::OutOfRangeError,
61  "Bounds of a Footprint fall outside mask set doClip to force");
62  }
63  }
64  };
65  mod.def("setMaskFromFootprintList", std::move(maskSetter), "mask"_a, "footprints"_a, "bitmask"_a,
66  "doClip"_a = true);
67 }
68 
69 } // end anonymous namespace
70 
71 PYBIND11_PLUGIN(_footprint) {
72  py::module mod("_footprint", "Python wrapper for afw Footprint library");
73  table::io::python::declarePersistableFacade<Footprint>(mod, "Footprint");
74 
75  if (_import_array() < 0) {
76  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
77  return nullptr;
78  }
79 
80  /* Footprint Constructors */
81  py::class_<Footprint, std::shared_ptr<Footprint>, daf::base::Citizen, table::io::Persistable,
83  clsFootprint(mod, "Footprint");
84  clsFootprint.def(py::init<std::shared_ptr<geom::SpanSet>, geom::Box2I const &>(), "inputSpans"_a,
85  "region"_a = geom::Box2I());
86 
87  clsFootprint.def(
88  py::init<std::shared_ptr<geom::SpanSet>, afw::table::Schema const &, geom::Box2I const &>(),
89  "inputSpans"_a, "peakSchema"_a, "region"_a = geom::Box2I());
90  clsFootprint.def(py::init<Footprint const &>());
91  clsFootprint.def(py::init<>());
92 
93  /* Footprint Methods */
94  clsFootprint.def("getSpans", &Footprint::getSpans);
95  clsFootprint.def("setSpans", &Footprint::setSpans);
96  clsFootprint.def("getPeaks", (PeakCatalog & (Footprint::*)()) & Footprint::getPeaks,
97  py::return_value_policy::reference_internal);
98  clsFootprint.def("addPeak", &Footprint::addPeak);
99  clsFootprint.def("sortPeaks", &Footprint::sortPeaks, "key"_a = afw::table::Key<float>());
100  clsFootprint.def("setPeakSchema", &Footprint::setPeakSchema);
101  clsFootprint.def("getArea", &Footprint::getArea);
102  clsFootprint.def("getCentroid", &Footprint::getCentroid);
103  clsFootprint.def("getShape", &Footprint::getShape);
104  clsFootprint.def("shift", (void (Footprint::*)(int, int)) & Footprint::shift);
105  clsFootprint.def("shift", (void (Footprint::*)(geom::ExtentI const &)) & Footprint::shift);
106  clsFootprint.def("getBBox", &Footprint::getBBox);
107  clsFootprint.def("getRegion", &Footprint::getRegion);
108  clsFootprint.def("setRegion", &Footprint::setRegion);
109  clsFootprint.def("clipTo", &Footprint::clipTo);
110  clsFootprint.def("contains", &Footprint::contains);
111  clsFootprint.def("transform", (std::shared_ptr<Footprint> (Footprint::*)(
112  std::shared_ptr<image::Wcs>, std::shared_ptr<image::Wcs>,
113  geom::Box2I const &, bool) const) &
114  Footprint::transform,
115  "source"_a, "target"_a, "region"_a, "doClip"_a = true);
116  clsFootprint.def("transform", (std::shared_ptr<Footprint> (Footprint::*)(
117  geom::LinearTransform const &, geom::Box2I const &, bool) const) &
118  Footprint::transform);
119  clsFootprint.def("transform", (std::shared_ptr<Footprint> (Footprint::*)(
120  geom::AffineTransform const &, geom::Box2I const &, bool) const) &
121  Footprint::transform);
122  clsFootprint.def("transform", (std::shared_ptr<Footprint> (Footprint::*)(
123  geom::XYTransform const &, geom::Box2I const &, bool) const) &
124  Footprint::transform);
125  clsFootprint.def("dilate", (void (Footprint::*)(int, geom::Stencil)) & Footprint::dilate, "r"_a,
126  "stencil"_a = geom::Stencil::CIRCLE);
127  clsFootprint.def("dilate", (void (Footprint::*)(geom::SpanSet const &)) & Footprint::dilate);
128  clsFootprint.def("erode", (void (Footprint::*)(int, geom::Stencil)) & Footprint::erode, "r"_a,
129  "stencil"_a = geom::Stencil::CIRCLE);
130  clsFootprint.def("erode", (void (Footprint::*)(geom::SpanSet const &)) & Footprint::erode);
131  clsFootprint.def("removeOrphanPeaks", &Footprint::removeOrphanPeaks);
132  clsFootprint.def("isContiguous", &Footprint::isContiguous);
133  clsFootprint.def("isHeavy", &Footprint::isHeavy);
134  clsFootprint.def("assign", (Footprint & (Footprint::*)(Footprint const &)) & Footprint::operator=);
135  clsFootprint.def("split", [](Footprint const &self) -> py::list {
136  // This is a work around for pybind not properly
137  // handling converting a vector of unique pointers
138  // to python lists of shared pointers
139  py::list l;
140  for (auto &ptr : self.split()) {
141  l.append(py::cast(std::shared_ptr<Footprint>(std::move(ptr))));
142  }
143  return l;
144  });
145 
146  /* Define python level properties */
147  clsFootprint.def_property("spans", &Footprint::getSpans, &Footprint::setSpans);
148  clsFootprint.def_property_readonly("peaks", (PeakCatalog & (Footprint::*)()) & Footprint::getPeaks,
149  py::return_value_policy::reference);
150 
151  /* Python Operators functions */
152  clsFootprint.def("__contains__", [](Footprint const &self, geom::Point2I const &point) -> bool {
153  return self.contains(point);
154  });
155  clsFootprint.def("__eq__",
156  [](Footprint const &self, Footprint const &other) -> bool { return self == other; },
157  py::is_operator());
158 
159  declareMaskFromFootprintList<lsst::afw::image::MaskPixel>(mod);
160 
161  mod.def("mergeFootprints", &mergeFootprints);
162  mod.def("footprintToBBoxList", &footprintToBBoxList);
163 
164  return mod.ptr();
165 }
166 }
167 }
168 } // close lsst::afw::detection
Defines the fields and offsets for a table.
Definition: Schema.h:46
Stencil
An enumeration class which describes the shapes.
Definition: SpanSet.h:64
A compact representation of a collection of pixels.
Definition: SpanSet.h:75
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: Image.h:456
An integer coordinate rectangle.
Definition: Box.h:55
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:73
A 2D linear coordinate transformation.
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
An affine coordinate transformation consisting of a linear transformation and an offset.
double const _a
Definition: RandomImage.cc:77
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:321
Class to describe the properties of a detected object from an image.
Definition: Footprint.h:62
afw::table::Key< afw::table::Array< MaskPixelT > > mask
Virtual base class for 2D transforms.
Definition: XYTransform.h:46
std::vector< lsst::afw::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:346
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:173