Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pybind11 bindings for core classes #15

Open
timdewhirst opened this issue Feb 13, 2022 · 13 comments
Open

add pybind11 bindings for core classes #15

timdewhirst opened this issue Feb 13, 2022 · 13 comments
Assignees
Labels
feature adding a new feature to the code

Comments

@timdewhirst
Copy link
Member

No description provided.

@timdewhirst timdewhirst added the feature adding a new feature to the code label Feb 13, 2022
@timdewhirst timdewhirst self-assigned this Feb 13, 2022
@timdewhirst
Copy link
Member Author

See discussion: OpenPIV/openpiv-python#224

@ErichZimmer
Copy link
Contributor

ErichZimmer commented Mar 25, 2022

Should we include the logging functions? It seems quite straight forward without the logging library as there seems to be only standard libraries. I haven't figured out how to install c++ dependencies reliably on different operating systems in setup.py to get a minimum working example.

@timdewhirst
Copy link
Member Author

No real reason to include spdlog if that's causing a problem, and its use is not widespread yet. If you already have some WIP I'd be more than happy to take a look - perhaps raise a PR to a new branch here?

@ErichZimmer
Copy link
Contributor

Please expect some time for a wrapper to be started and or completed since I started learning c++ only 4 weeks ago. However, I'll see how far I can get since I already wrapped a gaussian filter as an experiment.

@ErichZimmer
Copy link
Contributor

ErichZimmer commented Apr 2, 2022

For some reason, setup.py doesn't know how to deal with the header files. I'm trying to wrap a modified version of the processing example that has a declaration like;

void correlate_images(
    py::array_t<float> cmatrix,
    py::array_t<float> imgA,
    py::array_t<float> imgB,
    py::tuple window_size,
    py::tuple overlap,
    py::bool_ center_grid_on_image
);

The function can be modified to return a numpy array

@ErichZimmer
Copy link
Contributor

Should we include vcpkg or use setuptools to compile the package?

@timdewhirst
Copy link
Member Author

I was planning on trying vcpkg (for pybind11) and cmake (for compilation) - I have some time over Easter weekend so was planning on sketching this out to make sure it works.

timdewhirst added a commit that referenced this issue May 26, 2022
timdewhirst added a commit that referenced this issue May 31, 2022
timdewhirst added a commit that referenced this issue Jun 2, 2022
timdewhirst added a commit that referenced this issue Jun 4, 2022
timdewhirst added a commit that referenced this issue Jun 8, 2022
timdewhirst added a commit that referenced this issue Jul 29, 2022
timdewhirst added a commit that referenced this issue Oct 5, 2022
timdewhirst added a commit that referenced this issue Oct 5, 2022
@ErichZimmer
Copy link
Contributor

Currently, the wrapped functions and classes under pycoreopenpiv are not really usable as they have non-native data type returns (For instance, core::image::data() returns a pixel type that cannot be casted into a numpy array and has limited use). When I have time, should I play around with your wrapper to make it more python friendly? I have future plans on utilizing this repository more often so I can add more features while learning more about c++ and pybind11 (e.g., I want to add morphological and convolution filters and make the wrapper more user friendly).

@timdewhirst
Copy link
Member Author

Agreed! it's on my list of things to do - basically python has the concept of a buffer protocol which is what we need to use to allow seamless interchange from numpy to the core image type: https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html

Any/all contributions welcome; the aim is - as the title suggests - to provide python bindings to the core classes and also to allow interoperability with other more commonly used libraries such as numpy, pandas.

@ErichZimmer
Copy link
Contributor

I tried compiling your Python bindings for libopenpiv, but I get a lengthy compiler error, presumably due to pybind11 not handling type-deductions properly for one reason or another.

meson setup log [download]

meson compile cmd log [download]

@ErichZimmer
Copy link
Contributor

@timdewhirst
Copy link
Member Author

thanks, will take a look

@timdewhirst
Copy link
Member Author

timdewhirst commented Nov 25, 2023

I took a look and apart from a couple of warnings (which are now fixed) the code compiles and works fine:

  • visual studio 2022
  • cmake 3.25.1
  • python 3.11.6
  • windows 10 x86_64

Of the two install routes covered in README.md, using setup.py doesn't appear to work on windows, but manually building using cmake is fine:

  • setup a venv & activate
  • pip install pybind11 (and numpy, matplotlib if you want to do a simple test)
  • in openpiv-c--qt:
    • cmake -B build -S . -DBUILD_PYBIND=ON -DCMAKE_PREFIX_PATH="c:\path\to\openpiv-venv\Lib\site-packages"
    • cmake --build build
  • cd build/out/Debug
  • in python: import pyopenpivcore as popc - this should work
import pyopenpivcore as popc
import numpy as np
from matplotlib import pyplot as plt

ima = popc.load_g_u16('../../../examples/data/test1/exp1_001_a.tiff')
ima_np = np.array(ima, copy=False)
plt.imshow(ima_np, cmap='gray')
plt.show()

This shows a few things:

  • the module loads!
  • we can load an image - there is a simple wrapper to load data into a greyscale u16 image
  • we then create a numpy array with no copying of data - this is using the python protocol system!
  • we display the image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature adding a new feature to the code
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants