kafsemo.org

Extracting stats from Apple HealthKit

2026-11-30

I track my swimming with an Apple Watch. It’s useful just for counting lengths, and will also record times. The Fitness app will show times for an individual workout, but not if I’m getting faster. After tweaking my routine, what I want to know is quite specific: for workouts in a specific pool size, where I was only swimming a specific stroke, what does the distribution of length times do over time? The stock Fitness app is absolutely not going to show that. I need to write at least my own query.

Luckily, I can export all health data; unluckily, it arrives as a complex document, with absolutely all health information included for all time. Luckily, it’s a reasonably well-structured document that very much expects you to extract subsets.

Workouts are recorded as a structured entity, with several distinct times and durations. The HealthKit documentation describes the classes in the actual runtime; I’m assuming the XML representation is a simple transliteration:

<Workout workoutActivityType="HKWorkoutActivityTypeSwimming" duration="..." durationUnit="min" sourceName="..." sourceVersion="..." device="..." creationDate="..." startDate="..." endDate="...">
  <MetadataEntry key="HKLapLength" value="50 m"/>
...
  <WorkoutEvent type="HKWorkoutEventTypeSegment" date="..." duration="..." durationUnit="min">
...
  </WorkoutEvent>
  <WorkoutEvent type="HKWorkoutEventTypeLap" date="..." duration="..." durationUnit="min">
   <MetadataEntry key="HKSwimmingStrokeStyle" value="2"/>
  </WorkoutEvent>
...
  <WorkoutEvent type="HKWorkoutEventTypePause" date="..."/>
...
</Workout>

Specifically, a HKWorkout contains several HKWorkoutEvents. Some of those HKWorkoutEvents may be segments, indicated by an HKWorkoutEventType of segment, which will overlap the individual events that are Laps. Of course, multiple levels in this hierarchy have their own start, finish and duration. (Is ‘finish’ always ‘start’ + ‘duration’? Sometimes!) The schema has changed over time; laps used to be implicitly terminated by the next lap, or the end of the workout; in watch OS 4, they gained a duration.

I appreciate the goal here: a single data model to capture a diverse range of activities with enough generalisation to avoid rebuilding everything for each activity. However, my goal here is far simpler.

I don’t know exactly which timing the Fitness app surfaces in the UI, and I suspect that the XML representation rounding stamps to seconds is going to make it hard to reproduce exactly. However, by eye, the closest fit seemed to be using each HKWorkoutEventTypeLap’s start time, from its date attribute, finished by the workout’s end time.

I suspect that the first and last laps are miscounted, and any pauses are over-counted; but that should be lost in the noise.

That was enough to get me data to plot a chart of box plots:

Some later times are faster than earlier ones, but there’s a lot of noise

At this granularity there’s too much noise. Aggregating more coarsely looks a lot like this:

Four out of five boxplot show a clear descending trend, with a single outlier that’s significantly higher

That certainly looks like an improvement.

Code’s over here.

(Music: Madvillain, “All Caps”)
(More from this year, or the front page? [K])