Skip to content

Commit

Permalink
Refactor code to use DataManagerSingleton for data retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
perceptualrobots committed Aug 25, 2024
1 parent ce1231a commit fbd25eb
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 63 deletions.
101 changes: 62 additions & 39 deletions nbs/14_helpers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 81,
"metadata": {},
"outputs": [
{
Expand All @@ -33,7 +33,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -42,7 +42,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 83,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -64,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -173,7 +173,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -214,7 +214,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -255,7 +255,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -296,7 +296,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -330,7 +330,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 89,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -368,7 +368,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -399,7 +399,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -430,7 +430,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -472,7 +472,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 93,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -589,7 +589,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -629,14 +629,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution time of challenges load: 0.0912 seconds\n",
"Execution time of challenges load: 0.0610 seconds\n",
"Execution time of solutions load: 0.0040 seconds\n",
"['007bbfb7', '00d62c1b', '017c7c7b', '025d127b', '045e512c', '0520fde7', '05269061', '05f2a901', '06df4c85', '08ed6ac7', '09629e4f', '0962bcdd', '0a938d79', '0b148d64', '0ca9ddb6', '0d3d703e', '0dfd9992', '0e206a2e', '10fcaaa3', '11852cab', '1190e5a7', '137eaa0f', '150deff5', '178fcbfb', '1a07d186', '1b2d62fb', '1b60fb0c', '1bfc4729', '1c786137', '1caeab9d', '1cf80156', '1e0a9b12', '1e32b0e9', '1f0c79e5', '1f642eb9', '1f85a75f', '1f876c06', '1fad071e', '2013d3e2', '2204b7a8', '22168020', '22233c11', '2281f1f4', '228f6490', '22eb0ac0', '234bbc79', '23581191', '239be575', '23b5c85d', '253bf280', '25d487eb', '25d8a9c8', '25ff71a9', '264363fd', '272f95fa', '27a28665', '28bf18c6', '28e73c20', '29623171', '29c11459', '29ec7d0e', '2bcee788', '2bee17df', '2c608aff', '2dc579da', '2dd70a9a', '2dee498d', '31aa019c', '321b1fc6', '32597951', '3345333e', '3428a4f5', '3618c87e', '3631a71a', '363442ee', '36d67576', '36fdfd69', '3906de3d', '39a8645d', '39e1d7f9', '3aa6fb7a', '3ac3eb23', '3af2c5a8', '3bd67248', '3bdb4ada', '3befdf3e', '3c9b0459', '3de23699', '3e980e27', '3eda0437', '3f7978a0', '40853293', '4093f84a', '41e4d17e', '4258a5f9', '4290ef0e', '42a50994', '4347f46a', '444801d8', '445eab21', '447fd412', '44d8ac46', '44f52bb0', '4522001f', '4612dd53', '46442a0e', '469497ad', '46f33fce', '47c1f68c', '484b58aa', '48d8fb45', '4938f0c2', '496994bd', '49d1d64f', '4be741c5', '4c4377d9', '4c5c2cf0', '50846271', '508bd3b6', '50cb2852', '5117e062', '5168d44c', '539a4f51', '53b68214', '543a7ed5', '54d82841', '54d9e175', '5521c0d9', '5582e5ca', '5614dbcf', '56dc2b01', '56ff96f3', '57aa92db', '5ad4f10b', '5bd6f4ac', '5c0a986e', '5c2c9af4', '5daaa586', '60b61512', '6150a2bd', '623ea044', '62c24649', '63613498', '6430c8c4', '6455b5f5', '662c240a', '67385a82', '673ef223', '6773b310', '67a3c6ac', '67a423a3', '67e8384a', '681b3aeb', '6855a6e4', '68b16354', '694f12f3', '6a1e5592', '6aa20dc0', '6b9890af', '6c434453', '6cdd2623', '6cf79266', '6d0160f0', '6d0aefbc', '6d58a25d', '6d75e8bb', '6e02f1e3', '6e19193c', '6e82a1ae', '6ecd11f4', '6f8cd79b', '6fa7a44f', '72322fa7', '72ca375d', '73251a56', '7447852a', '7468f01a', '746b3537', '74dd1130', '75b8110e', '760b3cac', '776ffc46', '77fdfe62', '780d0b14', '7837ac64', '794b24be', '7b6016b9', '7b7f7511', '7c008303', '7ddcd7ec', '7df24a62', '7e0986d6', '7f4411dc', '7fe24cdd', '80af3007', '810b9b61', '82819916', '83302e8f', '834ec97d', '8403a5d5', '846bdb03', '855e0971', '85c4e7cd', '868de0fa', '8731374e', '88a10436', '88a62173', '890034e9', '8a004b2b', '8be77c9e', '8d5021e8', '8d510a79', '8e1813be', '8e5a5113', '8eb1be9a', '8efcae92', '8f2ea7aa', '90c28cc7', '90f3ed37', '913fb3ed', '91413438', '91714a58', '9172f3a0', '928ad970', '93b581b8', '941d9a10', '94f9d214', '952a094c', '9565186b', '95990924', '963e52fc', '97999447', '97a05b5b', '98cf29f8', '995c5fa3', '99b1bc43', '99fa7670', '9aec4887', '9af7a82c', '9d9215db', '9dfd6313', '9ecd008a', '9edfc990', '9f236235', 'a1570a43', 'a2fd1cf0', 'a3325580', 'a3df8b1e', 'a416b8f3', 'a48eeaf7', 'a5313dff', 'a5f85a15', 'a61ba2ce', 'a61f2674', 'a64e4611', 'a65b410d', 'a68b268e', 'a699fb00', 'a740d043', 'a78176bb', 'a79310a0', 'a85d4709', 'a87f7484', 'a8c38be5', 'a8d7556c', 'a9f96cdd', 'aabf363d', 'aba27056', 'ac0a08a4', 'ae3edfdc', 'ae4f1146', 'aedd82e4', 'af902bf9', 'b0c4d837', 'b190f7f5', 'b1948b0a', 'b230c067', 'b27ca6d3', 'b2862040', 'b527c5c6', 'b548a754', 'b60334d2', 'b6afb2da', 'b7249182', 'b775ac94', 'b782dc8a', 'b8825c91', 'b8cdaf2b', 'b91ae062', 'b94a9452', 'b9b7f026', 'ba26e723', 'ba97ae07', 'bb43febb', 'bbc9ae5d', 'bc1d5164', 'bd4472b8', 'bda2d7a6', 'bdad9b1f', 'be94b721', 'beb8660c', 'c0f76784', 'c1d99e64', 'c3e719e8', 'c3f564a4', 'c444b776', 'c59eb873', 'c8cbb738', 'c8f0f002', 'c909285e', 'c9e6f938', 'c9f8e694', 'caa06a1f', 'cbded52d', 'cce03e0d', 'cdecee7f', 'ce22a75a', 'ce4f8723', 'ce602527', 'ce9e57f2', 'cf98881b', 'd037b0a7', 'd06dbe63', 'd07ae81c', 'd0f5fe59', 'd10ecb37', 'd13f3404', 'd22278a0', 'd23f8c26', 'd2abd087', 'd364b489', 'd406998b', 'd43fd935', 'd4469b4b', 'd4a91cb9', 'd4f3cd78', 'd511f180', 'd5d6de2d', 'd631b094', 'd687bc17', 'd6ad076f', 'd89b689b', 'd8c310e9', 'd90796e8', 'd9f24cd1', 'd9fac9be', 'dae9d2b5', 'db3e9e38', 'db93a21d', 'dbc1a6ce', 'dc0a314f', 'dc1df850', 'dc433765', 'ddf7fa4f', 'de1cd16c', 'ded97339', 'e179c5f4', 'e21d9049', 'e26a3af2', 'e3497940', 'e40b9e2f', 'e48d4e1a', 'e5062a87', 'e509e548', 'e50d258f', 'e6721834', 'e73095fd', 'e76a88a6', 'e8593010', 'e8dc4411', 'e9614598', 'e98196ab', 'e9afcf9a', 'ea32f347', 'ea786f4a', 'eb281b96', 'eb5a1d5d', 'ec883f72', 'ecdecbb3', 'ed36ccf7', 'ef135b50', 'f15e1fac', 'f1cefba8', 'f25fbde4', 'f25ffba3', 'f2829549', 'f35d900a', 'f5b8619d', 'f76d97a5', 'f8a8fe49', 'f8b3ba0a', 'f8c80d96', 'f8ff0b80', 'f9012d9b', 'fafffa47', 'fcb5c309', 'fcc82909', 'feca6190', 'ff28f65a', 'ff805c23']\n",
"400\n",
Expand Down Expand Up @@ -716,7 +716,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -732,6 +732,11 @@
" DataManagerSingleton._instance = DataManagerSingleton(folder, prefix, show_timing)\n",
" return DataManagerSingleton._instance\n",
"\n",
" # def get_instance():\n",
" # if DataManagerSingleton._instance is None:\n",
" # raise ValueError(\"DataManagerSingleton instance has not been created. Use get_instance(folder, prefix, show_timing) method to create an instance.\")\n",
" # return DataManagerSingleton._instance\n",
"\n",
" def __init__(self, folder: str, prefix: str, show_timing: bool = False):\n",
" if DataManagerSingleton._instance is not None:\n",
" raise Exception(\"This class is a singleton!\")\n",
Expand All @@ -748,6 +753,12 @@
" solutions = self.solutions_manager.get_data_for_key(code)\n",
" return solutions\n",
"\n",
" def get_num_arrays_for_code(self, code: str) -> int:\n",
" num_arrays = 0\n",
" data = self.get_data_for_code(code)\n",
" if 'train' in data:\n",
" num_arrays += len(data['train'])\n",
" return num_arrays\n",
"\n",
" def reload_data(self, folder: str, prefix: str): \n",
" self.folder = folder\n",
Expand All @@ -758,19 +769,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution time of __init__: 0.0000 seconds\n",
"Execution time of __init__: 0.0011 seconds\n",
"Execution time of get_data_for_key: 0.0000 seconds\n",
"Execution time of __init__: 0.0010 seconds\n",
"{'test': [{'input': [[2]]}], 'train': [{'input': [[3]], 'output': [[4]]}, {'input': [[7]], 'output': [[8]]}, {'input': [[1]], 'output': [[2]]}]}\n",
"Execution time of get_data_for_key: 0.0000 seconds\n",
"[[3]]\n"
"[[3]]\n",
"3\n"
]
}
],
Expand All @@ -781,50 +791,50 @@
"data = data_mgr.get_data_for_code(code)\n",
"print(data)\n",
"out = data_mgr.get_solutions_for_code(code)\n",
"print(out)"
"print(out)\n",
"print(data_mgr.get_num_arrays_for_code(code))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution time of get_data_for_key: 0.0000 seconds\n",
"{'test': [{'input': [[2, 2], [2, 2]]}], 'train': [{'input': [[3, 3], [3, 3]], 'output': [[4, 4], [4, 4]]}, {'input': [[7, 7], [7, 7]], 'output': [[8, 8], [8, 8]]}, {'input': [[1, 1], [1, 1]], 'output': [[2, 2], [2, 2]]}]}\n",
"Execution time of get_data_for_key: 0.0000 seconds\n",
"[[3, 3], [3, 3]]\n"
"[[3, 3], [3, 3]]\n",
"3\n"
]
}
],
"source": [
"#| gui\n",
"code = '00000003'\n",
"# data_mgr = DataManagerSingleton.get_instance(folder = 'c:/tmp/arc-prize-2024', prefix = 'arc-agi_simple_')\n",
"data = data_mgr.get_data_for_code(code)\n",
"data_mgr1 = DataManagerSingleton.get_instance()\n",
"data = data_mgr1.get_data_for_code(code)\n",
"print(data)\n",
"out = data_mgr.get_solutions_for_code(code)\n",
"print(out)"
"out = data_mgr1.get_solutions_for_code(code)\n",
"print(out)\n",
"print(data_mgr1.get_num_arrays_for_code(code))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution time of reload_data: 0.0622 seconds\n",
"Execution time of reload_data: 0.0041 seconds\n",
"Execution time of get_data_for_key: 0.0000 seconds\n",
"Execution time of reload_data: 0.0610 seconds\n",
"Execution time of reload_data: 0.0340 seconds\n",
"{'test': [{'input': [[7, 0, 7], [7, 0, 7], [7, 7, 0]]}], 'train': [{'input': [[0, 7, 7], [7, 7, 7], [0, 7, 7]], 'output': [[0, 0, 0, 0, 7, 7, 0, 7, 7], [0, 0, 0, 7, 7, 7, 7, 7, 7], [0, 0, 0, 0, 7, 7, 0, 7, 7], [0, 7, 7, 0, 7, 7, 0, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7], [0, 7, 7, 0, 7, 7, 0, 7, 7], [0, 0, 0, 0, 7, 7, 0, 7, 7], [0, 0, 0, 7, 7, 7, 7, 7, 7], [0, 0, 0, 0, 7, 7, 0, 7, 7]]}, {'input': [[4, 0, 4], [0, 0, 0], [0, 4, 0]], 'output': [[4, 0, 4, 0, 0, 0, 4, 0, 4], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 4, 0, 0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 4, 0, 4, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 4, 0, 0, 0, 0]]}, {'input': [[0, 0, 0], [0, 0, 2], [2, 0, 2]], 'output': [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0, 2], [2, 0, 2, 0, 0, 0, 2, 0, 2]]}, {'input': [[6, 6, 0], [6, 0, 0], [0, 6, 6]], 'output': [[6, 6, 0, 6, 6, 0, 0, 0, 0], [6, 0, 0, 6, 0, 0, 0, 0, 0], [0, 6, 6, 0, 6, 6, 0, 0, 0], [6, 6, 0, 0, 0, 0, 0, 0, 0], [6, 0, 0, 0, 0, 0, 0, 0, 0], [0, 6, 6, 0, 0, 0, 0, 0, 0], [0, 0, 0, 6, 6, 0, 6, 6, 0], [0, 0, 0, 6, 0, 0, 6, 0, 0], [0, 0, 0, 0, 6, 6, 0, 6, 6]]}, {'input': [[2, 2, 2], [0, 0, 0], [0, 2, 2]], 'output': [[2, 2, 2, 2, 2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 2, 0, 2, 2, 0, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 2, 0, 2, 2]]}]}\n",
"Execution time of get_data_for_key: 0.0000 seconds\n",
"[[7, 0, 7, 0, 0, 0, 7, 0, 7], [7, 0, 7, 0, 0, 0, 7, 0, 7], [7, 7, 0, 0, 0, 0, 7, 7, 0], [7, 0, 7, 0, 0, 0, 7, 0, 7], [7, 0, 7, 0, 0, 0, 7, 0, 7], [7, 7, 0, 0, 0, 0, 7, 7, 0], [7, 0, 7, 7, 0, 7, 0, 0, 0], [7, 0, 7, 7, 0, 7, 0, 0, 0], [7, 7, 0, 7, 7, 0, 0, 0, 0]]\n"
"[[7, 0, 7, 0, 0, 0, 7, 0, 7], [7, 0, 7, 0, 0, 0, 7, 0, 7], [7, 7, 0, 0, 0, 0, 7, 7, 0], [7, 0, 7, 0, 0, 0, 7, 0, 7], [7, 0, 7, 0, 0, 0, 7, 0, 7], [7, 7, 0, 0, 0, 0, 7, 7, 0], [7, 0, 7, 7, 0, 7, 0, 0, 0], [7, 0, 7, 7, 0, 7, 0, 0, 0], [7, 7, 0, 7, 7, 0, 0, 0, 0]]\n",
"5\n"
]
}
],
Expand All @@ -835,12 +845,13 @@
"data = data_mgr.get_data_for_code(code)\n",
"print(data)\n",
"out = data_mgr.get_solutions_for_code(code)\n",
"print(out)"
"print(out)\n",
"print(data_mgr.get_num_arrays_for_code(code))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -854,6 +865,18 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit fbd25eb

Please sign in to comment.