Download scripts/build_corpus.py from Dhruv1000/tracegrpo: direct link, hf CLI and curl.
- Browser
- Download file 3.5 kB
-
https://huggingface.co/Dhruv1000/tracegrpo/resolve/main/scripts/build_corpus.py
- Command line
-
hf download hf://Dhruv1000/tracegrpo/scripts/build_corpus.py
-
curl -L -o build_corpus.py https://huggingface.co/Dhruv1000/tracegrpo/resolve/main/scripts/build_corpus.py
3.5 kB
| """Phase 1 §3.2: builds data/processed/corpus.parquet from all available | |
| sources, applies de-duplication, validates against the unified schema, and | |
| writes the Phase 1 data summary + data card. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import pandas as pd | |
| from src.data.dedup import exact_dedup_pairs | |
| from src.data.load_primevul import normalize_primevul | |
| from src.data.load_secvuleval import normalize_secvuleval | |
| from src.data.schema import REQUIRED_COLUMNS, validate_corpus | |
| from src.utils.seeding import set_seed | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--seed", type=int, default=42) | |
| parser.add_argument("--out", default="data/processed/corpus.parquet") | |
| parser.add_argument("--results-dir", default="results/phase1") | |
| args = parser.parse_args() | |
| set_seed(args.seed) | |
| results_dir = Path(args.results_dir) | |
| results_dir.mkdir(parents=True, exist_ok=True) | |
| all_rows: list[dict] = [] | |
| per_source_stats: dict[str, dict] = {} | |
| pv_rows, pv_stats = normalize_primevul() | |
| all_rows.extend(pv_rows) | |
| per_source_stats["primevul"] = pv_stats | |
| print(f"primevul: {pv_stats}") | |
| sv_rows, sv_stats = normalize_secvuleval() | |
| all_rows.extend(sv_rows) | |
| per_source_stats["secvuleval"] = sv_stats | |
| print(f"secvuleval: {sv_stats}") | |
| deduped_rows, dedup_stats = exact_dedup_pairs(all_rows) | |
| print(f"dedup: {dedup_stats}") | |
| violations = validate_corpus(deduped_rows) | |
| violation_counts = {k: len(v) for k, v in violations.items()} | |
| print(f"schema violations: {violation_counts}") | |
| if any(violation_counts.values()): | |
| (results_dir / "schema_violations.json").write_text( | |
| json.dumps({k: v[:50] for k, v in violations.items()}, indent=2, default=str) | |
| ) | |
| print( | |
| f"WARNING: {sum(violation_counts.values())} schema violations found; " | |
| f"first 50 of each logged to {results_dir / 'schema_violations.json'}. " | |
| "Proceeding — see spec §1.6 (fail loudly, don't silently drop)." | |
| ) | |
| df = pd.DataFrame(deduped_rows) | |
| df["patch_lines_vuln"] = df["patch_lines_vuln"].apply(list) # ensure list, not ndarray | |
| for col in REQUIRED_COLUMNS: | |
| assert col in df.columns, f"missing required column: {col}" | |
| out_path = Path(args.out) | |
| out_path.parent.mkdir(parents=True, exist_ok=True) | |
| df.to_parquet(out_path, index=False) | |
| print(f"wrote {len(df)} rows to {out_path}") | |
| target_counts = df["target"].value_counts().to_dict() | |
| cwe_counts = df[df["target"] == 1]["cwe"].value_counts().head(20).to_dict() | |
| source_counts = df["source"].value_counts().to_dict() | |
| n_with_date = df["commit_date"].notna().sum() | |
| summary = { | |
| "n_rows": len(df), | |
| "n_pairs": df["pair_id"].nunique(), | |
| "target_counts": {str(k): int(v) for k, v in target_counts.items()}, | |
| "source_counts": {str(k): int(v) for k, v in source_counts.items()}, | |
| "n_with_commit_date": int(n_with_date), | |
| "date_coverage": float(n_with_date / len(df)) if len(df) else 0.0, | |
| "top_20_cwe_counts_vuln_only": {str(k): int(v) for k, v in cwe_counts.items()}, | |
| "per_source_load_stats": per_source_stats, | |
| "dedup_stats": dedup_stats, | |
| "schema_violation_counts": violation_counts, | |
| } | |
| (results_dir / "data_summary.json").write_text(json.dumps(summary, indent=2)) | |
| print(json.dumps(summary, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |