Skip to content

Commit

Permalink
Merge pull request #706 from sulkaharo/wip/bwpfix
Browse files Browse the repository at this point in the history
More comprehensive BWP test and small uncovered fixes
  • Loading branch information
sulkaharo committed Jul 14, 2015
2 parents 466ca38 + 326d9d3 commit fb6758e
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/pebble.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function prepareSGVs (req, sbx) {

//for legacy reasons we need to return a 0 for delta if it can't be calculated
var deltaResult = delta.calc(prev, current, sbx);
bgs[0].bgdelta = deltaResult && deltaResult.scaled || 0;
bgs[0].bgdelta = String(deltaResult && deltaResult.scaled || 0);

bgs[0].battery = data.devicestatus && data.devicestatus.uploaderBattery && data.devicestatus.uploaderBattery.toString();
if (req.iob) {
Expand Down
11 changes: 9 additions & 2 deletions lib/plugins/boluswizardpreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,17 @@ function init() {
if (results.outcome > target_high) {
delta = results.outcome - target_high;
results.bolusEstimate = delta / sens;
results.aimTarget= target_high;
results.aimTargetString = "above high";
}

var target_low = profile.getLowBGTarget(sbx.time);

if (results.outcome < target_low) {
delta = Math.abs(results.outcome - target_low);
results.bolusEstimate = delta / sens * -1;
results.aimTarget = target_low;
results.aimTargetString = "below low";
}

if (results.bolusEstimate !== 0 && sbx.data.profile.getBasal(sbx.time)) {
Expand Down Expand Up @@ -222,15 +226,18 @@ function pushTempBasalAdjustments(prop, info, sbx) {
}

info.push({label: '---------', value: ''});
if (prop.aimTarget) {
info.push({label: 'Projection ' + prop.aimTargetString +' target', value: 'aiming at ' + prop.aimTarget + ' ' + sbx.units});
}
info.push({label: 'Current basal', value: sbx.data.profile.getBasal(sbx.time)});
info.push({label: 'Basal change to account for ' + prop.bolusEstimateDisplay + ':', value: ''});

if (prop.tempBasalAdjustment.thirtymin > 0 && prop.tempBasalAdjustment.thirtymin < 200) {
if (prop.tempBasalAdjustment.thirtymin >= 0 && prop.tempBasalAdjustment.thirtymin <= 200) {
info.push({label: '30m temp basal', value: '' + prop.tempBasalAdjustment.thirtymin + '% (' + sign + (prop.tempBasalAdjustment.thirtymin - 100) + '%)'});
} else {
info.push({label: '30m temp basal', value: carbsOrBolusMessage});
}
if (prop.tempBasalAdjustment.onehour > 0 && prop.tempBasalAdjustment.onehour < 200) {
if (prop.tempBasalAdjustment.onehour >= 0 && prop.tempBasalAdjustment.onehour <= 200) {
info.push({label: '1h temp basal', value: '' + prop.tempBasalAdjustment.onehour + '% (' + sign + (prop.tempBasalAdjustment.onehour - 100) + '%)'});
} else {
info.push({label: '1h temp basal', value: carbsOrBolusMessage});
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/iob.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function init() {
var lastBolus = null;

_.forEach(treatments, function eachTreatment(treatment) {
if (treatment.mills < time) {
if (treatment.mills <= time) {
var tIOB = iob.calcTreatment(treatment, profile, time);
if (tIOB.iobContrib > 0) {
lastBolus = treatment;
Expand Down
151 changes: 151 additions & 0 deletions tests/boluswizardpreview.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,157 @@ describe('boluswizardpreview', function ( ) {
, target_low: 100
};

it('should calculate IOB results correctly with 0 IOB', function (done) {
ctx.notifications.initRequests();
ctx.data.sgvs = [{mills: before, mgdl: 100}, {mills: now, mgdl: 100}];
ctx.data.treatments = [];
ctx.data.profiles = [profile];

var sbx = prepareSandbox();
var results = boluswizardpreview.calc(sbx);

results.effect.should.equal(0);
results.effectDisplay.should.equal(0);
results.outcome.should.equal(100);
results.outcomeDisplay.should.equal(100);
results.bolusEstimate.should.equal(0);
results.displayLine.should.equal('BWP: 0U');

done();
});

it('should calculate IOB results correctly with 1.0 U IOB', function (done) {
ctx.notifications.initRequests();
ctx.data.sgvs = [{mills: before, mgdl: 100}, {mills: now, mgdl: 100}];
ctx.data.treatments = [{mills: now, insulin: '1.0'}];

var profile = {
dia: 3
, sens: 50
, target_high: 100
, target_low: 50
};

ctx.data.profiles = [profile];

var sbx = prepareSandbox();
var results = boluswizardpreview.calc(sbx);

Math.round(results.effect).should.equal(50);
results.effectDisplay.should.equal(50);
Math.round(results.outcome).should.equal(50);
results.outcomeDisplay.should.equal(50);
results.bolusEstimate.should.equal(0);
results.displayLine.should.equal('BWP: 0U');

done();
});

it('should calculate IOB results correctly with 1.0 U IOB resulting in going low', function (done) {
ctx.notifications.initRequests();
ctx.data.sgvs = [{mills: before, mgdl: 100}, {mills: now, mgdl: 100}];
ctx.data.treatments = [{mills: now, insulin: '1.0'}];

var profile = {
dia: 3
, sens: 50
, target_high: 200
, target_low: 100
, basal: 1
};


ctx.data.profiles = [profile];

var sbx = prepareSandbox();
var results = boluswizardpreview.calc(sbx);

Math.round(results.effect).should.equal(50);
results.effectDisplay.should.equal(50);
Math.round(results.outcome).should.equal(50);
results.outcomeDisplay.should.equal(50);
Math.round(results.bolusEstimate).should.equal(-1);
results.displayLine.should.equal('BWP: -1.00U');
results.tempBasalAdjustment.thirtymin.should.equal(-100);
results.tempBasalAdjustment.onehour.should.equal(0);

done();
});

it('should calculate IOB results correctly with 1.0 U IOB resulting in going low in MMOL', function (done) {

// boilerplate for client sandbox running in mmol

var profileData = {
dia: 3
, units: 'mmol'
, sens: 10
, target_high: 10
, target_low: 5.6
, basal: 1
};

var sandbox = require('../lib/sandbox')();
var app = { };
var pluginBase = {};
var clientSettings = { units: 'mmol' };
var data = {sgvs: [{mills: before, mgdl: 100}, {mills: now, mgdl: 100}]};
data.treatments = [{mills: now, insulin: '1.0'}];
data.profile = require('../lib/profilefunctions')([profileData]);
var sbx = sandbox.clientInit(app, clientSettings, Date.now(), pluginBase, data);
var iob = require('../lib/plugins/iob')();
sbx.properties.iob = iob.calcTotal(data.treatments, data.profile, now);

var results = boluswizardpreview.calc(sbx);

results.effect.should.equal(10);
results.outcome.should.equal(-4.4);
results.bolusEstimate.should.equal(-1);
results.displayLine.should.equal('BWP: -1.00U');
results.tempBasalAdjustment.thirtymin.should.equal(-100);
results.tempBasalAdjustment.onehour.should.equal(0);

done();
});


it('should calculate IOB results correctly with 0.45 U IOB resulting in going low in MMOL', function (done) {

// boilerplate for client sandbox running in mmol

var profileData = {
dia: 3
, units: 'mmol'
, sens: 9
, target_high: 6
, target_low: 5
, basal: 0.125
};

var sandbox = require('../lib/sandbox')();
var app = { };
var pluginBase = {};
var clientSettings = { units: 'mmol' };
var data = {sgvs: [{mills: before, mgdl: 175}, {mills: now, mgdl: 153}]};
data.treatments = [{mills: now, insulin: '0.45'}];
data.profile = require('../lib/profilefunctions')([profileData]);
var sbx = sandbox.clientInit(app, clientSettings, Date.now(), pluginBase, data);
var iob = require('../lib/plugins/iob')();
sbx.properties.iob = iob.calcTotal(data.treatments, data.profile, now);

var results = boluswizardpreview.calc(sbx);

results.effect.should.equal(4.05);
results.outcome.should.equal(4.45);
Math.round(results.bolusEstimate*100).should.equal(-6);
results.displayLine.should.equal('BWP: -0.07U');
results.tempBasalAdjustment.thirtymin.should.equal(2);
results.tempBasalAdjustment.onehour.should.equal(51);

done();
});


it('Not trigger an alarm when in range', function (done) {
ctx.notifications.initRequests();
ctx.data.sgvs = [{mills: before, mgdl: 95}, {mills: now, mgdl: 100}];
Expand Down
8 changes: 4 additions & 4 deletions tests/pebble.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('Pebble Endpoint', function ( ) {
bgs.length.should.equal(1);
var bg = bgs[0];
bg.sgv.should.equal('82');
bg.bgdelta.should.equal(-2);
bg.bgdelta.should.equal('-2');
bg.trend.should.equal(4);
bg.direction.should.equal('Flat');
bg.datetime.should.equal(now);
Expand All @@ -127,7 +127,7 @@ describe('Pebble Endpoint', function ( ) {
bgs.length.should.equal(1);
var bg = bgs[0];
bg.sgv.should.equal('4.6');
bg.bgdelta.should.equal(-0.1);
bg.bgdelta.should.equal('-0.1');
bg.trend.should.equal(4);
bg.direction.should.equal('Flat');
bg.datetime.should.equal(now);
Expand All @@ -151,7 +151,7 @@ describe('Pebble Endpoint', function ( ) {
bgs.length.should.equal(2);
var bg = bgs[0];
bg.sgv.should.equal('82');
bg.bgdelta.should.equal(-2);
bg.bgdelta.should.equal('-2');
bg.trend.should.equal(4);
bg.direction.should.equal('Flat');
bg.datetime.should.equal(now);
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('Pebble Endpoint with Raw and IOB', function ( ) {
bgs.length.should.equal(2);
var bg = bgs[0];
bg.sgv.should.equal('82');
bg.bgdelta.should.equal(-2);
bg.bgdelta.should.equal('-2');
bg.trend.should.equal(4);
bg.direction.should.equal('Flat');
bg.datetime.should.equal(now);
Expand Down

0 comments on commit fb6758e

Please sign in to comment.