From 6c70fd3a0648bbfa639ec2c4d472c9aa57d20f50 Mon Sep 17 00:00:00 2001 From: Tim Gunn <2896311+Tornado-Tim@users.noreply.github.com> Date: Fri, 3 Jan 2020 13:12:05 +1300 Subject: [PATCH] Update to use 24 and 8 hours worth of sensitivity --- .../sensitivity/SensitivityOref1Plugin.java | 193 +++++++++++------- 1 file changed, 117 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/sensitivity/SensitivityOref1Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/sensitivity/SensitivityOref1Plugin.java index 131cafe89c..abc6bde1e8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/sensitivity/SensitivityOref1Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/sensitivity/SensitivityOref1Plugin.java @@ -79,8 +79,17 @@ public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { List siteChanges = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime, CareportalEvent.SITECHANGE, true); List profileSwitches = MainApp.getDbHelper().getProfileSwitchEventsFromTime(fromTime, true); - List deviationsArray = new ArrayList<>(); - String pastSensitivity = ""; + //[0] = 8 hour + //[1] = 24 hour + //Deviationshour has DeviationsArray + List deviationshour = Arrays.asList(new ArrayList(),new ArrayList()); + List pastSensitivityArray = Arrays.asList("",""); + List sensResultArray = Arrays.asList("",""); + List ratioArray = Arrays.asList(0d,0d); + List ratioLimitArray = Arrays.asList("",""); + List hoursDetection = Arrays.asList(8d,24d); + + int index = 0; while (index < autosensDataTable.size()) { AutosensData autosensData = autosensDataTable.valueAt(index); @@ -94,105 +103,137 @@ public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { index++; continue; } + int hoursegment = 0; + //hoursegment = 0 = 8 hour + //hoursegment = 1 = 24 hour + while (hoursegment < deviationshour.size()){ + ArrayList deviationsArray = deviationshour.get(hoursegment); + String pastSensitivity = pastSensitivityArray.get(hoursegment); - // reset deviations after site change - if (CareportalEvent.isEvent5minBack(siteChanges, autosensData.time)) { - deviationsArray.clear(); - pastSensitivity += "(SITECHANGE)"; - } + // reset deviations after site change + if (CareportalEvent.isEvent5minBack(siteChanges, autosensData.time)) { + deviationsArray.clear(); + pastSensitivity += "(SITECHANGE)"; + } - // reset deviations after profile switch - if (ProfileSwitch.isEvent5minBack(profileSwitches, autosensData.time, true)) { - deviationsArray.clear(); - pastSensitivity += "(PROFILESWITCH)"; - } + // reset deviations after profile switch + if (ProfileSwitch.isEvent5minBack(profileSwitches, autosensData.time, true)) { + deviationsArray.clear(); + pastSensitivity += "(PROFILESWITCH)"; + } - double deviation = autosensData.deviation; + double deviation = autosensData.deviation; - //set positive deviations to zero if bg < 80 - if (autosensData.bg < 80 && deviation > 0) - deviation = 0; + //set positive deviations to zero if bg < 80 + if (autosensData.bg < 80 && deviation > 0) + deviation = 0; - if (autosensData.validDeviation) - deviationsArray.add(deviation); + if (autosensData.validDeviation) + if (autosensData.time > toTime - hoursDetection.get(hoursegment) * 60 * 60 * 1000L) + deviationsArray.add(deviation); - for (int i = 0; i < autosensData.extraDeviation.size(); i++) - deviationsArray.add(autosensData.extraDeviation.get(i)); - if (deviationsArray.size() > 96) - deviationsArray.remove(0); + if (hoursegment == 0) { + for (int i = 0; i < autosensData.extraDeviation.size(); i++) + deviationsArray.add(autosensData.extraDeviation.get(i)); + } + if (deviationsArray.size() > hoursDetection.get(hoursegment) * 60 / 5){ + deviationsArray.remove(0); + } - pastSensitivity += autosensData.pastSensitivity; - int secondsFromMidnight = Profile.secondsFromMidnight(autosensData.time); - if (secondsFromMidnight % 3600 < 2.5 * 60 || secondsFromMidnight % 3600 > 57.5 * 60) { - pastSensitivity += "(" + Math.round(secondsFromMidnight / 3600d) + ")"; - } - index++; + pastSensitivity += autosensData.pastSensitivity; + int secondsFromMidnight = Profile.secondsFromMidnight(autosensData.time); + + if (secondsFromMidnight % 3600 < 2.5 * 60 || secondsFromMidnight % 3600 > 57.5 * 60) { + pastSensitivity += "(" + Math.round(secondsFromMidnight / 3600d) + ")"; + } + + //Update the data back to the parent + deviationshour.set(hoursegment,deviationsArray); + pastSensitivityArray.set(hoursegment,pastSensitivity); + hoursegment++; + } + index++; } // when we have less than 8h worth of deviation data, add up to 90m of zero deviations // this dampens any large sensitivity changes detected based on too little data, without ignoring them completely + // only apply for 8 hours of devations + ArrayList dev8h = deviationshour.get(0); + if (L.isEnabled(L.AUTOSENS)) - log.debug("Using most recent " + deviationsArray.size() + " deviations"); - if (deviationsArray.size() < 96) { - int pad = (int) Math.round((1 - (double) deviationsArray.size() / 96) * 18); + log.debug("Using most recent " + dev8h.size() + " deviations"); + if (dev8h.size() < 96) { + int pad = (int) Math.round((1 - (double) dev8h.size() / 96) * 18); if (L.isEnabled(L.AUTOSENS)) log.debug("Adding " + pad + " more zero deviations"); - for (int d = 0; d < pad; d++) { - //process.stderr.write("."); - deviationsArray.add(0d); + for (int d = 0; d < pad; d++) { ; + dev8h.add(0d); } } + //Update the data back to the parent + deviationshour.set(0,dev8h); - Double[] deviations = new Double[deviationsArray.size()]; - deviations = deviationsArray.toArray(deviations); - - double sens = profile.getIsfMgdl(); - - double ratio = 1; - String ratioLimit = ""; - String sensResult = ""; - - if (L.isEnabled(L.AUTOSENS)) - log.debug("Records: " + index + " " + pastSensitivity); - - Arrays.sort(deviations); - /* Not used in calculation - for (double i = 0.9; i > 0.1; i = i - 0.01) { - if (IobCobCalculatorPlugin.percentile(deviations, (i + 0.01)) >= 0 && IobCobCalculatorPlugin.percentile(deviations, i) < 0) { - if (L.isEnabled(L.AUTOSENS)) - log.debug(Math.round(100 * i) + "% of non-meal deviations negative (>50% = sensitivity)"); + int hourused = 0; + while (hourused < deviationshour.size()){ + ArrayList deviationsArray = deviationshour.get(hourused); + String pastSensitivity = pastSensitivityArray.get(hourused); + String sensResult = "(8 hours) "; + String senstime = sensResult; + if (hourused == 1){ + senstime = "(24 hours) "; + sensResult = senstime; } - if (IobCobCalculatorPlugin.percentile(deviations, (i + 0.01)) > 0 && IobCobCalculatorPlugin.percentile(deviations, i) <= 0) { - if (L.isEnabled(L.AUTOSENS)) - log.debug(Math.round(100 * i) + "% of non-meal deviations positive (>50% = resistance)"); + String ratioLimit = ""; + + Double[] deviations = new Double[deviationsArray.size()]; + deviations = (Double[]) deviationsArray.toArray(deviations); + + double sens = profile.getIsf(); + + if (L.isEnabled(L.AUTOSENS)) + log.debug(senstime + "Records: " + index + " " + pastSensitivity); + + Arrays.sort(deviations); + double pSensitive = IobCobCalculatorPlugin.percentile(deviations, 0.50); + double pResistant = IobCobCalculatorPlugin.percentile(deviations, 0.50); + + double basalOff = 0; + + if (pSensitive < 0) { // sensitive + basalOff = pSensitive * (60 / 5) / Profile.toMgdl(sens, profile.getUnits()); + sensResult+= "Excess insulin sensitivity detected"; + } else if (pResistant > 0) { // resistant + basalOff = pResistant * (60 / 5) / Profile.toMgdl(sens, profile.getUnits()); + sensResult+= "Excess insulin resistance detected"; + } else { + sensResult+= "Sensitivity normal"; } - } - */ - double pSensitive = IobCobCalculatorPlugin.percentile(deviations, 0.50); - double pResistant = IobCobCalculatorPlugin.percentile(deviations, 0.50); - double basalOff = 0; + if (L.isEnabled(L.AUTOSENS)) + log.debug(sensResult); - if (pSensitive < 0) { // sensitive - basalOff = pSensitive * (60 / 5.0) / sens; - sensResult = "Excess insulin sensitivity detected"; - } else if (pResistant > 0) { // resistant - basalOff = pResistant * (60 / 5.0) / sens; - sensResult = "Excess insulin resistance detected"; - } else { - sensResult = "Sensitivity normal"; + double ratio = 1 + (basalOff / profile.getMaxDailyBasal()); + + //Update the data back to the parent + sensResultArray.set(hourused,sensResult); + ratioArray.set(hourused,ratio); + ratioLimitArray.set(hourused,ratioLimit); + hourused++; } - if (L.isEnabled(L.AUTOSENS)) - log.debug(sensResult); - - ratio = 1 + (basalOff / profile.getMaxDailyBasal()); - - AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit, - sensResult, deviationsArray.size()); + int key = 1; + String comparison = " 8 h ratio " +ratioArray.get(0)+" vs 24h ratio "+ratioArray.get(1); + //use 24 hour ratio by default + //if the 8 hour ratio is less than the 24 hour ratio, the 8 hour ratio is used + if(ratioArray.get(0) < ratioArray.get(1)){ + key = 0; + } + String message = hoursDetection.get(key)+" of sensitivity used"; + AutosensResult output = fillResult(ratioArray.get(key), current.cob, pastSensitivityArray.get(key), ratioLimitArray.get(key), + sensResultArray.get(key)+comparison, deviationshour.get(key).size()); if (L.isEnabled(L.AUTOSENS)) - log.debug("Sensitivity to: {} ratio: {} mealCOB: {}", + log.debug(message+" Sensitivity to: {} ratio: {} mealCOB: {}", new Date(toTime).toLocaleString(), output.ratio, current.cob); return output;