在运行t检验时,Python运行时错误
Python Runtime Error on running T-Test
我正在调用一个python函数,以从我的C 代码中计算" t检验"。函数调用如下:
#include <iostream>
#include "Python.h"
#include "/usr/local/lib/python2.7/site-packages/numpy/core/include/numpy/arrayobject.h"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
int main(int argc, char** argv)
{
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append("PATH_TO_MOD")");
PyObject *pName = PyString_FromString("tmpPyth");
PyObject *pModule = PyImport_Import(pName);
double arr[] ={9.74219, 10.2226, 8.7469, 8.69791, 9.96442, 9.96472, 9.37913, 9.75004};
double arr1[] ={9.74219, 10.2226, 8.7469, 8.69791, 9.96442, 9.96472, 9.37913, 9.75004};
PyObject *lst = PyList_New(8);
PyObject *lst1 = PyList_New(8);
// if (!lst)
// return NULL;
for (int i = 0; i < 8; i++) {
PyObject *num = PyFloat_FromDouble(arr[i]);
PyObject *num1 = PyFloat_FromDouble(arr1[i]);
PyList_SET_ITEM(lst, i, num);
PyList_SET_ITEM(lst1, i, num1);
}
PyObject *pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, lst);
PyTuple_SetItem(pArgs, 1, lst1);
if (pModule != NULL) {
PyObject *pFunc = PyObject_GetAttrString(pModule, "blah");
if(pFunc != NULL){
PyObject_CallObject(pFunc, pArgs);
}
}
else
std::cout << "Module path provided may be wrong. Module not found.nn";
return 0;
}
和我的Python模块定义如下:
import numpy
import scipy
import matplotlib
from scipy import stats
def blah(baseline, follow_up):
paired_sample = stats.ttest_rel(baseline , follow_up )
print "The t-statistic is %.3f and the p-value is %.3f." % paired_sample
现在,当我尝试运行此操作时,我会得到以下运行时例外:
/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/stats/stats.py:3458: RuntimeWarning: invalid value encountered in divide
t = np.divide(dm, denom)
但是,如果我明确定义列表并尝试执行" t检验"功能,则运行良好。运行函数定义如下:
import numpy
import scipy
import matplotlib
from scipy import stats
def blah():
baseline = [9.74219, 10.2226, 8.7469, 8.69791, 9.96442, 9.96472, 9.37913, 9.75004]
follow_up = [9.94227,9.46763,8.53081,9.43679,9.97695,10.4285,10.159,8.86134]
paired_sample = stats.ttest_rel(baseline , follow_up )
print "The t-statistic is %.3f and the p-value is %.3f." % paired_sample
我假设我在定义传递给Python脚本的列表时犯了一些错误,但无法弄清楚什么。任何帮助将不胜感激。
arr1
在您的C 代码中与arr
相同,因此除以零。由于您的Python代码中的baseline
和follow_up
不同,因此您不会遇到错误。
对于较大的数组,您不想通过python列表将它们包装,而是直接将阵列发送到Python。我已经修改了您上面的代码以执行此操作:
-- pyfromc.cc --
#include <iostream>
#include <Python.h>
int main(int argc, char** argv)
{
Py_Initialize();
PyRun_SimpleString("import sys; sys.path.append('.')");
// PyRun_SimpleString("print '\n'.join(sys.path)");
PyObject *pName = PyString_FromString("ttest");
PyObject *pModule = PyImport_Import(pName);
double arr[] ={9.74219, 10.2226, 8.7469, 8.69791, 9.96442, 9.96472, 9.37913, 9.75004};
double arr1[] ={9.94227,9.46763,8.53081,9.43679,9.97695,10.4285,10.159,8.86134};
PyObject *pArgs = PyTuple_New(3);
PyTuple_SetItem(pArgs, 0, PyLong_FromLong(8));
PyTuple_SetItem(pArgs, 1, PyLong_FromVoidPtr(arr));
PyTuple_SetItem(pArgs, 2, PyLong_FromVoidPtr(arr1));
if (pModule != NULL) {
PyObject *pFunc = PyObject_GetAttrString(pModule, "blahptr");
if(pFunc != NULL){
PyObject_CallObject(pFunc, pArgs);
}
}
else
std::cout << "Module path provided may be wrong. Module not found.nn";
return 0;
}
和python侧:
-- ttest.py --
from ctypes import POINTER, c_double, cast
c_double_p = POINTER(c_double)
import numpy as np
from scipy import stats
def blahptr(n, baseline_ptr, follow_up_ptr):
baseline = np.ctypeslib.as_array(cast(baseline_ptr, c_double_p), shape=(n,))
follow_up = np.ctypeslib.as_array(cast(follow_up_ptr, c_double_p), shape=(n,))
return blah(baseline, follow_up)
def blah(baseline, follow_up):
paired_sample = stats.ttest_rel(baseline , follow_up )
print "The t-statistic is %.3f and the p-value is %.3f." % paired_sample
return paired_sample
为了在Mac OS X上编译并运行代码,我使用以下内容:
$ PYENV=/path/to/python/env
$ c++ pyfromc.cc -I$PYENV/include/python2.7 -L$PYENV/lib -lpython2.7
$ PYTHONHOME=$PYENV DYLD_LIBRARY_PATH=$PYENV/lib ./a.out
The t-statistic is -0.187 and the p-value is 0.857.
通过将环境变量设置在与可执行文件相同的行上,BASH解释器仅在命令的持续时间内将它们设置为其他命令。
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么当 vector 为空时会显示运行时错误?
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 运行时错误:矢量下标超出范围:正在检查空集
- 分配给gslice_array会导致运行时错误
- cout 新创建的对象引发运行时错误
- C++在使用std::multimap时出现运行时错误的几率很小
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 运行时错误:找不到与 ImageSensor 匹配的 Python 模块
- 在运行t检验时,Python运行时错误
- Boost Python运行时错误,将派生类型的对象从Python传递给c++函数,期望将shared_ptr传递给基类