Skip to content

Commit a2ba005

Browse files
authored
Convert nodes to list of dictionaries (#53)
* Convert nodes to list of dictionaries * update workflows * Update readme * separate values and functions * update readme
1 parent 2a2d958 commit a2ba005

File tree

11 files changed

+98
-88
lines changed

11 files changed

+98
-88
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ Python module. The connection of the Python functions are stored in the [workflo
3434
JSON file:
3535
```
3636
{
37-
"nodes": {
38-
"0": "simple_workflow.add_x_and_y_and_z",
39-
"1": "simple_workflow.add_x_and_y",
40-
"2": "simple_workflow.add_x_and_y",
41-
"3": "simple_workflow.add_x_and_y",
42-
"4": 1,
43-
"5": 2
44-
},
37+
"nodes": [
38+
{"id": 0, "function": "simple_workflow.add_x_and_y_and_z"},
39+
{"id": 1, "function": "simple_workflow.add_x_and_y"},
40+
{"id": 2, "function": "simple_workflow.add_x_and_y"},
41+
{"id": 3, "function": "simple_workflow.add_x_and_y"},
42+
{"id": 4, "value": 1},
43+
{"id": 5, "value": 2}
44+
],
4545
"edges": [
4646
{"target": 0, "targetPort": "x", "source": 1, "sourcePort": "x"},
4747
{"target": 1, "targetPort": "x", "source": 4, "sourcePort": null},

book/simple.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ Python module. The connection of the Python functions are stored in the [workflo
1919
JSON file:
2020
```
2121
{
22-
"nodes": {
23-
"0": "simple_workflow.add_x_and_y_and_z",
24-
"1": "simple_workflow.add_x_and_y",
25-
"2": "simple_workflow.add_x_and_y",
26-
"3": "simple_workflow.add_x_and_y",
27-
"4": 1,
28-
"5": 2
29-
},
22+
"nodes": [
23+
{"id": 0, "function": "simple_workflow.add_x_and_y_and_z"},
24+
{"id": 1, "function": "simple_workflow.add_x_and_y"},
25+
{"id": 2, "function": "simple_workflow.add_x_and_y"},
26+
{"id": 3, "function": "simple_workflow.add_x_and_y"},
27+
{"id": 4, "value": 1},
28+
{"id": 5, "value": 2}
29+
],
3030
"edges": [
3131
{"target": 0, "targetPort": "x", "source": 1, "sourcePort": "x"},
3232
{"target": 1, "targetPort": "x", "source": 4, "sourcePort": null},

python_workflow_definition/src/python_workflow_definition/aiida.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from aiida_workgraph import WorkGraph, task
88
from aiida_workgraph.socket import TaskSocketNamespace
99

10+
from python_workflow_definition.shared import convert_nodes_list_to_dict
11+
1012

1113
def load_workflow_json(file_name):
1214
with open(file_name) as f:
@@ -15,7 +17,7 @@ def load_workflow_json(file_name):
1517
wg = WorkGraph()
1618
task_name_mapping = {}
1719

18-
for id, identifier in data["nodes"].items():
20+
for id, identifier in convert_nodes_list_to_dict(nodes_list=data["nodes"]).items():
1921
if isinstance(identifier, str) and "." in identifier:
2022
p, m = identifier.rsplit(".", 1)
2123
mod = import_module(p)
@@ -66,7 +68,7 @@ def load_workflow_json(file_name):
6668

6769

6870
def write_workflow_json(wg, file_name):
69-
data = {"nodes": {}, "edges": []}
71+
data = {"nodes": [], "edges": []}
7072
node_name_mapping = {}
7173
data_node_name_mapping = {}
7274
i = 0
@@ -76,7 +78,7 @@ def write_workflow_json(wg, file_name):
7678

7779
callable_name = executor["callable_name"]
7880
callable_name = f"{executor['module_path']}.{callable_name}"
79-
data["nodes"][str(i)] = callable_name
81+
data["nodes"].append({"id": i, "function": callable_name})
8082
i += 1
8183

8284
for link in wg.links:
@@ -105,7 +107,7 @@ def write_workflow_json(wg, file_name):
105107
raw_value.pop("node_type", None)
106108
else:
107109
raw_value = input.value.value
108-
data["nodes"][str(i)] = raw_value
110+
data["nodes"].append({"id": i, "value": raw_value})
109111
input_node_name = i
110112
data_node_name_mapping[input.value.uuid] = input_node_name
111113
i += 1

python_workflow_definition/src/python_workflow_definition/executorlib.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from inspect import isfunction
44

55

6-
from python_workflow_definition.shared import get_dict, get_list, get_kwargs, get_source_handles
6+
from python_workflow_definition.shared import get_dict, get_list, get_kwargs, get_source_handles, convert_nodes_list_to_dict
77
from python_workflow_definition.purepython import resort_total_lst, group_edges
88

99

@@ -31,7 +31,8 @@ def load_workflow_json(file_name, exe):
3131

3232
edges_new_lst = content["edges"]
3333
nodes_new_dict = {}
34-
for k, v in content["nodes"].items():
34+
35+
for k, v in convert_nodes_list_to_dict(nodes_list=content["nodes"]).items():
3536
if isinstance(v, str) and "." in v:
3637
p, m = v.rsplit('.', 1)
3738
mod = import_module(p)

python_workflow_definition/src/python_workflow_definition/jobflow.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
from jobflow import job, Flow
77

8-
from python_workflow_definition.shared import get_dict, get_list, get_kwargs, get_source_handles
8+
from python_workflow_definition.shared import get_dict, get_list, get_kwargs, get_source_handles, convert_nodes_list_to_dict
99

1010

1111
def _get_function_dict(flow):
@@ -173,7 +173,7 @@ def load_workflow_json(file_name):
173173
)
174174

175175
nodes_new_dict = {}
176-
for k, v in content["nodes"].items():
176+
for k, v in convert_nodes_list_to_dict(nodes_list=content["nodes"]).items():
177177
if isinstance(v, str) and "." in v:
178178
p, m = v.rsplit('.', 1)
179179
mod = import_module(p)
@@ -204,14 +204,14 @@ def write_workflow_json(flow, file_name="workflow.json"):
204204
nodes_dict=nodes_dict,
205205
)
206206

207-
nodes_store_dict = {}
207+
nodes_store_lst = []
208208
for k, v in nodes_dict.items():
209209
if isfunction(v):
210-
nodes_store_dict[k] = v.__module__ + "." + v.__name__
210+
nodes_store_lst.append({"id": k, "function": v.__module__ + "." + v.__name__})
211211
elif isinstance(v, np.ndarray):
212-
nodes_store_dict[k] = v.tolist()
212+
nodes_store_lst.append({"id": k, "value": v.tolist()})
213213
else:
214-
nodes_store_dict[k] = v
214+
nodes_store_lst.append({"id": k, "value": v})
215215

216216
with open(file_name, "w") as f:
217-
json.dump({"nodes": nodes_store_dict, "edges": edges_lst}, f)
217+
json.dump({"nodes": nodes_store_lst, "edges": edges_lst}, f)

python_workflow_definition/src/python_workflow_definition/purepython.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from inspect import isfunction
44

55

6-
from python_workflow_definition.shared import get_dict, get_list, get_kwargs, get_source_handles
6+
from python_workflow_definition.shared import get_dict, get_list, get_kwargs, get_source_handles, convert_nodes_list_to_dict
77

88

99
def resort_total_lst(total_lst, nodes_dict):
@@ -55,7 +55,7 @@ def load_workflow_json(file_name):
5555

5656
edges_new_lst = content["edges"]
5757
nodes_new_dict = {}
58-
for k, v in content["nodes"].items():
58+
for k, v in convert_nodes_list_to_dict(nodes_list=content["nodes"]).items():
5959
if isinstance(v, str) and "." in v:
6060
p, m = v.rsplit('.', 1)
6161
mod = import_module(p)

python_workflow_definition/src/python_workflow_definition/pyiron_base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pyiron_base import job
77
from pyiron_base.project.delayed import DelayedObject
88

9-
from python_workflow_definition.shared import get_kwargs, get_source_handles
9+
from python_workflow_definition.shared import get_kwargs, get_source_handles, convert_nodes_list_to_dict
1010

1111

1212
def _resort_total_lst(total_lst, nodes_dict):
@@ -181,7 +181,7 @@ def load_workflow_json(project, file_name):
181181

182182
edges_new_lst = content["edges"]
183183
nodes_new_dict = {}
184-
for k, v in content["nodes"].items():
184+
for k, v in convert_nodes_list_to_dict(nodes_list=content["nodes"]).items():
185185
if isinstance(v, str) and "." in v:
186186
p, m = v.rsplit('.', 1)
187187
if p == "python_workflow_definition.shared":
@@ -211,17 +211,17 @@ def write_workflow_json(delayed_object, file_name="workflow.json"):
211211
nodes_new_dict = _get_nodes(connection_dict=connection_dict, delayed_object_updated_dict=delayed_object_updated_dict)
212212
edges_new_lst = _get_edges_dict(edges_lst=edges_lst, nodes_dict=nodes_dict, connection_dict=connection_dict, lookup_dict=lookup_dict)
213213

214-
nodes_store_dict = {}
214+
nodes_store_lst = []
215215
for k, v in nodes_new_dict.items():
216216
if isfunction(v):
217217
mod = v.__module__
218218
if mod == "python_workflow_definition.pyiron_base":
219219
mod = "python_workflow_definition.shared"
220-
nodes_store_dict[k] = mod + "." + v.__name__
220+
nodes_store_lst.append({"id": k, "function": mod + "." + v.__name__})
221221
elif isinstance(v, np.ndarray):
222-
nodes_store_dict[k] = v.tolist()
222+
nodes_store_lst.append({"id": k, "value": v.tolist()})
223223
else:
224-
nodes_store_dict[k] = v
224+
nodes_store_lst.append({"id": k, "value": v})
225225

226226
with open(file_name, "w") as f:
227-
json.dump({"nodes": nodes_store_dict, "edges": edges_new_lst}, f)
227+
json.dump({"nodes": nodes_store_lst, "edges": edges_new_lst}, f)

python_workflow_definition/src/python_workflow_definition/shared.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ def get_source_handles(edges_lst):
2222
return {
2323
k: list(range(len(v))) if len(v) > 1 and all([el is None for el in v]) else v
2424
for k, v in source_handle_dict.items()
25-
}
25+
}
26+
27+
28+
def convert_nodes_list_to_dict(nodes_list):
29+
return {
30+
str(el["id"]): el["value"] if "value" in el else el["function"]
31+
for el in sorted(nodes_list, key=lambda d: d["id"])
32+
}

workflow_nfdi.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"nodes": {
3-
"0": "nfdi_ing_workflow.generate_mesh",
4-
"1": "nfdi_ing_workflow.convert_to_xdmf",
5-
"2": "nfdi_ing_workflow.poisson",
6-
"3": "nfdi_ing_workflow.plot_over_line",
7-
"4": "nfdi_ing_workflow.substitute_macros",
8-
"5": "nfdi_ing_workflow.compile_paper",
9-
"6": 2.0
10-
},
2+
"nodes": [
3+
{"id": 0, "function": "nfdi_ing_workflow.generate_mesh"},
4+
{"id": 1, "function": "nfdi_ing_workflow.convert_to_xdmf"},
5+
{"id": 2, "function": "nfdi_ing_workflow.poisson"},
6+
{"id": 3, "function": "nfdi_ing_workflow.plot_over_line"},
7+
{"id": 4, "function": "nfdi_ing_workflow.substitute_macros"},
8+
{"id": 5, "function": "nfdi_ing_workflow.compile_paper"},
9+
{"id": 6, "value": 2.0}
10+
],
1111
"edges": [
1212
{"target": 0, "targetPort": "domain_size", "source": 6, "sourcePort": null},
1313
{"target": 1, "targetPort": "gmsh_output_file", "source": 0, "sourcePort": null},

workflow_qe.json

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
{
2-
"nodes": {
3-
"0": "quantum_espresso_workflow.get_bulk_structure",
4-
"1": "quantum_espresso_workflow.calculate_qe",
5-
"2": "quantum_espresso_workflow.generate_structures",
6-
"3": "quantum_espresso_workflow.calculate_qe",
7-
"4": "quantum_espresso_workflow.calculate_qe",
8-
"5": "quantum_espresso_workflow.calculate_qe",
9-
"6": "quantum_espresso_workflow.calculate_qe",
10-
"7": "quantum_espresso_workflow.calculate_qe",
11-
"8": "quantum_espresso_workflow.plot_energy_volume_curve",
12-
"9": "Al",
13-
"10": 4.05,
14-
"11": true,
15-
"12": "mini",
16-
"13": "python_workflow_definition.shared.get_dict",
17-
"14": {"Al": "Al.pbe-n-kjpaw_psl.1.0.0.UPF"},
18-
"15": [3, 3, 3],
19-
"16": "vc-relax",
20-
"17": 0.02,
21-
"18": [0.9, 0.95, 1.0, 1.05, 1.1],
22-
"19": "strain_0",
23-
"20": "python_workflow_definition.shared.get_dict",
24-
"21": "scf",
25-
"22": "strain_1",
26-
"23": "python_workflow_definition.shared.get_dict",
27-
"24": "strain_2",
28-
"25": "python_workflow_definition.shared.get_dict",
29-
"26": "strain_3",
30-
"27": "python_workflow_definition.shared.get_dict",
31-
"28": "strain_4",
32-
"29": "python_workflow_definition.shared.get_dict",
33-
"30": "python_workflow_definition.shared.get_list",
34-
"31": "python_workflow_definition.shared.get_list"
35-
},
2+
"nodes": [
3+
{"id": 0, "function": "quantum_espresso_workflow.get_bulk_structure"},
4+
{"id": 1, "function": "quantum_espresso_workflow.calculate_qe"},
5+
{"id": 2, "function": "quantum_espresso_workflow.generate_structures"},
6+
{"id": 3, "function": "quantum_espresso_workflow.calculate_qe"},
7+
{"id": 4, "function": "quantum_espresso_workflow.calculate_qe"},
8+
{"id": 5, "function": "quantum_espresso_workflow.calculate_qe"},
9+
{"id": 6, "function": "quantum_espresso_workflow.calculate_qe"},
10+
{"id": 7, "function": "quantum_espresso_workflow.calculate_qe"},
11+
{"id": 8, "function": "quantum_espresso_workflow.plot_energy_volume_curve"},
12+
{"id": 9, "value": "Al"},
13+
{"id": 10, "value": 4.05},
14+
{"id": 11, "value": true},
15+
{"id": 12, "value": "mini"},
16+
{"id": 13, "function": "python_workflow_definition.shared.get_dict"},
17+
{"id": 14, "value": {"Al": "Al.pbe-n-kjpaw_psl.1.0.0.UPF"}},
18+
{"id": 15, "value": [3, 3, 3]},
19+
{"id": 16, "value": "vc-relax"},
20+
{"id": 17, "value": 0.02},
21+
{"id": 18, "value": [0.9, 0.95, 1.0, 1.05, 1.1]},
22+
{"id": 19, "value": "strain_0"},
23+
{"id": 20, "function": "python_workflow_definition.shared.get_dict"},
24+
{"id": 21, "value": "scf"},
25+
{"id": 22, "value": "strain_1"},
26+
{"id": 23, "function": "python_workflow_definition.shared.get_dict"},
27+
{"id": 24, "value": "strain_2"},
28+
{"id": 25, "function": "python_workflow_definition.shared.get_dict"},
29+
{"id": 26, "value": "strain_3"},
30+
{"id": 27, "function": "python_workflow_definition.shared.get_dict"},
31+
{"id": 28, "value": "strain_4"},
32+
{"id": 29, "function": "python_workflow_definition.shared.get_dict"},
33+
{"id": 30, "function": "python_workflow_definition.shared.get_list"},
34+
{"id": 31, "function": "python_workflow_definition.shared.get_list"}
35+
],
3636
"edges": [
3737
{"target": 0, "targetPort": "element", "source": 9, "sourcePort": null},
3838
{"target": 0, "targetPort": "a", "source": 10, "sourcePort": null},

0 commit comments

Comments
 (0)