Fix issue where sets werent displayed in order on month overview
This commit is contained in:
76
utils.py
76
utils.py
@@ -5,36 +5,46 @@ import numpy as np
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
def get_workouts(topsets):
|
def get_workouts(topsets):
|
||||||
# Get all unique workout_ids (No duplicates)
|
# Ensure all entries have 'WorkoutId' and 'TopSetId', then sort by 'WorkoutId' and 'TopSetId'
|
||||||
workout_ids = list(set([t['WorkoutId']
|
filtered_topsets = sorted(
|
||||||
for t in topsets if t['WorkoutId'] is not None]))
|
[t for t in topsets if t['WorkoutId'] is not None and t['TopSetId'] is not None],
|
||||||
|
key=lambda x: (x['WorkoutId'], x['TopSetId'])
|
||||||
|
)
|
||||||
|
|
||||||
# Group topsets into workouts
|
workouts = {}
|
||||||
workouts = []
|
for t in filtered_topsets:
|
||||||
for workout_id in reversed(workout_ids):
|
workout_id = t['WorkoutId']
|
||||||
topsets_in_workout = [
|
if workout_id not in workouts:
|
||||||
t for t in topsets if t['WorkoutId'] == workout_id]
|
workouts[workout_id] = {
|
||||||
workouts.append({
|
'WorkoutId': workout_id,
|
||||||
'WorkoutId': workout_id,
|
'StartDate': t['StartDate'],
|
||||||
'StartDate': topsets_in_workout[0]['StartDate'],
|
'TopSets': []
|
||||||
'TopSets': [{"TopSetId": t['TopSetId'], "ExerciseId": t['ExerciseId'], "ExerciseName": t['ExerciseName'], "Weight": t['Weight'], "Repetitions": t['Repetitions'], "Estimated1RM": t['Estimated1RM']} for t in topsets_in_workout if t['TopSetId'] is not None]
|
}
|
||||||
|
workouts[workout_id]['TopSets'].append({
|
||||||
|
'TopSetId': t['TopSetId'],
|
||||||
|
'ExerciseId': t['ExerciseId'],
|
||||||
|
'ExerciseName': t['ExerciseName'],
|
||||||
|
'Weight': t['Weight'],
|
||||||
|
'Repetitions': t['Repetitions'],
|
||||||
|
'Estimated1RM': t['Estimated1RM']
|
||||||
})
|
})
|
||||||
|
|
||||||
workouts.sort(key=lambda x: x['StartDate'], reverse=True)
|
# Convert the workouts dictionary back to a list and sort by 'StartDate'
|
||||||
|
sorted_workouts = sorted(workouts.values(), key=lambda x: x['StartDate'], reverse=True)
|
||||||
|
|
||||||
return workouts
|
return sorted_workouts
|
||||||
|
|
||||||
|
|
||||||
def get_all_exercises_from_topsets(topsets):
|
def get_all_exercises_from_topsets(topsets):
|
||||||
exercise_ids = set([t['ExerciseId']
|
exercises_dict = {}
|
||||||
for t in topsets if t['ExerciseId'] is not None])
|
for t in topsets:
|
||||||
exercises = []
|
exercise_id = t.get('ExerciseId')
|
||||||
for exercise_id in exercise_ids:
|
if exercise_id and exercise_id not in exercises_dict:
|
||||||
exercises.append({
|
exercises_dict[exercise_id] = {
|
||||||
'ExerciseId': exercise_id,
|
'ExerciseId': exercise_id,
|
||||||
'ExerciseName': next((t['ExerciseName'] for t in topsets if t['ExerciseId'] == exercise_id), 'Unknown')
|
'ExerciseName': t.get('ExerciseName', 'Unknown')
|
||||||
})
|
}
|
||||||
return exercises
|
return list(exercises_dict.values())
|
||||||
|
|
||||||
def get_topsets_for_person(person_topsets):
|
def get_topsets_for_person(person_topsets):
|
||||||
person_exercises = get_all_exercises_from_topsets(person_topsets)
|
person_exercises = get_all_exercises_from_topsets(person_topsets)
|
||||||
@@ -262,10 +272,17 @@ def get_exercise_graph_model(title, estimated_1rm, repetitions, weight, start_da
|
|||||||
|
|
||||||
relative_positions = [(date - min_date).days / total_span for date in start_dates]
|
relative_positions = [(date - min_date).days / total_span for date in start_dates]
|
||||||
|
|
||||||
best_fit_points = []
|
# Convert the slope from scaled units per day to kg per day
|
||||||
best_fit_formula = {}
|
slope_kg_per_day = (max_e1rm - min_e1rm) / total_span
|
||||||
|
|
||||||
# trry catch LinAlgError
|
best_fit_formula = {
|
||||||
|
'kg_per_week': round(slope_kg_per_day * 7, 1), # Convert to kg/week
|
||||||
|
'kg_per_month': round(slope_kg_per_day * 30, 1) # Convert to kg/month
|
||||||
|
}
|
||||||
|
|
||||||
|
best_fit_points = []
|
||||||
|
|
||||||
|
# Catch LinAlgError
|
||||||
try:
|
try:
|
||||||
# Convert relative positions and scaled estimated 1RM values to numpy arrays
|
# Convert relative positions and scaled estimated 1RM values to numpy arrays
|
||||||
x = np.array(relative_positions)
|
x = np.array(relative_positions)
|
||||||
@@ -278,13 +295,6 @@ def get_exercise_graph_model(title, estimated_1rm, repetitions, weight, start_da
|
|||||||
y_best_fit = [m * xi + b for xi in x]
|
y_best_fit = [m * xi + b for xi in x]
|
||||||
best_fit_points = list(zip(y_best_fit, relative_positions))
|
best_fit_points = list(zip(y_best_fit, relative_positions))
|
||||||
|
|
||||||
# Convert the slope from scaled units per day to kg per day
|
|
||||||
slope_kg_per_day = (max_e1rm - min_e1rm) / total_span
|
|
||||||
|
|
||||||
best_fit_formula = {
|
|
||||||
'kg_per_week': round(slope_kg_per_day * 7, 1), # Convert to kg/week
|
|
||||||
'kg_per_month': round(slope_kg_per_day * 30, 1) # Convert to kg/month
|
|
||||||
}
|
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user