ValueError: ndarray is not C-contiguous in cython

1,825 views
Skip to first unread message

Zahra Sheikh

unread,
Nov 6, 2014, 8:22:29 AM11/6/14
to cython...@googlegroups.com
Hi all,
I have written the following function in cython to estimate the log-likelihood

    @cython.boundscheck(False)
    @cython.wraparound(False)
    def likelihood(double m,
                   double c,
                   np.ndarray[np.double_t, ndim=1, mode='c'] r_mpc not None,
                   np.ndarray[np.double_t, ndim=1, mode='c'] gtan not None,
                   np.ndarray[np.double_t, ndim=1, mode='c'] gcrs not None,
                   np.ndarray[np.double_t, ndim=1, mode='c'] shear_err not None,
                   np.ndarray[np.double_t, ndim=1, mode='c'] beta not None,
                   double rho_c,
                   np.ndarray[np.double_t, ndim=1, mode='c'] rho_c_sigma not None):
        r_mpc  = np.ascontiguousarray(r_mpc, dtype=np.double)
        gtan     = np.ascontiguousarray(gtan, dtype=np.double)
        gcrs     = np.ascontiguousarray(gcrs, dtype=np.double)
        shear_err= np.ascontiguousarray(shear_err, dtype=np.double)
        beta     = np.ascontiguousarray(beta, dtype=np.double)
        rho_c_over_sigma_c  = np.ascontiguousarray(rho_c_over_sigma_c, dtype=np.double)

        cdef double rscale = rscaleConstM(m, c,rho_c, 200)
   
        cdef Py_ssize_t ngals = r_mpc.shape[0]
   
        cdef np.ndarray[DTYPE_T, ndim=1, mode='c'] gamma_inf = Sh(r_mpc, c, rscale, rho_c_sigma)
        cdef np.ndarray[DTYPE_T, ndim=1, mode='c'] kappa_inf = Kap(r_mpc, c, rscale, rho_c_sigma)
 
   
        cdef double delta = 0.
        cdef double modelg = 0.
        cdef double modsig = 0.
   
        cdef Py_ssize_t i
        cdef DTYPE_T logProb = 0.
   
           
        #calculate logprob
        for i from ngals > i >= 0:
          
            modelg = (beta[i]*gamma_inf[i] / (1 - beta[i]*kappa_inf[i]))
   
            delta = gtan[i] - modelg
            
            modsig = shear_err[i]
   
            logProb = logProb -.5*(delta/modsig)**2  - logsqrt2pi - log(modsig)
   
           
        return logProb

but when I run the compiled version of this function, I get the following error message:

      File "Tools.pyx", line 3, in Tools.likelihood
        def likelihood(double m,
    ValueError: ndarray is not C-contiguous

I even added np.ascontiguousarray(arr, dtype=np.double) to get rid of this error message but didn't work.
I could not quite understand why this problem occurs??!!! I will appreciate to get any useful tips.

Cheers,
Zahra

Yury V. Zaytsev

unread,
Nov 6, 2014, 8:27:57 AM11/6/14
to cython...@googlegroups.com
On Thu, 2014-11-06 at 05:22 -0800, Zahra Sheikh wrote:
>
> I could not quite understand why this problem occurs??!!! I will
> appreciate to get any useful tips.

You should make sure that the arrays are C-contiguous outside of the
function call, not inside, after the error has already occurred.

--
Sincerely yours,
Yury V. Zaytsev


Zahra Sheikh

unread,
Nov 6, 2014, 8:37:55 AM11/6/14
to cython...@googlegroups.com
How could I test that whether they are C-contiguous or not ?

Cheers,
Zahra

Yury V. Zaytsev

unread,
Nov 6, 2014, 8:43:31 AM11/6/14
to cython...@googlegroups.com
On Thu, 2014-11-06 at 05:37 -0800, Zahra Sheikh wrote:
>
>
> How could I test that whether they are C-contiguous or not ?

Check the flags property (myarr.flags) for C_CONTIGUOUS, not
F_CONTIGUOUS.

Eric Moore

unread,
Nov 6, 2014, 9:19:19 AM11/6/14
to cython...@googlegroups.com

On Thu, Nov 6, 2014 at 8:43 AM, Yury V. Zaytsev <yu...@shurup.com> wrote:
On Thu, 2014-11-06 at 05:37 -0800, Zahra Sheikh wrote:
>
>
> How could I test that whether they are C-contiguous or not ?

Check the flags property (myarr.flags) for C_CONTIGUOUS, not
F_CONTIGUOUS.


Since this is a def function, you might want to accept any kind of array_like into the function, then call np.ascontiguousarray and assign the output to a set of typed variables.  This will make the necessary copy if the array is not C contiguous but otherwise won't.  Otherwise, you'll want to do the equivalent of calling this before calling your function.

Eric

Zahra Sheikh

unread,
Nov 6, 2014, 10:26:56 AM11/6/14
to cython...@googlegroups.com
Thanks for the tips.
Reply all
Reply to author
Forward
0 new messages