├── .coveragerc ├── .github └── workflows │ ├── release.yml │ └── test.yml ├── .gitignore ├── .pre-commit-config.yaml ├── .python-version ├── AUTHORS.rst ├── CHANGES.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── dev-requirements.txt ├── maintain.sh ├── setup.cfg ├── setup.py ├── tcxparser ├── __init__.py ├── exceptions.py └── tcxparser.py ├── tests ├── __init__.py ├── files │ ├── no_hr.tcx │ ├── sup_activity_1.tcx │ ├── test.tcx │ └── test2.tcx ├── test_cycling.py ├── test_standup_paddleboarding.py └── test_tcxparser.py └── tox.ini /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | branch = true 3 | source = tcxparser 4 | omit = */tests* 5 | 6 | [report] 7 | fail_under = 99 8 | show_missing = true 9 | skip_covered = true 10 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | # This workflows will upload a Python Package using Twine when a release is created 2 | # For more information see: 3 | # https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries 4 | 5 | name: Upload Python Package 6 | 7 | on: 8 | release: 9 | types: [created] 10 | 11 | jobs: 12 | release: 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-python@v5 18 | with: 19 | python-version: '3.13' 20 | - name: Install dependencies 21 | run: | 22 | python -m pip install --upgrade pip 23 | pip install setuptools wheel twine 24 | - name: Build and publish 25 | env: 26 | TWINE_USERNAME: __token__ 27 | TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} 28 | run: | 29 | python setup.py sdist bdist_wheel 30 | twine upload dist/* 31 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: lint-test 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - master 8 | schedule: 9 | # run once a week on early monday mornings 10 | - cron: '22 2 * * 1' 11 | 12 | jobs: 13 | pre-commit: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-python@v5 18 | with: 19 | python-version: | 20 | 3.9 21 | 3.10 22 | 3.11 23 | 3.12 24 | 3.13 25 | - uses: pre-commit/action@v3.0.1 26 | 27 | tox: 28 | runs-on: ubuntu-latest 29 | strategy: 30 | matrix: 31 | # tox-gh-actions will only run the tox environments which match the currently 32 | # running python-version. See [gh-actions] in tox.ini for the mapping 33 | python-version: [ 3.9, "3.10", "3.11", "3.12", "3.13"] 34 | steps: 35 | - uses: actions/checkout@v4 36 | - name: Set up Python ${{ matrix.python-version }} 37 | uses: actions/setup-python@v5 38 | with: 39 | python-version: ${{ matrix.python-version }} 40 | - name: Cache pip 41 | uses: actions/cache@v4 42 | with: 43 | path: ~/.cache/pip 44 | key: ${{ runner.os }}-pip-${{ hashFiles('dev-requirements.txt') }} 45 | restore-keys: | 46 | ${{ runner.os }}-pip- 47 | ${{ runner.os }}- 48 | - name: Install dependencies 49 | run: | 50 | python -m pip install --upgrade pip 51 | pip install -r dev-requirements.txt 52 | - name: Test with tox 53 | run: tox 54 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | 21 | # Installer logs 22 | pip-log.txt 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | 29 | # Translations 30 | *.mo 31 | 32 | # Mr Developer 33 | .mr.developer.cfg 34 | .project 35 | .pydevproject 36 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pre-commit/pre-commit-hooks 3 | rev: v3.3.0 4 | hooks: 5 | - id: check-added-large-files 6 | - id: check-case-conflict 7 | - id: check-json 8 | - id: check-merge-conflict 9 | - id: check-symlinks 10 | - id: check-yaml 11 | - id: debug-statements 12 | - id: detect-private-key 13 | - id: end-of-file-fixer 14 | - id: mixed-line-ending 15 | - id: trailing-whitespace 16 | 17 | - repo: https://github.com/psf/black 18 | rev: 25.1.0 19 | hooks: 20 | - id: black 21 | 22 | - repo: https://github.com/pycqa/isort 23 | rev: 5.13.2 24 | hooks: 25 | - id: isort 26 | 27 | - repo: https://github.com/pycqa/flake8 28 | rev: 7.1.2 29 | hooks: 30 | - id: flake8 31 | -------------------------------------------------------------------------------- /.python-version: -------------------------------------------------------------------------------- 1 | 3.9.21 2 | 3.10.16 3 | 3.11.11 4 | 3.12.9 5 | 3.13.2 6 | -------------------------------------------------------------------------------- /AUTHORS.rst: -------------------------------------------------------------------------------- 1 | Primary author: 2 | --------------- 3 | 4 | * Vinod Kurup (@vkurup) 5 | 6 | 7 | Other contributions from: 8 | ------------------------- 9 | 10 | * Iztok Fister Jr (@firefly-cpp) 11 | * Jason M. (@DaddyTheRunner) 12 | * Adam Neumann (@noizwaves) 13 | * Stephen Doyle (@stevedoyle) 14 | * Loïs Taulelle (@ltaulell) 15 | * Jan Pipek (@janpipek) 16 | * Kees Hink (@khink) 17 | * @terra-alex 18 | 19 | Thank you! 20 | -------------------------------------------------------------------------------- /CHANGES.rst: -------------------------------------------------------------------------------- 1 | CHANGES 2 | ======= 3 | 4 | v2.4.0, 2025-03-15 5 | ---------------- 6 | 7 | * Python updates 8 | 9 | 10 | v2.3.0, 2022-07-31 11 | ---------------- 12 | 13 | * Add support for steps. Thanks @firefly-cpp (#100) 14 | 15 | 16 | v2.2.0, 2022-01-23 17 | ---------------- 18 | 19 | * Minor release and test improvements. Thanks @firefly-cpp (#76, #77) 20 | * Add support for max and avg power. Thanks @firefly-cpp (#78) 21 | 22 | 23 | v2.1.0, 2021-12-30 24 | ---------------- 25 | 26 | * Calculate time in HR zones. Thanks @khink (#41) 27 | * Don't throw exceptions when certain data is missing. Thanks @terra-alex (#74) 28 | * Minor updates to dependencies. Thanks pyup-bot 29 | 30 | 31 | v2.0.0, 2020-12-20 32 | ------------------ 33 | 34 | * Add command to extract the percentage of workout spent in each HR zone. Thanks @khink (#29) 35 | * Drops support for Python 2.7 36 | 37 | 38 | v1.1.0, 2020-01-10 39 | ------------------ 40 | 41 | * Add distance_values, position_values and started_at attributes. Thanks @janpipek (#23) 42 | 43 | 44 | v1.0.0, 2018-12-15 45 | ------------------ 46 | 47 | * Add support for Notes field 48 | 49 | 50 | v0.8.0, 2018-04-27 51 | ------------------ 52 | 53 | * Add coverage, flake8 54 | * Support Python 3 55 | * Move code into its own package 56 | * Add some cycling support (#16) 57 | * Run tests on CI using tox-travis 58 | 59 | 60 | v0.7.2, 2017-03-02 61 | ------------------ 62 | 63 | * Don't fail if lat/lon not present. 64 | 65 | 66 | v0.7.1, 2016-08-14 67 | ------------------ 68 | 69 | * Fix for tracks with only 1 trackpoint. 70 | 71 | 72 | v0.7.0, 2016-01-01 73 | ------------------ 74 | 75 | * Added average altitude support. Thanks @firefly-cpp 76 | 77 | 78 | v0.6.0, 2014-11-18 79 | ------------------ 80 | 81 | * Added heart rate data & pace support. Thanks @stevedoyle 82 | 83 | 84 | v0.3.0, 2013-01-09 85 | ------------------ 86 | 87 | * Changed methods to properties 88 | 89 | 90 | v0.1.0, 2013-01-07 91 | ------------------ 92 | 93 | * Initial release. 94 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013-2025, Vinod Kurup 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 21 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include *.txt 2 | include *.md 3 | include *.rst 4 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | python-tcxparser 2 | ================ 3 | 4 | .. sidebar:: Build Status 5 | 6 | :master: |master-status| 7 | 8 | .. image:: https://pyup.io/repos/github/vkurup/python-tcxparser/shield.svg 9 | :target: https://pyup.io/repos/github/vkurup/python-tcxparser/ 10 | :alt: Requirement Updates 11 | 12 | .. image:: https://img.shields.io/fedora/v/python3-tcxparser?color=blue&label=Fedora%20Linux&logo=fedora 13 | :target: https://src.fedoraproject.org/rpms/python-tcxparser 14 | :alt: Fedora package 15 | 16 | .. |master-status| image:: 17 | https://github.com/vkurup/python-tcxparser/workflows/lint-test/badge.svg?branch=master 18 | :alt: Build Status 19 | :target: https://github.com/vkurup/python-tcxparser/actions?query=branch%3Amaster 20 | 21 | python-tcxparser is a minimal parser for Garmin's TCX file format. It 22 | is not in any way exhaustive. 23 | 24 | Data extracted: 25 | - latitude & longitude of start point of workout 26 | - type of workout (running, walking, etc) 27 | - time of completion of workout (in ISO UTC) 28 | - distance of workout (in meters) 29 | - duration of workout (in seconds) 30 | - calories burned during workout (as estimated by device) 31 | - average, max and min heart rate during workout 32 | - average pace during workout 33 | - average altitude during workout 34 | - ascent and descent of workout 35 | - max and min altitude 36 | - time stamp of each data point (in ISO UTC) 37 | - average and max cadence (cycling activities) 38 | - percentage and time spent in heart rate zone 39 | - average and max power 40 | - total steps (also strokes) 41 | 42 | Installation 43 | ------------ 44 | 45 | Install it from PyPI:: 46 | 47 | pip install python-tcxparser 48 | 49 | Usage 50 | ----- 51 | 52 | Basic usage example:: 53 | 54 | >>> import tcxparser 55 | >>> tcx = tcxparser.TCXParser('/home/vinod/Downloads/20121226-212953.tcx') 56 | >>> # Duration of workout in seconds 57 | ... tcx.duration 58 | 1992.78 59 | >>> # latitude/longitude at start of workout 60 | ... tcx.latitude 61 | 35.951880198 62 | >>> tcx.longitude 63 | -79.0931872185 64 | >>> tcx.activity_type 65 | 'running' 66 | >>> # ISO UTC timestamp when workout completed 67 | ... tcx.completed_at 68 | '2012-12-26T22:03:05Z' 69 | >>> # distance of workout in meters 70 | ... tcx.distance 71 | 4686.31103516 72 | >>> tcx.distance_units 73 | 'meters' 74 | >>> # calories burned (as reported by device) 75 | ... tcx.calories 76 | 379 77 | >>> # percentage of workout spent in each user-defined heart rate zone 78 | ... tcx.hr_percent_in_zones({"Z0": (0, 99), "Z1": (100, 129), "Z2": (130, 200)}) 79 | {"Z0": 14, "Z1": 36, "Z2": 50} 80 | 81 | Compatibility 82 | ------------- 83 | 84 | * Python 3.7+, see `tox.ini`_. 85 | 86 | .. _tox.ini: tox.ini 87 | 88 | License 89 | ------- 90 | 91 | * BSD 92 | 93 | 94 | Maintainer Information 95 | ---------------------- 96 | 97 | We use Github Actions to lint (using pre-commit, black, isort, and flake8), 98 | test (using tox and tox-gh-actions), and calculate coverage (using coverage). 99 | 100 | We have a local script to do these actions locally, named ``maintain.sh``:: 101 | 102 | $ ./maintain.sh 103 | 104 | A Github Action workflow also builds and pushes a new package to PyPI whenever a new 105 | Release is created in Github. This uses a project-specific PyPI token, as described in 106 | the `PyPI documentation here `_. That token has been 107 | saved in the ``PYPI_PASSWORD`` settings for this repo, but has not been saved anywhere 108 | else so if it is needed for any reason, the current one should be deleted and a new one 109 | generated. 110 | 111 | As always, be sure to bump the version in ``setup.py`` before creating a Release, so 112 | that the proper version gets pushed to PyPI. 113 | 114 | 115 | Contact 116 | ------- 117 | 118 | Please contact me with any questions: Vinod Kurup (vinod@kurup.com) 119 | -------------------------------------------------------------------------------- /dev-requirements.txt: -------------------------------------------------------------------------------- 1 | . # <- install ourselves 2 | coverage==7.6.12 3 | flake8==7.1.2 4 | pre-commit==4.1.0 5 | setuptools==76.0.0 6 | tox==4.24.2 7 | tox-gh-actions==3.3.0 8 | -------------------------------------------------------------------------------- /maintain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -ex 3 | 4 | pip install -Ur dev-requirements.txt 5 | pre-commit install 6 | pre-commit run -a 7 | tox 8 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description_file = README.rst 3 | 4 | [flake8] 5 | exclude = .tox 6 | max-line-length = 120 7 | 8 | [isort] 9 | profile = black 10 | multi_line_output = 3 11 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import find_packages, setup 2 | 3 | __version__ = "2.4.0" 4 | 5 | setup( 6 | name="python-tcxparser", 7 | version=__version__, 8 | description="Simple parser for Garmin TCX files", 9 | long_description=open("README.rst").read(), 10 | author="Vinod Kurup", 11 | author_email="vinod@kurup.com", 12 | url="https://github.com/vkurup/python-tcxparser/", 13 | packages=find_packages(include=["tcxparser"]), 14 | include_package_data=True, 15 | license="BSD", 16 | zip_safe=False, 17 | keywords="tcx", 18 | classifiers=[ 19 | "Development Status :: 5 - Production/Stable", 20 | "Intended Audience :: Developers", 21 | "License :: OSI Approved :: BSD License", 22 | "Operating System :: OS Independent", 23 | "Programming Language :: Python", 24 | "Programming Language :: Python :: 3", 25 | "Programming Language :: Python :: 3.9", 26 | "Programming Language :: Python :: 3.10", 27 | "Programming Language :: Python :: 3.11", 28 | "Programming Language :: Python :: 3.12", 29 | "Programming Language :: Python :: 3.13", 30 | "Topic :: Software Development :: Libraries :: Python Modules", 31 | ], 32 | install_requires=[ 33 | "lxml", 34 | "python-dateutil", 35 | ], 36 | test_suite="tests", 37 | ) 38 | -------------------------------------------------------------------------------- /tcxparser/__init__.py: -------------------------------------------------------------------------------- 1 | from .tcxparser import TCXParser # noqa 2 | -------------------------------------------------------------------------------- /tcxparser/exceptions.py: -------------------------------------------------------------------------------- 1 | class TcxParserException(Exception): 2 | """A generic error to raise when parsing a tcx file fails""" 3 | 4 | pass 5 | 6 | 7 | class NoHeartRateDataError(Exception): 8 | def __init__(self): 9 | self.message = "The tcx file contains no heart rate data." 10 | super().__init__(self.message) 11 | -------------------------------------------------------------------------------- /tcxparser/tcxparser.py: -------------------------------------------------------------------------------- 1 | "Simple parser for Garmin TCX files." 2 | 3 | import time 4 | from datetime import timedelta 5 | 6 | from dateutil.parser import isoparse 7 | from lxml import objectify 8 | 9 | from .exceptions import NoHeartRateDataError 10 | 11 | namespace = "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" 12 | namespace2 = "http://www.garmin.com/xmlschemas/ActivityExtension/v2" 13 | 14 | 15 | class TCXParser: 16 | def __init__(self, tcx_file): 17 | tree = objectify.parse(tcx_file) 18 | self.root = tree.getroot() 19 | self.activity = self.root.Activities.Activity 20 | 21 | def hr_values(self): 22 | return [ 23 | int(x.text) 24 | for x in self.root.xpath( 25 | "//ns:HeartRateBpm/ns:Value", namespaces={"ns": namespace} 26 | ) 27 | ] 28 | 29 | def altitude_points(self): 30 | return [ 31 | float(x.text) 32 | for x in self.root.xpath( 33 | "//ns:AltitudeMeters", namespaces={"ns": namespace} 34 | ) 35 | ] 36 | 37 | def position_values(self): 38 | return [ 39 | (float(pos.LatitudeDegrees.text), float(pos.LongitudeDegrees.text)) 40 | for pos in self.root.xpath( 41 | "//ns:Trackpoint/ns:Position", namespaces={"ns": namespace} 42 | ) 43 | ] 44 | 45 | def distance_values(self): 46 | return self.root.findall( 47 | ".//ns:Trackpoint/ns:DistanceMeters", namespaces={"ns": namespace} 48 | ) 49 | 50 | def time_values(self): 51 | return [ 52 | x.text for x in self.root.xpath("//ns:Time", namespaces={"ns": namespace}) 53 | ] 54 | 55 | def time_objects(self): 56 | """Return a list of timezone-aware datetime objects for each string in time_values().""" 57 | return [isoparse(x) for x in self.time_values()] 58 | 59 | def time_durations(self): 60 | """Return a list of timedelta objects for each string in time_values(). 61 | 62 | This can be used to calculate the total time in each HR zone, as opposed to the 63 | total amount of trackpoints in each HR zone. 64 | """ 65 | time_objects = self.time_objects() 66 | last_index = len(time_objects) - 1 67 | 68 | durations = [] 69 | for idx, dt in enumerate(time_objects): 70 | if idx > 0: 71 | # Add half the time elapsed since previous trackpoint 72 | duration = (dt - time_objects[idx - 1]) / 2 73 | else: 74 | duration = timedelta(0) 75 | if idx < last_index: 76 | # Add half the time to next trackpoint 77 | duration += (time_objects[idx + 1] - dt) / 2 78 | durations.append(duration) 79 | return durations 80 | 81 | def cadence_values(self): 82 | return [ 83 | int(x.text) 84 | for x in self.root.xpath("//ns:Cadence", namespaces={"ns": namespace}) 85 | ] 86 | 87 | def power_values(self): 88 | return [ 89 | int(x.text) 90 | for x in self.root.xpath("//ns:TPX/ns:Watts", namespaces={"ns": namespace2}) 91 | ] 92 | 93 | def steps_values(self): 94 | return [ 95 | int(x.text) 96 | for x in self.root.xpath("//ns:Steps", namespaces={"ns": namespace2}) 97 | ] 98 | 99 | @property 100 | def latitude(self): 101 | if hasattr(self.activity.Lap.Track.Trackpoint, "Position"): 102 | return self.activity.Lap.Track.Trackpoint.Position.LatitudeDegrees.pyval 103 | 104 | @property 105 | def longitude(self): 106 | if hasattr(self.activity.Lap.Track.Trackpoint, "Position"): 107 | return self.activity.Lap.Track.Trackpoint.Position.LongitudeDegrees.pyval 108 | 109 | @property 110 | def activity_type(self): 111 | return self.activity.attrib["Sport"].lower() 112 | 113 | @property 114 | def started_at(self): 115 | return self.activity.Lap[0].attrib["StartTime"] 116 | 117 | @property 118 | def completed_at(self): 119 | return self.activity.Lap[-1].Track.Trackpoint[-1].Time.pyval 120 | 121 | @property 122 | def cadence_avg(self): 123 | return ( 124 | self.activity.Lap[-1].Cadence 125 | if hasattr(self.activity.Lap[-1], "Cadence") 126 | else None 127 | ) 128 | 129 | @property 130 | def distance(self): 131 | distance_values = self.distance_values() 132 | return distance_values[-1] if distance_values else 0 133 | 134 | @property 135 | def distance_units(self): 136 | return "meters" 137 | 138 | @property 139 | def duration(self): 140 | """Returns duration of workout in seconds.""" 141 | return sum(lap.TotalTimeSeconds for lap in self.activity.Lap) 142 | 143 | @property 144 | def calories(self): 145 | return sum(lap.Calories for lap in self.activity.Lap) 146 | 147 | @property 148 | def hr_avg(self): 149 | """Average heart rate of the workout""" 150 | hr_data = self.hr_values() 151 | return int(sum(hr_data) / len(hr_data)) if hr_data else None 152 | 153 | @property 154 | def hr_max(self): 155 | """Maximum heart rate of the workout""" 156 | return max(self.hr_values()) if self.hr_values() else None 157 | 158 | @property 159 | def hr_min(self): 160 | """Minimum heart rate of the workout""" 161 | return min(self.hr_values()) if self.hr_values() else None 162 | 163 | def hr_percent_in_zones(self, zones): 164 | """Percentage of workout spent in each heart rate zone. 165 | 166 | Given these user's heart rate zones: 167 | zones = { 168 | "Z0": (0, 119), 169 | "Z1": (120, 199), 170 | "Z2": (200, 240), 171 | } 172 | 173 | Then `self.hr_percent_in_zones(zones)` would return something like: 174 | { 175 | "Z0": 5, 176 | "Z1": 95, 177 | "Z2": 0, 178 | } 179 | 180 | Correct calculation relies on evenly spaced measurement times. 181 | """ 182 | hr_values = self.hr_values() 183 | if not hr_values: 184 | raise NoHeartRateDataError 185 | 186 | # Initialize a dictionary with one item for each zone 187 | per_zone = dict.fromkeys(zones.keys(), 0) 188 | 189 | # count number of HR measurements per zone 190 | for hr in hr_values: 191 | for zone_name, zone_boundaries in zones.items(): 192 | if hr >= zone_boundaries[0] and hr <= zone_boundaries[1]: 193 | per_zone[zone_name] += 1 194 | 195 | # convert counts to percentages 196 | nr_hr_values = len(hr_values) 197 | for name, count in per_zone.items(): 198 | per_zone[name] = round(100 * count / nr_hr_values) 199 | return per_zone 200 | 201 | def hr_time_in_zones(self, zones): 202 | """Time spent in each heart rate zone. 203 | 204 | Given these user's heart rate zones: 205 | zones = { 206 | "Z0": (0, 119), 207 | "Z1": (120, 199), 208 | "Z2": (200, 240), 209 | } 210 | 211 | Then `self.hr_time_in_zones(zones)` would return something like: 212 | { 213 | "Z0": timedelta(seconds=3600), 214 | "Z1": timedelta(seconds=1800), 215 | "Z2": timedelta(seconds=300), 216 | } 217 | """ 218 | hr_values = self.hr_values() 219 | if not hr_values: 220 | raise NoHeartRateDataError 221 | 222 | # Initialize a dictionary with time=0 for each zone 223 | per_zone = dict.fromkeys(zones.keys(), timedelta(0)) 224 | 225 | # count number of HR measurements per zone 226 | for hr, td in zip(hr_values, self.time_durations()): 227 | for zone_name, zone_boundaries in zones.items(): 228 | if hr >= zone_boundaries[0] and hr <= zone_boundaries[1]: 229 | per_zone[zone_name] += td 230 | 231 | return per_zone 232 | 233 | @property 234 | def pace(self): 235 | """Average pace (mm:ss/km for the workout""" 236 | secs_per_km = self.duration / (self.distance / 1000) 237 | return time.strftime("%M:%S", time.gmtime(secs_per_km)) 238 | 239 | @property 240 | def altitude_avg(self): 241 | """Average altitude for the workout""" 242 | altitude_data = self.altitude_points() 243 | return sum(altitude_data) / len(altitude_data) 244 | 245 | @property 246 | def altitude_max(self): 247 | """Max altitude for the workout""" 248 | altitude_data = self.altitude_points() 249 | return max(altitude_data) 250 | 251 | @property 252 | def altitude_min(self): 253 | """Min altitude for the workout""" 254 | altitude_data = self.altitude_points() 255 | return min(altitude_data) 256 | 257 | @property 258 | def ascent(self): 259 | """Returns ascent of workout in meters""" 260 | total_ascent = 0.0 261 | altitude_data = self.altitude_points() 262 | for i in range(len(altitude_data) - 1): 263 | diff = altitude_data[i + 1] - altitude_data[i] 264 | if diff > 0.0: 265 | total_ascent += diff 266 | return total_ascent 267 | 268 | @property 269 | def descent(self): 270 | """Returns descent of workout in meters""" 271 | total_descent = 0.0 272 | altitude_data = self.altitude_points() 273 | for i in range(len(altitude_data) - 1): 274 | diff = altitude_data[i + 1] - altitude_data[i] 275 | if diff < 0.0: 276 | total_descent += abs(diff) 277 | return total_descent 278 | 279 | @property 280 | def cadence_max(self): 281 | """Returns max cadence of workout""" 282 | cadence_data = self.cadence_values() 283 | return max(cadence_data) if cadence_data else None 284 | 285 | @property 286 | def activity_notes(self): 287 | """Return contents of Activity/Notes field if it exists.""" 288 | return getattr(self.activity, "Notes", "") 289 | 290 | @property 291 | def power_max(self): 292 | """Returns max power (in watts) of workout""" 293 | power_data = self.power_values() 294 | return max(power_data) if power_data else None 295 | 296 | @property 297 | def power_avg(self): 298 | """Returns avg power (in watts) of workout""" 299 | power_data = self.power_values() 300 | return int((sum(power_data) / len(power_data))) if power_data else None 301 | 302 | @property 303 | def total_steps(self): 304 | """Returns total steps (strokes) of workout""" 305 | step_data = self.steps_values() 306 | return sum(step_data) 307 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vkurup/python-tcxparser/5ecaac92f8f2def592ed495b7877a97121dc59c4/tests/__init__.py -------------------------------------------------------------------------------- /tests/files/sup_activity_1.tcx: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 2022-07-16T16:08:25.000Z 12 | 13 | 966.796 14 | 1000.0 15 | 1.4110000133514404 16 | 80 17 | 18 | 88 19 | 20 | 21 | 108 22 | 23 | Active 24 | Manual 25 | 26 | 27 | 28 | 29 | 46.63692391477525 30 | 16.16836273111403 31 | 32 | 0.75 33 | 34 | 96 35 | 36 | 37 | 38 | 0.2680000066757202 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46.63699457421899 46 | 16.168359126895666 47 | 48 | 8.609999656677246 49 | 50 | 92 51 | 52 | 53 | 54 | 0.0 55 | 56 | 57 | 58 | 59 | 60 | 61 | 46.637007063254714 62 | 16.168354768306017 63 | 64 | 10.0 65 | 66 | 89 67 | 68 | 69 | 70 | 0.6439999938011169 71 | 72 | 73 | 74 | 75 | 76 | 77 | 46.6370128467679 78 | 16.168352086097002 79 | 80 | 10.670000076293945 81 | 82 | 86 83 | 84 | 85 | 86 | 0.6340000033378601 87 | 88 | 89 | 90 | 91 | 92 | 93 | 46.63705098442733 94 | 16.16833691485226 95 | 96 | 15.079999923706055 97 | 98 | 89 99 | 100 | 101 | 102 | 0.6140000224113464 103 | 104 | 105 | 106 | 107 | 108 | 109 | 46.63705718703568 110 | 16.168339429423213 111 | 112 | 15.739999771118164 113 | 114 | 93 115 | 116 | 117 | 118 | 0.0 119 | 120 | 121 | 122 | 123 | 124 | 125 | 46.63706154562533 126 | 16.168339429423213 127 | 128 | 16.219999313354492 129 | 130 | 96 131 | 132 | 133 | 134 | 0.5910000205039978 135 | 136 | 137 | 138 | 139 | 140 | 141 | 46.63709004409611 142 | 16.168337082490325 143 | 144 | 19.389999389648438 145 | 146 | 91 147 | 148 | 149 | 150 | 0.5180000066757202 151 | 152 | 153 | 154 | 155 | 156 | 157 | 46.63709490559995 158 | 16.168336663395166 159 | 160 | 19.93000030517578 161 | 162 | 91 163 | 164 | 165 | 166 | 0.49799999594688416 167 | 168 | 169 | 170 | 171 | 172 | 173 | 46.63705475628376 174 | 16.168371196836233 175 | 176 | 23.010000228881836 177 | 178 | 94 179 | 180 | 181 | 182 | 0.0 183 | 184 | 185 | 186 | 187 | 188 | 189 | 46.63705375045538 190 | 16.16836281493306 191 | 192 | 23.530000686645508 193 | 194 | 97 195 | 196 | 197 | 198 | 0.0 199 | 200 | 201 | 202 | 203 | 204 | 205 | 46.6371547523886 206 | 16.16833155043423 207 | 208 | 40.5099983215332 209 | 210 | 100 211 | 212 | 213 | 214 | 0.9089999794960022 215 | 216 | 217 | 218 | 219 | 220 | 221 | 46.63720764219761 222 | 16.16834370419383 223 | 224 | 46.459999084472656 225 | 226 | 103 227 | 228 | 229 | 230 | 1.1690000295639038 231 | 232 | 233 | 234 | 235 | 236 | 237 | 46.63725986145437 238 | 16.16837446577847 239 | 240 | 52.77000045776367 241 | 242 | 105 243 | 244 | 245 | 246 | 1.184999942779541 247 | 248 | 249 | 250 | 251 | 252 | 253 | 46.63757518865168 254 | 16.168481670320034 255 | 256 | 89.20999908447266 257 | 258 | 102 259 | 260 | 261 | 262 | 1.2339999675750732 263 | 264 | 265 | 266 | 267 | 268 | 269 | 46.63762154057622 270 | 16.168508743867278 271 | 272 | 94.8499984741211 273 | 274 | 99 275 | 276 | 277 | 278 | 0.9670000076293945 279 | 280 | 281 | 282 | 283 | 284 | 285 | 46.6378279030323 286 | 16.168646374717355 287 | 288 | 120.66000366210938 289 | 290 | 96 291 | 292 | 293 | 294 | 0.9660000205039978 295 | 296 | 297 | 298 | 299 | 300 | 301 | 46.63803116418421 302 | 16.16921877488494 303 | 304 | 171.14999389648438 305 | 306 | 100 307 | 308 | 309 | 310 | 1.0750000476837158 311 | 312 | 313 | 314 | 315 | 316 | 317 | 46.63803242146969 318 | 16.169230258092284 319 | 320 | 172.02000427246094 321 | 322 | 103 323 | 324 | 325 | 326 | 1.065000057220459 327 | 328 | 329 | 330 | 331 | 332 | 333 | 46.63802940398455 334 | 16.169246016070247 335 | 336 | 173.25999450683594 337 | 338 | 103 339 | 340 | 341 | 342 | 1.0829999446868896 343 | 344 | 345 | 346 | 347 | 348 | 349 | 46.63801691494882 350 | 16.16934877820313 351 | 352 | 181.27999877929688 353 | 354 | 100 355 | 356 | 357 | 358 | 1.0490000247955322 359 | 360 | 361 | 362 | 363 | 364 | 365 | 46.638010796159506 366 | 16.169397393241525 367 | 368 | 185.05999755859375 369 | 370 | 97 371 | 372 | 373 | 374 | 0.9860000014305115 375 | 376 | 377 | 378 | 379 | 380 | 381 | 46.638008281588554 382 | 16.169452546164393 383 | 384 | 189.32000732421875 385 | 386 | 94 387 | 388 | 389 | 390 | 0.921999990940094 391 | 392 | 393 | 394 | 395 | 396 | 397 | 46.63801230490208 398 | 16.169494288042188 399 | 400 | 192.5399932861328 401 | 402 | 91 403 | 404 | 405 | 406 | 0.8629999756813049 407 | 408 | 409 | 410 | 411 | 412 | 413 | 46.638005934655666 414 | 16.169966524466872 415 | 416 | 229.6300048828125 417 | 418 | 90 419 | 420 | 421 | 422 | 0.9509999752044678 423 | 424 | 425 | 426 | 427 | 428 | 429 | 46.63802278228104 430 | 16.170248156413436 431 | 432 | 251.33999633789062 433 | 434 | 84 435 | 436 | 437 | 438 | 1.13100004196167 439 | 440 | 441 | 442 | 443 | 444 | 445 | 46.63801523856819 446 | 16.170287551358342 447 | 448 | 254.47000122070312 449 | 450 | 91 451 | 452 | 453 | 454 | 1.0989999771118164 455 | 456 | 457 | 458 | 459 | 460 | 461 | 46.638010293245316 462 | 16.17029911838472 463 | 464 | 255.47000122070312 465 | 466 | 94 467 | 468 | 469 | 470 | 1.097000002861023 471 | 472 | 473 | 474 | 475 | 476 | 477 | 46.638006856665015 478 | 16.17031085304916 479 | 480 | 256.44000244140625 481 | 482 | 99 483 | 484 | 485 | 486 | 1.0800000429153442 487 | 488 | 489 | 490 | 491 | 492 | 493 | 46.638007778674364 494 | 16.1703207436949 495 | 496 | 257.1700134277344 497 | 498 | 103 499 | 500 | 501 | 502 | 1.059999942779541 503 | 504 | 505 | 506 | 507 | 508 | 509 | 46.638005431741476 510 | 16.17033583112061 511 | 512 | 258.3500061035156 513 | 514 | 106 515 | 516 | 517 | 518 | 1.062999963760376 519 | 520 | 521 | 522 | 523 | 524 | 525 | 46.637998558580875 526 | 16.17038805037737 527 | 528 | 262.42999267578125 529 | 530 | 103 531 | 532 | 533 | 534 | 1.0329999923706055 535 | 536 | 537 | 538 | 539 | 540 | 541 | 46.63799629546702 542 | 16.17040137760341 543 | 544 | 263.4800109863281 545 | 546 | 103 547 | 548 | 549 | 550 | 1.0230000019073486 551 | 552 | 553 | 554 | 555 | 556 | 557 | 46.63799579255283 558 | 16.170439934358 559 | 560 | 266.45001220703125 561 | 562 | 100 563 | 564 | 565 | 566 | 1.0089999437332153 567 | 568 | 569 | 570 | 571 | 572 | 573 | 46.63799763657153 574 | 16.17048586718738 575 | 576 | 269.9800109863281 577 | 578 | 97 579 | 580 | 581 | 582 | 0.9750000238418579 583 | 584 | 585 | 586 | 587 | 588 | 589 | 46.63800367154181 590 | 16.170509168878198 591 | 592 | 271.8399963378906 593 | 594 | 92 595 | 596 | 597 | 598 | 0.9570000171661377 599 | 600 | 601 | 602 | 603 | 604 | 605 | 46.638006856665015 606 | 16.170523837208748 607 | 608 | 273.010009765625 609 | 610 | 88 611 | 612 | 613 | 614 | 0.9639999866485596 615 | 616 | 617 | 618 | 619 | 620 | 621 | 46.638010293245316 622 | 16.170536912977695 623 | 624 | 274.07000732421875 625 | 626 | 88 627 | 628 | 629 | 630 | 0.9789999723434448 631 | 632 | 633 | 634 | 635 | 636 | 637 | 46.63803292438388 638 | 16.17064956575632 639 | 640 | 283.05999755859375 641 | 642 | 85 643 | 644 | 645 | 646 | 1.1009999513626099 647 | 648 | 649 | 650 | 651 | 652 | 653 | 46.63802077062428 654 | 16.17078778333962 655 | 656 | 293.760009765625 657 | 658 | 82 659 | 660 | 661 | 662 | 1.1859999895095825 663 | 664 | 665 | 666 | 667 | 668 | 669 | 46.637990260496736 670 | 16.170980902388692 671 | 672 | 308.95001220703125 673 | 674 | 85 675 | 676 | 677 | 678 | 1.1710000038146973 679 | 680 | 681 | 682 | 683 | 684 | 685 | 46.638022027909756 686 | 16.171511225402355 687 | 688 | 351.0199890136719 689 | 690 | 81 691 | 692 | 693 | 694 | 1.0920000076293945 695 | 696 | 697 | 698 | 699 | 700 | 701 | 46.63800023496151 702 | 16.17157844826579 703 | 704 | 356.7699890136719 705 | 706 | 84 707 | 708 | 709 | 710 | 0.906000018119812 711 | 712 | 713 | 714 | 715 | 716 | 717 | 46.63799009285867 718 | 16.171606024727225 719 | 720 | 359.1700134277344 721 | 722 | 87 723 | 724 | 725 | 726 | 0.8519999980926514 727 | 728 | 729 | 730 | 731 | 732 | 733 | 46.63798456080258 734 | 16.17175865918398 735 | 736 | 371.05999755859375 737 | 738 | 90 739 | 740 | 741 | 742 | 0.7649999856948853 743 | 744 | 745 | 746 | 747 | 748 | 749 | 46.63807793520391 750 | 16.171967117115855 751 | 752 | 390.0899963378906 753 | 754 | 90 755 | 756 | 757 | 758 | 0.9129999876022339 759 | 760 | 761 | 762 | 763 | 764 | 765 | 46.63810132071376 766 | 16.17261135019362 767 | 768 | 440.4800109863281 769 | 770 | 88 771 | 772 | 773 | 774 | 1.0420000553131104 775 | 776 | 777 | 778 | 779 | 780 | 781 | 46.63822688162327 782 | 16.17295584641397 783 | 784 | 474.25 785 | 786 | 86 787 | 788 | 789 | 790 | 0.9100000262260437 791 | 792 | 793 | 794 | 795 | 796 | 797 | 46.638283878564835 798 | 16.172961462289095 799 | 800 | 480.5299987792969 801 | 802 | 88 803 | 804 | 805 | 806 | 1.0169999599456787 807 | 808 | 809 | 810 | 811 | 812 | 813 | 46.6383149754256 814 | 16.17295299656689 815 | 816 | 484.0 817 | 818 | 89 819 | 820 | 821 | 822 | 1.0820000171661377 823 | 824 | 825 | 826 | 827 | 828 | 829 | 46.63833643309772 830 | 16.172942351549864 831 | 832 | 486.4800109863281 833 | 834 | 90 835 | 836 | 837 | 838 | 1.1319999694824219 839 | 840 | 841 | 842 | 843 | 844 | 845 | 46.63839996792376 846 | 16.172900777310133 847 | 848 | 494.1700134277344 849 | 850 | 90 851 | 852 | 853 | 854 | 1.2410000562667847 855 | 856 | 857 | 858 | 859 | 860 | 861 | 46.63846056908369 862 | 16.17284294217825 863 | 864 | 502.1400146484375 865 | 866 | 89 867 | 868 | 869 | 870 | 1.3220000267028809 871 | 872 | 873 | 874 | 875 | 876 | 877 | 46.638511111959815 878 | 16.172764571383595 879 | 880 | 510.3699951171875 881 | 882 | 88 883 | 884 | 885 | 886 | 1.3660000562667847 887 | 888 | 889 | 890 | 891 | 892 | 893 | 46.63854690268636 894 | 16.172694079577923 895 | 896 | 517.030029296875 897 | 898 | 87 899 | 900 | 901 | 902 | 1.3550000190734863 903 | 904 | 905 | 906 | 907 | 908 | 909 | 46.638559056445956 910 | 16.17266465909779 911 | 912 | 519.5700073242188 913 | 914 | 87 915 | 916 | 917 | 918 | 1.3459999561309814 919 | 920 | 921 | 922 | 923 | 924 | 925 | 46.638569785282016 926 | 16.172633981332183 927 | 928 | 522.1400146484375 929 | 930 | 87 931 | 932 | 933 | 934 | 1.347000002861023 935 | 936 | 937 | 938 | 939 | 940 | 941 | 46.63868176750839 942 | 16.17237632162869 943 | 944 | 545.719970703125 945 | 946 | 86 947 | 948 | 949 | 950 | 1.3329999446868896 951 | 952 | 953 | 954 | 955 | 956 | 957 | 46.63872535340488 958 | 16.172223687171936 959 | 960 | 558.3699951171875 961 | 962 | 85 963 | 964 | 965 | 966 | 1.3819999694824219 967 | 968 | 969 | 970 | 971 | 972 | 973 | 46.638731975108385 974 | 16.17217356339097 975 | 976 | 562.27001953125 977 | 978 | 85 979 | 980 | 981 | 982 | 1.3869999647140503 983 | 984 | 985 | 986 | 987 | 988 | 989 | 46.638887878507376 990 | 16.171739548444748 991 | 992 | 600.7899780273438 993 | 994 | 82 995 | 996 | 997 | 998 | 1.3389999866485596 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 46.638904474675655 1006 | 16.171718258410692 1007 | 1008 | 603.1699829101562 1009 | 1010 | 80 1011 | 1012 | 1013 | 1014 | 1.3289999961853027 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 46.63898485712707 1022 | 16.17152472026646 1023 | 1024 | 620.47998046875 1025 | 1026 | 79 1027 | 1028 | 1029 | 1030 | 1.3420000076293945 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 46.63898938335478 1038 | 16.171509129926562 1039 | 1040 | 621.75 1041 | 1042 | 76 1043 | 1044 | 1045 | 1046 | 1.3350000381469727 1047 | 1048 | 1049 | 1050 | 1051 | 1052 | 1053 | 46.63918962702155 1054 | 16.1711117438972 1055 | 1056 | 659.8499755859375 1057 | 1058 | 80 1059 | 1060 | 1061 | 1062 | 1.2640000581741333 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 46.63930336944759 1070 | 16.1709494702518 1071 | 1072 | 677.9000244140625 1073 | 1074 | 77 1075 | 1076 | 1077 | 1078 | 1.312000036239624 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 46.63933211937547 1086 | 16.170911164954305 1087 | 1088 | 682.27001953125 1089 | 1090 | 76 1091 | 1092 | 1093 | 1094 | 1.2280000448226929 1095 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | 46.63934745825827 1102 | 16.17088844999671 1103 | 1104 | 684.6400146484375 1105 | 1106 | 76 1107 | 1108 | 1109 | 1110 | 1.215000033378601 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 46.63940998725593 1118 | 16.170760206878185 1119 | 1120 | 696.6099853515625 1121 | 1122 | 79 1123 | 1124 | 1125 | 1126 | 1.2130000591278076 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | 1133 | 46.63943404331803 1134 | 16.170687284320593 1135 | 1136 | 702.77001953125 1137 | 1138 | 79 1139 | 1140 | 1141 | 1142 | 1.1970000267028809 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 46.639449214562774 1150 | 16.17062014527619 1151 | 1152 | 708.1400146484375 1153 | 1154 | 79 1155 | 1156 | 1157 | 1158 | 1.2599999904632568 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165 | 46.63957200944424 1166 | 16.170285623520613 1167 | 1168 | 737.1799926757812 1169 | 1170 | 79 1171 | 1172 | 1173 | 1174 | 1.0490000247955322 1175 | 1176 | 1177 | 1178 | 1179 | 1180 | 1181 | 46.639578714966774 1182 | 16.170241869986057 1183 | 1184 | 740.5700073242188 1185 | 1186 | 79 1187 | 1188 | 1189 | 1190 | 1.0920000076293945 1191 | 1192 | 1193 | 1194 | 1195 | 1196 | 1197 | 46.63958399556577 1198 | 16.1701965238899 1199 | 1200 | 743.969970703125 1201 | 1202 | 80 1203 | 1204 | 1205 | 1206 | 1.1239999532699585 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | 46.63958734832704 1214 | 16.170168612152338 1215 | 1216 | 746.0599975585938 1217 | 1218 | 80 1219 | 1220 | 1221 | 1222 | 1.1200000047683716 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 46.639587096869946 1230 | 16.170139024034142 1231 | 1232 | 748.239990234375 1233 | 1234 | 80 1235 | 1236 | 1237 | 1238 | 1.1239999532699585 1239 | 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 46.639628168195486 1246 | 16.169880107045174 1247 | 1248 | 769.0499877929688 1249 | 1250 | 83 1251 | 1252 | 1253 | 1254 | 1.3079999685287476 1255 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261 | 46.63968063890934 1262 | 16.169754713773727 1263 | 1264 | 780.239990234375 1265 | 1266 | 85 1267 | 1268 | 1269 | 1270 | 1.2690000534057617 1271 | 1272 | 1273 | 1274 | 1275 | 1276 | 1277 | 46.63968700915575 1278 | 16.169725712388754 1279 | 1280 | 782.52001953125 1281 | 1282 | 86 1283 | 1284 | 1285 | 1286 | 1.2410000562667847 1287 | 1288 | 1289 | 1290 | 1291 | 1292 | 1293 | 46.639681393280625 1294 | 16.169593948870897 1295 | 1296 | 792.77001953125 1297 | 1298 | 83 1299 | 1300 | 1301 | 1302 | 1.1460000276565552 1303 | 1304 | 1305 | 1306 | 1307 | 1308 | 1309 | 46.63962607271969 1310 | 16.169447265565395 1311 | 1312 | 805.6199951171875 1313 | 1314 | 84 1315 | 1316 | 1317 | 1318 | 1.1770000457763672 1319 | 1320 | 1321 | 1322 | 1323 | 1324 | 1325 | 46.63954594172537 1326 | 16.169302174821496 1327 | 1328 | 819.780029296875 1329 | 1330 | 84 1331 | 1332 | 1333 | 1334 | 1.093999981880188 1335 | 1336 | 1337 | 1338 | 1339 | 1340 | 1341 | 46.639373106881976 1342 | 16.168924486264586 1343 | 1344 | 854.8300170898438 1345 | 1346 | 84 1347 | 1348 | 1349 | 1350 | 1.218000054359436 1351 | 1352 | 1353 | 1354 | 1355 | 1356 | 1357 | 46.63934050127864 1358 | 16.168880313634872 1359 | 1360 | 859.7999877929688 1361 | 1362 | 84 1363 | 1364 | 1365 | 1366 | 1.2410000562667847 1367 | 1368 | 1369 | 1370 | 1371 | 1372 | 1373 | 46.63932541385293 1374 | 16.16885726340115 1375 | 1376 | 862.1900024414062 1377 | 1378 | 84 1379 | 1380 | 1381 | 1382 | 1.2319999933242798 1383 | 1384 | 1385 | 1386 | 1387 | 1388 | 1389 | 46.63915744051337 1390 | 16.16861779242754 1391 | 1392 | 888.3599853515625 1393 | 1394 | 82 1395 | 1396 | 1397 | 1398 | 1.1089999675750732 1399 | 1400 | 1401 | 1402 | 1403 | 1404 | 1405 | 46.63909977301955 1406 | 16.1685508210212 1407 | 1408 | 896.530029296875 1409 | 1410 | 78 1411 | 1412 | 1413 | 1414 | 1.1699999570846558 1415 | 1416 | 1417 | 1418 | 1419 | 1420 | 1421 | 46.639082338660955 1422 | 16.168533386662602 1423 | 1424 | 898.8400268554688 1425 | 1426 | 78 1427 | 1428 | 1429 | 1430 | 1.1710000038146973 1431 | 1432 | 1433 | 1434 | 1435 | 1436 | 1437 | 46.63903799839318 1438 | 16.168492985889316 1439 | 1440 | 904.6300048828125 1441 | 1442 | 78 1443 | 1444 | 1445 | 1446 | 1.1720000505447388 1447 | 1448 | 1449 | 1450 | 1451 | 1452 | 1453 | 46.63899349048734 1454 | 16.168455854058266 1455 | 1456 | 910.2000122070312 1457 | 1458 | 78 1459 | 1460 | 1461 | 1462 | 1.159000039100647 1463 | 1464 | 1465 | 1466 | 1467 | 1468 | 1469 | 46.6389590408653 1470 | 16.168423919007182 1471 | 1472 | 914.3599853515625 1473 | 1474 | 78 1475 | 1476 | 1477 | 1478 | 1.1549999713897705 1479 | 1480 | 1481 | 1482 | 1483 | 1484 | 1485 | 46.638871282339096 1486 | 16.168321324512362 1487 | 1488 | 926.8900146484375 1489 | 1490 | 81 1491 | 1492 | 1493 | 1494 | 1.0479999780654907 1495 | 1496 | 1497 | 1498 | 1499 | 1500 | 1501 | 46.63864899426699 1502 | 16.168156284838915 1503 | 1504 | 954.77001953125 1505 | 1506 | 82 1507 | 1508 | 1509 | 1510 | 1.2089999914169312 1511 | 1512 | 1513 | 1514 | 1515 | 1516 | 1517 | 46.638604989275336 1518 | 16.168145556002855 1519 | 1520 | 959.6599731445312 1521 | 1522 | 82 1523 | 1524 | 1525 | 1526 | 1.246000051498413 1527 | 1528 | 1529 | 1530 | 1531 | 1532 | 1533 | 46.638398710638285 1534 | 16.16807389073074 1535 | 1536 | 983.2999877929688 1537 | 1538 | 84 1539 | 1540 | 1541 | 1542 | 1.3919999599456787 1543 | 1544 | 1545 | 1546 | 1547 | 1548 | 1549 | 46.638326458632946 1550 | 16.168085038661957 1551 | 1552 | 991.3200073242188 1553 | 1554 | 85 1555 | 1556 | 1557 | 1558 | 1.3919999599456787 1559 | 1560 | 1561 | 1562 | 1563 | 1564 | 1565 | 46.63830223493278 1566 | 16.168097108602524 1567 | 1568 | 994.1199951171875 1569 | 1570 | 84 1571 | 1572 | 1573 | 1574 | 1.409000039100647 1575 | 1576 | 1577 | 1578 | 1579 | 1580 | 1581 | 46.63824205286801 1582 | 16.16813398897648 1583 | 1584 | 1001.3599853515625 1585 | 1586 | 84 1587 | 1588 | 1589 | 1590 | 1.4110000133514404 1591 | 1592 | 1593 | 1594 | 1595 | 1596 | 1597 | 1.034000039100647 1598 | 426 1599 | 1600 | 1601 | 1602 | 1603 | 145.539 1604 | 157.51 1605 | 1.3969999551773071 1606 | 12 1607 | 1608 | 87 1609 | 1610 | 1611 | 93 1612 | 1613 | Active 1614 | Manual 1615 | 1616 | 1617 | 1618 | 1619 | 46.63809830322862 1620 | 16.16813700646162 1621 | 1622 | 1017.6900024414062 1623 | 1624 | 83 1625 | 1626 | 1627 | 1628 | 1.1670000553131104 1629 | 1630 | 1631 | 1632 | 1633 | 1634 | 1635 | 46.63788087666035 1636 | 16.16815938614309 1637 | 1638 | 1042.1700439453125 1639 | 1640 | 86 1641 | 1642 | 1643 | 1644 | 1.3049999475479126 1645 | 1646 | 1647 | 1648 | 1649 | 1650 | 1651 | 46.63769446313381 1652 | 16.168130049481988 1653 | 1654 | 1063.1199951171875 1655 | 1656 | 87 1657 | 1658 | 1659 | 1660 | 1.0829999446868896 1661 | 1662 | 1663 | 1664 | 1665 | 1666 | 1667 | 46.63759094662964 1668 | 16.16813650354743 1669 | 1670 | 1074.6099853515625 1671 | 1672 | 88 1673 | 1674 | 1675 | 1676 | 1.069000005722046 1677 | 1678 | 1679 | 1680 | 1681 | 1682 | 1683 | 46.637246115133166 1684 | 16.168147567659616 1685 | 1686 | 1114.5799560546875 1687 | 1688 | 85 1689 | 1690 | 1691 | 1692 | 1.3580000400543213 1693 | 1694 | 1695 | 1696 | 1697 | 1698 | 1699 | 46.63711837492883 1700 | 16.1681604757905 1701 | 1702 | 1129.1700439453125 1703 | 1704 | 92 1705 | 1706 | 1707 | 1708 | 1.1139999628067017 1709 | 1710 | 1711 | 1712 | 1713 | 1714 | 1715 | 46.63711032830179 1716 | 16.1681691929698 1717 | 1718 | 1130.280029296875 1719 | 1720 | 92 1721 | 1722 | 1723 | 1724 | 1.1009999513626099 1725 | 1726 | 1727 | 1728 | 1729 | 1730 | 1731 | 46.63706841878593 1732 | 16.168218730017543 1733 | 1734 | 1136.280029296875 1735 | 1736 | 89 1737 | 1738 | 1739 | 1740 | 1.1610000133514404 1741 | 1742 | 1743 | 1744 | 1745 | 1746 | 1747 | 46.63698686286807 1748 | 16.16829777136445 1749 | 1750 | 1147.1800537109375 1751 | 1752 | 86 1753 | 1754 | 1755 | 1756 | 1.1130000352859497 1757 | 1758 | 1759 | 1760 | 1761 | 1762 | 1763 | 46.63692316040397 1764 | 16.16832710802555 1765 | 1766 | 1154.6099853515625 1767 | 1768 | 90 1769 | 1770 | 1771 | 1772 | 0.7099999785423279 1773 | 1774 | 1775 | 1776 | 1777 | 1778 | 1779 | 46.63691896945238 1780 | 16.168327778577805 1781 | 1782 | 1155.0799560546875 1783 | 1784 | 93 1785 | 1786 | 1787 | 1788 | 0.6710000038146973 1789 | 1790 | 1791 | 1792 | 1793 | 1794 | 1795 | 46.63691394031048 1796 | 16.168330293148756 1797 | 1798 | 1155.6700439453125 1799 | 1800 | 93 1801 | 1802 | 1803 | 1804 | 0.6470000147819519 1805 | 1806 | 1807 | 1808 | 1809 | 1810 | 1811 | 46.63689751178026 1812 | 16.168332304805517 1813 | 1814 | 1157.510009765625 1815 | 1816 | 92 1817 | 1818 | 1819 | 1820 | 0.0 1821 | 1822 | 1823 | 1824 | 1825 | 1826 | 1827 | 1.0820000171661377 1828 | 65 1829 | 1830 | 1831 | 1832 | 1833 | vívoactive HR 1834 | 3937453043 1835 | 2337 1836 | 1837 | 5 1838 | 30 1839 | 0 1840 | 0 1841 | 1842 | 1843 | 1844 | 1845 | 1846 | Connect Api 1847 | 1848 | 1849 | 0 1850 | 0 1851 | 0 1852 | 0 1853 | 1854 | 1855 | en 1856 | 006-D2449-00 1857 | 1858 | 1859 | -------------------------------------------------------------------------------- /tests/test_cycling.py: -------------------------------------------------------------------------------- 1 | import os 2 | from unittest import TestCase 3 | 4 | from tcxparser import TCXParser 5 | 6 | 7 | class TestParseCyclingTCX(TestCase): 8 | def setUp(self): 9 | """ 10 | TCX file test2.tcx was taken from the following dataset: 11 | S. Rauter, I. Jr. Fister, I. Fister. A collection of sport activity files 12 | for data analysis and data mining. 13 | Technical report 0201, University of Ljubljana and University of Maribor, 2015. 14 | """ 15 | tcx_file = "test2.tcx" 16 | path = os.path.join(os.path.dirname(__file__), "files", tcx_file) 17 | self.tcx = TCXParser(path) 18 | 19 | def test_cadence_max_is_correct(self): 20 | self.assertEqual(self.tcx.cadence_max, 115) 21 | 22 | def test_cadence_avg_is_correct(self): 23 | self.assertEqual(self.tcx.cadence_avg, 82) 24 | 25 | def test_power_avg_is_correct(self): 26 | self.assertEqual(self.tcx.power_avg, 146) 27 | 28 | def test_power_max_is_correct(self): 29 | self.assertEqual(self.tcx.power_max, 419) 30 | -------------------------------------------------------------------------------- /tests/test_standup_paddleboarding.py: -------------------------------------------------------------------------------- 1 | import os 2 | from unittest import TestCase 3 | 4 | from tcxparser import TCXParser 5 | 6 | 7 | class TestSupTCX(TestCase): 8 | def setUp(self): 9 | """ 10 | TCX file sup_activity_1.tcx was taken from the following repository: 11 | https://github.com/firefly-cpp/tcx-test-files 12 | """ 13 | tcx_file = "sup_activity_1.tcx" 14 | path = os.path.join(os.path.dirname(__file__), "files", tcx_file) 15 | self.tcx = TCXParser(path) 16 | 17 | def test_total_steps(self): 18 | self.assertEqual(self.tcx.total_steps, 491) 19 | -------------------------------------------------------------------------------- /tests/test_tcxparser.py: -------------------------------------------------------------------------------- 1 | import os 2 | from datetime import datetime, timedelta, timezone 3 | 4 | try: 5 | from StringIO import StringIO 6 | except ImportError: 7 | from io import StringIO 8 | from unittest import TestCase 9 | 10 | from tcxparser import TCXParser 11 | from tcxparser.exceptions import NoHeartRateDataError 12 | 13 | 14 | class TestParseTCX(TestCase): 15 | def setUp(self): 16 | tcx_file = "test.tcx" 17 | path = os.path.join(os.path.dirname(__file__), "files", tcx_file) 18 | self.tcx = TCXParser(path) 19 | 20 | def test_hr_values_are_correct(self): 21 | self.assertEqual(self.tcx.hr_values()[0], 62) 22 | self.assertEqual(self.tcx.hr_values()[-1], 180) 23 | 24 | def test_altitude_points_are_correct(self): 25 | self.assertEqual(self.tcx.altitude_points()[0], 178.942626953) 26 | self.assertEqual(self.tcx.altitude_points()[-1], 166.4453125) 27 | 28 | def test_time_values_are_correct(self): 29 | self.assertEqual(self.tcx.time_values()[0], "2012-12-26T21:29:53Z") 30 | self.assertEqual(self.tcx.time_values()[-1], "2012-12-26T22:03:05Z") 31 | 32 | def test_time_objects_are_correct(self): 33 | self.assertEqual( 34 | self.tcx.time_objects()[0], 35 | datetime(2012, 12, 26, 21, 29, 53, tzinfo=timezone.utc), 36 | ) 37 | self.assertEqual( 38 | self.tcx.time_objects()[-1], 39 | datetime(2012, 12, 26, 22, 3, 5, tzinfo=timezone.utc), 40 | ) 41 | 42 | def test_time_durations_are_correct(self): 43 | time_durations = self.tcx.time_durations() 44 | assert len(time_durations) == len(self.tcx.time_values()) 45 | self.assertEqual(time_durations[0], timedelta(microseconds=500000)) 46 | self.assertEqual(time_durations[-1], timedelta(microseconds=500000)) 47 | 48 | def test_latitude_is_correct(self): 49 | self.assertEqual(self.tcx.latitude, 35.951880198) 50 | 51 | def test_longitude_is_correct(self): 52 | self.assertEqual(self.tcx.longitude, -79.0931872185) 53 | 54 | def test_activity_type_is_correct(self): 55 | self.assertEqual(self.tcx.activity_type, "running") 56 | 57 | def test_started_at_is_correct(self): 58 | self.assertEqual(self.tcx.started_at, "2012-12-26T21:29:53Z") 59 | 60 | def test_completion_time_is_correct(self): 61 | self.assertEqual(self.tcx.completed_at, "2012-12-26T22:03:05Z") 62 | 63 | def test_distance_is_correct(self): 64 | self.assertEqual(self.tcx.distance, 4686.31103516) 65 | 66 | def test_distance_units_is_correct(self): 67 | self.assertEqual(self.tcx.distance_units, "meters") 68 | 69 | def test_duration_is_correct(self): 70 | self.assertEqual(self.tcx.duration, 1992.78) 71 | 72 | def test_calories_is_correct(self): 73 | self.assertEqual(self.tcx.calories, 379) 74 | 75 | def test_hr_max(self): 76 | self.assertEqual(self.tcx.hr_max, 189) 77 | 78 | def test_hr_min(self): 79 | self.assertEqual(self.tcx.hr_min, 60) 80 | 81 | def test_hr_avg(self): 82 | self.assertEqual(self.tcx.hr_avg, 156) 83 | 84 | def test_pace(self): 85 | self.assertEqual(self.tcx.pace, "07:05") 86 | 87 | def test_altitude_avg_is_correct(self): 88 | self.assertAlmostEqual(self.tcx.altitude_avg, 172.020056184) 89 | 90 | def test_altitude_max_is_correct(self): 91 | self.assertAlmostEqual(self.tcx.altitude_max, 215.95324707) 92 | 93 | def test_altitude_min_is_correct(self): 94 | self.assertAlmostEqual(self.tcx.altitude_min, 157.793579102) 95 | 96 | def test_ascent_is_correct(self): 97 | self.assertAlmostEqual(self.tcx.ascent, 153.80981445) 98 | 99 | def test_descent_is_correct(self): 100 | self.assertAlmostEqual(self.tcx.descent, 166.307128903) 101 | 102 | def test_activity_notes_is_correct(self): 103 | self.assertEqual(self.tcx.activity_notes, "Aerobics") 104 | 105 | def test_position_values_are_correct(self): 106 | values = self.tcx.position_values() 107 | self.assertAlmostEqual(values[-1][0], 35.9519544616) 108 | self.assertAlmostEqual(values[-1][1], -79.0930143837) 109 | 110 | def test_distance_values_are_correct(self): 111 | self.assertAlmostEqual(self.tcx.distance_values()[0], 0.0) 112 | self.assertAlmostEqual(self.tcx.distance_values()[-1], 4686.31103516) 113 | 114 | def test_hr_percent_in_zones(self): 115 | zones = { 116 | "Z0": (0, 99), 117 | "Z1": (100, 129), 118 | "Z2": (130, 149), 119 | "Z3": (150, 169), 120 | "Z4": (170, 184), 121 | "Z5": (185, 199), 122 | "Z6": (200, 220), 123 | } 124 | self.assertEqual( 125 | self.tcx.hr_percent_in_zones(zones), 126 | {"Z0": 14, "Z1": 1, "Z2": 1, "Z3": 46, "Z4": 33, "Z5": 5, "Z6": 0}, 127 | ) 128 | 129 | def test_hr_time_in_zones(self): 130 | zones = { 131 | "Z0": (0, 99), 132 | "Z1": (100, 129), 133 | "Z2": (130, 149), 134 | "Z3": (150, 169), 135 | "Z4": (170, 184), 136 | "Z5": (185, 199), 137 | "Z6": (200, 220), 138 | } 139 | self.assertEqual( 140 | self.tcx.hr_time_in_zones(zones), 141 | { 142 | "Z0": timedelta(seconds=217, microseconds=500000), 143 | "Z1": timedelta(seconds=3, microseconds=500000), 144 | "Z2": timedelta(seconds=4, microseconds=500000), 145 | "Z3": timedelta(seconds=969), 146 | "Z4": timedelta(seconds=676, microseconds=500000), 147 | "Z5": timedelta(seconds=120, microseconds=500000), 148 | "Z6": timedelta(0), 149 | }, 150 | ) 151 | 152 | 153 | class BugTest(TestCase): 154 | def test_single_trackpoint_in_track_is_ok(self): 155 | "https://github.com/vkurup/python-tcxparser/issues/9" 156 | xml = """ 157 | 158 | 159 | 160 | 161 | 162 | 163 | 5 164 | 165 | 166 | 167 | 168 | 169 | 170 | """ 171 | tcx_file = StringIO(xml) 172 | tcx = TCXParser(tcx_file) 173 | self.assertEqual(tcx.distance, 5) 174 | 175 | def test_no_error_if_no_position(self): 176 | "https://github.com/vkurup/python-tcxparser/issues/11" 177 | xml = """ 178 | 179 | 180 | 181 | 182 | 183 | 184 | 5 185 | 186 | 187 | 188 | 189 | 190 | 191 | """ 192 | tcx_file = StringIO(xml) 193 | tcx = TCXParser(tcx_file) 194 | self.assertEqual(tcx.latitude, None) 195 | self.assertEqual(tcx.longitude, None) 196 | 197 | def test_hr_percent_in_zones_no_hr_data(self): 198 | tcx_file = "no_hr.tcx" 199 | path = os.path.join(os.path.dirname(__file__), "files", tcx_file) 200 | tcx = TCXParser(path) 201 | with self.assertRaises(NoHeartRateDataError): 202 | tcx.hr_percent_in_zones({}) 203 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py39, py310, py311, py312, py313, coverage 3 | 4 | [gh-actions] 5 | python = 6 | 3.9: py39 7 | 3.10: py310 8 | 3.11: py311 9 | 3.12: py312 10 | 3.13: py313, coverage 11 | 12 | [testenv:coverage] 13 | deps = coverage 14 | commands = 15 | coverage run -m unittest 16 | coverage report 17 | 18 | [testenv] 19 | commands = python -m unittest 20 | --------------------------------------------------------------------------------