예제 #1
0
def dataDecompressing(compressingDictionary_, compressedData_):
    decompressedData_ = []

    priorPointer = compressedData_.pop(0)
    decompressedData_.append(compressingDictionary_[priorPointer])
    for nextPointer in compressedData_:
        lastKey = compressingDictionary_.keys()[-1]

        if nextPointer not in compressingDictionary_.keys():
            newEntry = list(
                listTools.flatten([
                    compressingDictionary_[priorPointer],
                    list(
                        listTools.flatten(
                            compressingDictionary_[priorPointer]))[0]
                ]))
            decompressedData_ += newEntry
        else:
            newEntry = list(
                listTools.flatten([
                    compressingDictionary_[priorPointer],
                    list(listTools.flatten(
                        compressingDictionary_[nextPointer]))[0]
                ]))
            decompressedData_ += list(
                listTools.flatten(compressingDictionary_[nextPointer]))

        compressingDictionary_[lastKey + 1] = newEntry
        priorPointer = nextPointer

    return decompressedData_
예제 #2
0
def dataCompressing(compressingDictionary_, rawData_):
    compressingDictionary_ = dict(compressingDictionary_)
    dataLength = len(rawData_)
    compressedData_ = []

    i = 0
    currentValue = rawData_[i]

    while i < dataLength - 1:
        lastKey = compressingDictionary_.keys()[-1]
        nextValue = rawData_[i + 1]

        currentEntry = list(listTools.flatten([currentValue, nextValue]))

        if currentEntry in compressingDictionary_.values():
            currentValue = currentEntry
        else:
            dictionaryKey = compressingDictionary_.keys()[
                compressingDictionary_.values().index(currentValue)]
            compressedData_.append(dictionaryKey)
            compressingDictionary_[lastKey + 1] = currentEntry
            currentValue = nextValue

        i += 1

    dictionaryKey = compressingDictionary_.keys()[
        compressingDictionary_.values().index(currentValue)]
    compressedData_.append(dictionaryKey)

    return compressedData_
예제 #3
0
def entropy(indexesList):
    indexesList = list(flatten(indexesList))
    indexHistogram = frequencyDictionary(indexesList)
    nIndexes = sum(indexHistogram.values())

    H = 0
    for indexFrequency in indexHistogram.values():
        Pr = float(indexFrequency) / nIndexes
        H += Pr * math.log(Pr, 2)

    if H > 0:
        H *= -1

    return H
예제 #4
0
def LZWHuffman(indexesList, compressionDictionary):
    indexesList = list(flatten(indexesList))

    compressedData = integerLZW.dataCompressing(compressionDictionary,
                                                indexesList)
    compressedData, huffmanDict = HuffmanCoding.dataCompressing(compressedData)

    dictionaryLength = len(compressionDictionary.keys())
    nBits = int(math.floor(math.log(dictionaryLength - 1) / math.log(2)) + 1)

    bitsRawData = len(indexesList) * nBits
    bitsCompressedData = len(compressedData)

    compressionRatio = (float(bitsRawData) / bitsCompressedData)

    return compressionRatio