import glob

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from scipy.stats import sem, t


def calculate_confidence_interval(data, confidence=0.95):
    n = len(data)
    m = np.mean(data)
    std_err = sem(data)
    h = std_err * t.ppf((1 + confidence) / 2, n - 1)
    return m, m-h, m+h

runs = glob.glob("run*.txt")
TOTAL_EVENTS = 22509
percent_delays = []
all_delays = []

for run in runs:
    with open(run, "r") as f:
        delays = [float(line.split()[3]) for line in f if float(line.split()[3]) > 0]  # consider only positive delays
        percent_delays.append((len(delays) / TOTAL_EVENTS) * 100)
        all_delays.extend(delays)

average_percent_delays = np.mean(percent_delays)
confidence_interval_percent_delays = calculate_confidence_interval(percent_delays)
print(f"Average percentage of delayed events across all runs: {average_percent_delays:.2f}%")
print(f"95% Confidence interval: ({confidence_interval_percent_delays[1]:.2f}%, {confidence_interval_percent_delays[2]:.2f}%)")

if all_delays:
    mean_delay = np.mean(all_delays)
    confidence_interval_delays = calculate_confidence_interval(all_delays)
    print(f"Mean delay time: {mean_delay:.2f}")
    print(f"95% Confidence interval for delay time: ({confidence_interval_delays[1]:.2f}, {confidence_interval_delays[2]:.2f})")
else:
    print("No delay data available for calculation.")

sns.histplot(all_delays, bins=30, kde=False)
plt.xlabel('Delay Time (ms)')
plt.ylabel('Frequency')
plt.yscale('log')
plt.title('Histogram of Delay Times (macOS)')

plt.savefig('delays.png', dpi=300)

plt.show()
