Calculate TIR

This commit is contained in:
Milos Kozak 2019-12-08 21:55:49 +01:00
parent 0d6743ab82
commit 00cd2ba343
4 changed files with 57 additions and 11 deletions

View file

@ -4,6 +4,7 @@ import android.os.Bundle
import android.widget.ArrayAdapter
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.FirebaseDatabase
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.defaultProfile.DefaultProfile
@ -19,6 +20,9 @@ import java.util.*
class SurveyActivity : NoSplashAppCompatActivity() {
private val log = LoggerFactory.getLogger(SurveyActivity::class.java)
val lowMgdl = 3.9 * Constants.MMOLL_TO_MGDL
val highMgdl = 10.0 * Constants.MMOLL_TO_MGDL
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.survey_fragment)
@ -26,15 +30,17 @@ class SurveyActivity : NoSplashAppCompatActivity() {
survey_id.text = InstanceId.instanceId()
val profileStore = ConfigBuilderPlugin.getPlugin().activeProfileInterface?.profile
val profileList: ArrayList<CharSequence>
profileList = profileStore?.getProfileList() ?: return
val adapter = ArrayAdapter(this, R.layout.spinner_centered, profileList)
survey_spinner.adapter = adapter
val profileList = profileStore?.getProfileList() ?: return
survey_spinner.adapter = ArrayAdapter(this, R.layout.spinner_centered, profileList)
val tdds = TddCalculator.calculate(7)
val averageTdd = TddCalculator.averageTDD(tdds)
survey_tdds.text = MainApp.gs(R.string.tdd) + ":\n" + TddCalculator.toText(tdds) + MainApp.gs(R.string.average) + ":\n" + averageTdd.toText()
val tirs = TirCalculator.calculate(7, lowMgdl, highMgdl)
val averageTir = TirCalculator.averageTIR(tirs)
survey_tir.text = "\n" + MainApp.gs(R.string.tir) + ":\n" + TirCalculator.toText(tirs) + MainApp.gs(R.string.average) + ":\n" + averageTir.toText()
survey_profile.setOnClickListener {
val age = SafeParse.stringToDouble(survey_age.text.toString())
val weight = SafeParse.stringToDouble(survey_weight.text.toString())

View file

@ -1,14 +1,24 @@
package info.nightscout.androidaps.utils
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import kotlin.math.roundToInt
class TIR(val date: Long, val lowThreshold: Double, val highThreshold: Double) {
private var below = 0;
private var inRange = 0;
private var above = 0;
private var error = 0;
private var count = 0;
internal var below = 0
internal var inRange = 0
internal var above = 0
internal var error = 0
internal var count = 0
fun error() = run { error++ }
fun below() = run { below++; count++ }
fun inRange() = run { inRange++; count++ }
fun above() = run { above++; count++ }
fun belowPct() = if (count > 0) (below.toDouble() / count * 100.0).roundToInt() else 0
fun inRangePct() = if (count > 0) (inRange.toDouble() / count * 100.0).roundToInt() else 0
fun abovePct() = if (count > 0) (above.toDouble() / count * 100.0).roundToInt() else 0
fun toText(): String = MainApp.gs(R.string.tirformat, DateUtil.dateString(date), belowPct(), inRangePct(), abovePct())
}

View file

@ -14,12 +14,40 @@ object TirCalculator {
val result = LongSparseArray<TIR>()
for (bg in bgReadings) {
val midnight = MidnightTime.calc(bg.date)
val tir = result[midnight] ?: TIR(midnight, lowMgdl, highMgdl)
var tir = result[midnight]
if (tir == null) {
tir = TIR(midnight, lowMgdl, highMgdl)
result.append(midnight, tir)
}
if (bg.value < 39) tir.error()
if (bg.value >= 39 && bg.value < lowMgdl) tir.below()
if (bg.value in lowMgdl..highMgdl) tir.inRange()
if (bg.value > highMgdl) tir.inRange()
if (bg.value > highMgdl) tir.above()
}
return result
}
fun averageTIR(tirs: LongSparseArray<TIR>): TIR {
val totalTir =
if (tirs.size() > 0) TIR(tirs.valueAt(0).date, tirs.valueAt(0).lowThreshold, tirs.valueAt(0).highThreshold)
else TIR(7, 70.0, 180.0)
for (i in 0 until tirs.size()) {
val tir = tirs.valueAt(i)
totalTir.below += tir.below
totalTir.inRange += tir.inRange
totalTir.above += tir.above
totalTir.error += tir.error
totalTir.count += tir.count
}
return totalTir
}
fun toText(tirs: LongSparseArray<TIR>): String {
var t = ""
for (i in 0 until tirs.size()) {
t += "${tirs.valueAt(i).toText()}\n"
}
return t
}
}

View file

@ -1672,7 +1672,9 @@
<string name="invalidage">Invalid age entry</string>
<string name="invalidweight">Invalid weight entry</string>
<string name="tddformat">Total: %1$.2f Bolus: %2$.2f Basal: %3$.2f</string>
<string name="tirformat">%1$s: Low: %2$d%% In: %3$d%% High: %4$d%%</string>
<string name="average">Average</string>
<string name="tdd">TDD</string>
<string name="tir">TIR</string>
</resources>