accidentally removed important function in utils

This commit is contained in:
2025-03-21 04:38:49 -05:00
parent d6c5e56a30
commit 192ce80598
4 changed files with 159 additions and 55 deletions

View File

@@ -6,42 +6,18 @@
"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 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 = \"||\" + \"\".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}|\"\n",
" table += \"|\".join([str(prop) for prop in keypad[key]])\n",
" table += \"|\"\n",
" return table"
"from src.utils import random_property_rotation, keypad_md_table\n",
"import numpy as np"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2025-03-20T16:15:24.214098Z",
"start_time": "2025-03-20T16:15:24.207220Z"
"end_time": "2025-03-21T09:28:57.936307Z",
"start_time": "2025-03-21T09:28:57.933359Z"
}
},
"outputs": [],
"execution_count": 80
"execution_count": 5
},
{
"metadata": {},
@@ -60,24 +36,24 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-20T16:15:24.225459Z",
"start_time": "2025-03-20T16:15:24.220286Z"
"end_time": "2025-03-21T09:28:57.950591Z",
"start_time": "2025-03-21T09:28:57.945584Z"
}
},
"cell_type": "code",
"source": [
"keypad_size = KeypadSize(numb_of_keys=5, props_per_key=4)\n",
"props = [1, 10, 11, 100]\n",
"keypad = []\n",
"keypad_list = []\n",
"for key_numb in range(1,keypad_size.numb_of_keys+1):\n",
" keypad.extend([key_numb * prop for prop in props])\n",
" keypad_list.extend([key_numb * prop for prop in props])\n",
"\n",
"demo_interface = UserKeypad(keypad_size=keypad_size, keypad=np.array(keypad))\n",
"user_keypad = UserKeypad(keypad_size=keypad_size, keypad=np.array(keypad_list))\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"
"display(Markdown(keypad_md_table(user_keypad.keypad, keypad_size)))\n"
],
"outputs": [
{
@@ -101,7 +77,7 @@
"output_type": "display_data"
}
],
"execution_count": 81
"execution_count": 6
},
{
"metadata": {},
@@ -111,14 +87,14 @@
{
"cell_type": "code",
"source": [
"prop_rotation = np.random.choice(range(keypad_size.numb_of_keys), size=keypad_size.props_per_key, replace=False)\n",
"prop_rotation = np.random.choice(keypad_size.numb_of_keys, size=keypad_size.props_per_key, replace=False)\n",
"print(f\"Property Rotation: {prop_rotation}\")\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2025-03-20T16:15:24.240606Z",
"start_time": "2025-03-20T16:15:24.237773Z"
"end_time": "2025-03-21T09:28:57.961890Z",
"start_time": "2025-03-21T09:28:57.959507Z"
}
},
"outputs": [
@@ -126,11 +102,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Property Rotation: [4 3 2 0]\n"
"Property Rotation: [0 3 1 2]\n"
]
}
],
"execution_count": 82
"execution_count": 7
},
{
"metadata": {},
@@ -140,14 +116,14 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-20T16:15:24.256086Z",
"start_time": "2025-03-20T16:15:24.253226Z"
"end_time": "2025-03-21T09:28:57.976227Z",
"start_time": "2025-03-21T09:28:57.973183Z"
}
},
"cell_type": "code",
"source": [
"dispersed_interface = random_property_rotation(\n",
" demo_interface.keypad.reshape(-1, keypad_size.props_per_key),\n",
" user_keypad.keypad_matrix(),\n",
" prop_rotation\n",
")\n",
"display(Markdown(keypad_md_table(dispersed_interface.reshape(-1), keypad_size)))"
@@ -158,13 +134,13 @@
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"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|"
"text/markdown": "||position 0|position 1|position 2|position 3|\n|-|-|-|-|-|\n|key 0|1|30|55|400|\n|key 1|2|40|11|500|\n|key 2|3|50|22|100|\n|key 3|4|10|33|200|\n|key 4|5|20|44|300|"
},
"metadata": {},
"output_type": "display_data"
}
],
"execution_count": 83
"execution_count": 8
}
],
"metadata": {

View File

@@ -0,0 +1,100 @@
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-21T09:22:33.939365Z",
"start_time": "2025-03-21T09:22:33.911673Z"
}
},
"cell_type": "code",
"source": [
"from src.models import KeypadSize\n",
"from src.user_keypad import UserKeypad\n",
"from src.utils import keypad_md_table\n",
"import numpy as np\n",
"from IPython.display import Markdown, display"
],
"id": "1f073371d04d02ef",
"outputs": [],
"execution_count": 1
},
{
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2025-03-21T09:22:33.948436Z",
"start_time": "2025-03-21T09:22:33.943046Z"
}
},
"cell_type": "code",
"source": [
"keypad_size = KeypadSize(numb_of_keys=5, props_per_key=4)\n",
"props = [1, 10, 11, 100]\n",
"keypad = []\n",
"for key_numb in range(1,keypad_size.numb_of_keys+1):\n",
" keypad.extend([key_numb * prop for prop in props])\n",
"\n",
"demo_interface = UserKeypad(keypad_size=keypad_size, keypad=np.array(keypad))\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)))"
],
"id": "initial_id",
"outputs": [
{
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"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": 2
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "keypad_mat = demo_interface",
"id": "43db2b9d247f420d"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -46,13 +46,11 @@ class UserKeypad:
self.keypad = set_view.T.reshape(-1)
def disperse_keypad(self):
# TODO: clean this up
if not self.keypad_size.is_dispersable:
raise ValueError("Keypad size is not dispersable")
rng = np.random.default_rng()
user_keypad_matrix = self.keypad_matrix()
shuffled_keys = rng.permutation(user_keypad_matrix, axis=0)
prop_rotation = rng.permutation(list(range(self.keypad_size.numb_of_keys)))[:self.keypad_size.props_per_key]
keypad_mat = self.keypad_matrix()
shuffled_keys = np.random.permutation(keypad_mat)
prop_rotation = np.random.choice(self.keypad_size.numb_of_keys, size=self.keypad_size.props_per_key, replace=False)
dispersed_keypad = random_property_rotation(
shuffled_keys,
prop_rotation.tolist(),
@@ -61,14 +59,14 @@ class UserKeypad:
def split_shuffle(self):
# shuffle all keys
keypad_view = self.keypad_matrix()
np.random.shuffle(keypad_view)
keypad_mat = self.keypad_matrix()
np.random.shuffle(keypad_mat)
# select half the property sets
prop_permutation = np.random.permutation(self.keypad_size.props_per_key)[: self.keypad_size.props_per_key // 2]
key_permutation = np.random.permutation(self.keypad_size.numb_of_keys)
# shuffle the selected property sets to new keys as a group
keypad_view[:, prop_permutation] = keypad_view[key_permutation, :][:, prop_permutation]
self.keypad = keypad_view.reshape(-1)
keypad_mat[:, prop_permutation] = keypad_mat[key_permutation, :][:, prop_permutation]
self.keypad = keypad_mat.reshape(-1)
def property_adjacency_graph(self) -> dict[int, set[int]]:
user_keypad_keypad = self.keypad_matrix()

30
src/utils.py Normal file
View File

@@ -0,0 +1,30 @@
import numpy as np
from src.models import KeypadSize
def random_property_rotation(
user_keypad: np.ndarray,
prop_rotation: list[int]
) -> np.ndarray:
transposed = user_keypad.T
if len(prop_rotation) != len(transposed):
raise ValueError("prop_rotation must be the same length as the number of properties")
for idx, prop_set in enumerate(transposed):
rotation = prop_rotation[idx]
rotation = rotation % len(prop_set) if len(prop_set) > 0 else 0
transposed[idx] = np.roll(prop_set, rotation)
return transposed.T
def keypad_md_table(keypad_list: np.ndarray, keypad_size: KeypadSize) -> str:
assert (keypad_size.total_props == len(keypad_list))
keypad = keypad_list.reshape(-1, keypad_size.props_per_key)
table = "||" + "".join([f"position {idx}|" for idx in range(keypad_size.props_per_key)])
table += "\n|" + "".join("-|" for _ in range(keypad_size.props_per_key + 1))
for key in range(keypad_size.numb_of_keys):
table += f"\n|key {key}|"
table += "|".join([str(prop) for prop in keypad[key]])
table += "|"
return table