Course / Tutorials / Week 4 / CS-4
Case study CS-4 · Week 4 · about 60 min · Foundation
A messy watch export to a clean table
Take 693 activity summaries in a watch's own field names and units, audit every unit with an identity, write the cleaning rules first, keep a change log, and produce a quarterly running summary a coach can trust.
- 1Gather (used)
- 2Statistics (used)
- 3Visualize (used)
- 4Cleanse (used)
- 5Features
- 6Balance
- 7Scale
- 8Split
- 9Deploy
- Ask
- Collect (this week)
- Check (this week)
- Interpret (this week)
- Coach (this week)
- Reassess
Download notebook ·Python file ·Read as Markdown ·Data folder ·Chapter 4 (sections 2.3, 4.2, 4.3)
Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser. Helpful first: Q-10 Open a phone sensor log and plot it, Q-11 Write a data dictionary for one file, Q-12 Find and mark the missing values without deleting anything.
The decision
A coach planning the athlete's next block asks: how much has he run in each quarter since mid-2024, at what pace, and did anything change? The answer has to come from an export no one has checked. If the units, the clock or the activity types were wrong, the totals could be off by a factor of a thousand, put sessions on the wrong day, or count walks and indoor sessions with made-up distances as runs.
Before any code: the first run has avg_running_cadence 86, avg_step_length 980, a timer time of 2355 s and an elapsed time of 3307 s. What units and which time would make it a plausible run?
The raw data
raw/sessions_fit.csv: 693 activity summaries, 28 June 2024 to 28 November 2026, one watch; the FIT session fields with the watch's own names and units. Activity summaries from a COROS Training Hub archive of an adult male athlete in his mid-40s, published in the course case file (athletes/jh/coros-history/). GPS positions, per-second records and identifiers were removed before publication.raw/activities_clean.csv: the case file's own conversion of the same activities to SI units with local start times, for checking your conversion. Same case file (athletes/jh/coros-history/).
| Field | Meaning | Unit | Range | Missing |
|---|---|---|---|---|
activity_id | row id (local start time and sport) | text | 2024-06-28 – 2026-11-28 | never |
start_time | start in UTC (the "Z") | ISO 8601 | 2024–2026 | never |
utc_offset_h | local clock minus UTC at the start | h | −7 to +3 | never |
sport / sub_sport | sport and sub-sport as the watch labels them | text | 8 sports / 7 sub-sports | sub_sport blank = none |
total_timer_time | moving time (the timer stops on pauses) | s | 1.8 s – 8 h | never |
total_elapsed_time | clock time from start to stop (pauses included) | s | 2.6 s – 8 h | never |
total_distance | distance (wrist accelerometer on indoor runs) | m | 0 – 22 000 | blank or 0 on elliptical and indoor bike |
enhanced_avg_speed / enhanced_max_speed | average (over timer time) and maximum speed | m/s | 0 – 24 | blank |
avg_running_cadence | cadence of ONE foot on runs | strides/min (×2 for steps/min) | 0 – 96 | 0 = no reading |
avg_cadence | cadence on other sports | pedal rpm (bike); strokes/min (swim); undefined elsewhere | 0 – 146 | 0 = no sensor |
total_strides | strides of one foot | count | 0 – 9 500 | blank on non-running sports |
avg_step_length | distance per step | mm | 0 – 2 490 | 0 = no reading |
avg_stance_time | ground-contact time | ms | 0 – 329 | 0 = not estimated |
avg_vertical_oscillation | vertical bounce of the torso | mm | 0 – 96 | 0 = not estimated |
avg_vertical_ratio | oscillation ÷ step length | % | 0 – 12.6 | 0 = not estimated |
avg_power | running power the watch estimates | W | 0 – 225 | 0 = not estimated |
avg_heart_rate / max_heart_rate | heart rate (optical at the wrist) | beats/min | 67 – 209 | blank (weak on swims) |
total_ascent | climb (barometer) | m | 0 – 1 185 | blank |
total_calories | energy estimate | kcal | 0 – 1 700 | never |
What is wrong with it: no units in the file, and three of them are unusual (step length and vertical oscillation in millimeters, running cadence counting one foot); one column, avg_cadence, that means pedal rpm, swim strokes or something undefined depending on the sport; two durations, of which only the timer gives the right pace; a UTC start with a separate offset, which changes on trips; 0 used as "not measured" for stance time, oscillation, vertical ratio, power and cadence; no distance on the elliptical and the indoor bike; indoor runs whose distance and cadence come from the wrist; walks saved as runs; aborted starts of a few meters; a few single-sample speed spikes.
Step by step
Step 1 · Gather: the export as the watch wrote it
An adult male athlete in his mid-40s asked his watch maker's website for his data and got back one FIT file per activity. The course case file keeps one row per file: the watch's own session fields (the summary the watch writes when you press stop), with the watch's own names and units, from one watch over two and a half years. GPS positions and per-second records were removed. A coach wants one thing from it: a quarter-by-quarter summary of the running that can be trusted.
Type a prediction to enable Run.
Show the expected result without running
693 activities, 29 fields, 2024-06-28 to 2026-11-28
sport sub_sport
cycling indoor_cycling 14
road 5
fitness_equipment elliptical 73
generic - 13
hiking - 13
running - 475
indoor_running 21
track 1
swimming - 1
lap_swimming 66
training strength_training 1
walking - 10
activities with each field (fewest first): {'num_active_lengths': 0, 'pool_length': 66, 'sub_sport': 181, 'avg_cadence': 195, 'Effort Pace': 473, 'avg_running_cadence': 497, 'max_running_cadence': 497, 'total_strides': 507, 'total_distance': 603}
{'activity_id': '2024-06-28_0719_running', 'start_time': '2024-06-28T13:19:52Z', 'utc_offset_h': -6.0, 'sport': 'running', 'total_timer_time': 2355.33, 'total_elapsed_time': 3306.93, 'total_distance': 6663.45, 'enhanced_avg_speed': 2.829, 'enhanced_max_speed': 3.861, 'avg_heart_rate': 168.0, 'max_heart_rate': 185.0, 'min_heart_rate': 106.0, 'avg_running_cadence': 86.0, 'max_running_cadence': 105.0, 'total_strides': 3309.0, 'avg_step_length': 980.0, 'avg_stance_time': 0.0, 'avg_vertical_oscillation': 0.0, 'avg_vertical_ratio': 0.0, 'avg_power': 118.0, 'total_ascent': 48.0, 'total_descent': 45.0, 'total_calories': 253, 'Effort Pace': 2.8980000019073486}Eight sports, and fields that come and go with them: running cadence only on runs, pool length only on some swims, a distance on 603 of 693 activities. The first run has avg_running_cadence 86 and avg_step_length 980. Eighty-six steps a minute is a slow walk, and 980 m is not a step. Nothing in the file says what the units are.
Step 2 · Statistics: a unit audit
Do not guess units from one value. Use identities that must hold whatever the units: speed × time = distance; distance ÷ steps = step length; strides ÷ minutes = cadence; cadence × step length = speed. Each one tests a unit hypothesis against the whole file at once. A stride is one full cycle of one foot, so one stride is two steps.
Type a prediction to enable Run.
Show the expected result without running
speed x timer / distance median 1.000; within 2% of it: 100% speed x elapsed / distance median 1.308; within 2% of it: 4% distance (mm) / (2 x strides) / step_length median 0.998; within 2% of it: 86% strides per timer minute / running_cadence median 1.005; within 2% of it: 88% 2 x cadence x step (mm) / 60000 / speed median 0.997; within 2% of it: 99% oscillation / step x 100 / vertical_ratio median 0.96 (179 runs with a value)
Speed × timer time gives the distance for every run; speed × elapsed time overshoots by about 31%. The timer stops when the watch pauses (traffic lights, a water stop) and elapsed time does not, so pace and moving time must come from the timer. Distance in millimeters ÷ twice the strides matches avg_step_length (median ratio 0.998), so step length is in millimeters (980 is 0.98 m). Strides per timer minute match avg_running_cadence (1.005), so that cadence counts one foot: double it for steps a minute (86 is 172). Twice the cadence times the step length gives the speed for 99% of runs, which ties the three together. Vertical oscillation ÷ step length comes out at 0.96 of the vertical ratio, so oscillation is in millimeters too.
Not every ratio sits within 2% for every run, and it does not need to. The file rounds step length to 10 mm and cadence to whole strides. The wrong units would miss by far more: centimeters by a factor of 10, steps instead of strides by a factor of 2. A median of 0.998 rules them out.
Three more traps do not show up in a runner's identities: a field whose meaning changes with the sport, zeros that mean "no value", and a clock.
Show the expected result without running
median avg_cadence by kind: {'elliptical': 57.0, 'generic': 89.0, 'hiking': 52.0, 'indoor_cycling': 0.0, 'lap_swimming': 29.0, 'road': 0.0, 'swimming': 27.0, 'walking': 50.0}
distance blank or 0: {'elliptical': 73, 'indoor_cycling': 14, 'running': 2, 'lap_swimming': 1, 'strength_training': 1}
running activities with 0 (of 497): {'avg_stance_time': 316, 'avg_vertical_oscillation': 316, 'avg_vertical_ratio': 316, 'avg_power': 23}
elapsed / timer on runs: median 1.30; over 1.05 in 376 runs
utc_offset_h: {-6.0: 465, -7.0: 191, 3.0: 16, -4.0: 14, -5.0: 7}
away from -6/-7, by month: {'2024-12': 2, '2025-03': 7, '2025-04': 7, '2025-12': 5, '2026-02': 14, '2026-03': 2}
activities whose UTC date is not the local date: 72avg_cadence is one column with at least four meanings: pedal revolutions a minute on a bike (where 0 means no cadence sensor), strokes a minute in the pool, most likely strides of one foot on a walk or a hike, and a rhythm the file does not define on the elliptical. It must never be averaged across sports. The elliptical and indoor-bike sessions have no distance at all: a sum of distance that treats them as 0 km is right, and a mean that does is wrong.
On 316 of 497 running activities the ground-contact time (stance time), vertical oscillation and vertical ratio are 0, and power is 0 on 23. Nobody runs with zero bounce: 0 is the watch's code for "not estimated", and a mean that keeps those zeros lands far below the true value. Elapsed time is a median 1.30 times the timer on runs, and more than 5% longer on 376 of them.
The start is in UTC (the "Z"), and utc_offset_h says how far the wall clock was from it. Most rows are −6 or −7 (US Mountain time, summer and winter), but the watch also logged trips at −5, −4 and +3. A date taken from UTC puts 72 activities on the wrong date, and one fixed home time zone would put every trip in the wrong hour. The local start is UTC plus the row's own offset.
Step 3 · Visualize: what is labelled running?
Convert every field with the units the audit found, then plot each running activity's pace against its cadence. Runs should cluster; anything else will stand apart.
Show the expected result without running
distance_km moving_min pace_min_km cadence_spm step_m max_speed_mps 50% 8.9 47.64 5.24 176.0 1.08 5.59 min 0.0 0.04 3.11 0.0 0.00 0.00 max 16.1 130.43 37.05 192.0 2.49 23.81
Type a prediction to enable Run.
Show the expected result without running
cadence_spm pace_min_km avg_hr_bpm sub_sport indoor_running 92.0 14.0 153.0 outdoor 176.0 5.2 157.0 track 168.0 4.4 157.0 indoor runs with cadence of 150 or more: 6

Most running activities form one tight cluster: about 176 steps a minute at 4.5 to 6 minutes a kilometer. Two groups stand apart. A few outdoor activities move at 84 to 140 steps a minute and 7 to 14 minutes a kilometer: that is walking cadence, so these are walks saved as runs. Pace alone would not find them, because a run up a mountain can be just as slow; cadence can. And 15 of the 21 indoor runs scatter far from the rest, at 36 to 132 steps a minute and paces as slow as 37 minutes a kilometer. Indoors there is no GPS, and the watch takes distance and cadence from its wrist accelerometer. Their median heart rate (153 beats a minute) says the work was real; their distances and cadences cannot be trusted. The other 6 indoor runs land in the cluster, but their distance still comes from the wrist, not from GPS. Last, the table's extremes: a run of 0 m and a few seconds (an aborted start), a step of 2.49 m (an indoor run), and a maximum speed of 23.81 m/s (86 km/h, one bad GPS sample).
Step 4 · Cleanse: rules first, a change log, a clean copy
The rules, written before they are applied (Section 4.3). None deletes a row; each assigns a category or marks a value, and the log counts what each one did:
- Not labelled running: category
cross-training(swims, elliptical, bike, hikes, walks). - Running with less than 200 m: category
aborted. - Indoor running: category
indoor run; it counts as training time, not toward distance or pace. - Cadence of 140 steps a minute or less and slower than 7 min/km: category
walk; training time, not running pace. - Everything else: category
run. - A run with a cadence of 140 or less at a running pace: cadence and step length set missing. The wrist missed steps; the distance and time are kept.
- On foot, a maximum speed above 10 m/s (36 km/h): the maximum speed is set missing; the rest of the row is kept.
- A 0 in distance, cadence, step length, stance time, vertical oscillation, vertical ratio, power or pedal cadence: set missing. It means "not measured".
Show the expected result without running
rule values
category = cross-training 196
category = aborted 4
category = indoor run 21
category = walk 15
run cadence and step set missing (missed steps) 3
max speed set missing (spike) 6
0 set missing (not measured) 1451
{'run': 457, 'cross-training': 196, 'indoor run': 21, 'walk': 15, 'aborted': 4}Show the expected result without running
all 693 rows match the case file's SI table: local start, distance, moving time; running cadence matches on all 495 rows where the case file has one
457 runs remain, and nothing was thrown away: the 196 cross-training sessions, the 21 indoor runs, the 15 walks and the 4 aborted starts are all still in the table with their category. Three runs keep their distance and time but lose a cadence the wrist undercounted, 6 maximum speeds that were single spikes are marked missing, and so are 1,451 zeros that meant "not measured". The case file's own conversion agrees with yours on every local start, distance and moving time, and on all 495 running cadences it gives, which checks the unit audit a second way. Notice what an "outlier" rule would have done: the walks and the indoor runs look like outliers in pace, and they are real sessions.
The quarter-by-quarter summary
Now the table the coach asked for, one row per calendar quarter, with the runs summarized and everything else counted as hours so that no training disappears. The first and last quarters are partial, so the summary also gives kilometers per week of the span each quarter covers.
Show the expected result without running
runs km long_km median_pace cadence hr other_h weeks km_per_week quarter 2024Q2 1 6.7 6.7 5:53 172.0 168.0 0.2 0.4 15.5 2024Q3 41 270.1 12.5 6:05 169.0 157.0 18.5 13.1 20.6 2024Q4 39 244.6 11.8 5:15 176.0 161.0 4.6 13.1 18.6 2025Q1 49 363.8 13.5 5:09 176.0 160.0 27.4 12.9 28.3 2025Q2 52 426.7 13.0 5:12 176.0 156.0 23.4 13.0 32.8 2025Q3 57 509.2 13.0 5:08 176.0 157.0 21.0 13.1 38.7 2025Q4 45 419.3 11.8 5:11 176.0 156.0 20.9 13.1 31.9 2026Q1 36 333.9 11.6 5:09 176.0 157.0 20.8 12.9 26.0 2026Q2 44 398.0 14.5 5:06 176.0 157.5 17.1 13.0 30.6 2026Q3 44 416.7 16.1 5:18 176.0 155.0 31.1 13.1 31.7 2026Q4 49 479.7 15.6 5:15 176.0 157.0 8.8 8.4 56.9 largest quarterly difference from activities_clean.csv: 0.005 km
Read the weeks column before the totals. The first "quarter" is three days in June 2024 with one run, and the last stops on 28 November 2026 (8.4 weeks), so compare kilometers per week, not totals. Per week, the running grew from about 19 to 21 km in the second half of 2024 to 28 to 39 km in 2025 and 26 to 32 km in most of 2026; the last quarter of 2026 is the biggest by far, at about 57 km a week. Pace changed once: the median was 6:05 a kilometer at 169 steps a minute in the summer of 2024, and 5:06 to 5:18 at 176 steps a minute in every quarter since, at about the same median heart rate (155 to 161). The quarterly totals agree with activities_clean.csv to 5 m.
Two dips are questions for the athlete, not for the file. The first quarter of 2026 holds a trip (the +3 h offset in February) and only 36 runs; and in May 2026 a second watch recorded most of the runs, so they are not in this export at all (the challenge).
Show the expected result without running
largest exported-minus-clean months (km): {'2024-08': 50.5, '2025-03': 24.3, '2025-04': 22.0, '2024-10': 21.5}
How good is the number?
The unit audit is checked two ways: by identities across every run (speed × timer time = distance for all of them; cadence × step length = speed within 2% for 99%), and against the case file's own SI table, which matches every local start, distance and moving time and all 495 running cadences it gives. The categories rest on written rules, and the change log says how many activities each one moved (196 cross-training, 21 indoor runs, 15 walks, 4 aborted) and how many values it marked (3 undercounted cadences, 6 speed spikes, 1,451 zeros). The quarterly totals agree with activities_clean.csv to 5 m. The summary's limits are coverage, not cleaning: the first and last quarters are partial, the indoor runs carry no trusted distance, and in May 2026 a second watch recorded most of the runs, so they are missing here. Wrist heart rate is a rough guide to effort.
Action card
- Finding
- Your running has grown from about 19 to 21 km a week in late 2024 to about 57 km a week since October 2026, and since the fall of 2024 your median pace has held at 5:06 to 5:18 a kilometer at about the same heart rate.
- Evidence
- 457 runs after eight written rules, from units checked by identity and against the case file's own conversion; walks, indoor runs and other sports kept and counted as hours; volume compared per week because the first and last quarters are partial.
- How sure
- High for outdoor volume and pace; indoor runs have no trusted distance, May 2026 is mostly on another watch, and wrist heart rate is approximate. Training information, not medical advice.
- Action
- Treat the current 57 km a week as a new high, not a baseline; hold it for a few weeks before adding more, and record treadmill runs with the treadmill's distance in the notes.
- Test again
- Rerun the same notebook on each new export; the rules and the change log make the quarters comparable.
Track it
This study makes the record a table with rules. Add one row per quarter: test=quarter-summary, metric=run_km_per_week (and run_km, run_count, median_pace_min_per_km, median_hr_bpm), date = the last day of the quarter, device=gps-watch, note = "partial quarter" where it is. Keep data_dictionary.csv and change_log.csv beside the record; rows are appended, never edited.
With your own data
Request your own data export from your watch maker's website, find the activity files or summary table, and run the unit audit before anything else. Write down which time your watch uses for pace and how it stores the time zone. Remove names, places and positions before you share any of it.
Check the AI
Asked to "summarize this watch export by quarter", an assistant replied:
> I doubled avg_running_cadence to steps a minute, divided avg_step_length by 1000 for meters, > took pace as total_elapsed_time over total_distance, dropped 40 outliers slower than 7 min/km, > and found that the 2025 runs had a median pace of 7:36 a kilometer.
What is wrong with it?
The pace uses the wrong time. Elapsed time keeps every pause (a median 1.30 times the timer on runs), so the pace comes out far too slow; speed × timer time is the identity that matches the distance, and with the timer the 2025 quarters run 5:08 to 5:12 a kilometer. Dropping the slow sessions is a second problem: they are walks and indoor runs, real training to be counted as hours, not deleted.
Check yourself
Speed × timer time = distance holds for every run in the file. Why is an identity a better unit check than one value?
The first run has avg_step_length 980 and avg_running_cadence 86. What are the step length and cadence in everyday units?
Fifteen outdoor activities labelled running move at 140 steps a minute or less and slower than 7 min/km. What do the rules do with them?
A swim starts at 2024-06-30T00:55:25Z with utc_offset_h = −6. On what local date and time did it start?
Challenge: The same audit on a second watch
In April and May 2026 the athlete also wore a second watch from the same maker, and the case file athletes/jh/watch/ holds its 19 runs: one summary row each, already in SI units, and a per-second record file for every run. One run was recorded by both watches; the others are missing from this study's export.
- Write a data dictionary for its
activities.csvbefore you check a value, and test each unit with an identity (distance, moving time, pace, cadence). - Compute distance and moving time for each run from its per-second record file and compare with the summary row.
- Find the run both watches recorded and compare the two rows field by field. Decide which watch's row to keep, and write the rule down.
- Add the other runs to this study's quarterly summary without counting any run twice, and recompute the second quarter of 2026.
Hand in
- the notebook with its outputs
- the dictionary, the change log and the new summary as CSV files
- three sentences for the coach on what the second watch changes in the summary
How it is judged
- Every unit tested by an identity (30%)
- Rules written before they are applied, with a change log (25%)
- No run counted twice; partial quarters stated (25%)
- Honest about where the two watches disagree (20%)
Hints
The solution unlocks after you make an attempt. Comparing your own attempt with it is where the learning happens.
The same method in another field
A unit audit by identities is how engineers check a historian export from a plant: flow × time must equal the volume a tank level shows, power must equal voltage × current. The Machine Learning for Engineers page on data cleansing shows rule-based filters for the same kinds of fault.
Acknowledgements
Activity summaries from a COROS Training Hub archive of an adult male athlete in his mid-40s, from the course case file athletes/jh/coros-history/, prepared by data-repo/prepare_watch.py.
References
- Data-Driven Athletics, Chapter 2, The Oxygen Path (Section 2.3, the energy cost of running); Chapter 4, The data dictionary (Section 4.2.5) and Cleaning (Section 4.3).
Built 2026-12-02 with Python packages numpy 2.5.5, pandas 3.1.1, matplotlib 3.11.2. In the browser the same code runs on Pyodide, so the last digit can differ.