1
donnees_meteo/docs/12 - Conclusion/scripts/plot_weekday_profiles.py
2025-11-26 17:22:57 +01:00

80 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from pathlib import Path
import sys
import pandas as pd
PROJECT_ROOT = Path(__file__).resolve().parents[3]
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
from meteo.dataset import load_raw_csv
from meteo.variables import VARIABLES_BY_KEY, Variable
from meteo.plots import plot_weekday_profiles
CSV_PATH = PROJECT_ROOT / "data" / "weather_minutely.csv"
DOC_DIR = Path(__file__).resolve().parent.parent
OUTPUT_PATH = DOC_DIR / "figures" / "weekday_profiles.png"
# On se concentre sur le ressenti "agréable" : température, humidité, lumière.
VARIABLE_KEYS = ["temperature", "humidity", "illuminance"]
def compute_weekday_means(df: pd.DataFrame, variables: list[Variable]) -> pd.DataFrame:
"""
Calcule, pour chaque jour de semaine (0=lundi,…,6=dimanche),
la moyenne des variables fournies.
"""
if df.empty:
return pd.DataFrame(index=range(7))
weekday_index = df.index.dayofweek
columns = [var.column for var in variables]
weekday_means = df.groupby(weekday_index)[columns].mean()
# S'assure que toutes les valeurs 06 sont présentes, même si certaines manquent.
weekday_means = weekday_means.reindex(range(7))
weekday_means.index.name = "weekday"
return weekday_means
def main() -> None:
if not CSV_PATH.exists():
print(f"⚠ Fichier introuvable : {CSV_PATH}")
return
df = load_raw_csv(CSV_PATH)
print(f"Dataset minuté chargé : {CSV_PATH}")
print(f" Lignes : {len(df)}")
print(f" Colonnes : {list(df.columns)}")
print()
variables = [VARIABLES_BY_KEY[key] for key in VARIABLE_KEYS]
weekday_means = compute_weekday_means(df, variables)
output_path = plot_weekday_profiles(
weekday_df=weekday_means,
variables=variables,
output_path=OUTPUT_PATH,
title="Moyennes par jour de semaine",
)
print(f"✔ Profils hebdomadaires exportés : {output_path}")
weekday_labels_long = ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"]
for var in variables:
series = weekday_means[var.column]
if series.isna().all():
continue
best_idx = int(series.idxmax())
best_label = weekday_labels_long[best_idx]
best_value = series.max()
unit = f" {var.unit}" if var.unit else ""
print(f"{var.label} maximale en moyenne le {best_label} (≈{best_value:.2f}{unit})")
if __name__ == "__main__":
main()