Skip to content

Commit

Permalink
DynISF cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MilosKozak committed Jun 14, 2022
1 parent 0b3223d commit 2d6de19
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 177 deletions.
40 changes: 7 additions & 33 deletions app/src/main/assets/OpenAPSSMBDynamicISF/determine-basal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 oldThreshold = threshold;
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) + "; ");

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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ class TddCalculator @Inject constructor(
return result
}

fun calculateDaily(): TotalDailyDose {
val startTime = MidnightTime.calc(dateUtil.now())
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)
//val result = TotalDailyDose()
repository.getBolusesDataFromTimeToTime(startTime, endTime, true).blockingGet()
.filter { it.type != Bolus.Type.PRIMING }
.forEach { t ->
Expand Down Expand Up @@ -131,133 +130,6 @@ class TddCalculator @Inject constructor(
return tdd
}

fun calculate24Daily(): TotalDailyDose {
val startTime = dateUtil.now() - T.hours(hour = 24).msecs()
val endTime = dateUtil.now()
val tdd = TotalDailyDose(timestamp = startTime)
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 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>): TotalDailyDose? {
val totalTdd = TotalDailyDose(timestamp = dateUtil.now())
if (tdds.size() == 0) return null
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@
<string name="smb_frequency_exceeded">A bolus was delivered within the last 3 minutes, skipping SMB</string>
<string name="basal_set_correctly">Basal set correctly</string>
<string name="key_treatmentssafety_maxbolus" translatable="false">treatmentssafety_maxbolus</string>
<string name="key_treatmentssafety_lgsThreshold" translatable="false">lgsThreshold</string>
<string name="key_lgs_threshold" translatable="false">lgsThreshold</string>
<string name="limitingextendedbolus">Limiting extended bolus to %1$.1f U because of %2$s</string>
<string name="limitingcarbs">Limiting carbs to %1$d g because of %2$s</string>
<string name="limitingiob">Limiting IOB to %1$.1f U because of %2$s</string>
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/xml/pref_openapssmbdynamicisf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
validate:testType="floatNumericRange" />

<info.nightscout.androidaps.utils.textValidator.ValidatingEditTextPreference
android:defaultValue="60"
android:defaultValue="65"
android:inputType="numberDecimal"
android:dialogMessage="@string/treatmentssafety_lgsThreshold_summary"
android:key="@string/key_treatmentssafety_lgsThreshold"
android:key="@string/key_lgs_threshold"
android:title="@string/treatmentssafety_lgsThreshold_title"
validate:floatmaxNumber="100"
validate:floatminNumber="3.3"
validate:testType="floatNumericRange" />
validate:floatmaxNumber="120"
validate:floatminNumber="65"
validate:testType="bgRange" />

<SwitchPreference
android:defaultValue="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ interface Profile {
if (isMmol(anyBg)) toUnitsString(anyBg * Constants.MMOLL_TO_MGDL, anyBg, profileFunction.getUnits())
else toUnitsString(anyBg, anyBg * Constants.MGDL_TO_MMOLL, profileFunction.getUnits())

fun toMgdl(value: Double): Double =
if (isMgdl(value)) value else value * Constants.MMOLL_TO_MGDL

fun toMgdl(value: Double, units: GlucoseUnit): Double =
if (units == GlucoseUnit.MGDL) value else value * Constants.MMOLL_TO_MGDL

Expand Down

0 comments on commit 2d6de19

Please sign in to comment.