Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add separate setting for sigmoid adjustment factor #114

Merged
merged 5 commits into from
May 5, 2024
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
2 changes: 1 addition & 1 deletion FreeAPS/Resources/javascript/bundle/determine-basal.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion FreeAPS/Resources/javascript/bundle/profile.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion FreeAPS/Resources/json/defaults/preferences.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"noisyCGMTargetMultiplier" : 1.3,
"suspend_zeros_iob" : false,
"maxDelta_bg_threshold" : 0.2,
"adjustmentFactor" : 0.5,
"adjustmentFactor" : 0.8,
"adjustmentFactorSigmoid" : 0.5,
"sigmoid" : false,
"enableDynamicCR" : false,
"useNewFormula" : false,
Expand Down
2 changes: 1 addition & 1 deletion FreeAPS/Sources/APS/APSManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ final class BaseAPSManager: APSManager, Injectable {
} else if preferences.useNewFormula, !preferences.sigmoid,!preferences.enableDynamicCR {
algo_ = "Dynamic ISF: Logarithmic"
}
let af = preferences.adjustmentFactor
let af = preferences.sigmoid ? preferences.adjustmentFactorSigmoid : preferences.adjustmentFactor
let insulin_type = preferences.curve
let buildDate = Bundle.main.buildDate
let version = Bundle.main.releaseVersionNumber
Expand Down
4 changes: 3 additions & 1 deletion FreeAPS/Sources/Models/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ struct Preferences: JSON {
var suspendZerosIOB: Bool = false
var timestamp: Date?
var maxDeltaBGthreshold: Decimal = 0.2
var adjustmentFactor: Decimal = 0.5
var adjustmentFactor: Decimal = 0.8
var adjustmentFactorSigmoid: Decimal = 0.5
var sigmoid: Bool = false
var enableDynamicCR: Bool = false
var useNewFormula: Bool = false
Expand Down Expand Up @@ -98,6 +99,7 @@ extension Preferences {
case suspendZerosIOB = "suspend_zeros_iob"
case maxDeltaBGthreshold = "maxDelta_bg_threshold"
case adjustmentFactor
case adjustmentFactorSigmoid
case sigmoid
case enableDynamicCR
case useNewFormula
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ extension PreferencesEditor {
displayName: NSLocalizedString("Adjustment Factor", comment: "Adjust Dynamic ISF constant"),
type: .decimal(keypath: \.adjustmentFactor),
infoText: NSLocalizedString(
"Adjust Dynamic ratios by a constant. Default is 0.5. The higher the value, the larger the correction of your ISF will be for a high or a low BG. Maximum correction is determined by the Autosens min/max settings. For Sigmoid function an adjustment factor of 0.4 - 0.5 is recommended to begin with. For the logaritmic formula threre is less consensus, but starting with 0.5 - 0.8 is more appropiate for most users",
"Adjust Dynamic ratios by a constant. Default is 0.8. The higher the value, the larger the correction of your ISF will be for a high or a low BG. Maximum correction is determined by the Autosens min/max settings.",
comment: "Adjust Dynamic ISF constant"
),
settable: self
Expand All @@ -122,6 +122,18 @@ extension PreferencesEditor {
),
settable: self
),
Field(
displayName: NSLocalizedString(
"Sigmoid Adjustment Factor",
comment: "Adjust Dynamic ISF constant for Sigmoid"
),
type: .decimal(keypath: \.adjustmentFactorSigmoid),
infoText: NSLocalizedString(
"Adjust Dynamic ratios by a constant. Default is 0.5. The higher the value, the larger the correction of your ISF will be for a high or a low BG. Maximum correction is determined by the Autosens min/max settings.",
comment: "Adjust Dynamic ISF constant for Sigmoid"
),
settable: self
),
Field(
displayName: NSLocalizedString(
"Weighted Average of TDD. Weight of past 24 hours:",
Expand Down
59 changes: 31 additions & 28 deletions open-iaps-oref/lib/determine-basal/determine-basal.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
const BG = glucose_status.glucose;
const useDynamicCR = preferences.enableDynamicCR;
const adjustmentFactor = preferences.adjustmentFactor;
const adjustmentFactorSigmoid = preferences.adjustmentFactorSigmoid;
const enable_sigmoid = preferences.sigmoid;
const currentMinTarget = profileTarget;
var exerciseSetting = false;
var log = "";
Expand Down Expand Up @@ -610,7 +612,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
}

var startLog = ", Dynamic ratios log: ";
var afLog = ", AF: " + adjustmentFactor;
var afLog = ", AF: " + (enable_sigmoid ? adjustmentFactorSigmoid : adjustmentFactor);
var bgLog = "BG: " + BG + " mg/dl (" + (BG * 0.0555).toPrecision(2) + " mmol/l)";
var formula = "";
var weightLog = "";
Expand Down Expand Up @@ -653,36 +655,37 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
// New logarithmic formula : var newRatio = profile.sens * adjustmentFactor * tdd * ln(( BG/insulinFactor) + 1 )) / 1800
//

const enable_sigmoid = preferences.sigmoid;
var sigmoidLog = ""

if (dynISFenabled) {
var newRatio = sensitivity * adjustmentFactor * tdd * Math.log(BG/insulinFactor+1) / 1800;
formula = ", Logarithmic formula";
}

// Sigmoid Function
if (dynISFenabled && enable_sigmoid) {
const as_min = minLimitChris;
const autosens_interval = maxLimitChris - as_min;
//Blood glucose deviation from set target (the lower BG target) converted to mmol/l to fit current formula.
const bg_dev = (BG - profileTarget) * 0.0555;
// Account for TDD of insulin. Compare last 2 hours with total data (up to 14 days)
var tdd_factor = tdd24h_14d_Ratio; // weighted average TDD / total data average TDD
var max_minus_one = maxLimitChris - 1;
// Avoid division by 0
if (maxLimitChris == 1) {
max_minus_one = maxLimitChris + 0.01 - 1;
// Logarithmic
if (!enable_sigmoid) {
var newRatio = sensitivity * adjustmentFactor * tdd * Math.log(BG/insulinFactor+1) / 1800;
formula = ", Logarithmic formula";
}
// Sigmoid
else {
const as_min = minLimitChris;
const autosens_interval = maxLimitChris - as_min;
//Blood glucose deviation from set target (the lower BG target) converted to mmol/l to fit current formula.
const bg_dev = (BG - profileTarget) * 0.0555;
// Account for TDD of insulin. Compare last 2 hours with total data (up to 14 days)
var tdd_factor = tdd24h_14d_Ratio; // weighted average TDD / total data average TDD
var max_minus_one = maxLimitChris - 1;
// Avoid division by 0
if (maxLimitChris == 1) {
max_minus_one = maxLimitChris + 0.01 - 1;
}
//Makes sigmoid factor(y) = 1 when BG deviation(x) = 0.
const fix_offset = (Math.log10(1/max_minus_one-as_min/max_minus_one) / Math.log10(Math.E));
//Exponent used in sigmoid formula
const exponent = bg_dev * adjustmentFactorSigmoid * tdd_factor + fix_offset;
// The sigmoid function
const sigmoid_factor = autosens_interval / (1 + Math.exp(-exponent)) + as_min;
newRatio = sigmoid_factor;
formula = ", Sigmoid function";
// Dynamic CR will be processed further down
}
//Makes sigmoid factor(y) = 1 when BG deviation(x) = 0.
const fix_offset = (Math.log10(1/max_minus_one-as_min/max_minus_one) / Math.log10(Math.E));
//Exponent used in sigmoid formula
const exponent = bg_dev * adjustmentFactor * tdd_factor + fix_offset;
// The sigmoid function
const sigmoid_factor = autosens_interval / (1 + Math.exp(-exponent)) + as_min;
newRatio = sigmoid_factor;
formula = ", Sigmoid function";
// Dynamic CR will be processed further down
}

var cr = carbRatio;
Expand Down
6 changes: 4 additions & 2 deletions open-iaps-oref/lib/profile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ function defaults ( ) {
, target_bg: false // set to an integer value in mg/dL to override pump min_bg
// autoISF variables
, smb_delivery_ratio: 0.5 //Default value: 0.5 Used if flexible delivery ratio is not used. This is another key OpenAPS safety cap, and specifies what share of the total insulin required can be delivered as SMB. This is to prevent people from getting into dangerous territory by setting SMB requests from the caregivers phone at the same time. Increase this experimental value slowly and with caution.
, adjustmentFactor: 1
, adjustmentFactor: 0.8
, adjustmentFactorSigmoid: 0.5
, useNewFormula: false
, enableDynamicCR: false
, sigmoid: false
, weightPercentage: 0.65
, tddAdjBasal: false // Enable adjustment of basal based on the ratio of 24 h : 7 day average TDD
, tddAdjBasal: false // Enable adjustment of basal based on the ratio of 24 h : 10 day average TDD
, enableSMB_high_bg: false // enable SMBs when a high BG is detected, based on the high BG target (adjusted or profile)
, enableSMB_high_bg_target: 110 // set the value enableSMB_high_bg will compare against to enable SMB. If BG > than this value, SMBs should enable.
, threshold_setting: 0.60 // Use a configurable threshold setting
Expand Down Expand Up @@ -102,6 +103,7 @@ function displayedDefaults () {
profile.smb_delivery_ratio = allDefaults.smb_delivery_ratio;
profile.maxDelta_bg_threshold = allDefaults.maxDelta_bg_threshold;
profile.adjustmentFactor = allDefaults.adjustmentFactor;
profile.adjustmentFactorSigmoid = allDefaults.adjustmentFactorSigmoid;
profile.useNewFormula = allDefaults.useNewFormula;
profile.enableDynamicCR = allDefaults.enableDynamicCR;
profile.sigmoid = allDefaults.sigmoid;
Expand Down
6 changes: 5 additions & 1 deletion oref0_source_version.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
oref0 branch: dev - git version: 5774155
oref0 branch: dev - git version: fa373c9

Last commits:
fa373c9 Merge pull request #22 from nightscout/tmhastings-tddAdjBasal
fc0ae69 tddAdjBasal pop-up correction
487bcbb Merge pull request #21 from MikePlante1/separate_adjustment_factors
5774155 Merge pull request #20 from MikePlante1/disable_smb_schedule
e3a8d73 Merge pull request #17 from MikePlante1/console_error_refactor
a29d9ce rename smbIsAlwaysOff to smbIsScheduledOff
b777c77 Separate Adjustment Factors for Logarithmic and Sigmoid
d898eb1 fix threshold console.error
b89a39b Merge pull request #16 from bjornoleh/webpack-update
b8aaf93 Merge pull request #15 from avouspierre/dev
Expand Down