本文整理汇总了Python中numpy.byte方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.byte方法的具体用法?Python numpy.byte怎么用?Python numpy.byte使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.byte方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _unsigned_subtract
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def _unsigned_subtract(a, b):
"""
Subtract two values where a >= b, and produce an unsigned result
This is needed when finding the difference between the upper and lower
bound of an int16 histogram
"""
# coerce to a single type
signed_to_unsigned = {
np.byte: np.ubyte,
np.short: np.ushort,
np.intc: np.uintc,
np.int_: np.uint,
np.longlong: np.ulonglong
}
dt = np.result_type(a, b)
try:
dt = signed_to_unsigned[dt.type]
except KeyError:
return np.subtract(a, b, dtype=dt)
else:
# we know the inputs are integers, and we are deliberately casting
# signed to unsigned
return np.subtract(a, b, casting='unsafe', dtype=dt)
示例2: _test_type_repr
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def _test_type_repr(self, t):
finfo = np.finfo(t)
last_fraction_bit_idx = finfo.nexp + finfo.nmant
last_exponent_bit_idx = finfo.nexp
storage_bytes = np.dtype(t).itemsize*8
# could add some more types to the list below
for which in ['small denorm', 'small norm']:
# Values from https://en.wikipedia.org/wiki/IEEE_754
constr = np.array([0x00]*storage_bytes, dtype=np.uint8)
if which == 'small denorm':
byte = last_fraction_bit_idx // 8
bytebit = 7-(last_fraction_bit_idx % 8)
constr[byte] = 1 << bytebit
elif which == 'small norm':
byte = last_exponent_bit_idx // 8
bytebit = 7-(last_exponent_bit_idx % 8)
constr[byte] = 1 << bytebit
else:
raise ValueError('hmm')
val = constr.view(t)[0]
val_repr = repr(val)
val2 = t(eval(val_repr))
if not (val2 == 0 and val < 1e-100):
assert_equal(val, val2)
示例3: invert
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def invert(data):
"""
Inverts the byte data it received utilizing an XOR operation.
:param data: A chunk of byte data
:return inverted: The same size of chunked data inverted bitwise
"""
# Convert the bytestring into an integer
intwave = np.fromstring(data, np.int32)
# Invert the integer
intwave = np.invert(intwave)
# Convert the integer back into a bytestring
inverted = np.frombuffer(intwave, np.byte)
# Return the inverted audio data
return inverted
示例4: mix_samples
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def mix_samples(sample_1, sample_2, ratio):
"""
Mixes two samples into each other
:param sample_1: A bytestring containing the first audio source
:param sample_2: A bytestring containing the second audio source
:param ratio: A float which determines the mix-ratio of the two samples (the higher, the louder the first sample)
:return mix: A bytestring containing the two samples mixed together
"""
# Calculate the actual ratios based on the float the function received
(ratio_1, ratio_2) = get_ratios(ratio)
# Convert the two samples to integers
intwave_sample_1 = np.fromstring(sample_1, np.int16)
intwave_sample_2 = np.fromstring(sample_2, np.int16)
# Mix the two samples together based on the calculated ratios
intwave_mix = (intwave_sample_1 * ratio_1 + intwave_sample_2 * ratio_2).astype(np.int16)
# Convert the new mix back to a playable bytestring
mix = np.frombuffer(intwave_mix, np.byte)
return mix
示例5: plot_results
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def plot_results(data, nth_iteration):
"""
Plots the list it receives and cuts off the first ten entries to circumvent the plotting of initial silence
:param data: A list of data to be plotted
:param nth_iteration: Used for the label of the x axis
"""
# Plot the data
plt.plot(data[10:])
# Label the axes
plt.xlabel('Time (every {}th {} byte)'.format(nth_iteration, CHUNK))
plt.ylabel('Volume level difference (in dB)')
# Calculate and output the absolute median difference level
plt.suptitle('Difference - Median (in dB): {}'.format(np.round(np.fabs(np.median(data)), decimals=5)), fontsize=14)
# Display the plotted graph
plt.show()
示例6: _unsigned_subtract
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def _unsigned_subtract(a, b):
"""
Subtract two values where a >= b, and produce an unsigned result
This is needed when finding the difference between the upper and lower
bound of an int16 histogram
"""
# coerce to a single type
signed_to_unsigned = {
np.byte: np.ubyte,
np.short: np.ushort,
np.intc: np.uintc,
np.int_: np.uint,
np.longlong: np.ulonglong
}
dt = np.result_type(a, b)
try:
dt = signed_to_unsigned[dt.type]
except KeyError: # pragma: no cover
return np.subtract(a, b, dtype=dt)
else:
# we know the inputs are integers, and we are deliberately casting
# signed to unsigned
return np.subtract(a, b, casting='unsafe', dtype=dt)
示例7: _test_type_repr
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def _test_type_repr(self, t):
finfo = np.finfo(t)
last_fraction_bit_idx = finfo.nexp + finfo.nmant
last_exponent_bit_idx = finfo.nexp
storage_bytes = np.dtype(t).itemsize*8
# could add some more types to the list below
for which in ['small denorm', 'small norm']:
# Values from http://en.wikipedia.org/wiki/IEEE_754
constr = np.array([0x00]*storage_bytes, dtype=np.uint8)
if which == 'small denorm':
byte = last_fraction_bit_idx // 8
bytebit = 7-(last_fraction_bit_idx % 8)
constr[byte] = 1 << bytebit
elif which == 'small norm':
byte = last_exponent_bit_idx // 8
bytebit = 7-(last_exponent_bit_idx % 8)
constr[byte] = 1 << bytebit
else:
raise ValueError('hmm')
val = constr.view(t)[0]
val_repr = repr(val)
val2 = t(eval(val_repr))
if not (val2 == 0 and val < 1e-100):
assert_equal(val, val2)
示例8: managed_geotiff_shapefile_dir
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def managed_geotiff_shapefile_dir():
"""Creates a temp dir with a globally contiguous shapefile and geotiff"""
with TestGeodataManager() as tgm:
array = np.array([[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1],
[1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1],
[1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1],
[1, 2, 1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1],
[1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]], dtype=np.byte)
tgm.create_temp_tiff("temp.tif", np.transpose(array, (0, 2, 1)))
tgm.create_100x100_shp("temp.shp")
yield tgm
示例9: _test_type_repr
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def _test_type_repr(self, t):
finfo=np.finfo(t)
last_fraction_bit_idx = finfo.nexp + finfo.nmant
last_exponent_bit_idx = finfo.nexp
storage_bytes = np.dtype(t).itemsize*8
# could add some more types to the list below
for which in ['small denorm', 'small norm']:
# Values from http://en.wikipedia.org/wiki/IEEE_754
constr = np.array([0x00]*storage_bytes, dtype=np.uint8)
if which == 'small denorm':
byte = last_fraction_bit_idx // 8
bytebit = 7-(last_fraction_bit_idx % 8)
constr[byte] = 1<<bytebit
elif which == 'small norm':
byte = last_exponent_bit_idx // 8
bytebit = 7-(last_exponent_bit_idx % 8)
constr[byte] = 1<<bytebit
else:
raise ValueError('hmm')
val = constr.view(t)[0]
val_repr = repr(val)
val2 = t(eval(val_repr))
if not (val2 == 0 and val < 1e-100):
assert_equal(val, val2)
示例10: makePathFromArrays
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def makePathFromArrays(points, tags, contours):
n_contours = len(contours)
n_points = len(tags)
assert len(points) >= n_points
assert points.shape[1:] == (2,)
if points.dtype != numpy.long:
points = numpy.floor(points + [0.5, 0.5])
points = points.astype(numpy.long)
assert tags.dtype == numpy.byte
assert contours.dtype == numpy.short
path = objc.objc_object(
c_void_p=_makePathFromArrays(
n_contours,
n_points,
points.ctypes.data_as(FT_Vector_p),
tags.ctypes.data_as(c_char_p),
contours.ctypes.data_as(c_short_p)))
# See comment in makePathFromOutline()
path.release()
return path
示例11: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def __init__(self, glyphName, masterModel, masterPoints, contours, tags, components, getSubGlyph):
self.model, masterPoints = masterModel.getSubModel(masterPoints)
masterPoints = [numpy.array(pts, coordinateType) for pts in masterPoints]
try:
self.deltas = self.model.getDeltas(masterPoints)
except ValueError:
# outlines are not compatible, fall back to the default master
print(f"Glyph '{glyphName}' is not interpolatable", file=sys.stderr)
self.deltas = [masterPoints[self.model.reverseMapping[0]]]
if components:
self._contours = None
self._tags = None
else:
self._contours = numpy.array(contours, numpy.short)
self._tags = numpy.array(tags, numpy.byte)
self.components = components
self._getSubGlyph = getSubGlyph
self.varLocation = {}
self._points = None
示例12: gs_from_np
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def gs_from_np(dtype):
dtype = np.dtype(dtype)
if dtype == np.byte:
return gxa.GS_BYTE
elif dtype == np.ubyte:
return gxa.GS_UBYTE
elif dtype == np.int16:
return gxa.GS_SHORT
elif dtype == np.uint16:
return gxa.GS_USHORT
elif dtype == np.int32:
return gxa.GS_LONG
elif dtype == np.uint32:
return gxa.GS_ULONG
elif dtype == np.int64:
return gxa.GS_LONG64
elif dtype == np.uint64:
return gxa.GS_ULONG64
elif dtype == np.float32:
return gxa.GS_FLOAT
elif dtype == np.float64:
return gxa.GS_DOUBLE
else:
raise gxa.GXAPIError("Numpy array type does not map to one of the supported GS_TYPES");
示例13: hash_array
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def hash_array(array):
"""Compute hash of a NumPy array by hashing data as a byte sequence.
Args:
array (array): NumPy array to compute hash of.
Returns:
hash (int): Computed hash as an integer.
"""
if XXHASH_AVAILABLE:
# If fast Python wrapper of fast xxhash implementation is available use
# in preference to built in hash function
h = xxhash.xxh64()
# Update hash by viewing array as byte sequence - no copy required
h.update(array.view(np.byte).data)
# Also update hash by array dtype, shape and strides to avoid clashes
# between different views of same array
h.update(bytes(f'{array.dtype}{array.shape}{array.strides}', 'utf-8'))
return h.intdigest()
else:
# Evaluate built-in hash function on *copy* of data as a byte sequence
return hash(array.tobytes())
示例14: batch_make_att_masks
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def batch_make_att_masks(node_list, tree_decoder = None, walker = None, dtype=np.byte):
if walker is None:
walker = OnehotBuilder()
if tree_decoder is None:
tree_decoder = create_tree_decoder()
true_binary = np.zeros((len(node_list), cmd_args.max_decode_steps, DECISION_DIM), dtype=dtype)
rule_masks = np.zeros((len(node_list), cmd_args.max_decode_steps, DECISION_DIM), dtype=dtype)
for i in range(len(node_list)):
node = node_list[i]
tree_decoder.decode(node, walker)
true_binary[i, np.arange(walker.num_steps), walker.global_rule_used[:walker.num_steps]] = 1
true_binary[i, np.arange(walker.num_steps, cmd_args.max_decode_steps), -1] = 1
for j in range(walker.num_steps):
rule_masks[i, j, walker.mask_list[j]] = 1
rule_masks[i, np.arange(walker.num_steps, cmd_args.max_decode_steps), -1] = 1.0
return true_binary, rule_masks
示例15: process_chunk
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import byte [as 别名]
def process_chunk(smiles_list):
grammar = parser.Grammar(cmd_args.grammar_file)
cfg_tree_list = []
for smiles in smiles_list:
ts = parser.parse(smiles, grammar)
assert isinstance(ts, list) and len(ts) == 1
n = AnnotatedTree2MolTree(ts[0])
cfg_tree_list.append(n)
walker = OnehotBuilder()
tree_decoder = create_tree_decoder()
onehot, masks = batch_make_att_masks(cfg_tree_list, tree_decoder, walker, dtype=np.byte)
return (onehot, masks)