Coverage for scripts/extend_frozen_weights.py: 100%
33 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""Extends a copy of a frozen weight file by its frozen rule, for the forward check (RUNBOOK "Forward check").
3The pinned file ends at its last decision before the data freeze. Rerunning it on newer prices measures old weights, not the
4signal on new prices, so a forward rerun needs the rows the same rule would have written. The original is never touched: this
5writes a copy and prints its sha256, for that run's config.
7 python scripts/extend_frozen_weights.py --definition ml/evaluation/v650/installed_candidate.json \\
8 --source ml/target_weights.csv --out ml/evaluation/v660/weights/forward.csv --end 2027-01-29
9"""
11from __future__ import annotations
13import argparse
14import json
15import sys
16from pathlib import Path
18ROOT = Path(__file__).resolve().parents[1]
19sys.path.insert(0, str(ROOT))
20sys.path.insert(0, str(ROOT / "scripts"))
23def extended_rows(source: dict[str, dict[str, float]], new_rows: dict[str, dict[str, float]], last_frozen: str) -> dict[str, dict[str, float]]:
24 """The frozen rows as they are, then every new row after them. A frozen row is never rewritten: that would change history."""
25 return {**source, **{date: row for date, row in new_rows.items() if date > last_frozen}}
28def main(argv: list[str] | None = None) -> int:
29 from v650_lean import candidate_rows
30 from v650_research import load_context
32 from portfolio.target_weights import load_weight_file, write_weight_file
34 parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
35 parser.add_argument("--definition", required=True)
36 parser.add_argument("--source", required=True)
37 parser.add_argument("--out", required=True)
38 parser.add_argument("--end", required=True)
39 args = parser.parse_args(argv)
40 if Path(args.out).resolve() == Path(args.source).resolve():
41 parser.error("--out must be a copy, never the frozen file")
42 definition = json.loads(Path(args.definition).read_text(encoding="utf-8"))
43 source = load_weight_file(args.source)
44 last_frozen = max(source)
45 context = load_context()
46 new_rows = candidate_rows(
47 definition, context, start=str(definition.get("history_start", "2014-12-01")), end=args.end,
48 phase_index=int(definition.get("phase_index", 0)),
49 )
50 rows = extended_rows(source, new_rows, last_frozen)
51 digest = write_weight_file(rows, args.out)
52 print(json.dumps({"last_frozen": last_frozen, "last_row": max(rows), "added": len(rows) - len(source), "sha256": digest}))
53 return 0
56if __name__ == "__main__":
57 raise SystemExit(main())