本文整理汇总了Python中numpy.packbits函数的典型用法代码示例。如果您正苦于以下问题:Python packbits函数的具体用法?Python packbits怎么用?Python packbits使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了packbits函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_name
def test_name(self):
for n in [16, 1, 5, 8, 16, 20, 20 * 8]:
w = np.random.random(n)
j = distpy.JaccardWeighted(w)
j2 = pickle.loads(pickle.dumps(j, -1))
for x in range(n):
a = np.zeros(n, dtype=np.uint8)
b = np.zeros(n, dtype=np.uint8)
a[x] = 1
b[x] = 1
a = np.packbits(a)
b = np.packbits(b)
out0 = j.dist(a, b)
out1 = jaccard_weighted_slow(a, b, w)
out2 = j2.dist(a, b)
self.assertEqual(out0, out1)
self.assertEqual(out0, w[x])
self.assertEqual(out0, out2)
print((out0, out1, w[x]))
for x in range(1000):
bytes = np.ceil(n / 8.0)
a = np.fromstring(np.random.bytes(bytes), dtype=np.uint8)
b = np.fromstring(np.random.bytes(bytes), dtype=np.uint8)
out0 = j.dist(a, b)
out1 = jaccard_weighted_slow(a, b, w)
out2 = j2.dist(a, b)
self.assertAlmostEqual(out0, out1)
self.assertEqual(out0, out2)
print((out0, out1))
示例2: test_packbits_very_large
def test_packbits_very_large():
# test some with a larger arrays gh-8637
# code is covered earlier but larger array makes crash on bug more likely
for s in range(950, 1050):
for dt in '?bBhHiIlLqQ':
x = np.ones((200, s), dtype=bool)
np.packbits(x, axis=1)
示例3: comprimir
def comprimir(arvore: HuffmanNode, bitGroupSize: int, compressedBitSize, codeTable : dict, dataBuffer):
global paddingSize, nomeFicheiro
try:
outputHandler = open(nomeFicheiro.replace('.pbm', '.cpbm'), 'wb')
#codificar a arvore de huffman e escreve-la no ficheiro
writeTreeToFile(outputHandler, compressedBitSize, paddingSize, codeTable, bitGroupSize, arvore)
print("A comprimir os dados com grupos de:", bitGroupSize)
reiniciarBufferDoFicheiro()
outBuffer = list()
get = codeTable.get
bitpos = 0
tamanhoFicheiroBits = tamanhoFicheiro * 8
while(bitpos < tamanhoFicheiroBits):
#ler um grupo de bits
bitGroup = read(dataBuffer, bitGroupSize)
codigo = get(bitGroup, None)
if(codigo != None):
outBuffer.extend(codigo)
bitpos +=bitGroupSize
print("Padding adicional no fim:", paddingSize)
np.packbits(outBuffer, -1).tofile(outputHandler)
outputHandler.close()
except:
print("Compressao de ficheiro pbm concluida sem exito")
示例4: elucidate_cc_split
def elucidate_cc_split(parent_id, split_id):
parent_id_bytes = numpy.array(tuple(parent_id)).view(dtype = numpy.uint8)
split_id_bytes = numpy.array(tuple(split_id)).view(dtype = numpy.uint8)
parent_id_bits = numpy.unpackbits(parent_id_bytes)
split_id_bits = numpy.unpackbits(split_id_bytes)
n_parent_bits = len(parent_id_bits)
n_split_bits = len(split_id_bits)
child1_bits = numpy.zeros(n_parent_bits, dtype = numpy.uint8)
child2_bits = numpy.zeros(n_parent_bits, dtype = numpy.uint8)
j = 0
for i in range(n_parent_bits):
if parent_id_bits[i] == 1:
if j < n_split_bits:
if split_id_bits[j] == 1:
child1_bits[i] = 1
else:
child2_bits[i] = 1
else:
child2_bits[i] = 1
j += 1
child1_bytes = numpy.packbits(child1_bits)
child2_bytes = numpy.packbits(child2_bits)
child1_id = child1_bytes.tostring().rstrip("\x00") # urgh C (null terminated strings)
child2_id = child2_bytes.tostring().rstrip("\x00") # vs Python (not null terminated) strings
return child1_id, child2_id
示例5: test_bad_count
def test_bad_count(self):
packed0 = np.packbits(self.x, axis=0)
assert_raises(ValueError, np.unpackbits, packed0, axis=0, count=-9)
packed1 = np.packbits(self.x, axis=1)
assert_raises(ValueError, np.unpackbits, packed1, axis=1, count=-9)
packed = np.packbits(self.x)
assert_raises(ValueError, np.unpackbits, packed, count=-57)
示例6: write_data
def write_data(dict_words):
items = []
with open(POSTS_FILE_PATH, 'r') as f:
for post in ijson.items(f, 'item'):
labels = [0] * len(CATEGORIES)
was = False
for hub in post['hubs']:
for cur_label, label in enumerate(CATEGORIES):
if hub in label:
labels[cur_label] = 1
was = True
if not was:
continue
words = [0] * BagOfWords.NUM_VOCABULARY_SIZE
post_words = post['content'] + post['title']
for word in post_words:
if word in dict_words:
words[dict_words[word]] = 1
else:
words[BagOfWords.NUM_VOCABULARY_SIZE - 1] = 1
labels = np.packbits(labels).tolist()
words = np.packbits(words).tolist()
items.append((labels, words))
shuffle(items)
train_set_size = int(len(items) * BagOfWords.TRAIN_RATIO)
_write_set(TF_ONE_SHOT_TRAIN_FILE_PATH, items[:train_set_size])
_write_set(TF_ONE_SHOT_EVAL_FILE_PATH, items[train_set_size:])
print('Set size : ', len(items))
print('Train set size : ', train_set_size)
print('Eval set size : ', len(items) - train_set_size)
示例7: payloadExists
def payloadExists(self):
buffer = []
if self.color_flag:
row = self.img[0]
stop_flag = 0
for col in row:
if col[0] & numpy.uint8(1) == 1:
buffer.append(1)
else:
buffer.append(0)
if stop_flag == 7:
print(buffer)
valid = chr(numpy.packbits(buffer)[0])
if valid == "<":
return True
else:
return False
stop_flag += 1
else:
i = 0
row = self.img[0]
while i < 8:
if row[i] & numpy.uint8(1) == 1:
buffer.append(1)
else:
buffer.append(0)
i+= 1
valid = chr(numpy.packbits(buffer)[0])
if valid == "<":
return True
else:
return False
示例8: image_from_bits
def image_from_bits(self, bits, filename):
# Convert the received payload to an image and save it
# No return value required .
pixel_values = np.array([], dtype=np.uint)
#Retrieve length of each row
row_length_bits = bits[0:8]
row_length = np.packbits(row_length_bits)[0]
#print "Forming image from bits..."
#print "Row length bits: " + str(row_length_bits)
#print "\tFound row length of " + str(row_length)
#print
pixel_idx = 8
while pixel_idx < len(bits):
pixel_bits = bits[pixel_idx:pixel_idx+8]
pixel_value = np.packbits(pixel_bits)[0]
pixel_values = np.append(pixel_values, pixel_value)
pixel_idx += 8 #int
img = Image.new('L', (len(pixel_values) / row_length, row_length))
img.putdata(pixel_values)
img.save(filename)
示例9: extractPayload
def extractPayload(self):
if self.payloadExists() is False:
raise Exception("Error: carrier does not contain a payload")
xml = ""
shape = self.img.shape
payload_bits = np.copy(self.img)
payload_bits &= 1
if len(shape) > 2:
red = payload_bits[:, :, 0]
green = payload_bits[:, :, 1]
blue = payload_bits[:, :, 2]
payload_buff = np.concatenate((red.flatten('C'), green.flatten('C'), blue.flatten('C')), axis=0)
payload = np.packbits(payload_buff)
payload_list = [chr(item) for item in payload]
for item in payload_list:
xml += item
if item == '>':
termination = re.search(r"</payload>", xml)
if termination:
final_payload = Payload(None, -1, xml)
return final_payload
else:
payload = np.packbits(payload_bits)
payload_list = [chr(item) for item in payload]
for item in payload_list:
xml += item
if item == '>':
termination = re.search(r"</payload>", xml)
if termination:
final_payload = Payload(None, -1, xml)
return final_payload
示例10: test_unpackbits_count
def test_unpackbits_count():
# test complete invertibility of packbits and unpackbits with count
x = np.array([
[1, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[1, 0, 1, 0, 1, 0, 1],
[0, 0, 1, 1, 1, 0, 0],
[0, 1, 0, 1, 0, 1, 0],
], dtype=np.uint8)
padded1 = np.zeros(57, dtype=np.uint8)
padded1[:49] = x.ravel()
packed = np.packbits(x)
for count in range(58):
unpacked = np.unpackbits(packed, count=count)
assert_equal(unpacked.dtype, np.uint8)
assert_array_equal(unpacked, padded1[:count])
for count in range(-1, -57, -1):
unpacked = np.unpackbits(packed, count=count)
assert_equal(unpacked.dtype, np.uint8)
# count -1 because padded1 has 57 instead of 56 elements
assert_array_equal(unpacked, padded1[:count-1])
for kwargs in [{}, {'count': None}]:
unpacked = np.unpackbits(packed, **kwargs)
assert_equal(unpacked.dtype, np.uint8)
assert_array_equal(unpacked, padded1[:-1])
assert_raises(ValueError, np.unpackbits, packed, count=-57)
padded2 = np.zeros((9, 9), dtype=np.uint8)
padded2[:7, :7] = x
packed0 = np.packbits(x, axis=0)
packed1 = np.packbits(x, axis=1)
for count in range(10):
unpacked0 = np.unpackbits(packed0, axis=0, count=count)
assert_equal(unpacked0.dtype, np.uint8)
assert_array_equal(unpacked0, padded2[:count, :x.shape[1]])
unpacked1 = np.unpackbits(packed1, axis=1, count=count)
assert_equal(unpacked1.dtype, np.uint8)
assert_array_equal(unpacked1, padded2[:x.shape[1], :count])
for count in range(-1, -9, -1):
unpacked0 = np.unpackbits(packed0, axis=0, count=count)
assert_equal(unpacked0.dtype, np.uint8)
# count -1 because one extra zero of padding
assert_array_equal(unpacked0, padded2[:count-1, :x.shape[1]])
unpacked1 = np.unpackbits(packed1, axis=1, count=count)
assert_equal(unpacked1.dtype, np.uint8)
assert_array_equal(unpacked1, padded2[:x.shape[0], :count-1])
for kwargs in [{}, {'count': None}]:
unpacked0 = np.unpackbits(packed0, axis=0, **kwargs)
assert_equal(unpacked0.dtype, np.uint8)
assert_array_equal(unpacked0, padded2[:-1, :x.shape[1]])
unpacked1 = np.unpackbits(packed1, axis=1, **kwargs)
assert_equal(unpacked1.dtype, np.uint8)
assert_array_equal(unpacked1, padded2[:x.shape[0], :-1])
assert_raises(ValueError, np.unpackbits, packed0, axis=0, count=-9)
assert_raises(ValueError, np.unpackbits, packed1, axis=1, count=-9)
示例11: calculate_node_hashes
def calculate_node_hashes(children_a, children_b, taxon_order):
n_taxa = len(taxon_order)
children = set.union(children_a, children_b)
parent_boolean = numpy.zeros(n_taxa, dtype=numpy.uint8)
split_boolean = numpy.zeros(n_taxa, dtype=numpy.uint8)
i = 0
for j in range(n_taxa):
t = taxon_order[j]
if t in children:
parent_boolean[j] = 1
if i == 0:
if t in children_a:
a_first = True
else:
a_first = False
if (t in children_b) ^ a_first: # first child always "True"
split_boolean[i] = 1
i += 1
parent_packed = numpy.packbits(parent_boolean)
split_packed = numpy.packbits(split_boolean)
parent_id = parent_packed.tostring()
split_id = split_packed.tostring()
return parent_id, split_id
示例12: write
def write(self, filename):
header_bytes = struct.pack(CHUNK_HEADER_FORMAT, self.data_size, self.board_size, self.input_planes, self.is_test)
position_bytes = np.packbits(self.pos_features).tostring()
next_move_bytes = np.packbits(self.next_moves).tostring()
with gzip.open(filename, "wb", compresslevel=6) as f:
f.write(header_bytes)
f.write(position_bytes)
f.write(next_move_bytes)
示例13: convert
def convert(data, se):
"""Convert data according to the schema encoding"""
dtype = data.dtype
type = se.type
converted_type = se.converted_type
if dtype.name in typemap:
if type in revmap:
out = data.values.astype(revmap[type], copy=False)
elif type == parquet_thrift.Type.BOOLEAN:
padded = np.lib.pad(data.values, (0, 8 - (len(data) % 8)),
'constant', constant_values=(0, 0))
out = np.packbits(padded.reshape(-1, 8)[:, ::-1].ravel())
elif dtype.name in typemap:
out = data.values
elif "S" in str(dtype)[:2] or "U" in str(dtype)[:2]:
out = data.values
elif dtype == "O":
try:
if converted_type == parquet_thrift.ConvertedType.UTF8:
out = array_encode_utf8(data)
elif converted_type is None:
if type in revmap:
out = data.values.astype(revmap[type], copy=False)
elif type == parquet_thrift.Type.BOOLEAN:
padded = np.lib.pad(data.values, (0, 8 - (len(data) % 8)),
'constant', constant_values=(0, 0))
out = np.packbits(padded.reshape(-1, 8)[:, ::-1].ravel())
else:
out = data.values
elif converted_type == parquet_thrift.ConvertedType.JSON:
out = np.array([json.dumps(x).encode('utf8') for x in data],
dtype="O")
elif converted_type == parquet_thrift.ConvertedType.BSON:
out = data.map(tobson).values
if type == parquet_thrift.Type.FIXED_LEN_BYTE_ARRAY:
out = out.astype('S%i' % se.type_length)
except Exception as e:
ct = parquet_thrift.ConvertedType._VALUES_TO_NAMES[
converted_type] if converted_type is not None else None
raise ValueError('Error converting column "%s" to bytes using '
'encoding %s. Original error: '
'%s' % (data.name, ct, e))
elif converted_type == parquet_thrift.ConvertedType.TIMESTAMP_MICROS:
out = np.empty(len(data), 'int64')
time_shift(data.values.view('int64'), out)
elif converted_type == parquet_thrift.ConvertedType.TIME_MICROS:
out = np.empty(len(data), 'int64')
time_shift(data.values.view('int64'), out)
elif type == parquet_thrift.Type.INT96 and dtype.kind == 'M':
ns_per_day = (24 * 3600 * 1000000000)
day = data.values.view('int64') // ns_per_day + 2440588
ns = (data.values.view('int64') % ns_per_day)# - ns_per_day // 2
out = np.empty(len(data), dtype=[('ns', 'i8'), ('day', 'i4')])
out['ns'] = ns
out['day'] = day
else:
raise ValueError("Don't know how to convert data type: %s" % dtype)
return out
示例14: test_unpackbits_large
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)
示例15: _generate_masks
def _generate_masks(self):
"""Creates left and right masks for all hash lengths."""
tri_size = MAX_HASH_SIZE + 1
# Called once on fitting, output is independent of hashes
left_mask = np.tril(np.ones((tri_size, tri_size), dtype=int))[:, 1:]
right_mask = left_mask[::-1, ::-1]
self._left_mask = np.packbits(left_mask).view(dtype=HASH_DTYPE)
self._right_mask = np.packbits(right_mask).view(dtype=HASH_DTYPE)