|
|
|
""" |
|
submission_checker for sampling challenge. |
|
|
|
Usage: |
|
python validate_submission.py submission.zip |
|
|
|
""" |
|
|
|
import argparse |
|
import json |
|
import os |
|
import re |
|
import sys |
|
import tempfile |
|
import zipfile |
|
from pathlib import Path |
|
from typing import List, Tuple |
|
|
|
from PIL import Image |
|
|
|
EXPECTED_NUM_IMAGES = 250 |
|
EXPECTED_SIZE = (512, 512) |
|
REQUIRED_README_KEYS = [ |
|
"method", |
|
"team", |
|
"authors", |
|
"e-mail", |
|
"institution", |
|
"country", |
|
] |
|
|
|
|
|
class ValidationError(Exception): |
|
"""Raised when one or more hard errors are detected.""" |
|
|
|
|
|
|
|
def _fail(msg: str, problems: List[str]) -> None: |
|
problems.append(msg) |
|
print(f"[ERROR] {msg}") |
|
|
|
|
|
def _warn(msg: str) -> None: |
|
print(f"[WARN] {msg}") |
|
|
|
|
|
def _check_readme(readme_path: Path, problems: List[str]) -> None: |
|
"""Verify README.txt format and fields.""" |
|
if not readme_path.is_file(): |
|
_fail("README.txt missing", problems) |
|
return |
|
|
|
txt = readme_path.read_text(encoding="utf-8").strip() |
|
|
|
relaxed = re.sub(r"'", '"', txt) |
|
relaxed = re.sub(r",\s*}", "}", relaxed) |
|
|
|
try: |
|
meta = json.loads(relaxed) |
|
except json.JSONDecodeError as e: |
|
_fail(f"README.txt is not valid JSON‑like: {e}", problems) |
|
return |
|
|
|
for key in REQUIRED_README_KEYS: |
|
if key not in meta: |
|
_fail(f"README.txt missing field: {key}", problems) |
|
|
|
|
|
def _check_image(img_path: Path, problems: List[str]) -> None: |
|
"""Open PNG and verify dimensions & mode.""" |
|
if img_path.stat().st_size == 0: |
|
_fail(f"{img_path.name} is empty (0 bytes)", problems) |
|
return |
|
|
|
try: |
|
with Image.open(img_path) as im: |
|
im.verify() |
|
except Exception as e: |
|
_fail(f"{img_path.name} is not a valid PNG: {e}", problems) |
|
return |
|
|
|
with Image.open(img_path) as im: |
|
if im.format != "PNG": |
|
_fail(f"{img_path.name}: not a PNG file", problems) |
|
if im.mode != "RGB": |
|
_fail(f"{img_path.name}: mode {im.mode}, expected RGB", problems) |
|
if im.size != EXPECTED_SIZE: |
|
_fail( |
|
f"{img_path.name}: size {im.size}, expected {EXPECTED_SIZE}", |
|
problems, |
|
) |
|
|
|
|
|
def validate(zip_path: str) -> Tuple[bool, List[str]]: |
|
problems: List[str] = [] |
|
|
|
if not Path(zip_path).is_file(): |
|
raise FileNotFoundError(zip_path) |
|
|
|
if Path(zip_path).name != "submission.zip": |
|
_warn("Zip should be named 'submission.zip' — continuing anyway.") |
|
|
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
tmpdir = Path(tmpdir) |
|
with zipfile.ZipFile(zip_path) as zf: |
|
zf.extractall(tmpdir) |
|
|
|
|
|
if any(p.is_dir() for p in tmpdir.iterdir()): |
|
_warn("Detected sub‑directories; the evaluator expects a flat layout.") |
|
|
|
|
|
files = [p for p in tmpdir.iterdir() if p.is_file()] |
|
png_files = [p for p in files if p.suffix.lower() == ".png"] |
|
readme_path = tmpdir / "README.txt" |
|
|
|
|
|
_check_readme(readme_path, problems) |
|
|
|
|
|
if len(png_files) != EXPECTED_NUM_IMAGES: |
|
_fail( |
|
f"Expected {EXPECTED_NUM_IMAGES} PNGs, found {len(png_files)}", |
|
problems, |
|
) |
|
|
|
|
|
expected_names = {f"{i}.png" for i in range(EXPECTED_NUM_IMAGES)} |
|
actual_names = {p.name for p in png_files} |
|
missing = expected_names - actual_names |
|
extra = actual_names - expected_names |
|
if missing: |
|
_fail(f"Missing PNG files: {', '.join(sorted(missing))}", problems) |
|
if extra: |
|
_fail(f"Unexpected files present: {', '.join(sorted(extra))}", problems) |
|
|
|
|
|
for img_path in sorted(png_files, key=lambda p: int(p.stem) if p.stem.isdigit() else p.stem): |
|
_check_image(img_path, problems) |
|
|
|
|
|
stray = [p for p in files if p.suffix.lower() != ".png" and p.name != "README.txt"] |
|
if stray: |
|
_warn( |
|
f"Found non‑PNG files that will be ignored by the evaluator: " |
|
+ ", ".join(p.name for p in stray) |
|
) |
|
|
|
return len(problems) == 0, problems |
|
|
|
|
|
def main() -> None: |
|
parser = argparse.ArgumentParser(description="Validate competition submission zip file.") |
|
parser.add_argument("zip", help="Path to submission.zip") |
|
args = parser.parse_args() |
|
|
|
ok, probs = validate(args.zip) |
|
if ok: |
|
print("\nAll checks passed. Good luck on the leaderboard!") |
|
sys.exit(0) |
|
|
|
print("\nFound problems:") |
|
for p in probs: |
|
print(" •", p) |
|
sys.exit(1) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|