From f5454968e7306424e8044953737243974a32cab3 Mon Sep 17 00:00:00 2001 From: Eran Date: Wed, 14 Feb 2024 10:58:50 -0500 Subject: [PATCH] working on navigating schema --- builder/builder_api.py | 21 ++--- notebooks/demo.ipynb | 178 ++++++++++++++++++++++++++++++++++------- 2 files changed, 155 insertions(+), 44 deletions(-) diff --git a/builder/builder_api.py b/builder/builder_api.py index 3fafa85..060c99e 100644 --- a/builder/builder_api.py +++ b/builder/builder_api.py @@ -11,7 +11,6 @@ from pprint import pformat as pf import warnings -from bigraph_schema.type_system import Edge from process_bigraph import Process, Step, Composite, ProcessTypes from bigraph_schema.protocols import local_lookup_module from bigraph_viz import plot_bigraph @@ -92,8 +91,8 @@ def register_process(self, process_name, address): self.core.process_registry.register(process_name, address) # Check if address is a string elif isinstance(address, str): - # separate out the protocol from the address try: + # separate out the protocol from the address protocol, addr = address.split(':', 1) assert protocol == 'local', 'BigraphBuilder only supports local protocol in the current version' @@ -106,7 +105,6 @@ def register_process(self, process_name, address): Exception('only support local addresses') except ValueError: - # Handle cases where the string does not conform to "protocol:address" Exception(f"Address '{address}' does not contain a protocol. Registration failed.") else: # Handle other types if necessary @@ -128,13 +126,13 @@ def __setitem__(self, keys, value): first_key = keys[0] if first_key not in self.tree: - self.tree[first_key] = Builder(core=self.core) + self.tree[first_key] = Builder(core=self.core, schema=self.schema.get(first_key, {})) remaining = keys[1:] if len(remaining) > 0: self.tree[first_key].__setitem__(remaining, value) elif isinstance(value, dict): - self.tree[first_key] = Builder(tree=value, core=self.core) + self.tree[first_key] = Builder(tree=value, core=self.core, schema=self.schema.get(first_key, {})) else: self.tree[first_key] = value @@ -147,7 +145,7 @@ def __getitem__(self, keys): first_key = keys[0] if first_key not in self.tree: - self.tree[first_key] = Builder(parent=self, core=self.core) + self.tree[first_key] = Builder(parent=self, core=self.core, schema=self.schema.get(first_key, {})) remaining = keys[1:] if len(remaining) > 0: @@ -159,10 +157,7 @@ def list_types(self): return self.core.type_registry.list() def list_processes(self): - types = self.core.process_registry.list() - processes = [ - types for type in types if type.get('_type') in EDGE_KEYS] - return processes + print(self.core.process_registry.list()) # def register(self, name, process, force=False): # if not self.core.access(name) and isinstance(process, Edge): @@ -199,13 +194,13 @@ def add_process( schema, state = self.core.complete(initial_schema, initial_state) self.tree = builder_tree_from_dict(state) - self.schema = schema + self.schema = schema or {} # reset compiled composite self.compile() def connect(self, port=None, target=None): - assert self.schema.get('_type') in EDGE_KEYS, f"Invalid type for connect: {self.schema}, needs to be in {EDGE_KEYS}" + # assert self.schema.get('_type', {}) in EDGE_KEYS, f"Invalid type for connect: {self.schema}, needs to be in {EDGE_KEYS}" if port in self.schema['_inputs']: self.tree['inputs'][port] = target if port in self.schema['_outputs']: @@ -250,7 +245,7 @@ def compile(self): return self.compiled_composite def update_tree(self, schema=None, state=None): - self.schema = schema + self.schema = schema or self.schema state = state or {} for k, i in state.items(): if isinstance(i, dict): diff --git a/notebooks/demo.ipynb b/notebooks/demo.ipynb index cbe6037..1a1966f 100644 --- a/notebooks/demo.ipynb +++ b/notebooks/demo.ipynb @@ -10,22 +10,9 @@ "start_time": "2024-01-21T21:29:13.531449Z" } }, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'builder'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbuilder\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Builder, Process\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbiosimulator_processes\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m process_registry\n", - "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'builder'" - ] - } - ], + "outputs": [], "source": [ - "from builder import Builder, Process\n", - "from biosimulator_processes import process_registry" + "from builder import Builder, Process" ] }, { @@ -40,7 +27,7 @@ }, "outputs": [], "source": [ - "b = Builder(process_registry=process_registry)" + "b = Builder()" ] }, { @@ -59,24 +46,141 @@ }, "outputs": [ { - "ename": "AttributeError", - "evalue": "'Builder' object has no attribute 'process_registry_list'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mb\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprocess_registry_list\u001b[49m()\n", - "\u001b[0;31mAttributeError\u001b[0m: 'Builder' object has no attribute 'process_registry_list'" + "name": "stdout", + "output_type": "stream", + "text": [ + "['ram-emitter', 'console-emitter']\n" ] } ], "source": [ - "b.process_registry_list()" + "b.list_processes()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "5bff2ea4-42f9-430e-b1cc-f0581510ec8d", + "metadata": {}, + "outputs": [], + "source": [ + "from process_bigraph.experiments.minimal_gillespie import GillespieEvent , GillespieInterval\n", + "b.register_process('GillespieEvent', GillespieEvent)\n", + "b.register_process('GillespieInterval', GillespieInterval)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "40cde984-cfdf-4571-af9c-3a3755e3763c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['GillespieEvent', 'ram-emitter', 'GillespieInterval', 'console-emitter']\n" + ] + } + ], + "source": [ + "b.list_processes()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "503863ee-0a58-44b3-accc-20e0167b947b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unexpected exception formatting exception. Falling back to standard exception\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Traceback (most recent call last):\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py\", line 3550, in run_code\n", + " exec(code_obj, self.user_global_ns, self.user_ns)\n", + " File \"/var/folders/vy/vr0_ytms6m95qrnk7xnh0bth0000gq/T/ipykernel_53425/3145499072.py\", line 2, in \n", + " b['event_process'].add_process(\n", + " File \"/Users/eranagmon/code/bigraph-builder/builder/builder_api.py\", line 201, in add_process\n", + " File \"/Users/eranagmon/code/bigraph-builder/builder/builder_api.py\", line 235, in compile\n", + " else:\n", + " File \"/Users/eranagmon/code/bigraph-builder/builder/builder_api.py\", line 241, in compile\n", + " File \"/Users/eranagmon/code/process-bigraph/process_bigraph/composite.py\", line 731, in __init__\n", + " super().__init__(config, core)\n", + " File \"/Users/eranagmon/code/process-bigraph/process_bigraph/composite.py\", line 440, in __init__\n", + " self.config = self.core.fill(\n", + "AttributeError: 'NoneType' object has no attribute 'fill'\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py\", line 2144, in showtraceback\n", + " stb = self.InteractiveTB.structured_traceback(\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 1435, in structured_traceback\n", + " return FormattedTB.structured_traceback(\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 1326, in structured_traceback\n", + " return VerboseTB.structured_traceback(\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 1173, in structured_traceback\n", + " formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 1088, in format_exception_as_a_whole\n", + " frames.append(self.format_record(record))\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 970, in format_record\n", + " frame_info.lines, Colors, self.has_colors, lvals\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 792, in lines\n", + " return self._sd.lines\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/stack_data/utils.py\", line 145, in cached_property_wrapper\n", + " value = obj.__dict__[self.func.__name__] = self.func(obj)\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/stack_data/core.py\", line 734, in lines\n", + " pieces = self.included_pieces\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/stack_data/utils.py\", line 145, in cached_property_wrapper\n", + " value = obj.__dict__[self.func.__name__] = self.func(obj)\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/stack_data/core.py\", line 681, in included_pieces\n", + " pos = scope_pieces.index(self.executing_piece)\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/stack_data/utils.py\", line 145, in cached_property_wrapper\n", + " value = obj.__dict__[self.func.__name__] = self.func(obj)\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/stack_data/core.py\", line 660, in executing_piece\n", + " return only(\n", + " File \"/Users/eranagmon/code/bigraph-builder/venv/lib/python3.9/site-packages/executing/executing.py\", line 116, in only\n", + " raise NotOneValueFound('Expected one value, found 0')\n", + "executing.executing.NotOneValueFound: Expected one value, found 0\n" + ] + } + ], + "source": [ + "## add processes\n", + "b['event_process'].add_process(\n", + " name='GillespieEvent',\n", + " kdeg=1.0, # kwargs fill parameters in the config\n", + ")" ] }, { "cell_type": "code", "execution_count": null, + "id": "2f1f606b-160c-4158-9a54-54dba22f0732", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a93508ae-95d3-4a7f-ae18-54c17e6f409e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 6, "id": "a84d340622208914", "metadata": { "ExecuteTime": { @@ -95,7 +199,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "235efcd79c0f13c1", "metadata": { "ExecuteTime": { @@ -113,7 +217,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "1ee84904801a9ab", "metadata": { "ExecuteTime": { @@ -155,7 +259,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "dae8ca8db70c4845", "metadata": { "ExecuteTime": { @@ -166,7 +270,19 @@ "outputs_hidden": false } }, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'Builder' object has no attribute 'process_registry_list'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[9], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mb\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprocess_registry_list\u001b[49m()\n", + "\u001b[0;31mAttributeError\u001b[0m: 'Builder' object has no attribute 'process_registry_list'" + ] + } + ], "source": [ "b.process_registry_list()" ] @@ -319,9 +435,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python (venv)", + "display_name": "venv", "language": "python", - "name": "venv_kernel" + "name": "venv" }, "language_info": { "codemirror_mode": {