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

Exercise mode #684

Merged
merged 16 commits into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions lib/determine-basal/autosens.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ function detectSensitivity(inputs) {
avgDelta = avgDelta.toFixed(2);
iob_inputs.clock=bgTime;
iob_inputs.profile.current_basal = basal.basalLookup(basalprofile, bgTime);
// make sure autosens doesn't use temptarget-adjusted insulin calculations
iob_inputs.profile.temptargetSet = false;
//console.log(JSON.stringify(iob_inputs.profile));
//console.error("Before: ", new Date().getTime());
var iob = get_iob(iob_inputs, true, treatments)[0];
Expand Down
50 changes: 35 additions & 15 deletions lib/determine-basal/determine-basal.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
}
var profile_current_basal = round_basal(profile.current_basal, profile);
var basal = profile_current_basal;
if (typeof autosens_data !== 'undefined' ) {
basal = profile.current_basal * autosens_data.ratio;
basal = round_basal(basal, profile);
if (basal != profile_current_basal) {
process.stderr.write("Autosens adjusting basal from "+profile_current_basal+" to "+basal+"; ");
} else {
process.stderr.write("Basal unchanged: "+basal+"; ");
}
}

var bg = glucose_status.glucose;
if (bg < 39) { //Dexcom is in ??? mode or calibrating
Expand Down Expand Up @@ -102,10 +93,39 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
return rT;
}

var sensitivityRatio;
var normalTarget = 100; // evaluate high/low temptarget against 100, not scheduled basal (which might change)
if ( profile.half_basal_target ) {
var halfBasalTarget = profile.half_basal_exercise_target;
} else {
var halfBasalTarget = 160; // when temptarget is 160 mg/dL, run 50% basal (120 = 75%; 140 = 60%)
}
if ( profile.exercise_mode && profile.temptargetSet && target_bg > normalTarget + 10 ) {
// w/ target 100, temp target 110 = .89, 120 = 0.8, 140 = 0.67, 160 = .57, and 200 = .44
// e.g.: Sensitivity ratio set to 0.8 based on temp target of 120; Adjusting basal from 1.65 to 1.35; ISF from 58.9 to 73.6
//sensitivityRatio = 2/(2+(target_bg-normalTarget)/40);
var c = halfBasalTarget - normalTarget;
sensitivityRatio = c/(c+target_bg-normalTarget);
sensitivityRatio = round(sensitivityRatio,2);
process.stderr.write("Sensitivity ratio set to "+sensitivityRatio+" based on temp target of "+target_bg+"; ");
} else if (typeof autosens_data !== 'undefined' ) {
sensitivityRatio = autosens_data.ratio;
process.stderr.write("Autosens ratio: "+sensitivityRatio+"; ");
}
if (sensitivityRatio) {
basal = profile.current_basal * sensitivityRatio;
basal = round_basal(basal, profile);
if (basal != profile_current_basal) {
process.stderr.write("Adjusting basal from "+profile_current_basal+" to "+basal+"; ");
} else {
process.stderr.write("Basal unchanged: "+basal+"; ");
}
}

// adjust min, max, and target BG for sensitivity, such that 50% increase in ISF raises target from 100 to 120
if (typeof autosens_data !== 'undefined' && profile.autosens_adjust_targets) {
if (profile.temptargetSet) {
process.stderr.write("Temp Target set, not adjusting with autosens; ");
//process.stderr.write("Temp Target set, not adjusting with autosens; ");
} else {
// with a target of 100, default 0.7-1.2 autosens min/max range would allow a 93-117 target range
min_bg = round((min_bg - 60) / autosens_data.ratio) + 60;
Expand Down Expand Up @@ -152,14 +172,14 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
var profile_sens = round(profile.sens,1)
var sens = profile.sens;
if (typeof autosens_data !== 'undefined' ) {
sens = profile.sens / autosens_data.ratio;
sens = profile.sens / sensitivityRatio;
sens = round(sens, 1);
if (sens != profile_sens) {
process.stderr.write("sens from "+profile_sens+" to "+sens);
process.stderr.write("ISF from "+profile_sens+" to "+sens);
} else {
process.stderr.write("sens unchanged: "+sens);
process.stderr.write("ISF unchanged: "+sens);
}
process.stderr.write(" (autosens ratio "+autosens_data.ratio+")");
//process.stderr.write(" (autosens ratio "+sensitivityRatio+")");
}
console.error("");

Expand Down Expand Up @@ -694,7 +714,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
if (eventualBG < min_bg) { // if eventual BG is below target:
rT.reason += "Eventual BG " + convert_bg(eventualBG, profile) + " < " + convert_bg(min_bg, profile);
// if 5m or 30m avg BG is rising faster than expected delta
if (minDelta > expectedDelta && minDelta > 0) {
if ( minDelta > expectedDelta && minDelta > 0 && !carbsReq ) {
// if naive_eventualBG < 40, set a 30m zero temp (oref0-pump-loop will let any longer SMB zero temp run)
if (naive_eventualBG < 40) {
rT.reason += ", naive_eventualBG < 40. ";
Expand Down
29 changes: 28 additions & 1 deletion lib/iob/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,38 @@ function calcTempTreatments (inputs) {
if (currentItem.duration > 0) {

var currentRate = profile_data.current_basal;

if (!_.isEmpty(profile_data.basalprofile)) {
currentRate = basalprofile.basalLookup(profile_data.basalprofile,new Date(currentItem.timestamp));
}

if (typeof profile_data.min_bg !== 'undefined' && typeof profile_data.max_bg !== 'undefined') {
target_bg = (profile_data.min_bg + profile_data.max_bg) / 2;
}
//if (profile_data.temptargetSet && target_bg > 110) {
//sensitivityRatio = 2/(2+(target_bg-100)/40);
//currentRate = profile_data.current_basal * sensitivityRatio;
//}
var sensitivityRatio;
var profile = profile_data;
var normalTarget = 100; // evaluate high/low temptarget against 100, not scheduled basal (which might change)
if ( profile.half_basal_target ) {
var halfBasalTarget = profile.half_basal_exercise_target;
} else {
var halfBasalTarget = 160; // when temptarget is 160 mg/dL, run 50% basal (120 = 75%; 140 = 60%)
}
if ( profile.exercise_mode && profile.temptargetSet && target_bg > normalTarget + 10 ) {
// w/ target 100, temp target 110 = .89, 120 = 0.8, 140 = 0.67, 160 = .57, and 200 = .44
// e.g.: Sensitivity ratio set to 0.8 based on temp target of 120; Adjusting basal from 1.65 to 1.35; ISF from 58.9 to 73.6
var c = halfBasalTarget - normalTarget;
sensitivityRatio = c/(c+target_bg-normalTarget);
//} else if (typeof autosens_data !== 'undefined' ) {
//sensitivityRatio = autosens_data.ratio;
//process.stderr.write("Autosens ratio: "+sensitivityRatio+"; ");
}
if ( sensitivityRatio ) {
currentRate = profile_data.current_basal * sensitivityRatio;
}

var netBasalRate = currentItem.rate - currentRate;
if (netBasalRate < 0) { tempBolusSize = -0.05; }
else { tempBolusSize = 0.05; }
Expand Down
3 changes: 3 additions & 0 deletions lib/profile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ function defaults ( ) {
, 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
, 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)
Expand Down Expand Up @@ -55,6 +57,7 @@ function displayedDefaults () {
profile.autosens_min = allDefaults.autosens_min;
profile.rewind_resets_autosens = allDefaults.rewind_resets_autosens;
profile.adv_target_adjustments = allDefaults.adv_target_adjustments;
profile.exercise_mode = allDefaults.exercise_mode;
profile.unsuspend_if_no_temp = allDefaults.unsuspend_if_no_temp;
profile.enableSMB_with_bolus = allDefaults.enableSMB_with_bolus;
profile.enableSMB_with_COB = allDefaults.enableSMB_with_COB;
Expand Down