我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用numpy.unpackbits()。
def hackrf_raw_IQ_format(ppm): """ real_signal = [] bits = numpy.unpackbits(numpy.asarray(ppm, dtype=numpy.uint8)) for bit in bits: if bit == 1: I = 127 else: I = 0 real_signal.append(I) analytic_signal = hilbert(real_signal) #for i in range(len(real_signal)): # print i, real_signal[i], int(analytic_signal[i]) """ signal = [] bits = numpy.unpackbits(numpy.asarray(ppm, dtype=numpy.uint8)) for bit in bits: if bit == 1: I = 127 Q = 127 else: I = 0 Q = 0 signal.append(I) signal.append(Q) return bytearray(signal)
def lsb_encode(data, image): bytes_io = BytesIO() dump(data, file=bytes_io) data_bytes = bytes_io.getvalue() data_bytes_array = np.fromiter(data_bytes, dtype=np.uint8) data_bits_list = np.unpackbits(data_bytes_array).tolist() data_bits_list += [0] * (image.size[0] * image.size[1] - len(data_bits_list)) watermark = Image.frombytes(data=bytes(data_bits_list), size=image.size, mode='L') red, green, blue = image.split() watermarked_red = ImageMath.eval("convert(a&0xFE|b&0x1,'L')", a=red, b=watermark) watermarked_image = Image.merge("RGB", (watermarked_red, green, blue)) return watermarked_image
def main(): import numpy.random as random from trace import trace import sys if len(sys.argv) == 1: sys.exit("{} [directory]".format(sys.argv[0])) directory = sys.argv[1] directory_ad = "{}_ad/".format(directory) discriminator = Discriminator(directory_ad).load() name = "generated_actions.csv" N = discriminator.net.input_shape[1] lowbit = 20 highbit = N - lowbit print("batch size: {}".format(2**lowbit)) xs = (((np.arange(2**lowbit )[:,None] & (1 << np.arange(N)))) > 0).astype(int) # xs_h = (((np.arange(2**highbit)[:,None] & (1 << np.arange(highbit)))) > 0).astype(int) try: print(discriminator.local(name)) with open(discriminator.local(name), 'wb') as f: for i in range(2**highbit): print("Iteration {}/{} base: {}".format(i,2**highbit,i*(2**lowbit)), end=' ') # h = np.binary_repr(i*(2**lowbit), width=N) # print(h) # xs_h = np.unpackbits(np.array([i*(2**lowbit)],dtype=int)) xs_h = (((np.array([i])[:,None] & (1 << np.arange(highbit)))) > 0).astype(int) xs[:,lowbit:] = xs_h # print(xs_h) # print(xs[:10]) ys = discriminator.discriminate(xs,batch_size=100000) ind = np.where(ys > 0.5) valid_xs = xs[ind] print(len(valid_xs)) np.savetxt(f,valid_xs,"%d") except KeyboardInterrupt: print("dump stopped")
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]]))
def make_cmap(N_CLASSES): # Generate the colors for the classes (with background class being 0,0,0) c_size = 2**N_CLASSES - 1 cmap = np.concatenate([[[0, 0, 0]], plt.cm.Set1(np.arange(c_size) / (c_size))[:, :3]]) cmap = (cmap * 255).astype(np.uint8) assert N_CLASSES <= 8, "ARGH!! can not handle more than 8 classes" c_full_label = np.unpackbits(np.arange(2 ** N_CLASSES).astype(np.uint8)[:, None], axis=-1)[:, -N_CLASSES:] return cmap, c_full_label
def decompress_raw(data, # type: bytes shape, # type: Tuple[int, int] depth, # type: int version # type: int ): # type: (...) -> np.ndarray """ Converts raw data to a Numpy array. {} """ depth = enums.ColorDepth(depth) dtype = color_depth_dtype_map[depth] itemsize = color_depth_size_map[depth] # Truncate the data to a multiple of the dtype size data = data[:(len(data) // itemsize) * itemsize] arr = np.frombuffer(data, dtype) if depth == 1: # Unpack 1-bit image data arr = np.unpackbits(arr) # Make 2-dimensional image = arr.reshape(shape) return image
def parse_rectangle(cls, client, x, y, width, height, data): split = width * height * client.framebuffer.bypp image = np.frombuffer(data[:split], np.uint8).reshape((height, width, 4))[:, :, [0, 1, 2]] # Turn raw bytes into uint8 array mask = np.frombuffer(data[split:], np.uint8) # Turn uint8 array into bit array, and go over the scanlines mask = np.unpackbits(mask).reshape((height, -1 if mask.size else 0))[:, :width] encoding = cls(image, mask) return Rectangle(x, y, width, height, encoding)
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)
def unpack(a, size): """From a packed array *a*, return the boolean array. Remove byte padding at the end.""" return np.unpackbits(a)[:size]
def imageToBoolMasks(arr): '''inverse of [boolMasksToImage]''' assert arr.dtype == np.uint8, 'image needs to be dtype=uint8' masks = np.unpackbits(arr).reshape(*arr.shape, 8) return np.swapaxes(masks, 2, 0)
def to_bit_sequence(self, event): """ Creates an array of bits containing the details in the event dictionary. Once created, the array is cached to speed up future writes. Parameters ---------- event: dict A dictionary describing the current component event. It should have 3 keys: name, action, and metadata. Returns ------- The array of bits """ if event["metadata"] is None: nbytes = self.action_bytes + self.name_bytes metadata_array = [] else: nbytes = self.metadata_bytes + self.action_bytes + self.name_bytes try: metadata_array = np.fromstring(event["metadata"], dtype=np.uint16).astype(np.uint8)[:self.metadata_bytes] except TypeError: metadata_array = np.array(map(ord, event["metadata"].ljust(self.metadata_bytes)[:self.metadata_bytes]), dtype=np.uint8) int8_array = np.zeros(nbytes, dtype="uint8") int8_array[:self.name_bytes] = map(ord, event["name"].ljust(self.name_bytes)[:self.name_bytes]) int8_array[self.name_bytes:self.name_bytes + self.action_bytes] = map(ord, event["action"].ljust(self.action_bytes)[:self.action_bytes]) int8_array[self.name_bytes + self.action_bytes:] = metadata_array sequence = ([True] + np.unpackbits(int8_array).astype(bool).tolist() + [False]) key = (event["name"], event["action"], event["metadata"]) self.map_to_bit[key] = sequence return sequence
def map_RB_data(data, datadepth): """ Map BLOB data to correct DataWidth and Type and convert it to numpy array Parameters ---------- data : string Blob Data datadepth : int bit depth of Blob data Returns ------- data : numpy array Content of blob """ flagdepth = None if datadepth < 8: flagdepth = datadepth datadepth = 8 datawidth, datatype = get_RB_data_layout(datadepth) # import from data buffer well aligned to data array data = np.ndarray(shape=(int(len(data) / datawidth),), dtype=datatype, buffer=data) if flagdepth: data = np.unpackbits(data) return data
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.fromstring(plane.data, dtype=Feature.dtypes[plane.bits_per_pixel]) if plane.bits_per_pixel == 1: data = np.unpackbits(data) return data.reshape(size.transpose())
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,)))
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)
def unpack_state(self, s, shape): a = np.fromstring(s, dtype=np.uint8) a = np.unpackbits(a) a = a.reshape(shape[0], -1) a = a[:, :shape[1]] b = np.zeros_like(a[0], np.int) b[a[0] == 1] = Board.STONE_BLACK b[a[1] == 1] = Board.STONE_WHITE b[a[2] == 1] = Board.STONE_EMPTY return b
def _read_data(self, fh, byteorder='>'): """Return image data from open file as numpy array.""" fh.seek(len(self.header)) data = fh.read() dtype = 'u1' if self.maxval < 256 else byteorder + 'u2' depth = 1 if self.magicnum == b"P7 332" else self.depth shape = [-1, self.height, self.width, depth] size = functools.reduce(operator.mul, shape[1:], 1) # prod() if self.magicnum in b"P1P2P3": data = numpy.array(data.split(None, size)[:size], dtype) data = data.reshape(shape) elif self.maxval == 1: shape[2] = int(math.ceil(self.width / 8)) data = numpy.frombuffer(data, dtype).reshape(shape) data = numpy.unpackbits(data, axis=-2)[:, :, :self.width, :] else: size *= numpy.dtype(dtype).itemsize data = numpy.frombuffer(data[:size], dtype).reshape(shape) if data.shape[0] < 2: data = data.reshape(data.shape[1:]) if data.shape[-1] < 2: data = data.reshape(data.shape[:-1]) if self.magicnum == b"P7 332": rgb332 = numpy.array(list(numpy.ndindex(8, 8, 4)), numpy.uint8) rgb332 *= [36, 36, 85] data = numpy.take(rgb332, data, axis=0) return data
def _generate_hypercube(samples, dimensions, rng): """Returns distinct binary samples of length dimensions """ if dimensions > 30: return np.hstack([_generate_hypercube(samples, dimensions - 30, rng), _generate_hypercube(samples, 30, rng)]) out = astype(sample_without_replacement(2 ** dimensions, samples, random_state=rng), dtype='>u4', copy=False) out = np.unpackbits(out.view('>u1')).reshape((-1, 32))[:, -dimensions:] return out
def unpackints(data, dtype, itemsize, runlen=0): """Decompress byte string to array of integers of any bit size <= 32. Parameters ---------- data : byte str Data to decompress. dtype : numpy.dtype or str A numpy boolean or integer type. itemsize : int Number of bits per integer. runlen : int Number of consecutive integers, after which to start at next byte. """ if itemsize == 1: # bitarray data = numpy.fromstring(data, '|B') data = numpy.unpackbits(data) if runlen % 8: data = data.reshape(-1, runlen + (8 - runlen % 8)) data = data[:, :runlen].reshape(-1) return data.astype(dtype) dtype = numpy.dtype(dtype) if itemsize in (8, 16, 32, 64): return numpy.fromstring(data, dtype) if itemsize < 1 or itemsize > 32: raise ValueError("itemsize out of range: %i" % itemsize) if dtype.kind not in "biu": raise ValueError("invalid dtype") itembytes = next(i for i in (1, 2, 4, 8) if 8 * i >= itemsize) if itembytes != dtype.itemsize: raise ValueError("dtype.itemsize too small") if runlen == 0: runlen = len(data) // itembytes skipbits = runlen*itemsize % 8 if skipbits: skipbits = 8 - skipbits shrbits = itembytes*8 - itemsize bitmask = int(itemsize*'1'+'0'*shrbits, 2) dtypestr = '>' + dtype.char # dtype always big endian? unpack = struct.unpack l = runlen * (len(data)*8 // (runlen*itemsize + skipbits)) result = numpy.empty((l, ), dtype) bitcount = 0 for i in range(len(result)): start = bitcount // 8 s = data[start:start+itembytes] try: code = unpack(dtypestr, s)[0] except Exception: code = unpack(dtypestr, s + b'\x00'*(itembytes-len(s)))[0] code <<= bitcount % 8 code &= bitmask result[i] = code >> shrbits bitcount += itemsize if (i+1) % runlen == 0: bitcount += skipbits return result
def to_bit_sequence(self, event): """ Creates an array of bits containing the details in the event dictionary. This array is then upsampled and converted to float64 to be sent down an analog output. Once created, the array is cached to speed up future calls. Parameters ---------- event: dict A dictionary describing the current component event. It should have 3 keys: name, action, and metadata. Returns ------- The array of bits expressed as analog values """ key = (event["name"], event["action"], event["metadata"]) # Check if the bit string is already stored if key in self.map_to_bit: return self.map_to_bit[key] trim = lambda ss, l: ss.ljust(l)[:l] # Set up int8 arrays where strings are converted to integers using ord name_array = np.array(map(ord, trim(event["name"], self.name_bytes)), dtype=np.uint8) action_array = np.array(map(ord, trim(event["action"], self.action_bytes)), dtype=np.uint8) # Add the metadata array if a value was passed if event["metadata"] is not None: metadata_array = np.array(map(ord, trim(event["metadata"], self.metadata_bytes)), dtype=np.uint8) else: metadata_array = np.array([], dtype=np.uint8) sequence = ([True] + np.unpackbits(name_array).astype(bool).tolist() + np.unpackbits(action_array).astype(bool).tolist() + np.unpackbits(metadata_array).astype(bool).tolist() + [False]) sequence = np.repeat(sequence, self.upsample_factor).astype("float64") sequence *= self.scaling self.map_to_bit[key] = sequence return sequence
def train(): X, m = get_data(['x', 'm']) # X_train, X_test, m_train, m_test = get_data(['x', 'm']) # for board in X_train[:2] : # show_board( board ) start = time.time() print 'shuffling...', idx = range(len(X)) random.shuffle(idx) X, m = X[idx], m[idx] print '%.2f sec' % (time.time() - start) # unpack the bits start = time.time() print 'unpacking...', X = np.array([numpy.unpackbits(x).reshape(28, 8, 8).astype(np.bool) for x in X]) print '%.2f sec' % (time.time() - start) model, name = make_model() print 'compiling...' # 5e5 too high on 2017-09-06 sgd = SGD(lr=3e-5, decay=1e-6, momentum=0.9, nesterov=True) # 1e-4 : nan, 1e-5 loss 137 epoch1, 5e-5 loss 121 epoch1 # model.compile(loss='squared_hinge', optimizer='adadelta') # model.compile(loss='mean_squared_error', optimizer='adadelta') model.compile(loss='mean_squared_error', optimizer=sgd) early_stopping = EarlyStopping( monitor = 'loss', patience = 50 ) # monitor='val_loss', verbose=0, mode='auto' #print 'fitting...' history = model.fit( X, m, nb_epoch = 10, batch_size = BATCH_SIZE, validation_split=0.05) #, callbacks = [early_stopping]) #, validation_split=0.05) #, verbose=2) #, show_accuracy = True ) # print 'evaluating...' # score = model.evaluate(X_test, m_test, batch_size = BATCH_SIZE ) # print 'score:', score now = datetime.datetime.now() suffix = str(now.strftime("%Y-%m-%d_%H%M%S")) model.save_weights( name.replace( '.model', '_%s.model' % suffix), overwrite = True ) #print X_train[:10] # print m_train[:20] # print model.predict( X_train[:20], batch_size = 5 ) # print m[:20] # print model.predict( X[:20], batch_size = 5 ) result = zip( m[-20:] * 100.0, model.predict( X[-20:], batch_size = 5 ) * 100.0) for a, b in result : print '%.4f %.4f %.2f%%' % (a, b, abs(a-b) * 100.0 / max(abs(a),abs(b))) # print m_test[:20] # print model.predict( X_test[:20], batch_size = 5 ) # with open( MODEL_DATA + '.history', 'w') as fout : # print >>fout, history.losses