-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_data.py
145 lines (105 loc) · 3.99 KB
/
import_data.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
import csv
import sys
from os import listdir, mkdir, rename
from os.path import isfile, isdir, join
import shutil
def import_data():
# Iterate through all date directories
dates_path = 'Dates'
dateDirs = [ d for d in listdir(dates_path) if isdir(join(dates_path, d)) ]
for date in dateDirs:
inputFiles = [ f for f in listdir(join(dates_path, date)) if isfile(join(join(dates_path, date), f)) ]
piece_averages = []
average_headers = []
for inputFile in inputFiles:
full_path = join(join(dates_path, date), inputFile)
averages, average_headers = read_file(full_path)
piece_averages.append(averages)
write_ranking(date, piece_averages, average_headers)
def read_file(full_path):
# full_path = Dates/m_d_yy/Name-#.csv
date = full_path.split('/')[1]
file_name = full_path.split('/')[2]
if(len(file_name.split('.')[0].split('-')) > 1):
name = file_name.split('.')[0].split('-')[0]
index = file_name.split('.')[0].split('-')[1]
with open(full_path, 'rU') as f:
reader = csv.DictReader(f)
length = 0
header_dict = {}
header_list = reader.fieldnames
aggregates = ['id', 'workout_interval_id', 'ref', \
'stroke_number', 'time', 'distance', \
'estimated_500m_time', 'energy_sum']
non_aggregates = ['power', 'avg_power', 'stroke_rate', \
'stroke_length', 'distance_per_stroke', \
'energy_per_stroke', 'pulse', 'work_per_pulse', \
'peak_force', 'peak_force_pos', 'rel_peak_force_pos', \
'drive_time', 'recover_time', 'k', 'avg_calculated_power']
final_row = {}
for header in header_list:
header_dict[header] = 0
for row in reader:
for header in header_dict:
if (header not in aggregates and \
row[header] != ''):
header_dict[header] += float(row[header])
length += 1
final_row = row
for header in header_dict:
if (header in non_aggregates and \
header_dict[header] != ''):
header_dict[header] = round(header_dict[header] / length, 2)
if (header in aggregates):
header_dict[header] = final_row[header]
# Update csv file with averages for each header
write_file(date, header_dict, full_path, header_list)
# Return important data points for ranking
focus_groups = ['energy_per_stroke', 'avg_power', \
'avg_calculated_power', 'stroke_length', \
'peak_force_pos', 'stroke_rate']
return_dict = {}
return_dict['name'] = name
return_dict['index'] = index
for group in focus_groups:
return_dict[group] = header_dict[group]
# Insert headers not given from RP3 data
focus_groups.insert(0, 'name')
focus_groups.insert(1, 'index')
return return_dict, focus_groups
def write_file(date, averages, srcfile, fieldNames):
# Extract file name, rower name, and index. Date value given.
file_name = srcfile.rsplit('/', 1)[-1]
last_name = file_name.split('-')[0]
index = file_name.split('-')[1]
dstdir = 'All/'
# Create new file name from date, last name, and index
new_file_name = date + '-' + last_name + '-' + index
# Check if directory already exists. If not, make directory from last name
if(not isdir(join(dstdir, last_name))):
mkdir(join(dstdir, last_name))
# Create path to rower's directory
full_path = join(dstdir, last_name)
# Create final path and rename file
shutil.copy(srcfile, full_path)
final_path = join(full_path, new_file_name)
rename(join(full_path, file_name), final_path)
# Open file and append averages for each category
with open(final_path, 'a') as f:
writer = csv.DictWriter(f, fieldnames=fieldNames, restval="", \
dialect="excel", lineterminator='\n',)
writer.writerow(averages)
def write_ranking(date, averages, headers):
dstdir = 'Rankings/'
file_name = date + ".csv"
final_path = join(dstdir, file_name)
averages = sorted(averages, key=lambda k: k['energy_per_stroke'], reverse=True)
with open(final_path, 'w') as f:
writer = csv.DictWriter(f, fieldnames=headers, restval="", \
dialect = "excel", )
writer.writeheader()
for entry in averages:
writer.writerow(entry)
def main():
import_data()
if __name__ == "__main__": main()