本文整理汇总了Python中numba.numpy_support.from_dtype函数的典型用法代码示例。如果您正苦于以下问题:Python from_dtype函数的具体用法?Python from_dtype怎么用?Python from_dtype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了from_dtype函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: typeof_pyval
def typeof_pyval(cls, val):
"""
This is called from numba._dispatcher as a fallback if the native code
cannot decide the type.
"""
if isinstance(val, numpy.ndarray):
# TODO complete dtype mapping
dtype = numpy_support.from_dtype(val.dtype)
ndim = val.ndim
if ndim == 0:
# is array scalar
return numpy_support.from_dtype(val.dtype)
layout = numpy_support.map_layout(val)
aryty = types.Array(dtype, ndim, layout)
return aryty
# The following are handled in the C version for exact type match
# So test these later
elif isinstance(val, INT_TYPES):
return types.int64
elif isinstance(val, float):
return types.float64
elif isinstance(val, complex):
return types.complex128
elif numpy_support.is_arrayscalar(val):
# Array scalar
return numpy_support.from_dtype(numpy.dtype(type(val)))
# Other object
else:
return types.pyobject
示例2: test_two_distinct_arrays
def test_two_distinct_arrays(self):
'''
Test with two arrays of distinct record types
'''
pyfunc = get_two_arrays_distinct
rec1 = numpy_support.from_dtype(recordtype)
rec2 = numpy_support.from_dtype(recordtype2)
cfunc = self.get_cfunc(pyfunc, (rec1[:], rec2[:], types.intp))
for i in range(self.refsample1d.size):
pres = pyfunc(self.refsample1d, self.refsample1d2, i)
cres = cfunc(self.nbsample1d, self.nbsample1d2, i)
self.assertEqual(pres,cres)
示例3: test_two_distinct_records
def test_two_distinct_records(self):
'''
Testing the use of two scalar records of differing type
'''
nbval1 = self.nbsample1d.copy()[0]
nbval2 = self.refsample1d2.copy()[0]
expected = nbval1['a'] + nbval2['f']
nbrecord1 = numpy_support.from_dtype(recordtype)
nbrecord2 = numpy_support.from_dtype(recordtype2)
cfunc = self.get_cfunc(get_two_records_distinct, (nbrecord1, nbrecord2))
got = cfunc(nbval1, nbval2)
self.assertEqual(expected, got)
示例4: test_gufunc
def test_gufunc(self):
datetime_t = from_dtype(np.dtype('datetime64[D]'))
timedelta_t = from_dtype(np.dtype('timedelta64[D]'))
@guvectorize([(datetime_t, datetime_t, timedelta_t[:])], '(),()->()',
target='cuda')
def timediff(start, end, out):
out[0] = end - start
arr1 = np.arange('2005-02', '2006-02', dtype='datetime64[D]')
arr2 = arr1 + np.random.randint(0, 10000, arr1.size)
delta = timediff(arr1, arr2)
self.assertPreciseEqual(delta, arr2 - arr1)
示例5: test_multiple_args_records
def test_multiple_args_records(self):
pyfunc = foobar
mystruct_dt = np.dtype([('p', np.float64),
('row', np.float64),
('col', np.float64)])
mystruct = numpy_support.from_dtype(mystruct_dt)
cres = compile_isolated(pyfunc, [mystruct[:], types.uint64, types.uint64],
return_type=mystruct[:])
cfunc = cres.entry_point
st1 = np.recarray(3, dtype=mystruct_dt)
st1.p = np.arange(st1.size) + 1
st1.row = np.arange(st1.size) + 1
st1.col = np.arange(st1.size) + 1
with self.assertRefCount(st1):
test_fail_args = ((st1, -1, 1), (st1, 1, -1))
for a, b, c in test_fail_args:
with self.assertRaises(OverflowError):
cfunc(a, b, c)
del test_fail_args, a, b, c
gc.collect()
示例6: _evaluate
def _evaluate(self):
# Get argument values
env = self.expr.env
is_local = lambda x: x.startswith("__pd_eval_local_")
call_args = [np.asarray(env.resolve(name, is_local(name)))
for name in self._args]
# Get argument types
call_types = tuple(from_dtype(a.dtype) for a in call_args)
# Check if the expression has already been compiled
cache_key = (self.target, str(self.expr), call_types)
fn = self._func_cache.get(cache_key)
if fn is None:
# Not cached. Compile new one
# Stringify the eval tree and get arg names
nameset = set()
exprstr = _stringify_eval_op_tree(self.expr.terms, nameset)
assert set(self._args) == nameset
function_name = '__numba_pandas_eval_ufunc'
fn = self._compile(exprstr, self._args, call_types, function_name)
self._func_cache[cache_key] = fn
# Execute
return fn(*call_args)
示例7: __call__
def __call__(self, *args, **kwargs):
if (self.neighborhood is not None and
len(self.neighborhood) != args[0].ndim):
raise ValueError("{} dimensional neighborhood specified for {} "
"dimensional input array".format(
len(self.neighborhood), args[0].ndim))
if 'out' in kwargs:
result = kwargs['out']
rdtype = result.dtype
rttype = numpy_support.from_dtype(rdtype)
result_type = types.npytypes.Array(rttype, result.ndim,
numpy_support.map_layout(result))
array_types = tuple([typing.typeof.typeof(x) for x in args])
array_types_full = tuple([typing.typeof.typeof(x) for x in args] +
[result_type])
else:
result = None
array_types = tuple([typing.typeof.typeof(x) for x in args])
array_types_full = array_types
if config.DEBUG_ARRAY_OPT == 1:
print("__call__", array_types, args, kwargs)
(real_ret, typemap, calltypes) = self.get_return_type(array_types)
new_func = self._stencil_wrapper(result, None, real_ret, typemap,
calltypes, *array_types_full)
if result is None:
return new_func.entry_point(*args)
else:
return new_func.entry_point(*(args+(result,)))
示例8: test_record_args
def test_record_args(self):
"""
Testing scalar record value as argument
"""
recval = self.sample1d.copy()[0]
attrs = 'abc'
valtypes = types.float64, types.int32, types.complex64
values = 1.23, 123432, 132j
old_refcnt = sys.getrefcount(recval)
for attr, valtyp, val in zip(attrs, valtypes, values):
expected = getattr(recval, attr)
pyfunc = globals()['get_record_' + attr]
nbrecord = numpy_support.from_dtype(recordtype)
cres = compile_isolated(pyfunc, [nbrecord, valtyp])
cfunc = cres.entry_point
got = cfunc(recval, val)
self.assertEqual(expected, got)
self.assertNotEqual(recval.a, got)
del got, expected
# Check for potential leaks (issue #441)
self.assertEqual(sys.getrefcount(recval), old_refcnt)
示例9: transpose
def transpose(a, b=None):
"""Compute the transpose of 'a' and store it into 'b', if given,
and return it. If 'b' is not given, allocate a new array
and return that.
This implements the algorithm documented in
http://devblogs.nvidia.com/parallelforall/efficient-matrix-transpose-cuda-cc/
:param a: an `np.ndarray` or a `DeviceNDArrayBase` subclass. If already on
the device its stream will be used to perform the transpose (and to copy
`b` to the device if necessary).
"""
# prefer `a`'s stream if
stream = getattr(a, 'stream', 0)
if not b:
cols, rows = a.shape
strides = a.dtype.itemsize * cols, a.dtype.itemsize
b = cuda.cudadrv.devicearray.DeviceNDArray(
(rows, cols),
strides,
dtype=a.dtype,
stream=stream)
dt=nps.from_dtype(a.dtype)
tpb = driver.get_device().MAX_THREADS_PER_BLOCK
# we need to factor available threads into x and y axis
tile_width = int(math.pow(2, math.log(tpb, 2)/2))
tile_height = int(tpb / tile_width)
tile_shape=(tile_height, tile_width + 1)
@cuda.jit
def kernel(input, output):
tile = cuda.shared.array(shape=tile_shape, dtype=dt)
tx = cuda.threadIdx.x
ty = cuda.threadIdx.y
bx = cuda.blockIdx.x * cuda.blockDim.x
by = cuda.blockIdx.y * cuda.blockDim.y
x = by + tx
y = bx + ty
if by+ty < input.shape[0] and bx+tx < input.shape[1]:
tile[ty, tx] = input[by+ty, bx+tx]
cuda.syncthreads()
if y < output.shape[0] and x < output.shape[1]:
output[y, x] = tile[tx, ty]
# one block per tile, plus one for remainders
blocks = int(b.shape[0]/tile_height + 1), int(b.shape[1]/tile_width + 1)
# one thread per tile element
threads = tile_height, tile_width
kernel[blocks, threads, stream](a, b)
return b
示例10: test_multiple_args_records
def test_multiple_args_records(self):
pyfunc = foobar
mystruct_dt = np.dtype([('p', np.float64),
('row', np.float64),
('col', np.float64)])
mystruct = numpy_support.from_dtype(mystruct_dt)
cres = compile_isolated(pyfunc, [mystruct[:], types.uint64, types.uint64],
return_type=mystruct[:])
cfunc = cres.entry_point
st1 = np.recarray(3, dtype=mystruct_dt)
st2 = np.recarray(3, dtype=mystruct_dt)
st1.p = np.arange(st1.size) + 1
st1.row = np.arange(st1.size) + 1
st1.col = np.arange(st1.size) + 1
st2.p = np.arange(st2.size) + 1
st2.row = np.arange(st2.size) + 1
st2.col = np.arange(st2.size) + 1
test_fail_args = ((st1, -1, st2), (st1, st2, -1))
# TypeError is for 2.6
if sys.version_info >= (2, 7):
with self.assertRaises(OverflowError):
for a, b, c in test_fail_args:
cfunc(a, b, c)
else:
with self.assertRaises(TypeError):
for a, b, c in test_fail_args:
cfunc(a, b, c)
示例11: _numba_type_
def _numba_type_(self):
"""
Magic attribute expected by Numba to get the numba type that
represents this object.
"""
dtype = numpy_support.from_dtype(self.dtype)
return types.Array(dtype, self.ndim, 'A')
示例12: test_structure_dtype_with_titles
def test_structure_dtype_with_titles(self):
# the following is the definition of int4 vector type from pyopencl
vecint4 = np.dtype([(('x', 's0'), 'i4'), (('y', 's1'), 'i4'),
(('z', 's2'), 'i4'), (('w', 's3'), 'i4')])
nbtype = numpy_support.from_dtype(vecint4)
self.assertEqual(len(nbtype.fields), len(vecint4.fields))
arr = np.zeros(10, dtype=vecint4)
def pyfunc(a):
for i in range(a.size):
j = i + 1
a[i]['s0'] = j * 2
a[i]['x'] += -1
a[i]['s1'] = j * 3
a[i]['y'] += -2
a[i]['s2'] = j * 4
a[i]['z'] += -3
a[i]['s3'] = j * 5
a[i]['w'] += -4
return a
expect = pyfunc(arr.copy())
cfunc = self.get_cfunc(pyfunc, (nbtype[:],))
got = cfunc(arr.copy())
np.testing.assert_equal(expect, got)
示例13: test_multiple_args_records
def test_multiple_args_records(self):
pyfunc = foobar
mystruct_dt = np.dtype([('p', np.float64),
('row', np.float64),
('col', np.float64)])
mystruct = numpy_support.from_dtype(mystruct_dt)
cres = compile_isolated(pyfunc, [mystruct[:], types.uint64, types.uint64],
return_type=mystruct[:])
cfunc = cres.entry_point
st1 = np.recarray(3, dtype=mystruct_dt)
st1.p = np.arange(st1.size) + 1
st1.row = np.arange(st1.size) + 1
st1.col = np.arange(st1.size) + 1
old_refcnt_st1 = sys.getrefcount(st1)
test_fail_args = ((st1, -1, 1), (st1, 1, -1))
# TypeError is for 2.6
exc_type = OverflowError if sys.version_info >= (2, 7) else TypeError
for a, b, c in test_fail_args:
with self.assertRaises(exc_type):
cfunc(a, b, c)
del test_fail_args, a, b, c
gc.collect()
self.assertEqual(sys.getrefcount(st1), old_refcnt_st1)
示例14: test_usecase1
def test_usecase1(self):
pyfunc = usecase1
# This is an unaligned dtype
mystruct_dt = numpy.dtype([('p', numpy.float64),
('row', numpy.float64),
('col', numpy.float64)])
mystruct = numpy_support.from_dtype(mystruct_dt)
cres = compile_isolated(pyfunc, (mystruct[:], mystruct[:]))
cfunc = cres.entry_point
st1 = numpy.recarray(3, dtype=mystruct_dt)
st2 = numpy.recarray(3, dtype=mystruct_dt)
st1.p = numpy.arange(st1.size) + 1
st1.row = numpy.arange(st1.size) + 1
st1.col = numpy.arange(st1.size) + 1
st2.p = numpy.arange(st2.size) + 1
st2.row = numpy.arange(st2.size) + 1
st2.col = numpy.arange(st2.size) + 1
expect1 = st1.copy()
expect2 = st2.copy()
got1 = expect1.copy()
got2 = expect2.copy()
pyfunc(expect1, expect2)
cfunc(got1, got2)
self.assertTrue(numpy.all(expect1 == got1))
self.assertTrue(numpy.all(expect2 == got2))
示例15: resolve_value_type
def resolve_value_type(self, val):
"""
Return the numba type of a Python value
Return None if fail to type.
"""
tp = self.resolve_data_type(val)
if tp is not None:
return tp
if isinstance(val, types.ExternalFunction):
return val
if isinstance(val, type) and issubclass(val, BaseException):
return types.ExceptionType(val)
if isinstance(val, numpy.dtype):
tp = numpy_support.from_dtype(val)
return types.DType(tp)
try:
# Try to look up target specific typing information
return self.get_global_type(val)
except KeyError:
pass
return None