Skip to content

Commit

Permalink
Merge pull request #372 from lsst/tickets/DM-14864
Browse files Browse the repository at this point in the history
DM-14864: Make afw pybind11 wrappers compatible with pybind11 2.2
  • Loading branch information
r-owen committed Jul 18, 2018
2 parents b4f0cd2 + 4c7bb7d commit d766a4d
Show file tree
Hide file tree
Showing 144 changed files with 584 additions and 1,645 deletions.
13 changes: 6 additions & 7 deletions examples/simpleStacker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace math = lsst::afw::math;
typedef image::Image<float> ImageF;
typedef image::MaskedImage<float> MImageF;
typedef std::vector<float> VecF;
typedef std::shared_ptr<VecF> VecFPtr;

int main(int argc, char **argv) {
int const nImg = 10;
Expand Down Expand Up @@ -85,13 +84,13 @@ int main(int argc, char **argv) {
std::cout << "MaskedImage (const weight): " << (*wmimgStack->getImage())(nX / 2, nY / 2) << std::endl;

// std::vector, and also with a constant weight vector
std::vector<VecFPtr> vecList;
std::vector<VecF> vecList;
for (int iImg = 0; iImg < nImg; ++iImg) {
VecFPtr v = VecFPtr(new VecF(nX * nY, iImg));
VecF v(nX * nY, iImg);
vecList.push_back(v);
}
VecFPtr vecStack = math::statisticsStack<float>(vecList, math::MEAN);
std::cout << "Vector: " << (*vecStack)[nX * nY / 2] << std::endl;
VecFPtr wvecStack = math::statisticsStack<float>(vecList, math::MEAN, sctrl, wvec);
std::cout << "Vector (const weight): " << (*wvecStack)[nX * nY / 2] << std::endl;
VecF vecStack = math::statisticsStack<float>(vecList, math::MEAN);
std::cout << "Vector: " << (vecStack)[nX * nY / 2] << std::endl;
VecF wvecStack = math::statisticsStack<float>(vecList, math::MEAN, sctrl, wvec);
std::cout << "Vector (const weight): " << (wvecStack)[nX * nY / 2] << std::endl;
}
4 changes: 2 additions & 2 deletions include/lsst/afw/math/Stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ void statisticsStack(lsst::afw::image::MaskedImage<PixelT>& out, ///< Output im
* A function to compute some statistics of a stack of std::vectors
*/
template <typename PixelT>
std::shared_ptr<std::vector<PixelT>> statisticsStack(
std::vector<std::shared_ptr<std::vector<PixelT>>>& vectors, ///< Vectors to process
std::vector<PixelT> statisticsStack(
std::vector<std::vector<PixelT>>& vectors, ///< Vectors to process
Property flags, ///< statistics requested
StatisticsControl const& sctrl = StatisticsControl(), ///< control structure
std::vector<lsst::afw::image::VariancePixel> const& wvector =
Expand Down
16 changes: 3 additions & 13 deletions python/lsst/afw/cameraGeom/SConscript
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
## -*- python -*-
from lsst.sconsUtils import scripts
scripts.BasicSConscript.pybind11(['cameraSys',
'detector',
'detector/detector',
'orientation',
'transformMap'])

#from lsst.sconsUtils import env, targets
#
## We can't use BasicSConscript here because the circular dependency requires the -D option in SWIGFLAGS
#targets["python"].extend(
# env.SwigLoadableModule(
# "_cameraGeomLib", ["cameraGeomLib.i"],
# SWIGFLAGS=(env["SWIGFLAGS"] + ["-DIMPORT_IMAGE_I"]),
# LIBS=env.getLibs("main python")
# )
#)
'transformMap'],
addUnderscore=False)
6 changes: 3 additions & 3 deletions python/lsst/afw/cameraGeom/cameraGeomLib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lsst.afw.geom import Point2D
from ._cameraSys import *
from .cameraSys import *
from .detector import *
from ._orientation import *
from ._transformMap import *
from .orientation import *
from .transformMap import *
4 changes: 2 additions & 2 deletions python/lsst/afw/cameraGeom/cameraSys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void declareCommonSysMethods(PyClass &cls) {
}
}

PYBIND11_PLUGIN(_cameraSys) {
py::module mod("_cameraSys", "Python wrapper for afw _cameraSys library");
PYBIND11_PLUGIN(cameraSys) {
py::module mod("cameraSys");

/* Module level */
py::class_<CameraSysPrefix> clsCameraSysPrefix(mod, "CameraSysPrefix");
Expand Down
2 changes: 2 additions & 0 deletions python/lsst/afw/cameraGeom/detector/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .detector import *
from .detectorContinued import *
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ void declare2SysMethods(PyClass &cls) {
"points"_a, "fromSys"_a, "toSys"_a);
}

PYBIND11_PLUGIN(_detector) {
py::module mod("_detector", "Python wrapper for afw _detector library");
PYBIND11_PLUGIN(detector) {
py::module mod("detector");

/* Module level */
py::class_<Detector, std::shared_ptr<Detector>> cls(mod, "Detector");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
# see <http://www.lsstcorp.org/LegalNotices/>.
#

from ._detector import Detector, DetectorType

__all__ = ["Detector", "DetectorType",
"SCIENCE", "FOCUS", "GUIDER", "WAVEFRONT"]

from lsst.utils import continueClass
from .detector import Detector, DetectorType

# export DetectorType enums as module globals for SWIG compatibility;
# @TODO update our code to stop using these globals and remove this code
SCIENCE = DetectorType.SCIENCE
Expand All @@ -33,8 +34,7 @@
WAVEFRONT = DetectorType.WAVEFRONT


def __iter__(self):
return (self[i] for i in range(len(self)))


Detector.__iter__ = __iter__
@continueClass # noqa F811
class Detector:
def __iter__(self):
return (self[i] for i in range(len(self)))
6 changes: 4 additions & 2 deletions python/lsst/afw/cameraGeom/orientation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ namespace lsst {
namespace afw {
namespace cameraGeom {

PYBIND11_PLUGIN(_orientation) {
py::module mod("_orientation", "Python wrapper for afw _orientation library");
PYBIND11_PLUGIN(orientation) {
py::module mod("orientation");

py::module::import("lsst.geom");

/* Module level */
py::class_<Orientation> cls(mod, "Orientation");
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/afw/cameraGeom/transformMap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ namespace lsst {
namespace afw {
namespace cameraGeom {

PYBIND11_PLUGIN(_transformMap) {
py::module mod("_transformMap");
PYBIND11_PLUGIN(transformMap) {
py::module mod("transformMap");

/* Module level */
py::class_<TransformMap, std::shared_ptr<TransformMap>> cls(mod, "TransformMap");
Expand Down
3 changes: 2 additions & 1 deletion python/lsst/afw/coord/SConscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## -*- python -*-
from lsst.sconsUtils import scripts
scripts.BasicSConscript.pybind11(['observatory',
'weather'])
'weather'],
addUnderscore=False)
4 changes: 2 additions & 2 deletions python/lsst/afw/coord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
#
import lsst.afw.geom

from ._observatory import *
from ._weather import *
from .observatory import *
from .weather import *
4 changes: 2 additions & 2 deletions python/lsst/afw/coord/observatory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace lsst {
namespace afw {
namespace coord {

PYBIND11_PLUGIN(_observatory) {
py::module mod("_observatory", "Python wrapper for afw _observatory library");
PYBIND11_PLUGIN(observatory) {
py::module mod("observatory");

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

Expand Down
4 changes: 2 additions & 2 deletions python/lsst/afw/coord/weather.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ namespace lsst {
namespace afw {
namespace coord {

PYBIND11_PLUGIN(_weather) {
py::module mod("_weather", "Python wrapper for afw _weather library");
PYBIND11_PLUGIN(weather) {
py::module mod("weather");

py::class_<lsst::afw::coord::Weather> cls(mod, "Weather");

Expand Down
7 changes: 4 additions & 3 deletions python/lsst/afw/detection/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ scripts.BasicSConscript.pybind11(['psf',
'footprint',
'footprintSet',
'threshold',
'peak',
'peak/peak',
'footprintCtrl',
'gaussianPsf',
'footprintMerge',
'heavyFootprint'])
'footprintMerge/footprintMerge',
'heavyFootprint'],
addUnderscore=False)
19 changes: 9 additions & 10 deletions python/lsst/afw/detection/detectionLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import lsst.afw.image
from lsst.afw.geom import Span

from ._psf import *
from ._footprint import *
from ._footprintSet import *
from ._threshold import *
from ._peak import *
from .psf import *
from .footprint import *
from .footprintSet import *
from .threshold import *
from .peak import *
from ._footprintCtrl import *
from ._gaussianPsf import *
from ._footprintMerge import *
from .peak import *
from .footprintCtrl import *
from .gaussianPsf import *
from .footprintMerge import *
from .footprintMerge import *
from ._heavyFootprint import *
from ._footprint import *
from .heavyFootprint import *
4 changes: 2 additions & 2 deletions python/lsst/afw/detection/footprint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ void declareMaskFromFootprintList(py::module &mod) {

} // end anonymous namespace

PYBIND11_PLUGIN(_footprint) {
py::module mod("_footprint", "Python wrapper for afw Footprint library");
PYBIND11_PLUGIN(footprint) {
py::module mod("footprint");

/* Footprint Constructors */
py::class_<Footprint, std::shared_ptr<Footprint>, daf::base::Citizen> clsFootprint(mod, "Footprint");
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/afw/detection/footprintCtrl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace lsst {
namespace afw {
namespace detection {

PYBIND11_PLUGIN(_footprintCtrl) {
py::module mod("_footprintCtrl", "Python wrapper for afw _footprintCtrl library");
PYBIND11_PLUGIN(footprintCtrl) {
py::module mod("footprintCtrl");

py::class_<FootprintControl> clsFootprintControl(mod, "FootprintControl");

Expand Down
46 changes: 0 additions & 46 deletions python/lsst/afw/detection/footprintMerge.py

This file was deleted.

2 changes: 2 additions & 0 deletions python/lsst/afw/detection/footprintMerge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .footprintMerge import *
from .footprintMergeContinued import *
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace lsst {
namespace afw {
namespace detection {

PYBIND11_PLUGIN(_footprintMerge) {
py::module mod("_footprintMerge", "Python wrapper for afw _footprintMerge library");
PYBIND11_PLUGIN(footprintMerge) {
py::module mod("footprintMerge");

py::class_<FootprintMergeList> clsFootprintMergeList(mod, "FootprintMergeList");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
__all__ = [] # only imported for side effects

from lsst.utils import continueClass
from .footprintMerge import FootprintMergeList


@continueClass # noqa F811
class FootprintMergeList:
def getMergedSourceCatalog(self, catalogs, filters,
peakDist, schema, idFactory, samePeakDist):
"""Add multiple catalogs and get the SourceCatalog with merged Footprints"""
import lsst.afw.table as afwTable

table = afwTable.SourceTable.make(schema, idFactory)
mergedList = afwTable.SourceCatalog(table)

# if peak is not an array, create an array the size of catalogs
try:
len(samePeakDist)
except TypeError:
samePeakDist = [samePeakDist] * len(catalogs)

try:
len(peakDist)
except TypeError:
peakDist = [peakDist] * len(catalogs)

if len(peakDist) != len(catalogs):
raise ValueError("Number of catalogs (%d) does not match length of peakDist (%d)"
% (len(catalogs), len(peakDist)))

if len(samePeakDist) != len(catalogs):
raise ValueError("Number of catalogs (%d) does not match length of samePeakDist (%d)"
% (len(catalogs), len(samePeakDist)))

if len(filters) != len(catalogs):
raise ValueError("Number of catalogs (%d) does not match number of filters (%d)"
% (len(catalogs), len(filters)))

self.clearCatalog()
for cat, filter, dist, sameDist in zip(catalogs, filters, peakDist, samePeakDist):
self.addCatalog(table, cat, filter, dist, True, sameDist)

self.getFinalSources(mergedList)
return mergedList
24 changes: 8 additions & 16 deletions python/lsst/afw/detection/footprintSet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace afw {
namespace detection {

namespace {

template <typename PixelT, typename PyClass>
void declareMakeHeavy(PyClass &cls) {
// cls.def("makeHeavy", [](FootprintSet & self, image::MaskedImage<PixelT, image::MaskPixel> const&
Expand Down Expand Up @@ -76,8 +77,10 @@ void declareTemplatedMembers(PyClass &cls) {
}
} // namespace

PYBIND11_PLUGIN(_footprintSet) {
py::module mod("_footprintSet", "Python wrapper for afw _footprintSet library");
PYBIND11_PLUGIN(footprintSet) {
py::module mod("footprintSet");

py::module::import("lsst.afw.detection.footprint");

py::class_<FootprintSet, std::shared_ptr<FootprintSet>, lsst::daf::base::Citizen> clsFootprintSet(
mod, "FootprintSet");
Expand All @@ -100,22 +103,11 @@ PYBIND11_PLUGIN(_footprintSet) {
"footprints2"_a, "includePeaks"_a);

clsFootprintSet.def("swap", &FootprintSet::swap);
// clsFootprintSet.def("swapFootprintList", &FootprintSet::swapFootprintList);
// The pybind11 wrapped getFootprints dereferences the shared pointer to the std::vector of Footprints
// held by the FootprintSet when returning to python to work around an STL caster issue. However this
// creates a problem for the setter that now expects a shared pointer, making the setter unable to
// operate on the output from the getter. Below defines two overrides for the setter function, one
// that can handle a shared pointer (in case somewhere else in the stack has one somehow) and one
// that will accept a standard vector of Footprints, which will be internally placed in a shared
// pointer to match the underlying C++ function signature
clsFootprintSet.def("setFootprints",
[](FootprintSet &self, std::shared_ptr<FootprintSet::FootprintList> footListPtr) {
self.setFootprints(footListPtr);
});
// setFootprints takes shared_ptr<FootprintList> and getFootprints returns it,
// but pybind11 can't handle that type, so use a custom getter and setter
clsFootprintSet.def("setFootprints", [](FootprintSet &self, FootprintSet::FootprintList footList) {
self.setFootprints(std::make_shared<FootprintSet::FootprintList>(footList));
self.setFootprints(std::make_shared<FootprintSet::FootprintList>(std::move(footList)));
});
// getFootprints returns shared_ptr<FootprintList>, but stl caster can't handle this
clsFootprintSet.def("getFootprints", [](FootprintSet &self) { return *(self.getFootprints()); });
clsFootprintSet.def("makeSources", &FootprintSet::makeSources);
clsFootprintSet.def("setRegion", &FootprintSet::setRegion);
Expand Down

0 comments on commit d766a4d

Please sign in to comment.