Stable run ids (fingerprints) for the form and the register; tests for the site statistics (#25)
CI / test (pull_request) Successful in 43s
CI / test (pull_request) Successful in 43s
- collection/etl/runs.py fingerprints(): a form or register run's id is now <prefix>-<hash of the row's non-empty answers, timestamp included> instead of its row number, so deleting or re-sorting rows in the sheet moves no other run's id, and a column added later changes none. The 32 rows that are exact copies of another row get -2, -3 (identical, so which is which does not matter). A row edited in the sheet gets a new id. Every id in runs_clean.csv and overlap.json changes once; nothing else does (no statistic or model reads the ids). - tests/core/test_site_stats.py (9 tests): measures.yaml conditions, source groups and presets, bond denominators without deliberate deaths (cards, encounters, route map), the riders' paths, and a JSON-safe stats.json, on a fixed sample of the published runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ca411e8315
commit
1dfa8fb4c3
@@ -0,0 +1,127 @@
|
||||
"""The page's statistics (analysis/stats/site_stats.py): the conditions of measures.yaml, how runs are grouped by
|
||||
where they came from, the presets, deliberate deaths, and the invariants of stats.json on a sample of the real runs."""
|
||||
import random
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / "analysis/stats"))
|
||||
sys.path.insert(0, str(ROOT / "core"))
|
||||
import site_stats as SS # noqa: E402
|
||||
from atlas import runs as runs_io # noqa: E402
|
||||
|
||||
DEATHS = ("Incinerated", "Died before the hour")
|
||||
|
||||
|
||||
def sample(per_source=300, seed=7):
|
||||
"""A fixed sample of the published runs, every statistics source represented, impossible runs left out."""
|
||||
rng = random.Random(seed)
|
||||
runs = [r for r in runs_io.read(drop=()) if not runs_io.impossible(r)]
|
||||
out = []
|
||||
for s in sorted({r["source"] for r in runs}):
|
||||
rs = [r for r in runs if r["source"] == s]
|
||||
out += rng.sample(rs, min(per_source, len(rs)))
|
||||
return out
|
||||
|
||||
|
||||
class Conditions(unittest.TestCase):
|
||||
def test_match(self):
|
||||
r = {"hour": "Tree", "branch": "Shadow (rare)", "outcome": "Bonded", "_deliberate": False}
|
||||
self.assertTrue(SS.match(r, {"hour": "Tree"}))
|
||||
self.assertTrue(SS.match(r, {"hour": {"in": ["Tree", "Valley"]}, "branch": {"exists": True}}))
|
||||
self.assertFalse(SS.match(r, {"hour": {"not": "Tree"}}))
|
||||
self.assertTrue(SS.match(r, {"any": [{"hour": "Valley"}, {"outcome": "Bonded"}]}))
|
||||
self.assertFalse(SS.match({**r, "branch": None}, {"branch": {"exists": True}}))
|
||||
# deliberate deaths: `{not: true}` keeps runs that were never marked (a test row, another caller)
|
||||
self.assertTrue(SS.match(r, {"_deliberate": {"not": True}}))
|
||||
self.assertTrue(SS.match({"hour": "Tree"}, {"_deliberate": {"not": True}}))
|
||||
self.assertFalse(SS.match({**r, "_deliberate": True}, {"_deliberate": {"not": True}}))
|
||||
|
||||
def test_bond_pools_leave_out_deliberate_deaths(self):
|
||||
for t, T in SS.TARGETS.items():
|
||||
if t.startswith("bond_"):
|
||||
self.assertEqual(T["pool"].get("_deliberate"), {"not": True}, t)
|
||||
self.assertEqual(SS.OUTCOMES["bonded_Red"]["denom"].get("_deliberate"), {"not": True})
|
||||
|
||||
|
||||
class Sources(unittest.TestCase):
|
||||
def test_groups_and_presets(self):
|
||||
form = {"source": "reddit spreadsheet", "entry": "Form", "goal": "Red", "data_flags": ""}
|
||||
bulk = {"source": "reddit spreadsheet", "entry": "Data Import", "goal": "Red", "data_flags": ""}
|
||||
bot = {"source": "bot", "entry": None, "goal": "Black", "data_flags": ""}
|
||||
com = {"source": "reddit comment", "entry": None, "goal": None, "data_flags": "read from a Reddit comment"}
|
||||
self.assertEqual([SS.entry_group(r) for r in (form, bulk, bot, com)],
|
||||
["Form", "Data Import", "DragonKind bot", "Reddit comments"])
|
||||
self.assertEqual([SS.stratum(r) for r in (bulk, bot, com)], ["Bulk logs (imports)", "DragonKind bot", "Reddit comments"])
|
||||
runs = [form, bulk, bot, com]
|
||||
f = lambda **kw: SS.apply_filters(runs, {"source": "all", "goal": "all", "excludeFlagged": False, **kw}) # noqa: E731
|
||||
self.assertEqual(f(source="form"), [form])
|
||||
self.assertEqual(f(source="bulk"), [bulk]) # the bot and Reddit are not bulk logs
|
||||
self.assertEqual(f(source="bot"), [bot])
|
||||
self.assertEqual(f(source="reddit"), [com])
|
||||
self.assertEqual(f(goal="only_rare"), [bot])
|
||||
self.assertEqual(f(excludeFlagged=True), [form, bulk, bot])
|
||||
|
||||
|
||||
class Build(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
every = sample()
|
||||
runs = [r for r in every if r["source"] in SS.M["sources"]]
|
||||
for r in runs:
|
||||
r["_deliberate"] = SS.deliberate(r)
|
||||
cls.runs, cls.every = runs, every
|
||||
cls.out = SS.build(runs, len(runs), None, every_run=every)
|
||||
|
||||
def test_presets(self):
|
||||
self.assertEqual(set(self.out["presets"]), set(SS.M["presets"]))
|
||||
for pid, p in self.out["presets"].items():
|
||||
self.assertEqual(set(p), {"label", "analyse", "routes", "map", "valley", "overview"}, pid)
|
||||
self.assertEqual(set(self.out["presets"]["bot"]["analyse"]["meta"]["sources"]), {"DragonKind bot"})
|
||||
self.assertEqual(set(self.out["presets"]["reddit"]["analyse"]["meta"]["sources"]), {"Reddit comments"})
|
||||
|
||||
def test_overview_counts(self):
|
||||
o = self.out["presets"]["all"]["overview"]
|
||||
ended = [r for r in self.runs if r["outcome"] in ("Bonded",) + DEATHS]
|
||||
self.assertEqual(o["cadets"] + o["deliberate"], len(ended))
|
||||
self.assertEqual(o["deliberate"], sum(1 for r in ended if r["_deliberate"]))
|
||||
self.assertEqual(o["bonded"] + o["dead"], o["cadets"])
|
||||
|
||||
def test_map_bond_denominators_skip_deliberate_deaths(self):
|
||||
m = self.out["presets"]["all"]["map"]
|
||||
for i, nd in enumerate(m["nodes"]):
|
||||
for t, bt in m["by_target"].items():
|
||||
n = bt["nodes"][i]["n"]
|
||||
if t.startswith("bond_"):
|
||||
self.assertLessEqual(n, nd["n"])
|
||||
else:
|
||||
self.assertEqual(n, nd["n"], t)
|
||||
# somewhere in the sample a deliberate death reached the map, so some bond denominator is smaller
|
||||
self.assertTrue(any(bt["nodes"][i]["n"] < nd["n"] for t, bt in m["by_target"].items() if t.startswith("bond_")
|
||||
for i, nd in enumerate(m["nodes"])))
|
||||
|
||||
def test_encounters_leave_out_deliberate_deaths(self):
|
||||
for e, spec in zip(self.out["presets"]["all"]["analyse"]["encounters"], SS.M["encounters"]):
|
||||
col = spec["column"]
|
||||
n = sum(1 for r in self.runs if SS.match(r, spec["where"]) and r.get(col) in ("Bonded!", "Incinerated")
|
||||
and not r["_deliberate"])
|
||||
self.assertEqual(e["overall"]["n"], n, e["context"])
|
||||
|
||||
def test_paths(self):
|
||||
p = self.out["paths"]
|
||||
self.assertEqual([lay["key"] for lay in p["layers"]], [lay["key"] for lay in SS.M["path_layers"]])
|
||||
for lay in p["layers"]:
|
||||
self.assertEqual(sum(lay["counts"].values()), lay["runs"], lay["key"])
|
||||
self.assertEqual(set(lay["lines"]), {"lived", "chose", "failed"})
|
||||
bot = next(lay for lay in p["layers"] if lay["key"] == "bot")
|
||||
self.assertGreater(bot["counts"]["chose"], 0) # the bot burns on purpose: drawn as chosen deaths
|
||||
self.assertEqual(set(p["unrouted"]), set(SS.M.get("path_unrouted", [])))
|
||||
|
||||
def test_json_safe(self):
|
||||
import json
|
||||
json.dumps(self.out, allow_nan=False) # the page cannot parse NaN or Infinity
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user