Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5a4659c
Use maxBolus and ratio to set maxAutoIOB
marionbarker Dec 15, 2022
b022c4c
Merge branch 'dev' into wip/max-auto-iob
marionbarker Dec 18, 2022
a97e007
Merge branch 'dev' into wip/max-auto-iob
marionbarker Dec 19, 2022
2afe19a
increase ratioMaxAutoInsulinOnBoardToMaxBolus to 2.0
marionbarker Dec 19, 2022
0733eaf
remove print statements
marionbarker Dec 19, 2022
90b0470
restore LoopContants
marionbarker Dec 19, 2022
74fd253
modify name from maxAutoIOB to automaticDosingIOBLimit
marionbarker Dec 19, 2022
dd4ed94
Merge branch 'dev' into wip/max-auto-iob
marionbarker Dec 29, 2022
34bf4cb
Code cleanup in DoseMath
marionbarker Dec 29, 2022
d6698c9
configure new optional commands with default nil
marionbarker Dec 30, 2022
0c182a1
remove whitespace
marionbarker Dec 30, 2022
fc654ff
Add automaticIOBLimitTests
marionbarker Dec 30, 2022
87478d8
DoseMathTests: add new args to all automated dosing tests
marionbarker Dec 31, 2022
fae59f4
remove defaults so new parameters are required
marionbarker Dec 31, 2022
659a10d
Merge branch 'dev' into wip/max-auto-iob
marionbarker Dec 31, 2022
ea948df
Modify method for providing insulinOnBoard in LoopDataManager
marionbarker Jan 2, 2023
bdbd78f
Merge branch 'dev' into wip/max-auto-iob
marionbarker Jan 2, 2023
800eba0
AlertManagerTests: add new parameter
marionbarker Jan 2, 2023
9ac85c3
Merge branch 'dev' into wip/max-auto-iob
marionbarker Jan 2, 2023
0ef5008
match whitespace
marionbarker Jan 2, 2023
c902fbe
`insulinOnBoardValue` -> `insulinOnBoard` for logging purposes
novalegra Jan 3, 2023
8699730
Add test for autobolus clamping
novalegra Jan 3, 2023
3c7c65b
Improve readability of dose clamping logic
novalegra Jan 3, 2023
97b7d8d
Merge pull request #1 from novalegra/max-auto-iob
marionbarker Jan 3, 2023
61bea9d
DoseMathTests: use non-zero value for insulinOnBoard
marionbarker Jan 4, 2023
01c775c
DoseMathTests: move insulinOnBoard internal to test functions
marionbarker Jan 5, 2023
027d5e8
Merge branch 'wip/max-auto-iob' of https://github.com/marionbarker/Lo…
ps2 Feb 10, 2023
c6323df
Move IOB limit handling into recommendedAutomaticDose, and recommende…
ps2 Feb 18, 2023
caf2709
Temp basals limited by iob max
ps2 Feb 18, 2023
374160f
Cleanup
ps2 Feb 18, 2023
e041840
Remove unintentional edit
ps2 Feb 18, 2023
6b83d2f
Fix maxThirtyMinuteRateToKeepIOBBelowLimit calculation
marionbarker Feb 18, 2023
cf808a8
Adjust IOB clamping for temp basals to be relative to scheduled basal
ps2 Feb 19, 2023
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
38 changes: 31 additions & 7 deletions Loop/Managers/DoseMath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ extension Collection where Element: GlucoseValue {
at date: Date,
suspendThreshold: HKQuantity,
sensitivity: HKQuantity,
model: InsulinModel
model: InsulinModel,
automaticDosingIOBLimit: Double?,
insulinOnBoard: Double?
) -> InsulinCorrection? {
var minGlucose: GlucoseValue?
var eventualGlucose: GlucoseValue?
Expand Down Expand Up @@ -328,8 +330,20 @@ extension Collection where Element: GlucoseValue {
units: units
)
} else if eventual.quantity > eventualGlucoseTargets.upperBound,
let minCorrectionUnits = minCorrectionUnits, let correctingGlucose = correctingGlucose
var minCorrectionUnits = minCorrectionUnits, let correctingGlucose = correctingGlucose
{
// Limit automatic dosing to prevent insulinOnBoard > automaticDosingIOBLimit
if minCorrectionUnits > 0 &&
insulinOnBoard != nil &&
automaticDosingIOBLimit != nil &&
Comment thread
marionbarker marked this conversation as resolved.
Outdated
automaticDosingIOBLimit! > 0 {
let checkAutomaticDosing = automaticDosingIOBLimit! - (insulinOnBoard! + minCorrectionUnits)
if checkAutomaticDosing < 0 {
// TO DO - nice to have logging but not required
minCorrectionUnits = Swift.max(minCorrectionUnits+checkAutomaticDosing, 0)
}
}

return .aboveRange(
min: min,
correcting: correctingGlucose,
Expand Down Expand Up @@ -371,14 +385,18 @@ extension Collection where Element: GlucoseValue {
rateRounder: ((Double) -> Double)? = nil,
isBasalRateScheduleOverrideActive: Bool = false,
duration: TimeInterval = .minutes(30),
continuationInterval: TimeInterval = .minutes(11)
continuationInterval: TimeInterval = .minutes(11),
insulinOnBoard: Double?,
automaticDosingIOBLimit: Double?
) -> TempBasalRecommendation? {
let correction = self.insulinCorrection(
to: correctionRange,
at: date,
suspendThreshold: suspendThreshold ?? correctionRange.quantityRange(at: date).lowerBound,
sensitivity: sensitivity.quantity(at: date),
model: model
model: model,
automaticDosingIOBLimit: automaticDosingIOBLimit,
insulinOnBoard: insulinOnBoard
)

let scheduledBasalRate = basalRates.value(at: date)
Expand Down Expand Up @@ -439,14 +457,18 @@ extension Collection where Element: GlucoseValue {
rateRounder: ((Double) -> Double)? = nil,
isBasalRateScheduleOverrideActive: Bool = false,
duration: TimeInterval = .minutes(30),
continuationInterval: TimeInterval = .minutes(11)
continuationInterval: TimeInterval = .minutes(11),
insulinOnBoard: Double?,
automaticDosingIOBLimit: Double?
) -> AutomaticDoseRecommendation? {
guard let correction = self.insulinCorrection(
to: correctionRange,
at: date,
suspendThreshold: suspendThreshold ?? correctionRange.quantityRange(at: date).lowerBound,
sensitivity: sensitivity.quantity(at: date),
model: model
model: model,
automaticDosingIOBLimit: automaticDosingIOBLimit,
insulinOnBoard: insulinOnBoard
) else {
return nil
}
Expand Down Expand Up @@ -516,7 +538,9 @@ extension Collection where Element: GlucoseValue {
at: date,
suspendThreshold: suspendThreshold ?? correctionRange.quantityRange(at: date).lowerBound,
sensitivity: sensitivity.quantity(at: date),
model: model
model: model,
automaticDosingIOBLimit: nil,
insulinOnBoard: nil
) else {
return ManualBolusRecommendation(amount: 0, pendingInsulin: pendingInsulin)
}
Expand Down
11 changes: 9 additions & 2 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,9 @@ extension LoopDataManager {

let dosingRecommendation: AutomaticDoseRecommendation?

// automaticDosingIOBLimit calculated from the user entered maxBolus
let automaticDosingIOBLimit = maxBolus! * 2.0

switch settings.automaticDosingStrategy {
case .automaticBolus:
let volumeRounder = { (_ units: Double) in
Expand All @@ -1634,7 +1637,9 @@ extension LoopDataManager {
lastTempBasal: lastTempBasal,
volumeRounder: volumeRounder,
rateRounder: rateRounder,
isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true
isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true,
insulinOnBoard: dosingDecision.insulinOnBoard?.value,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

DosingDecision's responsibility is for recording the state of things during a dosing decision. I think use its value as an input here is mixing up responsibilities too much, and will make it hard to extract dosing decision if we change how that's generated. The value should probably be passed to this method (preferred, to move more towards a functional implementation of the Loop algorithm, rather than the current stateful one), or the value should be assigned to a member variable on LDM.

Regardless of how this value gets here, failure to retrieve IOB should now be considered an error, as it is part of the Loop dosing algorithm. I think it was previously being treated as a warning.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Preliminary modification made, awaiting review.
Working on LoopDataManagerTests.swift, nothing pushed on this yet.

automaticDosingIOBLimit: automaticDosingIOBLimit
)
case .tempBasalOnly:
let temp = predictedGlucose.recommendedTempBasal(
Expand All @@ -1647,7 +1652,9 @@ extension LoopDataManager {
maxBasalRate: maxBasal!,
lastTempBasal: lastTempBasal,
rateRounder: rateRounder,
isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true
isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true,
insulinOnBoard: dosingDecision.insulinOnBoard?.value,
automaticDosingIOBLimit: automaticDosingIOBLimit
)
dosingRecommendation = AutomaticDoseRecommendation(basalAdjustment: temp)
}
Expand Down