"""Tests for the bare metric functions.""" import numpy as np import pytest from plug_challenge import metrics def test_spearman_perfect_and_inverted(): y = np.arange(20.0) assert metrics.spearman(y, y) == pytest.approx(1.0) assert metrics.spearman(y, -y) == pytest.approx(-1.0) def test_spearman_undefined_on_constant_input(): y = np.arange(10.0) assert not np.isfinite(metrics.spearman(y, np.ones(10))) assert not np.isfinite(metrics.spearman(y[:1], y[:1])) def test_spearman_ignores_nan_pairs(): y = np.array([1.0, 2.0, 3.0, np.nan]) p = np.array([1.0, 2.0, 3.0, 99.0]) assert metrics.spearman(y, p) == pytest.approx(1.0) def test_auroc_and_single_class(): y = np.array([0, 0, 1, 1]) assert metrics.auroc(y, np.array([0.1, 0.2, 0.8, 0.9])) == pytest.approx(1.0) assert not np.isfinite(metrics.auroc(np.ones(4), np.array([0.1, 0.2, 0.8, 0.9]))) def test_macro_auroc_skips_single_class_columns(): y = np.array([[0, 1], [1, 1], [0, 1], [1, 1]]) scores = np.array([[0.1, 0.5], [0.9, 0.5], [0.2, 0.5], [0.8, 0.5]]) # Column 0 is perfectly ranked; column 1 is all-positive and skipped. assert metrics.macro_auroc(y, scores) == pytest.approx(1.0) def test_grouped_spearman_averages_groups_not_rows(): # Group "a" is perfectly ranked, group "b" is perfectly inverted; each group # counts once, so the mean is 0 despite "b" having more rows. y = np.array([1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 4.0]) p = np.array([1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0]) groups = np.array(["a", "a", "a", "b", "b", "b", "b"]) assert metrics.grouped_spearman(y, p, groups) == pytest.approx(0.0) def test_grouped_auroc_skips_single_class_groups(): y = np.array([0, 1, 1, 1]) p = np.array([0.1, 0.9, 0.5, 0.5]) groups = np.array(["a", "a", "b", "b"]) assert metrics.grouped_auroc(y, p, groups) == pytest.approx(1.0) def test_fmax_perfect_prediction(): y = np.array([[1, 1, 0], [0, 1, 1]], dtype=bool) scores = np.where(y, 1.0, 0.0) assert metrics.fmax(y, scores)["fmax"] == pytest.approx(1.0) def test_fmax_drops_unannotated_targets(): y = np.array([[1, 0], [0, 0]], dtype=bool) scores = np.array([[1.0, 0.0], [1.0, 1.0]]) result = metrics.fmax(y, scores) assert result["targets"] == 1 assert result["fmax"] == pytest.approx(1.0) def test_fmax_long_matches_dense_fmax(): """The production long-form scorer must agree with the dense port.""" rng = np.random.default_rng(7) n_targets, n_terms = 12, 9 y = rng.random((n_targets, n_terms)) < 0.3 scores = np.round(rng.random((n_targets, n_terms)), 2) dense = metrics.fmax(y, scores) target_index, flat_scores, is_true = [], [], [] for target in range(n_targets): for term in range(n_terms): target_index.append(target) flat_scores.append(scores[target, term]) is_true.append(y[target, term]) long = metrics.fmax_long( target_index=np.array(target_index), scores=np.array(flat_scores), is_true=np.array(is_true), true_per_target=y.sum(axis=1), ) assert long["fmax"] == pytest.approx(dense["fmax"]) assert long["targets"] == dense["targets"] def test_fmax_long_only_counts_predicted_rows(): """A term left out of the submission costs recall but not precision.""" # Two targets, two annotations each; only the first annotation is predicted. long = metrics.fmax_long( target_index=np.array([0, 1]), scores=np.array([1.0, 1.0]), is_true=np.array([True, True]), true_per_target=np.array([2.0, 2.0]), ) assert long["precision"] == pytest.approx(1.0) assert long["recall"] == pytest.approx(0.5) assert long["fmax"] == pytest.approx(2 / 3) def test_bootstrap_zero_samples_is_the_point_estimate(): values = np.arange(10.0) mean, std = metrics.bootstrap(lambda rows: float(values[rows].mean()), 0, len(values)) assert mean == pytest.approx(values.mean()) assert std == 0.0 def test_bootstrap_is_reproducible_and_spread_is_positive(): values = np.arange(50.0) def metric(rows): return float(values[rows].mean()) first = metrics.bootstrap(metric, 50, len(values), seed=3) second = metrics.bootstrap(metric, 50, len(values), seed=3) assert first == second assert first[1] > 0 def test_bootstrap_resamples_whole_groups(): """With one group, every group-resample is the same data, so spread is zero.""" values = np.arange(20.0) groups = np.zeros(20) _, std = metrics.bootstrap( lambda rows: float(values[rows].mean()), 25, len(values), groups=groups ) assert std == pytest.approx(0.0)