Download scripts/make_human_indexes.py from XIAOHAOYANG/figure4-visualization-dataset: direct link, hf CLI and curl.
- Browser
- Download file 4.08 kB
-
https://huggingface.co/datasets/XIAOHAOYANG/figure4-visualization-dataset/resolve/main/scripts/make_human_indexes.py
- Command line
-
hf download hf://datasets/XIAOHAOYANG/figure4-visualization-dataset/scripts/make_human_indexes.py
-
curl -L -o make_human_indexes.py https://huggingface.co/datasets/XIAOHAOYANG/figure4-visualization-dataset/resolve/main/scripts/make_human_indexes.py
4.08 kB
| #!/usr/bin/env python3 | |
| """Generate human-readable case-order, prompt-pair, and category indexes.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from collections import defaultdict | |
| from pathlib import Path | |
| from typing import Any | |
| DEFAULT_ROOT = Path(__file__).resolve().parents[1] | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--dataset-root", type=Path, default=DEFAULT_ROOT) | |
| return parser.parse_args() | |
| def load_json(path: Path) -> dict[str, Any]: | |
| with path.open("r", encoding="utf-8") as handle: | |
| value = json.load(handle) | |
| if not isinstance(value, dict): | |
| raise TypeError(f"Expected JSON object: {path}") | |
| return value | |
| def inline(value: object) -> str: | |
| return str(value).replace("|", "\\|").replace("\n", " ").strip() | |
| def row_summary(instances: list[dict[str, Any]]) -> list[str]: | |
| grouped: dict[str, list[dict[str, Any]]] = {} | |
| for instance in instances: | |
| grouped.setdefault(str(instance["row"]), []).append(instance) | |
| lines: list[str] = [] | |
| for row, members in grouped.items(): | |
| ordered = sorted(members, key=lambda item: int(item["ordinal_from_left"])) | |
| tokens = [] | |
| for member in ordered: | |
| token = f"`{member['instance_id']}`" | |
| anchor = member.get("visual_anchor") | |
| if anchor: | |
| token += f" ({inline(anchor)})" | |
| tokens.append(token) | |
| lines.append(f"- `{row}` row, viewer left to right: " + "; ".join(tokens)) | |
| return lines | |
| def build_order_and_prompts(manifest: dict[str, Any]) -> str: | |
| lines = [ | |
| "# Evaluator-Private Order and Prompt Index", | |
| "", | |
| "Generated from `metadata/manifest.json`. This file contains spatial target information; do not expose it to an ordinary text-only baseline.", | |
| "", | |
| ] | |
| for case in manifest["cases"]: | |
| lines.extend( | |
| [ | |
| f"## `{case['case_id']}` — {inline(case['category'])}", | |
| "", | |
| f"- Image: `{case['image']}`", | |
| f"- Count/layout: {case['instance_count']}; {inline(case['layout'])}", | |
| "- Frozen instance order:", | |
| ] | |
| ) | |
| lines.extend(row_summary(case["instance_order"])) | |
| lines.append("") | |
| for variant in case["variants"]: | |
| lines.extend( | |
| [ | |
| f"- **{variant['pair']} target:** `{variant['target_instance_id']}` — {inline(variant['target_expression'])}", | |
| f" - Prompt: {inline(variant['prompt_full'])}", | |
| ] | |
| ) | |
| lines.append("") | |
| return "\n".join(lines).rstrip() + "\n" | |
| def build_category_index(manifest: dict[str, Any]) -> str: | |
| grouped: dict[str, list[dict[str, Any]]] = defaultdict(list) | |
| for case in manifest["cases"]: | |
| grouped[str(case["category"])].append(case) | |
| lines = [ | |
| "# Category Index", | |
| "", | |
| f"{len(manifest['cases'])} cases across {len(grouped)} semantic subject categories.", | |
| "", | |
| "| Category | Cases | Counts |", | |
| "|---|---|---|", | |
| ] | |
| for category in sorted(grouped, key=str.casefold): | |
| cases = sorted(grouped[category], key=lambda item: str(item["case_id"])) | |
| case_ids = ", ".join(f"`{case['case_id']}`" for case in cases) | |
| counts = ", ".join(str(case["instance_count"]) for case in cases) | |
| lines.append(f"| {inline(category)} | {case_ids} | {counts} |") | |
| return "\n".join(lines) + "\n" | |
| def main() -> None: | |
| args = parse_args() | |
| root = args.dataset_root.resolve() | |
| manifest = load_json(root / "metadata/manifest.json") | |
| metadata = root / "metadata" | |
| order_path = metadata / "ORDER_AND_PROMPTS.md" | |
| category_path = metadata / "CATEGORY_INDEX.md" | |
| order_path.write_text(build_order_and_prompts(manifest), encoding="utf-8") | |
| category_path.write_text(build_category_index(manifest), encoding="utf-8") | |
| print(order_path) | |
| print(category_path) | |
| if __name__ == "__main__": | |
| main() | |