"""Render v6 confuser trie images for the n50 and n20 counterpart datasets. Reuses the canonical renderer (distractor_generation_2/visualize.py) exactly like visualize_v5.py, but over the v6 level subsets. Calls are byte-identical to v5, so the n100 trie is unchanged (see trie_v5.*); these are the matching sub-tries for n50 / n20. Outputs (into this folder): trie_v6_n50.{txt,svg,png}, trie_v6_n20.{txt,svg,png} Run: python -u temp/story_remediation/unbundle/visualize_v6_levels.py """ from __future__ import annotations import json, sys from pathlib import Path HERE = Path(__file__).resolve().parent ROOT = HERE.parents[2] sys.path.insert(0, str(ROOT)) from models.trie import Trie # noqa: E402 from distractor_generation_2.visualize import ( # noqa: E402 _load_names, _text_tree, _render_image) N100 = ROOT / "distractor_generation_2" / "datasets" / "n100" REAL_APIS = ROOT / "data" / "tau-2" / "processed" / "apis.jsonl" OUT = HERE / "out" LEVELS = {"n50": OUT / "trajectories_all_v6_n50.jsonl", "n20": OUT / "trajectories_all_v6_n20.jsonl"} def seqs_from(path: Path): out = [] for line in path.read_text(encoding="utf-8").splitlines(): if not line.strip(): continue s = [c["name"] for c in json.loads(line).get("calls", []) if c.get("name")] if s: out.append(s) return out def build(seqs): t = Trie() for s in seqs: t.insert(list(s)) return t def stats(trie, classify): lines, st, _ = _text_tree(trie.root, classify, 1) depths = [] def walk(node, d): if not node.children: depths.append(d); return for c in node.children.values(): walk(c, d + 1) walk(trie.root, 0) return dict(nodes=st["nodes"], max_depth=st["max_depth"], root_branch=len(trie.root.children), leaves=len(depths), mean_leaf_depth=sum(depths) / max(len(depths), 1)), lines def main(): try: sys.stdout.reconfigure(encoding="utf-8", errors="replace") except Exception: pass real_names = _load_names(REAL_APIS) conf_names = _load_names(N100 / "apis.jsonl") def classify(name: str) -> str: if name in conf_names: return "confuser" if name in real_names: return "real" return "?" for level, path in LEVELS.items(): seqs = seqs_from(path) trie = build(seqs) st, lines = stats(trie, classify) stem = HERE / f"trie_v6_{level}" header = [ f"v6 UNBUNDLED CONFUSER TRIE ({level}) — subset of the finalized v6 dataset", "=" * 72, f"rows: {len(seqs)} nodes: {st['nodes']} max depth: {st['max_depth']} " f"root branches: {st['root_branch']} mean leaf depth: {st['mean_leaf_depth']:.2f}", "Legend: [real] BLUE = real prefix the confuser anchors to; " "[confuser] ORANGE = retail look-alike (the divergence node F).", "=" * 72, "", ] stem.with_suffix(".txt").write_text("\n".join(header + lines), encoding="utf-8") png = _render_image(trie.root, classify, stem, 1, "png") svg = _render_image(trie.root, classify, stem, 1, "svg") print(f"{level}: rows={len(seqs)} nodes={st['nodes']} max_depth={st['max_depth']} " f"root_branch={st['root_branch']} mean_leaf_depth={st['mean_leaf_depth']:.2f}") print(f" txt -> {stem.with_suffix('.txt').name} png -> {png} svg -> {svg}") if __name__ == "__main__": main()