【问题标题】:Cython PXD does not seem to be used for PYX fileCython PXD 似乎没有用于 PYX 文件
【发布时间】:2016-03-14 20:41:57
【问题描述】:

我正在尝试通过实现从 C++ 到 Python 的线性插值器来学习 cython。 我正在尝试将 PXD 头文件用于最终的 Intepolator 对象,以便它可以在其他方法/类中重用,所以我希望 PXD 头文件可用。

我有一个工作正常的 cpp_linear_interpolation.cpp 和 cpp_linear_interpolation.h,插值器被实例化为两个双精度向量(x 和 y)作为输入。

有我的文件

cy_linear_interpolation.pxd

# distutils: language = c++

import cython
import numpy as np
cimport numpy as np
from libcpp.vector cimport vector

cdef extern from "cpp_linear_interpolation.h":
    cdef cppclass cppLinearInterpolation:
        cppLinearInterpolation(vector[double], vector[double]) except +
        vector[double] interp(vector[double]) except +

        vector[double] m_x
        vector[double] m_y
        int m_len
        double m_x_min
        double m_x_max

py_linear_interpolation.pxd

from cy_linear_interpolation cimport cppLinearInterpolation

cdef class LinearInterpolation:
     cdef cppLinearInterpolation * thisptr

py_linear_interpolation.pyx

import cython
import numpy as np
cimport numpy as np
from libcpp.vector cimport vector
from cy_linear_interpolation cimport cppLinearInterpolation


cdef class LinearInterpolation:
    # cdef cppLinearInterpolation *thisptr

    def __cinit__(self,vector[double] x,vector[double] y):
        self.thisptr = new cppLinearInterpolation(x, y)

    def __dealloc__(self):
        del self.thisptr

    def interpolate(self,vector[double] x_new):
        return self.thisptr.interp(x_new)

setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy


setup( name = 'Cython_LinInt',
       ext_modules=[Extension("cython_linear_interpolation",
                              sources=["py_linear_interpolation.pyx", "cpp_linear_interpolation.cpp",],
                              language="c++",
                              include_dirs=[numpy.get_include()]) 
       ],
       cmdclass = {'build_ext': build_ext},
)    

使用 Microsoft (R) C/C++ 优化编译器版本 15.00.30729.01 for x64 进行编译

我收到错误消息

无法将“cppLinearInterpolation *”转换为 Python 对象

如果我移动

cdef cppLinearInterpolation * _thisptr

到 pyx 文件(py_linear_interpolation.pyx 中注释掉的行),它编译并运行,但是我无法从另一个 cython 文件访问指针。 理想情况下,我将能够从 python 实例化插值器,并将其用作其他 python / cython 函数的参数。 我确信我一定是在做一些愚蠢的事情,但是我已经被这个问题阻止了一段时间,还没有找到解决方案......

编辑:py_linear_interpolation.pyx 中有错字,现已更正 编辑 2:py_linear_interpolation.pyd 中的类型相同,成员名称是 thisptr,代码仍然没有编译,我得到了同样的错误。 cython 编译器似乎没有认识到 self.thisptr 不是 python 对象,应该是指向 cppLinearInterpolation 的指针

【问题讨论】:

    标签: c++ cython


    【解决方案1】:

    改变这个:

    self.thisptr = new cppLinearInterpolation(x, y)
    

    收件人:

    self._thisptr = new cppLinearInterpolation(x, y)
    

    【讨论】:

    • 感谢 Czarek 的回答。这是我的一种类型,代码仍然无法使用正确的 thisptr 名称
    • 随着最新的变化,我现在得到 py_linear_interpolation.pyx:12:5: C class 'LinearInterpolation' is declared but not defined
    • @Damien022 我提到的导入是不需要的。见here。您没有提供足够的详细信息来进一步调试。我怀疑您在其他 Cython 文件中使用“import”而不是“cimport”。
    • 我做了一个更基本的例子,但仍然遇到同样的问题。我应该将新示例作为新问题发布还是完全编辑我的原始问题?
    • 我还没有收到关于编译错误的Cython question 回复。您或许可以提供帮助。
    【解决方案2】:

    我会尝试将__cinit__ 更改为

    def __init__(self, x, y):
        self.thisptr = new cppLinearInterpolation(x, y)
    

    由于没有给出cpp_linear_interpolation.h等文件,所以无法自己测试。

    【讨论】:

    • 谢谢你,。我仍然没有找到问题的解决方案,我会尝试写一个关于 SO 的小而完整的新问题。
    • 我还没有收到关于编译错误的Cython question 回复。您或许可以提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多