Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable handling IBI data as heart rate data in openambit2gpx #211

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions tools/openambit2gpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,49 @@ def timeDiff(utcTime1,utcTime2):

return secs2-secs1

class ibiToHr(object):
def __init__(self, average_hr=False):
self.ibitimeLast = None
self.hrLast = 0
self.hrlist = []
self.average_hr = average_hr

def ibiToHr(self, element):
'''Parse a list of IBI times to heart rates, always return some hr rate'''
sampType = element.findtext("Type")

if sampType == 'ibi':
ibitime = element.findtext("Time")
if self.ibitimeLast != ibitime:
self.hrlist = [int(element.text) for element in element.findall('IBI')]
if self.average_hr:
# filter 1: average hr data, flatten data
ibi_avg = sum(self.hrlist) / len(self.hrlist)
self.hrlist = [ibi_avg]

self.ibitimeLast = ibitime

if len(self.hrlist) > 0:
# take first element of list to convert to HR
hr = 60./ (self.hrlist[0] / 1000.) # convert IBI to beats/min
del self.hrlist[0]
else:
# no data is available anymore
return self.hrLast

#filter 2: sensor errors
hrmin = 40 # Mimimum heart rate for humans
hrmax = 220 # Maximum heart rate for hiumans
if hr > hrmax or hr < hrmin:
hr = self.hrLast

self.hrLast = str(int(hr))
return self.hrLast

###########################
## getting activity data ##
###########################
ibiconvertor = ibiToHr()

for element in rootIn.iterfind("Log/Samples/Sample"):
trk=etree.Element("trkpt")
Expand All @@ -93,6 +132,9 @@ def timeDiff(utcTime1,utcTime2):

altitude=element.findtext("Altitude") if element.findtext("Altitude")!=None else altitudeLast
hr=element.findtext("HR") if element.findtext("HR")!=None else hrLast
if hr == None:
hr = ibiconvertor.ibiToHr(element)

cadence=element.findtext("Cadence") if element.findtext("Cadence")!=None else cadenceLast
power=element.findtext("BikePower") if element.findtext("BikePower")!=None else powerLast
speed=element.findtext("Speed") if element.findtext("Speed")!=None else speedLast
Expand Down