Cython ValueError: Buffer has wrong number of dimensions (expected 2, got 3)

Viewed 2960

There are some similar questions, but none of them addresses my issue, so I am posting a new one here.

Cython is giving me an error when I try to give a function a 3-dimensional numpy array as input, telling me: "ValueError: Buffer has wrong number of dimensions (expected 2, got 3)". But when I give it a 2-dimensional array, it crashes (python stops responding, I think this is because I am trying to perform 3-dimensional matrix operations on a 2-dimensional array).

Then, I tried to typeset the input as a 3 dimensional array, but the function is still expecting a 2-dimensional array. I thought there might be something wrong with my code, but when I got rid of the cython variable declarations and ran it as a python file, everything was fine.

Here is the function declaration:

def isfc(np.ndarray[double, ndim=3] multi_activations, int gaussian_variance):
  #cython variable declaration
  cdef int time_len, activations_len, subj_num, timepoint, subj
  cdef np.ndarray[double, ndim=2] correlations_vector, normalized_activations, coefficients,normalized_sum_activations
  cdef np.ndarray[double, ndim=3] c_activations, activations_sum, correlations_mean
  cdef np.ndarray[double, ndim=4] correlations
  cdef np.ndarray gaussian_array, coefficients_sum, coefficient, sigma_activations, sigma_activations_sum

  #assign initial parameters
  **subj_num, activations_len, time_len= multi_activations.shape[0],multi_activations.shape[1],multi_activations.shape[2]**
  coefficients_sum = np.zeros(time_len)
  correlations= np.zeros([subj_num, time_len,activations_len,activations_len])
  correlations_vector = np.zeros([time_len,(activations_len * (activations_len-1) / 2)])
  coefficients = np.zeros([time_len, activations_len,time_len])
  gaussian_array = np.array([exp(-timepoint**2/2/gaussian_variance)/sqrt(2*pi*gaussian_variance) for timepoint in range(-time_len+1,time_len)])
  **c_activations = np.array(multi_activations)**

The input in question is multi_activations, and it's only used on the lines marked with ** before it's copied to a 3-dimensional cython buffer.

I have narrowed down the error to be at the function call, specifically when I pass a 3-dimensional array to this function as the multi_activations input. I get an error at the function call, not within the function. It's simply a buffer size mismatch for the input parameter. Any help would be greatly appreciated

1 Answers
Related