text
stringlengths
0
1.47k
# Parent's level on the board → level the children are recorded as
NEXT_LEVEL = {"country": "admin1", "admin1": "admin2", "admin2": "district", "city": "district", "district": "area",
"area": "road", "road": "point"}
def _child_level(b: dict, args, gz_out: str) -> tuple[str, str]:
"""Candidate level for children: use --as-level if given; parent is a country → admin1; parent already on the board → its next level
(where a province's next level down is directly districts, as with a municipality, record as district); otherwise → district."""
if args.as_level:
return args.as_level, "set by --as-level"
m = re.search(r"admin_level (\d+)\) children admin_level (\d+)", gz_out)
p_lv, c_lv = (int(m.group(1)), int(m.group(2))) if m else (None, None)
if p_lv is not None and p_lv <= 2:
# When jumping straight from a country to a finer level (--level 5/6), don't record as province level
lv = "admin1" if c_lv is None or c_lv <= 4 else ("admin2" if c_lv == 5 else "district")
return lv, f"parent is a country, children admin_level {c_lv}"
hits = [k for k in b["candidates"] if _norm(k) == _norm(args.parent)]
if len(hits) == 1:
lv = b["candidates"][hits[0]]["level"]
nxt = NEXT_LEVEL.get(lv, "district")
if lv == "admin1" and p_lv is not None and c_lv is not None and c_lv >= p_lv + 2:
nxt = "district"
return nxt, f"parent {hits[0]} is {lv} on the candidate board"
return "district", "default"
def cmd_clue(args, p: Path) -> None:
b = _load(p)
kid = f"K{len(b['clues']) + 1}"
if args.status not in STATUS:
sys.exit(f"--status must be one of {STATUS}: observed=shape/color seen directly in the image, read=text/numbers read, inferred=inference (building about 8 floors, road going uphill), computed=computed by a script")
resolves = list(dict.fromkeys(getattr(args, "resolves", None) or []))
output = args.file or ""
if resolves:
if args.status != "computed":
sys.exit("--resolves requires --status computed: link a result that actually tests the named clues")
unknown = [k for k in resolves if k not in b["clues"]]
if unknown:
sys.exit(f"Unknown clues in --resolves: {', '.join(unknown)}")
if not output or not Path(output).is_file():
sys.exit("--resolves requires --file pointing to an existing computed output file")
output = str(Path(output).resolve())
kind = _clue_kind(args.kind)
b["clues"][kid] = {"text": args.text, "kind": kind, "status": args.status, "file": output,
"source": args.source or "", "used": False}
if resolves:
b["clues"][kid]["resolves"] = resolves
_log(b, f"clue {kid} [{kind}/{args.status}] {args.text}" + (f"; resolves {', '.join(resolves)}" if resolves else ""))
_save(p, b)
cap = LR_CAP[args.status]
print(f"{kid} recorded. Status {args.status}: likelihood ratio cap {cap:g}" + (", can only rank, can't exclude" if args.status in ("inferred", "observed") else ", usable for exclude (still needs a computed file)"))
def _parse_lr(items: list[str] | None, default: float | None) -> list[tuple[str, float]]:
out = []
for it in items or []:
if ":" in it:
name, v = it.rsplit(":", 1)
out.append((name, float(v)))
elif default is not None:
out.append((it, default))
else:
sys.exit(f"Write it as name:likelihood_ratio, e.g. {it}:5")
return out
def cmd_evidence(args, p: Path) -> None:
b = _load(p)
if args.clue not in b["clues"]:
sys.exit(f"No clue {args.clue}; run `board.py clue` first")
cl = b["clues"][args.clue]
cap = LR_CAP[cl["status"]]
pairs = _parse_lr(args.for_, 3.0) + [(n, v) for n, v in _parse_lr(args.against, 1 / 3)]
if not pairs:
sys.exit("Give at least one --for name:likelihood_ratio or --against name:likelihood_ratio")
clipped = []
for name, lr in pairs:
cname = _find(b, name)
if lr <= 0:
sys.exit("A likelihood ratio of 0 means exclusion; use `board.py exclude` (needs a computed file)")
lr2 = min(max(lr, 1 / cap), cap)
if abs(lr2 - lr) > 1e-9:
clipped.append(f"{cname}:{lr:g}→{lr2:g}")
b["evidence"].append({"id": f"E{len(b['evidence']) + 1}", "clue": args.clue, "candidate": cname, "lr": lr2,
"why": args.why or "", "file": args.file or "", "cmd": args.command_ or ""})
cl["used"] = True
if args.file and not Path(args.file).exists():
print(f"Note: {args.file} doesn't exist; evidence files must be real outputs of this session")
_log(b, f"evidence {args.clue} → {', '.join(f'{n}:{v:g}' for n, v in pairs)}")
_save(p, b)
if clipped:
print(f"Clue {args.clue} is \"{cl['status']}\", likelihood ratio clamped to 1/{cap:g}–{cap:g}: {'; '.join(clipped)}. "
f"For more weight, first verify the clue as read/computed (read the text, run a script), then record a new clue.")
print("Recorded." + ("" if args.why else " Suggest adding --why to state the basis."))
def cmd_exclude(args, p: Path) -> None:
b = _load(p)
cname = _find(b, args.name)
if args.clue not in b["clues"]: