LSSTApplications  18.1.0
LSSTDataManagementBasePackage
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 "ndarray/pybind11.h"
30 
31 #include "lsst/pex/exceptions.h"
32 #include "lsst/afw/table/io/python.h" // for addPersistableMethods
34 
35 namespace py = pybind11;
36 using namespace pybind11::literals;
37 
38 namespace lsst {
39 namespace afw {
40 namespace detection {
41 
42 namespace {
43 
44 template <typename MaskT>
45 void declareMaskFromFootprintList(py::module &mod) {
46  auto maskSetter = [](lsst::afw::image::Mask<MaskT> *mask,
48  MaskT const bitmask, bool doClip) {
49  for (auto const &foot : footprints) {
50  try {
51  if (doClip) {
52  auto tmpSpan = foot->getSpans()->clippedTo(mask->getBBox());
53  tmpSpan->setMask(*mask, bitmask);
54  } else {
55  foot->getSpans()->setMask(*mask, bitmask);
56  }
59  "Bounds of a Footprint fall outside mask set doClip to force");
60  }
61  }
62  };
63  mod.def("setMaskFromFootprintList", std::move(maskSetter), "mask"_a, "footprints"_a, "bitmask"_a,
64  "doClip"_a = true);
65 }
66 
67 } // end anonymous namespace
68 
70  /* Footprint Constructors */
71  py::class_<Footprint, std::shared_ptr<Footprint>, daf::base::Citizen> clsFootprint(mod, "Footprint");
72  clsFootprint.def(py::init<std::shared_ptr<geom::SpanSet>, lsst::geom::Box2I const &>(), "inputSpans"_a,
73  "region"_a = lsst::geom::Box2I());
74 
75  clsFootprint.def(
77  "inputSpans"_a, "peakSchema"_a, "region"_a = lsst::geom::Box2I());
78  clsFootprint.def(py::init<Footprint const &>());
79  clsFootprint.def(py::init<>());
80 
81  table::io::python::addPersistableMethods<Footprint>(clsFootprint);
82 
83  /* Footprint Methods */
84  clsFootprint.def("getSpans", &Footprint::getSpans);
85  clsFootprint.def("setSpans", &Footprint::setSpans);
86  clsFootprint.def("getPeaks", (PeakCatalog & (Footprint::*)()) & Footprint::getPeaks,
87  py::return_value_policy::reference_internal);
88  clsFootprint.def("addPeak", &Footprint::addPeak);
89  clsFootprint.def("sortPeaks", &Footprint::sortPeaks, "key"_a = afw::table::Key<float>());
90  clsFootprint.def("setPeakSchema", &Footprint::setPeakSchema);
91  clsFootprint.def("setPeakCatalog", &Footprint::setPeakCatalog, "otherPeaks"_a);
92  clsFootprint.def("getArea", &Footprint::getArea);
93  clsFootprint.def("getCentroid", &Footprint::getCentroid);
94  clsFootprint.def("getShape", &Footprint::getShape);
95  clsFootprint.def("shift", (void (Footprint::*)(int, int)) & Footprint::shift);
96  clsFootprint.def("shift", (void (Footprint::*)(lsst::geom::ExtentI const &)) & Footprint::shift);
97  clsFootprint.def("getBBox", &Footprint::getBBox);
98  clsFootprint.def("getRegion", &Footprint::getRegion);
99  clsFootprint.def("setRegion", &Footprint::setRegion);
100  clsFootprint.def("clipTo", &Footprint::clipTo);
101  clsFootprint.def("contains", &Footprint::contains);
102  clsFootprint.def("transform", (std::shared_ptr<Footprint> (Footprint::*)(
104  lsst::geom::Box2I const &, bool) const) &
106  "source"_a, "target"_a, "region"_a, "doClip"_a = true);
107  clsFootprint.def("transform", (std::shared_ptr<Footprint> (Footprint::*)(
108  lsst::geom::LinearTransform const &, lsst::geom::Box2I const &, bool) const) &
109  Footprint::transform);
110  clsFootprint.def("transform", (std::shared_ptr<Footprint> (Footprint::*)(
111  lsst::geom::AffineTransform const &, lsst::geom::Box2I const &, bool) const) &
112  Footprint::transform);
113  clsFootprint.def("transform",
115  lsst::geom::Box2I const &, bool) const) &
116  Footprint::transform);
117  clsFootprint.def("dilate", (void (Footprint::*)(int, geom::Stencil)) & Footprint::dilate, "r"_a,
118  "stencil"_a = geom::Stencil::CIRCLE);
119  clsFootprint.def("dilate", (void (Footprint::*)(geom::SpanSet const &)) & Footprint::dilate);
120  clsFootprint.def("erode", (void (Footprint::*)(int, geom::Stencil)) & Footprint::erode, "r"_a,
121  "stencil"_a = geom::Stencil::CIRCLE);
122  clsFootprint.def("erode", (void (Footprint::*)(geom::SpanSet const &)) & Footprint::erode);
123  clsFootprint.def("removeOrphanPeaks", &Footprint::removeOrphanPeaks);
124  clsFootprint.def("isContiguous", &Footprint::isContiguous);
125  clsFootprint.def("isHeavy", &Footprint::isHeavy);
126  clsFootprint.def("assign", (Footprint & (Footprint::*)(Footprint const &)) & Footprint::operator=);
127  clsFootprint.def("split", [](Footprint const &self) -> py::list {
128  // This is a work around for pybind not properly
129  // handling converting a vector of unique pointers
130  // to python lists of shared pointers
131  py::list l;
132  for (auto &ptr : self.split()) {
133  l.append(py::cast(std::shared_ptr<Footprint>(std::move(ptr))));
134  }
135  return l;
136  });
137 
138  /* Define python level properties */
139  clsFootprint.def_property("spans", &Footprint::getSpans, &Footprint::setSpans);
140  clsFootprint.def_property_readonly("peaks", (PeakCatalog & (Footprint::*)()) & Footprint::getPeaks,
141  py::return_value_policy::reference);
142 
143  /* Python Operators functions */
144  clsFootprint.def("__contains__", [](Footprint const &self, lsst::geom::Point2I const &point) -> bool {
145  return self.contains(point);
146  });
147  clsFootprint.def("__eq__",
148  [](Footprint const &self, Footprint const &other) -> bool { return self == other; },
149  py::is_operator());
150 
151  declareMaskFromFootprintList<lsst::afw::image::MaskPixel>(mod);
152 
153  mod.def("mergeFootprints", &mergeFootprints);
154  mod.def("footprintToBBoxList", &footprintToBBoxList);
155 }
156 }
157 }
158 } // close lsst::afw::detection
Defines the fields and offsets for a table.
Definition: Schema.h:50
uint64_t * ptr
Definition: RangeSet.cc:88
Stencil
An enumeration class which describes the shapes.
Definition: SpanSet.h:66
bool contains(VertexIterator const begin, VertexIterator const end, UnitVector3d const &v)
An affine coordinate transformation consisting of a linear transformation and an offset.
A compact representation of a collection of pixels.
Definition: SpanSet.h:77
def init()
Definition: tests.py:75
A base class for image defects.
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:463
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:78
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:333
PYBIND11_MODULE(footprint, mod)
Definition: footprint.cc:69
T move(T... args)
Class to describe the properties of a detected object from an image.
Definition: Footprint.h:62
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:358
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Reports attempts to access elements outside a valid range of indices.
Definition: Runtime.h:89
afw::table::Key< afw::table::Array< MaskPixelT > > mask
STL class.
ItemVariant const * other
Definition: Schema.cc:56
table::Key< int > transform
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:55
An integer coordinate rectangle.
Definition: Box.h:54
daf::base::PropertyList * list
Definition: fits.cc:885
A 2D linear coordinate transformation.
Transform LSST spatial data, such as lsst::geom::Point2D and lsst::geom::SpherePoint, using an AST mapping.
Definition: Transform.h:67