Skip to content

How to create a numpy array of objects? #647

@mwoehlke-kitware

Description

@mwoehlke-kitware

(Is there a mailing list or better place for questions? I couldn't find one...)

I'm trying to wrap an API that returns Eigen vectors/matrices of stuff like "symbols" (for symbolic algebra). I think I have wrapping of the actual value types working, but trying to convert them to numpy objects is harder, because they aren't POD data; basically I need a numpy array of objects.

I think I have a partly working solution, adapted from hacking together bits of eigen.h, numpy.h and stl.h:

    // in type_caster<MyType>::cast
    array a(
        npy_format_descriptor<Scalar>::dtype(), // type
        { (size_t) src.size() }                 // shape
    );
    for (size_t i = 0; i < src.size(); ++i) {
        auto value_ = reinterpret_steal<object>(value_conv::cast(src(i), policy, parent));
        if (!value_)
            return handle();
        auto p = PyArray_GETPTR1(a.ptr(), i);
        PyArray_SETITEM(a.ptr(), p, value_.release().ptr()); // steals a reference
    }
    return a.release();

...however, I don't understand the way that the numpy C API is called. Apparently there is some dynamic function loading magic such that no numpy headers are ever actually included, but the "functions" I need to call for this to work (PyArray_GETPTR1, PyArray_SETITEM) are actually macros. What's the right way in pybind11 to call these? Is there some other mechanism I don't know to fill the items in the newly created numpy array? Or is there some other approach than creating the numpy array and then filling it?

For performance reasons, and because this needs to work with matrices also, I really want to go directly to numpy without an intermediate Python list.

(Note: the dtype is PyArray_DescrFromType_(NPY_OBJECT_), which I believe is correct? Also, this is the first time I've ever tried to work with numpy; please go easy on my lack of knowledge / experience.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions