baseline.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Deterministic fixture verification, not an LLM or scientific-quality evaluation."""
  2. import argparse
  3. import csv
  4. import hashlib
  5. import json
  6. import statistics
  7. from pathlib import Path
  8. root = Path(__file__).resolve().parent
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument("--output", type=Path, required=True)
  11. args = parser.parse_args()
  12. for name, digest in json.loads((root / "inputs.sha256.json").read_text()).items():
  13. assert hashlib.sha256((root / name).read_bytes()).hexdigest() == digest, f"Fixture changed: {name}"
  14. rows = list(csv.DictReader((root / "csv/input.csv").open()))
  15. seen, cleaned = set(), []
  16. duplicates = missing = 0
  17. for row in rows:
  18. if row["id"] in seen:
  19. duplicates += 1
  20. continue
  21. seen.add(row["id"])
  22. if not row["value"].strip():
  23. missing += 1
  24. continue
  25. cleaned.append({**row, "value": float(row["value"])})
  26. stats = {}
  27. for group in ("control", "treatment"):
  28. values = [r["value"] for r in cleaned if r["group"] == group]
  29. stats[group] = {"n": len(values), "mean": statistics.mean(values), "sample_variance": statistics.variance(values)}
  30. difference = stats["treatment"]["mean"] - stats["control"]["mean"]
  31. result = {"raw_rows": len(rows), "duplicate_rows": duplicates, "missing_rows": missing,
  32. "clean_rows": len(cleaned), "groups": stats, "difference": difference,
  33. "relative_change": difference / stats["control"]["mean"]}
  34. assert result["clean_rows"] == 6 and result["difference"] == 3 and result["relative_change"] == .25
  35. args.output.mkdir(parents=True, exist_ok=True)
  36. (args.output / "expected.json").write_text(json.dumps(result, indent=2) + "\n")
  37. with (args.output / "clean.csv").open("w", newline="") as stream:
  38. writer = csv.DictWriter(stream, fieldnames=["id", "group", "value"])
  39. writer.writeheader()
  40. writer.writerows(cleaned)
  41. svg = ['<svg xmlns="http://www.w3.org/2000/svg" width="420" height="300" viewBox="0 0 420 300">',
  42. '<rect width="420" height="300" fill="white"/>',
  43. '<text x="20" y="25" font-size="16">Synthetic data: mean by group</text>',
  44. '<path d="M40 50 V240 H390" fill="none" stroke="#333"/>']
  45. for i, (group, values) in enumerate(stats.items()):
  46. x, height = 90 + i * 150, values["mean"] * 10
  47. svg += [f'<rect x="{x}" y="{240-height}" width="80" height="{height}" fill="#4286b5"/>',
  48. f'<text x="{x}" y="{230-height}">{values["mean"]}</text>',
  49. f'<text x="{x}" y="265">{group}</text>']
  50. svg.append('</svg>')
  51. (args.output / "means.svg").write_text("\n".join(svg))
  52. print(json.dumps({"fixture_integrity": "passed", "deterministic_baseline": result,
  53. "real_model": "not_run", "output": str(args.output)}, ensure_ascii=False))