RunningInParallel.py 2.12 KB
Newer Older
Bryce Hepner's avatar
Bryce Hepner committed
1 2 3 4 5
'''Note: This does not work.
   I'm not too sure what happened, but I ran small tests and they seemed to be fine,
   but on the big one in created an n-1 number of identical files plus an empty one.
   Not sure what happened there.'''

Bryce Hepner's avatar
Bryce Hepner committed
6 7 8 9 10 11 12 13 14 15 16
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[:]
17
    dview.execute("from WorkingPyDemo import *") #needed for others
Bryce Hepner's avatar
Bryce Hepner committed
18 19 20 21 22 23
    dview.block = True
    return dview
if __name__ == "__main__":
    scenes = file_extractor(folder_name)
    images = image_extractor(scenes)
    dview = initialize()
24
    list_dic = np.load("first_dict.npy", allow_pickle=True)
Bryce Hepner's avatar
Bryce Hepner committed
25 26 27 28 29
    bins = [21,32,48]
    starttime = time()
    
    def save_an_image(filename):
        """
Bryce Hepner's avatar
Bryce Hepner committed
30
        Takes in the filename and writes the compressed image
Bryce Hepner's avatar
Bryce Hepner committed
31
        """
32

33
        list_dic = np.load("first_dict.npy", allow_pickle=True)
Bryce Hepner's avatar
Bryce Hepner committed
34 35 36 37 38 39 40 41 42 43 44
        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):
45
        list_dic = np.load("first_dict.npy", allow_pickle=True)
Bryce Hepner's avatar
Bryce Hepner committed
46 47 48 49 50 51 52 53
        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)
54
    dview.map_sync(save_an_image, images[0])
Bryce Hepner's avatar
Bryce Hepner committed
55 56
    # dview.map_sync(decode_an_image, images[0:6])
    print(time() - starttime)