Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rawbg calculations #3360

Closed
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ 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)
* `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).
Expand Down
6 changes: 4 additions & 2 deletions lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions lib/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions lib/plugins/rawbg.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ function init (ctx) {
, pillFlip: true
};

rawbg.getPrefs = function getPrefs (sbx) {
return {
display: (sbx && sbx.extendedSettings.display) ? sbx.extendedSettings.display : 'unsmoothed'
};
};

rawbg.setProperties = function setProperties (sbx) {
sbx.offerProperty('rawbg', function setRawBG ( ) {
var result = { };
Expand Down Expand Up @@ -49,14 +55,18 @@ 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 (cleaned.filtered === 0 || sgv.mgdl < 40) {
} else if (cleaned.filtered === 0 || sgv.mgdl < 40 || prefs.display === 'unfiltered') {
raw = cleaned.scale * (cleaned.unfiltered - cleaned.intercept) / cleaned.slope;
} 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;
raw = cleaned.scale * (cleaned.unfiltered - cleaned.intercept) / cleaned.slope / ratio;
Expand Down