File size: 9,161 Bytes
6e07f8f 8536af5 6e07f8f 8536af5 6e07f8f aa4e7ae 8536af5 6e07f8f | 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | #!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
from pathlib import Path
from typing import Any
RUNNER_TASK_BY_SMALL = {
"Action Order Inference": "action",
"Connectivity": "cognitivemap",
"Long-Term Navigation": "cognitivemap",
"Regional Boundary": "cognitivemap",
"Traversable Passage": "cognitivemap",
"Category Ambiguity": "counting",
"Counting w Occlusion": "counting",
"Illumination Variability": "counting",
"Merged Observation": "counting",
"Spatial Segmentation": "counting",
"Structural Enclosure": "counting",
"Dimensional Size": "size",
"Spatial Distance": "distance",
"Material Transparency": "transparent",
"Partial Occlusion": "occlusion",
"View Hallucination": "angle_confusion",
"Inclined Plane": "slope",
"Stacking & Stability": "stacking",
"Deformable": "deformable",
"Liquid Volume": "pour",
"Rigid Containment": "storage",
"Geometric Configuration": "triangle",
"Linear Alignment": "line",
"Physical Contact": "touching",
"Correspondence": "mirror",
"Reflection Authoring": "mirror",
"Spatial Relations": "mirror",
"Agent Observation": "multiagent",
"Unobserved Change": "unobserved_changes",
}
LIGHT_METADATA_KEYS = {
"task",
"task_type",
"task_family",
"task_category",
"layout",
"run_idx",
"seed",
"floor_name",
"floor",
"question_index",
"question_id",
"n_objects",
"object_category",
"obj_category",
"container_cat",
"small_obj_cat",
"slope_angle_deg",
"static_friction",
"dynamic_friction",
"ground_truth_slid",
"ground_truth_fallen",
"true_count",
"proximity_thresh",
}
def text(value: Any) -> str | None:
if value is None:
return None
if isinstance(value, str):
value = value.strip()
return value or None
return str(value)
def as_list(value: Any) -> list[Any]:
if value is None:
return []
if isinstance(value, list):
return value
return [value]
def normalize_answer(raw: Any) -> str | int | float | bool | list[Any] | dict[str, Any] | None:
if raw is None:
return None
if isinstance(raw, str):
return raw.strip()
return raw
def json_text(value: Any) -> str:
return json.dumps(value, ensure_ascii=False, separators=(",", ":"))
def answer_text(answer: Any) -> str | None:
if answer is None:
return None
if isinstance(answer, str):
return answer.strip() or None
if isinstance(answer, (int, float, bool)):
return str(answer)
return json_text(answer)
def answer_type(answer: Any, options: list[Any]) -> str:
if isinstance(answer, bool):
return "boolean"
if isinstance(answer, int):
return "count"
if isinstance(answer, list):
return "sequence"
if options:
return "choice"
if isinstance(answer, str) and answer.strip().isdigit():
return "count"
return "freeform"
def collect_image_paths(payload: dict[str, Any]) -> list[str]:
paths: list[str] = []
def add(value: Any) -> None:
if isinstance(value, str) and value:
paths.append(value)
elif isinstance(value, list):
for item in value:
add(item)
elif isinstance(value, dict):
for key in ("image_path", "rgb", "view_file", "reference_image_path"):
add(value.get(key))
add(payload.get("image_paths"))
add(payload.get("reference_image_paths"))
add(payload.get("qa", {}).get("view_file") if isinstance(payload.get("qa"), dict) else None)
qd = payload.get("question_data") if isinstance(payload.get("question_data"), dict) else {}
render = qd.get("render") if isinstance(qd.get("render"), dict) else {}
add(render.get("image_paths"))
add(render.get("primary_image"))
add(render.get("target_closeups"))
add(qd.get("initial_view"))
add(qd.get("overview_view"))
add(qd.get("path_views"))
render_top = payload.get("render") if isinstance(payload.get("render"), dict) else {}
add(render_top.get("main_view"))
add(render_top.get("room_view"))
add(render_top.get("gt_view"))
# Preserve order while dropping duplicates.
seen: set[str] = set()
unique: list[str] = []
for path in paths:
if path not in seen:
seen.add(path)
unique.append(path)
return unique
def extract_question_answer(payload: dict[str, Any]) -> tuple[str | None, Any, list[Any]]:
qd = payload.get("question_data") if isinstance(payload.get("question_data"), dict) else {}
qa = payload.get("qa") if isinstance(payload.get("qa"), dict) else {}
question = (
text(payload.get("_question"))
or text(qa.get("question"))
or text(qd.get("question"))
or text((payload.get("_entry") or {}).get("question") if isinstance(payload.get("_entry"), dict) else None)
)
options: list[Any] = []
for candidate in (qd.get("options"), qa.get("options"), qa.get("choices")):
if isinstance(candidate, list):
options = candidate
break
answer = None
if qa.get("answer_A") is not None or qa.get("answer_C") is not None:
answer = {
key: qa[key]
for key in ("answer_A", "answer_C")
if qa.get(key) is not None
}
for candidate in (
payload.get("_ground_truth"),
qd.get("answer"),
qa.get("answer"),
qa.get("answer_text"),
qa.get("answer_count"),
qa.get("answer_label"),
payload.get("answer"),
(payload.get("_entry") or {}).get("ground_truth") if isinstance(payload.get("_entry"), dict) else None,
):
if answer is not None:
break
if candidate is not None:
answer = candidate
break
return question, normalize_answer(answer), options
def compact_metadata(payload: dict[str, Any]) -> dict[str, Any]:
metadata = {key: payload[key] for key in LIGHT_METADATA_KEYS if key in payload}
qd = payload.get("question_data") if isinstance(payload.get("question_data"), dict) else None
qa = payload.get("qa") if isinstance(payload.get("qa"), dict) else None
if qd is not None:
metadata["question_data"] = {
key: qd[key]
for key in ("task_type", "case", "case_id", "case_type", "count_target", "count_unit")
if key in qd
}
if qa is not None:
metadata["qa"] = {
key: qa[key]
for key in ("answer_A", "answer_C", "answer_option_id", "answer_text", "answer_count", "answer_label")
if key in qa
}
if payload.get("_missing") is not None:
metadata["_missing"] = payload.get("_missing")
if isinstance(payload.get("_entry"), dict):
metadata["_entry"] = payload["_entry"]
return metadata
def record_for_file(path: Path, json_root: Path, row_id: str) -> dict[str, Any]:
rel = path.relative_to(json_root)
parts = rel.parts
if len(parts) < 5:
raise ValueError(f"Unexpected question path: {rel}")
big_task, small_task, scene_from_path, room_from_path = parts[:4]
payload = json.loads(path.read_text(encoding="utf-8"))
question, answer, options = extract_question_answer(payload)
scene = text(payload.get("scene")) or scene_from_path
room = text(payload.get("room")) or room_from_path
return {
"id": row_id,
"big_task": big_task,
"small_task": small_task,
"runner_task": RUNNER_TASK_BY_SMALL.get(small_task),
"scene": scene,
"room": room,
"question": question,
"answer": answer_text(answer),
"answer_type": answer_type(answer, options),
"options_json": json_text(options),
"image_paths_json": json_text(collect_image_paths(payload)),
"metadata_json": json_text(compact_metadata(payload)),
}
def main() -> int:
parser = argparse.ArgumentParser(description="Export a fixed-column HF/Croissant-friendly questions table.")
parser.add_argument("--json-root", type=Path, default=Path("dataset/json"))
parser.add_argument("--output", type=Path, default=Path("data/questions.jsonl"))
args = parser.parse_args()
json_root = args.json_root
question_files = sorted(p for p in json_root.rglob("*.json") if p.parent != json_root)
if not question_files:
raise SystemExit(f"No question JSON files found under {json_root}")
id_width = max(4, len(str(len(question_files))))
records = [
record_for_file(path, json_root, f"{index:0{id_width}d}")
for index, path in enumerate(question_files, start=1)
]
args.output.parent.mkdir(parents=True, exist_ok=True)
with args.output.open("w", encoding="utf-8") as f:
for record in records:
f.write(json.dumps(record, ensure_ascii=False, separators=(",", ":")))
f.write("\n")
print(f"Wrote {len(records)} records to {args.output}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
|