File size: 3,192 Bytes
399df25
 
 
 
 
 
 
 
 
b9cc0d4
 
 
 
 
 
 
 
399df25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9cc0d4
399df25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import argparse
import json
from pathlib import Path


DETECTION_INSTRUCTION = (
    "Determine whether the video contains an anomalous event. "
    "Output 1 if an anomaly is present."
)
TIMESTAMP_INSTRUCTION = (
    "\n"
    "    Locate the position of the anomalous segment in the video. "
    "Report the start and end timestamps in mmss format rather than seconds, "
    "and use the following output format:\n"
    "    [xxxx, xxxx]\n"
    "    "
)

EXPECTED_TEST_TASKS = {
    "Classification",
    "Cause",
    "Result",
    "Timestamp",
    "Description",
    "Detection",
}


def load_json(path: Path):
    with path.open("r", encoding="utf-8") as f:
        return json.load(f)


def dump_json(path: Path, data) -> None:
    with path.open("w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
        f.write("\n")


def build_instruction_map(gt_rows):
    task_to_instruction = {}
    for row in gt_rows:
        task = row["task"]
        instruction = TIMESTAMP_INSTRUCTION if task == "Timestamp" else row["instruction"]
        existing = task_to_instruction.get(task)
        if existing is None:
            task_to_instruction[task] = instruction
        elif existing != instruction:
            raise ValueError(f"Task {task!r} has multiple instructions in gt")
    return task_to_instruction


def fill_test_rows(test_rows, task_to_instruction):
    filled = []
    for row in test_rows:
        task = row["task"]
        if task == "Detection":
            instruction = DETECTION_INSTRUCTION
        else:
            if task not in task_to_instruction:
                raise KeyError(f"Missing instruction mapping for task {task!r}")
            instruction = task_to_instruction[task]

        filled.append(
            {
                "instruction": instruction,
                "visual_input": row["visual_input"],
                "output": row["output"],
                "task": row["task"],
                "ID": row["ID"],
            }
        )
    return filled


def resolve_data_dir(repo_root: Path) -> Path:
    raw_dir = repo_root / "raw"
    return raw_dir if raw_dir.exists() else repo_root


def main():
    parser = argparse.ArgumentParser(description="Fill missing instructions in CUVA test annotations.")
    parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parent.parent)
    args = parser.parse_args()

    repo_root = args.repo_root.resolve()
    data_dir = resolve_data_dir(repo_root)

    gt_path = data_dir / "cuva_gt_cleaned.json"
    test_path = data_dir / "cuva_test.json"
    output_path = data_dir / "cuva_test_filled.json"

    gt_rows = load_json(gt_path)
    test_rows = load_json(test_path)

    task_to_instruction = build_instruction_map(gt_rows)
    test_tasks = {row["task"] for row in test_rows}
    unknown_tasks = test_tasks - EXPECTED_TEST_TASKS
    if unknown_tasks:
        raise ValueError(f"Unexpected tasks in test set: {sorted(unknown_tasks)}")

    filled_rows = fill_test_rows(test_rows, task_to_instruction)
    dump_json(output_path, filled_rows)

    print(f"Wrote {len(filled_rows)} rows to {output_path}")


if __name__ == "__main__":
    main()