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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
from __future__ import annotations

import copy
import json
import os
import re
import subprocess
import sys
import base64
import threading
import time
import uuid
from io import BytesIO
from datetime import datetime
from pathlib import Path
from typing import Any

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from flask import Blueprint, Flask, jsonify, render_template, request, send_file, Response

# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
BASE_DIR = Path(__file__).resolve().parent
SRC_DIR = BASE_DIR / "src"
CONFIG_DIR = BASE_DIR / "configs"
GRIDSEARCH_ROOT = BASE_DIR / "gridsearch" / "preoperative"
OUTPUTS_ROOT = BASE_DIR / "outputs"
STUDIO_RUNS_ROOT = OUTPUTS_ROOT / "studio_runs"
PREOPERATIVE_CONFIG_PATH = CONFIG_DIR / "preoperative.json"
EXPORT_DPI = 150
TRAINING_IMPORT_CHECK = "import joblib, numpy, openpyxl, pandas, sklearn, tqdm"

if str(SRC_DIR) not in sys.path:
    sys.path.append(str(SRC_DIR))

plt.rcParams.update({
    "figure.dpi": EXPORT_DPI,
    "savefig.dpi": EXPORT_DPI,
    "savefig.bbox": "tight",
    "savefig.pad_inches": 0.08,
})

# The administrator "training studio" is exposed as a Blueprint mounted under
# /admin inside the main public website (see doctor_app.py). It can also be run
# standalone via `python flask_app.py` for local development.
admin_bp = Blueprint(
    "admin",
    __name__,
    url_prefix="/admin",
    template_folder="templates",
    static_folder="static",
)
LAUNCH_JOBS: dict[str, dict[str, Any]] = {}
LAUNCH_JOBS_LOCK = threading.Lock()


@admin_bp.before_request
def _admin_auth_guard():
    """Optional HTTP Basic Auth for the admin studio.

    Disabled by default (keeps the panel hidden-but-open, as intended). If both
    ADMIN_USER and ADMIN_PASSWORD environment variables are set — e.g. as secrets
    on a public Hugging Face Space — the whole /admin/ area requires those
    credentials.
    """
    user = os.environ.get("ADMIN_USER")
    password = os.environ.get("ADMIN_PASSWORD")
    if not user or not password:
        return None  # auth not configured → open
    auth = request.authorization
    if auth and auth.username == user and auth.password == password:
        return None
    return Response(
        "Administrator authentication required.",
        401,
        {"WWW-Authenticate": 'Basic realm="SBM Stratify Admin"'},
    )


# ---------------------------------------------------------------------------
# Domain helpers
# ---------------------------------------------------------------------------

def load_json(path: str | Path) -> dict[str, Any]:
    p = Path(path)
    if not p.exists():
        return {}
    with open(p, encoding="utf-8") as fh:
        return json.load(fh)


def save_json(path: Path, payload: dict[str, Any]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w", encoding="utf-8") as fh:
        json.dump(payload, fh, indent=4)


def utc_now_iso() -> str:
    return datetime.utcnow().isoformat(timespec="seconds") + "Z"


def load_json_loose(path: str | Path) -> dict[str, Any]:
    try:
        return load_json(path)
    except (json.JSONDecodeError, OSError):
        return {}


def _python_candidates() -> list[Path]:
    candidates: list[Path] = []
    env_override = os.environ.get("TRAIN_PYTHON", "").strip()
    if env_override:
        candidates.append(Path(env_override))
    candidates.extend(
        [
            Path(sys.executable),
            BASE_DIR / ".venv" / "Scripts" / "python.exe",
            BASE_DIR / ".venv" / "bin" / "python",
        ]
    )

    unique: list[Path] = []
    seen: set[str] = set()
    for candidate in candidates:
        key = str(candidate).lower()
        if key in seen:
            continue
        seen.add(key)
        unique.append(candidate)
    return unique


def resolve_training_python() -> tuple[str | None, str | None]:
    failures: list[str] = []
    for candidate in _python_candidates():
        if not candidate.exists():
            continue
        try:
            probe = subprocess.run(
                [str(candidate), "-c", TRAINING_IMPORT_CHECK],
                cwd=BASE_DIR,
                capture_output=True,
                text=True,
                encoding="utf-8",
                errors="replace",
            )
        except OSError as exc:
            failures.append(f"{candidate}: {exc}")
            continue
        if probe.returncode == 0:
            return str(candidate), None

        details = (probe.stderr or probe.stdout or "").strip()
        if details:
            failures.append(f"{candidate}: {details.splitlines()[-1]}")
        else:
            failures.append(f"{candidate}: dependency check failed with exit code {probe.returncode}")

    if failures:
        return None, "No usable Python interpreter found for training. " + " | ".join(failures)
    return None, "No usable Python interpreter found for training."


def build_training_plan(targets: list[str], models: list[str]) -> tuple[list[dict[str, Any]], int]:
    plan: list[dict[str, Any]] = []
    total_units = 0
    for target in targets:
        best_params = best_parameters_for_target(target)
        target_models = [m for m in models if isinstance(best_params.get(m), dict)]
        plan.append(
            {
                "target": target,
                "models": target_models,
                "model_params": {m: best_params[m] for m in target_models},
            }
        )
        total_units += len(target_models)
    return plan, total_units


def get_launch_job(job_id: str) -> dict[str, Any] | None:
    with LAUNCH_JOBS_LOCK:
        job = LAUNCH_JOBS.get(job_id)
        return copy.deepcopy(job) if job else None


def update_launch_job(job_id: str, **updates: Any) -> None:
    with LAUNCH_JOBS_LOCK:
        job = LAUNCH_JOBS.get(job_id)
        if not job:
            return
        job.update(updates)
        job["updated_at"] = utc_now_iso()


def update_launch_execution_row(
    execution_rows: list[dict[str, Any]], target: str, **updates: Any
) -> list[dict[str, Any]]:
    for row in execution_rows:
        if row.get("target") == target:
            row.update(updates)
            break
    return execution_rows


def slugify(value: str) -> str:
    slug = re.sub(r"[^a-zA-Z0-9]+", "-", value.strip().lower()).strip("-")
    return slug or "run"


def feature_group(feature: str) -> str:
    if feature.startswith("Comorbidities_"):
        return "Comorbidities"
    if feature.startswith("Symptoms_"):
        return "Presentation"
    if feature.startswith("Radio_") or feature.startswith("Optic") or feature.startswith("ICA/"):
        return "Imaging & Anatomy"
    if "KPS" in feature or feature in {"ASA", "Charlson Comorbidity Index"}:
        return "Functional Status"
    if feature in {"Age", "Sex", "Date of surgery", "Date of Birth"}:
        return "Demographics & Timing"
    return "Clinical Core"


def available_targets() -> list[str]:
    if not GRIDSEARCH_ROOT.exists():
        return []
    return sorted([
        child.name for child in GRIDSEARCH_ROOT.iterdir()
        if child.is_dir() and (child / "best_parameters.json").exists()
    ])


def best_parameters_for_target(target: str) -> dict[str, Any]:
    return load_json(GRIDSEARCH_ROOT / target / "best_parameters.json")


def available_models_for_target(target: str) -> list[str]:
    params = best_parameters_for_target(target)
    return sorted([k for k, v in params.items() if isinstance(v, dict)])


def model_union_for_targets(targets: list[str]) -> list[str]:
    union: set[str] = set()
    for t in targets:
        union.update(available_models_for_target(t))
    return sorted(union)


def apply_preset(preset_name: str, all_features: list[str]) -> list[str]:
    if preset_name == "all":
        return list(all_features)
    if preset_name == "clear":
        return []
    if preset_name == "clinical":
        keep = {"Demographics & Timing", "Comorbidities", "Presentation", "Functional Status", "Clinical Core"}
        return [f for f in all_features if feature_group(f) in keep]
    if preset_name == "imaging":
        keep = {"Imaging & Anatomy", "Functional Status"}
        return [f for f in all_features if feature_group(f) in keep]
    if preset_name == "compact":
        compact = {
            "Age", "Sex", "Pre-Op KPS", "ASA", "Charlson Comorbidity Index",
            "Radio_Pre-Op max_axial_diam_mm", "Radio_Tumor Location", "Radio_Tumor side", "Radio_Edema",
        }
        return [f for f in all_features if f in compact]
    return list(all_features)


def build_subset_config(data_config: dict[str, Any], selected_features: list[str]) -> dict[str, Any]:
    selected_set = set(selected_features)
    ordered = [f for f in data_config.get("input_features", []) if f in selected_set]
    return {
        "input_file": data_config.get("input_file"),
        "input_features": ordered,
        "cols_string": [f for f in data_config.get("cols_string", []) if f in selected_set],
        "cols_date": [f for f in data_config.get("cols_date", []) if f in selected_set],
        "cols_multi": [f for f in data_config.get("cols_multi", []) if f in selected_set],
    }


# ---------------------------------------------------------------------------
# Chart helpers
# ---------------------------------------------------------------------------

def fig_to_b64(fig) -> str:
    buf = BytesIO()
    fig.savefig(buf, format="png", dpi=EXPORT_DPI, bbox_inches="tight", pad_inches=0.08)
    buf.seek(0)
    data = base64.b64encode(buf.read()).decode()
    plt.close(fig)
    return "data:image/png;base64," + data


def fig_to_bytes(fig, fmt: str = "png") -> bytes:
    buf = BytesIO()
    fig.savefig(buf, format=fmt, dpi=EXPORT_DPI, bbox_inches="tight", pad_inches=0.08)
    buf.seek(0)
    return buf.read()


def primary_metric_name(task_type: str, summary_df: pd.DataFrame) -> str:
    candidates = {
        "binary": ["roc_auc", "average_precision", "recall", "accuracy"],
        "categorical": ["f1_macro", "accuracy"],
        "continuous": ["r2", "rmse", "mae"],
    }
    for metric in candidates.get(task_type, []):
        if metric in summary_df.columns:
            return metric
    numeric = [c for c in summary_df.columns
               if pd.api.types.is_numeric_dtype(summary_df[c]) and c not in {"fit_seconds"}]
    return numeric[0] if numeric else ""


def format_metric_value(metric_name: str, value: Any) -> str:
    if value is None or (isinstance(value, float) and np.isnan(value)):
        return "n/a"
    if metric_name == "fit_seconds":
        return f"{float(value):.1f}s"
    return f"{float(value):.3f}"


def _dark_fig(w: float, h: float):
    fig, ax = plt.subplots(figsize=(w, h))
    fig.patch.set_facecolor("#08111f")
    ax.set_facecolor("#0d1727")
    return fig, ax


def build_bar_chart(summary_df: pd.DataFrame, task_type: str) -> tuple[str | None, str]:
    metric = primary_metric_name(task_type, summary_df)
    if not metric or metric not in summary_df.columns:
        return None, ""
    df = (summary_df[summary_df["status"] == "ok"].copy()
          if "status" in summary_df.columns else summary_df.copy())
    if df.empty:
        return None, metric
    ascending = metric in {"rmse", "mae"}
    df = df.sort_values(metric, ascending=ascending)
    sns.set_theme(style="dark")
    fig, ax = _dark_fig(7.5, 4.2)
    palette = ["#5eead4", "#60a5fa", "#38bdf8", "#f59e0b", "#f472b6", "#c084fc"]
    sns.barplot(data=df, x=metric, y="model", hue="model", dodge=False,
                palette=palette[:len(df)], ax=ax)
    legend = ax.get_legend()
    if legend is not None:
        legend.remove()
    ax.set_title(f"Model comparison — {metric}", color="white", fontsize=13, pad=12)
    ax.set_xlabel(metric, color="#dbeafe")
    ax.set_ylabel("")
    ax.tick_params(colors="#dbeafe")
    for spine in ax.spines.values():
        spine.set_color("#1f314f")
    ax.grid(axis="x", color="#24364f", alpha=0.4)
    fig.tight_layout()
    return fig_to_b64(fig), metric


def build_heatmap_chart(summary_df: pd.DataFrame, task_type: str) -> str | None:
    candidates = {
        "binary": ["roc_auc", "average_precision", "recall", "precision",
                   "specificity", "f_beta", "accuracy"],
        "categorical": ["f1_macro", "accuracy", "roc_auc_ovr"],
        "continuous": ["r2", "rmse", "mae"],
    }
    metrics = [m for m in candidates.get(task_type, []) if m in summary_df.columns]
    if not metrics:
        return None
    df = (summary_df[summary_df["status"] == "ok"][["model", *metrics]].copy()
          if "status" in summary_df.columns else summary_df[["model", *metrics]].copy())
    if df.empty:
        return None
    df = df.set_index("model")
    fig, ax = _dark_fig(max(6, len(metrics) * 1.1), max(2.5, len(df) * 0.65))
    sns.heatmap(df, annot=True, fmt=".3f",
                cmap=sns.color_palette(["#0f172a", "#1d4ed8", "#2dd4bf"], as_cmap=True),
                linewidths=0.6, linecolor="#14233b", cbar=False, ax=ax)
    ax.set_title("Metric matrix", color="white", fontsize=13, pad=12)
    ax.tick_params(colors="#dbeafe", labelrotation=0)
    fig.tight_layout()
    return fig_to_b64(fig)


def aggregate_importances(target_dir: Path) -> pd.DataFrame:
    frames = []
    for model_dir in sorted(p for p in target_dir.iterdir() if p.is_dir()):
        fi_path = model_dir / "feature_importance.csv"
        if not fi_path.exists():
            continue
        frame = pd.read_csv(fi_path)
        if frame.empty:
            continue
        frame["Model"] = model_dir.name
        frames.append(frame)
    if not frames:
        return pd.DataFrame()
    combined = pd.concat(frames, ignore_index=True)
    return (
        combined.groupby("Feature", as_index=False)
        .agg(
            mean_importance=("Importance", "mean"),
            mean_abs_importance=("Importance", lambda s: float(np.mean(np.abs(s)))),
            std_importance=("Importance", "std"),
            models_reported=("Model", "nunique"),
        )
        .fillna({"std_importance": 0.0})
        .sort_values("mean_abs_importance", ascending=False)
    )


def build_importance_chart(aggregated: pd.DataFrame) -> str | None:
    if aggregated.empty:
        return None
    top = aggregated.head(15).sort_values("mean_abs_importance", ascending=True)
    fig, ax = _dark_fig(8, max(4.2, len(top) * 0.35))
    ax.barh(top["Feature"], top["mean_abs_importance"],
            color="#5eead4", alpha=0.85, edgecolor="#99f6e4")
    ax.set_title("Cross-model permutation importance", color="white", fontsize=13, pad=12)
    ax.set_xlabel("Mean absolute importance", color="#dbeafe")
    ax.tick_params(colors="#dbeafe")
    for spine in ax.spines.values():
        spine.set_color("#1f314f")
    ax.grid(axis="x", color="#24364f", alpha=0.35)
    fig.tight_layout()
    return fig_to_b64(fig)


def build_group_dist_chart(features: list[str]) -> str | None:
    if not features:
        return None
    dist = (
        pd.Series([feature_group(f) for f in features])
        .value_counts()
        .sort_values(ascending=True)
    )
    fig, ax = _dark_fig(6.4, max(3.0, len(dist) * 0.55))
    ax.barh(dist.index, dist.values, color="#60a5fa", alpha=0.9)
    ax.set_title("Selected features by group", color="white", fontsize=13, pad=12)
    ax.tick_params(colors="#dbeafe")
    ax.set_xlabel("Count", color="#dbeafe")
    for spine in ax.spines.values():
        spine.set_color("#1f314f")
    ax.grid(axis="x", color="#24364f", alpha=0.35)
    fig.tight_layout()
    return fig_to_b64(fig)


# ---------------------------------------------------------------------------
# Run discovery and results
# ---------------------------------------------------------------------------

def discover_runs() -> list[dict[str, Any]]:
    groups: dict[Path, set[str]] = {}
    for metadata_path in OUTPUTS_ROOT.rglob("metadata.json"):
        target_dir = metadata_path.parent
        summary_path = target_dir / "benchmark_summary.csv"
        if not summary_path.exists():
            continue
        run_root = target_dir.parent
        groups.setdefault(run_root, set()).add(target_dir.name)

    runs = []
    for run_root, targets in groups.items():
        if run_root == OUTPUTS_ROOT:
            continue
        rel_path = run_root.relative_to(BASE_DIR)
        updated_at = datetime.fromtimestamp(run_root.stat().st_mtime)
        runs.append({
            "path": str(run_root),
            "rel_path": str(rel_path),
            "targets": sorted(targets),
            "label": f"{rel_path}  |  {len(targets)} target(s)  |  {updated_at.strftime('%Y-%m-%d %H:%M')}",
            "updated_at": updated_at.isoformat(),
        })
    return sorted(runs, key=lambda r: r["updated_at"], reverse=True)


def get_run_results(run_root: Path) -> dict[str, Any]:
    target_dirs = sorted(
        [c for c in run_root.iterdir()
         if c.is_dir() and c.name != "_runtime"
         and (c / "metadata.json").exists()
         and (c / "benchmark_summary.csv").exists()],
        key=lambda p: p.name,
    )

    results = []
    for target_dir in target_dirs:
        metadata = load_json(target_dir / "metadata.json")
        summary_path = target_dir / "benchmark_summary.csv"
        summary_df = pd.read_csv(summary_path) if summary_path.exists() else pd.DataFrame()
        task_type = metadata.get("task_type", "binary")

        ok_rows = (summary_df[summary_df["status"] == "ok"].copy()
                   if not summary_df.empty and "status" in summary_df.columns
                   else summary_df.copy())
        selected_metric = primary_metric_name(task_type, summary_df) if not summary_df.empty else ""

        cards: dict[str, Any] = {
            "target": target_dir.name,
            "split_strategy": metadata.get("split_strategy", "unknown").upper(),
            "feature_count": len(metadata.get("data_configuration", {}).get("input_features", [])),
            "model_count": len(ok_rows),
            "best_metric_name": None,
            "best_metric_value": None,
            "best_model": None,
        }
        if not ok_rows.empty and selected_metric in ok_rows.columns:
            ascending = selected_metric in {"rmse", "mae"}
            best_idx = (ok_rows[selected_metric].idxmin() if ascending
                        else ok_rows[selected_metric].idxmax())
            cards["best_metric_name"] = selected_metric
            cards["best_metric_value"] = format_metric_value(
                selected_metric, ok_rows.loc[best_idx, selected_metric])
            cards["best_model"] = str(ok_rows.loc[best_idx, "model"]).upper()

        preferred_cols = [c for c in [
            "model", "status", "roc_auc", "average_precision", "recall", "precision",
            "specificity", "f_beta", "accuracy", "r2", "rmse", "mae", "fit_seconds",
        ] if c in summary_df.columns]
        summary_table = (summary_df[preferred_cols].to_dict(orient="records")
                         if preferred_cols else [])

        bar_img, bar_metric = build_bar_chart(summary_df, task_type)
        heatmap_img = build_heatmap_chart(summary_df, task_type)

        aggregated = aggregate_importances(target_dir)
        importance_img = build_importance_chart(aggregated)
        weakest: list[dict] = []
        if not aggregated.empty:
            w = aggregated.sort_values("mean_abs_importance", ascending=True).head(10)
            weakest = w.rename(columns={
                "Feature": "Candidate to remove",
                "mean_importance": "Mean importance",
                "mean_abs_importance": "Mean abs importance",
                "models_reported": "Models",
            }).to_dict(orient="records")

        combined_curves = []
        for plot_name in ["combined_roc_curve.png", "combined_pr_curve.png"]:
            p = target_dir / plot_name
            if p.exists():
                combined_curves.append({
                    "path": str(p),
                    "title": plot_name.replace(".png", "").replace("_", " ").title(),
                })

        model_names = []
        if not summary_df.empty and "model" in summary_df.columns:
            model_names = summary_df["model"].tolist()
        else:
            model_names = sorted([c.name for c in target_dir.iterdir() if c.is_dir()])

        models_data = []
        for model_name in model_names:
            model_dir = target_dir / model_name
            metrics_raw = load_json(model_dir / "metrics.json")
            scalar_metrics = {
                k: format_metric_value(k, v)
                for k, v in metrics_raw.items()
                if isinstance(v, (int, float)) and k != "confusion_matrix"
            }
            plot_files = [
                "roc_curve.png", "pr_curve.png", "confusion_matrix.png",
                "feature_importance.png", "actual_vs_predicted.png",
            ]
            available_plots = [
                {"path": str(model_dir / p),
                 "title": p.replace(".png", "").replace("_", " ").title()}
                for p in plot_files if (model_dir / p).exists()
            ]
            fi_path = model_dir / "feature_importance.csv"
            fi_table: list[dict] = []
            if fi_path.exists():
                fi_df = pd.read_csv(fi_path)
                fi_table = fi_df.head(20).to_dict(orient="records")
            models_data.append({
                "name": model_name,
                "metrics": scalar_metrics,
                "plots": available_plots,
                "fi_table": fi_table,
                "raw_metrics": metrics_raw,
            })

        results.append({
            "target": target_dir.name,
            "cards": cards,
            "summary_table": summary_table,
            "summary_columns": preferred_cols,
            "bar_img": bar_img,
            "bar_metric": bar_metric,
            "heatmap_img": heatmap_img,
            "importance_img": importance_img,
            "weakest": weakest,
            "combined_curves": combined_curves,
            "models": models_data,
        })

    return {"targets": results, "run_path": str(run_root)}


def run_training_job(
    job_id: str,
    payload: dict[str, Any],
    training_plan: list[dict[str, Any]],
) -> None:
    try:
        run_root = Path(payload["run_root"])
        runtime_root = Path(payload["runtime_root"])
        data_config_path = Path(payload["data_config_path"])
        training_python = payload["training_python"]
        total_units = int(payload["total_units"])
        completed_units = 0
        completed_targets = 0
        execution_rows = copy.deepcopy(payload["execution"])

        update_launch_job(
            job_id,
            status="running",
            started_at=utc_now_iso(),
            current_step="Preparing training runtime...",
        )

        for item in training_plan:
            target = item["target"]
            target_models = item["models"]
            target_output_dir = run_root / target

            if not target_models:
                execution_rows = update_launch_execution_row(
                    execution_rows,
                    target,
                    status="skipped",
                    message="No saved best parameters for the selected models.",
                )
                completed_targets += 1
                update_launch_job(
                    job_id,
                    execution=copy.deepcopy(execution_rows),
                    completed_targets=completed_targets,
                )
                continue

            model_config_path = runtime_root / f"{slugify(target)}-model-config.json"
            save_json(model_config_path, item["model_params"])
            log_path = runtime_root / f"{slugify(target)}.log"
            progress_path = runtime_root / f"{slugify(target)}-progress.json"
            if progress_path.exists():
                progress_path.unlink()

            execution_rows = update_launch_execution_row(
                execution_rows,
                target,
                status="running",
                message="Starting training...",
            )
            update_launch_job(
                job_id,
                execution=copy.deepcopy(execution_rows),
                current_target=target,
                current_model=None,
                current_step=f"Preparing {target}...",
            )

            cmd = [
                training_python,
                str(SRC_DIR / "train.py"),
                "--target",
                target,
                "--data_config",
                str(data_config_path),
                "--model_config",
                str(model_config_path),
                "--models",
                ",".join(target_models),
                "--output_folder",
                str(target_output_dir),
                "--split_strategy",
                payload["split_strategy"],
                "--test_size",
                str(payload["test_size"]),
                "--split_column",
                payload["split_column"],
                "--date_column",
                payload["date_column"],
                "--feature_importance",
                "--threshold_val_size",
                str(payload["threshold_val_size"]),
                "--min_recall",
                str(payload["min_recall"]),
                "--f_beta",
                str(payload["f_beta"]),
                "--fn_cost",
                str(payload["fn_cost"]),
                "--fp_cost",
                str(payload["fp_cost"]),
                "--progress_path",
                str(progress_path),
            ]
            child_env = os.environ.copy()
            child_env.setdefault("LOKY_MAX_CPU_COUNT", "1")
            child_env.setdefault("PYTHONUNBUFFERED", "1")

            with open(log_path, "w", encoding="utf-8") as log_fh:
                proc = subprocess.Popen(
                    cmd,
                    cwd=BASE_DIR,
                    stdout=log_fh,
                    stderr=subprocess.STDOUT,
                    text=True,
                    encoding="utf-8",
                    errors="replace",
                    env=child_env,
                )

                while True:
                    rc = proc.poll()
                    progress = load_json_loose(progress_path)
                    target_completed_models = int(progress.get("completed_models", 0))
                    current_model = progress.get("current_model")
                    current_step = progress.get("current_step") or f"Training {target}..."
                    message = progress.get("message") or current_step
                    progress_pct = (
                        round(100.0 * (completed_units + target_completed_models) / total_units, 1)
                        if total_units
                        else 100.0
                    )

                    execution_rows = update_launch_execution_row(
                        execution_rows,
                        target,
                        status="running",
                        message=message,
                    )
                    update_launch_job(
                        job_id,
                        execution=copy.deepcopy(execution_rows),
                        current_target=target,
                        current_model=current_model,
                        current_step=current_step,
                        completed_units=completed_units + target_completed_models,
                        progress_pct=progress_pct,
                    )

                    if rc is not None:
                        break
                    time.sleep(0.8)

            combined_log = log_path.read_text(encoding="utf-8", errors="replace").strip()
            completed_units += len(target_models)
            completed_targets += 1
            status = "ok" if proc.returncode == 0 else "failed"
            execution_rows = update_launch_execution_row(
                execution_rows,
                target,
                status=status,
                returncode=proc.returncode,
                log=combined_log[:4000],
                message="" if proc.returncode == 0 else "Training subprocess failed. See console log.",
            )
            update_launch_job(
                job_id,
                execution=copy.deepcopy(execution_rows),
                completed_units=completed_units,
                completed_targets=completed_targets,
                progress_pct=(round(100.0 * completed_units / total_units, 1) if total_units else 100.0),
            )

        run_results = None
        try:
            run_results = get_run_results(run_root)
        except Exception:
            pass

        update_launch_job(
            job_id,
            status="completed",
            finished_at=utc_now_iso(),
            execution=copy.deepcopy(execution_rows),
            run_results=run_results,
            current_target=None,
            current_model=None,
            current_step="Training completed.",
            completed_units=total_units,
            completed_targets=len(training_plan),
            progress_pct=100.0,
        )
    except Exception as exc:
        update_launch_job(
            job_id,
            status="failed",
            finished_at=utc_now_iso(),
            error=str(exc),
            current_step="Training failed.",
        )


# ---------------------------------------------------------------------------
# API Routes
# ---------------------------------------------------------------------------

@admin_bp.route("/")
def index():
    return render_template("index.html")


@admin_bp.route("/api/config")
def get_config():
    data_config = load_json(PREOPERATIVE_CONFIG_PATH)
    all_features: list[str] = data_config.get("input_features", [])
    targets = available_targets()
    all_models = model_union_for_targets(targets)
    groups = sorted({feature_group(f) for f in all_features})
    group_map = {g: [f for f in all_features if feature_group(f) == g] for g in groups}
    return jsonify({
        "all_features": all_features,
        "targets": targets,
        "all_models": all_models,
        "groups": groups,
        "group_map": group_map,
    })


@admin_bp.route("/api/features/preset", methods=["POST"])
def feature_preset():
    data = request.get_json()
    preset = data.get("preset", "all")
    all_features: list[str] = data.get("all_features", [])
    return jsonify({"features": apply_preset(preset, all_features)})


@admin_bp.route("/api/features/group-action", methods=["POST"])
def feature_group_action():
    data = request.get_json()
    action = data.get("action", "add")
    group_name = data.get("group", "")
    all_features: list[str] = data.get("all_features", [])
    selected: list[str] = data.get("selected", [])
    group_features = [f for f in all_features if feature_group(f) == group_name]

    if action == "add":
        seen: set[str] = set(selected)
        new = list(selected)
        for f in group_features:
            if f not in seen:
                new.append(f)
    elif action == "remove":
        gset = set(group_features)
        new = [f for f in selected if f not in gset]
    elif action == "only":
        new = list(group_features)
    else:
        new = list(selected)

    return jsonify({"features": new})


@admin_bp.route("/api/chart/group-distribution", methods=["POST"])
def group_distribution_chart():
    data = request.get_json()
    features: list[str] = data.get("features", [])
    img = build_group_dist_chart(features)
    return jsonify({"image": img})


@admin_bp.route("/api/models-for-targets", methods=["POST"])
def models_for_targets():
    data = request.get_json()
    targets: list[str] = data.get("targets", [])
    models = model_union_for_targets(targets)
    availability = [
        {
            "target": t,
            "available_models": ", ".join(available_models_for_target(t)) or "none",
            "n_models": len(available_models_for_target(t)),
        }
        for t in targets
    ]
    return jsonify({"models": models, "availability": availability})


@admin_bp.route("/api/launch", methods=["POST"])
def launch_training():
    data = request.get_json()
    targets: list[str] = data.get("targets", [])
    models: list[str] = data.get("models", [])
    selected_features: list[str] = data.get("selected_features", [])
    run_name: str = data.get("run_name", "")
    split_strategy: str = data.get("split_strategy", "temporal")
    test_size = float(data.get("test_size", 0.20))
    threshold_val_size = float(data.get("threshold_val_size", 0.20))
    min_recall = float(data.get("min_recall", 0.90))
    f_beta = float(data.get("f_beta", 2.0))
    fn_cost = float(data.get("fn_cost", 5.0))
    fp_cost = float(data.get("fp_cost", 1.0))
    split_column: str = data.get("split_column", "Split")
    date_column: str = data.get("date_column", "Date of surgery")
    training_python, training_error = resolve_training_python()
    if not training_python:
        return jsonify({
            "execution": [],
            "run_path": "",
            "run_results": None,
            "error": training_error,
        }), 500

    base_config = load_json(PREOPERATIVE_CONFIG_PATH)
    timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
    label = slugify(run_name or "studio-run")
    run_root = STUDIO_RUNS_ROOT / f"{timestamp}-{label}"
    runtime_root = run_root / "_runtime"
    runtime_root.mkdir(parents=True, exist_ok=True)

    subset_config = build_subset_config(base_config, selected_features)
    data_config_path = runtime_root / "data_config.json"
    save_json(data_config_path, subset_config)
    training_plan, total_units = build_training_plan(targets, models)
    execution_rows: list[dict[str, Any]] = []
    for item in training_plan:
        target_models = item["models"]
        execution_rows.append(
            {
                "target": item["target"],
                "status": "queued" if target_models else "skipped",
                "models": ", ".join(target_models),
                "returncode": None,
                "log": "",
                "output_dir": str(run_root / item["target"]),
                "message": (
                    "Queued for training..."
                    if target_models
                    else "No saved best parameters for the selected models."
                ),
            }
        )

    job_id = uuid.uuid4().hex
    job_payload = {
        "run_root": str(run_root),
        "runtime_root": str(runtime_root),
        "data_config_path": str(data_config_path),
        "training_python": training_python,
        "split_strategy": split_strategy,
        "test_size": test_size,
        "threshold_val_size": threshold_val_size,
        "min_recall": min_recall,
        "f_beta": f_beta,
        "fn_cost": fn_cost,
        "fp_cost": fp_cost,
        "split_column": split_column,
        "date_column": date_column,
        "total_units": total_units,
        "execution": copy.deepcopy(execution_rows),
    }
    job = {
        "job_id": job_id,
        "status": "queued",
        "created_at": utc_now_iso(),
        "updated_at": utc_now_iso(),
        "started_at": None,
        "finished_at": None,
        "run_path": str(run_root),
        "run_results": None,
        "python_executable": training_python,
        "execution": copy.deepcopy(execution_rows),
        "progress_pct": 0.0,
        "completed_units": 0,
        "total_units": total_units,
        "completed_targets": 0,
        "total_targets": len(training_plan),
        "current_target": None,
        "current_model": None,
        "current_step": "Queued for training...",
        "error": None,
    }
    with LAUNCH_JOBS_LOCK:
        LAUNCH_JOBS[job_id] = job

    thread = threading.Thread(
        target=run_training_job,
        args=(job_id, job_payload, training_plan),
        daemon=True,
    )
    thread.start()
    return jsonify(get_launch_job(job_id)), 202


@admin_bp.route("/api/launch/status")
def launch_training_status():
    job_id = request.args.get("job_id", "").strip()
    if not job_id:
        return jsonify({"error": "No job id provided"}), 400
    job = get_launch_job(job_id)
    if not job:
        return jsonify({"error": "Training job not found"}), 404
    return jsonify(job)


@admin_bp.route("/api/runs")
def list_runs():
    return jsonify({"runs": discover_runs()})


@admin_bp.route("/api/run/results")
def run_results():
    run_path = request.args.get("path", "")
    if not run_path:
        return jsonify({"error": "No path provided"}), 400
    run_root = Path(run_path)
    if not run_root.exists():
        return jsonify({"error": "Run path does not exist"}), 404
    try:
        results = get_run_results(run_root)
        return jsonify(results)
    except Exception as exc:
        return jsonify({"error": str(exc)}), 500


@admin_bp.route("/api/image")
def serve_image():
    path = request.args.get("path", "")
    if not path:
        return "No path", 400
    p = Path(path)
    if not p.exists():
        return "Not found", 404
    try:
        p.resolve().relative_to(BASE_DIR.resolve())
    except ValueError:
        return "Forbidden", 403
    return send_file(str(p), mimetype="image/png")


def create_admin_app() -> Flask:
    """Build a standalone Flask app exposing only the admin studio (dev use)."""
    standalone = Flask(__name__)
    standalone.secret_key = "sbm-stratify-2024"
    standalone.register_blueprint(admin_bp)
    return standalone


if __name__ == "__main__":
    create_admin_app().run(debug=True, use_reloader=False, port=5000)