diff --git a/lib/iob/calculate.js b/lib/iob/calculate.js index 904e953f4..15051824d 100644 --- a/lib/iob/calculate.js +++ b/lib/iob/calculate.js @@ -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 @@ -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 @@ -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; diff --git a/lib/iob/index.js b/lib/iob/index.js index fd64e3473..7334fc485 100644 --- a/lib/iob/index.js +++ b/lib/iob/index.js @@ -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); diff --git a/lib/iob/total.js b/lib/iob/total.js index bc2c39bc1..6ab6e3573 100644 --- a/lib/iob/total.js +++ b/lib/iob/total.js @@ -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; @@ -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: diff --git a/package.json b/package.json index c7cbcb33b..bffeac98a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oref0", - "version": "0.7.0", + "version": "0.7.1", "description": "openaps oref0 reference implementation of the reference design", "scripts": { "test": "make test",