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

predBGs with zero temps #714

Merged
merged 46 commits into from Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
fd4d46c
remove all explicit references to basaliob
scottleibrand Sep 24, 2017
e4e84d5
add enableSMB_always preference, but don't add to displayedDefaults yet
scottleibrand Sep 24, 2017
5900502
comment out all bolus snooze code
scottleibrand Sep 26, 2017
e0be922
comment out bolus snooze / IOB code
scottleibrand Sep 26, 2017
9c0a0e9
comment out bolus snooze code
scottleibrand Sep 26, 2017
250da95
comment out bolus snooze code
scottleibrand Sep 26, 2017
fb855cb
comment out bolus snooze code
scottleibrand Sep 26, 2017
3d61acb
remove bolus snooze code
scottleibrand Sep 26, 2017
d232520
comment out bolus snooze code
scottleibrand Sep 26, 2017
c284686
comment out bolus snooze test
scottleibrand Sep 26, 2017
1ead519
uncomment eventualBG < max_bg code and remove snoozeBG
scottleibrand Sep 26, 2017
2c98dcf
comment out bolus snooze tests
scottleibrand Sep 26, 2017
65bfe47
rate <=0
scottleibrand Sep 26, 2017
91841f5
Merge branch 'dev' into no-basal-v-bolus
scottleibrand Sep 29, 2017
675f698
no more need to track hightempinsulin
scottleibrand Sep 30, 2017
1a6287b
no more need to track netbasalinsulin
scottleibrand Sep 30, 2017
5db2119
no more need to track microBolus(IOB|Insulin)
scottleibrand Sep 30, 2017
bed1855
remove one more bolus snooze reference
scottleibrand Oct 7, 2017
4bdc167
Merge branch 'dev' into no-basal-v-bolus
scottleibrand Oct 7, 2017
cfd1876
re-add basaliob and netbasalinsulin etc.
scottleibrand Oct 7, 2017
0c74c76
add IOBWithZeroTemps
scottleibrand Oct 9, 2017
35b5b16
calculate iobWithZeroTemp
scottleibrand Oct 9, 2017
e6dda0e
include the entire iobWithZeroTemp array
scottleibrand Oct 9, 2017
3f48c61
calculate ZTpredBGs
scottleibrand Oct 9, 2017
adc9dc2
show all ZTpredBGs for now
scottleibrand Oct 9, 2017
b74856a
use predZTBGI for ZTpredBGs
scottleibrand Oct 9, 2017
a6a6664
stop displaying ZTpredBGs once they're rising and above target
scottleibrand Oct 9, 2017
2c376a8
calculate and log minZTUAMPredBG
scottleibrand Oct 9, 2017
a3c0e6d
use minZTUAMPredBG to adjust dosing
scottleibrand Oct 9, 2017
37f507d
debugging output
scottleibrand Oct 9, 2017
a5351c0
only show 30m of ZTpredBGs if rising above target
scottleibrand Oct 9, 2017
5e400e4
disable aCOB for now
scottleibrand Oct 10, 2017
6d0d238
use >= to make it more clear when ZTpredBGs have flattened out
scottleibrand Oct 10, 2017
e64df2f
fix logic
scottleibrand Oct 9, 2017
44bdd48
if avgPredBG is below minZTGuardBG, bring it up to that level
scottleibrand Oct 10, 2017
31ee3de
start the zero temp 1m in the future to avoid clock skew
scottleibrand Oct 12, 2017
754f9c5
if minZTGuardBG is between threshold and target, blend in the averaging
scottleibrand Oct 13, 2017
7518a06
move minZTUAMPredBG outside of if (meal_data.carbs)
scottleibrand Oct 13, 2017
b20e116
round minZTUAMPredBG for readability
scottleibrand Oct 13, 2017
9bf842e
round predCIs for readability
scottleibrand Oct 13, 2017
43c88e8
round remainingCIs for readability
scottleibrand Oct 13, 2017
af26616
align predCIs and remainingCIs
scottleibrand Oct 13, 2017
5278849
fix comment
scottleibrand Oct 15, 2017
cc3caab
copy old_preferences.json for reference
scottleibrand Oct 18, 2017
3662308
Merge branch 'dev' into predBGWithZeroTemps
scottleibrand Oct 18, 2017
89cfda1
disable adv_target_adjustments and enableUAM by default
scottleibrand Oct 18, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/oref0-setup.sh
Expand Up @@ -581,8 +581,8 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
#fi

cd $directory || die "Can't cd $directory"
#if [[ "$max_iob" == "0" && -z "$max_daily_safety_multiplier" && -z "$current_basal_safety_multiplier" && -z "$bolussnooze_dia_divisor" && -z "$min_5m_carbimpact" ]]; then
if [[ "$max_iob" == "0" && -z "$max_daily_safety_multiplier" && -z "$current_basal_safety_multiplier" && -z "$min_5m_carbimpact" ]]; then
cp preferences.json old_preferences.json
oref0-get-profile --exportDefaults > preferences.json || die "Could not run oref0-get-profile"
else
preferences_from_args=()
Expand Down
73 changes: 56 additions & 17 deletions lib/determine-basal/determine-basal.js
Expand Up @@ -304,9 +304,11 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
var aCOBpredBGs = [];
var IOBpredBGs = [];
var UAMpredBGs = [];
var ZTpredBGs = [];
COBpredBGs.push(bg);
aCOBpredBGs.push(bg);
IOBpredBGs.push(bg);
ZTpredBGs.push(bg);
UAMpredBGs.push(bg);

// enable SMB whenever we have COB or UAM is enabled
Expand Down Expand Up @@ -421,6 +423,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
var minCOBGuardBG = 999;
var minUAMGuardBG = 999;
var minIOBGuardBG = 999;
var minZTGuardBG = 999;
var minPredBG;
var avgPredBG;
var IOBpredBG = eventualBG;
Expand All @@ -432,6 +435,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
var lastIOBpredBG;
var lastCOBpredBG;
var lastUAMpredBG;
var lastZTpredBG;
var UAMduration = 0;
var remainingCItotal = 0;
var remainingCIs = [];
Expand All @@ -440,11 +444,13 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
iobArray.forEach(function(iobTick) {
//console.error(iobTick);
predBGI = round(( -iobTick.activity * sens * 5 ), 2);
predZTBGI = round(( -iobTick.iobWithZeroTemp.activity * sens * 5 ), 2);
// for IOBpredBGs, predicted deviation impact drops linearly from current deviation down to zero
// over 60 minutes (data points every 5m)
predDev = ci * ( 1 - Math.min(1,IOBpredBGs.length/(60/5)) );
IOBpredBG = IOBpredBGs[IOBpredBGs.length-1] + predBGI + predDev;
//IOBpredBG = IOBpredBGs[IOBpredBGs.length-1] + predBGI;
// calculate predBGs with long zero temp without deviations
ZTpredBG = ZTpredBGs[ZTpredBGs.length-1] + predZTBGI;
// for COBpredBGs, predicted carb impact drops linearly from current carb impact down to zero
// eventually accounting for all carbs (if they can be absorbed over DIA)
predCI = Math.max(0, Math.max(0,ci) * ( 1 - COBpredBGs.length/Math.max(cid*2,1) ) );
Expand All @@ -455,8 +461,8 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
var intervals = Math.min( COBpredBGs.length, (remainingCATime*12)-COBpredBGs.length );
var remainingCI = Math.max(0, intervals / (remainingCATime/2*12) * remainingCIpeak );
remainingCItotal += predCI+remainingCI;
remainingCIs.push(round(remainingCI,1));
predCIs.push(round(predCI,1));
remainingCIs.push(round(remainingCI,0));
predCIs.push(round(predCI,0));
//process.stderr.write(round(predCI,1)+"+"+round(remainingCI,1)+" ");
COBpredBG = COBpredBGs[COBpredBGs.length-1] + predBGI + Math.min(0,predDev) + predCI + remainingCI;
aCOBpredBG = aCOBpredBGs[aCOBpredBGs.length-1] + predBGI + Math.min(0,predDev) + predACI;
Expand All @@ -480,10 +486,12 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
if ( COBpredBGs.length < 48) { COBpredBGs.push(COBpredBG); }
if ( aCOBpredBGs.length < 48) { aCOBpredBGs.push(aCOBpredBG); }
if ( UAMpredBGs.length < 48) { UAMpredBGs.push(UAMpredBG); }
if ( ZTpredBGs.length < 48) { ZTpredBGs.push(ZTpredBG); }
// calculate minGuardBGs without a wait from COB, UAM, IOB predBGs
if ( COBpredBG < minCOBGuardBG ) { minCOBGuardBG = round(COBpredBG); }
if ( UAMpredBG < minUAMGuardBG ) { minUAMGuardBG = round(UAMpredBG); }
if ( IOBpredBG < minIOBGuardBG ) { minIOBGuardBG = round(IOBpredBG); }
if ( ZTpredBG < minZTGuardBG ) { minZTGuardBG = round(ZTpredBG); }

// set minPredBGs starting when currently-dosed insulin activity will peak
// look ahead 60m (regardless of insulin type) so as to be less aggressive on slower insulins
Expand All @@ -509,8 +517,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
}
if (meal_data.mealCOB) {
console.error("predCIs (mg/dL/5m):",predCIs.join(" "));
console.error("remainingCIs:",remainingCIs.join(" "));
//console.error("COB:",meal_data.mealCOB,"remainingCItotal/csf:",round(remainingCItotal/csf,2),"remainingCarbs:",round(remainingCarbs,2));
console.error("remainingCIs: ",remainingCIs.join(" "));
}
//,"totalCA:",round(totalCA,2),"remainingCItotal/csf+totalCA:",round(remainingCItotal/csf+totalCA,2));
rT.predBGs = {};
Expand All @@ -523,6 +530,17 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
}
rT.predBGs.IOB = IOBpredBGs;
lastIOBpredBG=round(IOBpredBGs[IOBpredBGs.length-1]);
ZTpredBGs.forEach(function(p, i, theArray) {
theArray[i] = round(Math.min(401,Math.max(39,p)));
});
for (var i=ZTpredBGs.length-1; i > 6; i--) {
//if (ZTpredBGs[i-1] != ZTpredBGs[i]) { break; }
// stop displaying ZTpredBGs once they're rising and above target
if (ZTpredBGs[i-1] >= ZTpredBGs[i] || ZTpredBGs[i] < target_bg) { break; }
else { ZTpredBGs.pop(); }
}
rT.predBGs.ZT = ZTpredBGs;
lastZTpredBG=round(ZTpredBGs[ZTpredBGs.length-1]);
if (meal_data.mealCOB > 0) {
aCOBpredBGs.forEach(function(p, i, theArray) {
theArray[i] = round(Math.min(401,Math.max(39,p)));
Expand All @@ -531,7 +549,8 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
if (aCOBpredBGs[i-1] != aCOBpredBGs[i]) { break; }
else { aCOBpredBGs.pop(); }
}
rT.predBGs.aCOB = aCOBpredBGs;
// disable for now. may want to add a preference to re-enable
//rT.predBGs.aCOB = aCOBpredBGs;
}
if (meal_data.mealCOB > 0 && ( ci > 0 || remainingCIpeak > 0 )) {
COBpredBGs.forEach(function(p, i, theArray) {
Expand Down Expand Up @@ -585,6 +604,10 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
} else {
avgPredBG = round( IOBpredBG );
}
// if avgPredBG is below minZTGuardBG, bring it up to that level
if ( minZTGuardBG > avgPredBG ) {
avgPredBG = minZTGuardBG;
}

// if we have both minCOBGuardBG and minUAMGuardBG, blend according to fractionCarbsLeft
if ( (cid || remainingCIpeak > 0) ) {
Expand All @@ -601,6 +624,25 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
minGuardBG = round(minGuardBG);
//console.error(minCOBGuardBG, minUAMGuardBG, minIOBGuardBG, minGuardBG);

var minZTUAMPredBG = minUAMPredBG;
// if minZTGuardBG is below threshold, bring down any super-high minUAMPredBG by averaging
// this helps prevent UAM from giving too much insulin in case absorption falls off suddenly
if ( minZTGuardBG < threshold ) {
minZTUAMPredBG = (minUAMPredBG + minZTGuardBG) / 2;
// if minZTGuardBG is between threshold and target, blend in the averaging
} else if ( minZTGuardBG < target_bg ) {
// target 100, threshold 70, minZTGuardBG 85 gives 50%: (85-70) / (100-70)
var blendPct = (minZTGuardBG-threshold) / (target_bg-threshold);
var blendedMinZTGuardBG = minUAMPredBG*blendPct + minZTGuardBG*(1-blendPct);
minZTUAMPredBG = (minUAMPredBG + blendedMinZTGuardBG) / 2;
//minZTUAMPredBG = minUAMPredBG - target_bg + minZTGuardBG;
// if minUAMPredBG is below minZTGuardBG, bring minUAMPredBG up by averaging
// this allows more insulin if lastUAMPredBG is below target, but minZTGuardBG is still high
} else if ( minZTGuardBG > minUAMPredBG ) {
minZTUAMPredBG = (minUAMPredBG + minZTGuardBG) / 2;
}
minZTUAMPredBG = round(minZTUAMPredBG);
//console.error("minUAMPredBG:",minUAMPredBG,"minZTGuardBG:",minZTGuardBG,"minZTUAMPredBG:",minZTUAMPredBG);
// if any carbs have been entered recently
if (meal_data.carbs) {
// average the minIOBPredBG and minUAMPredBG if available
Expand All @@ -618,22 +660,25 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
// if we have COB, use minCOBPredBG, or blendedMinPredBG if it's higher
} else if ( minCOBPredBG < 999 ) {
// calculate blendedMinPredBG based on how many carbs remain as COB
blendedMinPredBG = fractionCarbsLeft*minCOBPredBG + (1-fractionCarbsLeft)*minUAMPredBG;
//blendedMinPredBG = fractionCarbsLeft*minCOBPredBG + (1-fractionCarbsLeft)*minUAMPredBG;
blendedMinPredBG = fractionCarbsLeft*minCOBPredBG + (1-fractionCarbsLeft)*minZTUAMPredBG;
// if blendedMinPredBG > minCOBPredBG, use that instead
minPredBG = round(Math.max(minIOBPredBG, minCOBPredBG, blendedMinPredBG));
// if carbs have been entered, but have expired, use avg of minIOBPredBG and minUAMPredBG
// if carbs have been entered, but have expired, use minUAMPredBG
} else {
minPredBG = minUAMPredBG;
//minPredBG = minUAMPredBG;
minPredBG = minZTUAMPredBG;
}
// in pure UAM mode, use the higher of minIOBPredBG,minUAMPredBG
} else if ( enableUAM ) {
minPredBG = round(Math.max(minIOBPredBG,minUAMPredBG));
//minPredBG = round(Math.max(minIOBPredBG,minUAMPredBG));
minPredBG = round(Math.max(minIOBPredBG,minZTUAMPredBG));
}

// make sure minPredBG isn't higher than avgPredBG
minPredBG = Math.min( minPredBG, avgPredBG );

process.stderr.write("minPredBG: "+minPredBG+" minIOBPredBG: "+minIOBPredBG);
process.stderr.write("minPredBG: "+minPredBG+" minIOBPredBG: "+minIOBPredBG+" minZTGuardBG: "+minZTGuardBG);
if (minCOBPredBG < 999) {
process.stderr.write(" minCOBPredBG: "+minCOBPredBG);
}
Expand All @@ -646,12 +691,6 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
if ( maxCOBPredBG > bg ) {
minPredBG = Math.min(minPredBG, maxCOBPredBG);
}
// set snoozeBG to minPredBG if it's higher
//if (minPredBG < 999) {
//snoozeBG = round(Math.max(snoozeBG,minPredBG));
//}
//rT.snoozeBG = snoozeBG;
//console.error(minPredBG, minIOBPredBG, minUAMPredBG, minCOBPredBG, maxCOBPredBG, snoozeBG);

rT.COB=meal_data.mealCOB;
rT.IOB=iob_data.iob;
Expand Down
11 changes: 8 additions & 3 deletions lib/iob/history.js
Expand Up @@ -78,7 +78,7 @@ function splitTimespan(event, splitterMoments) {
return results;
}

function calcTempTreatments (inputs) {
function calcTempTreatments (inputs, zeroTempDuration) {
var pumpHistory = inputs.history;
var profile_data = inputs.profile;
var tempHistory = [];
Expand Down Expand Up @@ -164,9 +164,14 @@ function calcTempTreatments (inputs) {
// Add a temp basal cancel event to ignore future temps and reduce predBG oscillation
var temp = {};
temp.rate = 0;
temp.started_at = new Date();
// start the zero temp 1m in the future to avoid clock skew
temp.started_at = new Date(now.getTime() + (1 * 60 * 1000));
temp.date = temp.started_at.getTime();
temp.duration = 0;
if (zeroTempDuration) {
temp.duration = zeroTempDuration;
} else {
temp.duration = 0;
}
tempHistory.push(temp);
}

Expand Down
16 changes: 16 additions & 0 deletions lib/iob/index.js
Expand Up @@ -8,13 +8,24 @@ function generate (inputs, currentIOBOnly, treatments) {

if (!treatments) {
var treatments = find_insulin(inputs);
// calculate IOB based on continuous future zero temping as well
var treatmentsWithZeroTemp = find_insulin(inputs, 240);
} else {
var treatmentsWithZeroTemp = [];
}
//console.error(treatments.length, treatmentsWithZeroTemp.length);
//console.error(treatments[treatments.length-1], treatmentsWithZeroTemp[treatmentsWithZeroTemp.length-1])

var opts = {
treatments: treatments
, profile: inputs.profile
, calculate: calculate
};
var optsWithZeroTemp = {
treatments: treatmentsWithZeroTemp
, profile: inputs.profile
, calculate: calculate
};

var iobArray = [];
//console.error(inputs.clock);
Expand Down Expand Up @@ -54,7 +65,12 @@ function generate (inputs, currentIOBOnly, treatments) {
t = new Date(clock.getTime() + i*60000);
//console.error(t);
var iob = sum(opts, t);
var iobWithZeroTemp = sum(optsWithZeroTemp, t);
//console.error(opts.treatments[opts.treatments.length-1], optsWithZeroTemp.treatments[optsWithZeroTemp.treatments.length-1])
iobArray.push(iob);
//console.error(iob.iob, iobWithZeroTemp.iob);
//console.error(iobArray.length-1, iobArray[iobArray.length-1]);
iobArray[iobArray.length-1].iobWithZeroTemp = iobWithZeroTemp;
}
//console.error(lastBolusTime);
iobArray[0].lastBolusTime = lastBolusTime;
Expand Down
2 changes: 1 addition & 1 deletion lib/iob/total.js
Expand Up @@ -46,7 +46,7 @@ function iobTotal(opts, time) {
}
//console.error(JSON.stringify(treatment));
}
}
} // else { console.error("ignoring future treatment:",treatment); }
});

var rval = {
Expand Down
6 changes: 3 additions & 3 deletions lib/profile/index.js
Expand Up @@ -14,12 +14,12 @@ function defaults ( ) {
, autosens_min: 0.7
, rewind_resets_autosens: true // reset autosensitivity to neutral for awhile after each pump rewind
, autosens_adjust_targets: true // when autosens detects sensitivity/resistance, also adjust BG target accordingly
, adv_target_adjustments: true // lower target automatically when BG and eventualBG are high
, adv_target_adjustments: false // lower target automatically when BG and eventualBG are high
, exercise_mode: false // when true, > 110 mg/dL high temp target adjusts sensitivityRatio for exercise_mode. This majorly changes the behavior of high temp targets from before.
, half_basal_exercise_target: 160 // when temptarget is 160 mg/dL *and* exercise_mode=true, run 50% basal at this level (120 = 75%; 140 = 60%)
// create maxCOB and default it to 120 because that's the most a typical body can absorb over 4 hours.
// (If someone enters more carbs or stacks more; OpenAPS will just truncate dosing based on 120.
// Essentially, this just limits AMA as a safety cap against weird COB calculations)
// Essentially, this just limits AMA/SMB as a safety cap against excessive COB entry)
, maxCOB: 120
, override_high_target_with_low: false // allows a much higher target used only for avoiding bolus wizard overcorrections
, skip_neutral_temps: false // if true, don't set neutral temps
Expand All @@ -31,7 +31,7 @@ function defaults ( ) {
, remainingCarbsFraction: 1.0 // fraction of carbs we'll assume will absorb over 4h if we don't yet see carb absorption
, remainingCarbsCap: 90 // max carbs we'll assume will absorb over 4h if we don't yet see carb absorption
// WARNING: the following are advanced oref1 features, and are not yet tested for general use
, enableUAM: false // enable detection of unannounced meal carb absorption
, enableUAM: true // enable detection of unannounced meal carb absorption
, enableSMB_always: false // always enable supermicrobolus (unless disabled by high temptarget)
, enableSMB_with_COB: false // enable supermicrobolus while COB is positive
, enableSMB_with_temptarget: false // enable supermicrobolus for eating soon temp targets
Expand Down