Passing a C++ std::Vector to numpy array in Python
I am trying a pass a vector of doubles that I generate in my C++ code to a
python numpy array. I am looking to do some downstream processing and want
to use some python facilities, once I populate the numpy array. Though I
am not very clear as to how to do it. I went though the Python C API
documentation and came across a function PyArray_SimpleNewFromData that
apparently can do the trick.
I am building certain very simple test cases to help me understand this
process. I generated the following code as a standlone Empty project in
Visual Studio express 2012. I call this file Project1
#include <Python.h>
#include
"C:/Python27/Lib/site-packages/numpy/core/include/numpy/arrayobject.h"
PyObject * testCreatArray()
{
float fArray[5] = {0,1,2,3,4};
npy_intp m = 5;
PyObject * c = PyArray_SimpleNewFromData(1,&m,PyArray_FLOAT,fArray);
return c;
}
My goal is to be able to read the PyObject in Python. I am stuck because I
don't know how to reference this module in Python. In particular how do I
import this Project from Python, I tried to do a import Project1, from the
project path in python, but failed.
Any experts who can help me with this, or maybe post a simple well
contained example of some code that reads in and populated a numpy array
from a c++ vector, I will be grateful. Many thanks in advance.
No comments:
Post a Comment