File size: 4,593 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
#!/usr/bin/env python3
"""Object-diversity analysis of the 783 accepted-question pool.

Per-type diversity SIGNATURE (the object(s) a question is fundamentally about):
  count   -> singleton: target_ref_folder            (the counted object)
  find    -> singleton: queried_ref_folder           (the searched object)
  beneath -> duplet:    (cover_folder, buried_target_folder)
  compare -> triplet:   frozenset(3 marked obj_folders)

Reports how concentrated the pool is on repeated signatures, and the union of
unique object folders — the raw material for a diversity-maximizing selection.
"""
import json, glob, os
from collections import Counter, defaultdict

BANK="approved-questions/bank"

def snap_folder_map(ep_dir):
    """name -> folder from the snapshot objects (authoritative folder ids)."""
    sp=os.path.join(ep_dir,"scene_snapshot","scene_snapshot.json")
    m={}
    if os.path.isfile(sp):
        s=json.load(open(sp))
        for o in (s.get("objects") or []):
            if o.get("name") and o.get("folder"):
                m[o["name"]]=o["folder"]
    return m

def episodes(ct):
    for rj in sorted(glob.glob(f"{BANK}/ep_*_{ct}/review.json")):
        yield os.path.dirname(rj), json.load(open(rj))

# ── build signatures ──────────────────────────────────────────────────
sig = defaultdict(dict)      # ct -> {ep_name: signature}
all_folders_by_type = defaultdict(set)
all_folders = set()

# count / find : singleton folder
for ct, key in [("count","target_ref_folder"), ("find","queried_ref_folder")]:
    for ep_dir, d in episodes(ct):
        q=d.get("question") or {}
        fol=q.get(key)
        sig[ct][os.path.basename(ep_dir)]=fol
        if fol: all_folders_by_type[ct].add(fol); all_folders.add(fol)

# beneath : (cover_folder, buried_target_folder)
for ep_dir, d in episodes("beneath"):
    q=d.get("question") or {}
    fmap=snap_folder_map(ep_dir)
    cover=fmap.get(q.get("cover_object"))
    tgt=fmap.get(q.get("target_name"))
    sig["beneath"][os.path.basename(ep_dir)]=(cover,tgt)
    for f in (cover,tgt):
        if f: all_folders_by_type["beneath"].add(f); all_folders.add(f)

# compare : triplet of marked folders
for ep_dir, d in episodes("compare"):
    q=d.get("question") or {}
    ml=q.get("marked_labels") or {}
    folders=tuple(sorted(v.get("obj_folder") for v in ml.values() if v.get("obj_folder")))
    sig["compare"][os.path.basename(ep_dir)]=folders
    for f in folders:
        if f: all_folders_by_type["compare"].add(f); all_folders.add(f)

# ── report ────────────────────────────────────────────────────────────
print("="*72)
print("PER-TYPE SIGNATURE REUSE  (how concentrated is the pool?)")
print("="*72)
for ct in ["count","find","beneath","compare"]:
    sigs=list(sig[ct].values())
    n=len(sigs)
    c=Counter(s for s in sigs if s not in (None,(None,None),()))
    n_unique=len(c)
    # histogram: how many signatures appear k times
    hist=Counter(c.values())
    max_reuse=max(c.values()) if c else 0
    # how many episodes are "unique-signature" (appear exactly once)
    n_once=sum(v for k,v in hist.items() if k==1)
    print(f"\n{ct.upper()}  ({n} episodes)")
    print(f"  distinct signatures      : {n_unique}")
    print(f"  episodes w/ unique sig    : {n_once}  ({100*n_once//max(n,1)}%)")
    print(f"  max reuse of one signature: {max_reuse}")
    print(f"  reuse histogram (k:sigs)  : {dict(sorted(hist.items()))}")
    # top offenders
    top=c.most_common(5)
    def short(s):
        if isinstance(s,tuple): return " + ".join(os.path.basename(x or "?") for x in s)
        return os.path.basename(s or "?")
    print(f"  most-reused signatures    :")
    for s,k in top:
        if k>1: print(f"      {k:>2}x  {short(s)}")

print("\n"+"="*72)
print("UNIQUE OBJECT FOLDERS")
print("="*72)
for ct in ["count","find","beneath","compare"]:
    print(f"  {ct:9s}: {len(all_folders_by_type[ct])} unique object folders")
print(f"  {'UNION':9s}: {len(all_folders)} unique object folders across all types")

# cross-type sharing
print("\n"+"="*72)
print("OBJECT SHARING ACROSS TYPES (folders used in N types)")
print("="*72)
fol_types=defaultdict(set)
for ct in ["count","find","beneath","compare"]:
    for f in all_folders_by_type[ct]: fol_types[f].add(ct)
byn=Counter(len(v) for v in fol_types.values())
print("  #folders used in exactly N types:", dict(sorted(byn.items())))