-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiit.py
executable file
·107 lines (91 loc) · 2.46 KB
/
hiit.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
#!/usr/bin/env python
# import datetime
import matplotlib
import matplotlib.pyplot as plt
from fitparse import FitFile
import sys
import os
from os import path
ff = FitFile(os.path.abspath(sys.argv[1]))
mm = ff.get_messages()
ts = []
hr = []
lapmarkers = []
for m in mm:
try:
x = m.as_dict()['fields']
except KeyError:
continue
h = None
t = None
if m.as_dict()['name'] == 'lap':
valid = False
for xx in x:
if xx['name'] == 'lap_trigger':
if "manual" == xx['value']:
valid = True
# if "session_end" == xx['value']:
# valid = True
if xx['name'] == 'timestamp':
lapmarker = xx['value']
if valid:
lapmarkers.append(lapmarker)
continue
if m.as_dict()['name'] == 'event':
valid = False
for xx in x:
if xx['name'] == 'timer_trigger':
if "manual" == xx['value']:
valid = True
if xx['name'] == 'timestamp':
lapmarker = xx['value']
if valid:
lapmarkers.append(lapmarker)
continue
if m.as_dict()['name'] != 'record':
continue
for xx in x:
if xx['name'] == 'heart_rate':
h = xx['value']
if xx['name'] == 'timestamp':
t = xx['value']
if h is not None and t is not None:
hr.append(h)
ts.append(t)
print("got HR values {}".format(str(len(hr))))
print("got timestamps {}".format(str(len(ts))))
dates = matplotlib.dates.date2num(ts)
# https://plot.ly/javascript/time-series/
with open(path.splitext(path.basename(sys.argv[1]))[0] + ".html", 'w') as f:
f.write('''<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="myDiv" style="width: 100%; height: 50%;">
<!-- Plotly chart will be drawn inside this DIV -->
</div>
<script>
var data = [
{
''')
f.write("x: [")
for t in ts:
# https://stackoverflow.com/questions/311627/how-to-print-date-in-a-regular-format-in-python
f.write(t.strftime("'%Y-%m-%d %H:%M:%S', "))
f.write("],\n")
f.write("y: [")
for h in hr:
f.write("%d, " % (h,))
f.write("],\n")
f.write(''' type: 'scatter'
}
];
Plotly.newPlot('myDiv', data);
</script>
</body>
''')
plt.plot(ts, hr)
for lapmarker in lapmarkers:
plt.plot([lapmarker, lapmarker], [0, 200])
plt.gcf().autofmt_xdate()
plt.show()