File size: 3,273 Bytes
2ef05ec | 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 | #!/usr/bin/env python3
"""Generate SecureCodePairs v1.1.0 statistics: CSV reports + ASCII bar charts (no deps)."""
import csv
import json
import os
from collections import Counter
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATASET_DIR = os.path.join(ROOT, "dataset")
STAT_DIR = os.path.join(ROOT, "statistics")
SPLITS = ("train", "validation", "test", "benchmark")
def load_all():
recs = []
for name in SPLITS:
with open(os.path.join(DATASET_DIR, f"{name}.jsonl"), encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
recs.append(json.loads(line))
return recs
def write_csv(path, rows, header):
with open(path, "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(header)
w.writerows(rows)
def bar(label, count, total, width=40):
n = max(1, int(round(count / max(total, 1) * width)))
return f"{label[:24]:<24} | {'#' * n} {count} ({count / max(total, 1) * 100:.0f}%)"
def main():
recs = load_all()
total = len(recs)
os.makedirs(STAT_DIR, exist_ok=True)
langs = Counter(r["language"] for r in recs)
fw = Counter(r["framework"] for r in recs)
sev = Counter(r["severity"] for r in recs)
cwe = Counter(r["cwe"] for r in recs)
owasp = Counter(r["owasp"] for r in recs)
diff = Counter(r["difficulty"] for r in recs)
dom = Counter(r["metadata"].get("domain", "unknown") for r in recs)
write_csv(os.path.join(STAT_DIR, "language_distribution.csv"),
[(k, v) for k, v in langs.most_common()], ["language", "count"])
write_csv(os.path.join(STAT_DIR, "framework_distribution.csv"),
[(k, v) for k, v in fw.most_common()], ["framework", "count"])
write_csv(os.path.join(STAT_DIR, "severity_distribution.csv"),
[(k, v) for k, v in sev.most_common()], ["severity", "count"])
write_csv(os.path.join(STAT_DIR, "difficulty_distribution.csv"),
[(k, v) for k, v in diff.most_common()], ["difficulty", "count"])
write_csv(os.path.join(STAT_DIR, "cwe_distribution.csv"),
[(k, v) for k, v in cwe.most_common()], ["cwe", "count"])
write_csv(os.path.join(STAT_DIR, "owasp_distribution.csv"),
[(k, v) for k, v in owasp.most_common()], ["owasp", "count"])
write_csv(os.path.join(STAT_DIR, "domain_distribution.csv"),
[(k, v) for k, v in dom.most_common()], ["domain", "count"])
# SecureCodePairs - Statistics (v1.2.0)
lines.append(f"Total records: **{total}**\n")
for title, ctr in (("Language", langs), ("Framework", fw),
("Severity", sev), ("Difficulty", diff),
("Domain", dom), ("CWE (top 25)", cwe), ("OWASP", owasp)):
lines.append(f"## {title} distribution\n")
top = ctr.most_common(25) if "CWE" in title else ctr.most_common()
for k, v in top:
lines.append(bar(k, v, total))
lines.append("")
with open(os.path.join(STAT_DIR, "charts.txt"), "w", encoding="utf-8") as f:
f.write("\n".join(lines))
print("\n".join(lines[:16]))
print("\nCSV reports + charts written to", STAT_DIR)
if __name__ == "__main__":
main()
|