1

I am working on my Project which implies the use of Empirical Mode Decomposition in C++ for EEG Signals. The input Data is Eigen::MatrixXd, where the rows are the Channels and the columns are the samples.

I did not found a good C++ library for EMD so I want to use a Python one (dsatools). I have downloaded the package through Pip installer from the setup.py file on Xubuntu... so it's a system package now.

the problem is that the program can't read the module.

this is the code:

std::vector <Eigen::MatrixXd> DataAquisition::EMD (Eigen::MatrixXd array, int order, int iterations, int locality) {
std::vector <Eigen::MatrixXd> IMFs;


for (int i = 0; i < array.rows(); i++) {
    Eigen::MatrixXd Kanals = array.row(i);
    Eigen::MatrixXd IMFs_Cpp;
    Py_Initialize();
    //PyRun_SimpleString("from dsatools._base._imf_decomposition import * ");
    PyObject* sys_path = PySys_GetObject("path");
    PyObject* ProgrammName = PyUnicode_FromString("/home/user/Schreibtisch/mne-cpp-main/applications/mne_bci/MNE-BCI-QT/dsatools-master/dsatools/_base/_imf_decomposition/_emd.py");
    PyList_Append(sys_path, ProgrammName);
    PyObject* pModuleString = PyUnicode_FromString ((char*)"_emd.py");
    PyObject* pModule = PyImport_Import(pModuleString);
    PyObject* pFunction = PyObject_GetAttrString(pModule,(char*)"emd");

    //PyObject* pDict = PyModule_GetDict(pModule);
    //PyObject* pFunc = PyDict_GetItemString(pDict, (char*)"emd");

    if (PyCallable_Check(pFunction))
    {
        PyObject* Signal = Py_BuildValue("(d)",(double*)Kanals.data());
        PyObject* Order = Py_BuildValue("(i)",order);
        PyObject* method = Py_BuildValue("(z)",(char*)"cubic");
        PyObject* max_itter = Py_BuildValue("(i)",iterations);
        PyObject* args = PyTuple_Pack(4,Signal,Order,method,max_itter);
        PyErr_Print();
        PyObject* IMFs_Py = PyObject_CallObject(pFunction,args);
        PyErr_Print();
        if (PyArray_Check(IMFs_Py))
            std::cout << "EMD Output is NOT Array \n";
        PyArrayObject *np_ret = reinterpret_cast <PyArrayObject*> (IMFs_Py);
        int Rows = PyArray_SHAPE(np_ret)[0];
        int Cols = PyArray_SHAPE(np_ret)[1];
        double* c_out = reinterpret_cast<double*>(PyArray_DATA(np_ret));
        Eigen::MatrixXd IMFs_Cpp = Eigen::Map <Eigen::MatrixXd> (c_out,Rows,Cols);
        IMFs.push_back(IMFs_Cpp);
    }
    else
        std::cout << "Python did not call the function \n";

    Py_Finalize();
}
return IMFs;}

this is how the code in Python should look like and I just want to call the emd function:

enter image description here

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • try calling `PyImport_Import('_emd')` instead of `PyImport_Import('_emd.py')`. in addition, try adding the parent directory of this script to the PYTHONPATH, instead of adding the script. [PYTHONPATH should contain directories](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH) – jsofri Nov 18 '21 at 07:05

0 Answers0