Add unit test to PR
This commit is contained in:
parent
d72cb96d2b
commit
faca79d13e
6 changed files with 113 additions and 100 deletions
|
@ -1,14 +1,69 @@
|
||||||
package info.nightscout.androidaps.interfaces;
|
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.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.
|
* Created by mike on 24.06.2017.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface SensitivityInterface {
|
public interface SensitivityInterface {
|
||||||
|
|
||||||
|
Logger LOG = LoggerFactory.getLogger(SensitivityInterface.class);
|
||||||
|
|
||||||
double MIN_HOURS = 1;
|
double MIN_HOURS = 1;
|
||||||
double MIN_HOURS_FULL_AUTOSENS = 4;
|
double MIN_HOURS_FULL_AUTOSENS = 4;
|
||||||
|
|
||||||
AutosensResult detectSensitivity(long fromTime, long toTime);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,36 +154,16 @@ public class SensitivityAAPSPlugin extends PluginBase implements SensitivityInte
|
||||||
if (Config.logAutosensData)
|
if (Config.logAutosensData)
|
||||||
log.debug(sensResult);
|
log.debug(sensResult);
|
||||||
|
|
||||||
double rawRatio = ratio;
|
AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit,
|
||||||
ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")));
|
sensResult, deviationsArray.size());
|
||||||
ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2")));
|
|
||||||
|
|
||||||
//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, deviationsArray.size() / 12d), MIN_HOURS_FULL_AUTOSENS) - MIN_HOURS) / (MIN_HOURS_FULL_AUTOSENS - MIN_HOURS);
|
|
||||||
ratio = autosensContrib * (ratio - 1) + 1;
|
|
||||||
|
|
||||||
if (autosensContrib != 1d) {
|
|
||||||
ratioLimit += "(" + deviationsArray.size() + " of " + MIN_HOURS_FULL_AUTOSENS * 12 + " values) ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ratio != rawRatio) {
|
|
||||||
ratioLimit += "Ratio limited from " + rawRatio + " to " + ratio;
|
|
||||||
log.debug(ratioLimit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Config.logAutosensData) {
|
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));
|
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;
|
return output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -161,34 +161,13 @@ public class SensitivityOref0Plugin extends PluginBase implements SensitivityInt
|
||||||
|
|
||||||
ratio = 1 + (basalOff / profile.getMaxDailyBasal());
|
ratio = 1 + (basalOff / profile.getMaxDailyBasal());
|
||||||
|
|
||||||
double rawRatio = ratio;
|
AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit,
|
||||||
ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")));
|
sensResult, deviationsArray.size());
|
||||||
ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2")));
|
|
||||||
|
|
||||||
//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, deviationsArray.size() / 12d), MIN_HOURS_FULL_AUTOSENS) - MIN_HOURS) / (MIN_HOURS_FULL_AUTOSENS - MIN_HOURS);
|
|
||||||
ratio = autosensContrib * (ratio - 1) + 1;
|
|
||||||
|
|
||||||
if (autosensContrib != 1d) {
|
|
||||||
ratioLimit += "(" + deviationsArray.size() + " of " + MIN_HOURS_FULL_AUTOSENS * 12 + " values) ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ratio != rawRatio) {
|
|
||||||
ratioLimit += "Ratio limited from " + rawRatio + " to " + ratio;
|
|
||||||
log.debug(ratioLimit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Config.logAutosensData)
|
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;
|
return output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -175,34 +175,13 @@ public class SensitivityOref1Plugin extends PluginBase implements SensitivityInt
|
||||||
|
|
||||||
ratio = 1 + (basalOff / profile.getMaxDailyBasal());
|
ratio = 1 + (basalOff / profile.getMaxDailyBasal());
|
||||||
|
|
||||||
double rawRatio = ratio;
|
AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit,
|
||||||
ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")));
|
sensResult, deviationsArray.size());
|
||||||
ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2")));
|
|
||||||
|
|
||||||
//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, deviationsArray.size() / 12d), MIN_HOURS_FULL_AUTOSENS) - MIN_HOURS) / (MIN_HOURS_FULL_AUTOSENS - MIN_HOURS);
|
|
||||||
ratio = autosensContrib * (ratio - 1) + 1;
|
|
||||||
|
|
||||||
if (autosensContrib != 1d) {
|
|
||||||
ratioLimit += "(" + deviationsArray.size() + " of " + MIN_HOURS_FULL_AUTOSENS * 12 + " values) ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ratio != rawRatio) {
|
|
||||||
ratioLimit += "Ratio limited from " + rawRatio + " to " + ratio;
|
|
||||||
log.debug(ratioLimit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Config.logAutosensData)
|
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;
|
return output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -176,34 +176,13 @@ public class SensitivityWeightedAveragePlugin extends PluginBase implements Sens
|
||||||
if (Config.logAutosensData)
|
if (Config.logAutosensData)
|
||||||
log.debug(sensResult);
|
log.debug(sensResult);
|
||||||
|
|
||||||
double rawRatio = ratio;
|
AutosensResult output = fillResult(ratio, current.cob, pastSensitivity, ratioLimit,
|
||||||
ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_min, "0.7")));
|
sensResult, data.size());
|
||||||
ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString(R.string.key_openapsama_autosens_max, "1.2")));
|
|
||||||
|
|
||||||
//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, data.size() / 12d), MIN_HOURS_FULL_AUTOSENS) - MIN_HOURS) / (MIN_HOURS_FULL_AUTOSENS - MIN_HOURS);
|
|
||||||
ratio = autosensContrib * (ratio - 1) + 1;
|
|
||||||
|
|
||||||
if (autosensContrib != 1d) {
|
|
||||||
ratioLimit += "(" + data.size() + " of " + MIN_HOURS_FULL_AUTOSENS * 12 + " values) ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ratio != rawRatio) {
|
|
||||||
ratioLimit += "Ratio limited from " + rawRatio + " to " + ratio;
|
|
||||||
log.debug(ratioLimit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Config.logAutosensData)
|
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;
|
return output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package info.nightscout.androidaps.plugins.Sensitivity;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.interfaces.SensitivityInterface;
|
||||||
|
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class SensitivityInterfaceTest {
|
||||||
|
|
||||||
|
private class SensitivityTestClass implements SensitivityInterface {
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue