Skip to content

Commit

Permalink
show ports
Browse files Browse the repository at this point in the history
  • Loading branch information
eagmon committed Jan 4, 2024
1 parent d747a53 commit c8ecf01
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 52 deletions.
76 changes: 52 additions & 24 deletions builder/builder_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ def dict_from_builder_tree(builder_tree):
return tree


def get_process_ports(value, schema):
ports = {}
if value.get('_type') in EDGE_KEYS:
ports['_inputs'] = schema.get('_inputs', {})
ports['_outputs'] = schema.get('_outputs', {})
return ports


def fill_process_ports(tree, schema):
new_tree = tree.copy()
for key, value in tree.items():
if value.get('_type') in EDGE_KEYS:
if '_ports' not in new_tree[key]:
new_tree[key]['_ports'] = {}
new_tree[key]['_ports'].update(schema[key].get('_inputs', {}))
new_tree[key]['_ports'].update(schema[key].get('_outputs', {}))
elif isinstance(value, dict) and key in schema:
new_tree[key] = fill_process_ports(value, schema[key])
return new_tree


class Builder:

def __init__(self, tree=None, schema=None, process_registry=None):
Expand Down Expand Up @@ -75,6 +96,7 @@ def __setitem__(self, keys, value):
else:
self.tree[first_key] = value

# reset compiled composite
self.compiled_composite = None

def __getitem__(self, keys):
Expand All @@ -91,17 +113,6 @@ def __getitem__(self, keys):
else:
return self.tree[first_key]

# # TODO: reach through a port
# if i < len(keys) - 1 and d.get('_type') in EDGE_KEYS:
# # The current item is a process, and there's another key after this
# next_key = keys[i + 1]
# # Check if next_key is a valid port
# if 'ports' not in d or next_key not in d['ports']:
# raise ValueError(f"Port '{next_key}' not found in process '{key}'.")
# d = d[key]
#
# return d

def process_registry_list(self):
return self.process_registry.list()

Expand Down Expand Up @@ -149,27 +160,19 @@ def add_process(

self.tree = builder_tree_from_dict(state)
self.schema = schema
self.compiled_composite = None

def ports(self):
if self.tree['_type'] not in EDGE_KEYS:
warnings.warn(f"Expected '_type' to be in {EDGE_KEYS}, found '{self.tree['_type']}' instead.",
RuntimeWarning)

if not self.compiled_composite:
self.compile()
# warnings.warn("ports requires compile", RuntimeWarning)

# TODO get the ports
# reset compiled composite
self.compiled_composite = None

def connect(self, port=None, target=None):
assert self.schema['_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']:
self.tree['outputs'][port] = target

# reset compiled composite
self.compiled_composite = None

def document(self):
Expand Down Expand Up @@ -204,12 +207,28 @@ def run(self, interval):
self.compile()
self.compiled_composite.run(interval)

def ports(self):
self.compile()
tree_dict = dict_from_builder_tree(self.tree)
tree_type = tree_dict.get('_type')
if not tree_type:
warnings.warn(f"no type provided.")
elif tree_type not in EDGE_KEYS:
warnings.warn(f"Expected '_type' to be in {EDGE_KEYS}, found '{tree_type}' instead.")
elif tree_type:
return get_process_ports(tree_dict, self.schema)

def plot(self, filename=None, out_dir=None, **kwargs):
if filename and not out_dir:
out_dir = 'out'
if not self.compiled_composite:
self.compile()

tree_dict = dict_from_builder_tree(self.tree)
tree_dict = fill_process_ports(tree_dict, self.schema)

return plot_bigraph(
dict_from_builder_tree(self.tree),
tree_dict,
out_dir=out_dir,
filename=filename,
show_process_schema=False,
Expand Down Expand Up @@ -280,6 +299,15 @@ def update(self, state, interval):

b['toy'].add_process(name='toy')

# b.tree
ports = b['toy'].ports()
print(ports)

b.compile()
ports = b['toy'].ports()
print(ports)

b.plot()


if __name__ == '__main__':
Expand Down
81 changes: 53 additions & 28 deletions notebooks/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"id": "b385dca6-942d-472c-9963-13b8cb33843c",
"metadata": {
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.542271Z",
"start_time": "2024-01-04T16:15:56.931758Z"
"end_time": "2024-01-04T17:23:41.275302Z",
"start_time": "2024-01-04T17:23:40.772396Z"
}
},
"outputs": [],
Expand All @@ -22,8 +22,8 @@
"id": "30297af1-2d65-43d9-9ab7-0589d94a8cfe",
"metadata": {
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.544976Z",
"start_time": "2024-01-04T16:15:57.543301Z"
"end_time": "2024-01-04T17:23:41.279284Z",
"start_time": "2024-01-04T17:23:41.277066Z"
}
},
"outputs": [],
Expand All @@ -37,7 +37,7 @@
"outputs": [
{
"data": {
"text/plain": "['smoldyn_process',\n 'tellurium_step',\n 'tellurium_process',\n 'database-emitter',\n 'cobra',\n 'copasi',\n 'ram-emitter',\n 'console-emitter']"
"text/plain": "['copasi',\n 'console-emitter',\n 'tellurium_process',\n 'cobra',\n 'ram-emitter',\n 'tellurium_step',\n 'database-emitter',\n 'smoldyn_process']"
},
"execution_count": 3,
"metadata": {},
Expand All @@ -50,8 +50,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.550546Z",
"start_time": "2024-01-04T16:15:57.547382Z"
"end_time": "2024-01-04T17:23:41.285807Z",
"start_time": "2024-01-04T17:23:41.278653Z"
}
},
"id": "979e0bd85a31d4b0"
Expand All @@ -66,8 +66,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.552690Z",
"start_time": "2024-01-04T16:15:57.550090Z"
"end_time": "2024-01-04T17:23:41.287886Z",
"start_time": "2024-01-04T17:23:41.285613Z"
}
},
"id": "a84d340622208914"
Expand Down Expand Up @@ -106,8 +106,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.556096Z",
"start_time": "2024-01-04T16:15:57.554555Z"
"end_time": "2024-01-04T17:23:41.292263Z",
"start_time": "2024-01-04T17:23:41.290397Z"
}
},
"id": "1ee84904801a9ab"
Expand All @@ -118,7 +118,7 @@
"outputs": [
{
"data": {
"text/plain": "['toy',\n 'smoldyn_process',\n 'tellurium_step',\n 'tellurium_process',\n 'database-emitter',\n 'cobra',\n 'copasi',\n 'ram-emitter',\n 'console-emitter']"
"text/plain": "['copasi',\n 'console-emitter',\n 'tellurium_process',\n 'cobra',\n 'toy',\n 'ram-emitter',\n 'tellurium_step',\n 'database-emitter',\n 'smoldyn_process']"
},
"execution_count": 6,
"metadata": {},
Expand All @@ -131,8 +131,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.560731Z",
"start_time": "2024-01-04T16:15:57.557296Z"
"end_time": "2024-01-04T17:23:41.296560Z",
"start_time": "2024-01-04T17:23:41.293008Z"
}
},
"id": "dae8ca8db70c4845"
Expand All @@ -147,38 +147,63 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.563116Z",
"start_time": "2024-01-04T16:15:57.560418Z"
"end_time": "2024-01-04T17:23:41.298756Z",
"start_time": "2024-01-04T17:23:41.296476Z"
}
},
"id": "8b1f49a78de17642"
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"data": {
"text/plain": "{'_inputs': {'A': 'float', 'B': 'float'}, '_outputs': {'C': 'float'}}"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b['toy'].ports()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-04T17:23:41.319178Z",
"start_time": "2024-01-04T17:23:41.299134Z"
}
},
"id": "329e64816e8ff626"
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [],
"source": [
"b.compile()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.581157Z",
"start_time": "2024-01-04T16:15:57.563243Z"
"end_time": "2024-01-04T17:23:41.319385Z",
"start_time": "2024-01-04T17:23:41.303020Z"
}
},
"id": "b217970ad04b9aeb"
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"outputs": [
{
"data": {
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 8.1.0 (20230707.0739)\n -->\n<!-- Title: bigraph Pages: 1 -->\n<svg width=\"60pt\" height=\"43pt\"\n viewBox=\"0.00 0.00 62.00 44.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(0.972222 0.972222) rotate(0) translate(4 40)\">\n<title>bigraph</title>\n<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-40 58,-40 58,4 -4,4\"/>\n<!-- (&#39;toy&#39;,) -->\n<g id=\"node1\" class=\"node\">\n<title>(&#39;toy&#39;,)</title>\n<polygon fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"54,-36 0,-36 0,0 54,0 54,-36\"/>\n<text text-anchor=\"start\" x=\"19.5\" y=\"-13.72\" font-family=\"Times,serif\" font-size=\"12.00\">toy</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<graphviz.graphs.Digraph at 0x11757ae80>"
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 8.1.0 (20230707.0739)\n -->\n<!-- Title: bigraph Pages: 1 -->\n<svg width=\"68pt\" height=\"124pt\"\n viewBox=\"0.00 0.00 69.72 127.25\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(0.972222 0.972222) rotate(0) translate(4 123.25)\">\n<title>bigraph</title>\n<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-123.25 65.72,-123.25 65.72,4 -4,4\"/>\n<!-- (&#39;toy&#39;,) -->\n<g id=\"node1\" class=\"node\">\n<title>(&#39;toy&#39;,)</title>\n<polygon fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"58.36,-36 4.36,-36 4.36,0 58.36,0 58.36,-36\"/>\n<text text-anchor=\"start\" x=\"23.86\" y=\"-13.72\" font-family=\"Times,serif\" font-size=\"12.00\">toy</text>\n</g>\n<!-- (&#39;toy&#39;, &#39;A&#39;) -->\n<!-- (&#39;toy&#39;, &#39;A&#39;)&#45;&gt;(&#39;toy&#39;,) -->\n<g id=\"edge1\" class=\"edge\">\n<title>(&#39;toy&#39;, &#39;A&#39;)&#45;&gt;(&#39;toy&#39;,)</title>\n<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M1.69,-96.76C5.29,-87.33 15.44,-60.73 22.86,-41.27\"/>\n<ellipse fill=\"black\" stroke=\"black\" cx=\"23.61\" cy=\"-39.32\" rx=\"2\" ry=\"2\"/>\n<text text-anchor=\"start\" x=\"18.36\" y=\"-55.75\" font-family=\"Times,serif\" font-size=\"10.00\">A</text>\n</g>\n<!-- (&#39;toy&#39;, &#39;B&#39;) -->\n<!-- (&#39;toy&#39;, &#39;B&#39;)&#45;&gt;(&#39;toy&#39;,) -->\n<g id=\"edge2\" class=\"edge\">\n<title>(&#39;toy&#39;, &#39;B&#39;)&#45;&gt;(&#39;toy&#39;,)</title>\n<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M31.36,-82.62C31.36,-70.67 31.36,-54.76 31.36,-41.85\"/>\n<ellipse fill=\"black\" stroke=\"black\" cx=\"31.36\" cy=\"-39.38\" rx=\"2\" ry=\"2\"/>\n<text text-anchor=\"start\" x=\"31.36\" y=\"-55.75\" font-family=\"Times,serif\" font-size=\"10.00\">B</text>\n</g>\n<!-- (&#39;toy&#39;, &#39;C&#39;) -->\n<!-- (&#39;toy&#39;, &#39;C&#39;)&#45;&gt;(&#39;toy&#39;,) -->\n<g id=\"edge3\" class=\"edge\">\n<title>(&#39;toy&#39;, &#39;C&#39;)&#45;&gt;(&#39;toy&#39;,)</title>\n<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M60.07,-96.76C56.59,-87.33 46.77,-60.73 39.58,-41.27\"/>\n<ellipse fill=\"black\" stroke=\"black\" cx=\"38.87\" cy=\"-39.33\" rx=\"2\" ry=\"2\"/>\n<text text-anchor=\"start\" x=\"47.36\" y=\"-55.75\" font-family=\"Times,serif\" font-size=\"10.00\">C</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<graphviz.graphs.Digraph at 0x11cc49fa0>"
},
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -189,25 +214,25 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.623412Z",
"start_time": "2024-01-04T16:15:57.566477Z"
"end_time": "2024-01-04T17:23:41.374092Z",
"start_time": "2024-01-04T17:23:41.306551Z"
}
},
"id": "3b1dd8569f3bb67a"
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-04T16:15:57.625237Z",
"start_time": "2024-01-04T16:15:57.623271Z"
"end_time": "2024-01-04T17:23:41.380858Z",
"start_time": "2024-01-04T17:23:41.378630Z"
}
},
"id": "dbef2d9d0f24898"
"id": "f02c15797bdb73b8"
}
],
"metadata": {
Expand Down

0 comments on commit c8ecf01

Please sign in to comment.