Python numpy.unpackbits() Examples
The following are 30 code examples for showing how to use numpy.unpackbits(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: recruit Author: Frank-qlu File: test_packbits.py License: Apache License 2.0 | 6 votes |
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)
Example 2
Project: numcodecs Author: zarr-developers File: packbits.py License: MIT License | 6 votes |
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)
Example 3
Project: lambda-packs Author: ryfeus File: test_packbits.py License: MIT License | 6 votes |
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)
Example 4
Project: vnpy_crypto Author: birforce File: test_packbits.py License: MIT License | 6 votes |
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)
Example 5
Project: Mastering-Elasticsearch-7.0 Author: PacktPublishing File: test_packbits.py License: MIT License | 6 votes |
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)
Example 6
Project: Steganography Author: ragibson File: bit_manipulation.py License: MIT License | 6 votes |
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]
Example 7
Project: Amulet-Core Author: Amulet-Team File: world_utils.py License: MIT License | 6 votes |
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]
Example 8
Project: alphago_demo Author: llSourcell File: load_data_sets.py License: Apache License 2.0 | 6 votes |
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)
Example 9
Project: GraphicDesignPatternByPython Author: Relph1119 File: test_packbits.py License: MIT License | 6 votes |
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)
Example 10
Project: IkaLog Author: hasegaw File: reference.py License: Apache License 2.0 | 6 votes |
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)
Example 11
Project: IkaLog Author: hasegaw File: decode_1bit.py License: Apache License 2.0 | 6 votes |
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
Example 12
Project: predictive-maintenance-using-machine-learning Author: awslabs File: test_packbits.py License: Apache License 2.0 | 6 votes |
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)
Example 13
Project: pySINDy Author: luckystarufo File: test_packbits.py License: MIT License | 6 votes |
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)
Example 14
Project: mxnet-lambda Author: awslabs File: test_packbits.py License: Apache License 2.0 | 6 votes |
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)
Example 15
Project: baseband Author: mhvk File: test_base.py License: GNU General Public License v3.0 | 5 votes |
def decode_1bit(values): return np.unpackbits(values.view(np.uint8)).astype(np.float32)
Example 16
Project: baseband Author: mhvk File: test_base.py License: GNU General Public License v3.0 | 5 votes |
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)))
Example 17
Project: baseband Author: mhvk File: test_vlbi_base.py License: GNU General Public License v3.0 | 5 votes |
def decode_1bit(values): return np.unpackbits(values.view(np.uint8)).astype(np.float32)
Example 18
Project: baseband Author: mhvk File: test_vlbi_base.py License: GNU General Public License v3.0 | 5 votes |
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)))
Example 19
Project: arctic Author: man-group File: test_tickstore.py License: GNU Lesser General Public License v2.1 | 5 votes |
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)
Example 20
Project: recruit Author: Frank-qlu File: test_packbits.py License: Apache License 2.0 | 5 votes |
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]]))
Example 21
Project: recruit Author: Frank-qlu File: test_packbits.py License: Apache License 2.0 | 5 votes |
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,)))
Example 22
Project: recruit Author: Frank-qlu File: test_packbits.py License: Apache License 2.0 | 5 votes |
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)
Example 23
Project: ibllib Author: int-brain-lab File: spikeglx.py License: MIT License | 5 votes |
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)
Example 24
Project: pyuavcan Author: UAVCAN File: _deserializer.py License: MIT License | 5 votes |
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)
Example 25
Project: pyuavcan Author: UAVCAN File: _deserializer.py License: MIT License | 5 votes |
def fetch_unaligned_array_of_bits(self, count: int) -> numpy.ndarray: _ensure_cardinal(count) byte_count = (count + 7) // 8 bs = self.fetch_unaligned_bytes(byte_count) assert len(bs) == byte_count backtrack = byte_count * 8 - count assert 0 <= backtrack < 8 self._bit_offset -= backtrack out: numpy.ndarray = numpy.unpackbits(bs, bitorder='little')[:count].astype(dtype=numpy.bool) assert len(out) == count return out
Example 26
Project: lambda-packs Author: ryfeus File: test_packbits.py License: MIT License | 5 votes |
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]]))
Example 27
Project: lambda-packs Author: ryfeus File: test_packbits.py License: MIT License | 5 votes |
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,)))
Example 28
Project: fastchess Author: thomasahle File: tensorsketch.py License: GNU General Public License v3.0 | 5 votes |
def board_to_vec(board): ar = [] for piece_type in chess.PIECE_TYPES: for color in chess.COLORS: ar.append(int(board.pieces(piece_type, color))) packed = np.unpackbits(np.array(ar).view(np.uint8)) return packed
Example 29
Project: auto-alt-text-lambda-api Author: abhisuri97 File: test_packbits.py License: MIT License | 5 votes |
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]]))
Example 30
Project: vnpy_crypto Author: birforce File: test_packbits.py License: MIT License | 5 votes |
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]]))