迁移到 numpy api 1.7

Posted

技术标签:

【中文标题】迁移到 numpy api 1.7【英文标题】:Migrating to numpy api 1.7 【发布时间】:2016-01-02 23:51:28 【问题描述】:

我有这段代码(我的 c++ python 模块中的主函数):

static PyObject* FMM(PyObject* self, PyObject* args)

    PyObject *model_obj;
    PyObject *time_obj;
    PyObject *accepted_obj;
    PyObject *lat_obj;
    PyObject *lon_obj;
    PyObject *h_obj;
    int N;

    if (!PyArg_ParseTuple(args, "OOOOOOi", &model_obj, &time_obj, &accepted_obj, &lat_obj, &lon_obj, &h_obj, &N))
    
        Py_INCREF(Py_None);
        return Py_None;
    

    PyObject *model = PyArray_FROM_OTF(model_obj, NPY_FLOAT, NPY_INOUT_ARRAY);
    PyObject *time = PyArray_FROM_OTF(time_obj, NPY_FLOAT, NPY_INOUT_ARRAY);
    PyObject *accepted = PyArray_FROM_OTF(accepted_obj, NPY_BOOL, NPY_INOUT_ARRAY);
    PyObject *lat = PyArray_FROM_OTF(lon_obj, NPY_DOUBLE, NPY_INOUT_ARRAY);
    PyObject *lon = PyArray_FROM_OTF(lon_obj, NPY_DOUBLE, NPY_INOUT_ARRAY);
    PyObject *h = PyArray_FROM_OTF(h_obj, NPY_DOUBLE, NPY_INOUT_ARRAY);

    float *MODEL    = static_cast<float *>(PyArray_DATA(model));
    float *TIME     = static_cast<float *>(PyArray_DATA(time));
    bool *ACCEPTED  = static_cast<bool *>(PyArray_DATA(accepted));
    double *LAT     = static_cast<double *>(PyArray_DATA(lat));
    double *LON     = static_cast<double *>(PyArray_DATA(lon));
    double *H       = static_cast<double *>(PyArray_DATA(h));
    _FMM(MODEL, TIME, ACCEPTED, LAT, LON, H, N);


    Py_INCREF(Py_None);
    return Py_None;

我收到了警告

#warning "Using deprecated NumPy API,

我想:

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

我应该如何修改我的函数以在新版本的 c-api 中复制结果?你能给我举个例子吗?

【问题讨论】:

【参考方案1】:

嗯,这很简单:

static PyObject* FMM(PyObject* self, PyObject* args)

    PyArrayObject *model_obj;
    PyArrayObject *time_obj;
    PyArrayObject *accepted_obj;
    PyArrayObject *lat_obj;
    PyArrayObject *lon_obj;
    PyArrayObject *h_obj;
    int N;

    if (!PyArg_ParseTuple(args, "OOOOOOi", &model_obj, &time_obj, &accepted_obj, &lat_obj, &lon_obj, &h_obj, &N))
    
        Py_INCREF(Py_None);
        return Py_None;
    

    //PyObject *model = PyArray_FROM_OTF(model_obj, NPY_FLOAT, NPY_INOUT_ARRAY);
    //PyObject *time = PyArray_FROM_OTF(time_obj, NPY_FLOAT, NPY_INOUT_ARRAY);
    //PyObject *accepted = PyArray_FROM_OTF(accepted_obj, NPY_BOOL, NPY_INOUT_ARRAY);
    //PyObject *lat = PyArray_FROM_OTF(lon_obj, NPY_DOUBLE, NPY_INOUT_ARRAY);
    //PyObject *lon = PyArray_FROM_OTF(lon_obj, NPY_DOUBLE, NPY_INOUT_ARRAY);
    //PyObject *h = PyArray_FROM_OTF(h_obj, NPY_DOUBLE, NPY_INOUT_ARRAY);

    float *MODEL    = static_cast<float *>(PyArray_DATA(model_obj));
    float *TIME     = static_cast<float *>(PyArray_DATA(time_obj));
    bool *ACCEPTED  = static_cast<bool *>(PyArray_DATA(accepted_obj));
    double *LAT     = static_cast<double *>(PyArray_DATA(lat_obj));
    double *LON     = static_cast<double *>(PyArray_DATA(lon_obj));
    double *H       = static_cast<double *>(PyArray_DATA(h_obj));
    _FMM(MODEL, TIME, ACCEPTED, LAT, LON, H, N);


    Py_INCREF(Py_None);
    return Py_None;

【讨论】:

以上是关于迁移到 numpy api 1.7的主要内容,如果未能解决你的问题,请参考以下文章

从 South 迁移到 Django 1.7 迁移:可交换依赖项

从 1.5 迁移到 1.7 时的单元测试警告

无法将 Django 从 1.7 迁移到 1.8

从 Tomcat 6 JDK 1.6 迁移到 Tomcat 7 JDK 1.7 会话问题

在 Django 1.7 迁移之前运行 South 迁移的推荐方法是啥?

Django 1.7 - 创建初始迁移