From 916fc47d26876fdc78e5621b623e53dea8c35d95 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sun, 7 Apr 2024 22:09:18 +1000 Subject: [PATCH] Fix issue where sets werent displayed in order on month overview --- utils.py | 76 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/utils.py b/utils.py index b11ee63..698bf21 100644 --- a/utils.py +++ b/utils.py @@ -5,36 +5,46 @@ import numpy as np import pandas as pd def get_workouts(topsets): - # Get all unique workout_ids (No duplicates) - workout_ids = list(set([t['WorkoutId'] - for t in topsets if t['WorkoutId'] is not None])) + # Ensure all entries have 'WorkoutId' and 'TopSetId', then sort by 'WorkoutId' and 'TopSetId' + filtered_topsets = sorted( + [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 = [] - for workout_id in reversed(workout_ids): - topsets_in_workout = [ - t for t in topsets if t['WorkoutId'] == workout_id] - workouts.append({ - 'WorkoutId': workout_id, - 'StartDate': topsets_in_workout[0]['StartDate'], - '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 = {} + for t in filtered_topsets: + workout_id = t['WorkoutId'] + if workout_id not in workouts: + workouts[workout_id] = { + 'WorkoutId': workout_id, + 'StartDate': t['StartDate'], + 'TopSets': [] + } + 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): - exercise_ids = set([t['ExerciseId'] - for t in topsets if t['ExerciseId'] is not None]) - exercises = [] - for exercise_id in exercise_ids: - exercises.append({ - 'ExerciseId': exercise_id, - 'ExerciseName': next((t['ExerciseName'] for t in topsets if t['ExerciseId'] == exercise_id), 'Unknown') - }) - return exercises + exercises_dict = {} + for t in topsets: + exercise_id = t.get('ExerciseId') + if exercise_id and exercise_id not in exercises_dict: + exercises_dict[exercise_id] = { + 'ExerciseId': exercise_id, + 'ExerciseName': t.get('ExerciseName', 'Unknown') + } + return list(exercises_dict.values()) def get_topsets_for_person(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] - best_fit_points = [] - best_fit_formula = {} + # Convert the slope from scaled units per day to kg per day + 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: # Convert relative positions and scaled estimated 1RM values to numpy arrays 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] 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: pass