Add support to set degree of line of best fit for exercise progress graphs in custom view
This commit is contained in:
12
utils.py
12
utils.py
@@ -13,7 +13,7 @@ def convert_str_to_date(date_str, format='%Y-%m-%d'):
|
||||
except TypeError:
|
||||
return None
|
||||
|
||||
def get_exercise_graph_model(title, estimated_1rm, repetitions, weight, start_dates, messages, epoch, person_id, exercise_id, min_date=None, max_date=None):
|
||||
def get_exercise_graph_model(title, estimated_1rm, repetitions, weight, start_dates, messages, epoch, person_id, exercise_id, min_date=None, max_date=None, degree=1):
|
||||
# Precompute ranges
|
||||
min_date, max_date = min(start_dates), max(start_dates)
|
||||
total_span = (max_date - min_date).days or 1
|
||||
@@ -47,9 +47,10 @@ def get_exercise_graph_model(title, estimated_1rm, repetitions, weight, start_da
|
||||
best_fit_points = []
|
||||
try:
|
||||
if len(relative_positions) > 1: # Ensure there are enough points for polyfit
|
||||
# Calculate line of best fit using NumPy
|
||||
m, b = np.polyfit(relative_positions, estimated_1rm_scaled, 1)
|
||||
y_best_fit = m * relative_positions + b
|
||||
# Fit a polynomial of the given degree
|
||||
coeffs = np.polyfit(relative_positions, estimated_1rm_scaled, degree)
|
||||
poly_fit = np.poly1d(coeffs)
|
||||
y_best_fit = poly_fit(relative_positions)
|
||||
best_fit_points = list(zip(y_best_fit.tolist(), relative_positions.tolist()))
|
||||
else:
|
||||
raise ValueError("Not enough data points for polyfit")
|
||||
@@ -92,7 +93,8 @@ def get_exercise_graph_model(title, estimated_1rm, repetitions, weight, start_da
|
||||
'person_id': person_id,
|
||||
'exercise_id': exercise_id,
|
||||
'min_date': min_date,
|
||||
'max_date': max_date
|
||||
'max_date': max_date,
|
||||
'degree': degree
|
||||
}
|
||||
|
||||
def get_distinct_colors(n):
|
||||
|
||||
Reference in New Issue
Block a user