How to properly declare numpy arrays in cython

Viewed 164

I am trying to Cythonise my code, and remove as many python interactions as I can. I am running into some issue when I am working with numpy arrays. I have a function which does the following. There are some if, else statements and for loops, which don't show any python interactions when I use %%cython --annotate, and I am happy with that. However, despite using the declaration for the array, those lines of code are highlighted in yellow, indicating python interactions.

cdef func(input1, input2):
    cdef np.ndarray[np.double_t, ndim=2] array = np.zeros((length, 6))
    ...
    "Some computations"

        array[index] = [val1, val2, val3, val4, val5, val6]
     "..."
   array = array[~np.all(array ==0., axis=1)]
   return array
    

    

clicking on the little plus sign at the line where it says cdef np.ndarray, I get the error message shown below, unfortunately I am not experience enough to make sense of it.

Pyx_GetModuleGlobalName(__pyx_t_2, __pyx_n_s_np); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 78, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_zeros); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 78, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = __Pyx_PyInt_From_long(__pyx_v_lenpropen); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 78, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 78, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_2);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2);
  __Pyx_INCREF(__pyx_int_6);
  __Pyx_GIVEREF(__pyx_int_6);
  PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_int_6);
  __pyx_t_2 = 0;
  __pyx_t_2 = NULL;
  if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_3))) {
    __pyx_t_2 = PyMethod_GET_SELF(__pyx_t_3);
    if (likely(__pyx_t_2)) {
      PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);
      __Pyx_INCREF(__pyx_t_2);
      __Pyx_INCREF(function);
      __Pyx_DECREF_SET(__pyx_t_3, function);
    }
  }
  __pyx_t_1 = (__pyx_t_2) ? __Pyx_PyObject_Call2Args(__pyx_t_3, __pyx_t_2, __pyx_t_4) : __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_t_4);
  __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 78, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 78, __pyx_L1_error)
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_propen.rcbuffer->pybuffer, (PyObject*)__pyx_t_5, &__Pyx_TypeInfo_nn___pyx_t_5numpy_double_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
      __pyx_v_propen = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_propen.rcbuffer->pybuffer.buf = NULL;
      __PYX_ERR(0, 78, __pyx_L1_error)
    } else {__pyx_pybuffernd_propen.diminfo[0].strides = __pyx_pybuffernd_propen.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_propen.diminfo[0].shape = __pyx_pybuffernd_propen.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_propen.diminfo[1].strides = __pyx_pybuffernd_propen.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_propen.diminfo[1].shape = __pyx_pybuffernd_propen.rcbuffer->pybuffer.shape[1];
    }
  }
  __pyx_t_5 = 0;
  __pyx_v_propen = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

readint the tutorial here I thought using the decorators @cython.boundscheck(False) # Deactivate bounds checking @cython.wraparound(False) # Deactivate negative indexing. might help, but that just fails to compile altogether

cdef func(input1, input2):
    @cython.wraparound(False)
    @cython.boundscheck(False)
    cdef np.ndarray[np.double_t, ndim=2] array = np.zeros((length, 6))
    ...
    "Some computations"

        array[index] = [val1, val2, val3, val4, val5, val6]
     "..."
   array = array[~np.all(array ==0., axis=1)]
   return array
0 Answers
Related