# file for PS1, Python Task 3 import matplotlib.pyplot as p import numpy,os import PS1_tests from PS1_1 import huffman from PS1_2 import decode if __name__ == '__main__': # read in the image, convert into vector of pixels img = p.imread('PS1_fax_image.png') nrows,ncols,pixels = PS1_tests.img2pixels(img) # convert the image into a sequence of alternating # white and black runs, with a maximum run length # of 255 (longer runs are converted into multiple # runs of 255 followed by a run of 0 of the other # color). So each element of the list is a number # between 0 and 255. runs = PS1_tests.pixels2runs(pixels,maxrun=255) # now print out number of bits for pixel-by-pixel # encoding and fixed-length encoding for runs print "Baseline 0:" print " bits to encode pixels:",pixels.size print "\nBaseline 1:" print " total number of runs:",runs.size print " bits to encode runs with fixed-length code:",\ 8*runs.size print "\nBaseline 2:"
def compare(a, b) : if len(a) != len(b) : return False for i in range(len(a)) : if a[i] != b[i] : return False return True if __name__ == '__main__': # start by building Huffman tree from probabilities plist = ((0.34,'A'),(0.5,'B'),(0.08,'C'),(0.08,'D')) cdict = huffman(plist) # test case 1: decode a simple message message = ['A', 'B', 'C', 'D'] encoded_message = PS1_tests.encode(cdict,message) decoded_message = decode(cdict,encoded_message) assert message == decoded_message, \ "Decoding failed: expected %s, got %s" % \ (message,decoded_message) # test case 2: construct a random message and encode it message = [random.choice('ABCD') for i in xrange(100)] encoded_message = PS1_tests.encode(cdict,message) decoded_message = decode(cdict,encoded_message) assert message == decoded_message, \ "Decoding failed: expected %s, got %s" % \ (message,decoded_message) print "Tests passed!"
if len(a) != len(b): return False for i in range(len(a)): if a[i] != b[i]: return False return True if __name__ == '__main__': # start by building Huffman tree from probabilities plist = ((0.34, 'A'), (0.5, 'B'), (0.08, 'C'), (0.08, 'D')) cdict = huffman(plist) # test case 1: decode a simple message message = ['A', 'B', 'C', 'D'] encoded_message = PS1_tests.encode(cdict, message) decoded_message = decode(cdict, encoded_message) assert message == decoded_message, \ "Decoding failed: expected %s, got %s" % \ (message,decoded_message) # test case 2: construct a random message and encode it message = [random.choice('ABCD') for i in xrange(100)] encoded_message = PS1_tests.encode(cdict, message) decoded_message = decode(cdict, encoded_message) assert message == decoded_message, \ "Decoding failed: expected %s, got %s" % \ (message,decoded_message) print "Tests passed!"
if type(left) == tuple: codebook = tree_to_codebook(left, codebook, prefix + [0]) else: codebook[left] = prefix + [0] if type(right) == tuple: codebook = tree_to_codebook(right, codebook, prefix + [1]) else: codebook[right] = prefix + [1] return codebook if __name__ == '__main__': # test case 1: four symbols with equal probability PS1_tests.test_huffman( huffman, # symbol probabilities ((0.25, 'A'), (0.25, 'B'), (0.25, 'C'), (0.25, 'D')), # expected encoding lengths ((2, 'A'), (2, 'B'), (2, 'C'), (2, 'D'))) # test case 2: example from section 22.3 in notes PS1_tests.test_huffman( huffman, # symbol probabilities ((0.34, 'A'), (0.5, 'B'), (0.08, 'C'), (0.08, 'D')), # expected encoding lengths ((2, 'A'), (1, 'B'), (3, 'C'), (3, 'D'))) # test case 3: example from Exercise 5 in notes PS1_tests.test_huffman( huffman, # symbol probabilities
interium[-1][0] = interium[-2][0] + interium[-1][0] for f in interium[-2][1] : interium[-1][1].insert(0, f) interium.pop(-2) interium.sort(key=lambda x:x[0], reverse=True) return rlt if __name__ == '__main__': # test case 1: four symbols with equal probability PS1_tests.test_huffman(huffman, # symbol probabilities ((0.25,'A'),(0.25,'B'),(0.25,'C'), (0.25,'D')), # expected encoding lengths ((2,'A'),(2,'B'),(2,'C'),(2,'D'))) # test case 2: example from section 22.3 in notes PS1_tests.test_huffman(huffman, # symbol probabilities ((0.34,'A'),(0.5,'B'),(0.08,'C'), (0.08,'D')), # expected encoding lengths ((2,'A'),(1,'B'),(3,'C'),(3,'D'))) # test case 3: example from Exercise 5 in notes PS1_tests.test_huffman(huffman, # symbol probabilities ((0.07,'I'),(0.23,'II'),(0.07,'III'),