Re: [Numpy-discussion] Getting a numpy array from a ctype pointer
x = np.frombuffer(int_asbuffer(C.addressof(x.contents), n*8)) I'll go with your faster solution. Very good. Thank you very much. Paulo ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Getting a numpy array from a ctype pointer
Hello, I am writing some code interfacing C and Python using ctypes. In a callback function (in Python) I get in a parameter x which is c_double pointer and a parameter n which is c_int representing the length of the array. How can I transform this information into a numpy array? Paulo ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Getting a numpy array from a ctype pointer
Paulo J. S. Silva wrote: Hello, I am writing some code interfacing C and Python using ctypes. In a callback function (in Python) I get in a parameter x which is c_double pointer and a parameter n which is c_int representing the length of the array. How can I transform this information into a numpy array? Something like this may work: from numpy import ctypeslib r = ctypeslib.as_array(x._type_ * n) If that doesn't work, then you can create an array from an arbitrary buffer or any object that exposes the __array_interface__ attribute. So, if you can get the address of c_double then you can use it as the data for the ndarray. Ask if you need more help. -Travis ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Getting a numpy array from a ctype pointer
Em Qui, 2008-09-04 às 15:01 -0500, Travis E. Oliphant escreveu: Paulo J. S. Silva wrote: Something like this may work: from numpy import ctypeslib r = ctypeslib.as_array(x._type_ * n) Unfortunately, it didn't work. If that doesn't work, then you can create an array from an arbitrary buffer or any object that exposes the __array_interface__ attribute. So, if you can get the address of c_double then you can use it as the data for the ndarray. Ask if you need more help. I think I need help. I discovered how to create a Python string from the pointer using ctypes' string_at function: r = C.string_at(x, n*C.sizeof(x._type_)) Now I can use numpy fromstring to get a array version of it. However this is not enough for my application as fromstring actually copies the data, hence change to the array are not reflected in the original array that goes back to the Fortran code. Any hints? Paulo ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Getting a numpy array from a ctype pointer
After some trial and erros, I found a solution, described below. Is this the best one? Looks a little convoluted to me (C represents ctypes module and np numpy): Array = n*C.c_double x = Array.from_address(C.addressof(x.contents)) x = np.ctypeslib.as_array(x) Thanks, Paulo Em Qui, 2008-09-04 às 17:31 -0400, Paulo J. S. Silva escreveu: Em Qui, 2008-09-04 às 15:01 -0500, Travis E. Oliphant escreveu: Paulo J. S. Silva wrote: Something like this may work: from numpy import ctypeslib r = ctypeslib.as_array(x._type_ * n) Unfortunately, it didn't work. If that doesn't work, then you can create an array from an arbitrary buffer or any object that exposes the __array_interface__ attribute. So, if you can get the address of c_double then you can use it as the data for the ndarray. Ask if you need more help. I think I need help. I discovered how to create a Python string from the pointer using ctypes' string_at function: r = C.string_at(x, n*C.sizeof(x._type_)) Now I can use numpy fromstring to get a array version of it. However this is not enough for my application as fromstring actually copies the data, hence change to the array are not reflected in the original array that goes back to the Fortran code. Any hints? Paulo ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Getting a numpy array from a ctype pointer
Paulo J. S. Silva wrote: After some trial and erros, I found a solution, described below. Is this the best one? Looks a little convoluted to me (C represents ctypes module and np numpy): Array = n*C.c_double x = Array.from_address(C.addressof(x.contents)) x = np.ctypeslib.as_array(x) That's a pretty simple approach. There is a faster approach which uses the undocumented function int_asbuffer function from numpy.core.multiarray (i.e. from numpy.core.multiarray import int_asbuffer) Then: x = np.frombuffer(int_asbuffer(C.addressof(x.contents), n*8)) If you don't like the hard-coded '8', then you can get that number from np.dtype(float).itemsize There may also be a way to get it from ctypes. -Travis ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion