本文整理匯總了Python中numpy.unpackbits方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.unpackbits方法的具體用法?Python numpy.unpackbits怎麽用?Python numpy.unpackbits使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.unpackbits方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_unpackbits_empty_with_axis
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def test_unpackbits_empty_with_axis():
# Lists of packed shapes for different axes and unpacked shapes.
shapes = [
([(0,)], (0,)),
([(2, 24, 0), (16, 3, 0), (16, 24, 0)], (16, 24, 0)),
([(2, 0, 24), (16, 0, 24), (16, 0, 3)], (16, 0, 24)),
([(0, 16, 24), (0, 2, 24), (0, 16, 3)], (0, 16, 24)),
([(3, 0, 0), (24, 0, 0), (24, 0, 0)], (24, 0, 0)),
([(0, 24, 0), (0, 3, 0), (0, 24, 0)], (0, 24, 0)),
([(0, 0, 24), (0, 0, 24), (0, 0, 3)], (0, 0, 24)),
([(0, 0, 0), (0, 0, 0), (0, 0, 0)], (0, 0, 0)),
]
for in_shapes, out_shape in shapes:
for ax, in_shape in enumerate(in_shapes):
a = np.empty(in_shape, dtype=np.uint8)
b = np.unpackbits(a, axis=ax)
assert_equal(b.dtype, np.uint8)
assert_equal(b.shape, out_shape)
示例2: decode
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def decode(self, buf, out=None):
# normalise input
enc = ensure_ndarray(buf).view('u1')
# flatten to simplify implementation
enc = enc.reshape(-1, order='A')
# find out how many bits were padded
n_bits_padded = int(enc[0])
# apply decoding
dec = np.unpackbits(enc[1:])
# remove padded bits
if n_bits_padded:
dec = dec[:-n_bits_padded]
# view as boolean array
dec = dec.view(bool)
# handle destination
return ndarray_copy(dec, out)
示例3: lsb_deinterleave_bytes
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [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]
示例4: decode_long_array
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [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: read
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def read(filename):
with gzip.open(filename, "rb") as f:
header_bytes = f.read(CHUNK_HEADER_SIZE)
data_size, board_size, input_planes, is_test = struct.unpack(CHUNK_HEADER_FORMAT, header_bytes)
position_dims = data_size * board_size * board_size * input_planes
next_move_dims = data_size * board_size * board_size
# the +7 // 8 compensates for numpy's bitpacking padding
packed_position_bytes = f.read((position_dims + 7) // 8)
packed_next_move_bytes = f.read((next_move_dims + 7) // 8)
# should have cleanly finished reading all bytes from file!
assert len(f.read()) == 0
flat_position = np.unpackbits(np.fromstring(packed_position_bytes, dtype=np.uint8))[:position_dims]
flat_nextmoves = np.unpackbits(np.fromstring(packed_next_move_bytes, dtype=np.uint8))[:next_move_dims]
pos_features = flat_position.reshape(data_size, board_size, board_size, input_planes)
next_moves = flat_nextmoves.reshape(data_size, board_size * board_size)
return DataSet(pos_features, next_moves, [], is_test=is_test)
示例6: decode
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [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)
示例7: decode_1bit
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def decode_1bit(self, img):
"""
Decode the image from popcnt internal image format.
"""
assert len(img.shape) == 1
assert img.shape[0] >= (self._h * self._w / 8)
assert img.shape[0] % self._align == 0
bitrev8 = lambda x: sum(1 << (8 - 1 - i) for i in range(8) if x >> i & 1)
img_reverse = np.array(
list(map(lambda x: bitrev8(x), img)), dtype=np.uint8)
img_8b_1d = np.unpackbits(img_reverse) * 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
示例8: decode_1bit
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def decode_1bit(values):
return np.unpackbits(values.view(np.uint8)).astype(np.float32)
示例9: test_payload_basics
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def test_payload_basics(self):
assert self.payload.complex_data is False
assert self.payload.sample_shape == (2,)
assert self.payload.bps == 8
assert self.payload.nbytes == 8
assert self.payload.shape == (4, 2)
assert self.payload.size == 8
assert self.payload.ndim == 2
assert np.all(self.payload.data.ravel()
== self.payload.words.view(np.int8))
assert np.all(np.array(self.payload).ravel()
== self.payload.words.view(np.int8))
assert np.all(np.array(self.payload, dtype=np.int8).ravel()
== self.payload.words.view(np.int8))
payload = self.Payload(self.payload.words, bps=4)
with pytest.raises(KeyError):
payload.data
with pytest.raises(ValueError):
self.Payload(self.payload.words.astype('>u4'), bps=4)
payload = self.Payload(self.payload.words, bps=8, complex_data=True)
assert np.all(payload.data
== (self.payload.data[:, 0]
+ 1j * self.payload.data[:, 1]))
assert self.payload1bit.complex_data is True
assert self.payload1bit.sample_shape == (5,)
assert self.payload1bit.bps == 1
assert self.payload1bit.shape == (16, 5)
assert self.payload1bit.nbytes == 20
assert np.all(self.payload1bit.data.ravel()
== (np.unpackbits(self.payload1bit.words.view(np.uint8))
.astype(np.float32).view(np.complex64)))
示例10: get_coldata
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def get_coldata(coldata):
""" return values and rowmask """
dtype = np.dtype(coldata[DTYPE])
values = np.frombuffer(decompress(coldata[DATA]), dtype=dtype)
rowmask = np.unpackbits(np.frombuffer(decompress(coldata[ROWMASK]), dtype='uint8'))
return list(values), list(rowmask)
示例11: test_unpackbits
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def test_unpackbits():
# Copied from the docstring.
a = np.array([[2], [7], [23]], dtype=np.uint8)
b = np.unpackbits(a, axis=1)
assert_equal(b.dtype, np.uint8)
assert_array_equal(b, np.array([[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 0, 1, 1, 1]]))
示例12: test_unpackbits_empty
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def test_unpackbits_empty():
a = np.empty((0,), dtype=np.uint8)
b = np.unpackbits(a)
assert_equal(b.dtype, np.uint8)
assert_array_equal(b, np.empty((0,)))
示例13: test_unpackbits_large
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def test_unpackbits_large():
# test all possible numbers via comparison to already tested packbits
d = np.arange(277, dtype=np.uint8)
assert_array_equal(np.packbits(np.unpackbits(d)), d)
assert_array_equal(np.packbits(np.unpackbits(d[::2])), d[::2])
d = np.tile(d, (3, 1))
assert_array_equal(np.packbits(np.unpackbits(d, axis=1), axis=1), d)
d = d.T.copy()
assert_array_equal(np.packbits(np.unpackbits(d, axis=0), axis=0), d)
示例14: split_sync
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def split_sync(sync_tr):
"""
The synchronization channelx are stored as single bits, this will split the int16 original
channel into 16 single bits channels
:param sync_tr: numpy vector: samples of synchronisation trace
:return: int8 numpy array of 16 channels, 1 column per sync trace
"""
sync_tr = np.int16(np.copy(sync_tr))
out = np.unpackbits(sync_tr.view(np.uint8)).reshape(sync_tr.size, 16)
out = np.flip(np.roll(out, 8, axis=1), axis=1)
return np.int8(out)
示例15: fetch_aligned_array_of_bits
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import unpackbits [as 別名]
def fetch_aligned_array_of_bits(self, count: int) -> numpy.ndarray:
"""
Quickly decodes an aligned array of bits using the numpy's fast bit unpacking routine.
A new array is always created (the memory cannot be shared with the buffer due to the layout transformation).
The returned array is of dtype :class:`numpy.bool`.
"""
_ensure_cardinal(count)
assert self._bit_offset % 8 == 0
bs = self._buf.get_unsigned_slice(self._byte_offset, self._byte_offset + (count + 7) // 8)
out = numpy.unpackbits(bs, bitorder='little')[:count]
self._bit_offset += count
assert len(out) == count
return out.astype(dtype=numpy.bool)