-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtss.py
executable file
·175 lines (138 loc) · 5.54 KB
/
rtss.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
#!/usr/bin/env python2
"""
Copyright (c) 2018
Redistribution and use in source and binary forms, with or without
odification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Author: Mika Kahola <[email protected]>
"""
import sys
import argparse
import math
import numpy
import datetime
# requires python-fitparse library from
# https://github.com/dtcooper/python-fitparse
from fitparse import FitFile, FitParseError
from scipy.signal import lfilter
class User():
def __init__(self, ftp, threshold):
self.ftp = ftp
self.hr_ftp = threshold
def average (data):
try:
avg = sum(filter(None, data))/float(len(filter(None, data)))
except ZeroDivisionError:
print("divide by zero")
return 0
return avg
def get_hrTSS(hr, duration, threshold):
if not hr:
return 0, 0, 0, 0
hr_avg = sum(hr)/float(len(hr))
hr_min = min(hr)
hr_max = max(hr)
tss = (duration * hr_avg)/(threshold * 3600.0) * 100.0
return tss, hr_avg, hr_max, hr_min
def get_TSS(speed, vertical_speed, duration, ftp):
if not speed:
return 0, 0, 0
# convert to m/s
ftp_speed = (60/ftp)/3.6
ngp = []
for i in range(1, duration):
slope = 6 * vertical_speed[i] / speed[i];
if slope > 0:
ngp.append(speed[i] / math.cos(math.atan2(slope, 1)))
else:
ngp.append(speed[i] * math.cos(math.atan2(slope, 1)))
# 30s moving average filter
h = numpy.empty(30)
h.fill(1.0/30)
# power of 4 of 30s average power
p_30s = lfilter(h, 1.0, ngp)**4
# average of power of 4
p_avg = average(p_30s)
# normalized power
NP = math.sqrt(math.sqrt(p_avg))
IFactor = NP/ftp_speed;
# training stress score as in trainingpeaks.com
tss = (duration * NP * IFactor) / (ftp_speed * 3600) * 100.0
return tss, NP, IFactor
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--fitfile", type=str, default="", help="FIT file")
parser.add_argument("-p", "--ftp", type=float, default=4.5, help="Functional Threshold Pace")
parser.add_argument("-t", "--threshold", type=int, default=155, help="Heartrate threshold")
args = parser.parse_args()
user = User(args.ftp, args.threshold)
try:
fitfile = FitFile(args.fitfile)
fitfile.parse()
except FitParseError, e:
print "Error while parsing .FIT file: %s" % e
sys.exit(1)
speed = []
vertical_speed = []
heartrate = []
cadence = []
start = ""
stop = ""
distance = 0
# Get all data messages that are of type record
for record in fitfile.get_messages('record'):
# Go through all the data entries in this record
for data in record:
if data.name == "timestamp":
if not start:
start = data.value
else:
stop = data.value
if data.name == "speed":
speed.append(data.value)
if data.name == "vertical_speed":
vertical_speed.append(data.value)
if data.name == "heart_rate":
heartrate.append(data.value)
if data.name == "distance":
if data.value != None:
distance = data.value
duration = stop - start
print("Duration: %s" % (duration))
print("Distance: %.2f km" % (distance/1000.0))
s = [i for i in speed if i is not None]
vs = [i for i in vertical_speed if i is not None]
hr = [i for i in heartrate if i is not None]
c = [i for i in cadence if i is not None]
if hr:
hr_tss, hr_avg, hr_max, hr_min = get_hrTSS(hr, duration.total_seconds(), user.hr_ftp)
print("Heartrate: TSS: %.1f, AVG: %d, MAX: %d, MIN: %d" % (hr_tss, hr_avg, hr_max, hr_min))
if speed:
tss, NP, IFactor = get_TSS(s, vs, len(speed), user.ftp)
print("Running: TSS: %.1f" % (tss))
print(" IF: %.2f" % (IFactor))
print(" NP: %.2f min/km" % (16.667/NP))
if hr:
ng = 16.667/NP
ngs = 1000*(60/ng)/60 # meters per minute
print(" NP:HR: %.2f" % (ngs/hr_avg))
if c:
print("Cadence: AVG: %d, MAX: %d" % (average(cadence), max(c)))
if __name__ == "__main__":
main(sys.argv[1:])