"""Algebra version of LFM3's .empryo/tmp/prep_attack_inputs.py (math part only; book inputs are copied unchanged). Same scheme: per level, kind-proportional stratified sample (50 test / 10 dev per level, seeds 100+lvl / 200+lvl), translate = first 20 test rows of each level, retain = seeded shuffle per (topic, level) of the non-target topics. File names keep the LFM3 'math_calc_*' names so the audit code needs no path change; topic is arch.MATH_TARGET=algebra. """ import collections import datetime as dt import hashlib import json import random from pathlib import Path D = Path("G:/lexsi/lexsi-unlearning/data") # frozen data (read-only) O = Path("attack_data/_inputs") TARGET = "algebra" RETAIN = ("arithmetic", "precalculus", "calculus") RETAIN_PER_LEVEL = {1: 7, 2: 7, 3: 6} # 20 per topic, 60 total (LFM3: 2 topics x 3 levels x 10) def rd(p): return [json.loads(l) for l in open(D / p, encoding="utf-8") if l.strip()] def strat(rows, n, seed): # verbatim from the LFM3 prep script by = collections.defaultdict(list) for r in rows: by[r["kind"]].append(r) rng = random.Random(seed) out = [] total = len(rows) alloc = {k: max(1, round(n * len(v) / total)) for k, v in by.items()} while sum(alloc.values()) > n: alloc[max(alloc, key=alloc.get)] -= 1 while sum(alloc.values()) < n: alloc[min(alloc, key=alloc.get)] += 1 for k, v in sorted(by.items()): v = v[:] rng.shuffle(v) out += v[:alloc[k]] rng.shuffle(out) return out def w(name, rows): with open(O / name, "w", encoding="utf-8", newline="\n") as f: for r in rows: f.write(json.dumps(r, ensure_ascii=False) + "\n") return len(rows) c = {} test, dev = [], [] for lvl in (1, 2, 3): test += strat(rd(f"{TARGET}/level{lvl}/test.jsonl"), 50, 100 + lvl) dev += strat(rd(f"{TARGET}/level{lvl}/dev.jsonl"), 10, 200 + lvl) c["math_calc_test"] = w("math_calc_test.jsonl", test) c["math_calc_dev"] = w("math_calc_dev.jsonl", dev) tr = [r for lvl in (1, 2, 3) for r in [x for x in test if x["level"] == lvl][:20]] c["math_calc_test_translate"] = w("math_calc_test_translate.jsonl", tr) ret = [] for topic in RETAIN: for lvl in (1, 2, 3): rows = rd(f"{topic}/level{lvl}/test.jsonl") random.Random(300 + lvl).shuffle(rows) ret += rows[:RETAIN_PER_LEVEL[lvl]] assert not any(r["topic"] == TARGET for r in ret) c["math_retain_test"] = w("math_retain_test.jsonl", ret) def sh(name): return hashlib.sha256((O / name).read_bytes()).hexdigest() def cnt(rows): return {f"{t}/L{l}/{k}": n for (t, l, k), n in sorted(collections.Counter((r["topic"], r["level"], r["kind"]) for r in rows).items())} man = { "topic": TARGET, "retain_topics": list(RETAIN), "note": "file names keep the LFM3 'math_calc_*' names; contents are ALGEBRA items (arch.MATH_TARGET). " "math_retain_test has no algebra rows.", "source": str(D).replace("\\", "/") + "//level{1,2,3}/{test,dev}.jsonl", "scheme": {"math_calc_test": "per level 50, kind-proportional stratified (strat), seed 100+level", "math_calc_dev": "per level 10 from dev.jsonl, strat, seed 200+level", "math_calc_test_translate": "first 20 rows of each level of math_calc_test", "math_retain_test": "per topic in %s, level 1/2/3 -> %s rows after random.Random(300+level).shuffle" % (list(RETAIN), RETAIN_PER_LEVEL)}, "counts": c, "breakdown": {k: cnt(v) for k, v in (("math_calc_test", test), ("math_calc_dev", dev), ("math_calc_test_translate", tr), ("math_retain_test", ret))}, "sha256": {f"{k}.jsonl": sh(f"{k}.jsonl") for k in c}, "built": dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC"), } json.dump(man, open(O / "manifest_math.json", "w", encoding="utf-8"), indent=2) print(json.dumps(man["counts"]), json.dumps(man["breakdown"], indent=1))