Esempio n. 1
0
def decompress(compressedfile, output, treefile='treefile.json'):
    """
  File now written to with bitstring 
  """

    with open(compressedfile, 'rb') as f:
        b = BitArray(f.read())

    tree = json.load(open(treefile))
    """
  Binary values returned here as string
  need to create a loop to go through and scan until a match it found
  start with 1 bit, loop until ended
  write to file when bit is found
  """

    newfile = open(output, "a", encoding="utf-8")
    while len(b.bin) >= 0:
        x = 0
        i = 1
        while x == 0:
            tosearch = b.bin[0:i]
            try:
                towrite = list(tree.keys())[list(
                    tree.values()).index(tosearch)]
                newfile.write(towrite)
                newfile.close()
                newfile = open(output, "a", encoding="utf-8")
                b.bin = b.bin[i:]
                x = 1
            except:
                i += 1
Esempio n. 2
0
def compress(text):
    tree = get_tree(text)
    decompression_tree = get_decompression_tree(tree)

    compressed_text = ""
    for c in text:
        compressed_text += get_code(c, tree)
    # Add filler to get to a round nb of bytes
    compressed_text += '0' * (8 - (len(compressed_text) % 8)) 

    binary = BitArray()
    binary.bin = compressed_text

    return binary, decompression_tree
Esempio n. 3
0
def get_weights(firstGeneration=False, DNA=''):
    if firstGeneration:
        #Generate random DNA for the first generation
        DNAlength = (inputCount * neuronsPerLayer + neuronsPerLayer +
                     ((neuronsPerLayer * neuronsPerLayer) * numHiddenLayers) +
                     neuronsPerLayer * outputNeurons +
                     neuronsPerLayer * numHiddenLayers + outputNeurons)
        DNAbin = []
        for i in range(0, DNAlength * 12):
            DNAbin.append(str(random.randint(0, 1)))

        DNAbin = ''.join(DNAbin)
        b = BitArray(int=0, length=12)
        DNA = []
        for i in range(0, len(DNAbin), 12):
            b.bin = DNAbin[i:i + 12]
            DNA.append(float(b.int))
    else:
        DNAbin = DNA
        DNA = []
        b = BitArray(int=0, length=12)
        for i in range(0, len(DNAbin), 12):
            b.bin = DNAbin[i:i + 12]
            DNA.append(float(b.int))

    #Convert DNA to list of weights
    weights = []
    count = 0
    #Normalise our converted DNA to floats between -0.5 and 0.5
    DNA = 2 * (DNA - np.float32(-2048)) / (np.float32(2047) -
                                           np.float32(-2048)) - 1

    #Convert list of weights into shaped arrays for each layer
    #Input weights: inputCount x neuronsPerLayer matrix
    l = np.array(DNA[count:count + inputCount * neuronsPerLayer])
    count += inputCount * neuronsPerLayer
    l = l.reshape(inputCount, neuronsPerLayer)
    weights.append(l)
    #Input activation weights: neuronsPerLayer array
    l = np.array(DNA[count:count + neuronsPerLayer])
    count += neuronsPerLayer
    l = l.reshape(neuronsPerLayer)
    weights.append(l)

    for i in range(numHiddenLayers):
        #Hidden layer weights: neuronsPerLayer x neuronsPerLayer matrix
        l = np.array(DNA[count:count + neuronsPerLayer * neuronsPerLayer])
        count += neuronsPerLayer * neuronsPerLayer
        l = l.reshape(neuronsPerLayer, neuronsPerLayer)
        weights.append(l)
        #Hidden activation weights: neuronsPerLayer array
        l = np.array(DNA[count:count + neuronsPerLayer])
        count += neuronsPerLayer
        l = l.reshape(neuronsPerLayer)
        weights.append(l)

    #Output layer weights: neuronsPerLayer x outputNeruons matrix
    l = np.array(DNA[count:count + outputNeurons * neuronsPerLayer])
    l = l.reshape(neuronsPerLayer, outputNeurons)
    weights.append(l)
    count += neuronsPerLayer * outputNeurons
    #Hidden activation weights: neuronsPerLayer array
    l = np.array(DNA[count:count + neuronsPerLayer])
    count += neuronsPerLayer
    l = l.reshape(outputNeurons)
    weights.append(l)
    return weights, DNAbin
Esempio n. 4
0
def BinaryValue_to_scapy(binvalue):
    """ take binaryvalue return Ether scapy packet
    """
    k = BitArray()
    k.bin = binvalue.binstr
    return Ether(k.bytes)
Esempio n. 5
0
def bitstoint(bits):
    a = BitArray();
    a.bin = bits;
    return a.int;
Esempio n. 6
0
def bitstofloat(bits):
    a = BitArray();
    a.bin = bits;
    return a.float;