-
Notifications
You must be signed in to change notification settings - Fork 0
/
code
66 lines (57 loc) · 2.49 KB
/
code
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
import xml.etree.ElementTree as ET
import dateutil.parser as dp
import pandas as pd
from matplotlib import pyplot as plt
filename = 'your_filename'
tree = ET.parse(filename)
root = tree.getroot()
run = root[0][0]
#checking elements inside our tree
for x in root[0][0][1][5]:
print(x)
NAMESPACES = {
'ns': 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2',
'ns2': 'http://www.garmin.com/xmlschemas/UserProfile/v2',
'ns3': 'http://www.garmin.com/xmlschemas/ActivityExtension/v2',
'ns4': 'http://www.garmin.com/xmlschemas/ProfileExtension/v1',
'ns5': 'http://www.garmin.com/xmlschemas/ActivityGoals/v1'
}
#build our data frame with the data we want, you can choose from the available data from your
#activity and round/change the type of variable if you want to work with less decimals
data_list = []
for lap_number, lap in enumerate(run.findall('ns:Lap', NAMESPACES)):
for tp in lap.find('ns:Track', NAMESPACES).findall('ns:Trackpoint', NAMESPACES):
data = {}
data['Time'] = dp.parse(tp.find('ns:Time', NAMESPACES).text)
data['Elevation'] = round(float(tp.find('ns:AltitudeMeters', NAMESPACES).text), 4)
data['Distance'] = round(float(tp.find('ns:DistanceMeters', NAMESPACES).text), 2)
data['Heart_rate'] = int(tp.find('ns:HeartRateBpm', NAMESPACES)[0].text)
data['Speed'] = round(float(tp.find('ns:Extensions', NAMESPACES)[0][0].text), 3)
data['Lap'] = lap_number+1
data_list.append(data)
df = pd.DataFrame(data_list)
#visualize data frame
print(df)
#plot with the color codes
#(the zones will vary for every person, as it depends on your performance, check the
#last section after the plots to change the limits here)
x = df['Distance']
y = df['Heart_rate']
plt.plot(x,y, color='black', linewidth=0.4)
plt.ylim(min(y), max(y))
plt.xlim(0,max(x))
plt.ylabel('Heart rate (BPM)')
plt.xlabel('Distance (m)')
plt.title('HR coloured by zones')
plt.fill_between(x,y,0, where=(y>=(min(y)))&(y<=123), facecolor='royalblue')
plt.fill_between(x,y,0, where=(y>=124)&(y<=144), facecolor='limegreen')
plt.fill_between(x,y,0, where=(y>=145)&(y<=165), facecolor='yellow')
plt.fill_between(x,y,0, where=(y>=166)&(y<=185), facecolor='orange')
plt.fill_between(x,y,0, where=(y>=186), facecolor='red')
plt.show()
#if you want to add the elevation colormap as well, replace line 47 with:
#plt.scatter(x,y, c=z, cmap='winter_r', linewidth=5)
#and add these lines:
#z = df['Elevation']
#cbar = plt.colorbar()
#cbar.set_label('Elevation (m)', rotation=90)