Added checks, comments and cleanup

This commit is contained in:
jbr7rr 2022-12-13 09:01:01 +01:00
parent c285a61e18
commit cd02fa6c0f
2 changed files with 18 additions and 14 deletions

View file

@ -8,7 +8,9 @@ import info.nightscout.interfaces.plugin.PluginDescription
import info.nightscout.interfaces.plugin.PluginType import info.nightscout.interfaces.plugin.PluginType
import info.nightscout.interfaces.smoothing.Smoothing import info.nightscout.interfaces.smoothing.Smoothing
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.T
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.math.max import kotlin.math.max
@ -33,28 +35,30 @@ class AvgSmoothingPlugin @Inject constructor(
@Suppress("LocalVariableName") @Suppress("LocalVariableName")
override fun smooth(data: MutableList<InMemoryGlucoseValue>): MutableList<InMemoryGlucoseValue> { override fun smooth(data: MutableList<InMemoryGlucoseValue>): MutableList<InMemoryGlucoseValue> {
for (i in data.lastIndex -1 downTo 1) { for (i in data.lastIndex -1 downTo 1) {
// TODO: Bucketed is always calculated to 5 min (CHECK!), Maybe add a separate timecheck? // Check if value's are in a valid range
// TODO: We could further improve this by adding a weight to the neighbours // 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)) 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())
{ {
var result = ((data[i - 1].value + data[i].value + data[i + 1].value) / 3.0) // We could further improve this by adding a weight to the neighbours, for simplicity this is not done.
data[i].smoothed = result data[i].smoothed = ((data[i - 1].value + data[i].value + data[i + 1].value) / 3.0)
aapsLogger.debug("RESULT $result")
} }
else else
{ {
// TODO: Decide what to do here // data[i].smoothed = data[i].value
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:
// append data we cannot smooth // data[data.lastIndex].smoothed = data[data.lastIndex].value
data[data.lastIndex].smoothed = data[data.lastIndex].value // data[0].smoothed = data[0].value
data[0].smoothed = data[0].value
return data return data
} }
private fun isValid(n: Double): Boolean { private fun isValid(n: Double): Boolean {
// For Dexcom: Below 39 is LOW, above 401 Dexcom just says HI
return n > 39 && n < 401 return n > 39 && n < 401
} }
} }

View file

@ -5,7 +5,7 @@
<string name="exponential_smoothing_name">Exponential smoothing</string> <string name="exponential_smoothing_name">Exponential smoothing</string>
<string name="description_exponential_smoothing">"Second-order exponential smoothing algorithm"</string> <string name="description_exponential_smoothing">"Second-order exponential smoothing algorithm"</string>
<string name="avg_smoothing_name">Average smoothing</string> <string name="avg_smoothing_name">Average smoothing</string>
<string name="description_avg_smoothing">"(Weighted) Average smoothing, newest data is not affected"</string> <string name="description_avg_smoothing">"Average smoothing algorithm, newest value is not affected"</string>
<string name="no_smoothing_name">No smoothing</string> <string name="no_smoothing_name">No smoothing</string>
<string name="description_no_smoothing">"No smoothing performed on input glucose data"</string> <string name="description_no_smoothing">"No smoothing performed on input glucose data"</string>
</resources> </resources>