Skip to content

Commit

Permalink
add example notebook for cache_database
Browse files Browse the repository at this point in the history
  • Loading branch information
XzzX committed Dec 6, 2024
1 parent 6e364a9 commit b562344
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions notebooks/cache_database.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "022f96ae-b55e-4eab-af77-d129b48986b0",
"metadata": {},
"outputs": [],
"source": [
"from pyiron_workflow import Workflow\n",
"from pyiron_xzzx.cache_database.node import store_node_in_database, restore_node_from_database, restore_node_outputs\n",
"from pyiron_xzzx.cache_database.cache_database import CacheDatabase"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "af7970c2-c9b9-425c-8772-890693b808f0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'xy__b': -1, 'yz__a': 5, 'output__a': 2, 'output__b': 4}\n"
]
}
],
"source": [
"@Workflow.wrap.as_function_node()\n",
"def AddNode(x: int = 1, y: int = 2) -> tuple[int, int]:\n",
" a = x + y\n",
" b = x - y\n",
" return a, b\n",
" \n",
"wf = Workflow(\"wf\")\n",
"wf.x = 1\n",
"wf.y = 2\n",
"wf.z = 3\n",
"wf.xy = AddNode(wf.x, wf.y)\n",
"wf.yz = AddNode(wf.y, wf.z)\n",
"wf.output = AddNode()\n",
"wf.output.inputs.x = wf.xy.outputs.a\n",
"wf.output.inputs.y = wf.yz.outputs.b\n",
"print(wf())\n",
"\n",
"# wf.draw()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ebaf4cec-898b-4b7f-b323-f6f27a6895a6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node_hash: 7444eb79b26437f3934876c731cb89a67d21dfc710847bf5b0f3a9ccbfdd4417\n"
]
},
{
"data": {
"text/plain": [
"{'output__a': 2, 'output__b': 4, 'xy__b': -1, 'yz__a': 5}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db = CacheDatabase(\"postgresql://seibl:seibl@localhost/pyiron\")\n",
"db.drop_table()\n",
"db.create_table()\n",
"node_hash = store_node_in_database(db, wf.output, store_outputs=True, store_input_nodes_recursively=True)\n",
"print(\"node_hash: \", node_hash)\n",
"\n",
"wf2 = Workflow(\"wf2\")\n",
"node = restore_node_from_database(db, node_hash, wf2)\n",
"wf2()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "845d4e32-084f-484e-b604-02d77af15dc9",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"connections": "[]",
"default": "\"NOT_DATA\"",
"owner": "\"/wf3/output\"",
"strict_hints": "true",
"type_hint": "\"<class 'int'>\"",
"value": "\"NOT_DATA\""
},
"text/plain": [
"<pyiron_workflow.mixin.injection.OutputDataWithInjection at 0x763f18ed6600>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"connections": "[]",
"default": "\"NOT_DATA\"",
"owner": "\"/wf3/output\"",
"strict_hints": "true",
"type_hint": "\"<class 'int'>\"",
"value": "2"
},
"text/plain": [
"<pyiron_workflow.mixin.injection.OutputDataWithInjection at 0x763f18ed6600>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"wf3 = Workflow(\"wf3\")\n",
"node = restore_node_from_database(db, node_hash, wf3)\n",
"display(wf3.output.outputs[\"a\"])\n",
"restore_node_outputs(wf3.output)\n",
"display(wf3.output.outputs[\"a\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c35fc4b4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pyiron_workflow",
"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.12.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit b562344

Please sign in to comment.