Datasets:
Formats:
parquet
Languages:
English
Size:
< 1K
Tags:
video-language-model
egocentric-video
laboratory
wet-lab
procedural-monitoring
error-detection
License:
| """I/O helpers and standard output layout for LSV benchmarks.""" | |
| from __future__ import annotations | |
| import json | |
| import shutil | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| from typing import Any, Iterable | |
| BENCHMARK_ROOT = Path(__file__).resolve().parents[1] | |
| DATA_DIR = BENCHMARK_ROOT / "data" | |
| class OutputLayout: | |
| root: Path | |
| def run_config_path(self) -> Path: | |
| return self.root / "run_config.json" | |
| def task_dir(self, task_name: str) -> Path: | |
| return self.root / task_name | |
| def predictions_path(self, task_name: str) -> Path: | |
| return self.task_dir(task_name) / "predictions.jsonl" | |
| def legacy_predictions_path(self, task_name: str) -> Path: | |
| return self.task_dir(task_name) / "per_video_results.jsonl" | |
| def shard_path(self, task_name: str, shard_index: int) -> Path: | |
| return self.task_dir(task_name) / f"predictions_shard_{shard_index:02d}.jsonl" | |
| def read_json(path: Path) -> dict[str, Any]: | |
| return json.loads(path.read_text(encoding="utf-8")) | |
| def write_json(path: Path, value: Any) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(json.dumps(value, indent=2, sort_keys=True, ensure_ascii=False) + "\n", encoding="utf-8") | |
| def read_jsonl(path: Path) -> list[dict[str, Any]]: | |
| rows: list[dict[str, Any]] = [] | |
| if not path.exists(): | |
| return rows | |
| with path.open("r", encoding="utf-8") as fh: | |
| for line in fh: | |
| line = line.strip() | |
| if not line: | |
| continue | |
| try: | |
| rows.append(json.loads(line)) | |
| except json.JSONDecodeError: | |
| continue | |
| return rows | |
| def write_jsonl(path: Path, rows: Iterable[dict[str, Any]]) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with path.open("w", encoding="utf-8") as fh: | |
| for row in rows: | |
| fh.write(json.dumps(row, ensure_ascii=False, sort_keys=True) + "\n") | |
| def append_jsonl(path: Path, row: dict[str, Any]) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with path.open("a", encoding="utf-8") as fh: | |
| fh.write(json.dumps(row, ensure_ascii=False) + "\n") | |
| def load_json_manifest(path: Path) -> dict[str, Any]: | |
| return read_json(path) | |
| def manifest_examples(path: Path) -> list[dict[str, Any]]: | |
| payload = load_json_manifest(path) | |
| rows = payload.get("examples") | |
| if not isinstance(rows, list): | |
| raise ValueError(f"Manifest has no examples list: {path}") | |
| return [dict(row) for row in rows if isinstance(row, dict)] | |
| def read_parquet(path: Path) -> list[dict[str, Any]]: | |
| import pyarrow.parquet as pq | |
| return pq.read_table(path).to_pylist() | |
| def select_shard(rows: list[dict[str, Any]], num_shards: int, shard_index: int) -> list[dict[str, Any]]: | |
| if num_shards < 1: | |
| raise ValueError("num_shards must be >= 1") | |
| if not (0 <= shard_index < num_shards): | |
| raise ValueError("shard_index must satisfy 0 <= shard_index < num_shards") | |
| if num_shards == 1: | |
| return rows | |
| return [row for idx, row in enumerate(rows) if idx % num_shards == shard_index] | |
| def candidate_prediction_files(task_dir: Path) -> list[Path]: | |
| paths = sorted(task_dir.glob("predictions_shard_*.jsonl")) | |
| if paths: | |
| return paths | |
| paths = sorted(task_dir.glob("per_video_results_shard_*.jsonl")) | |
| if paths: | |
| return paths | |
| for name in ("predictions.jsonl", "per_video_results.jsonl"): | |
| path = task_dir / name | |
| if path.exists(): | |
| return [path] | |
| return [] | |
| def load_prediction_rows(task_dir: Path, *, sort_key: str = "eval_id") -> list[dict[str, Any]]: | |
| rows: list[dict[str, Any]] = [] | |
| for path in candidate_prediction_files(task_dir): | |
| rows.extend(read_jsonl(path)) | |
| return sorted(rows, key=lambda row: str(row.get(sort_key) or row.get("video_id") or row.get("eval_id") or "")) | |
| def merge_shards(task_dir: Path, *, sort_key: str = "eval_id") -> list[dict[str, Any]]: | |
| rows = load_prediction_rows(task_dir, sort_key=sort_key) | |
| if rows: | |
| write_jsonl(task_dir / "predictions.jsonl", rows) | |
| return rows | |
| def copy_legacy_predictions(task_dir: Path) -> None: | |
| predictions = task_dir / "predictions.jsonl" | |
| legacy = task_dir / "per_video_results.jsonl" | |
| if predictions.exists() and not legacy.exists(): | |
| shutil.copyfile(predictions, legacy) | |