Skip to content

Commit

Permalink
Merge pull request #26 from lsst/tickets/DM-25877
Browse files Browse the repository at this point in the history
DM-25877: Change pybind11 to use a single importable module
  • Loading branch information
timj committed Jul 9, 2020
2 parents 009f3de + b0bfd1d commit d11eeda
Show file tree
Hide file tree
Showing 31 changed files with 334 additions and 241 deletions.
35 changes: 35 additions & 0 deletions include/lsst/sphgeom/python.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* LSST Data Management System
* See COPYRIGHT file at the top of the source tree.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <https://www.lsstcorp.org/LegalNotices/>.
*/

#ifndef PYTHON_LSST_SPHGEOM_SPHGEOM_H
#define PYTHON_LSST_SPHGEOM_SPHGEOM_H

namespace lsst {
namespace sphgeom {

template <typename Pybind11Class>
void defineClass(Pybind11Class &cls);

} // sphgeom
} // lsst

#endif // PYTHON_LSST_SPHGEOM_SPHGEOM_H
61 changes: 34 additions & 27 deletions python/lsst/sphgeom/SConscript
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
from lsst.sconsUtils import scripts
scripts.BasicSConscript.pybind11([
'angle',
'angleInterval',
'box',
'box3d',
'chunker',
'circle',
'convexPolygon',
'curve',
'ellipse',
'htmPixelization',
'interval1d',
'lonLat',
'matrix3d',
'mq3cPixelization',
'normalizedAngle',
'normalizedAngleInterval',
'orientation',
'pixelization',
'q3cPixelization',
'rangeSet',
'region',
'relationship',
'unitVector3d',
'utils',
'vector3d',
], addUnderscore=False)

scripts.BasicSConscript.pybind11(
["_sphgeom"],
extraSrc={
"_sphgeom": [
"_angle.cc",
"_angleInterval.cc",
"_box.cc",
"_box3d.cc",
"_chunker.cc",
"_circle.cc",
"_convexPolygon.cc",
"_curve.cc",
"_ellipse.cc",
"_htmPixelization.cc",
"_interval1d.cc",
"_lonLat.cc",
"_matrix3d.cc",
"_mq3cPixelization.cc",
"_normalizedAngle.cc",
"_normalizedAngleInterval.cc",
"_orientation.cc",
"_pixelization.cc",
"_q3cPixelization.cc",
"_rangeSet.cc",
"_region.cc",
"_relationship.cc",
"_unitVector3d.cc",
"_utils.cc",
"_vector3d.cc",
]
},
addUnderscore=False
)
26 changes: 1 addition & 25 deletions python/lsst/sphgeom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,6 @@
"""lsst.sphgeom
"""

from .region import *
from .pixelization import *
from .angle import *
from .angleInterval import *
from .box import *
from .box3d import *
from .chunker import *
from .circle import *
from .convexPolygon import *
from .curve import *
from .ellipse import *
from .htmPixelization import *
from .interval1d import *
from .lonLat import *
from .matrix3d import *
from .mq3cPixelization import *
from .normalizedAngle import *
from .normalizedAngleInterval import *
from .orientation import *
from .q3cPixelization import *
from .rangeSet import *
from .relationship import *
from .unitVector3d import *
from .utils import *
from .vector3d import *
from ._sphgeom import *
from .version import *
from ._yaml import *
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
#include "pybind11/pybind11.h"

#include "lsst/sphgeom/python.h"

#include "lsst/sphgeom/Angle.h"
#include "lsst/sphgeom/NormalizedAngle.h"

Expand All @@ -29,11 +31,9 @@ using namespace pybind11::literals;

namespace lsst {
namespace sphgeom {
namespace {

PYBIND11_MODULE(angle, mod) {
py::class_<Angle> cls(mod, "Angle");

template <>
void defineClass(py::class_<Angle> &cls) {
cls.def_static("nan", &Angle::nan);
cls.def_static("fromDegrees", &Angle::fromDegrees);
cls.def_static("fromRadians", &Angle::fromRadians);
Expand Down Expand Up @@ -92,6 +92,5 @@ PYBIND11_MODULE(angle, mod) {
});
}

} // <anonymous>
} // sphgeom
} // lsst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
*/
#include "pybind11/pybind11.h"

#include <memory>
#include <stdexcept>
#include "lsst/sphgeom/python.h"

#include "lsst/sphgeom/AngleInterval.h"
#include "lsst/sphgeom/python/interval.h"
Expand All @@ -32,14 +31,10 @@ using namespace pybind11::literals;

namespace lsst {
namespace sphgeom {
namespace {

PYBIND11_MODULE(angleInterval, mod) {
py::module::import("lsst.sphgeom.angle");

py::class_<AngleInterval, std::shared_ptr<AngleInterval>> cls(
mod, "AngleInterval");

template <>
void defineClass(
py::class_<AngleInterval, std::shared_ptr<AngleInterval>> &cls) {
python::defineInterval<decltype(cls), AngleInterval, Angle>(cls);

cls.def_static("fromDegrees", &AngleInterval::fromDegrees, "x"_a, "y"_a);
Expand All @@ -62,6 +57,5 @@ PYBIND11_MODULE(angleInterval, mod) {
});
}

} // <anonymous>
} // sphgeom
} // lsst
13 changes: 5 additions & 8 deletions python/lsst/sphgeom/box.cc → python/lsst/sphgeom/_box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
#include "pybind11/pybind11.h"

#include <memory>
#include "lsst/sphgeom/python.h"

#include "lsst/sphgeom/AngleInterval.h"
#include "lsst/sphgeom/Box.h"
Expand All @@ -42,20 +42,18 @@ using namespace pybind11::literals;

namespace lsst {
namespace sphgeom {
namespace {

namespace {
std::unique_ptr<Box> decode(py::bytes bytes) {
uint8_t const *buffer = reinterpret_cast<uint8_t const *>(
PYBIND11_BYTES_AS_STRING(bytes.ptr()));
size_t n = static_cast<size_t>(PYBIND11_BYTES_SIZE(bytes.ptr()));
return Box::decode(buffer, n);
}
}

PYBIND11_MODULE(box, mod) {
py::module::import("lsst.sphgeom.region");

py::class_<Box, std::unique_ptr<Box>, Region> cls(mod, "Box");

template <>
void defineClass(py::class_<Box, std::unique_ptr<Box>, Region> &cls) {
cls.attr("TYPE_CODE") = py::int_(Box::TYPE_CODE);

cls.def_static("fromDegrees", &Box::fromDegrees, "lon1"_a, "lat1"_a,
Expand Down Expand Up @@ -167,6 +165,5 @@ PYBIND11_MODULE(box, mod) {
[](py::bytes bytes) { return decode(bytes).release(); }));
}

} // <anonymous>
} // sphgeom
} // lsst
12 changes: 3 additions & 9 deletions python/lsst/sphgeom/box3d.cc → python/lsst/sphgeom/_box3d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
#include "pybind11/pybind11.h"

#include <memory>
#include "lsst/sphgeom/python.h"

#include "lsst/sphgeom/Box3d.h"
#include "lsst/sphgeom/python/relationship.h"
Expand All @@ -32,14 +32,9 @@ using namespace pybind11::literals;

namespace lsst {
namespace sphgeom {
namespace {

PYBIND11_MODULE(box3d, mod) {
py::module::import("lsst.sphgeom.interval1d");
py::module::import("lsst.sphgeom.vector3d");

py::class_<Box3d, std::shared_ptr<Box3d>> cls(mod, "Box3d");

template <>
void defineClass(py::class_<Box3d, std::shared_ptr<Box3d>> &cls) {
cls.def_static("empty", &Box3d::empty);
cls.def_static("full", &Box3d::full);
cls.def_static("aroundUnitSphere", &Box3d::aroundUnitSphere);
Expand Down Expand Up @@ -163,6 +158,5 @@ PYBIND11_MODULE(box3d, mod) {
});
}

} // <anonymous>
} // sphgeom
} // lsst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"

#include <memory>
#include "lsst/sphgeom/python.h"

#include "lsst/sphgeom/Chunker.h"

Expand All @@ -31,17 +31,16 @@ using namespace pybind11::literals;

namespace lsst {
namespace sphgeom {
namespace {

py::str toString(Chunker const & self) {
namespace {
py::str toString(Chunker const &self) {
return py::str("Chunker({!s}, {!s})")
.format(self.getNumStripes(), self.getNumSubStripesPerStripe());
}
}


PYBIND11_MODULE(chunker, mod) {
py::class_<Chunker, std::shared_ptr<Chunker>> cls(mod, "Chunker");

template <>
void defineClass(py::class_<Chunker, std::shared_ptr<Chunker>> &cls) {
cls.def(py::init<int32_t, int32_t>(), "numStripes"_a,
"numSubStripesPerStripe"_a);

Expand All @@ -57,7 +56,7 @@ PYBIND11_MODULE(chunker, mod) {
cls.def("getSubChunksIntersecting",
[](Chunker const &self, Region const &region) {
py::list results;
for (auto const & sc: self.getSubChunksIntersecting(region)) {
for (auto const &sc : self.getSubChunksIntersecting(region)) {
results.append(py::make_tuple(sc.chunkId, sc.subChunkIds));
}
return results;
Expand All @@ -76,6 +75,5 @@ PYBIND11_MODULE(chunker, mod) {
});
}

} // <anonymous>
} // sphgeom
} // lsst
13 changes: 5 additions & 8 deletions python/lsst/sphgeom/circle.cc → python/lsst/sphgeom/_circle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
#include "pybind11/pybind11.h"

#include <memory>
#include "lsst/sphgeom/python.h"

#include "lsst/sphgeom/Box.h"
#include "lsst/sphgeom/Circle.h"
Expand All @@ -38,20 +38,18 @@ using namespace pybind11::literals;

namespace lsst {
namespace sphgeom {
namespace {

namespace {
std::unique_ptr<Circle> decode(py::bytes bytes) {
uint8_t const *buffer = reinterpret_cast<uint8_t const *>(
PYBIND11_BYTES_AS_STRING(bytes.ptr()));
size_t n = static_cast<size_t>(PYBIND11_BYTES_SIZE(bytes.ptr()));
return Circle::decode(buffer, n);
}
}

PYBIND11_MODULE(circle, mod) {
py::module::import("lsst.sphgeom.region");

py::class_<Circle, std::unique_ptr<Circle>, Region> cls(mod, "Circle");

template <>
void defineClass(py::class_<Circle, std::unique_ptr<Circle>, Region> &cls) {
cls.attr("TYPE_CODE") = py::int_(Circle::TYPE_CODE);

cls.def_static("empty", &Circle::empty);
Expand Down Expand Up @@ -149,6 +147,5 @@ PYBIND11_MODULE(circle, mod) {
[](py::bytes bytes) { return decode(bytes).release(); }));
}

} // <anonymous>
} // sphgeom
} // lsst

0 comments on commit d11eeda

Please sign in to comment.