I am trying to learn cython by implementing a linear interpolator from C++ to Python. I am trying to use PXD header files for the final Intepolator object so that it can be reused in other methods / classes down the line, so I want to have the PXD header file available.
I have a cpp_linear_interpolation.cpp and cpp_linear_interpolation.h that work fine, the interpolator gets instantiated with two vectors of double (x and y) as input.
There are my files
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},
)
compiling with Microsoft (R) C/C++ Optimizing Compiler Version 15.00.30729.01 for x64
I get the error message
Cannot convert 'cppLinearInterpolation *' to Python object
If I move
cdef cppLinearInterpolation * _thisptr
to the pyx file (the commented out line in py_linear_interpolation.pyx), it compiles and run, but then I cannot access the pointer from another cython file. Ideally I would be able to instantiate the interpolator from python, and use it as argument for other python / cython functions. I am sure I must be doing something stupid, but I have been blocked on that problem for a little while now and have not found yet the solution...
EDIT: there was a typo in py_linear_interpolation.pyx, now corrected EDIT 2: the same type was in py_linear_interpolation.pyd, the member name is thisptr, the code still isn t compiling and I get the same error. It does not seem that the cython compiler recognizes that self.thisptr is not a python object and should be a pointer to a cppLinearInterpolation
Change this:
self.thisptr = new cppLinearInterpolation(x, y)
To:
self._thisptr = new cppLinearInterpolation(x, y)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With