本文整理汇总了Python中bitstring.Bits类的典型用法代码示例。如果您正苦于以下问题:Python Bits类的具体用法?Python Bits怎么用?Python Bits使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Bits类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testAppendToBits
def testAppendToBits(self):
a = Bits(BitArray())
with self.assertRaises(AttributeError):
a.append('0b1')
self.assertEqual(type(a), Bits)
b = bitstring.ConstBitStream(bitstring.BitStream())
self.assertEqual(type(b), bitstring.ConstBitStream)
示例2: read_spk_folder
def read_spk_folder(spk_folder, bin_size=1):
"""
Loads spike times from all spk files in a given folder.
The j-th item in the list corresponds to the j-th neuron.
It is the 1d array of spike times (microsec) for that neuron.
Parameters
----------
spk_folder : str
Path containing spk file names
bin_size : int, optional
Bin size in milliseconds (default 1)
Returns
-------
spikes : numpy array
numpy array containing binned spike times
"""
from bitstring import Bits
neuron_to_file = []
time_stamps = []
bin_size = bin_size or 1
fns = os.listdir(spk_folder)
for i, fn in enumerate(fns):
ext = os.path.splitext(fn)[1]
if ext in ('.spk', ): # Blanche spike format
neuron_to_file.append(fn)
f = open(os.path.join(spk_folder, fn), 'rb')
p = Bits(f)
fmt = str(p.length / 64) + ' * (intle:64)'
time_stamps.append(p.unpack(fmt))
spikes = SpkReader.load_from_spikes_times(time_stamps, bin_size=bin_size)
return Spikes(spikes)
示例3: uncompress_golomb_coding
def uncompress_golomb_coding(coded_bytes, hash_length, M):
"""Given a bytstream produced using golomb_coded_bytes, uncompress it."""
ret_list = []
instream = BitStream(
bytes=coded_bytes, length=len(coded_bytes) * 8)
hash_len_bits = hash_length * 8
m_bits = int(math.log(M, 2))
# First item is a full hash value.
prev = instream.read("bits:%d" % hash_len_bits)
ret_list.append(prev.tobytes())
while (instream.bitpos + m_bits) <= instream.length:
# Read Unary-encoded value.
read_prefix = 0
curr_bit = instream.read("uint:1")
while curr_bit == 1:
read_prefix += 1
curr_bit = instream.read("uint:1")
assert curr_bit == 0
# Read r, assuming M bits were used to represent it.
r = instream.read("uint:%d" % m_bits)
curr_diff = read_prefix * M + r
curr_value_int = prev.uint + curr_diff
curr_value = Bits(uint=curr_value_int, length=hash_len_bits)
ret_list.append(curr_value.tobytes())
prev = curr_value
return ret_list
示例4: readCommand
def readCommand(hexbits):
bits = Bits(bytes=HexToByte(hexbits))
# print bits
alarm, state, data1, data2, checksum = bits.unpack("uint:4, uint:4, uint:8, uint:8, uint:8")
dic = {"state": state, "alarm": alarm, "data1": data1, "data2": data2, "checksum": checksum}
return dic
示例5: read_spk_files
def read_spk_files(spk_files, bin_size=1):
"""
Loads spike times from a list of spk files.
The j-th item in the list corresponds to the j-th neuron.
It is the 1d array of spike times (microsec) for that neuron.
Parameters
----------
spk_files : list of str
List of strings containing spk file names
bin_size : int, optional
Bin size in milliseconds (default 1)
Returns
-------
spikes : numpy array
numpy array containing binned spike times
"""
from bitstring import Bits
neuron_to_file = []
time_stamps = []
bin_size = bin_size or 1
for fn in spk_files:
neuron_to_file.append(fn)
f = open(fn, 'rb')
p = Bits(f)
fmt = str(p.length / 64) + ' * (intle:64)'
time_stamps.append(p.unpack(fmt))
spikes = SpkReader.load_from_spikes_times(time_stamps, bin_size=bin_size)
return Spikes(spikes)
示例6: testUnpack
def testUnpack(self):
s = Bits('0b111000111')
x, y = s.unpack('3, pad:3, 3')
self.assertEqual((x, y), (7, 7))
x, y = s.unpack('2, pad:2, bin')
self.assertEqual((x, y), (3, '00111'))
x = s.unpack('pad:1, pad:2, pad:3')
self.assertEqual(x, [])
示例7: to_comp1
def to_comp1(n,v):
bits_raw=Bits(uint=abs(v),length=n)
if(v<0):
bits= (bits_raw.__invert__()).bin
else:
bits= bits_raw.bin
return bits
示例8: solve
def solve(par):
N, K = par
results = []
for i in range(1 << N):
b = Bits(int=i, length=N + 1)
if b.count(1) == K:
results.append(b.bin[1:])
return '\n'.join(results)
示例9: getStatusByte
def getStatusByte(byte1):
"""
Gets Two First Bytes, and returns a dictionary with:
Command
SetGroup
Address
"""
bits8 = Bits(bytes=byte1)
status1, status2 = bits8.unpack("uint:4,uint:4")
return dict(status1=getSt3st0(status1), status2=getSt7st4(status2))
示例10: uncompress_golomb_coding
def uncompress_golomb_coding(coded_bytes, hash_length, M):
ret_list = []
instream = BitStream(
bytes=coded_bytes, length=len(coded_bytes) * hash_length)
hash_len_bits = hash_length * 8
m_bits = int(math.log(M, 2))
prev = instream.read("bits:%d" % hash_len_bits)
ret_list.append(prev.tobytes())
while instream.bitpos < instream.length:
read_prefix = 0
curr_bit = instream.read("uint:1")
while curr_bit == 1:
read_prefix += 1
curr_bit = instream.read("uint:1")
assert curr_bit == 0
r = instream.read("uint:%d" % m_bits)
curr_diff = read_prefix * M + r
curr_value_int = prev.uint + curr_diff
curr_value = Bits(uint=curr_value_int, length=hash_len_bits)
ret_list.append(curr_value.tobytes())
prev = curr_value
return ret_list
示例11: testCut
def testCut(self):
s = Bits(30)
for t in s.cut(3):
self.assertEqual(t, [0] * 3)
示例12: testRfind
def testRfind(self):
a = Bits('0b11101010010010')
b = a.rfind('0b010')
self.assertEqual(b[0], 11)
示例13: testFindAll
def testFindAll(self):
a = Bits('0b0010011')
b = list(a.findall([1]))
self.assertEqual(b, [2, 5, 6])
示例14: make_exon_alignment
def make_exon_alignment(cursor, ensembl_db_name, human_exon_id, human_exon_known, mitochondrial,
min_similarity, flank_length, first_human_exon = True):
sequence_pep = {}
sequence_dna = {}
shortest_l = -1 # Uninitialized leading padding length
shortest_r = -1 # Uninitialized trailing padding length
pep_aln_length = 0
dna_aln_length = 0
# find all other exons that map to the human exon
maps = get_maps(cursor, ensembl_db_name, human_exon_id, human_exon_known)
maps = filter (lambda m: not m.exon_id_2 is None, maps)
maps_sw = filter (lambda m: m.source=='sw_sharp' or m.source=='usearch', maps)
for map in maps:
if map.similarity < min_similarity: continue
# get the raw (unaligned) sequence for the exon that maps onto human
exon_seqs = get_exon_seqs(cursor, map.exon_id_2, map.exon_known_2, ensembl_db_name[map.species_2])
if (not exon_seqs):
#print " exon_seqs for" , map.source
continue
[pepseq, pepseq_transl_start,
pepseq_transl_end, left_flank, right_flank, dna_seq] = exon_seqs[1:]
# rpl11 starts with an exon that translates into 2 aa's,
# rpl10A has a single methionine (or so they say) followed by a split codon
# *supposedly there is evidence at the protein level
# but will this give me tons of junk elsewhere? ...
pepseq_noX = pepseq.replace ('X','')
if len(pepseq_noX)<3:
# if this is the first exon, and if it starts with M, we'll let it off the hook
# abd then if it's human, we'll also salvage it at any price
if first_human_exon and pepseq_noX[0] == 'M' or map.species_2=='homo_sapiens':
pass
else:
continue
# check
dnaseq = Seq (dna_seq[pepseq_transl_start:pepseq_transl_end], generic_dna)
if (mitochondrial):
pepseq2 = dnaseq.translate(table="Vertebrate Mitochondrial").tostring()
else:
pepseq2 = dnaseq.translate().tostring()
if (not pepseq == pepseq2):
continue
# inflate the compressed sequence
if not map.bitmap:
continue
bs = Bits(bytes=map.bitmap)
if (not bs.count(1) == len(pepseq)): continue # check bitmap has correct number of 1s
usi = iter(pepseq)
#reconst_pepseq = "".join(('-' if c=='0' else next(usi) for c in bs.bin))
reconst_pepseq = ''
for c in bs.bin:
if c == '0': reconst_pepseq += '-'
else: reconst_pepseq += next(usi)
# come up with a unique name for this sequence
species = map.species_2
# let's also have the start in gene here - might make our lives easier later
exon2 = get_exon (cursor, map.exon_id_2, map.exon_known_2, ensembl_db_name[species])
sequence_name = species + "_" + str(map.exon_id_2)+"_"+str(map.exon_known_2)+"_"+str(exon2.start_in_gene)
if reconst_pepseq:
sequence_pep[sequence_name] = reconst_pepseq
pep_aln_length = len(reconst_pepseq)
reconst_ntseq = expand_pepseq (reconst_pepseq, exon_seqs[1:], flank_length)
if reconst_ntseq:
sequence_dna[sequence_name] = reconst_ntseq
dna_aln_length = len(reconst_ntseq)
# strip common gaps
sequence_stripped_pep = strip_gaps (sequence_pep)
if not sequence_stripped_pep:
c=inspect.currentframe()
#print " in %s:%d" % ( c.f_code.co_filename, c.f_lineno)
return ['','']
# strip common gaps
sequence_stripped_dna = strip_gaps (sequence_dna)
if not sequence_stripped_dna:
c=inspect.currentframe()
#print " in %s:%d" % ( c.f_code.co_filename, c.f_lineno)
return ['', '']
return [sequence_stripped_pep, sequence_stripped_dna]
示例15: testFind
def testFind(self):
a = Bits('0xabcd')
r = a.find('0xbc')
self.assertEqual(r[0], 4)
r = a.find('0x23462346246', bytealigned=True)
self.assertFalse(r)