"""Pinned release inputs shared by dataset analysis and publication tooling.""" from __future__ import annotations import json from collections import Counter from pathlib import Path from functools import lru_cache from utils.io import sha256_file from utils.paths import PROJECT_ROOT CONFIG = PROJECT_ROOT / "configs/dataset_release.json" def release_config(path: Path = CONFIG) -> dict: return json.loads(path.read_text()) def release_paths(*, verify: bool = True) -> dict[str, Path]: config = release_config() paths = {key: PROJECT_ROOT / value["path"] for key, value in config["sources"].items()} if verify: for key, path in paths.items(): if sha256_file(path) != config["sources"][key]["sha256"]: raise ValueError(f"Pinned release {key} changed: {path.name}") return paths def release_identity() -> dict: config = release_config() return {key: config[key] for key in ("id", "population_id", "benchmark_id", "split", "expected")} def validate_population(gold: list, iaa: list, manifest: list, expected: dict) -> None: sets = [] for name, rows in (("gold", gold), ("iaa", iaa), ("manifest", manifest)): ids = {row["image_id"] for row in rows} if len(ids) != len(rows) or len(ids) != expected["images"]: raise ValueError(f"Release {name} has duplicate IDs or wrong population size") sets.append(ids) if not sets[0] == sets[1] == sets[2]: raise ValueError("Release gold, IAA and manifest IDs differ") if dict(Counter(row["category"] for row in manifest)) != expected["categories"]: raise ValueError("Release contains unexpected image categories") if sum(len(row["first_pass"]) for row in iaa) != expected["first_pass_submissions"]: raise ValueError("Release first-pass submission count differs") positives = sum(bool(row["gold"]["violations"]) for row in gold) negatives = sum(row["gold"].get("no_violation_detected") is True for row in gold) if positives != expected["positive_images"] or negatives != expected["negative_images"]: raise ValueError("Release positive/negative counts differ") if any(bool(row["gold"]["violations"]) == bool(row["gold"].get("no_violation_detected")) or row["gold"].get("reported") for row in gold): raise ValueError("Release contains unusable or ambiguous gold") @lru_cache(maxsize=12) def _current_hash(path: Path, mtime_ns: int, size: int) -> str: return sha256_file(path) def sources_match_release(sources: dict) -> bool: config = release_config() for key, item in config["sources"].items(): if sources.get(key, {}).get("sha256") != item["sha256"]: return False try: path = PROJECT_ROOT / item["path"] stat = path.stat() if _current_hash(path, stat.st_mtime_ns, stat.st_size) != item["sha256"]: return False except FileNotFoundError: return False return True