diff --git a/builder/builder_api.py b/builder/builder_api.py index 0cadb19..86d3538 100644 --- a/builder/builder_api.py +++ b/builder/builder_api.py @@ -196,10 +196,16 @@ def __init__( schema=None, tree=None, core=None, + file_path=None, ): schema = schema or {} tree = tree or {} + if file_path: + with open(file_path, 'r') as file: + graph_data = json.load(file) + tree = deep_merge(tree, graph_data) + self.core = core or ProcessTypes() self.schema, self.tree = self.core.complete(schema, tree) self.node = node_from_tree(self, self.schema, self.tree) @@ -393,6 +399,12 @@ def test_builder(): # save document builder.write(filename='builder_test_doc') + # load builder from document + builder2 = Builder(core=core, file_path='out/builder_test_doc.json') + builder.visualize(filename='builder_test4', + show_values=True, + show_types=True) + if __name__ == '__main__': test_builder() diff --git a/notebooks/demo.ipynb b/notebooks/demo.ipynb index 1d59a05..d2a428a 100644 --- a/notebooks/demo.ipynb +++ b/notebooks/demo.ipynb @@ -20,7 +20,6 @@ }, "outputs": [], "source": [ - "# imports\n", "from builder import Builder, Process, ProcessTypes" ] }, @@ -210,11 +209,11 @@ { "data": { "text/plain": [ - "['console-emitter',\n", + "['toy',\n", " 'ram-emitter',\n", - " 'toy',\n", " 'GillespieInterval',\n", - " 'GillespieEvent']" + " 'GillespieEvent',\n", + " 'console-emitter']" ] }, "execution_count": 8, @@ -319,7 +318,7 @@ "\n" ], "text/plain": [ - "" + "" ] }, "execution_count": 10, @@ -366,7 +365,7 @@ "metadata": {}, "source": [ "### connect ports using connect_all\n", - "`Builder.connect_all` connects ports to stores of the same name" + "`Builder.connect_all` connects ports to stores of the same name." ] }, { @@ -382,7 +381,7 @@ " 'address': 'local:GillespieEvent',\n", " 'config': {'kdeg': 1.0, 'ktsc': 5.0},\n", " 'inputs': {'DNA': ['DNA_store'], 'mRNA': ['mRNA_store']},\n", - " 'instance': ,\n", + " 'instance': ,\n", " 'interval': 1.0,\n", " 'outputs': {'mRNA': ['mRNA_store']}}})" ] @@ -493,7 +492,7 @@ "\n" ], "text/plain": [ - "" + "" ] }, "execution_count": 13, @@ -510,7 +509,7 @@ "id": "c7687da5-6992-447b-a51b-14373984597f", "metadata": {}, "source": [ - "### add interval process" + "### add interval process to the config" ] }, { @@ -530,7 +529,6 @@ "source": [ "b['interval_process'].add_process(\n", " name='GillespieInterval',\n", - " # inputs={'port_id': ['store']} # we should be able to set the wires directly like this\n", ")" ] }, @@ -668,7 +666,7 @@ "\n" ], "text/plain": [ - "" + "" ] }, "execution_count": 15, @@ -695,10 +693,11 @@ "metadata": {}, "outputs": [], "source": [ + "# to connect a port in a more targeted way, use connect and specify the port and its target path\n", "b['interval_process'].connect(port='interval', target=['event_process', 'interval']) \n", "\n", "# the remaining ports can connect_all\n", - "b.connect_all()" + "b.connect_all() " ] }, { @@ -846,7 +845,7 @@ "\n" ], "text/plain": [ - "" + "" ] }, "execution_count": 17, @@ -863,7 +862,7 @@ "id": "ed3131ee-c607-42e5-88a3-930a84eedf0f", "metadata": {}, "source": [ - "## Generate composite" + "### check current Builder config" ] }, { @@ -880,7 +879,7 @@ " 'address': 'local:GillespieEvent',\n", " 'config': {'kdeg': 1.0, 'ktsc': 5.0},\n", " 'inputs': {'DNA': ['DNA_store'], 'mRNA': ['mRNA_store']},\n", - " 'instance': ,\n", + " 'instance': ,\n", " 'interval': 1.0,\n", " 'outputs': {'mRNA': ['mRNA_store']}},\n", " 'interval_process': { '_type': 'step',\n", @@ -888,7 +887,7 @@ " 'config': {'kdeg': 0.1, 'ktsc': 5.0},\n", " 'inputs': { 'DNA': ['DNA_store'],\n", " 'mRNA': ['mRNA_store']},\n", - " 'instance': ,\n", + " 'instance': ,\n", " 'outputs': {'interval': ['event_process', 'interval']}},\n", " 'mRNA_store': {}})" ] @@ -899,7 +898,6 @@ } ], "source": [ - "# check current Builder config\n", "b" ] }, @@ -908,7 +906,7 @@ "id": "9fd708e8-0688-4b97-a50c-e3d0a1c78d83", "metadata": {}, "source": [ - "### update the initial state" + "## Update the initial state" ] }, { @@ -926,6 +924,14 @@ "b.update(initial_state)" ] }, + { + "cell_type": "markdown", + "id": "305582d1-f701-4a55-88a3-f8ec21643033", + "metadata": {}, + "source": [ + "## Generate composite from builder config and simulate" + ] + }, { "cell_type": "code", "execution_count": 20, @@ -936,14 +942,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "produced interval: {'interval': 5.929431187150016}\n", - "received interval: 5.929431187150016\n", - "produced interval: {'interval': 6.845295472533056}\n" + "produced interval: {'interval': 1.21174311488629}\n", + "received interval: 1.21174311488629\n", + "produced interval: {'interval': 30.66671505248375}\n" ] } ], "source": [ - "# make composite from the current builder config, simulate\n", "composite = b.generate()\n", "composite.run(10)" ] @@ -957,7 +962,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 21, @@ -969,6 +974,14 @@ "composite" ] }, + { + "cell_type": "markdown", + "id": "743960b7-a6d3-41af-8737-2776a66c60cd", + "metadata": {}, + "source": [ + "## Retrieve the composite document" + ] + }, { "cell_type": "code", "execution_count": 22, @@ -1012,6 +1025,14 @@ "doc" ] }, + { + "cell_type": "markdown", + "id": "b32a13ed-134d-4de1-a1ed-b72807b4af45", + "metadata": {}, + "source": [ + "### save the document to file" + ] + }, { "cell_type": "code", "execution_count": 23, @@ -1039,9 +1060,8 @@ ] }, { - "cell_type": "code", - "execution_count": null, - "id": "c88f8059a315792e", + "cell_type": "markdown", + "id": "ebdd7599-0b0e-4f79-a8ab-d507a81646d8", "metadata": { "ExecuteTime": { "start_time": "2024-01-21T21:29:22.001943Z" @@ -1051,6 +1071,211 @@ "outputs_hidden": false } }, + "source": [ + "### load a composite from document\n", + "This document represents the full state of the composite, and so can reproduce the previous composite when loaded into a fresh Builder" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "56b14beb-27de-469d-b4e6-10fc628e15e0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Builder({ 'DNA_store': {'A gene': '2.0', 'B gene': '1.0'},\n", + " 'event_process': { '_type': 'process',\n", + " 'address': 'local:GillespieEvent',\n", + " 'config': {'kdeg': 1.0, 'ktsc': 5.0},\n", + " 'inputs': {'DNA': ['DNA_store'], 'mRNA': ['mRNA_store']},\n", + " 'instance': ,\n", + " 'interval': 1.0,\n", + " 'outputs': {'mRNA': ['mRNA_store']}},\n", + " 'global_time': '0.0',\n", + " 'interval_process': { '_type': 'step',\n", + " 'address': 'local:GillespieInterval',\n", + " 'config': {'kdeg': 0.1, 'ktsc': 5.0},\n", + " 'inputs': { 'DNA': ['DNA_store'],\n", + " 'mRNA': ['mRNA_store']},\n", + " 'instance': ,\n", + " 'outputs': {'interval': ['event_process', 'interval']}},\n", + " 'mRNA_store': {'A mRNA': '1.0', 'B mRNA': '1.0'}})" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b2 = Builder(core=core, file_path='out/toy_bigraph.json')\n", + "b2" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "6197f4c3-41ca-468b-929f-cc0ebc1fabe9", + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "bigraph\n", + "\n", + "\n", + "\n", + "('event_process', 'interval')\n", + "\n", + "interval\n", + "\n", + "\n", + "\n", + "('interval_process',)\n", + "\n", + "interval_process\n", + "\n", + "\n", + "\n", + "('event_process', 'interval')->('interval_process',)\n", + "\n", + "\n", + "interval\n", + "\n", + "\n", + "\n", + "('mRNA_store',)\n", + "\n", + "mRNA_store\n", + "\n", + "\n", + "\n", + "('mRNA_store', 'A mRNA')\n", + "\n", + "A mRNA\n", + "\n", + "\n", + "\n", + "('mRNA_store',)->('mRNA_store', 'A mRNA')\n", + "\n", + "\n", + "\n", + "\n", + "('mRNA_store', 'B mRNA')\n", + "\n", + "B mRNA\n", + "\n", + "\n", + "\n", + "('mRNA_store',)->('mRNA_store', 'B mRNA')\n", + "\n", + "\n", + "\n", + "\n", + "('event_process',)\n", + "\n", + "event_process\n", + "\n", + "\n", + "\n", + "('mRNA_store',)->('event_process',)\n", + "\n", + "\n", + "mRNA\n", + "\n", + "\n", + "\n", + "('mRNA_store',)->('event_process',)\n", + "\n", + "\n", + "mRNA\n", + "\n", + "\n", + "\n", + "('mRNA_store',)->('interval_process',)\n", + "\n", + "\n", + "mRNA\n", + "\n", + "\n", + "\n", + "('DNA_store',)\n", + "\n", + "DNA_store\n", + "\n", + "\n", + "\n", + "('DNA_store', 'A gene')\n", + "\n", + "A gene\n", + "\n", + "\n", + "\n", + "('DNA_store',)->('DNA_store', 'A gene')\n", + "\n", + "\n", + "\n", + "\n", + "('DNA_store', 'B gene')\n", + "\n", + "B gene\n", + "\n", + "\n", + "\n", + "('DNA_store',)->('DNA_store', 'B gene')\n", + "\n", + "\n", + "\n", + "\n", + "('DNA_store',)->('event_process',)\n", + "\n", + "\n", + "DNA\n", + "\n", + "\n", + "\n", + "('DNA_store',)->('interval_process',)\n", + "\n", + "\n", + "DNA\n", + "\n", + "\n", + "\n", + "('event_process',)->('event_process', 'interval')\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b2.visualize()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c11fc1f-23ee-45e5-9975-f3d3b2e1dff9", + "metadata": {}, "outputs": [], "source": [] }