Datasets:
Formats:
parquet
Languages:
English
Size:
10M - 100M
Tags:
biology
chemistry
drug-discovery
clinical-trials
protein-protein-interaction
gene-essentiality
License:
File size: 11,006 Bytes
6d1bbc7 | 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | """Tests for GE LLM evaluation module (parsing + metrics)."""
import pytest
from negbiodb_depmap.llm_eval import (
compute_all_ge_llm_metrics,
evaluate_ge_l1,
evaluate_ge_l2,
evaluate_ge_l3,
evaluate_ge_l4,
parse_ge_l1_answer,
parse_ge_l2_response,
parse_ge_l3_judge_scores,
parse_ge_l4_answer,
)
# ── L1 parsing ────────────────────────────────────────────────────────────
class TestParseL1:
def test_single_letter(self):
assert parse_ge_l1_answer("C") == "C"
def test_lowercase(self):
assert parse_ge_l1_answer("b") == "B"
def test_answer_prefix(self):
assert parse_ge_l1_answer("Answer: A") == "A"
def test_parenthesized(self):
assert parse_ge_l1_answer("(D)") == "D"
def test_with_explanation(self):
assert parse_ge_l1_answer("C\nBecause the gene is non-essential...") == "C"
def test_empty(self):
assert parse_ge_l1_answer("") is None
def test_no_valid_letter(self):
# Text without any A-D characters
assert parse_ge_l1_answer("no help here with this question") is None
def test_embedded_letter(self):
assert parse_ge_l1_answer("I think B is correct") == "B"
class TestEvaluateL1:
def test_perfect_accuracy(self):
preds = ["A", "B", "C", "D"]
gold = ["A", "B", "C", "D"]
result = evaluate_ge_l1(preds, gold)
assert result["accuracy"] == 1.0
assert result["valid_rate"] == 1.0
def test_zero_accuracy(self):
preds = ["B", "C", "D", "A"]
gold = ["A", "B", "C", "D"]
result = evaluate_ge_l1(preds, gold)
assert result["accuracy"] == 0.0
def test_invalid_responses(self):
# "nonsense" has no A-D letters, so it's truly unparseable
preds = ["nonsense", "A"]
gold = ["A", "A"]
result = evaluate_ge_l1(preds, gold)
assert result["n_valid"] == 1
assert result["valid_rate"] == 0.5
def test_empty_predictions(self):
result = evaluate_ge_l1([], [])
assert result["accuracy"] == 0.0
# ── L2 parsing ────────────────────────────────────────────────────────────
class TestParseL2:
def test_valid_json(self):
raw = '{"genes": [{"gene_name": "TP53"}], "total_genes_mentioned": 1, "screen_type": "CRISPR"}'
result = parse_ge_l2_response(raw)
assert result is not None
assert result["total_genes_mentioned"] == 1
def test_markdown_json(self):
raw = '```json\n{"genes": [], "total_genes_mentioned": 0, "screen_type": "RNAi"}\n```'
result = parse_ge_l2_response(raw)
assert result is not None
def test_json_with_text(self):
raw = 'Here is the extraction:\n{"genes": [], "total_genes_mentioned": 0, "screen_type": "CRISPR"}'
result = parse_ge_l2_response(raw)
assert result is not None
def test_invalid_json(self):
assert parse_ge_l2_response("not json at all") is None
def test_empty(self):
assert parse_ge_l2_response("") is None
class TestEvaluateL2:
def test_perfect_parse(self):
preds = ['{"genes": [], "total_genes_mentioned": 0, "screen_type": "CRISPR"}']
gold = [{"genes": [], "total_genes_mentioned": 0, "screen_type": "CRISPR"}]
result = evaluate_ge_l2(preds, gold)
assert result["parse_rate"] == 1.0
assert result["schema_compliance"] == 1.0
def test_missing_fields(self):
preds = ['{"genes": []}']
gold = [{"genes": [], "total_genes_mentioned": 0, "screen_type": "CRISPR"}]
result = evaluate_ge_l2(preds, gold)
assert result["schema_compliance"] == 0.0
def test_unparseable(self):
preds = ["invalid"]
gold = [{"genes": []}]
result = evaluate_ge_l2(preds, gold)
assert result["parse_rate"] == 0.0
def test_essentiality_accuracy_correct(self):
preds = ['{"genes": [{"gene_name": "TP53", "essentiality_status": "non-essential"}], "total_genes_mentioned": 1, "screen_type": "CRISPR"}']
gold = [{"genes": [{"gene_name": "TP53", "essentiality_status": "non-essential"}], "total_genes_mentioned": 1, "screen_type": "CRISPR"}]
result = evaluate_ge_l2(preds, gold)
assert result["essentiality_accuracy"] == 1.0
assert result["essentiality_n"] == 1
def test_essentiality_accuracy_wrong(self):
preds = ['{"genes": [{"gene_name": "TP53", "essentiality_status": "essential"}], "total_genes_mentioned": 1, "screen_type": "CRISPR"}']
gold = [{"genes": [{"gene_name": "TP53", "essentiality_status": "non-essential"}], "total_genes_mentioned": 1, "screen_type": "CRISPR"}]
result = evaluate_ge_l2(preds, gold)
assert result["essentiality_accuracy"] == 0.0
def test_legacy_essentiality_findings_key(self):
# gold uses old schema key 'essentiality_findings' — should still work
preds = ['{"genes": [{"gene_name": "BRCA1", "essentiality_status": "non-essential"}], "total_genes_mentioned": 1, "screen_type": "CRISPR"}']
gold = [{"essentiality_findings": [{"gene_name": "BRCA1", "essentiality_status": "non-essential"}], "total_gene_count": 1, "screen_type": "CRISPR"}]
result = evaluate_ge_l2(preds, gold)
assert result["essentiality_accuracy"] == 1.0
# ── L3 parsing ────────────────────────────────────────────────────────────
class TestParseL3:
def test_standard_format(self):
raw = """biological_plausibility: 4
pathway_reasoning: 3
context_specificity: 5
mechanistic_depth: 4"""
result = parse_ge_l3_judge_scores(raw)
assert result is not None
assert result["biological_plausibility"] == 4.0
assert result["context_specificity"] == 5.0
def test_json_format(self):
raw = '{"biological_plausibility": 3, "pathway_reasoning": 4, "context_specificity": 3, "mechanistic_depth": 2}'
result = parse_ge_l3_judge_scores(raw)
assert result is not None
assert result["pathway_reasoning"] == 4.0
def test_empty(self):
assert parse_ge_l3_judge_scores("") is None
class TestEvaluateL3:
def test_basic_evaluation(self):
judge_outputs = [
"biological_plausibility: 4\npathway_reasoning: 3\ncontext_specificity: 5\nmechanistic_depth: 4",
"biological_plausibility: 3\npathway_reasoning: 4\ncontext_specificity: 3\nmechanistic_depth: 3",
]
result = evaluate_ge_l3(judge_outputs)
assert result["n_parsed"] == 2
assert result["biological_plausibility_mean"] == 3.5
assert result["overall_mean"] > 0
def test_no_parseable(self):
result = evaluate_ge_l3(["invalid", "garbage"])
assert result["n_parsed"] == 0
assert result["overall_mean"] == 0.0
# ── L4 parsing ────────────────────────────────────────────────────────────
class TestParseL4:
def test_tested(self):
assert parse_ge_l4_answer("tested") == "tested"
def test_untested(self):
assert parse_ge_l4_answer("untested") == "untested"
def test_with_evidence(self):
assert parse_ge_l4_answer("tested\nThis gene is in DepMap 22Q2") == "tested"
def test_case_insensitive(self):
assert parse_ge_l4_answer("UNTESTED") == "untested"
def test_embedded(self):
assert parse_ge_l4_answer("I believe this is untested") == "untested"
def test_tested_priority_over_untested(self):
# "untested" contains "tested" — check correct parsing
assert parse_ge_l4_answer("untested because...") == "untested"
def test_empty(self):
assert parse_ge_l4_answer("") is None
class TestEvaluateL4:
def test_perfect(self):
preds = ["tested", "untested", "tested"]
gold = ["tested", "untested", "tested"]
result = evaluate_ge_l4(preds, gold)
assert result["accuracy"] == 1.0
assert result["mcc"] == 1.0
def test_all_wrong(self):
preds = ["untested", "tested"]
gold = ["tested", "untested"]
result = evaluate_ge_l4(preds, gold)
assert result["accuracy"] == 0.0
def test_distribution(self):
preds = ["tested", "tested", "untested"]
gold = ["tested", "tested", "untested"]
result = evaluate_ge_l4(preds, gold)
assert result["prediction_distribution"]["tested"] == 2
assert result["prediction_distribution"]["untested"] == 1
# ── Dispatch ──────────────────────────────────────────────────────────────
class TestDispatch:
def test_l1_dispatch(self):
result = compute_all_ge_llm_metrics("ge-l1", ["A", "B"], ["A", "B"])
assert "accuracy" in result
def test_l4_dispatch(self):
result = compute_all_ge_llm_metrics("ge-l4", ["tested"], ["tested"])
assert "accuracy" in result
def test_l2_dispatch(self):
pred = '{"genes": ["TP53"], "total_genes_mentioned": 1, "screen_type": "CRISPR"}'
gold = [{"genes": ["TP53"], "total_genes_mentioned": 1, "screen_type": "CRISPR"}]
result = compute_all_ge_llm_metrics("ge-l2", [pred], gold)
assert "parse_rate" in result
assert "schema_compliance" in result
assert "field_f1" in result
assert result["parse_rate"] == 1.0
def test_l2_dispatch_with_full_record(self):
pred = '{"genes": ["TP53"], "total_genes_mentioned": 1, "screen_type": "CRISPR"}'
gold_records = [
{
"question_id": "GEL2-001",
"task": "ge-l2",
"gold_extraction": {
"genes": ["TP53"],
"total_genes_mentioned": 1,
"screen_type": "CRISPR",
},
"gold_answer": "non-essential",
}
]
result = compute_all_ge_llm_metrics("ge-l2", [pred], gold_records)
assert result["parse_rate"] == 1.0
assert "field_f1" in result
def test_l3_dispatch(self):
result = compute_all_ge_llm_metrics(
"ge-l3",
["This gene is non-essential because it has redundant paralogs."],
[{}],
)
assert "n_parsed" in result
assert result["n_parsed"] == 0
assert result["overall_mean"] == 0.0
def test_invalid_task(self):
with pytest.raises(ValueError):
compute_all_ge_llm_metrics("ge-l99", [], [])
|