传递一个C ++的std ::矢量,以numpy的数组在Python [英] Passing a C++ std::Vector to numpy array in Python

查看:2264
本文介绍了传递一个C ++的std ::矢量,以numpy的数组在Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一通,我在我的 C ++ code到A 蟒蛇 numpy的数组。我希望做一些下游加工的Python 并要使用一些Python设施,一旦我填充numpy的数组。其中一个我想要做的最大的事情就是能够绘制的事情,C ++是一个有点笨拙,当谈到这一点。此外,我希望能够利用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 in Python and want to use some python facilities, once I populate the numpy array. One of the biggest things I want to do is to be able to plot things, and C++ is a bit clumsy when it comes to that. Also I want to be able to leverage Python's statistical power.

我虽然不是以如何做到这一点非常明确。我花了很多时间通过Python C API文档去。我碰到一个函数PyArray_SimpleNewFromData,显然可以做的伎俩。我仍然感到很不清楚至于code的整体设立关注。我建立一定非常简单的测试案例,以帮助我理解这个过程。我生成以下code作为在Visual Studio EX preSS 2012年一空并且standlone项目,我把这个文件PROJECT1

Though I am not very clear as to how to do it. I spent a lot of time going through the Python C API documentation. I came across a function PyArray_SimpleNewFromData that apparently can do the trick. I still am very unclear as far as the overall set up of the code is concerned. 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; 
}

我的目标是能够读取在Python中的PyObject。我坚持,因为我不知道如何在Python中引用这个模块。特别是如何导入从Python的这个项目,我试图做一个进口PROJECT1,从项目路径在python,但未能成功。有一次,我理解这一点基本情况,我的目标是找出一种方式来传递,我在我的主要功能到Python计算矢量容器。我不知道该怎么做,要么。

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. Once I understand this base case, my goal is to figure out a way to pass the vector container that I compute in my main function to Python. I am not sure how to do that either.

哪位高手谁可以帮我这个问题,或者发布一些code,它读取并填充从一个简单的C ++载体numpy的阵列的一个简单的良好控制的例子,我将非常感激。提前很多感谢。

Any experts who can help me with this, or maybe post a simple well contained example of some code that reads in and populates a numpy array from a simple c++ vector, I will be grateful. Many thanks in advance.

推荐答案

由于没有答案,这是实际上有助于人们可能会寻找这样的事情我想我把一个简单的解决方案。

Since there is no answer to this that is actually helpful for people that might be looking for this sort of thing I figured I'd put an easy solution.

首先,你需要在C ++中创建一个Python扩展模块,这是很容易做,是Python C-API的文档都在,所以我不打算去成。

First you will need to create a python extension module in C++, this is easy enough to do and is all in the python c-api documentation so i'm not going to go into that.

现在以一个C ++的std ::向量转换为numpy的数组是非常简单的。首先,您需要导入numpy的数组头

Now to convert a c++ std::vector to a numpy array is extremely simple. You first need to import the numpy array header

#include <numpy/arrayobject.h>

和你intialising功能,你需要import_array()

and in your intialising function you need to import_array()

PyModINIT_FUNC
inittestFunction(void){
   (void) Py_InitModule("testFunction". testFunctionMethods);
   import_array();
}

现在你可以使用所提供的numpy的阵列功能。
你会想这样做的一个是作为OP说几年前PyArray_SimpleNewFromData,这是愚蠢的简单易用。所有你需要的是一个类型npy_intp的数组,这是数组要创建的形状。确保它是与使用testVector.size()你的载体,(以及多个维度做testVector [0] .size(),testVector [0] [0] .size()等。向量保证是连续在C ++ 11,除非它是一个布尔)。

now you can use the numpy array functions that are provided. The one that you will want for this is as the OP said a few years back PyArray_SimpleNewFromData, it's stupidly simple to use. All you need is an array of type npy_intp, this is the shape of the array to be created. make sure it is the same as your vector using testVector.size(), (and for multiple dimensions do testVector[0].size(), testVector[0][0].size() ect. vectors are guaranteed to be continuous in c++11 unless it's a bool).

//create testVector with data initialised to 0
std::vector<std::vector<uint16_t>> testVector;
testVector.resize(width, std::vector<uint16_t>(height, 0);
//create shape for numpy array
npy_intp dims[2] = {width, height}
//convert testVector to a numpy array
PyArrayObject* numpyArray = (PyArrayObject*)PyArray_SimpleNewFromData(2, dims, NPY_UINT16, (uint16_t*)testVector.data());

要经过PARAMATERS。首先,你需要将其转换为PyArrayObject,否则这将是一个的PyObject,当回到蟒蛇不会是一个numpy的数组。
2,是阵列中的维数。
变暗,是阵列的形状。这具有为类型npy_intp的
NPY_UINT16的数据类型,数组将是蟒蛇。
然后使用testVector.data()来获取数组的*数据,施放此要么无效或相同的数据类型作为载体的指针。

To go through the paramaters. First you need to cast it to a PyArrayObject, otherwise it will be a PyObject and when returned to python won't be a numpy array. The 2, is the number of dimensions in the array. dims, is the shape of the array. This has to be of type npy_intp NPY_UINT16 is the data type that the array will be in python. you then use testVector.data() to get the data of the array, cast this to either void* or a pointer of the same data type as your vector.

希望这有助于其他人谁可能需要这个。

Hope this helps anyone else who may need this.

(另外,如果你不需要纯粹的速度我会建议使用C-API避免,它导致了不少问题,并用Cython或痛饮仍可能是你最好的选择。还有的C类型可以是非常有帮助

(Also if you don't need pure speed I would advise avoiding using the C-API, it causes quite a few problems and cython or swig are still probably your best choices. There is also c types which can be quite helpful.

这篇关于传递一个C ++的std ::矢量,以numpy的数组在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆