| from __future__ import annotations |
|
|
| import argparse |
| import sys |
| from pathlib import Path |
|
|
| ROOT = Path(__file__).resolve().parents[1] |
| if str(ROOT) not in sys.path: |
| sys.path.insert(0, str(ROOT)) |
|
|
| import numpy as np |
| import pandas as pd |
|
|
| from src.models.complementarity import save_json |
| from src.models.fusion import concat_features, cross_validated_benchmark, feature_matrix, stacking_fusion |
| from src.utils import ensure_dir, load_config, seed_everything |
|
|
|
|
| def _load_feature(path: Path) -> pd.DataFrame: |
| if not path.exists(): |
| raise FileNotFoundError(path) |
| return pd.read_csv(path) |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--config", default="configs/fusion_baseline.yaml") |
| args = parser.parse_args() |
| cfg = load_config(args.config) |
| seed_everything(cfg.get("seed", 17)) |
| features_dir = Path(cfg["paths"]["features_dir"]) |
| out_dir = ensure_dir(cfg["paths"]["output_dir"]) |
|
|
| labels = pd.read_csv(features_dir / "labels.csv") |
| y = labels["label"].to_numpy() |
| rad = _load_feature(features_dir / "radiomics_clean.csv") |
| dl_bbox = _load_feature(features_dir / "dl_bbox_medicalnet.csv") |
| dl_patch = _load_feature(features_dir / "dl_patch_triregion.csv") |
|
|
| results = {} |
| x_rad, _ = feature_matrix(rad) |
| x_bbox, _ = feature_matrix(dl_bbox) |
| x_patch, _ = feature_matrix(dl_patch) |
| fusion_x = concat_features([rad, dl_patch]).drop(columns=["patient_id"]).to_numpy() |
|
|
| results["radiomics"] = cross_validated_benchmark(x_rad, y, n_splits=cfg["classifier"]["cv_folds"], seed=cfg.get("seed", 17)) |
| results["dl_bbox"] = cross_validated_benchmark(x_bbox, y, n_splits=cfg["classifier"]["cv_folds"], seed=cfg.get("seed", 17)) |
| results["dl_patch"] = cross_validated_benchmark(x_patch, y, n_splits=cfg["classifier"]["cv_folds"], seed=cfg.get("seed", 17)) |
| results["fusion"] = cross_validated_benchmark(fusion_x, y, n_splits=cfg["classifier"]["cv_folds"], seed=cfg.get("seed", 17)) |
| results["stacking"] = stacking_fusion(x_rad, x_patch, y, n_splits=cfg["classifier"]["cv_folds"], seed=cfg.get("seed", 17)) |
|
|
| summary = {k: {"auc": v["auc"], "accuracy": v["accuracy"]} for k, v in results.items()} |
| save_json(summary, out_dir / "benchmark_summary.json") |
| print(summary) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|