Add line of best fit (adding dependency on numpy)

This commit is contained in:
Peter Stockings
2023-12-08 23:51:10 +11:00
parent ded5154acf
commit dd093e3819
3 changed files with 29 additions and 6 deletions

15
db.py
View File

@@ -1,5 +1,6 @@
import os
import psycopg2
import numpy as np
from psycopg2.extras import RealDictCursor
from datetime import datetime
from urllib.parse import urlparse
@@ -505,6 +506,17 @@ class DataBase():
total_span = date_range.days or 1
relative_positions = [(date - min_date).days / total_span for date in start_dates]
# Convert relative positions and scaled estimated 1RM values to numpy arrays
x = np.array(relative_positions)
y = np.array(estimated_1rm_scaled)
# Calculate the slope (m) and y-intercept (b) of the line of best fit
m, b = np.polyfit(x, y, 1)
# Generate points along the line of best fit
y_best_fit = [m * xi + b for xi in x]
best_fit_points = zip(y_best_fit, relative_positions)
# Create messages and zip data for SVG plotting
messages = [f'{t["repetitions"]} x {t["weight"]}kg ({t["estimated_1rm"]}kg E1RM) on {t["start_date"].strftime("%d %b %y")}' for t in topsets]
data_points = zip(estimated_1rm_scaled, relative_positions, messages)
@@ -514,5 +526,6 @@ class DataBase():
'exercise_name': topsets[0]['exercise_name'],
'vb_width': vb_width,
'vb_height': vb_height,
'data_points': list(data_points)
'data_points': list(data_points),
'best_fit_points': list(best_fit_points),
}