import json
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import sys
import os

# -------- Load JSON file --------
filename = "race_times1.json"  # Change or pass via sys.argv

if not os.path.exists(filename):
    print(f"File '{filename}' not found.")
    sys.exit(1)

with open(filename, "r") as f:
    data = json.load(f)

# -------- Analyze Arrays --------
stats = {}
plot_data = []
labels = []

for key, array in data.items():
    values = np.array(array)
    if values.ndim != 1:
        print(f"Skipping non-1D array: {key}")
        continue

    mean = np.mean(values)
    std = np.std(values)
    stats[key] = {"mean": mean, "std": std}

    plot_data.append(values)
    labels.append(key)

# -------- Print Stats --------
print("Array Statistics:")
for key, s in stats.items():
    print(f"{key}: mean = {s['mean']:.4f}, std = {s['std']:.4f}")

# -------- Plot Violin --------
plt.figure(figsize=(14, 12))
sns.violinplot(data=plot_data)
plt.xticks(ticks=range(len(labels)), labels=labels)


plt.grid(True)
plt.tight_layout()

plt.title("Time to finish", fontsize=24)
plt.ylabel("Time(s)", fontsize=24)
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)

plt.show()
