File size: 7,819 Bytes
b47994e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Collate ACCEPTED (verified) questions across the three review banks and
report distributions to plan a balanced 600-question benchmark.

Banks:
  qbank    rollouts/rollout_vlm_20260827_231647   (count/find/beneath/compare)
  count_v2 rollouts/rollout_vlm_20260901_220948   (count, GT=0 hidden via :+nogt0)
  find_v2  rollouts/rollout_vlm_20260902_044842   (find, yes-only + 30 broken excl)

Authoritative decision = latest-timestamp record per episode (union across
reviewers), matching review_server.global_decisions().  edited_gt overrides
the stored gt_answer.
"""
import json, glob, os
from collections import Counter, defaultdict

BANKS = {
    "qbank":    "rollouts/rollout_vlm_20260827_231647",
    "count_v2": "rollouts/rollout_vlm_20260901_220948",
    "find_v2":  "rollouts/rollout_vlm_20260902_044842",
}
# serve-time filters (must mirror start_review_server.sh so we only count
# questions actually in the review pool)
FILTERS = {
    "count_v2": {"exclude_gt0": True},
    "find_v2":  {"yes_only": True, "use_exclude_file": True},
    "qbank":    {},
}

QCANON = {"counting":"count","existence":"find","under_object":"beneath",
          "marked_comparison":"compare"}

def canon(qt): return QCANON.get(qt, qt)

def load_exclude(rd):
    p=os.path.join(rd,"EXCLUDE_EPISODES.txt"); out=set()
    if os.path.isfile(p):
        for line in open(p):
            line=line.split("#",1)[0].strip()
            if line: out.add(line)
    return out

def is_yes(q):
    t=str(q.get("gt_answer_text") or "").lower()
    if not t:
        gt=str(q.get("gt_answer") or ""); om=q.get("option_map") or {}
        t=str(om.get(gt) or om.get(gt.upper()) or om.get(gt.lower()) or "").lower()
    return t.startswith("y")

def latest_decisions(rd):
    """episode -> full latest decision record (by timestamp)."""
    out={}
    for f in glob.glob(os.path.join(rd,"reviews","*.jsonl")):
        if os.path.basename(f)=="claims.jsonl": continue
        for line in open(f):
            line=line.strip()
            if not line: continue
            try: d=json.loads(line)
            except: continue
            ep=d.get("episode")
            if not ep: continue
            ts=d.get("timestamp","")
            if ep not in out or ts>=out[ep].get("timestamp",""):
                out[ep]=d
    return out

def episode_review(rd, ep):
    """Load an episode's review.json (search top-level + worker_*)."""
    for cand in [os.path.join(rd,ep,"review.json")] + \
                glob.glob(os.path.join(rd,"worker_*",ep,"review.json")):
        if os.path.isfile(cand):
            return json.load(open(cand))
    return None

def obj_folders(review):
    """Unique asset folders used in the scene (from scene_objects/objects)."""
    fs=set()
    for o in (review.get("scene_objects") or review.get("objects") or []):
        # scene_objects carry ref image paths -> derive folder;
        # objects carry name only; fall back to name
        imgs=o.get("images") or []
        if imgs:
            # "_ref_views/<source__folder>/0.jpg"
            part=imgs[0].split("/")
            if len(part)>=2: fs.add(part[1])
        elif o.get("folder"): fs.add(o["folder"])
        elif o.get("name"): fs.add("name::"+o["name"])
    return fs

# ---- collate -------------------------------------------------------------
accepted = defaultdict(list)   # canon_type -> [ (bank, ep, review, decision) ]
compare_inverse = Counter()    # inverse_status among accepted compare
per_bank_counts = defaultdict(Counter)

for bank, rd in BANKS.items():
    if not os.path.isdir(rd):
        print(f"!! missing {rd}"); continue
    excl = load_exclude(rd) if FILTERS.get(bank,{}).get("use_exclude_file") else set()
    dec = latest_decisions(rd)
    for ep, d in dec.items():
        if d.get("status")!="accepted":
            continue
        if ep in excl:
            continue
        rv = episode_review(rd, ep)
        if rv is None:
            continue
        q = rv.get("question") or {}
        ct = canon(q.get("question_type"))
        # apply serve filters
        if FILTERS.get(bank,{}).get("exclude_gt0") and str(q.get("gt_answer"))=="0":
            continue
        if FILTERS.get(bank,{}).get("yes_only") and not is_yes(q):
            continue
        accepted[ct].append((bank, ep, rv, d))
        per_bank_counts[bank][ct]+=1
        if ct=="compare":
            compare_inverse[d.get("inverse_status") or "not_annotated"]+=1

# ---- report --------------------------------------------------------------
print("="*70)
print("ACCEPTED (verified) QUESTION COUNTS  — final benchmark candidate pool")
print("="*70)
grand=0
for ct in ["count","find","beneath","compare"]:
    n=len(accepted[ct]); grand+=n
    srcs=Counter(b for b,_,_,_ in accepted[ct])
    print(f"  {ct:9s}: {n:4d}   (by bank: {dict(srcs)})")
print(f"  {'TOTAL':9s}: {grand:4d}")

print("\n" + "="*70)
print("COMPARE — inverse-question annotation")
print("="*70)
tot_cmp=len(accepted['compare'])
inv_acc=compare_inverse.get('accepted',0)
print(f"  accepted compare questions            : {tot_cmp}")
print(f"  ...with inverse ALSO accepted (good)  : {inv_acc}")
print(f"  ...inverse rejected                    : {compare_inverse.get('rejected',0)}")
print(f"  ...inverse not annotated               : {compare_inverse.get('not_annotated',0)}")
print(f"  → usable as TWO questions (fwd+inverse): {inv_acc} pairs "
      f"(= {tot_cmp}+{inv_acc} = {tot_cmp+inv_acc} total if both counted)")

# ---- GT distribution per type -------------------------------------------
def gt_of(rv, d):
    q=rv.get("question") or {}
    if d.get("edited_gt") is not None: return str(d["edited_gt"])
    return str(q.get("gt_answer"))

print("\n" + "="*70)
print("GROUND-TRUTH DISTRIBUTION (accepted only; edited_gt applied)")
print("="*70)

# count / beneath -> integer or option
for ct in ["count"]:
    c=Counter(gt_of(rv,d) for _,_,rv,d in accepted[ct])
    print(f"  {ct}: {dict(sorted(c.items(), key=lambda x:str(x[0])))}")

# find -> yes/no (yes-only expected)
c=Counter("yes" if is_yes(rv.get("question") or {}) else "no"
          for _,_,rv,d in accepted["find"])
print(f"  find (yes/no): {dict(c)}")

# beneath -> gt is object name; report count of distinct answers + option letter
bl=Counter(gt_of(rv,d) for _,_,rv,d in accepted["beneath"])
print(f"  beneath: {len(bl)} distinct GT answers among {sum(bl.values())} (top5: {bl.most_common(5)})")

# compare -> A/B/C letter + polarity
cl=Counter(gt_of(rv,d) for _,_,rv,d in accepted["compare"])
pol=Counter((rv.get('question') or {}).get('comparison_extremum')
            for _,_,rv,d in accepted["compare"])
print(f"  compare letter: {dict(sorted(cl.items()))}")
print(f"  compare extremum (largest/smallest): {dict(pol)}")

# ---- unique-object usage -------------------------------------------------
print("\n" + "="*70)
print("UNIQUE OBJECTS USED (across accepted scenes, per type)")
print("="*70)
all_objs=set()
for ct in ["count","find","beneath","compare"]:
    objs=set()
    for _,_,rv,_ in accepted[ct]:
        objs|=obj_folders(rv)
    all_objs|=objs
    print(f"  {ct:9s}: {len(objs)} unique object folders across {len(accepted[ct])} scenes")
print(f"  {'UNION':9s}: {len(all_objs)} unique object folders across the whole pool")

# ---- target-object diversity (the queried/counted object) ---------------
print("\n" + "="*70)
print("TARGET / QUERIED OBJECT DIVERSITY (the object the question is ABOUT)")
print("="*70)
for ct in ["count","find","beneath"]:
    tgt=Counter()
    for _,_,rv,_ in accepted[ct]:
        q=rv.get("question") or {}
        name=q.get("target_name") or q.get("queried_name")
        if name: tgt[name]+=1
    print(f"  {ct:9s}: {len(tgt)} distinct target objects; "
          f"most reused: {tgt.most_common(3)}")

print("\n(done)")