| |
| """Bundle HumanCollab data into the Almanac Hugging Face release folder.""" |
|
|
| from __future__ import annotations |
|
|
| import json |
| import shutil |
| from pathlib import Path |
| from typing import Any |
|
|
| ROOT = Path(__file__).resolve().parents[1] |
| REPO_ROOT = ROOT.parent |
| EXP_DATA = REPO_ROOT / "benchmark_experiment" / "exp_data" |
| GROUNDING_DATA = REPO_ROOT / "benchmark_experiment" / "grounding_data" |
| SFT_DATA = REPO_ROOT / "sft_data" |
|
|
| C1_TRAIN = ( |
| "study3", "study9", "study12", "study14", "study18", |
| "study24", "study27", "study30", "study31", |
| ) |
| C2_TRAIN = ( |
| "study2", "study4", "study6", "study10", "study11", |
| "study13", "study20", "study22", "study35", "study37", |
| ) |
| C1_TEST = ("study8", "study21", "study25") |
| C2_TEST = ("study15", "study17", "study36") |
|
|
| SFT_CONFIGS = ( |
| "follower_next_action", |
| "guide_next_action", |
| "follower_mental_model", |
| "guide_mental_model", |
| ) |
|
|
| SESSION_FILES = ( |
| "guide_timeline", |
| "follower_timeline", |
| "actions_and_annotations", |
| "score_board", |
| ) |
|
|
|
|
| def study_split(study: str) -> str: |
| if study in C1_TRAIN or study in C2_TRAIN: |
| return "train" |
| if study in C1_TEST or study in C2_TEST: |
| return "test" |
| raise ValueError(f"Unknown study: {study}") |
|
|
|
|
| def list_studies(exp_root: Path) -> list[tuple[str, str]]: |
| out: list[tuple[str, str]] = [] |
| for cond_dir in sorted(exp_root.iterdir()): |
| if not cond_dir.is_dir() or not cond_dir.name.startswith("c"): |
| continue |
| for study_dir in sorted(cond_dir.iterdir()): |
| if study_dir.is_dir() and study_dir.name.startswith("study"): |
| out.append((cond_dir.name, study_dir.name)) |
| return out |
|
|
|
|
| def copy_session_files(src_study: Path, dst_study: Path) -> list[str]: |
| dst_study.mkdir(parents=True, exist_ok=True) |
| copied: list[str] = [] |
| study = src_study.name |
| for kind in SESSION_FILES: |
| if kind == "score_board": |
| src = src_study / "score_board.txt" |
| dst = dst_study / "score_board.txt" |
| else: |
| src = src_study / f"{study}_{kind}.json" |
| dst = dst_study / f"{study}_{kind}.json" |
| if src.exists(): |
| shutil.copy2(src, dst) |
| copied.append(dst.relative_to(ROOT).as_posix()) |
| return copied |
|
|
|
|
| def flatten_timeline( |
| timeline: list[dict[str, Any]], |
| *, |
| condition: str, |
| study: str, |
| role: str, |
| include_grounding: bool, |
| ) -> list[dict[str, Any]]: |
| rows: list[dict[str, Any]] = [] |
| for idx, ev in enumerate(timeline): |
| row: dict[str, Any] = { |
| "condition": condition, |
| "study_name": study, |
| "split": study_split(study), |
| "role": role, |
| "timeline_index": idx, |
| "timestamp": ev.get("timestamp", ""), |
| "action_type": ev.get("action_type", ""), |
| "action_content": ev.get("action_content", ""), |
| "map_canvas": ev.get("map_canvas", ""), |
| "drawing_accuracy": ev.get("drawing_accuracy", ""), |
| "mental_model": json.dumps(ev.get("mental_model") or {}, ensure_ascii=False), |
| } |
| if include_grounding: |
| row["grounding_act"] = ev.get("grounding_act", "") |
| row["grounding_rationale"] = ev.get("grounding_rationale", "") |
| rows.append(row) |
| return rows |
|
|
|
|
| def write_jsonl(path: Path, rows: list[dict[str, Any]]) -> None: |
| path.parent.mkdir(parents=True, exist_ok=True) |
| with path.open("w", encoding="utf-8") as f: |
| for row in rows: |
| f.write(json.dumps(row, ensure_ascii=False) + "\n") |
|
|
|
|
| def copy_sft_configs() -> dict[str, dict[str, int]]: |
| stats: dict[str, dict[str, int]] = {} |
| for cfg in SFT_CONFIGS: |
| stats[cfg] = {} |
| for split in ("train", "test"): |
| src_name = "training.jsonl" if split == "train" else "test.jsonl" |
| src = SFT_DATA / cfg / src_name |
| dst = ROOT / "data" / "sft" / cfg / f"{split}.jsonl" |
| dst.parent.mkdir(parents=True, exist_ok=True) |
| shutil.copy2(src, dst) |
| n = sum(1 for _ in dst.open(encoding="utf-8")) |
| stats[cfg][split] = n |
| return stats |
|
|
|
|
| def build_flat_tables(studies: list[tuple[str, str]]) -> tuple[int, int]: |
| raw_rows: list[dict[str, Any]] = [] |
| grounding_rows: list[dict[str, Any]] = [] |
|
|
| for condition, study in studies: |
| raw_study = ROOT / "data" / "raw_sessions" / condition / study |
| g_study = ROOT / "data" / "grounding" / condition / study |
|
|
| for role in ("guide", "follower"): |
| raw_path = raw_study / f"{study}_{role}_timeline.json" |
| if raw_path.exists(): |
| timeline = json.loads(raw_path.read_text(encoding="utf-8")) |
| raw_rows.extend( |
| flatten_timeline( |
| timeline, |
| condition=condition, |
| study=study, |
| role=role, |
| include_grounding=False, |
| ) |
| ) |
|
|
| g_path = g_study / f"{study}_{role}.json" |
| if g_path.exists(): |
| timeline = json.loads(g_path.read_text(encoding="utf-8")) |
| grounding_rows.extend( |
| flatten_timeline( |
| timeline, |
| condition=condition, |
| study=study, |
| role=role, |
| include_grounding=True, |
| ) |
| ) |
|
|
| write_jsonl(ROOT / "data" / "raw_timeline" / "all.jsonl", raw_rows) |
| write_jsonl(ROOT / "data" / "grounding_timeline" / "all.jsonl", grounding_rows) |
| for split in ("train", "test"): |
| write_jsonl( |
| ROOT / "data" / "raw_timeline" / f"{split}.jsonl", |
| [r for r in raw_rows if r["split"] == split], |
| ) |
| write_jsonl( |
| ROOT / "data" / "grounding_timeline" / f"{split}.jsonl", |
| [r for r in grounding_rows if r["split"] == split], |
| ) |
| return len(raw_rows), len(grounding_rows) |
|
|
|
|
| def build_studies_index(studies: list[tuple[str, str]]) -> list[dict[str, Any]]: |
| index: list[dict[str, Any]] = [] |
| for condition, study in studies: |
| meta_path = ROOT / "data" / "grounding" / condition / study / f"{study}_grounding_meta.json" |
| entry: dict[str, Any] = { |
| "condition": condition, |
| "study_name": study, |
| "split": study_split(study), |
| "files": {}, |
| } |
| if meta_path.exists(): |
| meta = json.loads(meta_path.read_text(encoding="utf-8")) |
| entry["n_actions"] = meta.get("n_actions") |
| entry["n_guide_actions"] = meta.get("n_guide_actions") |
| entry["n_follower_actions"] = meta.get("n_follower_actions") |
| entry["canvas_condition"] = meta.get("canvas_condition") |
| entry["llm_meta"] = meta.get("llm_meta") |
| study_dir = ROOT / "data" / "raw_sessions" / condition / study |
| for p in sorted(study_dir.iterdir()): |
| entry["files"][p.name] = f"data/raw_sessions/{condition}/{study}/{p.name}" |
| g_dir = ROOT / "data" / "grounding" / condition / study |
| for p in sorted(g_dir.iterdir()): |
| entry.setdefault("grounding_files", {})[p.name] = ( |
| f"data/grounding/{condition}/{study}/{p.name}" |
| ) |
| index.append(entry) |
| return index |
|
|
|
|
| def build_grounding_manifest(studies: list[tuple[str, str]]) -> dict[str, Any]: |
| return { |
| "n_studies": len(studies), |
| "llm_meta": { |
| "llm_provider": "azure", |
| "llm_model": "gpt-5.5", |
| }, |
| "note": "Grounding labels were produced with the prompt in Grounding Acts Human Annotation/llm_propmt.txt", |
| "studies": [ |
| { |
| "condition": c, |
| "study": s, |
| "split": study_split(s), |
| "paths": { |
| "guide": f"data/grounding/{c}/{s}/{s}_guide.json", |
| "follower": f"data/grounding/{c}/{s}/{s}_follower.json", |
| "meta": f"data/grounding/{c}/{s}/{s}_grounding_meta.json", |
| }, |
| } |
| for c, s in studies |
| ], |
| } |
|
|
|
|
| def main() -> None: |
| studies = list_studies(EXP_DATA) |
| print(f"Found {len(studies)} sessions") |
|
|
| for condition, study in studies: |
| src = EXP_DATA / condition / study |
| dst = ROOT / "data" / "raw_sessions" / condition / study |
| copy_session_files(src, dst) |
|
|
| g_src = GROUNDING_DATA / condition / study |
| g_dst = ROOT / "data" / "grounding" / condition / study |
| if g_src.exists(): |
| g_dst.mkdir(parents=True, exist_ok=True) |
| for f in g_src.iterdir(): |
| if f.is_file(): |
| shutil.copy2(f, g_dst / f.name) |
|
|
| sft_stats = copy_sft_configs() |
| n_raw, n_ground = build_flat_tables(studies) |
|
|
| split_protocol = { |
| "split_unit": "study", |
| "train": {"c1": list(C1_TRAIN), "c2": list(C2_TRAIN)}, |
| "test": {"c1": list(C1_TEST), "c2": list(C2_TEST)}, |
| "sft_configs": list(SFT_CONFIGS), |
| } |
| meta_dir = ROOT / "metadata" |
| meta_dir.mkdir(parents=True, exist_ok=True) |
| (meta_dir / "split_protocol.json").write_text( |
| json.dumps(split_protocol, indent=2, ensure_ascii=False) + "\n", |
| encoding="utf-8", |
| ) |
| (meta_dir / "studies_index.json").write_text( |
| json.dumps(build_studies_index(studies), indent=2, ensure_ascii=False) + "\n", |
| encoding="utf-8", |
| ) |
| (meta_dir / "grounding_manifest.json").write_text( |
| json.dumps(build_grounding_manifest(studies), indent=2, ensure_ascii=False) + "\n", |
| encoding="utf-8", |
| ) |
|
|
| summary = { |
| "n_studies": len(studies), |
| "n_raw_timeline_rows": n_raw, |
| "n_grounding_timeline_rows": n_ground, |
| "sft": sft_stats, |
| } |
| (meta_dir / "bundle_summary.json").write_text( |
| json.dumps(summary, indent=2, ensure_ascii=False) + "\n", |
| encoding="utf-8", |
| ) |
| print(json.dumps(summary, indent=2)) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|