File size: 5,270 Bytes
04ae9bb | 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | """Config loading, defaults, validation, and saving."""
from __future__ import annotations
from copy import deepcopy
from pathlib import Path
from typing import Any, Dict
from .utils import deep_update, load_yaml, save_yaml
DEFAULTS: Dict[str, Any] = {
"experiment": {
"name": "ampd_exp",
"seed": 42,
"device": "auto",
"output_dir": "runs",
"tags": [],
"notes": "",
},
"data": {
"root": "amp_data",
"base_visits": "protein",
"targets": ["updrs_1", "updrs_2", "updrs_3", "updrs_4"],
"horizons": [0, 6, 12, 24],
"include_supplemental": False,
"has_protein_only": False,
"impute": "median",
"standardize": True,
"target_transform": "none",
"process": {
"enabled": True,
"visit_source": "clinical",
"months": [0, 6, 12, 18, 24, 36, 48, 60, 72, 84],
"include_control": True,
"include_gap": True,
"include_prior_count": True,
"include_visit_month": True,
},
"proteins": {
"enabled": False,
"log1p": True,
"pca": 0,
},
"peptides": {
"enabled": False,
"mode": "protein_mean",
"top_k": 0,
"log1p": True,
},
},
"cv": {
"scheme": "patient_kfold",
"n_splits": 5,
"seed": 42,
"stratify_by": "cohort",
"cohort_scheme": "rule",
"holdout_cohorts": None,
"save_folds": True,
"single_val_fraction": 0.2,
},
"model": {
"name": "mlp",
"output_type": "regression",
"params": {
"hidden_dims": [64, 64],
"dropout": 0.1,
"activation": "relu",
"output_activation": "softplus",
"n_classes": 87,
},
},
"loss": {
"name": "masked_mae",
"params": {},
},
"optimizer": {
"name": "adamw",
"lr": 0.001,
"weight_decay": 0.0001,
},
"scheduler": {
"name": "cosine",
"interval": "epoch",
"params": {"eta_min": 1e-5},
},
"trainer": {
"max_epochs": 200,
"batch_size": 64,
"num_workers": 0,
"amp": True,
"grad_clip": 1.0,
"accumulate_grad_batches": 1,
"max_minutes": 0,
"stop_on_nan": True,
"log_every": 10,
"early_stopping": {
"enabled": True,
"monitor": "val_smape",
"mode": "min",
"patience": 20,
"min_delta": 0.0001,
},
"checkpoint": {
"monitor": "val_smape",
"mode": "min",
"save_top_k": 3,
"save_last": True,
"load_best_at_end": True,
},
"resume": "",
},
"evaluation": {
"metrics": ["smape_plus_1", "mae"],
"postprocess": {"updrs_4": "none"},
"per_target": True,
"per_horizon": True,
"per_cohort": True,
"bootstrap": 200,
"save_predictions": True,
},
"test": {
"enabled": False,
"test_csv": "",
"output": "submission.csv",
},
}
def default_config() -> Dict[str, Any]:
return deepcopy(DEFAULTS)
def load_config(path: str | Path, overrides=None) -> Dict[str, Any]:
user = load_yaml(path) or {}
cfg = deep_update(default_config(), user)
if overrides:
from .utils import apply_overrides
cfg = apply_overrides(cfg, overrides)
validate_config(cfg)
return cfg
def validate_config(cfg: Dict[str, Any]) -> None:
if cfg["data"]["base_visits"] not in {"all", "protein"}:
raise ValueError("data.base_visits must be 'all' or 'protein'")
if cfg["model"]["output_type"] not in {"regression", "distribution"}:
raise ValueError("model.output_type must be 'regression' or 'distribution'")
if cfg["cv"]["scheme"] not in {"patient_kfold", "cohort_heldout", "single"}:
raise ValueError("cv.scheme must be patient_kfold, cohort_heldout, or single")
if cfg["data"].get("process", {}).get("visit_source", "clinical") not in {"clinical", "sample"}:
raise ValueError("data.process.visit_source must be clinical or sample")
if cfg["model"]["output_type"] == "distribution" and cfg["loss"]["name"] != "masked_cross_entropy":
raise ValueError("distribution output requires loss.name=masked_cross_entropy")
if cfg["model"]["output_type"] == "regression" and cfg["loss"]["name"] == "masked_cross_entropy":
raise ValueError("regression output cannot use masked_cross_entropy")
if len(cfg["data"]["targets"]) * len(cfg["data"]["horizons"]) <= 0:
raise ValueError("targets x horizons must be non-empty")
if cfg["data"].get("target_transform") == "standardize" and cfg["model"]["output_type"] == "regression":
act = cfg["model"].get("params", {}).get("output_activation", "softplus")
if act == "softplus":
raise ValueError("target_transform=standardize cannot be combined with output_activation=softplus; use output_activation=none")
def save_config(cfg: Dict[str, Any], path: str | Path) -> None:
save_yaml(cfg, path)
|