From e13b66baeef569b4e80cd6b61bb5b875e14d3dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jan=C3=9Fen?= Date: Sat, 12 Apr 2025 11:07:55 +0200 Subject: [PATCH 1/3] Add plotting function --- .../src/python_workflow_definition/plot.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 python_workflow_definition/src/python_workflow_definition/plot.py diff --git a/python_workflow_definition/src/python_workflow_definition/plot.py b/python_workflow_definition/src/python_workflow_definition/plot.py new file mode 100644 index 0000000..aa106e6 --- /dev/null +++ b/python_workflow_definition/src/python_workflow_definition/plot.py @@ -0,0 +1,36 @@ +import json + +from IPython.display import SVG, display +import networkx as nx + + +from python_workflow_definition.shared import get_kwargs, convert_nodes_list_to_dict +from python_workflow_definition.purepython import group_edges + + +def plot(json_file_name): + with open(json_file_name, "r") as f: + content = json.load(f) + + graph = nx.DiGraph() + node_dict = convert_nodes_list_to_dict(nodes_list=content["nodes"]) + total_lst = group_edges(edges_lst=content["edges"]) + + for node_id, node_name in node_dict.items(): + graph.add_node(node_id, name=str(node_name), label=str(node_name)) + + for edge_tuple in total_lst: + target_node, edge_dict = edge_tuple + edge_label_dict = {} + for k, v in edge_dict.items(): + if v["source"] not in edge_label_dict: + edge_label_dict[v["source"]] = [] + if v["sourcePort"] is None: + edge_label_dict[v["source"]].append(k) + else: + edge_label_dict[v["source"]].append(v["sourcePort"] + "=" + k) + for k, v in edge_label_dict.items(): + graph.add_edge(str(k), str(target_node), label=", ".join(v)) + + svg = nx.nx_agraph.to_agraph(graph).draw(prog="dot", format="svg") + display(SVG(svg)) From 2ef0f010240e3956bc4cecfe1763cd021cfadf90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jan=C3=9Fen?= Date: Sat, 12 Apr 2025 12:40:43 +0200 Subject: [PATCH 2/3] Plot notebooks --- environment.yml | 1 + .../src/python_workflow_definition/plot.py | 31 ++++++++++++------- universal_workflow_nfdi.ipynb | 19 ++++++++++++ universal_workflow_qe.ipynb | 19 ++++++++++++ universal_workflow_simple.ipynb | 19 ++++++++++++ 5 files changed, 77 insertions(+), 12 deletions(-) diff --git a/environment.yml b/environment.yml index 5f7edb0..f58de08 100644 --- a/environment.yml +++ b/environment.yml @@ -7,3 +7,4 @@ dependencies: - pygraphviz=1.14 - aiida-workgraph=0.5.1 - conda_subprocess =0.0.6 +- networkx=3.4.2 \ No newline at end of file diff --git a/python_workflow_definition/src/python_workflow_definition/plot.py b/python_workflow_definition/src/python_workflow_definition/plot.py index aa106e6..94853d5 100644 --- a/python_workflow_definition/src/python_workflow_definition/plot.py +++ b/python_workflow_definition/src/python_workflow_definition/plot.py @@ -4,17 +4,24 @@ import networkx as nx -from python_workflow_definition.shared import get_kwargs, convert_nodes_list_to_dict from python_workflow_definition.purepython import group_edges - - -def plot(json_file_name): - with open(json_file_name, "r") as f: +from python_workflow_definition.shared import ( + get_kwargs, + convert_nodes_list_to_dict, + NODES_LABEL, + EDGES_LABEL, + SOURCE_LABEL, + SOURCE_PORT_LABEL, +) + + +def plot(file_name): + with open(file_name, "r") as f: content = json.load(f) graph = nx.DiGraph() - node_dict = convert_nodes_list_to_dict(nodes_list=content["nodes"]) - total_lst = group_edges(edges_lst=content["edges"]) + node_dict = convert_nodes_list_to_dict(nodes_list=content[NODES_LABEL]) + total_lst = group_edges(edges_lst=content[EDGES_LABEL]) for node_id, node_name in node_dict.items(): graph.add_node(node_id, name=str(node_name), label=str(node_name)) @@ -23,12 +30,12 @@ def plot(json_file_name): target_node, edge_dict = edge_tuple edge_label_dict = {} for k, v in edge_dict.items(): - if v["source"] not in edge_label_dict: - edge_label_dict[v["source"]] = [] - if v["sourcePort"] is None: - edge_label_dict[v["source"]].append(k) + if v[SOURCE_LABEL] not in edge_label_dict: + edge_label_dict[v[SOURCE_LABEL]] = [] + if v[SOURCE_PORT_LABEL] is None: + edge_label_dict[v[SOURCE_LABEL]].append(k) else: - edge_label_dict[v["source"]].append(v["sourcePort"] + "=" + k) + edge_label_dict[v[SOURCE_LABEL]].append(v[SOURCE_PORT_LABEL] + "=" + k) for k, v in edge_label_dict.items(): graph.add_edge(str(k), str(target_node), label=", ".join(v)) diff --git a/universal_workflow_nfdi.ipynb b/universal_workflow_nfdi.ipynb index 2ae6836..894bb02 100644 --- a/universal_workflow_nfdi.ipynb +++ b/universal_workflow_nfdi.ipynb @@ -7,6 +7,25 @@ "# Load Simple Workflow" ] }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "## Plot" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "from python_workflow_definition.plot import plot" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "plot(file_name='workflow_nfdi.json')" + }, { "cell_type": "markdown", "metadata": {}, diff --git a/universal_workflow_qe.ipynb b/universal_workflow_qe.ipynb index b0fde02..182229d 100644 --- a/universal_workflow_qe.ipynb +++ b/universal_workflow_qe.ipynb @@ -7,6 +7,25 @@ "# Load Quantum Espresso Energy Volume Curve Workflow" ] }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "## Plot" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "from python_workflow_definition.plot import plot" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "plot(file_name='workflow_qe.json')" + }, { "cell_type": "markdown", "metadata": {}, diff --git a/universal_workflow_simple.ipynb b/universal_workflow_simple.ipynb index 5e011c3..aaf8d2e 100644 --- a/universal_workflow_simple.ipynb +++ b/universal_workflow_simple.ipynb @@ -7,6 +7,25 @@ "# Load Simple Workflow" ] }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "## Plot" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "from python_workflow_definition.plot import plot" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "plot(file_name='workflow_simple.json')" + }, { "cell_type": "markdown", "metadata": {}, From 28a000e713c47fce0629f439ec840f51d0da61bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jan=C3=9Fen?= Date: Sat, 12 Apr 2025 12:44:16 +0200 Subject: [PATCH 3/3] add empty line at the end of environment --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index f58de08..ee30024 100644 --- a/environment.yml +++ b/environment.yml @@ -7,4 +7,4 @@ dependencies: - pygraphviz=1.14 - aiida-workgraph=0.5.1 - conda_subprocess =0.0.6 -- networkx=3.4.2 \ No newline at end of file +- networkx=3.4.2