File size: 4,777 Bytes
3893463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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()