Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 9,666 Bytes
b78ff28 580f4e5 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 580f4e5 b78ff28 580f4e5 b78ff28 580f4e5 b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 580f4e5 b78ff28 49442cb 580f4e5 49442cb 580f4e5 49442cb 580f4e5 49442cb b78ff28 580f4e5 b78ff28 49442cb b78ff28 | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | """Tests for the PLUG Score and the leaderboard build."""
import numpy as np
import pytest
from plug_challenge import aggregate, scoring
from plug_challenge.benchmarks import BENCHMARKS, get
from .conftest import make_predictions
N = len(BENCHMARKS)
SPEARMAN = get("megascale").primary
AUROC = get("ppi").primary
FMAX = get("cafa5").primary
def test_normalise_puts_chance_at_zero_and_perfect_at_one():
assert aggregate.normalise(0.0, SPEARMAN) == pytest.approx(0.0)
assert aggregate.normalise(0.5, AUROC) == pytest.approx(0.0)
assert aggregate.normalise(0.0, FMAX) == pytest.approx(0.0)
for metric in (SPEARMAN, AUROC, FMAX):
assert aggregate.normalise(1.0, metric) == pytest.approx(1.0)
assert aggregate.normalise(0.75, AUROC) == pytest.approx(0.5)
assert aggregate.normalise(0.4, FMAX) == pytest.approx(0.4)
def test_normalise_takes_a_correlation_by_its_magnitude():
# A strongly anti-correlated model is very predictive once reversed.
assert aggregate.normalise(-0.99, SPEARMAN) == pytest.approx(0.99)
assert aggregate.normalise(-0.3, get("dms").primary) == pytest.approx(0.3)
def test_normalise_does_not_clip_below_chance():
assert aggregate.normalise(0.25, AUROC) == pytest.approx(-0.5)
assert aggregate.normalise(0.0, AUROC) == pytest.approx(-1.0)
def test_normalise_passes_nan_through():
assert np.isnan(aggregate.normalise(float("nan"), AUROC))
def test_plug_score_of_a_perfect_submission_is_one(truth):
perfect = make_predictions(truth, np.random.default_rng(0), noise=0.0)
scores = scoring.score_bundle(perfect, truth, BENCHMARKS)
assert aggregate.plug_score(scores) == pytest.approx(1.0)
def test_plug_score_counts_an_absent_benchmark_as_chance(predictions, truth):
full = scoring.score_bundle(predictions, truth, BENCHMARKS)
partial = {name: value for name, value in full.items() if name != "atlas"}
atlas = get("atlas").primary
dropped = aggregate.normalise(full["atlas"][f"{atlas.key}_mean"], atlas)
# The mean stays over all eleven: atlas now contributes 0 instead of its score.
assert aggregate.plug_score(partial) == pytest.approx(
aggregate.plug_score(full) - dropped / N
)
def test_plug_score_of_nothing_is_chance():
assert aggregate.plug_score({}) == pytest.approx(0.0)
def test_flatten_and_column_agree_on_naming(predictions, truth):
scores = scoring.score_bundle(predictions, truth, BENCHMARKS)
flat = aggregate.flatten(scores)
for spec in BENCHMARKS:
assert aggregate.column(spec.name, spec.primary) in flat
def _entry(
username: str, submitted_at: str, plug_score: float, track: str = "supervised", **metrics
) -> dict:
return {
"username": username,
"submitted_at": submitted_at,
"track": track,
"plug_score": plug_score,
"used_proprietary_data": False,
"open_source_code": True,
"model_report_link": "https://example.org",
**metrics,
}
def test_leaderboard_is_ranked_by_plug_score():
spearman = aggregate.column("megascale", get("megascale").primary)
board = aggregate.build_leaderboard(
[
_entry("b", "2026-01-01T00:00:00Z", 0.4, **{spearman: 0.1}),
_entry("a", "2026-01-01T00:00:00Z", 0.9, **{spearman: 0.8}),
_entry("c", "2026-01-01T00:00:00Z", 0.6, **{spearman: 0.5}),
]
)
assert list(board["username"]) == ["a", "c", "b"]
assert list(board["rank"]) == [1, 2, 3]
def test_only_the_latest_submission_per_user_is_ranked():
spearman = aggregate.column("megascale", SPEARMAN)
board = aggregate.build_leaderboard(
[
_entry("a", "2026-01-01T00:00:00Z", 0.9, **{spearman: 0.9}),
_entry("a", "2026-02-01T00:00:00Z", 0.3, **{spearman: 0.3}),
]
)
assert len(board) == 1
assert board.loc[0, "plug_score"] == pytest.approx(0.3 / N)
def test_plug_score_is_recomputed_from_the_metrics_not_read_back():
spearman = aggregate.column("megascale", SPEARMAN)
auroc = aggregate.column("ppi", AUROC)
# 0.6 is what an older normalisation stored; the board must not trust it.
board = aggregate.build_leaderboard(
[_entry("a", "2026-01-01T00:00:00Z", 0.6, **{spearman: -0.4, auroc: 0.7})]
)
assert board.loc[0, "plug_score"] == pytest.approx((0.4 + 0.4) / N)
assert board.loc[0, "megascale__score"] == pytest.approx(0.4)
assert board.loc[0, "ppi__score"] == pytest.approx(0.4)
def test_a_negative_correlation_outranks_a_weaker_positive_one():
spearman = aggregate.column("megascale", SPEARMAN)
board = aggregate.build_leaderboard(
[
_entry("positive", "2026-01-01T00:00:00Z", 0.0, **{spearman: 0.5}),
_entry("negative", "2026-01-01T00:00:00Z", 0.0, **{spearman: -0.99}),
]
)
by_user = board.set_index("username")
assert by_user.loc["negative", "megascale__rank"] == 1
assert list(board["username"]) == ["negative", "positive"]
def test_a_below_chance_auroc_lowers_the_plug_score():
auroc = aggregate.column("ppi", AUROC)
board = aggregate.build_leaderboard(
[_entry("a", "2026-01-01T00:00:00Z", 0.0, **{auroc: 0.3})]
)
assert board.loc[0, "plug_score"] == pytest.approx(-0.4 / N)
def test_tracks_are_ranked_separately():
spearman = aggregate.column("megascale", SPEARMAN)
entries = [
_entry("a", "2026-01-01T00:00:00Z", 0.0, "supervised", **{spearman: 0.2}),
_entry("a", "2026-01-02T00:00:00Z", 0.0, "unsupervised", **{spearman: 0.8}),
_entry("b", "2026-01-01T00:00:00Z", 0.0, "unsupervised", **{spearman: 0.5}),
]
supervised = aggregate.build_leaderboard(entries, track="supervised")
unsupervised = aggregate.build_leaderboard(entries, track="unsupervised")
# One user's later entry to another track does not replace their entry here.
assert list(supervised["username"]) == ["a"]
assert supervised.loc[0, "plug_score"] == pytest.approx(0.2 / N)
assert list(unsupervised["username"]) == ["a", "b"]
def test_an_entry_without_a_track_counts_as_supervised():
entry = _entry("a", "2026-01-01T00:00:00Z", 0.5)
del entry["track"]
assert len(aggregate.build_leaderboard([entry], track="supervised")) == 1
assert aggregate.build_leaderboard([entry], track="unsupervised").empty
def test_per_benchmark_rank_and_mean_rank_are_added():
mega = aggregate.column("megascale", get("megascale").primary)
ppi = aggregate.column("ppi", get("ppi").primary)
board = aggregate.build_leaderboard(
[
_entry("a", "2026-01-01T00:00:00Z", 0.7, **{mega: 0.9, ppi: 0.5}),
_entry("b", "2026-01-01T00:00:00Z", 0.6, **{mega: 0.2, ppi: 0.9}),
]
)
by_user = board.set_index("username")
assert by_user.loc["a", "megascale__rank"] == 1
assert by_user.loc["b", "ppi__rank"] == 1
# Each is first on one benchmark and second on the other, and both rank last (2nd)
# on the nine benchmarks neither covered.
assert by_user.loc["a", "mean_rank"] == pytest.approx((1 + 2 + 2 * (N - 2)) / N)
assert by_user.loc["b", "mean_rank"] == pytest.approx(by_user.loc["a", "mean_rank"])
def test_a_single_benchmark_entry_leads_its_tab_but_not_the_overall_board():
mega = aggregate.column("megascale", SPEARMAN)
broad = {aggregate.column(spec.name, spec.primary): 0.7 if spec.primary.chance == 0.5
else 0.4 for spec in BENCHMARKS}
board = aggregate.build_leaderboard(
[
_entry("specialist", "2026-01-01T00:00:00Z", 0.0, **{mega: 0.95}),
_entry("generalist", "2026-01-01T00:00:00Z", 0.0, **broad),
]
)
by_user = board.set_index("username")
assert list(board["username"]) == ["generalist", "specialist"]
assert by_user.loc["specialist", "plug_score"] == pytest.approx(0.95 / N)
assert by_user.loc["specialist", "megascale__rank"] == 1
assert by_user.loc["specialist", "ppi__rank"] == 2
assert by_user.loc["specialist", "benchmarks_scored"] == 1
def test_benchmarks_scored_counts_only_finite_metrics():
mega = aggregate.column("megascale", get("megascale").primary)
ppi = aggregate.column("ppi", get("ppi").primary)
board = aggregate.build_leaderboard(
[
_entry("a", "2026-01-01T00:00:00Z", 0.7, **{mega: 0.9, ppi: 0.5}),
_entry("b", "2026-01-01T00:00:00Z", 0.6, **{mega: 0.2, ppi: float("nan")}),
]
)
by_user = board.set_index("username")
assert by_user.loc["a", "benchmarks_scored"] == 2
assert by_user.loc["b", "benchmarks_scored"] == 1
def test_empty_leaderboard_has_the_expected_columns():
board = aggregate.build_leaderboard([])
assert board.empty
assert "plug_score" in board.columns
def test_a_full_submission_round_trips_into_a_leaderboard_row(predictions, truth):
"""The end-to-end shape: score a bundle, flatten it, rank it."""
scores = scoring.score_bundle(predictions, truth, BENCHMARKS)
entry = {
"username": "someone",
"submitted_at": "2026-05-01T12:00:00Z",
"plug_score": aggregate.plug_score(scores),
"used_proprietary_data": False,
"open_source_code": False,
"model_report_link": "Not submitted",
**aggregate.flatten(scores),
}
board = aggregate.build_leaderboard([entry])
assert len(board) == 1
assert board.loc[0, "benchmarks_scored"] == len(BENCHMARKS)
assert 0.0 < board.loc[0, "plug_score"] <= 1.0
assert board.loc[0, "plug_score"] == pytest.approx(aggregate.plug_score(scores))
for spec in BENCHMARKS:
assert f"{spec.name}__rank" in board.columns
|