-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
273 lines (214 loc) · 10.3 KB
/
process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
Processes files downloaded from wodify and generate results.
"""
import os.path
import math
import pandas as pd
from exercises import exercises
from coaches import coaches
from utilities import clean_name
from conf_vars import CYCLE, DOWNLOADS_DIR, RESULTS_DIR, TESTING_START, TESTING_END
def lift_leaderboards(cycle, exercise, source):
"""Leaderboard for top lifters
PARAMETERS
----------
cycle : str
Name of cycle, will prefix filename
exercise : str
Name of exercise, will be included in filename
source : df
Dataframe of performance during testing period
RETURNS
-------
EXPORTS
-------
dataframes : csv
Dataframe of Athlete and Weight for the exercise,
sorted by top performance, one per gender.
"""
# reduce to rows of 1 x 1 (1RM)
lift_1r = source[source['Scheme'] == '1 x 1'][
~source['Athlete Name'].isin(coaches)]
# Get rid of lowest scores for people that may have tested more than once and sort
lift_1r = lift_1r.groupby('Athlete', group_keys=False).apply(lambda x: x.loc[x.Weight.idxmax()])
lift_1r_sort = lift_1r.sort_values('Weight', ascending=False)
users = pd.read_excel(f'{DOWNLOADS_DIR}\\{cycle}_Users.xlsx')
lift_1r_sort['Gender'] = lift_1r_sort['Athlete'].map(users.set_index('User')['Gender'])
lift_1r_male = lift_1r_sort[lift_1r_sort['Gender'] == 'Male']
lift_1r_female = lift_1r_sort[lift_1r_sort['Gender'] == 'Female']
lift_1r_male[['Athlete Name', 'Weight']].to_csv(
f'{RESULTS_DIR}\\{cycle}_leaderboard_{exercise}_male.csv', index=False)
lift_1r_female[['Athlete Name', 'Weight']].to_csv(
f'{RESULTS_DIR}\\{cycle}_leaderboard_{exercise}_female.csv', index=False)
def metcon_leaderboards(cycle):
"""Leaderboard for top metcon athletes
PARAMETERS
----------
cycle : str
Name of cycle, will prefix filename
RETURNS
-------
EXPORTS
-------
dataframes : csv
Dataframe of Athlete and performance for the metcon,
sorted by top performance, one per gender per rx.
"""
for exercise in exercises['metcon']:
f = f'{DOWNLOADS_DIR}\\{cycle}_{clean_name(exercise)}.xlsx'
if os.path.isfile(f):
source = pd.read_excel(f)
# reduce to useful columns
metcon = source[['Athlete', 'Result', 'Is As Prescribed', 'Is Rx Plus']][
~source['Athlete'].isin(coaches)]
# Get rid of lowest scores for people that may have tested more than once and sort
if clean_name(exercise) == 'CFCCindy':
metcon['Result'] = metcon['Result'].apply(lambda x: x.replace(' + ', '.'))
metcon['Result'] = metcon['Result'].apply(pd.to_numeric)
metcon = metcon.sort_values('Result', ascending=False)
metcon['Result'] = metcon['Result'].astype(str)
metcon['Result'] = metcon['Result'].apply(lambda x: x.replace('.', ' + '))
else:
metcon['Result'] = pd.to_datetime(metcon['Result'], format='%M:%S').dt.time
metcon = metcon.sort_values('Result', ascending=True) # because lower is better with time duh
metcon['duplicated'] = metcon.duplicated('Athlete', keep='first')
metcon = metcon[~metcon['duplicated']]
# Read in gender data and apply
users = pd.read_excel(f'{DOWNLOADS_DIR}\\{cycle}_Users.xlsx')
# Athlete name
users['Athlete Name'] = users['First Name'] + ' ' + users['Last Name']
# Look up gender
metcon['Gender'] = metcon['Athlete'].map(users.set_index('Athlete Name')['Gender'])
# Break out Rx and Rx+
metcon_rx = metcon[metcon['Is As Prescribed']]
metcon_rxp = metcon[metcon['Is Rx Plus']]
# Split on gender
metcon_rx_female = metcon_rx[metcon_rx['Gender'] == 'Female']
metcon_rx_male = metcon_rx[metcon_rx['Gender'] == 'Male']
metcon_rxp_female = metcon_rxp[metcon_rxp['Gender'] == 'Female']
metcon_rxp_male = metcon_rxp[metcon_rxp['Gender'] == 'Male']
# Write out
metcon_dfs = [metcon_rx_female, metcon_rx_male, metcon_rxp_female, metcon_rxp_male]
for df in metcon_dfs:
if len(df) > 0:
gender = df.Gender.unique()[0]
rx = 'rx' if df['Is As Prescribed'].unique()[0] == True else 'rxp'
df[['Athlete', 'Result']].to_csv(
f'{RESULTS_DIR}\\{cycle}_leaderboard_{clean_name(exercise)}_{gender}_{rx}.csv', index=False)
print(f'{exercise} results written to {RESULTS_DIR}')
else:
print(f'File does not exist for {exercise}.')
def weightsheets(cycle, start_date, end_date):
"""Weightsheets for all active members
PARAMETERS
----------
cycle : str
Name of cycle, will prefix filename
start_date : str
Testing start date, used to filter for leaderboard
end_date : df
Testing end date, used to filter for leaderboard
RETURNS
-------
EXPORTS
-------
dataframes : csv
Dataframe of percentsheets for each exercise by athlete.
"""
for exercise in exercises['weightlifting']:
f = f'{DOWNLOADS_DIR}\\{cycle}_{clean_name(exercise)}.xlsx'
if os.path.isfile(f):
source = pd.read_excel(f)
lift = source[['Date', 'Athlete', 'Athlete Name', 'Result']]
lift['Scheme'], lift['Weight'] = lift['Result'].str.split(' @ ', 1).str
lift['Weight'] = lift['Weight'].map(lambda x: x.rstrip(' lbs'))
lift['Weight'] = lift['Weight'].apply(pd.to_numeric)
testing_ind = (lift['Date'] >= start_date) & (lift['Date'] <= end_date)
lift_testing = lift.loc[testing_ind]
# get lift leaderboards
lift_leaderboards(cycle, exercise, lift_testing)
lift_sixmo = lift.loc[~testing_ind]
# reduce to rows of 1 x 1 (1RM) for teseters
lift_1r = lift_testing[lift_testing['Scheme'] == '1 x 1']
## stuff i will have to do for both dfs
# Get rid of lowest scores for people that may have tested more than once and sort
lift_1r = lift_1r.groupby('Athlete', group_keys=False).apply(lambda x: x.loc[x.Weight.idxmax()])
lift_sixmo = lift_sixmo.groupby('Athlete', group_keys=False).apply(lambda x: x.loc[x.Weight.idxmax()])
# combine dataframes where people in sixmo are not in lift_1r
common = lift_1r.merge(lift_sixmo, on=['Athlete'])
non_testers = lift_sixmo[(~lift_sixmo['Athlete'].isin(common['Athlete']))]
# Combine again
lift_all = pd.concat([lift_1r, non_testers])
# need to make sure everyone that is active is there
membership = pd.read_excel(f'{DOWNLOADS_DIR}\\{cycle}_AthletesAndMembershipDetails.xlsx') # assumes this is there >.<
members = membership[['Athlete', 'Athlete Name']]
members = members.drop_duplicates()
joined = pd.merge(members, lift_all, how='left')
joined = joined[['Athlete Name', 'Weight']][
~joined['Athlete Name'].isin(coaches)]
joined['Athlete Name'] = joined['Athlete Name'].str.upper()
joined_final = joined.sort_values('Athlete Name')
low_pcts = [i / 100.0 for i in range(40, 70, 5)]
high_pcts = [i / 1000.0 for i in range(675, 1025, 25)]
pcts = low_pcts + high_pcts
joined_final = joined_final.fillna(0)
for pct in pcts:
joined_final[str(round(pct*100, 1))+'%'] = joined_final['Weight'].apply(lambda x: math.ceil((x * pct)/5) * 5)
joined_final = joined_final.drop('Weight', axis=1)
if clean_name(exercise) == 'BackSquat':
frontsquat = joined_final.copy()
for col in list(frontsquat):
if col != 'Athlete Name':
frontsquat[col] = frontsquat[col].apply(lambda x: math.ceil((x * 0.8)/5) * 5)
frontsquat.to_csv(
f'{RESULTS_DIR}\\{cycle}_percentsheet_FrontSquat.csv', index=False)
joined_final.to_csv(
f'{RESULTS_DIR}\\{cycle}_percentsheet_{clean_name(exercise)}.csv', index=False)
print(f'{exercise} results written to {RESULTS_DIR}')
else:
print(f'File does not exist for {exercise}.')
def attendance_leaderboard(cycle):
"""Attendance leaderboard for cycle
PARAMETERS
----------
cycle : str
Name of cycle, will be on filenames
RETURNS
-------
EXPORTS
-------
dataframes : csv
Attendance sorted by most, one for each gender
"""
f = f'{DOWNLOADS_DIR}\\{cycle}_TotalAttendanceHistory.xlsx'
u = f'{DOWNLOADS_DIR}\\{cycle}_Users.xlsx'
if os.path.isfile(f) and os.path.isfile(u): # TODO: separate function to validate files exist
users = pd.read_excel(u)
att = pd.read_excel(f)
# add 'Class Start Date'
att['Class Start Date'] = pd.to_datetime(att['Class Start Date Time']).dt.date
# Group by user and sort based on attendance descending
att_cycle_users = pd.DataFrame(
{'Count': att.groupby(['User', 'Athlete'])
.apply(lambda x: len(x['Class Start Date'].unique()))
}).reset_index()
att_sort = att_cycle_users.sort_values('Count', ascending=False)
att_sort['Gender'] = att_sort['User'].map(users.set_index('User')['Gender'])
att_male = att_sort[att_sort['Gender'] == 'Male']
att_female = att_sort[att_sort['Gender'] == 'Female']
att_male[['Athlete', 'Count']].to_csv(
f'{RESULTS_DIR}\\{cycle}_attendance_male.csv', index=False)
att_female[['Athlete', 'Count']].to_csv(
f'{RESULTS_DIR}\\{cycle}_attendance_female.csv', index=False)
print(f'Attendance results written to {RESULTS_DIR}')
else:
print(f'File does not exist for either {f} or {u}')
def main():
"""Main function. Calculates attendance, metcon and weight leaderboards,
and weightshet percentages for current cycle.
"""
# attendance_leaderboard(CYCLE)
# metcon_leaderboards(CYCLE)
weightsheets(CYCLE, TESTING_START, TESTING_END)
if __name__ == '__main__':
main()