refactor dispersion_tutorial.ipynb

This commit is contained in:
2025-03-14 06:18:01 -05:00
parent 762639de8a
commit e99daab90a

View File

@@ -11,32 +11,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1,
"outputs": [
{
"data": {
"text/plain": "<IPython.core.display.Markdown object>",
"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": [ "source": [
"from src.utils import secure_fisher_yates_shuffle, matrix_to_list, list_to_matrix\n", "from src.user_keypad import UserKeypad\n",
"from src.user_interface import UserKeypad\n",
"from IPython.display import Markdown, display\n", "from IPython.display import Markdown, display\n",
"from src.models import KeypadSize\n", "from src.models import KeypadSize\n",
"import numpy as np\n",
"\n", "\n",
"def keypad_md_table(interface: list[int], keypad_size: KeypadSize) -> str:\n", "def keypad_md_table(keypad_list: np.ndarray, keypad_size: KeypadSize) -> str:\n",
" assert (keypad_size.numb_of_props == len(interface))\n", " assert (keypad_size.numb_of_props == len(keypad_list))\n",
" interface_keypad = list_to_matrix(interface, keypad_size.props_per_key)\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 = \"|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", " table += \"\\n|\" + \"\".join(\"-|\" for _ in range(keypad_size.props_per_key + 1))\n",
"\n", "\n",
" for key in range(keypad_size.numb_of_keys):\n", " for key in range(keypad_size.numb_of_keys):\n",
" table += f\"\\n|key{key+1}|\"\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", " table += \"|\"\n",
" return table\n", " return table\n",
"\n", "\n",
@@ -47,74 +36,91 @@
"for key_numb in range(1,keypad_size.numb_of_keys+1):\n", "for key_numb in range(1,keypad_size.numb_of_keys+1):\n",
" keypad.extend([key_numb * attr for attr in attrs])\n", " keypad.extend([key_numb * attr for attr in attrs])\n",
"\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", "\n",
"display(Markdown(keypad_md_table(demo_interface.keypad, keypad_size)))\n" "display(Markdown(keypad_md_table(demo_interface.keypad, keypad_size)))\n"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-07-24T22:23:18.200797Z", "end_time": "2025-03-14T11:14:49.006056Z",
"start_time": "2024-07-24T22:23:18.119660Z" "start_time": "2025-03-14T11:14:48.964902Z"
}
} }
}, },
{
"cell_type": "code",
"execution_count": 2,
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": "<IPython.core.display.Markdown object>", "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|" "<IPython.core.display.Markdown object>"
],
"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": {}, "metadata": {},
"output_type": "display_data" "output_type": "display_data"
} }
], ],
"execution_count": 1
},
{
"cell_type": "code",
"source": [ "source": [
"demo_interface_matrix = list_to_matrix(demo_interface.keypad, demo_interface.keypad_size.props_per_key)\n", "demo_interface_matrix = demo_interface.keypad.reshape(-1, demo_interface.keypad_size.props_per_key)\n",
"shuffled_keys = secure_fisher_yates_shuffle(demo_interface_matrix)\n", "shuffled_keys = np.random.permutation(demo_interface_matrix)\n",
"shuffled_keys_list = matrix_to_list(shuffled_keys)\n", "shuffled_keys_list = shuffled_keys.reshape(-1)\n",
"display(Markdown(keypad_md_table(shuffled_keys_list, keypad_size)))\n" "display(Markdown(keypad_md_table(shuffled_keys_list, keypad_size)))"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-07-24T22:23:18.201039Z", "end_time": "2025-03-14T11:15:32.023001Z",
"start_time": "2024-07-24T22:23:18.185696Z" "start_time": "2025-03-14T11:15:32.009838Z"
}
} }
}, },
{
"cell_type": "code",
"execution_count": 3,
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": "<IPython.core.display.Markdown object>", "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|" "<IPython.core.display.Markdown object>"
],
"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": {}, "metadata": {},
"output_type": "display_data" "output_type": "display_data"
} }
], ],
"execution_count": 3
},
{
"cell_type": "code",
"source": [ "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", "dispersed_interface = UserKeypad.random_attribute_rotation(\n",
" shuffled_keys,\n", " shuffled_keys,\n",
" attr_rotation\n", " attr_rotation\n",
")\n", ")\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": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-07-24T22:23:18.201164Z", "end_time": "2025-03-14T11:17:30.317806Z",
"start_time": "2024-07-24T22:23:18.190198Z" "start_time": "2025-03-14T11:17:30.313082Z"
} }
},
"outputs": [
{
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"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": { "metadata": {