File size: 2,402 Bytes
f03ba44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Check a ForensicBench submission file before uploading it.

Usage:
    python tools/validate_submission.py my_flags.csv --data-dir data

Expected columns: sector, document_id, scheme_type, plus replicate for the multi run format
(`rationale` is optional). Sectors: energy, healthcare, luxurygoods, manufacturing, transport.
Scheme types: fictitious_ap_disbursements, revenue_manipulation, vendor_collusion,
shadow_payroll, inventory_manipulation.
"""
import argparse, sys
import pandas as pd

SECTORS = ["energy", "healthcare", "luxurygoods", "manufacturing", "transport"]
TYPES = {"fictitious_ap_disbursements", "revenue_manipulation", "vendor_collusion", "shadow_payroll", "inventory_manipulation", "unknown"}

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("file")
    ap.add_argument("--data-dir", default="data")
    a = ap.parse_args()
    df = pd.read_csv(a.file, dtype=str) if a.file.endswith(".csv") else pd.read_json(a.file, lines=True, dtype=str)
    errors = []
    for c in ["sector", "document_id", "scheme_type"]:
        if c not in df.columns:
            errors.append(f"missing column: {c}")
    if errors:
        print("\n".join(errors)); sys.exit(1)
    bad = set(df["sector"]) - set(SECTORS)
    if bad: errors.append(f"unknown sectors: {sorted(bad)}")
    bad = set(df["scheme_type"]) - TYPES
    if bad: errors.append(f"unknown scheme types: {sorted(bad)}")
    for s in sorted(set(df["sector"]) & set(SECTORS)):
        ids = set(pd.read_parquet(f"{a.data_dir}/{s}/je_header.parquet", columns=["document_id"])["document_id"].str.lower())
        sub = df[df["sector"] == s]["document_id"].str.lower()
        unknown = set(sub) - ids
        if unknown:
            print(f"warning: {s}: {len(unknown)} document_id values are not in the ledger (they count as false positives)")
    if len(set(df["sector"]) & set(SECTORS)) < 5:
        errors.append("a submission must cover all 5 sectors")
    if "replicate" in df.columns:
        reps = df.groupby("sector")["replicate"].nunique().to_dict()
        print("replicates per sector:", reps)
        if any(v < 5 for v in reps.values()):
            print("note: the full protocol uses 5 replicates per sector (25 runs)")
    else:
        print("single run format (no replicate column)")
    if errors:
        print("\n".join(errors)); sys.exit(1)
    print("OK")

if __name__ == "__main__":
    main()