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
The error occurs on the line:
coefficients = np.zeros([time_len, activations_len,time_len])
I get a helpful error message pointing to the right line. If you don't then it's probable that you've moved or renamed a file during the build process so it can't find the .pyx file when you run it.
The solution is either to change the type of coefficients
to a 3D array, or to create a 2D array with np.zeros
.
I can't reproduce your crash when passing it a 2D array - I just get a ValueError: Buffer has wrong number of dimensions
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.