sandy45/ChestViT-Explainable-XRay-AI
0
1"""2config_loader.py3----------------4Loads config/config.yaml and exposes a typed namespace for the rest of the project.5"""6import yaml7from pathlib import Path8from types import SimpleNamespace9 10 11def _dict_to_namespace(d: dict) -> SimpleNamespace:12 """Recursively convert dict → SimpleNamespace for dot-access."""13 ns = SimpleNamespace()14 for key, value in d.items():15 if isinstance(value, dict):16 setattr(ns, key, _dict_to_namespace(value))17 else:18 setattr(ns, key, value)19 return ns20 21 22def load_config(config_path: str | Path | None = None) -> SimpleNamespace:23 """24 Load the project config from YAML.25 26 Args:27 config_path: Path to config.yaml. Defaults to <project_root>/config/config.yaml.28 29 Returns:30 SimpleNamespace with dot-access to all config values.31 """32 if config_path is None:33 config_path = Path(__file__).parent / "config" / "config.yaml"34 config_path = Path(config_path)35 if not config_path.exists():36 raise FileNotFoundError(f"Config file not found: {config_path}")37 with open(config_path, "r", encoding="utf-8") as f:38 raw = yaml.safe_load(f)39 return _dict_to_namespace(raw)40 41 42# Convenience singleton — import this anywhere in the project43CFG = load_config()44 