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
The call to the numpy's bitwise_or throws an error in the first function:
cimported module has no attribute 'bitwise_or'
This problem seems similar to (Make distutils look for numpy header files in the correct place), but I've tried both answers and they do not work.
Numpy and Cython both installed through Anaconda with Python 3.6.
The numpy header file is located at:
/home/myname/anaconda3/lib/python3.6/site-packages/numpy/core/include/numpy/ndarrayobject.h
Edit:
Adding import numpy as np
fixes the error when running cython, but introduces the following error when import the compiled file:
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
The setup is just a compiled cython file with numpy imports and a python file import it. ctest.py:
import gridfuncs
gridfuncs.pyx:
cimport numpy as np
import numpy as np
Compiled as such:
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