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.
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-APIArgsArray = 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 HEREif (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 ();