def decode_bittools(bseq): """ Decompress Lempel-Ziv-Welch encoded sequence. Parameters ---------- bseq : numpy.ndarray Returns ------- seq : str See also -------- encode_bittools """ init_invtable = [(code, chr(code)) for code in range(256)] table = [chr(code) for code in range(256)] + ['CODECLEAR', 'CODEEOI'] table_append = table.append table_len = table.__len__ bits = 9 max_code2 = (1 << bits) - 2 i = 0 seq = [] seq_append = seq.append while True: code, i = getword(bseq, i, bits) if code == CODEEOI: break elif code == CODECLEAR: del table[CODESTART:] bits = 9 max_code2 = (1 << bits) - 2 code, i = getword(bseq, i, bits) old_str = table[code] seq_append(old_str) old_code = code else: l = table_len() if code < l: s = table[code] table_append(old_str + s[0]) old_str = s else: old_str = old_str + old_str[0] table_append(old_str) seq_append(old_str) old_code = code if l == max_code2: bits += 1 max_code2 = (1 << bits) - 2 return ''.join(seq)
def decode_bittools(bseq): """ Decompress Lempel-Ziv-Welch encoded sequence. Parameters ---------- bseq : numpy.ndarray Returns ------- seq : str See also -------- encode_bittools """ init_invtable = [(code, chr(code)) for code in range (256)] table = [chr(code) for code in range(256)] + ['CODECLEAR', 'CODEEOI'] table_append = table.append table_len = table.__len__ bits = 9 max_code2 = (1<<bits) - 2 i = 0 seq = [] seq_append = seq.append while True: code, i = getword(bseq, i, bits) if code==CODEEOI: break elif code==CODECLEAR: del table[CODESTART:] bits = 9 max_code2 = (1<<bits) - 2 code, i = getword(bseq, i, bits) old_str = table[code] seq_append(old_str) old_code = code else: l = table_len() if code < l: s = table[code] table_append(old_str + s[0]) old_str = s else: old_str = old_str + old_str[0] table_append(old_str) seq_append(old_str) old_code = code if l==max_code2: bits += 1 max_code2 = (1<<bits) - 2 return ''.join(seq)
def test_wordbits (): for width in range (1,64+1): arr = numpy.array([131,17,235], dtype=numpy.int64) arr2 = numpy.zeros((1+width//8), dtype=arr.dtype) bstr = tobinary(arr) word, next = bittools.getword(arr, 0, width) if width > 7: assert word==arr[0],repr((word, arr[0], bstr)) bittools.setword (arr2, 0, width, word) assert bittools.getword (arr2, 0, width)[0]==word assert tobinary(arr2)[:width] == bstr[:width],repr((tobinary(arr2)[:width], bstr[:width]))
def test_wordbits(): for width in range(1, 64 + 1): arr = numpy.array([131, 17, 235], dtype=numpy.int64) arr2 = numpy.zeros((1 + width // 8), dtype=arr.dtype) bstr = tobinary(arr) word, next = bittools.getword(arr, 0, width) if width > 7: assert word == arr[0], ` word, arr[0], bstr ` bittools.setword(arr2, 0, width, word) assert bittools.getword(arr2, 0, width)[0] == word assert tobinary(arr2)[:width] == bstr[:width], ` tobinary( arr2)[:width], bstr[:width] `
def test_setgetword(): for dtype in [numpy.ubyte, numpy.int32, numpy.float64]: arr = numpy.array(range(-256, 256), dtype=dtype) arr2 = numpy.zeros(arr.shape, dtype=arr.dtype) for i in range(arr.nbytes): word, next = bittools.getword(arr, i * 8, 8) bittools.setword(arr2, i * 8, 8, word) assert (arr == arr2).all(), ` arr, arr2 ` print 'ok'
def test_setgetword(): for dtype in [numpy.ubyte, numpy.int32, numpy.float64]: arr = numpy.array(list(range(-256,256)), dtype=dtype) arr2 = numpy.zeros(arr.shape, dtype=arr.dtype) for i in range (arr.nbytes): word, next = bittools.getword (arr,i*8,8) bittools.setword (arr2,i*8,8,word) assert (arr==arr2).all(),repr((arr,arr2)) print('ok')