#!/usr/bin/env python3
"""Compare saved metadata ranges with a hypothetical episode holdout.

Only aggregate results are printed; no episode IDs, task labels, per-episode
statistics, recordings or raw frames are retained in the output.


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

import argparse
import hashlib
import json
import math
from pathlib import Path
import tempfile
import urllib.request

import duckdb


DATASET = "lerobot/svla_so101_pickplace"
REVISION = "f641879e22172be7e8161d5e6c1503c2d2feb657"
BASE = f"https://huggingface.co/datasets/{DATASET}/resolve/{REVISION}/"
FILES = {
    "meta/info.json": "254909942a6cbfb4692a239e4d0aa5c68ec8eb16f81b6593985ea7f2bb2823e3",
    "meta/stats.json": "4ae86bed785e0f98914812e87736e216139a22b43cf2b990e68384d85168c3c8",
    "meta/episodes/chunk-000/file-000.parquet": "191998bffd2680c477a4000270cf943bc423ba5fb54ee8b8244b57db062c5209",
}


def get(path):
    request = urllib.request.Request(BASE + path, headers={"User-Agent": "Sapien1Research/1.0"})
    with urllib.request.urlopen(request, timeout=30) as response:
        raw = response.read(1_000_001)
    if len(raw) > 1_000_000 or hashlib.sha256(raw).hexdigest() != FILES[path]:
        raise ValueError("source size or hash mismatch: " + path)
    return raw


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--scratch", help="existing directory for short-lived episode metadata")
    args = parser.parse_args()
    if duckdb.__version__ != "1.4.4":
        raise ValueError("this pilot was checked with DuckDB 1.4.4")
    info = json.loads(get("meta/info.json"))
    stats = json.loads(get("meta/stats.json"))
    raw = get("meta/episodes/chunk-000/file-000.parquet")

    with tempfile.TemporaryDirectory(prefix="lerobot-stats-pilot-", dir=args.scratch) as tmp:
        path = Path(tmp) / "episodes.parquet"
        path.write_bytes(raw)
        del raw
        con = duckdb.connect(":memory:", config={"threads": "1", "memory_limit": "128MB", "temp_directory": tmp})
        try:
            rows = con.execute('''
                SELECT episode_index, length, tasks,
                       "stats/observation.state/min", "stats/observation.state/max", "stats/observation.state/count",
                       "stats/action/min", "stats/action/max", "stats/action/count"
                FROM read_parquet(?) ORDER BY episode_index
            ''', [str(path)]).fetchall()
        finally:
            con.close()

    if ([row[0] for row in rows] != list(range(info["total_episodes"])) or
        sum(row[1] for row in rows) != info["total_frames"] or
        any(row[1] <= 0 or not row[2] for row in rows) or
        len({row[2][0] for row in rows}) != 1):
        raise ValueError("episode order, lengths or task grouping differs from the planned case")
    if info["total_episodes"] != 50 or info["total_frames"] != 11939:
        raise ValueError("snapshot totals differ from the planned case")
    held_out = math.ceil(len(rows) * 0.2)
    train, evaluation = rows[:-held_out], rows[-held_out:]
    features = {}
    for name, low, high, count in (("observation.state", 3, 4, 5), ("action", 6, 7, 8)):
        dims = len(rows[0][low])
        if any(len(row[low]) != dims or len(row[high]) != dims or row[count] != [row[1]] for row in rows):
            raise ValueError("episode statistics do not match declared dimensions or lengths")
        full_min = [min(row[low][i] for row in rows) for i in range(dims)]
        full_max = [max(row[high][i] for row in rows) for i in range(dims)]
        train_min = [min(row[low][i] for row in train) for i in range(dims)]
        train_max = [max(row[high][i] for row in train) for i in range(dims)]
        published = stats[name]
        if (published["count"] != [info["total_frames"]] or len(published["min"]) != dims or
            len(published["max"]) != dims or any(
                not math.isclose(full_min[i], published["min"][i], rel_tol=1e-6, abs_tol=1e-6) or
                not math.isclose(full_max[i], published["max"][i], rel_tol=1e-6, abs_tol=1e-6)
                for i in range(dims))):
            raise ValueError("whole-snapshot statistics do not match the episode metadata")
        # Report aggregate dimension counts, not the raw extrema or per-episode values.
        lower = [train_min[i] > full_min[i] + 1e-8 for i in range(dims)]
        upper = [train_max[i] < full_max[i] - 1e-8 for i in range(dims)]
        fractions = [(train_max[i] - train_min[i]) / (full_max[i] - full_min[i])
                     for i in range(dims) if full_max[i] > full_min[i]]
        published_min, published_max = published["min"], published["max"]
        direct_lower = [train_min[i] > published_min[i] + 1e-8 for i in range(dims)]
        direct_upper = [train_max[i] < published_max[i] - 1e-8 for i in range(dims)]
        robust_lower = [train_min[i] > published_min[i] and not math.isclose(
            train_min[i], published_min[i], rel_tol=1e-6, abs_tol=1e-6) for i in range(dims)]
        robust_upper = [train_max[i] < published_max[i] and not math.isclose(
            train_max[i], published_max[i], rel_tol=1e-6, abs_tol=1e-6) for i in range(dims)]
        direct_fractions = [(train_max[i] - train_min[i]) / (published_max[i] - published_min[i])
                            for i in range(dims) if published_max[i] > published_min[i]]
        if not fractions or any(not math.isfinite(value) for value in fractions):
            raise ValueError("cannot calculate finite range fractions")
        if not direct_fractions or any(not math.isfinite(value) for value in direct_fractions):
            raise ValueError("cannot calculate saved-stat range fractions")
        features[name] = {
            "dimensions": dims,
            "dataset_wide_count": published["count"][0],
            "dimensions_with_lower_heldout_min": sum(lower),
            "dimensions_with_higher_heldout_max": sum(upper),
            "dimensions_with_either_extension": sum(a or b for a, b in zip(lower, upper)),
            "smallest_train_to_full_range_fraction": round(min(fractions), 6),
            "saved_stats_direct_comparison": {
                "absolute_threshold": 1e-8,
                "dimensions_with_lower_saved_min": sum(direct_lower),
                "dimensions_with_higher_saved_max": sum(direct_upper),
                "dimensions_with_either_extension": sum(a or b for a, b in zip(direct_lower, direct_upper)),
                "isclose_relative_tolerance": 1e-6,
                "isclose_absolute_tolerance": 1e-6,
                "dimensions_with_lower_saved_min_beyond_isclose": sum(robust_lower),
                "dimensions_with_higher_saved_max_beyond_isclose": sum(robust_upper),
                "dimensions_with_either_extension_beyond_isclose": sum(a or b for a, b in zip(robust_lower, robust_upper)),
                "smallest_train_to_saved_range_fraction": round(min(direct_fractions), 6),
            },
        }
    print(json.dumps({
        "dataset": DATASET, "dataset_revision": REVISION, "source_hashes": FILES,
        "duckdb_version": duckdb.__version__, "task_groups": 1,
        "hypothetical_eval_fraction": 0.2,
        "train_episodes": len(train), "heldout_episodes": len(evaluation),
        "train_declared_frames": sum(row[1] for row in train),
        "heldout_declared_frames": sum(row[1] for row in evaluation),
        "features": features,
        "scope": "Pinned metadata and hypothetical split only; no raw frames, recordings, model or training run.",
    }, indent=2))


if __name__ == "__main__":
    main()
