tier_label, model_label, condition_label, and model_order duplicated the numeric codes; SPSS value labels already carry the names. model is now a numeric code (0=Bandit, 1=Semgrep, 2=DeepSeek V4.1 Flash, 3=Argon) with value labels in the .sav files and .sps syntax. Long file drops from 38 to 34 variables, summary from 30 to 26.
267 lines
8.5 KiB
Python
267 lines
8.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Convert the combined SPSS CSV exports to native SPSS .sav files."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
from pathlib import Path
|
|
|
|
import pandas as pd
|
|
import pyreadstat
|
|
|
|
BASE_DIR = Path(__file__).resolve().parents[1]
|
|
SPSS_DIR = BASE_DIR / "validation" / "spss_all"
|
|
|
|
CATEGORY_LABELS = {
|
|
0: "None",
|
|
1: "pathtraver",
|
|
2: "sqli",
|
|
3: "xpathi",
|
|
4: "xxe",
|
|
5: "cmdi",
|
|
6: "deserialization",
|
|
7: "xss",
|
|
8: "weakrand",
|
|
9: "trustbound",
|
|
10: "hash",
|
|
11: "codeinj",
|
|
12: "securecookie",
|
|
13: "redirect",
|
|
14: "ldapi",
|
|
99: "other",
|
|
}
|
|
|
|
TIER_LABELS = {
|
|
0: "Traditional SAST",
|
|
1: "Open-weight",
|
|
2: "Frontier",
|
|
}
|
|
|
|
CONDITION_LABELS = {0: "Original", 1: "Obfuscated v2", 2: "Comments removed"}
|
|
|
|
MODEL_LABELS = {
|
|
0: "Bandit",
|
|
1: "Semgrep",
|
|
2: "DeepSeek V4.1 Flash",
|
|
3: "Gemini 4 Argon",
|
|
}
|
|
|
|
BINARY_LABELS = {0: "No", 1: "Yes"}
|
|
|
|
|
|
def labels_from_codebook(path: Path) -> dict[str, str]:
|
|
with path.open(encoding="utf-8", newline="") as handle:
|
|
return {
|
|
row["variable"]: row["label"]
|
|
for row in csv.DictReader(handle)
|
|
}
|
|
|
|
|
|
def build_value_labels(columns: list[str], *, wide: bool = False) -> dict:
|
|
value_labels: dict[str, dict] = {}
|
|
for column in columns:
|
|
if column in {"tier"}:
|
|
value_labels[column] = TIER_LABELS
|
|
elif column in {"model"}:
|
|
value_labels[column] = MODEL_LABELS
|
|
elif column in {"condition"}:
|
|
value_labels[column] = CONDITION_LABELS
|
|
elif column in {"expected_category_code", "predicted_category_code"}:
|
|
value_labels[column] = CATEGORY_LABELS
|
|
elif column in {"token_data_available"}:
|
|
value_labels[column] = BINARY_LABELS
|
|
elif column == "in_scope":
|
|
value_labels[column] = BINARY_LABELS
|
|
elif column == "expected_vuln":
|
|
value_labels[column] = BINARY_LABELS
|
|
elif wide and (
|
|
column.endswith("_predicted_vuln")
|
|
or column.endswith("_correct")
|
|
or column.endswith("_strict_correct")
|
|
or column.endswith("_tp")
|
|
or column.endswith("_fp")
|
|
or column.endswith("_tn")
|
|
or column.endswith("_fn")
|
|
):
|
|
value_labels[column] = BINARY_LABELS
|
|
elif column in {
|
|
"predicted_vuln",
|
|
"correct_flag",
|
|
"tp",
|
|
"fp",
|
|
"tn",
|
|
"fn",
|
|
"category_correct",
|
|
"cwe_correct",
|
|
"strict_correct",
|
|
"wrong_category_tp",
|
|
"parse_ok",
|
|
}:
|
|
value_labels[column] = BINARY_LABELS
|
|
return value_labels
|
|
|
|
|
|
def write_long() -> None:
|
|
path = SPSS_DIR / "sast_all_long.csv"
|
|
df = pd.read_csv(path, keep_default_na=False)
|
|
labels = labels_from_codebook(SPSS_DIR / "codebook.csv")
|
|
column_labels = [labels.get(column, column) for column in df.columns]
|
|
pyreadstat.write_sav(
|
|
df,
|
|
SPSS_DIR / "sast_all_long.sav",
|
|
column_labels=column_labels,
|
|
variable_value_labels=build_value_labels(list(df.columns)),
|
|
file_label="SAST and LLM evaluation: long format",
|
|
)
|
|
print(f"wrote {SPSS_DIR / 'sast_all_long.sav'} ({len(df)} rows)")
|
|
|
|
|
|
def wide_column_labels(columns: list[str]) -> list[str]:
|
|
detector_labels = {
|
|
"bandit": "Bandit",
|
|
"semgrep": "Semgrep",
|
|
"deepseek": "DeepSeek V4.1 Flash",
|
|
"argon": "Gemini 4 Argon",
|
|
}
|
|
base = {
|
|
"sample_id": "Benchmark test identifier",
|
|
"in_scope": "In prompt target scope",
|
|
"expected_vuln": "Ground-truth vulnerability",
|
|
"expected_category": "Ground-truth category",
|
|
"expected_category_code": "Ground-truth category code",
|
|
"expected_cwe": "Ground-truth CWE",
|
|
"expected_cwe_num": "Ground-truth CWE number",
|
|
}
|
|
result: list[str] = []
|
|
for column in columns:
|
|
if column in base:
|
|
result.append(base[column])
|
|
continue
|
|
matched = False
|
|
for prefix, label in detector_labels.items():
|
|
for suffix, word in (
|
|
("orig", "Original"),
|
|
("obf", "Obfuscated"),
|
|
("comments", "Comments removed"),
|
|
):
|
|
start = f"{prefix}_{suffix}_"
|
|
if column.startswith(start):
|
|
metric = column[len(start) :]
|
|
metric_labels = {
|
|
"predicted_vuln": "predicted vulnerability",
|
|
"correct": "correct",
|
|
"strict_correct": "strict correct",
|
|
"tp": "TP",
|
|
"fp": "FP",
|
|
"tn": "TN",
|
|
"fn": "FN",
|
|
"total_tokens": "total tokens",
|
|
"latency_ms": "latency (ms)",
|
|
"findings": "findings",
|
|
}
|
|
result.append(f"{word} {label} {metric_labels.get(metric, metric)}")
|
|
matched = True
|
|
break
|
|
if matched:
|
|
break
|
|
if not matched:
|
|
result.append(column)
|
|
return result
|
|
|
|
|
|
def write_wide() -> None:
|
|
path = SPSS_DIR / "sast_all_wide.csv"
|
|
df = pd.read_csv(path, keep_default_na=False)
|
|
pyreadstat.write_sav(
|
|
df,
|
|
SPSS_DIR / "sast_all_wide.sav",
|
|
column_labels=wide_column_labels(list(df.columns)),
|
|
variable_value_labels=build_value_labels(list(df.columns), wide=True),
|
|
file_label="SAST and LLM evaluation: wide paired format",
|
|
)
|
|
print(f"wrote {SPSS_DIR / 'sast_all_wide.sav'} ({len(df)} rows)")
|
|
|
|
|
|
def write_summary() -> None:
|
|
path = SPSS_DIR / "sast_all_summary.csv"
|
|
df = pd.read_csv(path, keep_default_na=False)
|
|
labels = {
|
|
"tier": "Capability tier",
|
|
"model": "Detector",
|
|
"condition": "Dataset condition",
|
|
"n_all": "Samples in run",
|
|
"n_in_scope": "Samples in target scope",
|
|
"tp": "True positives",
|
|
"fp": "False positives",
|
|
"tn": "True negatives",
|
|
"fn": "False negatives",
|
|
"accuracy": "Flag accuracy",
|
|
"strict_accuracy": "Strict accuracy (flag + CWE)",
|
|
"precision": "Precision",
|
|
"recall": "Recall",
|
|
"fpr": "False-positive rate",
|
|
"tnr": "True-negative rate",
|
|
"balanced_accuracy": "Balanced accuracy",
|
|
"f1": "F1",
|
|
"mcc": "Matthews correlation",
|
|
"category_accuracy_tp": "Category accuracy among TP",
|
|
"cwe_accuracy_tp": "CWE accuracy among TP",
|
|
"wrong_category_tp": "Wrong-category true positives",
|
|
"total_tokens_final": "Final-attempt tokens",
|
|
"total_tokens_all_attempts": "Tokens across all attempts",
|
|
"mean_latency_ms_in_scope": "Mean latency (ms), in-scope",
|
|
"api_calls_all_attempts": "API calls across all attempts",
|
|
"token_data_available": "Token/latency data available",
|
|
}
|
|
pyreadstat.write_sav(
|
|
df,
|
|
SPSS_DIR / "sast_all_summary.sav",
|
|
column_labels=[labels.get(column, column) for column in df.columns],
|
|
variable_value_labels=build_value_labels(list(df.columns)),
|
|
file_label="SAST and LLM evaluation: aggregate summary",
|
|
)
|
|
print(f"wrote {SPSS_DIR / 'sast_all_summary.sav'} ({len(df)} rows)")
|
|
|
|
|
|
def write_category() -> None:
|
|
path = SPSS_DIR / "sast_all_category.csv"
|
|
df = pd.read_csv(path, keep_default_na=False)
|
|
labels = {
|
|
"tier": "Capability tier",
|
|
"model": "Detector",
|
|
"condition": "Dataset condition",
|
|
"expected_category": "Ground-truth category",
|
|
"expected_category_code": "Ground-truth category code",
|
|
"n": "Samples",
|
|
"tp": "True positives",
|
|
"fp": "False positives",
|
|
"tn": "True negatives",
|
|
"fn": "False negatives",
|
|
"accuracy": "Accuracy",
|
|
"precision": "Precision",
|
|
"recall": "Recall",
|
|
"f1": "F1",
|
|
}
|
|
pyreadstat.write_sav(
|
|
df,
|
|
SPSS_DIR / "sast_all_category.sav",
|
|
column_labels=[labels.get(column, column) for column in df.columns],
|
|
variable_value_labels=build_value_labels(list(df.columns)),
|
|
file_label="SAST and LLM evaluation: per-category metrics",
|
|
)
|
|
print(f"wrote {SPSS_DIR / 'sast_all_category.sav'} ({len(df)} rows)")
|
|
|
|
|
|
def main() -> int:
|
|
if not SPSS_DIR.is_dir():
|
|
raise SystemExit(f"Missing folder: {SPSS_DIR}")
|
|
write_long()
|
|
write_wide()
|
|
write_summary()
|
|
write_category()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|