31 lines
935 B
Python
31 lines
935 B
Python
import numpy as np
|
|
|
|
from backend.app.models.activity import DataPoint
|
|
|
|
# Standard durations for the power duration curve
|
|
DURATIONS = [1, 5, 10, 15, 30, 60, 120, 300, 600, 1200, 1800, 3600]
|
|
|
|
|
|
def calculate_power_curve(data_points: list[DataPoint]) -> dict[int, int]:
|
|
"""
|
|
Calculate max average power for standard durations.
|
|
Returns {duration_seconds: max_avg_power}.
|
|
"""
|
|
powers = np.array([dp.power for dp in data_points if dp.power is not None], dtype=float)
|
|
if len(powers) == 0:
|
|
return {}
|
|
|
|
result = {}
|
|
for dur in DURATIONS:
|
|
if dur > len(powers):
|
|
break
|
|
if dur == 1:
|
|
result[dur] = int(np.max(powers))
|
|
else:
|
|
# Rolling mean via cumsum for efficiency
|
|
cumsum = np.cumsum(np.insert(powers, 0, 0))
|
|
rolling_avg = (cumsum[dur:] - cumsum[:-dur]) / dur
|
|
result[dur] = int(np.max(rolling_avg))
|
|
|
|
return result
|