Datasets:
Download benchmark.py from amazeng-co/git-jit-defects: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/datasets/amazeng-co/git-jit-defects/resolve/main/benchmark.py
- Command line
-
hf download hf://datasets/amazeng-co/git-jit-defects/benchmark.py
-
curl -L -o benchmark.py https://huggingface.co/datasets/amazeng-co/git-jit-defects/resolve/main/benchmark.py
2.61 kB
| """Baseline for the git-jit-defects dataset: LA rule vs LightGBM on 15 git-derived features, 5-fold *repo-level* CV. | |
| Usage: python benchmark.py commits.parquet | |
| """ | |
| import hashlib, sys | |
| import numpy as np, polars as pl, lightgbm as lgb | |
| from sklearn.metrics import roc_auc_score | |
| HAND = ["l_nfiles", "l_la", "l_ld", "l_hunks", "ndirs", "nexts", "test_ratio", "l_a_prior_n", "a_prior_rate", "l_a_days", | |
| "msg_len", "msg_ref", "hour", "dow", "repo_rate"] | |
| PARAMS = dict(objective="binary", learning_rate=0.05, num_leaves=63, min_data_in_leaf=100, feature_fraction=0.8, | |
| bagging_fraction=0.8, bagging_freq=1, verbose=-1) | |
| def load(path): | |
| df = pl.read_parquet(path).sort(["repo", "t"]) | |
| # repo base rate from the repo's OWN past only (expanding mean, shifted): computable from local git at inference | |
| df = df.with_columns(repo_rate=((pl.col("y").cum_sum().shift(1, fill_value=0) + 1) / (pl.int_range(pl.len()) + 4)).over("repo")) | |
| return df.with_columns( | |
| **{f"l_{c}": pl.col(c).cast(pl.Float64).log1p() for c in ["nfiles", "la", "ld", "hunks", "a_prior_n", "a_days"]}, | |
| hour=(pl.col("t") // 3600) % 24, dow=(pl.col("t") // 86400) % 7, | |
| fold=pl.col("repo").map_elements(lambda r: int(hashlib.md5(r.encode()).hexdigest(), 16) % 5, return_dtype=pl.Int64)) | |
| if __name__ == "__main__": | |
| df = load(sys.argv[1]) | |
| X, y, t = df.select(HAND).to_numpy(), df["y"].to_numpy(), df["t"].to_numpy() | |
| folds, repos, la = df["fold"].to_numpy(), df["repo"].to_numpy(), df["la"].to_numpy().astype(float) | |
| p = np.zeros(len(y)) | |
| for k in range(5): | |
| te = folds == k; tr_all = ~te | |
| cut = np.quantile(t[tr_all], 0.85); tr = tr_all & (t < cut); va = tr_all & (t >= cut) # temporal early-stop split | |
| m = lgb.train(PARAMS, lgb.Dataset(X[tr], y[tr]), 2000, valid_sets=[lgb.Dataset(X[va], y[va])], | |
| callbacks=[lgb.early_stopping(50, verbose=False)]) | |
| p[te] = m.predict(X[te]) | |
| ok = [r for r in set(repos) if 0 < y[repos == r].mean() < 1] | |
| a_la = {r: roc_auc_score(y[repos == r], la[repos == r]) for r in ok} | |
| a_m = {r: roc_auc_score(y[repos == r], p[repos == r]) for r in ok} | |
| print(f"{len(y)} commits, {len(ok)} repos, inducing rate {y.mean():.1%}") | |
| print(f"{'method':<12}{'pooled AUC':>11}{'median repo AUC':>17}{'repos beating LA by 0.02':>26}") | |
| print(f"{'LA rule':<12}{roc_auc_score(y, la):>11.3f}{np.median(list(a_la.values())):>17.3f}") | |
| print(f"{'GBM hand':<12}{roc_auc_score(y, p):>11.3f}{np.median(list(a_m.values())):>17.3f}" | |
| f"{np.mean([a_m[r] > a_la[r] + 0.02 for r in ok]):>26.0%}") | |