Commit 60b8c173 authored by Kelly Chang's avatar Kelly Chang
parents 18a92d19 ac651fcd
...@@ -932,7 +932,7 @@ ...@@ -932,7 +932,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.9.1" "version": "3.8.11"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 626, "execution_count": 33,
"id": "dbef8759", "id": "dbef8759",
"metadata": { "metadata": {
"id": "dbef8759" "id": "dbef8759"
...@@ -21,12 +21,13 @@ ...@@ -21,12 +21,13 @@
"from numpy import linalg as la\n", "from numpy import linalg as la\n",
"from scipy.stats import gaussian_kde\n", "from scipy.stats import gaussian_kde\n",
"import seaborn as sns\n", "import seaborn as sns\n",
"import pywt" "import pywt\n",
"from collections import Counter"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 654, "execution_count": 2,
"id": "9ed20f84", "id": "9ed20f84",
"metadata": { "metadata": {
"id": "9ed20f84" "id": "9ed20f84"
...@@ -99,7 +100,7 @@ ...@@ -99,7 +100,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 647, "execution_count": 3,
"id": "ba2881d9", "id": "ba2881d9",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -111,7 +112,7 @@ ...@@ -111,7 +112,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 655, "execution_count": 4,
"id": "11e95c34", "id": "11e95c34",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -121,7 +122,7 @@ ...@@ -121,7 +122,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 537, "execution_count": 5,
"id": "434e4d2f", "id": "434e4d2f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -159,30 +160,18 @@ ...@@ -159,30 +160,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 606, "execution_count": 6,
"id": "bf427edd", "id": "4f4a5a35",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"-19\n",
"[22286.]\n",
"22285.0\n",
"[22266.]\n",
"22266.999999999996\n"
]
}
],
"source": [ "source": [
"new_error = reconstruct(err, A)" "new_error = reconstruct(err, A)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 669, "execution_count": 7,
"id": "5edcf208", "id": "6d95ffce",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -205,8 +194,8 @@ ...@@ -205,8 +194,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 673, "execution_count": 8,
"id": "953375c5", "id": "1c848109",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -304,22 +293,172 @@ ...@@ -304,22 +293,172 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 679, "execution_count": 9,
"id": "90387cb9", "id": "139938e3",
"metadata": {},
"outputs": [],
"source": [
"class NodeTree(object):\n",
" def __init__(self, left=None, right=None):\n",
" self.left = left\n",
" self.right = right\n",
"\n",
" def children(self):\n",
" return self.left, self.right\n",
"\n",
" def __str__(self):\n",
" return self.left, self.right\n",
"\n",
"\n",
"def huffman_code_tree(node, binString=''):\n",
" '''\n",
" Function to find Huffman Code\n",
" '''\n",
" if type(node) is str:\n",
" return {node: binString}\n",
" (l, r) = node.children()\n",
" d = dict()\n",
" d.update(huffman_code_tree(l, binString + '0'))\n",
" d.update(huffman_code_tree(r, binString + '1'))\n",
" return d\n",
"\n",
"\n",
"def make_tree(nodes):\n",
" '''\n",
" Function to make tree\n",
" :param nodes: Nodes\n",
" :return: Root of the tree\n",
" '''\n",
" while len(nodes) > 1:\n",
" (key1, c1) = nodes[-1]\n",
" (key2, c2) = nodes[-2]\n",
" nodes = nodes[:-2]\n",
" node = NodeTree(key1, key2)\n",
" nodes.append((node, c1 + c2))\n",
" nodes = sorted(nodes, key=lambda x: x[1], reverse=True)\n",
" return nodes[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 263,
"id": "e477d0c8",
"metadata": {},
"outputs": [],
"source": [
"def enc_experiment(images, plot=True):\n",
" origin, predict, diff, error, A = plot_hist(images, 2)\n",
" image = Image.open(images[0]) #Open the image and read it as an Image object\n",
" image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
" image = image.astype(int)\n",
" new_error = np.copy(image)\n",
" #new_error[1:-1,1:-1] = np.reshape(error[1:-1,1:-1],(510, 638))\n",
" new_error[1:-1, 1:-1] = error[1:-1, 1:-1]\n",
" keep = new_error[0,0]\n",
" new_error[0,:] = new_error[0,:] - keep\n",
" new_error[-1,:] = new_error[-1,:] - keep\n",
" new_error[1:-1,0] = new_error[1:-1,0] - keep\n",
" new_error[1:-1,-1] = new_error[1:-1,-1] - keep\n",
" new_error[0,0] = keep\n",
" new_error = np.ravel(new_error)\n",
" if plot:\n",
" plt.hist(new_error[1:],bins=100)\n",
" plt.show()\n",
" \n",
" #ab_error = np.abs(new_error)\n",
" #string = [str(i) for i in ab_error]\n",
" string = [str(i) for i in new_error]\n",
" #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\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" \n",
" node = make_tree(freq)\n",
" encoding_dict = huffman_code_tree(node)\n",
" #encoded = [\"1\"+encoding[str(-i)] if i < 0 else \"0\"+encoding[str(i)] for i in error]\n",
" #print(time.time()-start)\n",
" encoded = new_error.reshape((512,640)).copy().astype(str)\n",
" \n",
" #encoded = np.zeros_like(new_error.reshape((512,640))).astype(str)\n",
" print(encoded)\n",
" for i in range(encoded.shape[0]):\n",
" for j in range(encoded.shape[1]):\n",
" if i == 0 and j == 0:\n",
" encoded[i][j] = encoded[i][j]\n",
" else:\n",
" #print(encoding_dict[encoded[i][j]])\n",
" encoded[i][j] = encoding_dict[encoded[i][j]]\n",
" #print(encoded[i][j])\n",
" \n",
" return encoding_dict, encoded, new_error.reshape((512,640))\n",
" #print(encoding)"
]
},
{
"cell_type": "code",
"execution_count": 264,
"id": "fd8e96a5",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "name": "stdout",
"text/plain": [ "output_type": "stream",
"15" "text": [
] "[['22541' '-10' '14' ... '32' '48' '33']\n",
}, " ['7' '67' '-21' ... '-1' '-1' '77']\n",
"execution_count": 679, " ['7' '-15' '-3' ... '10' '-45' '58']\n",
"metadata": {}, " ...\n",
"output_type": "execute_result" " ['49' '82' '-2' ... '-64' '5' '151']\n",
" ['27' '-33' '18' ... '47' '-16' '208']\n",
" ['17' '0' '-5' ... '138' '207' '226']]\n"
]
} }
], ],
"source": [] "source": [
"encode_dict, encoding, error = enc_experiment(images, plot=False)"
]
},
{
"cell_type": "code",
"execution_count": 265,
"id": "7ebd8dd8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11110111110\n",
"92\n",
"11110111110001\n"
]
}
],
"source": [
"print(encoding[0][100])\n",
"print(error[0][100])\n",
"print(encode_dict['92'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6a579b7",
"metadata": {},
"outputs": [],
"source": [
"def decoder(A, encoded_matrix, encoding_dict):\n",
" \"\"\"\n",
" Function that accecpts the prediction matrix A for the linear system,\n",
" the encoded matrix of error values, and the encoding dicitonary.\n",
" \"\"\"\n",
" error_matrix = encoded_matrix.copy()\n",
" for i in range(error_matrix.shape[0]):\n",
" for j in range(error_matrix.shape[1]):\n",
" if i == 0 and j == 0:\n",
" error_matrix[i][j] = encoded_matrix[i][j]\n",
" else:\n",
" error_matrix[i][j] = encoding_dict."
]
} }
], ],
"metadata": { "metadata": {
...@@ -343,7 +482,7 @@ ...@@ -343,7 +482,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.9.1" "version": "3.8.11"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment