#!/usr/bin/env python3 """ Validate the public CraterBench-R release package. """ from __future__ import annotations import json import zipfile from pathlib import Path RELEASE_ROOT = Path(__file__).resolve().parents[1] LOCAL_STRING_PATTERNS = [ "/" + "lstr" + "/", "mars" + "sim/" + "jfang", "z" + "1977143", "sa" + "hara" + "/mar" + "ssim", ] def load_json(path: Path) -> dict: with path.open("r") as handle: return json.load(handle) def _build_file_index() -> set[str] | None: """Return set of image paths from extracted dirs or images.zip.""" images_dir = RELEASE_ROOT / "images" if images_dir.is_dir(): paths = set() for p in images_dir.rglob("*.jpg"): paths.add(str(p.relative_to(RELEASE_ROOT))) return paths zip_path = RELEASE_ROOT / "images.zip" if zip_path.exists(): with zipfile.ZipFile(zip_path) as zf: return set(zf.namelist()) return None def assert_relative_paths( entries: list[dict[str, object]], file_index: set[str] | None ) -> None: for entry in entries: relpath = str(entry["path"]) if Path(relpath).is_absolute(): raise ValueError(f"Absolute path found in manifest: {relpath}") if file_index is not None and relpath not in file_index: raise FileNotFoundError(f"Missing file referenced by manifest: {relpath}") def validate_split_manifests() -> None: test_manifest = load_json(RELEASE_ROOT / "splits" / "test.json") train_manifest = load_json(RELEASE_ROOT / "splits" / "train.json") file_index = _build_file_index() if file_index is None: print(" warning: no images/ dir or images.zip found, skipping file checks") assert_relative_paths(test_manifest["gallery_images"], file_index) assert_relative_paths(test_manifest["query_images"], file_index) assert_relative_paths(train_manifest["gallery_images"], file_index) if train_manifest["query_images"]: raise ValueError("Official train split must not contain query images.") if train_manifest["ground_truth"]: raise ValueError("Official train split must not contain ground truth.") gallery_ids = {str(entry["crater_id"]) for entry in test_manifest["gallery_images"]} query_ids = {str(entry["crater_id"]) for entry in test_manifest["query_images"]} ground_truth = test_manifest["ground_truth"] if query_ids != set(ground_truth): missing = sorted(query_ids - set(ground_truth)) extra = sorted(set(ground_truth) - query_ids) raise ValueError( f"Mismatch between query crater IDs and ground truth keys. Missing={missing[:5]} Extra={extra[:5]}" ) for crater_id, relevant_ids in ground_truth.items(): if not relevant_ids: raise ValueError(f"Empty ground-truth entry for query crater {crater_id}") if len(relevant_ids) != len(set(relevant_ids)): raise ValueError(f"Duplicate relevant IDs in ground truth for {crater_id}") if any(relevant_id not in gallery_ids for relevant_id in relevant_ids): raise ValueError(f"Non-gallery relevant ID found in ground truth for {crater_id}") train_ids = {str(entry["crater_id"]) for entry in train_manifest["gallery_images"]} excluded_ids = set(ground_truth) for relevant_ids in ground_truth.values(): excluded_ids.update(relevant_ids) overlap = train_ids & excluded_ids if overlap: raise ValueError( f"Train split overlaps with test relevance closure: {sorted(overlap)[:10]}" ) def scan_for_local_strings() -> None: text_files = [ *RELEASE_ROOT.glob("*.md"), *RELEASE_ROOT.glob("*.txt"), *RELEASE_ROOT.glob("*.toml"), *RELEASE_ROOT.glob("*.yml"), *RELEASE_ROOT.glob("*.yaml"), *RELEASE_ROOT.glob("scripts/*.py"), *RELEASE_ROOT.glob("examples/*.py"), *RELEASE_ROOT.glob("splits/*.json"), *RELEASE_ROOT.glob("metadata/*.json"), ] for path in text_files: content = path.read_text() for pattern in LOCAL_STRING_PATTERNS: if pattern in content: raise ValueError(f"Found local string {pattern!r} in {path}") def main() -> None: validate_split_manifests() scan_for_local_strings() test_manifest = load_json(RELEASE_ROOT / "splits" / "test.json") train_manifest = load_json(RELEASE_ROOT / "splits" / "train.json") print("Release package validation passed.") print( f" test: {len(test_manifest['gallery_images'])} gallery / " f"{len(test_manifest['query_images'])} query" ) print(f" train: {len(train_manifest['gallery_images'])} gallery") if __name__ == "__main__": main()