File size: 1,513 Bytes
52cc99a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0269b4b
52cc99a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
from pathlib import Path
from typing import Any, Dict, List
import logging

import yaml
from pydantic import BaseModel, ConfigDict

class ModelConfig(BaseModel):
    name: str
    description: str
    tags: List[str]

class DataConfig(BaseModel):
    features: List[str]
    target: str
    train_path: str
    store_path: str

class PipelineConfig(BaseModel):
    fourier_period: float = 365.25
    fourier_order: int = 5

class Config(BaseModel):
    model_config = ConfigDict(extra="ignore")
    model: ModelConfig
    data: DataConfig
    pipeline: PipelineConfig
    model_params: Dict[str, Dict[str, Any]]


PROJECT_ROOT = Path(__file__).resolve().parents[2]
DEFAULT_MODEL_PATH = PROJECT_ROOT / "models" / "rossmann_model.json"
DEFAULT_MODEL_METADATA_PATH = PROJECT_ROOT / "models" / "model_metadata.json"

def load_config(config_path: str = "config.yaml") -> Config:
    """Loads and validates the configuration from a YAML file."""
    candidate_paths = [Path(config_path), PROJECT_ROOT / config_path]

    actual_path = next((path for path in candidate_paths if path.exists()), None)
    if actual_path is None:
        raise FileNotFoundError(f"Configuration file '{config_path}' not found.")

    with actual_path.open("r", encoding="utf-8") as f:
        raw_config = yaml.safe_load(f)

    return Config(**raw_config)

# Global configuration instance
try:
    settings = load_config()
except Exception as e:
    logging.error(f"Failed to load configuration: {e}")
    settings = None  # type: ignore