refactor dispersion_tutorial.ipynb

This commit is contained in:
2025-03-20 11:15:48 -05:00
parent fdcf31948f
commit 7b1ba996ae
3 changed files with 263 additions and 229 deletions

View File

@@ -1,36 +1,71 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"## Dispersion"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"source": [
"from src.user_keypad import UserKeypad\n",
"from src.utils import random_property_rotation\n",
"from IPython.display import Markdown, display\n",
"from src.models import KeypadSize\n",
"import numpy as np\n",
"\n",
"def random_property_rotation(\n",
" user_keypad: np.ndarray,\n",
" prop_rotation: list[int]\n",
") -> np.ndarray:\n",
" transposed = user_keypad.T\n",
" if len(prop_rotation) != len(transposed):\n",
" raise ValueError(\"prop_rotation must be the same length as the number of properties\")\n",
" for idx, prop_set in enumerate(transposed):\n",
" rotation = prop_rotation[idx]\n",
" rotation = rotation % len(prop_set) if len(prop_set) > 0 else 0\n",
" transposed[idx] = np.roll(prop_set, rotation)\n",
" return transposed.T\n",
"\n",
"def keypad_md_table(keypad_list: np.ndarray, keypad_size: KeypadSize) -> str:\n",
" assert (keypad_size.total_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 = \"||\" + \"\".join([f\"position {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 += f\"\\n|key {key}|\"\n",
" table += \"|\".join([str(prop) for prop in keypad[key]])\n",
" table += \"|\"\n",
" return table\n",
" return table"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2025-03-20T16:15:24.214098Z",
"start_time": "2025-03-20T16:15:24.207220Z"
}
},
"outputs": [],
"execution_count": 80
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Keypad Dispersion\n",
"\n",
"Keypad dispersion refers to an operation that redistributes the properties assigned to each key on a keypad, ensuring that no property shares a key with a property that was previously adjacent to it.\n",
"Keypads are only dispersable if `numb_of_keys <= properites_per_key`\n",
"\n",
"A keypad dispersion is completed in two steps:\n",
"1. Create a property rotation array; a randomly permuted subset of indices, selected without replacement from a range equal to the number of keys on a keypad, with its length truncated to match the number of properties assigned per key.\n",
"2. Rotate each position, similar to a ring or combination lock, by a distance equal to its corresponding value in the property rotation array."
]
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-20T16:15:24.225459Z",
"start_time": "2025-03-20T16:15:24.220286Z"
}
},
"cell_type": "code",
"source": [
"keypad_size = KeypadSize(numb_of_keys=5, props_per_key=4)\n",
"props = [1, 10, 11, 100]\n",
"keypad = []\n",
@@ -38,90 +73,98 @@
" keypad.extend([key_numb * prop for prop in props])\n",
"\n",
"demo_interface = UserKeypad(keypad_size=keypad_size, keypad=np.array(keypad))\n",
"\n",
"display(Markdown(f\"\"\"\n",
"## Example Keypad\n",
"{keypad_size.numb_of_keys} X {keypad_size.props_per_key} keypad ({keypad_size.numb_of_keys} keys, {keypad_size.props_per_key} properties per key).\n",
"\"\"\"))\n",
"display(Markdown(keypad_md_table(demo_interface.keypad, keypad_size)))\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2025-03-14T14:34:55.702615Z",
"start_time": "2025-03-14T14:34:55.694963Z"
}
},
"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|"
"text/markdown": "\n## Example Keypad\n5 X 4 keypad (5 keys, 4 properties per key).\n"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "||position 0|position 1|position 2|position 3|\n|-|-|-|-|-|\n|key 0|1|10|11|100|\n|key 1|2|20|22|200|\n|key 2|3|30|33|300|\n|key 3|4|40|44|400|\n|key 4|5|50|55|500|"
},
"metadata": {},
"output_type": "display_data"
}
],
"execution_count": 4
"execution_count": 81
},
{
"cell_type": "code",
"source": [
"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": "2025-03-14T14:34:55.718161Z",
"start_time": "2025-03-14T14:34:55.714512Z"
}
},
"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|4|40|44|400|\n|key3|3|30|33|300|\n|key4|2|20|22|200|\n|key5|5|50|55|500|"
},
"metadata": {},
"output_type": "display_data"
}
],
"execution_count": 5
"metadata": {},
"cell_type": "markdown",
"source": "### Create Property Rotation Array"
},
{
"cell_type": "code",
"source": [
"prop_rotation = np.random.choice(range(keypad_size.numb_of_keys), size=keypad_size.props_per_key, replace=False)\n",
"dispersed_interface = random_property_rotation(\n",
" shuffled_keys,\n",
" prop_rotation\n",
")\n",
"\n",
"display(Markdown(keypad_md_table(dispersed_interface.reshape(-1), keypad_size)))\n"
"print(f\"Property Rotation: {prop_rotation}\")\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2025-03-14T14:34:55.731332Z",
"start_time": "2025-03-14T14:34:55.728135Z"
"end_time": "2025-03-20T16:15:24.240606Z",
"start_time": "2025-03-20T16:15:24.237773Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Property Rotation: [4 3 2 0]\n"
]
}
],
"execution_count": 82
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Apply the Rotation"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-20T16:15:24.256086Z",
"start_time": "2025-03-20T16:15:24.253226Z"
}
},
"cell_type": "code",
"source": [
"dispersed_interface = random_property_rotation(\n",
" demo_interface.keypad.reshape(-1, keypad_size.props_per_key),\n",
" prop_rotation\n",
")\n",
"display(Markdown(keypad_md_table(dispersed_interface.reshape(-1), keypad_size)))"
],
"outputs": [
{
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|5|30|44|100|\n|key2|1|20|33|400|\n|key3|4|50|22|300|\n|key4|3|10|55|200|\n|key5|2|40|11|500|"
"text/markdown": "||position 0|position 1|position 2|position 3|\n|-|-|-|-|-|\n|key 0|2|30|44|100|\n|key 1|3|40|55|200|\n|key 2|4|50|11|300|\n|key 3|5|10|22|400|\n|key 4|1|20|33|500|"
},
"metadata": {},
"output_type": "display_data"
}
],
"execution_count": 6
"execution_count": 83
}
],
"metadata": {