Datasets:
Tasks:
Text Classification
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
Tags:
code-review
defect-detection
software-engineering
label-noise
uncertainty-quantification
python
License:
File size: 2,020 Bytes
ecaa1ff 7deba50 ecaa1ff 7deba50 ecaa1ff 7deba50 ecaa1ff 7deba50 ecaa1ff 7deba50 ecaa1ff 7deba50 ecaa1ff 7deba50 | 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 | """Metrics for the benchmark, with the sklearn calling convention.
Every metric is called ``metric(y_true, y_pred)`` on 1-D arrays over candidate nodes --
by default the nodes of every change pooled together, with ``pooled=False`` the nodes
of one change at a time -- so any scikit-learn classification metric drops in unchanged
and CoReDD stays metric-agnostic. This module only keeps what scikit-learn does not
provide: the ``rank`` marker, ``mrr``, and an ``auroc`` wrapper that is NaN where
``roc_auc_score`` raises, so a degenerate change is dropped from a per-case average
rather than aborting the run.
A metric is either **label** (``y_pred`` is boolean: which nodes were predicted) or
**rank** (``y_pred`` is a per-node score). The ``rank`` marker tells the evaluator to
hand the metric the node scores rather than a boolean mask. Pass ``sklearn.metrics``
functions directly for label metrics; they are treated as label by default.
"""
from __future__ import annotations
import numpy as np
from sklearn.metrics import roc_auc_score
def rank(func):
"""Mark a metric as consuming per-node scores rather than a boolean mask."""
func.rank = True
return func
def is_rank(metric) -> bool:
"""Whether a metric expects scores (``rank``) instead of a boolean prediction."""
return bool(getattr(metric, "rank", False))
@rank
def mrr(y_true, scores) -> float:
"""Reciprocal rank of the first true positive under the score order; NaN if none."""
y_true = np.asarray(y_true, dtype=bool)
scores = np.asarray(scores, dtype=float)
order = np.argsort(-scores, kind="stable")
ranked = y_true[order]
if not ranked.any():
return float("nan")
return 1.0 / (int(np.argmax(ranked)) + 1)
@rank
def auroc(y_true, scores) -> float:
"""ROC AUC; NaN when there is no positive or no negative to separate."""
y_true = np.asarray(y_true, dtype=bool)
if not y_true.any() or y_true.all():
return float("nan")
return float(roc_auc_score(y_true, scores))
|