gc_plots/lactate-ramp.py
2025-05-03 20:54:38 +02:00

86 lines
2.1 KiB
Python

import numpy as np
from scipy import optimize
metrics = GC.activityMetrics(compare=True)
n_activities = len(metrics)
def piecewise_linear(x, x0, y0, k1, k2):
def fun0(x):
k1 * x + y0 - k1 * x0
def fun1(x):
k2 * x + y0 - k2 * x0
return np.piecewise(x, [x < x0], [fun0, fun1])
GC.setChart(
title="Lactate-Ramp",
type=GC.CHART_LINE,
animate=False,
legpos=GC_ALIGN_TOP,
stack=False,
)
for i, metric in enumerate(metrics):
date = metric[0]["date"]
color = metric[1]
print(color)
x = list(GC.xdataSeries("Lactate-Ramp", "Power", compareindex=i))
y = list(GC.xdataSeries("Lactate-Ramp", "Lactate", compareindex=i))
if not (x and y):
continue
GC.addCurve(
name=str(date),
x=x,
y=y,
xaxis="Power",
yaxis="Lactate",
color=color,
line=GC_LINE_SOLID,
symbol=GC_SYMBOL_CIRCLE,
size=3,
opacity=100,
opengl=False,
)
coeffs = np.polyfit(x, y, 3)
poly = np.poly1d(coeffs)
x_poly = np.linspace(x[0], x[-1], 51)
y_poly = poly(x_poly)
GC.addCurve(
name="Fit",
x=x_poly,
y=y_poly,
xaxis="Power",
yaxis="Lactate",
color=color,
line=GC_LINE_DASH,
symbol=GC_SYMBOL_NONE,
size=3,
opacity=100,
opengl=False,
)
x_poly = np.linspace(x[0], x[-2], 51)
x_poly = np.array(x)
y_poly = poly(x_poly)
p, e = optimize.curve_fit(piecewise_linear, np.log(x_poly), np.log(y_poly), p0=[np.mean(x_poly), np.mean(y_poly), 0, 0])
# p, e = optimize.curve_fit(piecewise_linear, x_poly, y_poly)
print(p)
GC.addCurve(
name="log-log",
x=x_poly,
y=piecewise_linear(x_poly, *p),
xaxis="Power",
yaxis="Lactate",
line=GC_LINE_DASH,
symbol=GC_SYMBOL_NONE,
size=1,
opacity=100,
opengl=False,
)
print(piecewise_linear(x_poly, *p))
GC.setAxis("Power", min=90, max=350, type=GC.AXIS_CONTINUOUS)
GC.setAxis("Lactate", min=0.0, max=10.0, type=GC.AXIS_CONTINUOUS)