SMOOTHING: reset trend arrow on smoothing

This commit is contained in:
Milos Kozak 2023-08-16 20:10:24 +02:00
parent 70a57ec475
commit fa7b73bc8b
4 changed files with 14 additions and 11 deletions

View file

@ -16,6 +16,7 @@ android {
dependencies {
implementation project(':database:entities')
implementation project(':app-wear-shared:shared')
implementation project(':core:interfaces')
implementation project(':core:ui')

View file

@ -2,6 +2,7 @@ package info.nightscout.smoothing
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.annotations.OpenForTesting
import info.nightscout.database.entities.GlucoseValue
import info.nightscout.interfaces.iob.InMemoryGlucoseValue
import info.nightscout.interfaces.plugin.PluginBase
import info.nightscout.interfaces.plugin.PluginDescription
@ -13,8 +14,7 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.T
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.math.max
import kotlin.math.round
import kotlin.math.abs
@OpenForTesting
@Singleton
@ -32,7 +32,6 @@ class AvgSmoothingPlugin @Inject constructor(
aapsLogger, rh, injector
), Smoothing {
@Suppress("LocalVariableName")
override fun smooth(data: MutableList<InMemoryGlucoseValue>): MutableList<InMemoryGlucoseValue> {
if (data.lastIndex < 4)
{
@ -44,20 +43,19 @@ class AvgSmoothingPlugin @Inject constructor(
// Check if value's are in a valid range
// Bucketed is always calculated to 5 min, we still check if our data is evenly spaced with an allowance of 30 seconds
if (isValid(data[i].value) && isValid(data[i - 1].value) && isValid(data[i + 1].value)
&& Math.abs(data[i].timestamp - data[i - 1].timestamp - (data[i + 1].timestamp - data[i].timestamp)) < T.secs(30).msecs())
{
&& abs(data[i].timestamp - data[i - 1].timestamp - (data[i + 1].timestamp - data[i].timestamp)) < T.secs(30).msecs()
) {
// We could further improve this by adding a weight to the neighbours, for simplicity this is not done.
data[i].smoothed = ((data[i - 1].value + data[i].value + data[i + 1].value) / 3.0)
}
else
{
data[i].trendArrow = GlucoseValue.TrendArrow.NONE
} else {
// data[i].smoothed = data[i].value
val currentTime = data[i].timestamp
val value = data[i].value
aapsLogger.debug(LTag.GLUCOSE, "Value: $value at $currentTime not smoothed")
}
}
// We leave the data we can not smooth as is, alternativly we could provide raw value's to the smoothed value's:
// We leave the data we can not smooth as is, alternatively we could provide raw value's to the smoothed value's:
// data[data.lastIndex].smoothed = data[data.lastIndex].value
// data[0].smoothed = data[0].value
return data

View file

@ -2,6 +2,7 @@ package info.nightscout.smoothing
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.annotations.OpenForTesting
import info.nightscout.database.entities.GlucoseValue
import info.nightscout.interfaces.iob.InMemoryGlucoseValue
import info.nightscout.interfaces.plugin.PluginBase
import info.nightscout.interfaces.plugin.PluginDescription
@ -118,7 +119,8 @@ class ExponentialSmoothingPlugin @Inject constructor(
//ssD.clear() // MP reset doubly smoothed delta array
if (!insufficientSmoothingData) { //MP Build doubly smoothed array only if there is enough valid readings
for (i in o2_sBG.indices) { //MP calculated doubly smoothed bg of all o1/o2 smoothed data available; o2 & o1 smoothbg array sizes are equal in size, so only one is used as a condition here
for (i in o2_sBG.indices) { //MP calculated doubly smoothed bg of all o1/o2 smoothed data available; o2 & o1 smooth bg array sizes are equal in size, so only one is used as a condition
// here
ssBG.add(o1_weight * o1_sBG[i] + (1 - o1_weight) * o2_sBG[i]) //MP build array of doubly smoothed bgs
}
/*
@ -128,11 +130,13 @@ class ExponentialSmoothingPlugin @Inject constructor(
*/
for (i in 0 until minOf(ssBG.size, data.size)) { // noise at the beginning of the smoothing window is the greatest, so only include the 10 most recent values in the output
data[i].smoothed = max(round(ssBG[i]), 39.0) //Make 39 the smallest value as smaller values trigger errors (xDrip error state = 38)
data[i].trendArrow = GlucoseValue.TrendArrow.NONE
}
} else {
for (i in 0 until data.size) { // noise at the beginning of the smoothing window is the greatest, so only include the 10 most recent values in the output
data[i].smoothed = max(data[i].value, 39.0) // if insufficient smoothing data, copy 'value' into 'smoothed' data column so that it isn't empty; Make 39 the smallest value as smaller
// values trigger errors (xDrip error state = 38)
data[i].trendArrow = GlucoseValue.TrendArrow.NONE
}
}

View file

@ -111,7 +111,7 @@ class RandomBgPlugin @Inject constructor(
value = bgMgdl,
raw = 0.0,
noise = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
trendArrow = GlucoseValue.TrendArrow.values().toList().shuffled().first(),
sourceSensor = GlucoseValue.SourceSensor.RANDOM
)
disposable += repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, emptyList(), null))