Skip to content

Commit

Permalink
Fix issue with target override application (#685)
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 authored Jul 16, 2024
1 parent e2898de commit ae58355
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
30 changes: 10 additions & 20 deletions Loop/Managers/LoopDataManager+CarbAbsorption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,31 @@ extension LoopDataManager {

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

var overrides = temporaryPresetsManager.overrideHistory.getOverrideHistory(startDate: sensitivityStart, endDate: end)
let overrides = temporaryPresetsManager.overrideHistory.getOverrideHistory(startDate: sensitivityStart, endDate: end)

guard !sensitivity.isEmpty else {
throw LoopError.configurationError(.insulinSensitivitySchedule)
}

let sensitivityWithOverrides = overrides.apply(over: sensitivity) { (quantity, override) in
let value = quantity.doubleValue(for: .milligramsPerDeciliter)
return HKQuantity(
unit: .milligramsPerDeciliter,
doubleValue: value / override.settings.effectiveInsulinNeedsScaleFactor
)
}
let sensitivityWithOverrides = overrides.applySensitivity(over: sensitivity)

guard !basal.isEmpty else {
throw LoopError.configurationError(.basalRateSchedule)
}
let basalWithOverrides = overrides.apply(over: basal) { (value, override) in
value * override.settings.effectiveInsulinNeedsScaleFactor
}
let basalWithOverrides = overrides.applyBasal(over: basal)

guard !carbRatio.isEmpty else {
throw LoopError.configurationError(.carbRatioSchedule)
}
let carbRatioWithOverrides = overrides.apply(over: carbRatio) { (value, override) in
value * override.settings.effectiveInsulinNeedsScaleFactor
}
let carbRatioWithOverrides = overrides.applyCarbRatio(over: carbRatio)

let carbModel: CarbAbsorptionModel = FeatureFlags.nonlinearCarbModelEnabled ? .piecewiseLinear : .linear

// Overlay basal history on basal doses, splitting doses to get amount delivered relative to basal
let annotatedDoses = doses.annotated(with: basal)
let annotatedDoses = doses.annotated(with: basalWithOverrides)

let insulinEffects = annotatedDoses.glucoseEffects(
insulinSensitivityHistory: sensitivity,
insulinSensitivityHistory: sensitivityWithOverrides,
from: start.addingTimeInterval(-CarbMath.maximumAbsorptionTimeInterval).dateFlooredToTimeInterval(GlucoseMath.defaultDelta),
to: nil)

Expand All @@ -94,15 +84,15 @@ extension LoopDataManager {
// Carb Effects
let carbStatus = carbEntries.map(
to: insulinCounteractionEffects,
carbRatio: carbRatio,
insulinSensitivity: sensitivity
carbRatio: carbRatioWithOverrides,
insulinSensitivity: sensitivityWithOverrides
)

let carbEffects = carbStatus.dynamicGlucoseEffects(
from: end,
to: end.addingTimeInterval(InsulinMath.defaultInsulinActivityDuration),
carbRatios: carbRatio,
insulinSensitivities: sensitivity,
carbRatios: carbRatioWithOverrides,
insulinSensitivities: sensitivityWithOverrides,
absorptionModel: carbModel.model
)

Expand Down
20 changes: 4 additions & 16 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,34 +349,22 @@ final class LoopDataManager: ObservableObject {
throw LoopError.configurationError(.insulinSensitivitySchedule)
}

let sensitivityWithOverrides = overrides.apply(over: sensitivity) { (quantity, override) in
let value = quantity.doubleValue(for: .milligramsPerDeciliter)
return HKQuantity(
unit: .milligramsPerDeciliter,
doubleValue: value / override.settings.effectiveInsulinNeedsScaleFactor
)
}
let sensitivityWithOverrides = overrides.applySensitivity(over: sensitivity)

guard !basal.isEmpty else {
throw LoopError.configurationError(.basalRateSchedule)
}
let basalWithOverrides = overrides.apply(over: basal) { (value, override) in
value * override.settings.effectiveInsulinNeedsScaleFactor
}
let basalWithOverrides = overrides.applyBasal(over: basal)

guard !carbRatio.isEmpty else {
throw LoopError.configurationError(.carbRatioSchedule)
}
let carbRatioWithOverrides = overrides.apply(over: carbRatio) { (value, override) in
value * override.settings.effectiveInsulinNeedsScaleFactor
}
let carbRatioWithOverrides = overrides.applyCarbRatio(over: carbRatio)

guard !target.isEmpty else {
throw LoopError.configurationError(.glucoseTargetRangeSchedule)
}
let targetWithOverrides = overrides.apply(over: target) { (range, override) in
override.settings.targetRange ?? range
}
let targetWithOverrides = overrides.applyTarget(over: target, at: baseTime)

// Create dosing strategy based on user setting
let applicationFactorStrategy: ApplicationFactorStrategy = UserDefaults.standard.glucoseBasedApplicationFactorEnabled
Expand Down

0 comments on commit ae58355

Please sign in to comment.