Chapter 4 · 37 pages · Part: Foundations
Athlete Data in Python
You wore a chest strap and a watch for the same interval session. The two files have gaps, a few impossible beats, and clocks that do not agree. What was your heart rate, second by second, and how do you know?
Read online PDF 963 kB Notebook + data 130 kB Markdown
Learning objectives
- Write a notebook someone else can run: restart and run all, stated versions, a fixed seed, and functions checked against known cases.
- Load a table, a time series and a folder of app results files into pandas, and put every time stamp on one clock; describe each file in a data dictionary.
- Find missing values, spikes, flat lines, duplicates and unit mix-ups with rules you wrote down before you looked; keep the raw file, and log every change.
- Resample two devices onto one grid, find the offset between their clocks by cross-correlation, and join tables on a key with a check after every join.
- Smooth a signal with a moving average, a median filter and a low-pass filter, and state the lag each one costs; explain why a derivative amplifies noise.
- Describe a disagreement with a bias and limits of agreement, show a distribution, and draw a chart that makes one point without misleading.
- Use an AI assistant to write the code, and a hand-computed answer to check it.
Apps
Sections
- 4.1A Working Setup
- 4.2Tables and Time Series
- 4.3Cleaning
- 4.4Resampling and Aligning
- 4.5Smoothing and Differentiating
- 4.6Describing Data
- 4.7Charts That Make One Point
- 4.8The App Results Files
- 4.9Coding with an Assistant
- 4.10Telling the Athlete
- 4.11Build Step B-4: A CSV File on a Canvas
- 4.12Summary
- 4.13Exercises
Lecture slides
Two decks for this chapter, one for each class of the week. Each has a speaker script; text CC BY 4.0.
Files You Can Defend
Class 7, sections 4.1–4.7 · 29 slides · about 38 min
Results Files, Assistants and Honest Charts
Class 8, sections 4.8–4.13 · 24 slides · about 34 min
Tutorials
From a raw file to an action, with every step runnable in the browser. Case studies are also printed in Part V.
- CS-4 A messy watch export to a clean table case study, 60 min
- CS-5 Phone in a pocket: walk, jog, or run? case study, 75 min
- Q-10 Open a phone sensor log and plot it quick tutorial, 15 min
- Q-11 Write a data dictionary for one file quick tutorial, 20 min
- Q-12 Find and mark the missing values without deleting anything quick tutorial, 20 min
Code listings
- Listing 4.1 The setup check code/l01_setup.py
- Listing 4.2 The first cell of a notebook someone else can run code/l16_pace.py
- Listing 4.3 A loop and an array give the same answer code/l17_arrays.py
- Listing 4.4 A wide sheet made tidy code/l02_tidy.py
- Listing 4.5 Two files on one clock code/l03_clocks.py
- Listing 4.6 The real sample rate of a phone log code/l04_rate.py
- Listing 4.7 A file checked against its data dictionary code/l18_dictionary.py
- Listing 4.8 Cleaning rules written before the data are seen code/l05_clean.py
- Listing 4.9 Duplicates, a missing week and a unit mix-up code/l06_units.py
- Listing 4.10 The cleaning rules write their own change log code/l19_changelog.py
- Listing 4.11 Resample both devices and find the clock offset code/l07_align.py
- Listing 4.12 A join, and the check to run after every join code/l20_join.py
- Listing 4.13 The hand calculations of Examples 4.5 and 4.6, confirmed code/l08_filters.py
- Listing 4.14 The lag of three filters code/l09_lag.py
- Listing 4.15 A simulated jump: the noise in velocity and acceleration code/l10_derivative.py
- Listing 4.16 Cadence from a cleaned walk code/l12_cadence.py
- Listing 4.17 Bias and limits of agreement, watch against strap code/l11_agree.py
- Listing 4.18 What a box plot and a histogram each draw code/l21_distribution.py
- Listing 4.19 Excerpt of `results_jump_wk3_t3.md` (synthetic) code/l13_results.py
- Listing 4.20 The receipt `results_jump_wk3_t3.json`, warnings shortened code/l14_receipts.py
- Listing 4.21 A folder of app results into one table code/l15_test.py
- Listing 4.22 Check the receipts against the session log, then compare weeks code/b4/csv-chart.js
- Listing 4.23 A test from a hand calculation catches a plausible error
- Listing 4.24 The B-4 module, `csv-chart.js`
The listings and their data are in the notebook download above.
Exercises
34 exercises; 8 are CORE. Answers to selected exercises are at the back of the book.
Concept check
- 4.1 CORE Foundation, 2 min
A watch file has the time stamp 2027-03-02 07:15:00 and no zone. Which statement is right? Options: It is UTC, because files store UTC unless told otherwise It cannot be placed on a common time line until you know… - 4.2 CORE Core, 2 min
You smooth a live heart-rate display with a trailing 9-point moving average of one value a second. On a steady rise, how late is the display? Options: 0 s, because averaging removes only noise 1 s 9 s 4 s - 4.3 Foundation, 2 min
Two heart-rate devices give a correlation of 0.99 over a session. What does that show? Options: That they rise and fall together; it says nothing about whether one reads higher than the other That they agree to within… - 4.4 Core, 2 min
A 120 fps slow-motion jump clip, stored with a 30 fps header, is processed with the capture rate left on “auto”. Which results in its Markdown file stay trustworthy? Options: All of them, because the app warns about the…
Hand calculation
- 4.5 CORE Foundation, 10 min
A GPS watch gives ten speeds, one a second, in m/s: \(3.2, 3.4, 3.3, 3.5, 3.6, 3.4, 3.7, 3.8, 3.6, 3.9\). parts Compute the centered five-point moving average at samples 3 to 8. 3.40 at sample 3; 3.68 at sample 8. The… - 4.6 Core, 10 min
A pose model places the hip with random noise of \(=3 mm\) in each frame. parts Compute the noise in a speed from two neighboring frames at 60 fps. 0.25 m/s. Repeat at 240 fps, with the same 3 mm per frame. 1.02 m/s.… - 4.7 Core, 8 min
A chest strap writes 05:58:41Z at the moment you press a lap button. A watch worn in a zone two hours ahead of UTC records the same press at local time 07:58:30. parts Convert the watch time to UTC. 05:58:30. Is the… - 4.25 Foundation, 8 min
Four 200 m repetitions take t = [40, 38, 36, 40] seconds. parts What are t[1:3] and t[-1]? \([38, 36]\); 40. Compute the speed of each repetition in m/s, as 200 / np.array(t) would. 5.00, 5.26, 5.56, 5.00. Compute the…
Python analysis
- 4.8 CORE Foundation, 20 min
The course module https://apmonitor.com/dde/index.php/Main/SmartphoneSensors publishes a real phone accelerometer file, https://apmonitor.com/dde/uploads/Main/Accelerometer.csv. Download it in a browser and upload it to… - 4.9 Core, 20 min
Change Listing 4.17 so that it does not subtract the 7 s offset, and run it. parts Report the bias, the SD and the limits of agreement. By what factor did the correction narrow the limits? Explain why the bias hardly… - 4.10 Extension, 20 min
The spike rule of Listing 4.8 uses a threshold of 5 m/s² from the five-point median. Run it with thresholds of 5, 2 and 1 m/s\(^2\) (keep the duplicate removal) and report how many samples each flags. Which threshold… - 4.11 Extension, 30 min
Give an AI assistant the seven .md files in data/results (not the .json receipts) and ask for a five-line summary of the athlete's progress for a coach. Then check its summary line by line against the files and against… - 4.22 Foundation, 15 min
Run Listing 4.23. Then fix the function so that the test passes, without changing the test. Show the passing output and say in one sentence why your version leaves the ends empty. - 4.23 Foundation, 15 min
Extend Listing 4.2 (Section 4.1.1). parts Add a fourth known case: a marathon, 42195 m, in 3:30:00. Compute the pace by hand first, then write the assert. 298.6 s a kilometer, 4:58.6/km. Add a check that the function… - 4.24 Foundation, 15 min
Write a function speed_kmh(distance_m, time_s) that returns speed in kilometers an hour, and three known cases you compute by hand before you write any code: 10 km in 50 min; one 400 m lap in 90 s; 100 m in 10 s. parts… - 4.26 Foundation, 15 min
Load data/walk_accel.csv and compute the magnitude \(x^2+y^2+z^2\) of every row twice: with a loop over the rows, and with one line of array arithmetic. parts Report the number of rows and the mean magnitude (ignore… - 4.27 Core, 20 min
Write a data dictionary (Section 4.2.5) for this chapter's two heart-rate files, the strap's and the watch's (strap_hr.csv and watch_hr.csv in data/): for each field, its meaning, unit, type, allowed range and… - 4.28 Core, 20 min
Apply provenance and a change log (Section 4.3.1) to the synthetic watch activity table in the file data/watch_activity.csv. Using the dictionary of Table 4.2, write rules that drop repeated time stamps, mark heart… - 4.30 Core, 20 min
Use data/sessions.csv and data/session_hr.csv (Section 4.4.1). parts Before running anything, predict the rows of an inner join of the raw tables, and of an outer join. Then run both with indicator=True. 300 and 346.… - 4.33 Core, 40 min, case-file data
Case file: Audit your case-file athlete's watch archive (see apopt.com/data/athletes) for data quality. Read each activity's FIT file with fitdecode and make a per-second record table (time stamp, heart rate, distance,…
App lab
- 4.12 CORE Core, 20 min
Analyze yourself with the jump app's process-a-folder page (https://apopt.com/jump/automation.html), following the five-step pattern of the book. steps Film three countermovement jumps from the side, phone on a tripod… - 4.13 Extension, 25 min
Process one slow-motion jump clip twice with the jump app's folder page: once with the capture rate on “auto” and once with the rate the camera recorded at (rename a copy of the clip so both sets of results can sit in… - 4.29 Foundation, 15 min
Process one clip of yourself with the jump app's process-a-folder page, https://apopt.com/jump/automation.html: side view, phone on a stand at hip height, one countermovement jump, a clip of 5 to 10 seconds, filmed at…
Field collection
- 4.14 CORE Foundation, 30 min
Record a walk with your phone's motion sensors and find your cadence. steps Install a free sensor-logging app that exports CSV. Set the accelerometer to 50–100 Hz and record raw acceleration (including gravity) if the…
Body science
- 4.15 Foundation, 5 min
In the synthetic session, the strap's heart rate takes tens of seconds to settle after each change of pace. In plain words, why does heart rate not jump at once when the work rate does, and what does that mean for a… - 4.16 Core, 5 min
A wrist heart-rate sensor shines light into the skin and measures how much returns as blood volume pulses with each beat. During a run it sometimes reports a value close to the runner's cadence (for example 165 a…
Science of the sport
- 4.17 Foundation, 5 min
You want to check a watch's clock against a chest strap by cross-correlation. Which session would you choose: a steady 40-minute easy run, or a session of 6 \(×\) 1-minute fast running with 1 minute of jogging between?… - 4.18 Extension, 8 min
A coach wants to see whether a squad's countermovement jump improved over a four-week block. From what you know about jumping and about measurement, list three things, other than training, that could change the measured…
Build
- 4.19 CORE Core, 60 min
Build step B-4. Add to your week-1 Vite project a page that loads a CSV file chosen by the user and draws one column on a canvas, using Listing 4.24 as a model. parts The page must read the file in the browser, without…
Communicate
- 4.20 CORE Core, 20 min
A coach sends you a default spreadsheet chart of the squad's jump heights: a 3-D column chart, one color per athlete, the axis starting at 25 cm, legend “Series1–Series8”, title “Chart 1”. Using Listing 4.9's cleaned… - 4.32 Core, 20 min
Draw a histogram and a box plot of the watch's heart rates in data/watch_hr.csv (Section 4.7.1). parts Report the five numbers of the box plot. 99, 112, 132, 162, 182 bpm. Write a two-sentence caption: one sentence of… - 4.34 Core, 20 min, case-file data
Case file: Write a half-page data-quality note about your case-file athlete's watch archive (see apopt.com/data/athletes) for the coach, from your audit in the previous exercise. Include one chart (for example the share…
Check the AI
- 4.21 Core, 8 min
An assistant wrote this to put the watch on UTC before aligning it with the strap: watch.index = pd.to_datetime(watch["timestamp"], utc=True). The cross-correlation over shifts of \(30\) s then gave a flat curve with no… - 4.31 Core, 8 min
An assistant writes df = hr.merge(log, on="session_id") for the files of Section 4.4.1 and reports: “Joined successfully: 300 rows, no errors, every heart-rate row now has its session type.” List what is wrong with the…
How to cite
Hedengren, J. (Ed.) (2027). Chapter 4: Athlete Data in Python. In Data-Driven Athletics: Measuring, Modeling and Coaching Human Performance with Video and Wearable Data (pp. 83–119). IRSRI Sports Research Institute. https://apopt.com/book/pdf/04-athlete-data-in-python.pdf
BibTeX
@incollection{hedengren2027ch4python,
editor = {Hedengren, John},
title = {Chapter 4: Athlete Data in Python},
booktitle = {Data-Driven Athletics: Measuring, Modeling and Coaching Human Performance with Video and Wearable Data},
chapter = {4},
pages = {83--119},
publisher = {IRSRI Sports Research Institute},
year = {2027},
note = {CC BY 4.0},
url = {https://apopt.com/book/pdf/04-athlete-data-in-python.pdf}
}
RIS
TY - CHAP ED - Hedengren, John TI - Chapter 4: Athlete Data in Python T2 - Data-Driven Athletics: Measuring, Modeling and Coaching Human Performance with Video and Wearable Data SP - 83 EP - 119 PB - IRSRI Sports Research Institute PY - 2027 N1 - CC BY 4.0 UR - https://apopt.com/book/pdf/04-athlete-data-in-python.pdf ER -