From a89487a515d947dbe7fa737382259179052540d4 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Tue, 13 Feb 2018 21:23:52 -0600 Subject: [PATCH 01/11] Fixed raw bg calculations to use noise level to determine filters vs. unfiltered. --- lib/plugins/rawbg.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/plugins/rawbg.js b/lib/plugins/rawbg.js index a3853f88e11..a15bd777918 100644 --- a/lib/plugins/rawbg.js +++ b/lib/plugins/rawbg.js @@ -55,11 +55,10 @@ function init (ctx) { if (cleaned.slope === 0 || cleaned.unfiltered === 0 || cleaned.scale === 0) { raw = 0; - } else if (cleaned.filtered === 0 || sgv.mgdl < 40) { + } else if (parseInt(sgv.noise) < 2 || sgv.mgdl < 40) { raw = cleaned.scale * (cleaned.unfiltered - cleaned.intercept) / cleaned.slope; } else { - var ratio = cleaned.scale * (cleaned.filtered - cleaned.intercept) / cleaned.slope / sgv.mgdl; - raw = cleaned.scale * (cleaned.unfiltered - cleaned.intercept) / cleaned.slope / ratio; + raw = cleaned.scale * (cleaned.filtered - cleaned.intercept) / cleaned.slope; } return Math.round(raw); From 5643aa0df86361b7e1a7c77f101a881fcb1bf6a5 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Tue, 13 Feb 2018 21:57:27 -0600 Subject: [PATCH 02/11] Changed test to use the updated rawbg calculation values. --- tests/rawbg.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/rawbg.test.js b/tests/rawbg.test.js index 4697bea900a..4a2ceac019b 100644 --- a/tests/rawbg.test.js +++ b/tests/rawbg.test.js @@ -27,7 +27,7 @@ describe('Raw BG', function ( ) { sbx.offerProperty = function mockedOfferProperty (name, setter) { name.should.equal('rawbg'); var result = setter(); - result.mgdl.should.equal(113); + result.mgdl.should.equal(115); result.noiseLabel.should.equal('Clean'); done(); }; @@ -47,7 +47,7 @@ describe('Raw BG', function ( ) { rawbg.alexa.intentHandlers[0].intentHandler(function next(title, response) { title.should.equal('Current Raw BG'); - response.should.equal('Your raw bg is 113'); + response.should.equal('Your raw bg is 115'); done(); }, [], sbx); From 7bafa94dfa23371afc821b148848c4ec143ea93d Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Thu, 16 Aug 2018 20:53:49 -0500 Subject: [PATCH 03/11] Made new functionality an option. --- lib/plugins/rawbg.js | 17 ++++++++++++++--- tests/rawbg.test.js | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/plugins/rawbg.js b/lib/plugins/rawbg.js index a15bd777918..22bdf7fa561 100644 --- a/lib/plugins/rawbg.js +++ b/lib/plugins/rawbg.js @@ -13,6 +13,12 @@ function init (ctx) { , pillFlip: true }; + rawbg.getPrefs = function getPrefs (sbx) { + return { + smooth: sbx.extendedSettings.smooth ? (sbx.extendedSettings.smooth.toLowerCase() == "true") : false + }; + }; + rawbg.setProperties = function setProperties (sbx) { sbx.offerProperty('rawbg', function setRawBG ( ) { var result = { }; @@ -49,16 +55,21 @@ function init (ctx) { sbx.pluginBase.updatePillText(rawbg, options); }; - rawbg.calc = function calc(sgv, cal) { + rawbg.calc = function calc(sgv, cal, sbx) { var raw = 0; var cleaned = cleanValues(sgv, cal); + var prefs = rawbg.getPrefs(sbx); + if (cleaned.slope === 0 || cleaned.unfiltered === 0 || cleaned.scale === 0) { raw = 0; - } else if (parseInt(sgv.noise) < 2 || sgv.mgdl < 40) { + } else if (cleaned.filtered === 0 || sgv.mgdl < 40) { raw = cleaned.scale * (cleaned.unfiltered - cleaned.intercept) / cleaned.slope; - } else { + } else if (prefs.smooth) { raw = cleaned.scale * (cleaned.filtered - cleaned.intercept) / cleaned.slope; + } else { + var ratio = cleaned.scale * (cleaned.filtered - cleaned.intercept) / cleaned.slope / sgv.mgdl; + raw = cleaned.scale * (cleaned.unfiltered - cleaned.intercept) / cleaned.slope / ratio; } return Math.round(raw); diff --git a/tests/rawbg.test.js b/tests/rawbg.test.js index 4a2ceac019b..4697bea900a 100644 --- a/tests/rawbg.test.js +++ b/tests/rawbg.test.js @@ -27,7 +27,7 @@ describe('Raw BG', function ( ) { sbx.offerProperty = function mockedOfferProperty (name, setter) { name.should.equal('rawbg'); var result = setter(); - result.mgdl.should.equal(115); + result.mgdl.should.equal(113); result.noiseLabel.should.equal('Clean'); done(); }; @@ -47,7 +47,7 @@ describe('Raw BG', function ( ) { rawbg.alexa.intentHandlers[0].intentHandler(function next(title, response) { title.should.equal('Current Raw BG'); - response.should.equal('Your raw bg is 115'); + response.should.equal('Your raw bg is 113'); done(); }, [], sbx); From 6f633b88988a8aebebcc171e0d13d29ae3267f35 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Thu, 16 Aug 2018 21:08:43 -0500 Subject: [PATCH 04/11] Added new rawbg extended setting. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a7e3bcc37dc..b78ae28851b 100644 --- a/README.md +++ b/README.md @@ -318,7 +318,8 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm An option plugin to enable adding foods from database in Bolus Wizard and enable . ##### `rawbg` (Raw BG) - Calculates BG using sensor and calibration records from and displays an alternate BG values and noise levels. + Calculates BG using sensor and calibration records from and displays an alternate BG values and noise levels. Defaults that can be adjusted with [extended setting](#extended-settings) + * `SMOOTH` (`false`) - If `SMOOTH` is false, rawbg will be calculated such that it exagerates direction changes to provide early visible indication of glucose trend changes. ##### `iob` (Insulin-on-Board) Adds the IOB pill visualization in the client and calculates values that used by other plugins. Uses treatments with insulin doses and the `dia` and `sens` fields from the [treatment profile](#treatment-profile). From 46c426d06eaa7307ab245dd396533b5a73958c1e Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Thu, 16 Aug 2018 21:22:47 -0500 Subject: [PATCH 05/11] Check if sbx argument was provided. --- lib/plugins/rawbg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/rawbg.js b/lib/plugins/rawbg.js index 22bdf7fa561..e4ab800d9f7 100644 --- a/lib/plugins/rawbg.js +++ b/lib/plugins/rawbg.js @@ -15,7 +15,7 @@ function init (ctx) { rawbg.getPrefs = function getPrefs (sbx) { return { - smooth: sbx.extendedSettings.smooth ? (sbx.extendedSettings.smooth.toLowerCase() == "true") : false + smooth: (sbx && sbx.extendedSettings.smooth) ? (sbx.extendedSettings.smooth.toLowerCase() == "true") : false }; }; From 0aa594fb3ea9f535e8b50db566082932cc16701b Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Thu, 16 Aug 2018 21:36:32 -0500 Subject: [PATCH 06/11] extended settings boolean auto detected --- lib/plugins/rawbg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/rawbg.js b/lib/plugins/rawbg.js index e4ab800d9f7..f988603d5db 100644 --- a/lib/plugins/rawbg.js +++ b/lib/plugins/rawbg.js @@ -15,7 +15,7 @@ function init (ctx) { rawbg.getPrefs = function getPrefs (sbx) { return { - smooth: (sbx && sbx.extendedSettings.smooth) ? (sbx.extendedSettings.smooth.toLowerCase() == "true") : false + smooth: (sbx && sbx.extendedSettings.smooth) ? sbx.extendedSettings.smooth : false }; }; From da31b3bb624288ab87e10bc515cd546cb62c493f Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Thu, 16 Aug 2018 22:13:14 -0500 Subject: [PATCH 07/11] rawbg needs sbx with extended settings. --- lib/client/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/client/index.js b/lib/client/index.js index ff9a337c675..757a5298f06 100644 --- a/lib/client/index.js +++ b/lib/client/index.js @@ -1052,9 +1052,11 @@ client.load = function load(serverSettings, callback) { function prepareEntries ( ) { // Post processing after data is in var temp1 = [ ]; - if (client.ddata.cal && client.rawbg.isEnabled(client.sbx)) { + var sbx = client.sbx.withExtendedSettings(client.rawbg); + + if (client.ddata.cal && client.rawbg.isEnabled(sbx)) { temp1 = client.ddata.sgvs.map(function (entry) { - var rawbgValue = client.rawbg.showRawBGs(entry.mgdl, entry.noise, client.ddata.cal, client.sbx) ? client.rawbg.calc(entry, client.ddata.cal, client.sbx) : 0; + var rawbgValue = client.rawbg.showRawBGs(entry.mgdl, entry.noise, client.ddata.cal, sbx) ? client.rawbg.calc(entry, client.ddata.cal, sbx) : 0; if (rawbgValue > 0) { return { mills: entry.mills - 2000, mgdl: rawbgValue, color: 'white', type: 'rawbg' }; } else { From f246c858dbe56cda822c281da2f3c4330f4cafc2 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Thu, 16 Aug 2018 22:22:49 -0500 Subject: [PATCH 08/11] rawbg needs withextendedsettings in renderer. --- lib/client/renderer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/client/renderer.js b/lib/client/renderer.js index f0666277715..0f8a18dd580 100644 --- a/lib/client/renderer.js +++ b/lib/client/renderer.js @@ -137,10 +137,11 @@ function init (client, d3) { function getRawbgInfo ( ) { var info = { }; + var sbx = client.sbx.withExtendedSettings(client.rawbg); if (d.type === 'sgv') { info.noise = client.rawbg.noiseCodeToDisplay(d.mgdl, d.noise); - if (client.rawbg.showRawBGs(d.mgdl, d.noise, client.ddata.cal, client.sbx)) { - info.value = utils.scaleMgdl(client.rawbg.calc(d, client.ddata.cal, client.sbx)); + if (client.rawbg.showRawBGs(d.mgdl, d.noise, client.ddata.cal, sbx)) { + info.value = utils.scaleMgdl(client.rawbg.calc(d, client.ddata.cal, sbx)); } } return info; From 99658b87acedd3bb89f4767b21d050890b36eefe Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Sat, 18 Aug 2018 09:29:03 -0500 Subject: [PATCH 09/11] Add preference to choose between plotting filtered or unfiltered raw values. --- README.md | 1 + lib/plugins/rawbg.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b78ae28851b..2da612ea8ed 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,7 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm ##### `rawbg` (Raw BG) Calculates BG using sensor and calibration records from and displays an alternate BG values and noise levels. Defaults that can be adjusted with [extended setting](#extended-settings) * `SMOOTH` (`false`) - If `SMOOTH` is false, rawbg will be calculated such that it exagerates direction changes to provide early visible indication of glucose trend changes. + * `UNFILTERED` (`false`) - If `UNFILTERED` is false, rawbg will be calculated using the raw filtered value. If `UNFILTERED` is true, rawbg will be calculated using the raw unfiltered value. ##### `iob` (Insulin-on-Board) Adds the IOB pill visualization in the client and calculates values that used by other plugins. Uses treatments with insulin doses and the `dia` and `sens` fields from the [treatment profile](#treatment-profile). diff --git a/lib/plugins/rawbg.js b/lib/plugins/rawbg.js index f988603d5db..32f9eb7503d 100644 --- a/lib/plugins/rawbg.js +++ b/lib/plugins/rawbg.js @@ -16,6 +16,7 @@ function init (ctx) { rawbg.getPrefs = function getPrefs (sbx) { return { smooth: (sbx && sbx.extendedSettings.smooth) ? sbx.extendedSettings.smooth : false + unfiltered: (sbx && sbx.extendedSettings.unfiltered) ? sbx.extendedSettings.unfiltered : false }; }; @@ -63,7 +64,7 @@ function init (ctx) { if (cleaned.slope === 0 || cleaned.unfiltered === 0 || cleaned.scale === 0) { raw = 0; - } else if (cleaned.filtered === 0 || sgv.mgdl < 40) { + } else if (cleaned.filtered === 0 || sgv.mgdl < 40 || prefs.unfiltered) { raw = cleaned.scale * (cleaned.unfiltered - cleaned.intercept) / cleaned.slope; } else if (prefs.smooth) { raw = cleaned.scale * (cleaned.filtered - cleaned.intercept) / cleaned.slope; From b4f16622ab0c26a59010adab11b7864d1073ce42 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Sat, 18 Aug 2018 09:36:30 -0500 Subject: [PATCH 10/11] Fix syntax error. --- lib/plugins/rawbg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/rawbg.js b/lib/plugins/rawbg.js index 32f9eb7503d..7f769b78be4 100644 --- a/lib/plugins/rawbg.js +++ b/lib/plugins/rawbg.js @@ -16,7 +16,7 @@ function init (ctx) { rawbg.getPrefs = function getPrefs (sbx) { return { smooth: (sbx && sbx.extendedSettings.smooth) ? sbx.extendedSettings.smooth : false - unfiltered: (sbx && sbx.extendedSettings.unfiltered) ? sbx.extendedSettings.unfiltered : false + , unfiltered: (sbx && sbx.extendedSettings.unfiltered) ? sbx.extendedSettings.unfiltered : false }; }; From 5679266feb6a03499a4676724cad5a19d33c469d Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Tue, 1 Jan 2019 15:50:29 -0600 Subject: [PATCH 11/11] Updated rawbg settings to use a single setting tri-state variable. --- README.md | 6 ++++-- lib/plugins/rawbg.js | 7 +++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2da612ea8ed..9b327fd5187 100644 --- a/README.md +++ b/README.md @@ -319,8 +319,10 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm ##### `rawbg` (Raw BG) Calculates BG using sensor and calibration records from and displays an alternate BG values and noise levels. Defaults that can be adjusted with [extended setting](#extended-settings) - * `SMOOTH` (`false`) - If `SMOOTH` is false, rawbg will be calculated such that it exagerates direction changes to provide early visible indication of glucose trend changes. - * `UNFILTERED` (`false`) - If `UNFILTERED` is false, rawbg will be calculated using the raw filtered value. If `UNFILTERED` is true, rawbg will be calculated using the raw unfiltered value. + * `DISPLAY` (`unsmoothed`) - Allows the user to control which algorithm is used to calculate the displayed raw BG values using the most recent calibration record. + * `unfiltered` - Raw BG is calculated by applying the calibration to the glucose record's unfiltered value. + * `filtered` - Raw BG is calculated by applying the calibration to the glucose record's filtered value. The glucose record's filtered values are generally produced by the CGM by a running average of the unfiltered values to produce a smoothed value when the sensor noise is high. + * `unsmoothed` - Raw BG is calculated by first finding the ratio of the calculated filtered value (the same value calculated by the `filtered` setting) to the reported glucose value. The displayed raw BG value is calculated by dividing the calculated unfiltered value (the same value calculated by the `unfiltered` setting) by the ratio. The effect is to exagerate changes in trend direction so the trend changes are more noticeable to the user. This is the legacy raw BG calculation algorithm. ##### `iob` (Insulin-on-Board) Adds the IOB pill visualization in the client and calculates values that used by other plugins. Uses treatments with insulin doses and the `dia` and `sens` fields from the [treatment profile](#treatment-profile). diff --git a/lib/plugins/rawbg.js b/lib/plugins/rawbg.js index 7f769b78be4..f19e669f63b 100644 --- a/lib/plugins/rawbg.js +++ b/lib/plugins/rawbg.js @@ -15,8 +15,7 @@ function init (ctx) { rawbg.getPrefs = function getPrefs (sbx) { return { - smooth: (sbx && sbx.extendedSettings.smooth) ? sbx.extendedSettings.smooth : false - , unfiltered: (sbx && sbx.extendedSettings.unfiltered) ? sbx.extendedSettings.unfiltered : false + display: (sbx && sbx.extendedSettings.display) ? sbx.extendedSettings.display : 'unsmoothed' }; }; @@ -64,9 +63,9 @@ function init (ctx) { if (cleaned.slope === 0 || cleaned.unfiltered === 0 || cleaned.scale === 0) { raw = 0; - } else if (cleaned.filtered === 0 || sgv.mgdl < 40 || prefs.unfiltered) { + } else if (cleaned.filtered === 0 || sgv.mgdl < 40 || prefs.display === 'unfiltered') { raw = cleaned.scale * (cleaned.unfiltered - cleaned.intercept) / cleaned.slope; - } else if (prefs.smooth) { + } else if (prefs.display === 'filtered') { raw = cleaned.scale * (cleaned.filtered - cleaned.intercept) / cleaned.slope; } else { var ratio = cleaned.scale * (cleaned.filtered - cleaned.intercept) / cleaned.slope / sgv.mgdl;