diff options
author | Jon Nordby <jononor@gmail.com> | 2025-04-27 21:38:29 +0200 |
---|---|---|
committer | Jon Nordby <jononor@gmail.com> | 2025-05-02 21:00:12 +0200 |
commit | be9f6b6e8e39a711dcfa7806f894b8ec3ff49ca9 (patch) | |
tree | 9b8a0c16ac47a7084b52bdf458f5d5a683b001c0 | |
parent | 594bdb32b9d0648ab4718b3672f54008c46ee3a6 (diff) |
bindings: Example of using pybind11 with numpy
-rw-r--r-- | bindings/.gitignore | 1 | ||||
-rw-r--r-- | bindings/README.md | 28 | ||||
-rw-r--r-- | bindings/galearn_pdm.cpp | 29 | ||||
-rw-r--r-- | bindings/setup.py | 22 | ||||
-rw-r--r-- | bindings/test_galearn_pdm.py | 16 |
5 files changed, 96 insertions, 0 deletions
diff --git a/bindings/.gitignore b/bindings/.gitignore new file mode 100644 index 0000000..140f8cf --- /dev/null +++ b/bindings/.gitignore @@ -0,0 +1 @@ +*.so diff --git a/bindings/README.md b/bindings/README.md new file mode 100644 index 0000000..dee8dfc --- /dev/null +++ b/bindings/README.md @@ -0,0 +1,28 @@ + +# Python module + +## Prerequisites + +The following packages are needed. +Install with pip or via OS package manager. + + pybind11 + numpy + setuptools + + +## Building + +To build the module + + python setup.py build_ext --inplace + +This will produce a shared library (`.so` on Linux) in PWD, that Python can load. + +## Test + +To use the module, see the .py file + + python test_galearn_pdm.py + + diff --git a/bindings/galearn_pdm.cpp b/bindings/galearn_pdm.cpp new file mode 100644 index 0000000..9274a01 --- /dev/null +++ b/bindings/galearn_pdm.cpp @@ -0,0 +1,29 @@ +#include <pybind11/pybind11.h> +#include <pybind11/numpy.h> + +namespace py = pybind11; + +void process(py::array_t<uint8_t> arr1, py::array_t<int16_t> arr2) { + // Check shapes or sizes if needed + auto buf1 = arr1.request(); + auto buf2 = arr2.request(); + + if (buf1.ndim != 1 || buf2.ndim != 1) { + throw std::runtime_error("Only 1D arrays supported"); + } + + if (buf1.size < buf2.size) { + throw std::runtime_error("Input 1 must be same or larger than input 2"); + } + + // Example: access data + uint8_t* in = static_cast<uint8_t*>(buf1.ptr); + int16_t* out = static_cast<int16_t*>(buf2.ptr); + for (int i=0; i<buf2.size; i++) { + out[i] = in[i] + 1; + } +} + +PYBIND11_MODULE(galearn_pdm, m) { + m.def("process", &process, "Process two numpy arrays (uint8 and int16)"); +} diff --git a/bindings/setup.py b/bindings/setup.py new file mode 100644 index 0000000..487949a --- /dev/null +++ b/bindings/setup.py @@ -0,0 +1,22 @@ +from setuptools import setup, Extension +import pybind11 +import numpy + +ext_modules = [ + Extension( + 'galearn_pdm', + ['galearn_pdm.cpp'], + include_dirs=[ + pybind11.get_include(), + numpy.get_include(), + ], + language='c++', + ), +] + +setup( + name='galearn', + version='0.0.1', + ext_modules=ext_modules, + zip_safe=False, +) diff --git a/bindings/test_galearn_pdm.py b/bindings/test_galearn_pdm.py new file mode 100644 index 0000000..43cb58e --- /dev/null +++ b/bindings/test_galearn_pdm.py @@ -0,0 +1,16 @@ + +import galearn_pdm +import numpy + +def test_one(): + + inp = numpy.ones(shape=10, dtype=numpy.uint8) + out = numpy.zeros(shape=10, dtype=numpy.int16) + + galearn_pdm.process(inp, out) + print(out) + assert out[0] == 2 + assert out[-1] == 2 + +if __name__ == '__main__': + test_one() |