| |
| """ |
| Create a remove-all subset from an eval_outputs directory. |
| |
| A sample is copied iff: |
| - directory name ends with "_remove_only" |
| - metadata.command_variant.command_type == "remove_only" |
| - metadata.command_variant.target_sources == ["speech"] |
| - every distractor name is explicitly present in user_input |
| |
| Output structure: |
| <src_eval_dir>/<dst_subdir>/outputs/<sample_dirs...> |
| """ |
|
|
| import argparse |
| import json |
| import os |
| import shutil |
| from pathlib import Path |
|
|
|
|
| def qualifies(metadata: dict) -> bool: |
| cmd = metadata.get("command_variant", {}) |
| user_input = (cmd.get("user_input") or "").lower() |
| distractors = [d.lower() for d in metadata.get("distractors", [])] |
|
|
| if cmd.get("command_type") != "remove_only": |
| return False |
| if cmd.get("target_sources") != ["speech"]: |
| return False |
| if not distractors: |
| return False |
|
|
| return all(d.replace("_", " ") in user_input for d in distractors) |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser( |
| description="Create removeall subset from an eval_outputs directory." |
| ) |
| parser.add_argument( |
| "--src_eval_dir", |
| type=Path, |
| required=True, |
| help="Path like experiments_final/<model>/eval_outputs_test_3k", |
| ) |
| parser.add_argument( |
| "--dst_subdir", |
| type=str, |
| default="removeall", |
| help="Subdirectory to create under src_eval_dir (default: removeall)", |
| ) |
| parser.add_argument( |
| "--force", |
| action="store_true", |
| help="Delete destination folder first if it already exists.", |
| ) |
| args = parser.parse_args() |
|
|
| src_eval_dir = args.src_eval_dir.resolve() |
| src_outputs = src_eval_dir / "outputs" |
| if not src_outputs.exists(): |
| raise FileNotFoundError(f"Missing source outputs dir: {src_outputs}") |
|
|
| dst_eval_dir = src_eval_dir / args.dst_subdir |
| dst_outputs = dst_eval_dir / "outputs" |
|
|
| if dst_eval_dir.exists() and args.force: |
| shutil.rmtree(dst_eval_dir) |
| dst_outputs.mkdir(parents=True, exist_ok=True) |
|
|
| total_remove_only = 0 |
| copied = 0 |
|
|
| for sample_dir in sorted(src_outputs.iterdir()): |
| if not sample_dir.is_dir(): |
| continue |
| if not sample_dir.name.endswith("_remove_only"): |
| continue |
|
|
| meta_path = sample_dir / "metadata.json" |
| if not meta_path.exists(): |
| continue |
|
|
| total_remove_only += 1 |
| with meta_path.open() as f: |
| metadata = json.load(f) |
|
|
| if not qualifies(metadata): |
| continue |
|
|
| dst_sample = dst_outputs / sample_dir.name |
| if dst_sample.exists(): |
| shutil.rmtree(dst_sample) |
| shutil.copytree(sample_dir, dst_sample) |
| copied += 1 |
|
|
| summary = { |
| "src_eval_dir": str(src_eval_dir), |
| "dst_eval_dir": str(dst_eval_dir), |
| "total_remove_only_samples_seen": total_remove_only, |
| "copied_removeall_samples": copied, |
| "filter": { |
| "command_type": "remove_only", |
| "target_sources": ["speech"], |
| "all_distractors_named_in_user_input": True, |
| }, |
| } |
| with (dst_eval_dir / "copy_summary.json").open("w") as f: |
| json.dump(summary, f, indent=2) |
|
|
| print(json.dumps(summary, indent=2)) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|