From 07f7c00d85a302d89dc6617249ebb39938d051ed Mon Sep 17 00:00:00 2001 From: "Markus M. May" Date: Sun, 24 Jun 2018 18:20:28 +0200 Subject: [PATCH] Move Algo to abstract class --- .../interfaces/SensitivityInterface.java | 41 ----------- .../AbstractSensitivityPlugin.java | 68 +++++++++++++++++++ .../Sensitivity/SensitivityAAPSPlugin.java | 2 +- .../Sensitivity/SensitivityOref0Plugin.java | 2 +- .../Sensitivity/SensitivityOref1Plugin.java | 2 +- .../SensitivityWeightedAveragePlugin.java | 2 +- ...ava => AbstractSensitivityPluginTest.java} | 14 +++- 7 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/AbstractSensitivityPlugin.java rename app/src/test/java/info/nightscout/androidaps/plugins/Sensitivity/{SensitivityInterfaceTest.java => AbstractSensitivityPluginTest.java} (73%) diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java index 669dbe69fd..25dab4494b 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java @@ -20,50 +20,9 @@ import info.nightscout.utils.SafeParse; public interface SensitivityInterface { - Logger LOG = LoggerFactory.getLogger(SensitivityInterface.class); - double MIN_HOURS = 1; double MIN_HOURS_FULL_AUTOSENS = 4; AutosensResult detectSensitivity(long fromTime, long toTime); - default AutosensResult fillResult(double ratio, double carbsAbsorbed, String pastSensitivity, - String ratioLimit, String sensResult, int deviationsArraySize) { - return this.fillResult(ratio, carbsAbsorbed, pastSensitivity, ratioLimit, sensResult, - deviationsArraySize, - SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")), - SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2"))); - } - - default AutosensResult fillResult(double ratio, double carbsAbsorbed, String pastSensitivity, - String ratioLimit, String sensResult, int deviationsArraySize, - double ratioMin, double ratioMax) { - double rawRatio = ratio; - ratio = Math.max(ratio, ratioMin); - ratio = Math.min(ratio, ratioMax); - - //If not-excluded data <= MIN_HOURS -> don't do Autosens - //If not-excluded data >= MIN_HOURS_FULL_AUTOSENS -> full Autosens - //Between MIN_HOURS and MIN_HOURS_FULL_AUTOSENS: gradually increase autosens - double autosensContrib = (Math.min(Math.max(MIN_HOURS, deviationsArraySize / 12d), - MIN_HOURS_FULL_AUTOSENS) - MIN_HOURS) / (MIN_HOURS_FULL_AUTOSENS - MIN_HOURS); - ratio = autosensContrib * (ratio - 1) + 1; - - if (autosensContrib != 1d) { - ratioLimit += "(" + deviationsArraySize + " of " + MIN_HOURS_FULL_AUTOSENS * 12 + " values) "; - } - - if (ratio != rawRatio) { - ratioLimit += "Ratio limited from " + rawRatio + " to " + ratio; - LOG.debug(ratioLimit); - } - - AutosensResult output = new AutosensResult(); - output.ratio = Round.roundTo(ratio, 0.01); - output.carbsAbsorbed = Round.roundTo(carbsAbsorbed, 0.01); - output.pastSensitivity = pastSensitivity; - output.ratioLimit = ratioLimit; - output.sensResult = sensResult; - return output; - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/AbstractSensitivityPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/AbstractSensitivityPlugin.java new file mode 100644 index 0000000000..f05cea1b9d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/AbstractSensitivityPlugin.java @@ -0,0 +1,68 @@ +package info.nightscout.androidaps.plugins.Sensitivity; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.PluginDescription; +import info.nightscout.androidaps.interfaces.SensitivityInterface; +import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; +import info.nightscout.utils.Round; +import info.nightscout.utils.SP; +import info.nightscout.utils.SafeParse; + +public abstract class AbstractSensitivityPlugin extends PluginBase implements SensitivityInterface { + + private static final Logger log = LoggerFactory.getLogger(SensitivityInterface.class); + + public AbstractSensitivityPlugin(PluginDescription pluginDescription) { + super(pluginDescription); + } + + @Override + public abstract AutosensResult detectSensitivity(long fromTime, long toTime); + + public AutosensResult fillResult(double ratio, double carbsAbsorbed, String pastSensitivity, + String ratioLimit, String sensResult, int deviationsArraySize) { + return this.fillResult(ratio, carbsAbsorbed, pastSensitivity, ratioLimit, sensResult, + deviationsArraySize, + SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")), + SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2"))); + } + + public AutosensResult fillResult(double ratio, double carbsAbsorbed, String pastSensitivity, + String ratioLimit, String sensResult, int deviationsArraySize, + double ratioMin, double ratioMax) { + double rawRatio = ratio; + ratio = Math.max(ratio, ratioMin); + ratio = Math.min(ratio, ratioMax); + + //If not-excluded data <= MIN_HOURS -> don't do Autosens + //If not-excluded data >= MIN_HOURS_FULL_AUTOSENS -> full Autosens + //Between MIN_HOURS and MIN_HOURS_FULL_AUTOSENS: gradually increase autosens + double autosensContrib = (Math.min(Math.max(MIN_HOURS, deviationsArraySize / 12d), + MIN_HOURS_FULL_AUTOSENS) - MIN_HOURS) / (MIN_HOURS_FULL_AUTOSENS - MIN_HOURS); + ratio = autosensContrib * (ratio - 1) + 1; + + if (autosensContrib != 1d) { + ratioLimit += "(" + deviationsArraySize + " of " + MIN_HOURS_FULL_AUTOSENS * 12 + " values) "; + } + + if (ratio != rawRatio) { + ratioLimit += "Ratio limited from " + rawRatio + " to " + ratio; + log.debug(ratioLimit); + } + + AutosensResult output = new AutosensResult(); + output.ratio = Round.roundTo(ratio, 0.01); + output.carbsAbsorbed = Round.roundTo(carbsAbsorbed, 0.01); + output.pastSensitivity = pastSensitivity; + output.ratioLimit = ratioLimit; + output.sensResult = sensResult; + return output; + } + + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityAAPSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityAAPSPlugin.java index 9a45633cd0..fdf85c064e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityAAPSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityAAPSPlugin.java @@ -30,7 +30,7 @@ import info.nightscout.utils.SafeParse; * Created by mike on 24.06.2017. */ -public class SensitivityAAPSPlugin extends PluginBase implements SensitivityInterface { +public class SensitivityAAPSPlugin extends AbstractSensitivityPlugin { private static Logger log = LoggerFactory.getLogger(SensitivityAAPSPlugin.class); static SensitivityAAPSPlugin plugin = null; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref0Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref0Plugin.java index 87228fc467..cfe5dd28f6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref0Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref0Plugin.java @@ -30,7 +30,7 @@ import info.nightscout.utils.SafeParse; * Created by mike on 24.06.2017. */ -public class SensitivityOref0Plugin extends PluginBase implements SensitivityInterface { +public class SensitivityOref0Plugin extends AbstractSensitivityPlugin { private static Logger log = LoggerFactory.getLogger(IobCobCalculatorPlugin.class); static SensitivityOref0Plugin plugin = null; 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 1b1901e662..f6d5a4ef2a 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 @@ -30,7 +30,7 @@ import info.nightscout.utils.SafeParse; * Created by mike on 19.06.2018. */ -public class SensitivityOref1Plugin extends PluginBase implements SensitivityInterface { +public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { private static Logger log = LoggerFactory.getLogger(IobCobCalculatorPlugin.class); static SensitivityOref1Plugin plugin = null; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityWeightedAveragePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityWeightedAveragePlugin.java index cfea341551..ed185e88f9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityWeightedAveragePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityWeightedAveragePlugin.java @@ -28,7 +28,7 @@ import info.nightscout.utils.SafeParse; * Created by mike on 24.06.2017. */ -public class SensitivityWeightedAveragePlugin extends PluginBase implements SensitivityInterface { +public class SensitivityWeightedAveragePlugin extends AbstractSensitivityPlugin { private static Logger log = LoggerFactory.getLogger(SensitivityWeightedAveragePlugin.class); private static SensitivityWeightedAveragePlugin plugin = null; diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityInterfaceTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Sensitivity/AbstractSensitivityPluginTest.java similarity index 73% rename from app/src/test/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityInterfaceTest.java rename to app/src/test/java/info/nightscout/androidaps/plugins/Sensitivity/AbstractSensitivityPluginTest.java index 9a2569a83e..0a4b01cd64 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityInterfaceTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Sensitivity/AbstractSensitivityPluginTest.java @@ -2,14 +2,24 @@ package info.nightscout.androidaps.plugins.Sensitivity; import org.junit.Test; +import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.SensitivityInterface; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import static org.junit.Assert.assertEquals; -public class SensitivityInterfaceTest { +public class AbstractSensitivityPluginTest { + + private class SensitivityTestClass extends AbstractSensitivityPlugin { + + public SensitivityTestClass() { + super(null); + } + + public SensitivityTestClass(PluginDescription pluginDescription) { + super(pluginDescription); + } - private class SensitivityTestClass implements SensitivityInterface { @Override public AutosensResult detectSensitivity(long fromTime, long toTime) { return null;