lsst.afw  13.0-92-g8ecf155+1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
spanSet.cc
Go to the documentation of this file.
1 
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2016 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 #include <sstream>
29 #include <string>
30 #include <iostream>
31 
32 #include "numpy/arrayobject.h"
33 #include "ndarray/pybind11.h"
34 
36 #include "lsst/afw/geom/SpanSet.h"
38 
39 namespace py = pybind11;
40 using namespace pybind11::literals;
41 
42 namespace lsst {
43 namespace afw {
44 namespace geom {
45 
46 namespace {
47 
48 using PySpanSet = py::class_<SpanSet, std::shared_ptr<SpanSet>, table::io::PersistableFacade<SpanSet>>;
49 
50 // SpanSet's inheritance from afw::table::io::Persistable is not exposed to
51 // Python because doing so introduces a circular dependency between afw.table
52 // and afw.geom. Luckily, Python code doesn't really care about the
53 // Persistable interface most of the time, and we can add Persistable's
54 // methods directly to SpanSet using this function. In the long term, this
55 // should be fixed by moving afw.table.io out of afw.table, since it actually
56 // has a much more lightweight set of dependencies than afw.table.
57 void declarePersistable(PySpanSet &cls) {
58  cls.def("writeFits",
59  (void (SpanSet::*)(std::string const &, std::string const &) const) & SpanSet::writeFits,
60  "fileName"_a, "mode"_a = "w");
61  cls.def("writeFits",
62  (void (SpanSet::*)(fits::MemFileManager &, std::string const &) const) & SpanSet::writeFits,
63  "manager"_a, "mode"_a = "w");
64  cls.def("isPersistable", &SpanSet::isPersistable);
65 }
66 
67 template <typename Pixel, typename PyClass>
68 void declareFlattenMethod(PyClass &cls) {
69  cls.def("flatten", (ndarray::Array<Pixel, 1, 1> (SpanSet::*)(ndarray::Array<Pixel, 2, 0> const &,
70  Point2I const &) const) &
71  SpanSet::flatten<Pixel, 2, 0>,
72  "input"_a, "xy0"_a = Point2I());
73  cls.def("flatten", (ndarray::Array<Pixel, 2, 2> (SpanSet::*)(ndarray::Array<Pixel, 3, 0> const &,
74  Point2I const &) const) &
75  SpanSet::flatten<Pixel, 3, 0>,
76  "input"_a, "xy0"_a = Point2I());
77  cls.def("flatten", (void (SpanSet::*)(ndarray::Array<Pixel, 1, 0> const &,
78  ndarray::Array<Pixel, 2, 0> const &, Point2I const &) const) &
79  SpanSet::flatten<Pixel, Pixel, 2, 0, 0>,
80  "output"_a, "input"_a, "xy0"_a = Point2I());
81  cls.def("flatten", (void (SpanSet::*)(ndarray::Array<Pixel, 2, 0> const &,
82  ndarray::Array<Pixel, 3, 0> const &, Point2I const &) const) &
83  SpanSet::flatten<Pixel, Pixel, 3, 0, 0>,
84  "output"_a, "input"_a, "xy0"_a = Point2I());
85 }
86 
87 template <typename Pixel, typename PyClass>
88 void declareUnflattenMethod(PyClass &cls) {
89  cls.def("unflatten",
90  (ndarray::Array<Pixel, 2, 2> (SpanSet::*)(ndarray::Array<Pixel, 1, 0> const &input) const) &
91  SpanSet::unflatten<Pixel, 1, 0>);
92  cls.def("unflatten",
93  (ndarray::Array<Pixel, 3, 3> (SpanSet::*)(ndarray::Array<Pixel, 2, 0> const &input) const) &
94  SpanSet::unflatten<Pixel, 2, 0>);
95  cls.def("unflatten", (void (SpanSet::*)(ndarray::Array<Pixel, 2, 0> const &,
96  ndarray::Array<Pixel, 1, 0> const &, Point2I const &) const) &
97  SpanSet::unflatten<Pixel, Pixel, 1, 0, 0>,
98  "output"_a, "input"_a, "xy0"_a = Point2I());
99  cls.def("unflatten", (void (SpanSet::*)(ndarray::Array<Pixel, 3, 0> const &,
100  ndarray::Array<Pixel, 2, 0> const &, Point2I const &) const) &
101  SpanSet::unflatten<Pixel, Pixel, 2, 0, 0>,
102  "output"_a, "input"_a, "xy0"_a = Point2I());
103 }
104 
105 template <typename Pixel, typename PyClass>
106 void declareSetMaskMethod(PyClass &cls) {
107  cls.def("setMask", (void (SpanSet::*)(image::Mask<Pixel> &, Pixel) const) & SpanSet::setMask);
108 }
109 
110 template <typename Pixel, typename PyClass>
111 void declareClearMaskMethod(PyClass &cls) {
112  cls.def("clearMask", (void (SpanSet::*)(image::Mask<Pixel> &, Pixel) const) & SpanSet::clearMask);
113 }
114 
115 template <typename Pixel, typename PyClass>
116 void declareIntersectMethod(PyClass &cls) {
117  cls.def("intersect", (std::shared_ptr<SpanSet> (SpanSet::*)(image::Mask<Pixel> const &, Pixel) const) &
118  SpanSet::intersect,
119  "other"_a, "bitmask"_a);
120  // Default to compare any bit set
121  cls.def("intersect",
122  [](SpanSet const &self, image::Mask<Pixel> const &mask) {
123  auto tempSpanSet = SpanSet::fromMask(mask);
124  return self.intersect(*tempSpanSet);
125  },
126  "other"_a);
127 }
128 
129 template <typename Pixel, typename PyClass>
130 void declareIntersectNotMethod(PyClass &cls) {
131  cls.def("intersectNot", (std::shared_ptr<SpanSet> (SpanSet::*)(image::Mask<Pixel> const &, Pixel) const) &
132  SpanSet::intersectNot,
133  "other"_a, "bitmask"_a);
134  // Default to compare any bit set
135  cls.def("intersectNot",
136  [](SpanSet const &self, image::Mask<Pixel> const &mask) {
137  auto tempSpanSet = SpanSet::fromMask(mask);
138  return self.intersectNot(*tempSpanSet);
139  },
140  "other"_a);
141 }
142 
143 template <typename Pixel, typename PyClass>
144 void declareUnionMethod(PyClass &cls) {
145  cls.def("union", (std::shared_ptr<SpanSet> (SpanSet::*)(image::Mask<Pixel> const &, Pixel) const) &
146  SpanSet::union_,
147  "other"_a, "bitmask"_a);
148  // Default to compare any bit set
149  cls.def("union",
150  [](SpanSet const &self, image::Mask<Pixel> const &mask) {
151  auto tempSpanSet = SpanSet::fromMask(mask);
152  return self.union_(*tempSpanSet);
153  },
154  "other"_a);
155 }
156 
157 template <typename ImageT, typename PyClass>
158 void declareCopyImage(PyClass &cls) {
159  cls.def("copyImage", &SpanSet::copyImage<ImageT>);
160 }
161 
162 template <typename ImageT, typename PyClass>
163 void declareCopyMaskedImage(PyClass &cls) {
164  using MaskPixel = image::MaskPixel;
166  cls.def("copyMaskedImage", &SpanSet::copyMaskedImage<ImageT, MaskPixel, VariancePixel>);
167 }
168 
169 template <typename ImageT, typename PyClass>
170 void declareSetImage(PyClass &cls) {
171  cls.def("setImage", (void (SpanSet::*)(image::Image<ImageT> &, ImageT, geom::Box2I const &, bool) const) &
172  SpanSet::setImage,
173  "image"_a, "val"_a, "region"_a = geom::Box2I(), "doClip"_a = false);
174 }
175 
176 template <typename MaskPixel, typename PyClass>
177 void declarefromMask(PyClass &cls) {
178  cls.def_static("fromMask", [](image::Mask<MaskPixel> mask) { return SpanSet::fromMask(mask); });
179  cls.def_static("fromMask", [](image::Mask<MaskPixel> mask, MaskPixel const &bitmask) {
180  return SpanSet::fromMask(mask, bitmask);
181  });
182 }
183 
184 template <typename Pixel, typename PyClass>
185 void declareMaskMethods(PyClass &cls) {
186  declareSetMaskMethod<Pixel>(cls);
187  declareClearMaskMethod<Pixel>(cls);
188  declareIntersectMethod<Pixel>(cls);
189  declareIntersectNotMethod<Pixel>(cls);
190  declareUnionMethod<Pixel>(cls);
191 }
192 
193 template <typename Pixel, typename PyClass>
194 void declareImageTypes(PyClass &cls) {
195  declareFlattenMethod<Pixel>(cls);
196  declareUnflattenMethod<Pixel>(cls);
197  declareCopyImage<Pixel>(cls);
198  declareCopyMaskedImage<Pixel>(cls);
199  declareSetImage<Pixel>(cls);
200 }
201 
202 } // end anonymous namespace
203 
204 PYBIND11_PLUGIN(spanSet) {
205  py::module mod("spanSet");
206  using MaskPixel = image::MaskPixel;
207 
208  py::module::import("lsst.afw.geom.span");
209 
210  if (_import_array() < 0) {
211  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
212  return nullptr;
213  }
214 
215  py::enum_<Stencil>(mod, "Stencil")
216  .value("CIRCLE", Stencil::CIRCLE)
217  .value("BOX", Stencil::BOX)
218  .value("MANHATTAN", Stencil::MANHATTAN);
219 
220  table::io::python::declarePersistableFacade<SpanSet>(mod, "SpanSet");
221 
222  PySpanSet cls(mod, "SpanSet");
223 
224  /* SpanSet Constructors */
225  cls.def(py::init<>());
226  cls.def(py::init<Box2I>(), "box"_a);
227  cls.def(py::init<std::vector<Span>, bool>(), "spans"_a, "normalize"_a = true);
228 
229  /* Mimic Persistable interface (see comment on declarePersitable above) */
230  declarePersistable(cls);
231 
232  /* SpanSet Methods */
233  cls.def("getArea", &SpanSet::getArea);
234  cls.def("getBBox", &SpanSet::getBBox);
235  cls.def("isContiguous", &SpanSet::isContiguous);
236  cls.def("shiftedBy", (std::shared_ptr<SpanSet> (SpanSet::*)(int, int) const) & SpanSet::shiftedBy);
237  cls.def("shiftedBy",
238  (std::shared_ptr<SpanSet> (SpanSet::*)(Extent2I const &) const) & SpanSet::shiftedBy);
239  cls.def("clippedTo", &SpanSet::clippedTo);
240  cls.def("transformedBy",
241  (std::shared_ptr<SpanSet> (SpanSet::*)(LinearTransform const &) const) & SpanSet::transformedBy);
242  cls.def("transformedBy",
243  (std::shared_ptr<SpanSet> (SpanSet::*)(AffineTransform const &) const) & SpanSet::transformedBy);
244  cls.def("transformedBy",
245  (std::shared_ptr<SpanSet> (SpanSet::*)(XYTransform const &) const) & SpanSet::transformedBy);
246  cls.def("overlaps", &SpanSet::overlaps);
247  cls.def("contains", (bool (SpanSet::*)(SpanSet const &) const) & SpanSet::contains);
248  cls.def("contains", (bool (SpanSet::*)(Point2I const &) const) & SpanSet::contains);
249  cls.def("computeCentroid", &SpanSet::computeCentroid);
250  cls.def("computeShape", &SpanSet::computeShape);
251  cls.def("dilated", (std::shared_ptr<SpanSet> (SpanSet::*)(int, Stencil) const) & SpanSet::dilated,
252  "radius"_a, "stencil"_a = Stencil::CIRCLE);
253  cls.def("dilated", (std::shared_ptr<SpanSet> (SpanSet::*)(SpanSet const &) const) & SpanSet::dilated);
254  cls.def("eroded", (std::shared_ptr<SpanSet> (SpanSet::*)(int, Stencil) const) & SpanSet::eroded,
255  "radius"_a, "stencil"_a = Stencil::CIRCLE);
256  cls.def("eroded", (std::shared_ptr<SpanSet> (SpanSet::*)(SpanSet const &) const) & SpanSet::eroded);
257  cls.def("intersect", (std::shared_ptr<SpanSet> (SpanSet::*)(SpanSet const &) const) & SpanSet::intersect);
258  cls.def("intersectNot",
259  (std::shared_ptr<SpanSet> (SpanSet::*)(SpanSet const &) const) & SpanSet::intersectNot);
260  cls.def("union", (std::shared_ptr<SpanSet> (SpanSet::*)(SpanSet const &) const) & SpanSet::union_);
261  cls.def_static("fromShape", (std::shared_ptr<SpanSet>(*)(int, Stencil, Point2I)) & SpanSet::fromShape,
262  "radius"_a, "stencil"_a = Stencil::CIRCLE, "offset"_a = Point2I());
263  cls.def_static("fromShape",
264  [](int r, Stencil s, std::pair<int, int> point) {
265  return SpanSet::fromShape(r, s, Point2I(point.first, point.second));
266  },
267  "radius"_a, "stencil"_a = Stencil::CIRCLE, "offset"_a = std::pair<int, int>(0, 0));
268  cls.def_static("fromShape",
269  (std::shared_ptr<SpanSet>(*)(geom::ellipses::Ellipse const &)) & SpanSet::fromShape);
270  cls.def("split", &SpanSet::split);
271  cls.def("findEdgePixels", &SpanSet::findEdgePixels);
272  cls.def("indices", [](SpanSet const &self) -> std::pair<std::vector<int>, std::vector<int>> {
273  std::vector<int> yind;
274  std::vector<int> xind;
275  yind.reserve(self.getArea());
276  xind.reserve(self.getArea());
277  for (auto const &span : self) {
278  auto y = span.getY();
279  for (int x = span.getX0(); x <= span.getX1(); ++x) {
280  yind.push_back(y);
281  xind.push_back(x);
282  }
283  }
284  return std::make_pair(yind, xind);
285  });
286 
287  /* SpanSet Operators */
288  cls.def("__eq__", [](SpanSet const &self, SpanSet const &other) -> bool { return self == other; },
289  py::is_operator());
290  cls.def("__ne__", [](SpanSet const &self, SpanSet const &other) -> bool { return self != other; },
291  py::is_operator());
292  cls.def("__iter__", [](SpanSet &self) { return py::make_iterator(self.begin(), self.end()); },
293  py::keep_alive<0, 1>());
294  cls.def("__len__", [](SpanSet const &self) -> decltype(self.size()) { return self.size(); });
295  cls.def("__contains__", [](SpanSet &self, SpanSet const &other) -> bool { return self.contains(other); });
296  cls.def("__contains__", [](SpanSet &self, Point2I &other) -> bool { return self.contains(other); });
297  cls.def("__repr__", [](SpanSet const &self) -> std::string {
298  std::ostringstream os;
299  image::Mask<MaskPixel> tempMask(self.getBBox());
300  self.setMask(tempMask, static_cast<MaskPixel>(1));
301  auto array = tempMask.getArray();
302  auto dims = array.getShape();
303  for (std::size_t i = 0; i < dims[0]; ++i) {
304  os << "[";
305  for (std::size_t j = 0; j < dims[1]; ++j) {
306  os << array[i][j];
307  if (j != dims[1] - 1) {
308  os << ", ";
309  }
310  }
311  os << "]" << std::endl;
312  }
313  return os.str();
314  });
315  cls.def("__str__", [](SpanSet const &self) -> std::string {
316  std::ostringstream os;
317  for (auto const &span : self) {
318  os << span.getY() << ": " << span.getMinX() << ".." << span.getMaxX() << std::endl;
319  }
320  return os.str();
321  });
322  // Instantiate all the templates
323 
324  declareMaskMethods<MaskPixel>(cls);
325 
326  declareImageTypes<std::uint16_t>(cls);
327  declareImageTypes<std::uint64_t>(cls);
328  declareImageTypes<int>(cls);
329  declareImageTypes<float>(cls);
330  declareImageTypes<double>(cls);
331 
332  // Extra instantiation for flatten unflatten methods
333  declareFlattenMethod<long>(cls);
334  declareUnflattenMethod<long>(cls);
335 
336  declarefromMask<MaskPixel>(cls);
337 
338  return mod.ptr();
339 }
340 }
341 }
342 } // end lsst::afw::geom
Stencil
An enumeration class which describes the shapes.
Definition: SpanSet.h:64
A compact representation of a collection of pixels.
Definition: SpanSet.h:75
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.
An ellipse defined by an arbitrary BaseCore and a center point.
Definition: Ellipse.h:51
double const _a
Definition: RandomImage.cc:77
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
double x
afw::table::Key< afw::table::Array< MaskPixelT > > mask
Virtual base class for 2D transforms.
Definition: XYTransform.h:46
Point< int, 2 > Point2I
Definition: Point.h:270
float VariancePixel
default type for MaskedImage variance images
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:486
int end