Merge pull request #1795 from Philoul/Autotune/SmallBasalAccuracy

Improve IOB accuracy for small basal/tbr rates
This commit is contained in:
Milos Kozak 2022-05-31 23:24:59 +02:00 committed by GitHub
commit dceca8d194
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.math.ceil
import kotlin.math.roundToInt import kotlin.math.roundToInt
@Singleton @Singleton
@ -181,7 +182,7 @@ class AutotuneIob @Inject constructor(
// even if profile rate is not the same // even if profile rate is not the same
private fun toSplittedTimestampTB(tb: TemporaryBasal, tunedProfile: ATProfile) { private fun toSplittedTimestampTB(tb: TemporaryBasal, tunedProfile: ATProfile) {
var splittedTimestamp = tb.timestamp var splittedTimestamp = tb.timestamp
val cutInMilliSec = T.mins(30).msecs() //30 min to compare with oref0 val cutInMilliSec = T.mins(60).msecs() //30 min to compare with oref0, 60 min to improve accuracy
var splittedDuration = tb.duration var splittedDuration = tb.duration
if (tb.isValid && tb.durationInMinutes > 0) { if (tb.isValid && tb.durationInMinutes > 0) {
val endTimestamp = splittedTimestamp + splittedDuration val endTimestamp = splittedTimestamp + splittedDuration
@ -248,21 +249,20 @@ class AutotuneIob @Inject constructor(
fun convertToBoluses(eb: ExtendedBolus): MutableList<Bolus> { fun convertToBoluses(eb: ExtendedBolus): MutableList<Bolus> {
val result: MutableList<Bolus> = ArrayList() val result: MutableList<Bolus> = ArrayList()
val tempBolusSize = 0.05 val aboutFiveMinIntervals = ceil(eb.duration / 5.0).toInt()
val tempBolusCount : Int = (eb.amount / tempBolusSize).roundToInt() val spacing = eb.duration / aboutFiveMinIntervals.toDouble()
if(tempBolusCount > 0) { for (j in 0L until aboutFiveMinIntervals) {
val tempBolusSpacing = eb.duration / tempBolusCount // find middle of the interval
for (j in 0L until tempBolusCount) { val calcDate = (eb.timestamp + j * spacing * 60 * 1000 + 0.5 * spacing * 60 * 1000).toLong()
val calcDate = eb.timestamp + j * tempBolusSpacing val tempBolusSize: Double = eb.amount / aboutFiveMinIntervals
val bolusInterfaceIDs = InterfaceIDs().also { it.nightscoutId = eb.interfaceIDs.nightscoutId + "_eb_$j" } val bolusInterfaceIDs = InterfaceIDs().also { it.nightscoutId = eb.interfaceIDs.nightscoutId + "_eb_$j" }
val tempBolusPart = Bolus( val tempBolusPart = Bolus(
interfaceIDs_backing = bolusInterfaceIDs, interfaceIDs_backing = bolusInterfaceIDs,
timestamp = calcDate, timestamp = calcDate,
amount = tempBolusSize, amount = tempBolusSize,
type = Bolus.Type.NORMAL type = Bolus.Type.NORMAL
) )
result.add(tempBolusPart) result.add(tempBolusPart)
}
} }
return result return result
} }
@ -277,22 +277,20 @@ class AutotuneIob @Inject constructor(
} else { } else {
tbr.rate / 100.0 * basalRate - tunedRate tbr.rate / 100.0 * basalRate - tunedRate
}, 0.001) }, 0.001)
val tempBolusSize = if (netBasalRate < 0 ) -0.05 else 0.05 val aboutFiveMinIntervals = ceil(realDuration / 5.0).toInt()
val netBasalAmount: Double = Round.roundTo(netBasalRate * realDuration / 60.0, 0.01) val tempBolusSpacing = realDuration / aboutFiveMinIntervals.toDouble()
val tempBolusCount : Int = (netBasalAmount / tempBolusSize).roundToInt() for (j in 0L until aboutFiveMinIntervals) {
if(tempBolusCount > 0) { // find middle of the interval
val tempBolusSpacing = realDuration * 60 * 1000 / tempBolusCount val calcDate = (tbr.timestamp + j * tempBolusSpacing * 60 * 1000 + 0.5 * tempBolusSpacing * 60 * 1000).toLong()
for (j in 0L until tempBolusCount) { val tempBolusSize = netBasalRate * tempBolusSpacing / 60.0
val calcDate = tbr.timestamp + j * tempBolusSpacing val bolusInterfaceIDs = InterfaceIDs().also { it.nightscoutId = tbr.interfaceIDs.nightscoutId + "_tbr_$j" }
val bolusInterfaceIDs = InterfaceIDs().also { it.nightscoutId = tbr.interfaceIDs.nightscoutId + "_tbr_$j" } val tempBolusPart = Bolus(
val tempBolusPart = Bolus( interfaceIDs_backing = bolusInterfaceIDs,
interfaceIDs_backing = bolusInterfaceIDs, timestamp = calcDate,
timestamp = calcDate, amount = tempBolusSize,
amount = tempBolusSize, type = Bolus.Type.NORMAL
type = Bolus.Type.NORMAL )
) result.add(tempBolusPart)
result.add(tempBolusPart)
}
} }
return result return result
} }