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
14 changes: 7 additions & 7 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.450, 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.625, 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.625, 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.625, dose.amount, accuracy: 1.0 / 40.0)
}

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

XCTAssertEqual(1.25, dose.amount)
XCTAssertEqual(1.30, 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.30, dose.amount, accuracy: 1.0 / 40.0)
}

func testRiseAfterDIA() {
Expand Down
8 changes: 5 additions & 3 deletions Loop/Managers/DoseMath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ extension Collection where Element: GlucoseValue {
let unit = correctionRange.unit
let sensitivityValue = sensitivity.doubleValue(for: unit)
let suspendThresholdValue = suspendThreshold.doubleValue(for: unit)
let delay = TimeInterval(minutes: 10)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This value should not be hard-coded. We are also using a default value (of 10m) in LoopKit; if one changes, so should the other, so they should be controlled by a settings value, or at least a single value specified in one place.

Copy link
Contributor Author

@dm61 dm61 Jan 15, 2020

Choose a reason for hiding this comment

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

Yup, I was going to look into how to get that value from LoopKit, but then decided to just put this out so you can quickly take a look.

I'd prefer including the delay in the insulin model, where it really belongs ([edit] (does not look very difficult, so will work on this).


// For each prediction above target, determine the amount of insulin necessary to correct glucose based on the modeled effectiveness of the insulin at that time
for prediction in self {
Expand Down Expand Up @@ -267,7 +268,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 <= delay, 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 - delay))
let effectedSensitivity = percentEffected * sensitivityValue
guard let correctionUnits = insulinCorrectionUnits(
fromValue: predictedGlucoseValue,
Expand Down Expand Up @@ -299,8 +301,8 @@ 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.
let percentEffected = Swift.max(.ulpOfOne, 1 - model.percentEffectRemaining(at: time))
// For 0 <= time <= delay, 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 - delay))

guard let units = insulinCorrectionUnits(
fromValue: min.quantity.doubleValue(for: unit),
Expand Down