1
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
tsorn
  • 3,365
  • 1
  • 29
  • 48
  • 2
    try adding `import numpy as np` in addition to `cimport` – ead Feb 13 '18 at 14:14
  • That worked, can I be sure it's 'using the right one'? – tsorn Feb 13 '18 at 14:43
  • @ead Error does not appear when running cython, but I get complaints about maximum recursion depth exceeded on the `import numpy as np` line. – tsorn Feb 13 '18 at 15:06
  • Could provide a minimal example and back trace of the error? – ead Feb 13 '18 at 15:22
  • @ead yes, have updated the post – tsorn Feb 13 '18 at 15:35
  • The minimum example just consists of importing the compiled cython file with the double numpy imports. All the other code can be removed and the error is still there. – tsorn Feb 13 '18 at 15:41
  • No error here. Please consider: 1. Fix the mix of `/usr/...` and anaconda include paths. 2. Check that `gcc` is the proper compiler for building extensions with anaconda. – Pierre de Buyl Feb 13 '18 at 16:04
  • I would also say that something is wrong with your python-installation (no pythonpath set or similar). – ead Feb 13 '18 at 16:31
  • It looks like it's picking up a "`dca`" somewhere in the module name (i.e. Cython has decided that your module is a submodule of something else). It's a guess but I think that might be relevant. No ideas for fixing it though... – DavidW Feb 13 '18 at 21:23

0 Answers0