-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_config.py
More file actions
41 lines (33 loc) · 1.17 KB
/
Copy pathproject_config.py
File metadata and controls
41 lines (33 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# project_config.py
import yaml
import torch
from pathlib import Path
from types import SimpleNamespace
def load_config():
"""Loads YAML config and resolves relative paths from project root."""
root = Path(__file__).resolve().parent
cfg_path = root / "config.yaml"
cfg = yaml.safe_load(cfg_path.read_text())
print(f"CONFIG FILE LOADED: \n{cfg}")
# inject absolute root_dir
cfg.setdefault("project", {})
cfg["project"]["root_dir"] = str(root)
# detect device (gpu / cpu)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
cfg["project"]["device"] = device
# makes all entries under 'paths' absolute
for key, rel_path in cfg.get("paths", {}).items():
if rel_path is not None:
cfg["paths"][key] = str((root / rel_path).resolve())
# Return config object with dot access
return _to_ns(cfg)
def validate_config(cfg):
# TODO: implement validation checks
pass
# Enables dot access
def _to_ns(obj):
if isinstance(obj, dict):
return SimpleNamespace(**{k: _to_ns(v) for k, v in obj.items()})
if isinstance(obj, list):
return [ _to_ns(v) for v in obj ]
return obj