From 25950fc6cfda9aefefe557f89874159daac1fab0 Mon Sep 17 00:00:00 2001 From: rob Date: Sun, 31 Mar 2019 11:03:48 +1000 Subject: [PATCH] Libre/miaomiao delta modifications * Remove hard limit of processing 9 records. Processing will now stop at first value outside of time range of interest * Keep a list of all values within 2.5 minutes. Average this value to use as the now value * Don't change the now value date. Always use the time of the latest value * Added new test for data spaced at 1 minute intervals, to mimic libre data from glimp * Fix existing test results for average < 2.5 minute data. Deltas are different due to not updating the now value time --- .../androidaps/data/GlucoseStatus.java | 16 +++++-- .../androidaps/data/GlucoseStatusTest.java | 45 ++++++++++++++++--- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/data/GlucoseStatus.java b/app/src/main/java/info/nightscout/androidaps/data/GlucoseStatus.java index d65bf6b99f..fc09cd5584 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/GlucoseStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/data/GlucoseStatus.java @@ -72,8 +72,6 @@ public class GlucoseStatus { return null; } - sizeRecords = Math.min(sizeRecords, 9); - if (data.get(0).date < DateUtil.now() - 7 * 60 * 1000L && !allowOldData) { return null; } @@ -93,10 +91,14 @@ public class GlucoseStatus { return status.round(); } + ArrayList now_value_list = new ArrayList(); ArrayList last_deltas = new ArrayList(); ArrayList short_deltas = new ArrayList(); ArrayList long_deltas = new ArrayList(); + // Use the latest sgv value in the now calculations + now_value_list.add(now.value); + for (int i = 1; i < sizeRecords; i++) { if (data.get(i).value > 38) { BgReading then = data.get(i); @@ -111,8 +113,9 @@ public class GlucoseStatus { // use the average of all data points in the last 2.5m for all further "now" calculations if (0 < minutesago && minutesago < 2.5) { - now.value = (now.value + then.value) / 2; - now_date = (now_date + then_date) / 2; + // Keep and average all values within the last 2.5 minutes + now_value_list.add(then.value); + now.value = average(now_value_list); // short_deltas are calculated from everything ~5-15 minutes ago } else if (2.5 < minutesago && minutesago < 17.5) { //console.error(minutesago, avgdelta); @@ -125,6 +128,11 @@ public class GlucoseStatus { } else if (17.5 < minutesago && minutesago < 42.5) { long_deltas.add(avgdelta); } + else + { + // Do not process any more records after >= 42.5 minutes + break; + } } } diff --git a/app/src/test/java/info/nightscout/androidaps/data/GlucoseStatusTest.java b/app/src/test/java/info/nightscout/androidaps/data/GlucoseStatusTest.java index 2ef139ffbd..b7a1d8b0b2 100644 --- a/app/src/test/java/info/nightscout/androidaps/data/GlucoseStatusTest.java +++ b/app/src/test/java/info/nightscout/androidaps/data/GlucoseStatusTest.java @@ -51,7 +51,6 @@ public class GlucoseStatusTest { when(iobCobCalculatorPlugin.getBgReadings()).thenReturn(generateValidBgData()); GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); - Assert.assertEquals(214d, glucoseStatus.glucose, 0.001d); Assert.assertEquals(-2d, glucoseStatus.delta, 0.001d); Assert.assertEquals(-2.5d, glucoseStatus.short_avgdelta, 0.001d); // -2 -2.5 -3 deltas are relative to current value @@ -65,13 +64,12 @@ public class GlucoseStatusTest { when(iobCobCalculatorPlugin.getBgReadings()).thenReturn(generateMostRecentBgData()); GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); - Assert.assertEquals(215d, glucoseStatus.glucose, 0.001d); // (214+216) / 2 - Assert.assertEquals(-1.25d, glucoseStatus.delta, 0.001d); - Assert.assertEquals(-1.25d, glucoseStatus.short_avgdelta, 0.001d); - Assert.assertEquals(-1.25d, glucoseStatus.avgdelta, 0.001d); + Assert.assertEquals(-1.0d, glucoseStatus.delta, 0.001d); + Assert.assertEquals(-1.0d, glucoseStatus.short_avgdelta, 0.001d); + Assert.assertEquals(-1.0d, glucoseStatus.avgdelta, 0.001d); Assert.assertEquals(0d, glucoseStatus.long_avgdelta, 0.001d); - Assert.assertEquals(1514766850000L, glucoseStatus.date); // date is average too + Assert.assertEquals(1514766900000L, glucoseStatus.date); // latest date, even when averaging } @Test @@ -117,6 +115,20 @@ public class GlucoseStatusTest { Assert.assertEquals(0d, GlucoseStatus.average(new ArrayList<>()), 0.001d); } + @Test + public void calculateGlucoseStatusForLibreTestBgData() { + when(iobCobCalculatorPlugin.getBgReadings()).thenReturn(generateLibreTestData()); + + GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); + + Assert.assertEquals(100d, glucoseStatus.glucose, 0.001d); // + Assert.assertEquals(-10d, glucoseStatus.delta, 0.001d); + Assert.assertEquals(-10d, glucoseStatus.short_avgdelta, 0.001d); + Assert.assertEquals(-10d, glucoseStatus.avgdelta, 0.001d); + Assert.assertEquals(-10d, glucoseStatus.long_avgdelta, 0.001d); + Assert.assertEquals(1514766900000L, glucoseStatus.date); // latest date + } + @Before public void initMocking() { AAPSMocker.mockMainApp(); @@ -184,4 +196,25 @@ public class GlucoseStatusTest { } return list; } + List generateLibreTestData() { + List list = new ArrayList<>(); + try { + long end_time = 1514766900000L; + double latest_reading = 100d; + // Now + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":" + (latest_reading) + ",\"mills\":" + (end_time) + ",\"direction\":\"Flat\"}")))); + // One minute ago + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":" + (latest_reading) + ",\"mills\":" + (end_time - (1000 * 60 * 1)) + ",\"direction\":\"Flat\"}")))); + // Two minutes ago + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":" + (latest_reading) + ",\"mills\":" + (end_time - (1000 * 60 * 2)) + ",\"direction\":\"Flat\"}")))); + + // Three minutes and beyond at constant rate + for (int i=3; i < 50; i++) { + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":" + (latest_reading + (i*2)) + ",\"mills\":" + (end_time - (1000 * 60 * i)) + ",\"direction\":\"Flat\"}")))); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return list; + } }