text stringlengths 0 1.47k |
|---|
if not hits: |
sys.exit(f"No candidate \"{name}\": add it or use children first (existing: {', '.join(list(b['candidates'])[:12])}…)") |
sys.exit(f"\"{name}\" matches several candidates: {hits}; write the full name") |
def _log(b: dict, text: str) -> None: |
b.setdefault("log", []).append(f"{datetime.now():%H:%M} {text}") |
def _run(cmd: list[str]) -> subprocess.CompletedProcess: |
"""Run gazetteer.py, clues.py. Child scripts and this one both use UTF-8: Chinese Windows reads and writes GBK by default, and if the two sides differ you get mojibake or crashes.""" |
return subprocess.run(cmd, text=True, encoding="utf-8", errors="replace", capture_output=True, |
env={**os.environ, "PYTHONUTF8": "1"}) |
def _bbox_km2(bb: list[float]) -> float: |
s, w, n, e = bb |
return abs(n - s) * 110.574 * abs(e - w) * 111.320 * math.cos(math.radians((s + n) / 2)) |
def _pages(bb: list[float], zoom: int, cell: int, cols: int) -> int: |
s, w, n, e = bb |
mpp = 156543.03392 * math.cos(math.radians((s + n) / 2)) / 2 ** zoom |
step = cell * mpp * 0.9 |
r = max(1, math.ceil((n - s) * 110574 / step)) |
c = max(1, math.ceil((e - w) * 111320 * math.cos(math.radians((s + n) / 2)) / step)) |
return math.ceil(r * c / (cols * cols)) |
def _inherited(b: dict, name: str, own_clues: set[str]) -> list[dict]: |
"""Support flows up, refutation doesn't: a point that matches supports its road, a point that doesn't match says nothing about the rest of the road. |
Per clue the strongest supporting evidence among descendants, for clues the candidate has no evidence of its own.""" |
kids: dict[str, list[str]] = {} |
for n, c in b["candidates"].items(): |
if c.get("parent") and c.get("status") != "excluded": |
kids.setdefault(c["parent"], []).append(n) |
seen, todo, best = {name}, list(kids.get(name, [])), {} |
while todo: |
n = todo.pop() |
if n in seen: |
continue |
seen.add(n) |
todo += kids.get(n, []) |
for e in b["evidence"]: |
if e["candidate"] == n and e["lr"] > 1 and e["clue"] not in own_clues and e["lr"] > best.get(e["clue"], {}).get("lr", 1): |
best[e["clue"]] = {**e, "via": n} |
return list(best.values()) |
def _scores(b: dict, level: str, prior_by: str) -> list[dict]: |
rows = [] |
for name, c in b["candidates"].items(): |
if c["level"] != level: |
continue |
prior = 1.0 |
if prior_by == "area" and c.get("bbox"): |
prior = max(_bbox_km2(c["bbox"]), 1.0) |
logit = math.log(prior) + math.log(max(c.get("prior", 1.0), 1e-9)) |
ev = [e for e in b["evidence"] if e["candidate"] == name] |
ev += _inherited(b, name, {e["clue"] for e in ev}) |
for e in ev: |
logit += math.log(max(e["lr"], 1e-9)) |
rows.append({"name": name, "logit": logit, "n_ev": len(ev), |
"n_verified": sum(1 for e in ev if b["clues"][e["clue"]]["status"] in ("read", "computed")), |
"excluded": c.get("status") == "excluded", "c": c, "ev": ev}) |
open_rows = [r for r in rows if not r["excluded"]] |
if open_rows: |
m = max(r["logit"] for r in open_rows) |
z = sum(math.exp(r["logit"] - m) for r in open_rows) |
for r in open_rows: |
r["share"] = math.exp(r["logit"] - m) / z |
for r in rows: |
r.setdefault("share", 0.0) |
rows.sort(key=lambda r: (r["excluded"], -r["share"])) |
return rows |
def _frontier(b: dict) -> str | None: |
"""The finest level that still has ≥2 non-excluded candidates; if none, the finest level that has candidates.""" |
for lv in reversed(LEVELS): |
if sum(1 for c in b["candidates"].values() if c["level"] == lv and c.get("status") != "excluded") >= 2: |
return lv |
for lv in reversed(LEVELS): |
if any(c["level"] == lv and c.get("status") != "excluded" for c in b["candidates"].values()): |
return lv |
return None |
def _cost_of(c: dict, args) -> tuple[int | None, str]: |
bb = c.get("scan_bbox") or c.get("bbox") |
if not bb: |
return None, "no extent" |
pages = _pages(bb, args.zoom, args.cell, args.cols) |
return pages, ("built-up area" if c.get("scan_bbox") else "whole-district bbox") |
# ---------------------------------------------------------------- subcommands |
def cmd_init(args, p: Path) -> None: |
if p.exists() and not args.force: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.