From 83e5644a9df3315755c86a8e49850294f5f30432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jan=C3=9Fen?= Date: Sun, 11 May 2025 20:19:06 +0200 Subject: [PATCH 1/2] Implement input node names --- example_workflows/arithmetic/workflow.json | 4 +-- example_workflows/nfdi/workflow.json | 2 +- .../quantum_espresso/workflow.json | 30 +++++++++---------- .../src/python_workflow_definition/aiida.py | 3 +- .../src/python_workflow_definition/jobflow.py | 6 +++- .../python_workflow_definition/pyiron_base.py | 6 +++- .../src/python_workflow_definition/shared.py | 25 ++++++++++++++++ 7 files changed, 55 insertions(+), 21 deletions(-) diff --git a/example_workflows/arithmetic/workflow.json b/example_workflows/arithmetic/workflow.json index d4b2d59..9ae194e 100644 --- a/example_workflows/arithmetic/workflow.json +++ b/example_workflows/arithmetic/workflow.json @@ -2,8 +2,8 @@ "nodes": [ {"id": 0, "type": "function", "value": "workflow.get_prod_and_div"}, {"id": 1, "type": "function", "value": "workflow.get_sum"}, - {"id": 2, "type": "input", "value": 1}, - {"id": 3, "type": "input", "value": 2} + {"id": 2, "type": "input", "value": 1, "name": "x"}, + {"id": 3, "type": "input", "value": 2, "name": "y"} ], "edges": [ {"target": 0, "targetPort": "x", "source": 2, "sourcePort": null}, diff --git a/example_workflows/nfdi/workflow.json b/example_workflows/nfdi/workflow.json index a4b2ad0..67b48af 100644 --- a/example_workflows/nfdi/workflow.json +++ b/example_workflows/nfdi/workflow.json @@ -6,7 +6,7 @@ {"id": 3, "type": "function", "value": "workflow.plot_over_line"}, {"id": 4, "type": "function", "value": "workflow.substitute_macros"}, {"id": 5, "type": "function", "value": "workflow.compile_paper"}, - {"id": 6, "type": "input", "value": 2.0} + {"id": 6, "type": "input", "value": 2.0, "name": "domain_size"} ], "edges": [ {"target": 0, "targetPort": "domain_size", "source": 6, "sourcePort": null}, diff --git a/example_workflows/quantum_espresso/workflow.json b/example_workflows/quantum_espresso/workflow.json index 6661a76..5ee7b4e 100644 --- a/example_workflows/quantum_espresso/workflow.json +++ b/example_workflows/quantum_espresso/workflow.json @@ -9,26 +9,26 @@ {"id": 6, "type": "function", "value": "workflow.calculate_qe"}, {"id": 7, "type": "function", "value": "workflow.calculate_qe"}, {"id": 8, "type": "function", "value": "workflow.plot_energy_volume_curve"}, - {"id": 9, "type": "input", "value": "Al"}, - {"id": 10, "type": "input", "value": 4.05}, - {"id": 11, "type": "input", "value": true}, - {"id": 12, "type": "input", "value": "mini"}, + {"id": 9, "type": "input", "value": "Al", "name": "element"}, + {"id": 10, "type": "input", "value": 4.05, "name": "a"}, + {"id": 11, "type": "input", "value": true, "name": "cubic"}, + {"id": 12, "type": "input", "value": "mini", "name": "working_directory_0"}, {"id": 13, "type": "function", "value": "python_workflow_definition.shared.get_dict"}, - {"id": 14, "type": "input", "value": {"Al": "Al.pbe-n-kjpaw_psl.1.0.0.UPF"}}, - {"id": 15, "type": "input", "value": [3, 3, 3]}, - {"id": 16, "type": "input", "value": "vc-relax"}, - {"id": 17, "type": "input", "value": 0.02}, - {"id": 18, "type": "input", "value": [0.9, 0.95, 1.0, 1.05, 1.1]}, - {"id": 19, "type": "input", "value": "strain_0"}, + {"id": 14, "type": "input", "value": {"Al": "Al.pbe-n-kjpaw_psl.1.0.0.UPF"}, "name": "pseudopotentials"}, + {"id": 15, "type": "input", "value": [3, 3, 3], "name": "kpts"}, + {"id": 16, "type": "input", "value": "vc-relax", "name": "calculation_0"}, + {"id": 17, "type": "input", "value": 0.02, "name": "smearing"}, + {"id": 18, "type": "input", "value": [0.9, 0.95, 1.0, 1.05, 1.1], "name": "strain_lst"}, + {"id": 19, "type": "input", "value": "strain_0", "name": "working_directory_1"}, {"id": 20, "type": "function", "value": "python_workflow_definition.shared.get_dict"}, - {"id": 21, "type": "input", "value": "scf"}, - {"id": 22, "type": "input", "value": "strain_1"}, + {"id": 21, "type": "input", "value": "scf", "name": "calculation_1"}, + {"id": 22, "type": "input", "value": "strain_1", "name": "working_directory_2"}, {"id": 23, "type": "function", "value": "python_workflow_definition.shared.get_dict"}, - {"id": 24, "type": "input", "value": "strain_2"}, + {"id": 24, "type": "input", "value": "strain_2", "name": "working_directory_3"}, {"id": 25, "type": "function", "value": "python_workflow_definition.shared.get_dict"}, - {"id": 26, "type": "input", "value": "strain_3"}, + {"id": 26, "type": "input", "value": "strain_3", "name": "working_directory_4"}, {"id": 27, "type": "function", "value": "python_workflow_definition.shared.get_dict"}, - {"id": 28, "type": "input", "value": "strain_4"}, + {"id": 28, "type": "input", "value": "strain_4", "name": "working_directory_5"}, {"id": 29, "type": "function", "value": "python_workflow_definition.shared.get_dict"}, {"id": 30, "type": "function", "value": "python_workflow_definition.shared.get_list"}, {"id": 31, "type": "function", "value": "python_workflow_definition.shared.get_list"} diff --git a/python_workflow_definition/src/python_workflow_definition/aiida.py b/python_workflow_definition/src/python_workflow_definition/aiida.py index 9b5c95b..cb7d93d 100644 --- a/python_workflow_definition/src/python_workflow_definition/aiida.py +++ b/python_workflow_definition/src/python_workflow_definition/aiida.py @@ -9,6 +9,7 @@ from python_workflow_definition.shared import ( convert_nodes_list_to_dict, + update_node_names, NODES_LABEL, EDGES_LABEL, SOURCE_LABEL, @@ -135,6 +136,6 @@ def write_workflow_json(wg: WorkGraph, file_name: str) -> dict: ) with open(file_name, "w") as f: # json.dump({"nodes": data[], "edges": edges_new_lst}, f) - json.dump(data, f, indent=2) + json.dump(update_node_names(content=data), f, indent=2) return data diff --git a/python_workflow_definition/src/python_workflow_definition/jobflow.py b/python_workflow_definition/src/python_workflow_definition/jobflow.py index c5966b4..9a5a192 100644 --- a/python_workflow_definition/src/python_workflow_definition/jobflow.py +++ b/python_workflow_definition/src/python_workflow_definition/jobflow.py @@ -10,6 +10,7 @@ get_list, get_kwargs, get_source_handles, + update_node_names, convert_nodes_list_to_dict, NODES_LABEL, EDGES_LABEL, @@ -330,4 +331,7 @@ def write_workflow_json(flow: Flow, file_name: str = "workflow.json"): nodes_store_lst.append({"id": k, "type": "input", "value": v}) with open(file_name, "w") as f: - json.dump({NODES_LABEL: nodes_store_lst, EDGES_LABEL: edges_lst}, f) + json.dump(update_node_names(content={ + NODES_LABEL: nodes_store_lst, + EDGES_LABEL: edges_lst + }), f, indent=2) diff --git a/python_workflow_definition/src/python_workflow_definition/pyiron_base.py b/python_workflow_definition/src/python_workflow_definition/pyiron_base.py index a611ae7..2a85340 100644 --- a/python_workflow_definition/src/python_workflow_definition/pyiron_base.py +++ b/python_workflow_definition/src/python_workflow_definition/pyiron_base.py @@ -11,6 +11,7 @@ get_kwargs, get_source_handles, convert_nodes_list_to_dict, + update_node_names, NODES_LABEL, EDGES_LABEL, SOURCE_LABEL, @@ -291,4 +292,7 @@ def write_workflow_json( nodes_store_lst.append({"id": k, "type": "input", "value": v}) with open(file_name, "w") as f: - json.dump({NODES_LABEL: nodes_store_lst, EDGES_LABEL: edges_new_lst}, f) + json.dump(update_node_names(content={ + NODES_LABEL: nodes_store_lst, + EDGES_LABEL: edges_new_lst + }), f, indent=2) diff --git a/python_workflow_definition/src/python_workflow_definition/shared.py b/python_workflow_definition/src/python_workflow_definition/shared.py index a8cb888..c613c03 100644 --- a/python_workflow_definition/src/python_workflow_definition/shared.py +++ b/python_workflow_definition/src/python_workflow_definition/shared.py @@ -1,3 +1,5 @@ +from collections import Counter + NODES_LABEL = "nodes" EDGES_LABEL = "edges" SOURCE_LABEL = "source" @@ -42,3 +44,26 @@ def convert_nodes_list_to_dict(nodes_list: list) -> dict: return { str(el["id"]): el["value"] for el in sorted(nodes_list, key=lambda d: d["id"]) } + + +def update_node_names(content: dict) -> dict: + node_names_final_dict = {} + input_nodes = [n for n in content[NODES_LABEL] if n["type"] == "input"] + node_names_dict = { + n["id"]: list(set([e[TARGET_PORT_LABEL] for e in content[EDGES_LABEL] if e[SOURCE_LABEL] == n["id"]]))[0] + for n in input_nodes + } + + counter_dict = Counter(node_names_dict.values()) + node_names_useage_dict = {k: -1 for k in counter_dict.keys()} + for k, v in node_names_dict.items(): + node_names_useage_dict[v] += 1 + if counter_dict[v] > 1: + node_names_final_dict[k] = v + "_" + str(node_names_useage_dict[v]) + else: + node_names_final_dict[k] = v + + for n in content[NODES_LABEL]: + if n["type"] == "input": + n["name"] = node_names_final_dict[n["id"]] + return content \ No newline at end of file From 8556c069c37bff54cf07a888f1ec6c1f966869ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jan=C3=9Fen?= Date: Sun, 11 May 2025 20:21:07 +0200 Subject: [PATCH 2/2] black formatting --- .../src/python_workflow_definition/jobflow.py | 11 +++++++---- .../src/python_workflow_definition/pyiron_base.py | 11 +++++++---- .../src/python_workflow_definition/shared.py | 12 ++++++++++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/python_workflow_definition/src/python_workflow_definition/jobflow.py b/python_workflow_definition/src/python_workflow_definition/jobflow.py index 9a5a192..f7e3baf 100644 --- a/python_workflow_definition/src/python_workflow_definition/jobflow.py +++ b/python_workflow_definition/src/python_workflow_definition/jobflow.py @@ -331,7 +331,10 @@ def write_workflow_json(flow: Flow, file_name: str = "workflow.json"): nodes_store_lst.append({"id": k, "type": "input", "value": v}) with open(file_name, "w") as f: - json.dump(update_node_names(content={ - NODES_LABEL: nodes_store_lst, - EDGES_LABEL: edges_lst - }), f, indent=2) + json.dump( + update_node_names( + content={NODES_LABEL: nodes_store_lst, EDGES_LABEL: edges_lst} + ), + f, + indent=2, + ) diff --git a/python_workflow_definition/src/python_workflow_definition/pyiron_base.py b/python_workflow_definition/src/python_workflow_definition/pyiron_base.py index 2a85340..77ab6e7 100644 --- a/python_workflow_definition/src/python_workflow_definition/pyiron_base.py +++ b/python_workflow_definition/src/python_workflow_definition/pyiron_base.py @@ -292,7 +292,10 @@ def write_workflow_json( nodes_store_lst.append({"id": k, "type": "input", "value": v}) with open(file_name, "w") as f: - json.dump(update_node_names(content={ - NODES_LABEL: nodes_store_lst, - EDGES_LABEL: edges_new_lst - }), f, indent=2) + json.dump( + update_node_names( + content={NODES_LABEL: nodes_store_lst, EDGES_LABEL: edges_new_lst} + ), + f, + indent=2, + ) diff --git a/python_workflow_definition/src/python_workflow_definition/shared.py b/python_workflow_definition/src/python_workflow_definition/shared.py index c613c03..8e642fe 100644 --- a/python_workflow_definition/src/python_workflow_definition/shared.py +++ b/python_workflow_definition/src/python_workflow_definition/shared.py @@ -50,7 +50,15 @@ def update_node_names(content: dict) -> dict: node_names_final_dict = {} input_nodes = [n for n in content[NODES_LABEL] if n["type"] == "input"] node_names_dict = { - n["id"]: list(set([e[TARGET_PORT_LABEL] for e in content[EDGES_LABEL] if e[SOURCE_LABEL] == n["id"]]))[0] + n["id"]: list( + set( + [ + e[TARGET_PORT_LABEL] + for e in content[EDGES_LABEL] + if e[SOURCE_LABEL] == n["id"] + ] + ) + )[0] for n in input_nodes } @@ -66,4 +74,4 @@ def update_node_names(content: dict) -> dict: for n in content[NODES_LABEL]: if n["type"] == "input": n["name"] = node_names_final_dict[n["id"]] - return content \ No newline at end of file + return content