Skip to content

Commit

Permalink
Merge pull request nightscout#194 from nightscout/wip/predictARLookback
Browse files Browse the repository at this point in the history
predictAR lookback abstraction
  • Loading branch information
jasoncalabrese committed Oct 17, 2014
2 parents 17288a1 + b57cda4 commit b914ea2
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions static/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
brushTimer,
brushInProgress = false,
clip,
ONE_MIN_IN_MS = 60000,
FIVE_MINS_IN_MS = 300000,
TWENTY_FIVE_MINS_IN_MS = 1500000,
THIRTY_MINS_IN_MS = 1800000,
FORTY_MINS_IN_MS = 2400000,
Expand Down Expand Up @@ -234,15 +236,25 @@
var nowDate = new Date(brushExtent[1] - THIRTY_MINS_IN_MS);

// predict for retrospective data
var lookback = 2;
if (brushExtent[1].getTime() - THIRTY_MINS_IN_MS < now && element != true) {
// filter data for -12 and +5 minutes from reference time for retrospective focus data prediction
var nowData = data.filter(function(d) {
return d.date.getTime() >= brushExtent[1].getTime() - FORTY_TWO_MINS_IN_MS &&
var lookbackTime = (lookback+2)*FIVE_MINS_IN_MS + 2*ONE_MIN_IN_MS;
var nowDataRaw = data.filter(function(d) {
return d.date.getTime() >= brushExtent[1].getTime() - TWENTY_FIVE_MINS_IN_MS - lookbackTime &&
d.date.getTime() <= brushExtent[1].getTime() - TWENTY_FIVE_MINS_IN_MS &&
d.type == 'sgv';
});
if (nowData.length > 1) {
var prediction = predictAR(nowData);
// sometimes nowDataRaw contains duplicates. uniq it.
var lastDate = new Date("1/1/1970");
var nowData = nowDataRaw.filter(function(n) {
if ( (lastDate.getTime() + ONE_MIN_IN_MS) < n.date.getTime()) {
lastDate = n.date;
return n;
}
});
if (nowData.length > lookback) {
var prediction = predictAR(nowData, lookback);
focusData = focusData.concat(prediction);
var focusPoint = nowData[nowData.length - 1];

Expand Down Expand Up @@ -276,8 +288,10 @@
var nowData = data.filter(function(d) {
return d.type == 'sgv';
});
nowData = [nowData[nowData.length - 2], nowData[nowData.length - 1]];
var prediction = predictAR(nowData);
var x=lookback+1;
nowData = nowData.slice(nowData.length-x, nowData.length);
//nowData = [nowData[nowData.length - lookback-1], nowData[nowData.length - 1]];
var prediction = predictAR(nowData, lookback);
focusData = focusData.concat(prediction);
var dateTime = new Date(now);
nowDate = dateTime;
Expand Down Expand Up @@ -1073,7 +1087,7 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// function to predict
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function predictAR(actual) {
function predictAR(actual, lookback) {
var ONE_MINUTE = 60 * 1000;
var FIVE_MINUTES = 5 * ONE_MINUTE;
var predicted = [];
Expand All @@ -1082,18 +1096,28 @@
var BG_MAX = scaleBg(400);
// these are the one sigma limits for the first 13 prediction interval uncertainties (65 minutes)
var CONE = [0.020, 0.041, 0.061, 0.081, 0.099, 0.116, 0.132, 0.146, 0.159, 0.171, 0.182, 0.192, 0.201];
if (actual.length < 2) {
var y = [Math.log(actual[0].sgv / BG_REF), Math.log(actual[0].sgv / BG_REF)];
// these are modified to make the cone much blunter
//var CONE = [0.030, 0.060, 0.090, 0.120, 0.140, 0.150, 0.160, 0.170, 0.180, 0.185, 0.190, 0.195, 0.200];
// for testing
//var CONE = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
if (actual.length < lookback+1) {
var y = [Math.log(actual[actual.length-1].sgv / BG_REF), Math.log(actual[actual.length-1].sgv / BG_REF)];
} else {
var elapsedMins = (actual[1].date - actual[0].date) / ONE_MINUTE;
if (elapsedMins < 5.1) {
y = [Math.log(actual[0].sgv / BG_REF), Math.log(actual[1].sgv / BG_REF)];
var elapsedMins = (actual[actual.length-1].date - actual[actual.length-1-lookback].date) / ONE_MINUTE;
// construct a "5m ago" sgv offset from current sgv by the average change over the lookback interval
var lookbackSgvChange = actual[lookback].sgv-actual[0].sgv;
var fiveMinAgoSgv = actual[lookback].sgv - lookbackSgvChange/elapsedMins*5;
y = [Math.log(fiveMinAgoSgv / BG_REF), Math.log(actual[lookback].sgv / BG_REF)];
/*
if (elapsedMins < lookback * 5.1) {
y = [Math.log(actual[0].sgv / BG_REF), Math.log(actual[lookback].sgv / BG_REF)];
} else {
y = [Math.log(actual[0].sgv / BG_REF), Math.log(actual[0].sgv / BG_REF)];
y = [Math.log(actual[lookback].sgv / BG_REF), Math.log(actual[lookback].sgv / BG_REF)];
}
*/
}
var AR = [-0.723, 1.716];
var dt = actual[1].date.getTime();
var dt = actual[lookback].date.getTime();
var predictedColor = 'blue';
if (browserSettings.theme == "colors") {
predictedColor = 'cyan';
Expand Down

0 comments on commit b914ea2

Please sign in to comment.