本文整理汇总了Python中numpy.ctypeslib.as_array方法的典型用法代码示例。如果您正苦于以下问题:Python ctypeslib.as_array方法的具体用法?Python ctypeslib.as_array怎么用?Python ctypeslib.as_array使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.ctypeslib
的用法示例。
在下文中一共展示了ctypeslib.as_array方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_reference_cycles
# 需要导入模块: from numpy import ctypeslib [as 别名]
# 或者: from numpy.ctypeslib import as_array [as 别名]
def test_reference_cycles(self):
# related to gh-6511
import ctypes
# create array to work with
# don't use int/long to avoid running into bpo-10746
N = 100
a = np.arange(N, dtype=np.short)
# get pointer to array
pnt = np.ctypeslib.as_ctypes(a)
with np.testing.assert_no_gc_cycles():
# decay the array above to a pointer to its first element
newpnt = ctypes.cast(pnt, ctypes.POINTER(ctypes.c_short))
# and construct an array using this data
b = np.ctypeslib.as_array(newpnt, (N,))
# now delete both, which should cleanup both objects
del newpnt, b
示例2: test_array
# 需要导入模块: from numpy import ctypeslib [as 别名]
# 或者: from numpy.ctypeslib import as_array [as 别名]
def test_array(self):
from ctypes import c_int
pair_t = c_int * 2
a = as_array(pair_t(1, 2))
assert_equal(a.shape, (2,))
assert_array_equal(a, np.array([1, 2]))
a = as_array((pair_t * 3)(pair_t(1, 2), pair_t(3, 4), pair_t(5, 6)))
assert_equal(a.shape, (3, 2))
assert_array_equal(a, np.array([[1, 2], [3, 4], [5, 6]]))
示例3: test_pointer
# 需要导入模块: from numpy import ctypeslib [as 别名]
# 或者: from numpy.ctypeslib import as_array [as 别名]
def test_pointer(self):
from ctypes import c_int, cast, POINTER
p = cast((c_int * 10)(*range(10)), POINTER(c_int))
a = as_array(p, shape=(10,))
assert_equal(a.shape, (10,))
assert_array_equal(a, np.arange(10))
a = as_array(p, shape=(2, 5))
assert_equal(a.shape, (2, 5))
assert_array_equal(a, np.arange(10).reshape((2, 5)))
# shape argument is required
assert_raises(TypeError, as_array, p)
示例4: test_struct_array_pointer
# 需要导入模块: from numpy import ctypeslib [as 别名]
# 或者: from numpy.ctypeslib import as_array [as 别名]
def test_struct_array_pointer(self):
from ctypes import c_int16, Structure, pointer
class Struct(Structure):
_fields_ = [('a', c_int16)]
Struct3 = 3 * Struct
c_array = (2 * Struct3)(
Struct3(Struct(a=1), Struct(a=2), Struct(a=3)),
Struct3(Struct(a=4), Struct(a=5), Struct(a=6))
)
expected = np.array([
[(1,), (2,), (3,)],
[(4,), (5,), (6,)],
], dtype=[('a', np.int16)])
def check(x):
assert_equal(x.dtype, expected.dtype)
assert_equal(x, expected)
# all of these should be equivalent
check(as_array(c_array))
check(as_array(pointer(c_array), shape=()))
check(as_array(pointer(c_array[0]), shape=(2,)))
check(as_array(pointer(c_array[0][0]), shape=(2, 3)))
示例5: c_int_vector_to_bool_python
# 需要导入模块: from numpy import ctypeslib [as 别名]
# 或者: from numpy.ctypeslib import as_array [as 别名]
def c_int_vector_to_bool_python(x):
return numpc.as_array(x).copy().astype(bool)
示例6: c_matrix_to_numpy
# 需要导入模块: from numpy import ctypeslib [as 别名]
# 或者: from numpy.ctypeslib import as_array [as 别名]
def c_matrix_to_numpy(x):
"""
Convert a ctypes 2d array (or matrix) into a numpy array for python use
:param x: thing to convert
:return: numpy.ndarray
"""
return numpc.as_array(x).copy()