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

fix(lib/iob): Move value checks up to index.js #1437

Closed
wants to merge 3 commits into from
Closed
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
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oref0",
"version": "0.7.0",
"version": "0.7.1",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is an artifact? Will need to check it before merging.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure! All I can say is that it wasn't in my original changeset.

Copy link
Contributor

Choose a reason for hiding this comment

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

It was because you based your change on master instead of dev, so shows up as a diff after I rebased to dev.

Copy link
Author

@ChanceHarrison ChanceHarrison Dec 8, 2022

Choose a reason for hiding this comment

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

Ah, of course. My bad on the base branch. Fortunately, it doesn't seem to make a significant difference in this context (beyond the version diff in package.json). Let me know if you would like me to do anything on my end.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll probably git reset --hard origin/dev and cherry-pick your commits, then git push -f it, unless you beat me to it.

"description": "openaps oref0 reference implementation of the reference design",
"scripts": {
"test": "make test",
Expand Down