Download code/ampd/config.py from fryabcfry/cs4486-amp-pd-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 5.27 kB
-
https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/config.py
- Command line
-
hf download hf://fryabcfry/cs4486-amp-pd-checkpoints/code/ampd/config.py
-
curl -L -o config.py https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/config.py
5.27 kB
| """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) | |