Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The corresponding ctypes type of a numpy.dtype?

Tags:

numpy

ctypes

If I have a numpy ndarray with a certain dtype, how do I know what is the corresponding ctypes type?

For example, if I have a ndarray, I can do the following to convert it to a shared array:

import multiprocessing as mp
import numpy as np
import ctypes
x_np = np.random.rand(10, 10)
x_mp = mp.Array(ctypes.c_double, x_np)

However, I have to specify c_double here. It works if I don't specify the exact same type, but I would like to keep the type the same. How should I find out the ctypes type of the ndarray x_np automatically, at least for some common elementary data types?

like image 811
shaoyl85 Avatar asked Oct 20 '15 21:10

shaoyl85


People also ask

Does numpy use Ctypes?

ndarray. ctypes. An object to simplify the interaction of the array with the ctypes module.

What is the data type of numpy?

The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float for float and int for integer.

What is U32 data type in numpy?

dtype='<U32' is a little-endian 32 character string. The documentation on dtypes goes into more depth about each of the character. 'U' Unicode string. Several kinds of strings can be converted.


2 Answers

This is now supported by numpy.ctypeslib.as_ctypes_type(dtype):

import numpy as np
x_np = np.random.rand(10, 10)
np.ctypeslib.as_ctypes_type(x_np.dtype)

Gives ctypes.c_double, as expected.

like image 98
Angus G Avatar answered Oct 20 '22 02:10

Angus G


There is actually a way to do this that's built into Numpy:

x_np = np.random.rand(10, 10)
typecodes = np.ctypeslib._get_typecodes()
typecodes[x_np.__array_interface__['typestr']]

Output:

ctypes.c_double

The caveat is that the np.ctypeslib._get_typecodes function is marked as private (ie it's name starts with _). However, it doesn't seem like its implementation has changed in some time, so you can probably use it fairly reliably.

Alternatively, the implementation of _get_typecodes is pretty short, so you could just also copy the whole function over to your own code:

import ctypes
import numpy as np

def get_typecodes():
    ct = ctypes
    simple_types = [
        ct.c_byte, ct.c_short, ct.c_int, ct.c_long, ct.c_longlong,
        ct.c_ubyte, ct.c_ushort, ct.c_uint, ct.c_ulong, ct.c_ulonglong,
        ct.c_float, ct.c_double,
    ]

    return {np.dtype(ctype).str: ctype for ctype in simple_types}
like image 37
tel Avatar answered Oct 20 '22 04:10

tel