From bc6792e9c2cb0474d529e87f114d0f2cd4573fd6 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Tue, 22 Feb 2022 14:21:01 +0100 Subject: [PATCH] add python script to generate morphisms json --- data/morphisms.dot | 2 ++ scripts/morphisms/connected_components.py | 41 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 scripts/morphisms/connected_components.py diff --git a/data/morphisms.dot b/data/morphisms.dot index b44d38c..8ba0d19 100644 --- a/data/morphisms.dot +++ b/data/morphisms.dot @@ -1,3 +1,4 @@ +digraph { "G^{14,14,48}_{0}" -> "G^{14,14,24}_{1}" "G^{14,14,48}_{0}" -> "G^{14,14,16}_{1}" "G^{14,14,48}_{1}" -> "G^{14,14,24}_{0}" @@ -302,3 +303,4 @@ "G^{54,54,54}_{2}" -> "G^{18,54,54}_{0}" "G^{54,54,54}_{2}" -> "G^{18,54,54}_{8}" "G^{54,54,54}_{2}" -> "G^{18,54,54}_{2}" +} diff --git a/scripts/morphisms/connected_components.py b/scripts/morphisms/connected_components.py new file mode 100644 index 0000000..3f74cce --- /dev/null +++ b/scripts/morphisms/connected_components.py @@ -0,0 +1,41 @@ +import networkx +import json +import os + +DATA_DIR = os.path.join("..", "..", "data") + +MORPHISMS_FILE = os.path.join(DATA_DIR, "morphisms.dot") +MORPHISMS_JSON = os.path.join(DATA_DIR, "triangle_groups_morphisms.json") + +def level(G, node): + parents = list(G.predecessors(node)) + if len(parents) == 0: + return 0 + else: + return 1 + max([level(G, n) for n in parents]) + +def nodes_from_component(G, cc, graph_json): + subG = networkx.induced_subgraph(G, cc) + assert networkx.is_weakly_connected(subG) + + root = [n for n,d in subG.in_degree() if d==0][0] + + for node in graph_json["nodes"]: + if node["id"] in subG: + print(node) + n = node["id"] + node["level"] = level(G, n) + node["component_id"] = root + + return graph_json + +G = networkx.nx_pydot.read_dot(MORPHISMS_FILE) +G_json = networkx.node_link_data(G) +G_components = networkx.weakly_connected_components(G) + +for cc in G_components: + nodes_from_component(G, cc, G_json) + +with open(MORPHISMS_JSON, "w") as file: + print("writing to ", MORPHISMS_JSON, "\n") + json.dump(G_json, file, indent=4)