Jayden118
Jayden118

Reputation: 21

how to pass an array from c++ to python function and retrieve python returned array to c++

I'm writting a c++ program to call a python function and retrieve the return array.but i always get an error as below:

only length-1 arrays can be converted to Python scalars

and my c++ code:

 int main(int argc, char *argv[])
{
    int i;
    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

    if (argc < 3) 
    {
        printf("Usage: exe_name python_source function_name\n");
        return 1;
    }

    // Initialize the Python Interpreter
    Py_Initialize();

    // Build the name object
    pName = PyString_FromString(argv[1]);

    // Load the module object
    pModule = PyImport_Import(pName);

    // pDict is a borrowed reference 
    pDict = PyModule_GetDict(pModule);

    // pFunc is also a borrowed reference 
    pFunc = PyDict_GetItemString(pDict, argv[2]);

    if (PyCallable_Check(pFunc)) 
    {
        // Prepare the argument list for the call
        if( argc > 3 )
        {
                pArgs = PyTuple_New(argc - 3);
                for (i = 0; i < argc - 3; i++)
                {
                    pValue = PyInt_FromLong(atoi(argv[i + 3]));
                    if (!pValue)
                    {
                        PyErr_Print();
                        return 1;
                    }
                    PyTuple_SetItem(pArgs, i, pValue);  
                }

                pValue = PyObject_CallObject(pFunc, pArgs);

                if (pArgs != NULL)
                {
                    Py_DECREF(pArgs);
                }
        } else
        {
                pValue = PyObject_CallObject(pFunc, NULL);
        }

        if (pValue != NULL) 
        {
            cout <<pValue;
            printf("Return of call : %ld\n", PyInt_AsLong(pValue));
            PyErr_Print();
            Py_DECREF(pValue);
        }
        else 
        {
            PyErr_Print();
        }
    } else 
    {
        PyErr_Print();
    }

    // Clean up
    Py_DECREF(pModule);
    Py_DECREF(pName);

    // Finish the Python Interpreter
    Py_Finalize();

    system("PAUSE");
    return 0;
}

Python function:

 import numpy as np
    _ZERO_THRESHOLD = 1e-9      # Everything below this is zero
def data():
    print "process starting..."
    N = 5
    obs = np.matrix([np.random.normal(size=5) for _ in xrange(N)])              
    V = pca_svd(obs)
    print "V:"
    print V[0:5]

    pca = IPCA(obs.shape[1], 3)
    for i in xrange(obs.shape[0]):
        x = obs[i,:].transpose()
        print " "
        print "new input:"
        print x
        pca.update(x)

    U = pca.components
    A = pca.variances
    B = U.T*x
    print B
    return B

I know there is something wrong with this statement

PyInt_AsLong(pValue) Can anyone tell me how to fix that in order to retrieve the matrix from python to c++

Thank you so much.

Upvotes: 2

Views: 6783

Answers (2)

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67157

You are using PyInt_AsLong(pValue) to convert the Python object pValue to a C long scalar. If pValue is a Numpy array, this means that you are trying to convert the array to a number, which is only possible for length-1 arrays:

only length-1 arrays can be converted to Python scalars

Instead of using PyInt_AsLong, use the PyArray_* functions provided by Numpy's C API to access the data; in particular, see section Array API.

Upvotes: 2

maxy
maxy

Reputation: 5457

You want to use NumPy C API, probably to get a data pointer with

#include <numpy/arrayobject.h>
...
p = (uint8_t*)PyArray_DATA(pValue);

after making really sure that you actually got an array of the right dimensions. See my hello.hpp for some example code.

Upvotes: 1

Related Questions