Merge pull request #2284 from jbr7rr/jbr7rr-avg-smoothing
Average smoothing
This commit is contained in:
commit
ecc5beca0b
|
@ -52,6 +52,7 @@ import info.nightscout.sensitivity.SensitivityAAPSPlugin
|
||||||
import info.nightscout.sensitivity.SensitivityOref1Plugin
|
import info.nightscout.sensitivity.SensitivityOref1Plugin
|
||||||
import info.nightscout.sensitivity.SensitivityWeightedAveragePlugin
|
import info.nightscout.sensitivity.SensitivityWeightedAveragePlugin
|
||||||
import info.nightscout.smoothing.ExponentialSmoothingPlugin
|
import info.nightscout.smoothing.ExponentialSmoothingPlugin
|
||||||
|
import info.nightscout.smoothing.AvgSmoothingPlugin
|
||||||
import info.nightscout.smoothing.NoSmoothingPlugin
|
import info.nightscout.smoothing.NoSmoothingPlugin
|
||||||
import info.nightscout.source.AidexPlugin
|
import info.nightscout.source.AidexPlugin
|
||||||
import info.nightscout.source.DexcomPlugin
|
import info.nightscout.source.DexcomPlugin
|
||||||
|
@ -448,6 +449,12 @@ abstract class PluginsListModule {
|
||||||
@IntKey(605)
|
@IntKey(605)
|
||||||
abstract fun bindExponentialSmoothingPlugin(plugin: ExponentialSmoothingPlugin): PluginBase
|
abstract fun bindExponentialSmoothingPlugin(plugin: ExponentialSmoothingPlugin): PluginBase
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
@AllConfigs
|
||||||
|
@IntoMap
|
||||||
|
@IntKey(610)
|
||||||
|
abstract fun bindAvgSmoothingPlugin(plugin: AvgSmoothingPlugin): PluginBase
|
||||||
|
|
||||||
@Qualifier
|
@Qualifier
|
||||||
annotation class AllConfigs
|
annotation class AllConfigs
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
package info.nightscout.smoothing
|
||||||
|
|
||||||
|
import dagger.android.HasAndroidInjector
|
||||||
|
import info.nightscout.androidaps.annotations.OpenForTesting
|
||||||
|
import info.nightscout.interfaces.iob.InMemoryGlucoseValue
|
||||||
|
import info.nightscout.interfaces.plugin.PluginBase
|
||||||
|
import info.nightscout.interfaces.plugin.PluginDescription
|
||||||
|
import info.nightscout.interfaces.plugin.PluginType
|
||||||
|
import info.nightscout.interfaces.smoothing.Smoothing
|
||||||
|
import info.nightscout.rx.logging.AAPSLogger
|
||||||
|
import info.nightscout.rx.logging.LTag
|
||||||
|
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
|
||||||
|
|
||||||
|
@OpenForTesting
|
||||||
|
@Singleton
|
||||||
|
class AvgSmoothingPlugin @Inject constructor(
|
||||||
|
injector: HasAndroidInjector,
|
||||||
|
aapsLogger: AAPSLogger,
|
||||||
|
rh: ResourceHelper
|
||||||
|
) : PluginBase(
|
||||||
|
PluginDescription()
|
||||||
|
.mainType(PluginType.SMOOTHING)
|
||||||
|
.pluginIcon(info.nightscout.core.ui.R.drawable.ic_timeline_24)
|
||||||
|
.pluginName(R.string.avg_smoothing_name)
|
||||||
|
.shortName(R.string.smoothing_shortname)
|
||||||
|
.description(R.string.description_avg_smoothing),
|
||||||
|
aapsLogger, rh, injector
|
||||||
|
), Smoothing {
|
||||||
|
|
||||||
|
@Suppress("LocalVariableName")
|
||||||
|
override fun smooth(data: MutableList<InMemoryGlucoseValue>): MutableList<InMemoryGlucoseValue> {
|
||||||
|
if (data.lastIndex < 4)
|
||||||
|
{
|
||||||
|
aapsLogger.debug(LTag.GLUCOSE, "Not enough value's to smooth!")
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i in data.lastIndex -1 downTo 1) {
|
||||||
|
// 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())
|
||||||
|
{
|
||||||
|
// 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].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:
|
||||||
|
// data[data.lastIndex].smoothed = data[data.lastIndex].value
|
||||||
|
// data[0].smoothed = data[0].value
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
private fun isValid(n: Double): Boolean {
|
||||||
|
// For Dexcom: Below 39 is LOW, above 401 Dexcom just says HI
|
||||||
|
return n > 39 && n < 401
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,8 @@
|
||||||
<string name="smoothing_shortname">SMOOTH</string>
|
<string name="smoothing_shortname">SMOOTH</string>
|
||||||
<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="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>
|
Loading…
Reference in a new issue