Package ivs :: Package aux :: Module numpyctypes
[hide private]
[frames] | no frames]

Module numpyctypes

source code

Module to convert a numpy array to a ctypes struct. This struct can then be passed to a native C library.

Author: Joris De Ridder

Functions [hide private]
ctypes structure
c_ndarray(a, dtype=None, ndim=None, shape=None, requirements=None)
Returns a ctypes structure of the array 'a' containing the arrays info (data, shape, strides, ndim).
source code
Variables [hide private]
  ctypesDict = {'d': C.c_double, 'b': C.c_char, 'h': C.c_short, ...
Function Details [hide private]

c_ndarray(a, dtype=None, ndim=None, shape=None, requirements=None)

source code 

Returns a ctypes structure of the array 'a' containing the arrays info (data, shape, strides, ndim). A check is made to ensure that the array has the specified dtype and requirements.

Example:

>>> myArray = np.arange(10.0)
>>> myCstruct = c_ndarray(myArray, dtype=np.double, ndim = 3, shape = (4,3,2), 
...                       requirements = ['c_contiguous'])
Parameters:
  • a (ndarray) - the numpy array to be converted
  • dtype (numpy dtype) - the required dtype of the array, convert if it doesn't match
  • ndim (integer) - the required number of axes of the array, complain if it doesn't match
  • shape (tuple) - required shape of the array, complain if it doesn't match
  • requirements (list) - "ensurearray", "aligned", "fortran", "f_contiguous", or "c_contiguous". Convert if it doesn't match.
Returns: ctypes structure
ctypes structure with the fields:
  • data: pointer to the data : the type is determined with the dtype of the array, and with ctypesDict.
  • shape: pointer to long array : size of each of the dimensions
  • strides: pointer to long array : strides in elements (not bytes)

Variables Details [hide private]

ctypesDict

Value:
{'d': C.c_double, 'b': C.c_char, 'h': C.c_short, 'i': C.c_int, 'l': C.\
c_long, 'q': C.c_longlong, 'B': C.c_ubyte, 'H': C.c_ushort, 'I': C.c_u\
int, 'L': C.c_ulong, 'Q': C.c_ulonglong}