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