|
| 1 | +from pathlib import Path |
| 2 | +from typing import Sequence |
| 3 | + |
| 4 | +from pydantic import BaseModel |
| 5 | +from strictyaml import YAML, load |
| 6 | + |
| 7 | +import classification_model |
| 8 | + |
| 9 | +# Project Directories |
| 10 | +PACKAGE_ROOT = Path(classification_model.__file__).resolve().parent |
| 11 | +ROOT = PACKAGE_ROOT.parent |
| 12 | +CONFIG_FILE_PATH = PACKAGE_ROOT / "config.yml" |
| 13 | +DATASET_DIR = PACKAGE_ROOT / "datasets" |
| 14 | +TRAINED_MODEL_DIR = PACKAGE_ROOT / "trained_models" |
| 15 | + |
| 16 | + |
| 17 | +class AppConfig(BaseModel): |
| 18 | + """ |
| 19 | + Application-level config. |
| 20 | + """ |
| 21 | + |
| 22 | + package_name: str |
| 23 | + raw_data_file: str |
| 24 | + pipeline_save_file: str |
| 25 | + |
| 26 | + |
| 27 | +class ModelConfig(BaseModel): |
| 28 | + """ |
| 29 | + All configuration relevant to model |
| 30 | + training and feature engineering. |
| 31 | + """ |
| 32 | + |
| 33 | + target: str |
| 34 | + unused_fields: Sequence[str] |
| 35 | + features: Sequence[str] |
| 36 | + test_size: float |
| 37 | + random_state: int |
| 38 | + numerical_vars: Sequence[str] |
| 39 | + categorical_vars: Sequence[str] |
| 40 | + cabin_vars: Sequence[str] |
| 41 | + |
| 42 | + |
| 43 | +class Config(BaseModel): |
| 44 | + """Master config object.""" |
| 45 | + |
| 46 | + app_config: AppConfig |
| 47 | + model_config: ModelConfig |
| 48 | + |
| 49 | + |
| 50 | +def find_config_file() -> Path: |
| 51 | + """Locate the configuration file.""" |
| 52 | + if CONFIG_FILE_PATH.is_file(): |
| 53 | + return CONFIG_FILE_PATH |
| 54 | + raise Exception(f"Config not found at {CONFIG_FILE_PATH!r}") |
| 55 | + |
| 56 | + |
| 57 | +def fetch_config_from_yaml(cfg_path: Path = None) -> YAML: |
| 58 | + """Parse YAML containing the package configuration.""" |
| 59 | + |
| 60 | + if not cfg_path: |
| 61 | + cfg_path = find_config_file() |
| 62 | + |
| 63 | + if cfg_path: |
| 64 | + with open(cfg_path, "r") as conf_file: |
| 65 | + parsed_config = load(conf_file.read()) |
| 66 | + return parsed_config |
| 67 | + raise OSError(f"Did not find config file at path: {cfg_path}") |
| 68 | + |
| 69 | + |
| 70 | +def create_and_validate_config(parsed_config: YAML = None) -> Config: |
| 71 | + """Run validation on config values.""" |
| 72 | + if parsed_config is None: |
| 73 | + parsed_config = fetch_config_from_yaml() |
| 74 | + |
| 75 | + # specify the data attribute from the strictyaml YAML type. |
| 76 | + _config = Config( |
| 77 | + app_config=AppConfig(**parsed_config.data), |
| 78 | + model_config=ModelConfig(**parsed_config.data), |
| 79 | + ) |
| 80 | + |
| 81 | + return _config |
| 82 | + |
| 83 | + |
| 84 | +config = create_and_validate_config() |
0 commit comments