Commit 53e2bb11 authored by Nathaniel Callens's avatar Nathaniel Callens

changes

parent b88a3252
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 33, "execution_count": 2,
"id": "dbef8759", "id": "dbef8759",
"metadata": { "metadata": {
"id": "dbef8759" "id": "dbef8759"
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 3,
"id": "9ed20f84", "id": "9ed20f84",
"metadata": { "metadata": {
"id": "9ed20f84" "id": "9ed20f84"
...@@ -100,7 +100,7 @@ ...@@ -100,7 +100,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"id": "ba2881d9", "id": "ba2881d9",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -112,17 +112,17 @@ ...@@ -112,17 +112,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 144,
"id": "11e95c34", "id": "11e95c34",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"predict, diff, im, err, A = plot_hist(num_images, 0)" "predict, diff, im, err, A = plot_hist(images, 2)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 139,
"id": "434e4d2f", "id": "434e4d2f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -147,7 +147,9 @@ ...@@ -147,7 +147,9 @@
" for c in range(1, columns-1):\n", " for c in range(1, columns-1):\n",
" 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]\n", " 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]\n",
" y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))\n", " y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))\n",
"\n", " \n",
" if r == 1 and c == 1:\n",
" print(np.linalg.solve(A,y)[-1])\n",
" \n", " \n",
" #Real solution that works, DO NOT DELETE\n", " #Real solution that works, DO NOT DELETE\n",
" #new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1]))\n", " #new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1]))\n",
...@@ -160,17 +162,52 @@ ...@@ -160,17 +162,52 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 140,
"id": "3cc609dc", "id": "3cc609dc",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[22471.5]\n"
]
}
],
"source": [ "source": [
"new_error = reconstruct(err, A)" "new_error = reconstruct(err, A)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 136,
"id": "5d290a0c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[22546, 22514, 22513, ..., 22581, 22576, 22587],\n",
" [22488, 67, -21, ..., -1, -1, 22576],\n",
" [22514, -15, -3, ..., 10, -45, 22575],\n",
" ...,\n",
" [22317, 82, -2, ..., -64, 5, 22937],\n",
" [22335, -33, 18, ..., 47, -16, 22932],\n",
" [22333, 22339, 22362, ..., 22952, 22947, 22961]])"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"err"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "706f2816", "id": "706f2816",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -194,7 +231,7 @@ ...@@ -194,7 +231,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 9,
"id": "530d2cab", "id": "530d2cab",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -293,7 +330,7 @@ ...@@ -293,7 +330,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 10,
"id": "bb11dcd0", "id": "bb11dcd0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -341,7 +378,7 @@ ...@@ -341,7 +378,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 263, "execution_count": 108,
"id": "c01fda28", "id": "c01fda28",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -376,10 +413,8 @@ ...@@ -376,10 +413,8 @@
" encoding_dict = huffman_code_tree(node)\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", " #encoded = [\"1\"+encoding[str(-i)] if i < 0 else \"0\"+encoding[str(i)] for i in error]\n",
" #print(time.time()-start)\n", " #print(time.time()-start)\n",
" encoded = new_error.reshape((512,640)).copy().astype(str)\n", " encoded = new_error.reshape((512,640)).copy().astype(str).astype(object)\n",
" \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 i in range(encoded.shape[0]):\n",
" for j in range(encoded.shape[1]):\n", " for j in range(encoded.shape[1]):\n",
" if i == 0 and j == 0:\n", " if i == 0 and j == 0:\n",
...@@ -388,60 +423,48 @@ ...@@ -388,60 +423,48 @@
" #print(encoding_dict[encoded[i][j]])\n", " #print(encoding_dict[encoded[i][j]])\n",
" encoded[i][j] = encoding_dict[encoded[i][j]]\n", " encoded[i][j] = encoding_dict[encoded[i][j]]\n",
" #print(encoded[i][j])\n", " #print(encoded[i][j])\n",
" \n", " \n",
" return encoding_dict, encoded, new_error.reshape((512,640))\n", " return encoding_dict, encoded, new_error.reshape((512,640)), image\n",
" #print(encoding)" " #print(encoding)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 264, "execution_count": 109,
"id": "ffa858e8", "id": "ffa858e8",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['22541' '-10' '14' ... '32' '48' '33']\n",
" ['7' '67' '-21' ... '-1' '-1' '77']\n",
" ['7' '-15' '-3' ... '10' '-45' '58']\n",
" ...\n",
" ['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)" "encode_dict, encoding, error, orig_image = enc_experiment(images, plot=False)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 265, "execution_count": 110,
"id": "85815094", "id": "8dfdedc6",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"11110111110\n", "[[22541 22531 22555 ... 22573 22589 22574]\n",
"92\n", " [22548 22544 22530 ... 22607 22612 22618]\n",
"11110111110001\n" " [22548 22544 22560 ... 22603 22605 22599]\n",
" ...\n",
" [22590 22593 22596 ... 22586 22627 22692]\n",
" [22568 22575 22555 ... 22625 22702 22749]\n",
" [22558 22541 22536 ... 22679 22748 22767]]\n"
] ]
} }
], ],
"source": [ "source": [
"print(encoding[0][100])\n", "print(orig_image)"
"print(error[0][100])\n",
"print(encode_dict['92'])"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 148,
"id": "825cc48c", "id": "825cc48c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -451,14 +474,61 @@ ...@@ -451,14 +474,61 @@
" Function that accecpts the prediction matrix A for the linear system,\n", " Function that accecpts the prediction matrix A for the linear system,\n",
" the encoded matrix of error values, and the encoding dicitonary.\n", " the encoded matrix of error values, and the encoding dicitonary.\n",
" \"\"\"\n", " \"\"\"\n",
" the_keys = list(encode_dict.keys())\n",
" the_values = list(encode_dict.values())\n",
" error_matrix = encoded_matrix.copy()\n", " error_matrix = encoded_matrix.copy()\n",
" \n",
" for i in range(error_matrix.shape[0]):\n", " for i in range(error_matrix.shape[0]):\n",
" for j in range(error_matrix.shape[1]):\n", " for j in range(error_matrix.shape[1]):\n",
" if i == 0 and j == 0:\n", " if i == 0 and j == 0:\n",
" error_matrix[i][j] = encoded_matrix[i][j]\n", " error_matrix[i][j] = int(encoded_matrix[i][j])\n",
" elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:\n",
" error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) + error_matrix[0][0]\n",
" else:\n", " else:\n",
" error_matrix[i][j] = encoding_dict." " if j == 1 and i == 1:\n",
" z0, z1, z2, z3 = error_matrix[i-1][j-1], error_matrix[i-1][j], \\\n",
" error_matrix[i-1][j+1], error_matrix[i][j-1]\n",
"\n",
" y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))\n",
" #Real solution that works, DO NOT DELETE\n",
" #new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1]))\n",
"\n",
" print(int(the_keys[the_values.index(error_matrix[i,j])]))\n",
" print(np.linalg.solve(A,y)[-1])\n",
" error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) + \\\n",
" np.linalg.solve(A,y)[-1][0]\n",
" #error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])])\n",
" break\n",
" \n",
" return error_matrix"
] ]
},
{
"cell_type": "code",
"execution_count": 149,
"id": "ba1d2c2c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"67\n",
"[22555.]\n"
]
}
],
"source": [
"em = decoder(A, encoding, encode_dict)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2cdce6d",
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 33, "execution_count": 2,
"id": "dbef8759", "id": "dbef8759",
"metadata": { "metadata": {
"id": "dbef8759" "id": "dbef8759"
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 3,
"id": "9ed20f84", "id": "9ed20f84",
"metadata": { "metadata": {
"id": "9ed20f84" "id": "9ed20f84"
...@@ -100,7 +100,7 @@ ...@@ -100,7 +100,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"id": "ba2881d9", "id": "ba2881d9",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -112,17 +112,17 @@ ...@@ -112,17 +112,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 144,
"id": "11e95c34", "id": "11e95c34",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"predict, diff, im, err, A = plot_hist(num_images, 0)" "predict, diff, im, err, A = plot_hist(images, 2)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 139,
"id": "434e4d2f", "id": "434e4d2f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -147,7 +147,9 @@ ...@@ -147,7 +147,9 @@
" for c in range(1, columns-1):\n", " for c in range(1, columns-1):\n",
" 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]\n", " 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]\n",
" y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))\n", " y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))\n",
"\n", " \n",
" if r == 1 and c == 1:\n",
" print(np.linalg.solve(A,y)[-1])\n",
" \n", " \n",
" #Real solution that works, DO NOT DELETE\n", " #Real solution that works, DO NOT DELETE\n",
" #new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1]))\n", " #new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1]))\n",
...@@ -160,17 +162,52 @@ ...@@ -160,17 +162,52 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 140,
"id": "3cc609dc", "id": "3cc609dc",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[22471.5]\n"
]
}
],
"source": [ "source": [
"new_error = reconstruct(err, A)" "new_error = reconstruct(err, A)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 136,
"id": "5d290a0c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[22546, 22514, 22513, ..., 22581, 22576, 22587],\n",
" [22488, 67, -21, ..., -1, -1, 22576],\n",
" [22514, -15, -3, ..., 10, -45, 22575],\n",
" ...,\n",
" [22317, 82, -2, ..., -64, 5, 22937],\n",
" [22335, -33, 18, ..., 47, -16, 22932],\n",
" [22333, 22339, 22362, ..., 22952, 22947, 22961]])"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"err"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "706f2816", "id": "706f2816",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -194,7 +231,7 @@ ...@@ -194,7 +231,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 9,
"id": "530d2cab", "id": "530d2cab",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -293,7 +330,7 @@ ...@@ -293,7 +330,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 10,
"id": "bb11dcd0", "id": "bb11dcd0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -341,7 +378,7 @@ ...@@ -341,7 +378,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 263, "execution_count": 108,
"id": "c01fda28", "id": "c01fda28",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -376,10 +413,8 @@ ...@@ -376,10 +413,8 @@
" encoding_dict = huffman_code_tree(node)\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", " #encoded = [\"1\"+encoding[str(-i)] if i < 0 else \"0\"+encoding[str(i)] for i in error]\n",
" #print(time.time()-start)\n", " #print(time.time()-start)\n",
" encoded = new_error.reshape((512,640)).copy().astype(str)\n", " encoded = new_error.reshape((512,640)).copy().astype(str).astype(object)\n",
" \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 i in range(encoded.shape[0]):\n",
" for j in range(encoded.shape[1]):\n", " for j in range(encoded.shape[1]):\n",
" if i == 0 and j == 0:\n", " if i == 0 and j == 0:\n",
...@@ -388,60 +423,48 @@ ...@@ -388,60 +423,48 @@
" #print(encoding_dict[encoded[i][j]])\n", " #print(encoding_dict[encoded[i][j]])\n",
" encoded[i][j] = encoding_dict[encoded[i][j]]\n", " encoded[i][j] = encoding_dict[encoded[i][j]]\n",
" #print(encoded[i][j])\n", " #print(encoded[i][j])\n",
" \n", " \n",
" return encoding_dict, encoded, new_error.reshape((512,640))\n", " return encoding_dict, encoded, new_error.reshape((512,640)), image\n",
" #print(encoding)" " #print(encoding)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 264, "execution_count": 109,
"id": "ffa858e8", "id": "ffa858e8",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['22541' '-10' '14' ... '32' '48' '33']\n",
" ['7' '67' '-21' ... '-1' '-1' '77']\n",
" ['7' '-15' '-3' ... '10' '-45' '58']\n",
" ...\n",
" ['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)" "encode_dict, encoding, error, orig_image = enc_experiment(images, plot=False)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 265, "execution_count": 110,
"id": "85815094", "id": "8dfdedc6",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"11110111110\n", "[[22541 22531 22555 ... 22573 22589 22574]\n",
"92\n", " [22548 22544 22530 ... 22607 22612 22618]\n",
"11110111110001\n" " [22548 22544 22560 ... 22603 22605 22599]\n",
" ...\n",
" [22590 22593 22596 ... 22586 22627 22692]\n",
" [22568 22575 22555 ... 22625 22702 22749]\n",
" [22558 22541 22536 ... 22679 22748 22767]]\n"
] ]
} }
], ],
"source": [ "source": [
"print(encoding[0][100])\n", "print(orig_image)"
"print(error[0][100])\n",
"print(encode_dict['92'])"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 148,
"id": "825cc48c", "id": "825cc48c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -451,14 +474,61 @@ ...@@ -451,14 +474,61 @@
" Function that accecpts the prediction matrix A for the linear system,\n", " Function that accecpts the prediction matrix A for the linear system,\n",
" the encoded matrix of error values, and the encoding dicitonary.\n", " the encoded matrix of error values, and the encoding dicitonary.\n",
" \"\"\"\n", " \"\"\"\n",
" the_keys = list(encode_dict.keys())\n",
" the_values = list(encode_dict.values())\n",
" error_matrix = encoded_matrix.copy()\n", " error_matrix = encoded_matrix.copy()\n",
" \n",
" for i in range(error_matrix.shape[0]):\n", " for i in range(error_matrix.shape[0]):\n",
" for j in range(error_matrix.shape[1]):\n", " for j in range(error_matrix.shape[1]):\n",
" if i == 0 and j == 0:\n", " if i == 0 and j == 0:\n",
" error_matrix[i][j] = encoded_matrix[i][j]\n", " error_matrix[i][j] = int(encoded_matrix[i][j])\n",
" elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:\n",
" error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) + error_matrix[0][0]\n",
" else:\n", " else:\n",
" error_matrix[i][j] = encoding_dict." " if j == 1 and i == 1:\n",
" z0, z1, z2, z3 = error_matrix[i-1][j-1], error_matrix[i-1][j], \\\n",
" error_matrix[i-1][j+1], error_matrix[i][j-1]\n",
"\n",
" y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))\n",
" #Real solution that works, DO NOT DELETE\n",
" #new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1]))\n",
"\n",
" print(int(the_keys[the_values.index(error_matrix[i,j])]))\n",
" print(np.linalg.solve(A,y)[-1])\n",
" error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) + \\\n",
" np.linalg.solve(A,y)[-1][0]\n",
" #error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])])\n",
" break\n",
" \n",
" return error_matrix"
] ]
},
{
"cell_type": "code",
"execution_count": 149,
"id": "ba1d2c2c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"67\n",
"[22555.]\n"
]
}
],
"source": [
"em = decoder(A, encoding, encode_dict)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2cdce6d",
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
......
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