Merge pull request #1148 from triplem/1133

1133
This commit is contained in:
Milos Kozak 2018-06-24 19:32:18 +02:00 committed by GitHub
commit 385f0c7bfe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 158 additions and 71 deletions

View file

@ -1,11 +1,28 @@
package info.nightscout.androidaps.interfaces;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult;
import info.nightscout.utils.Round;
import info.nightscout.utils.SP;
import info.nightscout.utils.SafeParse;
/**
* Created by mike on 24.06.2017.
*/
public interface SensitivityInterface {
double MIN_HOURS = 1;
double MIN_HOURS_FULL_AUTOSENS = 4;
AutosensResult detectSensitivity(long fromTime, long toTime);
}

View file

@ -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;
}
}

View file

@ -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;
@ -154,26 +154,16 @@ public class SensitivityAAPSPlugin extends PluginBase implements SensitivityInte
if (Config.logAutosensData)
log.debug(sensResult);
double rawRatio = ratio;
ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")));
ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2")));
if (ratio != rawRatio) {
ratioLimit = "Ratio limited from " + rawRatio + " to " + ratio;
log.debug(ratioLimit);
}
AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit,
sensResult, deviationsArray.size());
if (Config.logAutosensData) {
log.debug("Sensitivity to: " + new Date(toTime).toLocaleString() + " percentile: " + percentile + " ratio: " + ratio + " mealCOB: " + current.cob);
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));
}
AutosensResult output = new AutosensResult();
output.ratio = Round.roundTo(ratio, 0.01);
output.carbsAbsorbed = Round.roundTo(current.cob, 0.01);
output.pastSensitivity = pastSensitivity;
output.ratioLimit = ratioLimit;
output.sensResult = sensResult;
return output;
}
}

View file

@ -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;
@ -55,12 +55,7 @@ public class SensitivityOref0Plugin extends PluginBase implements SensitivityInt
public AutosensResult detectSensitivity(long fromTime, long toTime) {
LongSparseArray<AutosensData> autosensDataTable = IobCobCalculatorPlugin.getPlugin().getAutosensDataTable();
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 = 24;
if (age.equals(MainApp.gs(R.string.key_child))) defaultHours = 24;
int hoursForDetection = SP.getInt(R.string.key_openapsama_autosens_period, defaultHours);
int hoursForDetection = 24;
long now = System.currentTimeMillis();
Profile profile = MainApp.getConfigBuilder().getProfile();
@ -166,24 +161,13 @@ public class SensitivityOref0Plugin extends PluginBase implements SensitivityInt
ratio = 1 + (basalOff / profile.getMaxDailyBasal());
double rawRatio = ratio;
ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")));
ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2")));
if (ratio != rawRatio) {
ratioLimit = "Ratio limited from " + rawRatio + " to " + ratio;
log.debug(ratioLimit);
}
AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit,
sensResult, deviationsArray.size());
if (Config.logAutosensData)
log.debug("Sensitivity to: " + new Date(toTime).toLocaleString() + " ratio: " + ratio + " mealCOB: " + current.cob);
log.debug("Sensitivity to: {} ratio: {} mealCOB: {}",
new Date(toTime).toLocaleString(), output.ratio, current.cob);
AutosensResult output = new AutosensResult();
output.ratio = Round.roundTo(ratio, 0.01);
output.carbsAbsorbed = Round.roundTo(current.cob, 0.01);
output.pastSensitivity = pastSensitivity;
output.ratioLimit = ratioLimit;
output.sensResult = sensResult;
return output;
}
}

View file

@ -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;
@ -175,24 +175,13 @@ public class SensitivityOref1Plugin extends PluginBase implements SensitivityInt
ratio = 1 + (basalOff / profile.getMaxDailyBasal());
double rawRatio = ratio;
ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")));
ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2")));
if (ratio != rawRatio) {
ratioLimit = "Ratio limited from " + rawRatio + " to " + ratio;
log.debug(ratioLimit);
}
AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit,
sensResult, deviationsArray.size());
if (Config.logAutosensData)
log.debug("Sensitivity to: " + new Date(toTime).toLocaleString() + " ratio: " + ratio + " mealCOB: " + current.cob);
log.debug("Sensitivity to: {} ratio: {} mealCOB: {}",
new Date(toTime).toLocaleString(), output.ratio, current.cob);
AutosensResult output = new AutosensResult();
output.ratio = Round.roundTo(ratio, 0.01);
output.carbsAbsorbed = Round.roundTo(current.cob, 0.01);
output.pastSensitivity = pastSensitivity;
output.ratioLimit = ratioLimit;
output.sensResult = sensResult;
return output;
}
}

View file

@ -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;
@ -176,25 +176,13 @@ public class SensitivityWeightedAveragePlugin extends PluginBase implements Sens
if (Config.logAutosensData)
log.debug(sensResult);
double rawRatio = ratio;
ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")));
ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2")));
if (ratio != rawRatio) {
ratioLimit = "Ratio limited from " + rawRatio + " to " + ratio;
if (Config.logAutosensData)
log.debug(ratioLimit);
}
AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit,
sensResult, data.size());
if (Config.logAutosensData)
log.debug("Sensitivity to: " + new Date(toTime).toLocaleString() + " weightedaverage: " + average + " ratio: " + ratio + " mealCOB: " + current.cob);
log.debug("Sensitivity to: {} weightedaverage: {} ratio: {} mealCOB: {}", new Date(toTime).toLocaleString(),
average, output.ratio, current.cob);
AutosensResult output = new AutosensResult();
output.ratio = Round.roundTo(ratio, 0.01);
output.carbsAbsorbed = Round.roundTo(current.cob, 0.01);
output.pastSensitivity = pastSensitivity;
output.ratioLimit = ratioLimit;
output.sensResult = sensResult;
return output;
}
}

View file

@ -0,0 +1,51 @@
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 AbstractSensitivityPluginTest {
private class SensitivityTestClass extends AbstractSensitivityPlugin {
public SensitivityTestClass() {
super(null);
}
public SensitivityTestClass(PluginDescription pluginDescription) {
super(pluginDescription);
}
@Override
public AutosensResult detectSensitivity(long fromTime, long toTime) {
return null;
}
}
@Test
public void fillResultTest() {
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);
}
}