Skip to content

Commit 0dd8d02

Browse files
authored
Reduce JSON format (#34)
* Reduce JSON format * update readme
1 parent ed7055c commit 0dd8d02

File tree

9 files changed

+136
-136
lines changed

9 files changed

+136
-136
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ JSON file:
4242
"5": 2
4343
},
4444
"edges": [
45-
{"target": 0, "targetHandle": "x", "source": 1, "sourceHandle": "x"},
46-
{"target": 1, "targetHandle": "x", "source": 4, "sourceHandle": null},
47-
{"target": 1, "targetHandle": "y", "source": 5, "sourceHandle": null},
48-
{"target": 0, "targetHandle": "y", "source": 2, "sourceHandle": "y"},
49-
{"target": 2, "targetHandle": "x", "source": 4, "sourceHandle": null},
50-
{"target": 2, "targetHandle": "y", "source": 5, "sourceHandle": null},
51-
{"target": 0, "targetHandle": "z", "source": 3, "sourceHandle": "z"},
52-
{"target": 3, "targetHandle": "x", "source": 4, "sourceHandle": null},
53-
{"target": 3, "targetHandle": "y", "source": 5, "sourceHandle": null}
45+
{"tn": 0, "th": "x", "sn": 1, "sh": "x"},
46+
{"tn": 1, "th": "x", "sn": 4, "sh": null},
47+
{"tn": 1, "th": "y", "sn": 5, "sh": null},
48+
{"tn": 0, "th": "y", "sn": 2, "sh": "y"},
49+
{"tn": 2, "th": "x", "sn": 4, "sh": null},
50+
{"tn": 2, "th": "y", "sn": 5, "sh": null},
51+
{"tn": 0, "th": "z", "sn": 3, "sh": "z"},
52+
{"tn": 3, "th": "x", "sn": 4, "sh": null},
53+
{"tn": 3, "th": "y", "sn": 5, "sh": null}
5454
]
5555
}
5656
```

python_workflow_definition/src/python_workflow_definition/aiida.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,34 @@ def load_workflow_json(file_name):
3535
task_name_mapping[id] = wg.tasks[-1].name
3636
# add links
3737
for link in data["edges"]:
38-
if link["sourceHandle"] is None:
39-
link["sourceHandle"] = "result"
38+
if link["sh"] is None:
39+
link["sh"] = "result"
4040
try:
41-
from_task = wg.tasks[task_name_mapping[str(link["source"])]]
41+
from_task = wg.tasks[task_name_mapping[str(link["sn"])]]
4242
# because we are not define the outputs explicitly during the pythonjob creation
4343
# we add it here, and assume the output exit
44-
if link["sourceHandle"] not in from_task.outputs:
45-
# if str(link["sourceHandle"]) not in from_task.outputs:
44+
if link["sh"] not in from_task.outputs:
45+
# if str(link["sh"]) not in from_task.outputs:
4646
from_socket = from_task.add_output(
4747
"workgraph.any",
48-
name=link["sourceHandle"],
49-
# name=str(link["sourceHandle"]),
48+
name=link["sh"],
49+
# name=str(link["sh"]),
5050
metadata={"is_function_output": True},
5151
)
5252
else:
53-
from_socket = from_task.outputs[link["sourceHandle"]]
54-
to_task = wg.tasks[task_name_mapping[str(link["target"])]]
53+
from_socket = from_task.outputs[link["sh"]]
54+
to_task = wg.tasks[task_name_mapping[str(link["tn"])]]
5555
# if the input is not exit, it means we pass the data into to the kwargs
5656
# in this case, we add the input socket
57-
if link["targetHandle"] not in to_task.inputs:
57+
if link["th"] not in to_task.inputs:
5858
#
5959
to_socket = to_task.add_input(
6060
"workgraph.any",
61-
name=link["targetHandle"],
61+
name=link["th"],
6262
metadata={"is_function_input": True},
6363
)
6464
else:
65-
to_socket = to_task.inputs[link["targetHandle"]]
65+
to_socket = to_task.inputs[link["th"]]
6666
wg.add_link(from_socket, to_socket)
6767
except Exception as e:
6868
traceback.print_exc()
@@ -112,10 +112,10 @@ def write_workflow_json(wg, file_name):
112112
# if the from socket is the default result, we set it to None
113113
if link_data["from_socket"] == "result":
114114
link_data["from_socket"] = None
115-
link_data["target"] = node_name_mapping[link_data.pop("to_node")]
116-
link_data["targetHandle"] = link_data.pop("to_socket")
117-
link_data["source"] = node_name_mapping[link_data.pop("from_node")]
118-
link_data["sourceHandle"] = link_data.pop("from_socket")
115+
link_data["tn"] = node_name_mapping[link_data.pop("to_node")]
116+
link_data["th"] = link_data.pop("to_socket")
117+
link_data["sn"] = node_name_mapping[link_data.pop("from_node")]
118+
link_data["sh"] = link_data.pop("from_socket")
119119
data["edges"].append(link_data)
120120

121121
with open(file_name, "w") as f:

python_workflow_definition/src/python_workflow_definition/executorlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def get_item(obj, key):
1212

1313

1414
def _get_value(result_dict, nodes_new_dict, link_dict, exe):
15-
source, source_handle = link_dict["source"], link_dict["sourceHandle"]
15+
source, source_handle = link_dict["sn"], link_dict["sh"]
1616
if source in result_dict.keys():
1717
result = result_dict[source]
1818
elif source in nodes_new_dict.keys():

python_workflow_definition/src/python_workflow_definition/jobflow.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def _get_nodes_dict(function_dict):
2626

2727
def _get_edge_from_dict(target, key, value_dict, nodes_mapping_dict):
2828
if len(value_dict['attributes']) == 1:
29-
return {'target': target, 'targetHandle': key, "source": nodes_mapping_dict[value_dict['uuid']], 'sourceHandle': value_dict['attributes'][0][1]}
29+
return {'tn': target, 'th': key, "sn": nodes_mapping_dict[value_dict['uuid']], 'sh': value_dict['attributes'][0][1]}
3030
else:
31-
return {'target': target, 'targetHandle': key, "source": nodes_mapping_dict[value_dict['uuid']], 'sourceHandle': None}
31+
return {'tn': target, 'th': key, "sn": nodes_mapping_dict[value_dict['uuid']], 'sh': None}
3232

3333

3434
def _get_edges_and_extend_nodes(flow_dict, nodes_mapping_dict, nodes_dict):
@@ -59,8 +59,8 @@ def _get_edges_and_extend_nodes(flow_dict, nodes_mapping_dict, nodes_dict):
5959
nodes_dict[node_index] = vt
6060
else:
6161
node_index = {str(tv): tk for tk, tv in nodes_dict.items()}[str(vt)]
62-
edges_lst.append({'target': node_dict_index, 'targetHandle': kt, "source": node_index, 'sourceHandle': None})
63-
edges_lst.append({'target': nodes_mapping_dict[job["uuid"]], 'targetHandle': k, "source": node_dict_index, 'sourceHandle': None})
62+
edges_lst.append({'tn': node_dict_index, 'th': kt, "sn": node_index, 'sh': None})
63+
edges_lst.append({'tn': nodes_mapping_dict[job["uuid"]], 'th': k, "sn": node_dict_index, 'sh': None})
6464
elif isinstance(v, list) and any([isinstance(el, dict) and '@module' in el and '@class' in el and '@version' in el for el in v]):
6565
node_list_index = len(nodes_dict)
6666
nodes_dict[node_list_index] = get_list
@@ -78,15 +78,15 @@ def _get_edges_and_extend_nodes(flow_dict, nodes_mapping_dict, nodes_dict):
7878
nodes_dict[node_index] = vt
7979
else:
8080
node_index = {str(tv): tk for tk, tv in nodes_dict.items()}[str(vt)]
81-
edges_lst.append({'target': node_list_index, 'targetHandle': kt, "source": node_index, 'sourceHandle': None})
82-
edges_lst.append({'target': nodes_mapping_dict[job["uuid"]], 'targetHandle': k, "source": node_list_index, 'sourceHandle': None})
81+
edges_lst.append({'tn': node_list_index, 'th': kt, "sn": node_index, 'sh': None})
82+
edges_lst.append({'tn': nodes_mapping_dict[job["uuid"]], 'th': k, "sn": node_list_index, 'sh': None})
8383
else:
8484
if v not in nodes_dict.values():
8585
node_index = len(nodes_dict)
8686
nodes_dict[node_index] = v
8787
else:
8888
node_index = {tv: tk for tk, tv in nodes_dict.items()}[v]
89-
edges_lst.append({'target': nodes_mapping_dict[job["uuid"]], 'targetHandle': k, "source": node_index, 'sourceHandle': None})
89+
edges_lst.append({'tn': nodes_mapping_dict[job["uuid"]], 'th': k, "sn": node_index, 'sh': None})
9090
return edges_lst, nodes_dict
9191

9292

@@ -99,7 +99,7 @@ def _resort_total_lst(total_dict, nodes_dict):
9999
for ind in sorted(total_dict.keys()):
100100
connect = total_dict[ind]
101101
if ind not in ordered_lst:
102-
source_lst = [sd["source"] for sd in connect.values()]
102+
source_lst = [sd["sn"] for sd in connect.values()]
103103
if all([s in ordered_lst or s in nodes_without_dep_lst for s in source_lst]):
104104
ordered_lst.append(ind)
105105
total_new_dict[ind] = connect
@@ -109,11 +109,11 @@ def _resort_total_lst(total_dict, nodes_dict):
109109
def _group_edges(edges_lst):
110110
total_dict = {}
111111
for ed_major in edges_lst:
112-
target_id = ed_major["target"]
112+
target_id = ed_major["tn"]
113113
tmp_lst = []
114114
if target_id not in total_dict.keys():
115115
for ed in edges_lst:
116-
if target_id == ed["target"]:
116+
if target_id == ed["tn"]:
117117
tmp_lst.append(ed)
118118
total_dict[target_id] = get_kwargs(lst=tmp_lst)
119119
return total_dict
@@ -139,8 +139,8 @@ def get_attr_helper(obj, source_handle):
139139
else:
140140
fn = job(method=v)
141141
kwargs = {
142-
kw: input_dict[vw['source']] if vw['source'] in input_dict else get_attr_helper(
143-
obj=memory_dict[vw['source']], source_handle=vw['sourceHandle'])
142+
kw: input_dict[vw['sn']] if vw['sn'] in input_dict else get_attr_helper(
143+
obj=memory_dict[vw['sn']], source_handle=vw['sh'])
144144
for kw, vw in total_dict[k].items()
145145
}
146146
memory_dict[k] = fn(**kwargs)
@@ -160,15 +160,15 @@ def load_workflow_json(file_name):
160160

161161
edges_new_lst = []
162162
for edge in content["edges"]:
163-
if edge['sourceHandle'] is None:
163+
if edge['sh'] is None:
164164
edges_new_lst.append(edge)
165165
else:
166166
edges_new_lst.append(
167167
{
168-
'target': edge['target'],
169-
'targetHandle': edge['targetHandle'],
170-
'source': edge['source'],
171-
'sourceHandle': str(edge['sourceHandle']),
168+
'tn': edge['tn'],
169+
'th': edge['th'],
170+
'sn': edge['sn'],
171+
'sh': str(edge['sh']),
172172
}
173173
)
174174

python_workflow_definition/src/python_workflow_definition/purepython.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@ def resort_total_lst(total_lst, nodes_dict):
1313
while len(total_new_lst) < len(total_lst):
1414
for ind, connect in total_lst:
1515
if ind not in ordered_lst:
16-
source_lst = [sd["source"] for sd in connect.values()]
16+
source_lst = [sd["sn"] for sd in connect.values()]
1717
if all([s in ordered_lst or s in nodes_without_dep_lst for s in source_lst]):
1818
ordered_lst.append(ind)
1919
total_new_lst.append([ind, connect])
2020
return total_new_lst
2121

2222

2323
def group_edges(edges_lst):
24-
edges_sorted_lst = sorted(edges_lst, key=lambda x: x['target'], reverse=True)
24+
edges_sorted_lst = sorted(edges_lst, key=lambda x: x["tn"], reverse=True)
2525
total_lst, tmp_lst = [], []
26-
target_id = edges_sorted_lst[0]['target']
26+
target_id = edges_sorted_lst[0]["tn"]
2727
for ed in edges_sorted_lst:
28-
if target_id == ed["target"]:
28+
if target_id == ed["tn"]:
2929
tmp_lst.append(ed)
3030
else:
3131
total_lst.append((target_id, get_kwargs(lst=tmp_lst)))
32-
target_id = ed["target"]
32+
target_id = ed["tn"]
3333
tmp_lst = [ed]
3434
total_lst.append((target_id, get_kwargs(lst=tmp_lst)))
3535
return total_lst
3636

3737

3838
def _get_value(result_dict, nodes_new_dict, link_dict):
39-
source, source_handle = link_dict["source"], link_dict["sourceHandle"]
39+
source, source_handle = link_dict["sn"], link_dict["sh"]
4040
if source in result_dict.keys():
4141
result = result_dict[source]
4242
elif source in nodes_new_dict.keys():

python_workflow_definition/src/python_workflow_definition/pyiron_base.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ def _resort_total_lst(total_lst, nodes_dict):
1616
while len(total_new_lst) < len(total_lst):
1717
for ind, connect in total_lst:
1818
if ind not in ordered_lst:
19-
source_lst = [sd["source"] for sd in connect.values()]
19+
source_lst = [sd["sn"] for sd in connect.values()]
2020
if all([s in ordered_lst or s in nodes_without_dep_lst for s in source_lst]):
2121
ordered_lst.append(ind)
2222
total_new_lst.append([ind, connect])
2323
return total_new_lst
2424

2525

2626
def _group_edges(edges_lst):
27-
edges_sorted_lst = sorted(edges_lst, key=lambda x: x['target'], reverse=True)
27+
edges_sorted_lst = sorted(edges_lst, key=lambda x: x["tn"], reverse=True)
2828
total_lst, tmp_lst = [], []
29-
target_id = edges_sorted_lst[0]['target']
29+
target_id = edges_sorted_lst[0]["tn"]
3030
for ed in edges_sorted_lst:
31-
if target_id == ed["target"]:
31+
if target_id == ed["tn"]:
3232
tmp_lst.append(ed)
3333
else:
3434
total_lst.append((target_id, get_kwargs(lst=tmp_lst)))
35-
target_id = ed["target"]
35+
target_id = ed["tn"]
3636
tmp_lst = [ed]
3737
total_lst.append((target_id, get_kwargs(lst=tmp_lst)))
3838
return total_lst
@@ -53,8 +53,8 @@ def _get_delayed_object_dict(total_lst, nodes_dict, source_handle_dict, pyiron_p
5353
k: _get_source(
5454
nodes_dict=nodes_dict,
5555
delayed_object_dict=delayed_object_dict,
56-
source=v["source"],
57-
sourceHandle=v["sourceHandle"],
56+
source=v["sn"],
57+
sourceHandle=v["sh"],
5858
)
5959
for k, v in input_dict.items()
6060
}
@@ -151,24 +151,24 @@ def _get_edges_dict(edges_lst, nodes_dict, connection_dict, lookup_dict):
151151
if isinstance(output, DelayedObject):
152152
if output._list_index is not None:
153153
edges_dict_lst.append({
154-
"target": target,
155-
"targetHandle": target_handle,
156-
"source": connection_dict[output_name],
157-
"sourceHandle": f"s_{output._list_index}", # check for list index
154+
"tn": target,
155+
"th": target_handle,
156+
"sn": connection_dict[output_name],
157+
"sh": f"s_{output._list_index}", # check for list index
158158
})
159159
else:
160160
edges_dict_lst.append({
161-
"target": target,
162-
"targetHandle": target_handle,
163-
"source": connection_dict[output_name],
164-
"sourceHandle": output._output_key, # check for list index
161+
"tn": target,
162+
"th": target_handle,
163+
"sn": connection_dict[output_name],
164+
"sh": output._output_key, # check for list index
165165
})
166166
else:
167167
edges_dict_lst.append({
168-
"target": target,
169-
"targetHandle": target_handle,
170-
"source": connection_dict[output_name],
171-
"sourceHandle": None,
168+
"tn": target,
169+
"th": target_handle,
170+
"sn": connection_dict[output_name],
171+
"sh": None,
172172
})
173173
existing_connection_lst.append(connection_name)
174174
return edges_dict_lst

python_workflow_definition/src/python_workflow_definition/shared.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ def get_list(**kwargs):
99

1010

1111
def get_kwargs(lst):
12-
return {t['targetHandle']: {'source': t['source'], 'sourceHandle': t['sourceHandle']} for t in lst}
12+
return {t["th"]: {"sn": t["sn"], "sh": t["sh"]} for t in lst}
1313

1414

1515
def get_source_handles(edges_lst):
1616
source_handle_dict = {}
1717
for ed in edges_lst:
18-
if ed['source'] not in source_handle_dict.keys():
19-
source_handle_dict[ed['source']] = [ed['sourceHandle']]
18+
if ed["sn"] not in source_handle_dict.keys():
19+
source_handle_dict[ed["sn"]] = [ed["sh"]]
2020
else:
21-
source_handle_dict[ed['source']].append(ed['sourceHandle'])
21+
source_handle_dict[ed["sn"]].append(ed["sh"])
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()

0 commit comments

Comments
 (0)