From 2d6de1970b637222ce1951864748f520cf7cee72 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 14 Jun 2022 11:56:43 +0200 Subject: [PATCH] DynISF cleanup --- .../OpenAPSSMBDynamicISF/determine-basal.js | 40 +----- .../DetermineBasalAdapterSMBDynamicISFJS.kt | 14 +- .../androidaps/utils/stats/TddCalculator.kt | 134 +----------------- app/src/main/res/values/strings.xml | 2 +- .../res/xml/pref_openapssmbdynamicisf.xml | 10 +- .../androidaps/interfaces/Profile.kt | 3 + 6 files changed, 26 insertions(+), 177 deletions(-) diff --git a/app/src/main/assets/OpenAPSSMBDynamicISF/determine-basal.js b/app/src/main/assets/OpenAPSSMBDynamicISF/determine-basal.js index 8a9e08b15d..321f3a4871 100644 --- a/app/src/main/assets/OpenAPSSMBDynamicISF/determine-basal.js +++ b/app/src/main/assets/OpenAPSSMBDynamicISF/determine-basal.js @@ -511,40 +511,14 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_ } // min_bg of 90 -> threshold of 65, 100 -> 70 110 -> 75, and 130 -> 85, or if specified by user, take that value - var lgsThreshold = profile.lgsThreshold; - console.error("Profile LGS Threshold set to"+lgsThreshold+"; "); - //if(lgsThreshold < 5.6) - if(lgsThreshold > 3.2 && lgsThreshold < 5.6){ - lgsThreshold = (18 * lgsThreshold); - lgsThreshold = round(lgsThreshold, 2); - console.error("LGS Threshold converted to"+lgsThreshold+"; "); - }else{ - lgsThreshold; - } - var threshold = min_bg - 0.5*(min_bg-40); - var oldThreshold = threshold; - if(lgsThreshold < 65 || lgsThreshold > 120) { - threshold; - } - else if( lgsThreshold < threshold){ - threshold; - } - else { - threshold = lgsThreshold; - } - console.error("Threshold set from " + convert_bg(oldThreshold, profile) + " to " + convert_bg(threshold, profile) + "; "); + var lgsThreshold = profile.lgsThreshold; + var threshold = min_bg - 0.5*(min_bg-40); + var oldThreshold = threshold; + if (lgsThreshold >= 65 && lgsThreshold <= 120 && lgsThreshold > threshold) { + threshold = lgsThreshold; + } + console.error("Threshold set from " + convert_bg(oldThreshold, profile) + " to " + convert_bg(threshold, profile) + "; "); - /*var oldThreshold = threshold; - - if(lgsThreshold < 65 || lgsThreshold > 120) { - lgsThreshold = threshold; - } - else{ - threshold = Math.max(threshold, lgsThreshold); - } - if( threshold === lgsThreshold ) { - console.log("Threshold set from " + convert_bg(oldThreshold, profile) + " to " + convert_bg(threshold, profile) + "; ") - }*/ //console.error(reservoir_data); rT = { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMBDynamicISF/DetermineBasalAdapterSMBDynamicISFJS.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMBDynamicISF/DetermineBasalAdapterSMBDynamicISFJS.kt index f2b8916721..d5a33606eb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMBDynamicISF/DetermineBasalAdapterSMBDynamicISFJS.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMBDynamicISF/DetermineBasalAdapterSMBDynamicISFJS.kt @@ -192,11 +192,11 @@ class DetermineBasalAdapterSMBDynamicISFJS internal constructor(private val scri this.profile.put("sens", profile.getIsfMgdl()) this.profile.put("max_daily_safety_multiplier", sp.getInt(R.string.key_openapsama_max_daily_safety_multiplier, 3)) this.profile.put("current_basal_safety_multiplier", sp.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4.0)) - this.profile.put("lgsThreshold", sp.getDouble(R.string.key_treatmentssafety_lgsThreshold, 60.0)) + this.profile.put("lgsThreshold", Profile.toMgdl(sp.getDouble(R.string.key_lgs_threshold, 65.0))) val insulin = activePlugin.activeInsulin - val insulinType = insulin.friendlyName - val insulinPeak = insulin.peak + val insulinType = insulin.friendlyName + val insulinPeak = insulin.peak //mProfile.put("high_temptarget_raises_sensitivity", SP.getBoolean(R.string.key_high_temptarget_raises_sensitivity, SMBDefaults.high_temptarget_raises_sensitivity)); this.profile.put("high_temptarget_raises_sensitivity", sp.getBoolean(R.string.key_high_temptarget_raises_sensitivity, SMBDefaults.high_temptarget_raises_sensitivity)) @@ -227,7 +227,7 @@ class DetermineBasalAdapterSMBDynamicISFJS internal constructor(private val scri this.profile.put("enableSMB_after_carbs", smbEnabled && sp.getBoolean(R.string.key_enableSMB_after_carbs, false) && advancedFiltering) this.profile.put("maxSMBBasalMinutes", sp.getInt(R.string.key_smbmaxminutes, SMBDefaults.maxSMBBasalMinutes)) this.profile.put("maxUAMSMBBasalMinutes", sp.getInt(R.string.key_uamsmbmaxminutes, SMBDefaults.maxUAMSMBBasalMinutes)) - this.profile.put("DynISFAdjust", SafeParse.stringToDouble(sp.getString(R.string.key_DynISFAdjust,"100"))) + this.profile.put("DynISFAdjust", SafeParse.stringToDouble(sp.getString(R.string.key_DynISFAdjust, "100"))) this.profile.put("insulinType", insulinType) this.profile.put("insulinPeak", insulinPeak) this.profile.put("maxUAMSMBBasalMinutes", sp.getInt(R.string.key_uamsmbmaxminutes, SMBDefaults.maxUAMSMBBasalMinutes)) @@ -272,9 +272,9 @@ class DetermineBasalAdapterSMBDynamicISFJS internal constructor(private val scri this.mealData.put("TDDAIMI1", tddCalculator.averageTDD(tddCalculator.calculate(1))?.totalAmount) this.mealData.put("TDDAIMI7", tddCalculator.averageTDD(tddCalculator.calculate(7))?.totalAmount) - this.mealData.put("TDDLast4", tddCalculator.calculate4Daily().totalAmount) - this.mealData.put("TDD4to8", tddCalculator.calculate8Gap().totalAmount) - this.mealData.put("TDD24", tddCalculator.calculate24Daily().totalAmount) + this.mealData.put("TDDLast4", tddCalculator.calculateDaily(-4, 0).totalAmount) + this.mealData.put("TDD4to8", tddCalculator.calculateDaily(-8, -4).totalAmount) + this.mealData.put("TDD24", tddCalculator.calculateDaily(-24, 0).totalAmount) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt index 87c702911a..e07e492e9a 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt @@ -99,41 +99,9 @@ class TddCalculator @Inject constructor( return result } - fun calculateDaily(): TotalDailyDose { - val startTime = MidnightTime.calc(dateUtil.now()) - val endTime = dateUtil.now() - val tdd = TotalDailyDose(timestamp = startTime) - //val result = TotalDailyDose() - repository.getBolusesDataFromTimeToTime(startTime, endTime, true).blockingGet() - .filter { it.type != Bolus.Type.PRIMING } - .forEach { t -> - tdd.bolusAmount += t.amount - } - repository.getCarbsDataFromTimeToTimeExpanded(startTime, endTime, true).blockingGet().forEach { t -> - tdd.carbs += t.amount - } - val calculationStep = T.mins(5).msecs() - for (t in startTime until endTime step calculationStep) { - val tbr = iobCobCalculator.getTempBasalIncludingConvertedExtended(t) - val profile = profileFunction.getProfile(t) ?: continue - val absoluteRate = tbr?.convertedToAbsolute(t, profile) ?: profile.getBasal(t) - tdd.basalAmount += absoluteRate / 60.0 * 5.0 - - if (!activePlugin.activePump.isFakingTempsByExtendedBoluses) { - // they are not included in TBRs - val eb = iobCobCalculator.getExtendedBolus(t) - val absoluteEbRate = eb?.rate ?: 0.0 - tdd.bolusAmount += absoluteEbRate / 60.0 * 5.0 - } - } - tdd.totalAmount = tdd.bolusAmount + tdd.basalAmount - aapsLogger.debug(LTag.CORE, tdd.toString()) - return tdd - } - - fun calculate24Daily(): TotalDailyDose { - val startTime = dateUtil.now() - T.hours(hour = 24).msecs() - val endTime = dateUtil.now() + fun calculateDaily(startHours: Long, endHours: Long): TotalDailyDose { + val startTime = dateUtil.now() + T.hours(hour = startHours).msecs() + val endTime = dateUtil.now() + T.hours(hour = endHours).msecs() val tdd = TotalDailyDose(timestamp = startTime) repository.getBolusesDataFromTimeToTime(startTime, endTime, true).blockingGet() .filter { it.type != Bolus.Type.PRIMING } @@ -161,102 +129,6 @@ class TddCalculator @Inject constructor( aapsLogger.debug(LTag.CORE, tdd.toString()) return tdd } - fun calculate4Daily():TotalDailyDose { - val startTime = dateUtil.now() - T.hours(hour = 4).msecs() - val endTime = dateUtil.now() - val tdd = TotalDailyDose(timestamp = startTime) - //val result = TotalDailyDose() - repository.getBolusesDataFromTimeToTime(startTime, endTime, true).blockingGet() - .filter { it.type != Bolus.Type.PRIMING } - .forEach { t -> - //val midnight = MidnightTime.calc(t.timestamp) - //val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight) - tdd.bolusAmount += t.amount - //result.put(midnight, tdd) - } - repository.getCarbsDataFromTimeToTimeExpanded(startTime, endTime, true).blockingGet().forEach { t -> - //val midnight = MidnightTime.calc(t.timestamp) - //val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight) - tdd.carbs += t.amount - //result.put(midnight, tdd) - } - val calculationStep = T.mins(5).msecs() - val tempBasals = iobCobCalculator.getTempBasalIncludingConvertedExtendedForRange(startTime, endTime, calculationStep) - for (t in startTime until endTime step calculationStep) { - - //val midnight = MidnightTime.calc(t) - //val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight) - val tbr = iobCobCalculator.getTempBasalIncludingConvertedExtended(t) - val profile = profileFunction.getProfile(t) ?: continue - val absoluteRate = tbr?.convertedToAbsolute(t, profile) ?: profile.getBasal(t) - tdd.basalAmount += absoluteRate / 60.0 * 5.0 - - if (!activePlugin.activePump.isFakingTempsByExtendedBoluses) { - // they are not included in TBRs - val eb = iobCobCalculator.getExtendedBolus(t) - val absoluteEbRate = eb?.rate ?: 0.0 - tdd.bolusAmount += absoluteEbRate / 60.0 * 5.0 - } - //result.put(midnight, tdd) - } - //for (i in 0 until tdd.size()) { - //val tdd = result.valueAt(i) - tdd.totalAmount = tdd.bolusAmount + tdd.basalAmount - //} - - - aapsLogger.debug(LTag.CORE, tdd.toString()) - return tdd - } - - - fun calculate8Gap():TotalDailyDose { - val startTime = dateUtil.now() - T.hours(hour = 8).msecs() - val endTime = dateUtil.now() - T.hours(hour = 4).msecs() - val tdd = TotalDailyDose(timestamp = startTime) - //val result = TotalDailyDose() - repository.getBolusesDataFromTimeToTime(startTime, endTime, true).blockingGet() - .filter { it.type != Bolus.Type.PRIMING } - .forEach { t -> - //val midnight = MidnightTime.calc(t.timestamp) - //val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight) - tdd.bolusAmount += t.amount - //result.put(midnight, tdd) - } - repository.getCarbsDataFromTimeToTimeExpanded(startTime, endTime, true).blockingGet().forEach { t -> - //val midnight = MidnightTime.calc(t.timestamp) - //val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight) - tdd.carbs += t.amount - //result.put(midnight, tdd) - } - val calculationStep = T.mins(5).msecs() - val tempBasals = iobCobCalculator.getTempBasalIncludingConvertedExtendedForRange(startTime, endTime, calculationStep) - for (t in startTime until endTime step calculationStep) { - - //val midnight = MidnightTime.calc(t) - //val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight) - val tbr = iobCobCalculator.getTempBasalIncludingConvertedExtended(t) - val profile = profileFunction.getProfile(t) ?: continue - val absoluteRate = tbr?.convertedToAbsolute(t, profile) ?: profile.getBasal(t) - tdd.basalAmount += absoluteRate / 60.0 * 5.0 - - if (!activePlugin.activePump.isFakingTempsByExtendedBoluses) { - // they are not included in TBRs - val eb = iobCobCalculator.getExtendedBolus(t) - val absoluteEbRate = eb?.rate ?: 0.0 - tdd.bolusAmount += absoluteEbRate / 60.0 * 5.0 - } - //result.put(midnight, tdd) - } - //for (i in 0 until tdd.size()) { - //val tdd = result.valueAt(i) - tdd.totalAmount = tdd.bolusAmount + tdd.basalAmount - //} - - - aapsLogger.debug(LTag.CORE, tdd.toString()) - return tdd - } fun averageTDD(tdds: LongSparseArray): TotalDailyDose? { val totalTdd = TotalDailyDose(timestamp = dateUtil.now()) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 662224ae11..e34436ac85 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -705,7 +705,7 @@ A bolus was delivered within the last 3 minutes, skipping SMB Basal set correctly treatmentssafety_maxbolus - lgsThreshold + lgsThreshold Limiting extended bolus to %1$.1f U because of %2$s Limiting carbs to %1$d g because of %2$s Limiting IOB to %1$.1f U because of %2$s diff --git a/app/src/main/res/xml/pref_openapssmbdynamicisf.xml b/app/src/main/res/xml/pref_openapssmbdynamicisf.xml index 03feac98ed..c1267c75ef 100644 --- a/app/src/main/res/xml/pref_openapssmbdynamicisf.xml +++ b/app/src/main/res/xml/pref_openapssmbdynamicisf.xml @@ -40,14 +40,14 @@ validate:testType="floatNumericRange" /> + validate:floatmaxNumber="120" + validate:floatminNumber="65" + validate:testType="bgRange" />