24 #include "pybind11/pybind11.h"
25 #include "pybind11/stl.h"
32 #include "numpy/arrayobject.h"
33 #include "ndarray/pybind11.h"
39 namespace py = pybind11;
40 using namespace pybind11::literals;
48 using PySpanSet = py::class_<SpanSet, std::shared_ptr<SpanSet>, table::io::PersistableFacade<SpanSet>>;
57 void declarePersistable(PySpanSet &
cls) {
59 (
void (SpanSet::*)(std::string
const &, std::string
const &)
const) & SpanSet::writeFits,
60 "fileName"_a,
"mode"_a =
"w");
62 (
void (SpanSet::*)(fits::MemFileManager &, std::string
const &)
const) & SpanSet::writeFits,
63 "manager"_a,
"mode"_a =
"w");
64 cls.def(
"isPersistable", &SpanSet::isPersistable);
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 &,
71 SpanSet::flatten<Pixel, 2, 0>,
73 cls.def(
"flatten", (ndarray::Array<Pixel, 2, 2> (SpanSet::*)(ndarray::Array<Pixel, 3, 0>
const &,
75 SpanSet::flatten<Pixel, 3, 0>,
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>,
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>,
87 template <
typename Pixel,
typename PyClass>
88 void declareUnflattenMethod(PyClass &cls) {
90 (ndarray::Array<Pixel, 2, 2> (SpanSet::*)(ndarray::Array<Pixel, 1, 0>
const &input)
const) &
91 SpanSet::unflatten<Pixel, 1, 0>);
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>,
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>,
105 template <
typename Pixel,
typename PyClass>
106 void declareSetMaskMethod(PyClass &cls) {
107 cls.def(
"setMask", (
void (SpanSet::*)(
image::Mask<Pixel> &, Pixel)
const) & SpanSet::setMask);
110 template <
typename Pixel,
typename PyClass>
111 void declareClearMaskMethod(PyClass &cls) {
112 cls.def(
"clearMask", (
void (SpanSet::*)(
image::Mask<Pixel> &, Pixel)
const) & SpanSet::clearMask);
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) &
119 "other"_a,
"bitmask"_a);
123 auto tempSpanSet = SpanSet::fromMask(mask);
124 return self.intersect(*tempSpanSet);
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);
135 cls.def(
"intersectNot",
137 auto tempSpanSet = SpanSet::fromMask(mask);
138 return self.intersectNot(*tempSpanSet);
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) &
147 "other"_a,
"bitmask"_a);
151 auto tempSpanSet = SpanSet::fromMask(mask);
152 return self.union_(*tempSpanSet);
157 template <
typename ImageT,
typename PyClass>
158 void declareCopyImage(PyClass &cls) {
159 cls.def(
"copyImage", &SpanSet::copyImage<ImageT>);
162 template <
typename ImageT,
typename PyClass>
163 void declareCopyMaskedImage(PyClass &cls) {
166 cls.def(
"copyMaskedImage", &SpanSet::copyMaskedImage<ImageT, MaskPixel, VariancePixel>);
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) &
173 "image"_a,
"val"_a,
"region"_a = geom::Box2I(),
"doClip"_a =
false);
176 template <
typename MaskPixel,
typename PyClass>
177 void declarefromMask(PyClass &cls) {
180 return SpanSet::fromMask(mask, bitmask);
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);
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);
205 py::module mod(
"spanSet");
208 py::module::import(
"lsst.afw.geom.span");
210 if (_import_array() < 0) {
211 PyErr_SetString(PyExc_ImportError,
"numpy.core.multiarray failed to import");
215 py::enum_<Stencil>(mod,
"Stencil")
216 .value(
"CIRCLE", Stencil::CIRCLE)
217 .value(
"BOX", Stencil::BOX)
218 .value(
"MANHATTAN", Stencil::MANHATTAN);
220 table::io::python::declarePersistableFacade<SpanSet>(mod,
"SpanSet");
222 PySpanSet
cls(mod,
"SpanSet");
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);
230 declarePersistable(cls);
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);
238 (std::shared_ptr<SpanSet> (
SpanSet::*)(
Extent2I const &)
const) & SpanSet::shiftedBy);
239 cls.def(
"clippedTo", &SpanSet::clippedTo);
240 cls.def(
"transformedBy",
242 cls.def(
"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));
267 "radius"_a,
"stencil"_a = Stencil::CIRCLE,
"offset"_a = std::pair<int, int>(0, 0));
268 cls.def_static(
"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) {
284 return std::make_pair(yind, xind);
288 cls.def(
"__eq__", [](
SpanSet const &
self,
SpanSet const &other) ->
bool {
return self == other; },
290 cls.def(
"__ne__", [](
SpanSet const &
self,
SpanSet const &other) ->
bool {
return self != other; },
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;
300 self.setMask(tempMask, static_cast<MaskPixel>(1));
302 auto dims = array.getShape();
303 for (std::size_t i = 0; i < dims[0]; ++i) {
305 for (std::size_t j = 0; j < dims[1]; ++j) {
307 if (j != dims[1] - 1) {
311 os <<
"]" << std::endl;
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;
324 declareMaskMethods<MaskPixel>(
cls);
326 declareImageTypes<std::uint16_t>(
cls);
327 declareImageTypes<std::uint64_t>(
cls);
328 declareImageTypes<int>(
cls);
329 declareImageTypes<float>(
cls);
330 declareImageTypes<double>(
cls);
333 declareFlattenMethod<long>(
cls);
334 declareUnflattenMethod<long>(
cls);
336 declarefromMask<MaskPixel>(
cls);
Stencil
An enumeration class which describes the shapes.
A compact representation of a collection of pixels.
PYBIND11_PLUGIN(_cameraPoint)
Represent a 2-dimensional array of bitmask pixels.
An ellipse defined by an arbitrary BaseCore and a center point.
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
float VariancePixel
default type for MaskedImage variance images
A class to represent a 2-dimensional array of pixels.