本文整理汇总了Python中numpy.packbits方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.packbits方法的具体用法?Python numpy.packbits怎么用?Python numpy.packbits使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.packbits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lsb_deinterleave_bytes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def lsb_deinterleave_bytes(carrier, num_bits, num_lsb, byte_depth=1):
"""
Deinterleave num_bits bits from the num_lsb LSBs of carrier.
:param carrier: carrier bytes
:param num_bits: number of num_bits to retrieve
:param num_lsb: number of least significant bits to use
:param byte_depth: byte depth of carrier values
:return: The deinterleaved bytes
"""
plen = roundup(num_bits / num_lsb)
carrier_dtype = byte_depth_to_dtype[byte_depth]
payload_bits = np.unpackbits(
np.frombuffer(carrier, dtype=carrier_dtype, count=plen).view(np.uint8)
).reshape(plen, 8 * byte_depth)[:, 8 * byte_depth - num_lsb: 8 * byte_depth]
return np.packbits(payload_bits).tobytes()[: num_bits // 8]
示例2: test_packbits_empty_with_axis
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def test_packbits_empty_with_axis():
# Original shapes and lists of packed shapes for different axes.
shapes = [
((0,), [(0,)]),
((10, 20, 0), [(2, 20, 0), (10, 3, 0), (10, 20, 0)]),
((10, 0, 20), [(2, 0, 20), (10, 0, 20), (10, 0, 3)]),
((0, 10, 20), [(0, 10, 20), (0, 2, 20), (0, 10, 3)]),
((20, 0, 0), [(3, 0, 0), (20, 0, 0), (20, 0, 0)]),
((0, 20, 0), [(0, 20, 0), (0, 3, 0), (0, 20, 0)]),
((0, 0, 20), [(0, 0, 20), (0, 0, 20), (0, 0, 3)]),
((0, 0, 0), [(0, 0, 0), (0, 0, 0), (0, 0, 0)]),
]
for dt in '?bBhHiIlLqQ':
for in_shape, out_shapes in shapes:
for ax, out_shape in enumerate(out_shapes):
a = np.empty(in_shape, dtype=dt)
b = np.packbits(a, axis=ax)
assert_equal(b.dtype, np.uint8)
assert_equal(b.shape, out_shape)
示例3: update
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def update(self):
if self.inky_colour is None:
raise RuntimeError("You must specify which colour of Inky pHAT you're using: inkyphat.set_colour('red', 'black' or 'yellow')")
self._display_init()
x1, x2 = self.update_x1, self.update_x2
y1, y2 = self.update_y1, self.update_y2
region = self.buffer[y1:y2, x1:x2]
if self.v_flip:
region = numpy.fliplr(region)
if self.h_flip:
region = numpy.flipud(region)
buf_red = numpy.packbits(numpy.where(region == RED, 1, 0)).tolist()
if self.inky_version == 1:
buf_black = numpy.packbits(numpy.where(region == 0, 0, 1)).tolist()
else:
buf_black = numpy.packbits(numpy.where(region == BLACK, 0, 1)).tolist()
self._display_update(buf_black, buf_red)
self._display_fini()
示例4: decode_long_array
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def decode_long_array(
long_array: numpy.ndarray, size: int, dense=True
) -> numpy.ndarray:
"""
Decode an long array (from BlockStates or Heightmaps)
:param long_array: Encoded long array
:param size: int: The expected size of the returned array
:return: Decoded array as numpy array
"""
long_array = long_array.astype(">q")
bits_per_entry = (len(long_array) * 64) // size
bits = numpy.unpackbits(long_array[::-1].astype(">i8").view("uint8"))
if not dense:
entry_per_long = 64 // bits_per_entry
bits = bits.reshape(-1, 64)[:, -entry_per_long * bits_per_entry :]
return numpy.packbits(
numpy.pad(
bits.reshape(-1, bits_per_entry)[-size:, :],
[(0, 0), (16 - bits_per_entry, 0)],
"constant",
)
).view(dtype=">h")[::-1]
示例5: decode
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def decode(self, img):
"""
Decode the image from internal image format.
"""
assert len(img.shape) == 1
assert img.shape[0] >= (self._h * self._w / 8)
assert img.shape[0] % self._align == 0
img_8b_1d = np.unpackbits(img) * 255 # to 8bit gray scale.
img_8b_1d_trimmed = img_8b_1d[0: (self._h * self._w)]
img_8b_2d = np.reshape(img_8b_1d_trimmed, (self._h, self._w))
return img_8b_2d
# def convert(self, img):
# return np.packbits(img)
示例6: show
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def show(self, busy_wait=True):
"""Show buffer on display.
:param bool busy_wait: If True, wait for display update to finish before returning, default: `True`.
"""
region = self.buf
if self.v_flip:
region = numpy.fliplr(region)
if self.h_flip:
region = numpy.flipud(region)
if self.rotation:
region = numpy.rot90(region, self.rotation // 90)
buf_a = numpy.packbits(numpy.where(region == BLACK, 0, 1)).tolist()
buf_b = numpy.packbits(numpy.where(region == RED, 1, 0)).tolist()
self._update(buf_a, buf_b, busy_wait=busy_wait)
示例7: save_mask
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def save_mask (path, mask):
shape = np.array(list(mask.shape), dtype=np.uint32)
total = mask.size
totalx = (total +7 )/ 8 * 8
if totalx == total:
padded = mask
else:
padded = np.zeros((totalx,), dtype=np.uint8)
padded[:total] = np.reshape(mask, (total,))
pass
padded = np.reshape(padded, (totalx/8, 8))
print padded.shape
packed = np.packbits(padded)
print packed.shape
np.savez_compressed(path, shape, packed)
pass
示例8: save_mask
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def save_mask (path, mask):
shape = np.array(list(mask.shape), dtype=np.uint32)
total = mask.size
totalx = (total +7 )// 8 * 8
if totalx == total:
padded = mask
else:
padded = np.zeros((totalx,), dtype=np.uint8)
padded[:total] = np.reshape(mask, (total,))
pass
padded = np.reshape(padded, (totalx//8, 8))
#print padded.shape
packed = np.packbits(padded)
#print packed.shape
np.savez_compressed(path, shape, packed)
pass
示例9: GenerateSample
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def GenerateSample(filename, code_shape, layer_depth):
# {0, +1} binary codes.
# No conversion since the output file is expected to store
# codes using {0, +1} codes (and not {-1, +1}).
code = synthetic_model.GenerateSingleCode(code_shape)
code = np.round(code)
# Reformat the code so as to be compatible with what is generated
# by the image encoder.
# The image encoder generates a tensor of size:
# iteration_count x batch_size x height x width x iteration_depth.
# Here: batch_size = 1
if code_shape[-1] % layer_depth != 0:
raise ValueError('Number of layers is not an integer')
height = code_shape[0]
width = code_shape[1]
code = code.reshape([1, height, width, -1, layer_depth])
code = np.transpose(code, [3, 0, 1, 2, 4])
int_codes = code.astype(np.int8)
exported_codes = np.packbits(int_codes.reshape(-1))
output = io.BytesIO()
np.savez_compressed(output, shape=int_codes.shape, codes=exported_codes)
with tf.gfile.FastGFile(filename, 'wb') as code_file:
code_file.write(output.getvalue())
示例10: encode_1bit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def encode_1bit(values):
return np.packbits(values.ravel())
示例11: encode_1bit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def encode_1bit(values):
"""Encodes values using 1 bit per sample, packing the result into bytes."""
bitvalues = encode_1bit_base(values.reshape(-1, 8))
return np.packbits(bitvalues[:, ::-1])
示例12: encode_1bit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def encode_1bit(values):
"""Encodes values using 1 bit per sample, packing the result into bytes."""
bitvalues = np.signbit(values.reshape(-1, 8)).view(np.uint8)
return np.packbits(bitvalues[:, ::-1])
示例13: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def __init__(self, words, header=None, *, sample_shape=(1,), bps=2,
fanout=1, magnitude_bit=None, complex_data=False):
if header is not None:
magnitude_bit = header['magnitude_bit']
bps = 2 if magnitude_bit.any() else 1
ta = header.track_assignment
if bps == 1 or np.all(magnitude_bit[ta] == [False, True]):
magnitude_bit = None
else:
magnitude_bit = (np.packbits(magnitude_bit)
.view(header.stream_dtype).item())
ntrack = header.ntrack
fanout = header.fanout
sample_shape = (ntrack // (bps * fanout),)
self._nbytes = header.payload_nbytes
else:
ntrack = sample_shape[0] * bps * fanout
magnitude_bit = None
self._dtype_word = MARK4_DTYPES[ntrack]
self.fanout = fanout
super().__init__(words, sample_shape=sample_shape,
bps=bps, complex_data=complex_data)
self._coder = (self.sample_shape.nchan,
(self.bps if magnitude_bit is None else magnitude_bit),
self.fanout)
示例14: _pandas_to_bucket
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def _pandas_to_bucket(df, symbol, initial_image):
rtn = {SYMBOL: symbol, VERSION: CHUNK_VERSION_NUMBER, COLUMNS: {}, COUNT: len(df)}
end = to_dt(df.index[-1].to_pydatetime())
if initial_image:
if 'index' in initial_image:
start = min(to_dt(df.index[0].to_pydatetime()), initial_image['index'])
else:
start = to_dt(df.index[0].to_pydatetime())
image_start = initial_image.get('index', start)
rtn[IMAGE_DOC] = {IMAGE_TIME: image_start, IMAGE: initial_image}
final_image = TickStore._pandas_compute_final_image(df, initial_image, end)
else:
start = to_dt(df.index[0].to_pydatetime())
final_image = {}
rtn[END] = end
rtn[START] = start
logger.warning("NB treating all values as 'exists' - no longer sparse")
rowmask = Binary(lz4_compressHC(np.packbits(np.ones(len(df), dtype='uint8')).tostring()))
index_name = df.index.names[0] or "index"
if PD_VER < '0.23.0':
recs = df.to_records(convert_datetime64=False)
else:
recs = df.to_records()
for col in df:
array = TickStore._ensure_supported_dtypes(recs[col])
col_data = {
DATA: Binary(lz4_compressHC(array.tostring())),
ROWMASK: rowmask,
DTYPE: TickStore._str_dtype(array.dtype),
}
rtn[COLUMNS][col] = col_data
rtn[INDEX] = Binary(
lz4_compressHC(np.concatenate(
([recs[index_name][0].astype('datetime64[ms]').view('uint64')],
np.diff(
recs[index_name].astype('datetime64[ms]').view('uint64')))).tostring()))
return rtn, final_image
示例15: test_packbits
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import packbits [as 别名]
def test_packbits():
# Copied from the docstring.
a = [[[1, 0, 1], [0, 1, 0]],
[[1, 1, 0], [0, 0, 1]]]
for dt in '?bBhHiIlLqQ':
arr = np.array(a, dtype=dt)
b = np.packbits(arr, axis=-1)
assert_equal(b.dtype, np.uint8)
assert_array_equal(b, np.array([[[160], [64]], [[192], [32]]]))
assert_raises(TypeError, np.packbits, np.array(a, dtype=float))