Refactor run directory creation to use a unique naming function in training scripts

This commit is contained in:
2026-06-15 14:54:06 +08:00
parent 36ec36c8a8
commit 27aefb2f90
3 changed files with 24 additions and 12 deletions

View File

@@ -3,6 +3,8 @@ from __future__ import annotations
import json
import logging
import sys
import time
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, Iterable, Tuple
@@ -15,6 +17,18 @@ from dataset import AllFutureHealthDataset, HealthDataset
from models import DeepHealth
def create_unique_run_dir(name_fn, runs_root: Path = Path("runs")) -> tuple[Path, str]:
while True:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
run_name = name_fn(timestamp)
run_dir = runs_root / run_name
try:
run_dir.mkdir(parents=True, exist_ok=False)
return run_dir, run_name
except FileExistsError:
time.sleep(1.0)
def setup_logging(run_dir: Path) -> logging.Logger:
run_dir.mkdir(parents=True, exist_ok=True)
logger = logging.getLogger("DeepHealth")