code-pii / notebooks /make_stats_summary.py
josh-hud's picture
Release v3 cross-document coreference dataset
80a02b9 verified
Raw History Blame Contribute Delete
1.93 kB
#!/usr/bin/env python3
"""Aggregate part-*.stats.json + summary.json of a generated dataset dir into notebooks/stats_summary.json."""
import json, glob, collections, sys, os
d = sys.argv[1]; out = sys.argv[2] if len(sys.argv) > 2 else os.path.join(os.path.dirname(__file__), "stats_summary.json")
C = collections.Counter
agg = {k: C() for k in ("label", "value_type", "lang", "context", "cluster_kind", "records_by_kind", "cluster_size", "entity_type", "docs_per_entity", "mentions_per_entity", "surface_form")}; tot = C(); per = []
for p in sorted(glob.glob(os.path.join(d, "part-*.stats.json"))):
s = json.load(open(p))
for k in agg: agg[k].update(s[k])
for k in ("files", "injected_files", "clean_files", "bytes", "spans", "negs", "skipped_license", "skipped_size", "clusters", "entities", "owned_entities", "recurring_entities", "multi_entities"): tot[k] += s.get(k, 0)
per.append({k: s[k] for k in ("shard", "files", "bytes", "spans", "negs", "seconds")})
summ = json.load(open(os.path.join(d, "summary.json")))
res = {"note": f"Aggregated from per-shard stats emitted by generate_dataset.py for the full run ({summ['shards']} shards).", "summary": summ, "totals": dict(tot),
"label": dict(agg["label"]), "value_type": {k: v for k, v in agg["value_type"].items() if not k.startswith("neg:")},
"hard_negative_type": {k[4:]: v for k, v in agg["value_type"].items() if k.startswith("neg:")}, "language": dict(agg["lang"]), "context": dict(agg["context"]),
"cluster_kind": dict(agg["cluster_kind"]), "records_by_kind": dict(agg["records_by_kind"]), "cluster_size": dict(agg["cluster_size"]),
"entity_type": dict(agg["entity_type"]), "docs_per_entity": dict(agg["docs_per_entity"]), "mentions_per_entity": dict(agg["mentions_per_entity"]),
"surface_form": dict(agg["surface_form"]), "per_shard": per}
json.dump(res, open(out, "w"), indent=1); print(out, tot["files"], tot["bytes"])