diff --git a/bin/oref0-autosens-history.js b/bin/oref0-autosens-history.js index 45babff1a..bf51ccdd6 100755 --- a/bin/oref0-autosens-history.js +++ b/bin/oref0-autosens-history.js @@ -179,39 +179,4 @@ function convertBasal(item) //"minutes": Math.round(item.timeAsSeconds / 60), "rate": item.value }; -} - -// From https://gist.github.com/IceCreamYou/6ffa1b18c4c8f6aeaad2 -// Returns the value at a given percentile in a sorted numeric array. -// "Linear interpolation between closest ranks" method -function percentile(arr, p) { - if (arr.length === 0) return 0; - if (typeof p !== 'number') throw new TypeError('p must be a number'); - if (p <= 0) return arr[0]; - if (p >= 1) return arr[arr.length - 1]; - - var index = arr.length * p, - lower = Math.floor(index), - upper = lower + 1, - weight = index % 1; - - if (upper >= arr.length) return arr[lower]; - return arr[lower] * (1 - weight) + arr[upper] * weight; -} - -// Returns the percentile of the given value in a sorted numeric array. -function percentRank(arr, v) { - if (typeof v !== 'number') throw new TypeError('v must be a number'); - for (var i = 0, l = arr.length; i < l; i++) { - if (v <= arr[i]) { - while (i < l && v === arr[i]) i++; - if (i === 0) return 0; - if (v !== arr[i-1]) { - i += (v - arr[i-1]) / (arr[i] - arr[i-1]); - } - return i / l; - } - } - return 1; -} - +} \ No newline at end of file diff --git a/lib/autotune/index.js b/lib/autotune/index.js index 06fc207a4..4691d2e8a 100644 --- a/lib/autotune/index.js +++ b/lib/autotune/index.js @@ -1,3 +1,5 @@ +var percentile = require('../percentile') + // does three things - tunes basals, ISF, and CSF function tuneAllTheThings (inputs) { @@ -538,22 +540,3 @@ function tuneAllTheThings (inputs) { } exports = module.exports = tuneAllTheThings; - -// From https://gist.github.com/IceCreamYou/6ffa1b18c4c8f6aeaad2 -// Returns the value at a given percentile in a sorted numeric array. -// "Linear interpolation between closest ranks" method -function percentile(arr, p) { - if (arr.length === 0) return 0; - if (typeof p !== 'number') throw new TypeError('p must be a number'); - if (p <= 0) return arr[0]; - if (p >= 1) return arr[arr.length - 1]; - - var index = arr.length * p, - lower = Math.floor(index), - upper = lower + 1, - weight = index % 1; - - if (upper >= arr.length) return arr[lower]; - return arr[lower] * (1 - weight) + arr[upper] * weight; -} - diff --git a/lib/determine-basal/autosens.js b/lib/determine-basal/autosens.js index 3591fc8ea..bf178b1c7 100644 --- a/lib/determine-basal/autosens.js +++ b/lib/determine-basal/autosens.js @@ -4,6 +4,7 @@ var find_insulin = require('../iob/history'); var isf = require('../profile/isf'); var find_meals = require('../meal/history'); var tz = require('moment-timezone'); +var percentile = require('../percentile'); function detectSensitivity(inputs) { @@ -422,24 +423,6 @@ function detectSensitivity(inputs) { } module.exports = detectSensitivity; -// From https://gist.github.com/IceCreamYou/6ffa1b18c4c8f6aeaad2 -// Returns the value at a given percentile in a sorted numeric array. -// "Linear interpolation between closest ranks" method -function percentile(arr, p) { - if (arr.length === 0) return 0; - if (typeof p !== 'number') throw new TypeError('p must be a number'); - if (p <= 0) return arr[0]; - if (p >= 1) return arr[arr.length - 1]; - - var index = arr.length * p, - lower = Math.floor(index), - upper = lower + 1, - weight = index % 1; - - if (upper >= arr.length) return arr[lower]; - return arr[lower] * (1 - weight) + arr[upper] * weight; -} - function tempTargetRunning(temptargets_data, time) { // sort tempTargets by date so we can process most recent first try { diff --git a/lib/iob/calculate.js b/lib/iob/calculate.js index 3c9f8f85a..31498785c 100644 --- a/lib/iob/calculate.js +++ b/lib/iob/calculate.js @@ -136,12 +136,10 @@ function iobCalcExponential(treatment, minsAgo, dia, peak, profile) { //console.error('DIA: ' + dia + ' minsAgo: ' + minsAgo + ' end: ' + end + ' peak: ' + peak + ' tau: ' + tau + ' a: ' + a + ' S: ' + S + ' activityContrib: ' + activityContrib + ' iobContrib: ' + iobContrib); } - var results = { + return { activityContrib: activityContrib, iobContrib: iobContrib }; - - return results; } diff --git a/lib/percentile.js b/lib/percentile.js new file mode 100644 index 000000000..5e8b02049 --- /dev/null +++ b/lib/percentile.js @@ -0,0 +1,17 @@ +// From https://gist.github.com/IceCreamYou/6ffa1b18c4c8f6aeaad2 +// Returns the value at a given percentile in a sorted numeric array. +// "Linear interpolation between closest ranks" method +module.exports = function percentile(arr, p) { + if (arr.length === 0) return 0; + if (typeof p !== 'number') throw new TypeError('p must be a number'); + if (p <= 0) return arr[0]; + if (p >= 1) return arr[arr.length - 1]; + + var index = arr.length * p, + lower = Math.floor(index), + upper = lower + 1, + weight = index % 1; + + if (upper >= arr.length) return arr[lower]; + return arr[lower] * (1 - weight) + arr[upper] * weight; +} \ No newline at end of file