From e99daab90a69abe11af261cb8f40c48fa057115c Mon Sep 17 00:00:00 2001 From: Donovan Date: Fri, 14 Mar 2025 06:18:01 -0500 Subject: [PATCH] refactor dispersion_tutorial.ipynb --- notebooks/dispersion_tutorial.ipynb | 96 +++++++++++++++-------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/notebooks/dispersion_tutorial.ipynb b/notebooks/dispersion_tutorial.ipynb index 2d08272..bfd7754 100644 --- a/notebooks/dispersion_tutorial.ipynb +++ b/notebooks/dispersion_tutorial.ipynb @@ -11,32 +11,21 @@ }, { "cell_type": "code", - "execution_count": 1, - "outputs": [ - { - "data": { - "text/plain": "", - "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|1|10|11|100|\n|key2|2|20|22|200|\n|key3|3|30|33|300|\n|key4|4|40|44|400|\n|key5|5|50|55|500|" - }, - "metadata": {}, - "output_type": "display_data" - } - ], "source": [ - "from src.utils import secure_fisher_yates_shuffle, matrix_to_list, list_to_matrix\n", - "from src.user_interface import UserKeypad\n", + "from src.user_keypad import UserKeypad\n", "from IPython.display import Markdown, display\n", "from src.models import KeypadSize\n", + "import numpy as np\n", "\n", - "def keypad_md_table(interface: list[int], keypad_size: KeypadSize) -> str:\n", - " assert (keypad_size.numb_of_props == len(interface))\n", - " interface_keypad = list_to_matrix(interface, keypad_size.props_per_key)\n", + "def keypad_md_table(keypad_list: np.ndarray, keypad_size: KeypadSize) -> str:\n", + " assert (keypad_size.numb_of_props == len(keypad_list))\n", + " keypad = keypad_list.reshape(-1, keypad_size.props_per_key)\n", " table = \"|key|\" + \"\".join([f\"set{idx}|\" for idx in range(keypad_size.props_per_key)])\n", " table += \"\\n|\" + \"\".join(\"-|\" for _ in range(keypad_size.props_per_key + 1))\n", "\n", " for key in range(keypad_size.numb_of_keys):\n", " table += f\"\\n|key{key+1}|\"\n", - " table += \"|\".join([str(attr) for attr in interface_keypad[key]])\n", + " table += \"|\".join([str(attr) for attr in keypad[key]])\n", " table += \"|\"\n", " return table\n", "\n", @@ -47,74 +36,91 @@ "for key_numb in range(1,keypad_size.numb_of_keys+1):\n", " keypad.extend([key_numb * attr for attr in attrs])\n", "\n", - "demo_interface = UserKeypad(keypad_size=keypad_size, keypad=keypad)\n", + "demo_interface = UserKeypad(keypad_size=keypad_size, keypad=np.array(keypad))\n", "\n", "display(Markdown(keypad_md_table(demo_interface.keypad, keypad_size)))\n" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-07-24T22:23:18.200797Z", - "start_time": "2024-07-24T22:23:18.119660Z" + "end_time": "2025-03-14T11:14:49.006056Z", + "start_time": "2025-03-14T11:14:48.964902Z" } - } - }, - { - "cell_type": "code", - "execution_count": 2, + }, "outputs": [ { "data": { - "text/plain": "", - "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|3|30|33|300|\n|key2|1|10|11|100|\n|key3|5|50|55|500|\n|key4|2|20|22|200|\n|key5|4|40|44|400|" + "text/plain": [ + "" + ], + "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|1|10|11|100|\n|key2|2|20|22|200|\n|key3|3|30|33|300|\n|key4|4|40|44|400|\n|key5|5|50|55|500|" }, "metadata": {}, "output_type": "display_data" } ], + "execution_count": 1 + }, + { + "cell_type": "code", "source": [ - "demo_interface_matrix = list_to_matrix(demo_interface.keypad, demo_interface.keypad_size.props_per_key)\n", - "shuffled_keys = secure_fisher_yates_shuffle(demo_interface_matrix)\n", - "shuffled_keys_list = matrix_to_list(shuffled_keys)\n", - "display(Markdown(keypad_md_table(shuffled_keys_list, keypad_size)))\n" + "demo_interface_matrix = demo_interface.keypad.reshape(-1, demo_interface.keypad_size.props_per_key)\n", + "shuffled_keys = np.random.permutation(demo_interface_matrix)\n", + "shuffled_keys_list = shuffled_keys.reshape(-1)\n", + "display(Markdown(keypad_md_table(shuffled_keys_list, keypad_size)))" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-07-24T22:23:18.201039Z", - "start_time": "2024-07-24T22:23:18.185696Z" + "end_time": "2025-03-14T11:15:32.023001Z", + "start_time": "2025-03-14T11:15:32.009838Z" } - } - }, - { - "cell_type": "code", - "execution_count": 3, + }, "outputs": [ { "data": { - "text/plain": "", - "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|2|10|55|400|\n|key2|4|50|22|300|\n|key3|3|20|44|100|\n|key4|1|40|33|500|\n|key5|5|30|11|200|" + "text/plain": [ + "" + ], + "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|4|40|44|400|\n|key2|3|30|33|300|\n|key3|2|20|22|200|\n|key4|1|10|11|100|\n|key5|5|50|55|500|" }, "metadata": {}, "output_type": "display_data" } ], + "execution_count": 3 + }, + { + "cell_type": "code", "source": [ - "attr_rotation = secure_fisher_yates_shuffle(list(range(keypad_size.numb_of_keys)))[:keypad_size.props_per_key]\n", + "attr_rotation = np.random.choice(range(keypad_size.numb_of_keys), size=keypad_size.props_per_key, replace=False)\n", "dispersed_interface = UserKeypad.random_attribute_rotation(\n", " shuffled_keys,\n", " attr_rotation\n", ")\n", "\n", - "display(Markdown(keypad_md_table(matrix_to_list(dispersed_interface), keypad_size)))\n" + "display(Markdown(keypad_md_table(dispersed_interface.reshape(-1), keypad_size)))\n" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-07-24T22:23:18.201164Z", - "start_time": "2024-07-24T22:23:18.190198Z" + "end_time": "2025-03-14T11:17:30.317806Z", + "start_time": "2025-03-14T11:17:30.313082Z" } - } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ], + "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|2|50|33|100|\n|key2|1|40|22|500|\n|key3|5|30|11|400|\n|key4|4|20|55|300|\n|key5|3|10|44|200|" + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "execution_count": 5 } ], "metadata": {