Skip to content

Calculate Insulin Effects accurately by taking into account ISF according to the schedule - #588

Open
motinis wants to merge 12 commits into
LoopKit:devfrom
motinis:accurate-insulin-effects
Open

motinis wants to merge 12 commits into
LoopKit:devfrom
motinis:accurate-insulin-effects

Conversation

@motinis

@motinis motinis commented Feb 22, 2026 •

Copy link
Copy Markdown

Loop has historically used the ISF at the start of a dose to determine the ISF for the lifetime of the dose, ignoring changes to ISF (whether according to the regular schedule or via overrides).

This contribution changes this to using ISF over the lifetime of the dose. It does this by splitting the calculation in 2 parts:

  1. The drop in IOB for a given dose over time
  2. Multiplying the IOB drop for each "bin" by the constant ISF for that bin (where bins are typically 5 minutes, but can be less, if there happens to be a change in ISF).

Dosage determination is handled by considering the glucose effect change that would happen if giving 1U, and then scaling this to match Loop's existing criteria.

Note that the interpolation during dose determination is done for consistency, even though unitGlucoseEffect should be aligned on the same dates. The interpolation logic was originally implemented in #562 before being generalized in motinis@3040b7b and simplified for 1 value - it is designed to support interpolation as part of a loop where we want to continue to search for the next relevant data point(s) from where we left off at the previous iteration (so as to have O(n) behavior in the length of the data and not O(n^2)).

oliverstory added a commit to oliverstory/LoopWorkspace that referenced this pull request Mar 9, 2026

var unitEffectsIndex = 0

// 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This comment should now read something like:
// For each prediction above target, determine the amount of insulin necessary to correct glucose based on the modeled effectiveness of insulin over the dose duration

/// - correctionRange: The schedule of glucose values used for correction
/// - date: The date the insulin correction is delivered
/// - suspendThreshold: The glucose value below which only suspension is returned
/// - sensitivity: The insulin sensitivity at the time of delivery

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This comment needs updating as 'sensitivity' is now the insulin sensitivity schedule (not sensitivity at the time of delivery)

@oliverstory oliverstory left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

See comments on lines 226 and 252 of DoseMath.swift, where code comments need updating due to the switch from using ISF at the time of a dose to using the ISF schedule.

@ps2

ps2 commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Mid-absorption ISF is part of the changes landing with the tidepool merge, and LoopAlgorithm package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants