Skip to content

Commit

Permalink
Revert "fix(lib/iob): Move value checks up to index.js"
Browse files Browse the repository at this point in the history
This reverts commit 80493da.

This is causing future IOB predictions to be incorrect (highly negative).
That causes the predBG lines to swoop up and to the right.
That results in (slightly) too much insulin being dosed in some situations.
  • Loading branch information
scottleibrand committed Mar 5, 2023
1 parent 673dbc6 commit 10f9e27
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 96 deletions.
42 changes: 38 additions & 4 deletions lib/iob/calculate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function iobCalc(treatment, time, curve, dia, peak) {
function iobCalc(treatment, time, curve, dia, peak, profile) {
// iobCalc returns two variables:
// activityContrib = units of treatment.insulin used in previous minute
// iobContrib = units of treatment.insulin still remaining at a given point in time
Expand All @@ -24,7 +24,7 @@ function iobCalc(treatment, time, curve, dia, peak) {
if (curve === 'bilinear') {
return iobCalcBilinear(treatment, minsAgo, dia); // no user-specified peak with this model
} else {
return iobCalcExponential(treatment, minsAgo, dia, peak);
return iobCalcExponential(treatment, minsAgo, dia, peak, profile);
}

} else { // empty return if (treatment.insulin) == False
Expand Down Expand Up @@ -80,9 +80,43 @@ function iobCalcBilinear(treatment, minsAgo, dia) {
}


function iobCalcExponential(treatment, minsAgo, dia, peak) {

function iobCalcExponential(treatment, minsAgo, dia, peak, profile) {

// Use custom peak time (in minutes) if value is valid
if ( profile.curve === "rapid-acting" ) {
if (profile.useCustomPeakTime === true && profile.insulinPeakTime !== undefined) {
if ( profile.insulinPeakTime > 120 ) {
console.error('Setting maximum Insulin Peak Time of 120m for',profile.curve,'insulin');
peak = 120;
} else if ( profile.insulinPeakTime < 50 ) {
console.error('Setting minimum Insulin Peak Time of 50m for',profile.curve,'insulin');
peak = 50;
} else {
peak = profile.insulinPeakTime;
}
} else {
peak = 75;
}
} else if ( profile.curve === "ultra-rapid" ) {
if (profile.useCustomPeakTime === true && profile.insulinPeakTime !== undefined) {
if ( profile.insulinPeakTime > 100 ) {
console.error('Setting maximum Insulin Peak Time of 100m for',profile.curve,'insulin');
peak = 100;
} else if ( profile.insulinPeakTime < 35 ) {
console.error('Setting minimum Insulin Peak Time of 35m for',profile.curve,'insulin');
peak = 35;
} else {
peak = profile.insulinPeakTime;
}
} else {
peak = 55;
}
} else {
console.error('Curve of',profile.curve,'is not supported.');
}
var end = dia * 60; // end of insulin activity, in minutes


var activityContrib = 0;
var iobContrib = 0;

Expand Down
96 changes: 8 additions & 88 deletions lib/iob/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,99 +17,19 @@ function generate (inputs, currentIOBOnly, treatments) {
//console.error(treatments.length, treatmentsWithZeroTemp.length);
//console.error(treatments[treatments.length-1], treatmentsWithZeroTemp[treatmentsWithZeroTemp.length-1])

// Determine peak, curve, and DIA values once and put them in opts to be consumed by ./total
var profile_data = inputs.profile

var curveDefaults = {
'bilinear': {
requireLongDia: false,
peak: 75 // not really used, but prevents having to check later
},
'rapid-acting': {
requireLongDia: true,
peak: 75,
tdMin: 300
},
'ultra-rapid': {
requireLongDia: true,
peak: 55,
tdMin: 300
},
};

var curve = "rapid-acting"; // start as 'rapid-acting'
var dia = profile_data.dia;
var peak = 0;

if (profile_data.curve !== undefined) {
curve = profile_data.curve.toLowerCase(); // replace it with profile value, if it exists
}

if (!(curve in curveDefaults)) { // check that the profile value is one of three expected values, else put it back to 'rapid-acting'
console.error('Unsupported curve function: "' + curve + '". Supported curves: "bilinear", "rapid-acting" (Novolog, Novorapid, Humalog, Apidra) and "ultra-rapid" (Fiasp, Lyumjev). Defaulting to "rapid-acting".');
curve = 'rapid-acting';
}

var defaults = curveDefaults[curve];

// force minimum DIA of 3h
if (dia < 3) {
console.error("Warning: adjusting DIA from",dia,"to minimum of 3 hours for bilinear curve");
dia = 3;
}

// Force minimum of 5 hour DIA when default requires a Long DIA.
if (defaults.requireLongDia && dia < 5) {
console.error("Warning: adjusting DIA from",dia,"to minimum of 5 hours for non-bilinear curve");
dia = 5;
}

// Use custom insulinPeakTime, if value is sensible
if ( curve === "rapid-acting" ) {
if (profile_data.useCustomPeakTime === true && profile_data.insulinPeakTime !== undefined) {
if ( profile_data.insulinPeakTime > 120 ) {
console.error("Warning: adjusting insulin peak time from",profile_data.insulinPeakTime,"to a maximum of 120m for",profile_data.curve,"insulin");
peak = 120;
} else if ( profile_data.insulinPeakTime < 50 ) {
console.error("Warning: adjusting insulin peak time from",profile_data.insulinPeakTime,"to a minimum of 50m for",profile_data.curve,"insulin");
peak = 50;
} else {
peak = profile_data.insulinPeakTime;
}
} else {
peak = curveDefaults[curve].peak;
}
} else if ( curve === "ultra-rapid" ) {
if (profile_data.useCustomPeakTime === true && profile_data.insulinPeakTime !== undefined) {
if ( profile_data.insulinPeakTime > 100 ) {
console.error("Warning: adjusting insulin peak time from",profile_data.insulinPeakTime,"to a maximum of 100m for",profile_data.curve,"insulin");
peak = 100;
} else if ( profile_data.insulinPeakTime < 35 ) {
console.error("Warning: adjusting insulin peak time from",profile_data.insulinPeakTime,"to a minimum of 30m for",profile_data.curve,"insulin");
peak = 35;
} else {
peak = profile_data.insulinPeakTime;
}
}
else {
peak = curveDefaults[curve].peak;
}
} // any other curve (e.g., bilinear) does not use 'peak'

var opts = {
treatments: treatments,
calculate: calculate,
peak: peak,
curve: curve,
dia: dia,
treatments: treatments
, profile: inputs.profile
, calculate: calculate
};

var optsWithZeroTemp = opts;
optsWithZeroTemp.treatments = treatmentsWithZeroTemp;

if ( inputs.autosens ) {
opts.autosens = inputs.autosens;
}
var optsWithZeroTemp = {
treatments: treatmentsWithZeroTemp
, profile: inputs.profile
, calculate: calculate
};

var iobArray = [];
//console.error(inputs.clock);
Expand Down
52 changes: 48 additions & 4 deletions lib/iob/total.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ function iobTotal(opts, time) {
var now = time.getTime();
var iobCalc = opts.calculate;
var treatments = opts.treatments;
var dia = opts.dia;
var peak = opts.peak;
var curve = opts.curve;
var profile_data = opts.profile;
var dia = profile_data.dia;
var peak = 0;
var iob = 0;
var basaliob = 0;
var bolusiob = 0;
Expand All @@ -20,12 +20,56 @@ function iobTotal(opts, time) {
//var time = new Date();
//}

// force minimum DIA of 3h
if (dia < 3) {
//console.error("Warning; adjusting DIA from",dia,"to minimum of 3 hours");
dia = 3;
}

var curveDefaults = {
'bilinear': {
requireLongDia: false,
peak: 75 // not really used, but prevents having to check later
},
'rapid-acting': {
requireLongDia: true,
peak: 75,
tdMin: 300
},
'ultra-rapid': {
requireLongDia: true,
peak: 55,
tdMin: 300
},
};

var curve = 'bilinear';

if (profile_data.curve !== undefined) {
curve = profile_data.curve.toLowerCase();
}

if (!(curve in curveDefaults)) {
console.error('Unsupported curve function: "' + curve + '". Supported curves: "bilinear", "rapid-acting" (Novolog, Novorapid, Humalog, Apidra) and "ultra-rapid" (Fiasp). Defaulting to "rapid-acting".');
curve = 'rapid-acting';
}

var defaults = curveDefaults[curve];

// Force minimum of 5 hour DIA when default requires a Long DIA.
if (defaults.requireLongDia && dia < 5) {
//console.error('Pump DIA must be set to 5 hours or more with the new curves, please adjust your pump. Defaulting to 5 hour DIA.');
dia = 5;
}

peak = defaults.peak;

treatments.forEach(function(treatment) {
if( treatment.date <= now ) {
var dia_ago = now - dia*60*60*1000;
if( treatment.date > dia_ago ) {
// tIOB = total IOB
var tIOB = iobCalc(treatment, time, curve, dia, peak);
var tIOB = iobCalc(treatment, time, curve, dia, peak, profile_data);
if (tIOB && tIOB.iobContrib) { iob += tIOB.iobContrib; }
if (tIOB && tIOB.activityContrib) { activity += tIOB.activityContrib; }
// basals look like either of these:
Expand Down

0 comments on commit 10f9e27

Please sign in to comment.