Skip to content

Commit

Permalink
#447: if rewind_resets_autosens, use BG data since lastSiteChange
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleibrand committed Apr 27, 2017
1 parent 5580485 commit fbff878
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
42 changes: 39 additions & 3 deletions lib/determine-basal/cob-autosens.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ function detectSensitivityandCarbAbsorption(inputs) {

//console.error(mealTime);

// use last 24h worth of data by default
var lastSiteChange = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));
if (inputs.iob_inputs.profile.rewind_resets_autosens) {
// scan through pumphistory and set lastSiteChange to the time of the last pump rewind event
// if not present, leave lastSiteChange unchanged at 24h ago.
var history = inputs.iob_inputs.history;
for (var h=1; h < history.length; ++h) {
if ( ! history[h]._type || history[h]._type != "Rewind" ) {
//process.stderr.write("-");
continue;
}
if ( history[h].timestamp ) {
lastSiteChange = new Date( history[h].timestamp );
console.error("Setting lastSiteChange to",lastSiteChange,"using timestamp",history[h].timestamp);
break;
}
}
}

var avgDeltas = [];
var bgis = [];
var deviations = [];
Expand Down Expand Up @@ -43,8 +62,15 @@ function detectSensitivityandCarbAbsorption(inputs) {
// only consider BGs for 6h before a meal
if (mealTime) {
hoursBeforeMeal = (bgTime-mealTime)/(60*60*1000);
if (hoursBeforeMeal > 6 || hoursBeforeMeal < 0) {
continue;
if (hoursBeforeMeal > 6 || hoursBeforeMeal < 0) {
continue;
}
}
// only consider BGs since lastSiteChange
if (lastSiteChange) {
hoursSinceSiteChange = (bgTime-lastSiteChange)/(60*60*1000);
if (hoursSinceSiteChange < 0) {
continue;
}
}
var elapsed_minutes = (bgTime - lastbgTime)/(60*1000);
Expand Down Expand Up @@ -152,14 +178,24 @@ function detectSensitivityandCarbAbsorption(inputs) {
inputs.mealTime || process.stderr.write(" ");
//console.log(JSON.stringify(avgDeltas));
//console.log(JSON.stringify(bgis));
// when we have less than 12h worth of deviation data, add up to 1h of zero deviations
// this dampens any large sensitivity changes detected based on too little data, without ignoring them completely
if (deviations.length < 144) {
pad = Math.round((1 - deviations.length/144) * 12);
console.error("Found",deviations.length,"deviations since",lastSiteChange,"- adding",pad,"more zero deviations");
for (var d=0; d<pad; d++) {
//inputs.mealTime || process.stderr.write(".");
deviations.push(0);
}
}
avgDeltas.sort(function(a, b){return a-b});
bgis.sort(function(a, b){return a-b});
deviations.sort(function(a, b){return a-b});
for (var i=0.9; i > 0.1; i = i - 0.02) {
//console.error("p="+i.toFixed(2)+": "+percentile(avgDeltas, i).toFixed(2)+", "+percentile(bgis, i).toFixed(2)+", "+percentile(deviations, i).toFixed(2));
if ( percentile(deviations, (i+0.02)) >= 0 && percentile(deviations, i) < 0 ) {
//console.error("p="+i.toFixed(2)+": "+percentile(avgDeltas, i).toFixed(2)+", "+percentile(bgis, i).toFixed(2)+", "+percentile(deviations, i).toFixed(2));
inputs.mealTime || console.error(Math.round(100*i)+"% of non-meal deviations negative (target 45%-50%)");
inputs.mealTime || console.error(Math.round(100*i)+"% of non-meal deviations <= 0 (target 45%-50%)");
}
}
pSensitive = percentile(deviations, 0.50);
Expand Down
1 change: 1 addition & 0 deletions lib/profile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function defaults ( ) {
, bolussnooze_dia_divisor: 2
, min_5m_carbimpact: 3 //mg/dL/5m
, carbratio_adjustmentratio: 1 //if carb ratios on pump are set higher to lower initial bolus using wizard, .8 = assume only 80 percent of carbs covered with full bolus
, rewind_resets_autosens: false
};
return profile;
}
Expand Down

0 comments on commit fbff878

Please sign in to comment.