Upload 6 files
Browse files- analysis/Curve.py +44 -34
- analysis/curve_visualizer.py +67 -0
- analysis/dataset_normalization.py +47 -4
analysis/Curve.py
CHANGED
|
@@ -707,7 +707,7 @@ class Curve:
|
|
| 707 |
def _plot_gauge(self, ax, value):
|
| 708 |
"""Disegna un gauge per il pushing score"""
|
| 709 |
# Sfondo gauge
|
| 710 |
-
theta = np.linspace(
|
| 711 |
|
| 712 |
# Arco di sfondo
|
| 713 |
ax.plot(np.cos(theta), np.sin(theta), 'lightgray', linewidth=20, solid_capstyle='round')
|
|
@@ -722,60 +722,66 @@ class Curve:
|
|
| 722 |
else:
|
| 723 |
color = '#e74c3c' # Rosso
|
| 724 |
|
| 725 |
-
# Disegna l'arco fino al valore
|
| 726 |
-
theta_val = np.linspace(
|
| 727 |
ax.plot(np.cos(theta_val), np.sin(theta_val), color, linewidth=20, solid_capstyle='round')
|
| 728 |
|
| 729 |
-
# Testo centrale
|
| 730 |
-
ax.text(0, -0.
|
| 731 |
fontsize=48, fontweight='bold', color=color)
|
| 732 |
-
ax.text(0, -0.
|
| 733 |
fontsize=14, color='gray')
|
| 734 |
|
| 735 |
# Etichette
|
| 736 |
ax.text(-1, 0, '0', ha='center', va='center', fontsize=12, fontweight='bold')
|
| 737 |
ax.text(1, 0, '100', ha='center', va='center', fontsize=12, fontweight='bold')
|
| 738 |
-
|
| 739 |
-
|
|
|
|
|
|
|
|
|
|
| 740 |
|
| 741 |
ax.set_xlim(-1.5, 1.5)
|
| 742 |
-
ax.set_ylim(-0.
|
| 743 |
ax.axis('off')
|
| 744 |
ax.set_aspect('equal')
|
| 745 |
|
| 746 |
-
def plot_all(self, save_path: Optional[str] = None):
|
| 747 |
"""
|
| 748 |
Genera tutti i grafici
|
| 749 |
save_path: se specificato, salva i grafici invece di mostrarli
|
|
|
|
| 750 |
"""
|
| 751 |
if not save_path:
|
| 752 |
plt.close('all')
|
| 753 |
|
| 754 |
print("Generazione grafici in corso...\n")
|
| 755 |
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
|
|
|
|
|
|
| 779 |
fig4 = self.plot_pushing_analysis()
|
| 780 |
if save_path:
|
| 781 |
fig4.savefig(f'{save_path}_pushing.png', dpi=150, bbox_inches='tight')
|
|
@@ -785,7 +791,11 @@ class Curve:
|
|
| 785 |
|
| 786 |
if not save_path:
|
| 787 |
plt.show()
|
| 788 |
-
input
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
plt.close('all')
|
| 790 |
else:
|
| 791 |
plt.close('all')
|
|
|
|
| 707 |
def _plot_gauge(self, ax, value):
|
| 708 |
"""Disegna un gauge per il pushing score"""
|
| 709 |
# Sfondo gauge
|
| 710 |
+
theta = np.linspace(np.pi, 0, 100)
|
| 711 |
|
| 712 |
# Arco di sfondo
|
| 713 |
ax.plot(np.cos(theta), np.sin(theta), 'lightgray', linewidth=20, solid_capstyle='round')
|
|
|
|
| 722 |
else:
|
| 723 |
color = '#e74c3c' # Rosso
|
| 724 |
|
| 725 |
+
# Disegna l'arco fino al valore (da sinistra a destra)
|
| 726 |
+
theta_val = np.linspace(np.pi, np.pi * (1 - value/100), 50)
|
| 727 |
ax.plot(np.cos(theta_val), np.sin(theta_val), color, linewidth=20, solid_capstyle='round')
|
| 728 |
|
| 729 |
+
# Testo centrale (numero più in alto, scritta più in basso)
|
| 730 |
+
ax.text(0, -0.25, f'{value:.1f}', ha='center', va='center',
|
| 731 |
fontsize=48, fontweight='bold', color=color)
|
| 732 |
+
ax.text(0, -0.7, 'PUSHING SCORE', ha='center', va='center',
|
| 733 |
fontsize=14, color='gray')
|
| 734 |
|
| 735 |
# Etichette
|
| 736 |
ax.text(-1, 0, '0', ha='center', va='center', fontsize=12, fontweight='bold')
|
| 737 |
ax.text(1, 0, '100', ha='center', va='center', fontsize=12, fontweight='bold')
|
| 738 |
+
|
| 739 |
+
# Etichette Spinta/Gestione fuori dal cerchio
|
| 740 |
+
# Posizionate leggermente sopra e esterne
|
| 741 |
+
ax.text(-1.5, 0.6, 'GESTIONE', ha='center', va='center', fontsize=10, color='#2ecc71', fontweight='bold')
|
| 742 |
+
ax.text(1.5, 0.6, 'SPINTA', ha='center', va='center', fontsize=10, color='#e74c3c', fontweight='bold')
|
| 743 |
|
| 744 |
ax.set_xlim(-1.5, 1.5)
|
| 745 |
+
ax.set_ylim(-0.8, 1.3)
|
| 746 |
ax.axis('off')
|
| 747 |
ax.set_aspect('equal')
|
| 748 |
|
| 749 |
+
def plot_all(self, save_path: Optional[str] = None, show_only_score=False):
|
| 750 |
"""
|
| 751 |
Genera tutti i grafici
|
| 752 |
save_path: se specificato, salva i grafici invece di mostrarli
|
| 753 |
+
show_only_score: se True, genera solo il grafico del pushing score
|
| 754 |
"""
|
| 755 |
if not save_path:
|
| 756 |
plt.close('all')
|
| 757 |
|
| 758 |
print("Generazione grafici in corso...\n")
|
| 759 |
|
| 760 |
+
if not show_only_score:
|
| 761 |
+
# Grafico 1: G Forces
|
| 762 |
+
print("1/4 - Analisi Forze G...")
|
| 763 |
+
fig1 = self.plot_g_forces_map()
|
| 764 |
+
if save_path:
|
| 765 |
+
fig1.savefig(f'{save_path}_g_forces.png', dpi=150, bbox_inches='tight')
|
| 766 |
+
print(f" ✓ Salvato: {save_path}_g_forces.png")
|
| 767 |
+
|
| 768 |
+
# Grafico 2: Driver Inputs
|
| 769 |
+
print("2/4 - Input del Pilota...")
|
| 770 |
+
fig2 = self.plot_driver_inputs()
|
| 771 |
+
if save_path:
|
| 772 |
+
fig2.savefig(f'{save_path}_inputs.png', dpi=150, bbox_inches='tight')
|
| 773 |
+
print(f" ✓ Salvato: {save_path}_inputs.png")
|
| 774 |
+
|
| 775 |
+
# Grafico 3: Tire Management
|
| 776 |
+
print("3/4 - Gestione Gomme...")
|
| 777 |
+
fig3 = self.plot_tire_management()
|
| 778 |
+
if save_path:
|
| 779 |
+
fig3.savefig(f'{save_path}_tires.png', dpi=150, bbox_inches='tight')
|
| 780 |
+
print(f" ✓ Salvato: {save_path}_tires.png")
|
| 781 |
+
|
| 782 |
+
# Grafico 4: Pushing Analysis (Sempre generato o solo questo se flag attivo)
|
| 783 |
+
step_msg = "4/4" if not show_only_score else "1/1"
|
| 784 |
+
print(f"{step_msg} - Analisi Spinta...")
|
| 785 |
fig4 = self.plot_pushing_analysis()
|
| 786 |
if save_path:
|
| 787 |
fig4.savefig(f'{save_path}_pushing.png', dpi=150, bbox_inches='tight')
|
|
|
|
| 791 |
|
| 792 |
if not save_path:
|
| 793 |
plt.show()
|
| 794 |
+
# Se stiamo mostrando solo lo score, magari non serve l'input bloccante se è un loop veloce,
|
| 795 |
+
# ma per sicurezza lo lasciamo o lo condizioniamo.
|
| 796 |
+
# L'utente ha chiesto "solo per stampare lo score", assumiamo voglia vederlo.
|
| 797 |
+
if not show_only_score:
|
| 798 |
+
input("\n[INVIO per continuare...]")
|
| 799 |
plt.close('all')
|
| 800 |
else:
|
| 801 |
plt.close('all')
|
analysis/curve_visualizer.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.analysis.CurveDetector import CurveDetector
|
| 2 |
+
from src.models.dataset_loader import download_raw_telemetry_from_hf
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# =============================================================================
|
| 7 |
+
# CONFIGURATION
|
| 8 |
+
# =============================================================================
|
| 9 |
+
@dataclass
|
| 10 |
+
class VisualizerConfig:
|
| 11 |
+
"""Configuration for curve visualization."""
|
| 12 |
+
|
| 13 |
+
# ----- Paths -----
|
| 14 |
+
telemetry_path: str = "data/2025-main/Italian Grand Prix/Qualifying/LEC/1_tel.json"
|
| 15 |
+
corners_path: str = "data/2025-main/Italian Grand Prix/Race/corners.json"
|
| 16 |
+
|
| 17 |
+
# ----- Hugging Face -----
|
| 18 |
+
download_from_hf: bool = True # True = download raw telemetry from HF
|
| 19 |
+
raw_data_subfolder: str = "2025-main" # Subfolder to download (2024-main or 2025-main)
|
| 20 |
+
|
| 21 |
+
# ----- Visualization -----
|
| 22 |
+
show_track: bool = False # Show track overview with curves
|
| 23 |
+
show_score: bool = False # Show pushing score
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
CONFIG = VisualizerConfig()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# =============================================================================
|
| 30 |
+
# MAIN
|
| 31 |
+
# =============================================================================
|
| 32 |
+
def main(config: VisualizerConfig = CONFIG):
|
| 33 |
+
"""Funzione main per visualizzazione curve."""
|
| 34 |
+
|
| 35 |
+
print("=" * 60)
|
| 36 |
+
print("Curve Visualizer")
|
| 37 |
+
print("=" * 60)
|
| 38 |
+
|
| 39 |
+
# Download raw data from HF if configured
|
| 40 |
+
if config.download_from_hf:
|
| 41 |
+
print("\n[1/3] Downloading raw telemetry from Hugging Face...")
|
| 42 |
+
download_raw_telemetry_from_hf(subfolder=config.raw_data_subfolder)
|
| 43 |
+
else:
|
| 44 |
+
print("\n[1/3] Using local telemetry data...")
|
| 45 |
+
|
| 46 |
+
# Detect curves
|
| 47 |
+
print("\n[2/3] Detecting curves...")
|
| 48 |
+
curve_detector = CurveDetector(config.telemetry_path, config.corners_path)
|
| 49 |
+
curves = curve_detector.calcolo_curve()
|
| 50 |
+
print(f"Detected {len(curves)} curves")
|
| 51 |
+
|
| 52 |
+
# Visualize
|
| 53 |
+
print("\n[3/3] Visualizing...")
|
| 54 |
+
curve_detector.grafico(curves, config.show_track)
|
| 55 |
+
curve_detector.plot_curve_trajectories(curves)
|
| 56 |
+
|
| 57 |
+
if config.show_score:
|
| 58 |
+
for curve in curves:
|
| 59 |
+
curve.plot_all(show_only_score=config.show_score)
|
| 60 |
+
|
| 61 |
+
print("\n" + "=" * 60)
|
| 62 |
+
print("Done!")
|
| 63 |
+
print("=" * 60)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
main()
|
analysis/dataset_normalization.py
CHANGED
|
@@ -31,6 +31,37 @@ PADDING_VALUE = DEFAULT_CONFIG.padding_value
|
|
| 31 |
COMPOUND_CATEGORIES = list(DEFAULT_CONFIG.compound_categories)
|
| 32 |
MAX_SAMPLES_PER_CURVE = DEFAULT_CONFIG.max_samples_per_curve
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# =============================================================================
|
| 36 |
# SINGLE CURVE NORMALIZATION (for inference)
|
|
@@ -93,8 +124,11 @@ def curve_to_raw_features(
|
|
| 93 |
padding = config.padding_value
|
| 94 |
categories = config.compound_categories
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
speed = pad_or_truncate(curve.speed, max_samples, padding)
|
| 99 |
rpm = pad_or_truncate(curve.rpm, max_samples, padding)
|
| 100 |
throttle = pad_or_truncate(curve.throttle, max_samples, padding)
|
|
@@ -484,7 +518,7 @@ def load_normalized_data(
|
|
| 484 |
if __name__ == "__main__":
|
| 485 |
# Configuration
|
| 486 |
config = NormalizationConfig(
|
| 487 |
-
input_csv_path="data/dataset/
|
| 488 |
output_dir="data/dataset",
|
| 489 |
output_filename="normalized_dataset.npz"
|
| 490 |
)
|
|
@@ -498,10 +532,19 @@ if __name__ == "__main__":
|
|
| 498 |
decimal="."
|
| 499 |
)
|
| 500 |
|
| 501 |
-
# Remove unnecessary columns
|
| 502 |
df = df.drop(df.columns[:5], axis=1)
|
|
|
|
| 503 |
df = df.drop(df.columns[2], axis=1)
|
| 504 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
# Remove X, Y, Z columns
|
| 506 |
df = df.drop(df.columns[352:502], axis=1)
|
| 507 |
|
|
|
|
| 31 |
COMPOUND_CATEGORIES = list(DEFAULT_CONFIG.compound_categories)
|
| 32 |
MAX_SAMPLES_PER_CURVE = DEFAULT_CONFIG.max_samples_per_curve
|
| 33 |
|
| 34 |
+
# Massimo TireLife osservato per ogni compound (calcolato dal dataset 2024-2025)
|
| 35 |
+
# Usato per normalizzare TireLife relativamente al compound:
|
| 36 |
+
# TireLifeNorm = TireLife / max_per_compound → [0, 1]
|
| 37 |
+
TIRE_LIFE_MAX_PER_COMPOUND = {
|
| 38 |
+
'SOFT': 30,
|
| 39 |
+
'MEDIUM': 40,
|
| 40 |
+
'HARD': 50,
|
| 41 |
+
'INTERMEDIATE': 35,
|
| 42 |
+
'WET': 20,
|
| 43 |
+
}
|
| 44 |
+
# Fallback per compound sconosciuti
|
| 45 |
+
TIRE_LIFE_MAX_DEFAULT = 50
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def normalize_tire_life(life: float, compound: str) -> float:
|
| 49 |
+
"""
|
| 50 |
+
Normalizza TireLife relativamente al max del compound.
|
| 51 |
+
|
| 52 |
+
Compound diversi hanno durate diverse: una SOFT con TireLife=10
|
| 53 |
+
è molto più consumata di una HARD con TireLife=10.
|
| 54 |
+
|
| 55 |
+
Args:
|
| 56 |
+
life: Valore grezzo di TireLife (laps)
|
| 57 |
+
compound: Nome del compound (SOFT, MEDIUM, HARD, ...)
|
| 58 |
+
|
| 59 |
+
Returns:
|
| 60 |
+
TireLife normalizzato in [0, 1] (0=fresca, 1=fine vita)
|
| 61 |
+
"""
|
| 62 |
+
max_life = TIRE_LIFE_MAX_PER_COMPOUND.get(compound, TIRE_LIFE_MAX_DEFAULT)
|
| 63 |
+
return min(life / max_life, 1.0)
|
| 64 |
+
|
| 65 |
|
| 66 |
# =============================================================================
|
| 67 |
# SINGLE CURVE NORMALIZATION (for inference)
|
|
|
|
| 124 |
padding = config.padding_value
|
| 125 |
categories = config.compound_categories
|
| 126 |
|
| 127 |
+
# Normalize TireLife relative to compound
|
| 128 |
+
raw_life = curve.life if hasattr(curve, 'life') else 0
|
| 129 |
+
compound = curve.compound if hasattr(curve, 'compound') else 'UNKNOWN'
|
| 130 |
+
life = normalize_tire_life(raw_life, compound)
|
| 131 |
+
|
| 132 |
speed = pad_or_truncate(curve.speed, max_samples, padding)
|
| 133 |
rpm = pad_or_truncate(curve.rpm, max_samples, padding)
|
| 134 |
throttle = pad_or_truncate(curve.throttle, max_samples, padding)
|
|
|
|
| 518 |
if __name__ == "__main__":
|
| 519 |
# Configuration
|
| 520 |
config = NormalizationConfig(
|
| 521 |
+
input_csv_path="data/dataset/dataset_curves_2024_2025.csv",
|
| 522 |
output_dir="data/dataset",
|
| 523 |
output_filename="normalized_dataset.npz"
|
| 524 |
)
|
|
|
|
| 532 |
decimal="."
|
| 533 |
)
|
| 534 |
|
| 535 |
+
# Remove unnecessary columns (GrandPrix, Session, Driver, Lap, CornerID)
|
| 536 |
df = df.drop(df.columns[:5], axis=1)
|
| 537 |
+
# Remove Stint (index 2 after dropping first 5)
|
| 538 |
df = df.drop(df.columns[2], axis=1)
|
| 539 |
|
| 540 |
+
# Normalize TireLife relative to compound BEFORE Z-score
|
| 541 |
+
# Questo rende il valore comparabile tra compound diversi
|
| 542 |
+
print("Normalizing TireLife relative to compound...")
|
| 543 |
+
df['TireLife'] = df.apply(
|
| 544 |
+
lambda row: normalize_tire_life(row['TireLife'], row['Compound']), axis=1
|
| 545 |
+
)
|
| 546 |
+
print(f" TireLife range after normalization: [{df['TireLife'].min():.4f}, {df['TireLife'].max():.4f}]")
|
| 547 |
+
|
| 548 |
# Remove X, Y, Z columns
|
| 549 |
df = df.drop(df.columns[352:502], axis=1)
|
| 550 |
|