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

Interface with the extension type from cython #24

Open
jorisvandenbossche opened this issue Aug 18, 2019 · 4 comments
Open

Interface with the extension type from cython #24

jorisvandenbossche opened this issue Aug 18, 2019 · 4 comments
Labels
refactor Restructure code without changing function to-shapely This issue needs to be transferred to shapely

Comments

@jorisvandenbossche
Copy link
Member

As an experiment, I was trying to implement something in cython using the extension type / object array holding them (a simple vectorized contains; not to replace the contains ufunc, but as simple example of how to use cython with an array of pygeos objects, for potentially a custom non-ufunc algorithm).

So I succeeded in interfacing with the GeometryObject struct (to get the pointer out of the struct), without Python interaction.

But what I didn't get to work yet is to access a single pygeos.Geometry object out of the object array (which needs to happen in the loop).
And the result of this is that I also can't put this for loop in a nogil block, which would be needed to run it in a parallel context.

See experiment here: https://nbviewer.jupyter.org/gist/jorisvandenbossche/c90181a330972d5f4564d7c73b70a114

@caspervdw
Copy link
Member

Great that you got it to work with Cython!

I think the numpy-accessing bit is an issue unrelated to PyGEOS. Maybe you need to type the index correctly?

@jorisvandenbossche
Copy link
Member Author

It's not the index I think (that one is typed, and you can also see it in the output of the annotated code). As far as I can see, the problem is that you get a pygeos.Geometry object out of the array, and cast that to a GeometryObject. That casting to a GeometryObject is fine, as that is something that cython knows about, but getting the object out as pygeos.Geometry is the problem.

The annotated code shows (the first line is the cython line, the lines below that the C code that is generated from it):

+56:         pygeom2 = <GeometryObject *> array[idx]

    __pyx_t_10 = __pyx_v_idx;
    __pyx_t_6 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_array.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_array.diminfo[0].strides);
    __Pyx_INCREF((PyObject*)__pyx_t_6);
    __pyx_v_pygeom2 = ((GeometryObject *)__pyx_t_6);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;

So to cast the array[idx] element to a GeometryObject and assign it to pygeom2 variable, cython creates a temporary PyObject.
And I am not sure how to tell cython (or whether this is possible) that the object array holds all GeometryObjects, so that it can avoid this temporary python object.

@caspervdw
Copy link
Member

Right, would this mean that Cython casts any object from an object-typed bumpy array?

Btw the GeometryObject is the C symbol I use for an instance of pygeos.Geometry.

@caspervdw
Copy link
Member

This issue is probably the core problem of #226 .

@caspervdw caspervdw added refactor Restructure code without changing function to-shapely This issue needs to be transferred to shapely labels Dec 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Restructure code without changing function to-shapely This issue needs to be transferred to shapely
Projects
None yet
Development

No branches or pull requests

2 participants