"""Create the Hub tables from the archived graphs and exact check records.""" import gzip import json from pathlib import Path def write_record(stream, row): stream.write(json.dumps(row, separators=(",", ":")) + "\n") def export_tables(root): source = json.loads((root / "results" / "edge_only_checks.json").read_text(encoding="utf-8")) data = root / "data" data.mkdir(exist_ok=True) with (data / "signings.jsonl").open("w", encoding="utf-8", newline="\n") as signings, \ (data / "edges.jsonl").open("w", encoding="utf-8", newline="\n") as edges: for name, checks in sorted(source.items(), reverse=True): archive = root / "examples" / name graph_id = name.removesuffix(".json.gz") with gzip.open(archive, "rt", encoding="utf-8") as stream: graph = json.load(stream) order = graph["vertices"] graph_edges = graph["edges"] labels = graph["negative_edge_indices"].keys() if (order != checks["vertices"] or len(graph_edges) != checks["edges"] or labels != checks["above_five_halves"].keys() or labels != checks["signed_shortest_cycle_sums"].keys()): raise ValueError(f"Archive and exact check records disagree on {graph_id}.") for index, (u, v) in enumerate(graph_edges): write_record(edges, dict(graph_id=graph_id, edge_index=index, u=u, v=v)) for label, flips in graph["negative_edge_indices"].items(): negative = sorted(flips) if len(set(negative)) != len(negative) or any( not 0 <= i < len(graph_edges) for i in negative): raise ValueError(f"Invalid negative edge indices in {graph_id}/{label}.") upper_tail = checks["above_five_halves"][label] write_record(signings, dict( graph_id=graph_id, signing_id=label, vertices=order, edges=len(graph_edges), girth=checks["girth"], shortest_cycles=checks["shortest_cycles"], signed_shortest_cycle_sum=checks["signed_shortest_cycle_sums"][label], threshold_numerator=5, threshold_denominator=2, eigenvalues_above_threshold=upper_tail, signing_feasible_at_threshold=upper_tail == 0, negative_edge_indices=negative, edge_archive=f"examples/{name}", )) if __name__ == "__main__": export_tables(Path(__file__).resolve().parents[1])