Skip to content

Commit

Permalink
LOOP-4665 Fix bugs relating to determining span of time to use for IS…
Browse files Browse the repository at this point in the history
…F timeline (#716)

* Fix a couple of bugs in determining span of time to use for ISF timeline

* Use isf interval helper, and fix bugs with bolus preview and forecast details
  • Loading branch information
ps2 authored Oct 16, 2024
1 parent 7804046 commit cc8f328
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ final class LoopDataManager: ObservableObject {
throw LoopError.configurationError(.basalRateSchedule)
}

let forecastEndTime = baseTime.addingTimeInterval(InsulinMath.defaultInsulinActivityDuration).dateCeiledToTimeInterval(.minutes(GlucoseMath.defaultDelta))
let forecastEndTime = baseTime.addingTimeInterval(InsulinMath.defaultInsulinActivityDuration).dateCeiledToTimeInterval(GlucoseMath.defaultDelta)

let carbsStart = baseTime.addingTimeInterval(CarbMath.dateAdjustmentPast + .minutes(-1)) // additional minute to handle difference in seconds between carb entry and carb ratio

Expand All @@ -366,9 +366,24 @@ final class LoopDataManager: ObservableObject {

let glucose = try await glucoseStore.getGlucoseSamples(start: carbsStart, end: baseTime)

let sensitivityStart = min(carbsStart, dosesStart)
let dosesWithModel = doses.map { $0.simpleDose(with: insulinModel(for: $0.insulinType)) }

let sensitivity = try await settingsProvider.getInsulinSensitivityHistory(startDate: sensitivityStart, endDate: forecastEndTime)
let recommendationInsulinModel = insulinModel(for: deliveryDelegate?.pumpInsulinType ?? .novolog)

let recommendationEffectInterval = DateInterval(
start: baseTime,
duration: recommendationInsulinModel.effectDuration
)
let neededSensitivityTimeline = LoopAlgorithm.timelineIntervalForSensitivity(
doses: dosesWithModel,
glucoseHistoryStart: glucose.first?.startDate ?? baseTime,
recommendationEffectInterval: recommendationEffectInterval
)

let sensitivity = try await settingsProvider.getInsulinSensitivityHistory(
startDate: neededSensitivityTimeline.start,
endDate: neededSensitivityTimeline.end
)

let target = try await settingsProvider.getTargetRangeHistory(startDate: baseTime, endDate: forecastEndTime)

Expand All @@ -382,7 +397,7 @@ final class LoopDataManager: ObservableObject {
throw LoopError.configurationError(.maximumBasalRatePerHour)
}

var overrides = temporaryPresetsManager.overrideHistory.getOverrideHistory(startDate: sensitivityStart, endDate: forecastEndTime)
var overrides = temporaryPresetsManager.overrideHistory.getOverrideHistory(startDate: neededSensitivityTimeline.start, endDate: forecastEndTime)

// Bug (https://tidepool.atlassian.net/browse/LOOP-4759) pre-meal is not recorded in override history
// So currently we handle automatic forecast by manually adding it in, and when meal bolusing, we do not do this.
Expand Down Expand Up @@ -433,7 +448,7 @@ final class LoopDataManager: ObservableObject {

return StoredDataAlgorithmInput(
glucoseHistory: glucose,
doses: doses.map { $0.simpleDose(with: insulinModel(for: $0.insulinType)) },
doses: dosesWithModel,
carbEntries: carbEntries,
predictionStart: baseTime,
basal: basalWithOverrides,
Expand All @@ -446,7 +461,7 @@ final class LoopDataManager: ObservableObject {
useIntegralRetrospectiveCorrection: UserDefaults.standard.integralRetrospectiveCorrectionEnabled,
includePositiveVelocityAndRC: true,
carbAbsorptionModel: carbAbsorptionModel,
recommendationInsulinModel: insulinModel(for: deliveryDelegate?.pumpInsulinType ?? .novolog),
recommendationInsulinModel: recommendationInsulinModel,
recommendationType: .manualBolus,
automaticBolusApplicationFactor: effectiveBolusApplicationFactor)
}
Expand Down Expand Up @@ -1018,6 +1033,7 @@ extension StoredDataAlgorithmInput {
carbRatio: carbRatio,
algorithmEffectsOptions: effectsOptions,
useIntegralRetrospectiveCorrection: self.useIntegralRetrospectiveCorrection,
useMidAbsorptionISF: true,
carbAbsorptionModel: self.carbAbsorptionModel.model
)
return prediction.glucose
Expand Down

0 comments on commit cc8f328

Please sign in to comment.