File size: 3,109 Bytes
16231ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Normalize an external paired-dataset manifest for the ImageBind/ridge pipeline."""
from __future__ import annotations
import sys as _sys
from pathlib import Path as _Path
_SCRIPTS = next(_p for _p in _Path(__file__).resolve().parents if _p.name == "scripts")
_sys.path[:0] = [str(_SCRIPTS.parent), str(_SCRIPTS.parent / "models")] + [str(_d) for _d in sorted(_SCRIPTS.rglob("*")) if _d.is_dir() and _d.name != "__pycache__"]

import argparse
from pathlib import Path

import pandas as pd


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--input", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--dataset", required=True)
    parser.add_argument("--force-train", action="store_true")
    parser.add_argument(
        "--single-image-grain", action="store_true",
        help="Use each photo_id as location_id while preserving the original location as parent_location_id.",
    )
    args = parser.parse_args()

    frame = pd.read_csv(args.input)
    required = {"photo_id", "photo_path", "audio_path"}
    missing = required - set(frame.columns)
    if missing:
        raise SystemExit(f"Input manifest is missing: {sorted(missing)}")
    frame["source_dataset"] = args.dataset
    if "location_id" not in frame:
        if "recording_id" in frame:
            frame["location_id"] = args.dataset + "_" + frame.recording_id.astype(str)
        else:
            frame["location_id"] = frame.photo_id.astype(str)
    if args.single_image_grain:
        if "parent_location_id" not in frame:
            frame["parent_location_id"] = frame.location_id.astype(str)
        frame["location_id"] = frame.photo_id.astype(str)
    if "split_group" not in frame:
        source = frame["owner_or_session"] if "owner_or_session" in frame else frame["location_id"]
        frame["split_group"] = args.dataset + ":" + source.astype(str)
    if args.force_train:
        frame["split"] = "train"
        frame["use_for_training"] = True
    elif "split" not in frame:
        raise SystemExit("Input manifest has no split; pass --force-train or prepare a grouped split")
    if "city" not in frame:
        frame["city"] = args.dataset
    if "clip_index" not in frame:
        frame["clip_index"] = 0
    if "photo_view_paths" not in frame:
        frame["photo_view_paths"] = frame.photo_path.astype(str)
    if "panorama" not in frame:
        frame["panorama"] = False
    if "lon" not in frame:
        frame["lon"] = frame["longitude"] if "longitude" in frame else float("nan")
    if "lat" not in frame:
        frame["lat"] = frame["latitude"] if "latitude" in frame else float("nan")

    if frame.photo_id.duplicated().any():
        raise SystemExit("photo_id is not unique")
    args.output.parent.mkdir(parents=True, exist_ok=True)
    frame.to_csv(args.output, index=False)
    print(
        f"Wrote {len(frame):,} {args.dataset} rows to {args.output}; "
        f"location_ids={frame.location_id.nunique():,}"
    )


if __name__ == "__main__":
    main()