← Allen Wang Code ↗

Data-quality tool · Python

Rebuilding local time matches Apple's daily totals on 99.2 % of days, up from 92.6 %

Apple Health exports stamp every record with the offset from the day you exported, count steps twice and score each night three ways. This analyzer fixes each trap. Here it runs on a synthetic export whose right answers are planted.

Synthetic export, no real health dataSeed 01,095 daysOne run

Days whose active energy matches Apple's own daily total

Share of 968 days, by error against Apple's daily summary · one synthetic run

Each day's hourly active energy is summed by calendar day, either cut at the export's -0700 offset or at the local time rebuilt from the time-zone tags, then compared with the total Apple stored for that day. “Within 5 %” is the match criterion.

Each trap · naive vs analyzer vs planted truth

The analyzer recovers the planted answer on 6 of 7 traps; time zones reach 99.2 % of days, not 100 %

The naive answer is what you get by reading the export literally. The planted truth is what the generator put in. The time-zone row misses on 8 days; the next section shows where.

Each trap on the synthetic export
TrapNaive answerAnalyzerPlanted truthMatches truth
iPhone and Watch count the same steps13,913,705 steps (plain sum)8,047,9078,047,907Yes
Every timestamp carries the -0700 export offset92.6 % of days match Apple's total99.2 %100 %No
Three sources score each Watch night8.14 h (longest source)6.98 h6.98 hYes
Days with no data at all5 days of 0 steps5 days NULL5 unknown daysYes
Older runs keep only a few heart-rate samples124 runs averaged from 4 samples124 runs left blank124 runs with no reliable averageYes
Zones change with a move, two trips and an alias1 zone (-0700 throughout)6 zone segments6 tagged eras, after 1 untagged era shown as assumedYes
Watch worn only part of the day113 partial days counted as worn113 of them excluded from worn-day series113 partial daysYes

The traps and the fixes have the same author. The generator plants the traps the analyzer was written to fix, so a match shows the fixes work on these traps. Real exports may hold traps this generator does not plant.

Time zones · where the misses are

The rebuild's remaining 8 misses all fall within 1 day of a zone change

With the export offset, the worst week averages 171 kcal a day of error (week ending Mar 17, 2024). The rebuild cuts that week to 48 kcal, but its own worst week is the next one, week ending Mar 24, 2024, at 106 kcal a day: the error moved by a week, because zone changes are placed to within about a day.

The export offset misses all year; the rebuild misses only next to a move or trip

Days more than 5 % off Apple's total, by distance to the nearest planted zone change · 6 changes · one synthetic run

Each bar counts the days one method gets more than 5 % wrong, grouped by how far the day is from the nearest zone change (a move, two trips, a return). The export offset is wrong on every day spent outside -0700, so its misses are spread out.

Correctness evidence

Removing any one of the 14 fixes makes its test fail: 14 of 14

A mutation check reverts each fix to its naive version, one at a time, and runs the tests. A fix whose removal no test notices would be unguarded.

Every fix and the test that catches it · 14 rows
Mutation check, results/mutants.json
Fix removedFileTest that fails
Use the export offset for every hourhealthlab/build.pytest_local_time_is_rebuilt_from_zone_tags_not_the_export_offset
One fixed offset per zone, ignoring DSThealthlab/build.pytest_each_hour_uses_its_own_dst_offset
Treat zone aliases as different placeshealthlab/build.pytest_zone_aliases_merge_into_one_place
Present untagged days as observedhealthlab/build.pytest_days_before_the_first_zone_tag_are_marked_assumed
Sum steps across sourceshealthlab/build.pytest_iphone_and_watch_steps_are_not_double_counted
Fill missing days with zerohealthlab/build.pytest_a_day_without_samples_is_null_not_zero
Longest sleep source winshealthlab/build.pytest_one_sleep_source_per_night_named_and_never_blended
Key a night by the date it startedhealthlab/build.pytest_one_sleep_source_per_night_named_and_never_blended
Match device names on a plain spacehealthlab/build.pytest_device_names_with_a_non_breaking_space_still_match
Hard-code the sleep tracking dateshealthlab/insights.pytest_sleep_tracking_and_stage_dates_come_from_the_data
Count any heart-rate hour as a worn dayhealthlab/build.pytest_partial_wear_days_are_not_treated_as_worn
Average a handful of workout sampleshealthlab/build.pytest_sparse_workout_heart_rate_is_left_blank
Count Correlation members as sampleshealthlab/parse.pytest_correlation_members_are_not_counted_as_samples
Append every import without matchinghealthlab/build.pytest_importing_an_overlapping_export_again_adds_nothing

Methodology

How this was measured

healthlab/synth.py writes an Apple Health export zip for 1,095 days from seed 0, with each trap planted and its right answer recorded. The analyzer imports the zip the same way it imports a real one, then each trap's answer is compared with the recorded truth.

For time zones, hourly active energy is summed by calendar day in two ways and compared with Apple's own daily summary on the 968 days that have one. A day matches if the error is at most 5 % of Apple's total. Weeks run Monday to Sunday and are labeled by their last day.

What this shows

  • Each fix recovers the planted answer for its trap; time zones get 99.2 % of days.
  • Every fix is needed: removing any one makes a test fail.
  • The rebuild's misses sit next to zone changes.

What it doesn't show

  • Accuracy on real exports: no real health data was used.
  • Traps this generator does not plant.
  • Run-to-run spread: one seed, one run.

Reproduce

Run it yourself

git clone https://github.com/allenwang0/health-export-lab.git
cd health-export-lab
make setup      # virtualenv with pinned dependencies (Python 3.11)
make test
make mutants    # writes results/mutants.json
make report     # rebuilds this page from a fresh synthetic run
make demo       # local app on the synthetic export
Run settings
FieldValue
Python3.11
duckdb2.0.0
pandas3.1.1
lxml6.1.3
Generatorhealthlab/synth.py
Seed0
Span1,095 days (2022-01-03 to 2025-01-01)
Days compared968 days with active energy in Apple's summary
Commandpython scripts/report.py
Commit2273689
Generated2026-11-26 21:34 UTC