将2维C数组传递给python numpy

时间:2022-10-07 21:28:12

I need some help regarding passing C array to python(numpy). I have 2d array of doubles NumRows x NumInputs, it seems that PyArray_SimpleNewFromData does not convert it right way - it is hard to see because debugger does not show much, only pointers.

关于将C数组传递给python(numpy)我需要一些帮助。我有2d数组的双精度NumRows x NumInputs,似乎PyArray_SimpleNewFromData没有正确转换它 - 很难看到因为调试器没有显示太多,只有指针。

What would be the right way to pass 2 dimensional array ?

传递二维数组的正确方法是什么?

int NumRows = X_test.size();
int NumInputs = X_test_row.size();

double **X_test2 = new double*[NumRows];
for(int i = 0; i < NumRows; ++i) 
{
    X_test2[i] = new double[NumInputs];
}


for(int r = 0; r < NumRows; ++r) 
{
    for(int c = 0; c < NumInputs; ++c) 
    {
        X_test2[r][c] = X_test[r][c];
    }
}




const char *ScriptFName = "100-ABN-PREDICT";
char *FunctionName=NULL;

FunctionName="PredictGBC_DBG"; 

npy_intp Dims[2];
Dims[0]= NumRows;
Dims[1] = NumInputs;

PyObject *ArgsArray;
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs;

int row, col, rows, cols, size, type;

const double* outArray;
double ArrayItem;

//===================

Py_Initialize();

pName = PyBytes_FromString(ScriptFName);

pModule = PyImport_ImportModule(ScriptFName);

if (pModule != NULL)
{
    import_array(); // Required for the C-API

    ArgsArray = PyArray_SimpleNewFromData (2, Dims, NPY_DOUBLE, X_test2);//SOMETHING WRONG 

    pDict = PyModule_GetDict(pModule);

    pArgs = PyTuple_New (1);
    PyTuple_SetItem (pArgs, 0, ArgsArray);

    pFunc = PyDict_GetItemString(pDict, FunctionName);

    if (pFunc && PyCallable_Check(pFunc))
    {

        pValue = PyObject_CallObject(pFunc, pArgs);//CRASHING HERE

        if (pValue != NULL) 
        {
            rows = PyArray_DIM(pValue, 0);
            cols = PyArray_DIM(pValue, 1);
            size = PyArray_SIZE(pValue);
            type = PyArray_TYPE(pValue);


            // get direct access to the array data
            //PyObject* m_obj;
            outArray = static_cast<const double*>(PyArray_DATA(pValue));


            for (row=0; row < rows; row++) 
            {
                ArrayItem = outArray[row];
                y_pred.push_back(ArrayItem);
            }

        }
        else 
        {
            y_pred.push_back(EMPTY_VAL);
        }
    }
    else 
    {
        PyErr_Print();
    }//pFunc && PyCallable_Check(pFunc)



}//(pModule!=NULL
else
{
    PyErr_SetString(PyExc_TypeError, "Cannot call function ?!");
    PyErr_Print();
}




Py_DECREF(pValue);
Py_DECREF(pFunc);

Py_DECREF(ArgsArray);  
Py_DECREF(pModule);
Py_DECREF(pName);


Py_Finalize (); 

1 个解决方案

#1


5  

You'll have to copy your data to a contiguous block of memory. To represent a 2d array, numpy does not use an array of pointers to 1d arrays. Numpy expects the array to be stored in a contiguous block of memory, in (by default) row major order.

您必须将数据复制到连续的内存块。为了表示2d数组,numpy不使用指向1d数组的指针数组。 Numpy希望将数组存储在一个连续的内存块中(默认情况下)行主要顺序。

If you create your array using PyArray_SimpleNew(...), numpy allocates the memory for you. You have to copy X_test2 to this array, using, say, std::memcpy or std::copy in a loop over the rows.

如果使用PyArray_SimpleNew(...)创建数组,numpy会为您分配内存。您必须将X_test2复制到此数组,例如,在行上的循环中使用std :: memcpy或std :: copy。

That is, change this:

也就是说,改变这个:

ArgsArray = PyArray_SimpleNewFromData (2, Dims, NPY_DOUBLE, X_test2);//SOMETHING WRONG 

to something like this:

这样的事情:

// PyArray_SimpleNew allocates the memory needed for the array.
ArgsArray = PyArray_SimpleNew(2, Dims, NPY_DOUBLE);

// The pointer to the array data is accessed using PyArray_DATA()
double *p = (double *) PyArray_DATA(ArgsArray);

// Copy the data from the "array of arrays" to the contiguous numpy array.
for (int k = 0; k < NumRows; ++k) {
    memcpy(p, X_test2[k], sizeof(double) * NumInputs);
    p += NumInputs;
}

(It looks like X_test2 is a copy of X_test, so you might want to modify the above code to copy directly from X_test to the numpy array.)

(看起来X_test2是X_test的副本,因此您可能希望修改上面的代码以直接从X_test复制到numpy数组。)

#1


5  

You'll have to copy your data to a contiguous block of memory. To represent a 2d array, numpy does not use an array of pointers to 1d arrays. Numpy expects the array to be stored in a contiguous block of memory, in (by default) row major order.

您必须将数据复制到连续的内存块。为了表示2d数组,numpy不使用指向1d数组的指针数组。 Numpy希望将数组存储在一个连续的内存块中(默认情况下)行主要顺序。

If you create your array using PyArray_SimpleNew(...), numpy allocates the memory for you. You have to copy X_test2 to this array, using, say, std::memcpy or std::copy in a loop over the rows.

如果使用PyArray_SimpleNew(...)创建数组,numpy会为您分配内存。您必须将X_test2复制到此数组,例如,在行上的循环中使用std :: memcpy或std :: copy。

That is, change this:

也就是说,改变这个:

ArgsArray = PyArray_SimpleNewFromData (2, Dims, NPY_DOUBLE, X_test2);//SOMETHING WRONG 

to something like this:

这样的事情:

// PyArray_SimpleNew allocates the memory needed for the array.
ArgsArray = PyArray_SimpleNew(2, Dims, NPY_DOUBLE);

// The pointer to the array data is accessed using PyArray_DATA()
double *p = (double *) PyArray_DATA(ArgsArray);

// Copy the data from the "array of arrays" to the contiguous numpy array.
for (int k = 0; k < NumRows; ++k) {
    memcpy(p, X_test2[k], sizeof(double) * NumInputs);
    p += NumInputs;
}

(It looks like X_test2 is a copy of X_test, so you might want to modify the above code to copy directly from X_test to the numpy array.)

(看起来X_test2是X_test的副本,因此您可能希望修改上面的代码以直接从X_test复制到numpy数组。)