"""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