From 93820634b58cc4d55aa2f4c590c409d7b7281163 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 7 Mar 2020 00:43:28 +0100 Subject: [PATCH] AbstractSensitivityPluginTest --- .../AbstractSensitivityPlugin.java | 73 ------------------- .../sensitivity/AbstractSensitivityPlugin.kt | 58 +++++++++++++++ .../sensitivity/SensitivityAAPSPlugin.java | 47 +++++------- .../sensitivity/SensitivityOref0Plugin.java | 53 +++++++------- .../sensitivity/SensitivityOref1Plugin.java | 41 ++++------- .../SensitivityWeightedAveragePlugin.java | 65 ++++++++--------- .../AbstractSensitivityPluginTest.java | 63 ---------------- .../AbstractSensitivityPluginTest.kt | 47 ++++++++++++ 8 files changed, 197 insertions(+), 250 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPlugin.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPlugin.kt delete mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPluginTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPluginTest.kt 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 deleted file mode 100644 index 6c8f061417..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPlugin.java +++ /dev/null @@ -1,73 +0,0 @@ -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.logging.AAPSLogger; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.utils.Round; -import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.utils.SafeParse; -import info.nightscout.androidaps.utils.resources.ResourceHelper; - -public abstract class AbstractSensitivityPlugin extends PluginBase implements SensitivityInterface { - - private static final Logger log = StacktraceLoggerWrapper.getLogger(L.AUTOSENS); - - public AbstractSensitivityPlugin(PluginDescription pluginDescription, AAPSLogger aapsLogger, ResourceHelper resourceHelper) { - super(pluginDescription, aapsLogger, resourceHelper); - } - - @Override - public abstract AutosensResult detectSensitivity(IobCobCalculatorPlugin plugin, long fromTime, long toTime); - - 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; - if (L.isEnabled(L.AUTOSENS)) - 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.kt b/app/src/main/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPlugin.kt new file mode 100644 index 0000000000..540f669e02 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPlugin.kt @@ -0,0 +1,58 @@ +package info.nightscout.androidaps.plugins.sensitivity + +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.logging.AAPSLogger +import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin +import info.nightscout.androidaps.utils.Round +import info.nightscout.androidaps.utils.SafeParse +import info.nightscout.androidaps.utils.resources.ResourceHelper +import info.nightscout.androidaps.utils.sharedPreferences.SP + +abstract class AbstractSensitivityPlugin(pluginDescription: PluginDescription, aapsLogger: AAPSLogger, resourceHelper: ResourceHelper, val sp: SP) : PluginBase(pluginDescription, aapsLogger, resourceHelper), SensitivityInterface { + + abstract override fun detectSensitivity(plugin: IobCobCalculatorPlugin, fromTime: Long, toTime: Long): AutosensResult + + fun fillResult(ratio: Double, carbsAbsorbed: Double, pastSensitivity: String, + ratioLimit: String, sensResult: String, deviationsArraySize: Int): AutosensResult { + return 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"))) + } + + fun fillResult(ratio: Double, carbsAbsorbed: Double, pastSensitivity: String, + ratioLimit: String, sensResult: String, deviationsArraySize: Int, + ratioMin: Double, ratioMax: Double): AutosensResult { + var ratio = ratio + var ratioLimit = ratioLimit + val 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 + val autosensContrib = (Math.min(Math.max(SensitivityInterface.MIN_HOURS, deviationsArraySize / 12.0), + SensitivityInterface.MIN_HOURS_FULL_AUTOSENS) - SensitivityInterface.MIN_HOURS) / (SensitivityInterface.MIN_HOURS_FULL_AUTOSENS - SensitivityInterface.MIN_HOURS) + ratio = autosensContrib * (ratio - 1) + 1 + if (autosensContrib != 1.0) { + ratioLimit += "(" + deviationsArraySize + " of " + SensitivityInterface.MIN_HOURS_FULL_AUTOSENS * 12 + " values) " + } + if (ratio != rawRatio) { + ratioLimit += "Ratio limited from $rawRatio to $ratio" + aapsLogger.debug(LTag.AUTOSENS, ratioLimit) + } + val output = 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 + } +} \ No newline at end of file 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 81d58ba74d..321a75b6e5 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 @@ -2,12 +2,8 @@ package info.nightscout.androidaps.plugins.sensitivity; import androidx.collection.LongSparseArray; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.Arrays; -import java.util.Date; import java.util.List; import javax.inject.Inject; @@ -21,15 +17,14 @@ import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.AAPSLogger; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; +import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.resources.ResourceHelper; +import info.nightscout.androidaps.utils.sharedPreferences.SP; /** * Created by mike on 24.06.2017. @@ -37,12 +32,14 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper; @Singleton public class SensitivityAAPSPlugin extends AbstractSensitivityPlugin { - private static Logger log = StacktraceLoggerWrapper.getLogger(L.AUTOSENS); + + private SP sp; @Inject public SensitivityAAPSPlugin( AAPSLogger aapsLogger, - ResourceHelper resourceHelper + ResourceHelper resourceHelper, + SP sp ) { super(new PluginDescription() .mainType(PluginType.SENSITIVITY) @@ -50,38 +47,37 @@ public class SensitivityAAPSPlugin extends AbstractSensitivityPlugin { .shortName(R.string.sensitivity_shortname) .preferencesId(R.xml.pref_absorption_aaps) .description(R.string.description_sensitivity_aaps), - aapsLogger, resourceHelper + aapsLogger, resourceHelper, sp ); + this.sp = sp; } @Override public AutosensResult detectSensitivity(IobCobCalculatorPlugin iobCobCalculatorPlugin, long fromTime, long toTime) { LongSparseArray autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable(); - String age = SP.getString(R.string.key_age, ""); + String age = sp.getString(R.string.key_age, ""); int defaultHours = 24; if (age.equals(MainApp.gs(R.string.key_adult))) defaultHours = 24; if (age.equals(MainApp.gs(R.string.key_teenage))) defaultHours = 4; if (age.equals(MainApp.gs(R.string.key_child))) defaultHours = 4; - int hoursForDetection = SP.getInt(R.string.key_openapsama_autosens_period, defaultHours); + int hoursForDetection = sp.getInt(R.string.key_openapsama_autosens_period, defaultHours); Profile profile = ProfileFunctions.getInstance().getProfile(); if (profile == null) { - log.error("No profile"); + getAapsLogger().error("No profile"); return new AutosensResult(); } if (autosensDataTable == null || autosensDataTable.size() < 4) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("No autosens data available. lastDataTime=" + iobCobCalculatorPlugin.lastDataTime()); + getAapsLogger().debug(LTag.AUTOSENS, "No autosens data available. lastDataTime=" + iobCobCalculatorPlugin.lastDataTime()); return new AutosensResult(); } AutosensData current = iobCobCalculatorPlugin.getAutosensData(toTime); // this is running inside lock already if (current == null) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("No autosens data available. toTime: " + DateUtil.dateAndTimeString(toTime) + " lastDataTime: " + iobCobCalculatorPlugin.lastDataTime()); + getAapsLogger().debug(LTag.AUTOSENS, "No autosens data available. toTime: " + DateUtil.dateAndTimeString(toTime) + " lastDataTime: " + iobCobCalculatorPlugin.lastDataTime()); return new AutosensResult(); } @@ -146,8 +142,7 @@ public class SensitivityAAPSPlugin extends AbstractSensitivityPlugin { String ratioLimit = ""; String sensResult = ""; - if (L.isEnabled(L.AUTOSENS)) - log.debug("Records: " + index + " " + pastSensitivity); + getAapsLogger().debug(LTag.AUTOSENS, "Records: " + index + " " + pastSensitivity); Arrays.sort(deviations); @@ -163,18 +158,16 @@ public class SensitivityAAPSPlugin extends AbstractSensitivityPlugin { sensResult = "Sensitivity normal"; } - if (L.isEnabled(L.AUTOSENS)) - log.debug(sensResult); + getAapsLogger().debug(LTag.AUTOSENS, sensResult); AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit, sensResult, deviationsArray.size()); - if (L.isEnabled(L.AUTOSENS)) { - log.debug("Sensitivity to: {}, percentile: {} ratio: {} mealCOB: ", - new Date(toTime).toLocaleString(), - percentile, output.ratio, ratio, current.cob); - log.debug("Sensitivity to: deviations " + Arrays.toString(deviations)); - } + getAapsLogger().debug(LTag.AUTOSENS, "Sensitivity to: " + + DateUtil.dateAndTimeString(toTime) + + " ratio: " + output.ratio + + " mealCOB: " + current.cob); + getAapsLogger().debug(LTag.AUTOSENS, "Sensitivity to: deviations " + Arrays.toString(deviations)); return output; } 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 6fe086d387..8575ef0056 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 @@ -1,13 +1,10 @@ package info.nightscout.androidaps.plugins.sensitivity; +import androidx.annotation.NonNull; import androidx.collection.LongSparseArray; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.Arrays; -import java.util.Date; import java.util.List; import javax.inject.Inject; @@ -21,26 +18,29 @@ import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.AAPSLogger; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.logging.LTag; +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.resources.ResourceHelper; +import info.nightscout.androidaps.utils.sharedPreferences.SP; /** * Created by mike on 24.06.2017. */ @Singleton public class SensitivityOref0Plugin extends AbstractSensitivityPlugin { - private static Logger log = StacktraceLoggerWrapper.getLogger(L.AUTOSENS); + + private ProfileFunction profileFunction; @Inject public SensitivityOref0Plugin( AAPSLogger aapsLogger, - ResourceHelper resourceHelper + ResourceHelper resourceHelper, + SP sp, + ProfileFunction profileFunction ) { super(new PluginDescription() .mainType(PluginType.SENSITIVITY) @@ -48,33 +48,32 @@ public class SensitivityOref0Plugin extends AbstractSensitivityPlugin { .shortName(R.string.sensitivity_shortname) .preferencesId(R.xml.pref_absorption_oref0) .description(R.string.description_sensitivity_oref0), - aapsLogger, resourceHelper + aapsLogger, resourceHelper, sp ); + this.profileFunction = profileFunction; } - @Override + @NonNull @Override public AutosensResult detectSensitivity(IobCobCalculatorPlugin iobCobCalculatorPlugin, long fromTime, long toTime) { LongSparseArray autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable(); int hoursForDetection = 24; - Profile profile = ProfileFunctions.getInstance().getProfile(); + Profile profile = profileFunction.getProfile(); if (profile == null) { - log.error("No profile"); + getAapsLogger().error("No profile"); return new AutosensResult(); } if (autosensDataTable == null || autosensDataTable.size() < 4) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("No autosens data available. lastDataTime=" + iobCobCalculatorPlugin.lastDataTime()); + getAapsLogger().debug(LTag.AUTOSENS, "No autosens data available. lastDataTime=" + iobCobCalculatorPlugin.lastDataTime()); return new AutosensResult(); } AutosensData current = iobCobCalculatorPlugin.getAutosensData(toTime); // this is running inside lock already if (current == null) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("No autosens data available. toTime: " + DateUtil.dateAndTimeString(toTime) + " lastDataTime: " + iobCobCalculatorPlugin.lastDataTime()); + getAapsLogger().debug(LTag.AUTOSENS, "No autosens data available. toTime: " + DateUtil.dateAndTimeString(toTime) + " lastDataTime: " + iobCobCalculatorPlugin.lastDataTime()); return new AutosensResult(); } @@ -135,19 +134,17 @@ public class SensitivityOref0Plugin extends AbstractSensitivityPlugin { double sens = profile.getIsfMgdl(); - double ratio = 1; + double ratio; String ratioLimit = ""; - String sensResult = ""; + String sensResult; - if (L.isEnabled(L.AUTOSENS)) - log.debug("Records: " + index + " " + pastSensitivity); + getAapsLogger().debug(LTag.AUTOSENS, "Records: " + index + " " + pastSensitivity); Arrays.sort(deviations); for (double i = 0.9; i > 0.1; i = i - 0.02) { if (IobCobCalculatorPlugin.percentile(deviations, (i + 0.02)) >= 0 && IobCobCalculatorPlugin.percentile(deviations, i) < 0) { - if (L.isEnabled(L.AUTOSENS)) - log.debug(Math.round(100 * i) + "% of non-meal deviations negative (target 45%-50%)"); + getAapsLogger().debug(LTag.AUTOSENS, Math.round(100 * i) + "% of non-meal deviations negative (target 45%-50%)"); } } double pSensitive = IobCobCalculatorPlugin.percentile(deviations, 0.50); @@ -165,17 +162,17 @@ public class SensitivityOref0Plugin extends AbstractSensitivityPlugin { sensResult = "Sensitivity normal"; } - if (L.isEnabled(L.AUTOSENS)) - log.debug(sensResult); + getAapsLogger().debug(LTag.AUTOSENS, sensResult); ratio = 1 + (basalOff / profile.getMaxDailyBasal()); AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit, sensResult, deviationsArray.size()); - if (L.isEnabled(L.AUTOSENS)) - log.debug("Sensitivity to: {} ratio: {} mealCOB: {}", - new Date(toTime).toLocaleString(), output.ratio, current.cob); + getAapsLogger().debug(LTag.AUTOSENS, "Sensitivity to: " + + DateUtil.dateAndTimeString(toTime) + + " ratio: " + output.ratio + + " mealCOB: " + current.cob); return output; } 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 8f47ae1757..6edc42ec91 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 @@ -2,12 +2,8 @@ package info.nightscout.androidaps.plugins.sensitivity; import androidx.collection.LongSparseArray; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.Arrays; -import java.util.Date; import java.util.List; import javax.inject.Inject; @@ -21,26 +17,26 @@ import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.AAPSLogger; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; +import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.resources.ResourceHelper; +import info.nightscout.androidaps.utils.sharedPreferences.SP; /** * Created by mike on 19.06.2018. */ @Singleton public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { - private static Logger log = StacktraceLoggerWrapper.getLogger(L.AUTOSENS); @Inject public SensitivityOref1Plugin( AAPSLogger aapsLogger, - ResourceHelper resourceHelper + ResourceHelper resourceHelper, + SP sp ) { super(new PluginDescription() .mainType(PluginType.SENSITIVITY) @@ -49,7 +45,7 @@ public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { .enableByDefault(true) .preferencesId(R.xml.pref_absorption_oref1) .description(R.string.description_sensitivity_oref1), - aapsLogger, resourceHelper + aapsLogger, resourceHelper, sp ); } @@ -62,21 +58,19 @@ public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { Profile profile = ProfileFunctions.getInstance().getProfile(); if (profile == null) { - log.error("No profile"); + getAapsLogger().error("No profile"); return new AutosensResult(); } if (autosensDataTable == null || autosensDataTable.size() < 4) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("No autosens data available. lastDataTime=" + iobCobCalculatorPlugin.lastDataTime()); + getAapsLogger().debug(LTag.AUTOSENS, "No autosens data available. lastDataTime=" + iobCobCalculatorPlugin.lastDataTime()); return new AutosensResult(); } // the current AutosensData current = iobCobCalculatorPlugin.getAutosensData(toTime); // this is running inside lock already if (current == null) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("No autosens data available. toTime: " + DateUtil.dateAndTimeString(toTime) + " lastDataTime: " + iobCobCalculatorPlugin.lastDataTime()); + getAapsLogger().debug(LTag.AUTOSENS, "No autosens data available. toTime: " + DateUtil.dateAndTimeString(toTime) + " lastDataTime: " + iobCobCalculatorPlugin.lastDataTime()); return new AutosensResult(); } @@ -135,12 +129,10 @@ public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { // 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 - if (L.isEnabled(L.AUTOSENS)) - log.debug("Using most recent " + deviationsArray.size() + " deviations"); + getAapsLogger().debug(LTag.AUTOSENS, "Using most recent " + deviationsArray.size() + " deviations"); if (deviationsArray.size() < 96) { int pad = (int) Math.round((1 - (double) deviationsArray.size() / 96) * 18); - if (L.isEnabled(L.AUTOSENS)) - log.debug("Adding " + pad + " more zero deviations"); + getAapsLogger().debug(LTag.AUTOSENS, "Adding " + pad + " more zero deviations"); for (int d = 0; d < pad; d++) { //process.stderr.write("."); deviationsArray.add(0d); @@ -156,8 +148,7 @@ public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { String ratioLimit = ""; String sensResult = ""; - if (L.isEnabled(L.AUTOSENS)) - log.debug("Records: " + index + " " + pastSensitivity); + getAapsLogger().debug(LTag.AUTOSENS, "Records: " + index + " " + pastSensitivity); Arrays.sort(deviations); /* Not used in calculation @@ -187,17 +178,17 @@ public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { sensResult = "Sensitivity normal"; } - if (L.isEnabled(L.AUTOSENS)) - log.debug(sensResult); + getAapsLogger().debug(LTag.AUTOSENS, sensResult); ratio = 1 + (basalOff / profile.getMaxDailyBasal()); AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit, sensResult, deviationsArray.size()); - if (L.isEnabled(L.AUTOSENS)) - log.debug("Sensitivity to: {} ratio: {} mealCOB: {}", - new Date(toTime).toLocaleString(), output.ratio, current.cob); + getAapsLogger().debug(LTag.AUTOSENS, "Sensitivity to: " + + DateUtil.dateAndTimeString(toTime) + + " ratio: " + output.ratio + + " mealCOB: " + current.cob); return output; } 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 f02f73a120..3a560eca21 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 @@ -2,10 +2,6 @@ package info.nightscout.androidaps.plugins.sensitivity; import androidx.collection.LongSparseArray; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Date; import java.util.List; import javax.inject.Inject; @@ -19,27 +15,31 @@ import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.AAPSLogger; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.logging.LTag; +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.resources.ResourceHelper; +import info.nightscout.androidaps.utils.sharedPreferences.SP; /** * Created by mike on 24.06.2017. */ @Singleton public class SensitivityWeightedAveragePlugin extends AbstractSensitivityPlugin { - private static Logger log = StacktraceLoggerWrapper.getLogger(L.AUTOSENS); + + private SP sp; + private ResourceHelper resourceHelper; + private ProfileFunction profileFunction; @Inject public SensitivityWeightedAveragePlugin( AAPSLogger aapsLogger, - ResourceHelper resourceHelper + ResourceHelper resourceHelper, + SP sp, + ProfileFunction profileFunction ) { super(new PluginDescription() .mainType(PluginType.SENSITIVITY) @@ -47,39 +47,39 @@ public class SensitivityWeightedAveragePlugin extends AbstractSensitivityPlugin .shortName(R.string.sensitivity_shortname) .preferencesId(R.xml.pref_absorption_aaps) .description(R.string.description_sensitivity_weighted_average), - aapsLogger, resourceHelper + aapsLogger, resourceHelper, sp ); + this.sp = sp; + this.resourceHelper = resourceHelper; + this.profileFunction = profileFunction; } @Override public AutosensResult detectSensitivity(IobCobCalculatorPlugin iobCobCalculatorPlugin, long fromTime, long toTime) { LongSparseArray autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable(); - String age = SP.getString(R.string.key_age, ""); + String age = sp.getString(R.string.key_age, ""); int defaultHours = 24; - if (age.equals(MainApp.gs(R.string.key_adult))) defaultHours = 24; - if (age.equals(MainApp.gs(R.string.key_teenage))) defaultHours = 4; - if (age.equals(MainApp.gs(R.string.key_child))) defaultHours = 4; - int hoursForDetection = SP.getInt(R.string.key_openapsama_autosens_period, defaultHours); + if (age.equals(resourceHelper.gs(R.string.key_adult))) defaultHours = 24; + if (age.equals(resourceHelper.gs(R.string.key_teenage))) defaultHours = 4; + if (age.equals(resourceHelper.gs(R.string.key_child))) defaultHours = 4; + int hoursForDetection = sp.getInt(R.string.key_openapsama_autosens_period, defaultHours); if (autosensDataTable == null || autosensDataTable.size() < 4) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("No autosens data available. lastDataTime=" + iobCobCalculatorPlugin.lastDataTime()); + getAapsLogger().debug(LTag.AUTOSENS, "No autosens data available. lastDataTime=" + iobCobCalculatorPlugin.lastDataTime()); return new AutosensResult(); } AutosensData current = iobCobCalculatorPlugin.getAutosensData(toTime); // this is running inside lock already if (current == null) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("No autosens data available. toTime: " + DateUtil.dateAndTimeString(toTime) + " lastDataTime: " + iobCobCalculatorPlugin.lastDataTime()); + getAapsLogger().debug(LTag.AUTOSENS, "No autosens data available. toTime: " + DateUtil.dateAndTimeString(toTime) + " lastDataTime: " + iobCobCalculatorPlugin.lastDataTime()); return new AutosensResult(); } - Profile profile = ProfileFunctions.getInstance().getProfile(); + Profile profile = profileFunction.getProfile(); if (profile == null) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("No profile available"); + getAapsLogger().debug(LTag.AUTOSENS, "No profile available"); return new AutosensResult(); } @@ -143,12 +143,10 @@ public class SensitivityWeightedAveragePlugin extends AbstractSensitivityPlugin } if (data.size() == 0) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Data size: " + data.size() + " fromTime: " + DateUtil.dateAndTimeString(fromTime) + " toTime: " + DateUtil.dateAndTimeString(toTime)); + getAapsLogger().debug(LTag.AUTOSENS, "Data size: " + data.size() + " fromTime: " + DateUtil.dateAndTimeString(fromTime) + " toTime: " + DateUtil.dateAndTimeString(toTime)); return new AutosensResult(); } else { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Data size: " + data.size() + " fromTime: " + DateUtil.dateAndTimeString(fromTime) + " toTime: " + DateUtil.dateAndTimeString(toTime)); + getAapsLogger().debug(LTag.AUTOSENS, "Data size: " + data.size() + " fromTime: " + DateUtil.dateAndTimeString(fromTime) + " toTime: " + DateUtil.dateAndTimeString(toTime)); } double weightedsum = 0; @@ -172,8 +170,7 @@ public class SensitivityWeightedAveragePlugin extends AbstractSensitivityPlugin String ratioLimit = ""; String sensResult; - if (L.isEnabled(L.AUTOSENS)) - log.debug("Records: " + index + " " + pastSensitivity); + getAapsLogger().debug(LTag.AUTOSENS, "Records: " + index + " " + pastSensitivity); double average = weightedsum / weights; double basalOff = average * (60 / 5.0) / sens; @@ -187,15 +184,15 @@ public class SensitivityWeightedAveragePlugin extends AbstractSensitivityPlugin sensResult = "Sensitivity normal"; } - if (L.isEnabled(L.AUTOSENS)) - log.debug(sensResult); + getAapsLogger().debug(LTag.AUTOSENS, sensResult); AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit, sensResult, data.size()); - if (L.isEnabled(L.AUTOSENS)) - log.debug("Sensitivity to: {} weightedaverage: {} ratio: {} mealCOB: {}", new Date(toTime).toLocaleString(), - average, output.ratio, current.cob); + getAapsLogger().debug(LTag.AUTOSENS, "Sensitivity to: " + + DateUtil.dateAndTimeString(toTime) + + " ratio: " + output.ratio + + " mealCOB: " + current.cob); return output; } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPluginTest.java deleted file mode 100644 index 4a7fba4287..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPluginTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package info.nightscout.androidaps.plugins.sensitivity; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.interfaces.PluginDescription; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.utils.SP; - -import static org.junit.Assert.assertEquals; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, SP.class}) -public class AbstractSensitivityPluginTest { - - private class SensitivityTestClass extends AbstractSensitivityPlugin { - - public SensitivityTestClass() { - super(null); - } - - public SensitivityTestClass(PluginDescription pluginDescription) { - super(pluginDescription); - } - - @Override - public AutosensResult detectSensitivity(IobCobCalculatorPlugin plugin, long fromTime, long toTime) { - return null; - } - } - - @Test - public void fillResultTest() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockApplicationContext(); - AAPSMocker.mockSP(); - - SensitivityTestClass sut = new SensitivityTestClass(); - - AutosensResult ar = sut.fillResult(1d, 1d, "1", - "1.2", "1", 12, 0.7d, 1.2d); - assertEquals(1, ar.ratio, 0.01); - - ar = sut.fillResult(1.2d, 1d, "1", - "1.2", "1", 40, 0.7d, 1.2d); - assertEquals(1.16, ar.ratio, 0.01); - - ar = sut.fillResult(1.2d, 1d, "1", - "1.2", "1", 50, 0.7d, 1.2d); - assertEquals(1.2, ar.ratio, 0.01); - - ar = sut.fillResult(1.2d, 1d, "1", - "1.2", "1", 50, 0.7d, 1.1d); - assertEquals(1.1, ar.ratio, 0.01); - } - - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPluginTest.kt new file mode 100644 index 0000000000..9bc37ae383 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/sensitivity/AbstractSensitivityPluginTest.kt @@ -0,0 +1,47 @@ +package info.nightscout.androidaps.plugins.sensitivity + +import info.TestBase +import info.nightscout.androidaps.interfaces.PluginDescription +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin +import info.nightscout.androidaps.utils.resources.ResourceHelper +import info.nightscout.androidaps.utils.sharedPreferences.SP +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +class AbstractSensitivityPluginTest : TestBase() { + + @Mock lateinit var pluginDescription: PluginDescription + @Mock lateinit var aapsLogger: AAPSLogger + @Mock lateinit var resourceHelper: ResourceHelper + @Mock lateinit var sp: SP + + private inner class SensitivityTestClass(pluginDescription: PluginDescription, aapsLogger: AAPSLogger, resourceHelper: ResourceHelper, sp: SP) : AbstractSensitivityPlugin(pluginDescription, aapsLogger, resourceHelper, sp) { + + override fun detectSensitivity(plugin: IobCobCalculatorPlugin, fromTime: Long, toTime: Long): AutosensResult { + return AutosensResult() + } + } + + @Test + fun fillResultTest() { + val sut = SensitivityTestClass(pluginDescription, aapsLogger, resourceHelper, sp) + var ar = sut.fillResult(1.0, 1.0, "1", + "1.2", "1", 12, 0.7, 1.2) + Assert.assertEquals(1.0, ar.ratio, 0.01) + ar = sut.fillResult(1.2, 1.0, "1", + "1.2", "1", 40, 0.7, 1.2) + Assert.assertEquals(1.16, ar.ratio, 0.01) + ar = sut.fillResult(1.2, 1.0, "1", + "1.2", "1", 50, 0.7, 1.2) + Assert.assertEquals(1.2, ar.ratio, 0.01) + ar = sut.fillResult(1.2, 1.0, "1", + "1.2", "1", 50, 0.7, 1.1) + Assert.assertEquals(1.1, ar.ratio, 0.01) + } +} \ No newline at end of file