This commit is contained in:
Thies Lennart Alff 2025-07-18 13:39:03 +02:00
parent e53878f665
commit 6d2323e5ac
Signed by: lennartalff
GPG key ID: 4EC67D34D594104D
2 changed files with 54 additions and 12 deletions

View file

@ -1,6 +1,8 @@
import numpy as np import numpy as np
from scipy import optimize from scipy import optimize
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import yaml
x0 = np.array( x0 = np.array(
[ [
@ -59,6 +61,35 @@ y1 = np.array(
] ]
) )
x2 = np.array(
[
154,
172,
191,
209,
228,
246,
265,
283,
302,
]
)
y2 = np.array(
[
1.3,
1.4,
1.4,
1.8,
2,
2.6,
3.5,
4.1,
6.6,
]
)
def get_poly_fit(x, y, remove_first_n=1, remove_last_n=1, n_points=100): def get_poly_fit(x, y, remove_first_n=1, remove_last_n=1, n_points=100):
coeffs = np.polyfit(x, y, 3) coeffs = np.polyfit(x, y, 3)
poly = np.poly1d(coeffs) poly = np.poly1d(coeffs)
@ -68,10 +99,13 @@ def get_poly_fit(x, y, remove_first_n=1, remove_last_n=1, n_points=100):
y_poly = poly(x_poly) y_poly = poly(x_poly)
return x_poly, y_poly return x_poly, y_poly
def get_log_log_threshold(x, y): def get_log_log_threshold(x, y):
x_log = np.log(x) x_log = np.log(x)
y_log = np.log(y) y_log = np.log(y)
p, e = optimize.curve_fit(piecewise_linear, x_log, y_log, p0=[np.mean(x_log), np.mean(y_log), 0.5, 4.0]) p, e = optimize.curve_fit(
piecewise_linear, x_log, y_log, p0=[np.mean(x_log), np.mean(y_log), 0.5, 4.0]
)
return np.exp(p[0]) return np.exp(p[0])
@ -80,25 +114,32 @@ def piecewise_linear(x, x0, y0, k1, k2):
x, [x < x0], [lambda x: k1 * x + y0 - k1 * x0, lambda x: k2 * x + y0 - k2 * x0] x, [x < x0], [lambda x: k1 * x + y0 - k1 * x0, lambda x: k2 * x + y0 - k2 * x0]
) )
def main(): def main():
with open('/home/lennartalff/Nextcloud/Private/HRV4Training/lactate/ramp.yaml') as f:
data = yaml.safe_load(f)
first_x, first_y = get_poly_fit(x0, y0) first_x, first_y = get_poly_fit(x0, y0)
second_x, second_y = get_poly_fit(x1, y1) second_x, second_y = get_poly_fit(x1, y1)
first_lt1 = get_log_log_threshold(first_x, first_y) first_lt1 = get_log_log_threshold(first_x, first_y)
second_lt1 = get_log_log_threshold(second_x, second_y) second_lt1 = get_log_log_threshold(second_x, second_y)
plt.plot(first_x, first_y, color='C0') third_x, third_y = get_poly_fit(x2, y2)
plt.scatter(x0, y0, color='C0') third_lt1 = get_log_log_threshold(third_x, third_y)
plt.plot(second_x, second_y, color='C1') plt.plot(first_x, first_y, color="C0")
plt.scatter(x1, y1, color='C1') plt.scatter(x0, y0, color="C0")
plt.axvline(first_lt1, color='C0', linestyle='dashed') plt.plot(second_x, second_y, color="C1")
plt.axvline(second_lt1, color='C1', linestyle='dashed') plt.scatter(x1, y1, color="C1")
plt.grid(True, which='major') plt.plot(third_x, third_y, color="C2")
plt.scatter(x2, y2, color="C2")
plt.axvline(first_lt1, color="C0", linestyle="dashed")
plt.axvline(second_lt1, color="C1", linestyle="dashed")
plt.axvline(third_lt1, color="C2", linestyle="dashed")
plt.grid(True, which="major")
major_ticks = np.arange(0, 8, 1) major_ticks = np.arange(0, 8, 1)
minor_ticks = np.arange(0, 8, 0.1) minor_ticks = np.arange(0, 8, 0.1)
plt.yticks(major_ticks) plt.yticks(major_ticks)
plt.yticks(minor_ticks, minor=True) plt.yticks(minor_ticks, minor=True)
plt.show() plt.show()
if __name__ == '__main__':
if __name__ == "__main__":
main() main()

View file

@ -3,3 +3,4 @@ pandas
scipy scipy
matplotlib matplotlib
pwlf pwlf
pyyaml