Skip to content

Commit

Permalink
Merge branch 'ChanceHarrison-chance/fix-1436' (#1437) into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleibrand committed Dec 9, 2022
2 parents 7c85d32 + 80493da commit abf9c6b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 94 deletions.
42 changes: 4 additions & 38 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, profile) {
function iobCalc(treatment, time, curve, dia, peak) {
// 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, profile) {
if (curve === 'bilinear') {
return iobCalcBilinear(treatment, minsAgo, dia); // no user-specified peak with this model
} else {
return iobCalcExponential(treatment, minsAgo, dia, peak, profile);
return iobCalcExponential(treatment, minsAgo, dia, peak);
}

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


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

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

var end = dia * 60; // end of insulin activity, in minutes
var activityContrib = 0;
var iobContrib = 0;

Expand Down
96 changes: 88 additions & 8 deletions lib/iob/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,99 @@ 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
, profile: inputs.profile
, calculate: calculate
treatments: treatments,
calculate: calculate,
peak: peak,
curve: curve,
dia: dia,
};

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: 4 additions & 48 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 profile_data = opts.profile;
var dia = profile_data.dia;
var peak = 0;
var dia = opts.dia;
var peak = opts.peak;
var curve = opts.curve;
var iob = 0;
var basaliob = 0;
var bolusiob = 0;
Expand All @@ -20,56 +20,12 @@ 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, profile_data);
var tIOB = iobCalc(treatment, time, curve, dia, peak);
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 abf9c6b

Please sign in to comment.