Python源码示例:bitarray.bitarray()

示例1
def __init__(self, container):
        USBDevice.__init__(self, container)
        self.data = bitarray(48, endian='little')
        self.data.setall(False)
        self.start_time = datetime.datetime.now()
        self.isNunchukCalibrated = False
        self.center_x = 1
        self.x_neg_range = 1
        self.x_pos_range = 1

        self.center_y = 1
        self.y_neg_range = 1
        self.y_pos_range = 1

        self.previousAccX = 0
        self.previousAccY = 0 
示例2
def get_huffman_codebook(symb2freq):
    """
    Huffman encode the given dict mapping symbols to weights
    :param symb2freq: dict, {symbol: frequency}
    :return:
        dict, value(float/int) : code(bitarray)
    """
    heap = [[wt, [sym, ""]] for sym, wt in symb2freq.items()]
    heapify(heap)
    while len(heap) > 1:
        lo = heappop(heap)
        hi = heappop(heap)
        for pair in lo[1:]:
            pair[1] = '0' + pair[1]
        for pair in hi[1:]:
            pair[1] = '1' + pair[1]
        heappush(heap, [lo[0] + hi[0]] + lo[1:] + hi[1:])
    codebook = sorted(heappop(heap)[1:], key=lambda p: (len(p[-1]), p))
    return dict(map(lambda x: (x[0], bitarray(x[1])), codebook)) 
示例3
def __read__(self, fname, m, n):
        if not fname.endswith('.bed'):
            raise ValueError('.bed filename must end in .bed')

        fh = open(fname, 'rb')
        magicNumber = ba.bitarray(endian="little")
        magicNumber.fromfile(fh, 2)
        bedMode = ba.bitarray(endian="little")
        bedMode.fromfile(fh, 1)
        e = (4 - n % 4) if n % 4 != 0 else 0
        nru = n + e
        self.nru = nru
        # check magic number
        if magicNumber != ba.bitarray('0011011011011000'):
            raise IOError("Magic number from Plink .bed file not recognized")

        if bedMode != ba.bitarray('10000000'):
            raise IOError("Plink .bed file must be in default SNP-major mode")

        # check file length
        self.geno = ba.bitarray(endian="little")
        self.geno.fromfile(fh)
        self.__test_length__(self.geno, self.m, self.nru)
        return (self.nru, self.geno) 
示例4
def decode_full_lc(_data):
    binlc = bitarray(endian='big')   
    binlc.extend([_data[136],_data[121],_data[106],_data[91], _data[76], _data[61], _data[46], _data[31]])
    binlc.extend([_data[152],_data[137],_data[122],_data[107],_data[92], _data[77], _data[62], _data[47], _data[32], _data[17], _data[2]  ])
    binlc.extend([_data[123],_data[108],_data[93], _data[78], _data[63], _data[48], _data[33], _data[18], _data[3],  _data[184],_data[169]])
    binlc.extend([_data[94], _data[79], _data[64], _data[49], _data[34], _data[19], _data[4],  _data[185],_data[170],_data[155],_data[140]])
    binlc.extend([_data[65], _data[50], _data[35], _data[20], _data[5],  _data[186],_data[171],_data[156],_data[141],_data[126],_data[111]])
    binlc.extend([_data[36], _data[21], _data[6],  _data[187],_data[172],_data[157],_data[142],_data[127],_data[112],_data[97], _data[82] ])
    binlc.extend([_data[7],  _data[188],_data[173],_data[158],_data[143],_data[128],_data[113],_data[98], _data[83]])
    '''
    This is the rest of the Full LC data -- the RS1293 FEC that we don't need
     _data[68],_data[53],_data[174],_data[159],_data[144],_data[129],_data[114],_data[99],_data[84],_data[69],_data[54],_data[39],
    _data[24],_data[145],_data[130],_data[115],_data[100],_data[85],_data[70],_data[55],_data[40],_data[25],_data[10],_data[191]
    '''
    return binlc

#------------------------------------------------------------------------------
# BPTC(196,96) Encoding Routings
#------------------------------------------------------------------------------ 
示例5
def encode_19696(_data):
    # Create a bitarray from the 4 bytes of LC data (includes RS1293 ECC)
    _bdata = bitarray(endian='big')
    _bdata.frombytes(_data)
    
    # Insert R0-R3 bits
    for i in xrange(4):
        _bdata.insert(0, 0)
    
    # Get row hamming 15,11,3 and append. +1 is to account for R3 that makes an even 196bit string
    for index in xrange(9):
        spos = (index*15) + 1
        epos= spos + 11
        _rowp = hamming.enc_15113(_bdata[spos:epos])
        for pbit in xrange(4):
            _bdata.insert(epos+pbit,_rowp[pbit])
    
    # Get column hamming 13,9,3 and append. +1 is to account for R3 that makes an even 196bit string
    # Pad out the bitarray to a full 196 bits. Can't insert into 'columns'
    for i in xrange(60):
        _bdata.append(0)
    
    column = bitarray(9, endian='big')  # Temporary bitarray to hold column data
    for col in xrange(15):
        spos = col + 1
        for index in xrange(9):
            column[index] = _bdata[spos]
            spos += 15
        _colp = hamming.enc_1393(column)
        
        # Insert bits into matrix...
        cpar = 136 + col                # Starting location in the matrix for column bits
        for pbit in xrange(4):
            _bdata[cpar] =  _colp[pbit]
            cpar += 15

    return _bdata 
示例6
def decode_emblc(_elc):
    
    _binlc = bitarray(endian='big')
    _binlc.extend([_elc[0],_elc[8], _elc[16],_elc[24],_elc[32],_elc[40],_elc[48],_elc[56],_elc[64],_elc[72] ,_elc[80]])
    _binlc.extend([_elc[1],_elc[9], _elc[17],_elc[25],_elc[33],_elc[41],_elc[49],_elc[57],_elc[65],_elc[73] ,_elc[81]])
    _binlc.extend([_elc[2],_elc[10],_elc[18],_elc[26],_elc[34],_elc[42],_elc[50],_elc[58],_elc[66],_elc[74]])
    _binlc.extend([_elc[3],_elc[11],_elc[19],_elc[27],_elc[35],_elc[43],_elc[51],_elc[59],_elc[67],_elc[75]])
    _binlc.extend([_elc[4],_elc[12],_elc[20],_elc[28],_elc[36],_elc[44],_elc[52],_elc[60],_elc[68],_elc[76]])
    _binlc.extend([_elc[5],_elc[13],_elc[21],_elc[29],_elc[37],_elc[45],_elc[53],_elc[61],_elc[69],_elc[77]])
    _binlc.extend([_elc[6],_elc[14],_elc[22],_elc[30],_elc[38],_elc[46],_elc[54],_elc[62],_elc[70],_elc[78]])
    
    return(_binlc.tobytes())

#------------------------------------------------------------------------------
# BPTC Embedded LC Encoding Routines
#------------------------------------------------------------------------------


# Accepts 12 byte LC header + 5-bit checksum, converts to binary and builts out the BPTC
# encoded result with hamming(16,11,4) and parity. 
示例7
def _compute_coverage_scores(self):
        # Get total number of ORIGIN elements FIXME: (not KW, etc)
        (leafs, _) = self._provenance.get_leafs_and_heads()
        total_number = len(leafs)

        i = 0
        for origin in leafs:
            # Assign index to original values
            self._origin_values_coverage[origin] = i
            i += 1

        for el in self.data:
            # initialize coverage set to False
            coverage_set = bitarray(len(leafs))
            coverage_set.setall(False)
            # Get covered elements
            elements = self.why(el)
            for element_covered in elements:
                idx = self._origin_values_coverage[element_covered]
                coverage_set[idx] = True
            coverage = float(len(elements)) / float(total_number)
            self._rank_data[el]['coverage_score'] = (coverage, coverage_set) 
示例8
def transpose_low_mem(bitarrays):
    logger.info("Using slow, low memory transpose")
    # Takes a list of bitarrays and returns the transpose as a list of
    # bitarrays
    x = len(bitarrays)
    y = bitarrays[0].length()
    logger.info("BFM dims %i %i" % (x, y))

    tbitarrays = []
    for ii in range(y):
        ba = bitarray(x)
        ba.setall(False)
        tbitarrays.append(ba)
    for i in range(x):
        for j in range(y):
            tbitarrays[j][i] = bitarrays[i][j]
    return tbitarrays 
示例9
def test_get_set():
    rows = [
        bitarray("001"),
        bitarray("001"),
        bitarray("111"),
        bitarray("001"),
        bitarray("111"),
    ] * 5
    for storage in get_storages():
        storage.delete_all()
        bm = BitMatrix.create(storage, rows, len(rows), len(rows[0]))
        bm.set_rows(range(25), rows)
        assert list(bm.get_rows(range(3))) == rows[:3]
        assert bm.get_column(0) == bitarray("00101" * 5)
        assert bm.get_column(2) == bitarray("1" * 25)
        assert list(bm.get_columns([0, 2])) == [
            bitarray("00101" * 5),
            bitarray("1" * 25),
        ] 
示例10
def test_get_insert_column():
    rows = [
        bitarray("001"),
        bitarray("001"),
        bitarray("111"),
        bitarray("001"),
        bitarray("111"),
    ] * 5
    for storage in get_storages():
        storage.delete_all()
        bm = BitMatrix.create(storage, rows, len(rows), len(rows[0]))
        assert bm.get_column(0) == bitarray("00101" * 5)
        bm.insert_column(bitarray("1" * 25), 0)
        assert bm.get_column(0) == bitarray("1" * 25)

        assert bm.get_row(1) == bitarray("101")
        bm.insert_column(bitarray("1" * 25), 3)
        assert bm.get_column(3) == bitarray("1" * 25)
        assert bm.get_row(1) == bitarray("1011") 
示例11
def test_bloom_cmd():
    for config_file in CONFIG_FILES:
        f = "/tmp/test_kmers.bloom"
        response = hug.test.post(
            bigsi.__main__,
            "bloom",
            {
                "config": config_file,
                "ctx": "bigsi/tests/data/test_kmers.ctx",
                "outfile": f,
            },
        )
        a = bitarray()
        with open("/tmp/test_kmers.bloom", "rb") as inf:
            a.fromfile(inf)
        assert sum(a) > 0
        os.remove("/tmp/test_kmers.bloom") 
示例12
def program_global_reg(self):
        """
        Send the global register to the chip.

        Loads the values of self['GLOBAL_REG'] onto the chip.
        Includes enabling the clock, and loading the Control (CTR)
        and DAC shadow registers.

        """

        self._clear_strobes()

        gr_size = len(self['GLOBAL_REG'][:])  # get the size
        self['SEQ']['SHIFT_IN'][0:gr_size] = self['GLOBAL_REG'][:]  # this will be shifted out
        self['SEQ']['GLOBAL_SHIFT_EN'][0:gr_size] = bitarray(gr_size * '1')  # this is to enable clock
        self['SEQ']['GLOBAL_CTR_LD'][gr_size + 1:gr_size + 2] = bitarray("1")  # load signals
        self['SEQ']['GLOBAL_DAC_LD'][gr_size + 1:gr_size + 2] = bitarray("1")

        # Execute the program (write bits to output pins)
        # + 1 extra 0 bit so that everything ends on LOW instead of HIGH
        self._run_seq(gr_size + 3) 
示例13
def program_pixel_reg(self, enable_receiver=True):
        """
        Send the pixel register to the chip and store the output.

        Loads the values of self['PIXEL_REG'] onto the chip.
        Includes enabling the clock, and loading the Control (CTR)
        and DAC shadow registers.

        if(enable_receiver), stores the output (by byte) in
        self['DATA'], retrievable via `chip['DATA'].get_data()`.

        """

        self._clear_strobes()

        # enable receiver it work only if pixel register is enabled/clocked
        self['PIXEL_RX'].set_en(enable_receiver)

        px_size = len(self['PIXEL_REG'][:])  # get the size
        self['SEQ']['SHIFT_IN'][0:px_size] = self['PIXEL_REG'][:]  # this will be shifted out
        self['SEQ']['PIXEL_SHIFT_EN'][0:px_size] = bitarray(px_size * '1')  # this is to enable clock

        print('px_size = {}'.format(px_size))

        self._run_seq(px_size + 1)  # add 1 bit more so there is 0 at the end other way will stay high 
示例14
def program_global_reg(self):
        """
        Send the global register to the chip.

        Loads the values of self['GLOBAL_REG'] onto the chip.
        Includes enabling the clock, and loading the Control (CTR)
        and DAC shadow registers.

        """

        self._clear_strobes()

        gr_size = len(self['GLOBAL_REG'][:])  # get the size
        self['SEQ']['SHIFT_IN'][0:gr_size] = self['GLOBAL_REG'][:]  # this will be shifted out
        self['SEQ']['GLOBAL_SHIFT_EN'][0:gr_size] = bitarray(gr_size * '1')  # this is to enable clock
        self['SEQ']['GLOBAL_CTR_LD'][gr_size + 1:gr_size + 2] = bitarray("1")  # load signals
        self['SEQ']['GLOBAL_DAC_LD'][gr_size + 1:gr_size + 2] = bitarray("1")

        # Execute the program (write bits to output pins)
        # + 1 extra 0 bit so that everything ends on LOW instead of HIGH
        self._run_seq(gr_size + 3) 
示例15
def program_pixel_reg(self, enable_receiver=True):
        """
        Send the pixel register to the chip and store the output.

        Loads the values of self['PIXEL_REG'] onto the chip.
        Includes enabling the clock, and loading the Control (CTR)
        and DAC shadow registers.

        if(enable_receiver), stores the output (by byte) in
        self['DATA'], retrievable via `chip['DATA'].get_data()`.

        """

        self._clear_strobes()

        # enable receiver it work only if pixel register is enabled/clocked
        self['PIXEL_RX'].set_en(enable_receiver)

        px_size = len(self['PIXEL_REG'][:])  # get the size
        self['SEQ']['SHIFT_IN'][0:px_size] = self['PIXEL_REG'][:]  # this will be shifted out
        self['SEQ']['PIXEL_SHIFT_EN'][0:px_size] = bitarray(px_size * '1')  # this is to enable clock

        self._run_seq(px_size + 1)  # add 1 bit more so there is 0 at the end other way will stay high 
示例16
def test_simple(self):
        input_arr = bitarray('10' * 64)

        self.chip['PIXEL_REG'][:] = input_arr
        self.chip['PIXEL_REG'][0] = 0
        self.chip.program_pixel_reg()

        ret = self.chip['DATA'].get_data()

        data0 = ret.astype(np.uint8)
        data1 = np.right_shift(ret, 8).astype(np.uint8)
        data = np.reshape(np.vstack((data1, data0)), -1, order='F')
        bdata = np.unpackbits(data)

        input_arr[0] = 0

        self.assertEqual(input_arr.tolist(), bdata.tolist()) 
示例17
def write(self, size=-1):
        if size == -1:
            size = self._conf["seq_size"]

        bv = bitarray(self._conf["seq_width"] * size)
        for i in range(size):
            for track in self._conf['tracks']:
                bit = 0
                if self._conf["seq_width"] >= 8:
                    bit = i * self._conf["seq_width"] + self._conf["seq_width"] - 1 - track['position']
                elif self._conf["seq_width"] == 4:
                    if i % 2 == 0:
                        bit = (i + 1) * self._conf["seq_width"] + self._conf["seq_width"] - 1 - track['position']
                    else:
                        bit = (i - 1) * self._conf["seq_width"] + self._conf["seq_width"] - 1 - track['position']
                else:
                    raise NotImplementedError("To be implemented.")
                bv[bit] = self._tracks[track['name']][i]

        ba = utils.bitarray_to_byte_array(bv)
        ba = ba[::-1]
        # TODO: this probably has to be done different way
        self._drv.set_data(ba) 
示例18
def parse_bredr(self, packet, pre=PROMISC_MAC, max_depth=(PAYLOAD_SIZE*8)-64):
        bits = bitarray.bitarray()
        bits.frombytes(pre + packet)
        bits_data = bytes(bits.tolist())

        offset = BTBB.btbb_find_ac(bits_data,
                                   max_depth,
                                   0xffffffff,
                                   0,
                                   byref(self._btbb_packet))
        if offset < 0:
            return
        bits_data = bits_data[offset:]

        lap = BTBB.btbb_packet_get_lap(self._btbb_packet)
        BTBB.btbb_packet_set_data(self._btbb_packet, bits_data, len(bits_data), 0, 0)

        # Bruteforce CLK1-6 values (5 bits)
        uap_candidates = set([BTBB.try_clock(i, self._btbb_packet)
                              for i in range(64)])
        return (lap, uap_candidates) 
示例19
def __read__(self, fname, m, n):
        if not fname.endswith('.bed'):
            raise ValueError('.bed filename must end in .bed')

        fh = open(fname, 'rb')
        magicNumber = ba.bitarray(endian="little")
        magicNumber.fromfile(fh, 2)
        bedMode = ba.bitarray(endian="little")
        bedMode.fromfile(fh, 1)
        e = (4 - n % 4) if n % 4 != 0 else 0
        nru = n + e
        self.nru = nru
        # check magic number
        if magicNumber != ba.bitarray('0011011011011000'):
            raise IOError("Magic number from Plink .bed file not recognized")

        if bedMode != ba.bitarray('10000000'):
            raise IOError("Plink .bed file must be in default SNP-major mode")

        # check file length
        self.geno = ba.bitarray(endian="little")
        self.geno.fromfile(fh)
        self.__test_length__(self.geno, self.m, self.nru)
        return (self.nru, self.geno) 
示例20
def random_syn_masks(self):
        import bitarray
        workspace_path = os.environ.get('AE_WORKSPACE_PATH')

        random_syn_masks = bitarray.bitarray()
        with open(os.path.join(workspace_path,'random_tless_masks/arbitrary_syn_masks_1000.bin'), 'r') as fh:
            random_syn_masks.fromfile(fh)
        occlusion_masks = np.fromstring(random_syn_masks.unpack(), dtype=np.bool)
        occlusion_masks = occlusion_masks.reshape(-1,224,224,1).astype(np.float32)
        print(occlusion_masks.shape)

        occlusion_masks = np.array([cv2.resize(mask,(self.shape[0],self.shape[1]), interpolation = cv2.INTER_NEAREST) for mask in occlusion_masks])
        return occlusion_masks 
示例21
def get_vanilla_codebook(symb):
    codebook = dict()
    symb = set(symb)
    bit_length = int(math.ceil(math.log(len(symb), 2)))
    bit_format = '{:0%db}' % bit_length
    for i, s in enumerate(symb):
        codebook[s] = bitarray(bit_format.format(i))
    return codebook 
示例22
def __init__(self, fname, n, snp_list, keep_snps=None, keep_indivs=None, mafMin=None):
        self._bedcode = {
            2: ba.bitarray('11'),
            9: ba.bitarray('10'),
            1: ba.bitarray('01'),
            0: ba.bitarray('00')
            }

        __GenotypeArrayInMemory__.__init__(self, fname, n, snp_list, keep_snps=keep_snps,
            keep_indivs=keep_indivs, mafMin=mafMin) 
示例23
def __filter_indivs__(self, geno, keep_indivs, m, n):
        n_new = len(keep_indivs)
        e = (4 - n_new % 4) if n_new % 4 != 0 else 0
        nru_new = n_new + e
        nru = self.nru
        z = ba.bitarray(m*2*nru_new, endian="little")
        for e, i in enumerate(keep_indivs):
            z[2*e::2*nru_new] = geno[2*i::2*nru]
            z[2*e+1::2*nru_new] = geno[2*i+1::2*nru]

        self.nru = nru_new
        return (z, m, n_new) 
示例24
def test_filter_snps(self):
        keep_snps = [1, 4]
        bed = ld.PlinkBEDFile('test/plink_test/plink.bed', self.N, self.bim,
                              keep_snps=keep_snps)
        assert bed.m == 1
        assert bed.n == 5
    # pad bits are initialized with random memory --> can't test them
        assert bed.geno[0:10] == ba.bitarray('0001011111') 
示例25
def test_filter_indivs_and_snps(self):
        keep_indivs = [0, 1]
        keep_snps = [1, 5]
        bed = ld.PlinkBEDFile('test/plink_test/plink.bed', self.N, self.bim,
                              keep_snps=keep_snps, keep_indivs=keep_indivs)
        assert bed.m == 1
        assert bed.n == 2
        print bed.geno
        assert bed.geno[0:4] == ba.bitarray('0001') 
示例26
def interleave_19696(_data):
    inter = bitarray(196, endian='big')
    for index in xrange(196):
        inter[INDEX_181[index]] = _data[index]  # the real math is slower: deint[index] = _data[(index * 181) % 196]
    return inter

# Accepts 12 byte LC header + RS1293, converts to binary and pads for 196 bit
# encode hamming 15113 to rows and 1393 to columns 
示例27
def encode_voice( self, _ambe, _rx_slot ):
        _frame_type = _rx_slot.vf
        if _frame_type > 0:                                                 # if not a SYNC frame cccxss
            index = (_rx_slot.cc << 3) | self.lcss[_frame_type]             # index into the encode table makes this a simple lookup
            emb = bitarray(format(qr.ENCODE_1676[ index ], '016b'))         # create emb of 16 bits
            embedded = emb[8:16] + _rx_slot.emblc[_frame_type] + emb[0:8]   # Take emb and a chunk of the embedded LC and combine them into 48 bits
        else:
            embedded = BitArray(DMR_VOICE_SYNC_MS)                          # Voice SYNC (48 bits)
        _new_frame = _ambe[0:108] +  embedded + _ambe[108:216]              # Construct the dmr frame from AMBE(108 bits) + sync/emb (48 bits) + AMBE(108 bits)
        return _new_frame
    
    # Create a voice terminator DMR frame 
示例28
def to_bits(_string):
    _bits = bitarray(endian='big')
    _bits.frombytes(_string)
    return _bits 
示例29
def csum5(_data):
    _data = bytearray(_data)
    accum = 0
    assert len(_data) == 9, 'csum5 expected 9 bytes of data and got something else'
    
    for i in xrange(9):
        accum += _data[i]
    accum = chr(accum % 31)
    csum = bitarray()
    csum.frombytes(accum)
    del csum[0:3]

    return csum 
示例30
def eccAmbe3600x2450Data(ambe_fr):
    ambe = bitarray()
    
    # just copy C0
    for j in range(23, 11, -1):
        ambe.append(ambe_fr[0][j])
    
#        # ecc and copy C1
#        gin = 0
#        for j in range(23):
#            gin = (gin << 1) | ambe_fr[1][j]
#
#        gout = BitArray(hex(golay2312(gin)))
#        for j in range(22, 10, -1):
#            ambe[bitIndex] = gout[j]
#            bitIndex += 1
    for j in range(22, 10, -1):
        ambe.append(ambe_fr[1][j])

    # just copy C2
    for j in range(10, -1, -1):
        ambe.append(ambe_fr[2][j])

    # just copy C3
    for j in range(13, -1, -1):
        ambe.append(ambe_fr[3][j])

    return ambe

# Convert a 49 bit raw AMBE frame into a deinterleaved structure (ready for decode by AMBE3000)