3

Okay so I followed the instructions on the Cython docs to get numpy arrays to be recognized by cython. Here is the "preamble" of my script

from __future__ import division
import numpy as np 
cimport numpy as np 

DTYPE = np.int 
ctypedef np.int_t DTYPE_t

I'll be honest, I'm not sure exactly what the line ctypedef np.int_t DTYPE_t does, simply because i'm not sure why there's the suffix _t. However it seems to handle the numpy arrays fine now so I'm not complaining. Now the problem lies herein:

I have two files called cell_spks and cell_dirs. These are both text files, for which I posted a gist of both here and here respectively. As you can see they're just 5x8 arrays of numbers. The problem is now when I load these in and try to do manipulations it just WONT work. Here's a gist of my code (warning, its pretty long) and here is my setup file. Now I did not write the big functions, and in fact this is only a snippet of my actual computations, but this is where my error lies so I'm only posting this section. In essence, I need the platemethod function however calling the platemethod function gives me an error "TypeError: Cannot convert numpy.int64 to numpy.ndarray". I'm not sure what's going wrong, why would it try to convert it in the first place?? I'm sorry if this is a rather large question guys.

EDIT: I should mention this function used to be a python script, I "translated" it to cython (to the best of my abilities)

3
  • ctypedef np.int_t DTYPE_t just creates an alias to np.int_t, a type that is meaningful within C. When something is cdef'd you should declare it using such a type. You don't need to use the pair of lines above; you can replace every instance of DTYPE_t with np.int_p and DTYPE with np.int (which is a python object). Dec 4, 2013 at 20:08
  • Can you post some more of the traceback? Specifically, on what line are you getting that error? Dec 4, 2013 at 20:11
  • @HenryGomersall Sorry I took long to answer back. Here is what it spits out
    – faskiat
    Dec 5, 2013 at 18:31

1 Answer 1

4

Your problem is that you have defined IX (and IY and IXY and ...) to be type np.ndarray on line 99. The point about Cython is that you can assign variables to be statically typed, which you do with a cdef, which you have done in those cases. Given the variable is of type np.ndarray, you can't then assign another type to it.

sum(IX) sums along the array and, in this case, returns an object of type numpy.int64, which can't be assigned to IX. You'll need to use a different variable name, which can either be cdef'd (and statically typed) or just a normal python object.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.