Coverage for scripts/v650_install.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""Installs the R-LEAN19 winner (V6.5.0): config.json becomes the frozen median-phase run's config, without its window override, and the 

2weight file moves next to the models. Refuses anything but the verdict's winner. 

3 

4 python scripts/v650_install.py 

5""" 

6 

7from __future__ import annotations 

8 

9import hashlib 

10import json 

11import shutil 

12import sys 

13from pathlib import Path 

14 

15ROOT = Path(__file__).resolve().parents[1] 

16V650 = ROOT / "ml" / "evaluation" / "v650" 

17INSTALLED_WEIGHTS = "ml/target_weights.csv" 

18INSTALLED_CANDIDATE = "ml/target_weights_candidate.json" 

19HISTORY_START = "2023-07-01" 

20 

21 

22def median_phase(phases: dict[str, dict]) -> str: 

23 """The phase with the median ex-T-bill Sharpe of an odd number of phases.""" 

24 ordered = sorted(phases, key=lambda phase: phases[phase]["strategy_sharpe"]) 

25 return ordered[len(ordered) // 2] 

26 

27 

28def main() -> int: 

29 verdict = json.loads((V650 / "r_lean19.json").read_text(encoding="utf-8")) 

30 winner = verdict["verdict"]["install"] 

31 if winner is None: 

32 print("R-LEAN19 installs nothing", file=sys.stderr) 

33 return 1 

34 phases = verdict["detail"][winner]["phases"] 

35 phase = median_phase(phases) 

36 run = phases[phase]["run"] 

37 config = json.loads((ROOT / "ml" / "evaluation" / "run_configs" / f"{run}.json").read_text(encoding="utf-8")) 

38 target = config["phase_v2"]["target_weights"] 

39 source = ROOT / target["weights_path"] 

40 if hashlib.sha256(source.read_bytes()).hexdigest() != target["weights_sha256"]: 

41 print(f"{source} is not the file the run froze", file=sys.stderr) 

42 return 1 

43 shutil.copyfile(source, ROOT / INSTALLED_WEIGHTS) 

44 target["weights_path"] = INSTALLED_WEIGHTS 

45 config["runtime"]["lean_window_override"] = None 

46 (ROOT / "config.json").write_text(json.dumps(config, indent=4), encoding="utf-8", newline="\n") 

47 candidate = {"survivors": [winner], "method": None, "phase_index": int(phase[1:]), "history_start": HISTORY_START, "pinned_run": run} 

48 (ROOT / INSTALLED_CANDIDATE).write_text(json.dumps(candidate, indent=1), encoding="utf-8", newline="\n") 

49 digest = hashlib.sha256((ROOT / "config.json").read_bytes()).hexdigest() 

50 print(json.dumps({"winner": winner, "phase": phase, "run": run, "config_sha256": digest, "weights_sha256": target["weights_sha256"]}, indent=1)) 

51 return 0 

52 

53 

54if __name__ == "__main__": 

55 raise SystemExit(main())