Skip to content
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a058fd8
feat: record metric inputs and run metadata during generation
bokalnv Oct 27, 2026
96a15bd
docs: describe what generation records per step, attempt and run
bokalnv Oct 27, 2026
5f3264b
feat: add the autodata_metrics registry and the run report
bokalnv Oct 27, 2026
2a5d495
docs: add the metrics reference page with a table generated from the …
bokalnv Oct 27, 2026
5303c8f
fix: keep earlier runs' records and label only this run's datasets
bokalnv Oct 28, 2026
a2006af
Merge the PR #84 fixes into the metrics framework
bokalnv Oct 28, 2026
9ca575b
fix: report a run from the attempt log of the dataset's own run
bokalnv Oct 28, 2026
5948fc0
docs: say the result file and attempt log come with --result_file
bokalnv Oct 28, 2026
729ffc7
Merge the PR #84 docs fix into the metrics framework
bokalnv Oct 28, 2026
77b511d
feat: make metric recording opt-in and keep algorithm diagnostics in …
bokalnv Oct 28, 2026
d147c48
Merge the opt-in metric recording from PR #84
bokalnv Oct 28, 2026
ca4a948
fix: reject non-finite values, require --report for report options, r…
bokalnv Oct 28, 2026
9ea6a76
fix: record metric inputs with a custom recorder and without a succes…
bokalnv Oct 28, 2026
4f6e1ab
Merge the recorder fixes from PR #84
bokalnv Oct 28, 2026
44a57b8
Merge main after PR #84
bokalnv Oct 28, 2026
c632ff6
feat: add episode checks and validity metrics
bokalnv Oct 28, 2026
2bd8352
fix: check the attempt log against its result and keep reports valid …
bokalnv Oct 29, 2026
f205d26
Merge the review fixes from #90
bokalnv Oct 29, 2026
45d5ca4
refactor: compute baselines on the source as is and share the docs ta…
bokalnv Oct 29, 2026
475e598
fix: refuse a source other than the run's and non-finite gate bounds
bokalnv Oct 29, 2026
7e84d20
Merge the source and gate-bound checks from #90
bokalnv Oct 29, 2026
3d4f435
fix: require scalar gate bounds and qualify the source check in the docs
bokalnv Oct 29, 2026
f0111a3
Merge the gate-bound and docs fixes from #90
bokalnv Oct 29, 2026
fc8e3a8
Merge main after #90
bokalnv Oct 29, 2026
573e049
fix: warn about a dataset without an autodata entry only against a ru…
bokalnv Oct 29, 2026
1a591f2
feat: summarize the metrics report on the console and time annotation…
bokalnv Oct 29, 2026
19cd660
fix: tighten the episode checks and require valid episodes in the e2e…
bokalnv Oct 29, 2026
b3b2d9a
Merge the episode-check fixes from #91
bokalnv Oct 29, 2026
9cbd00a
refactor: derive skipped checks from the check groups and trim the su…
bokalnv Oct 29, 2026
b9c9d5b
fix: report degenerate data instead of failing on it
bokalnv Oct 29, 2026
622af85
fix: keep the report and its summary whole on malformed metric output…
bokalnv Oct 29, 2026
1b74d5b
fix: refuse corrupt run metadata and malformed gate files
bokalnv Oct 29, 2026
88446f0
refactor: parse every record the report reads against one declared shape
bokalnv Oct 29, 2026
1cc004c
fix: check subtask signals and pose shapes, and time frozen frames
bokalnv Oct 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions autodata_metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
Importing this package registers the built-in metrics.
"""

from autodata_metrics import viability
from autodata_metrics import validity, viability
from autodata_metrics.evidence import Episode, Evidence
from autodata_metrics.registry import AREAS, REGISTERED_METRICS, MetricSpec, NotApplicable, Value, metric
from autodata_metrics.registry import (
AREAS,
REGISTERED_CHECKS,
REGISTERED_METRICS,
CheckSpec,
MetricSpec,
NotApplicable,
Value,
check,
metric,
)
from autodata_metrics.report import build_report, format_report, gate_violations
33 changes: 31 additions & 2 deletions autodata_metrics/evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import numpy as np
from collections.abc import Mapping
from functools import cached_property
from typing import Any


Expand Down Expand Up @@ -41,12 +42,27 @@ def __contains__(self, key: str) -> bool:
def __getitem__(self, key: str) -> np.ndarray:
return self._group[key][()]

def children(self, key: str) -> list[str]:
"""Names under the group ``key``, such as the end effectors under ``obs/datagen_info/eef_pose``."""
return list(self._group[key])

def datasets(self) -> list[tuple[str, h5py.Dataset]]:
"""Every dataset of the episode with its path, unread until accessed."""
found: list[tuple[str, h5py.Dataset]] = []
self._group.visititems(
lambda path, item: found.append((path, item)) if isinstance(item, h5py.Dataset) else None
)
return found


class Evidence:
"""A dataset, and optionally the generation run that produced it, as metrics read them.

Attributes:
episodes: The dataset's episodes.
all_episodes: The dataset's episodes.
episodes: The episodes that passed every episode check; dataset metrics read these.
failures: Per episode check that ran, the failing episodes' names and reasons.
check_groups: Per episode check that ran, its group.
env_args: The dataset's ``env_args``; its ``autodata`` entry was written by the producer.
result: Generation result, or None for a dataset without a run record or an interrupted run.
run: The result's ``run`` block, or empty.
Expand All @@ -62,7 +78,10 @@ def __init__(
source: Evidence | None = None,
) -> None:
self.env_args = read_env_args(h5_file)
self.episodes = [Episode(group) for group in h5_file["data"].values()]
self.all_episodes = [Episode(group) for group in h5_file["data"].values()]
self.episodes = list(self.all_episodes)
self.failures: dict[str, dict[str, str]] = {}
self.check_groups: dict[str, str] = {}
self.result = result
self.run: Mapping[str, Any] = (result or {}).get("run", {})
self.attempts = attempts
Expand All @@ -88,3 +107,13 @@ def has(self, key: str) -> bool:
if head == "run":
return _has_path(self.run, rest)
return bool(self.episodes) and all(key in episode for episode in self.episodes)

@cached_property
def camera_paths(self) -> frozenset[str]:
"""Paths of the camera streams, 4-D ``uint8`` arrays, that any episode records."""
return frozenset(
path
for episode in self.all_episodes
for path, dataset in episode.datasets()
if dataset.dtype == np.uint8 and dataset.ndim == 4
)
48 changes: 44 additions & 4 deletions autodata_metrics/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import TYPE_CHECKING, Any, Literal

if TYPE_CHECKING:
from autodata_metrics.evidence import Evidence
from autodata_metrics.evidence import Episode, Evidence

AREAS = ("validity", "diversity", "viability", "diagnostics")

Expand Down Expand Up @@ -47,41 +47,57 @@ class MetricSpec:
compute: Callable[[Evidence], Value]
area: str
signal: str
scope: Literal["dataset", "run"]
scope: Literal["episode", "dataset", "run"]
unit: str
better: Literal["higher", "lower"] | None
requires: tuple[str, ...]
primary: str | None
version: int
baseline: bool = False


@dataclass(frozen=True)
class CheckSpec:
"""A registered episode check."""

name: str
check: Callable[[Episode, Evidence], str | None]
group: str
requires: tuple[str, ...]


REGISTERED_METRICS: dict[str, MetricSpec] = {}
REGISTERED_CHECKS: dict[str, CheckSpec] = {}
CHECK_GROUPS = ("correctness", "image")


def metric(
name: str,
*,
area: str,
signal: str,
scope: Literal["dataset", "run"],
scope: Literal["episode", "dataset", "run"],
unit: str,
better: Literal["higher", "lower"] | None = None,
requires: tuple[str, ...] | list[str] = (),
primary: str | None = None,
version: int = 1,
baseline: bool = False,
) -> Callable[[Callable[[Evidence], Value]], Callable[[Evidence], Value]]:
"""Register the decorated function as the metric ``name``.

Args:
name: Unique metric name, used in reports and gates.
area: One of :data:`AREAS`.
signal: What the metric measures within its area, e.g. ``"task correctness"``.
scope: ``"dataset"`` metrics read episodes; ``"run"`` metrics read attempts and the run record.
scope: ``"episode"`` metrics summarize the episode checks over every episode; ``"dataset"`` metrics read
the episodes that passed them; ``"run"`` metrics read attempts and the run record.
unit: Unit of the value.
better: Which direction is better, if either.
requires: Evidence keys; see :meth:`~autodata_metrics.evidence.Evidence.has`.
primary: For a dict value, the entry a gate checks by default.
version: Raise it whenever the definition changes, so gates pinned to the old one fail.
baseline: Also compute the metric on the source dataset.
"""
assert area in AREAS, f"Metric {name!r} has area {area!r}; expected one of {AREAS}."

Expand All @@ -98,7 +114,31 @@ def register(compute: Callable[[Evidence], Value]) -> Callable[[Evidence], Value
requires=tuple(requires),
primary=primary,
version=version,
baseline=baseline,
)
return compute

return register


def check(
name: str, *, group: str = "correctness", requires: tuple[str, ...] | list[str] = ()
) -> Callable[[Callable[[Episode, Evidence], str | None]], Callable[[Episode, Evidence], str | None]]:
"""Register the decorated function as the episode check ``name``.

A check returns None for an episode that passes, a reason for one that fails, or raises :class:`NotApplicable`
for an episode it does not cover. Episodes that fail any check are left out of every dataset metric.

Args:
name: Unique check name.
group: One of :data:`CHECK_GROUPS`; the pass-rate metric the check counts toward.
requires: Evidence keys the check needs; without them it does not run.
"""
assert group in CHECK_GROUPS, f"Check {name!r} has group {group!r}; expected one of {CHECK_GROUPS}."

def register(function: Callable[[Episode, Evidence], str | None]) -> Callable[[Episode, Evidence], str | None]:
assert name not in REGISTERED_CHECKS, f"Check {name!r} is already registered."
REGISTERED_CHECKS[name] = CheckSpec(name=name, check=function, group=group, requires=tuple(requires))
return function

return register
58 changes: 53 additions & 5 deletions autodata_metrics/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Any

from autodata_metrics.evidence import Evidence, read_env_args
from autodata_metrics.registry import AREAS, REGISTERED_METRICS, MetricSpec, NotApplicable
from autodata_metrics.registry import AREAS, REGISTERED_CHECKS, REGISTERED_METRICS, CheckSpec, MetricSpec, NotApplicable
from autodata_utils.generation_result import attempts_file_for, code_revision, input_record

SCHEMA_VERSION = 1
Expand Down Expand Up @@ -58,7 +58,7 @@ def check_same_run(evidence: Evidence) -> list[str]:
Raises:
ValueError: If the dataset, result and attempt log disagree on their run or their counts.
"""
num_episodes = len(evidence.episodes)
num_episodes = len(evidence.all_episodes)
result = evidence.result
if result is not None and num_episodes != result["num_success"]:
raise ValueError(
Expand All @@ -72,8 +72,10 @@ def check_same_run(evidence: Evidence) -> list[str]:
f" {result['num_attempts']} and {result['num_success']}."
)
entry = evidence.env_args.get("autodata")
if entry is None:
if entry is None and result is not None:
return ["the dataset has no autodata entry, so it is matched to its run by episode count only"]
if entry is None:
return []
if entry["exported_episodes"] != num_episodes:
raise ValueError(
f"The dataset holds {num_episodes} episodes but its run exported {entry['exported_episodes']};"
Expand All @@ -87,6 +89,30 @@ def check_same_run(evidence: Evidence) -> list[str]:
return []


def run_checks(evidence: Evidence, checks: Mapping[str, CheckSpec]) -> None:
"""Run the episode checks and keep only the episodes that pass all of them in ``evidence.episodes``."""
for spec in checks.values():
if any(not evidence.has(key) for key in spec.requires):
continue
failed: dict[str, str] = {}
applied = False
for episode in evidence.all_episodes:
try:
reason = spec.check(episode, evidence)
except NotApplicable:
continue
except Exception as exc: # A broken check fails its episodes visibly instead of sinking the report.
reason = f"{type(exc).__name__}: {exc}"
applied = True
if reason is not None:
failed[episode.name] = reason
if applied:
evidence.failures[spec.name] = failed
evidence.check_groups[spec.name] = spec.group
failing = {name for failed in evidence.failures.values() for name in failed}
evidence.episodes = [episode for episode in evidence.all_episodes if episode.name not in failing]


def evaluate(spec: MetricSpec, evidence: Evidence) -> dict[str, Any]:
"""One report row: the metric's value, or why it is not applicable or failed."""
row = {
Expand All @@ -98,6 +124,8 @@ def evaluate(spec: MetricSpec, evidence: Evidence) -> dict[str, Any]:
"primary": spec.primary,
"version": spec.version,
}
if spec.scope == "dataset" and not evidence.episodes:
return {**row, "status": "not_applicable", "reason": "no valid episodes"}
missing = next((key for key in spec.requires if not evidence.has(key)), None)
if missing is not None:
return {**row, "status": "not_applicable", "reason": f"missing {missing}"}
Expand All @@ -109,7 +137,10 @@ def evaluate(spec: MetricSpec, evidence: Evidence) -> dict[str, Any]:
return {**row, "status": "error", "reason": f"{type(exc).__name__}: {exc}"}
if not _is_finite(value.value):
return {**row, "status": "error", "reason": f"non-finite value {value.value}"}
return {**row, "status": "ok", "value": value.value, "n": value.n, "details": _finite_or_none(value.details)}
row = {**row, "status": "ok", "value": value.value, "n": value.n, "details": _finite_or_none(value.details)}
if spec.baseline and evidence.source is not None:
row["baseline"] = evaluate(spec, evidence.source).get("value")
return row


def _is_finite(value: Any) -> bool:
Expand All @@ -134,6 +165,7 @@ def build_report(
result_file: str | None = None,
source: str | None = None,
metrics: Mapping[str, MetricSpec] | None = None,
checks: Mapping[str, CheckSpec] | None = None,
) -> dict[str, Any]:
"""Compute every metric for a dataset and, when ``result_file`` is given, its generation run.

Expand All @@ -142,6 +174,7 @@ def build_report(
result_file: Generation result (``generate_dataset.py --result_file``); its attempt log is read beside it.
source: Source dataset the run started from.
metrics: Metrics to compute; defaults to every registered metric.
checks: Episode checks to run first; defaults to every registered check.

Raises:
ValueError: If the dataset, result, attempt log and source do not all come from one run.
Expand All @@ -157,9 +190,17 @@ def build_report(
recorded_source = evidence.run.get("inputs", {}).get("source")
if source_record and recorded_source and source_record["sha256"] != recorded_source["sha256"]:
raise ValueError(f"The source {source} is not the one the run started from, {recorded_source['path']}.")
for checked in (evidence, source_evidence):
if checked is not None:
run_checks(checked, REGISTERED_CHECKS if checks is None else checks)
metrics = REGISTERED_METRICS if metrics is None else metrics
rows = {name: evaluate(spec, evidence) for name, spec in metrics.items()}
num_episodes = len(evidence.episodes)
num_episodes = len(evidence.all_episodes)
num_valid = len(evidence.episodes)
failures = {
name: {"failed": len(failed), "examples": dict(list(failed.items())[:3])}
for name, failed in evidence.failures.items()
}
return {
"schema_version": SCHEMA_VERSION,
"inputs": {
Expand All @@ -170,6 +211,8 @@ def build_report(
"revision": code_revision(),
},
"num_episodes": num_episodes,
"num_valid_episodes": num_valid,
"check_failures": failures,
"warnings": warnings,
"metrics": rows,
}
Expand Down Expand Up @@ -234,5 +277,10 @@ def format_report(report: Mapping[str, Any]) -> str:
widths = [max(len(row[column]) for row in rows) for column in range(len(rows[0]))]
lines = [" ".join(cell.ljust(width) for cell, width in zip(row, widths)).rstrip() for row in rows]
lines.insert(1, " ".join("-" * width for width in widths))
failed = [f"{name}={entry['failed']}" for name, entry in report["check_failures"].items() if entry["failed"]]
lines.append(
f"episodes: {report['num_valid_episodes']} valid of {report['num_episodes']}"
+ (f"; failed checks: {', '.join(failed)}" if failed else "")
)
lines += [f"warning: {warning}" for warning in report["warnings"]]
return "\n".join(lines)
Loading
Loading