Skip to content

Commit

Permalink
bg data smoothing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Zorchenko committed Nov 9, 2022
1 parent 34320ad commit 428196e
Show file tree
Hide file tree
Showing 49 changed files with 4,137 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class WizardDialog : DaggerDialogFragment() {
binding.bgInput.step = if (units == GlucoseUnit.MGDL) 1.0 else 0.1

// Set BG if not old
binding.bgInput.value = iobCobCalculator.ads.actualBg()?.valueToUnits(units) ?: 0.0
binding.bgInput.value = iobCobCalculator.ads.actualBg()?.valueToUnits(units, sp) ?: 0.0

binding.ttCheckbox.isEnabled = tempTarget is ValueWrapper.Existing
binding.ttCheckboxIcon.visibility = binding.ttCheckbox.isEnabled.toVisibility()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
val lastBgDescription = overviewData.lastBgDescription
runOnUiThread {
_binding ?: return@runOnUiThread
binding.infoLayout.bg.text = lastBg?.valueToUnitsString(units)
binding.infoLayout.bg.text = lastBg?.valueToUnitsString(units, sp)
?: rh.gs(R.string.value_unavailable_short)
binding.infoLayout.bg.setTextColor(lastBgColor)
binding.infoLayout.arrow.setImageResource(trendArrow.directionToIcon())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import info.nightscout.rx.events.EventAutosensCalculationFinished
import info.nightscout.rx.events.EventInitializationChanged
import info.nightscout.rx.events.EventRefreshOverview
import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.shared.sharedPreferences.SP
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import javax.inject.Inject
Expand All @@ -42,6 +43,7 @@ class PersistentNotificationPlugin @Inject constructor(
injector: HasAndroidInjector,
aapsLogger: AAPSLogger,
rh: ResourceHelper,
private val sp: SP,
private val aapsSchedulers: AapsSchedulers,
private val profileFunction: ProfileFunction,
private val fabricPrivacy: FabricPrivacy,
Expand Down Expand Up @@ -120,7 +122,7 @@ class PersistentNotificationPlugin @Inject constructor(
val lastBG = iobCobCalculator.ads.lastBg()
val glucoseStatus = glucoseStatusProvider.glucoseStatusData
if (lastBG != null) {
line1aa = lastBG.valueToUnitsString(units)
line1aa = lastBG.valueToUnitsString(units, sp)
line1 = line1aa
if (glucoseStatus != null) {
line1 += (" Δ" + Profile.toSignedUnitsString(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, units)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ class DataHandlerMobile @Inject constructor(
tempTarget = tempTarget,
carbs = carbsAfterConstraints,
cob = cobInfo.displayCob!!,
bg = bgReading.valueToUnits(profileFunction.getUnits()),
bg = bgReading.valueToUnits(profileFunction.getUnits(), sp),
correction = 0.0,
percentageCorrection = percentage,
useBg = sp.getBoolean(R.string.key_wearwizard_bg, true),
Expand Down Expand Up @@ -855,7 +855,7 @@ class DataHandlerMobile @Inject constructor(
val finalLastRun = loop.lastRun
if (sp.getBoolean("wear_predictions", true) && finalLastRun?.request?.hasPredictions == true && finalLastRun.constraintsProcessed != null) {
val predArray = finalLastRun.constraintsProcessed!!.predictions
.stream().map { bg: GlucoseValue -> GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh) }
.stream().map { bg: GlucoseValue -> GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh, sp) }
.collect(Collectors.toList())
if (predArray.isNotEmpty())
for (bg in predArray) if (bg.data.value > 39)
Expand Down Expand Up @@ -942,7 +942,7 @@ class DataHandlerMobile @Inject constructor(

return EventData.SingleBg(
timeStamp = glucoseValue.timestamp,
sgvString = glucoseValue.valueToUnitsString(units),
sgvString = glucoseValue.valueToUnitsString(units, sp),
glucoseUnits = units.asText,
slopeArrow = trendCalculator.getTrendArrow(glucoseValue).symbol,
delta = glucoseStatus?.let { deltaString(it.delta, it.delta * Constants.MGDL_TO_MMOLL, units) } ?: "--",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class IobCobCalculatorPlugin @Inject constructor(
private var iobTable = LongSparseArray<IobTotal>() // oldest at index 0
private var basalDataTable = LongSparseArray<BasalData>() // oldest at index 0

override var ads: AutosensDataStore = AutosensDataStore()
override var ads: AutosensDataStore = AutosensDataStore(sp)

private val dataLock = Any()
private var thread: Thread? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ class IobCobOref1Worker(
//console.error(bgTime , bucketed_data[i].glucose);
var avgDelta: Double
var delta: Double
val bg: Double = bucketedData[i].value
if (bg < 39 || bucketedData[i + 3].value < 39) {
val bg: Double = bucketedData[i].rawOrSmoothed(sp)
if (bg < 39 || bucketedData[i + 3].rawOrSmoothed(sp) < 39) {
aapsLogger.error("! value < 39")
continue
}
autosensData.bg = bg
delta = bg - bucketedData[i + 1].value
avgDelta = (bg - bucketedData[i + 3].value) / 3
delta = bg - bucketedData[i + 1].rawOrSmoothed(sp)
avgDelta = (bg - bucketedData[i + 3].rawOrSmoothed(sp)) / 3
val iob = data.iobCobCalculator.calculateFromTreatmentsAndTemps(bgTime, profile)
val bgi = -iob.activity * sens * 5
val deviation = delta - bgi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ class IobCobOrefWorker @Inject internal constructor(
//console.error(bgTime , bucketed_data[i].glucose);
var avgDelta: Double
var delta: Double
val bg: Double = bucketedData[i].value
if (bg < 39 || bucketedData[i + 3].value < 39) {
val bg: Double = bucketedData[i].rawOrSmoothed(sp)
if (bg < 39 || bucketedData[i + 3].rawOrSmoothed(sp) < 39) {
aapsLogger.error("! value < 39")
continue
}
autosensData.bg = bg
delta = bg - bucketedData[i + 1].value
avgDelta = (bg - bucketedData[i + 3].value) / 3
delta = bg - bucketedData[i + 1].rawOrSmoothed(sp)
avgDelta = (bg - bucketedData[i + 3].rawOrSmoothed(sp)) / 3
val iob = data.iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime, profile)
val bgi = -iob.activity * sens * 5
val deviation = delta - bgi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class AidexPlugin @Inject constructor(
glucoseValues += CgmSourceTransaction.TransactionGlucoseValue(
timestamp = timestamp,
value = bgValueTarget,
smoothed = null,
raw = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(bundle.getString(Intents.AIDEX_BG_SLOPE_NAME)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ class DexcomPlugin @Inject constructor(
glucoseValues += CgmSourceTransaction.TransactionGlucoseValue(
timestamp = timestamp,
value = glucoseValueBundle.getInt("glucoseValue").toDouble(),
noise = null,
raw = null,
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(glucoseValueBundle.getString("trendArrow")!!),
sourceSensor = sourceSensor
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class EversensePlugin @Inject constructor(
timestamp = glucoseTimestamps[i],
value = glucoseLevels[i].toDouble(),
raw = glucoseLevels[i].toDouble(),
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.EVERSENSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class GlimpPlugin @Inject constructor(
timestamp = inputData.getLong("myTimestamp", 0),
value = inputData.getDouble("mySGV", 0.0),
raw = inputData.getDouble("mySGV", 0.0),
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(inputData.getString("myTrend")),
sourceSensor = GlucoseValue.SourceSensor.GLIMP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class GlunovoPlugin @Inject constructor(
timestamp = timestamp,
value = value * Constants.MMOLL_TO_MGDL,
raw = 0.0,
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.GLUNOVO_NATIVE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class IntelligoPlugin @Inject constructor(
timestamp = timestamp,
value = value * Constants.MMOLL_TO_MGDL,
raw = 0.0,
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.INTELLIGO_NATIVE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class MM640gPlugin @Inject constructor(
timestamp = jsonObject.getLong("date"),
value = jsonObject.getDouble("sgv"),
raw = jsonObject.getDouble("sgv"),
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(jsonObject.getString("direction")),
sourceSensor = GlucoseValue.SourceSensor.MM_600_SERIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class PoctechPlugin @Inject constructor(
timestamp = json.getLong("date"),
value = if (safeGetString(json, "units", Constants.MGDL) == "mmol/L") json.getDouble("current") * Constants.MMOLL_TO_MGDL
else json.getDouble("current"),
smoothed = null,
raw = json.getDouble("raw"),
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(json.getString("direction")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class RandomBgPlugin @Inject constructor(
value = bgMgdl,
raw = 0.0,
noise = null,
smoothed = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.RANDOM
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class TomatoPlugin @Inject constructor(
value = inputData.getDouble("com.fanqies.tomatofn.Extras.BgEstimate", 0.0),
raw = 0.0,
noise = null,
smoothed = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.LIBRE_1_TOMATO
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class XdripPlugin @Inject constructor(
timestamp = bundle.getLong(Intents.EXTRA_TIMESTAMP, 0),
value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE, 0.0),
raw = bundle.getDouble(Intents.EXTRA_RAW, 0.0),
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(bundle.getString(Intents.EXTRA_BG_SLOPE_NAME)),
sourceSensor = GlucoseValue.SourceSensor.fromString(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class QuickWizardEntry @Inject constructor(private val injector: HasAndroidInjec
//BG
var bg = 0.0
if (useBG() == YES) {
bg = lastBG.valueToUnits(profileFunction.getUnits())
bg = lastBG.valueToUnits(profileFunction.getUnits(), sp)
}
// COB
val cob =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import androidx.work.Worker
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.extensions.rawOrSmoothed
import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.IobCobCalculator
import info.nightscout.androidaps.interfaces.Profile
Expand All @@ -18,6 +20,7 @@ import info.nightscout.androidaps.receivers.DataWorkerStorage
import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.interfaces.utils.Round
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import java.util.ArrayList
import javax.inject.Inject

Expand All @@ -29,6 +32,7 @@ class PrepareBgDataWorker(
@Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var rh: ResourceHelper
@Inject lateinit var sp: SP
@Inject lateinit var defaultValueHelper: DefaultValueHelper
@Inject lateinit var repository: AppRepository

Expand All @@ -45,14 +49,15 @@ class PrepareBgDataWorker(

val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareBgData?
?: return Result.failure(workDataOf("Error" to "missing input data"))
val useSmoothed = sp.getBoolean(R.string.key_use_data_smoothing, false)

data.overviewData.maxBgValue = Double.MIN_VALUE
data.overviewData.bgReadingsArray = repository.compatGetBgReadingsDataFromTime(data.overviewData.fromTime, data.overviewData.toTime, false).blockingGet()
val bgListArray: MutableList<DataPointWithLabelInterface> = ArrayList()
for (bg in data.overviewData.bgReadingsArray) {
if (bg.timestamp < data.overviewData.fromTime || bg.timestamp > data.overviewData.toTime) continue
if (bg.value > data.overviewData.maxBgValue) data.overviewData.maxBgValue = bg.value
bgListArray.add(GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh))
if (bg.rawOrSmoothed(useSmoothed) > data.overviewData.maxBgValue) data.overviewData.maxBgValue = bg.rawOrSmoothed(useSmoothed)
bgListArray.add(GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh, sp))
}
bgListArray.sortWith { o1: DataPointWithLabelInterface, o2: DataPointWithLabelInterface -> o1.x.compareTo(o2.x) }
data.overviewData.bgReadingGraphSeries = PointsWithLabelGraphSeries(Array(bgListArray.size) { i -> bgListArray[i] })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import info.nightscout.androidaps.plugins.general.overview.graphExtensions.InMem
import info.nightscout.androidaps.plugins.general.overview.graphExtensions.PointsWithLabelGraphSeries
import info.nightscout.androidaps.receivers.DataWorkerStorage
import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.shared.sharedPreferences.SP
import javax.inject.Inject

class PrepareBucketedDataWorker(
Expand All @@ -25,6 +26,7 @@ class PrepareBucketedDataWorker(
@Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var rh: ResourceHelper
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var sp: SP

init {
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
Expand All @@ -48,7 +50,7 @@ class PrepareBucketedDataWorker(
val bucketedListArray: MutableList<DataPointWithLabelInterface> = ArrayList()
for (inMemoryGlucoseValue in bucketedData) {
if (inMemoryGlucoseValue.timestamp < data.overviewData.fromTime || inMemoryGlucoseValue.timestamp > data.overviewData.toTime) continue
bucketedListArray.add(InMemoryGlucoseValueDataPoint(inMemoryGlucoseValue, profileFunction, rh))
bucketedListArray.add(InMemoryGlucoseValueDataPoint(inMemoryGlucoseValue, profileFunction, rh, sp))
}
bucketedListArray.sortWith { o1: DataPointWithLabelInterface, o2: DataPointWithLabelInterface -> o1.x.compareTo(o2.x) }
data.overviewData.bucketedGraphSeries = PointsWithLabelGraphSeries(Array(bucketedListArray.size) { i -> bucketedListArray[i] })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import info.nightscout.plugins.sync.nsclient.data.ProcessedDeviceStatusData
import info.nightscout.rx.bus.RxBus
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.T
import info.nightscout.shared.sharedPreferences.SP
import java.util.Calendar
import javax.inject.Inject
import kotlin.math.ceil
Expand All @@ -43,6 +44,7 @@ class PreparePredictionsWorker(
@Inject lateinit var defaultValueHelper: DefaultValueHelper
@Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var rh: ResourceHelper
@Inject lateinit var sp: SP

init {
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
Expand Down Expand Up @@ -83,7 +85,7 @@ class PreparePredictionsWorker(

val bgListArray: MutableList<DataPointWithLabelInterface> = ArrayList()
val predictions: MutableList<GlucoseValueDataPoint>? = apsResult?.predictions
?.map { bg -> GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh) }
?.map { bg -> GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh, sp) }
?.toMutableList()
if (predictions != null) {
predictions.sortWith { o1: GlucoseValueDataPoint, o2: GlucoseValueDataPoint -> o1.x.compareTo(o2.x) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.database.entities.Bolus
import info.nightscout.androidaps.database.entities.TherapyEvent
import info.nightscout.androidaps.extensions.rawOrSmoothed
import info.nightscout.androidaps.interfaces.ActivePlugin
import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.Profile
Expand All @@ -28,6 +29,7 @@ import info.nightscout.interfaces.utils.Round
import info.nightscout.shared.utils.T
import info.nightscout.androidaps.utils.Translator
import info.nightscout.rx.bus.RxBus
import info.nightscout.shared.sharedPreferences.SP
import javax.inject.Inject

class PrepareTreatmentsDataWorker(
Expand All @@ -43,6 +45,7 @@ class PrepareTreatmentsDataWorker(
@Inject lateinit var activePlugin: ActivePlugin
@Inject lateinit var repository: AppRepository
@Inject lateinit var defaultValueHelper: DefaultValueHelper
@Inject lateinit var sp: SP

init {
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
Expand Down Expand Up @@ -143,9 +146,10 @@ class PrepareTreatmentsDataWorker(
overviewData.bgReadingsArray.let { bgReadingsArray ->
for (reading in bgReadingsArray) {
if (reading.timestamp > date) continue
return Profile.fromMgdlToUnits(reading.value, profileFunction.getUnits())
return Profile.fromMgdlToUnits(reading.rawOrSmoothed(sp), profileFunction
.getUnits())
}
return if (bgReadingsArray.isNotEmpty()) Profile.fromMgdlToUnits(bgReadingsArray[0].value, profileFunction.getUnits())
return if (bgReadingsArray.isNotEmpty()) Profile.fromMgdlToUnits(bgReadingsArray[0].rawOrSmoothed(sp), profileFunction.getUnits())
else Profile.fromMgdlToUnits(100.0, profileFunction.getUnits())
}
}
Expand Down
Loading

0 comments on commit 428196e

Please sign in to comment.