|
| 1 | +import os |
| 2 | +from dataclasses import dataclass |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +from tqdm import tqdm |
| 7 | + |
| 8 | +from agentlab.analyze import inspect_results |
| 9 | +from agentlab.experiments.exp_utils import RESULTS_DIR |
| 10 | +from agentlab.experiments.study import Study |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class StudyInfo: |
| 15 | + study_dir: Path |
| 16 | + study: Study |
| 17 | + summary_df: pd.DataFrame |
| 18 | + should_delete: bool = False |
| 19 | + reason: str = "" |
| 20 | + |
| 21 | + |
| 22 | +def search_for_reasons_to_archive(result_dir: Path, min_study_size: int = 0) -> list[StudyInfo]: |
| 23 | + |
| 24 | + study_info_list = [] |
| 25 | + study_dirs = list(result_dir.iterdir()) |
| 26 | + progress = tqdm(study_dirs, desc="Processing studies") |
| 27 | + for study_dir in progress: |
| 28 | + |
| 29 | + progress.set_postfix({"study_dir": study_dir}) |
| 30 | + if not study_dir.is_dir(): |
| 31 | + progress.set_postfix({"status": "skipped"}) |
| 32 | + continue |
| 33 | + |
| 34 | + try: |
| 35 | + study = Study.load(study_dir) |
| 36 | + except Exception: |
| 37 | + study = None |
| 38 | + # get summary*.csv files and find the most recent |
| 39 | + summary_files = list(study_dir.glob("summary*.csv")) |
| 40 | + |
| 41 | + if len(summary_files) != 0: |
| 42 | + most_recent_summary = max(summary_files, key=os.path.getctime) |
| 43 | + summary_df = pd.read_csv(most_recent_summary) |
| 44 | + |
| 45 | + else: |
| 46 | + try: |
| 47 | + result_df = inspect_results.load_result_df(study_dir, progress_fn=None) |
| 48 | + summary_df = inspect_results.summarize_study(result_df) |
| 49 | + except Exception as e: |
| 50 | + print(f" Error processing {study_dir}: {e}") |
| 51 | + continue |
| 52 | + |
| 53 | + study_info = StudyInfo( |
| 54 | + study_dir=study_dir, |
| 55 | + study=study, |
| 56 | + summary_df=summary_df, |
| 57 | + ) |
| 58 | + |
| 59 | + if len(study_info.summary_df) == 0: |
| 60 | + study_info.should_delete = True |
| 61 | + study_info.reason = "Empty summary DataFrame" |
| 62 | + |
| 63 | + n_completed, n_total, n_err = 0, 0, 0 |
| 64 | + |
| 65 | + for _, row in study_info.summary_df.iterrows(): |
| 66 | + n_comp, n_tot = row["n_completed"].split("/") |
| 67 | + n_completed += int(n_comp) |
| 68 | + n_total += int(n_tot) |
| 69 | + n_err += int(row.get("n_err")) |
| 70 | + |
| 71 | + n_finished = n_completed - n_err |
| 72 | + |
| 73 | + # print(summary_df) |
| 74 | + # print(f" {n_completed} / {n_total}, {n_err} errors") |
| 75 | + |
| 76 | + if "miniwob-tiny-test" in study_dir.name: |
| 77 | + study_info.should_delete = True |
| 78 | + study_info.reason += "Miniwob tiny test\n" |
| 79 | + if n_total == 0: |
| 80 | + study_info.should_delete = True |
| 81 | + study_info.reason += "No tasks\n" |
| 82 | + if n_completed == 0: |
| 83 | + study_info.should_delete = True |
| 84 | + study_info.reason += "No tasks completed\n" |
| 85 | + if float(n_finished) / float(n_total) < 0.5: |
| 86 | + study_info.should_delete = True |
| 87 | + study_info.reason += f"Less than 50% tasks finished, n_err: {n_err}, n_total: {n_total}, n_finished: {n_finished}, n_completed: {n_completed}\n" |
| 88 | + |
| 89 | + if n_total <= min_study_size: |
| 90 | + study_info.should_delete = True |
| 91 | + study_info.reason += ( |
| 92 | + f"Too few tasks. n_total ({n_total}) <= min_study_size ({min_study_size})\n" |
| 93 | + ) |
| 94 | + |
| 95 | + study_info_list.append(study_info) |
| 96 | + return study_info_list |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + study_list_info = search_for_reasons_to_archive(RESULTS_DIR, min_study_size=5) |
| 101 | + archive_dir = RESULTS_DIR.parent / "archived_agentlab_results" # type: Path |
| 102 | + archive_dir.mkdir(parents=True, exist_ok=True) |
| 103 | + |
| 104 | + # Uncomment the line below to prevent moving studies to archive |
| 105 | + archive_dir = None |
| 106 | + |
| 107 | + for study_info in study_list_info: |
| 108 | + if not study_info.should_delete: |
| 109 | + continue |
| 110 | + |
| 111 | + print(f"Study: {study_info.study_dir.name}") |
| 112 | + print(f" Reason: {study_info.reason}") |
| 113 | + print(study_info.summary_df) |
| 114 | + print() |
| 115 | + |
| 116 | + if archive_dir is not None: |
| 117 | + # move to new dir |
| 118 | + new_path = archive_dir / study_info.study_dir.name |
| 119 | + study_info.study_dir.rename(new_path) |
| 120 | + # save reason in a file |
| 121 | + reason_file = new_path / "reason_to_archive.txt" |
| 122 | + reason_file.write_text(study_info.reason) |
0 commit comments