| |
| """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"]) |
|
|
| |
| 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() |
|
|