RunningInParallel.py 1.89 KB
Newer Older
Bryce Hepner's avatar
Bryce Hepner committed
1 2 3 4 5 6 7 8 9 10 11
from ipyparallel import Client
from WorkingPyDemo import *
from time import time
def initialize():
    """
    Write a function that initializes a Client object, creates a Direct
    View with all available engines, and imports scipy.sparse as spar on
    all engines. Return the DirectView.
    """
    client = Client() # Only works if a cluster is running.
    dview = client[:]
12
    dview.execute("from WorkingPyDemo import *") #needed for others
Bryce Hepner's avatar
Bryce Hepner committed
13 14 15 16 17 18 19 20 21 22 23 24
    dview.block = True
    return dview
if __name__ == "__main__":
    scenes = file_extractor(folder_name)
    images = image_extractor(scenes)
    dview = initialize()
    list_dic = np.load("first_dic.npy", allow_pickle=True)
    bins = [21,32,48]
    starttime = time()
    
    def save_an_image(filename):
        """
Bryce Hepner's avatar
Bryce Hepner committed
25
        Takes in the filename and writes the compressed image
Bryce Hepner's avatar
Bryce Hepner committed
26
        """
27

Bryce Hepner's avatar
Bryce Hepner committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        list_dic = np.load("first_dic.npy", allow_pickle=True)
        bins = [21,32,48]
        image, new_error, diff = huffman(filename, 4, False)
        encoded_string = encoder(new_error, list_dic, diff, bins)
        inletters = bitstring_to_bytes(encoded_string)
        if filename[-5:] == ".tiff":
            newname = filename[:-5]
        else:
            newname = filename[:-4]
        with open(newname + "_Compressed.txt", 'wb') as f:
            f.write(inletters)
    def decode_an_image(filename):
        list_dic = np.load("first_dic.npy", allow_pickle=True)
        bins = [21,32,48]
        image, new_error, diff = huffman(filename, 4, False)
        if filename[-5:] == ".tiff":
            newname = filename[:-5]
        else:
            newname = filename[:-4]
        encoded_string2 = bytes_to_bitstring(read_from_file(newname + "_Compressed.txt"))
        reconstruct_image = decoder(encoded_string2, list_dic, bins, False)
49
    dview.map_sync(save_an_image, images[0])
Bryce Hepner's avatar
Bryce Hepner committed
50 51
    # dview.map_sync(decode_an_image, images[0:6])
    print(time() - starttime)