Cython无法访问numpy头文件

时间:2018-02-13 10:53:11

标签: numpy anaconda cython

cimport numpy as np
import functools

cpdef _get_eligible_chs_bitmap(np.ndarray[np.npy_bool, ndim=3] grid, tuple cell):
    """Find eligible chs by bitwise ORing the allocation maps of neighbors"""
    cdef int r, c
    r, c = cell
    cdef list neighs = neighbors(2, r, c, separate=True, include_self=True)
    cdef np.ndarray[np.int64_t, ndim=1] alloc_map = np.bitwise_or.reduce(grid[neighs])
    return alloc_map

def hex_distance(cell_a, cell_b):
    r1, c1 = cell_a
    r2, c2 = cell_b
    return (abs(r1 - r2) + abs(r1 + c1 - r2 - c2) + abs(c1 - c2)) / 2


@functools.lru_cache(maxsize=None)
def neighbors(dist, row, col, separate=False, include_self=False, rows=7, cols=7):
    """
    Returns a list with indices of neighbors with a distance of 'dist' or less
    from the cell at (row, col)

    If 'separate' is True, return ([r1, r2, ...], [c1, c2, ...]),
    else return [(r1, c1), (r2, c2), ...]
    """
    if separate:
        rs = []
        cs = []
    else:
        idxs = []
    for r2 in range(rows):
        for c2 in range(cols):
            if (include_self or (row, col) != (r2, c2)) \
                and hex_distance((row, col), (r2, c2)) <= dist:
                if separate:
                    rs.append(r2)
                    cs.append(c2)
                else:
                    idxs.append((r2, c2))
    if separate:
        return (rs, cs)
    return idxs

调用numpy的bitwise_or会在第一个函数中抛出错误:

  

cimported模块没有属性'bitwise_or'

此问题与(Make distutils look for numpy header files in the correct place)类似,但我尝试了两种答案,但它们无效。

Numpy和Cython都是通过Anaconda用Python 3.6安装的。 numpy头文件位于: /home/myname/anaconda3/lib/python3.6/site-packages/numpy/core/include/numpy/ndarrayobject.h

编辑:

添加import numpy as np修复了运行cython时的错误,但在导入编译文件时引入了以下错误:

Traceback (most recent call last):
  File "ctest.py", line 1, in <module>
    import gridfuncs
  File "gridfuncs.pyx", line 2, in init dca.gridfuncs
    import numpy as np
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 951, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 894, in _find_spec
  File "<frozen importlib._bootstrap_external>", line 1157, in find_spec
  File "<frozen importlib._bootstrap_external>", line 1129, in _get_spec
  File "<frozen importlib._bootstrap_external>", line 1271, in find_spec
  File "<frozen importlib._bootstrap_external>", line 96, in _path_isfile
  File "<frozen importlib._bootstrap_external>", line 88, in _path_is_mode_type
RecursionError: maximum recursion depth exceeded

设置只是一个编译的cython文件,有numpy导入和python文件导入它。 ctest.py:

import gridfuncs

gridfuncs.pyx:

cimport numpy as np
import numpy as np

编译如下:

gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python3.6m -I/home/torstein/anaconda3/lib/python3.6/site-packages/numpy/core/include/ -o gridfuncs.so gridfuncs.c

0 个答案:

没有答案