Commit 6f37e209 authored by Kelly Chang's avatar Kelly Chang
Browse files

kelly push

parent 332395dc
Loading
Loading
Loading
Loading
+813 −147

File changed.

Preview size limit exceeded, changes collapsed.

+0 −0
Original line number Diff line number Diff line
+145 −24
Original line number Diff line number Diff line
%% Cell type:code id:8868bc30 tags:

``` python
import numpy as np
from matplotlib import pyplot as plt
from itertools import product
import os
import sys
from PIL import Image
from scipy.optimize import minimize,linprog
import time
import seaborn as sns
from sklearn.neighbors import KernelDensity
import pandas as pd
from collections import Counter
import time
```

%% Cell type:code id:76317b02 tags:

``` python
def file_extractor(dirname="images"):
    files = os.listdir(dirname)
    scenes = []
    for file in files:
        scenes.append(os.path.join(dirname, file))
    return scenes

def image_extractor(scenes):
    image_folder = []
    for scene in scenes:
        files = os.listdir(scene)
        for file in files:
            image_folder.append(os.path.join(scene, file))
    images = []
    for folder in image_folder:
        ims = os.listdir(folder)
        for im in ims:
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
                continue
            else:
                images.append(os.path.join(folder, im))
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor

def im_distribution(images, num):
    """
    Function that extracts tiff files from specific cameras and returns a list of all
    the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise
    specified camera numbers.

    Parameters:
        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but
        of specific tiff files that can be opened right away. This is the list that we iterate through and
        divide.

        num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits
        of "_1" for single digits.

    Returns:
        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted
        from the 'images' list that correspond to the given num.
    """
    tiff = []
    for im in images:
        if im[-7:-5] == num:
            tiff.append(im)
    return tiff
```

%% Cell type:code id:be1ff8a1 tags:

``` python
def plot_hist(tiff_list):
    """
    This function is the leftovers from the first attempt to plot histograms.
    As it stands it needs some work in order to function again. We will
    fix this later. 1/25/22
    """

    image = tiff_list
    image = Image.open(image)    #Open the image and read it as an Image object
    image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
    image = image.astype(int)
    A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation
    z0 = image[0:-2,0:-2]   # get all the first pixel for the entire image
    z1 = image[0:-2,1:-1]   # get all the second pixel for the entire image
    z2 = image[0:-2,2::]    # get all the third pixel for the entire image
    z3 = image[1:-1,0:-2]   # get all the forth pixel for the entire image
    # calculate the out put of the system of equation
    y0 = np.ravel(-z0+z2-z3)
    y1 = np.ravel(z0+z1+z2)
    y2 = np.ravel(-z0-z1-z2-z3)
    y = np.vstack((y0,y1,y2))
    # use numpy solver to solve the system of equations all at once
    predict = np.floor(np.linalg.solve(A,y)[-1])
    # flatten the neighbor pixlels and stack them together
    z0 = np.ravel(z0)
    z1 = np.ravel(z1)
    z2 = np.ravel(z2)
    z3 = np.ravel(z3)
    neighbor = np.vstack((z0,z1,z2,z3)).T
    # calculate the difference
    diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)

    # flatten the image to a vector
    image = np.ravel(image[1:-1,1:-1])
    error = image-predict

    return image, predict, diff, error, A
```

%% Cell type:code id:8483903e tags:

``` python
class NodeTree(object):
    def __init__(self, left=None, right=None):
        self.left = left
        self.right = right

    def children(self):
        return self.left, self.right

    def __str__(self):
        return self.left, self.right


def huffman_code_tree(node, binString=''):
    '''
    Function to find Huffman Code
    '''
    if type(node) is str:
        return {node: binString}
    (l, r) = node.children()
    d = dict()
    d.update(huffman_code_tree(l, binString + '0'))
    d.update(huffman_code_tree(r, binString + '1'))
    return d


def make_tree(nodes):
    '''
    Function to make tree
    :param nodes: Nodes
    :return: Root of the tree
    '''
    while len(nodes) > 1:
        (key1, c1) = nodes[-1]
        (key2, c2) = nodes[-2]
        nodes = nodes[:-2]
        node = NodeTree(key1, key2)
        nodes.append((node, c1 + c2))
        nodes = sorted(nodes, key=lambda x: x[1], reverse=True)
    return nodes[0][0]
```

%% Cell type:markdown id:c7104fbf tags:

### Huffman without dividing into bins

%% Cell type:code id:a43f3f1c tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
def huffman_nb(image):
    origin, predict, diff, error, A = plot_hist(image)
    image = Image.open(image)
    image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
    image = image.astype(int)

    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error = np.ravel(new_error)



    #ab_error = np.abs(new_error)
    #string = [str(i) for i in ab_error]
    string = [str(i) for i in new_error.astype(int)]
    freq = dict(Counter(string))

    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encoding = huffman_code_tree(node)
    #encoded = ["1"+encoding[str(-i)] if i < 0 else "0"+encoding[str(i)] for i in error]

    # return the huffman dictionary
    return encoding, new_error, image.reshape(-1)


def compress_rate_nb(image, error, encoding):
    #original = original.reshape(-1)
    #error = error.reshape(-1)
    o_len = 0
    c_len = 0
    for i in range(0, len(original)):
        o_len += len(bin(original[i])[2:])
        c_len += len(encoding[str(int(error[i]))])


    return c_len/o_len


encoding, error, image = huffman_nb(images[0])
print(compress_rate_nb(image, error, encoding))
```

%% Output

    0.444949904726781
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    /var/folders/z2/plvrsqjs023g1cmx7k19mhzr0000gn/T/ipykernel_15308/409627503.py in <module>
         47
         48 encoding, error, image = huffman_nb(images[0])
    ---> 49 print(compress_rate_nb(image, error, encoding))

    /var/folders/z2/plvrsqjs023g1cmx7k19mhzr0000gn/T/ipykernel_15308/409627503.py in compress_rate_nb(image, error, encoding)
         38     o_len = 0
         39     c_len = 0
    ---> 40     for i in range(0, len(original)):
         41         o_len += len(bin(original[i])[2:])
         42         c_len += len(encoding[str(int(error[i]))])
    NameError: name 'original' is not defined

%% Cell type:markdown id:eac2f456 tags:

### Huffman with dividing into non-uniform bins

%% Cell type:code id:207b0bd2 tags:

``` python
def huffman(image):
    origin, predict, diff, error, A = plot_hist(image)

    image = Image.open(image)
    image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
    image = image.astype(int)


    boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))
    boundary = boundary - image[0,0]
    boundary[0] = image[0,0]

    string = [str(i) for i in boundary]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode1 = huffman_code_tree(node)


    mask = diff <= 25
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode2 = huffman_code_tree(node)


    mask = diff > 25
    new_error = error[mask]
    mask2 = diff[mask] <= 40
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode3 = huffman_code_tree(node)


    mask = diff > 40
    new_error = error[mask]
    mask2 = diff[mask] <= 70
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode4 = huffman_code_tree(node)


    mask = diff > 70
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode5 = huffman_code_tree(node)




    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error = np.ravel(new_error)


    #new_error = np.ravel(new_error)

    bins = [25,40,70]

    # return the huffman dictionary
    return encode1, encode2, encode3, encode4, encode5, np.ravel(image), error, diff, boundary
    return encode1, encode2, encode3, encode4, encode5, np.ravel(image), error, diff, boundary, bins

def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5):
    #original = original.reshape(-1)
    #error = error.reshape(-1)

    o_len = 0
    c_len = 0
    im = np.reshape(image,(512, 640))
    real_b = np.hstack((im[0,:],im[-1,:],im[1:-1,0],im[1:-1,-1]))
    original = im[1:-1,1:-1].reshape(-1)

    for i in range(0,len(bound)):
        o_len += len(bin(real_b[i])[2:])
        c_len += len(encode1[str(bound[i])])

    for i in range(0, len(original)):
        o_len += len(bin(original[i])[2:])
        if diff[i] <= 25:
            c_len += len(encode2[str(int(error[i]))])

        if diff[i] <= 40 and diff[i] > 25:
            c_len += len(encode3[str(int(error[i]))])

        if diff[i] <= 70 and diff[i] > 40:
            c_len += len(encode4[str(int(error[i]))])

        if diff[i] > 70:
            c_len += len(encode5[str(int(error[i]))])

    return c_len/o_len
scenes = file_extractor()
images = image_extractor(scenes)
encode1, encode2, encode3, encode4, encode5, image, error, diff, boundary = huffman(images[0])
encode1, encode2, encode3, encode4, encode5, image, error, diff, boundary, bins = huffman(images[0])
compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5)
```

%% Output

    325380

    0.44205322265625

%% Cell type:markdown id:3a3f06a5 tags:

### Huffman with dividing into uniform bins

%% Cell type:code id:14075c94 tags:

``` python
def huffman_u(image):
    origin, predict, diff, error, A = plot_hist(image)
    image = Image.open(image)
    image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
    image = image.astype(int)

    print(len(diff))

    boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))
    boundary = boundary - image[0,0]
    boundary[0] = image[0,0]

    string = [str(i) for i in boundary]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode1 = huffman_code_tree(node)


    mask = diff <= 100
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode2 = huffman_code_tree(node)


    mask = diff > 100
    #new_error = error[mask]
    #mask2 = diff[mask] <= 200
    #string = [str(i) for i in new_error[mask2].astype(int)]
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode3 = huffman_code_tree(node)


    '''mask = diff > 200
    new_error = error[mask]
    mask2 = diff[mask] <= 300
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode4 = huffman_code_tree(node)


    mask = diff > 300
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode5 = huffman_code_tree(node)'''




    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error = np.ravel(new_error)

    # return the huffman dictionary
    #return encode1, encode2, encode3, encode4, encode5, np.ravel(image), error, diff, boundary
    print(len(diff))
    return encode1, encode2, encode3, np.ravel(image), error, diff, boundary

#def compress_rate_u(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5):
def compress_rate_u(image, error, diff, bound, encode1, encode2, encode3):
    #original = original.reshape(-1)
    #error = error.reshape(-1)
    o_len = 0
    c_len = 0
    im = np.reshape(image,(512, 640))
    real_b = np.hstack((im[0,:],im[-1,:],im[1:-1,0],im[1:-1,-1]))
    original = im[1:-1,1:-1].reshape(-1)

    for i in range(0,len(bound)):
        o_len += len(bin(real_b[i])[2:])
        c_len += len(encode1[str(bound[i])])

    for i in range(0, len(original)):
        o_len += len(bin(original[i])[2:])
        if diff[i] <= 100:
            c_len += len(encode2[str(int(error[i]))])

        if diff[i] > 100:
            c_len += len(encode3[str(int(error[i]))])

        '''if diff[i] <= 200 and diff[i] > 100:
            c_len += len(encode3[str(int(error[i]))])'''

        '''if diff[i] <= 300 and diff[i] > 200:
            c_len += len(encode4[str(int(error[i]))])

        if diff[i] > 300:
            c_len += len(encode5[str(int(error[i]))])'''

    return c_len/o_len
scenes = file_extractor()
images = image_extractor(scenes)
encode1, encode2, encode3, image, error, diff, boundary = huffman_u(images[0])
compress_rate_u(image, error, diff, boundary, encode1, encode2, encode3)
```

%% Output

    325380
    325380

    0.4432273356119792

%% Cell type:code id:f8b93cc5 tags:

``` python
```

%% Cell type:code id:6abed5da tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
num_images = im_distribution(images, "_9")
rate = []
rate_nb = []
rate_u = []
for i in range(len(num_images)):
    encode1, encode2, encode3, encode4, encode5, image, error, diff, bound = huffman(num_images[i])
    r = compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5)
    rate.append(r)
    encoding, error, image = huffman_nb(num_images[i])
    r = compress_rate_nb(image, error, encoding)
    rate_nb.append(r)
    encode1, encode2, encode3, image, error, diff, bound = huffman_u(num_images[i])
    r = compress_rate_u(image, error, diff, bound, encode1, encode2, encode3)
    rate_u.append(r)

print(f"Compression rate of huffman with different bins: {np.mean(rate)}")
print(f"Compression rate of huffman without bins: {np.mean(rate_nb)}")
print(f"Compression rate of huffman with uniform bins: {np.mean(rate_u)}")
```

%% Output

    Compression rate of huffman with different bins: 0.44946919759114584
    Compression rate of huffman without bins: 0.4513634314749933
    Compression rate of huffman with uniform bins: 0.44956921895345053

%% Cell type:code id:15eecad3 tags:

``` python
def huffman6(image):
    origin, predict, diff, error, A = plot_hist(image)
    image = Image.open(image)
    image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
    image = image.astype(int)


    boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))
    boundary = boundary - image[0,0]
    boundary[0] = image[0,0]

    string = [str(i) for i in boundary]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode1 = huffman_code_tree(node)


    mask = diff <= 5
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode2 = huffman_code_tree(node)


    mask = diff > 5
    new_error = error[mask]
    mask2 = diff[mask] <= 15
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode3 = huffman_code_tree(node)


    mask = diff > 15
    new_error = error[mask]
    mask2 = diff[mask] <= 30
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode4 = huffman_code_tree(node)


    mask = diff > 30
    new_error = error[mask]
    mask2 = diff[mask] <= 50
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode5 = huffman_code_tree(node)


    mask = diff > 50
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode6 = huffman_code_tree(node)




    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error = np.ravel(new_error)

    # return the huffman dictionary
    return encode1, encode2, encode3, encode4, encode5, encode6, np.ravel(image), error, diff, boundary

def compress_rate6(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5, encode6):
    #original = original.reshape(-1)
    #error = error.reshape(-1)
    o_len = 0
    c_len = 0
    im = np.reshape(image,(512, 640))
    real_b = np.hstack((im[0,:],im[-1,:],im[1:-1,0],im[1:-1,-1]))
    original = im[1:-1,1:-1].reshape(-1)

    for i in range(0,len(bound)):
        o_len += len(bin(real_b[i])[2:])
        c_len += len(encode1[str(bound[i])])

    for i in range(0, len(original)):
        o_len += len(bin(original[i])[2:])
        if diff[i] <= 5:
            c_len += len(encode2[str(int(error[i]))])

        if diff[i] <= 15 and diff[i] > 5:
            c_len += len(encode3[str(int(error[i]))])

        if diff[i] <= 30 and diff[i] > 15:
            c_len += len(encode4[str(int(error[i]))])

        if diff[i] <= 50 and diff[i] > 30:
            c_len += len(encode5[str(int(error[i]))])

        if diff[i] > 50:
            c_len += len(encode6[str(int(error[i]))])

    return c_len/o_len
scenes = file_extractor()
images = image_extractor(scenes)
encode1, encode2, encode3, encode4, encode5, encode6, image, error, diff, boundary = huffman(images[0])
compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5, encode6)
```

%% Output

    0.4421759033203125

%% Cell type:code id:f8a8c717 tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
num_images = im_distribution(images, "_9")
rate = []

for i in range(len(num_images)):
    encode1, encode2, encode3, encode4, encode5, encode6, image, error, diff, bound = huffman6(num_images[i])
    r = compress_rate6(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5, encode6)
    rate.append(r)


print(f"Compression rate of huffman with different bins: {np.mean(rate)}")
```

%% Output

    Compression rate of huffman with different bins: 0.448723882039388

%% Cell type:code id:992dd8bb tags:

``` python
origin, predict, diff, error, A = plot_hist(images[0])
```

%% Cell type:code id:d3b29278 tags:

``` python
def enc_experiment(image, plot=True):

    origin, predict, diff, error, A = plot_hist(image)
    image = Image.open(image)
    image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
    image = image.astype(int)

    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error = np.ravel(new_error)


    '''origin, predict, diff, error, A = plot_hist(images)
    image = Image.open(images)    #Open the image and read it as an Image object
    image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
    image = image.astype(int)
    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error[1:-1,1:-1],(510, 638))
    #new_error[1:-1, 1:-1] = error[1:-1, 1:-1]
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error = np.ravel(new_error)
    if plot:
        plt.hist(new_error[1:],bins=100)
        plt.show()'''

    #ab_error = np.abs(new_error)
    #string = [str(i) for i in ab_error]
    string = [str(i) for i in new_error]
    #string = [str(i) for i in np.arange(0,5)] + [str(i) for i in np.arange(0,5)] + [str(i) for i in np.arange(0,2)]*2
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)

    node = make_tree(freq)
    encoding_dict = huffman_code_tree(node)
    #encoded = ["1"+encoding[str(-i)] if i < 0 else "0"+encoding[str(i)] for i in error]
    #print(time.time()-start)
    encoded = new_error.reshape((512,640)).copy().astype(str).astype(object)

    for i in range(encoded.shape[0]):
        for j in range(encoded.shape[1]):
            if i == 0 and j == 0:
                encoded[i][j] = encoding_dict[encoded[i][j]]
            else:
                #print(encoding_dict[encoded[i][j]])
                encoded[i][j] = encoding_dict[encoded[i][j]]
                #print(encoded[i][j])

    return encoding_dict, encoded, new_error.reshape((512,640)), image
    #print(encoding)
```

%% Cell type:code id:f4665493 tags:

``` python
def encoder(list_dic,diff):
    encoded = new_error.reshape((512,640)).copy().astype(str).astype(object)
def encoder(error, list_dic, diff, bound, bins):
    encoded = np.copy(error).astype(int).astype(str).astype(object)
    diff = np.reshape(diff,(510,638))

    for i in range(encoded.shape[0]):
        for j in range(encoded.shape[1]):
            if i == 0 and j == 0:
                encoded[i][j] = encoding_dict[encoded[i][j]]
            '''if i == 0 and j == 0:
                encoded[i][j] = encoding_dict[encoded[i][j]]'''
            if i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:
                print(i,j)
                encoded[i][j] = list_dic[0][encoded[i][j]]
            elif diff[i+1][j+1] <= bins[0]:
                encoded[i][j] = list_dic[1][encoded[i][j]]
            elif diff[i+1][j+1] <= bins[1] and diff[i+1][j+1] > bins[0]:
                encoded[i][j] = list_dic[2][encoded[i][j]]
            elif diff[i+1][j+1] <= bins[2] and diff[i+1][j+1] > bins[1]:
                encoded[i][j] = list_dic[3][encoded[i][j]]
            else:
                #print(encoding_dict[encoded[i][j]])
                encoded[i][j] = encoding_dict[encoded[i][j]]
                #print(encoded[i][j])

    return encoding_dict, encoded, new_error.reshape((512,640)), image
```
                encoded[i][j] = list_dic[4][encoded[i][j]]

%% Output

      File "/var/folders/z2/plvrsqjs023g1cmx7k19mhzr0000gn/T/ipykernel_20810/591304675.py", line 1
        def encoder():
                      ^
    IndentationError: expected an indented block
    return encoded
```

%% Cell type:code id:1ec5c5e3 tags:

``` python
encoding_dict, encoded_matrix, error, orig_image = enc_experiment(images[0], plot=False)
encode1, encode2, encode3, encode4, encode5, image, error, diff, bound, bins = huffman(images[0])
encoded_matrix = encoder(np.reshape(error,(510,638)), [encode1, encode2, encode3, encode4, encode5], diff, bound, bins)

```

%% Output

    510 702
    0 0
    0 1
    0 2
    0 3
    0 4
    0 5
    0 6
    0 7
    0 8
    0 9
    0 10
    0 11
    0 12
    0 13
    0 14
    0 15
    0 16
    0 17
    0 18
    0 19
    0 20
    0 21
    0 22
    0 23
    0 24
    0 25
    0 26
    0 27
    0 28
    0 29
    0 30
    0 31
    0 32
    0 33
    0 34
    0 35
    0 36
    0 37
    0 38
    0 39
    0 40
    0 41
    0 42
    0 43
    0 44
    0 45
    0 46
    0 47
    0 48
    0 49
    0 50
    0 51
    0 52
    0 53
    0 54
    0 55
    0 56
    0 57
    0 58
    0 59
    0 60
    0 61
    0 62
    0 63
    0 64
    0 65
    0 66
    0 67
    0 68
    0 69
    0 70
    0 71
    0 72
    0 73
    0 74
    0 75

    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    /var/folders/z2/plvrsqjs023g1cmx7k19mhzr0000gn/T/ipykernel_29322/362411884.py in <module>
          1 encode1, encode2, encode3, encode4, encode5, image, error, diff, bound, bins = huffman(images[0])
    ----> 2 encoded_matrix = encoder(np.reshape(error,(510,638)), [encode1, encode2, encode3, encode4, encode5], diff, bound, bins)
          3
    /var/folders/z2/plvrsqjs023g1cmx7k19mhzr0000gn/T/ipykernel_29322/153815811.py in encoder(error, list_dic, diff, bound, bins)
          9             if i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:
         10                 print(i,j)
    ---> 11                 encoded[i][j] = list_dic[0][encoded[i][j]]
         12             elif diff[i+1][j+1] <= bins[0]:
         13                 encoded[i][j] = list_dic[1][encoded[i][j]]
    KeyError: '-36'

%% Cell type:code id:4927a12f tags:

``` python
print(encoding)
A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]])
```

%% Output

    [['111001110100111000' '110001' '11111000' ... '001010' '1110000'
      '0010010']
     ['101000' '100101' '110001' ... '0010111' '110111' '00111111']
     ['110011' '010011' '010000' ... '110110' '110001' '1110000']
     ...
     ['1111111101011101' '010010' '101110' ... '0010111' '110111' '01111110']
     ['1111111101011110' '100110' '110110' ... '11111000' '1110111' '000101']
     ['011111110000000' '111111110110010' '01011010111001' ...
      '01011010001001' '11111001001' '0110100110']]

%% Cell type:code id:f145c221 tags:

``` python
def decoder(A, encoded_matrix, encoding_dict):
    """
    Function that accecpts the prediction matrix A for the linear system,
    the encoded matrix of error values, and the encoding dicitonary.
    """
    the_keys = list(encode_dict.keys())
    the_values = list(encode_dict.values())
    error_matrix = encoded_matrix.copy()

    for i in range(error_matrix.shape[0]):
        for j in range(error_matrix.shape[1]):
            if i == 0 and j == 0:
                error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])])
            elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:
                error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) + error_matrix[0][0]
            else:
                if j == 1 and i == 1:
                    z0, z1, z2, z3 = error_matrix[i-1][j-1], error_matrix[i-1][j], \
                    error_matrix[i-1][j+1], error_matrix[i][j-1]

                    y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))
                    #Real solution that works, DO NOT DELETE
                    #new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1]))

                    print(int(the_keys[the_values.index(error_matrix[i,j])]))
                    print(np.linalg.solve(A,y)[-1])
                    error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) + \
                                                np.floor(np.linalg.solve(A,y)[-1][0])
                    #error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])])
                break

    return error_matrix
```

%% Cell type:code id:23b4c68b tags:

``` python
def decoder(A, encoded_matrix, encoding_dict):
    """
    Function that accecpts the prediction matrix A for the linear system,
    the encoded matrix of error values, and the encoding dicitonary.
    """
    the_keys = list(encode_dict.keys())
    the_values = list(encode_dict.values())
    error_matrix = encoded_matrix.copy()

    for i in range(error_matrix.shape[0]):
        for j in range(error_matrix.shape[1]):
            if i == 0 and j == 0:
                error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])])

            elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:
                error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) + error_matrix[0][0]
            else:
                """z0, z1, z2, z3 = error_matrix[i-1][j-1], error_matrix[i-1][j], \
                error_matrix[i-1][j+1], error_matrix[i][j-1]
                y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))"""

                error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])])

    return error_matrix.astype(int)
```

%% Cell type:code id:f33ed5ae tags:

``` python
image_error = decoder(A, encoded_matrix, encoding_dict)
```

%% Cell type:code id:74eafda3 tags:

``` python
def reconstruct(error, A):
    """
    Function that reconstructs the original image
    from the error matrix and using the predictive
    algorithm developed in the encoding.

    Parameters:
        error (array): matrix of errors computed in encoding. Same
                       shape as the original image (512, 640) in this case
        A (array): Matrix used for the system of equations to create predictions
    Returns: cd cdcd
        image (array): The reconstructed image
    """
    new_e = error.copy()
    rows, columns = new_e.shape

    for r in range(1, rows-1):
        for c in range(1, columns-1):
            z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1]
            y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))

            if r == 1 and c == 1:
                print(np.linalg.solve(A,y)[-1])

            #Real solution that works, DO NOT DELETE
            #new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1]))

            new_e[r][c] = np.round(new_e[r][c] + np.linalg.solve(A,y)[-1], 1)

    return new_e.astype(int)
```

%% Cell type:code id:0c40bfe1 tags:

``` python
new_image = reconstruct(image_error, A)
```

%% Output

    [22543.5]

%% Cell type:code id:5495cb59 tags:

``` python
print(new_image)
print(orig_image)
```

%% Output

    [[22554 22552 22519 ... 22537 22529 22523]
     [22561 22552 22543 ... 22544 22533 22513]
     [22559 22565 22548 ... 22526 22508 22529]
     ...
     [22674 22661 22654 ... 22670 22617 22594]
     [22656 22652 22644 ... 22640 22625 22573]
     [22659 22653 22642 ... 22649 22615 22613]]
    [[22554 22552 22519 ... 22537 22529 22523]
     [22561 22552 22543 ... 22544 22533 22513]
     [22559 22565 22548 ... 22526 22508 22529]
     ...
     [22674 22661 22654 ... 22670 22617 22594]
     [22656 22652 22644 ... 22640 22625 22573]
     [22659 22653 22642 ... 22649 22615 22613]]

%% Cell type:code id:c1f26059 tags:

``` python
np.all(new_image - orig_image == 0)
```

%% Output

    True

%% Cell type:code id:9200fa53 tags:

``` python
```
+387 −0

File added.

Preview size limit exceeded, changes collapsed.