File size: 3,117 Bytes
ee24db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# scripts/validate_metadata.py
import csv, sys, pathlib

# Two accepted schemas:
REQUIRED_A = {"rir_id","file_path","sample_rate","rir_length","method",
              "src_x","src_y","src_z","mic_x","mic_y","mic_z",
              "distance","azimuth_deg","elevation_deg",
              "rt60_t20","rt60_t30","drr_db","room_l","room_w","room_h","vol_m3","split"}
REQUIRED_B = {"id","split","fs","wav"}  # compact schema

def find_default_meta():
    here = pathlib.Path(__file__).resolve().parents[1]
    candidates = [
        here / "data" / "metadata" / "metadata.csv",
        here / "data" / "metadata.csv",
    ]
    for p in candidates:
        if p.exists():
            return p
    return None

def validate(path: pathlib.Path):
    with path.open(newline="", encoding="utf-8", errors="replace") as f:
        r = csv.DictReader(f)
        headers = { (h or "").strip().lstrip("\ufeff").lower() for h in (r.fieldnames or []) }
        if REQUIRED_A.issubset(headers):
            schema = "A"
        elif REQUIRED_B.issubset(headers):
            schema = "B"
        else:
            missingA = sorted(REQUIRED_A - headers)
            missingB = sorted(REQUIRED_B - headers)
            raise SystemExit(
                "metadata.csv does not match a known schema.\n"
                f"- Missing for Schema A: {missingA}\n"
                f"- Missing for Schema B: {missingB}\n"
                f"Headers seen: {sorted(headers)}"
            )

        for i,row in enumerate(r, 1):
            if schema == "A":
                if not (row.get("file_path") or "").strip():
                    raise SystemExit(f"row {i}: empty file_path")
                try:
                    if int(float(row["sample_rate"])) <= 0:
                        raise ValueError
                except Exception:
                    raise SystemExit(f"row {i}: invalid sample_rate -> {row.get('sample_rate')}")
                try:
                    if int(float(row["rir_length"])) <= 0:
                        raise ValueError
                except Exception:
                    raise SystemExit(f"row {i}: invalid rir_length -> {row.get('rir_length')}")
            else:
                if not (row.get("wav") or "").strip():
                    raise SystemExit(f"row {i}: empty wav path")
                try:
                    if int(float(row["fs"])) <= 0:
                        raise ValueError
                except Exception:
                    raise SystemExit(f"row {i}: invalid fs -> {row.get('fs')}")

    print(f"metadata OK ✅ ({schema=}) @ {path}")

if __name__ == "__main__":
    # Accept optional path; fallback to default
    p = None
    if len(sys.argv) >= 2 and sys.argv[1].strip():
        p = pathlib.Path(sys.argv[1]).expanduser().resolve()
        if not p.exists():
            raise SystemExit(f"[ERR] Not found: {p}")
    else:
        p = find_default_meta()
        if p is None:
            raise SystemExit("Usage: python scripts/validate_metadata.py <path/to/metadata.csv>\n"
                             "Could not auto-find data/metadata/metadata.csv")
    validate(p)