Accessing numpy array data in C (for numpy 1.7+)
NickName:El Sampsa Ask DateTime:2016-09-14T14:33:28

Accessing numpy array data in C (for numpy 1.7+)

Following examples and the numpy C-API (http://docs.scipy.org/doc/numpy/reference/c-api.html), I'm trying to access numpy array data in cpp, like this:

#include <Python.h>
#include <frameobject.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION // TOGGLE OR NOT
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
...
// here I have passed "some_python_object" to the C code
// .. and "some_python_object" has member "infobuf" that is a numpy array
//
unsigned long* fInfoBuffer;
PyObject* infobuffer = PyObject_GetAttrString(some_python_object, "infobuf");
PyObject* x_array    = PyArray_FROM_OT(infobuffer, NPY_UINT32);
fInfoBuffer          = (unsigned long*)PyArray_DATA(x_array); // DOES NOT WORK WHEN API DEPRECATION IS TOGGLED

When the API deprecation is toggled, I get, when compiling:

error: cannot convert ‘PyObject* {aka _object*}’ to ‘PyArrayObject* {aka tagPyArrayObject*}’ for argument ‘1’ to ‘void* PyArray_DATA(PyArrayObject*)’

What would be the legitimate way of doing this in numpy 1.7+ ?

Copyright Notice:Content Author:「El Sampsa」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39483862/accessing-numpy-array-data-in-c-for-numpy-1-7

Answers
Quant 2017-04-12T00:30:44

You could try using a higher-level library that wraps numpy arrays in C++ containers with proper container semantics.\n\nTry out xtensor and the xtensor-python bindings.\n\n\nNumpy to xtensor cheat sheet http://xtensor.readthedocs.io/en/latest/numpy.html\nThe xtensor-python project\nhttp://xtensor-python.readthedocs.io/en/latest/\n\n\nThere is also a cookiecutter to generate a minimal C++ extension project with all the boilerplate for testing, html documentation and setup.p...\n\nExample: C++ code\n\n#include <numeric> // Standard library import for std::accumulate\n#include \"pybind11/pybind11.h\" // Pybind11 import to define Python bindings\n#include \"xtensor/xmath.hpp\" // xtensor import for the C++ universal functions\n#define FORCE_IMPORT_ARRAY // numpy C api loading\n#include \"xtensor-python/pyarray.hpp\" // Numpy bindings\n\ndouble sum_of_sines(xt::pyarray<double> &m)\n{\n auto sines = xt::sin(m);\n // sines does not actually hold any value, which are only computed upon access\n return std::accumulate(sines.begin(), sines.end(), 0.0);\n}\n\nPYBIND11_PLUGIN(xtensor_python_test)\n{\n xt::import_numpy();\n pybind11::module m(\"xtensor_python_test\", \"Test module for xtensor python bindings\");\n\n m.def(\"sum_of_sines\", sum_of_sines,\n \"Computes the sum of the sines of the values of the input array\");\n\n return m.ptr();\n}\n\n\nPython Code:\n\nimport numpy as np\nimport xtensor_python_test as xt\n\na = np.arange(15).reshape(3, 5)\ns = xt.sum_of_sines(v)\ns\n",


More about “Accessing numpy array data in C (for numpy 1.7+)” related questions

Accessing numpy array data in C (for numpy 1.7+)

Following examples and the numpy C-API (http://docs.scipy.org/doc/numpy/reference/c-api.html), I'm trying to access numpy array data in cpp, like this: #include &lt;Python.h&gt; #include &lt;frame...

Show Detail

Compatibility numpy 1.7 vs numpy 1.8

Hi is there a compatibility issue with numpy 1.7 and 1.8? I am getting error with a npy that was created using python 2.7 and numpy 1.7. Now I use python 3.4 and numpy 1.8 Traceback (most recent c...

Show Detail

Convert numpy array into Ray dataset

What is the correct way to convert numpy array into ray's dataset? I tried the following, but its not working. import numpy as np arr = np.array([[1.9, 1.0, 1.1, 0.9], [1.8, 1.0, 1.1, 0.9]...

Show Detail

Can NumPy directly load data into a Fortran ordered array?

I often work with data stored in text files with [x, y, z] like format. When loading the data into a NumPy array, it is convenient to maintain the ordering from the text file, where each column is a

Show Detail

Migrating to numpy api 1.7

I have this code (main function in my c++ python module): static PyObject* FMM(PyObject* self, PyObject* args) { PyObject *model_obj; PyObject *time_obj; PyObject *accepted_obj; Py...

Show Detail

Casting a Numpy Structured Array to a C-array of structs

I'm trying to pass and access a Numpy Structured Array created in Python from the C part of an application. The usage I want to make of such array is to create the array from the Python side, use i...

Show Detail

Convert a numpy array of lists to a numpy array

I have some data which is stored as a numpy array with dtype=object, and I would like to extract one column of lists and convert it to a numpy array. It seems like a simple problem, but the only w...

Show Detail

Convert a numpy array of lists to a numpy array

I have some data which is stored as a numpy array with dtype=object, and I would like to extract one column of lists and convert it to a numpy array. It seems like a simple problem, but the only w...

Show Detail

Accessing structured data types in numba vs. numpy

I’ve noticed a discrepancy between how numba and numpy allow access to the different sub-fields of arrays with structured datatypes. I’m testing the following definitions of a few functions accessi...

Show Detail

Embedding a python/cython numpy array: Passing numpy array to c pointer

I am trying to pass a cython-numpy array to a c struct. Which works sometimes but crashes if the array becomes too 'large'. While this is not really large (like &lt;100k double type entries) and I ...

Show Detail