Memoryview 'DTYPE_t[:, :]' not conformable to memoryview 'DTYPE_t[:, :]'

96 views
Skip to first unread message

rens holmer

unread,
Mar 8, 2018, 1:06:21 PM3/8/18
to cython-users
I am relatively new to cython, but I have managed to make some small functions work.

Currently I am trying to build a modular cython codebase where important functions are declared with 'cpdef' so I can write unit tests in python, but use the optimized c-version internally. As far as my understanding of cython goes, this is what cpdef is supposed to do: expose a function to both python and c.

My test codebase is as follows:

test.pyx:
cimport cython
import numpy as np
cimport numpy as np

from fit cimport fit #<--- CHANGING THIS FIXES THE ERROR

ctypedef np.float64_t DTYPE_t

cpdef DTYPE_t test(DTYPE_t[:,:] dat, DTYPE_t[:,:] regs):
cdef DTYPE_t score = fit(dat, regs)
return score

fit.pyx
cimport cython
import numpy as np
cimport numpy as np

cpdef DTYPE_t fit(DTYPE_t[:,:] A, DTYPE_t[:,:] B):
cdef DTYPE_t score
return score

fit.pxd
cimport cython
import numpy as np
cimport numpy as np

ctypedef np.float64_t DTYPE_t

cpdef DTYPE_t fit(DTYPE_t[:,:] dat, DTYPE_t[:,:] regs)

setup.py
from setuptools import setup, find_packages
from setuptools.extension import Extension
from Cython.Build import cythonize
import numpy

extensions = [
Extension(
'*',
include_dirs=[numpy.get_include()],
language = 'c++',
extra_compile_args = ['-ffast-math','-O3','-Wno-unused-function'],
sources = ['*.pyx']
)
]

setup(
name = 'test',
version = '0.0.1',
packages = find_packages(),
ext_modules = cythonize(extensions)
)



In summary: test takes two 2D numpy arrays as memoryviews and passes those to fit, which will do the actual calculations and return a score.
Ideally I would write unit tests in python that import these functions.

The above codebase results in the following error:

Memoryview 'DTYPE_t[:, :]' not conformable to memoryview 'DTYPE_t[:, :]'


Which seems odd, given that the error is reporting two times the same memoryview type, but still declares it 'not conformable'?

If I change the line 'from fit cimport fit'  to   'from fit import fit'  (i.e. use regular python import), the error no longer occurs.

What am I missing here?

Stefan Behnel

unread,
Mar 9, 2018, 4:47:18 AM3/9/18
to Cython-users
You are redefining the DTYPE_t both in fit.pxd and in test.pyx. While this
should normally work, it fails in your case for some reason. Probably a bug
in Cython somewhere, and probably also related to the fact that this is
using external NumPy types here...

I suggest using "fit.DTYPE_t" in test.pyx instead of redeclaring it. That's
definitely the clean way of doing it.

Stefan

rens holmer

unread,
Mar 15, 2018, 1:43:52 PM3/15/18
to cython-users
Awesome, that fixed the error, thanks!

For the record, in test.pyx I changed
from fit cimport fit 

ctypedef np.float64_t DTYPE_t

To:
from fit cimport fit, DTYPE_t
Reply all
Reply to author
Forward
0 new messages