Python numpy.unpackbits() Examples
The following are 30
code examples of numpy.unpackbits().
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 also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: decode_1bit.py From IkaLog with 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 #2
Source File: test_packbits.py From vnpy_crypto with 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 #3
Source File: test_packbits.py From predictive-maintenance-using-machine-learning with 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 #4
Source File: test_packbits.py From Mastering-Elasticsearch-7.0 with 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
Source File: test_packbits.py From lambda-packs with 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
Source File: test_packbits.py From pySINDy with 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 #7
Source File: packbits.py From numcodecs with 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 #8
Source File: test_packbits.py From mxnet-lambda with 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 #9
Source File: reference.py From IkaLog with 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 #10
Source File: test_packbits.py From recruit with 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 #11
Source File: bit_manipulation.py From Steganography with 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 #12
Source File: test_packbits.py From GraphicDesignPatternByPython with 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 #13
Source File: world_utils.py From Amulet-Core with 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 #14
Source File: load_data_sets.py From alphago_demo with 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 #15
Source File: fields.py From occupancy_flow with MIT License | 5 votes |
def __init__(self, folder_name, transform=None, seq_len=17, all_steps=False, fixed_time_step=None, unpackbits=False, **kwargs): self.folder_name = folder_name self.transform = transform self.seq_len = seq_len self.all_steps = all_steps self.sample_padding = 0.1 self.fixed_time_step = fixed_time_step self.unpackbits = unpackbits
Example #16
Source File: trial_result.py From Cirq with Apache License 2.0 | 5 votes |
def _unpack_bits(packed_bits: str, dtype: str, shape: Sequence[int]) -> np.ndarray: bits_bytes = bytes.fromhex(packed_bits) bits = np.unpackbits(np.frombuffer(bits_bytes, dtype=np.uint8)) return bits[:np.prod(shape)].reshape(shape).astype(dtype)
Example #17
Source File: programs.py From Cirq with Apache License 2.0 | 5 votes |
def unpack_results(data: bytes, repetitions: int, key_sizes: Sequence[Tuple[str, int]] ) -> Dict[str, np.ndarray]: """Unpack data from a bitstring into individual measurement results. Args: data: Packed measurement results, in the form <rep0><rep1>... where each repetition is <key0_0>..<key0_{size0-1}><key1_0>... with bits packed in little-endian order in each byte. repetitions: number of repetitions. key_sizes: Keys and sizes of the measurements in the data. Returns: Dict mapping measurement key to a 2D array of boolean results. Each array has shape (repetitions, size) with size for that measurement. """ bits_per_rep = sum(size for _, size in key_sizes) total_bits = repetitions * bits_per_rep byte_arr = np.frombuffer(data, dtype='uint8').reshape((len(data), 1)) bits = np.unpackbits(byte_arr, axis=1)[:, ::-1].reshape(-1).astype(bool) bits = bits[:total_bits].reshape((repetitions, bits_per_rep)) results = {} ofs = 0 for key, size in key_sizes: results[key] = bits[:, ofs:ofs + size] ofs += size return results
Example #18
Source File: fields.py From occupancy_flow with MIT License | 5 votes |
def load_single_step(self, files, points_dict, loc0, scale0): ''' Loads data for a single step. Args: files (list): list of files points_dict (dict): points dictionary for first step of sequence loc0 (tuple): location of first time step mesh scale0 (float): scale of first time step mesh ''' if self.fixed_time_step is None: # Random time step time_step = np.random.choice(self.seq_len) else: time_step = int(self.fixed_time_step) if time_step != 0: points_dict = np.load(files[time_step]) # Load points points = points_dict['points'].astype(np.float32) occupancies = points_dict['occupancies'] if self.unpackbits: occupancies = np.unpackbits(occupancies)[:points.shape[0]] occupancies = occupancies.astype(np.float32) loc = points_dict['loc'].astype(np.float32) scale = points_dict['scale'].astype(np.float32) # Transform to loc0, scale0 points = (loc + scale * points - loc0) / scale0 if self.seq_len > 1: time = np.array( time_step / (self.seq_len - 1), dtype=np.float32) else: time = np.array([1], dtype=np.float32) data = { None: points, 'occ': occupancies, 'time': time, } return data
Example #19
Source File: test_packbits.py From pySINDy with 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 #20
Source File: results.py From Cirq with Apache License 2.0 | 5 votes |
def unpack_bits(data: bytes, repetitions: int) -> np.ndarray: """Unpack bits from a byte array into numpy array of bools.""" byte_arr = np.frombuffer(data, dtype='uint8').reshape((len(data), 1)) bits = np.unpackbits(byte_arr, axis=1)[:, ::-1].reshape(-1).astype(bool) return bits[:repetitions]
Example #21
Source File: features.py From pysc2 with Apache License 2.0 | 5 votes |
def unpack_layer(plane): """Return a correctly shaped numpy array given the feature layer bytes.""" size = point.Point.build(plane.size) if size == (0, 0): # New layer that isn't implemented in this SC2 version. return None data = np.frombuffer(plane.data, dtype=Feature.dtypes[plane.bits_per_pixel]) if plane.bits_per_pixel == 1: data = np.unpackbits(data) if data.shape[0] != size.x * size.y: # This could happen if the correct length isn't a multiple of 8, leading # to some padding bits at the end of the string which are incorrectly # interpreted as data. data = data[:size.x * size.y] return data.reshape(size.y, size.x)
Example #22
Source File: test_packbits.py From pySINDy with 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 #23
Source File: test_packbits.py From predictive-maintenance-using-machine-learning with 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 #24
Source File: test_packbits.py From predictive-maintenance-using-machine-learning with 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 #25
Source File: test_packbits.py From predictive-maintenance-using-machine-learning with 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 #26
Source File: generate_xor.py From IntroductionToMachineLearningForSecurityPros with GNU General Public License v3.0 | 5 votes |
def make_dataset(dataset, nsamp, slen, maxkeylen): x = np.zeros((nsamp, slen, 8)) y = np.zeros((nsamp, maxkeylen)) for i in xrange(nsamp): keylen = np.random.randint(maxkeylen) + 1 # save key len as categorical variable y[i, keylen - 1] = 1.0 dataptr = np.random.randint(len(dataset) - slen) data = dataset[dataptr:dataptr + slen] data = np.fromstring(data, dtype=np.uint8) key = generate_random_string(keylen) while principal_period(key) is not None: key = generate_random_string(keylen) key = np.fromstring(key, dtype=np.uint8) key_nrep = int(np.ceil(float(slen) / float(len(key)))) key_exp = np.tile(key, key_nrep)[:slen] xor_ciphertext = np.bitwise_xor(data, key_exp) x[i, :, :] = np.unpackbits(xor_ciphertext).reshape(slen, 8) return x, y
Example #27
Source File: train_model.py From IntroductionToMachineLearningForSecurityPros with GNU General Public License v3.0 | 5 votes |
def make_dataset(dataset, nsamp, slen, maxkeylen): x = np.zeros((nsamp, slen, 8)) y = np.zeros((nsamp, maxkeylen)) for i in xrange(nsamp): keylen = np.random.randint(maxkeylen) + 1 # save key len as categorical variable y[i, keylen - 1] = 1.0 dataptr = np.random.randint(len(dataset) - slen) data = dataset[dataptr:dataptr + slen] data = np.fromstring(data, dtype=np.uint8) key = generate_random_string(keylen) while principal_period(key) is not None: key = generate_random_string(keylen) key = np.fromstring(key, dtype=np.uint8) key_nrep = int(np.ceil(float(slen) / float(len(key)))) key_exp = np.tile(key, key_nrep)[:slen] xor_ciphertext = np.bitwise_xor(data, key_exp) x[i, :, :] = np.unpackbits(xor_ciphertext).reshape(slen, 8) return x, y
Example #28
Source File: test_packbits.py From GraphicDesignPatternByPython with MIT License | 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 #29
Source File: test_packbits.py From GraphicDesignPatternByPython with 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 #30
Source File: test_packbits.py From GraphicDesignPatternByPython with 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]]))