-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splitting bindings into multiple files.
Proposal on how to split Pybind bindings into multiple files for readability. Implemented on the example of a PSF class. The code currently doesn't work because we can not have multiple entry point calls to the macro PYBIND11_MODULE in a single codebase. Or at least I don't think we can and nothing I tried worked. We certainly can not extend a once-declared module by repeatedly calling PYBIND11_MODULE, but seemingly we can not define a new one either.
- Loading branch information
1 parent
16f58e4
commit b1ac016
Showing
7 changed files
with
136 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#ifndef PSF_BINDINGS | ||
#define PSF_BINDINGS | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/numpy.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include "PointSpreadFunc.h" | ||
|
||
|
||
namespace py = pybind11; | ||
|
||
|
||
namespace image_base_bindings { | ||
|
||
static const auto DOC_PSF = R"doc( | ||
Point Spread Function. | ||
Parameters | ||
---------- | ||
stdev : `float`, optional | ||
Standard deviation of the Gaussian PSF. | ||
psf : `PointSpreadFunc`, optional | ||
Another PSF object. | ||
arr : `numpy.array`, optional | ||
A realization of the PSF. | ||
Notes | ||
----- | ||
When instantiated with another `psf` object, returns its copy. | ||
When instantiated with an array-like object, that array must be | ||
a square matrix and have an odd number of dimensions. Only one | ||
of the arguments is required. | ||
)doc"; | ||
|
||
|
||
static const auto DOC_PSF_set_array = R"doc( | ||
Set the kernel values of a realized PSF. | ||
Parameters | ||
---------- | ||
arr : `numpy.array` | ||
A realization of the PSF. | ||
Notes | ||
----- | ||
Given realization of a PSF has to be an odd-dimensional square | ||
matrix. | ||
)doc"; | ||
|
||
static void psf_bindings_factory(py::module &m) { | ||
|
||
using psf = image_base::PointSpreadFunc; | ||
|
||
py::class_<psf>(m, "PSF", py::buffer_protocol(), DOC_PSF) | ||
.def_buffer([](psf &m) -> py::buffer_info { | ||
return py::buffer_info(m.data(), sizeof(float), py::format_descriptor<float>::format(), | ||
2, {m.get_dim(), m.get_dim()}, | ||
{sizeof(float) * m.get_dim(), sizeof(float)}); | ||
}) | ||
.def(py::init<float>()) | ||
.def(py::init<py::array_t<float>>()) | ||
.def(py::init<psf &>()) | ||
.def("set_array", &psf::set_array, DOC_PSF_set_array) | ||
.def("get_stdev", &psf::get_stdev, "Returns the PSF's standard deviation.") | ||
.def("get_sum", &psf::get_sum, "Returns the sum of PSFs kernel elements.") | ||
.def("get_dim", &psf::get_dim, "Returns the PSF kernel dimensions.") | ||
.def("get_radius", &psf::get_radius, "Returns the radius of the PSF") | ||
.def("get_size", &psf::get_size, "Returns the number of elements in the PSFs kernel.") | ||
.def("get_kernel", &psf::get_kernel, "Returns the PSF kernel.") | ||
.def("get_value", &psf::get_value, "Returns the PSF kernel value at a specific point.") | ||
.def("square_psf", &psf::square_psf, | ||
"Squares, raises to the power of two, the elements of the PSF kernel.") | ||
.def("print_psf", &psf::print, "Pretty-prints the PSF."); | ||
} | ||
|
||
} /* namesapce image_base_bindings */ | ||
|
||
#endif /* PSF_BINDINGS */ |