|
| 1 | +"""Tests for hidden-active recovery benchmark. |
| 2 | +
|
| 3 | +These tests verify that the pipeline recovers known-active AMPs better than |
| 4 | +a random ranker when mixed with non-active sequences. This is a key Phase 2 |
| 5 | +requirement per AGENTS.md. |
| 6 | +
|
| 7 | +No biological activity is implied or proven by these results. |
| 8 | +The benchmark only tests whether physicochemical features of known AMPs |
| 9 | +correlate with higher ensemble scores than simple non-AMP sequences. |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import json |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from openamp_foundry.benchmark.evaluate import ( |
| 18 | + benchmark_summary, |
| 19 | + enrichment_factor, |
| 20 | + recall_at_k, |
| 21 | +) |
| 22 | +from openamp_foundry.cli import main |
| 23 | +from openamp_foundry.data.loaders import load_candidates_csv |
| 24 | +from openamp_foundry.pipeline import score_candidates |
| 25 | +from openamp_foundry.selection.pareto import rank_candidates |
| 26 | + |
| 27 | + |
| 28 | +MIXED_CANDIDATES = "examples/benchmark/mixed_candidates.csv" |
| 29 | +ACTIVE_LABELS = "examples/benchmark/active_labels.csv" |
| 30 | + |
| 31 | +POSITIVE_IDS = { |
| 32 | + "BM-POS-001", |
| 33 | + "BM-POS-002", |
| 34 | + "BM-POS-003", |
| 35 | + "BM-POS-004", |
| 36 | + "BM-POS-005", |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +class TestHiddenActiveRecovery: |
| 41 | + def test_all_positives_rank_in_top_half(self): |
| 42 | + scored, _ = score_candidates(MIXED_CANDIDATES) |
| 43 | + ranked = rank_candidates(scored) |
| 44 | + n = len(ranked) |
| 45 | + top_half_ids = {item.candidate.candidate_id for item in ranked[: n // 2]} |
| 46 | + for pid in POSITIVE_IDS: |
| 47 | + assert pid in top_half_ids, f"{pid} not in top half" |
| 48 | + |
| 49 | + def test_recall_at_5_equals_1(self): |
| 50 | + """All 5 positives should appear in top-5 of 20 candidates.""" |
| 51 | + scored, _ = score_candidates(MIXED_CANDIDATES) |
| 52 | + rc = recall_at_k(scored, POSITIVE_IDS, k=5) |
| 53 | + assert rc == 1.0, f"Expected recall@5=1.0, got {rc}" |
| 54 | + |
| 55 | + def test_enrichment_factor_at_5_exceeds_2(self): |
| 56 | + """Pipeline should achieve at least 2× enrichment vs random at k=5.""" |
| 57 | + scored, _ = score_candidates(MIXED_CANDIDATES) |
| 58 | + ef = enrichment_factor(scored, POSITIVE_IDS, k=5) |
| 59 | + assert ef >= 2.0, f"Enrichment factor {ef} below minimum of 2.0" |
| 60 | + |
| 61 | + def test_pipeline_verdict_outperforms_random(self): |
| 62 | + scored, _ = score_candidates(MIXED_CANDIDATES) |
| 63 | + summary = benchmark_summary(scored, POSITIVE_IDS, ks=[5]) |
| 64 | + assert summary["verdict"] == "pipeline outperforms random" |
| 65 | + |
| 66 | + def test_negatives_score_lower_than_positives_on_average(self): |
| 67 | + scored, _ = score_candidates(MIXED_CANDIDATES) |
| 68 | + pos_scores = [ |
| 69 | + s.scores["ensemble"] |
| 70 | + for s in scored |
| 71 | + if s.candidate.candidate_id in POSITIVE_IDS |
| 72 | + ] |
| 73 | + neg_scores = [ |
| 74 | + s.scores["ensemble"] |
| 75 | + for s in scored |
| 76 | + if s.candidate.candidate_id not in POSITIVE_IDS |
| 77 | + ] |
| 78 | + assert pos_scores |
| 79 | + assert neg_scores |
| 80 | + assert sum(pos_scores) / len(pos_scores) > sum(neg_scores) / len(neg_scores) |
| 81 | + |
| 82 | + def test_bench_baseline_cli_shows_enrichment(self, tmp_path, capsys): |
| 83 | + out = str(tmp_path / "bench_report.json") |
| 84 | + ret = main([ |
| 85 | + "bench", "baseline", |
| 86 | + "--candidates", MIXED_CANDIDATES, |
| 87 | + "--positives", ACTIVE_LABELS, |
| 88 | + "--k", "5", |
| 89 | + "--out", out, |
| 90 | + ]) |
| 91 | + assert ret == 0 |
| 92 | + data = json.loads((tmp_path / "bench_report.json").read_text()) |
| 93 | + k5_result = next(r for r in data["results"] if r["k"] == 5) |
| 94 | + assert k5_result["enrichment_factor"] >= 2.0 |
| 95 | + assert data["verdict"] == "pipeline outperforms random" |
| 96 | + |
| 97 | + def test_benchmark_summary_disclaimer_present(self): |
| 98 | + scored, _ = score_candidates(MIXED_CANDIDATES) |
| 99 | + summary = benchmark_summary(scored, POSITIVE_IDS, ks=[5]) |
| 100 | + assert "disclaimer" in summary |
| 101 | + assert len(summary["disclaimer"]) > 20 |
| 102 | + |
| 103 | + def test_known_active_amps_outrank_all_repeat_sequences(self): |
| 104 | + """Biologically implausible repeat sequences should rank below AMP-like ones.""" |
| 105 | + scored, _ = score_candidates(MIXED_CANDIDATES) |
| 106 | + ranked = rank_candidates(scored) |
| 107 | + ranked_ids = [s.candidate.candidate_id for s in ranked] |
| 108 | + |
| 109 | + # All positives should appear before all-same-AA negatives |
| 110 | + for pos_id in POSITIVE_IDS: |
| 111 | + pos_rank = ranked_ids.index(pos_id) |
| 112 | + for neg_id in ["BM-NEG-001", "BM-NEG-002", "BM-NEG-004"]: |
| 113 | + neg_rank = ranked_ids.index(neg_id) |
| 114 | + assert pos_rank < neg_rank, ( |
| 115 | + f"{pos_id} (rank {pos_rank}) should outrank {neg_id} (rank {neg_rank})" |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +class TestBenchmarkDataIntegrity: |
| 120 | + def test_benchmark_csvs_exist(self): |
| 121 | + from pathlib import Path |
| 122 | + assert Path(MIXED_CANDIDATES).exists() |
| 123 | + assert Path(ACTIVE_LABELS).exists() |
| 124 | + |
| 125 | + def test_all_positive_ids_in_mixed_candidates(self): |
| 126 | + mixed = load_candidates_csv(MIXED_CANDIDATES) |
| 127 | + mixed_ids = {c.candidate_id for c in mixed} |
| 128 | + for pid in POSITIVE_IDS: |
| 129 | + assert pid in mixed_ids, f"Positive {pid} missing from mixed candidates" |
| 130 | + |
| 131 | + def test_mixed_has_both_positives_and_negatives(self): |
| 132 | + mixed = load_candidates_csv(MIXED_CANDIDATES) |
| 133 | + ids = {c.candidate_id for c in mixed} |
| 134 | + positive_overlap = ids & POSITIVE_IDS |
| 135 | + negative_only = ids - POSITIVE_IDS |
| 136 | + assert len(positive_overlap) >= 3 |
| 137 | + assert len(negative_only) >= 5 |
| 138 | + |
| 139 | + def test_active_labels_are_subset_of_mixed(self): |
| 140 | + mixed = load_candidates_csv(MIXED_CANDIDATES) |
| 141 | + active = load_candidates_csv(ACTIVE_LABELS) |
| 142 | + mixed_ids = {c.candidate_id for c in mixed} |
| 143 | + for a in active: |
| 144 | + assert a.candidate_id in mixed_ids |
0 commit comments