File size: 3,021 Bytes
4415131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a457ecc
4415131
 
 
 
 
 
 
 
 
 
 
 
 
 
a457ecc
4415131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Load the screening-ceiling dataset.

Stdlib by default so the data is usable with nothing installed; pandas and
`datasets` are optional conveniences, imported only if you ask for them.

    from loader import load_regions, load_counterexamples, load_theorem

    theorem = load_theorem()
    print(theorem["statement"])

    for c in load_counterexamples():
        print(c["case_id"], c["k_predicted"])
"""
from __future__ import annotations

import json
import pathlib

HERE = pathlib.Path(__file__).resolve().parent
DATA = HERE / "data"

__all__ = ["load_regions", "load_counterexamples", "load_theorem",
           "to_pandas", "to_hf_dataset", "family_layout"]


def _jsonl(path: pathlib.Path) -> list[dict]:
    return [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line.strip()]


def load_regions() -> list[dict]:
    """256 certified regions of the branch-and-bound partition."""
    return _jsonl(DATA / "certified_regions.jsonl")


def load_counterexamples() -> list[dict]:
    """Concrete layouts where a second-order Born extractor predicts k > 1."""
    return _jsonl(DATA / "counterexamples.jsonl")


def load_theorem() -> dict:
    """The universal claim, its scope, and the provenance of the source proof."""
    return json.loads((DATA / "theorem.json").read_text(encoding="utf-8"))


def family_layout(d0_um: float, pt_mult: float, sep_mult: float, jog_mult: float):
    """Build the conductor geometry for a point in the family box.

    Returns (xy_metres, radius_metres). Kept here as well as in `verify.py` so a
    reader who only wants to generate layouts does not have to read the checker.
    """
    pt = 1.6 * d0_um * pt_mult * 1e-6
    sep = pt * sep_mult
    jog = jog_mult * sep
    return ([[0.0, 0.0], [pt, 0.0], [sep, jog], [sep + pt, jog]],
            [d0_um * 1e-6 / 2.0] * 4)


def to_pandas(split: str = "regions"):
    """`regions` or `counterexamples` as a DataFrame. Requires pandas."""
    import pandas as pd
    if split == "regions":
        rows = []
        for r in load_regions():
            flat = {k: v for k, v in r.items() if k != "bounds"}
            for name, b in r["bounds"].items():
                flat[f"{name}_lo"], flat[f"{name}_hi"] = b["lo"], b["hi"]
            rows.append(flat)
        return pd.DataFrame(rows)
    if split == "counterexamples":
        return pd.DataFrame(load_counterexamples())
    raise ValueError(f"unknown split {split!r}: use 'regions' or 'counterexamples'")


def to_hf_dataset():
    """Both splits as a `datasets.DatasetDict`. Requires `datasets`."""
    from datasets import Dataset, DatasetDict
    return DatasetDict({
        "regions": Dataset.from_list(load_regions()),
        "counterexamples": Dataset.from_list(load_counterexamples()),
    })


if __name__ == "__main__":
    t = load_theorem()
    print(t["statement"])
    print(f"\nregions        {len(load_regions())}")
    print(f"counterexamples {len(load_counterexamples())}")
    print(f"\nscope: {t['honest_scope']}")