File size: 26,539 Bytes
17b7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f7ab42
 
 
 
 
 
 
17b7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f7ab42
 
 
 
 
 
 
17b7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
"""Runbook-aligned leaderboard pipeline."""

from __future__ import annotations

import hashlib
import json
import os
import platform
import subprocess
import sys
from pathlib import Path
from typing import Any, Dict, Iterable, List, Sequence, Tuple

import numpy as np
import pandas as pd

from .bench_config import (
    BENCHMARK_VERSION,
    CORE_METHODS,
    DEFAULT_METHOD_CONFIGS,
    SCENARIO_PERIODS,
    SCENARIO_TIER,
    get_suite,
    normalize_periods,
    resolve_methods,
    select_primary_period,
)
from .metrics import r2_score, spectral_correlation

try:  # pragma: no cover - optional accelerator
    from numba import njit
except ImportError:  # pragma: no cover - fallback
    njit = None

from synthetic_ts_bench import generate_series, make_scenario
from synthetic_ts_bench.decomp_methods import decompose_series

LEADERBOARD_COLUMNS = [
    "suite_version",
    "suite",
    "package_version",
    "git_commit",
    "scenario_id",
    "scenario_tier",
    "seed",
    "draw_id",
    "length",
    "dt",
    "method",
    "status",
    "error_type",
    "error_message",
    "scenario_periods_json",
    "method_config_json",
    "metric_T_r2",
    "metric_T_dtw",
    "metric_S_spectral_corr",
    "metric_S_maxlag_corr",
    "metric_S_r2",
]

PRIMARY_METRICS = [
    "metric_T_r2",
    "metric_T_dtw",
    "metric_S_r2",
    "metric_S_spectral_corr",
    "metric_S_maxlag_corr",
]


def parse_seeds(seeds: str | Sequence[int]) -> List[int]:
    if isinstance(seeds, str):
        spec = seeds.strip()
        if not spec:
            return []
        if ":" in spec:
            parts = [p for p in spec.split(":") if p.strip()]
            if len(parts) not in {2, 3}:
                raise ValueError(f"Invalid seed range '{seeds}'. Use start:end[:step].")
            start = int(parts[0])
            end = int(parts[1])
            step = int(parts[2]) if len(parts) == 3 else 1
            return list(range(start, end, step))
        return [int(val.strip()) for val in spec.split(",") if val.strip()]
    return [int(val) for val in seeds]


def _hash_to_seed(text: str) -> int:
    digest = hashlib.blake2b(text.encode("utf-8"), digest_size=8).digest()
    return int.from_bytes(digest, "little") % (2**32 - 1)


def derive_sample_seed(base_seed: int, scenario_id: str, draw_id: int) -> int:
    return _hash_to_seed(f"{base_seed}:{scenario_id}:{draw_id}")


def derive_method_seed(base_seed: int, scenario_id: str, draw_id: int, method: str) -> int:
    return _hash_to_seed(f"{base_seed}:{scenario_id}:{draw_id}:{method}")


def _safe_json_dumps(payload: Any) -> str:
    return json.dumps(payload, sort_keys=True, ensure_ascii=True, separators=(",", ":"))


def _truncate(text: str, limit: int = 200) -> str:
    if text is None:
        return ""
    text = str(text)
    if len(text) <= limit:
        return text
    return text[: limit - 3] + "..."


def _safe_corr(x: np.ndarray, y: np.ndarray) -> float:
    x = np.asarray(x, dtype=float)
    y = np.asarray(y, dtype=float)
    if x.shape != y.shape or x.size == 0:
        return float("nan")
    x_c = x - x.mean()
    y_c = y - y.mean()
    vx = np.mean(x_c**2)
    vy = np.mean(y_c**2)
    if vx <= 1e-12 or vy <= 1e-12:
        return float("nan")
    return float(np.mean(x_c * y_c) / np.sqrt(vx * vy))


def _max_lag_corr(x: np.ndarray, y: np.ndarray, max_lag: int = 10) -> float:
    x = np.asarray(x, dtype=float)
    y = np.asarray(y, dtype=float)
    if x.shape != y.shape or x.size < 3:
        return float("nan")
    best = -np.inf
    for lag in range(-max_lag, max_lag + 1):
        if lag == 0:
            xc, yc = x, y
        elif lag > 0:
            xc, yc = x[lag:], y[:-lag]
        else:
            k = -lag
            xc, yc = x[:-k], y[k:]
        if xc.size < 3:
            continue
        val = _safe_corr(xc, yc)
        if np.isfinite(val) and val > best:
            best = val
    return float(best if best != -np.inf else np.nan)


def _dtw_distance_numpy(x: np.ndarray, y: np.ndarray) -> float:
    n = x.size
    m = y.size
    if n == 0 or m == 0:
        return float("nan")
    prev = np.full(m + 1, np.inf, dtype=float)
    curr = np.full(m + 1, np.inf, dtype=float)
    prev[0] = 0.0
    for i in range(1, n + 1):
        curr[0] = np.inf
        xi = x[i - 1]
        for j in range(1, m + 1):
            cost = (xi - y[j - 1]) ** 2
            curr[j] = cost + min(prev[j], curr[j - 1], prev[j - 1])
        prev, curr = curr, prev
    return float(np.sqrt(prev[m]))


if njit:  # pragma: no cover - jit in tests is optional

    @njit
    def _dtw_distance_numba(x: np.ndarray, y: np.ndarray) -> float:
        n = x.size
        m = y.size
        if n == 0 or m == 0:
            return np.nan
        prev = np.empty(m + 1, dtype=np.float64)
        curr = np.empty(m + 1, dtype=np.float64)
        for j in range(m + 1):
            prev[j] = np.inf
            curr[j] = np.inf
        prev[0] = 0.0
        for i in range(1, n + 1):
            curr[0] = np.inf
            xi = x[i - 1]
            for j in range(1, m + 1):
                cost = (xi - y[j - 1]) ** 2
                a = prev[j]
                b = curr[j - 1]
                c = prev[j - 1]
                if b < a:
                    a = b
                if c < a:
                    a = c
                curr[j] = cost + a
            for j in range(m + 1):
                prev[j] = curr[j]
        return np.sqrt(prev[m])

    def dtw_distance(x: np.ndarray, y: np.ndarray) -> float:
        return float(_dtw_distance_numba(x, y))

else:

    def dtw_distance(x: np.ndarray, y: np.ndarray) -> float:
        return _dtw_distance_numpy(x, y)


def compute_leaderboard_metrics(

    true_trend: np.ndarray,

    est_trend: np.ndarray,

    true_season: np.ndarray,

    est_season: np.ndarray,

    fs: float,

) -> Dict[str, float]:
    metrics: Dict[str, float] = {
        "metric_T_r2": float("nan"),
        "metric_T_dtw": float("nan"),
        "metric_S_spectral_corr": float("nan"),
        "metric_S_maxlag_corr": float("nan"),
        "metric_S_r2": float("nan"),
    }
    if true_trend.shape == est_trend.shape and true_trend.size > 0:
        metrics["metric_T_r2"] = float(r2_score(true_trend, est_trend))
        metrics["metric_T_dtw"] = float(dtw_distance(true_trend, est_trend))
    if true_season.shape == est_season.shape and true_season.size > 0:
        metrics["metric_S_r2"] = float(r2_score(true_season, est_season))
        metrics["metric_S_spectral_corr"] = float(
            spectral_correlation(true_season, est_season, fs=fs)
        )
        metrics["metric_S_maxlag_corr"] = float(_max_lag_corr(true_season, est_season))
    return metrics


def build_method_config(

    method: str,

    scenario_periods: Sequence[int],

    length: int,

) -> Tuple[Dict[str, Any], List[int]]:
    base_cfg = dict(DEFAULT_METHOD_CONFIGS.get(method, {}))
    periods = normalize_periods(scenario_periods, length)
    primary = select_primary_period(periods)

    if method in {"stl", "robuststl"}:
        if primary is None:
            raise ValueError(f"Method '{method}' requires a primary period.")
        base_cfg["period"] = primary
    elif method == "mstl":
        if not periods:
            raise ValueError("MSTL requires seasonal periods.")
        base_cfg["periods"] = periods
    elif method in {"ssa", "emd", "ceemdan", "vmd"}:
        if primary is not None:
            base_cfg["primary_period"] = primary
    elif method == "ma_baseline":
        if primary is not None:
            base_cfg["season_period"] = primary

    cfg = {k: v for k, v in base_cfg.items() if v is not None}
    return cfg, periods


def _method_meta_from_periods(periods: Sequence[int]) -> Dict[str, Any]:
    meta: Dict[str, Any] = {"cycles": [{"params": {"periods": list(periods)}}]}
    primary = select_primary_period(periods)
    if primary is not None:
        meta["primary_period"] = primary
    return meta


def _series_from_scenario(

    scenario_id: str,

    length: int,

    dt: float,

    seed: int,

) -> Dict[str, Any]:
    cfg = make_scenario(scenario_id, length=length, random_seed=seed)
    cfg.dt = dt
    series = generate_series(cfg)
    meta = series.get("meta", {})
    meta["scenario_id"] = scenario_id
    series["meta"] = meta
    return series


def _package_version() -> str:
    try:
        from importlib import metadata

        return metadata.version("tsdecomp")
    except Exception:
        pyproject = Path(__file__).resolve().parent.parent / "pyproject.toml"
        if pyproject.exists():
            for line in pyproject.read_text(encoding="utf-8").splitlines():
                if line.strip().startswith("version"):
                    _, val = line.split("=", 1)
                    return val.strip().strip('"').strip("'")
        return "0.0.0+local"


def _git_commit() -> str:
    try:
        out = subprocess.check_output(
            ["git", "rev-parse", "--short", "HEAD"],
            stderr=subprocess.DEVNULL,
        )
        return out.decode("utf-8").strip()
    except Exception:
        return "unknown"


def _runtime_env() -> Dict[str, Any]:
    return {
        "python_version": sys.version.split()[0],
        "platform": platform.platform(),
        "executable": sys.executable,
        "threads": {
            "OMP_NUM_THREADS": os.environ.get("OMP_NUM_THREADS"),
            "MKL_NUM_THREADS": os.environ.get("MKL_NUM_THREADS"),
            "OPENBLAS_NUM_THREADS": os.environ.get("OPENBLAS_NUM_THREADS"),
        },
    }


def write_env_artifacts(out_dir: Path, package_version: str, git_commit: str) -> None:
    env_dir = out_dir / "env"
    env_dir.mkdir(parents=True, exist_ok=True)
    (env_dir / "git_commit.txt").write_text(f"{git_commit}\n", encoding="utf-8")
    (env_dir / "package_version.txt").write_text(
        f"{package_version}\n", encoding="utf-8"
    )
    (env_dir / "runtime_env.json").write_text(
        _safe_json_dumps(_runtime_env()), encoding="utf-8"
    )
    try:
        out = subprocess.check_output(
            [sys.executable, "-m", "pip", "freeze"],
            stderr=subprocess.STDOUT,
        )
        (env_dir / "pip_freeze.txt").write_bytes(out)
    except Exception as exc:  # pragma: no cover - env-specific
        (env_dir / "pip_freeze.txt").write_text(
            f"pip freeze failed: {exc}\n", encoding="utf-8"
        )


def validate_runbook(

    suite: str,

    methods: Sequence[str],

    length: int,

    dt: float,

) -> None:
    if length <= 1:
        raise ValueError("length must be > 1.")
    if dt <= 0:
        raise ValueError("dt must be > 0.")
    scenarios = get_suite(suite)
    if not scenarios:
        raise ValueError(f"Suite '{suite}' has no scenarios.")
    missing_periods = [s for s in scenarios if s not in SCENARIO_PERIODS]
    if missing_periods:
        raise ValueError(f"Missing SCENARIO_PERIODS for: {missing_periods}")
    missing_tiers = [s for s in scenarios if s not in SCENARIO_TIER]
    if missing_tiers:
        raise ValueError(f"Missing SCENARIO_TIER for: {missing_tiers}")

    for method in methods:
        if method not in DEFAULT_METHOD_CONFIGS:
            raise ValueError(f"Unknown method '{method}'.")
    period_fields = {
        "stl": "period",
        "robuststl": "period",
        "mstl": "periods",
        "ma_baseline": "season_period",
    }
    for method, field in period_fields.items():
        if method not in DEFAULT_METHOD_CONFIGS:
            continue
        val = DEFAULT_METHOD_CONFIGS[method].get(field)
        if val is not None and val != []:
            raise ValueError(
                f"{method} config sets '{field}' directly; period injection must be dynamic."
            )


def run_benchmark(

    suite: str,

    methods: Sequence[str],

    seeds: Sequence[int],

    n_samples: int,

    length: int,

    dt: float,

) -> pd.DataFrame:
    scenarios = get_suite(suite)
    package_version = _package_version()
    git_commit = _git_commit()
    fs = 1.0 / dt if dt else 1.0

    records: List[Dict[str, Any]] = []
    for seed in seeds:
        for scenario_id in scenarios:
            scenario_periods = SCENARIO_PERIODS.get(scenario_id, [])
            tier = SCENARIO_TIER.get(scenario_id, 0)
            for draw_id in range(n_samples):
                sample_seed = derive_sample_seed(seed, scenario_id, draw_id)
                series = _series_from_scenario(
                    scenario_id, length=length, dt=dt, seed=sample_seed
                )
                true_trend = np.asarray(series.get("trend", []), dtype=float)
                true_season = np.asarray(series.get("season", []), dtype=float)

                for method in methods:
                    method_seed = derive_method_seed(seed, scenario_id, draw_id, method)
                    np.random.seed(method_seed)
                    status = "ok"
                    error_type = ""
                    error_message = ""
                    metrics: Dict[str, Any] = {
                        "metric_T_r2": float("nan"),
                        "metric_T_dtw": float("nan"),
                        "metric_S_spectral_corr": float("nan"),
                        "metric_S_maxlag_corr": float("nan"),
                        "metric_S_r2": float("nan"),
                    }
                    method_cfg: Dict[str, Any] = {}
                    periods_used: List[int] = []
                    try:
                        method_cfg, periods_used = build_method_config(
                            method, scenario_periods, length
                        )
                        meta = _method_meta_from_periods(periods_used)
                        result = decompose_series(
                            np.asarray(series["y"], dtype=float),
                            method=method,
                            config=method_cfg,
                            fs=fs,
                            meta=meta,
                        )
                        metrics = compute_leaderboard_metrics(
                            true_trend,
                            np.asarray(result.trend, dtype=float),
                            true_season,
                            np.asarray(result.season, dtype=float),
                            fs=fs,
                        )
                    except Exception as exc:
                        status = "error"
                        error_type = exc.__class__.__name__
                        error_message = _truncate(str(exc))

                    record = {
                        "suite_version": BENCHMARK_VERSION,
                        "suite": suite,
                        "package_version": package_version,
                        "git_commit": git_commit,
                        "scenario_id": scenario_id,
                        "scenario_tier": tier,
                        "seed": int(seed),
                        "draw_id": int(draw_id),
                        "length": int(length),
                        "dt": float(dt),
                        "method": method,
                        "status": status,
                        "error_type": error_type,
                        "error_message": error_message,
                        "scenario_periods_json": _safe_json_dumps(periods_used),
                        "method_config_json": _safe_json_dumps(method_cfg),
                    }
                    record.update(metrics)
                    records.append(record)

    df = pd.DataFrame.from_records(records)
    if not df.empty:
        df = df.sort_values(
            ["scenario_id", "seed", "draw_id", "method"], kind="mergesort"
        ).reset_index(drop=True)
    return df


def export_leaderboard_csv(df: pd.DataFrame, path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    out = df.copy()
    for col in LEADERBOARD_COLUMNS:
        if col not in out.columns:
            out[col] = np.nan
    out = out[LEADERBOARD_COLUMNS]
    out.to_csv(path, index=False)


def aggregate_by_scenario(df: pd.DataFrame) -> pd.DataFrame:
    if df.empty:
        return df
    metrics = [c for c in df.columns if c.startswith("metric_")]
    df = df.copy()
    df["is_ok"] = df["status"] == "ok"
    group_cols = ["scenario_id", "method"]
    summary = df.groupby(group_cols)[metrics].mean().reset_index()
    std = df.groupby(group_cols)[metrics].std().reset_index()
    coverage = df.groupby(group_cols)["is_ok"].mean().reset_index(name="coverage")
    summary = summary.merge(std, on=group_cols, suffixes=("_mean", "_std"))
    summary = summary.merge(coverage, on=group_cols)
    summary["scenario_tier"] = summary["scenario_id"].map(SCENARIO_TIER)
    return summary


def aggregate_by_tier(scenario_summary: pd.DataFrame) -> pd.DataFrame:
    if scenario_summary.empty:
        return scenario_summary
    metric_mean_cols = [c for c in scenario_summary.columns if c.endswith("_mean")]
    metric_std_cols = [c for c in scenario_summary.columns if c.endswith("_std")]
    group_cols = ["scenario_tier", "method"]
    means = (
        scenario_summary.groupby(group_cols)[metric_mean_cols]
        .mean()
        .reset_index()
    )
    stds = (
        scenario_summary.groupby(group_cols)[metric_mean_cols]
        .std()
        .reset_index()
    )
    stds = stds.rename(
        columns={col: col.replace("_mean", "_std") for col in metric_mean_cols}
    )
    coverage = (
        scenario_summary.groupby(group_cols)["coverage"]
        .mean()
        .reset_index()
    )
    merged = means.merge(stds, on=group_cols).merge(coverage, on=group_cols)
    return merged


def aggregate_overall(tier_summary: pd.DataFrame) -> pd.DataFrame:
    if tier_summary.empty:
        return tier_summary
    metric_cols = [c for c in tier_summary.columns if c.endswith("_mean")]
    group_cols = ["method"]
    means = tier_summary.groupby(group_cols)[metric_cols].mean().reset_index()
    coverage = tier_summary.groupby(group_cols)["coverage"].mean().reset_index()
    merged = means.merge(coverage, on=group_cols)
    return merged


def plot_heatmaps(

    scenario_summary: pd.DataFrame,

    out_dir: Path,

) -> None:
    if scenario_summary.empty:
        return
    import matplotlib.pyplot as plt

    out_dir.mkdir(parents=True, exist_ok=True)
    metrics = [
        ("metric_T_r2_mean", "heatmap_T_r2.png", "Trend R2 (higher is better)"),
        ("metric_T_dtw_mean", "heatmap_T_dtw.png", "Trend DTW (lower is better)"),
        ("metric_S_r2_mean", "heatmap_S_r2.png", "Seasonal R2 (higher is better)"),
        (
            "metric_S_spectral_corr_mean",
            "heatmap_S_spectral.png",
            "Seasonal spectral corr",
        ),
        (
            "metric_S_maxlag_corr_mean",
            "heatmap_S_maxlag.png",
            "Seasonal max-lag corr",
        ),
    ]

    for metric, filename, title in metrics:
        if metric not in scenario_summary.columns:
            continue
        pivot = scenario_summary.pivot_table(
            index="scenario_id", columns="method", values=metric
        )
        data = pivot.values.astype(float)
        fig, ax = plt.subplots(
            figsize=(max(6, data.shape[1] * 0.8), max(4, data.shape[0] * 0.6))
        )
        im = ax.imshow(data, aspect="auto", interpolation="nearest")
        ax.set_xticks(np.arange(pivot.shape[1]))
        ax.set_xticklabels(pivot.columns, rotation=45, ha="right")
        ax.set_yticks(np.arange(pivot.shape[0]))
        ax.set_yticklabels(pivot.index)
        ax.set_title(title)
        fig.colorbar(im, ax=ax, shrink=0.8)
        fig.tight_layout()
        fig.savefig(out_dir / filename, dpi=150)
        plt.close(fig)


def run_leaderboard(

    suite: str = "core",

    methods: str | Sequence[str] = "core",

    seeds: str | Sequence[int] = "0",

    n_samples: int = 50,

    length: int = 512,

    dt: float = 1.0,

    out_dir: str | Path = "artifacts/tscomp_v1_core",

    export_format: str = "leaderboard_csv",

    aggregate: bool = False,

    plots: bool = False,

) -> pd.DataFrame:
    method_list = resolve_methods(methods)
    seed_list = parse_seeds(seeds)
    validate_runbook(suite, method_list, length, dt)

    out_root = Path(out_dir)
    raw_dir = out_root / "raw"
    summary_dir = out_root / "summary"
    figures_dir = out_root / "figures"
    logs_dir = out_root / "logs"
    logs_dir.mkdir(parents=True, exist_ok=True)

    df = run_benchmark(
        suite=suite,
        methods=method_list,
        seeds=seed_list,
        n_samples=n_samples,
        length=length,
        dt=dt,
    )

    package_version = _package_version()
    git_commit = _git_commit()
    write_env_artifacts(out_root, package_version, git_commit)

    if export_format == "leaderboard_csv":
        raw_name = f"leaderboard_{suite}_official_raw.csv"
        raw_path = raw_dir / raw_name
        export_leaderboard_csv(df, raw_path)
        export_leaderboard_csv(df, out_root / "leaderboard.csv")

    if aggregate:
        summary_dir.mkdir(parents=True, exist_ok=True)
        scenario_summary = aggregate_by_scenario(df)
        scenario_summary.to_csv(
            summary_dir / f"{suite}_by_scenario.csv", index=False
        )
        tier_summary = aggregate_by_tier(scenario_summary)
        tier_summary.to_csv(summary_dir / f"{suite}_by_tier.csv", index=False)
        overall = aggregate_overall(tier_summary)
        overall.to_csv(summary_dir / f"{suite}_overall.csv", index=False)
        if plots:
            figures_dir.mkdir(parents=True, exist_ok=True)
            plot_heatmaps(scenario_summary, figures_dir)
    elif plots:
        scenario_summary = aggregate_by_scenario(df)
        figures_dir.mkdir(parents=True, exist_ok=True)
        plot_heatmaps(scenario_summary, figures_dir)

    if not df.empty:
        errors = df[df["status"] == "error"]
        if not errors.empty:
            error_path = logs_dir / "errors.jsonl"
            with error_path.open("w", encoding="utf-8") as f:
                for _, row in errors.iterrows():
                    payload = {
                        "scenario_id": row["scenario_id"],
                        "method": row["method"],
                        "seed": int(row["seed"]),
                        "draw_id": int(row["draw_id"]),
                        "error_type": row["error_type"],
                        "error_message": row["error_message"],
                    }
                    f.write(_safe_json_dumps(payload) + "\n")
    log_path = logs_dir / "run.log"
    total_rows = int(df.shape[0]) if not df.empty else 0
    error_rows = int((df["status"] == "error").sum()) if not df.empty else 0
    log_lines = [
        f"suite={suite}",
        f"methods={','.join(method_list)}",
        f"seeds={','.join(str(s) for s in seed_list)}",
        f"n_samples={n_samples}",
        f"length={length}",
        f"dt={dt}",
        f"rows={total_rows}",
        f"errors={error_rows}",
    ]
    log_path.write_text("\n".join(log_lines) + "\n", encoding="utf-8")
    return df


def merge_results(

    input_dirs: Sequence[str | Path],

    out_dir: str | Path,

    export_format: str = "leaderboard_csv",

    aggregate: bool = True,

    plots: bool = True,

) -> pd.DataFrame:
    """Merge leaderboard results from multiple run directories."""
    dfs = []
    for d in input_dirs:
        path = Path(d)
        # Try raw first, then root leaderboard.csv
        raw_csv = path / "raw" / f"leaderboard_csv_official_raw.csv"  # heuristic pattern
        # Actually standard name is leaderboard_{suite}_official_raw.csv or just leaderboard.csv
        # Let's search for *raw.csv in raw/ or leaderboard.csv in root
        candidates = list((path / "raw").glob("*_raw.csv"))
        if not candidates:
            candidates = [path / "leaderboard.csv"]

        found = False
        for c in candidates:
            if c.exists():
                try:
                    df_part = pd.read_csv(c)
                    dfs.append(df_part)
                    found = True
                    break
                except Exception:
                    pass
        if not found:
            print(f"Warning: No valid leaderboard csv found in {d}")

    if not dfs:
        raise ValueError("No results found to merge.")

    df = pd.concat(dfs, ignore_index=True)
    # Deduplicate if overlapping seeds/methods
    subset_cols = ["scenario_id", "seed", "draw_id", "method"]
    df = df.drop_duplicates(subset=subset_cols, keep="last")

    out_root = Path(out_dir)
    raw_dir = out_root / "raw"
    summary_dir = out_root / "summary"
    figures_dir = out_root / "figures"
    logs_dir = out_root / "logs"

    raw_dir.mkdir(parents=True, exist_ok=True)
    summary_dir.mkdir(parents=True, exist_ok=True)
    figures_dir.mkdir(parents=True, exist_ok=True)
    logs_dir.mkdir(parents=True, exist_ok=True)

    if export_format == "leaderboard_csv":
        export_leaderboard_csv(df, out_root / "leaderboard.csv")
        # Try to infer suite from first record or default to 'merged'
        suite = df["suite"].iloc[0] if "suite" in df.columns else "core"
        export_leaderboard_csv(df, raw_dir / f"leaderboard_{suite}_merged_raw.csv")

    if aggregate:
        suite = df["suite"].iloc[0] if "suite" in df.columns else "core"
        scenario_summary = aggregate_by_scenario(df)
        scenario_summary.to_csv(
            summary_dir / f"{suite}_by_scenario.csv", index=False
        )
        tier_summary = aggregate_by_tier(scenario_summary)
        tier_summary.to_csv(summary_dir / f"{suite}_by_tier.csv", index=False)
        overall = aggregate_overall(tier_summary)
        overall.to_csv(summary_dir / f"{suite}_overall.csv", index=False)
        if plots:
            plot_heatmaps(scenario_summary, figures_dir)

    return df