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
This commit is contained in:
rob 2019-03-31 11:03:48 +10:00
parent 361ffa91c8
commit 25950fc6cf
2 changed files with 51 additions and 10 deletions

View file

@ -72,8 +72,6 @@ public class GlucoseStatus {
return null; return null;
} }
sizeRecords = Math.min(sizeRecords, 9);
if (data.get(0).date < DateUtil.now() - 7 * 60 * 1000L && !allowOldData) { if (data.get(0).date < DateUtil.now() - 7 * 60 * 1000L && !allowOldData) {
return null; return null;
} }
@ -93,10 +91,14 @@ public class GlucoseStatus {
return status.round(); return status.round();
} }
ArrayList<Double> now_value_list = new ArrayList<Double>();
ArrayList<Double> last_deltas = new ArrayList<Double>(); ArrayList<Double> last_deltas = new ArrayList<Double>();
ArrayList<Double> short_deltas = new ArrayList<Double>(); ArrayList<Double> short_deltas = new ArrayList<Double>();
ArrayList<Double> long_deltas = new ArrayList<Double>(); ArrayList<Double> long_deltas = new ArrayList<Double>();
// Use the latest sgv value in the now calculations
now_value_list.add(now.value);
for (int i = 1; i < sizeRecords; i++) { for (int i = 1; i < sizeRecords; i++) {
if (data.get(i).value > 38) { if (data.get(i).value > 38) {
BgReading then = data.get(i); 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 // use the average of all data points in the last 2.5m for all further "now" calculations
if (0 < minutesago && minutesago < 2.5) { if (0 < minutesago && minutesago < 2.5) {
now.value = (now.value + then.value) / 2; // Keep and average all values within the last 2.5 minutes
now_date = (now_date + then_date) / 2; now_value_list.add(then.value);
now.value = average(now_value_list);
// short_deltas are calculated from everything ~5-15 minutes ago // short_deltas are calculated from everything ~5-15 minutes ago
} else if (2.5 < minutesago && minutesago < 17.5) { } else if (2.5 < minutesago && minutesago < 17.5) {
//console.error(minutesago, avgdelta); //console.error(minutesago, avgdelta);
@ -125,6 +128,11 @@ public class GlucoseStatus {
} else if (17.5 < minutesago && minutesago < 42.5) { } else if (17.5 < minutesago && minutesago < 42.5) {
long_deltas.add(avgdelta); long_deltas.add(avgdelta);
} }
else
{
// Do not process any more records after >= 42.5 minutes
break;
}
} }
} }

View file

@ -51,7 +51,6 @@ public class GlucoseStatusTest {
when(iobCobCalculatorPlugin.getBgReadings()).thenReturn(generateValidBgData()); when(iobCobCalculatorPlugin.getBgReadings()).thenReturn(generateValidBgData());
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
Assert.assertEquals(214d, glucoseStatus.glucose, 0.001d); Assert.assertEquals(214d, glucoseStatus.glucose, 0.001d);
Assert.assertEquals(-2d, glucoseStatus.delta, 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 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()); when(iobCobCalculatorPlugin.getBgReadings()).thenReturn(generateMostRecentBgData());
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
Assert.assertEquals(215d, glucoseStatus.glucose, 0.001d); // (214+216) / 2 Assert.assertEquals(215d, glucoseStatus.glucose, 0.001d); // (214+216) / 2
Assert.assertEquals(-1.25d, glucoseStatus.delta, 0.001d); Assert.assertEquals(-1.0d, glucoseStatus.delta, 0.001d);
Assert.assertEquals(-1.25d, glucoseStatus.short_avgdelta, 0.001d); Assert.assertEquals(-1.0d, glucoseStatus.short_avgdelta, 0.001d);
Assert.assertEquals(-1.25d, glucoseStatus.avgdelta, 0.001d); Assert.assertEquals(-1.0d, glucoseStatus.avgdelta, 0.001d);
Assert.assertEquals(0d, glucoseStatus.long_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 @Test
@ -117,6 +115,20 @@ public class GlucoseStatusTest {
Assert.assertEquals(0d, GlucoseStatus.average(new ArrayList<>()), 0.001d); 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 @Before
public void initMocking() { public void initMocking() {
AAPSMocker.mockMainApp(); AAPSMocker.mockMainApp();
@ -184,4 +196,25 @@ public class GlucoseStatusTest {
} }
return list; return list;
} }
List<BgReading> generateLibreTestData() {
List<BgReading> 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;
}
} }