File size: 3,520 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 | #!/usr/bin/env python3
"""Write the final model's pairs and CV folds as a machine-independent table: data/final_split/final_pairs.csv.
One row per pair of the final model (16,422 pairs: every pair trains in 9 folds and is tested once). A pair is identified by
its dataset and the file name of its photo (unique within a dataset); view_id is not used because it hashes the
absolute image path. Columns: dataset, view_file, audio (file name of the paired clip) and clip_start (s), both checked
by apply_final_split.py, group (dataset | split_group), fold.
Pairs of the non-public datasets (Taiwan sound survey, Michigan recordings) go to a separate file outside the
repository (--private-output); the public table holds only the public datasets.
python scripts/data_preparation/assemble/export_final_split.py --revision-dir <rev> \
--folds <rev>/cv_folds_combined_no_tau_indoor_manual_dark_360indoor_allscored.csv \
--pairs <rev>/score_views_final_train_only.csv
"""
import argparse
import ntpath
from pathlib import Path
import pandas as pd
def tail(path, n):
parts = str(path).replace("\\", "/").split("/")
return "/".join(parts[-n:])
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("--revision-dir", type=Path, required=True)
p.add_argument("--folds", type=Path, required=True, help="fold table of the final model (view_id, group, fold)")
p.add_argument("--pairs", type=Path, required=True, help="pairs of the final model (view_id): trained and scored")
p.add_argument("--output", type=Path, default=Path("data/final_split/final_pairs.csv"))
p.add_argument("--private-datasets", nargs="*", default=["taiwan_linebot", "local_ann_arbor_detroit", "seasonal_local"])
p.add_argument("--private-output", type=Path, required=True, help="file for the non-public pairs (outside the repository)")
a = p.parse_args()
m = pd.read_csv(a.revision_dir / "metadata_manifest.csv", low_memory=False)
folds = pd.read_csv(a.folds)
keep = set(pd.read_csv(a.pairs).view_id.astype(str))
t = folds[folds.view_id.astype(str).isin(keep)].drop(columns=["view_path"], errors="ignore").merge(m[["view_id", "training_dataset", "view_path", "audio_path", "clip_start_seconds"]], on="view_id")
if len(t) != len(keep) or (t.fold < 0).any() or not t.dataset.eq(t.training_dataset).all():
raise SystemExit("pairs, folds and manifest do not line up")
out = pd.DataFrame({"dataset": t.dataset, "view_file": t.view_path.map(lambda v: ntpath.basename(str(v).replace("/", "\\"))),
"audio": t.audio_path.map(lambda v: tail(v, 1)),
"clip_start": pd.to_numeric(t.clip_start_seconds, errors="coerce").round(3), "group": t.group, "fold": t.fold.astype(int)})
if out.duplicated(["dataset", "view_file"]).any():
raise SystemExit("photo file names are not unique within a dataset")
out = out.sort_values(["dataset", "view_file"])
private = out.dataset.isin(a.private_datasets)
for rows, path in ((out[~private], a.output), (out[private], a.private_output)):
path.parent.mkdir(parents=True, exist_ok=True)
rows.to_csv(path, index=False)
print(f"{len(rows)} pairs ({', '.join(sorted(rows.dataset.unique()))}): {path}")
print(f"final model: {len(out)} pairs, {out.group.nunique()} groups; per fold {out.fold.value_counts().sort_index().to_dict()}")
if __name__ == "__main__":
main()
|