for node in nodes:
            codeLength += len(node.code) * node.prob
        return codeLength

    remainingElements = [
        Node(prob=pList[x], index=x) for x in xrange(0, len(pList))
    ]

    while len(remainingElements) > 1:
        remainingElements = aggregate(remainingElements)

    buildCodebook(remainingElements[0], "")
    codeBook.sort(key=lambda x: x.index)
    return ([x.code for x in codeBook], findCodeLength(codeBook))


if __name__ == "__main__":
    import sys
    import numpy
    import PS1bin
    if len(sys.argv) > 1:
        pList = numpy.array(eval(sys.argv[1]))
    else:
        pList = PS1bin.get_dist(5)
    print 'pList = ', pList
    (codeBook, codeLength) = huffman(pList)
    print 'codeLength = ', codeLength
    print 'codeBook:'
    for x in codeBook:
        print x
            buildCodebook(node.right, currentCode + "0")
    def findCodeLength(nodes):
        codeLength = 0.0
        for node in nodes:
            codeLength += len(node.code)*node.prob
        return codeLength
    remainingElements = [Node(prob=pList[x],index=x) for x in
            xrange(0,len(pList))]
    
    while len(remainingElements) > 1:
        remainingElements = aggregate(remainingElements)

    buildCodebook(remainingElements[0], "")
    codeBook.sort(key=lambda x: x.index)
    return ([x.code for x in codeBook], findCodeLength(codeBook))

if __name__ == "__main__":
    import sys
    import numpy
    import PS1bin
    if len(sys.argv)>1:
        pList = numpy.array(eval(sys.argv[1]))
    else:
        pList = PS1bin.get_dist(5)
    print 'pList = ', pList
    (codeBook, codeLength) = huffman(pList)
    print 'codeLength = ', codeLength
    print 'codeBook:'
    for x in codeBook:
        print x
Example #3
0
import sys
import random
import numpy as np
import PS1bin
import PS13mycode

if __name__ == "__main__":
    for maxInput, tableSize in [(2,5), (3,10)]:
        print '\n\ntesting with maxInput=%d, tableSize=%d'%(maxInput,tableSize)
        iA = range(maxInput)
        cA = range(tableSize)
        for i in xrange(10):
            print '..'
            msg = [random.choice(iA) for j in xrange(20)]
            cmsg = PS13mycode.compress(msg,maxInput,tableSize)
            assert type(cmsg)==type([]), "encoded message not a list"
            for x in cmsg:
                assert x in cA, "coded message out of range"
            dmsg = PS13mycode.uncompress(cmsg,maxInput,tableSize)
            assert type(dmsg)==type([]), "decoded message not a list"
            for x in dmsg:
                assert x in iA, "decoded message out of range"
            print msg, '->', cmsg
            assert PS1bin.chk3(msg,cmsg,maxInput,tableSize), "incorrect encoding"
            print dmsg
            assert len(msg)==len(dmsg), "incorrect length after decoding"
            for i in xrange(len(msg)):
                assert msg[i]==dmsg[i], "incorrect decoding"
                
    print '\n\nGREAT! NO ERRORS FOUND'  
Example #4
0
import sys
import numpy as np
import PS1bin
import PS11mycode


if __name__ == "__main__":
    for n in [4, 5, 8, 10, 20]:
        pp = PS1bin.get_dist(n)
        ppl = len(pp)
        print '\n\nchecking with pList = ', pp
        print '...'
        (codeBook, codeLength) = PS11mycode.huffman(pp.copy())
        print 'your codeBook ( codeLength = ', codeLength, ' ):'
        print codeBook
        assert type(codeBook)==type([]), "code book is not a list"
        assert ppl==len(codeBook), "wrong code book length"
        for word in codeBook:
            assert type(word)==type(''), "code word not a string"
            for x in word:
                assert x in ['0','1'], "code word has symbols other than 1 or 0"
        for i in xrange(ppl):
            for j in xrange(ppl):
                if i != j:
                    assert not codeBook[i].startswith(codeBook[j]),\
                           "code book is not prefix-free"
        s = 0.0
        for i in xrange(ppl):
            s += pp[i]*len(codeBook[i])
        assert abs(s-codeLength)<1e-6, "computed and actual code length do not match"
        assert PS1bin.chk1(pp,codeLength), "code book is far from optimal"    
Example #5
0
import sys
import random
import numpy as np
import PS1bin
import PS12mycode

alp = "ABCDEFGH"

if __name__ == "__main__":
    for n in [4, 5, 8]:
        cb = PS1bin.get_cb(n)
        ppl = len(cb)
        alph = range(ppl)
        print "\n\nchecking with code book"
        for word in cb:
            print word
        for i in xrange(5):
            print ".."
            msg = [random.choice(alph) for j in xrange(10)]
            cmsg = PS12mycode.encode(cb, msg)
            assert type(cmsg) == type(""), "encoded message not a string"
            dmsg = PS12mycode.decode(cb, cmsg)
            assert type(dmsg) == type([]), "decoded message not a list"
            for word in dmsg:
                assert type(word) == type(0), "decoded message not a lst of integers"
            print msg, "  ->  ", cmsg
            assert PS1bin.chk2(msg, cmsg, cb), "incorrect encoding"
            print dmsg
            assert len(dmsg) == len(msg), "incorrect length after decoding"
            for i in xrange(len(msg)):
                assert msg[i] == dmsg[i], "incorrect decoding"