Skip to content

Commit e2c3852

Browse files
Nutcaseyclaude
andcommitted
Measure CGM coverage by time rather than by reading count
percentActive counted each reading as five minutes and capped the result at 100%, so a CGM reporting every minute always read 100%: the GMI's 70% check passed through gaps and the hours a day per band were overstated. Apple Health readings, thinned to one per 2.5 minutes, and Loop's own also disagreed at the boundary. Each reading now covers the time until the next, up to one five-minute slot. Current Setup's coverage check uses the same figure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QHnVP76NGVVgM8zLKUCSEG
1 parent f3ee252 commit e2c3852

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

‎Loop/Models/GlucoseStatistics.swift‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ struct GlucoseStatistics: Equatable {
6363
let coefficientOfVariation: Double?
6464
/// Fraction of time (0…1) spent in each band. Time-weighted, gap-capped.
6565
let timeInRange: [GlucoseBand: Double]
66-
/// Fraction (0…1) of the window for which CGM data is present.
66+
/// Fraction (0…1) of the window for which CGM data is present: each reading
67+
/// covers the time until the next, up to one `assumedCadence` slot, so a
68+
/// sensor that reports every minute doesn't count its gaps as covered.
6769
let percentActive: Double
6870
/// Per-time-of-day percentile bands for the AGP chart, ordered by time of day.
6971
let agpProfile: [AGPBand]
@@ -122,14 +124,16 @@ struct GlucoseStatistics: Equatable {
122124

123125
// Time-in-range: weight each reading by the time until the next reading,
124126
// capping each interval so an offline gap can't dominate the breakdown.
127+
// Percent active counts the same intervals, each capped at one reading's slot.
125128
let maxInterval = Self.assumedCadence * 2
126129
var durations: [GlucoseBand: TimeInterval] = [:]
130+
var covered: TimeInterval = 0
127131
for (index, reading) in sorted.enumerated() {
128132
let nextDate = index + 1 < sorted.count ? sorted[index + 1].date : end
129-
let duration = min(nextDate.timeIntervalSince(reading.date), maxInterval)
130-
guard duration > 0 else { continue }
131-
let band = GlucoseBand.classify(mgdl: values[index])
132-
durations[band, default: 0] += duration
133+
let interval = nextDate.timeIntervalSince(reading.date)
134+
guard interval > 0 else { continue }
135+
durations[GlucoseBand.classify(mgdl: values[index]), default: 0] += min(interval, maxInterval)
136+
covered += min(interval, Self.assumedCadence)
133137
}
134138
let totalDuration = durations.values.reduce(0, +)
135139
if totalDuration > 0 {
@@ -138,14 +142,8 @@ struct GlucoseStatistics: Equatable {
138142
timeInRange = [:]
139143
}
140144

141-
// Percent active: covered time (gap-capped) over the window length.
142145
let windowDuration = end.timeIntervalSince(start)
143-
if windowDuration > 0 {
144-
let covered = Double(values.count) * Self.assumedCadence
145-
percentActive = min(1, covered / windowDuration)
146-
} else {
147-
percentActive = 0
148-
}
146+
percentActive = windowDuration > 0 ? min(1, covered / windowDuration) : 0
149147

150148
// AGP: pool readings into time-of-day buckets across all days, then take
151149
// percentiles per bucket. Skip buckets with too few readings to be useful.

‎LoopTests/Models/GlucoseStatisticsTests.swift‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ final class GlucoseStatisticsTests: XCTestCase {
132132
XCTAssertEqual(stats.percentActive, 0.5, accuracy: 1e-6)
133133
}
134134

135+
/// A sensor that reports every minute covers only the minutes it reports, not five
136+
/// minutes a reading.
137+
func testPercentActiveWithOneMinuteReadings() {
138+
let start = midnight
139+
let end = start.addingTimeInterval(.hours(1))
140+
// Readings every minute for the first 20 minutes, then nothing.
141+
let readings = (0..<20).map { (date: start.addingTimeInterval(.minutes(Double($0))), mgdl: 120.0) }
142+
let stats = GlucoseStatistics(readings: readings, start: start, end: end)
143+
XCTAssertEqual(stats.percentActive, 24.0 / 60, accuracy: 1e-6) // 19 one-minute gaps, then one 5-minute slot
144+
XCTAssertEqual(stats.timeInRange[.target]!, 1, accuracy: 1e-6)
145+
}
146+
147+
/// Readings thinned to one every 2½ minutes and readings every minute give the same coverage.
148+
func testPercentActiveDoesNotDependOnCadence() {
149+
let start = midnight
150+
let end = start.addingTimeInterval(.hours(1))
151+
let everyMinute = (0..<60).map { (date: start.addingTimeInterval(.minutes(Double($0))), mgdl: 120.0) }
152+
let thinned = stride(from: 0.0, to: 60, by: 2.5).map { (date: start.addingTimeInterval(.minutes($0)), mgdl: 120.0) }
153+
XCTAssertEqual(GlucoseStatistics(readings: everyMinute, start: start, end: end).percentActive, 1, accuracy: 1e-6)
154+
XCTAssertEqual(GlucoseStatistics(readings: thinned, start: start, end: end).percentActive, 1, accuracy: 1e-6)
155+
}
156+
135157
// MARK: - AGP profile
136158

137159
func testAGPProfileBucketing() {

0 commit comments

Comments
 (0)