Skip to content

Commit 76dced1

Browse files
committed
Add result/output node
1 parent 10d9f7f commit 76dced1

File tree

9 files changed

+64
-16
lines changed

9 files changed

+64
-16
lines changed

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": 4, "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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from python_workflow_definition.shared import (
1111
convert_nodes_list_to_dict,
1212
update_node_names,
13+
remove_result,
1314
NODES_LABEL,
1415
EDGES_LABEL,
1516
SOURCE_LABEL,
@@ -21,7 +22,7 @@
2122

2223
def load_workflow_json(file_name: str) -> WorkGraph:
2324
with open(file_name) as f:
24-
data = json.load(f)
25+
data = remove_result(workflow_dict=json.load(f))
2526

2627
wg = WorkGraph()
2728
task_name_mapping = {}

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
get_source_handles,
1313
update_node_names,
1414
convert_nodes_list_to_dict,
15+
remove_result,
1516
NODES_LABEL,
1617
EDGES_LABEL,
1718
SOURCE_LABEL,
@@ -271,7 +272,7 @@ def _get_item_from_tuple(input_obj, index, index_lst):
271272

272273
def load_workflow_json(file_name: str) -> Flow:
273274
with open(file_name, "r") as f:
274-
content = json.load(f)
275+
content = remove_result(workflow_dict=json.load(f))
275276

276277
edges_new_lst = []
277278
for edge in content[EDGES_LABEL]:

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 = {}

python_workflow_definition/src/python_workflow_definition/pyiron_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
get_source_handles,
1313
convert_nodes_list_to_dict,
1414
update_node_names,
15+
remove_result,
1516
NODES_LABEL,
1617
EDGES_LABEL,
1718
SOURCE_LABEL,
@@ -229,7 +230,7 @@ def load_workflow_json(file_name: str, project: Optional[Project] = None):
229230
project = Project(".")
230231

231232
with open(file_name, "r") as f:
232-
content = json.load(f)
233+
content = remove_result(workflow_dict=json.load(f))
233234

234235
edges_new_lst = content[EDGES_LABEL]
235236
nodes_new_dict = {}

python_workflow_definition/src/python_workflow_definition/shared.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ def convert_nodes_list_to_dict(nodes_list: list) -> dict:
4646
}
4747

4848

49-
def update_node_names(content: dict) -> dict:
49+
def update_node_names(workflow_dict: dict) -> dict:
5050
node_names_final_dict = {}
51-
input_nodes = [n for n in content[NODES_LABEL] if n["type"] == "input"]
51+
input_nodes = [n for n in workflow_dict[NODES_LABEL] if n["type"] == "input"]
5252
node_names_dict = {
5353
n["id"]: list(
5454
set(
5555
[
5656
e[TARGET_PORT_LABEL]
57-
for e in content[EDGES_LABEL]
57+
for e in workflow_dict[EDGES_LABEL]
5858
if e[SOURCE_LABEL] == n["id"]
5959
]
6060
)
@@ -71,7 +71,44 @@ def update_node_names(content: dict) -> dict:
7171
else:
7272
node_names_final_dict[k] = v
7373

74-
for n in content[NODES_LABEL]:
74+
for n in workflow_dict[NODES_LABEL]:
7575
if n["type"] == "input":
7676
n["name"] = node_names_final_dict[n["id"]]
77-
return content
77+
return workflow_dict
78+
79+
80+
def set_result_node(workflow_dict):
81+
node_id_lst = [n["id"] for n in workflow_dict[NODES_LABEL]]
82+
source_lst = list(set([e[SOURCE_LABEL] for e in workflow_dict[EDGES_LABEL]]))
83+
84+
end_node_lst = []
85+
for ni in node_id_lst:
86+
if ni not in source_lst:
87+
end_node_lst.append(ni)
88+
89+
node_id = len(workflow_dict[NODES_LABEL])
90+
workflow_dict[NODES_LABEL].append(
91+
{"id": node_id, "type": "output", "name": "result"}
92+
)
93+
workflow_dict[EDGES_LABEL].append(
94+
{
95+
TARGET_LABEL: node_id,
96+
TARGET_PORT_LABEL: None,
97+
SOURCE_LABEL: end_node_lst[0],
98+
SOURCE_PORT_LABEL: None,
99+
}
100+
)
101+
102+
return workflow_dict
103+
104+
105+
def remove_result(workflow_dict):
106+
node_output_id = [
107+
n["id"] for n in workflow_dict[NODES_LABEL] if n["type"] == "output"
108+
][0]
109+
return {
110+
NODES_LABEL: [n for n in workflow_dict[NODES_LABEL] if n["type"] != "output"],
111+
EDGES_LABEL: [
112+
e for e in workflow_dict[EDGES_LABEL] if e[TARGET_LABEL] != node_output_id
113+
],
114+
}

0 commit comments

Comments
 (0)