Add support to set degree of line of best fit for exercise progress graphs in custom view
This commit is contained in:
3
app.py
3
app.py
@@ -404,11 +404,12 @@ def get_exercise_progress_for_user(person_id, exercise_id):
|
|||||||
max_date = convert_str_to_date(request.args.get(
|
max_date = convert_str_to_date(request.args.get(
|
||||||
'max_date'), '%Y-%m-%d')
|
'max_date'), '%Y-%m-%d')
|
||||||
epoch = request.args.get('epoch', default='All')
|
epoch = request.args.get('epoch', default='All')
|
||||||
|
degree = request.args.get('degree', type=int, default=1)
|
||||||
|
|
||||||
if epoch == 'Custom' and (min_date is None or max_date is None):
|
if epoch == 'Custom' and (min_date is None or max_date is None):
|
||||||
(min_date, max_date) = db.get_exercise_earliest_and_latest_dates(person_id, exercise_id)
|
(min_date, max_date) = db.get_exercise_earliest_and_latest_dates(person_id, exercise_id)
|
||||||
|
|
||||||
exercise_progress = db.get_exercise_progress_for_user(person_id, exercise_id, min_date, max_date, epoch)
|
exercise_progress = db.get_exercise_progress_for_user(person_id, exercise_id, min_date, max_date, epoch, degree=degree)
|
||||||
|
|
||||||
if not exercise_progress:
|
if not exercise_progress:
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|||||||
18
db.py
18
db.py
@@ -481,7 +481,7 @@ class DataBase():
|
|||||||
'SELECT exercise_id, name FROM exercise')
|
'SELECT exercise_id, name FROM exercise')
|
||||||
return exercises
|
return exercises
|
||||||
|
|
||||||
def get_exercise_progress_for_user(self, person_id, exercise_id, min_date=None, max_date=None, epoch='all'):
|
def get_exercise_progress_for_user(self, person_id, exercise_id, min_date=None, max_date=None, epoch='all', degree=1):
|
||||||
today = datetime.now()
|
today = datetime.now()
|
||||||
if epoch == '1M':
|
if epoch == '1M':
|
||||||
min_date = today - relativedelta(months=1)
|
min_date = today - relativedelta(months=1)
|
||||||
@@ -489,6 +489,7 @@ class DataBase():
|
|||||||
min_date = today - relativedelta(months=3)
|
min_date = today - relativedelta(months=3)
|
||||||
elif epoch == '6M':
|
elif epoch == '6M':
|
||||||
min_date = today - relativedelta(months=6)
|
min_date = today - relativedelta(months=6)
|
||||||
|
|
||||||
# Execute SQL query to fetch topset data for a specific person and exercise
|
# Execute SQL query to fetch topset data for a specific person and exercise
|
||||||
topsets = self.execute("""
|
topsets = self.execute("""
|
||||||
SELECT
|
SELECT
|
||||||
@@ -516,13 +517,26 @@ class DataBase():
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
# Extracting values and calculating value ranges for SVG dimensions
|
# Extracting values and calculating value ranges for SVG dimensions
|
||||||
|
exercise_name = topsets[0]['exercise_name']
|
||||||
estimated_1rm = [t['estimated_1rm'] for t in topsets]
|
estimated_1rm = [t['estimated_1rm'] for t in topsets]
|
||||||
repetitions = [t['repetitions'] for t in topsets]
|
repetitions = [t['repetitions'] for t in topsets]
|
||||||
weight = [t['weight'] for t in topsets]
|
weight = [t['weight'] for t in topsets]
|
||||||
start_dates = [t['start_date'] for t in topsets]
|
start_dates = [t['start_date'] for t in topsets]
|
||||||
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]
|
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]
|
||||||
|
|
||||||
exercise_progress = get_exercise_graph_model(topsets[0]['exercise_name'], estimated_1rm, repetitions, weight, start_dates, messages, epoch, person_id, exercise_id, min_date, max_date)
|
exercise_progress = get_exercise_graph_model(
|
||||||
|
exercise_name,
|
||||||
|
estimated_1rm,
|
||||||
|
repetitions,
|
||||||
|
weight,
|
||||||
|
start_dates,
|
||||||
|
messages,
|
||||||
|
epoch,
|
||||||
|
person_id,
|
||||||
|
exercise_id,
|
||||||
|
min_date,
|
||||||
|
max_date,
|
||||||
|
degree)
|
||||||
|
|
||||||
return exercise_progress
|
return exercise_progress
|
||||||
|
|
||||||
|
|||||||
@@ -72,53 +72,129 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% if selected_epoch == 'Custom' %}
|
{% if selected_epoch == 'Custom' %}
|
||||||
<div>
|
<div class="flex flex-col md:flex-row justify-center pb-4">
|
||||||
<div class="flex justify-center pb-4">
|
<!-- Min Date -->
|
||||||
<div class="w-full md:w-1/3 px-2 md:px-3 mb-6 md:mb-0">
|
<div class="w-full md:w-1/3 px-2 md:px-3 mb-6 md:mb-0">
|
||||||
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">
|
<label
|
||||||
|
class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
|
||||||
|
for="grid-city"
|
||||||
|
>
|
||||||
Min date
|
Min date
|
||||||
</label>
|
</label>
|
||||||
<div class="relative pr-2">
|
<div class="relative pr-2">
|
||||||
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
|
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
|
||||||
<svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor"
|
<svg
|
||||||
viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
|
aria-hidden="true"
|
||||||
<path fill-rule="evenodd"
|
class="w-5 h-5 text-gray-500 dark:text-gray-400"
|
||||||
d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
|
fill="currentColor"
|
||||||
clip-rule="evenodd"></path>
|
viewBox="0 0 20 20"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
fill-rule="evenodd"
|
||||||
|
d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2
|
||||||
|
2 0 002 2h12a2 2 0 002-2V6a2 2 0
|
||||||
|
00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1
|
||||||
|
1 0 00-1-1zm0 5a1 1 0 000
|
||||||
|
2h8a1 1 0 100-2H6z"
|
||||||
|
clip-rule="evenodd"
|
||||||
|
></path>
|
||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
<input type="date"
|
<input
|
||||||
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 w-full"
|
type="date"
|
||||||
name="min_date" value="{{ min_date }}"
|
class="bg-gray-50 border border-gray-300 text-gray-900
|
||||||
|
text-sm rounded-lg focus:ring-blue-500
|
||||||
|
focus:border-blue-500 block w-full pl-10 p-2.5"
|
||||||
|
name="min_date"
|
||||||
|
value="{{ min_date }}"
|
||||||
hx-get="{{ url_for('get_exercise_progress_for_user', person_id=person_id, exercise_id=exercise_id) }}"
|
hx-get="{{ url_for('get_exercise_progress_for_user', person_id=person_id, exercise_id=exercise_id) }}"
|
||||||
hx-include="#svg-plot-{{ unique_id }} [name='max_date']"
|
hx-include="#svg-plot-{{ unique_id }} [name='max_date'], #svg-plot-{{ unique_id }} [name='degree']"
|
||||||
hx-vals='{"epoch": "Custom"}'
|
hx-vals='{"epoch": "Custom"}'
|
||||||
hx-target="#svg-plot-{{ unique_id }}" hx-swap="outerHTML" hx-trigger="change">
|
hx-target="#svg-plot-{{ unique_id }}"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
hx-trigger="change"
|
||||||
|
>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Max Date -->
|
||||||
<div class="w-full md:w-1/3 px-2 md:px-3 mb-6 md:mb-0">
|
<div class="w-full md:w-1/3 px-2 md:px-3 mb-6 md:mb-0">
|
||||||
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-zip">
|
<label
|
||||||
|
class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
|
||||||
|
for="grid-zip"
|
||||||
|
>
|
||||||
Max date
|
Max date
|
||||||
</label>
|
</label>
|
||||||
<div class="relative pr-2">
|
<div class="relative pr-2">
|
||||||
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
|
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
|
||||||
<svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor"
|
<svg
|
||||||
viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
|
aria-hidden="true"
|
||||||
<path fill-rule="evenodd"
|
class="w-5 h-5 text-gray-500 dark:text-gray-400"
|
||||||
d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
|
fill="currentColor"
|
||||||
clip-rule="evenodd"></path>
|
viewBox="0 0 20 20"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
fill-rule="evenodd"
|
||||||
|
d="M6 2a1 1 0 00-1 1v1H4a2 2 0
|
||||||
|
00-2 2v10a2 2 0 002 2h12a2
|
||||||
|
2 0 002-2V6a2 2 0 00-2-2h-1V3a1
|
||||||
|
1 0 10-2 0v1H7V3a1 1 0
|
||||||
|
00-1-1zm0 5a1 1 0 000
|
||||||
|
2h8a1 1 0 100-2H6z"
|
||||||
|
clip-rule="evenodd"
|
||||||
|
></path>
|
||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
<input type="date"
|
<input
|
||||||
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 w-full"
|
type="date"
|
||||||
name="max_date" value="{{ max_date }}"
|
class="bg-gray-50 border border-gray-300
|
||||||
|
text-gray-900 text-sm rounded-lg
|
||||||
|
focus:ring-blue-500 focus:border-blue-500
|
||||||
|
block w-full pl-10 p-2.5"
|
||||||
|
name="max_date"
|
||||||
|
value="{{ max_date }}"
|
||||||
hx-get="{{ url_for('get_exercise_progress_for_user', person_id=person_id, exercise_id=exercise_id) }}"
|
hx-get="{{ url_for('get_exercise_progress_for_user', person_id=person_id, exercise_id=exercise_id) }}"
|
||||||
hx-include="#svg-plot-{{ unique_id }} [name='min_date']"
|
hx-include="#svg-plot-{{ unique_id }} [name='min_date'], #svg-plot-{{ unique_id }} [name='degree']"
|
||||||
|
hx-vals='{"epoch": "Custom"}'
|
||||||
|
hx-target="#svg-plot-{{ unique_id }}"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
hx-trigger="change"
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Degree -->
|
||||||
|
<div class="w-full md:w-1/3 px-2 md:px-3 mb-6 md:mb-0">
|
||||||
|
<label
|
||||||
|
class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
|
||||||
|
for="grid-zip"
|
||||||
|
>
|
||||||
|
Degree
|
||||||
|
</label>
|
||||||
|
<div class="relative pr-2">
|
||||||
|
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
|
||||||
|
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5 text-gray-500 dark:text-gray-400">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" d="M3 13.125C3 12.504 3.504 12 4.125 12h2.25c.621 0 1.125.504 1.125 1.125v6.75C7.5 20.496 6.996 21 6.375 21h-2.25A1.125 1.125 0 0 1 3 19.875v-6.75ZM9.75 8.625c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125v11.25c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 0 1-1.125-1.125V8.625ZM16.5 4.125c0-.621.504-1.125 1.125-1.125h2.25C20.496 3 21 3.504 21 4.125v15.75c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 0 1-1.125-1.125V4.125Z" />
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<input type="number"
|
||||||
|
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 w-full"
|
||||||
|
name="degree"
|
||||||
|
value="{{ degree }}"
|
||||||
|
min="1"
|
||||||
|
step="1"
|
||||||
|
hx-get="{{ url_for('get_exercise_progress_for_user', person_id=person_id, exercise_id=exercise_id) }}"
|
||||||
|
hx-include="#svg-plot-{{ unique_id }} [name='min_date'], #svg-plot-{{ unique_id }} [name='max_date']"
|
||||||
hx-vals='{"epoch": "Custom"}'
|
hx-vals='{"epoch": "Custom"}'
|
||||||
hx-target="#svg-plot-{{ unique_id }}" hx-swap="outerHTML" hx-trigger="change">
|
hx-target="#svg-plot-{{ unique_id }}" hx-swap="outerHTML" hx-trigger="change">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<svg viewBox="0 0 {{ (vb_width + 2*margin) | int }} {{ (vb_height + 2*margin) | int }}" preserveAspectRatio="none">
|
<svg viewBox="0 0 {{ (vb_width + 2*margin) | int }} {{ (vb_height + 2*margin) | int }}" preserveAspectRatio="none">
|
||||||
{% for plot in plots %}
|
{% for plot in plots %}
|
||||||
|
|||||||
12
utils.py
12
utils.py
@@ -13,7 +13,7 @@ def convert_str_to_date(date_str, format='%Y-%m-%d'):
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
return None
|
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
|
# Precompute ranges
|
||||||
min_date, max_date = min(start_dates), max(start_dates)
|
min_date, max_date = min(start_dates), max(start_dates)
|
||||||
total_span = (max_date - min_date).days or 1
|
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 = []
|
best_fit_points = []
|
||||||
try:
|
try:
|
||||||
if len(relative_positions) > 1: # Ensure there are enough points for polyfit
|
if len(relative_positions) > 1: # Ensure there are enough points for polyfit
|
||||||
# Calculate line of best fit using NumPy
|
# Fit a polynomial of the given degree
|
||||||
m, b = np.polyfit(relative_positions, estimated_1rm_scaled, 1)
|
coeffs = np.polyfit(relative_positions, estimated_1rm_scaled, degree)
|
||||||
y_best_fit = m * relative_positions + b
|
poly_fit = np.poly1d(coeffs)
|
||||||
|
y_best_fit = poly_fit(relative_positions)
|
||||||
best_fit_points = list(zip(y_best_fit.tolist(), relative_positions.tolist()))
|
best_fit_points = list(zip(y_best_fit.tolist(), relative_positions.tolist()))
|
||||||
else:
|
else:
|
||||||
raise ValueError("Not enough data points for polyfit")
|
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,
|
'person_id': person_id,
|
||||||
'exercise_id': exercise_id,
|
'exercise_id': exercise_id,
|
||||||
'min_date': min_date,
|
'min_date': min_date,
|
||||||
'max_date': max_date
|
'max_date': max_date,
|
||||||
|
'degree': degree
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_distinct_colors(n):
|
def get_distinct_colors(n):
|
||||||
|
|||||||
Reference in New Issue
Block a user