Files
patient_leakage_detection/scripts/classification/classification_siamese.py
T
rpotter6298 35cbd9ac3c 2026001
2026-07-01 17:35:58 +02:00

78 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""classification_siamese.py — Run classification with siamese patient manifest."""
import os, sys, json
import numpy as np
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["OPENBLAS_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(
os.path.abspath(__file__)))))
from classes import PatientLeakageClassifier
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(
os.path.abspath(__file__))))
MANIFEST = os.path.join(ROOT, "results", "siamese_manifest.csv")
FEATURES_DIR = os.path.join(ROOT, "features")
RESULTS_DIR = os.path.join(ROOT, "results")
SEED = 20
MODELS = ["VGG16", "DenseNet121", "EfficientNetB1", "MobileNetV2", "ResNet50"]
clf = PatientLeakageClassifier(MANIFEST, FEATURES_DIR, n_jobs=6)
print("=" * 60)
print("Siamese-based patient classification")
print("=" * 60)
results = []
for model_name in MODELS:
print(f"\n {model_name} ...", flush=True)
img = clf.run(model_name, SEED, "image")
pat = clf.run(model_name, SEED, "patient")
drop = img["test"] - pat["test"]
results.append({
"model": model_name, "manifest": "siamese",
"image_cv": img["cv"], "image_test": img["test"],
"patient_cv": pat["cv"], "patient_test": pat["test"],
"drop": drop,
})
print(f" Image: CV={img['cv']:.4f} Test={img['test']:.4f}")
print(f" Patient: CV={pat['cv']:.4f} Test={pat['test']:.4f} "
f"Drop={drop:.4f}")
# Save
out = os.path.join(RESULTS_DIR, "classification_siamese.json")
with open(out, "w") as f:
json.dump(results, f, indent=2)
# Comparison table
print(f"\n{'='*80}")
print("COMPARISON — All three patient-clustering methods (seed=20)")
print(f"{'='*80}")
def safe_load(path):
if os.path.exists(path):
with open(path) as f: return {r["model"]: r for r in json.load(f)}
return None
pca50 = safe_load(os.path.join(RESULTS_DIR, "classification_pca50.json"))
thumbnail = safe_load(os.path.join(RESULTS_DIR, "classification_thumbnail.json"))
siamese = {r["model"]: r for r in results}
print(f"\n{'Model':<18s} {'PCA50 Pat':>10s} {'Thumb Pat':>11s} {'Siam Pat':>10s} "
f"{'PCA50 Drop':>11s} {'Thumb Drop':>11s} {'Siam Drop':>10s}")
print("-" * 82)
for m in MODELS:
f_pat = f"{pca50[m]['patient_test']:>10.4f}" if pca50 else " N/A"
t_pat = f"{thumbnail[m]['patient_test']:>11.4f}" if thumbnail else " N/A"
f_drop = f"{pca50[m]['drop']:>11.4f}" if pca50 else " N/A"
t_drop = f"{thumbnail[m]['drop']:>11.4f}" if thumbnail else " N/A"
print(f"{m:<18s} {f_pat} {t_pat} "
f"{siamese[m]['patient_test']:>10.4f} {f_drop} {t_drop} "
f"{siamese[m]['drop']:>10.4f}")
print(f"\nSaved → {out}")
print("DONE")