When it comes to usign the Numpy C API to create arrays,
there are plenty of resources to use the simpler functions,
such as the PyArray_SimpleNewXXX()
series. To use the most
generic ones, you are supposed to be fairly well versed in
Numpy. Unfortunately, there are functions of intermediate
complexity, and resources for these are scarce.
Today, I had to create a Numpy array as a view (without
copying data) of some fields in a vector of structs.
Clearly, not a job for the PyArray_SimpleNewXXX()
functions,
but for PyArray_New()
. Besides basic info such as the buffer,
the type, and the strides, this function requires some
arcane arguments, such as the PyTypeObject* subtype
.
For the life of me, I could not figure out what to pass
to the function as that argument. The Numpy sources were
of no help. since they passed a pointer to a function
(&PyArray_Type
) as that argument (?!?!?!). After much
experimentation and frustration, I found the answer
to my predicament in this thread:
https://groups.google.com/forum/#!topic/cython-users/fFBiCCwblD0
Argh!!!! Just np.ndarray
!!!!!! So simple, yet so
cryptic! I am sure that the answer to this
question was somehow in the Numpy docs, but
WHY NOT PUT AN EXAMPLE CALL FOR EACH FUNCTION?
Anyway, let's see a snippet of code with the solution...