screening-ceiling / loader.py
nickh007's picture
Give every text read an explicit encoding
a457ecc verified
Raw
History Blame Contribute Delete
3.02 kB
"""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']}")