1

Ajout des matrices de corrélation + Refactoring

This commit is contained in:
2025-11-19 23:31:38 +01:00
parent 3a1f7e2a7e
commit a4d3ce7b49
13 changed files with 165 additions and 26 deletions

View File

@@ -6,9 +6,11 @@ from .core import BinnedStatistics, DiurnalCycleStats, MONTH_ORDER
from .correlations import (
compute_correlation_matrix,
compute_correlation_matrix_for_variables,
compute_correlation_matrices_for_methods,
compute_lagged_correlation,
compute_rolling_correlation_series,
compute_rolling_correlations_for_pairs,
transform_correlation_matrix,
)
from .events import build_event_aligned_segments, detect_threshold_events
from .filters import filter_by_condition
@@ -28,9 +30,11 @@ __all__ = [
"MONTH_ORDER",
"compute_correlation_matrix",
"compute_correlation_matrix_for_variables",
"compute_correlation_matrices_for_methods",
"compute_lagged_correlation",
"compute_rolling_correlation_series",
"compute_rolling_correlations_for_pairs",
"transform_correlation_matrix",
"build_event_aligned_segments",
"detect_threshold_events",
"filter_by_condition",

View File

@@ -2,7 +2,7 @@
from __future__ import annotations
from typing import Literal, Sequence
from typing import Callable, Literal, Sequence
import numpy as np
import pandas as pd
@@ -11,13 +11,16 @@ from meteo.variables import Variable
from .core import _ensure_datetime_index
__all__ = ['compute_correlation_matrix', 'compute_correlation_matrix_for_variables', 'compute_lagged_correlation', 'compute_rolling_correlation_series', 'compute_rolling_correlations_for_pairs']
__all__ = ['compute_correlation_matrix', 'compute_correlation_matrix_for_variables', 'compute_correlation_matrices_for_methods', 'compute_lagged_correlation', 'compute_rolling_correlation_series', 'compute_rolling_correlations_for_pairs', 'transform_correlation_matrix']
CorrelationMethod = Literal["pearson", "spearman", "kendall"]
CorrelationTransform = Literal["identity", "absolute", "square"]
def compute_correlation_matrix(
df: pd.DataFrame,
*,
method: Literal["pearson", "spearman"] = "pearson",
method: CorrelationMethod = "pearson",
) -> pd.DataFrame:
"""
Calcule la matrice de corrélation entre toutes les colonnes numériques
@@ -36,7 +39,7 @@ def compute_correlation_matrix_for_variables(
df: pd.DataFrame,
variables: Sequence[Variable],
*,
method: Literal["pearson", "spearman"] = "pearson",
method: CorrelationMethod = "pearson",
) -> pd.DataFrame:
"""
Calcule la matrice de corrélation pour un sous-ensemble de variables,
@@ -70,6 +73,46 @@ def compute_correlation_matrix_for_variables(
corr = corr.loc[columns, columns]
return corr
def transform_correlation_matrix(
corr: pd.DataFrame,
*,
transform: CorrelationTransform | Callable[[pd.DataFrame], pd.DataFrame] = "identity",
) -> pd.DataFrame:
"""Applique une transformation générique sur une matrice de corrélation."""
if callable(transform):
return transform(corr)
if transform == "identity":
return corr
if transform == "absolute":
return corr.abs()
if transform == "square":
return corr.pow(2)
raise ValueError(f"Transformation de corrélation inconnue : {transform!r}")
def compute_correlation_matrices_for_methods(
df: pd.DataFrame,
variables: Sequence[Variable],
*,
methods: Sequence[CorrelationMethod],
transform: CorrelationTransform | Callable[[pd.DataFrame], pd.DataFrame] = "identity",
) -> dict[str, pd.DataFrame]:
"""Calcule plusieurs matrices de corrélation en une seule passe."""
if not methods:
raise ValueError("La liste des méthodes de corrélation est vide.")
matrices: dict[str, pd.DataFrame] = {}
for method in methods:
corr = compute_correlation_matrix_for_variables(df, variables, method=method)
matrices[method] = transform_correlation_matrix(corr, transform=transform)
return matrices
def compute_lagged_correlation(
df: pd.DataFrame,
var_x: Variable,