Add dataset-stats plot script
Browse files- plot_dataset_stats.py +94 -0
plot_dataset_stats.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Render a composition graph for a Wordmaze config, for the dataset card.
|
| 2 |
+
|
| 3 |
+
uv run plot_dataset_stats.py [data/wordmaze-m4-6/metadata.json] [-o dataset_stats.png]
|
| 4 |
+
|
| 5 |
+
Reads the metadata.json a generation run writes and produces two panels: puzzle counts
|
| 6 |
+
across (word length x move count), and the vocabulary -> ladder-graph sizes per length.
|
| 7 |
+
"""
|
| 8 |
+
# /// script
|
| 9 |
+
# requires-python = ">=3.11"
|
| 10 |
+
# dependencies = ["matplotlib>=3.7", "numpy>=1.24"]
|
| 11 |
+
# ///
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
import argparse
|
| 15 |
+
import json
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
|
| 18 |
+
import matplotlib.pyplot as plt
|
| 19 |
+
import numpy as np
|
| 20 |
+
from matplotlib import rcParams
|
| 21 |
+
|
| 22 |
+
INDIGO = "#6366f1"
|
| 23 |
+
INDIGO_DK = "#4f46e5"
|
| 24 |
+
INDIGO_LT = "#a5a6f6"
|
| 25 |
+
GRAY = "#9ca3af"
|
| 26 |
+
INK = "#1a1a1a"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def main() -> None:
|
| 30 |
+
here = Path(__file__).resolve().parent
|
| 31 |
+
ap = argparse.ArgumentParser(description="Plot Wordmaze dataset composition.")
|
| 32 |
+
ap.add_argument("metadata", nargs="?", default=str(here / "data" / "wordmaze-m4-6" / "metadata.json"))
|
| 33 |
+
ap.add_argument("-o", "--out", default=None, help="Output PNG (default: next to metadata.json).")
|
| 34 |
+
args = ap.parse_args()
|
| 35 |
+
|
| 36 |
+
meta = json.loads(Path(args.metadata).read_text())
|
| 37 |
+
out = Path(args.out) if args.out else Path(args.metadata).with_name("dataset_stats.png")
|
| 38 |
+
slug = meta.get("move_count_slug", "wordmaze")
|
| 39 |
+
|
| 40 |
+
# examples_by_combo keys look like "4_letters/5_moves"
|
| 41 |
+
combo = meta.get("examples_by_combo", {})
|
| 42 |
+
lengths = sorted({int(k.split("_")[0]) for k in combo})
|
| 43 |
+
moves = sorted({int(k.split("/")[1].split("_")[0]) for k in combo})
|
| 44 |
+
wbl = {int(k): v for k, v in meta.get("words_by_length", {}).items()}
|
| 45 |
+
nodes = {int(k): v for k, v in meta.get("graph_nodes_by_length", {}).items()}
|
| 46 |
+
splits = meta.get("splits", {})
|
| 47 |
+
total = meta.get("generated_examples", sum(combo.values()))
|
| 48 |
+
|
| 49 |
+
rcParams.update({
|
| 50 |
+
"font.family": "DejaVu Sans", "font.size": 9.5,
|
| 51 |
+
"axes.spines.top": False, "axes.spines.right": False,
|
| 52 |
+
"axes.grid": True, "axes.axisbelow": True, "grid.color": "#ececec",
|
| 53 |
+
"axes.facecolor": "white", "figure.facecolor": "white",
|
| 54 |
+
"xtick.color": "#6b7280", "ytick.color": "#6b7280",
|
| 55 |
+
"xtick.major.size": 0, "ytick.major.size": 0,
|
| 56 |
+
})
|
| 57 |
+
shades = {lengths[i]: [INDIGO_LT, INDIGO, INDIGO_DK][i % 3] for i in range(len(lengths))}
|
| 58 |
+
|
| 59 |
+
fig, (axA, axB) = plt.subplots(1, 2, figsize=(11, 4.6))
|
| 60 |
+
|
| 61 |
+
# panel A: puzzles per (length x moves)
|
| 62 |
+
x = np.arange(len(moves))
|
| 63 |
+
w = 0.8 / max(1, len(lengths))
|
| 64 |
+
for i, L in enumerate(lengths):
|
| 65 |
+
vals = [combo.get(f"{L}_letters/{m}_moves", 0) for m in moves]
|
| 66 |
+
axA.bar(x + (i - (len(lengths) - 1) / 2) * w, vals, w, color=shades[L],
|
| 67 |
+
edgecolor="white", label=f"{L} letters", zorder=3)
|
| 68 |
+
axA.set_xticks(x)
|
| 69 |
+
axA.set_xticklabels([f"{m} moves" for m in moves])
|
| 70 |
+
axA.set_ylabel("puzzles")
|
| 71 |
+
split_txt = " · ".join(f"{k} {v}" for k, v in sorted(splits.items()))
|
| 72 |
+
axA.set_title(f"{total:,} puzzles, balanced across length x moves", fontsize=11, fontweight="bold", color=INK, pad=10)
|
| 73 |
+
axA.legend(fontsize=8.5, loc="lower center", ncol=len(lengths), frameon=False)
|
| 74 |
+
axA.set_ylim(0, max(combo.values()) * 1.25 if combo else 1)
|
| 75 |
+
|
| 76 |
+
# panel B: vocabulary -> graph nodes per length
|
| 77 |
+
xb = np.arange(len(lengths))
|
| 78 |
+
axB.bar(xb - 0.2, [wbl.get(L, 0) for L in lengths], 0.4, color=GRAY, edgecolor="white", label="words in dictionary", zorder=3)
|
| 79 |
+
axB.bar(xb + 0.2, [nodes.get(L, 0) for L in lengths], 0.4, color=INDIGO, edgecolor="white", label="words with a neighbor", zorder=3)
|
| 80 |
+
axB.set_xticks(xb)
|
| 81 |
+
axB.set_xticklabels([f"{L} letters" for L in lengths])
|
| 82 |
+
axB.set_ylabel("count")
|
| 83 |
+
axB.set_title("Vocabulary → ladder graph", fontsize=11, fontweight="bold", color=INK, pad=10)
|
| 84 |
+
axB.legend(fontsize=8.5, loc="upper right", frameon=False)
|
| 85 |
+
|
| 86 |
+
fig.suptitle(f"Wordmaze {slug} · wordfreq top-10k, proper nouns filtered",
|
| 87 |
+
fontsize=12.5, fontweight="bold", color=INK, y=1.02)
|
| 88 |
+
out.parent.mkdir(parents=True, exist_ok=True)
|
| 89 |
+
fig.savefig(out, dpi=200, bbox_inches="tight", facecolor="white")
|
| 90 |
+
print(f"saved {out}")
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
main()
|