Skip to content

Commit

Permalink
leave ISF unchanged if fewer than 5 ISF data points
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleibrand committed Jan 8, 2017
1 parent b03e900 commit 6991d15
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions lib/autotune/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,32 @@ function tuneAllTheThings (inputs) {
var ratios = [];
var count = 0;
for (var i=0; i < ISFGlucose.length; ++i) {
deviation = parseFloat(ISFGlucose[i].deviation);
deviations.push(deviation);
BGI = parseFloat(ISFGlucose[i].BGI);
BGIs.push(BGI);
avgDelta = parseFloat(ISFGlucose[i].avgDelta);
avgDeltas.push(avgDelta);
ratio = 1 + deviation / BGI;
//console.error("Deviation:",deviation,"BGI:",BGI,"avgDelta:",avgDelta,"ratio:",ratio);
ratios.push(ratio);
count++;
deviation = parseFloat(ISFGlucose[i].deviation);
deviations.push(deviation);
BGI = parseFloat(ISFGlucose[i].BGI);
BGIs.push(BGI);
avgDelta = parseFloat(ISFGlucose[i].avgDelta);
avgDeltas.push(avgDelta);
ratio = 1 + deviation / BGI;
//console.error("Deviation:",deviation,"BGI:",BGI,"avgDelta:",avgDelta,"ratio:",ratio);
ratios.push(ratio);
count++;
}
avgDeltas.sort(function(a, b){return a-b});
BGIs.sort(function(a, b){return a-b});
deviations.sort(function(a, b){return a-b});
ratios.sort(function(a, b){return a-b});
p50deviation = percentile(deviations, 0.50);
p50BGI = percentile(BGIs, 0.50);
p50ratios = Math.round( percentile(ratios, 0.50) * 1000)/1000;
if (count < 5) {
// leave ISF unchanged if fewer than 5 ISF data points
fullNewISF = ISF;
} else {
avgDeltas.sort(function(a, b){return a-b});
BGIs.sort(function(a, b){return a-b});
deviations.sort(function(a, b){return a-b});
ratios.sort(function(a, b){return a-b});
p50deviation = percentile(deviations, 0.50);
p50BGI = percentile(BGIs, 0.50);
p50ratios = Math.round( percentile(ratios, 0.50) * 1000)/1000;

// calculate what adjustments to ISF would have been necessary to bring median deviation to zero
fullNewISF = ISF * p50ratios;
// calculate what adjustments to ISF would have been necessary to bring median deviation to zero
fullNewISF = ISF * p50ratios;
}
fullNewISF = Math.round( fullNewISF * 1000 ) / 1000;
// and apply 10% of that adjustment
var newISF = ( 0.9 * ISF ) + ( 0.1 * fullNewISF );
Expand Down

0 comments on commit 6991d15

Please sign in to comment.