import json

import matplotlib.pyplot as plt

with open("output/autotune.json", "r") as f:
    history = json.load(f)

history = sorted(history, key=lambda x: x["metrics"]["combined_score"], reverse=False)

with open("output/autotune_sorted.json", "w") as f:
    json.dump(history, f, indent=2)


# Extract combined scores
scores = [item["metrics"]["coherence"] for item in history]

# Plot histogram
plt.hist(scores, bins=20, edgecolor="black")
plt.title("Distribution of Combined Scores")
plt.xlabel("Combined Score")
plt.ylabel("Frequency")
plt.grid(True)
plt.tight_layout()
plt.savefig("combined_score_distribution.png")
plt.close()
