|
|
|
|
|
import csv, sys, pathlib |
|
|
|
|
|
|
|
|
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"} |
|
|
|
|
|
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__": |
|
|
|
|
|
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) |
|
|
|