from pathlib import Path import os def _log_to_csv(arch, mode, accuracy, report_dict, strategy): """Handles directory structures, file setups, and distinct CSV column formatting.""" #arch_name = model.__class__.__name__.lower() save_dir = Path(f"reports/{strategy}/{arch}") save_dir.mkdir(parents=True, exist_ok=True) csv_path = save_dir / f"{mode}.csv" file_exists = csv_path.exists() headers = [ "accuracy", "macro_precision", "macro_recall", "macro_f1", "weighted_precision", "weighted_recall", "weighted_f1" ] row = [ f"{accuracy / 100.0:.4f}", f"{report_dict['macro avg']['precision']:.4f}", f"{report_dict['macro avg']['recall']:.4f}", f"{report_dict['macro avg']['f1-score']:.4f}", f"{report_dict['weighted avg']['precision']:.4f}", f"{report_dict['weighted avg']['recall']:.4f}", f"{report_dict['weighted avg']['f1-score']:.4f}" ] with open(csv_path, "a") as f: if not file_exists: f.write(",".join(headers) + "\n") f.write(",".join(row) + "\n") print(f">> Direct CSV metrics appended to {csv_path}") def _initialize_log_file(log_file): """Creates a unique log file for this strategy with a header if it doesn't exist.""" log_file.parent.mkdir(parents=True, exist_ok=True) if not os.path.exists(log_file): with open(log_file, "w") as f: f.write("execution_time_sec\n") def log_metric(log_file, execution_time: float): """Appends the execution time to this strategy's specific file.""" with open(log_file, "a") as f: f.write(f"{execution_time:.6f}\n")