Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 9,420 Bytes
b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 580f4e5 b78ff28 580f4e5 b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb 580f4e5 b78ff28 49442cb b78ff28 580f4e5 b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 49442cb b78ff28 | 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | """Command line entry points for the PLUG challenge.
Participant-facing::
plug-challenge template ./my_submission --track supervised # an empty bundle skeleton
plug-challenge validate my_submission.zip --track supervised # check it before uploading
Organiser-facing::
plug-challenge score my_submission.zip --track supervised --ground-truth ./gt
plug-challenge rebuild # leaderboard <- scores/**
``validate`` needs only the public manifests, so a participant can run it
offline against a downloaded copy of the data repo (``--data-dir``) or let it
fetch the manifests from the Hub.
"""
import argparse
import json
import sys
from pathlib import Path
import pandas as pd
from loguru import logger
from . import aggregate, scoring, store
from .benchmarks import BENCHMARKS, ID_COLUMN, TRACKS, select_track
from .validation import validate_bundle
def _add_track(parser: argparse.ArgumentParser) -> None:
"""Add the ``--track`` argument every participant-facing command takes."""
parser.add_argument(
"--track",
choices=TRACKS,
required=True,
help="supervised: the Lo-Hi test rows; unsupervised: every row, unsplit",
)
def _add_common(parser: argparse.ArgumentParser) -> None:
"""Add the arguments shared by ``validate`` and ``score``."""
parser.add_argument("bundle", type=Path, help="The submission .zip to check")
_add_track(parser)
parser.add_argument(
"--data-dir",
type=Path,
default=None,
help="Directory holding <benchmark>/manifest.parquet (default: fetch from the Hub)",
)
parser.add_argument(
"--require-complete",
action="store_true",
help="Fail on a bundle missing any benchmark (the Space accepts partial bundles, "
"and counts each missing benchmark as chance)",
)
def build_parser() -> argparse.ArgumentParser:
"""Build the argument parser.
Returns
-------
argparse.ArgumentParser
The configured parser with one subcommand per entry point.
"""
parser = argparse.ArgumentParser(
prog="plug-challenge",
description="Build, check and score PLUG challenge submissions.",
epilog=(
"typical use:\n"
" plug-challenge template ./my_submission --track supervised\n"
" cd my_submission && zip ../plug_submission.zip *.csv && cd ..\n"
" plug-challenge validate plug_submission.zip --track supervised\n"
),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
sub = parser.add_subparsers(dest="command", required=True)
_add_common(sub.add_parser("validate", help="Validate a submission bundle"))
score = sub.add_parser("score", help="Validate and score a bundle (organisers)")
_add_common(score)
score.add_argument(
"--ground-truth",
type=Path,
required=True,
help="Directory holding <benchmark>.parquet ground-truth files, every row with its split",
)
score.add_argument(
"--bootstrap",
type=int,
default=0,
help="Bootstrap resamples per metric (0 = point estimates only)",
)
sub.add_parser("rebuild", help="Rebuild the leaderboard file from scores/** (organisers)")
template = sub.add_parser("template", help="Write an empty submission bundle skeleton")
template.add_argument(
"out_dir", type=Path, help="Directory to write the per-benchmark CSVs into"
)
_add_track(template)
template.add_argument(
"--data-dir",
type=Path,
default=None,
help="Directory holding the manifests (default: fetch from the Hub)",
)
return parser
def _expected_ids(track: str, data_dir: Path | None) -> dict[str, set[str]]:
"""Load one track's per-benchmark id sets from the manifests."""
ids = store.manifest_ids(track, local_dir=data_dir)
if not ids:
logger.error(
"No manifests found. Pass --data-dir pointing at a local copy of "
"the challenge data repo, or check network access to the Hub."
)
raise SystemExit(2)
return ids
def command_validate(args: argparse.Namespace) -> int:
"""Validate a bundle and print the report.
Parameters
----------
args : argparse.Namespace
Parsed arguments.
Returns
-------
int
0 if the bundle is valid, 1 otherwise.
"""
result = validate_bundle(
args.bundle,
expected_ids=_expected_ids(args.track, args.data_dir),
require_complete=args.require_complete,
)
if result.ok:
print(f"OK — {len(result.frames)} benchmark file(s) validated.")
if result.warnings:
print("\nWarnings:")
print(result.report(limit=100))
return 0
print("Bundle is not valid:")
print(result.report(limit=100))
return 1
def command_score(args: argparse.Namespace) -> int:
"""Validate, score and print one bundle's metrics.
Parameters
----------
args : argparse.Namespace
Parsed arguments.
Returns
-------
int
0 if the bundle scored, 1 if validation failed.
"""
truth: dict[str, pd.DataFrame] = {}
for spec in BENCHMARKS:
path = args.ground_truth / f"{spec.name}.parquet"
if path.exists():
frame = pd.read_parquet(path)
frame[ID_COLUMN] = frame[ID_COLUMN].astype(str)
truth[spec.name] = select_track(frame, args.track)
else:
logger.warning("No ground truth for {} at {}", spec.name, path)
result = validate_bundle(
args.bundle,
expected_ids={name: set(frame[ID_COLUMN]) for name, frame in truth.items()},
require_complete=args.require_complete,
)
if not result.ok:
print("Bundle is not valid:")
print(result.report(limit=100))
return 1
if result.warnings:
print("Warnings:")
print(result.report(limit=100))
print()
scores = scoring.score_bundle(
result.frames, truth, BENCHMARKS, bootstrap_samples=args.bootstrap
)
print(json.dumps(scores, indent=2))
print(f"\nPLUG Score: {aggregate.plug_score(scores):.4f}")
return 0
def command_rebuild(_: argparse.Namespace) -> int:
"""Rebuild the leaderboard file from the per-submission score files.
Parameters
----------
_ : argparse.Namespace
Unused.
Returns
-------
int
0 on success, 2 when no token is configured.
"""
if not store.configured():
logger.error("HF_TOKEN is not set — cannot write to the private repo.")
return 2
entries = store.all_scores()
frame = pd.DataFrame(entries) if entries else pd.DataFrame()
store.write_leaderboard(frame)
print(f"Rebuilt the leaderboard from {len(entries)} score file(s).")
return 0
def command_template(args: argparse.Namespace) -> int:
"""Write an empty per-benchmark CSV skeleton with the right ids and columns.
Every file comes out with the track's ``datapoint_id`` values already in
place, the manifest's ``row_index`` (and ``position``) beside them to join
against plug's loaders, and the prediction columns filled with zeros, so a
participant only has to overwrite the prediction column(s). cafa5 is the
exception: it is long form, so its file is written with the header alone.
Parameters
----------
args : argparse.Namespace
Parsed arguments.
Returns
-------
int
0 on success.
"""
args.out_dir.mkdir(parents=True, exist_ok=True)
for spec in BENCHMARKS:
try:
manifest = select_track(
store.load_manifest(spec.name, local_dir=args.data_dir), args.track
)
except FileNotFoundError:
logger.warning("Skipping {} — no manifest available.", spec.name)
continue
path = args.out_dir / spec.filename
if spec.long_form:
pd.DataFrame(columns=list(spec.required_columns)).to_csv(path, index=False)
print(
f"wrote {path} (header only — add one row per (protein, branch, GO term) "
f"you predict, for any of the {len(manifest)} proteins in this track)"
)
else:
keys = [ID_COLUMN, "row_index", *(["position"] if spec.per_residue else [])]
frame = manifest[keys].copy()
for column in spec.pred_columns:
frame[column] = 0.0
frame.to_csv(path, index=False)
print(f"wrote {path} ({len(manifest)} rows — replace the zeros)")
print(
f"\nFill in the prediction columns, then zip the directory's contents:\n"
f" cd {args.out_dir} && zip ../plug_submission.zip *.csv"
)
return 0
def main(argv: list[str] | None = None) -> int:
"""Parse arguments and dispatch.
Parameters
----------
argv : list[str] | None
Argument list; ``None`` reads ``sys.argv``.
Returns
-------
int
Process exit status.
"""
args = build_parser().parse_args(argv)
handlers = {
"validate": command_validate,
"score": command_score,
"rebuild": command_rebuild,
"template": command_template,
}
return handlers[args.command](args)
if __name__ == "__main__":
sys.exit(main())
|