#!/usr/bin/env python3
"""Render two compact SVG exhibits from the reviewed aggregate observation.

Sapien1 makes this script available to download and run locally. No additional
license to modify or redistribute it is stated. Dataset terms are separate.
"""

import hashlib
import json
from pathlib import Path


ROOT = Path(__file__).resolve().parent
EXPECTED = "e112ec1fc919e86b8d51e125bbec0c4c975029ce67e04061c553bad52c778967"


def main():
    raw = (ROOT / "observations.json").read_bytes()
    if hashlib.sha256(raw).hexdigest() != EXPECTED:
        raise ValueError("reviewed aggregate hash changed")
    data = json.loads(raw)
    if (data["train_episodes"], data["heldout_episodes"], data["train_declared_frames"],
        data["heldout_declared_frames"]) != (40, 10, 9180, 2759):
        raise ValueError("unexpected split totals")
    features = data["features"]
    state = features["observation.state"]["saved_stats_direct_comparison"]
    action = features["action"]["saved_stats_direct_comparison"]
    if state["dimensions_with_either_extension_beyond_isclose"] != 3 or action["dimensions_with_either_extension_beyond_isclose"] != 3:
        raise ValueError("unexpected dimension counts")
    state_ratio = state["smallest_train_to_saved_range_fraction"]
    action_ratio = action["smallest_train_to_saved_range_fraction"]
    if not (0 <= state_ratio <= 1 and 0 <= action_ratio <= 1):
        raise ValueError("unexpected range ratio")

    chart = f'''<svg xmlns="http://www.w3.org/2000/svg" width="360" height="306" viewBox="0 0 360 306" role="img" aria-labelledby="title desc">
<title id="title">Training-episode range width relative to saved range</title>
<desc id="desc">For the hypothetical 40 and 10 episode split, the narrowest training-to-saved range-width ratio is {state_ratio * 100:.1f} percent for state and {action_ratio * 100:.1f} percent for action. Saved ranges extend beyond training-episode ranges in three of six dimensions for each feature. This compares reported widths, not endpoints or model performance.</desc>
<rect width="360" height="306" fill="#ffffff"/>
<g font-family="Arial, sans-serif" fill="#1c1917">
  <text x="18" y="29" font-size="19" font-weight="700">Training range / saved range</text>
  <text x="18" y="49" font-size="12" fill="#514b46">Smallest width ratio among six dimensions</text>
  <text x="18" y="67" font-size="12" fill="#514b46">One snapshot · hypothetical 40/10 split</text>
  <text x="18" y="99" font-size="14" font-weight="700">State</text>
  <rect x="18" y="110" width="245" height="13" rx="2" fill="#e5ddd6"/>
  <rect x="18" y="110" width="{245 * state_ratio:.3f}" height="13" rx="2" fill="#ad4518"/>
  <text x="342" y="122" font-size="14" font-weight="700" text-anchor="end">{state_ratio * 100:.1f}%</text>
  <text x="18" y="145" font-size="12" fill="#514b46">A saved bound extends in 3 of 6 dimensions</text>
  <text x="18" y="180" font-size="14" font-weight="700">Action</text>
  <rect x="18" y="191" width="245" height="13" rx="2" fill="#e5ddd6"/>
  <rect x="18" y="191" width="{245 * action_ratio:.3f}" height="13" rx="2" fill="#ad4518"/>
  <text x="342" y="203" font-size="14" font-weight="700" text-anchor="end">{action_ratio * 100:.1f}%</text>
  <text x="18" y="226" font-size="12" fill="#514b46">A saved bound extends in 3 of 6 dimensions</text>
  <text x="18" y="265" font-size="11.5" fill="#514b46">Bars compare widths, not range endpoints.</text>
  <text x="18" y="283" font-size="11.5" fill="#514b46">Source: pinned LeRobot episode metadata.</text>
</g>
</svg>
'''
    path = '''<svg xmlns="http://www.w3.org/2000/svg" width="360" height="425" viewBox="0 0 360 425" role="img" aria-labelledby="title desc">
<title id="title">Episode selection and saved statistics follow separate paths</title>
<desc id="desc">A pinned dataset root supplies an episode split of 40 training and 10 held-out episodes to a dataset reader. Separately, it supplies saved state and action statistics with a count of 11,939 each to a fresh Diffusion processor. This source diagram is conditional and does not establish a run or the provenance of the saved statistics.</desc>
<rect width="360" height="425" fill="#ffffff"/>
<g font-family="Arial, sans-serif" fill="#1c1917">
  <text x="18" y="29" font-size="19" font-weight="700">Separate paths from one root</text>
  <text x="18" y="49" font-size="12" fill="#514b46">Pinned source · hypothetical 40/10 split</text>
  <rect x="18" y="68" width="324" height="49" rx="6" fill="#f5f1ed" stroke="#cfc3b9"/>
  <text x="180" y="98" text-anchor="middle" font-size="15" font-weight="700">Dataset root and revision</text>
  <path d="M180 117 V134 M93 134 H267 M93 134 V151 M267 134 V151" fill="none" stroke="#8c7d71" stroke-width="1.5"/>
  <rect x="18" y="151" width="150" height="98" rx="6" fill="#ffffff" stroke="#cfc3b9"/>
  <rect x="192" y="151" width="150" height="98" rx="6" fill="#ffffff" stroke="#cfc3b9"/>
  <text x="30" y="175" font-size="13" font-weight="700">Episode selection</text>
  <text x="30" y="198" font-size="12">40 train</text>
  <text x="30" y="218" font-size="12">10 held out</text>
  <text x="204" y="175" font-size="13" font-weight="700">Saved statistics</text>
  <text x="204" y="198" font-size="12">State and action</text>
  <text x="204" y="218" font-size="12">count: 11,939 each</text>
  <path d="M93 249 V278 M267 249 V278 M88 272 L93 278 L98 272 M262 272 L267 278 L272 272" fill="none" stroke="#8c7d71" stroke-width="1.5"/>
  <rect x="18" y="278" width="150" height="76" rx="6" fill="#f5f1ed" stroke="#cfc3b9"/>
  <rect x="192" y="278" width="150" height="76" rx="6" fill="#f8e9df" stroke="#ad4518"/>
  <text x="93" y="301" text-anchor="middle" font-size="13" font-weight="700">Dataset reader</text>
  <text x="93" y="321" text-anchor="middle" font-size="12">receives selected</text>
  <text x="93" y="338" text-anchor="middle" font-size="12">episodes</text>
  <text x="267" y="301" text-anchor="middle" font-size="13" font-weight="700">Fresh Diffusion</text>
  <text x="267" y="318" text-anchor="middle" font-size="13" font-weight="700">processor</text>
  <text x="267" y="338" text-anchor="middle" font-size="12">receives saved stats</text>
  <text x="18" y="378" font-size="11.5" fill="#514b46">No pretrained or resumed processor in this path.</text>
  <text x="18" y="396" font-size="11.5" fill="#514b46">No run or saved-stat provenance was verified.</text>
</g>
</svg>
'''
    (ROOT / "range-comparison.svg").write_text(chart, encoding="utf-8")
    (ROOT / "stats-path.svg").write_text(path, encoding="utf-8")


if __name__ == "__main__":
    main()
