Skip to content

Commit

Permalink
Merge pull request #306 from lsst/tickets/DM-37791
Browse files Browse the repository at this point in the history
DM-37791: use pybind wrappers, merge into one pybind shared lib
  • Loading branch information
mwittgen committed Feb 11, 2023
2 parents 583670f + 1e2bed6 commit 592df81
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 248 deletions.
28 changes: 15 additions & 13 deletions python/lsst/meas/algorithms/SConscript
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# -*- python -*-
from lsst.sconsUtils import scripts
scripts.BasicSConscript.pybind11(["cr",
"coaddBoundedField",
"coaddPsf/coaddPsf",
"coaddTransmissionCurve",
"doubleGaussianPsf",
"imagePsf",
"interp",
"kernelPsf",
"pcaPsf",
"psfCandidate/psfCandidate",
"singleGaussianPsf",
"spatialModelPsf",
"warpedPsf"], addUnderscore=False)

scripts.BasicSConscript.python(['_algorithmsLib'], [
'_algorithmsLib.cc',
'cr.cc',
'coaddBoundedField.cc',
'coaddPsf/coaddPsf.cc',
'coaddTransmissionCurve.cc',
'doubleGaussianPsf.cc',
'imagePsf.cc',
'interp.cc',
'kernelPsf.cc',
'pcaPsf.cc',
'psfCandidate/psfCandidate.cc',
'singleGaussianPsf.cc',
'spatialModelPsf.cc',
'warpedPsf.cc'])
12 changes: 1 addition & 11 deletions python/lsst/meas/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@
import lsst.afw.image
import lsst.afw.math

from .cr import *
from .coaddBoundedField import *
from .imagePsf import *
from .interp import *
from .kernelPsf import *
from .pcaPsf import *
from._algorithmsLib import *
from .psfCandidate import * #python
from .singleGaussianPsf import *
from .spatialModelPsf import *
from .warpedPsf import *
from .coaddPsf import *
from .coaddTransmissionCurve import *
from .doubleGaussianPsf import *
from .simple_curve import *

from .psfDeterminer import *
Expand Down
73 changes: 73 additions & 0 deletions python/lsst/meas/algorithms/_algorithmsLib.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of meas_algorithms.
*
* Developed for the LSST Data Management System.
* This product includes software developed by the LSST Project
* (https://www.lsst.org).
* See the COPYRIGHT file at the top-level directory of this distribution
* for details of code ownership.
*
* 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 GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"

namespace py = pybind11;
using namespace pybind11::literals;
using lsst::cpputils::python::WrapperCollection;

namespace lsst {
namespace meas {
namespace algorithms {
void wrapCoaddTransmissionCurve(WrapperCollection &wrappers);
void wrapSingleGaussianPsf(WrapperCollection &wrappers);
void wrapKernelPsf(WrapperCollection &wrappers);
void wrapDoubleGaussianPsf(WrapperCollection &wrappers);
void wrapInterp(WrapperCollection &wrappers);
void wrapPcaPsf(WrapperCollection &wrappers);
void wrapWarpedPsf(WrapperCollection &wrappers);
void wapPsfCandidate(WrapperCollection &wrappers);
void wrapImagePsf(WrapperCollection &wrappers);
void wrapSpatialModelPsf(WrapperCollection &wrappers);
void wrapCoaddPsf(WrapperCollection &wrappers);
void wrapCoaddBoundedField(WrapperCollection &wrappers);
void wrapCr(WrapperCollection &wrappers);

PYBIND11_MODULE(_algorithmsLib, mod) {
WrapperCollection wrappers(mod, "lsst.meas.algorithms");
wrappers.addInheritanceDependency("lsst.afw.geom");
wrappers.addInheritanceDependency("lsst.afw.image");
wrappers.addInheritanceDependency("lsst.afw.table");
wrappers.addInheritanceDependency("lsst.afw.detection");

wrapImagePsf(wrappers);
wrapKernelPsf(wrappers);
wrapCoaddTransmissionCurve(wrappers);
wrapSingleGaussianPsf(wrappers);
wrapDoubleGaussianPsf(wrappers);
wrapInterp(wrappers);
wrapPcaPsf(wrappers);
wrapWarpedPsf(wrappers);
wapPsfCandidate(wrappers);
wrapSpatialModelPsf(wrappers);
wrapCoaddPsf(wrappers);
wrapCoaddBoundedField(wrappers);
wrapCr(wrappers);
wrappers.finish();
}

} // algorithms
} // meas
} // lsst
87 changes: 45 additions & 42 deletions python/lsst/meas/algorithms/coaddBoundedField.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* see <https://www.lsstcorp.org/LegalNotices/>.
*/
#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"
#include "pybind11/stl.h"

#include "lsst/geom/Box.h"
Expand All @@ -34,56 +35,58 @@ namespace meas {
namespace algorithms {
namespace {

PYBIND11_MODULE(coaddBoundedField, mod) {
py::class_<CoaddBoundedFieldElement> clsCoaddBoundedFieldElement(mod, "CoaddBoundedFieldElement");
void declareCoaddBoundedField(lsst::cpputils::python::WrapperCollection &wrappers) {
using PyElement = py::class_<CoaddBoundedFieldElement>;
static auto clsElement = wrappers.wrapType(PyElement(wrappers.module, "CoaddBoundedFieldElement"), [](auto &mod, auto &cls) {
cls.def(py::init([](std::shared_ptr<afw::math::BoundedField> field,
std::shared_ptr<afw::geom::SkyWcs const> wcs, py::object polygon, double weight) {
if (polygon.is(py::none())) {
return new CoaddBoundedFieldElement(field, wcs, nullptr, weight);
} else {
auto pgon = py::cast<std::shared_ptr<afw::geom::polygon::Polygon const>>(polygon);
return new CoaddBoundedFieldElement(field, wcs, pgon, weight);
}
}),
"field"_a, "wcs"_a, "validPolygon"_a, "weight"_a = 1.0);
cls.def_readwrite("field", &CoaddBoundedFieldElement::field);
cls.def_readwrite("wcs", &CoaddBoundedFieldElement::wcs);
cls.def_readwrite("validPolygon", &CoaddBoundedFieldElement::validPolygon);
cls.def_readwrite("weight", &CoaddBoundedFieldElement::weight);

clsCoaddBoundedFieldElement.def(
py::init([](std::shared_ptr<afw::math::BoundedField> field,
std::shared_ptr<afw::geom::SkyWcs const> wcs, py::object polygon, double weight) {
if (polygon.is(py::none())) {
return new CoaddBoundedFieldElement(field, wcs, nullptr, weight);
} else {
auto pgon = py::cast<std::shared_ptr<afw::geom::polygon::Polygon const>>(polygon);
return new CoaddBoundedFieldElement(field, wcs, pgon, weight);
}
}),
"field"_a, "wcs"_a, "validPolygon"_a, "weight"_a = 1.0);
cls.def("__eq__", &CoaddBoundedFieldElement::operator==, py::is_operator());
cls.def("__ne__", &CoaddBoundedFieldElement::operator!=, py::is_operator());
});
using PyClass = py::class_<CoaddBoundedField, std::shared_ptr<CoaddBoundedField>, afw::math::BoundedField>;
auto clsField = wrappers.wrapType(PyClass(wrappers.module, "CoaddBoundedField"), [](auto &mod, auto &cls) {

clsCoaddBoundedFieldElement.def_readwrite("field", &CoaddBoundedFieldElement::field);
clsCoaddBoundedFieldElement.def_readwrite("wcs", &CoaddBoundedFieldElement::wcs);
clsCoaddBoundedFieldElement.def_readwrite("validPolygon", &CoaddBoundedFieldElement::validPolygon);
clsCoaddBoundedFieldElement.def_readwrite("weight", &CoaddBoundedFieldElement::weight);
cls.attr("Element") = clsElement;

clsCoaddBoundedFieldElement.def("__eq__", &CoaddBoundedFieldElement::operator==, py::is_operator());
clsCoaddBoundedFieldElement.def("__ne__", &CoaddBoundedFieldElement::operator!=, py::is_operator());
/* Constructors */
cls.def(py::init<geom::Box2I const &, std::shared_ptr<afw::geom::SkyWcs const>,
typename CoaddBoundedField::ElementVector const &>(),
"bbox"_a, "coaddWcs"_a, "elements"_a);
cls.def(py::init<geom::Box2I const &, std::shared_ptr<afw::geom::SkyWcs const>,
typename CoaddBoundedField::ElementVector const &, double>(),
"bbox"_a, "coaddWcs"_a, "elements"_a, "default"_a);

py::class_<CoaddBoundedField, std::shared_ptr<CoaddBoundedField>, afw::math::BoundedField>
clsCoaddBoundedField(mod, "CoaddBoundedField");
afw::table::io::python::addPersistableMethods<CoaddBoundedField>(clsCoaddBoundedField);
/* Operators */
cls.def("__eq__", &CoaddBoundedField::operator==, py::is_operator());
cls.def("__ne__", &CoaddBoundedField::operator!=, py::is_operator());
cls.def("__imul__", &CoaddBoundedField::operator*);

clsCoaddBoundedField.attr("Element") = clsCoaddBoundedFieldElement;

/* Constructors */
clsCoaddBoundedField.def(py::init<geom::Box2I const &, std::shared_ptr<afw::geom::SkyWcs const>,
typename CoaddBoundedField::ElementVector const &>(),
"bbox"_a, "coaddWcs"_a, "elements"_a);
clsCoaddBoundedField.def(py::init<geom::Box2I const &, std::shared_ptr<afw::geom::SkyWcs const>,
typename CoaddBoundedField::ElementVector const &, double>(),
"bbox"_a, "coaddWcs"_a, "elements"_a, "default"_a);

/* Operators */
clsCoaddBoundedField.def("__eq__", &CoaddBoundedField::operator==, py::is_operator());
clsCoaddBoundedField.def("__ne__", &CoaddBoundedField::operator!=, py::is_operator());
clsCoaddBoundedField.def("__imul__", &CoaddBoundedField::operator*);

/* Members */
clsCoaddBoundedField.def("evaluate", &CoaddBoundedField::evaluate);
clsCoaddBoundedField.def("getCoaddWcs", &CoaddBoundedField::getCoaddWcs);
clsCoaddBoundedField.def("getDefault", &CoaddBoundedField::getDefault);
clsCoaddBoundedField.def("getElements", &CoaddBoundedField::getElements);
/* Members */
cls.def("evaluate", &CoaddBoundedField::evaluate);
cls.def("getCoaddWcs", &CoaddBoundedField::getCoaddWcs);
cls.def("getDefault", &CoaddBoundedField::getDefault);
cls.def("getElements", &CoaddBoundedField::getElements);
});
afw::table::io::python::addPersistableMethods<CoaddBoundedField>(clsField);
}

} // namespace
void wrapCoaddBoundedField(lsst::cpputils::python::WrapperCollection &wrappers) {
declareCoaddBoundedField(wrappers);
}
} // namespace algorithms
} // namespace meas
} // namespace lsst
2 changes: 1 addition & 1 deletion python/lsst/meas/algorithms/coaddPsf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
# see <https://www.lsstcorp.org/LegalNotices/>.
#

from .coaddPsf import *
from .._algorithmsLib import CoaddPsfControl, CoaddPsf
from .coaddPsfContinued import *
73 changes: 42 additions & 31 deletions python/lsst/meas/algorithms/coaddPsf/coaddPsf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* see <https://www.lsstcorp.org/LegalNotices/>.
*/
#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"

#include "lsst/afw/table/io/python.h"
#include "lsst/meas/algorithms/CoaddPsf.h"
Expand All @@ -33,45 +34,55 @@ namespace meas {
namespace algorithms {
namespace {

PYBIND11_MODULE(coaddPsf, mod) {
void declareCoaddPsf(lsst::cpputils::python::WrapperCollection &wrappers) {
/* CoaddPsfControl */
py::class_<CoaddPsfControl, std::shared_ptr<CoaddPsfControl>> clsControl(mod, "CoaddPsfControl");
clsControl.def(py::init<std::string, int>(), "warpingKernelName"_a = "lanczos3", "cacheSize"_a = 10000);
LSST_DECLARE_CONTROL_FIELD(clsControl, CoaddPsfControl, warpingKernelName);
LSST_DECLARE_CONTROL_FIELD(clsControl, CoaddPsfControl, cacheSize);
using PyCoaddPsfControl = py::class_<CoaddPsfControl, std::shared_ptr<CoaddPsfControl>>;

wrappers.wrapType(PyCoaddPsfControl(wrappers.module, "CoaddPsfControl"), [](auto &mod, auto &cls) {
cls.def(py::init<std::string, int>(), "warpingKernelName"_a = "lanczos3", "cacheSize"_a = 10000);
LSST_DECLARE_CONTROL_FIELD(cls, CoaddPsfControl, warpingKernelName);
LSST_DECLARE_CONTROL_FIELD(cls, CoaddPsfControl, cacheSize);
});

/* CoaddPsf */
py::class_<CoaddPsf, std::shared_ptr<CoaddPsf>, ImagePsf> clsCoaddPsf(mod, "CoaddPsf");
afw::table::io::python::addPersistableMethods<CoaddPsf>(clsCoaddPsf);
using PyCoaddPsf = py::class_<CoaddPsf, std::shared_ptr<CoaddPsf>, ImagePsf>;

auto clsCoaddPsf = wrappers.wrapType(PyCoaddPsf(wrappers.module, "CoaddPsf"), [](auto &mod, auto &cls) {
/* Constructors */
cls.def(py::init<afw::table::ExposureCatalog const &, afw::geom::SkyWcs const &,
std::string const &, std::string const &, int>(),
"catalog"_a, "coaddWcs"_a, "weightFieldName"_a = "weight",
"warpingKernelName"_a = "lanczos3", "cacheSize"_a = 10000);
cls.def(py::init<afw::table::ExposureCatalog const &, afw::geom::SkyWcs const &,
geom::Point2D const &, std::string const &, int>(),
"catalog"_a, "coaddWcs"_a, "averagePosition"_a,
"warpingKernelName"_a = "lanczos3", "cacheSize"_a = 10000);
cls.def(py::init<afw::table::ExposureCatalog const &, afw::geom::SkyWcs const &,
CoaddPsfControl const &, std::string const &>(),
"catalog"_a, "coaddWcs"_a, "ctrl"_a, "weightFieldName"_a = "weight");

/* Constructors */
clsCoaddPsf.def(py::init<afw::table::ExposureCatalog const &, afw::geom::SkyWcs const &,
std::string const &, std::string const &, int>(),
"catalog"_a, "coaddWcs"_a, "weightFieldName"_a = "weight",
"warpingKernelName"_a = "lanczos3", "cacheSize"_a = 10000);
clsCoaddPsf.def(py::init<afw::table::ExposureCatalog const &, afw::geom::SkyWcs const &,
geom::Point2D const &, std::string const &, int>(),
"catalog"_a, "coaddWcs"_a, "averagePosition"_a,
"warpingKernelName"_a = "lanczos3", "cacheSize"_a = 10000);
clsCoaddPsf.def(py::init<afw::table::ExposureCatalog const &, afw::geom::SkyWcs const &,
CoaddPsfControl const &, std::string const &>(),
"catalog"_a, "coaddWcs"_a, "ctrl"_a, "weightFieldName"_a = "weight");
/* Members */
cls.def("clone", &CoaddPsf::clone);
cls.def("getAveragePosition", &CoaddPsf::getAveragePosition);
cls.def("getCoaddWcs", &CoaddPsf::getCoaddWcs);
cls.def("getComponentCount", &CoaddPsf::getComponentCount);
cls.def("getPsf", &CoaddPsf::getPsf);
cls.def("getWcs", &CoaddPsf::getWcs);
cls.def("getWeight", &CoaddPsf::getWeight);
cls.def("getId", &CoaddPsf::getId);
cls.def("getBBox", &CoaddPsf::getBBox);
cls.def("getValidPolygon", &CoaddPsf::getValidPolygon);
cls.def("isPersistable", &CoaddPsf::isPersistable);
});

/* Members */
clsCoaddPsf.def("clone", &CoaddPsf::clone);
clsCoaddPsf.def("getAveragePosition", &CoaddPsf::getAveragePosition);
clsCoaddPsf.def("getCoaddWcs", &CoaddPsf::getCoaddWcs);
clsCoaddPsf.def("getComponentCount", &CoaddPsf::getComponentCount);
clsCoaddPsf.def("getPsf", &CoaddPsf::getPsf);
clsCoaddPsf.def("getWcs", &CoaddPsf::getWcs);
clsCoaddPsf.def("getWeight", &CoaddPsf::getWeight);
clsCoaddPsf.def("getId", &CoaddPsf::getId);
clsCoaddPsf.def("getBBox", &CoaddPsf::getBBox);
clsCoaddPsf.def("getValidPolygon", &CoaddPsf::getValidPolygon);
clsCoaddPsf.def("isPersistable", &CoaddPsf::isPersistable);
afw::table::io::python::addPersistableMethods<CoaddPsf>(clsCoaddPsf);
}

} // namespace
void wrapCoaddPsf(lsst::cpputils::python::WrapperCollection &wrappers)
{
declareCoaddPsf(wrappers);
}
} // namespace algorithms
} // namespace meas
} // namespace lsst
2 changes: 1 addition & 1 deletion python/lsst/meas/algorithms/coaddPsf/coaddPsfContinued.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

__all__ = ["CoaddPsfConfig"]

from .coaddPsf import CoaddPsfControl
from .._algorithmsLib import CoaddPsfControl
from lsst.pex.config import makeConfigClass

CoaddPsfConfig = makeConfigClass(CoaddPsfControl, "CoaddPsfConfig")
10 changes: 3 additions & 7 deletions python/lsst/meas/algorithms/coaddTransmissionCurve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* see <https://www.lsstcorp.org/LegalNotices/>.
*/
#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"

#include "lsst/meas/algorithms/CoaddTransmissionCurve.h"

Expand All @@ -30,16 +31,11 @@ using namespace pybind11::literals;
namespace lsst {
namespace meas {
namespace algorithms {
namespace {

PYBIND11_MODULE(coaddTransmissionCurve, mod) {
py::module::import("lsst.afw.geom");
py::module::import("lsst.afw.image");
py::module::import("lsst.afw.table");
mod.def("makeCoaddTransmissionCurve", &makeCoaddTransmissionCurve, "coaddWcs"_a, "inputSensors"_a);
void wrapCoaddTransmissionCurve(lsst::cpputils::python::WrapperCollection &wrappers) {
wrappers.module.def("makeCoaddTransmissionCurve", &makeCoaddTransmissionCurve, "coaddWcs"_a, "inputSensors"_a);
}

} // namespace
} // namespace algorithms
} // namespace meas
} // namespace lsst

0 comments on commit 592df81

Please sign in to comment.