diff --git a/misc/all-graph/02-dump_igraph.py b/misc/all-graph/02-dump_igraph.py index bbb608a..9a15cf0 100755 --- a/misc/all-graph/02-dump_igraph.py +++ b/misc/all-graph/02-dump_igraph.py @@ -5,7 +5,7 @@ import json import igraph as ig -def dump_igraph(graph: dict[str, dict]) -> ig.Graph: +def dump_graph(graph: dict[str, dict]) -> ig.Graph: index_map = {x: i for i, x in enumerate(graph)} g = ig.Graph(len(graph)) for index, (layout, info) in enumerate(graph.items()): @@ -15,13 +15,38 @@ def dump_igraph(graph: dict[str, dict]) -> ig.Graph: return g +def dump_sub_graph(graph: dict[str, dict], target: str) -> ig.Graph: + cases = [x for x, info in graph.items() if target in info['pivots']] + assert sorted(cases) == cases + + index_map = {x: i for i, x in enumerate(cases)} + g = ig.Graph(len(cases)) + + for index, layout in enumerate(cases): + info = graph[layout] + g.vs[index]['code'] = layout + g.vs[index]['step'] = info['step'] + + for x in info['next']: + assert x in index_map + g.add_edge(index, index_map[x]) + + return g + + def convert_ig(file: str, output: str) -> None: raw = json.loads(open(file).read()) - dump_igraph(raw['graph']).write_pickle(output) + g_main = dump_graph(raw['graph']) + g_main.write_pickle(f'{output}.pkl') + + for layout, sub_tag in raw['targets'].items(): + print(layout, sub_tag) + g = dump_sub_graph(raw['graph'], layout) + g.write_pickle(f'{output}-{sub_tag}_{layout}.pkl') if __name__ == '__main__': for name in sorted(os.listdir('output-json')): name = name.removesuffix('.json') print(name) - convert_ig(f'output-json/{name}.json', f'output-ig/{name}.pickle') + convert_ig(f'output-json/{name}.json', f'output-ig/{name}') diff --git a/misc/all-graph/compare_ig.py b/misc/all-graph/compare_ig.py deleted file mode 100644 index 828b60a..0000000 --- a/misc/all-graph/compare_ig.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python3 - -import os -import igraph as ig - - -def compare(file_1: str, file_2: str) -> None: - print(f'{file_1} vs {file_2}') - g1 = ig.Graph.Read_Pickle(file_1) - g2 = ig.Graph.Read_Pickle(file_2) - - assert g1.vcount() == g2.vcount() - assert g1.ecount() == g2.ecount() - assert g1.isomorphic(g2) - - for edge in g1.es: - assert edge.attributes() == {} - - for edge in g2.es: - assert edge.attributes() == {} - - for i in range(g1.vcount()): - assert g1.vs[i].attributes() == g2.vs[i].attributes() - - -if __name__ == '__main__': - for name in sorted(os.listdir('output-ig')): - compare(f'output/{name}', f'output-ig/{name}') diff --git a/misc/all-graph/compare_json.py b/misc/all-graph/compare_json.py deleted file mode 100644 index 746ea06..0000000 --- a/misc/all-graph/compare_json.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python3 - -import os -import json - - -def format_1(raw: str) -> str: - graph = json.loads(raw) - assert sorted(graph) == list(graph) - for layout, info in graph.items(): - info['next'] = sorted(info['next']) - return json.dumps(graph) - - -def format_2(raw: str) -> str: - graph = json.loads(raw)['graph'] - for layout, info in graph.items(): - graph[layout] = {'step': info['step'], 'next': info['next']} - return json.dumps(graph) - - -def compare(file_1: str, file_2: str) -> None: - print(f'{file_1} vs {file_2}') - data_1 = format_1(open(file_1).read()) - data_2 = format_2(open(file_2).read()) - assert data_1 == data_2 - - -if __name__ == '__main__': - for name in sorted(os.listdir('output-json')): - compare(f'output/{name}', f'output-json/{name}')