mirror of
https://github.com/sstent/Garmin_Analyser.git
synced 2026-01-25 08:35:12 +00:00
removing old endpoints etc
This commit is contained in:
@@ -12,461 +12,652 @@ import plotly.express as px
|
||||
from plotly.subplots import make_subplots
|
||||
|
||||
from models.workout import WorkoutData
|
||||
from models.zones import ZoneCalculator
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ChartGenerator:
|
||||
"""Generate various charts and visualizations for workout data."""
|
||||
|
||||
|
||||
def __init__(self, output_dir: Path = None):
|
||||
"""Initialize chart generator.
|
||||
|
||||
|
||||
Args:
|
||||
output_dir: Directory to save charts
|
||||
"""
|
||||
self.output_dir = output_dir or Path('charts')
|
||||
self.output_dir.mkdir(exist_ok=True)
|
||||
|
||||
self.zone_calculator = ZoneCalculator()
|
||||
|
||||
# Set style
|
||||
plt.style.use('seaborn-v0_8')
|
||||
sns.set_palette("husl")
|
||||
|
||||
|
||||
def _get_avg_max_values(self, analysis: Dict[str, Any], data_type: str, workout: WorkoutData) -> Tuple[float, float]:
|
||||
"""Get avg and max values from analysis dict or compute from workout data.
|
||||
|
||||
Args:
|
||||
analysis: Analysis results from WorkoutAnalyzer
|
||||
data_type: 'power', 'hr', or 'speed'
|
||||
workout: WorkoutData object
|
||||
|
||||
Returns:
|
||||
Tuple of (avg_value, max_value)
|
||||
"""
|
||||
if analysis and 'summary' in analysis:
|
||||
summary = analysis['summary']
|
||||
if data_type == 'power':
|
||||
avg_key, max_key = 'avg_power', 'max_power'
|
||||
elif data_type == 'hr':
|
||||
avg_key, max_key = 'avg_hr', 'max_hr'
|
||||
elif data_type == 'speed':
|
||||
avg_key, max_key = 'avg_speed_kmh', 'max_speed_kmh'
|
||||
else:
|
||||
raise ValueError(f"Unsupported data_type: {data_type}")
|
||||
|
||||
avg_val = summary.get(avg_key)
|
||||
max_val = summary.get(max_key)
|
||||
|
||||
if avg_val is not None and max_val is not None:
|
||||
return avg_val, max_val
|
||||
|
||||
# Fallback: compute from workout data
|
||||
if data_type == 'power' and workout.power and workout.power.power_values:
|
||||
return np.mean(workout.power.power_values), np.max(workout.power.power_values)
|
||||
elif data_type == 'hr' and workout.heart_rate and workout.heart_rate.heart_rate_values:
|
||||
return np.mean(workout.heart_rate.heart_rate_values), np.max(workout.heart_rate.heart_rate_values)
|
||||
elif data_type == 'speed' and workout.speed and workout.speed.speed_values:
|
||||
return np.mean(workout.speed.speed_values), np.max(workout.speed.speed_values)
|
||||
|
||||
# Default fallback
|
||||
return 0, 0
|
||||
|
||||
def _get_avg_max_labels(self, data_type: str, analysis: Dict[str, Any], workout: WorkoutData) -> Tuple[str, str]:
|
||||
"""Get formatted average and maximum labels for chart annotations.
|
||||
|
||||
Args:
|
||||
data_type: 'power', 'hr', or 'speed'
|
||||
analysis: Analysis results from WorkoutAnalyzer
|
||||
workout: WorkoutData object
|
||||
|
||||
Returns:
|
||||
Tuple of (avg_label, max_label)
|
||||
"""
|
||||
avg_val, max_val = self._get_avg_max_values(analysis, data_type, workout)
|
||||
|
||||
if data_type == 'power':
|
||||
avg_label = f'Avg: {avg_val:.0f}W'
|
||||
max_label = f'Max: {max_val:.0f}W'
|
||||
elif data_type == 'hr':
|
||||
avg_label = f'Avg: {avg_val:.0f} bpm'
|
||||
max_label = f'Max: {max_val:.0f} bpm'
|
||||
elif data_type == 'speed':
|
||||
avg_label = f'Avg: {avg_val:.1f} km/h'
|
||||
max_label = f'Max: {max_val:.1f} km/h'
|
||||
else:
|
||||
avg_label = f'Avg: {avg_val:.1f}'
|
||||
max_label = f'Max: {max_val:.1f}'
|
||||
|
||||
return avg_label, max_label
|
||||
|
||||
def generate_workout_charts(self, workout: WorkoutData, analysis: Dict[str, Any]) -> Dict[str, str]:
|
||||
"""Generate all workout charts.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
analysis: Analysis results from WorkoutAnalyzer
|
||||
|
||||
|
||||
Returns:
|
||||
Dictionary mapping chart names to file paths
|
||||
"""
|
||||
charts = {}
|
||||
|
||||
|
||||
# Time series charts
|
||||
charts['power_time_series'] = self._create_power_time_series(workout)
|
||||
charts['heart_rate_time_series'] = self._create_heart_rate_time_series(workout)
|
||||
charts['speed_time_series'] = self._create_speed_time_series(workout)
|
||||
charts['power_time_series'] = self._create_power_time_series(workout, analysis, elevation_overlay=True, zone_shading=True)
|
||||
charts['heart_rate_time_series'] = self._create_heart_rate_time_series(workout, analysis, elevation_overlay=True)
|
||||
charts['speed_time_series'] = self._create_speed_time_series(workout, analysis, elevation_overlay=True)
|
||||
charts['elevation_time_series'] = self._create_elevation_time_series(workout)
|
||||
|
||||
|
||||
# Distribution charts
|
||||
charts['power_distribution'] = self._create_power_distribution(workout, analysis)
|
||||
charts['heart_rate_distribution'] = self._create_heart_rate_distribution(workout, analysis)
|
||||
charts['speed_distribution'] = self._create_speed_distribution(workout, analysis)
|
||||
|
||||
|
||||
# Zone charts
|
||||
charts['power_zones'] = self._create_power_zones_chart(analysis)
|
||||
charts['heart_rate_zones'] = self._create_heart_rate_zones_chart(analysis)
|
||||
|
||||
|
||||
# Correlation charts
|
||||
charts['power_vs_heart_rate'] = self._create_power_vs_heart_rate(workout)
|
||||
charts['power_vs_speed'] = self._create_power_vs_speed(workout)
|
||||
|
||||
|
||||
# Summary dashboard
|
||||
charts['workout_dashboard'] = self._create_workout_dashboard(workout, analysis)
|
||||
|
||||
|
||||
return charts
|
||||
|
||||
def _create_power_time_series(self, workout: WorkoutData) -> str:
|
||||
|
||||
def _create_power_time_series(self, workout: WorkoutData, analysis: Dict[str, Any] = None, elevation_overlay: bool = True, zone_shading: bool = True) -> str:
|
||||
"""Create power vs time chart.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
|
||||
analysis: Analysis results from WorkoutAnalyzer
|
||||
elevation_overlay: Whether to add an elevation overlay
|
||||
zone_shading: Whether to add power zone shading
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if not workout.power or not workout.power.power_values:
|
||||
return None
|
||||
|
||||
fig, ax = plt.subplots(figsize=(12, 6))
|
||||
|
||||
|
||||
fig, ax1 = plt.subplots(figsize=(12, 6))
|
||||
|
||||
power_values = workout.power.power_values
|
||||
time_minutes = np.arange(len(power_values)) / 60
|
||||
|
||||
ax.plot(time_minutes, power_values, linewidth=0.5, alpha=0.8)
|
||||
ax.axhline(y=workout.power.avg_power, color='r', linestyle='--',
|
||||
label=f'Avg: {workout.power.avg_power:.0f}W')
|
||||
ax.axhline(y=workout.power.max_power, color='g', linestyle='--',
|
||||
label=f'Max: {workout.power.max_power:.0f}W')
|
||||
|
||||
ax.set_xlabel('Time (minutes)')
|
||||
ax.set_ylabel('Power (W)')
|
||||
ax.set_title('Power Over Time')
|
||||
ax.legend()
|
||||
ax.grid(True, alpha=0.3)
|
||||
|
||||
|
||||
# Plot power
|
||||
ax1.plot(time_minutes, power_values, linewidth=0.5, alpha=0.8, color='blue')
|
||||
ax1.set_xlabel('Time (minutes)')
|
||||
ax1.set_ylabel('Power (W)', color='blue')
|
||||
ax1.tick_params(axis='y', labelcolor='blue')
|
||||
|
||||
# Add avg/max annotations from analysis or fallback
|
||||
avg_power_label, max_power_label = self._get_avg_max_labels('power', analysis, workout)
|
||||
ax1.axhline(y=self._get_avg_max_values(analysis, 'power', workout)[0], color='red', linestyle='--',
|
||||
label=avg_power_label)
|
||||
ax1.axhline(y=self._get_avg_max_values(analysis, 'power', workout)[1], color='green', linestyle='--',
|
||||
label=max_power_label)
|
||||
|
||||
# Add power zone shading
|
||||
if zone_shading and analysis and 'power_analysis' in analysis:
|
||||
power_zones = self.zone_calculator.get_power_zones()
|
||||
# Try to get FTP from analysis, otherwise use a default or the zone calculator's default
|
||||
ftp = analysis.get('power_analysis', {}).get('ftp', 250) # Fallback to 250W if not in analysis
|
||||
|
||||
# Recalculate zones based on FTP percentage
|
||||
power_zones_percent = {
|
||||
'Recovery': {'min': 0, 'max': 0.5}, # <50% FTP
|
||||
'Endurance': {'min': 0.5, 'max': 0.75}, # 50-75% FTP
|
||||
'Tempo': {'min': 0.75, 'max': 0.9}, # 75-90% FTP
|
||||
'Threshold': {'min': 0.9, 'max': 1.05}, # 90-105% FTP
|
||||
'VO2 Max': {'min': 1.05, 'max': 1.2}, # 105-120% FTP
|
||||
'Anaerobic': {'min': 1.2, 'max': 10} # >120% FTP (arbitrary max for shading)
|
||||
}
|
||||
|
||||
for zone_name, zone_def_percent in power_zones_percent.items():
|
||||
min_power = ftp * zone_def_percent['min']
|
||||
max_power = ftp * zone_def_percent['max']
|
||||
|
||||
# Find the corresponding ZoneDefinition to get the color
|
||||
zone_color = next((z.color for z_name, z in power_zones.items() if z_name == zone_name), 'grey')
|
||||
|
||||
ax1.axhspan(min_power, max_power,
|
||||
alpha=0.1, color=zone_color,
|
||||
label=f'{zone_name} ({min_power:.0f}-{max_power:.0f}W)')
|
||||
|
||||
# Add elevation overlay if available
|
||||
if elevation_overlay and workout.elevation and workout.elevation.elevation_values:
|
||||
# Create twin axis for elevation
|
||||
ax2 = ax1.twinx()
|
||||
elevation_values = workout.elevation.elevation_values
|
||||
|
||||
# Apply light smoothing to elevation for visual stability
|
||||
# Using a simple rolling mean, NaN-safe
|
||||
elevation_smoothed = pd.Series(elevation_values).rolling(window=5, min_periods=1, center=True).mean().values
|
||||
|
||||
# Align lengths (assume same sampling rate)
|
||||
min_len = min(len(power_values), len(elevation_smoothed))
|
||||
elevation_aligned = elevation_smoothed[:min_len]
|
||||
time_aligned = time_minutes[:min_len]
|
||||
|
||||
ax2.fill_between(time_aligned, elevation_aligned, alpha=0.2, color='brown', label='Elevation')
|
||||
ax2.set_ylabel('Elevation (m)', color='brown')
|
||||
ax2.tick_params(axis='y', labelcolor='brown')
|
||||
|
||||
# Combine legends
|
||||
lines1, labels1 = ax1.get_legend_handles_labels()
|
||||
lines2, labels2 = ax2.get_legend_handles_labels()
|
||||
ax1.legend(lines1 + lines2, labels1 + labels2, loc='upper left')
|
||||
else:
|
||||
ax1.legend()
|
||||
|
||||
ax1.set_title('Power Over Time')
|
||||
ax1.grid(True, alpha=0.3)
|
||||
|
||||
filepath = self.output_dir / 'power_time_series.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
def _create_heart_rate_time_series(self, workout: WorkoutData) -> str:
|
||||
|
||||
def _create_heart_rate_time_series(self, workout: WorkoutData, analysis: Dict[str, Any] = None, elevation_overlay: bool = True) -> str:
|
||||
"""Create heart rate vs time chart.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
|
||||
analysis: Analysis results from WorkoutAnalyzer
|
||||
elevation_overlay: Whether to add an elevation overlay
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if not workout.heart_rate or not workout.heart_rate.heart_rate_values:
|
||||
return None
|
||||
|
||||
fig, ax = plt.subplots(figsize=(12, 6))
|
||||
|
||||
|
||||
fig, ax1 = plt.subplots(figsize=(12, 6))
|
||||
|
||||
hr_values = workout.heart_rate.heart_rate_values
|
||||
time_minutes = np.arange(len(hr_values)) / 60
|
||||
|
||||
ax.plot(time_minutes, hr_values, linewidth=0.5, alpha=0.8, color='red')
|
||||
ax.axhline(y=workout.heart_rate.avg_hr, color='darkred', linestyle='--',
|
||||
label=f'Avg: {workout.heart_rate.avg_hr:.0f} bpm')
|
||||
ax.axhline(y=workout.heart_rate.max_hr, color='darkgreen', linestyle='--',
|
||||
label=f'Max: {workout.heart_rate.max_hr:.0f} bpm')
|
||||
|
||||
ax.set_xlabel('Time (minutes)')
|
||||
ax.set_ylabel('Heart Rate (bpm)')
|
||||
ax.set_title('Heart Rate Over Time')
|
||||
ax.legend()
|
||||
ax.grid(True, alpha=0.3)
|
||||
|
||||
|
||||
# Plot heart rate
|
||||
ax1.plot(time_minutes, hr_values, linewidth=0.5, alpha=0.8, color='red')
|
||||
ax1.set_xlabel('Time (minutes)')
|
||||
ax1.set_ylabel('Heart Rate (bpm)', color='red')
|
||||
ax1.tick_params(axis='y', labelcolor='red')
|
||||
|
||||
# Add avg/max annotations from analysis or fallback
|
||||
avg_hr_label, max_hr_label = self._get_avg_max_labels('hr', analysis, workout)
|
||||
ax1.axhline(y=self._get_avg_max_values(analysis, 'hr', workout)[0], color='darkred', linestyle='--',
|
||||
label=avg_hr_label)
|
||||
ax1.axhline(y=self._get_avg_max_values(analysis, 'hr', workout)[1], color='darkgreen', linestyle='--',
|
||||
label=max_hr_label)
|
||||
|
||||
# Add elevation overlay if available
|
||||
if elevation_overlay and workout.elevation and workout.elevation.elevation_values:
|
||||
# Create twin axis for elevation
|
||||
ax2 = ax1.twinx()
|
||||
elevation_values = workout.elevation.elevation_values
|
||||
|
||||
# Apply light smoothing to elevation for visual stability
|
||||
elevation_smoothed = pd.Series(elevation_values).rolling(window=5, min_periods=1, center=True).mean().values
|
||||
|
||||
# Align lengths (assume same sampling rate)
|
||||
min_len = min(len(hr_values), len(elevation_smoothed))
|
||||
elevation_aligned = elevation_smoothed[:min_len]
|
||||
time_aligned = time_minutes[:min_len]
|
||||
|
||||
ax2.fill_between(time_aligned, elevation_aligned, alpha=0.2, color='brown', label='Elevation')
|
||||
ax2.set_ylabel('Elevation (m)', color='brown')
|
||||
ax2.tick_params(axis='y', labelcolor='brown')
|
||||
|
||||
# Combine legends
|
||||
lines1, labels1 = ax1.get_legend_handles_labels()
|
||||
lines2, labels2 = ax2.get_legend_handles_labels()
|
||||
ax1.legend(lines1 + lines2, labels1 + labels2, loc='upper left')
|
||||
else:
|
||||
ax1.legend()
|
||||
|
||||
ax1.set_title('Heart Rate Over Time')
|
||||
ax1.grid(True, alpha=0.3)
|
||||
|
||||
filepath = self.output_dir / 'heart_rate_time_series.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
def _create_speed_time_series(self, workout: WorkoutData) -> str:
|
||||
|
||||
def _create_speed_time_series(self, workout: WorkoutData, analysis: Dict[str, Any] = None, elevation_overlay: bool = True) -> str:
|
||||
"""Create speed vs time chart.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
|
||||
analysis: Analysis results from WorkoutAnalyzer
|
||||
elevation_overlay: Whether to add an elevation overlay
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if not workout.speed or not workout.speed.speed_values:
|
||||
return None
|
||||
|
||||
fig, ax = plt.subplots(figsize=(12, 6))
|
||||
|
||||
|
||||
fig, ax1 = plt.subplots(figsize=(12, 6))
|
||||
|
||||
speed_values = workout.speed.speed_values
|
||||
time_minutes = np.arange(len(speed_values)) / 60
|
||||
|
||||
ax.plot(time_minutes, speed_values, linewidth=0.5, alpha=0.8, color='blue')
|
||||
ax.axhline(y=workout.speed.avg_speed, color='darkblue', linestyle='--',
|
||||
label=f'Avg: {workout.speed.avg_speed:.1f} km/h')
|
||||
ax.axhline(y=workout.speed.max_speed, color='darkgreen', linestyle='--',
|
||||
label=f'Max: {workout.speed.max_speed:.1f} km/h')
|
||||
|
||||
ax.set_xlabel('Time (minutes)')
|
||||
ax.set_ylabel('Speed (km/h)')
|
||||
ax.set_title('Speed Over Time')
|
||||
ax.legend()
|
||||
ax.grid(True, alpha=0.3)
|
||||
|
||||
|
||||
# Plot speed
|
||||
ax1.plot(time_minutes, speed_values, linewidth=0.5, alpha=0.8, color='blue')
|
||||
ax1.set_xlabel('Time (minutes)')
|
||||
ax1.set_ylabel('Speed (km/h)', color='blue')
|
||||
ax1.tick_params(axis='y', labelcolor='blue')
|
||||
|
||||
# Add avg/max annotations from analysis or fallback
|
||||
avg_speed_label, max_speed_label = self._get_avg_max_labels('speed', analysis, workout)
|
||||
ax1.axhline(y=self._get_avg_max_values(analysis, 'speed', workout)[0], color='darkblue', linestyle='--',
|
||||
label=avg_speed_label)
|
||||
ax1.axhline(y=self._get_avg_max_values(analysis, 'speed', workout)[1], color='darkgreen', linestyle='--',
|
||||
label=max_speed_label)
|
||||
|
||||
# Add elevation overlay if available
|
||||
if elevation_overlay and workout.elevation and workout.elevation.elevation_values:
|
||||
# Create twin axis for elevation
|
||||
ax2 = ax1.twinx()
|
||||
elevation_values = workout.elevation.elevation_values
|
||||
|
||||
# Apply light smoothing to elevation for visual stability
|
||||
elevation_smoothed = pd.Series(elevation_values).rolling(window=5, min_periods=1, center=True).mean().values
|
||||
|
||||
# Align lengths (assume same sampling rate)
|
||||
min_len = min(len(speed_values), len(elevation_smoothed))
|
||||
elevation_aligned = elevation_smoothed[:min_len]
|
||||
time_aligned = time_minutes[:min_len]
|
||||
|
||||
ax2.fill_between(time_aligned, elevation_aligned, alpha=0.2, color='brown', label='Elevation')
|
||||
ax2.set_ylabel('Elevation (m)', color='brown')
|
||||
ax2.tick_params(axis='y', labelcolor='brown')
|
||||
|
||||
# Combine legends
|
||||
lines1, labels1 = ax1.get_legend_handles_labels()
|
||||
lines2, labels2 = ax2.get_legend_handles_labels()
|
||||
ax1.legend(lines1 + lines2, labels1 + labels2, loc='upper left')
|
||||
else:
|
||||
ax1.legend()
|
||||
|
||||
ax1.set_title('Speed Over Time')
|
||||
ax1.grid(True, alpha=0.3)
|
||||
|
||||
filepath = self.output_dir / 'speed_time_series.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def _create_elevation_time_series(self, workout: WorkoutData) -> str:
|
||||
"""Create elevation vs time chart.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if not workout.elevation or not workout.elevation.elevation_values:
|
||||
return None
|
||||
|
||||
|
||||
fig, ax = plt.subplots(figsize=(12, 6))
|
||||
|
||||
|
||||
elevation_values = workout.elevation.elevation_values
|
||||
time_minutes = np.arange(len(elevation_values)) / 60
|
||||
|
||||
|
||||
ax.plot(time_minutes, elevation_values, linewidth=1, alpha=0.8, color='brown')
|
||||
ax.fill_between(time_minutes, elevation_values, alpha=0.3, color='brown')
|
||||
|
||||
|
||||
ax.set_xlabel('Time (minutes)')
|
||||
ax.set_ylabel('Elevation (m)')
|
||||
ax.set_title('Elevation Profile')
|
||||
ax.grid(True, alpha=0.3)
|
||||
|
||||
|
||||
filepath = self.output_dir / 'elevation_time_series.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def _create_power_distribution(self, workout: WorkoutData, analysis: Dict[str, Any]) -> str:
|
||||
"""Create power distribution histogram.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
analysis: Analysis results
|
||||
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if not workout.power or not workout.power.power_values:
|
||||
return None
|
||||
|
||||
|
||||
fig, ax = plt.subplots(figsize=(10, 6))
|
||||
|
||||
|
||||
power_values = workout.power.power_values
|
||||
|
||||
|
||||
ax.hist(power_values, bins=50, alpha=0.7, color='orange', edgecolor='black')
|
||||
ax.axvline(x=workout.power.avg_power, color='red', linestyle='--',
|
||||
ax.axvline(x=workout.power.avg_power, color='red', linestyle='--',
|
||||
label=f'Avg: {workout.power.avg_power:.0f}W')
|
||||
|
||||
|
||||
ax.set_xlabel('Power (W)')
|
||||
ax.set_ylabel('Frequency')
|
||||
ax.set_title('Power Distribution')
|
||||
ax.legend()
|
||||
ax.grid(True, alpha=0.3)
|
||||
|
||||
|
||||
filepath = self.output_dir / 'power_distribution.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def _create_heart_rate_distribution(self, workout: WorkoutData, analysis: Dict[str, Any]) -> str:
|
||||
"""Create heart rate distribution histogram.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
analysis: Analysis results
|
||||
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if not workout.heart_rate or not workout.heart_rate.heart_rate_values:
|
||||
return None
|
||||
|
||||
|
||||
fig, ax = plt.subplots(figsize=(10, 6))
|
||||
|
||||
|
||||
hr_values = workout.heart_rate.heart_rate_values
|
||||
|
||||
|
||||
ax.hist(hr_values, bins=30, alpha=0.7, color='red', edgecolor='black')
|
||||
ax.axvline(x=workout.heart_rate.avg_hr, color='darkred', linestyle='--',
|
||||
ax.axvline(x=workout.heart_rate.avg_hr, color='darkred', linestyle='--',
|
||||
label=f'Avg: {workout.heart_rate.avg_hr:.0f} bpm')
|
||||
|
||||
|
||||
ax.set_xlabel('Heart Rate (bpm)')
|
||||
ax.set_ylabel('Frequency')
|
||||
ax.set_title('Heart Rate Distribution')
|
||||
ax.legend()
|
||||
ax.grid(True, alpha=0.3)
|
||||
|
||||
|
||||
filepath = self.output_dir / 'heart_rate_distribution.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def _create_speed_distribution(self, workout: WorkoutData, analysis: Dict[str, Any]) -> str:
|
||||
"""Create speed distribution histogram.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
analysis: Analysis results
|
||||
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if not workout.speed or not workout.speed.speed_values:
|
||||
return None
|
||||
|
||||
|
||||
fig, ax = plt.subplots(figsize=(10, 6))
|
||||
|
||||
|
||||
speed_values = workout.speed.speed_values
|
||||
|
||||
|
||||
ax.hist(speed_values, bins=30, alpha=0.7, color='blue', edgecolor='black')
|
||||
ax.axvline(x=workout.speed.avg_speed, color='darkblue', linestyle='--',
|
||||
ax.axvline(x=workout.speed.avg_speed, color='darkblue', linestyle='--',
|
||||
label=f'Avg: {workout.speed.avg_speed:.1f} km/h')
|
||||
|
||||
|
||||
ax.set_xlabel('Speed (km/h)')
|
||||
ax.set_ylabel('Frequency')
|
||||
ax.set_title('Speed Distribution')
|
||||
ax.legend()
|
||||
ax.grid(True, alpha=0.3)
|
||||
|
||||
|
||||
filepath = self.output_dir / 'speed_distribution.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def _create_power_zones_chart(self, analysis: Dict[str, Any]) -> str:
|
||||
"""Create power zones pie chart.
|
||||
|
||||
|
||||
Args:
|
||||
analysis: Analysis results
|
||||
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if 'power_analysis' not in analysis or 'power_zones' not in analysis['power_analysis']:
|
||||
return None
|
||||
|
||||
|
||||
power_zones = analysis['power_analysis']['power_zones']
|
||||
|
||||
|
||||
fig, ax = plt.subplots(figsize=(8, 8))
|
||||
|
||||
|
||||
labels = list(power_zones.keys())
|
||||
sizes = list(power_zones.values())
|
||||
colors = plt.cm.Set3(np.linspace(0, 1, len(labels)))
|
||||
|
||||
|
||||
ax.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
|
||||
ax.set_title('Time in Power Zones')
|
||||
|
||||
|
||||
filepath = self.output_dir / 'power_zones.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def _create_heart_rate_zones_chart(self, analysis: Dict[str, Any]) -> str:
|
||||
"""Create heart rate zones pie chart.
|
||||
|
||||
|
||||
Args:
|
||||
analysis: Analysis results
|
||||
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if 'heart_rate_analysis' not in analysis or 'hr_zones' not in analysis['heart_rate_analysis']:
|
||||
return None
|
||||
|
||||
|
||||
hr_zones = analysis['heart_rate_analysis']['hr_zones']
|
||||
|
||||
|
||||
fig, ax = plt.subplots(figsize=(8, 8))
|
||||
|
||||
|
||||
labels = list(hr_zones.keys())
|
||||
sizes = list(hr_zones.values())
|
||||
colors = plt.cm.Set3(np.linspace(0, 1, len(labels)))
|
||||
|
||||
|
||||
ax.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
|
||||
ax.set_title('Time in Heart Rate Zones')
|
||||
|
||||
|
||||
filepath = self.output_dir / 'heart_rate_zones.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def _create_power_vs_heart_rate(self, workout: WorkoutData) -> str:
|
||||
"""Create power vs heart rate scatter plot.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if (not workout.power or not workout.power.power_values or
|
||||
not workout.heart_rate or not workout.heart_rate.heart_rate_values):
|
||||
return None
|
||||
|
||||
|
||||
power_values = workout.power.power_values
|
||||
hr_values = workout.heart_rate.heart_rate_values
|
||||
|
||||
|
||||
# Align arrays
|
||||
min_len = min(len(power_values), len(hr_values))
|
||||
if min_len == 0:
|
||||
return None
|
||||
|
||||
|
||||
power_values = power_values[:min_len]
|
||||
hr_values = hr_values[:min_len]
|
||||
|
||||
|
||||
fig, ax = plt.subplots(figsize=(10, 6))
|
||||
|
||||
|
||||
ax.scatter(power_values, hr_values, alpha=0.5, s=1)
|
||||
|
||||
|
||||
# Add trend line
|
||||
z = np.polyfit(power_values, hr_values, 1)
|
||||
p = np.poly1d(z)
|
||||
ax.plot(power_values, p(power_values), "r--", alpha=0.8)
|
||||
|
||||
|
||||
ax.set_xlabel('Power (W)')
|
||||
ax.set_ylabel('Heart Rate (bpm)')
|
||||
ax.set_title('Power vs Heart Rate')
|
||||
ax.grid(True, alpha=0.3)
|
||||
|
||||
|
||||
filepath = self.output_dir / 'power_vs_heart_rate.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def _create_power_vs_speed(self, workout: WorkoutData) -> str:
|
||||
"""Create power vs speed scatter plot.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
if (not workout.power or not workout.power.power_values or
|
||||
not workout.speed or not workout.speed.speed_values):
|
||||
return None
|
||||
|
||||
|
||||
power_values = workout.power.power_values
|
||||
speed_values = workout.speed.speed_values
|
||||
|
||||
|
||||
# Align arrays
|
||||
min_len = min(len(power_values), len(speed_values))
|
||||
if min_len == 0:
|
||||
return None
|
||||
|
||||
|
||||
power_values = power_values[:min_len]
|
||||
speed_values = speed_values[:min_len]
|
||||
|
||||
|
||||
fig, ax = plt.subplots(figsize=(10, 6))
|
||||
|
||||
|
||||
ax.scatter(power_values, speed_values, alpha=0.5, s=1)
|
||||
|
||||
|
||||
# Add trend line
|
||||
z = np.polyfit(power_values, speed_values, 1)
|
||||
p = np.poly1d(z)
|
||||
ax.plot(power_values, p(power_values), "r--", alpha=0.8)
|
||||
|
||||
|
||||
ax.set_xlabel('Power (W)')
|
||||
ax.set_ylabel('Speed (km/h)')
|
||||
ax.set_title('Power vs Speed')
|
||||
ax.grid(True, alpha=0.3)
|
||||
|
||||
|
||||
filepath = self.output_dir / 'power_vs_speed.png'
|
||||
plt.tight_layout()
|
||||
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
||||
plt.close()
|
||||
|
||||
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def _create_workout_dashboard(self, workout: WorkoutData, analysis: Dict[str, Any]) -> str:
|
||||
"""Create comprehensive workout dashboard.
|
||||
|
||||
|
||||
Args:
|
||||
workout: WorkoutData object
|
||||
analysis: Analysis results
|
||||
|
||||
|
||||
Returns:
|
||||
Path to saved chart
|
||||
"""
|
||||
@@ -479,7 +670,7 @@ class ChartGenerator:
|
||||
[{"secondary_y": False}, {"secondary_y": False}],
|
||||
[{"secondary_y": False}, {"secondary_y": False}]]
|
||||
)
|
||||
|
||||
|
||||
# Power time series
|
||||
if workout.power and workout.power.power_values:
|
||||
power_values = workout.power.power_values
|
||||
@@ -488,7 +679,7 @@ class ChartGenerator:
|
||||
go.Scatter(x=time_minutes, y=power_values, name='Power', line=dict(color='orange')),
|
||||
row=1, col=1
|
||||
)
|
||||
|
||||
|
||||
# Heart rate time series
|
||||
if workout.heart_rate and workout.heart_rate.heart_rate_values:
|
||||
hr_values = workout.heart_rate.heart_rate_values
|
||||
@@ -497,7 +688,7 @@ class ChartGenerator:
|
||||
go.Scatter(x=time_minutes, y=hr_values, name='Heart Rate', line=dict(color='red')),
|
||||
row=1, col=2
|
||||
)
|
||||
|
||||
|
||||
# Speed time series
|
||||
if workout.speed and workout.speed.speed_values:
|
||||
speed_values = workout.speed.speed_values
|
||||
@@ -506,7 +697,7 @@ class ChartGenerator:
|
||||
go.Scatter(x=time_minutes, y=speed_values, name='Speed', line=dict(color='blue')),
|
||||
row=2, col=1
|
||||
)
|
||||
|
||||
|
||||
# Elevation profile
|
||||
if workout.elevation and workout.elevation.elevation_values:
|
||||
elevation_values = workout.elevation.elevation_values
|
||||
@@ -515,7 +706,7 @@ class ChartGenerator:
|
||||
go.Scatter(x=time_minutes, y=elevation_values, name='Elevation', line=dict(color='brown')),
|
||||
row=2, col=2
|
||||
)
|
||||
|
||||
|
||||
# Power distribution
|
||||
if workout.power and workout.power.power_values:
|
||||
power_values = workout.power.power_values
|
||||
@@ -523,7 +714,7 @@ class ChartGenerator:
|
||||
go.Histogram(x=power_values, name='Power Distribution', nbinsx=50),
|
||||
row=3, col=1
|
||||
)
|
||||
|
||||
|
||||
# Heart rate distribution
|
||||
if workout.heart_rate and workout.heart_rate.heart_rate_values:
|
||||
hr_values = workout.heart_rate.heart_rate_values
|
||||
@@ -531,14 +722,14 @@ class ChartGenerator:
|
||||
go.Histogram(x=hr_values, name='HR Distribution', nbinsx=30),
|
||||
row=3, col=2
|
||||
)
|
||||
|
||||
|
||||
# Update layout
|
||||
fig.update_layout(
|
||||
height=1200,
|
||||
title_text=f"Workout Dashboard - {workout.metadata.activity_name}",
|
||||
showlegend=False
|
||||
)
|
||||
|
||||
|
||||
# Update axes labels
|
||||
fig.update_xaxes(title_text="Time (minutes)", row=1, col=1)
|
||||
fig.update_yaxes(title_text="Power (W)", row=1, col=1)
|
||||
@@ -550,8 +741,8 @@ class ChartGenerator:
|
||||
fig.update_yaxes(title_text="Elevation (m)", row=2, col=2)
|
||||
fig.update_xaxes(title_text="Power (W)", row=3, col=1)
|
||||
fig.update_xaxes(title_text="Heart Rate (bpm)", row=3, col=2)
|
||||
|
||||
|
||||
filepath = self.output_dir / 'workout_dashboard.html'
|
||||
fig.write_html(str(filepath))
|
||||
|
||||
|
||||
return str(filepath)
|
||||
@@ -40,8 +40,8 @@ class ReportGenerator:
|
||||
self.jinja_env.filters['format_power'] = self._format_power
|
||||
self.jinja_env.filters['format_heart_rate'] = self._format_heart_rate
|
||||
|
||||
def generate_workout_report(self, workout: WorkoutData, analysis: Dict[str, Any],
|
||||
format: str = 'html') -> str:
|
||||
def generate_workout_report(self, workout: WorkoutData, analysis: Dict[str, Any],
|
||||
format: str = 'html') -> str:
|
||||
"""Generate comprehensive workout report.
|
||||
|
||||
Args:
|
||||
@@ -50,7 +50,7 @@ class ReportGenerator:
|
||||
format: Report format ('html', 'pdf', 'markdown')
|
||||
|
||||
Returns:
|
||||
Path to generated report
|
||||
Rendered report content as a string (for html/markdown) or path to PDF file.
|
||||
"""
|
||||
# Prepare report data
|
||||
report_data = self._prepare_report_data(workout, analysis)
|
||||
@@ -59,7 +59,7 @@ class ReportGenerator:
|
||||
if format == 'html':
|
||||
return self._generate_html_report(report_data)
|
||||
elif format == 'pdf':
|
||||
return self._generate_pdf_report(report_data)
|
||||
return self._generate_pdf_report(report_data, workout.metadata.activity_name)
|
||||
elif format == 'markdown':
|
||||
return self._generate_markdown_report(report_data)
|
||||
else:
|
||||
@@ -75,24 +75,57 @@ class ReportGenerator:
|
||||
Returns:
|
||||
Dictionary with report data
|
||||
"""
|
||||
return {
|
||||
'workout': {
|
||||
'metadata': workout.metadata,
|
||||
'summary': analysis.get('summary', {}),
|
||||
'power_analysis': analysis.get('power_analysis', {}),
|
||||
'heart_rate_analysis': analysis.get('heart_rate_analysis', {}),
|
||||
'speed_analysis': analysis.get('speed_analysis', {}),
|
||||
'elevation_analysis': analysis.get('elevation_analysis', {}),
|
||||
'intervals': analysis.get('intervals', []),
|
||||
'zones': analysis.get('zones', {}),
|
||||
'efficiency': analysis.get('efficiency', {})
|
||||
# Normalize and alias data for template compatibility
|
||||
summary = analysis.get('summary', {})
|
||||
summary['avg_speed'] = summary.get('avg_speed_kmh')
|
||||
summary['avg_heart_rate'] = summary.get('avg_hr')
|
||||
|
||||
power_analysis = analysis.get('power_analysis', {})
|
||||
if 'avg_power' not in power_analysis and 'avg_power' in summary:
|
||||
power_analysis['avg_power'] = summary['avg_power']
|
||||
if 'max_power' not in power_analysis and 'max_power' in summary:
|
||||
power_analysis['max_power'] = summary['max_power']
|
||||
|
||||
heart_rate_analysis = analysis.get('heart_rate_analysis', {})
|
||||
if 'avg_hr' not in heart_rate_analysis and 'avg_hr' in summary:
|
||||
heart_rate_analysis['avg_hr'] = summary['avg_hr']
|
||||
if 'max_hr' not in heart_rate_analysis and 'max_hr' in summary:
|
||||
heart_rate_analysis['max_hr'] = summary['max_hr']
|
||||
# For templates using avg_heart_rate
|
||||
heart_rate_analysis['avg_heart_rate'] = heart_rate_analysis.get('avg_hr')
|
||||
heart_rate_analysis['max_heart_rate'] = heart_rate_analysis.get('max_hr')
|
||||
|
||||
|
||||
speed_analysis = analysis.get('speed_analysis', {})
|
||||
speed_analysis['avg_speed'] = speed_analysis.get('avg_speed_kmh')
|
||||
speed_analysis['max_speed'] = speed_analysis.get('max_speed_kmh')
|
||||
|
||||
|
||||
report_context = {
|
||||
"workout": {
|
||||
"metadata": workout.metadata,
|
||||
"summary": summary,
|
||||
"power_analysis": power_analysis,
|
||||
"heart_rate_analysis": heart_rate_analysis,
|
||||
"speed_analysis": speed_analysis,
|
||||
"elevation_analysis": analysis.get("elevation_analysis", {}),
|
||||
"intervals": analysis.get("intervals", []),
|
||||
"zones": analysis.get("zones", {}),
|
||||
"efficiency": analysis.get("efficiency", {}),
|
||||
},
|
||||
"report": {
|
||||
"generated_at": datetime.now().isoformat(),
|
||||
"version": "1.0.0",
|
||||
"tool": "Garmin Analyser",
|
||||
},
|
||||
'report': {
|
||||
'generated_at': datetime.now().isoformat(),
|
||||
'version': '1.0.0',
|
||||
'tool': 'Garmin Analyser'
|
||||
}
|
||||
}
|
||||
|
||||
# Add minute-by-minute aggregation if data is available
|
||||
if workout.df is not None and not workout.df.empty:
|
||||
report_context["minute_by_minute"] = self._aggregate_minute_by_minute(
|
||||
workout.df, analysis
|
||||
)
|
||||
return report_context
|
||||
|
||||
def _generate_html_report(self, report_data: Dict[str, Any]) -> str:
|
||||
"""Generate HTML report.
|
||||
@@ -101,36 +134,40 @@ class ReportGenerator:
|
||||
report_data: Report data
|
||||
|
||||
Returns:
|
||||
Path to generated HTML report
|
||||
Rendered HTML content as a string.
|
||||
"""
|
||||
template = self.jinja_env.get_template('workout_report.html')
|
||||
html_content = template.render(**report_data)
|
||||
|
||||
output_path = Path('reports') / f"workout_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.html"
|
||||
output_path.parent.mkdir(exist_ok=True)
|
||||
|
||||
with open(output_path, 'w', encoding='utf-8') as f:
|
||||
f.write(html_content)
|
||||
|
||||
return str(output_path)
|
||||
# In a real application, you might save this to a file or return it directly
|
||||
# For testing, we return the content directly
|
||||
return html_content
|
||||
|
||||
def _generate_pdf_report(self, report_data: Dict[str, Any]) -> str:
|
||||
def _generate_pdf_report(self, report_data: Dict[str, Any], activity_name: str) -> str:
|
||||
"""Generate PDF report.
|
||||
|
||||
Args:
|
||||
report_data: Report data
|
||||
activity_name: Name of the activity for the filename.
|
||||
|
||||
Returns:
|
||||
Path to generated PDF report
|
||||
Path to generated PDF report.
|
||||
"""
|
||||
# First generate HTML
|
||||
html_path = self._generate_html_report(report_data)
|
||||
html_content = self._generate_html_report(report_data)
|
||||
|
||||
# Convert to PDF
|
||||
pdf_path = html_path.replace('.html', '.pdf')
|
||||
HTML(html_path).write_pdf(pdf_path)
|
||||
output_dir = Path('reports')
|
||||
output_dir.mkdir(exist_ok=True)
|
||||
|
||||
return pdf_path
|
||||
# Sanitize activity_name for filename
|
||||
sanitized_activity_name = "".join(
|
||||
[c if c.isalnum() or c in (' ', '-', '_') else '_' for c in activity_name]
|
||||
).replace(' ', '_')
|
||||
|
||||
pdf_path = output_dir / f"{sanitized_activity_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
||||
|
||||
HTML(string=html_content).write_pdf(str(pdf_path))
|
||||
|
||||
return str(pdf_path)
|
||||
|
||||
def _generate_markdown_report(self, report_data: Dict[str, Any]) -> str:
|
||||
"""Generate Markdown report.
|
||||
@@ -139,44 +176,52 @@ class ReportGenerator:
|
||||
report_data: Report data
|
||||
|
||||
Returns:
|
||||
Path to generated Markdown report
|
||||
Rendered Markdown content as a string.
|
||||
"""
|
||||
template = self.jinja_env.get_template('workout_report.md')
|
||||
markdown_content = template.render(**report_data)
|
||||
|
||||
output_path = Path('reports') / f"workout_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
|
||||
output_path.parent.mkdir(exist_ok=True)
|
||||
|
||||
with open(output_path, 'w', encoding='utf-8') as f:
|
||||
f.write(markdown_content)
|
||||
|
||||
return str(output_path)
|
||||
# In a real application, you might save this to a file or return it directly
|
||||
# For testing, we return the content directly
|
||||
return markdown_content
|
||||
|
||||
def generate_summary_report(self, workouts: List[WorkoutData],
|
||||
analyses: List[Dict[str, Any]]) -> str:
|
||||
def generate_summary_report(self, workouts: List[WorkoutData],
|
||||
analyses: List[Dict[str, Any]],
|
||||
format: str = 'html') -> str:
|
||||
"""Generate summary report for multiple workouts.
|
||||
|
||||
Args:
|
||||
workouts: List of WorkoutData objects
|
||||
analyses: List of analysis results
|
||||
format: Report format ('html', 'pdf', 'markdown')
|
||||
|
||||
Returns:
|
||||
Path to generated summary report
|
||||
Rendered summary report content as a string (for html/markdown) or path to PDF file.
|
||||
"""
|
||||
# Aggregate data
|
||||
summary_data = self._aggregate_workout_data(workouts, analyses)
|
||||
|
||||
# Generate summary report
|
||||
# Generate report based on format
|
||||
if format == 'html':
|
||||
template = self.jinja_env.get_template("summary_report.html")
|
||||
return template.render(**summary_data)
|
||||
elif format == 'pdf':
|
||||
html_content = self._generate_summary_html_report(summary_data)
|
||||
output_dir = Path('reports')
|
||||
output_dir.mkdir(exist_ok=True)
|
||||
pdf_path = output_dir / f"summary_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
||||
HTML(string=html_content).write_pdf(str(pdf_path))
|
||||
return str(pdf_path)
|
||||
elif format == 'markdown':
|
||||
template = self.jinja_env.get_template('summary_report.md')
|
||||
return template.render(**summary_data)
|
||||
else:
|
||||
raise ValueError(f"Unsupported format: {format}")
|
||||
|
||||
def _generate_summary_html_report(self, report_data: Dict[str, Any]) -> str:
|
||||
"""Helper to generate HTML for summary report, used by PDF generation."""
|
||||
template = self.jinja_env.get_template('summary_report.html')
|
||||
html_content = template.render(**summary_data)
|
||||
|
||||
output_path = Path('reports') / f"summary_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.html"
|
||||
output_path.parent.mkdir(exist_ok=True)
|
||||
|
||||
with open(output_path, 'w', encoding='utf-8') as f:
|
||||
f.write(html_content)
|
||||
|
||||
return str(output_path)
|
||||
return template.render(**report_data)
|
||||
|
||||
def _aggregate_workout_data(self, workouts: List[WorkoutData],
|
||||
analyses: List[Dict[str, Any]]) -> Dict[str, Any]:
|
||||
@@ -195,11 +240,11 @@ class ReportGenerator:
|
||||
for workout, analysis in zip(workouts, analyses):
|
||||
data = {
|
||||
'date': workout.metadata.start_time,
|
||||
'activity_type': workout.metadata.activity_type,
|
||||
'activity_type': workout.metadata.sport or workout.metadata.activity_type,
|
||||
'duration_minutes': analysis.get('summary', {}).get('duration_minutes', 0),
|
||||
'distance_km': analysis.get('summary', {}).get('distance_km', 0),
|
||||
'avg_power': analysis.get('summary', {}).get('avg_power', 0),
|
||||
'avg_heart_rate': analysis.get('summary', {}).get('avg_heart_rate', 0),
|
||||
'avg_heart_rate': analysis.get('summary', {}).get('avg_hr', 0),
|
||||
'avg_speed': analysis.get('summary', {}).get('avg_speed_kmh', 0),
|
||||
'elevation_gain': analysis.get('summary', {}).get('elevation_gain_m', 0),
|
||||
'calories': analysis.get('summary', {}).get('calories', 0),
|
||||
@@ -237,6 +282,74 @@ class ReportGenerator:
|
||||
}
|
||||
}
|
||||
|
||||
def _aggregate_minute_by_minute(
|
||||
self, df: pd.DataFrame, analysis: Dict[str, Any]
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Aggregate workout data into minute-by-minute summaries.
|
||||
|
||||
Args:
|
||||
df: Workout DataFrame.
|
||||
analysis: Analysis results.
|
||||
|
||||
Returns:
|
||||
A list of dictionaries, each representing one minute of the workout.
|
||||
"""
|
||||
if "timestamp" not in df.columns:
|
||||
return []
|
||||
|
||||
df = df.copy()
|
||||
df["elapsed_time"] = (
|
||||
df["timestamp"] - df["timestamp"].iloc[0]
|
||||
).dt.total_seconds()
|
||||
df["minute_index"] = (df["elapsed_time"] // 60).astype(int)
|
||||
|
||||
agg_rules = {}
|
||||
if "speed" in df.columns:
|
||||
agg_rules["avg_speed_kmh"] = ("speed", "mean")
|
||||
if "cadence" in df.columns:
|
||||
agg_rules["avg_cadence"] = ("cadence", "mean")
|
||||
if "heart_rate" in df.columns:
|
||||
agg_rules["avg_hr"] = ("heart_rate", "mean")
|
||||
agg_rules["max_hr"] = ("heart_rate", "max")
|
||||
if "power" in df.columns:
|
||||
agg_rules["avg_real_power"] = ("power", "mean")
|
||||
elif "estimated_power" in df.columns:
|
||||
agg_rules["avg_power_estimate"] = ("estimated_power", "mean")
|
||||
|
||||
if not agg_rules:
|
||||
return []
|
||||
|
||||
minute_stats = df.groupby("minute_index").agg(**agg_rules).reset_index()
|
||||
|
||||
# Distance and elevation require special handling
|
||||
if "distance" in df.columns:
|
||||
minute_stats["distance_km"] = (
|
||||
df.groupby("minute_index")["distance"]
|
||||
.apply(lambda x: (x.max() - x.min()) / 1000.0)
|
||||
.values
|
||||
)
|
||||
if "altitude" in df.columns:
|
||||
minute_stats["elevation_change"] = (
|
||||
df.groupby("minute_index")["altitude"]
|
||||
.apply(lambda x: x.iloc[-1] - x.iloc[0] if not x.empty else 0)
|
||||
.values
|
||||
)
|
||||
if "gradient" in df.columns:
|
||||
minute_stats["avg_gradient"] = (
|
||||
df.groupby("minute_index")["gradient"].mean().values
|
||||
)
|
||||
|
||||
# Convert to km/h if speed is in m/s
|
||||
if "avg_speed_kmh" in minute_stats.columns:
|
||||
minute_stats["avg_speed_kmh"] *= 3.6
|
||||
|
||||
# Round and format
|
||||
for col in minute_stats.columns:
|
||||
if minute_stats[col].dtype == "float64":
|
||||
minute_stats[col] = minute_stats[col].round(2)
|
||||
|
||||
return minute_stats.to_dict("records")
|
||||
|
||||
def _format_duration(self, seconds: float) -> str:
|
||||
"""Format duration in seconds to human-readable format.
|
||||
|
||||
@@ -246,6 +359,8 @@ class ReportGenerator:
|
||||
Returns:
|
||||
Formatted duration string
|
||||
"""
|
||||
if pd.isna(seconds):
|
||||
return ""
|
||||
hours = int(seconds // 3600)
|
||||
minutes = int((seconds % 3600) // 60)
|
||||
seconds = int(seconds % 60)
|
||||
@@ -470,6 +585,40 @@ class ReportGenerator:
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
{% if minute_by_minute %}
|
||||
<h2>Minute-by-Minute Analysis</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Minute</th>
|
||||
<th>Distance (km)</th>
|
||||
<th>Avg Speed (km/h)</th>
|
||||
<th>Avg Cadence</th>
|
||||
<th>Avg HR</th>
|
||||
<th>Max HR</th>
|
||||
<th>Avg Gradient (%)</th>
|
||||
<th>Elevation Change (m)</th>
|
||||
<th>Avg Power (W)</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for row in minute_by_minute %}
|
||||
<tr>
|
||||
<td>{{ row.minute_index }}</td>
|
||||
<td>{{ "%.2f"|format(row.distance_km) if row.distance_km is not none }}</td>
|
||||
<td>{{ "%.1f"|format(row.avg_speed_kmh) if row.avg_speed_kmh is not none }}</td>
|
||||
<td>{{ "%.0f"|format(row.avg_cadence) if row.avg_cadence is not none }}</td>
|
||||
<td>{{ "%.0f"|format(row.avg_hr) if row.avg_hr is not none }}</td>
|
||||
<td>{{ "%.0f"|format(row.max_hr) if row.max_hr is not none }}</td>
|
||||
<td>{{ "%.1f"|format(row.avg_gradient) if row.avg_gradient is not none }}</td>
|
||||
<td>{{ "%.1f"|format(row.elevation_change) if row.elevation_change is not none }}</td>
|
||||
<td>{{ "%.0f"|format(row.avg_real_power or row.avg_power_estimate) if (row.avg_real_power or row.avg_power_estimate) is not none }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endif %}
|
||||
|
||||
<div class="footer">
|
||||
<p>Report generated on {{ report.generated_at }} using {{ report.tool }} v{{ report.version }}</p>
|
||||
</div>
|
||||
@@ -516,6 +665,16 @@ class ReportGenerator:
|
||||
- **Average Speed:** {{ workout.speed_analysis.avg_speed|format_speed }}
|
||||
- **Maximum Speed:** {{ workout.speed_analysis.max_speed|format_speed }}
|
||||
|
||||
{% if minute_by_minute %}
|
||||
### Minute-by-Minute Analysis
|
||||
|
||||
| Minute | Dist (km) | Speed (km/h) | Cadence | HR | Max HR | Grad (%) | Elev (m) | Power (W) |
|
||||
|--------|-----------|--------------|---------|----|--------|----------|----------|-----------|
|
||||
{% for row in minute_by_minute -%}
|
||||
| {{ row.minute_index }} | {{ "%.2f"|format(row.distance_km) if row.distance_km is not none }} | {{ "%.1f"|format(row.avg_speed_kmh) if row.avg_speed_kmh is not none }} | {{ "%.0f"|format(row.avg_cadence) if row.avg_cadence is not none }} | {{ "%.0f"|format(row.avg_hr) if row.avg_hr is not none }} | {{ "%.0f"|format(row.max_hr) if row.max_hr is not none }} | {{ "%.1f"|format(row.avg_gradient) if row.avg_gradient is not none }} | {{ "%.1f"|format(row.elevation_change) if row.elevation_change is not none }} | {{ "%.0f"|format(row.avg_real_power or row.avg_power_estimate) if (row.avg_real_power or row.avg_power_estimate) is not none }} |
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
---
|
||||
|
||||
*Report generated on {{ report.generated_at }} using {{ report.tool }} v{{ report.version }}*"""
|
||||
|
||||
89
visualizers/templates/summary_report.html
Normal file
89
visualizers/templates/summary_report.html
Normal file
@@ -0,0 +1,89 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Workout Summary Report</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
background: white;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
}
|
||||
h1, h2 {
|
||||
color: #333;
|
||||
}
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin: 20px 0;
|
||||
}
|
||||
th, td {
|
||||
padding: 10px;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid #ddd;
|
||||
}
|
||||
th {
|
||||
background-color: #f8f9fa;
|
||||
font-weight: bold;
|
||||
}
|
||||
.footer {
|
||||
margin-top: 40px;
|
||||
padding-top: 20px;
|
||||
border-top: 1px solid #eee;
|
||||
color: #666;
|
||||
font-size: 12px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Workout Summary Report</h1>
|
||||
|
||||
<h2>All Workouts</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Sport</th>
|
||||
<th>Duration</th>
|
||||
<th>Distance (km)</th>
|
||||
<th>Avg Speed (km/h)</th>
|
||||
<th>Avg HR</th>
|
||||
<th>NP</th>
|
||||
<th>IF</th>
|
||||
<th>TSS</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for analysis in analyses %}
|
||||
<tr>
|
||||
<td>{{ analysis.summary.start_time.strftime('%Y-%m-%d') if analysis.summary.start_time else 'N/A' }}</td>
|
||||
<td>{{ analysis.summary.sport if analysis.summary.sport else 'N/A' }}</td>
|
||||
<td>{{ analysis.summary.duration_minutes|format_duration if analysis.summary.duration_minutes else 'N/A' }}</td>
|
||||
<td>{{ "%.2f"|format(analysis.summary.distance_km) if analysis.summary.distance_km else 'N/A' }}</td>
|
||||
<td>{{ "%.1f"|format(analysis.summary.avg_speed_kmh) if analysis.summary.avg_speed_kmh else 'N/A' }}</td>
|
||||
<td>{{ "%.0f"|format(analysis.summary.avg_hr) if analysis.summary.avg_hr else 'N/A' }}</td>
|
||||
<td>{{ "%.0f"|format(analysis.summary.normalized_power) if analysis.summary.normalized_power else 'N/A' }}</td>
|
||||
<td>{{ "%.2f"|format(analysis.summary.intensity_factor) if analysis.summary.intensity_factor else 'N/A' }}</td>
|
||||
<td>{{ "%.1f"|format(analysis.summary.training_stress_score) if analysis.summary.training_stress_score else 'N/A' }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div class="footer">
|
||||
<p>Report generated on {{ report.generated_at }} using {{ report.tool }} v{{ report.version }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -159,6 +159,40 @@
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
{% if minute_by_minute %}
|
||||
<h2>Minute-by-Minute Analysis</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Minute</th>
|
||||
<th>Distance (km)</th>
|
||||
<th>Avg Speed (km/h)</th>
|
||||
<th>Avg Cadence</th>
|
||||
<th>Avg HR</th>
|
||||
<th>Max HR</th>
|
||||
<th>Avg Gradient (%)</th>
|
||||
<th>Elevation Change (m)</th>
|
||||
<th>Avg Power (W)</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for row in minute_by_minute %}
|
||||
<tr>
|
||||
<td>{{ row.minute_index }}</td>
|
||||
<td>{{ "%.2f"|format(row.distance_km) if row.distance_km is not none }}</td>
|
||||
<td>{{ "%.1f"|format(row.avg_speed_kmh) if row.avg_speed_kmh is not none }}</td>
|
||||
<td>{{ "%.0f"|format(row.avg_cadence) if row.avg_cadence is not none }}</td>
|
||||
<td>{{ "%.0f"|format(row.avg_hr) if row.avg_hr is not none }}</td>
|
||||
<td>{{ "%.0f"|format(row.max_hr) if row.max_hr is not none }}</td>
|
||||
<td>{{ "%.1f"|format(row.avg_gradient) if row.avg_gradient is not none }}</td>
|
||||
<td>{{ "%.1f"|format(row.elevation_change) if row.elevation_change is not none }}</td>
|
||||
<td>{{ "%.0f"|format(row.avg_real_power or row.avg_power_estimate) if (row.avg_real_power or row.avg_power_estimate) is not none }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endif %}
|
||||
|
||||
<div class="footer">
|
||||
<p>Report generated on {{ report.generated_at }} using {{ report.tool }} v{{ report.version }}</p>
|
||||
</div>
|
||||
|
||||
@@ -33,6 +33,16 @@
|
||||
- **Average Speed:** {{ workout.speed_analysis.avg_speed|format_speed }}
|
||||
- **Maximum Speed:** {{ workout.speed_analysis.max_speed|format_speed }}
|
||||
|
||||
{% if minute_by_minute %}
|
||||
### Minute-by-Minute Analysis
|
||||
|
||||
| Minute | Dist (km) | Speed (km/h) | Cadence | HR | Max HR | Grad (%) | Elev (m) | Power (W) |
|
||||
|--------|-----------|--------------|---------|----|--------|----------|----------|-----------|
|
||||
{% for row in minute_by_minute -%}
|
||||
| {{ row.minute_index }} | {{ "%.2f"|format(row.distance_km) if row.distance_km is not none }} | {{ "%.1f"|format(row.avg_speed_kmh) if row.avg_speed_kmh is not none }} | {{ "%.0f"|format(row.avg_cadence) if row.avg_cadence is not none }} | {{ "%.0f"|format(row.avg_hr) if row.avg_hr is not none }} | {{ "%.0f"|format(row.max_hr) if row.max_hr is not none }} | {{ "%.1f"|format(row.avg_gradient) if row.avg_gradient is not none }} | {{ "%.1f"|format(row.elevation_change) if row.elevation_change is not none }} | {{ "%.0f"|format(row.avg_real_power or row.avg_power_estimate) if (row.avg_real_power or row.avg_power_estimate) is not none }} |
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
---
|
||||
|
||||
*Report generated on {{ report.generated_at }} using {{ report.tool }} v{{ report.version }}*
|
||||
Reference in New Issue
Block a user