-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevaluate.py
More file actions
119 lines (98 loc) · 4.65 KB
/
evaluate.py
File metadata and controls
119 lines (98 loc) · 4.65 KB
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
# evaluate.py
"""
DiffQuant — evaluation entry point.
Runs walk-forward evaluation on the test and backtest splits using
the best checkpoint saved during training.
Usage:
python evaluate.py --config configs/experiments/itransformer_hybrid.py
python evaluate.py --config configs/experiments/itransformer_hybrid.py \\
--checkpoint output/itransformer_hybrid/models/best.pth
python evaluate.py --config configs/experiments/itransformer_hybrid.py \\
--mode test # only test split
"""
import argparse
import logging
import sys
from pathlib import Path
logging.basicConfig(
level = logging.INFO,
format = "%(asctime)s | %(message)s",
datefmt = "%Y-%m-%d %H:%M:%S",
handlers = [logging.StreamHandler(sys.stdout)],
)
log = logging.getLogger(__name__)
def main() -> None:
parser = argparse.ArgumentParser(description="DiffQuant evaluation")
parser.add_argument("--config", "-c", required=True)
parser.add_argument("--checkpoint", "-k", default=None,
help="Path to .pth checkpoint. Default: output/<exp>/models/best.pth")
parser.add_argument("--mode", "-m", default="both",
choices=["test", "backtest", "both"],
help="Which splits to evaluate (default: both)")
args = parser.parse_args()
# ── Load config ───────────────────────────────────────────────────────────
from configs.experiments import load_config
cfg = load_config(args.config)
# ── Attach file handler — all evaluation logs go to training.log ──────────
import pathlib
out_dir = pathlib.Path(cfg.paths.output_dir)
out_dir.mkdir(parents=True, exist_ok=True)
log_path = out_dir / "training.log"
root_logger = logging.getLogger()
already_added = any(
isinstance(h, logging.FileHandler) and
getattr(h, "baseFilename", None) == str(log_path.resolve())
for h in root_logger.handlers
)
if not already_added:
fh = logging.FileHandler(log_path)
fh.setLevel(logging.INFO)
fh.setFormatter(logging.Formatter(
"%(asctime)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
))
root_logger.addHandler(fh)
# ── Resolve checkpoint ────────────────────────────────────────────────────
ckpt_path = Path(args.checkpoint or Path(cfg.paths.output_dir) / "models" / "best.pth")
if not ckpt_path.exists():
log.error("Checkpoint not found: %s", ckpt_path)
sys.exit(1)
log.info("Experiment : %s", cfg.experiment_name)
log.info("Checkpoint : %s", ckpt_path)
log.info("Mode : %s", args.mode)
# ── Load model ────────────────────────────────────────────────────────────
import torch
from model.policy_network import PolicyNetwork
device = torch.device(cfg.device)
model = PolicyNetwork(cfg)
state = torch.load(ckpt_path, map_location=device)
model.load_state_dict(state["model_state_dict"])
model.to(device)
model.eval()
best_sharpe = state.get("best_val_sharpe", float("nan"))
log.info("Best val Sharpe from training: %+.4f", best_sharpe)
# ── Data ──────────────────────────────────────────────────────────────────
from data.pipeline import load_or_build
splits = load_or_build(
source_path = cfg.paths.source_data,
cfg = cfg,
cache_dir = cfg.paths.cache_dir,
)
# ── Evaluate ──────────────────────────────────────────────────────────────
from evaluation.backtest import run_backtest
modes = (
["test", "backtest"] if args.mode == "both"
else [args.mode]
)
for mode in modes:
if mode not in splits or "raw_features" not in splits[mode]:
log.error("Split '%s' not available in processed dataset.", mode)
continue
log.info("Running %s evaluation...", mode)
run_backtest(
model = model,
raw = splits[mode],
cfg = cfg,
mode = mode,
)
if __name__ == "__main__":
main()