Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions DoseMathTests/DoseMathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class RecommendTempBasalTests: XCTestCase {
lastTempBasal: nil
)

XCTAssertEqual(1.425, dose!.unitsPerHour, accuracy: 1.0 / 40.0)
XCTAssertEqual(1.60, dose!.unitsPerHour, accuracy: 1.0 / 40.0)
XCTAssertEqual(TimeInterval(minutes: 30), dose!.duration)
}

Expand All @@ -365,7 +365,7 @@ class RecommendTempBasalTests: XCTestCase {
lastTempBasal: nil
)

XCTAssertEqual(1.475, dose!.unitsPerHour, accuracy: 1.0 / 40.0)
XCTAssertEqual(1.60, dose!.unitsPerHour, accuracy: 1.0 / 40.0)
XCTAssertEqual(TimeInterval(minutes: 30), dose!.duration)
}

Expand Down Expand Up @@ -588,7 +588,7 @@ class RecommendBolusTests: XCTestCase {
volumeRounder: fortyIncrementsPerUnitRounder
)

XCTAssertEqual(1.575, dose.amount)
XCTAssertEqual(1.7, dose.amount)

if case BolusRecommendationNotice.currentGlucoseBelowTarget(let glucose) = dose.notice! {
XCTAssertEqual(glucose.quantity.doubleValue(for: .milligramsPerDeciliter), 60)
Expand Down Expand Up @@ -657,7 +657,7 @@ class RecommendBolusTests: XCTestCase {
volumeRounder: fortyIncrementsPerUnitRounder
)

XCTAssertEqual(1.4, dose.amount)
XCTAssertEqual(1.575, dose.amount)
XCTAssertEqual(BolusRecommendationNotice.predictedGlucoseBelowTarget(minGlucose: glucose[1]), dose.notice!)
}

Expand All @@ -676,7 +676,7 @@ class RecommendBolusTests: XCTestCase {
volumeRounder: fortyIncrementsPerUnitRounder
)

XCTAssertEqual(0.575, dose.amount)
XCTAssertEqual(0.7, dose.amount)
}

func testStartVeryLowEndHigh() {
Expand Down Expand Up @@ -708,7 +708,7 @@ class RecommendBolusTests: XCTestCase {
maxBolus: maxBolus
)

XCTAssertEqual(1.575, dose.amount, accuracy: 1.0 / 40.0)
XCTAssertEqual(1.7, dose.amount, accuracy: 1.0 / 40.0)
}

func testHighAndFalling() {
Expand All @@ -724,7 +724,7 @@ class RecommendBolusTests: XCTestCase {
maxBolus: maxBolus
)

XCTAssertEqual(0.325, dose.amount, accuracy: 1.0 / 40.0)
XCTAssertEqual(0.4, dose.amount, accuracy: 1.0 / 40.0)
}

func testInRangeAndRising() {
Expand All @@ -740,7 +740,7 @@ class RecommendBolusTests: XCTestCase {
maxBolus: maxBolus
)

XCTAssertEqual(0.325, dose.amount, accuracy: 1.0 / 40.0)
XCTAssertEqual(0.4, dose.amount, accuracy: 1.0 / 40.0)

// Less existing temp

Expand Down Expand Up @@ -771,7 +771,7 @@ class RecommendBolusTests: XCTestCase {
volumeRounder: fortyIncrementsPerUnitRounder
)

XCTAssertEqual(0.275, dose.amount)
XCTAssertEqual(0.375, dose.amount)
}

func testHighAndRising() {
Expand All @@ -787,7 +787,7 @@ class RecommendBolusTests: XCTestCase {
maxBolus: maxBolus
)

XCTAssertEqual(1.25, dose.amount)
XCTAssertEqual(1.35, dose.amount, accuracy: 1.0 / 40.0)

// Use mmol sensitivity value
let insulinSensitivitySchedule = InsulinSensitivitySchedule(unit: HKUnit.millimolesPerLiter, dailyItems: [RepeatingScheduleValue(startTime: 0.0, value: 10.0 / 3)])!
Expand All @@ -802,7 +802,7 @@ class RecommendBolusTests: XCTestCase {
maxBolus: maxBolus
)

XCTAssertEqual(1.25, dose.amount, accuracy: 1.0 / 40.0)
XCTAssertEqual(1.35, dose.amount, accuracy: 1.0 / 40.0)
}

func testRiseAfterDIA() {
Expand Down
5 changes: 3 additions & 2 deletions Loop/Managers/DoseMath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ extension Collection where Element: GlucoseValue {
// Compute the dose required to bring this prediction to target:
// dose = (Glucose Δ) / (% effect × sensitivity)

let percentEffected = 1 - model.percentEffectRemaining(at: time)
// For 0 <= time <= effectDelay, assume a small amount effected. This will result in large unit recommendation rather than no recommendation at all.
let percentEffected = Swift.max(.ulpOfOne, 1 - model.percentEffectRemaining(at: time))
let effectedSensitivity = percentEffected * sensitivityValue
guard let correctionUnits = insulinCorrectionUnits(
fromValue: predictedGlucoseValue,
Expand Down Expand Up @@ -299,7 +300,7 @@ extension Collection where Element: GlucoseValue {
eventual.quantity < eventualGlucoseTargets.lowerBound
{
let time = min.startDate.timeIntervalSince(date)
// For time = 0, assume a small amount effected. This will result in large (negative) unit recommendation rather than no recommendation at all.
// For 0 <= time <= effectDelay, assume a small amount effected. This will result in large (negative) unit recommendation rather than no recommendation at all.
let percentEffected = Swift.max(.ulpOfOne, 1 - model.percentEffectRemaining(at: time))

guard let units = insulinCorrectionUnits(
Expand Down
17 changes: 16 additions & 1 deletion LoopCore/Insulin/ExponentialInsulinModelPreset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,20 @@ extension ExponentialInsulinModelPreset {
return .minutes(55)
}
}

var effectDelay: TimeInterval {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dm61 ideally, this should be user configurable (due to YDMV) IMHO. In many cases 15 or even 20 minutes might be more adequate (without the user needing to make a code change).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. The main purpose of this PR is to address a slight inconsistency in dosing versus prediction algorithms, as discussed in #1238. UI-related decisions and design are separate issues, beyond the scope of this particular PR.

switch self {
case .humalogNovologAdult:
return .minutes(10)
case .humalogNovologChild:
return .minutes(10)
case .fiasp:
return .minutes(10)
}
}

var model: InsulinModel {
return ExponentialInsulinModel(actionDuration: actionDuration, peakActivityTime: peakActivity)
return ExponentialInsulinModel(actionDuration: actionDuration, peakActivityTime: peakActivity, delay: effectDelay)
}
}

Expand All @@ -49,6 +60,10 @@ extension ExponentialInsulinModelPreset: InsulinModel {
public var effectDuration: TimeInterval {
return model.effectDuration
}

public var delay: TimeInterval {
return model.delay
}

public func percentEffectRemaining(at time: TimeInterval) -> Double {
return model.percentEffectRemaining(at: time)
Expand Down