# -*- coding: utf-8 -*-
"""
Created on Wed Jan 28 04:28:34 2026

@author: pjaub
"""

from math import ceil, sqrt
import random as rd
import matplotlib.pyplot as plt

plt.close("all")

def hms_en_heures(s):
    h, m, sec = int(s[:2]), int(s[3:5]), int(s[6:8])
    return (3600*h + 60*m + sec)/3600


with open("diagonale_des_fous_temps_2025.txt", "r", encoding="utf-8") as f:
    temps = [hms_en_heures(line.strip()) for line in f if line.strip()]
T_2025 = sorted(temps)

with open("diagonale_des_fous_temps_2024.txt", "r", encoding="utf-8") as f:
    temps = [hms_en_heures(line.strip()) for line in f if line.strip()]
T_2024 = sorted(temps)

with open("diagonale_des_fous_temps_2023.txt", "r", encoding="utf-8") as f:
    temps = [hms_en_heures(line.strip()) for line in f if line.strip()]
T_2023 = sorted(temps)

with open("diagonale_des_fous_temps_2022.txt", "r", encoding="utf-8") as f:
    temps = [hms_en_heures(line.strip()) for line in f if line.strip()]
T_2022 = sorted(temps)

def moyenne(L):
    return # A compléter

def ecart_type(L):
    m = moyenne(L)

    return # A compléter

def frequences_cumulées(Serie):
    """
         A compléter
    """
    # A compléter
    return x, y



def proportion_intervalle(L):
    """
         A compléter
    """
    xbar = moyenne(L)
    s = ecart_type(L)
    a, b = xbar - 2*s, xbar + 2*s
    return sum(1 for x in L if a <= x <= b)/len(L)



def intervalle_min_95(Serie):
    """
         A compléter
    """
    L= sorted(Serie)
    n = len(L)
    N = ceil(0.95*(n))    # A commenter
    i = 0
    for k in range(1, n-N+1):
        if L[k+N-1] - L[k] < L[i+N-1] - L[i]:
            i = k
    return i, i+N-1, L[i], L[i+N-1], (L[i]+L[i+N-1])/2


for a, L in [(2022,T_2022), (2023,T_2023), (2024,T_2024), (2025,T_2025)]:
    print(a)
    print(moyenne(L))
    print(ecart_type(L))
    print(proportion_intervalle(L))
    print(intervalle_min_95(L))
    print('')
    S = L 
    plt.figure(str(a))
    x, y = frequences_cumulées(S)
    plt.plot(x,y, label="Frequences cumulées")
    t = intervalle_min_95(S)
    plt.plot( [t[2],t[2]], [0, 1],'r',label = 'intervalle min')
    plt.plot( [t[3],t[3]], [0, 1],'r')
    m = moyenne(S)
    s = ecart_type(S)
    plt.plot( [m, m], [0, 1], 'c--',label = 'moyenne')
    plt.plot( [m-2*s, m-2*s], [0, 1],'c',label = 'moyenne ± 2*sigma')
    plt.plot( [m+2*s, m+2*s], [0, 1],'c')
    plt.grid(True)
    plt.legend()
    plt.show()
    
    
