From 6991d15835d4af609f6da57e231b7bfd8a520d85 Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 8 Jan 2017 13:25:18 -0800 Subject: [PATCH 1/2] leave ISF unchanged if fewer than 5 ISF data points --- lib/autotune/index.js | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/autotune/index.js b/lib/autotune/index.js index 40531f7c..c503d467 100644 --- a/lib/autotune/index.js +++ b/lib/autotune/index.js @@ -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 ); From 4651218502bac93fa28555757815bc224c62f5e0 Mon Sep 17 00:00:00 2001 From: Scott Date: Sun, 8 Jan 2017 13:31:37 -0800 Subject: [PATCH 2/2] move stuff out of the else block --- lib/autotune/index.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/autotune/index.js b/lib/autotune/index.js index c503d467..5e7a88d2 100644 --- a/lib/autotune/index.js +++ b/lib/autotune/index.js @@ -154,18 +154,17 @@ function tuneAllTheThings (inputs) { 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; }