File size: 8,765 Bytes
b78ff28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580f4e5
b78ff28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49442cb
b78ff28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580f4e5
 
 
 
 
 
 
 
 
 
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
"""Tests for submission bundle validation."""

import zipfile
from pathlib import Path

import pandas as pd

from plug_challenge.benchmarks import BENCHMARK_NAMES, ID_COLUMN, get
from plug_challenge.validation import bundle_members, validate_bundle

from .conftest import write_bundle


def _errors_for(result, benchmark: str) -> list[str]:
    return [e.message for e in result.errors if e.benchmark == benchmark]


def test_valid_bundle_passes(bundle: Path, expected_ids):
    result = validate_bundle(bundle, expected_ids)
    assert result.ok, result.report()
    assert set(result.frames) == set(BENCHMARK_NAMES)


def test_bundle_must_be_a_zip(tmp_path: Path, expected_ids):
    loose = tmp_path / "megascale.csv"
    loose.write_text("a,b\n1,2\n")
    result = validate_bundle(loose, expected_ids)
    assert not result.ok
    assert "single .zip" in result.report()


def test_missing_file_is_rejected(predictions, tmp_path: Path, expected_ids):
    del predictions["megascale"]
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert not result.ok
    assert "megascale" in result.report()


def test_partial_bundle_allowed_when_not_required(predictions, tmp_path: Path, expected_ids):
    del predictions["megascale"]
    result = validate_bundle(
        write_bundle(predictions, tmp_path / "b.zip"), expected_ids, require_complete=False
    )
    # Scorable: the absent benchmark is a warning, not a blocking error...
    assert result.ok
    assert [w.benchmark for w in result.warnings] == ["bundle"]
    assert "megascale" in result.report()
    # ...and every other benchmark still parsed.
    assert "megascale" not in result.frames
    assert len(result.frames) == len(BENCHMARK_NAMES) - 1


def test_partial_bundle_still_rejects_a_real_error(predictions, tmp_path: Path, expected_ids):
    """A partial bundle is forgiven an absent benchmark, never a malformed one."""
    del predictions["megascale"]
    predictions["atlas"]["pred"] = 1.0  # constant column
    result = validate_bundle(
        write_bundle(predictions, tmp_path / "b.zip"), expected_ids, require_complete=False
    )
    assert not result.ok
    assert any("constant" in e.message for e in result.errors)


def test_directory_prefixed_and_macos_entries_are_handled(predictions, tmp_path: Path):
    path = tmp_path / "nested.zip"
    with zipfile.ZipFile(path, "w") as archive:
        archive.writestr("__MACOSX/._megascale.csv", "junk")
        for name, frame in predictions.items():
            archive.writestr(f"plug_submission/{name}.csv", frame.to_csv(index=False))
    assert set(bundle_members(path)) == set(BENCHMARK_NAMES)


def test_parquet_files_are_accepted(predictions, tmp_path: Path, expected_ids):
    path = tmp_path / "parquet.zip"
    with zipfile.ZipFile(path, "w") as archive:
        for name, frame in predictions.items():
            member = tmp_path / f"{name}.parquet"
            frame.to_parquet(member, index=False)
            archive.write(member, arcname=f"{name}.parquet")
    result = validate_bundle(path, expected_ids)
    assert result.ok, result.report()


def test_missing_column_is_reported(predictions, tmp_path: Path, expected_ids):
    predictions["megascale"] = predictions["megascale"].drop(columns=["pred"])
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("Missing required column" in m for m in _errors_for(result, "megascale"))


def test_missing_and_unknown_ids_are_reported(predictions, tmp_path: Path, expected_ids):
    frame = predictions["ppi"].copy()
    frame.loc[0, ID_COLUMN] = "not-a-real-id"
    predictions["ppi"] = frame
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    messages = " ".join(_errors_for(result, "ppi"))
    assert "no prediction" in messages
    assert "not scored in this track" in messages


def test_duplicate_ids_are_reported(predictions, tmp_path: Path, expected_ids):
    frame = predictions["fireprot"]
    predictions["fireprot"] = pd.concat([frame, frame.head(1)], ignore_index=True)
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("duplicated" in m for m in _errors_for(result, "fireprot"))


def test_constant_prediction_column_is_rejected(predictions, tmp_path: Path, expected_ids):
    predictions["atlas"]["pred"] = 1.0
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("constant" in m for m in _errors_for(result, "atlas"))


def test_nan_prediction_is_rejected(predictions, tmp_path: Path, expected_ids):
    predictions["dms"].loc[0, "pred"] = float("nan")
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("missing value" in m for m in _errors_for(result, "dms"))


def test_infinite_prediction_is_rejected(predictions, tmp_path: Path, expected_ids):
    predictions["lp-pdbbind"].loc[0, "pred"] = float("inf")
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("infinite" in m for m in _errors_for(result, "lp-pdbbind"))


def test_non_numeric_prediction_is_rejected(predictions, tmp_path: Path, expected_ids):
    predictions["megascale"]["pred"] = predictions["megascale"]["pred"].astype(str) + "x"
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("non-numeric" in m for m in _errors_for(result, "megascale"))


def test_out_of_range_score_is_rejected(predictions, tmp_path: Path, expected_ids):
    predictions["cafa5"].loc[0, "score"] = 1.5
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("outside [0.0, 1.0]" in m for m in _errors_for(result, "cafa5"))


def test_unknown_go_aspect_is_rejected(predictions, tmp_path: Path, expected_ids):
    predictions["cafa5"].loc[0, "aspect"] = "molecular_function"
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("unrecognised 'aspect'" in m for m in _errors_for(result, "cafa5"))


def test_duplicate_go_term_is_rejected(predictions, tmp_path: Path, expected_ids):
    frame = predictions["cafa5"]
    predictions["cafa5"] = pd.concat([frame, frame.head(1)], ignore_index=True)
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("duplicated" in m for m in _errors_for(result, "cafa5"))


def test_long_form_allows_missing_targets(predictions, tmp_path: Path, expected_ids):
    """Declining to annotate a protein is legal for cafa5 and costs only recall."""
    dropped = predictions["cafa5"][ID_COLUMN].unique()[0]
    predictions["cafa5"] = predictions["cafa5"][predictions["cafa5"][ID_COLUMN] != dropped]
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert result.ok, result.report()


def test_shs27k_needs_all_seven_mode_columns(predictions, tmp_path: Path, expected_ids):
    spec = get("shs27k")
    predictions["shs27k"] = predictions["shs27k"].drop(columns=[spec.pred_columns[3]])
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any(spec.pred_columns[3] in m for m in _errors_for(result, "shs27k"))


def test_benchmark_without_ground_truth_is_not_scored(predictions, tmp_path: Path, expected_ids):
    del expected_ids["atlas"]
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert any("not open for scoring" in m for m in _errors_for(result, "atlas"))


def test_constant_confidence_is_allowed_for_cafa5(predictions, tmp_path: Path, expected_ids):
    """Fmax sweeps a threshold rather than ranking, so "confident in all of these"
    is a legitimate CAFA submission — unlike a constant column on a rank metric.
    """
    predictions["cafa5"]["score"] = 1.0
    result = validate_bundle(write_bundle(predictions, tmp_path / "b.zip"), expected_ids)
    assert result.ok, result.report()


def test_constant_predictions_are_still_rejected_for_rank_metrics():
    assert get("megascale").needs_varying_predictions
    assert get("ppi").needs_varying_predictions
    assert not get("cafa5").needs_varying_predictions


def test_a_bundle_with_no_benchmark_file_is_rejected(tmp_path: Path, expected_ids):
    """Partial bundles are allowed, but one covering nothing is not a submission."""
    path = tmp_path / "b.zip"
    with zipfile.ZipFile(path, "w") as archive:
        archive.writestr("notes.txt", "nothing here")
    result = validate_bundle(path, expected_ids, require_complete=False)
    assert not result.ok
    assert "none of the benchmark files" in result.report()