import pickle
from pathlib import Path

import matplotlib.pyplot as plt
from sklearn.metrics import auc

results_path = Path(
    "/home/fedex/mt/projects/thesis-kowalczyk-jan/Deep-SAD-PyTorch/log/DeepSAD/subter_selective"
)

# Paths to your result files
deepsad_results_file = results_path / "results.pkl"
isoforest_results_file = results_path / "results_isoforest.pkl"

# Load DeepSAD precision-recall data
with deepsad_results_file.open("rb") as f:
    data = pickle.load(f)
    deep_precision, deep_recall, _ = data["test_prc"]

# Compute AP for DeepSAD
deep_ap = auc(deep_recall, deep_precision)

# Load IsoForest precision-recall data
with isoforest_results_file.open("rb") as f:
    data = pickle.load(f)
    iso_precision, iso_recall, _ = data["test_prc"]

# Compute AP for IsoForest
iso_ap = auc(iso_recall, iso_precision)

# Create plot
plt.figure(figsize=(8, 6))

# Plot DeepSAD PR curve
plt.plot(deep_recall, deep_precision, color="b", label=f"DeepSAD (AP = {deep_ap:.2f})")

# Plot IsoForest PR curve
plt.plot(iso_recall, iso_precision, color="r", label=f"IsoForest (AP = {iso_ap:.2f})")

# Labels
plt.xlabel("Recall")
plt.ylabel("Precision")
plt.title("Precision-Recall Curve (Single Run)")

# Add legend
plt.legend(loc="upper right")

# Save and/or show plot
plt.savefig("pr_curve_single_run.png")
plt.show()
