Run experiment with the code in the folder Args: folder_name: The folder containing the code run_num: The run number timeout: Maximum execution time in seconds log_file: Optional file object to write logs to Returns: Tuple of (return_code, next_
(folder_name, run_num, timeout=180000, log_file=None)
| 144 | |
| 145 | # RUN EXPERIMENT |
| 146 | def run_experiment(folder_name, run_num, timeout=180000, log_file=None): |
| 147 | """ |
| 148 | Run experiment with the code in the folder |
| 149 | |
| 150 | Args: |
| 151 | folder_name: The folder containing the code |
| 152 | run_num: The run number |
| 153 | timeout: Maximum execution time in seconds |
| 154 | log_file: Optional file object to write logs to |
| 155 | |
| 156 | Returns: |
| 157 | Tuple of (return_code, next_prompt, traceback, message) |
| 158 | """ |
| 159 | def log_message(msg): |
| 160 | """Write message to both stdout and log file""" |
| 161 | print(msg) |
| 162 | sys.stdout.flush() |
| 163 | if log_file: |
| 164 | try: |
| 165 | log_file.write(msg + "\n") |
| 166 | log_file.flush() |
| 167 | except (ValueError, OSError): |
| 168 | pass |
| 169 | |
| 170 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 171 | log_message(f"[{timestamp}] Starting experiment run {run_num} in {folder_name}") |
| 172 | |
| 173 | cwd = osp.abspath(folder_name) |
| 174 | # Create run directory |
| 175 | run_dir = osp.join(cwd, f"run_{run_num}") |
| 176 | if not osp.exists(run_dir): |
| 177 | os.makedirs(run_dir, exist_ok=True) |
| 178 | |
| 179 | # Copy all files from the main folder to the run directory |
| 180 | log_message(f"Copying files to run directory: {run_dir}") |
| 181 | for item in os.listdir(cwd): |
| 182 | if item.startswith("run_") or item == ".git": |
| 183 | continue |
| 184 | src = osp.join(cwd, item) |
| 185 | dst = osp.join(run_dir, item) |
| 186 | if osp.isdir(src): |
| 187 | if osp.exists(dst): |
| 188 | shutil.rmtree(dst) |
| 189 | shutil.copytree(src, dst) |
| 190 | else: |
| 191 | shutil.copy2(src, dst) |
| 192 | |
| 193 | # LAUNCH COMMAND |
| 194 | command = ["bash", f"launcher.sh", f"run_{run_num}"] |
| 195 | log_message(f"Running command: {command} in {cwd}") |
| 196 | |
| 197 | log_message(f"\n{'='*80}") |
| 198 | log_message(f"Running experiment: run_{run_num}") |
| 199 | log_message(f"{'='*80}\n") |
| 200 | |
| 201 | try: |
| 202 | # Use Popen to capture output in real-time |
| 203 | process = subprocess.Popen( |
no test coverage detected