File size: 3,223 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
"""Command line interface.

Examples:
    python -m ampd.cli inspect --config configs/process_mlp.yaml
    python -m ampd.cli train --config configs/process_mlp.yaml
    python -m ampd.cli train --config configs/smoke.yaml --override trainer.max_epochs=2
    python -m ampd.cli baseline --config configs/process_mlp.yaml --kind median
    python -m ampd.cli make-folds --config configs/process_mlp.yaml
"""

from __future__ import annotations

import argparse
import json
from pathlib import Path

from .config import load_config
from .data.clinical import build_all_data
from .data.splits import make_folds, save_folds


def _cfg(args):
    cfg = load_config(args.config, args.override)
    device = getattr(args, "device", None)
    if device:
        cfg["experiment"]["device"] = device
    return cfg


def cmd_inspect(args):
    from .inspect import inspect_data

    cfg = _cfg(args)
    inspect_data(cfg)


def cmd_train(args):
    from .runner import ExperimentRunner

    cfg = _cfg(args)
    runner = ExperimentRunner(cfg)
    result = runner.run()
    print(json.dumps({"run_dir": result["run_dir"], "smape_plus_1": result["metrics"].get("smape_plus_1")}, indent=2))


def cmd_baseline(args):
    from .baselines import run_baselines

    cfg = _cfg(args)
    run_baselines(cfg, kind=args.kind)


def cmd_compare(args):
    from .compare import write_comparison

    write_comparison(args.root, args.out)


def cmd_make_folds(args):
    cfg = _cfg(args)
    bundle = build_all_data(cfg)
    folds = make_folds(bundle.patients, cfg["cv"])
    out = Path(args.out or "folds.csv")
    save_folds(folds, out)
    print(f"wrote {out} ({len(folds)} patients)")
    print(folds.groupby("fold").size().to_string())


def build_parser():
    p = argparse.ArgumentParser(description="AMP-PD experiment platform")
    sub = p.add_subparsers(dest="command", required=True)

    def common(sp):
        sp.add_argument("--config", required=True, help="YAML config path")
        sp.add_argument("--override", nargs="*", default=[], help="key=value overrides")
        sp.add_argument("--device", default=None, choices=["auto", "cpu", "cuda"], help="device override")

    sp = sub.add_parser("inspect", help="build data and print shapes without training")
    common(sp)
    sp.set_defaults(func=cmd_inspect)

    sp = sub.add_parser("train", help="run CV training and automatic evaluation")
    common(sp)
    sp.set_defaults(func=cmd_train)

    sp = sub.add_parser("baseline", help="run median or Ridge baselines")
    common(sp)
    sp.add_argument("--kind", default="median", choices=["median", "process", "protein", "peptide", "all"])
    sp.set_defaults(func=cmd_baseline)

    sp = sub.add_parser("compare", help="collect run metrics into a table")
    sp.add_argument("--root", default="runs")
    sp.add_argument("--out", default="runs/comparison.csv")
    sp.set_defaults(func=cmd_compare)

    sp = sub.add_parser("make-folds", help="write patient folds.csv")
    common(sp)
    sp.add_argument("--out", default="folds.csv")
    sp.set_defaults(func=cmd_make_folds)
    return p


def main():
    parser = build_parser()
    args = parser.parse_args()
    args.func(args)


if __name__ == "__main__":
    main()