"""Score test-split predictions from a Spatial-Qwen bench run. This script is **evaluation-only** — it does not load the model. It consumes a `predictions.jsonl` (produced by `scripts/bench_test_generate.py` or the older `batch_bench_spatial_beats_qa.py`) plus the original QA split, joins on `pair_id`, and computes per-task metrics. Supported task_names (see `all_qa_llm_by_difficulty_v2`): estimate_azimuth / estimate_elevation - numeric angle identify_source_by_doa - single source label identify_source_by_location - single source label detect_time - one time span per event detect_source - list of (event, start, end) Key features: * Robust regex-based extractors with per-task error categories. * Optional LLM judge (OpenAI-compatible, uses `gpt4o_api.py` endpoint style) for semantic equivalence on source-identification tasks where surface-form exact match fails but the prediction might still be correct (e.g. "bell" vs "church_bell"). LLM is ALSO used as a last-resort answer-extractor for verbose generations before scoring. * Detailed parse-fail tracking: every record records a `parse_status` ∈ {"ok", "fail_regex", "fail_llm_extract", "fail_empty", "fail_no_answer_meta"}. * Aggregate report distinguishes task-correctness, parse rate, and LLM-assist rate. Usage (no LLM): python scripts/score_test_predictions.py \\ --predictions-jsonl runs/.../bench/test//predictions.jsonl \\ --qa-root /apdcephfs.../easy_filtered \\ --output-json runs/.../bench/test//result.json Usage (LLM judge on ambiguous source identification): python scripts/score_test_predictions.py \\ --predictions-jsonl ... --qa-root ... \\ --llm-judge --llm-model gemini-3.1-pro-preview \\ --llm-concurrency 8 The LLM judge is only invoked when: 1. exact match fails, 2. the task is `identify_source_by_doa` / `identify_source_by_location` (where synonyms are common) or the user passes `--llm-judge-all-tasks`. 3. both prediction and answer are non-empty after cleaning. Fully deterministic regex path still runs regardless of --llm-judge, so the non-LLM `correct` column is always populated as a fallback. """ from __future__ import annotations import argparse import json import math import os import re import sys import time from collections import defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed from dataclasses import dataclass, field from statistics import median from typing import Any, Callable, Dict, List, Optional, Tuple # --------------------------------------------------------------------------- # Regex + small parsers # --------------------------------------------------------------------------- FLOAT_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") # "0.1s to 5.8s" / "from 0.1 s to 5.8 s" / "0.1 - 5.8 seconds" TIME_SPAN_RE = re.compile( r"(?P[-+]?\d+(?:\.\d+)?)\s*(?:s|sec|seconds)?\s*(?:to|-|–|—|until)\s*(?P[-+]?\d+(?:\.\d+)?)\s*(?:s|sec|seconds)?", re.IGNORECASE, ) # Event spans embedded in a longer sentence, e.g. # "The audio contains a camera from 0.0s to 9.6s, glass from 0.0s to 0.3s, ..." EVENT_SPAN_RE = re.compile( # Captures "