File size: 2,699 Bytes
0dd9df7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""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])