Skip to content

Commit cd022f5

Browse files
authored
Add result/output node (#96)
* Add result/output node * rename function parameter * update documentation * set result node * fix black * fix plot * fix node id
1 parent 10d9f7f commit cd022f5

File tree

12 files changed

+109
-37
lines changed

12 files changed

+109
-37
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,28 @@ def get_prod_and_div(x: float, y: float) -> dict:
3434
```
3535
These two Python functions are combined in the following example workflow:
3636
```python
37-
tmp_dict = get_prod_and_div(x=1, y=2)
38-
result = get_sum(x=tmp_dict["prod"], y=tmp_dict["div"])
37+
def combined_workflow(x=1, y=2):
38+
tmp_dict = get_prod_and_div(x=x, y=y)
39+
return get_sum(x=tmp_dict["prod"], y=tmp_dict["div"])
3940
```
4041
For the workflow representation of these Python functions the Python functions are stored in the [example_workflows/arithmetic/workflow.py](example_workflows/arithmetic/workflow.py)
4142
Python module. The connection of the Python functions are stored in the [example_workflows/arithmetic/workflow.json](example_workflows/arithmetic/workflow.json)
4243
JSON file:
4344
```
4445
{
4546
"nodes": [
46-
{"id": 0, "function": "simple_workflow.get_prod_and_div"},
47-
{"id": 1, "function": "simple_workflow.get_sum"},
48-
{"id": 2, "value": 1},
49-
{"id": 3, "value": 2}
47+
{"id": 0, "type": "function", "value": "workflow.get_prod_and_div"},
48+
{"id": 1, "type": "function", "value": "workflow.get_sum"},
49+
{"id": 2, "type": "input", "value": 1, "name": "x"},
50+
{"id": 3, "type": "input", "value": 2, "name": "y"},
51+
{"id": 4, "type": "output", "name": "result"}
5052
],
5153
"edges": [
5254
{"target": 0, "targetPort": "x", "source": 2, "sourcePort": null},
5355
{"target": 0, "targetPort": "y", "source": 3, "sourcePort": null},
5456
{"target": 1, "targetPort": "x", "source": 0, "sourcePort": "prod"},
55-
{"target": 1, "targetPort": "y", "source": 0, "sourcePort": "div"}
57+
{"target": 1, "targetPort": "y", "source": 0, "sourcePort": "div"},
58+
{"target": 4, "targetPort": null, "source": 1, "sourcePort": null}
5659
]
5760
}
5861
```

documentation/arithmetic.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,28 @@ def get_prod_and_div(x: float, y: float) -> dict:
99
```
1010
These two Python functions are combined in the following example workflow:
1111
```python
12-
tmp_dict = get_prod_and_div(x=1, y=2)
13-
result = get_sum(x=tmp_dict["prod"], y=tmp_dict["div"])
12+
def combined_workflow(x=1, y=2):
13+
tmp_dict = get_prod_and_div(x=x, y=y)
14+
return get_sum(x=tmp_dict["prod"], y=tmp_dict["div"])
1415
```
1516
For the workflow representation of these Python functions the Python functions are stored in the [workflow.py](example_workflows/arithmetic/workflow.py)
1617
Python module. The connection of the Python functions are stored in the [workflow.json](example_workflows/arithmetic/workflow.json)
1718
JSON file:
1819
```
1920
{
2021
"nodes": [
21-
{"id": 0, "function": "workflow.get_prod_and_div"},
22-
{"id": 1, "function": "workflow.get_sum"},
23-
{"id": 2, "value": 1},
24-
{"id": 3, "value": 2}
22+
{"id": 0, "type": "function", "value": "workflow.get_prod_and_div"},
23+
{"id": 1, "type": "function", "value": "workflow.get_sum"},
24+
{"id": 2, "type": "input", "value": 1, "name": "x"},
25+
{"id": 3, "type": "input", "value": 2, "name": "y"},
26+
{"id": 4, "type": "output", "name": "result"}
2527
],
2628
"edges": [
2729
{"target": 0, "targetPort": "x", "source": 2, "sourcePort": null},
2830
{"target": 0, "targetPort": "y", "source": 3, "sourcePort": null},
2931
{"target": 1, "targetPort": "x", "source": 0, "sourcePort": "prod"},
30-
{"target": 1, "targetPort": "y", "source": 0, "sourcePort": "div"}
32+
{"target": 1, "targetPort": "y", "source": 0, "sourcePort": "div"},
33+
{"target": 4, "targetPort": null, "source": 1, "sourcePort": null}
3134
]
3235
}
3336
```

example_workflows/arithmetic/workflow.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
{"id": 0, "type": "function", "value": "workflow.get_prod_and_div"},
44
{"id": 1, "type": "function", "value": "workflow.get_sum"},
55
{"id": 2, "type": "input", "value": 1, "name": "x"},
6-
{"id": 3, "type": "input", "value": 2, "name": "y"}
6+
{"id": 3, "type": "input", "value": 2, "name": "y"},
7+
{"id": 4, "type": "output", "name": "result"}
78
],
89
"edges": [
910
{"target": 0, "targetPort": "x", "source": 2, "sourcePort": null},
1011
{"target": 0, "targetPort": "y", "source": 3, "sourcePort": null},
1112
{"target": 1, "targetPort": "x", "source": 0, "sourcePort": "prod"},
12-
{"target": 1, "targetPort": "y", "source": 0, "sourcePort": "div"}
13+
{"target": 1, "targetPort": "y", "source": 0, "sourcePort": "div"},
14+
{"target": 4, "targetPort": null, "source": 1, "sourcePort": null}
1315
]
1416
}

example_workflows/nfdi/workflow.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
{"id": 3, "type": "function", "value": "workflow.plot_over_line"},
77
{"id": 4, "type": "function", "value": "workflow.substitute_macros"},
88
{"id": 5, "type": "function", "value": "workflow.compile_paper"},
9-
{"id": 6, "type": "input", "value": 2.0, "name": "domain_size"}
9+
{"id": 6, "type": "input", "value": 2.0, "name": "domain_size"},
10+
{"id": 7, "type": "output", "name": "result"}
1011
],
1112
"edges": [
1213
{"target": 0, "targetPort": "domain_size", "source": 6, "sourcePort": null},
@@ -19,6 +20,7 @@
1920
{"target": 4, "targetPort": "ndofs", "source": 2, "sourcePort": "numdofs"},
2021
{"target": 4, "targetPort": "domain_size", "source": 6, "sourcePort": null},
2122
{"target": 5, "targetPort": "macros_tex", "source": 4, "sourcePort": null},
22-
{"target": 5, "targetPort": "plot_file", "source": 3, "sourcePort": null}
23+
{"target": 5, "targetPort": "plot_file", "source": 3, "sourcePort": null},
24+
{"target": 7, "targetPort": null, "source": 5, "sourcePort": null}
2325
]
2426
}

example_workflows/quantum_espresso/workflow.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
{"id": 28, "type": "input", "value": "strain_4", "name": "working_directory_5"},
3232
{"id": 29, "type": "function", "value": "python_workflow_definition.shared.get_dict"},
3333
{"id": 30, "type": "function", "value": "python_workflow_definition.shared.get_list"},
34-
{"id": 31, "type": "function", "value": "python_workflow_definition.shared.get_list"}
34+
{"id": 31, "type": "function", "value": "python_workflow_definition.shared.get_list"},
35+
{"id": 32, "type": "output", "name": "result"}
3536
],
3637
"edges": [
3738
{"target": 0, "targetPort": "element", "source": 9, "sourcePort": null},
@@ -92,6 +93,7 @@
9293
{"target": 31, "targetPort": "2", "source": 5, "sourcePort": "energy"},
9394
{"target": 31, "targetPort": "3", "source": 6, "sourcePort": "energy"},
9495
{"target": 31, "targetPort": "4", "source": 7, "sourcePort": "energy"},
95-
{"target": 8, "targetPort": "energy_lst", "source": 31, "sourcePort": null}
96+
{"target": 8, "targetPort": "energy_lst", "source": 31, "sourcePort": null},
97+
{"target": 32, "targetPort": null, "source": 8, "sourcePort": null}
9698
]
9799
}

python_workflow_definition/src/python_workflow_definition/aiida.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from python_workflow_definition.shared import (
1111
convert_nodes_list_to_dict,
1212
update_node_names,
13+
remove_result,
14+
set_result_node,
1315
NODES_LABEL,
1416
EDGES_LABEL,
1517
SOURCE_LABEL,
@@ -21,7 +23,7 @@
2123

2224
def load_workflow_json(file_name: str) -> WorkGraph:
2325
with open(file_name) as f:
24-
data = json.load(f)
26+
data = remove_result(workflow_dict=json.load(f))
2527

2628
wg = WorkGraph()
2729
task_name_mapping = {}
@@ -136,6 +138,10 @@ def write_workflow_json(wg: WorkGraph, file_name: str) -> dict:
136138
)
137139
with open(file_name, "w") as f:
138140
# json.dump({"nodes": data[], "edges": edges_new_lst}, f)
139-
json.dump(update_node_names(content=data), f, indent=2)
141+
json.dump(
142+
set_result_node(workflow_dict=update_node_names(workflow_dict=data)),
143+
f,
144+
indent=2,
145+
)
140146

141147
return data

python_workflow_definition/src/python_workflow_definition/executorlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
get_kwargs,
1111
get_source_handles,
1212
convert_nodes_list_to_dict,
13+
remove_result,
1314
NODES_LABEL,
1415
EDGES_LABEL,
1516
SOURCE_LABEL,
@@ -38,7 +39,7 @@ def _get_value(result_dict: dict, nodes_new_dict: dict, link_dict: dict, exe: Ex
3839

3940
def load_workflow_json(file_name: str, exe: Executor):
4041
with open(file_name, "r") as f:
41-
content = json.load(f)
42+
content = remove_result(workflow_dict=json.load(f))
4243

4344
edges_new_lst = content[EDGES_LABEL]
4445
nodes_new_dict = {}

python_workflow_definition/src/python_workflow_definition/jobflow.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
get_source_handles,
1313
update_node_names,
1414
convert_nodes_list_to_dict,
15+
remove_result,
16+
set_result_node,
1517
NODES_LABEL,
1618
EDGES_LABEL,
1719
SOURCE_LABEL,
@@ -271,7 +273,7 @@ def _get_item_from_tuple(input_obj, index, index_lst):
271273

272274
def load_workflow_json(file_name: str) -> Flow:
273275
with open(file_name, "r") as f:
274-
content = json.load(f)
276+
content = remove_result(workflow_dict=json.load(f))
275277

276278
edges_new_lst = []
277279
for edge in content[EDGES_LABEL]:
@@ -332,8 +334,10 @@ def write_workflow_json(flow: Flow, file_name: str = "workflow.json"):
332334

333335
with open(file_name, "w") as f:
334336
json.dump(
335-
update_node_names(
336-
content={NODES_LABEL: nodes_store_lst, EDGES_LABEL: edges_lst}
337+
set_result_node(
338+
workflow_dict=update_node_names(
339+
workflow_dict={NODES_LABEL: nodes_store_lst, EDGES_LABEL: edges_lst}
340+
)
337341
),
338342
f,
339343
indent=2,

python_workflow_definition/src/python_workflow_definition/plot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def plot(file_name: str):
3939
k + "=result[" + v[SOURCE_PORT_LABEL] + "]"
4040
)
4141
for k, v in edge_label_dict.items():
42-
graph.add_edge(str(k), str(target_node), label=", ".join(v))
42+
if len(v) == 1 and v[0] is not None:
43+
graph.add_edge(str(k), str(target_node), label=", ".join(v))
44+
else:
45+
graph.add_edge(str(k), str(target_node))
4346

4447
svg = nx.nx_agraph.to_agraph(graph).draw(prog="dot", format="svg")
4548
display(SVG(svg))

python_workflow_definition/src/python_workflow_definition/purepython.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
get_kwargs,
1010
get_source_handles,
1111
convert_nodes_list_to_dict,
12+
remove_result,
1213
NODES_LABEL,
1314
EDGES_LABEL,
1415
SOURCE_LABEL,
@@ -67,7 +68,7 @@ def _get_value(result_dict: dict, nodes_new_dict: dict, link_dict: dict):
6768

6869
def load_workflow_json(file_name: str):
6970
with open(file_name, "r") as f:
70-
content = json.load(f)
71+
content = remove_result(workflow_dict=json.load(f))
7172

7273
edges_new_lst = content[EDGES_LABEL]
7374
nodes_new_dict = {}

0 commit comments

Comments
 (0)