diff --git a/lib/autotune/index.js b/lib/autotune/index.js index 40531f7c..5e7a88d2 100644 --- a/lib/autotune/index.js +++ b/lib/autotune/index.js @@ -143,16 +143,16 @@ 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}); @@ -161,9 +161,13 @@ function tuneAllTheThings (inputs) { 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; + if (count < 5) { + // leave ISF unchanged if fewer than 5 ISF data points + fullNewISF = ISF; + } else { + // 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 );