"""Treadmill pace converter: belt pace, the flat pace of equal oxygen cost, the outdoor road equivalent, and calories, by the ACSM metabolic equations and the 1% rule of Jones and Doust. Inputs: speed_kph (1 to 30), incline_pct (0 to 30, default 1), weight_kg (1 to 300), gait (walking or running), duration_minutes (1 to 600, default 30). Returns: speedMph; paceMinPerKm, paceMinPerMile; adjustedSpeedKph, adjustedPaceMinPerKm, adjustedPaceMinPerMile; outdoorEquivalentSpeedKph, outdoorEquivalentPaceMinPerKm, outdoorEquivalentPaceMinPerMile; caloriesPerHour, caloriesTotal (kcal); distanceKm; raceTimesMinutes (5 km, 10 km, half and full marathon at the belt speed). mysi, Treadmill pace converter, mysi.dev/treadmill-pace-converter, version 1, changed 2026-12-04. MIT. """ def _treadmill_pace_converter_num(name, value, low, high): if isinstance(value, bool) or not isinstance(value, (int, float)) or not low <= value <= high: raise ValueError(f"{name} must be a number from {low} to {high}") return float(value) def treadmill_pace_converter(speed_kph, weight_kg, gait, incline_pct=1, duration_minutes=30): num = _treadmill_pace_converter_num s = num("speed_kph", speed_kph, 1, 30) w = num("weight_kg", weight_kg, 1, 300) g = num("incline_pct", incline_pct, 0, 30) / 100 minutes = num("duration_minutes", duration_minutes, 1, 600) if gait not in ("walking", "running"): raise ValueError("gait must be one of: walking, running") mile = 1.609344 # km per international mile adjusted = s * (0.2 + 0.9 * g) / 0.2 # ACSM running equation, solved for 0% grade outdoor = s * (0.2 + 0.9 * (g - 0.01)) / 0.2 # 1% treadmill grade = flat road (Jones 1996) v = s * 1000 / 60 # metres per minute if gait == "walking": # ACSM walking and running equations, ml/kg/min vo2 = 0.1 * v + 1.8 * v * g + 3.5 else: vo2 = 0.2 * v + 0.9 * v * g + 3.5 per_hour = vo2 * w * 5 / 1000 * 60 # 5 kcal per litre of oxygen (ACSM) return { "speedMph": s / mile, "paceMinPerKm": 60 / s, "paceMinPerMile": 60 / s * mile, "adjustedSpeedKph": adjusted, "adjustedPaceMinPerKm": 60 / adjusted, "adjustedPaceMinPerMile": 60 / adjusted * mile, "outdoorEquivalentSpeedKph": outdoor, "outdoorEquivalentPaceMinPerKm": 60 / outdoor, "outdoorEquivalentPaceMinPerMile": 60 / outdoor * mile, "caloriesPerHour": per_hour, "caloriesTotal": per_hour * minutes / 60, "distanceKm": s * minutes / 60, # road race distances in km: 5 km, 10 km, half and full marathon (World Athletics) "raceTimesMinutes": [d / s * 60 for d in (5, 10, 21.0975, 42.195)], } TREADMILL_PACE_CONVERTER_CASES = [ ({"speed_kph": 10, "incline_pct": 2, "weight_kg": 70, "gait": "running", "duration_minutes": 30}, {"outdoorEquivalentSpeedKph": 10.45, "adjustedSpeedKph": 10.9, "caloriesTotal": 418.25}), ({"speed_kph": 6, "incline_pct": 12, "weight_kg": 80, "gait": "walking", "duration_minutes": 60}, {"paceMinPerKm": 10.0, "caloriesPerHour": 842.4, "distanceKm": 6.0}), ({"speed_kph": 15, "incline_pct": 0, "weight_kg": 60, "gait": "running", "duration_minutes": 20}, {"outdoorEquivalentPaceMinPerKm": 4.18848, "caloriesTotal": 321.0, "distanceKm": 5.0}), ] def treadmill_pace_converter_check(tolerance=1e-3): for given, want in TREADMILL_PACE_CONVERTER_CASES: got = treadmill_pace_converter(**given) for name, value in want.items(): close = abs(got[name] - value) <= tolerance * max(1, abs(value)) assert close, (name, got[name], value) return "ok" if __name__ == "__main__": import json, sys treadmill_pace_converter_check() given = dict(a.split("=", 1) for a in sys.argv[1:] if "=" in a) js = lambda v: v.lstrip("-")[:1].isdigit() or v[:1] in '[{".' or v in ("true", "false", "null") given = {k: json.loads(v) if js(v) else v for k, v in given.items()} if given: print(json.dumps(treadmill_pace_converter(**given)))